Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

file_md5 string does not work in Python3 - Recommending a Cross Compatible Fix #16

Closed
antonizoon opened this issue Nov 9, 2015 · 1 comment
Assignees
Labels

Comments

@antonizoon
Copy link
Member

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.

@antonizoon antonizoon added the bug label Nov 9, 2015
@antonizoon antonizoon self-assigned this Nov 9, 2015
@antonizoon
Copy link
Member Author

Now fixed in the following commit, after backporting the File Class from py8chan:

b91304b

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant