Closed neurino closed 4 years ago
In Python 2, "str" referred to a bytestring and "unicode" referred to a unicode string. In Python 3, "str" refers to unicode and "bytes" refers to a bytestring. In both cases, key.urlsafe() returns a bytestring. As urlsafe() is base64, this behavior is identical to the standard library's base64 behavior, which also returns a bytestring in both versions. I agree this is inconvenient in practice; however, it's working as intended for Python 3 strings.
And the reason why with the very same code in python 2 I never had to encode and decode is... ?
My best guess is that the behavior of Flask or another library you are using changed from 2 to 3 -- in 2 it accepted bytestrings (str) and in 3 it only accepts unicode (str). To find out exactly where this change occurred, look at the part of your code that breaks if you don't decode/encode -- that will be where the change in behavior occurred.
Hello,
Converting a flask app form Python 2 to 3 I'm having very hard times copeing with
Key.urlsafe()
returningbytes
instead ofstr
.Besides being this possibly formally correct, the value returned is not safe for sure or, in other terms, you're forced every time to decode bytes in the current encoding — utf-8 for me — to make it usable in a template, see example.
I started adding
.decode('utf-8')
and.encode('utf-8')
here and there and is making my code uglier and uglier,I wonder: if it is thought to be used in webpages and webpages are made of unicode strings, should
Key.urlsafe
be a unicode string?Environment details
OS type and version:
Python version and virtual environment information:
python --version
pip show google-<service>
orpip freeze
Code example