FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

fix unittest.mock: make mock_open.__exit__ accept variable args (gh-150484) by hahahuy · Pull Request #150535 · python/cpython · GitHub

Repository navigation

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .py  (2) .rst  (1) All 2 file types selected
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
10 changes: 10 additions & 0 deletions Lib/test/test_unittest/testmock/testmock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -2108,6 +2108,16 @@ def test_mock_open_after_eof(self):
self.assertEqual([], h.readlines())
self.assertEqual([], h.readlines())

def test_mock_open_exit_stack_issue_150484(self):
# Regression test for gh#150484
# mock_open().__exit__ must accept 4 args (ExitStack passes
# exc_info tuple) while previously it only accepted 3.
from contextlib import ExitStack
with mock.patch('builtins.open', mock.mock_open()):
with mock.mock_open() as m:
with ExitStack() as exit_stack:
exit_stack.enter_context(open('/tmp/test.txt', 'w'))

def test_mock_parents(self):
for Klass in Mock, MagicMock:
m = Klass()
Expand Down
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
Expand Up @@ -3002,7 +3002,7 @@ def _next_side_effect():
return handle.readline.return_value
return next(_state[0])

def _exit_side_effect(exctype, excinst, exctb):
def _exit_side_effect(*exc_details):
handle.close()

global file_spec
Expand Down
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode characters
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Fix :meth:`unittest.mock.mock_open.__exit__` to accept variable arguments
(``*exc_details``) to support Python 3.13+ where ``ExitStack.__exit__``
passes the full exc_info tuple.
Loading

Back | FazBrowse Home | New Git URL