Issue 26151: str(bytes) does repr() instead of str() (original) (raw)

Created on 2016-01-19 11:52 by jwezel, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Messages (10)
msg258583 - (view) Author: Johnny Wezel (jwezel) Date: 2016-01-19 11:52
str(b'xxx') returns "b'xxx'" instead of 'xxx'
msg258588 - (view) Author: STINNER Victor (vstinner) * (Python committer) Date: 2016-01-19 12:33
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')).
msg258589 - (view) Author: Johnny Wezel (jwezel) Date: 2016-01-19 12:42
Bad feature, as it is a violation of POLA.
msg258603 - (view) Author: Eryk Sun (eryksun) * (Python triager) Date: 2016-01-19 16:02
> 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.
msg258604 - (view) Author: Johnny Wezel (jwezel) Date: 2016-01-19 16:06
Who's talking about latin-1 in Python3? Of course str() needs to return decode('utf-8').
msg258606 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2016-01-19 16:14
> 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!
msg258608 - (view) Author: Johnny Wezel (jwezel) Date: 2016-01-19 16:19
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.
msg258610 - (view) Author: Walter Dörwald (doerwalter) * (Python committer) Date: 2016-01-19 17:15
But this leads to uninspectable objects.
msg258620 - (view) Author: Martin Panter (martin.panter) * (Python committer) Date: 2016-01-19 20:46
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 "", line 1, in BytesWarning: str() on a bytes instance >>> print(repr(b"\xFF")) b'\xff'
msg258625 - (view) Author: Johnny Wezel (jwezel) Date: 2016-01-19 21:58
> But this leads to uninspectable objects. 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.
History
Date User Action Args
2022-04-11 14:58:26 admin set github: 70339
2016-01-19 21:58:17 jwezel set messages: +
2016-01-19 21:19:25 vstinner set nosy: - vstinner
2016-01-19 20:46:56 martin.panter set nosy: + martin.pantermessages: +
2016-01-19 17:15:22 doerwalter set messages: +
2016-01-19 16:19:08 jwezel set messages: +
2016-01-19 16:14:00 doerwalter set nosy: + doerwaltermessages: +
2016-01-19 16:06:55 jwezel set messages: +
2016-01-19 16:02:21 eryksun set nosy: + eryksunmessages: +
2016-01-19 12:42:38 jwezel set messages: +
2016-01-19 12:33:43 vstinner set status: open -> closedresolution: not a bugmessages: +
2016-01-19 11:52:04 jwezel create