8000 gh-89727: Fix pathlib.Path.walk RecursionError on deep trees by zmievsa · Pull Request #100282 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-89727: Fix pathlib.Path.walk RecursionError on deep trees #100282

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
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
Code review fixes
  • Loading branch information
zmievsa committed Jan 1, 2023
commit 5609e4ff73cc916cc1c3f3a6aada0f4afa84eae5
18 changes: 9 additions & 9 deletions Lib/pathlib.py
Original file line number Diff line number Diff line change
Expand Up @@ -1214,12 +1214,12 @@ def expanduser(self):
def walk(self, top_down=True, on_error=None, follow_symlinks=False):
"""Walk the directory tree from this directory, similar to os.walk()."""
sys.audit("pathlib.Path.walk", self, on_error, follow_symlinks)
stack = [(False, self)]
stack = [self]

while stack:
must_yield, top = stack.pop()
if must_yield:
yield top
path = stack.pop()
if isinstance(path, tuple):
yield path
continue

# We may not have read permission for self, in which case we can't
Expand All @@ -1228,7 +1228,7 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
# minor reason when (say) a thousand readable directories are still
# left to visit. That logic is copied here.
try:
scandir_it = top._scandir()
scandir_it = path._scandir()
except OSError as error:
if on_error is not None:
on_error(error)
Expand All @@ -1250,13 +1250,13 @@ def walk(self, top_down=True, on_error=None, follow_symlinks=False):
filenames.append(entry.name)

if top_down:
yield top, dirnames, filenames
yield path, dirnames, filenames
else:
stack.append((True, (top, dirnames, filenames)))
stack.append((path, dirnames, filenames))

for dirname in reversed(dirnames):
dirpath = top._make_child_relpath(dirname)
stack.append((False, dirpath))
dirpath = path._make_child_relpath(dirname)
stack.append(dirpath)


class PosixPath(Path, PurePosixPath):
Expand Down
0