code.InteractiveInterpreterをサブクラス化するとき、ドキュメントで期待するようにwrite()メソッドを実行できないようです。
import code
class PythonInterpreter(code.InteractiveInterpreter):
def __init__(self, localVars):
self.runResult = ''
print 'init called'
code.InteractiveInterpreter.__init__(self, localVars)
def write(self, data):
print 'write called'
self.runResult = data
test = 'Hello'
interpreter = PythonInterpreter({'test':test})
interpreter.runcode('print test')
print 'Result:' + interpreter.runResult
期待される出力:
init called
write called
Result: Hello
実際の出力:
init called
Hello <- shouldn't print
Result:
何かご意見は?