intuited
intuited

Reputation: 24034

Getting more detail from stack traces

Is there a convenient way to get a more detailed stack trace on a Python exception? I'm hoping to find a wrapper utility/module or some other way to get a bit more info from the stack trace without having to actually modify the Python script that generates it. I'd like to be able to use this when running unit tests, or doctests, or when running utilities or inline scripts from the shell.

Specifically I think I'd like to have the values of local variables, or maybe just the values of the arguments passed to the innermost function in the stack trace. Some options to set the detail level would be nifty.

Upvotes: 0

Views: 658

Answers (3)

Rod
Rod

Reputation: 55752

As mentionned by pyfunc, you can use the function in the traceback module but you only get a stacktrace.

If you want to inspect the stack you have to use the sys.exc_info() function and walk the traceback member and dump information from its frame (tb_frame). See the python Reference Manual for further information on these types.

Here is an example:

def killit(a):
    a[10000000000000] = 1

def test(a):
    killit(a)

def iterate_traceback(tb):
    while tb is not None:
        yield tb
        tb = tb.tb_next

try:
    test(tuple())
except Exception as e:

    import sys
    exception_info = sys.exc_info()

    traceback = exception_info[2]
    for tb in iterate_traceback(traceback):
        print "-" * 10
        print tb.tb_frame.f_code
        print tb.tb_frame.f_locals
        print tb.tb_frame.f_globals

Upvotes: 0

synthesizerpatel
synthesizerpatel

Reputation: 28036

Not specifically related to your problem, but you might find this code useful -- automatically starts up the python debugger when a fatal exception occurs. Good for working with interactive code. It's originally from ActiveState

# code snippet, to be included in 'sitecustomize.py'
import sys

def info(type, value, tb):
   if hasattr(sys, 'ps1') or not sys.stderr.isatty():
      # we are in interactive mode or we don't have a tty-like
      # device, so we call the default hook
      sys.__excepthook__(type, value, tb)
   else:
      import traceback, pdb
      # we are NOT in interactive mode, print the exception...
      traceback.print_exception(type, value, tb)
      print
      # ...then start the debugger in post-mortem mode.
      pdb.pm()

sys.excepthook = info

Upvotes: 2

pyfunc
pyfunc

Reputation: 66709

Did you have a look at traceback module?

Also on SO:

Upvotes: 0

Related Questions