aboutsummaryrefslogtreecommitdiffstats
path: root/test/test_exceptions.py
diff options
context:
space:
mode:
authorMartin von Gagern <gagern@google.com>2019-07-01 14:35:54 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2019-07-01 15:14:41 -0400
commit06d67cf4677e10e5c42581c02f293101e1c39a3a (patch)
treebf5355518d8b14d79fd822c357a14e29d2b575c7 /test/test_exceptions.py
parentf384df792ae5eeeaa4bd622191b3ca33d9b86a4d (diff)
downloadexternal_python_mako-06d67cf4677e10e5c42581c02f293101e1c39a3a.tar.gz
external_python_mako-06d67cf4677e10e5c42581c02f293101e1c39a3a.tar.bz2
external_python_mako-06d67cf4677e10e5c42581c02f293101e1c39a3a.zip
Correctly track line numbers for multi-line code blocks
Improved the line-number tracking for source lines inside of Python ``<% ... %>`` blocks, such that text- and HTML-formatted exception traces such as that of :func:`.html_error_template` now report the correct source line inside the block, rather than the first line of the block itself. Exceptions in ``<%! ... %>`` blocks which get raised while loading the module are still not reported correctly, as these are handled before the Mako code is generated. Pull request courtesy Martin von Gagern. Closes: #297 Pull-request: https://github.com/sqlalchemy/mako/pull/297 Pull-request-sha: 60ad749604f8de0a9b8133430995045a93d0939c Change-Id: I2086e4370e8e2db7099de01743840286f0160efc
Diffstat (limited to 'test/test_exceptions.py')
-rw-r--r--test/test_exceptions.py48
1 files changed, 48 insertions, 0 deletions
diff --git a/test/test_exceptions.py b/test/test_exceptions.py
index c904d65..46fbcdd 100644
--- a/test/test_exceptions.py
+++ b/test/test_exceptions.py
@@ -383,3 +383,51 @@ ${foobar}
"local variable &#39;y&#39; referenced before assignment"
in html_error
)
+
+ def test_code_block_line_number(self):
+ l = TemplateLookup()
+ l.put_string(
+ "foo.html",
+ """
+<%
+msg = "Something went wrong."
+raise RuntimeError(msg) # This is the line.
+%>
+ """,
+ )
+ t = l.get_template("foo.html")
+ try:
+ t.render()
+ assert False
+ except:
+ text_error = exceptions.text_error_template().render_unicode()
+ assert 'File "foo_html", line 4, in render_body' in text_error
+ assert "raise RuntimeError(msg) # This is the line." in text_error
+ else:
+ assert False
+
+ def test_module_block_line_number(self):
+ l = TemplateLookup()
+ l.put_string(
+ "foo.html",
+ """
+<%!
+def foo():
+ msg = "Something went wrong."
+ raise RuntimeError(msg) # This is the line.
+%>
+${foo()}
+ """,
+ )
+ t = l.get_template("foo.html")
+ try:
+ t.render()
+ assert False
+ except:
+ text_error = exceptions.text_error_template().render_unicode()
+ sys.stderr.write(text_error)
+ assert 'File "foo_html", line 7, in render_body' in text_error
+ assert 'File "foo_html", line 5, in foo' in text_error
+ assert "raise RuntimeError(msg) # This is the line." in text_error
+ else:
+ assert False