473,397 Members | 1,961 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Executing other python code

I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
Jan 29 '08 #1
6 997
AFAIK this can't be done with just python. You can use the C API of
Python to achieve this. I don't know the details of that, but I guess
you will need this (http://docs.python.org/api/api.html).

Rolf

Tim Rau wrote:
I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
Jan 29 '08 #2
Tim Rau wrote:
I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
You might consider spawning a process and using Pyro to communicate. I've
done that before and it worked pretty well.

Diez
Jan 29 '08 #3
On Jan 29, 12:18*am, Tim Rau <bladedpeng...@gmail.comwrote:
I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
One simple solution would be to forbid import statements in the
scripts, to import the scripts as modules and inject whatever
functions you want them to be able to use in the module's namespace.

So:

====== script.py =======

def play():
if enemy_is_near():
fire_gun()
else:
move_forward()

=======================

====== Game engine ====

scriptobjs = [enemy_is_near, fire_gun, move_forward, turn_left,
turn_right]

def getscript(scriptname):
script = __import__(scriptname)
for obj in scriptobjs:
setattr(script, obj.__name__, obj)
return script

def playscript(script):
script.play()

=======================

Something like this. Obviously this is over simplified!

--
Arnaud
Jan 29 '08 #4
Over-simplified yes, but it will work!

Python is beautiful :-)
Jan 29 '08 #5
Hi,

Tim Rau wrote:
I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
Depends. If you are concerned about security, this will be hard to achieve in
Python. I imagine that this could be related to cheating prevention.

If you are more concerned about namespace polution and making only a well
defined API available to the scripts, go the module-import way and hand some
API object over, either through an initialisation function or by injecting it
into the module namespace, as Arnaud suggested.

Stefan
Jan 29 '08 #6
One simple solution would be to forbid import statements in the
scripts, to import the scripts as modules and inject whatever
functions you want them to be able to use in the module's namespace.

how do you 'forbid' imports?
Guyon
http://gumuz.nl
Jan 31 '08 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: David Faden | last post by:
Hi, Given arbitrary Python source code input by the user, I want to get the result of executing that source as an expression if it is an expression and otherwise execute the source as a...
13
by: Darren Dale | last post by:
Some time ago I asked about executing a python program or script. For windows, I was informed that the .py extension could be added to some list of executable extensions, and then I could just type...
0
by: Nick Coghlan | last post by:
Anyone playing with the CPython interpreter's new command line switch might have noticed that it only works with top-level modules (i.e. scripts that are directly on sys.path). If the script is...
15
by: Nick Coghlan | last post by:
Python 2.4's -m command line switch only works for modules directly on sys.path. Trying to use it with modules inside packages will fail with a "Module not found" error. This PEP aims to fix that...
1
by: Valentina Boycheva | last post by:
Thanks for the reply. I already have "Learning Python" from Mark Lutz and David Ascher, which covers 2.3 (I am in 2.4). However, it seems like heavy artillery to me. What I want is, for instance,...
3
by: Pankaj | last post by:
I am facing a very basic problem as any new bie would face. I know perl and now i want to use python In perl, it is very simple , just "perl scriptname.pl" will execute the script. But i...
2
by: krishna.000.k | last post by:
file1.py ---------- a = 20 from abc import * print "Should this be printed when 'a' is alone imported from this module" file2.py ---------- from file1 import a
0
by: Gordon Fraser | last post by:
Hi, I'm trying to parse Python code to an AST, apply some changes to the AST and then compile and run the AST, but I'm running into problems when trying to evaluate/execute the resulting code...
0
by: Orestis Markou | last post by:
Have you tried passing in empty dicts for globals and locals? I think that the defaults will be the *current* globals and locals, and then of course your namespace is broken... On Tue, Oct 7,...
3
by: jim3371 | last post by:
Using wxPython, I'm looking to build a GUI app for a daemon-based app, on Win32 platform, how would I go about executing the daemon app so it stays in the background when the Py app is running?...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.