Embedding Python in Python

JCM joshway_without_spam at myway.com
Wed Oct 8 15:00:54 EDT 2003


Darryl <madhobbit at geocities.com> wrote:
...
> Basically, I want to be able to type scripts in from within the game
> and attach them to objects, monsters, rooms, whatever. Now, I -could-
> use exec to run the code, but in an imaginary world where I intended
> to actually release this game to the public, I'd need to make sure
> that the user won't break anything, either accidentally or
> maliciously.

I worked on a similar project.  What we did was parse the users' code
(take a look at the compiler module) and statically analyze it for
anything "dangerous".  We took a very conservative approach--if I
remember correctly, we disallowed import statements, exec statements,
and a bunch of identifiers including eval, globals, and anything of
the form __ident__ (and maybe some other things).  Then we coded up
some machinery to let users safely import their own modules and a
subset of the Python library modules.  We also provided special
classes from which they could inherit which didn't require defining or
calling any functions named like __this__.

We could have done some dataflow analysis to be smarter about letting
users use some identifiers which we'd prohibited, but we never got
that far.




More information about the Python-list mailing list