Executing other python code

T

Tim Rau

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?
 
D

Diez B. Roggisch

Tim said:
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
 
A

Arnaud Delobelle

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!
 
S

Stefan Behnel

Hi,

Tim said:
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
 
G

Guyon Morée

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top