Skip to content

Commit 846a23d

Browse files
[3.11] GH-87695: Fix OSError from pathlib.Path.glob() (GH-104292) (GH-104362)
Fix issue where `pathlib.Path.glob()` raised `OSError` when it encountered a symlink to an overly long path. (cherry picked from commit a33ce66) Co-authored-by: Barney Gale <[email protected]>
1 parent 1cbf844 commit 846a23d

File tree

3 files changed

+13
-2
lines changed

3 files changed

+13
-2
lines changed

Lib/pathlib.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -388,11 +388,11 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
388388
for entry in entries:
389389
entry_is_dir = False
390390
try:
391-
entry_is_dir = entry.is_dir()
391+
entry_is_dir = entry.is_dir(follow_symlinks=False)
392392
except OSError as e:
393393
if not _ignore_error(e):
394394
raise
395-
if entry_is_dir and not entry.is_symlink():
395+
if entry_is_dir:
396396
path = parent_path._make_child_relpath(entry.name)
397397
for p in self._iterate_directories(path, is_dir, scandir):
398398
yield p

Lib/test/test_pathlib.py

+9
Original file line numberDiff line numberDiff line change
@@ -1790,6 +1790,15 @@ def my_scandir(path):
17901790
subdir.chmod(000)
17911791
self.assertEqual(len(set(base.glob("*"))), 4)
17921792

1793+
@os_helper.skip_unless_symlink
1794+
def test_glob_long_symlink(self):
1795+
# See gh-87695
1796+
base = self.cls(BASE) / 'long_symlink'
1797+
base.mkdir()
1798+
bad_link = base / 'bad_link'
1799+
bad_link.symlink_to("bad" * 200)
1800+
self.assertEqual(sorted(base.glob('**/*')), [bad_link])
1801+
17931802
def _check_resolve(self, p, expected, strict=True):
17941803
q = p.resolve(strict)
17951804
self.assertEqual(q, expected)
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)