From f88765d504ce2fa9bc3926c76910b11510522892 Mon Sep 17 00:00:00 2001 From: defuz Date: Sun, 26 Oct 2014 18:28:12 +0300 Subject: [PATCH 1/3] set TEMPLATE_AUTO_RELOAD default value to None --- CHANGES | 6 +++--- docs/config.rst | 12 ++++++------ flask/app.py | 8 +++++--- tests/test_templating.py | 30 +++++++++++++++++++++++++++--- 4 files changed, 41 insertions(+), 15 deletions(-) diff --git a/CHANGES b/CHANGES index 5df663ea..f028df2c 100644 --- a/CHANGES +++ b/CHANGES @@ -23,9 +23,9 @@ Version 1.0 - Added :meth:`flask.Config.from_json`. - Added :attr:`flask.Flask.config_class`. - Added :meth:`flask.config.Config.get_namespace`. -- Added ``TEMPLATES_AUTO_RELOAD`` config key. If disabled the - templates will be reloaded only if the application is running in - debug mode. For higher performance it’s possible to disable that. +- Added ``TEMPLATES_AUTO_RELOAD`` config key. Now by default templates will be + reloaded every time a template is requested only if the application is + running in debug mode. - Added a workaround for a limitation in Python 3.3's namespace loader. - Added support for explicit root paths when using Python 3.3's namespace packages. diff --git a/docs/config.rst b/docs/config.rst index 5f2b275e..d20f9a33 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -182,12 +182,12 @@ The following configuration values are used internally by Flask: if they are not requested by an XMLHttpRequest object (controlled by the ``X-Requested-With`` header) -``TEMPLATES_AUTO_RELOAD`` Flask checks if template was modified each - time it is requested and reloads it if - necessary. But disk I/O is costly and it may - be viable to disable this feature by setting - this key to ``False``. This option does not - affect debug mode. +``TEMPLATES_AUTO_RELOAD`` If this is set to `True` every time a template + is requested Flask checks if the template was + modified and if yes, it will reload the + template. By default the value is ``None`` + which means that Flask checks template + sources only in debug mode. ``EXPLAIN_TEMPLATE_LOADING`` If this is enabled then every attempt to load a template will write an info message to the logger explaining the diff --git a/flask/app.py b/flask/app.py index 4e97605e..9efa53d7 100644 --- a/flask/app.py +++ b/flask/app.py @@ -297,7 +297,7 @@ class Flask(_PackageBoundObject): 'JSON_AS_ASCII': True, 'JSON_SORT_KEYS': True, 'JSONIFY_PRETTYPRINT_REGULAR': True, - 'TEMPLATES_AUTO_RELOAD': True, + 'TEMPLATES_AUTO_RELOAD': None, }) #: The rule object to use for URL rules created. This is used by @@ -673,8 +673,10 @@ class Flask(_PackageBoundObject): if 'autoescape' not in options: options['autoescape'] = self.select_jinja_autoescape if 'auto_reload' not in options: - options['auto_reload'] = self.debug \ - or self.config['TEMPLATES_AUTO_RELOAD'] + if self.config['TEMPLATES_AUTO_RELOAD'] is not None: + options['auto_reload'] = self.config['TEMPLATES_AUTO_RELOAD'] + else: + options['auto_reload'] = self.debug rv = Environment(self, **options) rv.globals.update( url_for=url_for, diff --git a/tests/test_templating.py b/tests/test_templating.py index 5aaf9081..862b9b1f 100644 --- a/tests/test_templating.py +++ b/tests/test_templating.py @@ -296,12 +296,36 @@ def test_iterable_loader(): assert rv.data == b'

Jameson

' def test_templates_auto_reload(): + # debug is False, config option is None app = flask.Flask(__name__) - assert app.config['TEMPLATES_AUTO_RELOAD'] - assert app.jinja_env.auto_reload + assert app.debug is False + assert app.config['TEMPLATES_AUTO_RELOAD'] is None + assert app.jinja_env.auto_reload is False + # debug is False, config option is False app = flask.Flask(__name__) app.config['TEMPLATES_AUTO_RELOAD'] = False - assert not app.jinja_env.auto_reload + assert app.debug is False + assert app.jinja_env.auto_reload is False + # debug is False, config option is True + app = flask.Flask(__name__) + app.config['TEMPLATES_AUTO_RELOAD'] = True + assert app.debug is False + assert app.jinja_env.auto_reload is True + # debug is True, config option is None + app = flask.Flask(__name__) + app.config['DEBUG'] = True + assert app.config['TEMPLATES_AUTO_RELOAD'] is None + assert app.jinja_env.auto_reload is True + # debug is True, config option is False + app = flask.Flask(__name__) + app.config['DEBUG'] = True + app.config['TEMPLATES_AUTO_RELOAD'] = False + assert app.jinja_env.auto_reload is False + # debug is True, config option is True + app = flask.Flask(__name__) + app.config['DEBUG'] = True + app.config['TEMPLATES_AUTO_RELOAD'] = True + assert app.jinja_env.auto_reload is True def test_template_loader_debugging(test_apps): from blueprintapp import app From 76f3d6b45e76b9979375ef2519cc6164b07c54c1 Mon Sep 17 00:00:00 2001 From: defuz Date: Thu, 30 Oct 2014 18:40:38 +0300 Subject: [PATCH 2/3] improve TEMPLATE_AUTO_RELOAD docs --- CHANGES | 5 ++--- docs/config.rst | 11 +++++------ 2 files changed, 7 insertions(+), 9 deletions(-) diff --git a/CHANGES b/CHANGES index f028df2c..29218ade 100644 --- a/CHANGES +++ b/CHANGES @@ -23,9 +23,8 @@ Version 1.0 - Added :meth:`flask.Config.from_json`. - Added :attr:`flask.Flask.config_class`. - Added :meth:`flask.config.Config.get_namespace`. -- Added ``TEMPLATES_AUTO_RELOAD`` config key. Now by default templates will be - reloaded every time a template is requested only if the application is - running in debug mode. +- Templates are no longer automatically reloaded outside of debug mode. This + can be configured with the new ``TEMPLATES_AUTO_RELOAD`` config key. - Added a workaround for a limitation in Python 3.3's namespace loader. - Added support for explicit root paths when using Python 3.3's namespace packages. diff --git a/docs/config.rst b/docs/config.rst index d20f9a33..eaaa0ae6 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -182,12 +182,11 @@ The following configuration values are used internally by Flask: if they are not requested by an XMLHttpRequest object (controlled by the ``X-Requested-With`` header) -``TEMPLATES_AUTO_RELOAD`` If this is set to `True` every time a template - is requested Flask checks if the template was - modified and if yes, it will reload the - template. By default the value is ``None`` - which means that Flask checks template - sources only in debug mode. +``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of + the template source and reload it + automatically. By default the value is + `None` which means that Flask checks + original file only in debug mode. ``EXPLAIN_TEMPLATE_LOADING`` If this is enabled then every attempt to load a template will write an info message to the logger explaining the From 6a6acfbb091229ccc28215e92868fa45ec4c2d79 Mon Sep 17 00:00:00 2001 From: Ivan Ivaschenko Date: Sun, 2 Nov 2014 22:41:38 +0300 Subject: [PATCH 3/3] Using double-backticks: ``None`` --- docs/config.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/config.rst b/docs/config.rst index eaaa0ae6..c8468d02 100644 --- a/docs/config.rst +++ b/docs/config.rst @@ -185,7 +185,7 @@ The following configuration values are used internally by Flask: ``TEMPLATES_AUTO_RELOAD`` Whether to check for modifications of the template source and reload it automatically. By default the value is - `None` which means that Flask checks + ``None`` which means that Flask checks original file only in debug mode. ``EXPLAIN_TEMPLATE_LOADING`` If this is enabled then every attempt to load a template will write an info