summaryrefslogtreecommitdiffstats
path: root/Lib/tempfile.py
diff options
context:
space:
mode:
authorVictor Stinner <victor.stinner@gmail.com>2014-03-25 08:18:04 (GMT)
committerVictor Stinner <victor.stinner@gmail.com>2014-03-25 08:18:04 (GMT)
commit1f99f9d5c2ebe35838e275b3b4d4bb96ab212def (patch)
tree4603f21b28fe3adfe66603b3d88922dfe7384515 /Lib/tempfile.py
parent93569c2b3d48b36df80635dbe3c02e3d5baa00d7 (diff)
downloadcpython-1f99f9d5c2ebe35838e275b3b4d4bb96ab212def.zip
cpython-1f99f9d5c2ebe35838e275b3b4d4bb96ab212def.tar.gz
cpython-1f99f9d5c2ebe35838e275b3b4d4bb96ab212def.tar.bz2
Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
close the file descriptor if io.open() fails
Diffstat (limited to 'Lib/tempfile.py')
-rw-r--r--Lib/tempfile.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
index 57e8a82..f0e25fc 100644
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -458,10 +458,14 @@ def NamedTemporaryFile(mode='w+b', buffering=-1, encoding=None,
flags |= _os.O_TEMPORARY
(fd, name) = _mkstemp_inner(dir, prefix, suffix, flags)
- file = _io.open(fd, mode, buffering=buffering,
- newline=newline, encoding=encoding)
+ try:
+ file = _io.open(fd, mode, buffering=buffering,
+ newline=newline, encoding=encoding)
- return _TemporaryFileWrapper(file, name, delete)
+ return _TemporaryFileWrapper(file, name, delete)
+ except Exception:
+ _os.close(fd)
+ raise
if _os.name != 'posix' or _os.sys.platform == 'cygwin':
# On non-POSIX and Cygwin systems, assume that we cannot unlink a file