Skip to content

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

Merged
brettcannon merged 7 commits into
python:masterfrom
onyb:fix-issue33747-avoid-sys-modules-mutation
Dec 7, 2018
Merged

bpo-33747: Avoid mutating the global sys.modules dict#8520
brettcannon merged 7 commits into
python:masterfrom
onyb:fix-issue33747-avoid-sys-modules-mutation

Conversation

@onyb
Copy link
Copy Markdown
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.
Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
)

from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
)

from unittest.test.testmock import support
from unittest.test.testmock.support import SomeClass, is_instance
Copy link
Copy Markdown
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
Copy Markdown
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.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
with swap_attr(sys, 'modules', sys.modules.copy()):
squizz = Mock()
sys.modules['squizz'] = squizz
yield squizz
Copy link
Copy Markdown
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
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Resolved.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
p1.start()
p1.stop()
self.assertEqual(squizz.squozz, 3)
with swap_attr(sys, 'modules', sys.modules.copy()):
Copy link
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
Contributor Author

onyb commented Jul 30, 2018

I have made the requested changes; please review again

@bedevere-bot
Copy link
Copy Markdown

Thanks for making the requested changes!

: please review the changes made to this pull request.

Copy link
Copy Markdown
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.

Comment thread Lib/unittest/test/testmock/testpatch.py Outdated
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown
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
Copy Markdown

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
Copy Markdown

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
Copy Markdown
Contributor Author

onyb commented Dec 7, 2018

I have made the requested changes; please review again

@bedevere-bot
Copy link
Copy Markdown

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
Copy Markdown
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
Copy Markdown

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 <ani07nov@gmail.com>
@bedevere-bot
Copy link
Copy Markdown

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 <ani07nov@gmail.com>
@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
…st.mock tests (GH-8520) (GH-11031)

(cherry picked from commit 3cf7438)


Co-authored-by: Anirudha Bose <ani07nov@gmail.com>


https://bugs.python.org/issue33747
miss-islington added a commit that referenced this pull request Dec 11, 2018
…st.mock tests (GH-8520) (GH-11032)

(cherry picked from commit 3cf7438)


Co-authored-by: Anirudha Bose <ani07nov@gmail.com>


https://bugs.python.org/issue33747
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