Better tracebacks for exec and execfile

Erwin S. Andreasen erwin at andreasen.com
Tue Sep 3 19:12:21 CEST 2002


Stefan Franke <franke at ableton.com> writes:

> 1. I'm toying araound with a text templating engine containing
> embedded Python scripts. These scripts are run with exec.
> If they throw an exception, the traceback looks like
> 
>   File "<string>", line 5
>      e = a+c+b+
>                ^
>  SyntaxError: invalid syntax

I don't know how to make SyntaxError appear correctly, but for the
others you can use compile which lets you pass in a file name. You can
then exec the compiled string.

compile won't let you specify a start line number: for that I simply
added "\n" * start_line to the start of the evaluated string :)


-- 
===============================================================
<erwin at andreasen.org>                           Herlev, Denmark     
<URL:http://www.andreasen.org/>                             <*>   
===============================================================




More information about the Python-list mailing list