diff options
author | Victor Stinner <victor.stinner@gmail.com> | 2014-03-25 08:18:04 (GMT) |
---|---|---|
committer | Victor Stinner <victor.stinner@gmail.com> | 2014-03-25 08:18:04 (GMT) |
commit | 1f99f9d5c2ebe35838e275b3b4d4bb96ab212def (patch) | |
tree | 4603f21b28fe3adfe66603b3d88922dfe7384515 /Lib/test/test_tempfile.py | |
parent | 93569c2b3d48b36df80635dbe3c02e3d5baa00d7 (diff) | |
download | cpython-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/test/test_tempfile.py')
-rw-r--r-- | Lib/test/test_tempfile.py | 24 |
1 files changed, 24 insertions, 0 deletions
diff --git a/Lib/test/test_tempfile.py b/Lib/test/test_tempfile.py index cf2ae08..fda914e 100644 --- a/Lib/test/test_tempfile.py +++ b/Lib/test/test_tempfile.py @@ -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: |