diff --git a/Lib/test/test_unittest/testmock/testthreadingmock.py b/Lib/test/test_unittest/testmock/testthreadingmock.py index dda4916434ec1b4..e0225bc93f49569 100644 --- a/Lib/test/test_unittest/testmock/testthreadingmock.py +++ b/Lib/test/test_unittest/testmock/testthreadingmock.py @@ -70,6 +70,22 @@ def test_instance_check(self): self.assertIsInstance(something.method_1, ThreadingMock) self.assertIsInstance(something.method_1().method_2(), ThreadingMock) + def test_wait_after_raising_side_effect(self): + mock = self._make_mock(side_effect=ValueError, timeout=0) + with self.assertRaises(ValueError): + mock(1, key=2) + mock.wait_until_called() + mock.wait_until_any_call_with(1, key=2) + + def test_wait_during_side_effect(self): + def side_effect(*args, **kwargs): + mock.wait_until_called() + mock.wait_until_any_call_with(*args, **kwargs) + return 42 + + mock = self._make_mock(side_effect=side_effect, timeout=0) + self.assertEqual(mock(1, key=2), 42) + def test_dynamic_child_mocks_are_threading_mocks(self): waitable_mock = self._make_mock() self.assertIsInstance(waitable_mock.child, ThreadingMock) diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index d3d47ff81e85969..fc942b633695666 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -3107,14 +3107,12 @@ def __get_event(self, expected_args, expected_kwargs): return new_event def _mock_call(self, *args, **kwargs): - ret_value = super()._mock_call(*args, **kwargs) - call_event = self.__get_event(args, kwargs) call_event.set() self._mock_event.set() - return ret_value + return super()._mock_call(*args, **kwargs) def _increment_mock_call(self, /, *args, **kwargs): with self._mock_calls_events_lock: diff --git a/Misc/NEWS.d/next/Library/2026-10-13-20-30-00.gh-issue-159344.mOcKwa.rst b/Misc/NEWS.d/next/Library/2026-10-13-20-30-00.gh-issue-159344.mOcKwa.rst new file mode 100644 index 000000000000000..3b42094cfa4e0a1 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-10-13-20-30-00.gh-issue-159344.mOcKwa.rst @@ -0,0 +1,2 @@ +Fix :class:`unittest.mock.ThreadingMock` to notify waiting threads when a +call is recorded, including when its side effect raises an exception or blocks.