]> granicus.if.org Git - python/commitdiff
Issue #16800: tempfile.gettempdir() no longer left temporary files when
authorSerhiy Storchaka <storchaka@gmail.com>
Tue, 12 Feb 2013 22:35:30 +0000 (00:35 +0200)
committerSerhiy Storchaka <storchaka@gmail.com>
Tue, 12 Feb 2013 22:35:30 +0000 (00:35 +0200)
the disk is full.  Original patch by Amir Szekely.

Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/ACKS
Misc/NEWS

index ae35314bd6ba06066b5ff311af78d78448f6fd77..0dbd889609d317b1d6cd8e02bcf2b9f3630f37a4 100644 (file)
@@ -175,11 +175,14 @@ def _get_default_tempdir():
             filename = _os.path.join(dir, name)
             try:
                 fd = _os.open(filename, _bin_openflags, 0o600)
-                fp = _io.open(fd, 'wb')
-                fp.write(b'blat')
-                fp.close()
-                _os.unlink(filename)
-                del fp, fd
+                try:
+                    try:
+                        fp = _io.open(fd, 'wb', buffering=0, closefd=False)
+                        fp.write(b'blat')
+                    finally:
+                        _os.close(fd)
+                finally:
+                    _os.unlink(filename)
                 return dir
             except (OSError, IOError) as e:
                 if e.args[0] != _errno.EEXIST:
index a90cd0ef0ff1f927a7f1473cbd678ed107d38ec0..cf8b35ac80e0f2b93dd9b161077a589f61cf1101 100644 (file)
@@ -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):
index 7ed6b317ec71cc63f8252b36d620070afe3ce14d..1ea66e4ebc526742163b3fbb0391279e7ac8ecf7 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -1051,6 +1051,7 @@ Andrew Svetlov
 Paul Swartz
 Thenault Sylvain
 Péter Szabó
+Amir Szekely
 Arfrever Frehtes Taifersar Arahesis
 Neil Tallim
 Geoff Talvola
index a341bf43b3eab786a760a55f35aa75f1b98be696..4b22d9b31bed2a4a699ad46776b2e911c4aa0213 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -221,6 +221,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #16800: tempfile.gettempdir() no longer left temporary files when
+  the disk is full.  Original patch by Amir Szekely.
+
 - Issue #16564: Fixed regression relative to Python2 in the operation of
   email.encoders.encode_7or8bit when used with binary data.