Skip to content

Fix importing anydbm within pytest #2249

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
merged 5 commits into from
Mar 1, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ Nicolas Delaby
Oleg Pidsadnyi
Oliver Bestwalter
Omar Kohl
Patrick Hayes
Pieter Mulder
Piotr Banaszkiewicz
Punyashloka Biswal
Expand Down
8 changes: 8 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
3.0.7 (unreleased)
==================


* Fix issue in assertion rewriting breaking due to modules silently discarding
other modules when importing fails
Notably, importing the `anydbm` module is fixed. (`#2248`_).
Thanks `@pfhayes`_ for the PR.

* junitxml: Fix problematic case where system-out tag occured twice per testcase
element in the XML report. Thanks `@kkoukiou`_ for the PR.

Expand Down Expand Up @@ -30,12 +36,14 @@

*

.. _@pfhayes: https://github.com/pfhayes
.. _@bluetech: https://github.com/bluetech
.. _@gst: https://github.com/gst
.. _@sirex: https://github.com/sirex
.. _@vidartf: https://github.com/vidartf
.. _@kkoukiou: https://github.com/KKoukiou

.. _#2248: https://github.com/pytest-dev/pytest/issues/2248
.. _#2137: https://github.com/pytest-dev/pytest/issues/2137
.. _#2160: https://github.com/pytest-dev/pytest/issues/2160
.. _#2231: https://github.com/pytest-dev/pytest/issues/2231
Expand Down
3 changes: 2 additions & 1 deletion _pytest/assertion/rewrite.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Member

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?

del sys.modules[name]
raise
return sys.modules[name]

Expand Down