[issue11395] print(s) fails on Windows with long strings
David-Sarah Hopwood
report at bugs.python.org
Sun Mar 27 03:31:41 CEST 2011
David-Sarah Hopwood <david-sarah at jacaranda.org> added the comment:
If I understand the bug in the Windows console functions correctly, a limit of 32767 bytes might not always be small enough. The problem is that if two or more threads are concurrently using any console functions (which all use the same 64 KiB heap), they could try to allocate up to 32767 bytes plus overhead at the same time, which will fail.
I wasn't able to provoke this by writing to sys.stdout.buffer (maybe there is locking that prevents concurrent writes), but the following code that calls WriteFile directly, does provoke it. GetLastError() returns 8 (ERROR_NOT_ENOUGH_MEMORY; see http://msdn.microsoft.com/en-us/library/ms681382%28v=vs.85%29.aspx), indicating that it's the same bug.
# Warning: this test may DoS your system.
from threading import Thread
import sys
from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_int
from ctypes.wintypes import BOOL, HANDLE, DWORD, LPVOID, LPCVOID
GetStdHandle = WINFUNCTYPE(HANDLE, DWORD)(("GetStdHandle", windll.kernel32))
WriteFile = WINFUNCTYPE(BOOL, HANDLE, LPCVOID, DWORD, POINTER(DWORD), LPVOID) \
(("WriteFile", windll.kernel32))
GetLastError = WINFUNCTYPE(DWORD)(("GetLastError", windll.kernel32))
STD_OUTPUT_HANDLE = DWORD(-11)
INVALID_HANDLE_VALUE = DWORD(-1).value
hStdout = GetStdHandle(STD_OUTPUT_HANDLE)
assert hStdout is not None and hStdout != INVALID_HANDLE_VALUE
L = 32760
data = b'a'*L
def run():
n = DWORD(0)
while True:
ret = WriteFile(hStdout, data, L, byref(n), None)
if ret == 0 or n.value != L:
print(ret, n.value, GetLastError())
sys.exit(1)
[Thread(target=run).start() for i in range(10)]
----------
nosy: +davidsarah
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue11395>
_______________________________________
More information about the Python-bugs-list
mailing list