Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 830b43d

Browse files
tirkarthipablogsal
authored andcommitted
bpo-36593: Fix isinstance check for Mock objects with spec executed under tracing (GH-12790)
In Python having a trace function in effect while mock is imported causes isinstance to be wrong for MagicMocks. This is due to the usage of super() in some class methods, as this sets the __class__ attribute. To avoid this, as a workaround, alias the usage of super .
1 parent fde9b33 commit 830b43d

File tree

3 files changed

+41
-1
lines changed

3 files changed

+41
-1
lines changed

Lib/unittest/mock.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -739,7 +739,7 @@ def __delattr__(self, name):
739739

740740
obj = self._mock_children.get(name, _missing)
741741
if name in self.__dict__:
742-
super().__delattr__(name)
742+
_safe_super(NonCallableMock, self).__delattr__(name)
743743
elif obj is _deleted:
744744
raise AttributeError(name)
745745
if obj is not _missing:

Lib/unittest/test/testmock/testmock.py

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1847,6 +1847,44 @@ def foo(a, b):
18471847
self.assertRaises(TypeError, mock.child, 1)
18481848
self.assertEqual(mock.mock_calls, [call.child(1, 2)])
18491849

1850+
def test_isinstance_under_settrace(self):
1851+
# bpo-36593 : __class__ is not set for a class that has __class__
1852+
# property defined when it's used with sys.settrace(trace) set.
1853+
# Delete the module to force reimport with tracing function set
1854+
# restore the old reference later since there are other tests that are
1855+
# dependent on unittest.mock.patch. In testpatch.PatchTest
1856+
# test_patch_dict_test_prefix and test_patch_test_prefix not restoring
1857+
# causes the objects patched to go out of sync
1858+
1859+
old_patch = unittest.mock.patch
1860+
1861+
# Directly using __setattr__ on unittest.mock causes current imported
1862+
# reference to be updated. Use a lambda so that during cleanup the
1863+
# re-imported new reference is updated.
1864+
self.addCleanup(lambda patch: setattr(unittest.mock, 'patch', patch),
1865+
old_patch)
1866+
1867+
with patch.dict('sys.modules'):
1868+
del sys.modules['unittest.mock']
1869+
1870+
def trace(frame, event, arg):
1871+
return trace
1872+
1873+
sys.settrace(trace)
1874+
self.addCleanup(sys.settrace, None)
1875+
1876+
from unittest.mock import (
1877+
Mock, MagicMock, NonCallableMock, NonCallableMagicMock
1878+
)
1879+
1880+
mocks = [
1881+
Mock, MagicMock, NonCallableMock, NonCallableMagicMock
1882+
]
1883+
1884+
for mock in mocks:
1885+
obj = mock(spec=Something)
1886+
self.assertIsInstance(obj, Something)
1887+
18501888

18511889
if __name__ == '__main__':
18521890
unittest.main()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix ``isinstance`` check for Mock objects with spec when the code is
2+
executed under tracing. Patch by Karthikeyan Singaravelan.

0 commit comments

Comments
 (0)