excepthook doesn't give exact line number

Hari Sekhon hpsekhon at googlemail.com
Thu Oct 5 13:09:35 CEST 2006

I've tried the sample code you provided but it seems to just hang, it 
must be doing something but unfortunately it must take too long, by 
which time a second control-c gives an awful dual traceback message 
showing the original traceback and the new one from the tbiter() func.

I've tried a few variations since yesterday but unfortunately those 2 
funcs take too long to run to make a viable solution and the script ends 
up hanging on an uncaught exception.

If anybody has any improvements, suggestions or alternatives for getting 
the proper line number and traceback message inside an excepthook then 
I'd be grateful to hear them.


Hari Sekhon

Peter Otten wrote:
> 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
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20061005/004314a2/attachment.html>

More information about the Python-list mailing list