From: Martin Panter Date: Sun, 28 Feb 2016 05:22:20 +0000 (+0000) Subject: Issue #26385: Cleanup NamedTemporaryFile if open() fails, by SilentGhost X-Git-Tag: v3.6.0a1~553^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=7869a227795da08841f8139b69b7d4521b12e184;p=thirdparty%2FPython%2Fcpython.git Issue #26385: Cleanup NamedTemporaryFile if open() fails, by SilentGhost --- diff --git a/Lib/tempfile.py b/Lib/tempfile.py index c39820e198c1..ad687b9a03d3 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -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 diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 4a077cc4ca11..51df1ecd7d18 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -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().""" diff --git a/Misc/NEWS b/Misc/NEWS index b895ff6c0d34..2904360595df 100644 --- 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.