From 0832e77b145b226bac4ee82144221800a0f7d34a Mon Sep 17 00:00:00 2001 From: Paul Brown Date: Fri, 30 Dec 2016 15:02:08 -0600 Subject: [PATCH 1/2] prevent NoAppException when ImportError occurs within imported module --- flask/cli.py | 14 ++++++++++---- tests/test_apps/cliapp/importerrorapp.py | 7 +++++++ tests/test_cli.py | 1 + 3 files changed, 18 insertions(+), 4 deletions(-) create mode 100644 tests/test_apps/cliapp/importerrorapp.py diff --git a/flask/cli.py b/flask/cli.py index da988e80..074ee768 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -89,10 +89,16 @@ def locate_app(app_id): try: __import__(module) except ImportError: - raise NoAppException('The file/path provided (%s) does not appear to ' - 'exist. Please verify the path is correct. If ' - 'app is not on PYTHONPATH, ensure the extension ' - 'is .py' % module) + # Reraise the ImportError if it occurred within the imported module. + # Determine this by checking whether the trace has a depth > 1. + if sys.exc_info()[-1].tb_next: + raise + else: + raise NoAppException('The file/path provided (%s) does not appear' + ' to exist. Please verify the path is ' + 'correct. If app is not on PYTHONPATH, ' + 'ensure the extension is .py' % module) + mod = sys.modules[module] if app_obj is None: app = find_best_app(mod) diff --git a/tests/test_apps/cliapp/importerrorapp.py b/tests/test_apps/cliapp/importerrorapp.py new file mode 100644 index 00000000..fb87c9b1 --- /dev/null +++ b/tests/test_apps/cliapp/importerrorapp.py @@ -0,0 +1,7 @@ +from __future__ import absolute_import, print_function + +from flask import Flask + +raise ImportError() + +testapp = Flask('testapp') diff --git a/tests/test_cli.py b/tests/test_cli.py index 18026a75..313a34d2 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -83,6 +83,7 @@ def test_locate_app(test_apps): pytest.raises(NoAppException, locate_app, "notanpp.py") pytest.raises(NoAppException, locate_app, "cliapp/app") pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp") + pytest.raises(ImportError, locate_app, "cliapp.importerrorapp") def test_find_default_import_path(test_apps, monkeypatch, tmpdir): From 31e25facd3972b42d118792c847299cc8b2b25a5 Mon Sep 17 00:00:00 2001 From: Paul Brown Date: Fri, 30 Dec 2016 15:40:30 -0600 Subject: [PATCH 2/2] update change log --- CHANGES | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGES b/CHANGES index a0a423d1..03194421 100644 --- a/CHANGES +++ b/CHANGES @@ -8,6 +8,8 @@ Version 0.12.1 Bugfix release, unreleased +- Prevent `flask run` from showing a NoAppException when an ImportError occurs + within the imported application module. - Fix encoding behavior of ``app.config.from_pyfile`` for Python 3. Fix ``#2118``.