amoffat / sh

Python process launching
https://sh.readthedocs.io/en/latest/
MIT License
6.98k stars 506 forks source link

Idiomatic way of handling binary data from commands? #724

Closed chaserhkj closed 5 months ago

chaserhkj commented 5 months ago

Since 2.0.0, sh now returns a str on any executed command.

I wonder what is the idiomatic way of handling binary data from commands if binary is expected?

The closest I can think up is sh.command(*args, _decode_errors="surrogateescape").encode(errors="surrogateescape"), but that is quite a bit of boilerplate to add for me.

Shouldn't we add some flags like _stdout_bytes=True to make it just return bytes?

amoffat commented 5 months ago

Can you pipe _out to a BytesIO buffer?

chaserhkj commented 5 months ago

Yes, that is one way to do it, but still feel a little bit of extra since that would require importing another module as well. I'm just suggesting maybe the addition of a flag to handle this, but this is ultimately up to your preference and taste. I would probably be happy with BytesIO

ecederstrand commented 5 months ago

I think using BytesIO is perfectly idiomatic, short and readable:

from io import BytesIO
from sh import cat

out = BytesIO()
# tmp.out created as: dd if=/dev/random of=tmp.out count=100
cat("tmp.out", _out=out)
out.seek(0)
out.read()
# b'\xa2\x99\x1f4\xb8l\xdf\xbb[...]'

I don't think adding a _stdout_bytes to handle this is necessary.