Skip to content
Myr

Merge pull request #2530 from pallets/feature/factory-detection

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Improved bad factory error handling

authored by David Lord · committed by GitHub · parents 83b5625, 2cba0d7 · Browse files · 2 files+36 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+36 added−0 removed

  • tests:+7 added−0 removed
  • other:+29 added−0 removed

2 files+36 added−0 removed

modified:flask/cli.py+29 added
75 unmodified lines
7677 if isinstance(app, Flask):78 return app79 except TypeError:80 if not _called_with_wrong_args(app_factory):81 raise82 raise NoAppException(83 'Detected factory "{factory}" in module "{module}", but '84 'could not call it without arguments. Use '85 '"FLASK_APP=\'{module}:{factory}(args)\'" to specify '
28 unmodified lines
114115 return app_factory()116117118def _called_with_wrong_args(factory):119 """Check whether calling a function raised a ``TypeError`` because120 the call failed or because something in the factory raised the121 error.122123 :param factory: the factory function that was called124 :return: true if the call failed125 """126 tb = sys.exc_info()[2]127128 try:129 while tb is not None:130 if tb.tb_frame.f_code is factory.__code__:131 # in the factory, it was called successfully132 return False133134 tb = tb.tb_next135136 # didn't reach the factory137 return True138 finally:139 del tb140141142def find_app_by_string(script_info, module, app_name):143 """Checks if the given string is a variable name or a function. If it is a144 function, it checks for specified arguments and whether it takes a145 ``script_info`` argument and calls the function with the appropriate
29 unmodified lines
175176 try:177 app = call_factory(script_info, attr, args)178 except TypeError as e:179 if not _called_with_wrong_args(attr):180 raise181182 raise NoAppException(183 '{e}\nThe factory "{app_name}" in module "{module}" could not '184 'be called with the specified arguments.'.format(185 e=e, app_name=app_name, module=module.__name__
699 unmodified lines
modified:tests/test_cli.py+7 added
133 unmodified lines
134 return Flask('appname2')135136 pytest.raises(NoAppException, find_best_app, script_info, Module)137138 class Module:139 @staticmethod140 def create_app():141 raise TypeError('bad bad factory!')142143 pytest.raises(TypeError, find_best_app, script_info, Module)144145146@pytest.mark.parametrize('value,path,result', (147 ('test', cwd, 'test'),148 ('test.py', cwd, 'test'),149 ('a/test', os.path.join(cwd, 'a'), 'test'),
385 unmodified lines