Issue #26385: Cleanup NamedTemporaryFile if fdopen() fails, by SilentGhost
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 @@
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 @@
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)