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
Provenance
- Commit
7d72eb7- Tree
9afc5d1- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
b285d0c- Parent 2
2903565
6 files changed+25 added−6 removed
modified:src/jinja2/compiler.py+1 added
722 unmodified lines723 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 linesmodified:src/jinja2/environment.py+6 added-2 removed
279 unmodified lines280 #: the class that is used for code generation. See281 #: :class:`~jinja2.compiler.CodeGenerator` for more information.282 code_generator_class: t.Type["CodeGenerator"] = CodeGenerator283 284+ concat = "".join285+286 #: 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 lines1283 1284 ctx = self.new_context(dict(*args, **kwargs))1285 1286 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()1290 1291 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> str:12 unmodified lines1304 1305 ctx = self.new_context(dict(*args, **kwargs))1306 1307 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()1313 1314 def stream(self, *args: t.Any, **kwargs: t.Any) -> "TemplateStream":341 unmodified linesmodified:src/jinja2/nativetypes.py+9 added-3 removed
1 unmodified line2 from ast import literal_eval3 from ast import parse4 from itertools import chain5 from itertools import islice6+from types import GeneratorType7 8 from . import nodes9 from .compiler import CodeGenerator10 from .compiler import Frame20 unmodified lines31 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])38 39 try:40 return literal_eval(41 # In Python 3.10+ ast.literal_eval removes leading spaces/tabs46 unmodified lines88 class NativeEnvironment(Environment):89 """An environment that renders templates to native Python types."""90 91 code_generator_class = NativeCodeGenerator92+ concat = staticmethod(native_concat) # type: ignore93 94 95 class NativeTemplate(Template):96 environment_class = NativeEnvironment7 unmodified lines104 """105 ctx = self.new_context(dict(*args, **kwargs))106 107 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()113 114 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> t.Any:4 unmodified lines119 120 ctx = self.new_context(dict(*args, **kwargs))121 122 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 linesmodified:src/jinja2/runtime.py-1 removed
47 unmodified lines48 "Macro",49 "Markup",50 "TemplateRuntimeError",51 "missing",52- "concat",52 "escape",53 "markup_join",54 "str_join",55 "identity",998 unmodified linesmodified:tests/test_nativetypes.py+7 added
151 unmodified lines152 def test_leading_spaces(env):153 t = env.from_string(" {{ True }}")154 result = t.render()155 assert result == " True"156+157+158+def 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 lines21 supported. Override ``resolve_or_missing`` instead of22 ``resolve``.23 - ``unicode_urlencode`` is renamed to ``url_quote``.24 25+- Add support for native types in macros. :issue:`1510`26 27+28 Version 3.0.329 -------------30 31 Released 2021-11-09909 unmodified lines