Python: How to use code.InteractiveConsole?
- by Rosarch
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.