Skip to content
Myr

nativetypes.py

This file

Path

src/jinja2/nativetypes.py

130 lines · 4.1 KiB · Python

Last changed by

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

History of this file

Take it

The walk stopped after 200 commits: lines older than d312609 point at it.

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 1why, one step back
1import typing as t

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 2why, one step back
2from ast import literal_eval

Merge branch '3.0.x'David Lordbefore adoptiona42b291

line 3why, one step back
3from ast import parse

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 4–5why, one step back
4from itertools import chain
5from itertools import islice

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

line 6why, one step back
6from types import GeneratorType

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 7–9why, one step back
7
8from . import nodes9from .compiler import CodeGenerator

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 10why, one step back
10from .compiler import Frame

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 11–15why, one step back
11from .compiler import has_safe_repr
12from .environment import Environment13from .environment import Template1415

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 16why, one step back
16def native_concat(values: t.Iterable[t.Any]) -> t.Optional[t.Any]:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 17–22why, one step back
17 """Return a native Python type from the list of compiled nodes. If
18 the result is a single node, its value is returned. Otherwise, the19 nodes are concatenated as strings. If the result can be parsed with20 :func:`ast.literal_eval`, the parsed value is returned. Otherwise,21 the string is returned.22

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 23why, one step back
23 :param values: Iterable of outputs to concatenate.

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 24why, one step back
24 """

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 25why, one step back
25 head = list(islice(values, 2))

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 26–31why, one step back
26
27 if not head:28 return None2930 if len(head) == 1:31 raw = head[0]

Merge pull request #1339 from mkrizek/issue-1335David Lordbefore adoptionbc22a8b

lines 32–33why, one step back
32 if not isinstance(raw, str):
33 return raw

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 34why, one step back
34 else:

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

lines 35–37why, one step back
35 if isinstance(values, GeneratorType):
36 values = chain(head, values)37 raw = "".join([str(v) for v in values])

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 38–39why, one step back
38
39 try:

Merge branch '3.0.x'David Lordbefore adoptiona42b291

lines 40–45why, one step back
40 return literal_eval(
41 # In Python 3.10+ ast.literal_eval removes leading spaces/tabs42 # from the given string. For backwards compatibility we need to43 # parse the string ourselves without removing leading spaces/tabs.44 parse(raw, mode="eval")45 )

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 46–51why, one step back
46 except (ValueError, SyntaxError, MemoryError):
47 return raw484950class NativeCodeGenerator(CodeGenerator):51 """A code generator which renders Python types by not adding

Merge branch '2.11.x'David Lordbefore adoptionda81281

line 52why, one step back
52 ``str()`` around output nodes.

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 53–55why, one step back
53 """
5455 @staticmethod

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 56why, one step back
56 def _default_finalize(value: t.Any) -> t.Any:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 57–58why, one step back
57 return value
58

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 59why, one step back
59 def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:

Merge branch '2.11.x'David Lordbefore adoptionda81281

line 60why, one step back
60 return repr("".join([str(v) for v in group]))

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 61why, one step back
61

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 62–64why, one step back
62 def _output_child_to_const(
63 self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo64 ) -> t.Any:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 65–72why, one step back
65 const = node.as_const(frame.eval_ctx)
6667 if not has_safe_repr(const):68 raise nodes.Impossible()6970 if isinstance(node, nodes.TemplateData):71 return const72

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 73why, one step back
73 return finalize.const(const) # type: ignore

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 74why, one step back
74

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 75–77why, one step back
75 def _output_child_pre(
76 self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo77 ) -> None:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 78–80why, one step back
78 if finalize.src is not None:
79 self.write(finalize.src)80

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 81–83why, one step back
81 def _output_child_post(
82 self, node: nodes.Expr, frame: Frame, finalize: CodeGenerator._FinalizeInfo83 ) -> None:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 84–91why, one step back
84 if finalize.src is not None:
85 self.write(")")868788class NativeEnvironment(Environment):89 """An environment that renders templates to native Python types."""9091 code_generator_class = NativeCodeGenerator

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

line 92why, one step back
92 concat = staticmethod(native_concat) # type: ignore

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 93–97why, one step back
93
9495class NativeTemplate(Template):96 environment_class = NativeEnvironment97

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 98why, one step back
98 def render(self, *args: t.Any, **kwargs: t.Any) -> t.Any:

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 99–104why, one step back
99 """Render the template to produce a native Python type. If the
100 result is a single node, its value is returned. Otherwise, the101 nodes are concatenated as strings. If the result can be parsed102 with :func:`ast.literal_eval`, the parsed value is returned.103 Otherwise, the string is returned.104 """

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 105why, one step back
105 ctx = self.new_context(dict(*args, **kwargs))

Merge branch '2.11.x'David Lordbefore adoptionda81281

line 106why, one step back
106

isolate bytecode cache testsDavid Lordbefore adoptiond312609

line 107why, one step back
107 try:

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

lines 108–110why, one step back
108 return self.environment_class.concat( # type: ignore
109 self.root_render_func(ctx) # type: ignore110 )

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 111–113why, one step back
111 except Exception:
112 return self.environment.handle_exception()113

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

line 114why, one step back
114 async def render_async(self, *args: t.Any, **kwargs: t.Any) -> t.Any:

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

lines 115–119why, one step back
115 if not self.environment.is_async:
116 raise RuntimeError(117 "The environment was not created with async mode enabled."118 )119

Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b

line 120why, one step back
120 ctx = self.new_context(dict(*args, **kwargs))

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

lines 121–122why, one step back
121
122 try:

Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7

line 123why, one step back
123 return self.environment_class.concat( # type: ignore

Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b

lines 124–125why, one step back
124 [n async for n in self.root_render_func(ctx)] # type: ignore
125 )

Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb

lines 126–128why, one step back
126 except Exception:
127 return self.environment.handle_exception()128

isolate bytecode cache testsDavid Lordbefore adoptiond312609

lines 129–130why, one step back
129
130NativeEnvironment.template_class = NativeTemplate