Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > excepthook doesn't give exact line number

Reply
Thread Tools

excepthook doesn't give exact line number

 
 
Hari Sekhon
Guest
Posts: n/a
 
      10-03-2006
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):
<do something>

exception_message = ( "\nLine no %s: %s - %s\n"
"\nStack Trace:\n\n%s\n" %
(tb.tb_lineno,type,value,str(traceback.print_exc(2 ))) )

<send me an email etc....>

sys.excepthook = myexcepthook

<now do some work....>



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

 
Reply With Quote
 
 
 
 
Peter Otten
Guest
Posts: n/a
 
      10-03-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why wouldn't this line of give give the address of the pointer? grocery_stocker C Programming 10 05-25-2005 03:05 AM
Read a file line by line with a maximum number of characters per line Hugo Java 10 10-18-2004 11:42 AM
Python Services and excepthook (not working) Larry Bates Python 0 03-05-2004 05:47 PM
RE: "Error in sys.excepthook" message from Python 2.3multi-threadedscript Tim Peters Python 0 10-30-2003 10:19 PM
"Error in sys.excepthook" message from Python 2.3 multi-threaded script Follower Python 1 10-27-2003 03:12 PM



Advertisments