summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tempfile.py
diff options
context:
space:
mode:
authorCAM Gerlach <CAM.Gerlach@Gerlach.CAM>2021-03-14 18:06:56 (GMT)
committerGitHub <noreply@github.com>2021-03-14 18:06:56 (GMT)
commitbd2fa3c416ffe6107b500a2180fa1764645c0a61 (patch)
tree44a53d55e02511cb6f6a1641c678c1b179f87863 /Lib/test/test_tempfile.py
parentd48848c83e0f3e41b65c8f741f3fb6dbce5b9c29 (diff)
downloadcpython-bd2fa3c416ffe6107b500a2180fa1764645c0a61.zip
cpython-bd2fa3c416ffe6107b500a2180fa1764645c0a61.tar.gz
cpython-bd2fa3c416ffe6107b500a2180fa1764645c0a61.tar.bz2
bpo-29982: Add "ignore_cleanup_errors" param to tempfile.TemporaryDirectory() (GH-24793)
Diffstat (limited to 'Lib/test/test_tempfile.py')
-rw-r--r--Lib/test/test_tempfile.py90
1 files changed, 88 insertions, 2 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 5822c75..3a3f6a9 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -1365,13 +1365,17 @@ class NulledModules:
d.clear()
d.update(c)
+
class TestTemporaryDirectory(BaseTestCase):
"""Test TemporaryDirectory()."""
- def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1):
+ def do_create(self, dir=None, pre="", suf="", recurse=1, dirs=1, files=1,
+ ignore_cleanup_errors=False):
if dir is None:
dir = tempfile.gettempdir()
- tmp = tempfile.TemporaryDirectory(dir=dir, prefix=pre, suffix=suf)
+ tmp = tempfile.TemporaryDirectory(
+ dir=dir, prefix=pre, suffix=suf,
+ ignore_cleanup_errors=ignore_cleanup_errors)
self.nameCheck(tmp.name, dir, pre, suf)
self.do_create2(tmp.name, recurse, dirs, files)
return tmp
@@ -1410,6 +1414,30 @@ class TestTemporaryDirectory(BaseTestCase):
finally:
os.rmdir(dir)
+ def test_explict_cleanup_ignore_errors(self):
+ """Test that cleanup doesn't return an error when ignoring them."""
+ with tempfile.TemporaryDirectory() as working_dir:
+ temp_dir = self.do_create(
+ dir=working_dir, ignore_cleanup_errors=True)
+ temp_path = pathlib.Path(temp_dir.name)
+ self.assertTrue(temp_path.exists(),
+ f"TemporaryDirectory {temp_path!s} does not exist")
+ with open(temp_path / "a_file.txt", "w+t") as open_file:
+ open_file.write("Hello world!\n")
+ temp_dir.cleanup()
+ self.assertEqual(len(list(temp_path.glob("*"))),
+ int(sys.platform.startswith("win")),
+ "Unexpected number of files in "
+ f"TemporaryDirectory {temp_path!s}")
+ self.assertEqual(
+ temp_path.exists(),
+ sys.platform.startswith("win"),
+ f"TemporaryDirectory {temp_path!s} existance state unexpected")
+ temp_dir.cleanup()
+ self.assertFalse(
+ temp_path.exists(),
+ f"TemporaryDirectory {temp_path!s} exists after cleanup")
+
@os_helper.skip_unless_symlink
def test_cleanup_with_symlink_to_a_directory(self):
# cleanup() should not follow symlinks to directories (issue #12464)
@@ -1444,6 +1472,27 @@ class TestTemporaryDirectory(BaseTestCase):
finally:
os.rmdir(dir)
+ @support.cpython_only
+ def test_del_on_collection_ignore_errors(self):
+ """Test that ignoring errors works when TemporaryDirectory is gced."""
+ with tempfile.TemporaryDirectory() as working_dir:
+ temp_dir = self.do_create(
+ dir=working_dir, ignore_cleanup_errors=True)
+ temp_path = pathlib.Path(temp_dir.name)
+ self.assertTrue(temp_path.exists(),
+ f"TemporaryDirectory {temp_path!s} does not exist")
+ with open(temp_path / "a_file.txt", "w+t") as open_file:
+ open_file.write("Hello world!\n")
+ del temp_dir
+ self.assertEqual(len(list(temp_path.glob("*"))),
+ int(sys.platform.startswith("win")),
+ "Unexpected number of files in "
+ f"TemporaryDirectory {temp_path!s}")
+ self.assertEqual(
+ temp_path.exists(),
+ sys.platform.startswith("win"),
+ f"TemporaryDirectory {temp_path!s} existance state unexpected")
+
def test_del_on_shutdown(self):
# A TemporaryDirectory may be cleaned up during shutdown
with self.do_create() as dir:
@@ -1476,6 +1525,43 @@ class TestTemporaryDirectory(BaseTestCase):
self.assertNotIn("Exception ", err)
self.assertIn("ResourceWarning: Implicitly cleaning up", err)
+ def test_del_on_shutdown_ignore_errors(self):
+ """Test ignoring errors works when a tempdir is gc'ed on shutdown."""
+ with tempfile.TemporaryDirectory() as working_dir:
+ code = """if True:
+ import pathlib
+ import sys
+ import tempfile
+ import warnings
+
+ temp_dir = tempfile.TemporaryDirectory(
+ dir={working_dir!r}, ignore_cleanup_errors=True)
+ sys.stdout.buffer.write(temp_dir.name.encode())
+
+ temp_dir_2 = pathlib.Path(temp_dir.name) / "test_dir"
+ temp_dir_2.mkdir()
+ with open(temp_dir_2 / "test0.txt", "w") as test_file:
+ test_file.write("Hello world!")
+ open_file = open(temp_dir_2 / "open_file.txt", "w")
+ open_file.write("Hello world!")
+
+ warnings.filterwarnings("always", category=ResourceWarning)
+ """.format(working_dir=working_dir)
+ __, out, err = script_helper.assert_python_ok("-c", code)
+ temp_path = pathlib.Path(out.decode().strip())
+ self.assertEqual(len(list(temp_path.glob("*"))),
+ int(sys.platform.startswith("win")),
+ "Unexpected number of files in "
+ f"TemporaryDirectory {temp_path!s}")
+ self.assertEqual(
+ temp_path.exists(),
+ sys.platform.startswith("win"),
+ f"TemporaryDirectory {temp_path!s} existance state unexpected")
+ err = err.decode('utf-8', 'backslashreplace')
+ self.assertNotIn("Exception", err)
+ self.assertNotIn("Error", err)
+ self.assertIn("ResourceWarning: Implicitly cleaning up", err)
+
def test_exit_on_shutdown(self):
# Issue #22427
with self.do_create() as dir: