f



Embedding Python in Python

Hi,

I've been programming in Python for a couple of weeks now and am quite
impressed with the language - it was very easy to learn (it actually
reminds me a lot of programming in BASIC many many years ago - it's
very fast to hack together a couple lines of code in interactive
mode). I'm throwing together a simple little game (sort of a MUD-type
thing) as my first 'big' project, and I thought it would be cool to be
able to script the game in Python.

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. So what I'd like to be able to do is essentially sandbox
the code: expose a set of global
variables/functions/modules/classes/whatever to the script for
interaction with the game world, and make sure that the script doesn't
do silly things like file access. A little research has indicated that
previous versions of Python had sandboxing capabilities but these have
broken in new releases and don't look like they're getting fixed any
time soon. So is there any (safe) way to embed Python within itself
for application scripting?

Thanks,
- Darryl Shpak
0
madhobbit (3)
10/8/2003 6:35:14 PM
comp.lang.python 77058 articles. 6 followers. Post Follow

6 Replies
1396 Views

Similar Articles

[PageSpeed] 43

Darryl <madhobbit@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.
0
10/8/2003 7:00:54 PM
JCM <joshway_without_spam@myway.com> wrote in message news:<bm1mt6$maa$1@fred.mathworks.com>...
> (...) What we did was parse the users' code
> (take a look at the compiler module) and statically analyze it for
> anything "dangerous".

Ugh...I had considered this approach but was hoping I didn't have to
do it :) I've only been hacking Python for a couple weeks, so I don't
know all the ins and outs of what I'd need to watch for...in my
experience, most languages seem to have obscure ways of doing things
that you wouldn't think of normally, and Python seems no exception.
From a more pragmatic perspective though, this is just a toy project,
so it's not too important if I miss something.

> (...) anything of the form __ident__

Now that's an interesting idea...from what I've seen of Python, that
should catch most of the really obscure ways of doing things. I
haven't looked, but I can only assume that Python has regexp matching
in it somewhere, so some of the simple checks should be only a few
lines of code. A blacklist of keywords seems like a good first start
(it's a fairly restrictive context, so even common things like def and
lambda can probably be blocked).

I also just tested and noticed that syntax errors seem to be thrown as
exceptions (rather than causing the interpreter to gasp and die), so I
shouldn't have to worry about a badly-written script crashing the
whole app.

Thanks for the input,
- Darryl
0
madhobbit (3)
10/9/2003 2:39:29 PM
In article <e56627ef.0310090639.2b1eb0a2@posting.google.com>,
Darryl <madhobbit@geocities.com> wrote:
>JCM <joshway_without_spam@myway.com> wrote in message
>news:<bm1mt6$maa$1@fred.mathworks.com>...
>> (...) What we did was parse the users' code
>> (take a look at the compiler module) and statically analyze it for
>> anything "dangerous".
>
>Ugh...I had considered this approach but was hoping I didn't have to
>do it :) I've only been hacking Python for a couple weeks, so I don't
>know all the ins and outs of what I'd need to watch for...in my
>experience, most languages seem to have obscure ways of doing things
>that you wouldn't think of normally, and Python seems no exception.
>From a more pragmatic perspective though, this is just a toy project,
>so it's not too important if I miss something.
>
>> (...) anything of the form __ident__
>
>Now that's an interesting idea...from what I've seen of Python, that
>should catch most of the really obscure ways of doing things. I
>haven't looked, but I can only assume that Python has regexp matching
>in it somewhere, so some of the simple checks should be only a few
>lines of code. A blacklist of keywords seems like a good first start
>(it's a fairly restrictive context, so even common things like def and
>lambda can probably be blocked).
>
>I also just tested and noticed that syntax errors seem to be thrown as
>exceptions (rather than causing the interpreter to gasp and die), so I
>shouldn't have to worry about a badly-written script crashing the
>whole app.
			.
			.
			.
I'm surprised--astounded, in fact--that those more expert
with Python than I haven't already jumped in to correct
errors that seem to be arising in this thread.

Yes, we all count on the Python interpreter to toss excep-
tions when it's unhappy, *not* "to gasp and die".  There
are a few situations it can't handle, but only few.

There's a lot to say on the subject of interpretation of
code supplied by users.  In particular, the three Python
projects
  rexec
  Bastion
  RestrictedPython
all address this requirement on a technical level.  If 
your goal is a useful working application, I strongly 
urge you to read up on these.  Correct construction on
your own of a "blacklist" is ... difficult.
-- 

Cameron Laird <claird@phaseit.net>
Business:  http://www.Phaseit.net
0
claird429 (924)
10/10/2003 7:23:29 PM
Cameron Laird <claird@lairds.com> wrote:
....
> I'm surprised--astounded, in fact--that those more expert
> with Python than I haven't already jumped in to correct
> errors that seem to be arising in this thread.

What errors?

> Yes, we all count on the Python interpreter to toss excep-
> tions when it's unhappy, *not* "to gasp and die".  There
> are a few situations it can't handle, but only few.

The original question was about ensuring users write code which is,
by some definition, safe.  For example, you don't want users calling
"os.system('rm -rf /')".  You also don't want them overriding
builtins.

> There's a lot to say on the subject of interpretation of
> code supplied by users.  In particular, the three Python
> projects
>   rexec
>   Bastion
>   RestrictedPython
> all address this requirement on a technical level.  If 
> your goal is a useful working application, I strongly 
> urge you to read up on these.  Correct construction on
> your own of a "blacklist" is ... difficult.

I'm under the impression that the rexec and Bastion modules do not
work with Python 2.3+.  I'm not familiar with RestrictedPython.
0
10/10/2003 9:02:36 PM
JCM wrote:
   ...
> I'm under the impression that the rexec and Bastion modules do not
> work with Python 2.3+.  

Right -- alas -- they don't give 100% security, so they were disabled
(also in 2.2.3 -- they had just the same exploitable weaknesses in
earlier 2.2 releases).

> I'm not familiar with RestrictedPython.

It's part of Zope.  I have no experience of how well or badly it works.


Alex


0
aleaxit (1612)
10/10/2003 9:11:58 PM
claird@lairds.com (Cameron Laird) wrote in message news:<voe1ph32i3s93@corp.supernews.com>...
> There's a lot to say on the subject of interpretation of
> code supplied by users.  In particular, the three Python
> projects
>   rexec
>   Bastion
>   RestrictedPython
> all address this requirement on a technical level.  If 
> your goal is a useful working application, I strongly 
> urge you to read up on these.  Correct construction on
> your own of a "blacklist" is ... difficult.

Given my current level of Python knowledge, I don't consider myself
capable of writing such a blacklist - I might be able to pull it off
in other languages, but not Python. I've looked at rexec and Bastion,
and (as another poster mentioned) got the impression they were
defunct. I've also been pointed at Zope, but haven't had time to
investigate it.

Since this is a casual project that I'm not spending too much time on,
I'll probably ignore the issue for now. Since I often see Python
promoted as an application scripting language, I had hoped that it had
built-in restrictions of this nature, akin to a Java applet's sandbox.
I'm now getting the impression that if I embedded Python as the
scripting language in any app, it would be similar to using VBA - an
unrestricted environment that gives the script the power to do
whatever it wants.

I think I'll go ahead with the project with only a few simple
restrictions right now (i.e. no 'import') and only allow trusted users
to write scripts. I'll also run the application in a chroot jail
(OS-specific, I know) to limit the amount of damage that can be done.

Thanks for all the advice,
- Darryl
0
madhobbit (3)
10/14/2003 1:47:59 PM
Reply:

Similar Artilces:

python is a python
python is a python ...

embedding python in python #3
Hi, anyone had any experiences in embedding python in python? I've tried to do this but it doesn't work. eval("from Tkinter import *") Thanks maurice Use exec. On Wed, Sep 29, 2004 at 09:23:28AM +0000, Maurice LING wrote: > Hi, > > anyone had any experiences in embedding python in python? > > I've tried to do this but it doesn't work. > > eval("from Tkinter import *") Maurice LING <mauriceling@acm.org> wrote in message news:<415a7f0b$1@news.unimelb.edu.au>... > Hi, > > anyone had any experiences in embeddi...

Embedding Python in Python #2
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 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 im...

Invoking Python from Python
Hi all, I have a need to create a Python script on the fly from another Python program and then execute the script so created. Do I need to invoke Python through os.spawnl or is there a better way? Thanks, -- John Am Tue, 08 Nov 2005 08:10:25 -0800 schrieb John Henry: > Hi all, > > I have a need to create a Python script on the fly from another Python > program and then execute the script so created. Do I need to invoke > Python through os.spawnl or is there a better way? Hi, creating source code with a script, is no good solution. Once I had to maintain lisp code w...

Python mascot? How about a Pythoneer?
I've been thinking that a good Python mascot might help in raising public awareness of Python. The problem with a snake as a mascot is that it is not anthropomorphic enough. An effective mascot has to have something approximating two arms, two legs, and a head, so that he can be put into various interesting situations and adventures. (At the very least, he has to be able to work a keyboard and a mouse!) Good examples can be found in some of the imaginative things that have been done with Linux's Tux. So a snake is out. So are Terry Gilliam's wonderful cartoons for the Flyin...

Python to Python communication
Hello, I have a group of Python programms which I want to teach to "talk to each other". All run on Windows, on the same computer or in the same intranet. Security of communication is not an issue (encryption on lower level protocols / intra computer communication). Partially these programs are using wxPython. It is important that the communcation is "only an added feature", I am not willing to spend big memory / computing ressources on it. So I googled and came up with at least the following opportunities: XMLRPC - quite "simple" to implement within py...

Embedded Python
I embed multiple interpreters. I create the interpreter and modules in the primary thread of my application: PyEval_AcquireLock(); thread = Py_NewInterpreter(); PyThreadState_Swap(thread); ....initialize modules, etc.... PyThreadState_Swap(maininterpreter); PyEval_ReleaseLock(); Then I create a C thread called "main" which calls a function called "Main" in the Python interpreter: PyEval_AcquireLock(); PyThreadState_Swap(thread); moduledictionary = PyModule_GetDict(pmodule); PyObject_CallObject(PyDict_GetItemString(moduledictionary, "Main"), NULL); PyThreadSta...

Embedding a python console inside a python application
Hi, I am starting an application in which I would like to have some scripting functionality. It will obviously be done in Python. The thing is that I would like my scripts to have access to the rest of the application as an object it could manipulate. I made some research and I found the code module, which allows to have a Python interpreter inside an application, but I am not sure if I can access to the "parent" which created this interpreter. A solution could be to launch my application through an interpreter, but the problem there is how to integrate it back into the GUI, redirec...

Python loading library containing embedded python...
I have a dll/so which embeds python. I can verify it works by compiling it as an executable and adding an appropriate main. I tried to write unit tests for this library with ctypes and a simple python script. Access violations and other strange things result. I suspect this is because I am basically embedding python in python at this point. How can I make my dll/so with embedded python support use via ctypes? If Py_NewInterpreter is the answer, why does it hang indefinitely? The dll/so must support use in processes which might contain other instances of Python. I can not change that requi...

Can I embedding a (python) console on python program?
HI All, Apologize for being a newbie to python area and sorry for my English.=20 Actually what I need is embedding a python interactive console(or other shell console alike module) on my python program for debugging and controlling purpose during the program runtime.=20 For example, print/set some important value, query some runtime status when I remote login to host via telnet/ssh when the program running on the host. One of the idea is I can write the program use the GUI tech, but that is not fit for someone use a text based session. In summary, in my scheme, it should something looks li...

Injecting python function in an embedded python engine
Hi All in the list, I've embedded python v2.6.x engine into my application without any problem. Now I would like to inject some additional functions after importing a python module. So, basically I'm importing a python module via PyImport_ImportModule() function. The python module is a simple set of functions and I would check if some functions does exist or not, than if doesn't I would add it from my C application. Checking if a function does exist or not isn't an issue, it works without any problem, my problem is only how to inject a new function in an already imported modul...

Embedding Python: Creating Python Class from Application
Hello All, Let's say I have a following python code: class hw_class: def __init__(self): pass def hello_world(self): print 'Hello World!' create_instance('hw_class', 'hw') hw.hello_world() hw = None The 'create_instance' function should be implemented in the application (powered by Delphi - P4D) which is embedding the Python.dll. I am trying to do this for some time having no success. I am missing these informations: 1. How to register global python variable from application which is embedding python? 2. How to assign this variable wi...

Embedded python on systems without python installed
I have python successfully embedded in a program I wrote. What files do I need and where do I need to put them so that it can run on systems that don't have python installed? KillSwitch wrote: > I have python successfully embedded in a program I wrote. > > What files do I need and where do I need to put them so that it can > run on systems that don't have python installed? > I embed python in Delphi apps, and the only thing I add is python24.dll, which I put in the same directory as the Delphi executable, (but i you want a less clean install, you can put the dll als...

real-time monitoring of propriety system: embedding python in C or embedding C in python?
Hi Group, at work, we are thinking to replace some legacy application, which is a hom= e-grown scripting language for monitoring and controlling a large experimen= t. It is able to read live data from sensors, do some simple logic and calc= ulations, send commands to other subsystems and finally generate some new s= ignals. The way it is implemented is that it gets a chunk of 1 second of da= ta (thousands of signals at sample rates from 1Hz to several kHz), does som= e simple calculations on selected signals, does some simple logic, sends so= me commands and finally computes some 1Hz...

Web resources about - Embedding Python in Python - comp.lang.python

Embedding - Wikipedia, the free encyclopedia
In mathematics , an embedding (or imbedding ) is one instance of some mathematical structure contained within another instance, such as a group ...

Improved Embedding Tools: Embedded Video Player API and oEmbed support
Launch of embedded video API and oEmbed support for facebook videos and posts

Instagram adds web embedding, makes sharing content on the web easier
... its platform beyond its mobile applications. Today, the photo sharing application has made it easier to share photos and videos with web embedding. ...

School transforms a generation by embedding Indigenous culture into its curriculum
From Indigenous dolls in the childcare centre to a discussion of the treatment of Adam Goodes, embedding Indigenous culture into the curriculum ...


Tumblr for iOS adds blog creation, trending tags widget, quick video embedding, more
... it, post your first post to it” right from the iPhone and iPad app. No web browser or computer required. Tumblr says its new video embedding ...

Instagram Adds Web Embedding in Bid for Real-Time Relevance
The real-time battle rages on. Instagram on Wednesday announced the latest in a string of recent updates, now allowing users to embed videos ...

The new Android Central Photography forums are open and feature Photosphere embedding
A great place to talk about the camera on your Android phone, and share the great pictures you take with it

Apple granted patent for embedding sapphire displays in LiquidMetal iPhone chassis
The U.S. Patent and Trademark Office on Tuesday issued Apple a patent describing a process in which an iPhone's display glass including sapphire ...

MPAA: you can infringe copyright just by embedding a video
... coalition of Internet giants and public interest groups over the key question of whether it's possible to directly infringe copyright by embedding ...

Resources last updated: 1/29/2016 8:34:56 AM