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
Provenance
- Commit
2cba0d7- Tree
4aadbca- Author
David Lord <davidism@gmail.com>- Committer
David Lord <davidism@gmail.com>- Parent
1a2dc6f
1 file changed+27 added−10 removed
modified:flask/cli.py+27 added-10 removed
31 unmodified lines32 except ImportError:33 dotenv = None34 35 36-def _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__))42-43-36 class NoAppException(click.UsageError):37 """Raised if an application cannot be found or loaded."""38 39 34 unmodified lines74 75 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 lines112 113 return app_factory()114 115 116+def _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.120+121+ :param factory: the factory function that was called122+ :return: true if the call failed123+ """124+ tb = sys.exc_info()[2]125+126+ 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 False131+132+ tb = tb.tb_next133+134+ # didn't reach the factory135+ return True136+ finally:137+ del tb138+139+140 def 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 appropriate29 unmodified lines173 174 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 raise179+180 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