Last active
December 15, 2015 04:19
-
-
Save EBNull/5200354 to your computer and use it in GitHub Desktop.
Testcase of Python issue 706263, with a workaround.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#This file is a testcase of http://bugs.python.org/issue706263 with a workaround. | |
#Z:\>python 4096_tc.py | |
#Z:\>cat result.txt | |
#stdin: 0 stdout: 1 stderr: 2 | |
#Bytes printed: 199998 | |
#Z:\>pythonw 4096_tc.py | |
#Z:\>cat result.txt | |
#stdin: -2 stdout: -2 stderr: -2 | |
#Bytes printed before exception was raised: 4096 | |
#Traceback (most recent call last): | |
# File "4096_tc.py", line 42, in <module> | |
# sys.stdout.write('a ') | |
#IOError: [Errno 9] Bad file descriptor | |
def fixup_win_gui_stdio(): | |
"""Correct potential issues with a win32 gui application that uses stdio. | |
A Win32 GUI Application sets it's stdin, stdout, and stderr to -2 or -1 to indicate that the streams do not exist. | |
With default configurations, writes to stdout/err will raise InvalidHandle after 4096 bytes (the default buffer size). | |
See http://bugs.python.org/issue706263 for more information. | |
""" | |
import sys, os | |
if hasattr(sys.stdin, 'fileno') and sys.stdin.fileno() < 0: | |
sys.stdin = open(os.devnull, "rb") | |
if hasattr(sys.stdout, 'fileno') and sys.stdout.fileno() < 0: | |
sys.stdout = open(os.devnull, "wb") | |
if hasattr(sys.stderr, 'fileno') and sys.stderr.fileno() < 0: | |
sys.stderr = open(os.devnull, "wb") | |
#Uncomment to test above solution | |
#fixup_win_gui_stdio() | |
import sys | |
import traceback | |
counter = 0 | |
fout = open('result.txt', 'w') | |
try: | |
for counter in range(100000): | |
sys.stdout.write('a ') | |
#sys.stdout.flush() | |
fout.write('stdin: %s stdout: %s stderr: %s\n'%(sys.stdin.fileno(), sys.stdout.fileno(), sys.stderr.fileno())) | |
fout.write('Bytes printed: %d\n' % (counter*2)) | |
except: | |
fout.write('stdin: %s stdout: %s stderr: %s\n'%(sys.stdin.fileno(), sys.stdout.fileno(), sys.stderr.fileno())) | |
fout.write('Bytes printed before exception was raised: %d\n' % (counter*2)) | |
traceback.print_exc(100, fout) | |
finally: | |
fout.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment