]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
#4489 Make fd based rmtree work on bytes
authorHynek Schlawack <hs@ox.cx>
Mon, 25 Jun 2012 11:27:31 +0000 (13:27 +0200)
committerHynek Schlawack <hs@ox.cx>
Mon, 25 Jun 2012 11:27:31 +0000 (13:27 +0200)
Lib/shutil.py
Lib/test/test_shutil.py

index 2c00f4a025f4ec93ce6a1210080915768039f181..3cafd01d239686611c01b1748b31f226c9f4089e 100644 (file)
@@ -426,6 +426,9 @@ def rmtree(path, ignore_errors=False, onerror=None):
         def onerror(*args):
             raise
     if _use_fd_functions:
+        # While the unsafe rmtree works fine on bytes, the fd based does not.
+        if isinstance(path, bytes):
+            path = os.fsdecode(path)
         # Note: To guard against symlink races, we use the standard
         # lstat()/open()/fstat() trick.
         try:
index c879fdd8307ad12dc841dc59d6ba6e06fbc424e5..d23deee62496ab29e520958e4854bda00d981a59 100644 (file)
@@ -108,6 +108,15 @@ class TestShutil(unittest.TestCase):
         self.tempdirs.append(d)
         return d
 
+    def test_rmtree_works_on_bytes(self):
+        tmp = self.mkdtemp()
+        victim = os.path.join(tmp, 'killme')
+        os.mkdir(victim)
+        write_file(os.path.join(victim, 'somefile'), 'foo')
+        victim = os.fsencode(victim)
+        self.assertIsInstance(victim, bytes)
+        shutil.rmtree(victim)
+
     def test_rmtree_errors(self):
         # filename is guaranteed not to exist
         filename = tempfile.mktemp()