-
-
Notifications
You must be signed in to change notification settings - Fork 32.2k
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
bpo-33747: Avoid mutating the global sys.modules dict #8520
Conversation
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 |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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 |
There was a problem hiding this comment.
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
...
There was a problem hiding this comment.
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()): |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
…eference of sys.modules
I have made the requested changes; please review again |
Thanks for making the requested changes! : please review the changes made to this pull request. |
There was a problem hiding this 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 |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Resolved.
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 |
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! |
I have made the requested changes; please review again |
Thanks for making the requested changes! @brettcannon: please review the changes made to this pull request. |
Thanks @onyb for the PR, and @brettcannon for merging it 🌮🎉.. I'm working now to backport this PR to: 3.6, 3.7. |
GH-11031 is a backport of this pull request to the 3.7 branch. |
…k tests (pythonGH-8520) (cherry picked from commit 3cf7438) Co-authored-by: Anirudha Bose <[email protected]>
GH-11032 is a backport of this pull request to the 3.6 branch. |
…k tests (pythonGH-8520) (cherry picked from commit 3cf7438) Co-authored-by: Anirudha Bose <[email protected]>
…st.mock tests (GH-8520) (GH-11031) (cherry picked from commit 3cf7438) Co-authored-by: Anirudha Bose <[email protected]> https://bugs.python.org/issue33747
…st.mock tests (GH-8520) (GH-11032) (cherry picked from commit 3cf7438) Co-authored-by: Anirudha Bose <[email protected]> https://bugs.python.org/issue33747
The test method
test_patch_imports_lazily
was injecting a mocked moduleto the
sys.modules
dict
, which was reused in another test module calledtest_patch_propogrates_exc_on_exit
. Hence, the other test module failedwhenever 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