I've simplified and modularized the design of this tool. Included is a patch against the 2.6 svn head which adds a module to the stdlib called bindann (for "binding annotation"), and then modifies the traceback module to use it. It also includes a demo script which compares classic and annotated exception tracebacks.
The interface to traceback is backwards compatible (it introduces optional arguments defaulting to pre-patch behavior).
bindann is much simpler than the previous annex file, because it doesn't try parsing, only tokenizing the source line, to look for name references. Thus it succeeds where annex would fail (for example if the head of a for-loop causes an uncaught exception, the source line will only be the head which is not a complete/parsable expression).
Here's the output of the demo:
$ ./Demo/newfeatures/demo-bindann.py
This demonstrates binding-annoted exception tracebacks.
Consider the following function:
def f(c):
for char in c + " hello":
print 'The string contains "%s".' % (char,)
-With standard tracebacks, calling f(42) gives:
Traceback (most recent call last):
File "./Demo/newfeatures/demo-bindann.py", line 17, in main
f(42)
File "./Demo/newfeatures/demo-bindann.py", line 28, in f
for char in c + " hello":
TypeError: unsupported operand type(s) for +: 'int' and 'str'
-And now with annotated exceptions:
Traceback (most recent call last):
File "./Demo/newfeatures/demo-bindann.py", line 33, in <module>
# With bindings:
# main = <function main at 0x300fa870>
# Source:
main()
File "./Demo/newfeatures/demo-bindann.py", line 24, in main
# With bindings:
# f = <function f at 0x300fa8b0>
# Source:
f(42)
File "./Demo/newfeatures/demo-bindann.py", line 28, in f
# With bindings:
c = 42
# char = <UnboundLocal>
# Source:
for char in c + " hello":
TypeError: unsupported operand type(s) for +: 'int' and 'str'
File Added: bindann-patch.1
|