Better tracebacks for exec and execfile

Erwin S. Andreasen erwin at
Tue Sep 3 13:12:21 EDT 2002

Stefan Franke <franke at> 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>                           Herlev, Denmark     
<URL:>                             <*>   

More information about the Python-list mailing list