Merge remote-tracking branch 'pallets/master' into 2313-refactor-gevent-tests-into-class
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Rene A. Zelaya · committed by Rene A. Zelaya · parents 2eb2816, 39f7aaa · Browse files · 7 files·+84 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
7 files+84 added−32 removed
- tests:+41 added−21 removed
- other:+43 added−11 removed
Provenance
- Commit
f406170- Tree
651a2b8- Author
Rene A. Zelaya <Rene.Zelaya.F@gmail.com>- Committer
Rene A. Zelaya <Rene.Zelaya.F@gmail.com>- Parent 1
2eb2816- Parent 2
39f7aaa
7 files changed+84 added−32 removed
modified:docs/quickstart.rst+8 added-1 removed
48 unmodified lines49 $ export FLASK_APP=hello.py50 $ flask run51 * Running on http://127.0.0.1:5000/52 53-If you are on Windows you need to use ``set`` instead of ``export``.53+If you are on Windows, the environment variable syntax depends on command line54+interpreter. On Command Prompt::55 56+ C:\path\to\app>set FLASK_APP=hello.py57+58+And on PowerShell::59+60+ PS C:\path\to\app> $env:FLASK_APP = "hello.py"61+62 Alternatively you can use :command:`python -m flask`::63 64 $ export FLASK_APP=hello.py65 $ python -m flask run784 unmodified linesmodified:flask/_compat.py+2 added
23 unmodified lines24 iterkeys = lambda d: iter(d.keys())25 itervalues = lambda d: iter(d.values())26 iteritems = lambda d: iter(d.items())27 28+ from inspect import getfullargspec as getargspec29 from io import StringIO30 31 def reraise(tp, value, tb=None):32 if value.__traceback__ is not tb:10 unmodified lines43 iterkeys = lambda d: d.iterkeys()44 itervalues = lambda d: d.itervalues()45 iteritems = lambda d: d.iteritems()46 47+ from inspect import getargspec48 from cStringIO import StringIO49 50 exec('def reraise(tp, value, tb=None):\n raise tp, value, tb')51 47 unmodified linesmodified:flask/cli.py+21 added-7 removed
20 unmodified lines21 from . import __version__22 from ._compat import iteritems, reraise23 from .globals import current_app24 from .helpers import get_debug_flag25+from ._compat import getargspec26 27 28 class NoAppException(click.UsageError):29 """Raised if an application cannot be found or loaded."""30 31 31-def find_best_app(module):32+def 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 Flask23 unmodified lines60 app_factory = getattr(module, attr_name, None)61 62 if callable(app_factory):63 try:63- app = app_factory()64-64+ app = call_factory(app_factory, script_info)65 if isinstance(app, Flask):66 return app67 except TypeError:68 raise NoAppException(9 unmodified lines78 'middleware.'.format(module=module.__name__)79 )80 81 82+def call_factory(func, script_info):83+ """Checks if the given app factory function has an argument named 84+ ``script_info`` or just a single argument and calls the function passing 85+ ``script_info`` if so. Otherwise, calls the function without any arguments86+ and returns the result.87+ """88+ arguments = getargspec(func).args89+ if 'script_info' in arguments:90+ return func(script_info=script_info)91+ elif len(arguments) == 1:92+ return func(script_info)93+ return func()94+95+96 def prepare_exec_for_file(filename):97 """Given a filename this will try to calculate the python path, add it98 to the search path and return the actual module name that is expected.99 """21 unmodified lines121 sys.path.insert(0, dirpath)122 return '.'.join(module[::-1])123 124 111-def locate_app(app_id):125+def locate_app(script_info, app_id):126 """Attempts to locate the application."""127 __traceback_hide__ = True128 if ':' in app_id:129 module, app_obj = app_id.split(':', 1)17 unmodified lines147 'ensure the extension is .py' % module)148 149 mod = sys.modules[module]150 if app_obj is None:137- app = find_best_app(mod)151+ app = find_best_app(script_info, mod)152 else:153 app = getattr(mod, app_obj, None)154 if app is None:155 raise RuntimeError('Failed to find application in module "%s"'116 unmodified lines272 __traceback_hide__ = True273 if self._loaded_app is not None:274 return self._loaded_app275 if self.create_app is not None:262- rv = self.create_app(self)276+ rv = call_factory(self.create_app, self)277 else:278 if not self.app_import_path:279 raise NoAppException(280 'Could not locate Flask application. You did not provide '281 'the FLASK_APP environment variable.\n\nFor more '282 'information see '283 'http://flask.pocoo.org/docs/latest/quickstart/')270- rv = locate_app(self.app_import_path)284+ rv = locate_app(self, self.app_import_path)285 debug = get_debug_flag()286 if debug is not None:287 rv.debug = debug288 self._loaded_app = rv333 unmodified linesmodified:tests/test_cli.py+41 added-21 removed
37 unmodified lines38 39 40 def 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.app46 47 class Module:48 application = Flask('appname')48- assert find_best_app(Module) == Module.application49+ assert find_best_app(script_info, Module) == Module.application50 51 class Module:52 myapp = Flask('appname')52- assert find_best_app(Module) == Module.myapp53+ assert find_best_app(script_info, Module) == Module.myapp54 55 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'61 62 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'68+69+ 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'75+76+ 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'82 83 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.myapp89 90 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.myapp96 97 class Module:98 pass84- pytest.raises(NoAppException, find_best_app, Module)99+ pytest.raises(NoAppException, find_best_app, script_info, Module)100 101 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)105 106 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)111 112 113 def 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 lines131 132 133 def 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")147 148 149 def test_find_default_import_path(test_apps, monkeypatch, tmpdir):150 """Test of find_default_import_path."""174 unmodified linesmodified:CHANGES+5 added
40 unmodified lines41 - ``SESSION_COOKIE_DOMAIN`` is set if it is detected through ``SERVER_NAME``.42 (`#2282`_)43 - Auto-detect zero-argument app factory called ``create_app`` or ``make_app``44 from ``FLASK_APP``. (`#2297`_)45+- Factory functions are not required to take a ``script_info`` parameter to46+ work with the ``flask`` command. If they take a single parameter or a47+ parameter named ``script_info``, the ``ScriptInfo`` object will be passed.48+ (`#2319`_)49 50 .. _#1489: https://github.com/pallets/flask/pull/148951 .. _#1898: https://github.com/pallets/flask/pull/189852 .. _#1936: https://github.com/pallets/flask/pull/19363 unmodified lines56 .. _#2256: https://github.com/pallets/flask/pull/225657 .. _#2259: https://github.com/pallets/flask/pull/225958 .. _#2282: https://github.com/pallets/flask/pull/228259 .. _#2297: https://github.com/pallets/flask/pull/229760+.. _#2319: https://github.com/pallets/flask/pull/231961 62 Version 0.12.263 --------------64 615 unmodified linesmodified:setup.py+1 added-1 removed
72 unmodified lines73 install_requires=[74 'Werkzeug>=0.7',75 'Jinja2>=2.4',76 'itsdangerous>=0.21',77- 'click>=2.0',77+ 'click>=4.0',78 ],79 classifiers=[80 'Development Status :: 4 - Beta',81 'Environment :: Web Environment',19 unmodified linesmodified:tox.ini+6 added-2 removed
1 [tox]2 envlist = {py26,py27,pypy}-{lowest,release,devel}{,-simplejson}, {py33,py34,py35,py36}-{release,devel}{,-simplejson}3 4-5-4 [testenv]5 passenv = LANG6 usedevelop=true7 commands =9 unmodified lines17 18 lowest: Werkzeug==0.719 lowest: Jinja2==2.420 lowest: itsdangerous==0.2121+ lowest: Click==4.022 lowest: blinker==1.023+24 release: blinker25+26 devel: git+https://github.com/pallets/werkzeug.git27+ devel: git+https://github.com/pallets/markupsafe.git28 devel: git+https://github.com/pallets/jinja.git29 devel: git+https://github.com/pallets/itsdangerous.git30+ devel: git+https://github.com/pallets/click.git31 devel: git+https://github.com/jek/blinker.git32+33 simplejson: simplejson34 35 [testenv:docs]36 deps = sphinx1 unmodified line