Loading and executing an arbitrary Python script from within Python

T

TheDustbustr

I'm writing a game in C++ that calls out to Python for scripting. The C++
kernel holds an instance of ScriptCtl and calls the load(filename) method to
load a script, then run() to run all loaded scripts.

<code>
[scriptctl.py]
class ScriptCtl:
threads=[]

def load(self, filename):
f=file(filename, 'r')
contents=f.read()
f.close()
exec(contents) #contents exec'ed within the current scope and
namespace, not within their own
self.threads.append(main)

def run(self):
while self.threads:
for g in self.threads:
try:
g.next()
except StopIteration:
self.threads.remove(g)

[cutscene.py]
from __future__ import generators
def cutscene():
from time import time
import sys
from utility import sleep

print "EVIL KNIGHT: I will kill you now!"; sys.stdout.flush()
for s in sleep(1): yield None
print "OUR HERO: I shall fight you to the death. Bring it on!";
sys.stdout.flush()
for s in sleep(1.5): yield None
print "***End of cutscene***"; sys.stdout.flush()

main=cutscene() #initialize the generator for use by scriptctl
</code>

This works, barely. As a result of using exec, each script is executed in
ScriptCtl.load()'s namespace. Because of this, imports have to be done inside
the script functions, or the imports go out of scope before the functions get
called (in scriptctl.run()). Imports also have to be done as from
scriptctl.py, even though scriptctl.py deeper in the package hierarchy (so
instead of 'import core.utility' I must do 'import utility' because both
utility.py and scriptctl.py are in the same folder (core).

This sucks.

How else can I solve this problem? Ideally, I'd use Stackless Python
microthreads, but version 3.0 is not out yet and there is no documentation on
the current was to do microthreads.

Any ideas? I'm stuck.
Thanks, Dustin
 
S

Steven Taschuk

Quoth Raymond Arthur St. Marie II of III :
[...]
Did I get the concept wrong about using
a generater in a try section? Is this code legal anyone?

Or does this only pertain to the yeild statement in a try.

The prohibition is on yield statements in the try section of a
try-finally statement. The posted code is fine.

(This is prohibited because there is no guarantee that execution
will ever return to the generator after the yield -- the caller
might never call .next() again -- and so the finally block might
never be executed. This was judged too grave a violation of
try/finally's semantics to permit.)
 
C

Christian Tismer

TheDustbustr wrote:

.....
This sucks.
Indeed.

How else can I solve this problem? Ideally, I'd use Stackless Python
microthreads, but version 3.0 is not out yet and there is no documentation on
the current way to do microthreads.

Stackless is in fact the best way to do this, IMHO.

Unfortunately, 5 minutes before getting SLP 3.0 out,
most support for Stackless was shut down (no names here).
So I have to find a way to continue, which I definately
will, but things will slow down, again.

Maybe you can help me, and I will help you!

cheers - chris

--
Christian Tismer :^) <mailto:[email protected]>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
 

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

Forum statistics

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

Latest Threads

Top