Line by line execution of python code

Discussion in 'Python' started by Justin Powell, Jul 13, 2006.

  1. Hi, I'm looking for suggestions on how to accomplish something in python. If
    this is the wrong list for such things, I appologize and please disregard the
    rest.

    My application needs to allow users to create scripts which will be executed
    in a statement-by-statement fashion. Here's a little pseudo-code:

    def someUserProgram():
    while 1:
    print "I am a user program"

    def mainProgram():
    someProgram = someUserProgram
    while 1:
    print "Doing some stuff here"
    executeOneStatement(someProgram)

    def executeOneStatement(program):
    # What goes in here?

    I would expect the output to look like this:

    Doing some stuff here
    Doing some stuff here
    I am a sub program
    Doing some stuff here
    Doing some stuff here
    I am a sub program
    etc.

    It's possible to use generators to accomplish this, but unfortunately a
    state-ment by statement executing would require that every other statement is
    a yield. That would either force the users to put in a yield after every
    statement, or require a mechanism that parses the source and inserts yield
    statement before execution. Neither of these are really satisfactory

    The other methods I've considered for doing this cleanly (subclassing the
    debugger, or the interactive interpreter) seem to revolve around sys.settrace
    and a callback trace function. I couldn't figure out a way to use this
    mechanism to resume the main loop from the trace function, and then also
    resume the user program the next time around, without the call stack just
    spiraling out of control.

    Anybody have a solution to this? I've done a bit of searching, looking
    through python docs and sources and so forth, and I'm pretty stumped at this
    point.

    Thanks.

    __________________________________________________
    Do You Yahoo!?
    Tired of spam? Yahoo! Mail has the best spam protection around
    http://mail.yahoo.com
     
    Justin Powell, Jul 13, 2006
    #1
    1. Advertising

  2. Justin Powell

    mkPyVS Guest

    Try this... slightly more complex but get's the job done-> added some
    wait state in the user program/thread so it didn't kill the output
    stream... Enjoy

    import thread, sys, time

    def someUserProgram(mutexRef):
    while 1:
    mutexRef.acquire()
    print "I am a user program"
    mutexRef.release()
    time.sleep(1)

    class main:
    def __init__(self):
    self.mutex = thread.allocate_lock()
    self.someProgram = None

    def mainProgram(self,someUserProgram):

    self.someProgram = someUserProgram
    self.executeOneStatement(self.someProgram)

    inval = ''
    while not inval == 'q':
    inval = sys.stdin.readline().strip()
    print "Doing some stuff here"
    thread.exit()

    def executeOneStatement(self,program):
    thread.start_new(program, (self.mutex,))



    l = main()

    l.mainProgram(someUserProgram)
    Justin Powell wrote:
    > Hi, I'm looking for suggestions on how to accomplish something in python. If
    > this is the wrong list for such things, I appologize and please disregard the
    > rest.
    >
    > My application needs to allow users to create scripts which will be executed
    > in a statement-by-statement fashion. Here's a little pseudo-code:
    >
    > def someUserProgram():
    > while 1:
    > print "I am a user program"
    >
    > def mainProgram():
    > someProgram = someUserProgram
    > while 1:
    > print "Doing some stuff here"
    > executeOneStatement(someProgram)
    >
    > def executeOneStatement(program):
    > # What goes in here?
    >
    > I would expect the output to look like this:
    >
    > Doing some stuff here
    > Doing some stuff here
    > I am a sub program
    > Doing some stuff here
    > Doing some stuff here
    > I am a sub program
    > etc.
    >
    > It's possible to use generators to accomplish this, but unfortunately a
    > state-ment by statement executing would require that every other statement is
    > a yield. That would either force the users to put in a yield after every
    > statement, or require a mechanism that parses the source and inserts yield
    > statement before execution. Neither of these are really satisfactory
    >
    > The other methods I've considered for doing this cleanly (subclassing the
    > debugger, or the interactive interpreter) seem to revolve around sys.settrace
    > and a callback trace function. I couldn't figure out a way to use this
    > mechanism to resume the main loop from the trace function, and then also
    > resume the user program the next time around, without the call stack just
    > spiraling out of control.
    >
    > Anybody have a solution to this? I've done a bit of searching, looking
    > through python docs and sources and so forth, and I'm pretty stumped at this
    > point.
    >
    > Thanks.
    >
    > __________________________________________________
    > Do You Yahoo!?
    > Tired of spam? Yahoo! Mail has the best spam protection around
    > http://mail.yahoo.com
     
    mkPyVS, Jul 13, 2006
    #2
    1. Advertising

  3. Justin Powell wrote:
    > Hi, I'm looking for suggestions on how to accomplish something in python. If
    > this is the wrong list for such things, I appologize and please disregard the
    > rest.


    No, this is totally the right place.


    > My application needs to allow users to create scripts which will be executed
    > in a statement-by-statement fashion. Here's a little pseudo-code:
    >
    > def someUserProgram():
    > while 1:
    > print "I am a user program"
    >
    > def mainProgram():
    > someProgram = someUserProgram
    > while 1:
    > print "Doing some stuff here"
    > executeOneStatement(someProgram)
    >
    > def executeOneStatement(program):
    > # What goes in here?
    >
    > I would expect the output to look like this:
    >
    > Doing some stuff here
    > Doing some stuff here
    > I am a sub program
    > Doing some stuff here
    > Doing some stuff here
    > I am a sub program
    > etc.
    >
    > It's possible to use generators to accomplish this, but unfortunately a
    > state-ment by statement executing would require that every other statement is
    > a yield. That would either force the users to put in a yield after every
    > statement, or require a mechanism that parses the source and inserts yield
    > statement before execution. Neither of these are really satisfactory.


    Hmm, ok, so you don't want to manually interfere with the partial programs.
    What I could think of is some form of code manipulation where you scan the
    source code of the function you get passed and add a yield after each statement.

    http://docs.python.org/dev/lib/inspect-source.html
    http://docs.python.org/dev/lib/module-parser.html


    > The other methods I've considered for doing this cleanly (subclassing the
    > debugger, or the interactive interpreter) seem to revolve around sys.settrace
    > and a callback trace function. I couldn't figure out a way to use this
    > mechanism to resume the main loop from the trace function, and then also
    > resume the user program the next time around, without the call stack just
    > spiraling out of control.


    I assume that tracing would be a good solution, though. Maybe someone else can
    tell you how to use it better than I could.

    You should tell us a bit more about the actual use case, i.e. *why* you need
    this. Maybe there's a better solution to the whole problem after all.

    Stefan
     
    Stefan Behnel, Jul 14, 2006
    #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. Andr? Roberge
    Replies:
    2
    Views:
    293
    Alexander Schliep
    Oct 18, 2004
  2. Sandra-24
    Replies:
    5
    Views:
    418
    Leif K-Brooks
    Apr 9, 2006
  3. noahwatkins

    displaying execution of Python code

    noahwatkins, Apr 3, 2008, in forum: Python
    Replies:
    3
    Views:
    257
    ajaksu
    Apr 4, 2008
  4. nehal
    Replies:
    2
    Views:
    730
    Tom Anderson
    Jan 17, 2009
  5. Erwin Moller
    Replies:
    7
    Views:
    126
    Phillip Gawlowski
    May 8, 2008
Loading...

Share This Page