#4489 Make fd based rmtree work on bytes

This commit is contained in:
Hynek Schlawack 2012-06-25 13:27:31 +02:00
parent 77892dc1e3
commit 3b52778c74
2 changed files with 12 additions and 0 deletions

View 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:

View 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()