Skip to content

Importing anydbm within pytest fails #2248

@pfhayes

Description

@pfhayes

When trying to import the anydbm module from within pytest on a machine that does not have bsddb installed, the import will fail. This appears to be because

  • Importing anydbm tries to import dbhash which imports bsddb. If bsddb is not present on the machine, this fails with an ImportError, which anydbm catches and silences.
  • When running pytest, importing a module is overridden by load_module in _pytest/assertion/rewrite.py
  • When the import of dbhash fails, it tries to del sys.modules['dbhash'] but this throws a KeyError since 'dbhash' is not present in sys.modules
  • anydbm fails to catch the KeyError since it is expecting ImportError

Observed in pytest 3.0.5 on both OS X and Ubuntu.

This can be reproduced by including import anydbm in any test file. For example

import anydbm

def test_run():
  assert True

This error occurs:

testl.py:1: in <module>
    import anydbm
../.virtualenvs/test2/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:216: in load_module
    py.builtin.exec_(co, mod.__dict__)
/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/anydbm.py:45: in <module>
    _mod = __import__(_name)
../.virtualenvs/test2/lib/python2.7/site-packages/_pytest/assertion/rewrite.py:218: in load_module
    del sys.modules[name]
E   KeyError: 'dbhash'
$ pip list
appdirs (1.4.0)
packaging (16.8)
pip (9.0.1)
py (1.4.32)
pyparsing (2.1.10)
pytest (3.0.5)
setuptools (34.2.0)
six (1.10.0)
wheel (0.30.0a0)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions