gh-156707: Do not follow junctions in os_helper.rmtree() on Windows (GH-156710)
os.lstat() reports a junction as a directory, so the junction was followed
and files in the directory it points to could be removed. Now the junction
itself is removed, as in os.walk() and shutil.rmtree().
diff --git a/Lib/test/support/os_helper.py b/Lib/test/support/os_helper.py
index e1e2e69..12d34de 100644
--- a/Lib/test/support/os_helper.py
+++ b/Lib/test/support/os_helper.py
@@ -434,7 +434,10 @@ def _rmtree_inner(path):
file=sys.__stderr__)
mode = 0
if stat.S_ISDIR(mode):
- _waitfor(_rmtree_inner, fullname, waitall=True)
+ # Do not follow junctions, which os.lstat() reports
+ # as directories.
+ if not os.path.isjunction(fullname):
+ _waitfor(_rmtree_inner, fullname, waitall=True)
_force_run(fullname, os.rmdir, fullname)
else:
_force_run(fullname, os.unlink, fullname)