summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_tempfile.py
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2013-02-12 22:35:30 (GMT)
committerSerhiy Storchaka <storchaka@gmail.com>2013-02-12 22:35:30 (GMT)
commitf6b361ec1a10b93ff8b927b400ca1777429bb23a (patch)
tree19679051591f3dda745d47ebca1d0d95e4bd1e6c /Lib/test/test_tempfile.py
parente4ad8aacd1b10ef14d2a6d44c4c1ebd283ede6bb (diff)
downloadcpython-f6b361ec1a10b93ff8b927b400ca1777429bb23a.zip
cpython-f6b361ec1a10b93ff8b927b400ca1777429bb23a.tar.gz
cpython-f6b361ec1a10b93ff8b927b400ca1777429bb23a.tar.bz2
Issue #16800: tempfile.gettempdir() no longer left temporary files when
the disk is full. Original patch by Amir Szekely.
Diffstat (limited to 'Lib/test/test_tempfile.py')
-rw-r--r--Lib/test/test_tempfile.py46
1 files changed, 44 insertions, 2 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index a90cd0e..cf8b35a 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -1,5 +1,7 @@
# tempfile.py unit tests.
import tempfile
+import errno
+import io
import os
import signal
import sys
@@ -211,8 +213,48 @@ class test__candidate_tempdir_list(TC):
test_classes.append(test__candidate_tempdir_list)
-
-# We test _get_default_tempdir by testing gettempdir.
+# We test _get_default_tempdir some more by testing gettempdir.
+
+class TestGetDefaultTempdir(TC):
+ """Test _get_default_tempdir()."""
+
+ def test_no_files_left_behind(self):
+ # use a private empty directory
+ with tempfile.TemporaryDirectory() as our_temp_directory:
+ # force _get_default_tempdir() to consider our empty directory
+ def our_candidate_list():
+ return [our_temp_directory]
+
+ with support.swap_attr(tempfile, "_candidate_tempdir_list",
+ our_candidate_list):
+ # verify our directory is empty after _get_default_tempdir()
+ tempfile._get_default_tempdir()
+ self.assertEqual(os.listdir(our_temp_directory), [])
+
+ def raise_OSError(*args, **kwargs):
+ raise OSError(-1)
+
+ with support.swap_attr(io, "open", raise_OSError):
+ # test again with failing io.open()
+ with self.assertRaises(IOError) as cm:
+ tempfile._get_default_tempdir()
+ self.assertEqual(cm.exception.args[0], errno.ENOENT)
+ self.assertEqual(os.listdir(our_temp_directory), [])
+
+ open = io.open
+ def bad_writer(*args, **kwargs):
+ fp = open(*args, **kwargs)
+ fp.write = raise_OSError
+ return fp
+
+ with support.swap_attr(io, "open", bad_writer):
+ # test again with failing write()
+ with self.assertRaises(IOError) as cm:
+ tempfile._get_default_tempdir()
+ self.assertEqual(cm.exception.errno, errno.ENOENT)
+ self.assertEqual(os.listdir(our_temp_directory), [])
+
+test_classes.append(TestGetDefaultTempdir)
class test__get_candidate_names(TC):