There is a known issue in Python, where "close failed in file object destructor" when "Broken pipe" happens on stdout - Python tracker Issue 11380; also seen in python - Why does my Python3 script balk at piping its output to head or tail (sys module)? - Stack Overflow.
What I want to do, is printout the same custom message when this problem happens, in both Python 2.7 and Python 3+. So I prepare a test script, testprint.py
and run it (snippets shown done in bash
, Ubuntu 11.04):
$ cat > testprint.py <<"EOF"
import sys
def main():
teststr = "Hello " * 5
sys.stdout.write(teststr + "\n")
if __name__ == "__main__":
main()
EOF
$ python2.7 testprint.py
Hello Hello Hello Hello Hello
$ python2.7 testprint.py | echo
close failed in file object destructor:
sys.excepthook is missing
lost sys.stderr
$ python3.2 testprint.py | echo
Exception IOError: (32, 'Broken pipe') in <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> ignored
As expected from above links, there are two different messages. In Help with a piping error (velocityreviews.com), it is recommended to use sys.stdout.flush()
to force Python 2 to register an IOError instead of that message; with that, we have:
$ cat > testprint.py <<"EOF"
import sys
def main():
teststr = "Hello " * 5
sys.stdout.write(teststr + "\n")
sys.stdout.flush()
if __name__ == "__main__":
main()
EOF
$ python2.7 testprint.py | echo
Traceback (most recent call last):
File "testprint.py", line 9, in <module>
main()
File "testprint.py", line 6, in main
sys.stdout.flush()
IOError: [Errno 32] Broken pipe
$ python3.2 testprint.py | echo
Traceback (most recent call last):
File "testprint.py", line 9, in <module>
main()
File "testprint.py", line 6, in main
sys.stdout.flush()
IOError: [Errno 32] Broken pipe
Exception IOError: (32, 'Broken pipe') in <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> ignored
OK, getting closer... Now, the way to "ignore" these exceptions (or in my case, replace with a custom error message), is to handle them:
Ignore exceptions - comp.lang.python
> Is there any way to make [interpreter] ignore exceptions.
Nope. Either handle the exceptions or write code that doesn't generate exceptions.
... and as An Introduction to Python - Handling Exceptions notes, the way to do that is a try/except block. So let's try that:
$ cat > testprint.py <<"EOF"
import sys
def main():
teststr = "Hello " * 5
try:
sys.stdout.write(teststr + "\n")
sys.stdout.flush()
except IOError:
sys.stderr.write("Exc: " + str(sys.exc_info()[0]) + "\n")
if __name__ == "__main__":
main()
EOF
$ python2.7 testprint.py | echo
Exc: <type 'exceptions.IOError'>
$ python3.2 testprint.py | echo
Exc: <class 'IOError'>
Exception IOError: (32, 'Broken pipe') in <_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'> ignored
Ok, so try/except works as I expect it for Python 2.7 - but then, Python 3.2 both handles as expected, and still generates a Exception ... ignored
message! What's the problem - isn't "except IOError
" enough for Python 3? But it must be - otherwise it wouldn't have printed the custom "Exc:...
" message!
So - what is the problem here, and why is the Exception ... ignored
still printed in Python 3, even if I'm handling the exception? And more importantly, how do I handle it so the Exception ... ignored
does not get printed anymore?