[Python-checkins] cpython (merge 3.5 -> default): Issue #26385: Merge NamedTemporaryFile fix from 3.5

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


https://hg.python.org/cpython/rev/865cf8eba51a
changeset:   100370:865cf8eba51a
parent:      100367:83814cdca928
parent:      100369:a1c125f21db4
user:        Martin Panter <vadmium+py at gmail.com>
date:        Mon Feb 29 11:25:09 2016 +0000
summary:
  Issue #26385: Merge NamedTemporaryFile fix from 3.5

files:
  Lib/tempfile.py           |   3 ++-
  Lib/test/test_tempfile.py |  10 +++++++++-
  Misc/NEWS                 |   3 +++
  3 files changed, 14 insertions(+), 2 deletions(-)


diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -552,7 +552,8 @@
                         newline=newline, encoding=encoding)
 
         return _TemporaryFileWrapper(file, name, delete)
-    except Exception:
+    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
@@ -948,9 +948,17 @@
                 self.assertRaises(ValueError, tempfile.NamedTemporaryFile)
                 self.assertEqual(len(closed), 1)
 
+    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()."""
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -194,6 +194,9 @@
 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.

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


More information about the Python-checkins mailing list