Skip to content

Commit 5e1325b

Browse files
committed
pythonGH-125413: pathlib ABCs: use scandir() to speed up walk()
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.
1 parent 260843d commit 5e1325b

2 files changed

Lines changed: 14 additions & 12 deletions

File tree

‎Lib/pathlib/_abc.py‎

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -705,16 +705,18 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
705705
if not top_down:
706706
paths.append((path, dirnames, filenames))
707707
try:
708-
for child in path.iterdir():
709-
try:
710-
if child.is_dir(follow_symlinks=follow_symlinks):
711-
if not top_down:
712-
paths.append(child)
713-
dirnames.append(child.name)
714-
else:
715-
filenames.append(child.name)
716-
except OSError:
717-
filenames.append(child.name)
708+
with path.scandir() as entries:
709+
for entry in entries:
710+
name = entry.name
711+
try:
712+
if entry.is_dir(follow_symlinks=follow_symlinks):
713+
if not top_down:
714+
paths.append(path.joinpath(name))
715+
dirnames.append(name)
716+
else:
717+
filenames.append(name)
718+
except OSError:
719+
filenames.append(name)
718720
except OSError as error:
719721
if on_error is not None:
720722
on_error(error)

‎Lib/test/test_pathlib/test_pathlib_abc.py‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1949,7 +1949,7 @@ def ordered_walk(path):
19491949
if self.can_symlink:
19501950
# Add some symlinks
19511951
source.joinpath('linkC').symlink_to('fileC')
1952-
source.joinpath('linkD').symlink_to('dirD')
1952+
source.joinpath('linkD').symlink_to('dirD', target_is_directory=True)
19531953

19541954
# Perform the copy
19551955
target = base / 'copyC'
@@ -2967,7 +2967,7 @@ def setUpWalk(self):
29672967
f.write(f"I'm {path} and proud of it. Blame test_pathlib.\n")
29682968

29692969
if self.can_symlink:
2970-
self.link_path.symlink_to(t2_path)
2970+
self.link_path.symlink_to(t2_path, target_is_directory=True)
29712971
broken_link_path.symlink_to('broken')
29722972
broken_link2_path.symlink_to(self.cls('tmp3', 'broken'))
29732973
self.sub2_tree = (self.sub2_path, [], ["broken_link", "broken_link2", "link", "tmp3"])

0 commit comments

Comments
 (0)