-
-
Notifications
You must be signed in to change notification settings - Fork 2.7k
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
Fix importing anydbm within pytest #2249
Conversation
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 a lot @pfhayes for the PR, we appreciate it.
CHANGELOG.rst
Outdated
@@ -1,6 +1,9 @@ | |||
3.0.7 (unreleased) | |||
================== | |||
|
|||
* Fix issue when trying to import the `anydbm` module. (`#2248`_). |
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.
Probably this can be made more general, since it is not exclusive to anydbm
. Perhaps: Fix issue in assertion rewriting breaking due to modules silently discarding other modules when importing fails?
@@ -215,7 +215,8 @@ def load_module(self, name): | |||
mod.__loader__ = self | |||
py.builtin.exec_(co, mod.__dict__) | |||
except: | |||
del sys.modules[name] | |||
if name in sys.modules: |
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.
This looks good, I think we should add a test for it though.
I tried this test on master
, but it passes:
pytest_plugins = ['pytester']
def test_import_error(testdir):
testdir.syspathinsert()
testdir.makepyfile(top_module='''
try:
import other_module
except ImportError:
pass
''')
testdir.makepyfile('''
def test_import_error():
import top_module
assert 1
''')
r = testdir.runpytest()
r.stdout.fnmatch_lines(['* 1 passed *'])
Perhaps anydbm
is doing some more magic behind the scenes?
Thanks @nicoddemus I updated the CHANGELOG at your request I've been trying to add in a test for this behaviour but can't get one to work. Here's where I landed, which matches exactly the anydbm -> dbhash -> bsddb import chain, but passes in test mode. +def test_troublesome_imports(testdir):
+ testdir.syspathinsert()
+ testdir.makepyfile(third_module='''
+ import fakething
+ ''')
+ testdir.makepyfile(other_module='''
+ import sys
+ try:
+ import third_module
+ except ImportError:
+ del sys.modules[__name__]
+ raise
+ ''')
+ testdir.makepyfile(top_module='''
+ import sys
+ try:
+ __import__('other_module')
+ except ImportError:
+ pass
+ ''')
+ testdir.makepyfile('''
+ def test_import_error():
+ import top_module
+ assert 1
+ ''')
+ r = testdir.runpytest()
+ r.stdout.fnmatch_lines(['* 1 passed *']) However, this test also passes, which is surprising to me + testdir.makepyfile('''
+ def test_import_error():
+ import anydbm
+ assert 1
+ ''')
+ r = testdir.runpytest()
+ r.stdout.fnmatch_lines(['* 1 passed *']) Is it possible that this error would not occur while testing? |
so a module that removes itself is possible and an import hook may be supposed NOT to clean up @benjaminp do you have any insights on this detail? |
@pfhayes the main and interesting question is, how and why the interaction is set up to fail, so what you see in your environment, is something that should happen neither in test not in normal usage and its not surprising that it doesnt happen i believe marking one of those modules for assertion rewriting may trigger the exception |
Cool @RonnyPfannschmidt, nice find. I think the module is not actually being rewritten for asserts, it is just being handled by the |
Regarding plugins: In the past we have used |
I'm happy to add a test for this case but a bit lost about how to add one that reproduces this behaviour. Any advice? Or can we merge this in without a test? |
I can't reproduce either. Since the change is dead simple (one might consider a small oversight in the implementation) I think we can merge it without an explicit test. What do you think @RonnyPfannschmidt? |
Sounds good to me! Let me know if I can provide anything else |
@RonnyPfannschmidt gentle ping 😄 |
Thanks again @pfhayes! |
Target: master
Addresses the issue here: #2248
I'm not sure how to test this but am happy to add tests