Author xtreak
Recipients hmvp, xtreak
Date 2018-09-14.10:10:37
SpamBayes Score -1.0
Marked as misclassified Yes
Message-id <1536919837.66.0.956365154283.issue31177@psf.upfronthosting.co.za>
In-reply-to
Content
Can confirm this behavior on CPython master as well. It seems that when an attribute is deleted then a deleted flag is set for the attribute at https://github.com/python/cpython/blob/73820a60cc3c990abb351540ca27bf7689bce8ac/Lib/unittest/mock.py#L737 . But when reset_mock is called it doesn't check for the deleted flag at https://github.com/python/cpython/blob/73820a60cc3c990abb351540ca27bf7689bce8ac/Lib/unittest/mock.py#L543

➜  cpython git:(master) ./python.exe ../backups/bpo31177.py
Traceback (most recent call last):
  File "../backups/bpo31177.py", line 5, in <module>
    m.reset_mock()
  File "/Users/karthikeyansingaravelan/stuff/python/cpython/Lib/unittest/mock.py", line 546, in reset_mock
    child.reset_mock(visited)
AttributeError: '_SentinelObject' object has no attribute 'reset_mock'

A simple patch would be to skip the deleted as below but some of the code in mock module raise an AttributeError. I don't know the correct behavior here. But applying the below patch and running tests with `./python.exe Lib/unittest/test/` doesn't cause any test failure.

diff --git a/Lib/unittest/mock.py b/Lib/unittest/mock.py
index db1e642c00..700e2fb8b9 100644
--- a/Lib/unittest/mock.py
+++ b/Lib/unittest/mock.py
@@ -541,7 +541,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)
 
I will try to make a PR if it's ok.

Thanks
History
Date User Action Args
2018-09-14 10:10:37xtreaksetrecipients: + xtreak, hmvp
2018-09-14 10:10:37xtreaksetmessageid: <1536919837.66.0.956365154283.issue31177@psf.upfronthosting.co.za>
2018-09-14 10:10:37xtreaklinkissue31177 messages
2018-09-14 10:10:37xtreakcreate