deprecate 'with' and 'autoescape' extensions
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by FawziyahAlebiosu · committed by Amy · parent 35e8f0a · 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
Provenance
- Commit
1843d6d- Tree
2febcc5- Author
FawziyahAlebiosu <fawziyah.alebiosu@gmail.com>- Committer
Amy <leiamy12@gmail.com>- Parent
35e8f0a
4 files changed+29 added−16 removed
modified:src/jinja2/ext.py+17 added-2 removed
1 """Extension API for adding custom tags and behavior."""2 import pprint3 import re4+import warnings5 from sys import version_info6 from typing import Set7 8 from markupsafe import Markup448 unmodified lines457 return nodes.Continue(lineno=token.lineno)458 459 460 class 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+ )469 470 471 class 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+ )480 481 482 class DebugExtension(Extension):483 """A ``{% debug %}`` tag that dumps the available variables,233 unmodified linesmodified:tests/test_ext.py+10 added-11 removed
163 unmodified lines164 165 class 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() == "<test>"170 171 def test_loop_controls(self):294 unmodified lines466 assert tmpl.render(LANGUAGE="de", apples=1) == "1 Apfel"467 assert tmpl.render(LANGUAGE="de", apples=5) == "5 Äpfel"468 469 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 lines480 assert t.render(ae=True) == "<strong>Wert: <test></strong>"481 assert t.render(ae=False) == "<strong>Wert: <test></strong>"482 483 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 lines527 528 529 class 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 lines543 "<HelloWorld>",544 "<HelloWorld>",545 ]546 548- 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 lines560 "<HelloWorld>",561 ]562 563 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="<test>"'567 tmpl = env.from_string(568 '{% autoescape false %}{{ {"foo": "<test>"}'1 unmodified line570 )571 assert tmpl.render() == " foo="&lt;test&gt;""572 573 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="&lt;test&gt;""580 assert tmpl.render(foo=True) == ' foo="<test>"'581 582 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) == "<x>1<y>"589 590 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 lines603 assert tmpl.render(val=False).split()[0] == "str"604 605 # 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 pysource610 612- env = Environment(extensions=["jinja2.ext.autoescape"], autoescape=True)611+ env = Environment(autoescape=True)612 pysource = env.compile(tmplsource, raw=True)613 assert "<testing>\\n" in pysource614 615 def test_overlay_scopes(self):26 unmodified linesmodified:tests/test_regression.py+1 added-3 removed
353 unmodified lines354 )355 assert t.render().strip() == "45|6"356 357 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 linesmodified:CHANGES.rst+1 added
19 unmodified lines20 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`25 26 27 Version 2.11.228 --------------770 unmodified lines