]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-125413: pathlib ABCs: use caching `path.info.exists()` when globbing (#130422)
authorBarney Gale <barney.gale@gmail.com>
Mon, 24 Feb 2025 19:07:54 +0000 (19:07 +0000)
committerGitHub <noreply@github.com>
Mon, 24 Feb 2025 19:07:54 +0000 (19:07 +0000)
Call `ReadablePath.info.exists()` rather than `ReadablePath.exists()` when
globbing so that we use (or populate) the `info` cache.

Lib/glob.py
Lib/pathlib/_abc.py
Lib/test/test_pathlib/test_pathlib_abc.py

index cd8859e63318f3f9c5a1f442d6f8aa4e68ab85aa..d1a6dddeeb1610018b33cd4b7b03fdc04e19dc37 100644 (file)
@@ -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):
index 115e120572d9f12d7e3ad56141de54ca11dad364..4106d47882208412276d96f50c9471924df82736 100644 (file)
@@ -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:
index ee4c2b59fb9a399b636a0ce90a3db8b0348df3ff..68fe3521410f25efad96db4637e95c0479a1274f 100644 (file)
@@ -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())