Skip to content

Commit e2231b4

Browse files
committed
Report the line of a syntax error within a python block
A syntax error in Python code within a template was reported against the line on which the enclosing construct began, rather than the line the error is on; for a <% %> block of any length, this is always the line of the opening tag. The underlying SyntaxError knows the line within the code that was parsed, and the line the construct starts on is already reported, so both halves of the answer were present but never combined. pyparser.parse() now applies the line of the SyntaxError to the line reported for the construct. The offset between the two is not the same for every construct, so it is computed rather than assumed: the code of a <% %> block usually begins on the line after the tag, but may begin on the tag's own line, so PythonCode counts the lines it strips from the front of the code, while PythonFragment accounts for the line it places before a statement that is only valid as a continuation, such as elif or except. Constructs whose code occupies a single line, such as an expression or a control line, already reported the correct line and are unaffected. Fixes: sqlalchemy#245 Change-Id: I4a6c32d687a15797c03e4ec0a146874254ce2a00
1 parent 717e18e commit e2231b4

4 files changed

Lines changed: 142 additions & 6 deletions

File tree

‎doc/build/unreleased/245.rst‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.. change::
2+
:tags: bug, exceptions
3+
:tickets: 245
4+
5+
The ``SyntaxException`` raised for a syntax error in Python code spanning
6+
several lines of a template, such as that within a ``<% %>`` or ``<%! %>``
7+
block, is now reported against the line the error is on, rather than
8+
against the line on which the block begins. The line reported for code
9+
that occupies a single line, such as an expression or a control line, is
10+
unchanged.

‎mako/ast.py‎

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
class PythonCode:
1717
"""represents information about a string containing Python code"""
1818

19-
def __init__(self, code, **exception_kwargs):
19+
def __init__(self, code, lineno_offset=0, **exception_kwargs):
2020
self.code = code
2121

2222
# represents all identifiers which are assigned to at some point in
@@ -38,7 +38,19 @@ def __init__(self, code, **exception_kwargs):
3838
# (for example, the behavior of co_names changed a little bit
3939
# in python version 2.5)
4040
if isinstance(code, str):
41-
expr = pyparser.parse(code.lstrip(), "exec", **exception_kwargs)
41+
stripped = code.lstrip()
42+
43+
# the code of a <% %> block usually begins on the line after the
44+
# tag; count the lines that are stripped off so that a syntax
45+
# error is reported against the line it is on
46+
lineno_offset += code[: len(code) - len(stripped)].count("\n")
47+
48+
expr = pyparser.parse(
49+
stripped,
50+
"exec",
51+
lineno_offset=lineno_offset,
52+
**exception_kwargs,
53+
)
4254
else:
4355
expr = code
4456

@@ -89,22 +101,30 @@ def __init__(self, code, **exception_kwargs):
89101
if m.group(3):
90102
code = code[: m.start(3)]
91103
keyword, expr = m.group(1, 2)
104+
105+
# a statement that is only valid as a continuation is completed by
106+
# a line placed before it; the line the fragment is on is that many
107+
# lines further along than the code which is parsed
108+
lineno_offset = 0
109+
92110
if keyword in ["for", "if", "while"]:
93111
code = code + "pass"
94112
elif keyword == "try":
95113
code = code + "pass\nexcept:pass"
96114
elif keyword in ["elif", "else"]:
97115
code = "if False:pass\n" + code + "pass"
116+
lineno_offset = -1
98117
elif keyword == "except":
99118
code = "try:pass\n" + code + "pass"
119+
lineno_offset = -1
100120
elif keyword == "with":
101121
code = code + "pass"
102122
else:
103123
raise exceptions.CompileException(
104124
"Unsupported control keyword: '%s'" % keyword,
105125
**exception_kwargs,
106126
)
107-
super().__init__(code, **exception_kwargs)
127+
super().__init__(code, lineno_offset=lineno_offset, **exception_kwargs)
108128

109129

110130
class FunctionDecl:

‎mako/pyparser.py‎

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,15 @@
2626
EXPRESSION_FILENAME = "<unknown>"
2727

2828

29-
def parse(code, mode="exec", **exception_kwargs):
30-
"""Parse an expression into AST"""
29+
def parse(code, mode="exec", lineno_offset=0, **exception_kwargs):
30+
"""Parse an expression into AST.
31+
32+
``lineno_offset`` is the line within the template on which ``code``
33+
begins, relative to the line given in ``exception_kwargs``. It is used
34+
to report a syntax error against the line it occurred on, rather than
35+
against the start of the construct that contains it.
36+
37+
"""
3138

3239
try:
3340
return _ast_util.parse(code, EXPRESSION_FILENAME, mode)
@@ -39,10 +46,28 @@ def parse(code, mode="exec", **exception_kwargs):
3946
compat.exception_as(),
4047
code[0:50],
4148
),
42-
**exception_kwargs,
49+
**_adjust_lineno(e, lineno_offset, exception_kwargs),
4350
) from e
4451

4552

53+
def _adjust_lineno(exc, lineno_offset, exception_kwargs):
54+
"""Return ``exception_kwargs`` with the line of ``exc`` within the parsed
55+
code applied to it.
56+
57+
"""
58+
59+
lineno = exception_kwargs.get("lineno")
60+
exc_lineno = getattr(exc, "lineno", None)
61+
62+
if lineno is None or exc_lineno is None:
63+
return exception_kwargs
64+
65+
return {
66+
**exception_kwargs,
67+
"lineno": lineno + lineno_offset + exc_lineno - 1,
68+
}
69+
70+
4671
class FindIdentifiers(_ast_util.NodeVisitor):
4772
def __init__(self, listener, **exception_kwargs):
4873
self.in_function = False

‎test/test_exceptions.py‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -720,6 +720,8 @@ class UnterminatedTagLocationTest(TemplateTest):
720720
"""test that a tag or expression which is never closed is reported
721721
against the line it begins on.
722722
723+
tests for #428
724+
723725
"""
724726

725727
def _assert_syntax_error(self, text, message):
@@ -761,3 +763,82 @@ def test_unterminated_expression_filter(self):
761763
r"Expected: \}; unterminated tag or expression beginning "
762764
r"in file 'foo.mako' at line: 4 char: 6",
763765
)
766+
767+
768+
class SyntaxExceptionLocationTest(TemplateTest):
769+
"""test that a syntax error in Python code within a template is
770+
reported against the line it is on.
771+
772+
tests for #245
773+
"""
774+
775+
def _assert_syntax_error(self, text, message):
776+
assert_raises_message(
777+
exceptions.SyntaxException,
778+
message,
779+
Template,
780+
text,
781+
filename="foo.mako",
782+
)
783+
784+
def test_code_block(self):
785+
"""test #245"""
786+
787+
self._assert_syntax_error(
788+
"one\ntwo\nthree\nfour\n<%\n a = 1\n b = 2\n c = (\n%>\n",
789+
r"\(SyntaxError\) '\(' was never closed .* "
790+
r"in file 'foo.mako' at line: 8 char: 1",
791+
)
792+
793+
def test_code_block_beginning_on_tag_line(self):
794+
self._assert_syntax_error(
795+
"one\ntwo\n<% a = 1\nc = ( %>\n",
796+
r"\(SyntaxError\) '\(' was never closed .* "
797+
r"in file 'foo.mako' at line: 4 char: 1",
798+
)
799+
800+
def test_module_level_block(self):
801+
self._assert_syntax_error(
802+
"one\n<%!\n a = 1\n c = (\n%>\n",
803+
r"\(SyntaxError\) '\(' was never closed .* "
804+
r"in file 'foo.mako' at line: 4 char: 1",
805+
)
806+
807+
def test_expression(self):
808+
self._assert_syntax_error(
809+
"one\ntwo\n${x +* 1}\nfour\n",
810+
r"\(SyntaxError\) invalid syntax .* "
811+
r"in file 'foo.mako' at line: 3 char: 1",
812+
)
813+
814+
def test_def_signature(self):
815+
self._assert_syntax_error(
816+
'one\n<%def name="d(a b)">\nx\n</%def>\n',
817+
r"\(SyntaxError\) .* in file 'foo.mako' at line: 2 char: 1",
818+
)
819+
820+
def test_control_line_if(self):
821+
self._assert_syntax_error(
822+
"one\ntwo\n% if x +* 1:\nb\n% endif\n",
823+
r"\(SyntaxError\) invalid syntax .* "
824+
r"in file 'foo.mako' at line: 3 char: 1",
825+
)
826+
827+
def test_control_line_elif(self):
828+
self._assert_syntax_error(
829+
"one\n% if x:\na\n% elif y +* 1:\nb\n% endif\n",
830+
r"\(SyntaxError\) invalid syntax .* "
831+
r"in file 'foo.mako' at line: 4 char: 1",
832+
)
833+
834+
def test_control_line_except(self):
835+
self._assert_syntax_error(
836+
"one\n% try:\na\n% except (E e):\nb\n% endtry\n",
837+
r"\(SyntaxError\) .* in file 'foo.mako' at line: 4 char: 1",
838+
)
839+
840+
def test_control_line_for(self):
841+
self._assert_syntax_error(
842+
"one\ntwo\nthree\n% for x in (:\na\n% endfor\n",
843+
r"\(SyntaxError\) .* in file 'foo.mako' at line: 4 char: 1",
844+
)

0 commit comments

Comments
 (0)