]> granicus.if.org Git - python/commitdiff
Issue #28666: Now test.support.rmtree is able to remove unwritable or
authorSerhiy Storchaka <storchaka@gmail.com>
Sun, 20 Nov 2016 15:42:32 +0000 (17:42 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Sun, 20 Nov 2016 15:42:32 +0000 (17:42 +0200)
unreadable directories on Windows too.

Lib/test/support/__init__.py

index 1d658513acd5fd4344bdb2bb4a7ba1c915427523..f4d80b4998c6fb478a58bfaddd5d2b6732e19771 100644 (file)
@@ -297,6 +297,16 @@ def unload(name):
     except KeyError:
         pass
 
+def _force_run(path, func, *args):
+    try:
+        return func(*args)
+    except OSError as err:
+        if verbose >= 2:
+            print('%s: %s' % (err.__class__.__name__, err))
+            print('re-run %s%r' % (func.__name__, args))
+        os.chmod(path, stat.S_IRWXU)
+        return func(*args)
+
 if sys.platform.startswith("win"):
     def _waitfor(func, pathname, waitall=False):
         # Perform the operation
@@ -339,7 +349,7 @@ if sys.platform.startswith("win"):
 
     def _rmtree(path):
         def _rmtree_inner(path):
-            for name in os.listdir(path):
+            for name in _force_run(path, os.listdir, path):
                 fullname = os.path.join(path, name)
                 try:
                     mode = os.lstat(fullname).st_mode
@@ -349,9 +359,9 @@ if sys.platform.startswith("win"):
                     mode = 0
                 if stat.S_ISDIR(mode):
                     _waitfor(_rmtree_inner, fullname, waitall=True)
-                    os.rmdir(fullname)
+                    _force_run(path, os.rmdir, fullname)
                 else:
-                    os.unlink(fullname)
+                    _force_run(path, os.unlink, fullname)
         _waitfor(_rmtree_inner, path, waitall=True)
         _waitfor(os.rmdir, path)
 else:
@@ -365,17 +375,8 @@ else:
         except OSError:
             pass
 
-        def force_run(path, func, *args):
-            try:
-                return func(*args)
-            except OSError as err:
-                if verbose >= 2:
-                    print('%s: %s' % (err.__class__.__name__, err))
-                    print('re-run %s%r' % (func.__name__, args))
-                os.chmod(path, stat.S_IRWXU)
-                return func(*args)
         def _rmtree_inner(path):
-            for name in force_run(path, os.listdir, path):
+            for name in _force_run(path, os.listdir, path):
                 fullname = os.path.join(path, name)
                 try:
                     mode = os.lstat(fullname).st_mode
@@ -383,9 +384,9 @@ else:
                     mode = 0
                 if stat.S_ISDIR(mode):
                     _rmtree_inner(fullname)
-                    force_run(path, os.rmdir, fullname)
+                    _force_run(path, os.rmdir, fullname)
                 else:
-                    force_run(path, os.unlink, fullname)
+                    _force_run(path, os.unlink, fullname)
         _rmtree_inner(path)
         os.rmdir(path)