GH-89727: Partially fix shutil.rmtree() recursion error on deep trees (#119634)

Make `shutil._rmtree_unsafe()` call `os.walk()`, which is implemented
without recursion.

`shutil._rmtree_safe_fd()` is not affected and can still raise a recursion
error.

Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
Barney Gale 2024-05-29 21:11:30 +01:00 committed by GitHub
parent c22323cd1c
commit a150679f90
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
4 changed files with 33 additions and 28 deletions

View file

@ -741,6 +741,17 @@ class TestRmTree(BaseTest, unittest.TestCase):
shutil.rmtree(TESTFN)
raise
@unittest.skipIf(shutil._use_fd_functions, "fd-based functions remain unfixed (GH-89727)")
def test_rmtree_above_recursion_limit(self):
recursion_limit = 40
# directory_depth > recursion_limit
directory_depth = recursion_limit + 10
base = os.path.join(TESTFN, *(['d'] * directory_depth))
os.makedirs(base)
with support.infinite_recursion(recursion_limit):
shutil.rmtree(TESTFN)
class TestCopyTree(BaseTest, unittest.TestCase):