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
The 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’sruncommand 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 assys.excepthook.If you explicitely write to
sys.stderrin 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>>.