Help with use of code.InteractiveInterpreter for multiline code

Jean Brouwers mrjean1ATcomcastDOTnet at no.spam.net
Fri Dec 3 12:01:11 EST 2004


Take a look at the source code of the code module (file code.py in the
lib directory).  The push() method of the InteractiveConsole class
shows how to handle multi-line statements.

Basically, keep collecting lines as long as the result returned by the
runsource() call is true.  Maybe instead of calling the runsource()
method, just call the push() method.

/Jean Brouwers


In article <1102089880.142191.29790 at z14g2000cwz.googlegroups.com>,
<"rick_muller at yahoo.com"> wrote:

> I'm trying to embed a Python interpreter in a GUI I'm developing, and
> I'm having trouble understanding the proper use of
> code.InteractiveInterpreter.
> 
> Here's what I'm trying:
> 
> % python
> Python 2.3 (#1, Sep 13 2003, 00:49:11)
> [GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
> Type "help", "copyright", "credits" or "license" for more information.
> >>> from code import InteractiveInterpreter
> >>> a = InteractiveInterpreter()
> >>> a.runsource('a = 0')
> False
> >>> a.runsource('b = 0')
> False
> >>> a.runsource('print a,b')
> 0 0
> False
> >>> a.runsource('def q():')
> True
> >>> a.runsource('    print "hi"')
> File "<input>", line 1
> print "hi"
> ^
> SyntaxError: invalid syntax
> False
> 
> 
> What's the proper way to call the interpreter instance for a multiline
> example like this?
> 
> Thanks in advance,
> 
> Rick
>



More information about the Python-list mailing list