我正在Google應用程序引擎環境中工作,我正在從字符串加載doctests和python代碼來測試Python作業分配。我的基本實現(Provided by Alex Martelli)似乎適用於我所有的問題,除了那些包含print語句的問題。當我嘗試在GAE中執行打印命令時,某些東西似乎出錯了。在Google App Engine中的Python中,如何捕獲由print語句生成的輸出?
你會如何修改這個例子來捕獲由print語句寫出的任何東西?
#This and most other code works
class X(object): pass
x=X()
exec 'a=23' in vars(x)
#This throws an error.
class X(object): pass
x=X()
exec 'print 23' in vars(x)
...使用日誌模塊代替,不是嗎? – jldupont 2010-01-21 20:12:27
它拋出了什麼錯誤? – 2010-01-21 21:48:28