Embedding Python in Python

Robey Holderith robey at slash_dev_slash_random.org
Wed Aug 18 15:56:04 EDT 2004


On Wed, 18 Aug 2004 14:35:21 -0400, Phil Frost wrote:

> You probably want something like this:
> 
> globalDict = {}
> exec(stringOfPythonCodeFromUser, globalDict)
> 
> globalDict is now the global namespace of whatever was in
> stringOfPythonCodeFromUser, so you can grab values from that and
> selectivly import them into your namespace.
> 

So using this (with a little additional reading) it looks like I
can do this:

globalDict = {'__builtins__': <my modules here>}
exec(<pythonCodeFromUser>, globalDict)

And that this will disallow both importing of new modules and direct
access to my namespace.  It will however allow access to the

Would this be secure?

Paul, what's your take on this?

-Robey

> On Wed, Aug 18, 2004 at 02:26:00PM -0500, Robey Holderith wrote:
>> 
>> Anyone know a good way to embed python within python?
>> 
>> Now before you tell me that's silly, let me explain
>> what I'd like to do.
>> 
>> I'd like to allow user-defined scriptable objects.  I'd
>> like to give them access to modify pieces of my classes.
>> I'd like to disallow access to pretty much the rest of
>> the modules.
>> 
>> Any ideas/examples?
>> 
>> -Robey





More information about the Python-list mailing list