Passing indented code to compile()

Peter Hansen peter at engcorp.com
Wed May 7 09:29:53 EDT 2003


John Wilson wrote:
> 
> I hit a problem when trying to compile some Python code embedded in an XML
> document.
> 
> For example:
> 
>         <execute globals="globals" locals="locals">
>             print "hello"
>         </execute>
> 
> If I just pass the string I construct from the SAX callbacks the compile
> function complains of a syntax error.
> 
> There seem to be two problems here:
> 
> 1/ The line '            print "hello"' is preceded by a blank line and
> followed by some whitespace.
> 
> 2/  The line '            print "hello"' starts with whitespace.

What you're faced with is, in effect, broken Python code.  Why not
just stop creating files with the bad/unnecessary indentation?

(I assume this code is created manually, because if it was written
out by some automated process, the tool that did it is broken and
should be fixed.  It looks like it was written by hand, with aesthetics
in mind.  As Harvey points out, this kind of thing is not how XML
(or Python!) was intended to be used.

-Peter




More information about the Python-list mailing list