Skip to content
Myr

use traceback to detect call more reliably

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

authored by David Lord · committed by David Lord · parent 1a2dc6f · Browse files · 1 file+27 added−10 removed
This commit

State

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

Diff stats

1 file+27 added−10 removed

  • other:+27 added−10 removed

1 file+27 added−10 removed

modified:flask/cli.py+27 added-10 removed
31 unmodified lines
32except ImportError:33 dotenv = None343536def _called_with_wrong_args(factory, exc_info):37 exc_type, exc_value, tb = exc_info38 return exc_type is TypeError and \39 str(exc_value).startswith((40 '%s() takes' % factory.__name__,41 '%s() missing' % factory.__name__))424336class NoAppException(click.UsageError):37 """Raised if an application cannot be found or loaded."""3839
34 unmodified lines
7475 if isinstance(app, Flask):76 return app77 except TypeError:86 if not _called_with_wrong_args(app_factory, sys.exc_info()):78 if not _called_with_wrong_args(app_factory):79 raise80 raise NoAppException(81 'Detected factory "{factory}" in module "{module}", but '82 'could not call it without arguments. Use '
29 unmodified lines
112113 return app_factory()114115116def _called_with_wrong_args(factory):117 """Check whether calling a function raised a ``TypeError`` because118 the call failed or because something in the factory raised the119 error.120121 :param factory: the factory function that was called122 :return: true if the call failed123 """124 tb = sys.exc_info()[2]125126 try:127 while tb is not None:128 if tb.tb_frame.f_code is factory.__code__:129 # in the factory, it was called successfully130 return False131132 tb = tb.tb_next133134 # didn't reach the factory135 return True136 finally:137 del tb138139140def find_app_by_string(script_info, module, app_name):141 """Checks if the given string is a variable name or a function. If it is a142 function, it checks for specified arguments and whether it takes a143 ``script_info`` argument and calls the function with the appropriate
29 unmodified lines
173174 try:175 app = call_factory(script_info, attr, args)176 except TypeError as e:161 if not _called_with_wrong_args(attr, sys.exc_info()):177 if not _called_with_wrong_args(attr):178 raise179180 raise NoAppException(181 '{e}\nThe factory "{app_name}" in module "{module}" could not '182 'be called with the specified arguments.'.format(183 e=e, app_name=app_name, module=module.__name__
605 unmodified lines