Skip to content
Myr

Wrap stdout/stderr to avoid "Not enough space" in Python 2 on Windows 7

Before adoption: this commit came in with the history the project adopted, so no landing records it.

When you set the standard streams to binary you seem to only be able to write up to some limit in a single call to write. So we wrap the streams and write in chunks to avoid this. A similar hack was also implemented in Python 3 to work around this. But that hack doesn't seem to exist in Python 2. This might have unintended side effects due to replacing the standard stream objects. Fixes #825

authored by Segev Finer · committed by David Lord · parent 065653e · Browse files · 2 files+33 added−0 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+33 added−0 removed

  • other:+33 added−0 removed

2 files+33 added−0 removed

modified:click/_compat.py+1 added
667 unmodified lines
668 if rv is not None:669 return rv670 rv = wrapper_func()671 try:672 stream = src_func() # In case wrapper_func() modified the stream673 cache[stream] = rv674 except Exception:675 pass676 return rv
22 unmodified lines
modified:click/_winconsole.py+32 added
199 unmodified lines
200 self.encoding,201 )202203204class StreamWrapper(object):205 """206 Wraps a stream (such as stdout), acting as a transparent proxy for all207 attribute access apart from method 'write()' which we wrap to write in208 limited chunks due to a Windows limitation on binary console streams.209 """210 def __init__(self, wrapped):211 # double-underscore everything to prevent clashes with names of212 # attributes on the wrapped stream object.213 self.__wrapped = wrapped214215 def __getattr__(self, name):216 return getattr(self.__wrapped, name)217218 def write(self, text):219 total_to_write = len(text)220 written = 0221222 while written < total_to_write:223 to_write = min(total_to_write - written, MAX_BYTES_WRITTEN)224 self.__wrapped.write(text[written:written+to_write])225 written += to_write226227228def _get_text_stdin(buffer_stream):229 text_stream = _NonClosingTextIOWrapper(230 io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)),231 'utf-16-le', 'strict', line_buffering=True)232 return ConsoleStream(text_stream, buffer_stream)233234235def _get_text_stdout(buffer_stream):236 if PY2:237 buffer_stream = StreamWrapper(buffer_stream)238 sys.stdout = StreamWrapper(sys.stdout)239240 text_stream = _NonClosingTextIOWrapper(241 io.BufferedWriter(_WindowsConsoleWriter(STDOUT_HANDLE)),242 'utf-16-le', 'strict', line_buffering=True)243 return ConsoleStream(text_stream, buffer_stream)244245246def _get_text_stderr(buffer_stream):247 if PY2:248 buffer_stream = StreamWrapper(buffer_stream)249 sys.stderr = StreamWrapper(sys.stderr)250251 text_stream = _NonClosingTextIOWrapper(252 io.BufferedWriter(_WindowsConsoleWriter(STDERR_HANDLE)),253 'utf-16-le', 'strict', line_buffering=True)254 return ConsoleStream(text_stream, buffer_stream)
51 unmodified lines