Skip to content
Myr

Merge pull request #1243 from MLH-Fellowship/deprecate-extension

Before adoption: this commit came in with the history the project adopted, so no landing records it.

deprecate 'autoescape' and 'with' extensions for removal in 3.1

authored by Amy Lei · committed by GitHub · parents 35e8f0a, 1843d6d · Browse files · 4 files+29 added−16 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+29 added−16 removed

  • tests:+11 added−14 removed
  • other:+18 added−2 removed

4 files+29 added−16 removed

modified:src/jinja2/ext.py+17 added-2 removed
1"""Extension API for adding custom tags and behavior."""2import pprint3import re4import warnings5from sys import version_info6from typing import Set78from markupsafe import Markup
448 unmodified lines
457 return nodes.Continue(lineno=token.lineno)458459460class WithExtension(Extension):460 pass461 def __init__(self, environment):462 super().__init__(environment)463 warnings.warn(464 "The 'with' extension is deprecated and will be removed in"465 " version 3.1. This is built in now.",466 DeprecationWarning,467 stacklevel=2,468 )469470471class AutoEscapeExtension(Extension):464 pass472 def __init__(self, environment):473 super().__init__(environment)474 warnings.warn(475 "The 'autoescape' extension is deprecated and will be removed in"476 " version 3.1. This is built in now.",477 DeprecationWarning,478 stacklevel=2,479 )480481482class DebugExtension(Extension):483 """A ``{% debug %}`` tag that dumps the available variables,
233 unmodified lines
modified:tests/test_ext.py+10 added-11 removed
163 unmodified lines
164165class TestExtensions:166 def test_extend_late(self):167 env = Environment()168 env.add_extension("jinja2.ext.autoescape")168 t = env.from_string('{% autoescape true %}{{ "<test>" }}{% endautoescape %}')169 assert t.render() == "&lt;test&gt;"170171 def test_loop_controls(self):
294 unmodified lines
466 assert tmpl.render(LANGUAGE="de", apples=1) == "1 Apfel"467 assert tmpl.render(LANGUAGE="de", apples=5) == "5 Äpfel"468469 def test_autoescape_support(self):471 env = Environment(extensions=["jinja2.ext.autoescape", "jinja2.ext.i18n"])470 env = Environment(extensions=["jinja2.ext.i18n"])471 env.install_gettext_callables(472 lambda x: "<strong>Wert: %(name)s</strong>",473 lambda s, p, n: s,474 newstyle=True,
5 unmodified lines
480 assert t.render(ae=True) == "<strong>Wert: &lt;test&gt;</strong>"481 assert t.render(ae=False) == "<strong>Wert: <test></strong>"482483 def test_autoescape_macros(self):485 env = Environment(autoescape=False, extensions=["jinja2.ext.autoescape"])484 env = Environment(autoescape=False)485 template = (486 "{% macro m() %}<html>{% endmacro %}"487 "{% autoescape true %}{{ m() }}{% endautoescape %}"488 )
38 unmodified lines
527528529class TestAutoEscape:530 def test_scoped_setting(self):532 env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)531 env = Environment(autoescape=True)532 tmpl = env.from_string(533 """534 {{ "<HelloWorld>" }}535 {% autoescape false %}
7 unmodified lines
543 "<HelloWorld>",544 "&lt;HelloWorld&gt;",545 ]546548 env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=False)547 env = Environment(autoescape=False)548 tmpl = env.from_string(549 """550 {{ "<HelloWorld>" }}551 {% autoescape true %}
8 unmodified lines
560 "<HelloWorld>",561 ]562563 def test_nonvolatile(self):565 env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)564 env = Environment(autoescape=True)565 tmpl = env.from_string('{{ {"foo": "<test>"}|xmlattr|escape }}')566 assert tmpl.render() == ' foo="&lt;test&gt;"'567 tmpl = env.from_string(568 '{% autoescape false %}{{ {"foo": "<test>"}'
1 unmodified line
570 )571 assert tmpl.render() == " foo=&#34;&amp;lt;test&amp;gt;&#34;"572573 def test_volatile(self):575 env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)574 env = Environment(autoescape=True)575 tmpl = env.from_string(576 '{% autoescape foo %}{{ {"foo": "<test>"}'577 "|xmlattr|escape }}{% endautoescape %}"578 )579 assert tmpl.render(foo=False) == " foo=&#34;&amp;lt;test&amp;gt;&#34;"580 assert tmpl.render(foo=True) == ' foo="&lt;test&gt;"'581582 def test_scoping(self):584 env = Environment(extensions=["jinja2.ext.autoescape"])583 env = Environment()584 tmpl = env.from_string(585 '{% autoescape true %}{% set x = "<x>" %}{{ x }}'586 '{% endautoescape %}{{ x }}{{ "<y>" }}'587 )588 assert tmpl.render(x=1) == "&lt;x&gt;1<y>"589590 def test_volatile_scoping(self):592 env = Environment(extensions=["jinja2.ext.autoescape"])591 env = Environment()592 tmplsource = """593 {% autoescape val %}594 {% macro foo(x) %}595 [{{ x }}]
7 unmodified lines
603 assert tmpl.render(val=False).split()[0] == "str"604605 # looking at the source we should see <testing> there in raw606 # (and then escaped as well)608 env = Environment(extensions=["jinja2.ext.autoescape"])607 env = Environment()608 pysource = env.compile(tmplsource, raw=True)609 assert "<testing>\\n" in pysource610612 env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)611 env = Environment(autoescape=True)612 pysource = env.compile(tmplsource, raw=True)613 assert "&lt;testing&gt;\\n" in pysource614615 def test_overlay_scopes(self):
26 unmodified lines
modified:tests/test_regression.py+1 added-3 removed
353 unmodified lines
354 )355 assert t.render().strip() == "45|6"356357 def test_macro_escaping(self):358 env = Environment(359 autoescape=lambda x: False, extensions=["jinja2.ext.autoescape"]360 )358 env = Environment(autoescape=lambda x: False)359 template = "{% macro m() %}<html>{% endmacro %}"360 template += "{% autoescape true %}{{ m() }}{% endautoescape %}"361 assert env.from_string(template).render()362
249 unmodified lines
modified:CHANGES.rst+1 added
19 unmodified lines
20 instead of ``Undefined`` being returned on21 ``NativeEnvironment`` on Python 3.10. :issue:`1335`22- Add ``required`` attribute to blocks that must be overridden at some23 point, but not necessarily by the direct child :issue:`1147`24- Deprecate ``autoescape`` and ``with`` extensions :issue:`1203`252627Version 2.11.228--------------
770 unmodified lines