473,396 Members | 2,024 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,396 software developers and data experts.

Loading and executing an arbitrary Python script from within Python

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
Jul 18 '05 #1
2 2093
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.)

--
Steven Taschuk st******@telusplanet.net
"Its force is immeasurable. Even Computer cannot determine it."
-- _Space: 1999_ episode "Black Sun"

Jul 18 '05 #2
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:ti****@tismer.com>
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/

Jul 18 '05 #3

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

Similar topics

0
by: SS | last post by:
I am trying to execute some python commands within a perl script and running into problems. The following works: python -c "import mypkg;do this; do that" however when I try to do something like...
1
by: cwnewbe1 | last post by:
I would like to be able to add environment symbols to the Microsoft XP registry. I tried the example in the Python Cookbook by Orielly by running the script locally and it worked fine. Although I...
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...
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...
11
by: Bryant Huang | last post by:
Hello! I would like to read in files, during run-time, which contain plain Python function definitions, and then call those functions by their string name. In other words, I'd like to read in...
10
by: sneill | last post by:
Using XMLHTTP and DOM I'm able to load new HTML page content. I'd now like to load small snippets of javascript with the HTML markup and have that <script> incorporated into the page. If any of...
3
by: olaufr | last post by:
Hi, I need to call a python script, with command line arguments (it is an autonomous script with a __main__), from within another python script. Can I use exec() or execfile() for this? How to...
5
by: tdan | last post by:
When loading Javascript files with <scripttags in your HTML, do the files load AND run sequentially or does the HTML simply load the files and run them simultaneously. If not, how can you force...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.