Deeper tracebacks?

R. Bernstein rocky at panix.com
Thu Dec 11 04:49:42 EST 2008


brooklineTom <BrooklineTom at gmail.com> writes:

> 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.
>
> 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
>
> When I call "perform_" (... SomeClass().perform_('error')), I want to
> collect and report the location *within the method ("error") that
> failed*. The above code reports the location of "perform_", and no
> deeper in the call tree.
>
> Anybody know how to accomplish this?

extract_stack() without any arguments is getting this from the
*current frame* which as you noted doesn't have the last exception
info included which has been popped; variable sys.last_traceback has the frames
at the time of the exception, I think. 

So in your code try changing:
             aRawStack = traceback.extract_stack()
to
             aRawStack = traceback.extract_stack(sys.last_traceback)



More information about the Python-list mailing list