Opened 8 months ago

Last modified 8 months ago

#9305 defect new

Failure.getTracebackObject result lacks outer stack frames

Reported by: Richard van der Hoff Owned by:
Priority: normal Milestone:
Component: core Keywords: review
Cc: Branch:
Author:

Description

Failure.getTracebackObject constructs a fake Traceback which includes the stack frames between the point the exception was thrown and where it was caught, but doesn't include any of the frames above it. We have this information (in failure.stack), and it may be useful for debugging, so it's regrettable that it isn't included in the fake Traceback.

We ought to be able to pass the stack into traceback.print_stack, but doing so actually causes exceptions.AttributeError: '_Frame' object has no attribute 'f_lineno'.

Test program:

#!/usr/bin/env python
import traceback

import sys
from twisted.logger import globalLogBeginner, textFileLogObserver
from twisted.internet import defer

globalLogBeginner.beginLoggingTo([textFileLogObserver(sys.stdout)])

def raiser(_):
    raise Exception()

def report(failure):
    tb = failure.getTracebackObject()
    # if tb were a real Traceback, this would work
    traceback.print_stack(tb.tb_frame)

# raise an exception from a callback, but don't yet handle it
# (which prompts the Failure object to forget its real Traceback object)
d = defer.Deferred()
d.addCallback(raiser)
d.callback(None)

# now try to report it
d.addErrback(report)

Change History (2)

comment:1 Changed 8 months ago by Richard van der Hoff

Type: enhancementdefect

comment:2 Changed 8 months ago by Richard van der Hoff

Keywords: review added
Note: See TracTickets for help on using tickets.