"IOError: [Errno 12] Not enough space" on Windows 7 and Python 2

This is caused by click/_winconsole.py:268-272. It seems that switching the standard streams to binary mode reveals the underlying limit of the Windows console of limiting a single write to 64K (I think this limit was lifted in Windows 8). This affects print, sys.stdout.write and click.echo.

It seems that many libraries and Python itself limit a single write to 32767 bytes to workaround this. Python 3 for example: Python/fileutils.c:1244-1250. It doesn't seem like Python 2 has this hack though.

We will have to either:

  1. Don't set the streams to binary and read/write to dupped stream fds (and file objects) that are set to binary.
  2. Wrap the standard streams and workaround this.
  3. Submit a patch to Python 2 which I'm not sure will ever be accepted.
  4. Other?

To reproduce:

import sys
import click

click.echo("spam")

print('A' * 100000) # or
sys.stdout.write('A' * 100000) # or
click.echo('A' * 100000)

Windows 7 SP1 x64
Python: 2.7.10
Click: 6.7