[Python-checkins] cpython (2.7): Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost

martin.panter python-checkins at python.org
Mon Feb 29 06:28:34 EST 2016


https://hg.python.org/cpython/rev/5bfb4147405e
changeset:   100368:5bfb4147405e
branch:      2.7
parent:      100365:408891646a37
user:        Martin Panter <vadmium+py at gmail.com>
date:        Mon Feb 29 00:31:38 2016 +0000
summary:
  Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost

files:
  Lib/tempfile.py           |  3 ++-
  Lib/test/test_tempfile.py |  7 +++++++
  Misc/NEWS                 |  3 +++
  3 files changed, 12 insertions(+), 1 deletions(-)


diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -476,7 +476,8 @@
     try:
         file = _os.fdopen(fd, mode, bufsize)
         return _TemporaryFileWrapper(file, name, delete)
-    except:
+    except BaseException:
+        _os.unlink(name)
         _os.close(fd)
         raise
 
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -827,6 +827,13 @@
             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)
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -55,6 +55,9 @@
 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.

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list