Patching Python Tracebacks — Part Two

After posting this I hacked up a “tbtools” module that contains various traceback helper functions to modify tracebacks. You can find it here: tbtools.

It’s far from stable and I want to ask some of the authors of other template engines (namely zzeeek) for their ideas. There is also an example of the werkzeug minitmpl engine with support for tbtools. Now what it does is the following:

It converts this useless traceback of a template engine with code generation:

Traceback (most recent call last):
  File "templating.py", line 151, in <module>
    print tmpl.render(broken=broken)
  File "templating.py", line 120, in render
    return u''.join(tuple(tmp['__generate']()))
  File "<template>", line 10, in __generate
  File "templating.py", line 149, in broken
    foo()
  File "templating.py", line 148, in foo
    raise RuntimeError("nested broken")
RuntimeError: nested broken

Into this:

Traceback (most recent call last):
  File "templating.py", line 151, in <module>
    print tmpl.render(broken=broken)
  File "test.html", line 3, in <module>
    <%= broken(item) %>
  File "templating.py", line 149, in broken
    foo()
  File "templating.py", line 148, in foo
    raise RuntimeError("nested broken")
RuntimeError: nested broken

Because Python locks the traceback object a minimal C module is required. ctypes also doesn’t work because the python API doesn’t export the struct we need.

Leave a Reply

cogitations driven by wordpress