Merge pull request #2373 from davidism/reload_templates_run_debug
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Reconfigure app when debug is set by run command
authored by David Lord · committed by GitHub · parents 2c97ed9, 4d2a3ab · Browse files · 4 files·+75 added−8 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+75 added−8 removed
- tests:+15 added−0 removed
- other:+60 added−8 removed
Provenance
- Commit
1e112cc- Tree
f805f7e- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
2c97ed9- Parent 2
4d2a3ab
4 files changed+75 added−8 removed
modified:flask/app.py+50 added-7 removed
701 unmodified lines702 :param mode: resource file opening mode, default is 'rb'.703 """704 return open(os.path.join(self.instance_path, resource), mode)705 706+ def _get_templates_auto_reload(self):707+ """Reload templates when they are changed. Used by708+ :meth:`create_jinja_environment`.709+710+ This attribute can be configured with :data:`TEMPLATES_AUTO_RELOAD`. If711+ not set, it will be enabled in debug mode.712+713+ .. versionadded:: 1.0714+ This property was added but the underlying config and behavior715+ already existed.716+ """717+ rv = self.config['TEMPLATES_AUTO_RELOAD']718+ return rv if rv is not None else self.debug719+720+ def _set_templates_auto_reload(self, value):721+ self.config['TEMPLATES_AUTO_RELOAD'] = value722+723+ templates_auto_reload = property(724+ _get_templates_auto_reload, _set_templates_auto_reload725+ )726+ del _get_templates_auto_reload, _set_templates_auto_reload727+728 def create_jinja_environment(self):729 """Creates the Jinja2 environment based on :attr:`jinja_options`730 and :meth:`select_jinja_autoescape`. Since 0.7 this also adds731 the Jinja2 globals and filters after initialization. Override4 unmodified lines736 ``Environment.auto_reload`` set in accordance with737 ``TEMPLATES_AUTO_RELOAD`` configuration option.738 """739 options = dict(self.jinja_options)740+741 if 'autoescape' not in options:742 options['autoescape'] = self.select_jinja_autoescape743+744 if 'auto_reload' not in options:721- if self.config['TEMPLATES_AUTO_RELOAD'] is not None:722- options['auto_reload'] = self.config['TEMPLATES_AUTO_RELOAD']723- else:724- options['auto_reload'] = self.debug745+ options['auto_reload'] = self.templates_auto_reload746+747 rv = self.jinja_environment(self, **options)748 rv.globals.update(749 url_for=url_for,750 get_flashed_messages=get_flashed_messages,76 unmodified lines827 for processor in self.shell_context_processors:828 rv.update(processor())829 return rv830 831+ def _reconfigure_for_run_debug(self, debug):832+ """The ``run`` commands will set the application's debug flag. Some833+ application configuration may already be calculated based on the834+ previous debug value. This method will recalculate affected values.835+836+ Called by the :func:`flask.cli.run` command or :meth:`Flask.run`837+ method if the debug flag is set explicitly in the call.838+839+ :param debug: the new value of the debug flag840+841+ .. versionadded:: 1.0842+ Reconfigures ``app.jinja_env.auto_reload``.843+ """844+ self.debug = debug845+ self.jinja_env.auto_reload = self.templates_auto_reload846+847 def run(self, host=None, port=None, debug=None, **options):848 """Runs the application on a local development server.849 850 Do not use ``run()`` in a production setting. It is not intended to45 unmodified lines896 from .debughelpers import explain_ignored_app_run897 explain_ignored_app_run()898 return899 862- from werkzeug.serving import run_simple900+ if debug is not None:901+ self._reconfigure_for_run_debug(bool(debug))902+903 _host = '127.0.0.1'904 _port = 5000905 server_name = self.config.get("SERVER_NAME")906 sn_host, sn_port = None, None907+908 if server_name:909 sn_host, _, sn_port = server_name.partition(':')910+911 host = host or sn_host or _host912 port = int(port or sn_port or _port)871- if debug is not None:872- self.debug = bool(debug)913 options.setdefault('use_reloader', self.debug)914 options.setdefault('use_debugger', self.debug)915+916+ from werkzeug.serving import run_simple917+918 try:919 run_simple(host, port, self, **options)920 finally:921 # reset the first request information if the development server1286 unmodified linesmodified:flask/cli.py+7 added-1 removed
317 unmodified lines318 this multiple times will just result in the already loaded app to319 be returned.320 """321 __traceback_hide__ = True322+323 if self._loaded_app is not None:324 return self._loaded_app325+326 if self.create_app is not None:327 rv = call_factory(self.create_app, self)328 else:329 if not self.app_import_path:1 unmodified line331 'Could not locate Flask application. You did not provide '332 'the FLASK_APP environment variable.\n\nFor more '333 'information see '334 'http://flask.pocoo.org/docs/latest/quickstart/')335+336 rv = locate_app(self, self.app_import_path)337+338 debug = get_debug_flag()339+340 if debug is not None:336- rv.debug = debug341+ rv._reconfigure_for_run_debug(debug)342+343 self._loaded_app = rv344 return rv345 346 330 unmodified linesmodified:tests/test_templating.py+15 added
12 unmodified lines13 14 import flask15 import logging16 from jinja2 import TemplateNotFound17+import werkzeug.serving18 19 20 def test_context_processing(app, client):21 @app.context_processor363 unmodified lines385 app.config['DEBUG'] = True386 app.config['TEMPLATES_AUTO_RELOAD'] = True387 assert app.jinja_env.auto_reload is True388 389+def test_templates_auto_reload_debug_run(app, monkeypatch):390+ def run_simple_mock(*args, **kwargs):391+ pass392 393+ monkeypatch.setattr(werkzeug.serving, 'run_simple', run_simple_mock)394+395+ app.run()396+ assert app.templates_auto_reload == False397+ assert app.jinja_env.auto_reload == False398+399+ app.run(debug=True)400+ assert app.templates_auto_reload == True401+ assert app.jinja_env.auto_reload == True402+403+404 def test_template_loader_debugging(test_apps):405 from blueprintapp import app406 407 called = []42 unmodified linesmodified:CHANGES+3 added
77 unmodified lines78 the response to make testing JSON response much easier. (`#2358`_)79 - Removed error handler caching because it caused unexpected results for some80 exception inheritance hierarchies. Register handlers explicitly for each81 exception if you don't want to traverse the MRO. (`#2362`_)82+- Template auto reloading will honor the ``run`` command's ``debug`` flag even83+ if ``app.jinja_env`` was already accessed. (`#2373`_)84 85 .. _#1489: https://github.com/pallets/flask/pull/148986 .. _#1621: https://github.com/pallets/flask/pull/162187 .. _#1898: https://github.com/pallets/flask/pull/189815 unmodified lines103 .. _#2352: https://github.com/pallets/flask/pull/2352104 .. _#2354: https://github.com/pallets/flask/pull/2354105 .. _#2358: https://github.com/pallets/flask/pull/2358106 .. _#2362: https://github.com/pallets/flask/pull/2362107+.. _#2373: https://github.com/pallets/flask/pull/2373108 109 Version 0.12.2110 --------------111 617 unmodified lines