basak / glacier-cli

Command-line interface to Amazon Glacier
Other
617 stars 55 forks source link

Issue #74: read stdin in binary mode (archive_upload) with Python 3 #77

Closed soxofaan closed 4 years ago

soxofaan commented 4 years ago

An alternative solution for #74 that does not require version sniffing like #75

soxofaan commented 4 years ago

oh wait, this will probably not work under python 2. So version sniffing will be necessary unfortunately

soxofaan commented 4 years ago

I force-pushed a reworked version that re-introduces the version sniffing to make sure things keep working under python 2.

Note that under python 2, even though the object looks like <open file '<stdin>', mode 'r' at 0x10dd440c0>, the data read from it return as str but that is equivalent with bytes from Python 3.

soxofaan commented 4 years ago

quick experiment about python 2 and 3 handling binary data on stdin:

$ head -c 10 /bin/ls | python2 -c "import sys; x=sys.stdin.read(); print type(x), repr(x)"
<type 'str'> '\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00'

$ head -c 10 /bin/ls | python3 -c "import sys; x=sys.stdin.buffer.read(); print(type(x), repr(x))"
<class 'bytes'> b'\xcf\xfa\xed\xfe\x07\x00\x00\x01\x03\x00'
basak commented 4 years ago

Thank you. I appreciate the investigation and comment referencing the upstream bug, and the explicit check against file_obj.mode. For this reason I prefer this over #75, but thank you also to @cnjr2 for finding the original workaround.