From 13d427476338f11983110f846b07db117916a207 Mon Sep 17 00:00:00 2001 From: Sinhal Joshi Date: Tue, 24 Nov 2026 16:57:28 +0530 Subject: [PATCH 1/2] FIX: Use UTF-8 for redirected CLI input and output The CLI reads files as UTF-8, but Python uses the locale encoding (e.g. cp1252 on Windows) for standard streams that are not a terminal. So `markdown-it doc.md > doc.html` raised UnicodeEncodeError for characters outside it, and `--stdin` decoded UTF-8 input as cp1252. When converting files or stdin, redirected stdout now writes UTF-8 and redirected stdin is read as UTF-8, ignoring undecodable bytes like files. PYTHONIOENCODING is kept and a terminal is left as is. Co-Authored-By: Claude Opus 5.5 --- markdown_it/cli/parse.py | 25 ++++++++++++++++++++++ tests/test_cli.py | 45 ++++++++++++++++++++++++++++++++++++++++ 2 files changed, 70 insertions(+) diff --git a/markdown_it/cli/parse.py b/markdown_it/cli/parse.py index 9c1f2fb3..a8aeb0cc 100644 --- a/markdown_it/cli/parse.py +++ b/markdown_it/cli/parse.py @@ -8,7 +8,10 @@ import argparse from collections.abc import Iterable, Sequence +import io +import os import sys +from typing import TextIO from markdown_it import __version__ from markdown_it.main import MarkdownIt @@ -27,14 +30,36 @@ def main(args: Sequence[str] | None = None) -> int: if namespace.enable_tables: md.enable("table") if namespace.filenames: + _use_utf8(sys.stdout) convert(namespace.filenames, md) elif namespace.stdin: + # Read standard input like a file: UTF-8, ignoring undecodable bytes. + _use_utf8(sys.stdin, errors="ignore") + _use_utf8(sys.stdout) convert_stdin(md) else: interactive(md) return 0 +def _use_utf8(stream: TextIO, errors: str | None = None) -> None: + """Use UTF-8 for a redirected or piped standard stream. + + Files are read as UTF-8, but Python uses the locale encoding (e.g. cp1252 on + Windows) for standard streams that are not a terminal, so + ``markdown-it doc.md > doc.html`` raised UnicodeEncodeError for characters + outside it, and ``--stdin`` misread UTF-8 input. + An encoding set with PYTHONIOENCODING is kept, and a terminal is left as is. + """ + if ( + os.environ.get("PYTHONIOENCODING") + or not isinstance(stream, io.TextIOWrapper) + or stream.isatty() + ): + return + stream.reconfigure(encoding="utf-8", errors=errors or stream.errors) + + def convert(filenames: Iterable[str], md: MarkdownIt | None = None) -> None: for filename in filenames: convert_file(filename, md) diff --git a/tests/test_cli.py b/tests/test_cli.py index 450c862f..89656a94 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -104,6 +104,51 @@ def test_stdin(): assert string_io.getvalue() == "

a b c

\n" +def _redirected(encoding: str, data: bytes = b"") -> io.TextIOWrapper: + """A standard stream redirected to a file or pipe, e.g. cp1252 on Windows.""" + return io.TextIOWrapper( + io.BytesIO(data), encoding=encoding, newline="\n", write_through=True + ) + + +def test_redirected_stdout_gets_utf8(tmp_path, monkeypatch): + monkeypatch.delenv("PYTHONIOENCODING", raising=False) + path = tmp_path / "test.md" + path.write_text("# ʤ ✓", encoding="utf8") + stdout = _redirected("cp1252") + monkeypatch.setattr(sys, "stdout", stdout) + assert parse.main([str(path)]) == 0 + assert stdout.buffer.getvalue().decode("utf8") == "

ʤ ✓

\n" + + +def test_redirected_stdin_is_read_as_utf8(monkeypatch): + monkeypatch.delenv("PYTHONIOENCODING", raising=False) + monkeypatch.setattr(sys, "stdin", _redirected("cp1252", "# Łódź".encode())) + stdout = _redirected("cp1252") + monkeypatch.setattr(sys, "stdout", stdout) + assert parse.main(["--stdin"]) == 0 + assert stdout.buffer.getvalue().decode("utf8") == "

Łódź

\n" + + +def test_redirected_stdin_ignores_undecodable_bytes(monkeypatch): + monkeypatch.delenv("PYTHONIOENCODING", raising=False) + monkeypatch.setattr(sys, "stdin", _redirected("cp1252", b"\x80abc")) + stdout = _redirected("cp1252") + monkeypatch.setattr(sys, "stdout", stdout) + assert parse.main(["--stdin"]) == 0 + assert stdout.buffer.getvalue() == b"

abc

\n" + + +def test_redirected_stdout_keeps_pythonioencoding(tmp_path, monkeypatch): + monkeypatch.setenv("PYTHONIOENCODING", "utf-16") + path = tmp_path / "test.md" + path.write_text("# a", encoding="utf8") + stdout = _redirected("utf-16") + monkeypatch.setattr(sys, "stdout", stdout) + assert parse.main([str(path)]) == 0 + assert stdout.buffer.getvalue().decode("utf-16") == "

a

\n" + + def test_multiple_files(): with tempfile.TemporaryDirectory() as tempdir: path1 = pathlib.Path(tempdir).joinpath("test1.md") From d397dc8bc8a0c5a9f3f56e40873bb8eec9a5f71d Mon Sep 17 00:00:00 2001 From: Sinhal Joshi Date: Fri, 27 Nov 2026 13:11:42 +0530 Subject: [PATCH 2/2] TEST: Read redirected output through a typed helper mypy types `TextIOWrapper.buffer` without `getvalue`; narrow it to the `BytesIO` the tests create. Co-Authored-By: Claude Opus 5.5 --- tests/test_cli.py | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tests/test_cli.py b/tests/test_cli.py index 89656a94..45175859 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -111,6 +111,13 @@ def _redirected(encoding: str, data: bytes = b"") -> io.TextIOWrapper: ) +def _written(stream: io.TextIOWrapper) -> bytes: + """The bytes written to a stream made by `_redirected`.""" + buffer = stream.buffer + assert isinstance(buffer, io.BytesIO) + return buffer.getvalue() + + def test_redirected_stdout_gets_utf8(tmp_path, monkeypatch): monkeypatch.delenv("PYTHONIOENCODING", raising=False) path = tmp_path / "test.md" @@ -118,7 +125,7 @@ def test_redirected_stdout_gets_utf8(tmp_path, monkeypatch): stdout = _redirected("cp1252") monkeypatch.setattr(sys, "stdout", stdout) assert parse.main([str(path)]) == 0 - assert stdout.buffer.getvalue().decode("utf8") == "

ʤ ✓

\n" + assert _written(stdout).decode("utf8") == "

ʤ ✓

\n" def test_redirected_stdin_is_read_as_utf8(monkeypatch): @@ -127,7 +134,7 @@ def test_redirected_stdin_is_read_as_utf8(monkeypatch): stdout = _redirected("cp1252") monkeypatch.setattr(sys, "stdout", stdout) assert parse.main(["--stdin"]) == 0 - assert stdout.buffer.getvalue().decode("utf8") == "

Łódź

\n" + assert _written(stdout).decode("utf8") == "

Łódź

\n" def test_redirected_stdin_ignores_undecodable_bytes(monkeypatch): @@ -136,7 +143,7 @@ def test_redirected_stdin_ignores_undecodable_bytes(monkeypatch): stdout = _redirected("cp1252") monkeypatch.setattr(sys, "stdout", stdout) assert parse.main(["--stdin"]) == 0 - assert stdout.buffer.getvalue() == b"

abc

\n" + assert _written(stdout) == b"

abc

\n" def test_redirected_stdout_keeps_pythonioencoding(tmp_path, monkeypatch): @@ -146,7 +153,7 @@ def test_redirected_stdout_keeps_pythonioencoding(tmp_path, monkeypatch): stdout = _redirected("utf-16") monkeypatch.setattr(sys, "stdout", stdout) assert parse.main([str(path)]) == 0 - assert stdout.buffer.getvalue().decode("utf-16") == "

a

\n" + assert _written(stdout).decode("utf-16") == "

a

\n" def test_multiple_files():