Function call arguments in stack trace?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Jun 7 22:45:15 EDT 2011


En Tue, 07 Jun 2011 15:09:54 -0300, Dun Peal <dunpealer at gmail.com>  
escribió:

> In a stack trace, is it possible to somehow get the arguments with
> which each function was called?
>
> So for example, if function `foo` in module `bar` was called with
> arguments `(1, [2])` when it raised an exception, then instead of:
>
>     Traceback (most recent call last):
>       File "bar.py", line 123, in foo
>         build_rpms()
>
> The stack trace would read:
>
>     Traceback (most recent call last):
>       File "bar.py", line 123, in foo(1, [2])
>         build_rpms()
>
> This would save a lot of debugging time!

The cgitb module does exactly that; some third-party modules offer similar  
functionality, but I don't remember any names.
Despite its name, cgitb works with any script.

Given this test script:

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

spam = []

def a(x, y):
   "This is function a"
   z = x+y
   return b(z)


def b(z, n=3):
   """This is function b.

   Its docstring is longer."""

   if n!=3:
     just(to_consume_space)

   w = c(foo=z*n)

   return w


def c(foo=0, bar=1):
   "This is function c"
   baz = foo+bar
   spam.somenamethatdoesnotexist(foo+bar)
   anotherglobal("thatdoesnotexisteither")

a(10, 20)
# end test_traceback.py

the output is:


AttributeError
Python 3.2: d:\apps\Python32\python.exe
Tue Jun  7 23:36:36 2011

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

  D:\TEMP\test_traceback.py in <module>()
    27   baz = foo+bar
    28   spam.somenamethatdoesnotexist(foo+bar)
    29   anotherglobal("thatdoesnotexisteither")
    30
    31 a(10, 20)
a = <function a>

  D:\TEMP\test_traceback.py in a(x=10, y=20)
     7   "This is function a"
     8   z = x+y
     9   return b(z)
    10
    11
global b = <function b>
z = 30

  D:\TEMP\test_traceback.py in b(z=30, n=3)
    18     just(to_consume_space)
    19
    20   w = c(foo=z*n)
    21
    22   return w
w undefined
global c = <function c>
foo undefined
z = 30
n = 3

  D:\TEMP\test_traceback.py in c(foo=90, bar=1)
    26   "This is function c"
    27   baz = foo+bar
    28   spam.somenamethatdoesnotexist(foo+bar)
    29   anotherglobal("thatdoesnotexisteither")
    30
global spam = []
spam.somenamethatdoesnotexist undefined
foo = 90
bar = 1
AttributeError: 'list' object has no attribute 'somenamethatdoesnotexist'
    [... exception attributes ...]
    [... original traceback ...]

-- 
Gabriel Genellina




More information about the Python-list mailing list