Skip to content

Commit

Permalink
Replace unrecognised unicode characters rather than raising an except…
Browse files Browse the repository at this point in the history
…ion (#607)
  • Loading branch information
LewisGaul authored Jul 10, 2024
1 parent b8fe6ab commit 773725c
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
4 changes: 2 additions & 2 deletions python_on_whales/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions tests/python_on_whales/components/test_container.py
Original file line number Diff line number Diff line change
Expand Up @@ -1438,3 +1438,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 == "�"

0 comments on commit 773725c

Please sign in to comment.