Fix crash if sys.getfilesystemencoding() is None
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Fix #355
authored by Markus Unterwaditzer · committed by Markus Unterwaditzer · parent 60e4ea3 · Browse files · 4 files·+15 added−9 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+15 added−9 removed
- other:+15 added−9 removed
Provenance
- Commit
be2c42b- Tree
74d3cf4- Author
Markus Unterwaditzer <markus@unterwaditzer.net>- Committer
Markus Unterwaditzer <markus@unterwaditzer.net>- Parent
60e4ea3
4 files changed+15 added−9 removed
modified:click/_compat.py+6 added-2 removed
12 unmodified lines13 14 _ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')15 16 17+def get_filesystem_encoding():18+ return sys.getfilesystemencoding() or sys.getdefaultencoding()19+20+21 def _make_text_stream(stream, encoding, errors):22 if encoding is None:23 encoding = get_best_encoding(stream)24 if errors is None:167 unmodified lines192 return _make_text_stream(sys.stderr, encoding, errors)193 194 def filename_to_ui(value):195 if isinstance(value, bytes):192- value = value.decode(sys.getfilesystemencoding(), 'replace')196+ value = value.decode(get_filesystem_encoding(), 'replace')197 return value198 else:199 import io200 text_type = str166 unmodified lines367 return _force_correct_text_writer(sys.stderr, encoding, errors)368 369 def filename_to_ui(value):370 if isinstance(value, bytes):367- value = value.decode(sys.getfilesystemencoding(), 'replace')371+ value = value.decode(get_filesystem_encoding(), 'replace')372 else:373 value = value.encode('utf-8', 'surrogateescape') \374 .decode('utf-8', 'replace')375 return value201 unmodified linesmodified:click/types.py+3 added-2 removed
1 import os2 import sys3 import stat4 5-from ._compat import open_stream, text_type, filename_to_ui, get_streerror5+from ._compat import open_stream, text_type, filename_to_ui, \6+ get_filesystem_encoding, get_streerror7 from .exceptions import BadParameter8 from .utils import safecall, LazyFile9 10 103 unmodified lines114 if enc is not None:115 value = value.decode(enc)116 except UnicodeError:117 try:117- value = value.decode(sys.getfilesystemencoding())118+ value = value.decode(get_filesystem_encoding())119 except UnicodeError:120 value = value.decode('utf-8', 'replace')121 return value122 return value393 unmodified linesmodified:click/utils.py+5 added-5 removed
1 import os2 import sys3 from collections import deque4 5-from ._compat import text_type, open_stream, get_streerror, string_types, \6- PY2, binary_streams, text_streams, filename_to_ui, \7- auto_wrap_for_ansi, strip_ansi, should_strip_ansi, \8- _default_text_stdout, _default_text_stderr, is_bytes, WIN5+from ._compat import text_type, open_stream, get_filesystem_encoding, \6+ get_streerror, string_types, PY2, binary_streams, text_streams, \7+ filename_to_ui, auto_wrap_for_ansi, strip_ansi, should_strip_ansi, \8+ _default_text_stdout, _default_text_stderr, is_bytes, WIN9 10 if not PY2:11 from ._compat import _find_binary_writer12 76 unmodified lines89 def make_str(value):90 """Converts a value into a valid string."""91 if isinstance(value, bytes):92 try:93- return value.decode(sys.getfilesystemencoding())93+ return value.decode(get_filesystem_encoding())94 except UnicodeError:95 return value.decode('utf-8', 'replace')96 return text_type(value)97 333 unmodified linesmodified:CHANGES+1 added
11 unmodified lines12 - Fix a bug where Click would crash on docstrings with trailing newlines.13 - Support streams with encoding set to `None` on Python 3 by barfing with14 a better error.15 - Handle ^C in less-pager properly.16+- Handle return value of `None` from `sys.getfilesystemencoding`17 18 Version 4.019 -----------20 207 unmodified lines