Skip to content

Commit 48c84a4

Browse files
authored
GH-125413: pathlib ABCs: use caching path.info.exists() when globbing (#130422)
Call `ReadablePath.info.exists()` rather than `ReadablePath.exists()` when globbing so that we use (or populate) the `info` cache.
1 parent d73d69e commit 48c84a4

File tree

3 files changed

+9
-10
lines changed

3 files changed

+9
-10
lines changed

Lib/glob.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -533,7 +533,9 @@ class _PathGlobber(_GlobberBase):
533533
"""Provides shell-style pattern matching and globbing for pathlib paths.
534534
"""
535535

536-
lexists = operator.methodcaller('exists', follow_symlinks=False)
536+
@staticmethod
537+
def lexists(path):
538+
return path.info.exists(follow_symlinks=False)
537539

538540
@staticmethod
539541
def scandir(path):

Lib/pathlib/_abc.py

+5-8
Original file line numberDiff line numberDiff line change
@@ -316,14 +316,11 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
316316
paths.append((path, dirnames, filenames))
317317
try:
318318
for child in path.iterdir():
319-
try:
320-
if child.info.is_dir(follow_symlinks=follow_symlinks):
321-
if not top_down:
322-
paths.append(child)
323-
dirnames.append(child.name)
324-
else:
325-
filenames.append(child.name)
326-
except OSError:
319+
if child.info.is_dir(follow_symlinks=follow_symlinks):
320+
if not top_down:
321+
paths.append(child)
322+
dirnames.append(child.name)
323+
else:
327324
filenames.append(child.name)
328325
except OSError as error:
329326
if on_error is not None:

Lib/test/test_pathlib/test_pathlib_abc.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1107,7 +1107,7 @@ def test_glob_posix(self):
11071107
p = P(self.base)
11081108
q = p / "FILEa"
11091109
given = set(p.glob("FILEa"))
1110-
expect = {q} if q.exists() else set()
1110+
expect = {q} if q.info.exists() else set()
11111111
self.assertEqual(given, expect)
11121112
self.assertEqual(set(p.glob("FILEa*")), set())
11131113

0 commit comments

Comments
 (0)