handle empty bytes in echo
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Iain-S · committed by David Lord · parent d42f15b · Browse files · 3 files·+15 added−8 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+15 added−8 removed
- tests:+5 added−0 removed
- other:+10 added−8 removed
Provenance
- Commit
4d3db84- Tree
3e3f1d4- Author
Iain-S <25081046+Iain-S@users.noreply.github.com>- Committer
David Lord <davidism@gmail.com>- Parent
d42f15b
3 files changed+15 added−8 removed
modified:src/click/utils.py+8 added-8 removed
230 unmodified lines231 return iter(self._file)232 233 234 def echo(235- message: t.Any | None = None,235+ message: object = None,236 file: t.IO[t.Any] | None = None,237 nl: bool = True,238 err: bool = False,239 color: bool | None = None,46 unmodified lines286 # pythonw on Windows.287 if file is None:288 return289 290- # Convert non bytes/text into the native string type.291- if message is not None and not isinstance(message, (str, bytes, bytearray)):292- out: str | bytes | bytearray | None = str(message)293- else:294- out = message290+ match message:291+ case str() | bytes() | bytearray():292+ out = message293+ case None:294+ out = ""295+ case _:296+ out = str(message)297 298 if nl:297- out = out or ""299 if isinstance(out, str):300 out += "\n"301 else:302 out += b"\n"7 unmodified lines310 # This is done separately so that most stream types will work as you311 # would expect. Eg: you can write to StringIO for other cases.312 if isinstance(out, (bytes, bytearray)):313 binary_file = _find_binary_writer(file)313-314 if binary_file is not None:315 file.flush()316 binary_file.write(out)317 binary_file.flush()324 unmodified linesmodified:tests/test_utils.py+5 added
6 unmodified lines7 from contextlib import nullcontext8 from decimal import Decimal9 from fractions import Fraction10 from functools import partial11+from io import BytesIO12 from io import StringIO13 from unittest.mock import patch14 15 import pytest81 unmodified lines97 f = StringIO()98 click.echo("hello", file=f)99 assert f.getvalue() == "hello\n"100 101+ b = BytesIO()102+ click.echo(b"", b)103+ assert b.getvalue() == b"\n"104 105+106 def test_echo_no_streams(monkeypatch, runner):107 """echo should not fail when stdout and stderr are None with pythonw on Windows."""108 with runner.isolation():109 sys.stdout = None683 unmodified linesmodified:CHANGES.rst+2 added
8 unmodified lines9 conversion again. :issue:`3458` :issue:`3484`10 - Zsh completion scripts parse correctly on Windows. :issue:`3277`11 - Shell completion of `Choice` `Enum` values produces a valid completion12 result. :issue:`3015`13+- Fix empty byte-string handling in echo. :issue:`3487`14 15+16 Version 8.4.017 -------------18 19 Released 2026-05-171461 unmodified lines