First things first: I am writing a program which has, among other things, an embedded Python interpreter. So, before telling me that "using exec is unsafe because one could enter <...>", consider that the user could just as well enter <...> in the interpreter.

(Having gotten this off my chest :-)

In "Python in a Nutshell", Martelli writes, in at least two places, something like
"you should use exec only with specific, explicit dictionaries".


I would like to figure out how to do this [yes, for safety reasons :-), and also because I am curious] and, so far, I have been unsuccesful.

I have a "robot" that can do some actions like "move()" and "turn_left()". I can program this robot using python like this:
====
.def move_and_turn():
. move()
. turn_left()
.
.def draw_square():
. for i in range(4):
. move_and_turn()
.
.draw_square()
========
To execute such a program within my larger program, I use
exec code in globals()


where "code" is the little program above, and it works as expected. The question I have is: how do I do this with an explicit dictionary. I would *guess* that this is somehow equivalent to "how do I create a dictionary that has access only to robot instructions [move(), turn_left(), etc.] and Python's basic syntax" ... but I don't know how to do this.

Any help would be appreciated.

André
_______________________________________________
Tutor maillist  -  Tutor@python.org
http://mail.python.org/mailman/listinfo/tutor

Reply via email to