1- import types
21from ast import literal_eval
32from itertools import chain
43from itertools import islice
1110from .environment import Template
1211
1312
14- def native_concat (nodes , preserve_quotes = True ):
13+ def native_concat (nodes ):
1514 """Return a native Python type from the list of compiled nodes. If
1615 the result is a single node, its value is returned. Otherwise, the
1716 nodes are concatenated as strings. If the result can be parsed with
1817 :func:`ast.literal_eval`, the parsed value is returned. Otherwise,
1918 the string is returned.
2019
2120 :param nodes: Iterable of nodes to concatenate.
22- :param preserve_quotes: Whether to re-wrap literal strings with
23- quotes, to preserve quotes around expressions for later parsing.
24- Should be ``False`` in :meth:`NativeEnvironment.render`.
2521 """
2622 head = list (islice (nodes , 2 ))
2723
@@ -31,37 +27,25 @@ def native_concat(nodes, preserve_quotes=True):
3127 if len (head ) == 1 :
3228 raw = head [0 ]
3329 else :
34- if isinstance (nodes , types .GeneratorType ):
35- nodes = chain (head , nodes )
36- raw = u"" .join ([text_type (v ) for v in nodes ])
30+ raw = u"" .join ([text_type (v ) for v in chain (head , nodes )])
3731
3832 try :
39- literal = literal_eval (raw )
33+ return literal_eval (raw )
4034 except (ValueError , SyntaxError , MemoryError ):
4135 return raw
4236
43- # If literal_eval returned a string, re-wrap with the original
44- # quote character to avoid dropping quotes between expression nodes.
45- # Without this, "'{{ a }}', '{{ b }}'" results in "a, b", but should
46- # be ('a', 'b').
47- if preserve_quotes and isinstance (literal , str ):
48- return "{quote}{}{quote}" .format (literal , quote = raw [0 ])
49-
50- return literal
51-
5237
5338class NativeCodeGenerator (CodeGenerator ):
5439 """A code generator which renders Python types by not adding
55- ``to_string()`` around output nodes, and using :func:`native_concat`
56- to convert complex strings back to Python types if possible.
40+ ``to_string()`` around output nodes.
5741 """
5842
5943 @staticmethod
6044 def _default_finalize (value ):
6145 return value
6246
6347 def _output_const_repr (self , group ):
64- return repr (native_concat ( group ))
48+ return repr (u"" . join ([ text_type ( v ) for v in group ] ))
6549
6650 def _output_child_to_const (self , node , frame , finalize ):
6751 const = node .as_const (frame .eval_ctx )
@@ -100,10 +84,9 @@ def render(self, *args, **kwargs):
10084 Otherwise, the string is returned.
10185 """
10286 vars = dict (* args , ** kwargs )
87+
10388 try :
104- return native_concat (
105- self .root_render_func (self .new_context (vars )), preserve_quotes = False
106- )
89+ return native_concat (self .root_render_func (self .new_context (vars )))
10790 except Exception :
10891 return self .environment .handle_exception ()
10992
0 commit comments