[Python-ideas] Verbose traceback formatting

Mike Graham mikegraham at gmail.com
Wed Aug 29 00:26:04 CEST 2012


It's possible to give a lot more on error than the default traceback
gives you. I propose that Python should ship a more verbose formatter
and a command line switch to use it.

Here's an example of IPython's verbose formatter. I wrote a buggy program:

> def f(a):
>     x = a * 4
>     y = a - 4
>     return x / y
>
> def main():
>     for i in xrange(100):
>         f(i)
>
> main()


and then ran it in IPython with verbose tracebacks and got the following output:

> ZeroDivisionError                         Traceback (most recent call last)
>
> /home/mike/foo.py in <module>()
>       8         f(i)
>       9
> ---> 10 main()
>         global main = <function main at 0x10bd7d0>
>      11
>      12
>
> /home/mike/foo.py in main()
>       6 def main():
>       7     for i in xrange(100):
> ----> 8         f(i)
>         global f = <function f at 0x10bd758>
>         i = 4
>       9
>      10 main()
>
> /home/mike/foo.py in f(a=4)
>       2     x = a * 4
>       3     y = a - 4
> ----> 4     return x / y
>         x = 16
>         y = 0
>       5
>       6 def main():
>
> ZeroDivisionError: integer division or modulo by zero



This is very handy! The reprs of all locals are input so I can see
what the values of a, x, and y were when I had my error and there are
a few lines of code on either side of the line that matters to help me
get oriented. The former feature is the more powerful one, although
enabling this by default is a bad idea; (first and foremost, this can
be a security hazard). I can't count how many trips into pdb this
would have saved me.

I think having this feature be part of Python itself would be very
helpful to new learners and to those helping them. I constantly deal
with learners seeking help who are unable to clearly provide the
actual values and types of the objects in the code they're having
trouble with; it would be nice simply to say, "Show me a verbose
traceback" and might even help them to debug their code without
assistance.

Mike



More information about the Python-ideas mailing list