From: tim_one at email.msn.com (Tim Peters)
Date: Tue, 13 Apr 1999 02:40:39 GMT
Subject: Problems with InteractiveInterpreter
In-Reply-To: <370F80BE.30B889A9@isoe.ch>
References: <370F80BE.30B889A9@isoe.ch>
Message-ID: <001001be8557$04b9ffa0$97a02299@tim>
Content-Length: 1044
X-UID: 549                                                  

[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






