diff options
author | David Lord <davidism@gmail.com> | 2020-04-13 09:26:39 -0700 |
---|---|---|
committer | David Lord <davidism@gmail.com> | 2020-04-13 09:26:39 -0700 |
commit | da812816ff1a459eefa7ca946b4c108cc7106c85 (patch) | |
tree | 88b1e7e3351f5031471c8ba3291a08e7305f0b70 /src | |
parent | 91eb39b153ce387428d8dc72385440a597cffeca (diff) | |
parent | 9ec465baefe32e305bd4e61da49e6c39360c194e (diff) | |
download | jinja-da812816ff1a459eefa7ca946b4c108cc7106c85.tar.gz |
Merge branch '2.11.x'
Diffstat (limited to 'src')
-rw-r--r-- | src/jinja2/lexer.py | 13 | ||||
-rw-r--r-- | src/jinja2/nativetypes.py | 32 |
2 files changed, 15 insertions, 30 deletions
diff --git a/src/jinja2/lexer.py b/src/jinja2/lexer.py index fc44dbeb..0a992073 100644 --- a/src/jinja2/lexer.py +++ b/src/jinja2/lexer.py @@ -637,6 +637,7 @@ class Lexer: balancing_stack = [] lstrip_unless_re = self.lstrip_unless_re newlines_stripped = 0 + line_starting = True while 1: # tokenizer loop @@ -686,11 +687,11 @@ class Lexer: ): # The start of text between the last newline and the tag. l_pos = text.rfind("\n") + 1 - - # If there's only whitespace between the newline and the - # tag, strip it. - if not lstrip_unless_re.search(text, l_pos): - groups = (text[:l_pos],) + groups[1:] + if l_pos > 0 or line_starting: + # If there's only whitespace between the newline and the + # tag, strip it. + if not lstrip_unless_re.search(text, l_pos): + groups = (text[:l_pos],) + groups[1:] for idx, token in enumerate(tokens): # failure group @@ -747,6 +748,8 @@ class Lexer: yield lineno, tokens, data lineno += data.count("\n") + line_starting = m.group()[-1:] == "\n" + # fetch new position into new variable so that we can check # if there is a internal parsing error which would result # in an infinite loop diff --git a/src/jinja2/nativetypes.py b/src/jinja2/nativetypes.py index e0ad94d3..5ecf72b5 100644 --- a/src/jinja2/nativetypes.py +++ b/src/jinja2/nativetypes.py @@ -1,4 +1,3 @@ -import types from ast import literal_eval from itertools import chain from itertools import islice @@ -10,7 +9,7 @@ from .environment import Environment from .environment import Template -def native_concat(nodes, preserve_quotes=True): +def native_concat(nodes): """Return a native Python type from the list of compiled nodes. If the result is a single node, its value is returned. Otherwise, the nodes are concatenated as strings. If the result can be parsed with @@ -18,9 +17,6 @@ def native_concat(nodes, preserve_quotes=True): the string is returned. :param nodes: Iterable of nodes to concatenate. - :param preserve_quotes: Whether to re-wrap literal strings with - quotes, to preserve quotes around expressions for later parsing. - Should be ``False`` in :meth:`NativeEnvironment.render`. """ head = list(islice(nodes, 2)) @@ -30,30 +26,17 @@ def native_concat(nodes, preserve_quotes=True): if len(head) == 1: raw = head[0] else: - if isinstance(nodes, types.GeneratorType): - nodes = chain(head, nodes) - raw = "".join([str(v) for v in nodes]) + raw = "".join([str(v) for v in chain(head, nodes)]) try: - literal = literal_eval(raw) + return literal_eval(raw) except (ValueError, SyntaxError, MemoryError): return raw - # If literal_eval returned a string, re-wrap with the original - # quote character to avoid dropping quotes between expression nodes. - # Without this, "'{{ a }}', '{{ b }}'" results in "a, b", but should - # be ('a', 'b'). - if preserve_quotes and isinstance(literal, str): - quote = raw[0] - return f"{quote}{literal}{quote}" - - return literal - class NativeCodeGenerator(CodeGenerator): """A code generator which renders Python types by not adding - ``str()`` around output nodes, and using :func:`native_concat` - to convert complex strings back to Python types if possible. + ``str()`` around output nodes. """ @staticmethod @@ -61,7 +44,7 @@ class NativeCodeGenerator(CodeGenerator): return value def _output_const_repr(self, group): - return repr(native_concat(group)) + return repr("".join([str(v) for v in group])) def _output_child_to_const(self, node, frame, finalize): const = node.as_const(frame.eval_ctx) @@ -100,10 +83,9 @@ class NativeTemplate(Template): Otherwise, the string is returned. """ vars = dict(*args, **kwargs) + try: - return native_concat( - self.root_render_func(self.new_context(vars)), preserve_quotes=False - ) + return native_concat(self.root_render_func(self.new_context(vars))) except Exception: return self.environment.handle_exception() |