Deeper tracebacks?

brooklineTom BrooklineTom at gmail.com
Wed Dec 10 18:50:16 EST 2008


On Dec 10, 5:03 pm, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Wed, 10 Dec 2008 16:59:16 -0200, brooklineTom <Brookline... at gmail.com>
> escribió:
>
> > I want my exception handler to report the method that originally
> > raised an exception, at the deepest level in the call-tree. Let give
> > an example.
>
> That's the default behavior, you don't have to do anything special.
>
> > import sys, traceback
> > class SomeClass:
> >     def error(self):
> >         """Raises an AttributeError exception."""
> >         int(3).zork()
>
> >     def perform_(self, aSelector):
> >         try:
> >             aMethod = getattr(self, aSelector, None)
> >             answer = apply(aMethod, [], {})
> >         except: AttributeError, anAttributeErrorException:
> >             aRawStack = traceback.extract_stack()
> >             answer = None
>
> (I assume you're using Python < 3.0)
> Use the 3-names form of the except statement:
>
>          try:
>              aMethod = getattr(self, aSelector, None)
>              answer = aMethod()
>          except AttributeError, e, tb:
>              # the tb variable holds the traceback up to the error
>              # the same thing you see printed by Python when
>              # an unhandled error happens
>              answer = None
>
> Alternatively, you can obtain the same thing with sys.exc_info()[2]
> Remember to delete any reference to the traceback object as soon as you're
> done with it; seehttp://docs.python.org/library/sys.html#sys.exc_info
>
> --
> Gabriel Genellina

I'm using Python 2.5.

As I understand it, "aRawStack" (above) has the same information as
sys.exc_info()[2].

The deepest entry in aRawStack is the perform_ invocation. The
contents of the two bottom-most stack frames are:
>>> aRawStack[8][3]
"answer = anObject.perform_('error')"
>>> aRawStack[9][3]
'aRawStack = traceback.extract_stack()'


By the time the handler is called, "zork" -- the method that was
called when the exception was raised, and "error", the method that
invoked zork, have already been removed from the stack.

In this example, I only show one call for simplicity. In practice, the
method being invoked by perform_ may nest calls arbitrarily deeply. I
know, by being in the exception handler, that an exception happened.
What I need to know is which nested call raised it.



More information about the Python-list mailing list