Closed florentx closed 10 years ago
I guess you would expect it to convert to string silently? Btw, this is a change that was "accidentally" added because of the Python 3 port, where typing-related problems forced us to be strict about the types accepted by to_bytes
.
This does not look consistent:
On the Python 2 code, str is a subclass of bytes and therefore x is casted to bytes:
def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
if x is None:
return None
if isinstance(x, (bytes, bytearray, buffer)):
return bytes(x)
if isinstance(x, unicode):
return x.encode(charset, errors)
raise TypeError('Expected bytes')
On the Python 3 code, str is not a subclass of bytes anymore but is encoded to the charset:
def to_bytes(x, charset=sys.getdefaultencoding(), errors='strict'):
if x is None:
return None
if isinstance(x, (bytes, bytearray, memoryview)):
return bytes(x)
if isinstance(x, str):
return x.encode(charset, errors)
raise TypeError('Expected bytes')
Is it what you really wanted to do?
I think this is not a regression but a progression: this should never have been working with integers. Seriously, the current behavior is much more pythonic.
@cecton OT: AFAIK, there is no difference in Python 2 between str
and bytes
, so there's no casting. to_bytes
returns a string.
In Python 3 instead a str
contains Unicode data, which is different from bytes
, so there's casting. to_bytes
returns... bytes.
Working perfectly.
there is no difference in Python 2 between str and bytes, so there's no casting. to_bytes returns a string. In Python 3 instead a str contains Unicode data, which is different from bytes, so there's casting. to_bytes returns... bytes. Working perfectly.
Right! Didn't know that. Thanks for the information.
Now it sounds great to me.
(And yes it makes sense that x should be some kind of string)
@cecton Should this be closed then?
@untitaker Well... tbh I don't remember that I wrote something here (>_<) What about you @florentx (ticket opener)?
Closing this for the exact reasons @ghost stated: It was never intended to work that way.
We have a regression when moving from Werkzeug 0.8 to 0.9, because the method
Response.set_cookie
no longer accepts integer values.AFAICT, this change of behavior is not documented.