Thanks to visit codestin.com Credit goes to github.com
We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
There was an error while loading. Please reload this page.
1 parent 18d16e9 commit 3f7c081Copy full SHA for 3f7c081
3 files changed
Lib/test/test_unittest/testmock/testwith.py
@@ -158,7 +158,7 @@ def test_mock_open_context_manager(self):
158
f.read()
159
160
expected_calls = [call('foo'), call().__enter__(), call().read(),
161
- call().__exit__(None, None, None)]
+ call().__exit__(None, None, None), call().close()]
162
self.assertEqual(mock.mock_calls, expected_calls)
163
self.assertIs(f, handle)
164
@@ -172,9 +172,9 @@ def test_mock_open_context_manager_multiple_times(self):
172
173
expected_calls = [
174
call('foo'), call().__enter__(), call().read(),
175
- call().__exit__(None, None, None),
+ call().__exit__(None, None, None), call().close(),
176
call('bar'), call().__enter__(), call().read(),
177
178
179
180
def test_explicit_mock(self):
Lib/unittest/mock.py
@@ -2941,6 +2941,9 @@ def _next_side_effect():
2941
return handle.readline.return_value
2942
return next(_state[0])
2943
2944
+ def _exit_side_effect(exctype, excinst, exctb):
2945
+ handle.close()
2946
+
2947
global file_spec
2948
if file_spec is None:
2949
import _io
@@ -2967,6 +2970,7 @@ def _next_side_effect():
2967
2970
handle.readlines.side_effect = _readlines_side_effect
2968
2971
handle.__iter__.side_effect = _iter_side_effect
2969
2972
handle.__next__.side_effect = _next_side_effect
2973
+ handle.__exit__.side_effect = _exit_side_effect
2974
2975
def reset_data(*args, **kwargs):
2976
_state[0] = _to_stream(read_data)
Misc/NEWS.d/next/Library/2021-06-24-20-45-03.bpo-44185.ZHb8yJ.rst
@@ -0,0 +1,3 @@
1
+:func:`unittest.mock.mock_open` will call the :func:`close` method of the file
2
+handle mock when it is exiting from the context manager.
3
+Patch by Samet Yaslan.
0 commit comments