summaryrefslogtreecommitdiffstats
path: root/Lib/pathlib.py
diff options
context:
space:
mode:
authorBarney Gale <barney.gale@gmail.com>2023-05-15 17:33:32 (GMT)
committerGitHub <noreply@github.com>2023-05-15 17:33:32 (GMT)
commitcb88ae635e96d7020ba6187bcfd45ace4dcd8395 (patch)
tree54a3c649da8786ad5a05ccdc999095a25211d709 /Lib/pathlib.py
parentb378d991f8cd41c33416e590cb83472cce1d6b98 (diff)
downloadcpython-cb88ae635e96d7020ba6187bcfd45ace4dcd8395.zip
cpython-cb88ae635e96d7020ba6187bcfd45ace4dcd8395.tar.gz
cpython-cb88ae635e96d7020ba6187bcfd45ace4dcd8395.tar.bz2
GH-102613: Fix recursion error from `pathlib.Path.glob()` (GH-104373)
Use `Path.walk()` to implement the recursive wildcard `**`. This method uses an iterative (rather than recursive) walk - see GH-100282.
Diffstat (limited to 'Lib/pathlib.py')
-rw-r--r--Lib/pathlib.py25
1 files changed, 5 insertions, 20 deletions
diff --git a/Lib/pathlib.py b/Lib/pathlib.py
index 40b7293..ef7c47c 100644
--- a/Lib/pathlib.py
+++ b/Lib/pathlib.py
@@ -164,30 +164,15 @@ class _RecursiveWildcardSelector(_Selector):
def __init__(self, pat, child_parts, flavour, case_sensitive):
_Selector.__init__(self, child_parts, flavour, case_sensitive)
- def _iterate_directories(self, parent_path, scandir):
+ def _iterate_directories(self, parent_path):
yield parent_path
- try:
- # We must close the scandir() object before proceeding to
- # avoid exhausting file descriptors when globbing deep trees.
- with scandir(parent_path) as scandir_it:
- entries = list(scandir_it)
- except OSError:
- pass
- else:
- for entry in entries:
- entry_is_dir = False
- try:
- entry_is_dir = entry.is_dir(follow_symlinks=False)
- except OSError:
- pass
- if entry_is_dir:
- path = parent_path._make_child_relpath(entry.name)
- for p in self._iterate_directories(path, scandir):
- yield p
+ for dirpath, dirnames, _ in parent_path.walk():
+ for dirname in dirnames:
+ yield dirpath._make_child_relpath(dirname)
def _select_from(self, parent_path, scandir):
successor_select = self.successor._select_from
- for starting_point in self._iterate_directories(parent_path, scandir):
+ for starting_point in self._iterate_directories(parent_path):
for p in successor_select(starting_point, scandir):
yield p