[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 a33ce66dca)

Co-authored-by: Barney Gale <barney.gale@gmail.com>
This commit is contained in:
Miss Islington (bot) 2023-05-10 16:19:49 -07:00 committed by GitHub
parent 1cbf844875
commit 846a23d0b8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 13 additions and 2 deletions

View file

@ -388,11 +388,11 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
for entry in entries: for entry in entries:
entry_is_dir = False entry_is_dir = False
try: try:
entry_is_dir = entry.is_dir() entry_is_dir = entry.is_dir(follow_symlinks=False)
except OSError as e: except OSError as e:
if not _ignore_error(e): if not _ignore_error(e):
raise raise
if entry_is_dir and not entry.is_symlink(): if entry_is_dir:
path = parent_path._make_child_relpath(entry.name) path = parent_path._make_child_relpath(entry.name)
for p in self._iterate_directories(path, is_dir, scandir): for p in self._iterate_directories(path, is_dir, scandir):
yield p yield p

View file

@ -1790,6 +1790,15 @@ def my_scandir(path):
subdir.chmod(000) subdir.chmod(000)
self.assertEqual(len(set(base.glob("*"))), 4) self.assertEqual(len(set(base.glob("*"))), 4)
@os_helper.skip_unless_symlink
def test_glob_long_symlink(self):
# See gh-87695
base = self.cls(BASE) / 'long_symlink'
base.mkdir()
bad_link = base / 'bad_link'
bad_link.symlink_to("bad" * 200)
self.assertEqual(sorted(base.glob('**/*')), [bad_link])
def _check_resolve(self, p, expected, strict=True): def _check_resolve(self, p, expected, strict=True):
q = p.resolve(strict) q = p.resolve(strict)
self.assertEqual(q, expected) self.assertEqual(q, expected)

View file

@ -0,0 +1,2 @@
Fix issue where :meth:`pathlib.Path.glob` raised :exc:`OSError` when it
encountered a symlink to an overly long path.