Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace unrecognised unicode characters rather than raising an exception #607

Merged
merged 1 commit into from
Jul 10, 2024
Merged
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
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 @@ -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 == "�"