From e4f3d2bc0e1a71ec9a38f047919f2e38f6ab145a Mon Sep 17 00:00:00 2001 From: barneygale Date: Mon, 8 May 2023 15:42:08 +0100 Subject: [PATCH] GH-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. --- Lib/pathlib.py | 4 ++-- Lib/test/test_pathlib.py | 8 ++++++++ .../Library/2023-05-08-15-39-00.gh-issue-87695.f6iO7v.rst | 2 ++ 3 files changed, 12 insertions(+), 2 deletions(-) create mode 100644 Misc/NEWS.d/next/Library/2023-05-08-15-39-00.gh-issue-87695.f6iO7v.rst diff --git a/Lib/pathlib.py b/Lib/pathlib.py index 20ec1ce9d80374d..25863c76f3086d6 100644 --- a/Lib/pathlib.py +++ b/Lib/pathlib.py @@ -178,11 +178,11 @@ def _iterate_directories(self, parent_path, scandir): for entry in entries: entry_is_dir = False try: - entry_is_dir = entry.is_dir() + entry_is_dir = entry.is_dir(follow_symlinks=False) except OSError as e: if not _ignore_error(e): raise - if entry_is_dir and not entry.is_symlink(): + if entry_is_dir: path = parent_path._make_child_relpath(entry.name) for p in self._iterate_directories(path, scandir): yield p diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py index ee0ef9a34c385c0..74d36d14146b0f4 100644 --- a/Lib/test/test_pathlib.py +++ b/Lib/test/test_pathlib.py @@ -1977,6 +1977,14 @@ def my_scandir(path): subdir.chmod(000) self.assertEqual(len(set(base.glob("*"))), 4) + 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): q = p.resolve(strict) self.assertEqual(q, expected) diff --git a/Misc/NEWS.d/next/Library/2023-05-08-15-39-00.gh-issue-87695.f6iO7v.rst b/Misc/NEWS.d/next/Library/2023-05-08-15-39-00.gh-issue-87695.f6iO7v.rst new file mode 100644 index 000000000000000..7b677656bea3084 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2023-05-08-15-39-00.gh-issue-87695.f6iO7v.rst @@ -0,0 +1,2 @@ +Fix issue where :meth:`pathlib.Path.glob` raised :exc:`OSError` when it +encountered a symlink to an overly long path.