Skip to content
Myr

Ensure we do not strip color codes for bytes.

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 e539622 · Browse files · 3 files+39 added−18 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+39 added−18 removed

  • tests:+5 added−0 removed
  • other:+34 added−18 removed

3 files+39 added−18 removed

modified:click/_compat.py+8 added-2 removed
47 unmodified lines
48 # python 3 text behavior forced on, so we need to unbreak49 # it to look like python 2 stuff.50 if PY2:51 def write(self, x):52 if isinstance(x, str):52 if isinstance(x, str) or is_bytes(x):53 self.flush()54 return self.buffer.write(x)54 return self.buffer.write(str(x))55 return io.TextIOWrapper.write(self, x)5657 def writelines(self, lines):58 for line in lines:
65 unmodified lines
124 string_types = (str, unicode)125 iteritems = lambda x: x.iteritems()126 range_type = xrange127128 def is_bytes(x):129 return isinstance(x, (buffer, bytearray))130131 _identifier_re = re.compile(r'^[a-zA-Z_][a-zA-Z0-9_]*$')132133 # For Windows we need to force stdout/stdin/stderr to binary if it's134 # fetched for that. This obviously is not the most correct way to do
51 unmodified lines
186 range_type = range187 isidentifier = lambda x: x.isidentifier()188 iteritems = lambda x: iter(x.items())189190 def is_bytes(x):191 return isinstance(x, (bytes, memoryview, bytearray))192193 def _is_binary_reader(stream, default=False):194 try:195 return isinstance(stream.read(0), bytes)196 except Exception:
316 unmodified lines
modified:click/utils.py+26 added-16 removed
2 unmodified lines
3from collections import deque45from ._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, isatty, _default_text_stdout7 auto_wrap_for_ansi, strip_ansi, isatty, _default_text_stdout, \8 is_bytes910if not PY2:11 from ._compat import _find_binary_writer12
211 unmodified lines
224 """225 if file is None:226 file = _default_text_stdout()227228 # Convert non bytes/text into the native string type.229 if message is not None and not isinstance(message, echo_native_types):230 message = text_type(message)231232 # If there is a message, and we're on python 3, and the value looks233 # like bytes we manually need to find the binary stream and write the234 # message in there. This is done separately so that most stream235 # types will work as you would expect. Eg: you can write to StringIO230 if message:231 if not PY2 and isinstance(message, (bytes, bytearray)):232 binary_file = _find_binary_writer(file)233 if binary_file is not None:234 file.flush()235 binary_file.write(message)236 if nl:237 binary_file.write(b'\n')238 binary_file.flush()239 return236 # for other cases.237 if message and not PY2 and is_bytes(message):238 binary_file = _find_binary_writer(file)239 if binary_file is not None:240 file.flush()241 binary_file.write(message)242 if nl:243 binary_file.write(b'\n')244 binary_file.flush()245 return246247 # If we have colorama support we wrap the stream to handle colors248 # for us. In case colorama is not supported and our output stream249 # is not a terminal, we strip the ansi codes ourselves. We also250 # do not do the stripping if we're dealing with true bytes (this will251 # only ever be reached on python 2 where is_bytes is true for buffers252 # and bytearrays but not for regular strings).243 # is not a terminal, we strip the ansi codes ourselves.244 if auto_wrap_for_ansi is not None:245 file = auto_wrap_for_ansi(file)246 elif message and not isatty(file):247 message = strip_ansi(message)253 if message and not is_bytes(message):254 if auto_wrap_for_ansi is not None:255 file = auto_wrap_for_ansi(file)256 elif not isatty(file):257 message = strip_ansi(message)258259 if message:260 file.write(message)261 if nl:
103 unmodified lines
modified:tests/test_utils.py+5 added
28 unmodified lines
29 click.echo(b'\xf6')30 result = runner.invoke(cli, [])31 assert result.output_bytes == b'\xf6\n'3233 # Ensure we do not strip for bytes.34 with runner.isolation() as out:35 click.echo(bytearray(b'\x1b[31mx\x1b[39m'), nl=False)36 assert out.getvalue() == '\x1b[31mx\x1b[39m'373839def test_filename_formatting():40 assert click.format_filename(b'foo.txt') == 'foo.txt'41 assert click.format_filename(b'/x/foo.txt') == '/x/foo.txt'42 assert click.format_filename(u'/x/foo.txt') == '/x/foo.txt'
3 unmodified lines