Skip to content

bpo-33747: Avoid mutating the global sys.modules dict #8520

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

Conversation

onyb
Copy link
Contributor

@onyb onyb commented Jul 28, 2018

The test method test_patch_imports_lazily was injecting a mocked module
to the sys.modules dict, which was reused in another test module called
test_patch_propogrates_exc_on_exit. Hence, the other test module failed
whenever it was launched independently.

Implemented a context manager to make sure modifications to sys.modules
are not persisted after the test methods finish execution.

https://bugs.python.org/issue33747

The test method test_patch_imports_lazily was injecting a mocked module
to the sys.modules dict, which was reused in another test module called
test_patch_propogrates_exc_on_exit. Hence, the other test module failed
whenever it was launched independently.

Implemented a context manager to make sure modifications to sys.modules
are not persisted after the test methods finish execution.
from unittest.mock import (
NonCallableMock, CallableMixin, sentinel,
MagicMock, Mock, NonCallableMagicMock, patch, _patch,
DEFAULT, call, _get_target
)

from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
Copy link
Member

Choose a reason for hiding this comment

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

Why are these lines changed?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I was rearranging the imports. Just pushed a commit to revert them.

from unittest.mock import (
NonCallableMock, CallableMixin, sentinel,
MagicMock, Mock, NonCallableMagicMock, patch, _patch,
DEFAULT, call, _get_target
)

from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
Copy link
Member

Choose a reason for hiding this comment

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

Could you avoid rearranging the imports? It makes it hard to see what is being changed.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry. I just fixed it in a recent commit.

with swap_attr(sys, 'modules', sys.modules.copy()):
squizz = Mock()
sys.modules['squizz'] = squizz
yield squizz
Copy link
Member

Choose a reason for hiding this comment

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

What exactly the method does is not very clear from the name.
There are two places that need to patch in a mocked squizz module, but it seems to me that moving this into a method is too much indirection: the testcases would be much easier to read if they included the three lines directly even though it means a little code duplication:

    with swap_attr(sys, 'modules', sys.modules.copy()):
        squizz = Mock()
        sys.modules['squizz'] = squizz
        ...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

p1.start()
p1.stop()
self.assertEqual(squizz.squozz, 3)
with swap_attr(sys, 'modules', sys.modules.copy()):
Copy link
Member

Choose a reason for hiding this comment

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

I have doubts about changing the reference to sys.modules. it can be cached in some places. It is safer to change sys.modules in-place. See for example sys_modules_context in Lib/test/test_importlib/test_namespace_pkgs.py. There are several other helpers in modules test.support and test.test_importlib.util.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Okay with your suggestion, but could you please explain to me what kind of risk is posed by a change in reference?

I like the sys_modules_context helper more. Where do you think I should move this function to, before using it in the tests?

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if @brettcannon or any of the other importlib maintainers expect the tests to be standalone, but if not then you could move sys_modules_context into test.support. Alternatively, it could be copied into test.support and leave the importlib tests using their own copy.

Copy link
Member

Choose a reason for hiding this comment

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

Okay with your suggestion, but could you please explain to me what kind of risk is posed by a change in reference?

I don't remember details (and these details were changed in recent releases), but the comment to sys_modules_context implies that there is such risk.

Where do you think I should move this function to, before using it in the tests?

You can just import it.

from test.test_importlib.test_namespace_pkgs import sys_modules_context

There are many import-related helpers scattered through different files, and I think that once we should merge and standardize them, but this is a different issue.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks. I have made the modifications.

@onyb
Copy link
Contributor Author

onyb commented Jul 30, 2018

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

: please review the changes made to this pull request.

Copy link
Member

@brettcannon brettcannon left a comment

Choose a reason for hiding this comment

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

Minor swap to another context manager which is supported for use by other test modules.

@@ -9,6 +9,7 @@
from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance

from test.test_importlib.test_namespace_pkgs import sys_modules_context
Copy link
Member

Choose a reason for hiding this comment

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

I know that @serhiy-storchaka recommended using this context manager, but since it isn't in test.test_importlib.util then I wouldn't consider it stable. Instead, I would use test.test_importlib.util.uncache() which will do what you need and is considered usable by other tests.

Copy link
Member

Choose a reason for hiding this comment

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

test.test_importlib.util.uncache() is less strong than test.test_importlib.test_namespace_pkgs.sys_modules_context(), but I think in this case it would be enough.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

@bedevere-bot
Copy link

A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated.

Once you have made the requested changes, please leave a comment on this pull request containing the phrase I have made the requested changes; please review again. I will then notify any core developers who have left a review that you're ready for them to take another look at this pull request.

@the-knights-who-say-ni
Copy link

Hello, and thanks for your contribution!

I'm a bot set up to make sure that the project can legally accept your contribution by verifying you have signed the PSF contributor agreement (CLA).

Unfortunately we couldn't find an account corresponding to your GitHub username on bugs.python.org (b.p.o) to verify you have signed the CLA (this might be simply due to a missing "GitHub Name" entry in your b.p.o account settings). This is necessary for legal reasons before we can look at your contribution. Please follow the steps outlined in the CPython devguide to rectify this issue.

You can check yourself to see if the CLA has been received.

Thanks again for your contribution, we look forward to reviewing it!

@onyb
Copy link
Contributor Author

onyb commented Dec 7, 2018

I have made the requested changes; please review again

@bedevere-bot
Copy link

Thanks for making the requested changes!

@brettcannon: please review the changes made to this pull request.

@brettcannon brettcannon merged commit 3cf7438 into python:master Dec 7, 2018
@miss-islington
Copy link
Contributor

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

@bedevere-bot
Copy link

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

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 7, 2018
…k tests (pythonGH-8520)

(cherry picked from commit 3cf7438)

Co-authored-by: Anirudha Bose <[email protected]>
@bedevere-bot
Copy link

GH-11032 is a backport of this pull request to the 3.6 branch.

miss-islington pushed a commit to miss-islington/cpython that referenced this pull request Dec 7, 2018
…k tests (pythonGH-8520)

(cherry picked from commit 3cf7438)

Co-authored-by: Anirudha Bose <[email protected]>
@onyb onyb deleted the fix-issue33747-avoid-sys-modules-mutation branch December 8, 2018 10:12
miss-islington added a commit that referenced this pull request Dec 11, 2018
miss-islington added a commit that referenced this pull request Dec 11, 2018
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
skip news tests Tests in the Lib/test dir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants