Skip to content

Commit 717e18e

Browse files
committed
Report the start of an unterminated tag or expression
A tag or expression with no closing token was reported against the position at which the lexer stopped looking for one. As the search consumes the rest of the template, that position is the end of the file, which for the templates where this is hardest to spot is thousands of lines from the construct that is actually at fault. Lexer.parse_until_text() now records the position it began scanning at and reports the exception against that, so that the construct which was never closed is identified. The message is amended to say that the position given is where the unterminated construct begins, as it would otherwise read as the point at which the problem was detected. Note that the filter section of an expression is scanned separately, so an expression such as ${ x | trim with no closing brace is reported at the position of the filter rather than that of the expression. Fixes: sqlalchemy#428 Change-Id: Ia9191db5c62cca0de498c48a643e336876427d37
1 parent a162c9c commit 717e18e

3 files changed

Lines changed: 69 additions & 1 deletion

File tree

‎doc/build/unreleased/428.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. change::
2+
:tags: bug, lexer
3+
:tickets: 428
4+
5+
The ``SyntaxException`` raised for a tag or expression that is never
6+
closed is now reported against the line the construct begins on, rather
7+
than the point at which the search for the closing token gave up, which
8+
for an unclosed construct is the end of the template. The message of the
9+
exception is amended to indicate that the position given is where the
10+
unterminated construct begins.

‎mako/lexer.py‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ def match_reg(self, reg):
8383

8484
def parse_until_text(self, watch_nesting, *text):
8585
startpos = self.match_position
86+
startlineno = self.matched_lineno
87+
startcharpos = self.matched_charpos
8688
text_re = r"|".join(text)
8789
brace_level = 0
8890
paren_level = 0
@@ -117,8 +119,17 @@ def parse_until_text(self, watch_nesting, *text):
117119
bracket_level += match.group(1).count("[")
118120
bracket_level -= match.group(1).count("]")
119121
continue
122+
# the scan consumes the remaining text looking for the
123+
# closing token, so report the position the construct began
124+
# at, rather than the position the scan gave up at
120125
raise exceptions.SyntaxException(
121-
"Expected: %s" % ",".join(text), **self.exception_kwargs
126+
"Expected: %s; unterminated tag or expression beginning"
127+
% ",".join(text),
128+
**{
129+
**self.exception_kwargs,
130+
"lineno": startlineno,
131+
"pos": startcharpos,
132+
},
122133
)
123134

124135
def append_node(self, nodecls, *args, **kwargs):

‎test/test_exceptions.py‎

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -714,3 +714,50 @@ def test_unknown_filename_warning_from_import_passes_through(self, source):
714714
)
715715
},
716716
)
717+
718+
719+
class UnterminatedTagLocationTest(TemplateTest):
720+
"""test that a tag or expression which is never closed is reported
721+
against the line it begins on.
722+
723+
"""
724+
725+
def _assert_syntax_error(self, text, message):
726+
assert_raises_message(
727+
exceptions.SyntaxException,
728+
message,
729+
Template,
730+
text,
731+
filename="foo.mako",
732+
)
733+
734+
def test_unterminated_expression(self):
735+
"""test #428"""
736+
737+
self._assert_syntax_error(
738+
"one\ntwo\n${ d['x'\n"
739+
+ "".join('line%d "quoted" text\n' % i for i in range(4, 40)),
740+
r"Expected: \\\|,\}; unterminated tag or expression beginning "
741+
r"in file 'foo.mako' at line: 3 char: 1",
742+
)
743+
744+
def test_unterminated_code_block(self):
745+
self._assert_syntax_error(
746+
"one\n<%\n x = 1\n"
747+
+ "".join('line%d "quoted" text\n' % i for i in range(4, 40)),
748+
r"Expected: %>; unterminated tag or expression beginning "
749+
r"in file 'foo.mako' at line: 2 char: 1",
750+
)
751+
752+
def test_unterminated_expression_filter(self):
753+
"""the filter of an expression is scanned separately, so the
754+
position reported is where the filter begins
755+
756+
"""
757+
758+
self._assert_syntax_error(
759+
"one\ntwo\nthree\n${ x | trim\n"
760+
+ "".join('line%d "quoted" text\n' % i for i in range(5, 40)),
761+
r"Expected: \}; unterminated tag or expression beginning "
762+
r"in file 'foo.mako' at line: 4 char: 6",
763+
)

0 commit comments

Comments
 (0)