472,328 Members | 1,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,328 software developers and data experts.

catching syntax errors via excepthook?

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)
I've been intentionally introducing errors into the code to try to test
it and while it catches import errors and other things, it doesn't catch
syntax errors.

Is there a way to get it to catch syntax errors?
Or is there a better way?

-h
Jul 3 '06 #1
1 2088
Hari Sekhon <se*********@googlemail.comwrote:
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)
I've been intentionally introducing errors into the code to try to test
it and while it catches import errors and other things, it doesn't catch
syntax errors.

Is there a way to get it to catch syntax errors?
Python, of course, parses (and in fact compiles) a whole module before
executing any of it, so if you're talking about syntax error in the
source of the very module which (when executed) will install an
excepthook, no way. Apart from this, no problem, e.g.:
>>def myexcepthook(type,value,tb):
.... print 'error', value
....
>>sys.excepthook=myexcepthook
2+
error invalid syntax (<stdin>, line 1)
>>m=open('za.py', 'w')
m.writelines('''print "hello"
.... print 2+
.... ''')
>>m.close()
import za
error invalid syntax (za.py, line 2)

etc, etc, i.e., the hook does catch all syntax errors due to parsing
which occurs *after* the hook is installed (in interactive sessions or
other modules). Clearly the hook cannot catch any error which occur
*before* the hook is installed.

You can delay the detection of syntax errors, for example, by explicitly
calling the compile built-in function on string literals, rather than
relying on implicit compilation of sources; and/or you can strive to
have your excepthook installed ASAP, e.g. by playing with
sitecustomize.py and/or $PYTHONSTARTUP .
Alex
Jul 3 '06 #2

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

Similar topics

3
by: Peter A. Schott | last post by:
I know there's got to be an easy way to do this - I want a way to catch the error text that would normally be shown in an interactive session and...
6
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...
1
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...
0
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...
1
by: Hari Sekhon | last post by:
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...
1
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 :...
25
by: JJ | last post by:
I only want to catch 404 errors at the application level (the rest are will be handled by the customerrors section of the web.config). How do I...
5
by: Stefan Bellon | last post by:
Hi all! I am embedding Python into a GUI application in a way that the GUI is scriptable using Python. Now I have come to a problem that when...
3
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:...
0
by: tammygombez | last post by:
Hey everyone! I've been researching gaming laptops lately, and I must say, they can get pretty expensive. However, I've come across some great...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: teenabhardwaj | last post by:
How would one discover a valid source for learning news, comfort, and help for engineering designs? Covering through piles of books takes a lot of...
0
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
1
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...

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.