Skip to content

Commit ee08594

Browse files
committed
apply code review changes
- added test for `runtime.py`'s `markup_join` and fixed wrong return - reverted warning for escape and Markup imports as they will be already removed in 3.1 - changed change messages from 3.0 as PR will be part of 3.1
1 parent 8c1ec6d commit ee08594

File tree

11 files changed

+97
-51
lines changed

11 files changed

+97
-51
lines changed

CHANGES.rst

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ Version 3.1.0
55

66
Unreleased
77

8+
- Allow custom autoescape functions :issue:`1377`
9+
- Removed hardcoded Markup and escape calls
10+
811

912
Version 3.0.2
1013
-------------
@@ -37,8 +40,6 @@ Released 2021-05-11
3740

3841
- Drop support for Python 2.7 and 3.5.
3942
- Bump MarkupSafe dependency to >=1.1.
40-
- Allow custom autoescape functions :issue:`1377`
41-
- Removed hardcoded Markup and escape calls
4243
- Bump Babel optional dependency to >=2.1.
4344
- Remove code that was marked deprecated.
4445
- Add type hinting. :pr:`1412`

docs/api.rst

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ useful if you want to dig deeper into Jinja or :ref:`develop extensions
224224

225225
Safe Strings and Escaping
226226
-------------------------
227-
.. versionchanged:: 3.0
227+
.. versionchanged:: 3.1
228228

229229
To handle untrusted input when rendering templates to
230230
avoid injection attacks Jinja uses a combination of trusted strings
@@ -236,7 +236,7 @@ multiple times and at the same time make sure, that using string
236236
operation like ``%`` the original escaped string stays escaped, even
237237
when unescaped string are thrown at it.
238238

239-
Before Jinja 3.0 this was done by the hardcoded
239+
Before Jinja 3.1 this was done by the hardcoded
240240
:class:`markupsafe.Markup` class and
241241
:func:`markupsafe.escape` function from the `MarkupSafe`_ package.
242242
The ``escape(s: str)`` function converts the characters
@@ -257,7 +257,7 @@ This is done in a way so that the result of these operations
257257
in combination with an raw strings is always an escaped ``Markup``
258258
class by using the ``escape`` method of the ``Markup`` class.
259259

260-
With version 3.0 this hardcoded relation to the `MarkupSafe`_ and it's
260+
With version 3.1 this hardcoded relation to the `MarkupSafe`_ and it's
261261
HTML based escaping was removed, as Jinja is intended to be a Language
262262
independent template system.
263263
It is still the default but now you are able to provide a custom escape
@@ -324,7 +324,7 @@ future. It's recommended to configure a sensible default for
324324
autoescaping. This makes it possible to enable and disable autoescaping
325325
on a per-template basis (HTML versus text for instance).
326326

327-
.. versionchanged:: 3.0
327+
.. versionchanged:: 3.1
328328

329329
Jinja now also allows the usage of different escape functions selected
330330
by template suffix.

src/jinja2/compiler.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1702,7 +1702,7 @@ def visit_Concat(self, node: nodes.Concat, frame: Frame) -> None:
17021702
self.visit(arg, frame)
17031703
self.write(", ")
17041704
self.write(")")
1705-
self.write(", escape_func=context.eval_ctx.autoescape")
1705+
self.write(", mark_safe=context.eval_ctx.mark_safe")
17061706
self.write(")")
17071707

17081708
@optimizeconst

src/jinja2/environment.py

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -232,7 +232,7 @@ class Environment:
232232
return ``True`` or ``False`` depending on autoescape should be
233233
enabled by default.
234234
235-
As of Jinja 3.0 the autoescape can be even smarter.
235+
As of Jinja 3.1 the autoescape can be even smarter.
236236
If the given function does not return a boolean but a
237237
function again, this function is considered to be the
238238
escape function that shall be used. So you can use the
@@ -247,7 +247,7 @@ class Environment:
247247
248248
See :ref:`escaping` and :ref:`autoescaping` for details.
249249
250-
.. versionchanged:: 3.0
250+
.. versionchanged:: 3.1
251251
if the `autoescape` function doesn't return True or False but a
252252
callable, it is assumed to be a custom escape function
253253
@@ -265,7 +265,7 @@ class Environment:
265265
266266
Defaults to False
267267
268-
.. versionadded:: 3.0
268+
.. versionadded:: 3.1
269269
270270
`default_escape`
271271
define a custom escape function or class.
@@ -288,7 +288,7 @@ class Environment:
288288
This setting will also overwrite the filter ``{{ var | safe }}``,
289289
``{{ var | e }}`` and ``{{ var | escape }}`` accordingly.
290290
291-
.. versionadded:: 3.0
291+
.. versionadded:: 3.1
292292
293293
`loader`
294294
The template loader for this environment.
@@ -450,7 +450,7 @@ def get_markup_class(self, template_name: t.Optional[str] = None) -> t.Type[Mark
450450
for special escpaing in the autoescape
451451
settings
452452
453-
.. versionadded:: 3.0
453+
.. versionadded:: 3.1
454454
"""
455455
if callable(self.autoescape) and callable(self.autoescape(template_name)):
456456
return get_wrapped_escape_class(self.autoescape(template_name))
@@ -1120,7 +1120,7 @@ def get_template(
11201120
function was calling it, i.e. 'extends' or 'include'.
11211121
Required to define behavior for custom autoescape.
11221122
1123-
.. versionchanged:: 3.0
1123+
.. versionchanged:: 3.1
11241124
Added caller parameter and a check if we need to raise an
11251125
error due to usage different autoescape function within
11261126
extends
@@ -1165,7 +1165,7 @@ def select_template(
11651165
function was calling it, i.e. 'extends' or 'include'.
11661166
Required to define behavior for custom autoescape.
11671167
1168-
.. versionchanged:: 3.0
1168+
.. versionchanged:: 3.1
11691169
Added caller parameter
11701170
11711171
.. versionchanged:: 3.0
@@ -1233,7 +1233,7 @@ def get_or_select_template(
12331233
function was calling it, i.e. 'extends' or 'include'.
12341234
Required to define behavior for custom autoescape.
12351235
1236-
.. versionchanged:: 3.0
1236+
.. versionchanged:: 3.1
12371237
Added caller parameter
12381238
12391239
.. versionadded:: 2.3

src/jinja2/filters.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def do_escape(eval_ctx: "EvalContext", s: t.Union[str, "HasHTML"]) -> markupsafe
106106
Escape a string with the escape function active in the current
107107
eval context
108108
109-
.. versionadded:: 3.0
109+
.. versionadded:: 3.1
110110
replaced the hard coded HTML :func:`markupsafe.escape` function
111111
with an context aware escape function
112112
"""
@@ -210,7 +210,7 @@ def do_forceescape(
210210
"""
211211
Enforce HTML escaping. This will probably double escape variables.
212212
213-
.. versionchanged:: 3.0
213+
.. versionchanged:: 3.1
214214
made function context aware to use context based escape filter
215215
"""
216216
if hasattr(value, "__html__"):
@@ -271,7 +271,7 @@ def do_replace(
271271
{{ "aaaaargh"|replace("a", "d'oh, ", 2) }}
272272
-> d'oh, d'oh, aaargh
273273
274-
.. versionchanged:: 3.0
274+
.. versionchanged:: 3.1
275275
made function context aware to use context based escape filter
276276
"""
277277
if count is None:
@@ -631,7 +631,7 @@ def sync_do_join(
631631
632632
{{ users|join(', ', attribute='username') }}
633633
634-
.. versionchanged:: 3.0
634+
.. versionchanged:: 3.1
635635
made function context aware to use context based escape filter
636636
637637
.. versionadded:: 2.6
@@ -808,7 +808,7 @@ def do_urlize(
808808
``env.policies["urlize.extra_schemes"]``, which defaults to no
809809
extra schemes.
810810
811-
.. versionchanged:: 3.0
811+
.. versionchanged:: 3.1
812812
made function context aware to use context based escape filter
813813
814814
.. versionchanged:: 3.0
@@ -874,7 +874,7 @@ def do_indent(
874874
:param first: Don't skip indenting the first line.
875875
:param blank: Don't skip indenting empty lines.
876876
877-
.. versionchanged:: 3.0
877+
.. versionchanged:: 3.1
878878
made function context aware to use context based escape filter
879879
880880
.. versionchanged:: 3.0

src/jinja2/nodes.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,11 @@ class EvalContext:
7070
"""Holds evaluation time information. Custom attributes can be attached
7171
to it in extensions.
7272
73-
.. versionchanged:: 3.0
73+
.. versionchanged:: 3.1
7474
75-
- Added ``get_escape_function`` and ``mark_safe`` functions
76-
- allow autoescape to be not only boolean but also an
77-
escape function
75+
- Added ``get_escape_function`` and ``mark_safe`` functions
76+
- allow autoescape to be not only boolean but also an
77+
escape function
7878
"""
7979

8080
def __init__(
@@ -98,7 +98,7 @@ def get_escape_function(self) -> t.Callable[[t.Any], "Markup"]:
9898
"""
9999
return the currently valid escape function
100100
101-
.. versionadded:: 3.0
101+
.. versionadded:: 3.1
102102
103103
"""
104104
return self._markup_class.escape
@@ -111,7 +111,7 @@ def mark_safe(self, input: str) -> "Markup":
111111
if possible so custom escape functions
112112
are correctly handled by the Markup class.
113113
114-
.. versionadded:: 3.0
114+
.. versionadded:: 3.1
115115
116116
"""
117117
return self._markup_class(input)

src/jinja2/runtime.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@
55
from collections import abc
66
from itertools import chain
77

8-
from markupsafe import escape as html_escape # noqa: F401
9-
from markupsafe import soft_str
8+
import markupsafe
109

1110
from .async_utils import auto_aiter
1211
from .async_utils import auto_await # noqa: F401
@@ -41,6 +40,8 @@ def __call__(
4140
...
4241

4342

43+
html_escape = markupsafe.escape
44+
4445
# these variables are exported to the template runtime
4546
exported = [
4647
"LoopContext",
@@ -72,30 +73,32 @@ def identity(x: V) -> V:
7273
return x
7374

7475

75-
def markup_join(seq: t.Iterable[t.Any], escape_func: EscapeFunc = html_escape) -> str:
76+
def markup_join(
77+
seq: t.Iterable[t.Any], mark_safe: EscapeFunc = markupsafe.Markup
78+
) -> t.Union[str, markupsafe.Markup]:
7679
"""
7780
Concatenation that escapes if necessary and converts to string.
7881
79-
.. versionchanged:: 3.0
82+
.. versionchanged:: 3.1
8083
added optional parameter escape_function to make
81-
use the contex based escape function
84+
use the context based escape function
8285
"""
8386
buf = []
84-
iterator = map(soft_str, seq)
87+
iterator = map(markupsafe.soft_str, seq)
8588
for arg in iterator:
8689
buf.append(arg)
8790
if hasattr(arg, "__html__"):
88-
return "".join(map(escape_func, chain(buf, iterator)))
91+
return mark_safe("").join(chain(buf, iterator))
8992
return concat(buf)
9093

9194

92-
def str_join(seq: t.Iterable[t.Any], escape_func: EscapeFunc = html_escape) -> str:
95+
def str_join(seq: t.Iterable[t.Any], mark_safe: EscapeFunc = markupsafe.Markup) -> str:
9396
"""
9497
Simple args to string conversion and concatenation.
9598
96-
.. versionchanged:: 3.0
99+
.. versionchanged:: 3.1
97100
added optional and currently ignored parameter
98-
``escape_function`` to allow easier usage of ``markup_join``
101+
``mark_safe`` to allow easier usage of ``markup_join``
99102
"""
100103
return concat(map(str, seq))
101104

src/jinja2/utils.py

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -318,8 +318,11 @@ def urlize(
318318
:param extra_schemes: Recognize URLs that start with these schemes
319319
in addition to the default behavior.
320320
321+
.. versionchanged:: 3.1
322+
The ``do_escape`` parameter was added.
323+
321324
.. versionchanged:: 3.0
322-
The ``extra_schemes`` and ``do_escape`` parameter was added.
325+
The ``extra_schemes`` parameter was added.
323326
324327
.. versionchanged:: 3.0
325328
Generate ``https://`` links for URLs without a scheme.
@@ -422,7 +425,7 @@ def generate_lorem_ipsum(
422425
) -> t.Union[markupsafe.Markup, str]:
423426
"""Generate some lorem ipsum for the template.
424427
425-
.. versionchanged:: 3.0
428+
.. versionchanged:: 3.1
426429
added mark_safe and do_escape parameter
427430
"""
428431
from .constants import LOREM_IPSUM_WORDS
@@ -714,7 +717,7 @@ def select_autoescape(
714717
715718
.. versionadded:: 2.9
716719
created function
717-
.. versionchanged:: 3.0
720+
.. versionchanged:: 3.1
718721
parameter ``special_extensions`` was added
719722
"""
720723

@@ -780,9 +783,12 @@ def htmlsafe_json_dumps(
780783
:param kwargs: Extra arguments to pass to ``dumps``. Merged onto
781784
``env.policies["json.dumps_kwargs"]``.
782785
786+
.. versionchanged:: 3.1
787+
Added required mark_safe parameter
788+
783789
.. versionchanged:: 3.0
784-
- The ``dumper`` parameter is renamed to ``dumps``.
785-
- Added required mark_safe parameter
790+
The ``dumper`` parameter is renamed to ``dumps``.
791+
786792
787793
.. versionadded:: 2.9
788794
"""
@@ -820,7 +826,7 @@ def get_wrapped_escape_class(
820826
821827
:return: a Markup class using this escape function
822828
823-
.. versionadded:: 3.0
829+
.. versionadded:: 3.1
824830
"""
825831

826832
class MarkupWrapper(markupsafe.Markup):
@@ -950,9 +956,7 @@ class Markup(markupsafe.Markup):
950956
def __new__(cls, base="", encoding=None, errors="strict"): # type: ignore
951957
warnings.warn(
952958
"'jinja2.Markup' is deprecated and will be removed in Jinja"
953-
" 3.1. Use Environment.get_markup_class and "
954-
"EvalContext.mark_safe instead. "
955-
"(See Escape in API Documentation)",
959+
" 3.1. Import 'markupsafe.Markup' instead.",
956960
DeprecationWarning,
957961
stacklevel=2,
958962
)
@@ -961,10 +965,8 @@ def __new__(cls, base="", encoding=None, errors="strict"): # type: ignore
961965

962966
def escape(s: t.Any) -> str:
963967
warnings.warn(
964-
"'jinja2.Markup' is deprecated and will be removed in Jinja"
965-
" 3.1. Use Environment.get_markup_class and "
966-
"EvalContext.get_escape_function instead. "
967-
"(See Escape in API Documentation)",
968+
"'jinja2.escape' is deprecated and will be removed in Jinja"
969+
" 3.1. Import 'markupsafe.escape' instead.",
968970
DeprecationWarning,
969971
stacklevel=2,
970972
)

tests/test_async.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,7 @@ def test_unoptimized_scopes(self, test_env_async):
349349
)
350350
assert t.render().strip() == "(FOO)"
351351

352-
def test_unoptimized_scopes_autoescape(self, return_custom_autoescape):
352+
def test_unoptimized_scopes_autoescape(self):
353353
env = Environment(
354354
loader=DictLoader({"o_printer": "({{ o }})"}),
355355
autoescape=True,

0 commit comments

Comments
 (0)