Skip to content
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

Closed
jwezel mannequin opened this issue Jan 19, 2016 · 10 comments
Closed

str(bytes) does __repr__() instead of __str__() #70339

jwezel mannequin opened this issue Jan 19, 2016 · 10 comments
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error

Comments

@jwezel
Copy link
Mannequin

jwezel mannequin commented Jan 19, 2016

BPO 26151
Nosy @doerwalter, @ezio-melotti, @vadmium, @eryksun

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:

assignee = None
closed_at = <Date 2016-01-19.12:33:43.188>
created_at = <Date 2016-01-19.11:52:03.890>
labels = ['interpreter-core', 'type-bug', 'invalid', 'expert-unicode']
title = 'str(bytes) does __repr__() instead of __str__()'
updated_at = <Date 2016-01-19.21:58:17.946>
user = 'https://bugs.python.org/jwezel'

bugs.python.org fields:

activity = <Date 2016-01-19.21:58:17.946>
actor = 'jwezel'
assignee = 'none'
closed = True
closed_date = <Date 2016-01-19.12:33:43.188>
closer = 'vstinner'
components = ['Interpreter Core', 'Unicode']
creation = <Date 2016-01-19.11:52:03.890>
creator = 'jwezel'
dependencies = []
files = []
hgrepos = []
issue_num = 26151
keywords = []
message_count = 10.0
messages = ['258583', '258588', '258589', '258603', '258604', '258606', '258608', '258610', '258620', '258625']
nosy_count = 5.0
nosy_names = ['doerwalter', 'ezio.melotti', 'martin.panter', 'eryksun', 'jwezel']
pr_nums = []
priority = 'normal'
resolution = 'not a bug'
stage = None
status = 'closed'
superseder = None
type = 'behavior'
url = 'https://bugs.python.org/issue26151'
versions = ['Python 3.4']

@jwezel
Copy link
Mannequin Author

jwezel mannequin commented Jan 19, 2016

str(b'xxx') returns "b'xxx'" instead of 'xxx'

@jwezel jwezel mannequin added interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error labels Jan 19, 2016
@vstinner
Copy link
Member

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')).

@jwezel
Copy link
Mannequin Author

jwezel mannequin commented Jan 19, 2016

Bad feature, as it is a violation of POLA.

@eryksun
Copy link
Contributor

eryksun commented Jan 19, 2016

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.

@jwezel
Copy link
Mannequin Author

jwezel mannequin commented Jan 19, 2016

Who's talking about latin-1 in Python3? Of course str() needs to return decode('utf-8').

@doerwalter
Copy link
Contributor

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!

@jwezel
Copy link
Mannequin Author

jwezel mannequin commented Jan 19, 2016

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.

@doerwalter
Copy link
Contributor

But this leads to uninspectable objects.

@vadmium
Copy link
Member

vadmium commented Jan 19, 2016

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'

@jwezel
Copy link
Mannequin Author

jwezel mannequin commented Jan 19, 2016

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.

@ezio-melotti ezio-melotti transferred this issue from another repository Apr 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
interpreter-core (Objects, Python, Grammar, and Parser dirs) topic-unicode type-bug An unexpected behavior, bug, or error
Projects
None yet
Development

No branches or pull requests

4 participants