Skip to content
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

Better traceback for bad imports in custom path #735

Merged
merged 1 commit into from Nov 22, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 20 additions & 0 deletions tests/test_utils.py
Expand Up @@ -186,6 +186,26 @@ def test_import_string():
pytest.raises(ImportError, utils.import_string, 'cgi.XXXXXXXXXX')


def test_import_string_provides_traceback(tmpdir, monkeypatch):
monkeypatch.syspath_prepend(str(tmpdir))
# Couple of packages
dir_a = tmpdir.mkdir('a')
dir_b = tmpdir.mkdir('b')
# Totally packages, I promise
dir_a.join('__init__.py').write('')
dir_b.join('__init__.py').write('')
# 'aa.a' that depends on 'bb.b', which in turn has a broken import
dir_a.join('aa.py').write('from b import bb')
dir_b.join('bb.py').write('from os import a_typo')

# Do we get all the useful information in the traceback?
with pytest.raises(ImportError) as baz_exc:
utils.import_string('a.aa')
traceback = ''.join((str(line) for line in baz_exc.traceback))
assert 'bb.py\':1' in traceback # a bit different than typical python tb
assert 'from os import a_typo' in traceback


def test_import_string_attribute_error(tmpdir, monkeypatch):
monkeypatch.syspath_prepend(str(tmpdir))
tmpdir.join('foo_test.py').write('from bar_test import value')
Expand Down
8 changes: 1 addition & 7 deletions werkzeug/utils.py
Expand Up @@ -423,13 +423,7 @@ def import_string(import_name, silent=False):
return sys.modules[import_name]

module_name, obj_name = import_name.rsplit('.', 1)
try:
module = __import__(module_name, None, None, [obj_name])
except ImportError:
# support importing modules not yet set up by the parent module
# (or package for that matter)
module = import_string(module_name)

module = __import__(module_name, globals(), locals(), [obj_name])
try:
return getattr(module, obj_name)
except AttributeError as e:
Expand Down