Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #7208

Re: Function call arguments in stack trace?

From "Gabriel Genellina" <gagsl-py2@yahoo.com.ar>
Subject Re: Function call arguments in stack trace?
Date 2011-06-07 23:45 -0300
References <9d344c45-8017-4c80-9a17-bc7accd81047@l26g2000yqm.googlegroups.com>
Newsgroups comp.lang.python
Message-ID <mailman.12.1307500925.11593.python-list@python.org> (permalink)

Show all headers | View raw


En Tue, 07 Jun 2011 15:09:54 -0300, Dun Peal <dunpealer@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

Back to comp.lang.python | Previous | NextPrevious in thread | Find similar | Unroll thread


Thread

Function call arguments in stack trace? Dun Peal <dunpealer@gmail.com> - 2011-06-07 11:09 -0700
  Re: Function call arguments in stack trace? Neil Cerutti <neilc@norwich.edu> - 2011-06-07 18:23 +0000
    Re: Function call arguments in stack trace? Dun Peal <dunpealer@gmail.com> - 2011-06-07 12:31 -0700
      Re: Function call arguments in stack trace? Ian Kelly <ian.g.kelly@gmail.com> - 2011-06-07 13:52 -0600
      Re: Function call arguments in stack trace? Irmen de Jong <irmen.NOSPAM@xs4all.nl> - 2011-06-07 22:01 +0200
      Re: Function call arguments in stack trace? Neil Cerutti <neilc@norwich.edu> - 2011-06-07 20:29 +0000
  Re: Function call arguments in stack trace? "Gabriel Genellina" <gagsl-py2@yahoo.com.ar> - 2011-06-07 23:45 -0300

csiph-web