FPE: Add bindings to exception tracebacks.

Gabriel Genellina gagsl-py at yahoo.com.ar
Mon Feb 19 20:30:10 EST 2007


En Mon, 19 Feb 2007 19:47:26 -0300, Nathan <nejucomo at gmail.com> escribió:

> Throughout my python development career, I've occasionally made
> various developer tools to show more information about assertions or
> exceptions with less hassle to the programmer.  Until now, these tools
> didn't pass a utility vs pain-to-use threshold.
>
> Now I've created a tool I believe to have passed that threshold, which
> I call "binding annotated exception tracebacks".  In short, this tool
> adds text showing relevant local bindings to each level in a stack
> trace print out.

Something very similar already exists in the standard library, but using a  
very misleading name, "cgitb".
It works much better with a source file (so it can print source lines too)

=== begin tb.py ===
import cgitb
cgitb.enable(format="text")

def f(c):
     d = 2*c
     return g(c)

def g(x):
      return (lambda z: z+'foo')(x)

f(42)
=== end tb.py ===

C:\TEMP>python tb.py
<type 'exceptions.TypeError'>
Python 2.5: c:\apps\python\python.exe
Mon Feb 19 22:27:26 2007

A problem occurred in a Python script.  Here is the sequence of
function calls leading up to the error, in the order they occurred.

  C:\TEMP\tb.py in <module>()
     7
     8 def g(x):
     9      return (lambda z: z+'foo')(x)
    10
    11 f(42)
f = <function f at 0x00AD89F0>

  C:\TEMP\tb.py in f(c=42)
     4 def f(c):
     5     d = 2*c
     6     return g(c)
     7
     8 def g(x):
global g = <function g at 0x00AD8A30>
c = 42

  C:\TEMP\tb.py in g(x=42)
     7
     8 def g(x):
     9      return (lambda z: z+'foo')(x)
    10
    11 f(42)
z undefined
x = 42

  C:\TEMP\tb.py in <lambda>(z=42)
     7
     8 def g(x):
     9      return (lambda z: z+'foo')(x)
    10
    11 f(42)
z = 42
x undefined
<type 'exceptions.TypeError'>: unsupported operand type(s) for +: 'int'  
and 'str
'

The above is a description of an error in a Python program.  Here is
the original traceback:

Traceback (most recent call last):
   File "tb.py", line 11, in <module>
     f(42)
   File "tb.py", line 6, in f
     return g(c)
   File "tb.py", line 9, in g
     return (lambda z: z+'foo')(x)
   File "tb.py", line 9, in <lambda>
     return (lambda z: z+'foo')(x)
TypeError: unsupported operand type(s) for +: 'int' and 'str'

-- 
Gabriel Genellina




More information about the Python-list mailing list