summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMartin Panter <vadmium+py@gmail.com>2016-02-28 05:22:20 (GMT)
committerMartin Panter <vadmium+py@gmail.com>2016-02-28 05:22:20 (GMT)
commit7869a227795da08841f8139b69b7d4521b12e184 (patch)
treefa6da1d13f8e13379f3914e328cf25a10e5f5eac /Lib
parent96421d6f1136292abfe8748903f11231fba6cb16 (diff)
downloadcpython-7869a227795da08841f8139b69b7d4521b12e184.zip
cpython-7869a227795da08841f8139b69b7d4521b12e184.tar.gz
cpython-7869a227795da08841f8139b69b7d4521b12e184.tar.bz2
Issue #26385: Cleanup NamedTemporaryFile if open() fails, by SilentGhost
Diffstat (limited to 'Lib')
-rw-r--r--Lib/tempfile.py3
-rw-r--r--Lib/test/test_tempfile.py10
2 files changed, 11 insertions, 2 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index c39820e..ad687b9 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -552,7 +552,8 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
newline=newline, encoding=encoding)
return _TemporaryFileWrapper(file, name, delete)
- except Exception:
+ except BaseException:
+ _os.unlink(name)
_os.close(fd)
raise
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
index 4a077cc..51df1ec 100644
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -948,8 +948,16 @@ class TestNamedTemporaryFile(BaseTestCase):
self.assertRaises(ValueError, tempfile.NamedTemporaryFile)
self.assertEqual(len(closed), 1)
- # How to test the mode and bufsize parameters?
+ def test_bad_mode(self):
+ dir = tempfile.mkdtemp()
+ self.addCleanup(support.rmtree, dir)
+ with self.assertRaises(ValueError):
+ tempfile.NamedTemporaryFile(mode='wr', dir=dir)
+ with self.assertRaises(TypeError):
+ tempfile.NamedTemporaryFile(mode=2, dir=dir)
+ self.assertEqual(os.listdir(dir), [])
+ # How to test the mode and bufsize parameters?
class TestSpooledTemporaryFile(BaseTestCase):
"""Test SpooledTemporaryFile()."""