From 48c84a400aa90014a16ceac92df4b0cf5ca6b3d4 Mon Sep 17 00:00:00 2001 From: Barney Gale Date: Mon, 24 Feb 2025 19:07:54 +0000 Subject: 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. --- Lib/glob.py | 4 +++- Lib/pathlib/_abc.py | 13 +++++-------- Lib/test/test_pathlib/test_pathlib_abc.py | 2 +- 3 files changed, 9 insertions(+), 10 deletions(-) diff --git a/Lib/glob.py b/Lib/glob.py index cd8859e6..d1a6ddd 100644 --- a/Lib/glob.py +++ b/Lib/glob.py @@ -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): diff --git a/Lib/pathlib/_abc.py b/Lib/pathlib/_abc.py index 115e120..4106d47 100644 --- a/Lib/pathlib/_abc.py +++ b/Lib/pathlib/_abc.py @@ -316,14 +316,11 @@ class ReadablePath(JoinablePath): 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: diff --git a/Lib/test/test_pathlib/test_pathlib_abc.py b/Lib/test/test_pathlib/test_pathlib_abc.py index ee4c2b5..68fe352 100644 --- a/Lib/test/test_pathlib/test_pathlib_abc.py +++ b/Lib/test/test_pathlib/test_pathlib_abc.py @@ -1107,7 +1107,7 @@ class ReadablePathTest(JoinablePathTest): 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()) -- cgit v0.12