Added workaround for jupyter. Fixes #918
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 3480d2c · Browse files · 2 files·+42 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
2 files+42 added−15 removed
- other:+42 added−15 removed
Provenance
- Commit
570db30- Tree
c0b51ac- Author
Armin Ronacher <armin.ronacher@active-4.com>- Committer
Armin Ronacher <armin.ronacher@active-4.com>- Parent
3480d2c
2 files changed+42 added−15 removed
modified:click/_compat.py+40 added-15 removed
17 unmodified lines18 def get_filesystem_encoding():19 return sys.getfilesystemencoding() or sys.getdefaultencoding()20 21 22-def _make_text_stream(stream, encoding, errors):22+def _make_text_stream(stream, encoding, errors,23+ force_readable=False, force_writable=False):24 if encoding is None:25 encoding = get_best_encoding(stream)26 if errors is None:27 errors = 'replace'28 return _NonClosingTextIOWrapper(stream, encoding, errors,28- line_buffering=True)29+ line_buffering=True,30+ force_readable=force_readable,31+ force_writable=force_writable)32 33 34 def is_ascii_encoding(encoding):35 """Checks if a given encoding is ascii."""12 unmodified lines48 49 50 class _NonClosingTextIOWrapper(io.TextIOWrapper):51 49- def __init__(self, stream, encoding, errors, **extra):50- self._stream = stream = _FixupStream(stream)52+ def __init__(self, stream, encoding, errors,53+ force_readable=False, force_writable=False, **extra):54+ self._stream = stream = _FixupStream(stream, force_readable,55+ force_writable)56 io.TextIOWrapper.__init__(self, stream, encoding, errors, **extra)57 58 # The io module is a place where the Python 3 text behavior59 # was forced upon Python 2, so we need to unbreak26 unmodified lines86 class _FixupStream(object):87 """The new io interface needs more from streams than streams88 traditionally implement. As such, this fix-up code is necessary in89 some circumstances.90+91+ The forcing of readable and writable flags are there because some tools92+ put badly patched objects on sys (one such offender are certain version93+ of jupyter notebook).94 """95 87- def __init__(self, stream):96+ def __init__(self, stream, force_readable=False, force_writable=False):97 self._stream = stream98+ self._force_readable = force_readable99+ self._force_writable = force_writable100 101 def __getattr__(self, name):102 return getattr(self._stream, name)103 8 unmodified lines112 return self._stream.readline(size)113 return self._stream.read(size)114 115 def readable(self):116+ if self._force_readable:117+ return True118 x = getattr(self._stream, 'readable', None)119 if x is not None:120 return x()121 try:2 unmodified lines124 return False125 return True126 127 def writable(self):128+ if self._force_writable:129+ return True130 x = getattr(self._stream, 'writable', None)131 if x is not None:132 return x()133 try:94 unmodified lines228 def get_text_stdin(encoding=None, errors=None):229 rv = _get_windows_console_stream(sys.stdin, encoding, errors)230 if rv is not None:231 return rv217- return _make_text_stream(sys.stdin, encoding, errors)232+ return _make_text_stream(sys.stdin, encoding, errors,233+ force_readable=True)234 235 def get_text_stdout(encoding=None, errors=None):236 rv = _get_windows_console_stream(sys.stdout, encoding, errors)237 if rv is not None:238 return rv223- return _make_text_stream(sys.stdout, encoding, errors)239+ return _make_text_stream(sys.stdout, encoding, errors,240+ force_writable=True)241 242 def get_text_stderr(encoding=None, errors=None):243 rv = _get_windows_console_stream(sys.stderr, encoding, errors)244 if rv is not None:245 return rv229- return _make_text_stream(sys.stderr, encoding, errors)246+ return _make_text_stream(sys.stderr, encoding, errors,247+ force_writable=True)248 249 def filename_to_ui(value):250 if isinstance(value, bytes):251 value = value.decode(get_filesystem_encoding(), 'replace')83 unmodified lines335 return stream_encoding is not None336 337 return False338 321- def _force_correct_text_reader(text_reader, encoding, errors):339+ def _force_correct_text_reader(text_reader, encoding, errors,340+ force_readable=False):341 if _is_binary_reader(text_reader, False):342 binary_reader = text_reader343 else:344 # If there is no target encoding set, we need to verify that the17 unmodified lines362 # because nobody handles those errors anyways and at this point363 # we're so fundamentally fucked that nothing can repair it.364 if errors is None:365 errors = 'replace'347- return _make_text_stream(binary_reader, encoding, errors)366+ return _make_text_stream(binary_reader, encoding, errors,367+ force_readable=force_readable)368 349- def _force_correct_text_writer(text_writer, encoding, errors):369+ def _force_correct_text_writer(text_writer, encoding, errors,370+ force_writable=False):371 if _is_binary_writer(text_writer, False):372 binary_writer = text_writer373 else:374 # If there is no target encoding set, we need to verify that the17 unmodified lines392 # because nobody handles those errors anyways and at this point393 # we're so fundamentally fucked that nothing can repair it.394 if errors is None:395 errors = 'replace'375- return _make_text_stream(binary_writer, encoding, errors)396+ return _make_text_stream(binary_writer, encoding, errors,397+ force_writable=force_writable)398 399 def get_binary_stdin():400 reader = _find_binary_reader(sys.stdin)401 if reader is None:18 unmodified lines420 def get_text_stdin(encoding=None, errors=None):421 rv = _get_windows_console_stream(sys.stdin, encoding, errors)422 if rv is not None:423 return rv402- return _force_correct_text_reader(sys.stdin, encoding, errors)424+ return _force_correct_text_reader(sys.stdin, encoding, errors,425+ force_readable=True)426 427 def get_text_stdout(encoding=None, errors=None):428 rv = _get_windows_console_stream(sys.stdout, encoding, errors)429 if rv is not None:430 return rv408- return _force_correct_text_writer(sys.stdout, encoding, errors)431+ return _force_correct_text_writer(sys.stdout, encoding, errors,432+ force_writable=True)433 434 def get_text_stderr(encoding=None, errors=None):435 rv = _get_windows_console_stream(sys.stderr, encoding, errors)436 if rv is not None:437 return rv414- return _force_correct_text_writer(sys.stderr, encoding, errors)438+ return _force_correct_text_writer(sys.stderr, encoding, errors,439+ force_writable=True)440 441 def filename_to_ui(value):442 if isinstance(value, bytes):443 value = value.decode(get_filesystem_encoding(), 'replace')250 unmodified linesmodified:CHANGES+2 added
35 unmodified lines36 `my_command` it becomes `my-command` in the command line interface.37 - Stdout is now automatically set to non blocking.38 - Use realpath to convert atomic file internally into its full canonical39 path so that changing working directories does not harm it.40+- Force stdout/stderr writable. This works around issues with badly patched41+ standard streams like those from jupyter.42 43 Version 6.844 -----------45 357 unmodified lines