# HG changeset patch # Parent c62526580ff0040a7c9459f6e6fbf2a5511bc7b9 Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost diff -r c62526580ff0 Lib/tempfile.py --- a/Lib/tempfile.py Tue Feb 23 22:30:50 2016 +0000 +++ b/Lib/tempfile.py Wed Feb 24 02:17:14 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 c62526580ff0 Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Tue Feb 23 22:30:50 2016 +0000 +++ b/Lib/test/test_tempfile.py Wed Feb 24 02:17:14 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 c62526580ff0 Misc/NEWS --- a/Misc/NEWS Tue Feb 23 22:30:50 2016 +0000 +++ b/Misc/NEWS Wed Feb 24 02:17:14 2016 +0000 @@ -50,6 +50,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.