summaryrefslogtreecommitdiffstats
path: root/Lib/test
diff options
context:
space:
mode:
authorStanislav Zmiev <szmiev2000@gmail.com>2023-03-22 14:45:25 (GMT)
committerGitHub <noreply@github.com>2023-03-22 14:45:25 (GMT)
commit713df2c53489ce8012d0ede10b70950e6b0d8372 (patch)
tree8ef14b4352a308817169ae77533537b958aedef4 /Lib/test
parentaf9c34f6ef8dceb21871206eb3e4d350f6e3d3dc (diff)
downloadcpython-713df2c53489ce8012d0ede10b70950e6b0d8372.zip
cpython-713df2c53489ce8012d0ede10b70950e6b0d8372.tar.gz
cpython-713df2c53489ce8012d0ede10b70950e6b0d8372.tar.bz2
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>
Diffstat (limited to 'Lib/test')
-rw-r--r--Lib/test/test_pathlib.py13
1 files changed, 13 insertions, 0 deletions
diff --git a/Lib/test/test_pathlib.py b/Lib/test/test_pathlib.py
index f05dead..3041630 100644
--- a/Lib/test/test_pathlib.py
+++ b/Lib/test/test_pathlib.py
@@ -13,6 +13,7 @@ import unittest
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 @@ class WalkTests(unittest.TestCase):
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