diff options
author | Martin Panter <vadmium+py@gmail.com> | 2016-02-29 00:31:38 (GMT) |
---|---|---|
committer | Martin Panter <vadmium+py@gmail.com> | 2016-02-29 00:31:38 (GMT) |
commit | b6b1ab4fa83c000f14bd63c92d9182fbd507d0be (patch) | |
tree | 9daa8c046428ef7caf829cf3898227a069686f55 /Lib | |
parent | 97b6e0bd1b1305f3273b1de9d03f238dea161951 (diff) | |
download | cpython-b6b1ab4fa83c000f14bd63c92d9182fbd507d0be.zip cpython-b6b1ab4fa83c000f14bd63c92d9182fbd507d0be.tar.gz cpython-b6b1ab4fa83c000f14bd63c92d9182fbd507d0be.tar.bz2 |
Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/tempfile.py | 3 | ||||
-rw-r--r-- | Lib/test/test_tempfile.py | 7 |
2 files changed, 9 insertions, 1 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py index 8a2e9cd..7e3b25a 100644 --- a/Lib/tempfile.py +++ b/Lib/tempfile.py @@ -476,7 +476,8 @@ def NamedTemporaryFile(mode='w+b', bufsize=-1, suffix="", try: file = _os.fdopen(fd, mode, bufsize) return _TemporaryFileWrapper(file, name, delete) - except: + except BaseException: + _os.unlink(name) _os.close(fd) raise diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index 3d0ac57..078e4a9 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -827,6 +827,13 @@ class test_NamedTemporaryFile(TC): 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) |