compile() source code from a string

Chris Gonnerman chris.gonnerman at usa.net
Thu Mar 1 23:26:46 EST 2001


There IS a syntax error in your code.  To wit:

----- Original Message -----
From: "Costas Menico" <costas at meezon.com>
Subject: compile() source code from a string
[ vicious hack ]
> However I would like to execute the following example from string
> variable:
>
> if x == 1:
>   print "1"
>   print "2"
>   print "3"
> elif x = 2:

ERROR   ^^^
Should be:

elif x == 2:

>   print 'A'
>   print 'B'
>   print 'C'
>
> print 1+2
>
[ brutal hack ]

Whenever you do something fancy like this and it doesn't work, you should
build a stripped example and test that.  Your code doesn't work in a string,
but hey, it doesn't work as a normal module file either.

I don't intend to be mean about it; I've been there too.

Attached is an example using your sample, corrected, which works for me.

-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: testcompile.py
URL: <http://mail.python.org/pipermail/python-list/attachments/20010301/ae939545/attachment.ksh>


More information about the Python-list mailing list