Problems with InteractiveInterpreter
Tim Peters
tim_one at email.msn.com
Mon Apr 12 22:40:39 EDT 1999
[Marcel Lanz, runs this:
from code import InteractiveInterpreter
lines = open('test2.py', 'r').readlines()
ii = InteractiveInterpreter()
for line in lines:
ii.runsource(line)
over input containing
for i in 'hello':
print i
and gets an error]
> any ideas?
Sure! Stick "print " in front of your ii.run... line, look at the return
values it prints, then read runsource's docstring. You can't expect it to
work feeding it one line at a time; you need to feed it a complete
executable fragment; & the return values tell you whether you've fed it
enough.
You'll get a bit closer doing this instead:
input = ""
for line in lines:
input = input + line
status = ii.runsource(input)
if status == 0:
input = ""
But then you'll find that
for i in 'hello':
print i
print "OK!"
doesn't work as you want. At that point, you're ready to study the
InteractiveConsole class (also in code.py) to see what's really needed.
try-to-think-like-the-interpreter<wink>-ly y'rs - tim
More information about the Python-list
mailing list