(original) (raw)

changeset: 100369:a1c125f21db4 branch: 3.5 parent: 100366:8f8e86ea3abb user: Martin Panter vadmium+py@gmail.com date: Sun Feb 28 05:22:20 2016 +0000 files: Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS description: Issue #26385: Cleanup NamedTemporaryFile if open() fails, by SilentGhost diff -r 8f8e86ea3abb -r a1c125f21db4 Lib/tempfile.py --- a/Lib/tempfile.py Sun Feb 28 21:13:23 2016 +0100 +++ b/Lib/tempfile.py Sun Feb 28 05:22:20 2016 +0000 @@ -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 -r 8f8e86ea3abb -r a1c125f21db4 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Sun Feb 28 21:13:23 2016 +0100 +++ b/Lib/test/test_tempfile.py Sun Feb 28 05:22:20 2016 +0000 @@ -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 -r 8f8e86ea3abb -r a1c125f21db4 Misc/NEWS --- a/Misc/NEWS Sun Feb 28 21:13:23 2016 +0100 +++ b/Misc/NEWS Sun Feb 28 05:22:20 2016 +0000 @@ -84,6 +84,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. /vadmium+py@gmail.com