Skip to content

Commit e4f3d2b

Browse files
committed
pythonGH-87695: Fix OSError from pathlib.Path.glob()
Fix issue where `pathlib.Path.glob()` raised `OSError` when it encountered a symlink to an overly long path.
1 parent c0ece3d commit e4f3d2b

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

Lib/pathlib.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -178,11 +178,11 @@ def _iterate_directories(self, parent_path, scandir):
178178
for entry in entries:
179179
entry_is_dir = False
180180
try:
181-
entry_is_dir = entry.is_dir()
181+
entry_is_dir = entry.is_dir(follow_symlinks=False)
182182
except OSError as e:
183183
if not _ignore_error(e):
184184
raise
185-
if entry_is_dir and not entry.is_symlink():
185+
if entry_is_dir:
186186
path = parent_path._make_child_relpath(entry.name)
187187
for p in self._iterate_directories(path, scandir):
188188
yield p

Lib/test/test_pathlib.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1977,6 +1977,14 @@ def my_scandir(path):
19771977
subdir.chmod(000)
19781978
self.assertEqual(len(set(base.glob("*"))), 4)
19791979

1980+
def test_glob_long_symlink(self):
1981+
# See gh-87695
1982+
base = self.cls(BASE) / 'long_symlink'
1983+
base.mkdir()
1984+
bad_link = base / 'bad_link'
1985+
bad_link.symlink_to("bad" * 200)
1986+
self.assertEqual(sorted(base.glob('**/*')), [bad_link])
1987+
19801988
def _check_resolve(self, p, expected, strict=True):
19811989
q = p.resolve(strict)
19821990
self.assertEqual(q, expected)
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix issue where :meth:`pathlib.Path.glob` raised :exc:`OSError` when it
2+
encountered a symlink to an overly long path.

0 commit comments

Comments
 (0)