While writing py8chan, I discovered that the file_md5 property in the Post class does not convert the base64 string correctly in python3. This is because Python3 treats all string literals as unicode, but decode only works on ASCII.
Thus, instead of using .encode() or .decode(), use the included Python libraries base64 and binascii, which abstract away the issues, and make our code work on both Python 2 and 3. More info at StackOverflow.
from base64 import b64decode
from binascii import hexlify
@property
def file_md5(self):
# Py 2/3 compatible equivalent of:
#return self._data['md5'].decode('base64')
# More info: http://stackoverflow.com/a/16033232
# returns a bytestring
return b64decode('NOetrLVnES3jUn1x5ZPVAg==')
@property
def file_md5_hex(self):
return hexlify(self.file_md5).decode('ascii')
Pull request coming soon, but here's a public notice.
While writing py8chan, I discovered that the
file_md5
property in thePost
class does not convert the base64 string correctly in python3. This is because Python3 treats all string literals as unicode, but decode only works on ASCII.Thus, instead of using
.encode()
or.decode()
, use the included Python libraries base64 and binascii, which abstract away the issues, and make our code work on both Python 2 and 3. More info at StackOverflow.Pull request coming soon, but here's a public notice.