mirror of
https://github.com/python/cpython.git
synced 2025-11-03 15:11:34 +00:00
GH-89727: Fix pathlib.Path.walk RecursionError on deep trees (GH-100282)
Use a stack to implement `pathlib.Path.walk()` iteratively instead of recursively to avoid hitting recursion limits on deeply nested trees. Co-authored-by: Barney Gale <barney.gale@gmail.com> Co-authored-by: Brett Cannon <brett@python.org>
This commit is contained in:
parent
af9c34f6ef
commit
713df2c534
3 changed files with 49 additions and 33 deletions
|
|
@ -13,6 +13,7 @@
|
|||
from unittest import mock
|
||||
|
||||
from test.support import import_helper
|
||||
from test.support import set_recursion_limit
|
||||
from test.support import is_emscripten, is_wasi
|
||||
from test.support import os_helper
|
||||
from test.support.os_helper import TESTFN, FakePath
|
||||
|
|
@ -2793,6 +2794,18 @@ def test_walk_many_open_files(self):
|
|||
self.assertEqual(next(it), expected)
|
||||
path = path / 'd'
|
||||
|
||||
def test_walk_above_recursion_limit(self):
|
||||
recursion_limit = 40
|
||||
# directory_depth > recursion_limit
|
||||
directory_depth = recursion_limit + 10
|
||||
base = pathlib.Path(os_helper.TESTFN, 'deep')
|
||||
path = pathlib.Path(base, *(['d'] * directory_depth))
|
||||
path.mkdir(parents=True)
|
||||
|
||||
with set_recursion_limit(recursion_limit):
|
||||
list(base.walk())
|
||||
list(base.walk(top_down=False))
|
||||
|
||||
|
||||
class PathTest(_BasePathTest, unittest.TestCase):
|
||||
cls = pathlib.Path
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue