docker-py icon indicating copy to clipboard operation
docker-py copied to clipboard

`exec_run` with `demux=True`: output is `None` instead of empty `bytes`

Open just-max opened this issue 1 year ago • 1 comments

When calling exec_run with demux=True, the documentation states that a pair of bytes is returned.

However, if no standard output (or no standard error) is produced, then the corresponding tuple entry is None instead of an empty bytes (b'').

Minimum working example:

import docker
client = docker.from_env()
container = client.containers.run("alpine:latest", detach=True, tty=True)
container.exec_run(("sh", "-c", "echo out >&1"), demux=True)  # ExecResult(exit_code=0, output=(b'out\n', None))
container.exec_run(("sh", "-c", "echo err >&2"), demux=True)  # ExecResult(exit_code=0, output=(None, b'err\n'))

The expected behaviour is that both tuple entries are always bytes. Alternatively, the possibility of returning None should be documented clearly!

just-max avatar Sep 02 '23 11:09 just-max

Yes, I checked that in documentation here and it seems very easily solvable as below:

+ exec_output = tuple(b'' if x is None else x for x in exec_output)

Khushiyant avatar Dec 17 '23 15:12 Khushiyant