Skip to content

Commit 57f40f7

Browse files
committed
Call accessor.scandir() from only one method.
1 parent 740953b commit 57f40f7

File tree

1 file changed

+16
-14
lines changed

1 file changed

+16
-14
lines changed

Lib/pathlib.py

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -401,15 +401,14 @@ def select_from(self, parent_path):
401401
path_cls = type(parent_path)
402402
is_dir = path_cls.is_dir
403403
exists = path_cls.exists
404-
scandir = parent_path._accessor.scandir
405404
if not is_dir(parent_path):
406405
return iter([])
407-
return self._select_from(parent_path, is_dir, exists, scandir)
406+
return self._select_from(parent_path, is_dir, exists)
408407

409408

410409
class _TerminatingSelector:
411410

412-
def _select_from(self, parent_path, is_dir, exists, scandir):
411+
def _select_from(self, parent_path, is_dir, exists):
413412
yield parent_path
414413

415414

@@ -419,11 +418,11 @@ def __init__(self, name, child_parts, flavour):
419418
self.name = name
420419
_Selector.__init__(self, child_parts, flavour)
421420

422-
def _select_from(self, parent_path, is_dir, exists, scandir):
421+
def _select_from(self, parent_path, is_dir, exists):
423422
try:
424423
path = parent_path._make_child_relpath(self.name)
425424
if (is_dir if self.dironly else exists)(path):
426-
for p in self.successor._select_from(path, is_dir, exists, scandir):
425+
for p in self.successor._select_from(path, is_dir, exists):
427426
yield p
428427
except PermissionError:
429428
return
@@ -435,9 +434,9 @@ def __init__(self, pat, child_parts, flavour):
435434
self.match = flavour.compile_pattern(pat)
436435
_Selector.__init__(self, child_parts, flavour)
437436

438-
def _select_from(self, parent_path, is_dir, exists, scandir):
437+
def _select_from(self, parent_path, is_dir, exists):
439438
try:
440-
with scandir(parent_path) as scandir_it:
439+
with parent_path.scandir() as scandir_it:
441440
entries = list(scandir_it)
442441
for entry in entries:
443442
if self.dironly:
@@ -454,7 +453,7 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
454453
name = entry.name
455454
if self.match(name):
456455
path = parent_path._make_child_relpath(name)
457-
for p in self.successor._select_from(path, is_dir, exists, scandir):
456+
for p in self.successor._select_from(path, is_dir, exists):
458457
yield p
459458
except PermissionError:
460459
return
@@ -465,10 +464,10 @@ class _RecursiveWildcardSelector(_Selector):
465464
def __init__(self, pat, child_parts, flavour):
466465
_Selector.__init__(self, child_parts, flavour)
467466

468-
def _iterate_directories(self, parent_path, is_dir, scandir):
467+
def _iterate_directories(self, parent_path, is_dir):
469468
yield parent_path
470469
try:
471-
with scandir(parent_path) as scandir_it:
470+
with parent_path.scandir() as scandir_it:
472471
entries = list(scandir_it)
473472
for entry in entries:
474473
entry_is_dir = False
@@ -479,18 +478,18 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
479478
raise
480479
if entry_is_dir and not entry.is_symlink():
481480
path = parent_path._make_child_relpath(entry.name)
482-
for p in self._iterate_directories(path, is_dir, scandir):
481+
for p in self._iterate_directories(path, is_dir):
483482
yield p
484483
except PermissionError:
485484
return
486485

487-
def _select_from(self, parent_path, is_dir, exists, scandir):
486+
def _select_from(self, parent_path, is_dir, exists):
488487
try:
489488
yielded = set()
490489
try:
491490
successor_select = self.successor._select_from
492-
for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
493-
for p in successor_select(starting_point, is_dir, exists, scandir):
491+
for starting_point in self._iterate_directories(parent_path, is_dir):
492+
for p in successor_select(starting_point, is_dir, exists):
494493
if p not in yielded:
495494
yield p
496495
yielded.add(p)
@@ -1018,6 +1017,9 @@ def iterdir(self):
10181017
continue
10191018
yield self._make_child_relpath(name)
10201019

1020+
def scandir(self):
1021+
return self._accessor.scandir(self)
1022+
10211023
def glob(self, pattern):
10221024
"""Iterate over this subtree and yield all existing files (of any
10231025
kind, including directories) matching the given relative pattern.

0 commit comments

Comments
 (0)