Skip to content
Myr

Check if app factory takes script_info argument and call it with(out) script_info as an argument depending on that

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

authored by Hendrik Makait · committed by Hendrik Makait · parent 81c2440 · Browse files · 3 files+66 added−28 removed
This commit

State

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

Diff stats

3 files+66 added−28 removed

  • tests:+41 added−21 removed
  • other:+25 added−7 removed

3 files+66 added−28 removed

modified:flask/_compat.py+2 added
23 unmodified lines
24 iterkeys = lambda d: iter(d.keys())25 itervalues = lambda d: iter(d.values())26 iteritems = lambda d: iter(d.items())2728 from inspect import getfullargspec as getargspec29 from io import StringIO3031 def reraise(tp, value, tb=None):32 if value.__traceback__ is not tb:
10 unmodified lines
43 iterkeys = lambda d: d.iterkeys()44 itervalues = lambda d: d.itervalues()45 iteritems = lambda d: d.iteritems()4647 from inspect import getargspec48 from cStringIO import StringIO4950 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')51
47 unmodified lines
modified:flask/cli.py+23 added-7 removed
20 unmodified lines
21from . import __version__22from ._compat import iteritems, reraise23from .globals import current_app24from .helpers import get_debug_flag25from ._compat import getargspec262728class NoAppException(click.UsageError):29 """Raised if an application cannot be found or loaded."""303131def find_best_app(module):32def find_best_app(script_info, module):33 """Given a module instance this tries to find the best possible34 application in the module or raises an exception.35 """36 from . import Flask
23 unmodified lines
60 app_factory = getattr(module, attr_name, None)6162 if callable(app_factory):63 try:63 app = app_factory()6464 app = check_factory_for_script_info_and_call(app_factory,65 script_info)66 if isinstance(app, Flask):67 return app68 except TypeError:69 raise NoAppException(
9 unmodified lines
79 'middleware.'.format(module=module.__name__)80 )818283def check_factory_for_script_info_and_call(func, script_info):84 """Given a function this checks if the function has an argument named 85 script_info or just a single argument and calls the function with 86 script_info if so. Otherwise calls the function without any arguments and 87 returns the result."""88 arguments = getargspec(func).args89 if 'script_info' in arguments:90 result = func(script_info=script_info)91 elif len(arguments) == 1:92 result = func(script_info)93 else:94 result = func()95 return result969798def prepare_exec_for_file(filename):99 """Given a filename this will try to calculate the python path, add it100 to the search path and return the actual module name that is expected.101 """
21 unmodified lines
123 sys.path.insert(0, dirpath)124 return '.'.join(module[::-1])125126111def locate_app(app_id):127def locate_app(script_info, app_id):128 """Attempts to locate the application."""129 __traceback_hide__ = True130 if ':' in app_id:131 module, app_obj = app_id.split(':', 1)
17 unmodified lines
149 'ensure the extension is .py' % module)150151 mod = sys.modules[module]152 if app_obj is None:137 app = find_best_app(mod)153 app = find_best_app(script_info, mod)154 else:155 app = getattr(mod, app_obj, None)156 if app is None:157 raise RuntimeError('Failed to find application in module "%s"'
116 unmodified lines
274 __traceback_hide__ = True275 if self._loaded_app is not None:276 return self._loaded_app277 if self.create_app is not None:262 rv = self.create_app(self)278 rv = check_factory_for_script_info_and_call(self.create_app, self)279 else:280 if not self.app_import_path:281 raise NoAppException(282 'Could not locate Flask application. You did not provide '283 'the FLASK_APP environment variable.\n\nFor more '284 'information see '285 'http://flask.pocoo.org/docs/latest/quickstart/')270 rv = locate_app(self.app_import_path)286 rv = locate_app(self, self.app_import_path)287 debug = get_debug_flag()288 if debug is not None:289 rv.debug = debug290 self._loaded_app = rv
333 unmodified lines
modified:tests/test_cli.py+41 added-21 removed
37 unmodified lines
383940def test_find_best_app(test_apps):41 """Test if `find_best_app` behaves as expected with different combinations of input."""42 script_info = ScriptInfo()43 class Module:44 app = Flask('appname')44 assert find_best_app(Module) == Module.app45 assert find_best_app(script_info, Module) == Module.app4647 class Module:48 application = Flask('appname')48 assert find_best_app(Module) == Module.application49 assert find_best_app(script_info, Module) == Module.application5051 class Module:52 myapp = Flask('appname')52 assert find_best_app(Module) == Module.myapp53 assert find_best_app(script_info, Module) == Module.myapp5455 class Module:56 @staticmethod57 def create_app():58 return Flask('appname')58 assert isinstance(find_best_app(Module), Flask)59 assert find_best_app(Module).name == 'appname'59 assert isinstance(find_best_app(script_info, Module), Flask)60 assert find_best_app(script_info, Module).name == 'appname'6162 class Module:63 @staticmethod64 def create_app(foo):65 return Flask('appname')66 assert isinstance(find_best_app(script_info, Module), Flask)67 assert find_best_app(script_info, Module).name == 'appname'6869 class Module:70 @staticmethod71 def create_app(foo=None, script_info=None):72 return Flask('appname')73 assert isinstance(find_best_app(script_info, Module), Flask)74 assert find_best_app(script_info, Module).name == 'appname'7576 class Module:77 @staticmethod78 def make_app():79 return Flask('appname')65 assert isinstance(find_best_app(Module), Flask)66 assert find_best_app(Module).name == 'appname'80 assert isinstance(find_best_app(script_info, Module), Flask)81 assert find_best_app(script_info, Module).name == 'appname'8283 class Module:84 myapp = Flask('appname1')85 @staticmethod86 def create_app():87 return Flask('appname2')73 assert find_best_app(Module) == Module.myapp88 assert find_best_app(script_info, Module) == Module.myapp8990 class Module:91 myapp = Flask('appname1')92 @staticmethod78 def create_app(foo):93 def create_app():94 return Flask('appname2')80 assert find_best_app(Module) == Module.myapp95 assert find_best_app(script_info, Module) == Module.myapp9697 class Module:98 pass84 pytest.raises(NoAppException, find_best_app, Module)99 pytest.raises(NoAppException, find_best_app, script_info, Module)100101 class Module:102 myapp1 = Flask('appname1')103 myapp2 = Flask('appname2')89 pytest.raises(NoAppException, find_best_app, Module)104 pytest.raises(NoAppException, find_best_app, script_info, Module)105106 class Module:107 @staticmethod93 def create_app(foo):108 def create_app(foo, bar):109 return Flask('appname2')95 pytest.raises(NoAppException, find_best_app, Module)110 pytest.raises(NoAppException, find_best_app, script_info, Module)111112113def test_prepare_exec_for_file(test_apps):114 """Expect the correct path to be set and the correct module name to be returned.
16 unmodified lines
131132133def test_locate_app(test_apps):134 """Test of locate_app."""120 assert locate_app("cliapp.app").name == "testapp"121 assert locate_app("cliapp.app:testapp").name == "testapp"122 assert locate_app("cliapp.multiapp:app1").name == "app1"123 pytest.raises(NoAppException, locate_app, "notanpp.py")124 pytest.raises(NoAppException, locate_app, "cliapp/app")125 pytest.raises(RuntimeError, locate_app, "cliapp.app:notanapp")126 pytest.raises(NoAppException, locate_app, "cliapp.importerrorapp")135 script_info = ScriptInfo()136 assert locate_app(script_info, "cliapp.app").name == "testapp"137 assert locate_app(script_info, "cliapp.app:testapp").name == "testapp"138 assert locate_app(script_info, "cliapp.multiapp:app1").name == "app1"139 pytest.raises(NoAppException, locate_app,140 script_info, "notanpp.py")141 pytest.raises(NoAppException, locate_app,142 script_info, "cliapp/app")143 pytest.raises(RuntimeError, locate_app,144 script_info, "cliapp.app:notanapp")145 pytest.raises(NoAppException, locate_app,146 script_info, "cliapp.importerrorapp")147148149def test_find_default_import_path(test_apps, monkeypatch, tmpdir):150 """Test of find_default_import_path."""
174 unmodified lines