Function call arguments in stack trace?

Neil Cerutti neilc at norwich.edu
Tue Jun 7 16:29:47 EDT 2011


On 2011-06-07, Dun Peal <dunpealer at gmail.com> wrote:
> On Jun 7, 1:23?pm, Neil Cerutti <ne... at norwich.edu> wrote:
>> Use pdb.
>
> Neil, thanks for the tip; `pdb` is indeed a great debugging
> tool.
>
> Still, it doesn't obviate the need for arguments in the stack
> trace. For example:
>
> 1) Arguments in stack trace can expedite a debugging session, and even
> obviate it completely: "Why did `foo()` fail?  Oh, because it got `-1`
> as its first argument, while I only coded for positive integers!".
> 2) In some environments, it's very hard to recreate a rare exception
> and analyze it with `pdb`. For instance, on a web application that
> emails the stack traces of unhandled exceptions, it's very important
> for that stack trace to be as informative as possible, since often
> that's the only debugging feedback you will get.
>
> Hope that makes sense, D.

The locals should be in the frame object of the traceback. Here's
a sketch of a decorator to print them out before your program
bombs:

import sys

def report_arg_info(fn):
    def wrapper(*arg, **kw):
        try:
            return fn(*arg, **kw)
        except:
            frame = sys.exc_info()[2].tb_next.tb_frame
            print(frame.f_locals)
            raise
    return wrapper

Use it as usual:

@report_arg_info
def my_func(bombs)
    raise ValueError

You could log the local arguments instead.

-- 
Neil Cerutti



More information about the Python-list mailing list