pythonでコンソールを作る

コンソールをエミュレートするcodeというモジュールがあるらしい。
http://www.python.jp/doc/release/library/code.html

codeでは

InteractiveInterpreter : python構文解析インタプリタ状態を担当
InteractiveConsole : コンソールエミュレータ (InteractiveInterpreterの子クラス)
の2つが定義されている。

例えば、以下の様なコードを書くとコンソールもどきが起動できる。

#!/usr/bin/env python
import code
console = code.InteractiveConsole()
console.interact()

$ python codo_consoele.py
Python 2.7.1 (r271:86832, Jun 25 2011, 05:09:01)
[GCC 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2335.15.00)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
(InteractiveConsole)
>>> 1 + 1 * 2
3
>>> for i in range(3):
... print i
... 0
1
2
>>> ^D