diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-02-20 21:24:49 -0500 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2013-02-20 21:24:49 -0500 |
commit | 779e1f330ee112500f862d5fdf056654abd48d37 (patch) | |
tree | b17a43b3a7e6fcf6ec04de57f803551266302eb0 | |
parent | b809f95b4e5327b8dab10c923226ae6047399404 (diff) | |
download | external_python_mako-779e1f330ee112500f862d5fdf056654abd48d37.tar.gz external_python_mako-779e1f330ee112500f862d5fdf056654abd48d37.tar.bz2 external_python_mako-779e1f330ee112500f862d5fdf056654abd48d37.zip |
- [bug] Fixed bug whereby an exception in Python 3
against a module compiled to the filesystem would
fail trying to produce a RichTraceback due to the
content being in bytes. [ticket:209]
-rw-r--r-- | CHANGES | 5 | ||||
-rw-r--r-- | mako/template.py | 2 | ||||
-rw-r--r-- | mako/util.py | 11 | ||||
-rw-r--r-- | test/test_exceptions.py | 13 |
4 files changed, 29 insertions, 2 deletions
@@ -1,4 +1,9 @@ 0.7.4 +- [bug] Fixed bug whereby an exception in Python 3 + against a module compiled to the filesystem would + fail trying to produce a RichTraceback due to the + content being in bytes. [ticket:209] + - [bug] Change default for compile()->reserved_names from tuple to frozenset, as this is expected to be a set by default. [ticket:208] diff --git a/mako/template.py b/mako/template.py index f140a08..9c64987 100644 --- a/mako/template.py +++ b/mako/template.py @@ -601,7 +601,7 @@ class ModuleInfo(object): if self.module_source is not None: return self.module_source else: - return util.read_file(self.module_filename) + return util.read_python_file(self.module_filename) @property def source(self): diff --git a/mako/util.py b/mako/util.py index f54fda2..87be71b 100644 --- a/mako/util.py +++ b/mako/util.py @@ -353,6 +353,17 @@ def read_file(path, mode='rb'): finally: fp.close() +def read_python_file(path): + fp = open(path, "rb") + try: + encoding = parse_encoding(fp) + data = fp.read() + if encoding: + data = data.decode(encoding) + return data + finally: + fp.close() + def load_module(module_id, path): fp = open(path, 'rb') try: diff --git a/test/test_exceptions.py b/test/test_exceptions.py index 012f76f..74bc34c 100644 --- a/test/test_exceptions.py +++ b/test/test_exceptions.py @@ -282,7 +282,7 @@ ${foobar} # isn't in the 'wrong' exception assert "".join(reversed(");93#&rab;93#&(oof")) in html_error - def test_tback_no_trace(self): + def test_tback_no_trace_from_py_file(self): try: t = self._file_template("runtimeerr.html") t.render() @@ -297,3 +297,14 @@ ${foobar} html_error = exceptions.html_error_template().\ render_unicode(error=v, traceback=None) assert "local variable 'y' referenced before assignment" in html_error + + def test_tback_trace_from_py_file(self): + t = self._file_template("runtimeerr.html") + try: + t.render() + assert False + except: + html_error = exceptions.html_error_template().\ + render_unicode() + + assert "local variable 'y' referenced before assignment" in html_error |