Help with use of code.InteractiveInterpreter for multiline code

Discussion in 'Python' started by rick_muller@yahoo.com, Dec 3, 2004.

  1. Guest

    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
     
    , Dec 3, 2004
    #1
    1. Advertising

  2. 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 <>,
    <""> 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
    >
     
    Jean Brouwers, Dec 3, 2004
    #2
    1. Advertising

  3. Jim Sizelove Guest

    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.
    >

    [examples of calling the interpreter instance]
    > What's the proper way to call the interpreter instance for a multiline
    > example like this?
    >
    > Thanks in advance,
    >
    > Rick
    >


    I haven't worked with the code module before, but here is my go at it.
    It seems that the call to runsource() needs the entire multiline code at
    once, with newlines including a trailing newline:

    >>> import code
    >>> a = code.InteractiveInterpreter()
    >>> a.runsource('def q():\n print "hi, a"\n')

    False
    >>> a.runsource('q()')

    hi, a
    False

    With the InteractiveConsole, you can push each line of code
    individually, like so:

    >>> import code
    >>> b = code.InteractiveConsole()
    >>> b.push('def q():')

    True
    >>> b.push(' print "hi, b"')

    True
    >>> b.push('')

    False
    >>> b.runsource('q()')

    hi, b
    False

    HTH,
    Jim
     
    Jim Sizelove, Dec 3, 2004
    #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. =?Utf-8?B?RGFuIFNpa29yc2t5?=

    HOWTO: Use multiline asp:texbox scrollbars on panel that is disabl

    =?Utf-8?B?RGFuIFNpa29yc2t5?=, Apr 11, 2006, in forum: ASP .Net
    Replies:
    0
    Views:
    484
    =?Utf-8?B?RGFuIFNpa29yc2t5?=
    Apr 11, 2006
  2. Yatima

    Multiline regex help

    Yatima, Mar 3, 2005, in forum: Python
    Replies:
    13
    Views:
    560
    Kent Johnson
    Mar 4, 2005
  3. Dave W.
    Replies:
    7
    Views:
    909
    Dave W.
    Apr 16, 2010
  4. Colin Howell
    Replies:
    2
    Views:
    575
    John Bokma
    Apr 25, 2010
  5. dale zhang
    Replies:
    8
    Views:
    428
    Tintin
    Nov 30, 2004
Loading...

Share This Page