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
Provenance
- Commit
a589835- Tree
5ae58b1- Author
David Lord <davidism@gmail.com>- Committer
GitHub <noreply@github.com>- Parent 1
065653e- Parent 2
3496086
4 files changed+54 added−3 removed
modified:click/_compat.py+7 added-1 removed
222 unmodified lines223 def get_binary_stdin():224 return set_binary_mode(sys.stdin)225 226 def get_binary_stdout():227+ _wrap_std_stream('stdout')228 return set_binary_mode(sys.stdout)229 230 def get_binary_stderr():231+ _wrap_std_stream('stderr')232 return set_binary_mode(sys.stderr)233 234 def get_text_stdin(encoding=None, errors=None):235 rv = _get_windows_console_stream(sys.stdin, encoding, errors)2 unmodified lines238 return _make_text_stream(sys.stdin, encoding, errors,239 force_readable=True)240 241 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)248 249 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 lines585 if WIN:586 # Windows has a smaller terminal587 DEFAULT_COLUMNS = 79588 585- from ._winconsole import _get_windows_console_stream589+ from ._winconsole import _get_windows_console_stream, _wrap_std_stream590 591 def _get_argv_encoding():592 import locale593 return locale.getpreferredencoding()53 unmodified lines647 def _get_argv_encoding():648 return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()649 650 _get_windows_console_stream = lambda *x: None651+ _wrap_std_stream = lambda *x: None652 653 654 def term_len(x):655 return len(strip_ansi(x))17 unmodified lines673 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 rv22 unmodified linesmodified:click/_winconsole.py+34 added
199 unmodified lines200 self.encoding,201 )202 203 204+class 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 = wrapped214+215+ def __getattr__(self, name):216+ return getattr(self.__wrapped, name)217+218+ def write(self, text):219+ total_to_write = len(text)220+ written = 0221+222+ 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_write226+227+228+_wrapped_std_streams = set()229+230+231+def _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)236+237+238 def _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 linesmodified:docs/wincmd.rst+9 added-2 removed
57 unmodified lines58 This hackery is used on both Python 2 and Python 3 as neither version of59 Python has native support for cmd.exe with unicode characters. There are60 some limitations you need to be aware of:61 62-* 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.77 78 Another important thing to note is that the Windows console's default79 fonts do not support a lot of characters which means that you are mostly80 limited to international letters but no emojis or special characters.modified:CHANGES.rst+4 added
85 unmodified lines86 - 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 Unicode117 unmodified lines213 .. _#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/863413 unmodified lines