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

ImageValue: hashlib.md5 problems with Python 3.x #28

Closed
rolexCoder opened this issue Jan 11, 2016 · 1 comment
Closed

ImageValue: hashlib.md5 problems with Python 3.x #28

rolexCoder opened this issue Jan 11, 2016 · 1 comment

Comments

@rolexCoder
Copy link

Hello,

I'm using Python 3.5 and Dango 1.9.

When I use ImageValue i'm getting the following error:

File "/home/user/virtualenv3.5/lib/python3.5/site-packages/dbsettings/values.py", line 306, in get_db_prep_save
    hashed_name = md5(six.text_type(time.time())).hexdigest() + value.name[-4:]
TypeError: Unicode-objects must be encoded before hashing

So you can see that the problem is on file dbsettings/values.py on line 306:

hashed_name = md5(six.text_type(time.time())).hexdigest() + value.name[-4:]

CAUSE

Since Python 3.0, for hashlib.md5 you need to pass bytes instead of str. You can confirm that in Python documentation:

https://docs.python.org/2.7/library/hashlib.html
https://docs.python.org/3.0/library/hashlib.html
https://docs.python.org/3.1/library/hashlib.html
...
https://docs.python.org/3.5/library/hashlib.html

RESOLUTION

Replace line 306 with the following code:

        import sys
        PYTHON_VERSION = sys.version_info
        value_to_hash = six.text_type(time.time())
        if PYTHON_VERSION >= (3, 0):
            value_to_hash = value_to_hash.encode()
        hashed_name = md5(value_to_hash).hexdigest() + value.name[-4:]

Thanks for your great work.

@zlorf zlorf closed this as completed in 1256a7d May 1, 2016
@rolexCoder
Copy link
Author

Hello,

With your last modification i'm getting an error on Python 3.5:

hashed_name = md5(six.binary_type(time.time())).hexdigest() + value.name[-4:]
TypeError: 'float' object is not iterable

Current Line 309:

        hashed_name = md5(six.binary_type(time.time())).hexdigest() + value.name[-4:]

SOLUTION

Please go back using function six.text_type() and encode the result so it is compatible with Python 3.0+.

Line 309 must be like this:

        hashed_name = md5(six.text_type(time.time()).encode()).hexdigest() + value.name[-4:]

It was tested on Python 2.7 and Python 3.5 and works great!

Regards.

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

No branches or pull requests

1 participant