From 1843d6d4110f7d46094fa71cdf1eef998fd7cf71 Mon Sep 17 00:00:00 2001 From: FawziyahAlebiosu Date: Thu, 18 Jun 2020 16:06:46 -0400 Subject: [PATCH] deprecate 'with' and 'autoescape' extensions --- CHANGES.rst | 1 + src/jinja2/ext.py | 19 +++++++++++++++++-- tests/test_ext.py | 21 ++++++++++----------- tests/test_regression.py | 4 +--- 4 files changed, 29 insertions(+), 16 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 03c162a4..658d2d37 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -21,6 +21,7 @@ Unreleased ``NativeEnvironment`` on Python 3.10. :issue:`1335` - Add ``required`` attribute to blocks that must be overridden at some point, but not necessarily by the direct child :issue:`1147` +- Deprecate ``autoescape`` and ``with`` extensions :issue:`1203` Version 2.11.2 diff --git a/src/jinja2/ext.py b/src/jinja2/ext.py index 4b89cf40..73a2e779 100644 --- a/src/jinja2/ext.py +++ b/src/jinja2/ext.py @@ -1,6 +1,7 @@ """Extension API for adding custom tags and behavior.""" import pprint import re +import warnings from sys import version_info from typing import Set @@ -457,11 +458,25 @@ class LoopControlExtension(Extension): class WithExtension(Extension): - pass + def __init__(self, environment): + super().__init__(environment) + warnings.warn( + "The 'with' extension is deprecated and will be removed in" + " version 3.1. This is built in now.", + DeprecationWarning, + stacklevel=2, + ) class AutoEscapeExtension(Extension): - pass + def __init__(self, environment): + super().__init__(environment) + warnings.warn( + "The 'autoescape' extension is deprecated and will be removed in" + " version 3.1. This is built in now.", + DeprecationWarning, + stacklevel=2, + ) class DebugExtension(Extension): diff --git a/tests/test_ext.py b/tests/test_ext.py index 61abdc5b..261abd21 100644 --- a/tests/test_ext.py +++ b/tests/test_ext.py @@ -165,7 +165,6 @@ class StreamFilterExtension(Extension): class TestExtensions: def test_extend_late(self): env = Environment() - env.add_extension("jinja2.ext.autoescape") t = env.from_string('{% autoescape true %}{{ "" }}{% endautoescape %}') assert t.render() == "<test>" @@ -468,7 +467,7 @@ class TestNewstyleInternationalization: assert tmpl.render(LANGUAGE="de", apples=5) == "5 Äpfel" def test_autoescape_support(self): - env = Environment(extensions=["jinja2.ext.autoescape", "jinja2.ext.i18n"]) + env = Environment(extensions=["jinja2.ext.i18n"]) env.install_gettext_callables( lambda x: "Wert: %(name)s", lambda s, p, n: s, @@ -482,7 +481,7 @@ class TestNewstyleInternationalization: assert t.render(ae=False) == "Wert: " def test_autoescape_macros(self): - env = Environment(autoescape=False, extensions=["jinja2.ext.autoescape"]) + env = Environment(autoescape=False) template = ( "{% macro m() %}{% endmacro %}" "{% autoescape true %}{{ m() }}{% endautoescape %}" @@ -529,7 +528,7 @@ class TestNewstyleInternationalization: class TestAutoEscape: def test_scoped_setting(self): - env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True) + env = Environment(autoescape=True) tmpl = env.from_string( """ {{ "" }} @@ -545,7 +544,7 @@ class TestAutoEscape: "<HelloWorld>", ] - env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=False) + env = Environment(autoescape=False) tmpl = env.from_string( """ {{ "" }} @@ -562,7 +561,7 @@ class TestAutoEscape: ] def test_nonvolatile(self): - env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True) + env = Environment(autoescape=True) tmpl = env.from_string('{{ {"foo": ""}|xmlattr|escape }}') assert tmpl.render() == ' foo="<test>"' tmpl = env.from_string( @@ -572,7 +571,7 @@ class TestAutoEscape: assert tmpl.render() == " foo="&lt;test&gt;"" def test_volatile(self): - env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True) + env = Environment(autoescape=True) tmpl = env.from_string( '{% autoescape foo %}{{ {"foo": ""}' "|xmlattr|escape }}{% endautoescape %}" @@ -581,7 +580,7 @@ class TestAutoEscape: assert tmpl.render(foo=True) == ' foo="<test>"' def test_scoping(self): - env = Environment(extensions=["jinja2.ext.autoescape"]) + env = Environment() tmpl = env.from_string( '{% autoescape true %}{% set x = "" %}{{ x }}' '{% endautoescape %}{{ x }}{{ "" }}' @@ -589,7 +588,7 @@ class TestAutoEscape: assert tmpl.render(x=1) == "<x>1" def test_volatile_scoping(self): - env = Environment(extensions=["jinja2.ext.autoescape"]) + env = Environment() tmplsource = """ {% autoescape val %} {% macro foo(x) %} @@ -605,11 +604,11 @@ class TestAutoEscape: # looking at the source we should see there in raw # (and then escaped as well) - env = Environment(extensions=["jinja2.ext.autoescape"]) + env = Environment() pysource = env.compile(tmplsource, raw=True) assert "\\n" in pysource - env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True) + env = Environment(autoescape=True) pysource = env.compile(tmplsource, raw=True) assert "<testing>\\n" in pysource diff --git a/tests/test_regression.py b/tests/test_regression.py index 65ace87f..d052f43e 100644 --- a/tests/test_regression.py +++ b/tests/test_regression.py @@ -355,9 +355,7 @@ class TestBug: assert t.render().strip() == "45|6" def test_macro_escaping(self): - env = Environment( - autoescape=lambda x: False, extensions=["jinja2.ext.autoescape"] - ) + env = Environment(autoescape=lambda x: False) template = "{% macro m() %}{% endmacro %}" template += "{% autoescape true %}{{ m() }}{% endautoescape %}" assert env.from_string(template).render() -- 2.47.2