Help with use of code.InteractiveInterpreter for multiline code

R

rick_muller

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.0 0
FalseFile "<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
 
J

Jean Brouwers

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


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.0 0
FalseFile "<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
 
J

Jim Sizelove

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:
hi, a
False

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

HTH,
Jim
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top