A reactive notebook for Python — run reproducible experiments, query with SQL, execute as a script, deploy as an app, and version with git. Stored as pure Python. All in a modern, AI-native editor.
Python artificial-intelligencedagdata-sciencedata-visualizationdataflowdeveloper-toolsmachine-learningnotebookspipelinepythonreactivesqlweb-app
fix(cli): handle any non-encodable character in echo fallback (#11079)
Dhruv Gupta516b34fd73dc
diff --git a/marimo/_cli/print.py b/marimo/_cli/print.pyindex 1421cfb97e..56ef7d3ab3 100644--- a/marimo/_cli/print.py+++ b/marimo/_cli/print.py@@ -163,6 +163,25 @@ def _echo_or_print(*args: Any, **kwargs: Any) -> None:print(*args, **kwargs) # noqa: T201+def _target_encoding(kwargs: dict[str, Any]) -> str:+ file = kwargs.get("file")+ if file is None:+ file = sys.stderr if kwargs.get("err") else sys.stdout+ return getattr(file, "encoding", None) or "ascii"+++def _make_encodable(text: str, encoding: str) -> str:+ """Make `text` writable to a stream that uses `encoding`.++ Common Unicode characters are replaced with ASCII equivalents; anything+ else the stream cannot encode is backslash-escaped rather than raising.+ """+ text = text.replace("→", "->").replace("←", "<-")+ text = text.replace("✓", "v").replace("✗", "x")+ text = text.replace("•", "*").replace("…", "...")+ return text.encode(encoding, errors="backslashreplace").decode(encoding)++def echo(*args: Any, **kwargs: Any) -> None:if GLOBAL_SETTINGS.QUIET:return@@ -170,16 +189,13 @@ def echo(*args: Any, **kwargs: Any) -> None:try:_echo_or_print(*args, **kwargs)except UnicodeEncodeError:- # Handle non-UTF-8 terminals (such as CP-1252, Windows) by replacing- # common Unicode characters with ASCII equivalents for non-UTF-8- # terminals.- ascii_args = []- for arg in args:- if isinstance(arg, str):- ascii_arg = arg.replace("→", "->").replace("←", "<-")- ascii_arg = ascii_arg.replace("✓", "v").replace("✗", "x")- ascii_arg = ascii_arg.replace("•", "*").replace("…", "...")- ascii_args.append(ascii_arg)- else:- ascii_args.append(arg)+ # Handle non-UTF-8 terminals (such as CP-1252 on Windows). The text+ # may contain arbitrary user content (e.g. a converted notebook), so+ # every character the stream cannot encode must be handled, not just+ # a known set.+ encoding = _target_encoding(kwargs)+ ascii_args = [+ _make_encodable(arg, encoding) if isinstance(arg, str) else arg+ for arg in args+ ]_echo_or_print(*ascii_args, **kwargs)diff --git a/tests/_cli/test_cli_print.py b/tests/_cli/test_cli_print.pynew file mode 100644index 0000000000..fc87f7841b--- /dev/null+++ b/tests/_cli/test_cli_print.py@@ -0,0 +1,53 @@+# Copyright 2026 Marimo. All rights reserved.+from __future__ import annotations++import io+from contextlib import redirect_stderr, redirect_stdout++from marimo._cli.print import echo+++def _stream(encoding: str) -> io.TextIOWrapper:+ return io.TextIOWrapper(io.BytesIO(), encoding=encoding)+++def _read(stream: io.TextIOWrapper) -> str:+ stream.flush()+ raw: bytes = stream.buffer.getvalue() # type: ignore[attr-defined]+ return raw.decode(stream.encoding, errors="replace")+++def test_echo_utf8_stream_is_unchanged() -> None:+ stream = _stream("utf-8")+ with redirect_stdout(stream):+ echo("✓ done → (−2,4%)")+ assert _read(stream) == "✓ done → (−2,4%)\n"+++def test_echo_replaces_known_characters_on_cp1252() -> None:+ stream = _stream("cp1252")+ with redirect_stdout(stream):+ echo("✓ done → next • item…")+ assert _read(stream) == "v done -> next * item...\n"+++def test_echo_does_not_crash_on_arbitrary_characters() -> None:+ # Regression test: a converted notebook containing U+2212 (minus sign)+ # used to raise UnicodeEncodeError from inside the fallback itself.+ stream = _stream("cp1252")+ with redirect_stdout(stream):+ echo("IC -2,3 a -1,4; (−2,4%) → ok")+ assert _read(stream) == "IC -2,3 a -1,4; (\\u22122,4%) -> ok\n"+++def test_echo_fallback_uses_explicit_file_encoding() -> None:+ stream = _stream("ascii")+ echo("café ✓", file=stream)+ assert _read(stream) == "caf\\xe9 v\n"+++def test_echo_fallback_uses_stderr_encoding() -> None:+ stream = _stream("cp1252")+ with redirect_stderr(stream):+ echo("− err", err=True)+ assert _read(stream) == "\\u2212 err\n"