[Python-checkins] cpython (merge 3.4 -> default): (Merge 3.4) Issue #21058: Fix a leak of file descriptor in
victor.stinner
python-checkins at python.org
Tue Mar 25 09:19:55 CET 2014
http://hg.python.org/cpython/rev/b1e3035216f8
changeset: 89959:b1e3035216f8
parent: 89954:f7a40517f0ac
parent: 89958:7f87c26f59ab
user: Victor Stinner <victor.stinner at gmail.com>
date: Tue Mar 25 09:19:14 2014 +0100
summary:
(Merge 3.4) Issue #21058: Fix a leak of file descriptor in
tempfile.NamedTemporaryFile(), close the file descriptor if io.open() fails
files:
Lib/tempfile.py | 10 +++++++---
Lib/test/test_tempfile.py | 24 ++++++++++++++++++++++++
Misc/NEWS | 4 ++++
3 files changed, 35 insertions(+), 3 deletions(-)
diff --git a/Lib/tempfile.py b/Lib/tempfile.py
--- a/Lib/tempfile.py
+++ b/Lib/tempfile.py
@@ -458,10 +458,14 @@
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
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py
--- a/Lib/test/test_tempfile.py
+++ b/Lib/test/test_tempfile.py
@@ -9,6 +9,7 @@
import warnings
import contextlib
import weakref
+from unittest import mock
import unittest
from test import support, script_helper
@@ -758,6 +759,17 @@
pass
self.assertRaises(ValueError, use_closed)
+ def test_no_leak_fd(self):
+ # Issue #21058: don't leak file descriptor when io.pen() fails
+ closed = []
+ def close(fd):
+ closed.append(fd)
+
+ with mock.patch('os.close', side_effect=close):
+ with mock.patch('io.open', side_effect=ValueError):
+ self.assertRaises(ValueError, tempfile.NamedTemporaryFile)
+ self.assertEqual(len(closed), 1)
+
# How to test the mode and bufsize parameters?
@@ -1061,6 +1073,18 @@
roundtrip("\u039B", "w+", encoding="utf-16")
roundtrip("foo\r\n", "w+", newline="")
+ def test_no_leak_fd(self):
+ # Issue #21058: don't leak file descriptor when io.open() fails
+ closed = []
+ def close(fd):
+ closed.append(fd)
+
+ with mock.patch('os.close', side_effect=close):
+ with mock.patch('io.open', side_effect=ValueError):
+ self.assertRaises(ValueError, tempfile.TemporaryFile)
+ self.assertEqual(len(closed), 1)
+
+
# Helper for test_del_on_shutdown
class NulledModules:
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -26,6 +26,10 @@
Library
-------
+- Issue #21058: Fix a leak of file descriptor in
+ :func:`tempfile.NamedTemporaryFile`, close the file descriptor if
+ :func:`io.open` fails
+
- Issue #21013: Enhance ssl.create_default_context() when used for server side
sockets to provide better security by default.
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list