|
| 1 | +import io |
1 | 2 | import os
|
2 | 3 | import sys
|
3 | 4 | import textwrap
|
4 | 5 | import unittest
|
| 6 | +import types |
5 | 7 | from subprocess import Popen, PIPE
|
6 | 8 | from test import support
|
7 | 9 | from test.support.script_helper import assert_python_ok, assert_python_failure
|
| 10 | +from unittest import mock |
8 | 11 |
|
9 | 12 |
|
10 | 13 | class TestTool(unittest.TestCase):
|
@@ -121,3 +124,55 @@ def test_sort_keys_flag(self):
|
121 | 124 | self.assertEqual(out.splitlines(),
|
122 | 125 | self.expect_without_sort_keys.encode().splitlines())
|
123 | 126 | self.assertEqual(err, b'')
|
| 127 | + |
| 128 | + def test_no_fd_leak_infile_outfile(self): |
| 129 | + closed = [] |
| 130 | + opened = [] |
| 131 | + io_open = io.open |
| 132 | + |
| 133 | + def open(*args, **kwargs): |
| 134 | + fd = io_open(*args, **kwargs) |
| 135 | + opened.append(fd) |
| 136 | + fd_close = fd.close |
| 137 | + def close(self): |
| 138 | + closed.append(self) |
| 139 | + fd_close() |
| 140 | + fd.close = types.MethodType(close, fd) |
| 141 | + return fd |
| 142 | + |
| 143 | + infile = self._create_infile() |
| 144 | + with mock.patch('builtins.open', side_effect=open): |
| 145 | + with mock.patch.object(sys, 'argv', ['tool.py', infile, infile + '.out']): |
| 146 | + import json.tool |
| 147 | + json.tool.main() |
| 148 | + |
| 149 | + os.unlink(infile + '.out') |
| 150 | + self.assertEqual(opened, closed) |
| 151 | + self.assertEqual(len(opened), len(closed)) |
| 152 | + |
| 153 | + def test_no_fd_leak_same_infile_outfile(self): |
| 154 | + closed = [] |
| 155 | + opened = [] |
| 156 | + io_open = io.open |
| 157 | + |
| 158 | + def open(*args, **kwargs): |
| 159 | + fd = io_open(*args, **kwargs) |
| 160 | + opened.append(fd) |
| 161 | + fd_close = fd.close |
| 162 | + def close(self): |
| 163 | + closed.append(self) |
| 164 | + fd_close() |
| 165 | + fd.close = types.MethodType(close, fd) |
| 166 | + return fd |
| 167 | + |
| 168 | + infile = self._create_infile() |
| 169 | + with mock.patch('builtins.open', side_effect=open): |
| 170 | + with mock.patch.object(sys, 'argv', ['tool.py', infile, infile]): |
| 171 | + try: |
| 172 | + import json.tool |
| 173 | + json.tool.main() |
| 174 | + except SystemExit: # We expect SystemExit to happen on c9d43c |
| 175 | + pass |
| 176 | + |
| 177 | + self.assertEqual(opened, closed) |
| 178 | + self.assertEqual(len(opened), len(closed)) |
0 commit comments