Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/subprocess_tee/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ async def _stream_subprocess( # noqa: C901
err: list[str] = []

def tee_func(line: bytes, sink: list[str], pipe: Any | None) -> None:
line_str = line.decode("utf-8").rstrip()
line_str = line.decode("utf-8", errors="replace").rstrip()
sink.append(line_str)
if not kwargs.get("quiet"):
if pipe and hasattr(pipe, "write"):
Expand Down
43 changes: 43 additions & 0 deletions test/test_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,3 +174,46 @@ def test_run_exc_no_args() -> None:
subprocess.run(check=False) # type: ignore[call-overload]
with pytest.raises(TypeError, match=expected):
subprocess_tee.run()


@pytest.mark.parametrize(
("quiet", "tee"), [(False, True), (True, True), (False, False)]
)
def test_run_invalid_utf8(
capsys: pytest.CaptureFixture[str], *, quiet: bool, tee: bool
) -> None:
"""Keep reading both streams after replacing undecodable output bytes."""
cmd = [
sys.executable,
"-c",
"import os; "
"os.write(1, b'out\\xff' b'put\\xe2\\x98\\x83\\nnext out\\n'); "
"os.write(2, b'err\\xc0\\x80or\\nnext err\\n')",
]
result = run(cmd, quiet=quiet, tee=tee)
assert result.args == cmd
assert result.returncode == 0
assert result.stdout == "out�put☃\nnext out\n"
assert result.stderr == "err��or\nnext err\n"
captured = capsys.readouterr()
if quiet or not tee:
assert not captured.out
assert not captured.err
else:
assert captured.out == result.stdout
assert captured.err == result.stderr


def test_run_invalid_utf8_with_check() -> None:
"""Preserve captured output on a failed child when check=True."""
cmd = [
sys.executable,
"-c",
"import os; os.write(1, b'out\\xff\\n'); "
"os.write(2, b'err\\xff\\n'); raise SystemExit(7)",
]
with pytest.raises(subprocess.CalledProcessError) as error:
run(cmd, check=True, quiet=True)
assert error.value.returncode == 7
assert error.value.stdout == "out�\n"
assert error.value.stderr == "err�\n"
2 changes: 1 addition & 1 deletion tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ set_env =
PIP_CONSTRAINT = {tox_root}/.config/constraints.txt
PIP_DISABLE_PIP_VERSION_CHECK = 1
PRE_COMMIT_COLOR = always
PYTEST_REQPASS = 18
PYTEST_REQPASS = 22
PYTHONDONTWRITEBYTECODE = 1
PYTHONUNBUFFERED = 1
UV_CONSTRAINT = {tox_root}/.config/constraints.txt
Expand Down
Loading