gabrieldemarmiesse / python-on-whales

An awesome Python wrapper for an awesome Docker CLI!
MIT License
537 stars 100 forks source link

UnicodeDecodeError getting container output #606

Closed LewisGaul closed 1 month ago

LewisGaul commented 1 month ago

This is seen when calling DockerClient.container.run() or DockerClient.container.logs() if the container console contains output that is not UTF-8 encoded. This can be reproduced by outputting a character in another encoding as shown below.

Related to https://github.com/gabrieldemarmiesse/python-on-whales/issues/370. Perhaps DockerClient.container.logs() should take an encoding parameter rather than assuming utf-8.

>>> import python_on_whales as pow
>>> podman = pow.DockerClient(client_call=["podman"])
>>> bash_cmd = r"echo -n $'\xfe'"
>>> full_cmd = ["bash", "-c", bash_cmd]
>>> ctr = podman.run("ubuntu", full_cmd)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File ".../python_on_whales/components/container/cli_wrapper.py", line 1764, in run
    return run(full_cmd, tty=tty, capture_stderr=False, pass_fds=pass_fds)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python_on_whales/utils.py", line 215, in run
    return post_process_stream(completed_process.stdout)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File ".../python_on_whales/utils.py", line 221, in post_process_stream
    stream = stream.decode()
             ^^^^^^^^^^^^^^^
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xfe in position 0: invalid start byte