(original) (raw)

changeset: 100368:5bfb4147405e branch: 2.7 parent: 100365:408891646a37 user: Martin Panter vadmium+py@gmail.com date: Mon Feb 29 00:31:38 2016 +0000 files: Lib/tempfile.py Lib/test/test_tempfile.py Misc/NEWS description: Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost diff -r 408891646a37 -r 5bfb4147405e Lib/tempfile.py --- a/Lib/tempfile.py Sun Feb 28 21:09:36 2016 +0100 +++ b/Lib/tempfile.py Mon Feb 29 00:31:38 2016 +0000 @@ -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 -r 408891646a37 -r 5bfb4147405e Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Sun Feb 28 21:09:36 2016 +0100 +++ b/Lib/test/test_tempfile.py Mon Feb 29 00:31:38 2016 +0000 @@ -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 -r 408891646a37 -r 5bfb4147405e Misc/NEWS --- a/Misc/NEWS Sun Feb 28 21:09:36 2016 +0100 +++ b/Misc/NEWS Mon Feb 29 00:31:38 2016 +0000 @@ -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. /vadmium+py@gmail.com