开发者

inconsistency of redirecting sys.stderr in IPython and console

Redirecting sys.stderr to a text file in IPython and console(Gnome terminal) yields different results.

f=open('std.log','w')
sys.stderr=f
raise Exception,"message goes here"

In IPython, the error message is printed directed into the screen.

In [13]: run std.py

---------------------------------------------------------------------------
Exception                                 Traceback (most recent call last)

/home/xiaohan/code/diving-in-python/htmlparser/std.py in <module>()
      2 f=open('std.log','w')
      3 sys.stderr=f
----> 4 raise Exception,"message goes here"
      5 
      6

Exception: message goes here
WARNING: Failure executing file: <std.py>

However, if I ran it in console directly.

python std.py 

T开发者_JAVA百科he error message is hidden and redirected to the text file.

Any suggestions on what is going on here??


The problem is not writing to stderr, but IPython's run command intercepting exceptions. When the program raises an unhandled exception, IPython will print the backtrace to its own console, plus an additional warning (WARNING: Failure executing file). This is done by installing an exception hook function as sys.excepthook.

If you explicitely write to sys.stderr in your test script, it will get written to the log file as expected.

To see the exception hook, add print(sys.excepthook) to your script. When executed directly, it will be <built-in function excepthook>, within IPython, something like <bound method InteractiveShell.excepthook of <IPython.iplib.InteractiveShell object at 0x022FA3F0>>.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜