Skip to content

GH-125413: pathlib ABCs: use caching path.info.exists() when globbing #130422

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Feb 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Lib/glob.py
Original file line number Diff line number Diff line change
Expand Up @@ -533,7 +533,9 @@ class _PathGlobber(_GlobberBase):
"""Provides shell-style pattern matching and globbing for pathlib paths.
"""

lexists = operator.methodcaller('exists', follow_symlinks=False)
@staticmethod
def lexists(path):
return path.info.exists(follow_symlinks=False)

@staticmethod
def scandir(path):
Expand Down
13 changes: 5 additions & 8 deletions Lib/pathlib/_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -316,14 +316,11 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
paths.append((path, dirnames, filenames))
try:
for child in path.iterdir():
try:
if child.info.is_dir(follow_symlinks=follow_symlinks):
if not top_down:
paths.append(child)
dirnames.append(child.name)
else:
filenames.append(child.name)
except OSError:
if child.info.is_dir(follow_symlinks=follow_symlinks):
if not top_down:
paths.append(child)
dirnames.append(child.name)
else:
filenames.append(child.name)
except OSError as error:
if on_error is not None:
Expand Down
2 changes: 1 addition & 1 deletion Lib/test/test_pathlib/test_pathlib_abc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1107,7 +1107,7 @@ def test_glob_posix(self):
p = P(self.base)
q = p / "FILEa"
given = set(p.glob("FILEa"))
expect = {q} if q.exists() else set()
expect = {q} if q.info.exists() else set()
self.assertEqual(given, expect)
self.assertEqual(set(p.glob("FILEa*")), set())

Expand Down
Loading