473,738 Members | 6,332 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(ty pe,value,tb):
<do something>

exception_messa ge = ( "\nLine no %s: %s - %s\n"
"\nStack Trace:\n\n%s\n" %
(tb.tb_lineno,t ype,value,str(t raceback.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

Oct 3 '06 #1
1 1815
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(ty pe, value, tb):
tb_tail = last(tbiter(tb) )
print tb_tail.tb_line no

Peter
Oct 3 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
7314
by: Follower | last post by:
Hi, I've run into an issue which seems to have been discussed previously on `python-dev` but only in context of Zope3: "Fun with 2.3 shutdown" -- Tim Peters <http://mail.python.org/pipermail/python-dev/2003-September/038151.html> The following message is displayed (one or more times) when exiting (in this case via a `KeyboardInterrupt` exception) a multi-threaded
1
2270
by: Josh Close | last post by:
When sys.excepthook is called, type, value and traceback are passed into it. How do I get the values of traceback? I've tried printing it, but I get the memory location. I'm gussing there are some methods to use on it. >From what I can tell the traceback that's passed in works differently from the module traceback. I just need to get the traceback to log it. I can use type and value,
149
25179
by: Christopher Benson-Manica | last post by:
(Followups set to comp.std.c. Apologies if the crosspost is unwelcome.) strchr() is to strrchr() as strstr() is to strrstr(), but strrstr() isn't part of the standard. Why not? -- Christopher Benson-Manica | I *should* know what I'm talking about - if I ataru(at)cyberspace.org | don't, I need to know. Flames welcome.
6
4445
by: Lunchtimemama | last post by:
Yo all, I'm getting into Python for the first time and I'm really having a blast. I've hit a bit of a snag and was wondering if someone could lend some insight. Here be the code: import sys def myexcepthook(type, value, tb): import traceback rawreport = traceback.format_exception(type, value, tb) report = '\n'.join(rawreport) errorlog = open('error.log','a')
1
8014
by: Jesus Rivero - (Neurogeek) | last post by:
Hello guys, I have this problem and i don't know any workarounds yet. Im implementing a DB Connection pool, which initially creates 20 connections and keep them in a dictionary. It also implements a method for allowing external method/classes to get a connection from that pool. he main issue is that, from a testing app, I create a loop like this to test the pool: import thread
1
2225
by: Hari Sekhon | last post by:
I've written an except hook into a script as shown below which works well for the most part and catches exceptions. import sys def myexcepthook(type,value,tb): do something sys.excepthook=myexcepthook rest of script.... (now protected by catchall exception hook)
0
1144
by: Larry Bates | last post by:
I have some classes that trap hook sys.excepthook and log exceptions prior to the program exiting. This has proven to be an effective way to log what is going on with many of my "lights out" processes. I'm doing some testing with Python 2.5b2 and can't seem to get it to work properly. Here is a very small script that I think should work but doesn't. import sys
1
1933
by: ian | last post by:
Hi, sys.excepthook don't work if an exception come in a thread... It's normal or its a bug ? There are any tip ? look here : http://spyced.blogspot.com/2005_06_01_archive.html Thx
3
2159
by: Sami | last post by:
Hello, I am new to Python. I tried to hook my own ExceptionPrintingFunction sys.excepthook but it does not work. This is what I wrote: ----------------------------------------------------------------------- import sys
0
8787
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9473
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9334
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8208
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6053
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4569
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.