excepthook doesn't give exact line number

Peter Otten __peter__ at web.de
Tue Oct 3 10:46:10 EDT 2006


Hari Sekhon wrote:

> The problem is that the excepthook gives the line of the topmost called
> function rather that the actual line that generated the error the way
> you get it with a normal traceback.

A look into the traceback module shows that tracebacks are stored as a
linked list. Here's a way to get hold of its tail:

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

def last(items):
    for  item in items:
        pass
    return item

# example usage
def myexcepthook(type, value, tb):
    tb_tail = last(tbiter(tb))
    print tb_tail.tb_lineno
 
Peter



More information about the Python-list mailing list