Skip to content
Myr

Handle empty bytes in echo (#3493)

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

authored by David Lord · committed by GitHub · parents d42f15b, 4d3db84 · 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

3 files+15 added−8 removed

modified:src/click/utils.py+8 added-8 removed
230 unmodified lines
231 return iter(self._file)232233234def 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 lines
286 # pythonw on Windows.287 if file is None:288 return289290 # 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)297298 if nl:297 out = out or ""299 if isinstance(out, str):300 out += "\n"301 else:302 out += b"\n"
7 unmodified lines
310 # 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)313314 if binary_file is not None:315 file.flush()316 binary_file.write(out)317 binary_file.flush()
324 unmodified lines
modified:tests/test_utils.py+5 added
6 unmodified lines
7from contextlib import nullcontext8from decimal import Decimal9from fractions import Fraction10from functools import partial11from io import BytesIO12from io import StringIO13from unittest.mock import patch1415import pytest
81 unmodified lines
97 f = StringIO()98 click.echo("hello", file=f)99 assert f.getvalue() == "hello\n"100101 b = BytesIO()102 click.echo(b"", b)103 assert b.getvalue() == b"\n"104105106def 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 = None
683 unmodified lines
modified:CHANGES.rst+2 added
8 unmodified lines
9 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`141516Version 8.4.017-------------1819Released 2026-05-17
1461 unmodified lines