]> granicus.if.org Git - python/commitdiff
Issue #26385: Cleanup NamedTemporaryFile if open() fails, by SilentGhost
authorMartin Panter <vadmium+py@gmail.com>
Sun, 28 Feb 2016 05:22:20 +0000 (05:22 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Sun, 28 Feb 2016 05:22:20 +0000 (05:22 +0000)
Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/NEWS

index c39820e198c19f7f62e87ea0af1ef4ad7ff87476..ad687b9a03d36669170bcd4a3d574965f15e69d2 100644 (file)
@@ -552,7 +552,8 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
                         newline=newline, encoding=encoding)
 
         return _TemporaryFileWrapper(file, name, delete)
-    except Exception:
+    except BaseException:
+        _os.unlink(name)
         _os.close(fd)
         raise
 
index 4a077cc4ca11038ad0bd7f7b78446c7f1efa3b69..51df1ecd7d18e63e0f927cde322087f007d3eee5 100644 (file)
@@ -948,8 +948,16 @@ class TestNamedTemporaryFile(BaseTestCase):
                 self.assertRaises(ValueError, tempfile.NamedTemporaryFile)
                 self.assertEqual(len(closed), 1)
 
-    # How to test the mode and bufsize parameters?
+    def test_bad_mode(self):
+        dir = tempfile.mkdtemp()
+        self.addCleanup(support.rmtree, dir)
+        with self.assertRaises(ValueError):
+            tempfile.NamedTemporaryFile(mode='wr', dir=dir)
+        with self.assertRaises(TypeError):
+            tempfile.NamedTemporaryFile(mode=2, dir=dir)
+        self.assertEqual(os.listdir(dir), [])
 
+    # How to test the mode and bufsize parameters?
 
 class TestSpooledTemporaryFile(BaseTestCase):
     """Test SpooledTemporaryFile()."""
index b895ff6c0d349c60b60f465569710080733f02d6..2904360595dffb9b741e533cd23ddba2b9871e03 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -84,6 +84,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #26385: Remove the file if the internal open() call in
+  NamedTemporaryFile() fails.  Patch by Silent Ghost.
+
 - Issue #26402: Fix XML-RPC client to retry when the server shuts down a
   persistent connection.  This was a regression related to the new
   http.client.RemoteDisconnected exception in 3.5.0a4.