[Python-checkins] bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)

Miss Islington (bot) webhook-mailer at python.org
Sat Dec 1 05:24:53 EST 2018


https://github.com/python/cpython/commit/422c1658b7d34fdc73c5fc895b135862103d1983
commit: 422c1658b7d34fdc73c5fc895b135862103d1983
branch: 3.7
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2018-12-01T02:24:47-08:00
summary:

bpo-31177: Skip deleted attributes while calling reset_mock (GH-9302)

(cherry picked from commit edeca92c84a3b08902ecdfe987cde00c7e617887)

Co-authored-by: Xtreak <tirkarthi at users.noreply.github.com>

files:
A Misc/NEWS.d/next/Library/2018-09-14-10-38-18.bpo-31177.Sv91TN.rst
M Lib/unittest/mock.py
M Lib/unittest/test/testmock/testmock.py

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index b0c03c03bd6f..6ba186fb6631 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -541,7 +541,7 @@ def reset_mock(self,  visited=None,*, return_value=False, side_effect=False):
             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 c7bfa277b511..cc45f02409ef 100644
--- a/Lib/unittest/test/testmock/testmock.py
+++ b/Lib/unittest/test/testmock/testmock.py
@@ -1566,6 +1566,16 @@ def test_attribute_deletion(self):
             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 000000000000..f385571e99cc
--- /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 <unittest.mock.Mock.reset_mock>`
+on mock instances with deleted attributes



More information about the Python-checkins mailing list