Get Only the Last Items in a Traceback

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Fri Nov 9 20:52:10 EST 2007


En Wed, 07 Nov 2007 01:53:31 -0300, gregpinero at gmail.com  
<gregpinero at gmail.com> escribió:

> This works great except for syntax errors.  Any idea why your solution
> doesn't catch those?
>
> Here's the output it gives me, followed by the code I'm using (running
> in Python 2.5):
>
> Traceback (most recent call last):
>   File "traceback_test.py", line 27, in gamma
>     exec s in {}
>   File "<string>", line 6
>      print hi'
>              ^
>  SyntaxError: EOL while scanning single-quoted string

Which would be your expected output?
Syntax errors happen when the code is *compiled*, before it gets executed.
You may catch syntax errors before even trying to execute the code, if you  
do it in two steps:

     try:
         ccode = compile(s, filename, 'exec')
     except SyntaxError:
         ...show the error appropiately...
         ...abort execution...
     try:
         exec ccode in {}
     except Exception, e:
         ...handle exception...


-- 
Gabriel Genellina




More information about the Python-list mailing list