(original) (raw)
changeset: 77767:2e2329aeb5c1 user: Hynek Schlawack hs@ox.cx date: Mon Jun 25 13:27:31 2012 +0200 files: Lib/shutil.py Lib/test/test_shutil.py description: #4489 Make fd based rmtree work on bytes diff -r c890cbad4748 -r 2e2329aeb5c1 Lib/shutil.py --- a/Lib/shutil.py Mon Jun 25 03:27:33 2012 -0700 +++ b/Lib/shutil.py Mon Jun 25 13:27:31 2012 +0200 @@ -426,6 +426,9 @@ 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: diff -r c890cbad4748 -r 2e2329aeb5c1 Lib/test/test_shutil.py --- a/Lib/test/test_shutil.py Mon Jun 25 03:27:33 2012 -0700 +++ b/Lib/test/test_shutil.py Mon Jun 25 13:27:31 2012 +0200 @@ -108,6 +108,15 @@ 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() /hs@ox.cx