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
str(bytes) does __repr__() instead of __str__() #70339
Comments
str(b'xxx') returns "b'xxx'" instead of 'xxx' |
Yes. It's a feature, not a bug. You muse decode manually bytes to get type: b'xxx'.decode('ascii') or str(b'xxx'.decode('ascii')). |
Bad feature, as it is a violation of POLA. |
I would be astonished if the default __str__ conversion returned a Latin-1 decoding, which won't fail, or used the locale encoding or UTF-8, which could fail. The more explicit call x.decode() uses UTF-8 as the default encoding. |
Who's talking about latin-1 in Python3? Of course str() needs to return decode('utf-8'). |
So that would mean that: print(b"\xff") will always fail! |
The other question is why one would want to run such a statement. This is almost certainly a bug in which case an error one would be better off with an exception. |
But this leads to uninspectable objects. |
A warning is already emitted if you enable it with the “-b” flag, which I recommend. You can even turn the warning into an error with “-bb”. And you can always use repr() or ascii() to inspect for a more robust inspection. $ python3 -bb
Python 3.5.0 (default, Sep 20 2015, 11:28:25)
[GCC 5.2.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> print(b"\xFF")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
BytesWarning: str() on a bytes instance
>>> print(repr(b"\xFF"))
b'\xff' |
An inspection would be done with repr() which should not cause a problem. Besides. the official and correct way of handling an ff character is to emit a Unicode replacement code, not to issue an exception. Another flaw in the whole story. |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: