From ac6c4d98a8de540105d9906a8bb57c7780953509 Mon Sep 17 00:00:00 2001 From: Lewis Gaul Date: Wed, 10 Jul 2024 19:44:42 +0100 Subject: [PATCH] Replace unrecognised unicode characters rather than raising an exception --- python_on_whales/utils.py | 4 ++-- tests/python_on_whales/components/test_container.py | 13 +++++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/python_on_whales/utils.py b/python_on_whales/utils.py index e74585e6..50372526 100644 --- a/python_on_whales/utils.py +++ b/python_on_whales/utils.py @@ -237,10 +237,10 @@ def run( return post_process_stream(completed_process.stdout) -def post_process_stream(stream: Optional[bytes]): +def post_process_stream(stream: Optional[bytes]) -> str: if stream is None: return "" - stream = stream.decode() + stream = stream.decode(errors="replace") if len(stream) != 0 and stream[-1] == "\n": stream = stream[:-1] return stream diff --git a/tests/python_on_whales/components/test_container.py b/tests/python_on_whales/components/test_container.py index ef7978cb..1f16f4fb 100644 --- a/tests/python_on_whales/components/test_container.py +++ b/tests/python_on_whales/components/test_container.py @@ -1437,3 +1437,16 @@ def test_run_always_pull_existent( assert remote_id != local_id docker_client.container.run(test_image_name, pull="always") assert docker_client.image.inspect(test_image_name).id == remote_id + + +@pytest.mark.parametrize("ctr_client", ["docker", "podman"], indirect=True) +def test_non_unicode_output(ctr_client: DockerClient): + """Non-unicode characters in container output should not lead to an exception.""" + latin_char = "þ".encode("latin") + with pytest.raises(UnicodeDecodeError): + latin_char.decode(encoding="utf-8") + byte_repr = r"\x{:x}".format(ord(latin_char)) + output = ctr_client.container.run( + "ubuntu", ["bash", "-c", f"echo -n $'{byte_repr}'"], remove=True + ) + assert output == "�"