From edeca92c84a3b08902ecdfe987cde00c7e617887 Mon Sep 17 00:00:00 2001 From: Xtreak Date: Sat, 1 Dec 2018 15:33:54 +0530 Subject: bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302) --- Lib/unittest/mock.py | 2 +- Lib/unittest/test/testmock/testmock.py | 10 ++++++++++ .../next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst | 2 ++ 3 files changed, 13 insertions(+), 1 deletion(-) create mode 100644 Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py index a9c82dc..9547b1a 100644 --- a/Lib/unittest/mock.py +++ b/Lib/unittest/mock.py @@ -542,7 +542,7 @@ class NonCallableMock(Base): self._mock_side_effect = None for child in self._mock_children.values(): - if isinstance(child, _SpecState): + if isinstance(child, _SpecState) or child is _deleted: continue child.reset_mock(visited) diff --git a/Lib/unittest/test/testmock/testmock.py b/Lib/unittest/test/testmock/testmock.py index 8cd284a..ac6eea3 100644 --- a/Lib/unittest/test/testmock/testmock.py +++ b/Lib/unittest/test/testmock/testmock.py @@ -1596,6 +1596,16 @@ class MockTest(unittest.TestCase): self.assertRaises(AttributeError, getattr, mock, 'f') + def test_reset_mock_does_not_raise_on_attr_deletion(self): + # bpo-31177: reset_mock should not raise AttributeError when attributes + # were deleted in a mock instance + mock = Mock() + mock.child = True + del mock.child + mock.reset_mock() + self.assertFalse(hasattr(mock, 'child')) + + def test_class_assignable(self): for mock in Mock(), MagicMock(): self.assertNotIsInstance(mock, int) diff --git a/Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst b/Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst new file mode 100644 index 0000000..f385571 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst @@ -0,0 +1,2 @@ +Fix bug that prevented using :meth:`reset_mock ` +on mock instances with deleted attributes -- cgit v0.12