]> granicus.if.org Git - python/commitdiff
Issue #21058: Fix a leak of file descriptor in tempfile.NamedTemporaryFile(),
authorVictor Stinner <victor.stinner@gmail.com>
Tue, 25 Mar 2014 08:18:04 +0000 (09:18 +0100)
committerVictor Stinner <victor.stinner@gmail.com>
Tue, 25 Mar 2014 08:18:04 +0000 (09:18 +0100)
close the file descriptor if io.open() fails

Lib/tempfile.py
Lib/test/test_tempfile.py
Misc/NEWS

index 57e8a829ec00d93b4ca518066628816d7d341329..f0e25fc2d5492f42ad9e75bbc585dd62790458b4 100644 (file)
@@ -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
index cf2ae080bed9269d4cb4c40d27e964fefb779988..fda914e2a3c7c6a3cb982c5ccfd07a32b80426b8 100644 (file)
@@ -9,6 +9,7 @@ import re
 import warnings
 import contextlib
 import weakref
+from unittest import mock
 
 import unittest
 from test import support, script_helper
@@ -758,6 +759,17 @@ class TestNamedTemporaryFile(BaseTestCase):
                 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 @@ if tempfile.NamedTemporaryFile is not tempfile.TemporaryFile:
             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:
index d1fbd4b06a1672d24f9f79c25e87edd4d890a9ed..2a7cd3bf9e233d664cfdbe5b2096f0f6b926d1c2 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -24,6 +24,10 @@ Core and Builtins
 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.