Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > catching syntax errors via excepthook?

Reply
Thread Tools

catching syntax errors via excepthook?

 
 
Hari Sekhon
Guest
Posts: n/a
 
      07-03-2006
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
 
Reply With Quote
 
 
 
 
Alex Martelli
Guest
Posts: n/a
 
      07-03-2006
Hari Sekhon <(E-Mail Removed)> wrote:

> 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
 
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 does catching errors that aren't thrown give syntax errors? yawnmoth Java 97 02-27-2009 12:07 AM
Syntax check not catching error marc rei VHDL 6 10-20-2006 02:45 AM
Errors, errors, errors Mark Goldin ASP .Net 2 01-17-2004 08:05 PM
Catching Framework Errors TonyG ASP .Net 1 08-26-2003 06:52 PM
Catching ASP.NET errors Mikael Engdahl ASP .Net 3 08-15-2003 06:18 PM



Advertisments