Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deprecate 'autoescape' and 'with' extensions #1243

Merged
merged 1 commit into from
Jan 30, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 17 additions & 2 deletions src/jinja2/ext.py
Original file line number Diff line number Diff line change
@@ -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

Expand Down Expand Up @@ -457,11 +458,25 @@ def parse(self, parser):


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):
Expand Down
21 changes: 10 additions & 11 deletions tests/test_ext.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,6 @@ def interpolate(self, token):
class TestExtensions:
def test_extend_late(self):
env = Environment()
env.add_extension("jinja2.ext.autoescape")
t = env.from_string('{% autoescape true %}{{ "<test>" }}{% endautoescape %}')
assert t.render() == "&lt;test&gt;"

Expand Down Expand Up @@ -468,7 +467,7 @@ def test_newstyle_plural(self):
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: "<strong>Wert: %(name)s</strong>",
lambda s, p, n: s,
Expand All @@ -482,7 +481,7 @@ def test_autoescape_support(self):
assert t.render(ae=False) == "<strong>Wert: <test></strong>"

def test_autoescape_macros(self):
env = Environment(autoescape=False, extensions=["jinja2.ext.autoescape"])
env = Environment(autoescape=False)
template = (
"{% macro m() %}<html>{% endmacro %}"
"{% autoescape true %}{{ m() }}{% endautoescape %}"
Expand Down Expand Up @@ -529,7 +528,7 @@ def test_novars_vars_escaping(self):

class TestAutoEscape:
def test_scoped_setting(self):
env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)
env = Environment(autoescape=True)
tmpl = env.from_string(
"""
{{ "<HelloWorld>" }}
Expand All @@ -545,7 +544,7 @@ def test_scoped_setting(self):
"&lt;HelloWorld&gt;",
]

env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=False)
env = Environment(autoescape=False)
tmpl = env.from_string(
"""
{{ "<HelloWorld>" }}
Expand All @@ -562,7 +561,7 @@ def test_scoped_setting(self):
]

def test_nonvolatile(self):
env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)
env = Environment(autoescape=True)
tmpl = env.from_string('{{ {"foo": "<test>"}|xmlattr|escape }}')
assert tmpl.render() == ' foo="&lt;test&gt;"'
tmpl = env.from_string(
Expand All @@ -572,7 +571,7 @@ def test_nonvolatile(self):
assert tmpl.render() == " foo=&#34;&amp;lt;test&amp;gt;&#34;"

def test_volatile(self):
env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)
env = Environment(autoescape=True)
tmpl = env.from_string(
'{% autoescape foo %}{{ {"foo": "<test>"}'
"|xmlattr|escape }}{% endautoescape %}"
Expand All @@ -581,15 +580,15 @@ def test_volatile(self):
assert tmpl.render(foo=True) == ' foo="&lt;test&gt;"'

def test_scoping(self):
env = Environment(extensions=["jinja2.ext.autoescape"])
env = Environment()
tmpl = env.from_string(
'{% autoescape true %}{% set x = "<x>" %}{{ x }}'
'{% endautoescape %}{{ x }}{{ "<y>" }}'
)
assert tmpl.render(x=1) == "&lt;x&gt;1<y>"

def test_volatile_scoping(self):
env = Environment(extensions=["jinja2.ext.autoescape"])
env = Environment()
tmplsource = """
{% autoescape val %}
{% macro foo(x) %}
Expand All @@ -605,11 +604,11 @@ def test_volatile_scoping(self):

# looking at the source we should see <testing> there in raw
# (and then escaped as well)
env = Environment(extensions=["jinja2.ext.autoescape"])
env = Environment()
pysource = env.compile(tmplsource, raw=True)
assert "<testing>\\n" in pysource

env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)
env = Environment(autoescape=True)
pysource = env.compile(tmplsource, raw=True)
assert "&lt;testing&gt;\\n" in pysource

Expand Down
4 changes: 1 addition & 3 deletions tests/test_regression.py
Original file line number Diff line number Diff line change
Expand Up @@ -355,9 +355,7 @@ def test_callable_defaults(self):
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() %}<html>{% endmacro %}"
template += "{% autoescape true %}{{ m() }}{% endautoescape %}"
assert env.from_string(template).render()
Expand Down