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 fileTake 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 back1import typing as tisolate bytecode cache testsDavid Lordbefore adoptiond312609
line 2why, one step back2from ast import literal_evalMerge branch '3.0.x'David Lordbefore adoptiona42b291
line 3why, one step back3from ast import parseisolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 4–5why, one step back4from itertools import chain5from itertools import isliceMerge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
line 6why, one step back6from types import GeneratorTypeisolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 7–9why, one step back78from . import nodes9from .compiler import CodeGeneratorMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 10why, one step back10from .compiler import Frameisolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 11–15why, one step back11from .compiler import has_safe_repr12from .environment import Environment13from .environment import Template1415Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 16why, one step back16def native_concat(values: t.Iterable[t.Any]) -> t.Optional[t.Any]:isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 17–22why, one step back17 """Return a native Python type from the list of compiled nodes. If18 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.22Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 23why, one step back23 :param values: Iterable of outputs to concatenate.isolate bytecode cache testsDavid Lordbefore adoptiond312609
line 24why, one step back24 """Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 25why, one step back25 head = list(islice(values, 2))isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 26–31why, one step back2627 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 back32 if not isinstance(raw, str):33 return rawisolate bytecode cache testsDavid Lordbefore adoptiond312609
line 34why, one step back34 else:Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
lines 35–37why, one step back35 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 back3839 try:Merge branch '3.0.x'David Lordbefore adoptiona42b291
lines 40–45why, one step back40 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 back46 except (ValueError, SyntaxError, MemoryError):47 return raw484950class NativeCodeGenerator(CodeGenerator):51 """A code generator which renders Python types by not addingMerge branch '2.11.x'David Lordbefore adoptionda81281
line 52why, one step back52 ``str()`` around output nodes.isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 53–55why, one step back53 """5455 @staticmethodMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 56why, one step back56 def _default_finalize(value: t.Any) -> t.Any:isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 57–58why, one step back57 return value58Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 59why, one step back59 def _output_const_repr(self, group: t.Iterable[t.Any]) -> str:Merge branch '2.11.x'David Lordbefore adoptionda81281
line 60why, one step back60 return repr("".join([str(v) for v in group]))isolate bytecode cache testsDavid Lordbefore adoptiond312609
line 61why, one step back61Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 62–64why, one step back62 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 back65 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 const72Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 73why, one step back73 return finalize.const(const) # type: ignoreisolate bytecode cache testsDavid Lordbefore adoptiond312609
line 74why, one step back74Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 75–77why, one step back75 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 back78 if finalize.src is not None:79 self.write(finalize.src)80Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 81–83why, one step back81 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 back84 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 = NativeCodeGeneratorMerge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
line 92why, one step back92 concat = staticmethod(native_concat) # type: ignoreisolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 93–97why, one step back939495class NativeTemplate(Template):96 environment_class = NativeEnvironment97Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 98why, one step back98 def render(self, *args: t.Any, **kwargs: t.Any) -> t.Any:isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 99–104why, one step back99 """Render the template to produce a native Python type. If the100 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 back105 ctx = self.new_context(dict(*args, **kwargs))Merge branch '2.11.x'David Lordbefore adoptionda81281
line 106why, one step back106isolate bytecode cache testsDavid Lordbefore adoptiond312609
line 107why, one step back107 try:Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
lines 108–110why, one step back108 return self.environment_class.concat( # type: ignore109 self.root_render_func(ctx) # type: ignore110 )isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 111–113why, one step back111 except Exception:112 return self.environment.handle_exception()113Merge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
line 114why, one step back114 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 back115 if not self.environment.is_async:116 raise RuntimeError(117 "The environment was not created with async mode enabled."118 )119Merge pull request #1392 from pallets/inline-asyncDavid Lordbefore adoption9d4689b
line 120why, one step back120 ctx = self.new_context(dict(*args, **kwargs))Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb
lines 121–122why, one step back121122 try:Merge pull request #1511 from mkrizek/issue-1510David Lordbefore adoption7d72eb7
line 123why, one step back123 return self.environment_class.concat( # type: ignoreMerge pull request #1412 from pallets/typingDavid Lordbefore adoption1a3342b
lines 124–125why, one step back124 [n async for n in self.root_render_func(ctx)] # type: ignore125 )Merge pull request #1363 from remidebette/add-native-async-supportDavid Lordbefore adoption59de6fb
lines 126–128why, one step back126 except Exception:127 return self.environment.handle_exception()128isolate bytecode cache testsDavid Lordbefore adoptiond312609
lines 129–130why, one step back129130NativeEnvironment.template_class = NativeTemplate