]> granicus.if.org Git - python/commitdiff
Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost
authorMartin Panter <vadmium+py@gmail.com>
Mon, 29 Feb 2016 00:31:38 +0000 (00:31 +0000)
committerMartin Panter <vadmium+py@gmail.com>
Mon, 29 Feb 2016 00:31:38 +0000 (00:31 +0000)
Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/NEWS

index 8a2e9cd875670ad03bdec5f0f4f1669895397171..7e3b25a07025f4555da84117851060dac0da22d3 100644 (file)
@@ -476,7 +476,8 @@ def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="",
     try:
         file = _os.fdopen(fd, mode, bufsize)
         return _TemporaryFileWrapper(file, name, delete)
-    except:
+    except BaseException:
+        _os.unlink(name)
         _os.close(fd)
         raise
 
index 3d0ac57a5b0581bb79983cd57ee9f472a5c0b444..078e4a9a2d06a92187055ffc8380f5e7634ff204 100644 (file)
@@ -827,6 +827,13 @@ class test_NamedTemporaryFile(TC):
             os.close = old_close
             os.fdopen = old_fdopen
 
+    def test_bad_mode(self):
+        dir = tempfile.mkdtemp()
+        self.addCleanup(support.rmtree, dir)
+        with self.assertRaises(TypeError):
+            tempfile.NamedTemporaryFile(mode=(), dir=dir)
+        self.assertEqual(os.listdir(dir), [])
+
     # How to test the mode and bufsize parameters?
 
 test_classes.append(test_NamedTemporaryFile)
index 012f7d70adffd0c44342537e0fff37e835067c48..ac564b771796413db844d19c298e7980c7799b69 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -55,6 +55,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #26385: Remove the file if the internal fdopen() call in
+  NamedTemporaryFile() fails.  Based on patch by Silent Ghost.
+
 - Issue #26309: In the "socketserver" module, shut down the request (closing
   the connected socket) when verify_request() returns false.  Based on patch
   by Aviv Palivoda.