Skip to content
Myr

Merge pull request #830 from segevfiner/not-enough-space-workaround

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

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

authored by David Lord · committed by GitHub · parents 065653e, 3496086 · Browse files · 4 files+54 added−3 removed
This commit

State

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

Diff stats

4 files+54 added−3 removed

  • other:+54 added−3 removed

4 files+54 added−3 removed

modified:click/_compat.py+7 added-1 removed
222 unmodified lines
223 def get_binary_stdin():224 return set_binary_mode(sys.stdin)225226 def get_binary_stdout():227 _wrap_std_stream('stdout')228 return set_binary_mode(sys.stdout)229230 def get_binary_stderr():231 _wrap_std_stream('stderr')232 return set_binary_mode(sys.stderr)233234 def get_text_stdin(encoding=None, errors=None):235 rv = _get_windows_console_stream(sys.stdin, encoding, errors)
2 unmodified lines
238 return _make_text_stream(sys.stdin, encoding, errors,239 force_readable=True)240241 def get_text_stdout(encoding=None, errors=None):242 _wrap_std_stream('stdout')243 rv = _get_windows_console_stream(sys.stdout, encoding, errors)244 if rv is not None:245 return rv246 return _make_text_stream(sys.stdout, encoding, errors,247 force_writable=True)248249 def get_text_stderr(encoding=None, errors=None):250 _wrap_std_stream('stderr')251 rv = _get_windows_console_stream(sys.stderr, encoding, errors)252 if rv is not None:253 return rv254 return _make_text_stream(sys.stderr, encoding, errors,
330 unmodified lines
585if WIN:586 # Windows has a smaller terminal587 DEFAULT_COLUMNS = 79588585 from ._winconsole import _get_windows_console_stream589 from ._winconsole import _get_windows_console_stream, _wrap_std_stream590591 def _get_argv_encoding():592 import locale593 return locale.getpreferredencoding()
53 unmodified lines
647 def _get_argv_encoding():648 return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()649650 _get_windows_console_stream = lambda *x: None651 _wrap_std_stream = lambda *x: None652653654def term_len(x):655 return len(strip_ansi(x))
17 unmodified lines
673 if rv is not None:674 return rv675 rv = wrapper_func()676 try:677 stream = src_func() # In case wrapper_func() modified the stream678 cache[stream] = rv679 except Exception:680 pass681 return rv
22 unmodified lines
modified:click/_winconsole.py+34 added
199 unmodified lines
200 self.encoding,201 )202203204class WindowsChunkedWriter(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_write226227228_wrapped_std_streams = set()229230231def _wrap_std_stream(name):232 # Python 2 & Windows 7 and below233 if PY2 and sys.getwindowsversion()[:2] <= (6, 1) and name not in _wrapped_std_streams:234 setattr(sys, name, WindowsChunkedWriter(getattr(sys, name)))235 _wrapped_std_streams.add(name)236237238def _get_text_stdin(buffer_stream):239 text_stream = _NonClosingTextIOWrapper(240 io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)),241 'utf-16-le', 'strict', line_buffering=True)
66 unmodified lines
modified:docs/wincmd.rst+9 added-2 removed
57 unmodified lines
58This hackery is used on both Python 2 and Python 3 as neither version of59Python has native support for cmd.exe with unicode characters. There are60some limitations you need to be aware of:6162* this unicode support is limited to ``click.echo``, ``click.prompt`` as62* This unicode support is limited to ``click.echo``, ``click.prompt`` as63 well as ``click.get_text_stream``.64* depending on if unicode values or byte strings are passed the control64* Depending on if unicode values or byte strings are passed the control65 flow goes completely different places internally which can have some66 odd artifacts if data partially ends up being buffered. Click67 attempts to protect against that by manually always flushing but if68 you are mixing and matching different string types to ``stdout`` or69 ``stderr`` you will need to manually flush.70* The raw output stream is set to binary mode, which is a global71 operation on Windows, so ``print`` calls will be affected. Prefer72 ``click.echo`` over ``print``.73* On Windows 7 and below, there is a limitation where at most 64k74 characters can be written in one call in binary mode. In this75 situation, ``sys.stdout`` and ``sys.stderr`` are replaced with76 wrappers that work around the limitation.7778Another important thing to note is that the Windows console's default79fonts do not support a lot of characters which means that you are mostly80limited to international letters but no emojis or special characters.
modified:CHANGES.rst+4 added
85 unmodified lines
86- Update doc to match arg name for ``path_type``. (`#801`_)87- Raw strings added so correct escaping occurs. (`#807`_)88- Fix 16k character limit of ``click.echo`` on Windows. (`#816`_,89 `#819`_)90- Overcome 64k character limit when writing to binary stream on91 Windows 7. (`#825`_, `#830`_)92- Add bool conversion for "t" and "f". (`#842`_)93- ``NoSuchOption`` errors take ``ctx`` so that ``--help`` hint gets94 printed in error output. (`#860`_)95- Fixed the behavior of Click error messages with regards to Unicode
117 unmodified lines
213.. _#816: https://github.com/pallets/click/pull/816214.. _#819: https://github.com/pallets/click/pull/819215.. _#821: https://github.com/pallets/click/issues/821216.. _#822: https://github.com/pallets/click/issues/822217.. _#825: https://github.com/pallets/click/issues/825218.. _#830: https://github.com/pallets/click/pull/830219.. _#842: https://github.com/pallets/click/pull/842220.. _#860: https://github.com/pallets/click/issues/860221.. _#862: https://github.com/pallets/click/issues/862222.. _#863: https://github.com/pallets/click/pull/863
413 unmodified lines