Skip to content

Commit 9ad7ae0

Browse files
committed
Test cleanup of TemporaryDirectory with a bytes name
1 parent 0d0aade commit 9ad7ae0

1 file changed

Lines changed: 34 additions & 7 deletions

File tree

‎Lib/test/test_tempfile.py‎

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,10 +1653,14 @@ def __exit__(self, *exc_info):
16531653
class TestTemporaryDirectory(BaseTestCase):
16541654
"""Test TemporaryDirectory()."""
16551655

1656-
def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,
1656+
def do_create(self, dir=None, pre=None, suf=None, recurse=1, dirs=1, files=1,
16571657
ignore_cleanup_errors=False):
16581658
if dir is None:
16591659
dir = tempfile.gettempdir()
1660+
if pre is None:
1661+
pre = type(dir)()
1662+
if suf is None:
1663+
suf = type(dir)()
16601664
tmp = tempfile.TemporaryDirectory(
16611665
dir=dir, prefix=pre, suffix=suf,
16621666
ignore_cleanup_errors=ignore_cleanup_errors)
@@ -1666,13 +1670,17 @@ def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,
16661670

16671671
def do_create2(self, path, recurse=1, dirs=1, files=1):
16681672
# Create subdirectories and some files
1673+
def join(name):
1674+
if isinstance(path, bytes):
1675+
name = os.fsencode(name)
1676+
return os.path.join(path, name)
16691677
if recurse:
16701678
for i in range(dirs):
1671-
name = os.path.join(path, "dir%d" % i)
1679+
name = join("dir%d" % i)
16721680
os.mkdir(name)
16731681
self.do_create2(name, recurse-1, dirs, files)
16741682
for i in range(files):
1675-
with open(os.path.join(path, "test%d.txt" % i), "wb") as f:
1683+
with open(join("test%d.txt" % i), "wb") as f:
16761684
f.write(b"Hello world!")
16771685

16781686
def test_mkdtemp_failure(self):
@@ -1698,6 +1706,15 @@ def test_explicit_cleanup(self):
16981706
finally:
16991707
os.rmdir(dir)
17001708

1709+
def test_explicit_cleanup_bytes(self):
1710+
# A TemporaryDirectory with a bytes name is deleted when cleaned up
1711+
with tempfile.TemporaryDirectory() as working_dir:
1712+
d = self.do_create(dir=os.fsencode(working_dir), recurse=2)
1713+
self.assertIsInstance(d.name, bytes)
1714+
self.assertTrue(os.path.exists(d.name))
1715+
d.cleanup()
1716+
self.assertFalse(os.path.exists(d.name))
1717+
17011718
def test_explicit_cleanup_ignore_errors(self):
17021719
"""Test that cleanup doesn't return an error when ignoring them."""
17031720
with tempfile.TemporaryDirectory() as working_dir:
@@ -2149,11 +2166,17 @@ def test_context_manager(self):
21492166
self.assertEqual(name, d.name)
21502167
self.assertFalse(os.path.exists(name))
21512168

2152-
def test_modes(self):
2169+
@support.subTests('bytes_name', (False, True))
2170+
def test_modes(self, bytes_name):
21532171
for mode in range(8):
21542172
mode <<= 6
21552173
with self.subTest(mode=format(mode, '03o')):
2156-
d = self.do_create(recurse=3, dirs=2, files=2)
2174+
if bytes_name:
2175+
d = self.do_create(recurse=3, dirs=2, files=2,
2176+
dir=tempfile.gettempdirb())
2177+
else:
2178+
d = self.do_create(recurse=3, dirs=2, files=2)
2179+
self.assertIsInstance(d.name, bytes if bytes_name else str)
21572180
with d:
21582181
# Change files and directories mode recursively.
21592182
for root, dirs, files in os.walk(d.name, topdown=False):
@@ -2164,13 +2187,17 @@ def test_modes(self):
21642187
self.assertFalse(os.path.exists(d.name))
21652188

21662189
@support.subTests('ignore_errors', (True, False))
2167-
def test_parent_mode_preserved(self, ignore_errors):
2190+
@support.subTests('bytes_name', (False, True))
2191+
def test_parent_mode_preserved(self, ignore_errors, bytes_name):
21682192
# Test that cleanup does not touch the parent directory,
21692193
# even if that prevents removal.
21702194
for mode in range(8):
21712195
mode <<= 6
21722196
with self.subTest(mode=format(mode, '03o')):
2173-
outer = self.do_create()
2197+
if bytes_name:
2198+
outer = self.do_create(dir=tempfile.gettempdirb())
2199+
else:
2200+
outer = self.do_create()
21742201
with outer:
21752202
d = self.do_create(dir=outer.name, dirs=2, files=2,
21762203
ignore_cleanup_errors=ignore_errors)

0 commit comments

Comments
 (0)