Loading and executing an arbitrary Python script from within Python

Discussion in 'Python' started by TheDustbustr, Jul 26, 2003.

  1. TheDustbustr

    TheDustbustr Guest

    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.

    class ScriptCtl:

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

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

    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!";
    for s in sleep(1.5): yield None
    print "***End of cutscene***"; sys.stdout.flush()

    main=cutscene() #initialize the generator for use by scriptctl

    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
    TheDustbustr, Jul 26, 2003
    1. Advertisements

  2. 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
    "Its force is immeasurable. Even Computer cannot determine it."
    -- _Space: 1999_ episode "Black Sun"
    Steven Taschuk, Jul 28, 2003
    1. Advertisements

  3. Re: Loading and executing an arbitrary Python script from withinPython

    TheDustbustr wrote:


    > 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 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:>
    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/
    Christian Tismer, Aug 17, 2003
    1. Advertisements

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Honestmath
    Dec 13, 2004
  2. SS
  3. Chris Rebert

    Re: executing arbitrary statements

    Chris Rebert, Oct 1, 2011, in forum: Python
    Jonathan Hartley
    Oct 3, 2011
  4. Lloyd Zusman
    Lloyd Zusman
    Feb 20, 2005
  5. Mario Gr
    Mario Gr
    Jul 4, 2009

Share This Page