Also use the console emulation on Python 3. Refs #442
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Armin Ronacher · committed by Armin Ronacher · parent dde42ed · Browse files · 3 files·+29 added−15 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+29 added−15 removed
- other:+29 added−15 removed
Provenance
- Commit
2be872b- Tree
d9281b1- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
dde42ed
3 files changed+29 added−15 removed
modified:click/_compat.py+13 added-3 removed
172 unmodified lines173 return '<%s name=%r>' % (174 self.__class__.__name__,175 self.name,176 )177-178- from ._winconsole import _get_windows_console_stream177 else:178 set_binary_mode = lambda x: x181- _get_windows_console_stream = lambda *x: None179 180 def isidentifier(x):181 return _identifier_re.search(x) is not None182 190 unmodified lines373 'stream for sys.stderr.')374 return writer375 376 def get_text_stdin(encoding=None, errors=None):377+ rv = _get_windows_console_stream(sys.stdin, encoding, errors)378+ if rv is not None:379+ return rv380 return _force_correct_text_reader(sys.stdin, encoding, errors)381 382 def get_text_stdout(encoding=None, errors=None):383+ rv = _get_windows_console_stream(sys.stdout, encoding, errors)384+ if rv is not None:385+ return rv386 return _force_correct_text_writer(sys.stdout, encoding, errors)387 388 def get_text_stderr(encoding=None, errors=None):389+ rv = _get_windows_console_stream(sys.stderr, encoding, errors)390+ if rv is not None:391+ return rv392 return _force_correct_text_writer(sys.stderr, encoding, errors)393 394 def filename_to_ui(value):395 if isinstance(value, bytes):134 unmodified lines530 if WIN:531 # Windows has a smaller terminal532 DEFAULT_COLUMNS = 79533 534+ from ._winconsole import _get_windows_console_stream535+536 def _get_argv_encoding():537 import locale538 return locale.getpreferredencoding()539 51 unmodified lines591 else:592 def _get_argv_encoding():593 return getattr(sys.stdin, 'encoding', None) or get_filesystem_encoding()594 595+ _get_windows_console_stream = lambda *x: None596 597+598 def term_len(x):599 return len(strip_ansi(x))600 601 45 unmodified linesmodified:click/_winconsole.py+12 added-8 removed
9 unmodified lines10 11 import io12 import sys13 import ctypes14-from click._compat import _NonClosingTextIOWrapper, text_type14+from click._compat import _NonClosingTextIOWrapper, text_type, PY215 from ctypes import byref, POINTER, pythonapi, c_int, c_char, c_char_p, \16 c_void_p, py_object, c_ssize_t, c_ulong, windll, WINFUNCTYPE17 from ctypes.wintypes import LPWSTR, LPCWSTR18 171 unmodified lines190 self.encoding,191 )192 193 194-def _get_text_stdin():194+def _get_text_stdin(buffer_stream):195 text_stream = _NonClosingTextIOWrapper(196 io.BufferedReader(_WindowsConsoleReader(STDIN_HANDLE)),197 'utf-16-le', 'strict', line_buffering=True)198- return ConsoleStream(text_stream, sys.stdin)198+ return ConsoleStream(text_stream, buffer_stream)199 200 201-def _get_text_stdout():201+def _get_text_stdout(buffer_stream):202 text_stream = _NonClosingTextIOWrapper(203 _WindowsConsoleWriter(STDOUT_HANDLE),204 'utf-16-le', 'strict', line_buffering=True)205- return ConsoleStream(text_stream, sys.stdout)205+ return ConsoleStream(text_stream, buffer_stream)206 207 208-def _get_text_stderr():208+def _get_text_stderr(buffer_stream):209 text_stream = _NonClosingTextIOWrapper(210 _WindowsConsoleWriter(STDERR_HANDLE),211 'utf-16-le', 'strict', line_buffering=True)212- return ConsoleStream(text_stream, sys.stderr)212+ return ConsoleStream(text_stream, buffer_stream)213 214 215 def _get_windows_argv():216 argc = c_int(0)28 unmodified lines245 and errors in ('strict', None) and \246 hasattr(f, 'isatty') and f.isatty():247 func = _stream_factories.get(f.fileno())248 if func is not None:249+ if not PY2:250+ f = getattr(f, 'buffer')251+ if f is None:252+ return None249- return func()253+ return func(f)modified:CHANGES+4 added-4 removed
27 unmodified lines28 unicode by default in the `Path` type which means that this can29 introduce small bugs for code not aware of this.30 - Added a `type` parameter to `Path` to force a specific string type31 on the value.32-- For users running Python 2 the echo() and prompt() functions now work33- with unicode in the Python windows console. For Python 3 the work34- is delegated to the Python interpreter as general support for Unicode35- in the console is implemented to some degree.32+- For users running Python on Windows the `echo`) and `prompt` functions33+ now work with full unicode functionality in the Python windows console34+ by emulating an output stream. This also applies to getting the35+ virtual output and input streams via `click.get_text_stream(...)`.36 - Unittests now always force a certain virtual terminal width.37 38 Version 5.139 -----------252 unmodified lines