sh icon indicating copy to clipboard operation
sh copied to clipboard

Idiomatic way of handling binary data from commands?

Open chaserhkj opened this issue 1 year ago • 2 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?

chaserhkj avatar May 10 '24 21:05 chaserhkj

Can you pipe _out to a BytesIO buffer?

amoffat avatar May 11 '24 21:05 amoffat

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

chaserhkj avatar May 12 '24 18:05 chaserhkj

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.

ecederstrand avatar May 13 '24 11:05 ecederstrand