[Tutor] Isolation of code
Noufal Ibrahim
noufal at nibrahim.net.in
Sun Oct 8 09:06:09 CEST 2006
Greetings everyone,
I'm working on a little project that's similar to tcltutor for python.
I have a string which has some python code. I "compile" and "eval" this
to get the output. So far so good. However, when this happens, the
evaluation is happening in the current namespace so I'm afraid of
symbols being redefined and messed up.
What I want to do is to create a separate namespace for all the code so
that I can execute it without worrying about symbol mangling.
Something like if the code I have is
code = """
x=5
if x==5:
print "Yes"
else:
print "No"
"""
I'd like to be able to create a separate module called (say) "tester"
with contents
def __start_exec__():
<contents of the code variable>
then say something like
import tester
tester.__start_exec__()
to actually run the code. This way, the namespace would be separate.
Any idea how I can go about this? I'm guessing I should use the parser
module to actually create the construct and then compile it but that looks
little hairy. Is there something I'm missing? Any suggestions?
Peace,
--
-NI
More information about the Tutor
mailing list