]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 1 Dec 2018 10:16:27 +0000 (02:16 -0800)
committerGitHub <noreply@github.com>
Sat, 1 Dec 2018 10:16:27 +0000 (02:16 -0800)
(cherry picked from commit edeca92c84a3b08902ecdfe987cde00c7e617887)

Co-authored-by: Xtreak <tirkarthi@users.noreply.github.com>
Lib/unittest/mock.py
Lib/unittest/test/testmock/testmock.py
Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst [new file with mode: 0644]

index 31b1985161616bc8bfbb1992ada423d55cbd6d14..4bb0c32c745bf839c918e6ce54acf7c934dc01c8 100644 (file)
@@ -543,7 +543,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)
 
index b64c8663d212266e7d9e40697b50ea52d8bcd845..4601136eff9c16e897d226527b6866da31294b6e 100644 (file)
@@ -1556,6 +1556,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 (file)
index 0000000..f385571
--- /dev/null
@@ -0,0 +1,2 @@
+Fix bug that prevented using :meth:`reset_mock <unittest.mock.Mock.reset_mock>`
+on mock instances with deleted attributes