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.

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

  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
    #2
    1. Advertising

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

    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:>
    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
    #3
    1. Advertising

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
    Replies:
    5
    Views:
    554
    Honestmath
    Dec 13, 2004
  2. SS
    Replies:
    0
    Views:
    343
  3. Chris Rebert

    Re: executing arbitrary statements

    Chris Rebert, Oct 1, 2011, in forum: Python
    Replies:
    4
    Views:
    116
    Jonathan Hartley
    Oct 3, 2011
  4. Lloyd Zusman
    Replies:
    9
    Views:
    155
    Lloyd Zusman
    Feb 20, 2005
  5. Mario Gr
    Replies:
    3
    Views:
    299
    Mario Gr
    Jul 4, 2009
Loading...

Share This Page