Skip to content
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Call accessor.scandir() from only one method.
  • Loading branch information
barneygale committed Jan 1, 2022
commit 57f40f79eab2ada61a6d25929620d69a529a5f6e
30 changes: 16 additions & 14 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -401,15 +401,14 @@ def select_from(self, parent_path):
path_cls = type(parent_path)
is_dir = path_cls.is_dir
exists = path_cls.exists
scandir = parent_path._accessor.scandir
if not is_dir(parent_path):
return iter([])
return self._select_from(parent_path, is_dir, exists, scandir)
return self._select_from(parent_path, is_dir, exists)


class _TerminatingSelector:

def _select_from(self, parent_path, is_dir, exists, scandir):
def _select_from(self, parent_path, is_dir, exists):
yield parent_path


Expand All @@ -419,11 +418,11 @@ def __init__(self, name, child_parts, flavour):
self.name = name
_Selector.__init__(self, child_parts, flavour)

def _select_from(self, parent_path, is_dir, exists, scandir):
def _select_from(self, parent_path, is_dir, exists):
try:
path = parent_path._make_child_relpath(self.name)
if (is_dir if self.dironly else exists)(path):
for p in self.successor._select_from(path, is_dir, exists, scandir):
for p in self.successor._select_from(path, is_dir, exists):
yield p
except PermissionError:
return
Expand All @@ -435,9 +434,9 @@ def __init__(self, pat, child_parts, flavour):
self.match = flavour.compile_pattern(pat)
_Selector.__init__(self, child_parts, flavour)

def _select_from(self, parent_path, is_dir, exists, scandir):
def _select_from(self, parent_path, is_dir, exists):
try:
with scandir(parent_path) as scandir_it:
with parent_path.scandir() as scandir_it:
entries = list(scandir_it)
for entry in entries:
if self.dironly:
Expand All @@ -454,7 +453,7 @@ def _select_from(self, parent_path, is_dir, exists, scandir):
name = entry.name
if self.match(name):
path = parent_path._make_child_relpath(name)
for p in self.successor._select_from(path, is_dir, exists, scandir):
for p in self.successor._select_from(path, is_dir, exists):
yield p
except PermissionError:
return
Expand All @@ -465,10 +464,10 @@ class _RecursiveWildcardSelector(_Selector):
def __init__(self, pat, child_parts, flavour):
_Selector.__init__(self, child_parts, flavour)

def _iterate_directories(self, parent_path, is_dir, scandir):
def _iterate_directories(self, parent_path, is_dir):
yield parent_path
try:
with scandir(parent_path) as scandir_it:
with parent_path.scandir() as scandir_it:
entries = list(scandir_it)
for entry in entries:
entry_is_dir = False
Expand All @@ -479,18 +478,18 @@ def _iterate_directories(self, parent_path, is_dir, scandir):
raise
if entry_is_dir and not entry.is_symlink():
path = parent_path._make_child_relpath(entry.name)
for p in self._iterate_directories(path, is_dir, scandir):
for p in self._iterate_directories(path, is_dir):
yield p
except PermissionError:
return

def _select_from(self, parent_path, is_dir, exists, scandir):
def _select_from(self, parent_path, is_dir, exists):
try:
yielded = set()
try:
successor_select = self.successor._select_from
for starting_point in self._iterate_directories(parent_path, is_dir, scandir):
for p in successor_select(starting_point, is_dir, exists, scandir):
for starting_point in self._iterate_directories(parent_path, is_dir):
for p in successor_select(starting_point, is_dir, exists):
if p not in yielded:
yield p
yielded.add(p)
Expand Down Expand Up @@ -1018,6 +1017,9 @@ def iterdir(self):
continue
yield self._make_child_relpath(name)

def scandir(self):
return self._accessor.scandir(self)

def glob(self, pattern):
"""Iterate over this subtree and yield all existing files (of any
kind, including directories) matching the given relative pattern.
Expand Down