]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
GH-125413: pathlib ABCs: use `scandir()` to speed up `walk()` (#126262)
authorBarney Gale <barney.gale@gmail.com>
Fri, 1 Nov 2024 18:52:00 +0000 (18:52 +0000)
committerGitHub <noreply@github.com>
Fri, 1 Nov 2024 18:52:00 +0000 (18:52 +0000)
Use the new `PathBase.scandir()` method in `PathBase.walk()`, which greatly
reduces the number of `PathBase.stat()` calls needed when walking.

There are no user-facing changes, because the pathlib ABCs are still
private and `Path.walk()` doesn't use the implementation in its superclass.

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

index cc7c1991d0e528a7352c0a9a72553bf3f39c2d69..f5eed6f025c2505fbf984f2d2eb6e19f1b77498b 100644 (file)
@@ -693,16 +693,18 @@ class PathBase(PurePathBase):
             if not top_down:
                 paths.append((path, dirnames, filenames))
             try:
-                for child in path.iterdir():
-                    try:
-                        if child.is_dir(follow_symlinks=follow_symlinks):
-                            if not top_down:
-                                paths.append(child)
-                            dirnames.append(child.name)
-                        else:
-                            filenames.append(child.name)
-                    except OSError:
-                        filenames.append(child.name)
+                with path.scandir() as entries:
+                    for entry in entries:
+                        name = entry.name
+                        try:
+                            if entry.is_dir(follow_symlinks=follow_symlinks):
+                                if not top_down:
+                                    paths.append(path.joinpath(name))
+                                dirnames.append(name)
+                            else:
+                                filenames.append(name)
+                        except OSError:
+                            filenames.append(name)
             except OSError as error:
                 if on_error is not None:
                     on_error(error)
index 4596d0b0e26763026479760ff3e67cc69d8666b5..4ab804850e9c3e4137a9936d22ca7f223daa8457 100644 (file)
@@ -1951,7 +1951,7 @@ class DummyPathTest(DummyPurePathTest):
         if self.can_symlink:
             # Add some symlinks
             source.joinpath('linkC').symlink_to('fileC')
-            source.joinpath('linkD').symlink_to('dirD')
+            source.joinpath('linkD').symlink_to('dirD', target_is_directory=True)
 
         # Perform the copy
         target = base / 'copyC'
@@ -2969,7 +2969,7 @@ class DummyPathTest(DummyPurePathTest):
                 f.write(f"I'm {path} and proud of it.  Blame test_pathlib.\n")
 
         if self.can_symlink:
-            self.link_path.symlink_to(t2_path)
+            self.link_path.symlink_to(t2_path, target_is_directory=True)
             broken_link_path.symlink_to('broken')
             broken_link2_path.symlink_to(self.cls('tmp3', 'broken'))
             self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])