This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: str(bytes) does __repr__() instead of __str__()
Type: behavior Stage:
Components: Interpreter Core, Unicode Versions: Python 3.4
process
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: doerwalter, eryksun, ezio.melotti, jwezel, martin.panter
Priority: normal Keywords:

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 "<stdin>", line 1, in <module>
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:26adminsetgithub: 70339
2016-01-19 21:58:17jwezelsetmessages: + msg258625
2016-01-19 21:19:25vstinnersetnosy: - vstinner
2016-01-19 20:46:56martin.pantersetnosy: + martin.panter
messages: + msg258620
2016-01-19 17:15:22doerwaltersetmessages: + msg258610
2016-01-19 16:19:08jwezelsetmessages: + msg258608
2016-01-19 16:14:00doerwaltersetnosy: + doerwalter
messages: + msg258606
2016-01-19 16:06:55jwezelsetmessages: + msg258604
2016-01-19 16:02:21eryksunsetnosy: + eryksun
messages: + msg258603
2016-01-19 12:42:38jwezelsetmessages: + msg258589
2016-01-19 12:33:43vstinnersetstatus: open -> closed
resolution: not a bug
messages: + msg258588
2016-01-19 11:52:04jwezelcreate