Skip to content

Commit

Permalink
- [bug] Fixed bug whereby an exception in Python 3
Browse files Browse the repository at this point in the history
  against a module compiled to the filesystem would
  fail trying to produce a RichTraceback due to the
  content being in bytes. [ticket:209]
  • Loading branch information
zzzeek committed Feb 21, 2013
1 parent b809f95 commit 779e1f3
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
5 changes: 5 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
@@ -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]
Expand Down
2 changes: 1 addition & 1 deletion mako/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -601,7 +601,7 @@ def code(self):
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):
Expand Down
11 changes: 11 additions & 0 deletions mako/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
13 changes: 12 additions & 1 deletion test/test_exceptions.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ def test_custom_tback(self):
# 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()
Expand All @@ -297,3 +297,14 @@ def test_tback_no_trace(self):
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

0 comments on commit 779e1f3

Please sign in to comment.