diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-05-31 16:11:33 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2010-05-31 16:11:33 -0400 |
commit | 3ef596c2f5217993d92e1cded3efc44d249eb183 (patch) | |
tree | c4fd14958bd5a3bc6367e9bd58f1124fa1284fbb /mako/exceptions.py | |
parent | e23877edcbddde78ed56ca8e824520b8ab6af0b5 (diff) | |
download | external_python_mako-3ef596c2f5217993d92e1cded3efc44d249eb183.tar.gz external_python_mako-3ef596c2f5217993d92e1cded3efc44d249eb183.tar.bz2 external_python_mako-3ef596c2f5217993d92e1cded3efc44d249eb183.zip |
- Added conditional to RichTraceback
such that if no traceback is passed
and sys.exc_info() has been reset,
the formatter just returns blank
for the "traceback" portion.
[ticket:135]
- some long line cleanup
Diffstat (limited to 'mako/exceptions.py')
-rw-r--r-- | mako/exceptions.py | 77 |
1 files changed, 44 insertions, 33 deletions
diff --git a/mako/exceptions.py b/mako/exceptions.py index 0c1f844..6e95751 100644 --- a/mako/exceptions.py +++ b/mako/exceptions.py @@ -48,8 +48,8 @@ class TopLevelLookupException(TemplateLookupException): pass class RichTraceback(object): - """pulls the current exception from the sys traceback and extracts Mako-specific - template information. + """pulls the current exception from the sys traceback and extracts + Mako-specific template information. Usage: @@ -127,8 +127,10 @@ class RichTraceback(object): @property def traceback(self): - """return a list of 4-tuple traceback records (i.e. normal python format) - with template-corresponding lines remapped to the originating template + """return a list of 4-tuple traceback records (i.e. normal python + format) with template-corresponding lines remapped to the originating + template. + """ return list(self._get_reformatted_records(self.records)) @@ -138,15 +140,17 @@ class RichTraceback(object): @property def reverse_traceback(self): - """return the same data as traceback, except in reverse order + """return the same data as traceback, except in reverse order. """ + return list(self._get_reformatted_records(self.reverse_records)) def _init(self, trcback): - """format a traceback from sys.exc_info() into 7-item tuples, containing - the regular four traceback tuple items, plus the original template - filename, the line number adjusted relative to the template source, and - code line from that line number of the template.""" + """format a traceback from sys.exc_info() into 7-item tuples, + containing the regular four traceback tuple items, plus the original + template filename, the line number adjusted relative to the template + source, and code line from that line number of the template.""" + import mako.template mods = {} rawrecords = traceback.extract_tb(trcback) @@ -189,7 +193,8 @@ class RichTraceback(object): template_ln += 1 module_ln += 1 line_map[module_ln] = template_ln - template_lines = [line for line in template_source.split("\n")] + template_lines = [line for line in + template_source.split("\n")] mods[filename] = (line_map, template_lines) template_ln = line_map[lineno] @@ -207,25 +212,29 @@ class RichTraceback(object): self.lineno = new_trcback[l][5] break else: - try: - # A normal .py file (not a Template) - fp = open(new_trcback[-1][0], 'rb') - encoding = util.parse_encoding(fp) - fp.seek(0) - self.source = fp.read() - fp.close() - if encoding: - self.source = self.source.decode(encoding) - except IOError: - self.source = '' - self.lineno = new_trcback[-1][1] + if new_trcback: + try: + # A normal .py file (not a Template) + fp = open(new_trcback[-1][0], 'rb') + encoding = util.parse_encoding(fp) + fp.seek(0) + self.source = fp.read() + fp.close() + if encoding: + self.source = self.source.decode(encoding) + except IOError: + self.source = '' + self.lineno = new_trcback[-1][1] return new_trcback def text_error_template(lookup=None): - """provides a template that renders a stack trace in a similar format to the Python interpreter, - substituting source template filenames, line numbers and code for that of the originating - source template, as applicable.""" + """Provides a template that renders a stack trace in a similar format to + the Python interpreter, substituting source template filenames, line + numbers and code for that of the originating source template, as + applicable. + + """ import mako.template return mako.template.Template(r""" <%page args="error=None, traceback=None"/> @@ -244,15 +253,17 @@ ${tback.errorname}: ${tback.message} """) def html_error_template(): - """provides a template that renders a stack trace in an HTML format, providing an excerpt of - code as well as substituting source template filenames, line numbers and code - for that of the originating source template, as applicable. - - the template's default encoding_errors value is 'htmlentityreplace'. the template has - two options: + """Provides a template that renders a stack trace in an HTML format, + providing an excerpt of code as well as substituting source template + filenames, line numbers and code for that of the originating source + template, as applicable. - with the full option disabled, only a section of an HTML document is returned. - with the css option disabled, the default stylesheet won't be included.""" + The template's default encoding_errors value is 'htmlentityreplace'. the + template has two options. With the full option disabled, only a section of + an HTML document is returned. with the css option disabled, the default + stylesheet won't be included. + + """ import mako.template return mako.template.Template(r""" <%! |