Skip to content
Myr

Made the echo function accept bytes transparently on 3.x

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 3e51448 · Browse files · 1 file+34 added−10 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+34 added−10 removed

  • other:+34 added−10 removed

1 file+34 added−10 removed

modified:click/utils.py+34 added-10 removed
2 unmodified lines
34from ._compat import text_type, open_stream, get_streerror, string_types, \5 PY2, get_best_encoding, binary_streams, text_streams67if not PY2:8 from ._compat import _find_binary_writer91011echo_native_types = string_types + (bytes, bytearray)121314def unpack_args(args, nargs_spec):15 """Given an iterable of arguments and an iterable of nargs specifications16 it returns a tuple with all the unpacked arguments at the first index17 and all remaining arguments as the second.
164 unmodified lines
182 def __exit__(self, exc_type, exc_value, tb):183 self.close_intelligently()184185180def echo(message=None, file=None):186def echo(message=None, file=None, nl=True):187 """Prints a message plus a newline to the given file or stdout. On188 first sight this looks like the print function but it has improved183 support for handling unicode data that does not fail no matter how184 badly configured the system is.189 support for handling unicode and binary data that does not fail no190 matter how badly configured the system is.191192 Primarily it means that you can print binary data as well as unicode193 data on both 2.x and 3.x to the given file in the most appropriate way194 possible. This is a very carefree function as in that it will try its195 best to not fail.196197 :param message: the message to print198 :param file: the file to write to (defaults to ``stdout``)199 :param nl: if set to `True` (the default) a newline is printed afterwards.200 """201 if file is None:202 file = sys.stdout191 if message is not None:192 if not isinstance(message, string_types):193 message = text_type(message)194 if message:195 if PY2 and isinstance(message, text_type):203204 if message is not None and not isinstance(message, echo_native_types):205 message = text_type(message)206207 if message:208 if PY2:209 if isinstance(message, text_type):210 encoding = get_best_encoding(file)211 message = message.encode(encoding, 'replace')198 file.write(message)199 file.write('\n')212 elif isinstance(message, (bytes, bytearray)):213 binary_file = _find_binary_writer(file)214 if binary_file is not None:215 file.flush()216 binary_file.write(message)217 if nl:218 binary_file.write(b'\n')219 binary_file.flush()220 return221 file.write(message)222 if nl:223 file.write('\n')224 file.flush()225226227def get_binary_stream(name):
30 unmodified lines