Re: excepthook doesn't give exact line number
Thanks for the pointer, I've now got this giving me the right line number when an exception occurs, although I still get an empty stack trace from print "Stack Trace:\n%s\n" % str(traceback.print_exc(2)) inside the excepthook. Any ideas why this is? Is there no traceback since the traceback was fed to the excepthook? Is there another way of getting the traceback like you see when the exception isn't caught? Thanks for the help. -h 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: excepthook doesn't give exact line number
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. -h 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 -- http://mail.python.org/mailman/listinfo/python-list
Re: excepthook doesn't give exact line number
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 -- http://mail.python.org/mailman/listinfo/python-list
excepthook doesn't give exact line number
Hi, I'm wondering if anyone can please help me on figuring out a better way of doing an excepthook. I have a script which is using an excepthook to catch any uncaught exceptions - there usually aren't any except when I am messing with the code, like right now :-) 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. import sys, traceback def myexcepthook(type,value,tb): exception_message = ( "\nLine no %s: %s - %s\n" "\nStack Trace:\n\n%s\n" % (tb.tb_lineno,type,value,str(traceback.print_exc(2))) ) sys.excepthook = myexcepthook So the tb object that is passed into the function gives the tb.tb_lineno as a line right near the end where the original topmost called function happens. This is a bit useless to me since I don't want to go looking for the exception manually through the entire code. As you can see from my example, I have already used the traceback module which I usually use to give the normal traceback printout via traceback.print_exc(). Here it doesn't seem to work, it always give "None", likely because the excepthook has taken it or something. Any guiding wisdom from the masters out there? Much appreciated, thanks for reading. -h -- Hari Sekhon -- http://mail.python.org/mailman/listinfo/python-list