Skip to content
Myr

Merge pull request #1511 from mkrizek/issue-1510

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

Support native types in macros

authored by David Lord · committed by GitHub · parents b285d0c, 2903565 · Browse files · 6 files+25 added−6 removed
This commit

State

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

Diff stats

6 files+25 added−6 removed

  • tests:+7 added−0 removed
  • other:+18 added−6 removed

6 files+25 added−6 removed

modified:src/jinja2/compiler.py+1 added
722 unmodified lines
723 through a dead branch.724 """725 self.writeline("resolve = context.resolve_or_missing")726 self.writeline("undefined = environment.undefined")727 self.writeline("concat = environment.concat")728 # always use the standard Undefined class for the implicit else of729 # conditional expressions730 self.writeline("cond_expr_undefined = Undefined")731 self.writeline("if 0: yield None")
1226 unmodified lines
modified:src/jinja2/environment.py+6 added-2 removed
279 unmodified lines
280 #: the class that is used for code generation. See281 #: :class:`~jinja2.compiler.CodeGenerator` for more information.282 code_generator_class: t.Type["CodeGenerator"] = CodeGenerator283284 concat = "".join285286 #: the context class that is used for templates. See287 #: :class:`~jinja2.runtime.Context` for more information.288 context_class: t.Type[Context] = Context289
993 unmodified lines
12831284 ctx = self.new_context(dict(*args, **kwargs))12851286 try:1285 return concat(self.root_render_func(ctx)) # type: ignore1287 return self.environment.concat(self.root_render_func(ctx)) # type: ignore1288 except Exception:1289 self.environment.handle_exception()12901291 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:
12 unmodified lines
13041305 ctx = self.new_context(dict(*args, **kwargs))13061307 try:1306 return concat([n async for n in self.root_render_func(ctx)]) # type: ignore1308 return self.environment.concat( # type: ignore1309 [n async for n in self.root_render_func(ctx)] # type: ignore1310 )1311 except Exception:1312 return self.environment.handle_exception()13131314 def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream":
341 unmodified lines
modified:src/jinja2/nativetypes.py+9 added-3 removed
1 unmodified line
2from ast import literal_eval3from ast import parse4from itertools import chain5from itertools import islice6from types import GeneratorType78from . import nodes9from .compiler import CodeGenerator10from .compiler import Frame
20 unmodified lines
31 raw = head[0]32 if not isinstance(raw, str):33 return raw34 else:35 if isinstance(values, GeneratorType):36 values = chain(head, values)34 raw = "".join([str(v) for v in chain(head, values)])37 raw = "".join([str(v) for v in values])3839 try:40 return literal_eval(41 # In Python 3.10+ ast.literal_eval removes leading spaces/tabs
46 unmodified lines
88class NativeEnvironment(Environment):89 """An environment that renders templates to native Python types."""9091 code_generator_class = NativeCodeGenerator92 concat = staticmethod(native_concat) # type: ignore939495class NativeTemplate(Template):96 environment_class = NativeEnvironment
7 unmodified lines
104 """105 ctx = self.new_context(dict(*args, **kwargs))106107 try:104 return native_concat(self.root_render_func(ctx)) # type: ignore108 return self.environment_class.concat( # type: ignore109 self.root_render_func(ctx) # type: ignore110 )111 except Exception:112 return self.environment.handle_exception()113114 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> t.Any:
4 unmodified lines
119120 ctx = self.new_context(dict(*args, **kwargs))121122 try:117 return native_concat(123 return self.environment_class.concat( # type: ignore124 [n async for n in self.root_render_func(ctx)] # type: ignore125 )126 except Exception:127 return self.environment.handle_exception()
3 unmodified lines
modified:src/jinja2/runtime.py-1 removed
47 unmodified lines
48 "Macro",49 "Markup",50 "TemplateRuntimeError",51 "missing",52 "concat",52 "escape",53 "markup_join",54 "str_join",55 "identity",
998 unmodified lines
modified:tests/test_nativetypes.py+7 added
151 unmodified lines
152def test_leading_spaces(env):153 t = env.from_string(" {{ True }}")154 result = t.render()155 assert result == " True"156157158def test_macro(env):159 t = env.from_string("{%- macro x() -%}{{- [1,2] -}}{%- endmacro -%}{{- x()[1] -}}")160 result = t.render()161 assert result == 2162 assert isinstance(result, int)
modified:CHANGES.rst+2 added
20 unmodified lines
21 supported. Override ``resolve_or_missing`` instead of22 ``resolve``.23 - ``unicode_urlencode`` is renamed to ``url_quote``.2425- Add support for native types in macros. :issue:`1510`262728Version 3.0.329-------------3031Released 2021-11-09
909 unmodified lines