Skip to content
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

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

Merged
merged 3 commits into from
Apr 13, 2019

Conversation

tirkarthi
Copy link
Member

@tirkarthi tirkarthi commented Apr 11, 2019

This issue occurs only when the mock module import and code are executed under tracing. In order to test this I have tried patching sys.modules to remove unittest.mock in order to trigger an import under tracing. Though unittest.mock is deleted reference to unittest.mock.patch is held by two tests (test_patch_dict_test_prefix and test_patch_test_prefix) that change the TEST_PREFIX. Hence somehow there is a difference in the unittest.mock.patch object referred under these tests and inside mock module causing reference to unchanged TEST_PREFIX and hence test case failures. So I have kept a reference to old patch object and restored it in the end of this test to make sure everything is in sync.

https://bugs.python.org/issue36593

@tirkarthi tirkarthi changed the title bpo-36598: Fix isinstance check for Mock objects with spec executed under tracing bpo-36593: Fix isinstance check for Mock objects with spec executed under tracing Apr 11, 2019
Copy link
Member

@pablogsal pablogsal left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@tirkarthi
Copy link
Member Author

Thanks @pablogsal for the review :)

I think this shouldn't have failed and have created an issue for sys.settrace(trace) scenario where classes with __class__ defined should be set for amidst super() calls : https://bugs.python.org/issue36606 to see if this is a missed case as part of the original fix.

@miss-islington
Copy link
Contributor

Thanks @tirkarthi for the PR, and @pablogsal for merging it 🌮🎉.. I'm working now to backport this PR to: 3.7.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Apr 13, 2019
…nder tracing (pythonGH-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 .
(cherry picked from commit 830b43d)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
@bedevere-bot
Copy link

GH-12821 is a backport of this pull request to the 3.7 branch.

miss-islington added a commit that referenced this pull request Apr 13, 2019
…nder 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 .
(cherry picked from commit 830b43d)

Co-authored-by: Xtreak <tir.karthi@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants