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

Skip to content

bpo-36593: Fix isinstance check for Mock objects with spec executed under tracing #12790

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 13, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Lib/unittest/mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -739,7 +739,7 @@ def __delattr__(self, name):

obj = self._mock_children.get(name, _missing)
if name in self.__dict__:
super().__delattr__(name)
_safe_super(NonCallableMock, self).__delattr__(name)
elif obj is _deleted:
raise AttributeError(name)
if obj is not _missing:
Expand Down
38 changes: 38 additions & 0 deletions Lib/unittest/test/testmock/testmock.py
Original file line number Diff line number Diff line change
Expand Up @@ -1847,6 +1847,44 @@ def foo(a, b):
self.assertRaises(TypeError, mock.child, 1)
self.assertEqual(mock.mock_calls, [call.child(1, 2)])

def test_isinstance_under_settrace(self):
# bpo-36593 : __class__ is not set for a class that has __class__
# property defined when it's used with sys.settrace(trace) set.
# Delete the module to force reimport with tracing function set
# restore the old reference later since there are other tests that are
# dependent on unittest.mock.patch. In testpatch.PatchTest
# test_patch_dict_test_prefix and test_patch_test_prefix not restoring
# causes the objects patched to go out of sync

old_patch = unittest.mock.patch

# Directly using __setattr__ on unittest.mock causes current imported
# reference to be updated. Use a lambda so that during cleanup the
# re-imported new reference is updated.
self.addCleanup(lambda patch: setattr(unittest.mock, 'patch', patch),
old_patch)

with patch.dict('sys.modules'):
del sys.modules['unittest.mock']

def trace(frame, event, arg):
return trace

sys.settrace(trace)
self.addCleanup(sys.settrace, None)

from unittest.mock import (
Mock, MagicMock, NonCallableMock, NonCallableMagicMock
)

mocks = [
Mock, MagicMock, NonCallableMock, NonCallableMagicMock
]

for mock in mocks:
obj = mock(spec=Something)
self.assertIsInstance(obj, Something)


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