diff -r c5fbacf79b1e Lib/tempfile.py --- a/Lib/tempfile.py Sat Jan 30 12:34:12 2016 +0200 +++ b/Lib/tempfile.py Thu Feb 18 19:46:45 2016 +0100 @@ -554,6 +554,7 @@ return _TemporaryFileWrapper(file, name, delete) except Exception: _os.close(fd) + _os.unlink(name) raise if _os.name != 'posix' or _os.sys.platform == 'cygwin': diff -r c5fbacf79b1e Lib/test/test_tempfile.py --- a/Lib/test/test_tempfile.py Sat Jan 30 12:34:12 2016 +0200 +++ b/Lib/test/test_tempfile.py Thu Feb 18 19:46:45 2016 +0100 @@ -9,6 +9,7 @@ import warnings import contextlib import weakref +import glob from unittest import mock import unittest @@ -948,9 +949,17 @@ self.assertRaises(ValueError, tempfile.NamedTemporaryFile) self.assertEqual(len(closed), 1) + def test_bad_mode(self): + dir = tempfile.mkdtemp() + try: + tempfile.NamedTemporaryFile(mode='wr', dir=dir) + except ValueError: + self.assertEqual(os.listdir(dir), []) + finally: + support.rmtree(dir) + # How to test the mode and bufsize parameters? - class TestSpooledTemporaryFile(BaseTestCase): """Test SpooledTemporaryFile()."""