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

Fixed AttributeError: 'bytes' object has no attribute 'encode' #229

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Conversation

yuhr123
Copy link

@yuhr123 yuhr123 commented Aug 15, 2018

The key field already be decode in Python 3. So I add a condition statement to check version of python to fix this error.

@carlosvega
Copy link

Please consider this

@and-semakin
Copy link
Collaborator

Hi @yuhr123! Thank you for your contribution!

Could you please explain how to reproduce the error you are trying to fix? What version of Python does it affect -- 2 or 3?

I don't think that this solution is appropriate as it breaks a valid use case on Python 3 when flask_secret_key is str with following error:

>           key = key + b' '*32    # Make sure the key is at least 32 bytes long
E           TypeError: can only concatenate str (not "bytes") to str

@and-semakin
Copy link
Collaborator

Maybe you wanted to do the opposite?

if sys.version_info[0] < 3:
    key = flask_secret_key
else:
    key = flask_secret_key.encode()

So don't encode on python2 as str is already a byte string, but do encode on python3 to get bytes.

@webcoderz
Copy link

can we push this please having difficulty with docker images that rely on this this change enables it to work on py3

@blaushild
Copy link

blaushild commented Mar 18, 2021

I use urandom() for a secret key generation. The key is always in bytes. And it doesn't matter which version of python is in use.

I guess something like this:

if isinstance(key, bytes):
    key = flask_secret_key
else:
    key = flask_secret_key.encode()

PS Sorry, I'm new here. I don't know how to work correctly with github system.

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

Successfully merging this pull request may close these issues.

5 participants