Skip to content
Myr

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

2 files+42 added−15 removed

modified:click/_compat.py+40 added-15 removed
17 unmodified lines
18def get_filesystem_encoding():19 return sys.getfilesystemencoding() or sys.getdefaultencoding()202122def _make_text_stream(stream, encoding, errors):22def _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)323334def is_ascii_encoding(encoding):35 """Checks if a given encoding is ascii."""
12 unmodified lines
484950class _NonClosingTextIOWrapper(io.TextIOWrapper):5149 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)5758 # The io module is a place where the Python 3 text behavior59 # was forced upon Python 2, so we need to unbreak
26 unmodified lines
86class _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.9091 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 """9587 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_writable100101 def __getattr__(self, name):102 return getattr(self._stream, name)103
8 unmodified lines
112 return self._stream.readline(size)113 return self._stream.read(size)114115 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 lines
124 return False125 return True126127 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 lines
228 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)234235 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)241242 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)248249 def filename_to_ui(value):250 if isinstance(value, bytes):251 value = value.decode(get_filesystem_encoding(), 'replace')
83 unmodified lines
335 return stream_encoding is not None336337 return False338321 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 the
17 unmodified lines
362 # 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)368349 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 the
17 unmodified lines
392 # 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)398399 def get_binary_stdin():400 reader = _find_binary_reader(sys.stdin)401 if reader is None:
18 unmodified lines
420 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)426427 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)433434 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)440441 def filename_to_ui(value):442 if isinstance(value, bytes):443 value = value.decode(get_filesystem_encoding(), 'replace')
250 unmodified lines
modified:CHANGES+2 added
35 unmodified lines
36 `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.4243Version 6.844-----------45
357 unmodified lines