Idiomatic way of handling binary data from commands?
chaserhkj opened this issue · comments
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?
Can you pipe _out
to a BytesIO buffer?
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
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.