From 0f1cf50f97a617114078be3dbae3152339ff13ad Mon Sep 17 00:00:00 2001 From: Nate Prewitt Date: Fri, 12 Aug 2016 07:12:00 -0600 Subject: [PATCH] adding in try around __import__ to catch invalid files/paths (#1950) --- flask/cli.py | 8 +++++++- tests/test_cli.py | 2 ++ 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/flask/cli.py b/flask/cli.py index 58b6fb3a..9b8fa2cd 100644 --- a/flask/cli.py +++ b/flask/cli.py @@ -86,7 +86,13 @@ def locate_app(app_id): module = app_id app_obj = None - __import__(module) + 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) mod = sys.modules[module] if app_obj is None: app = find_best_app(mod) diff --git a/tests/test_cli.py b/tests/test_cli.py index 3f2cceab..d2bb61b9 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -80,6 +80,8 @@ def test_locate_app(test_apps): assert locate_app("cliapp.app").name == "testapp" assert locate_app("cliapp.app:testapp").name == "testapp" assert locate_app("cliapp.multiapp:app1").name == "app1" + pytest.raises(NoAppException, locate_app, "notanpp.py") + pytest.raises(NoAppException, locate_app, "cliapp/app") pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")