Python: How to use code.InteractiveConsole?

Posted by Rosarch on Stack Overflow See other posts from Stack Overflow or by Rosarch
Published on 2010-05-29T17:30:33Z Indexed on 2010/05/29 17:32 UTC
Read the original article Hit count: 147

Filed under:
|
|

I'm trying to use InteractiveConsole to create a new front-end for a Python interpreter. These code fragments are from me playing around with InteractiveConsole in IDLE:

>>> ses = code.InteractiveConsole()
>>> ses.runsource("def foo():")
True
>>> ses.runsource("    return 2")
  File "<input>", line 1
SyntaxError: 'return' outside function (<input>, line 1)
False

Why does it raise a syntax error? How else can I finish writing the function?

Also, for something like this:

>>> ses.runsource("x = 1")
False
>>> ses.runsource("x")
1
False

How can I capture the 1 value from above? False is the return value, but 1 is written to some stream.

© Stack Overflow or respective owner

Related posts about python

Related posts about idle