[Python-ideas] Stop displaying elements of bytes objects as printable ASCII characters in CPython 3

Andrew Barnert abarnert at yahoo.com
Tue Sep 16 22:09:59 CEST 2014


On Sep 16, 2014, at 8:04, Ethan Furman <ethan at stoneleaf.us> wrote:

> On 09/16/2014 06:02 AM, Eric V. Smith wrote:
>> On 09/15/2014 08:43 PM, Ethan Furman wrote:
>>> On 09/15/2014 04:47 PM, Nick Coghlan wrote:
>>>> 
>>>> The current suggestion on the issue tracker is to add __format__ to
>>>> bytes/bytearray/memoryview with a suitable symbolic mini-language to
>>>>  control the formatting details.
>>> 
>>> PEP 461 specifically did not add back __format__ to bytes/bytearrays.  I
>>> think a PEP is appropriate to reverse that decision.
>> 
>> That's different. PEP 461 excluded them because it was talking about
>> bytes.format(). bytes.__format__() would be much easier to deal with,
>> because its result must be unicode (str in 3.x).
>> 
>> I don't think just adding bytes/bytearray.__format__() per se requires a
>> PEP. It's not a very radical addition, similar to datetime.__format__().
>> But I wouldn't be opposed to a PEP to decide on the specifics of the
>> mini-language that bytes.__format__() supports.
> 
> So the difference is:
> 
>   b'Hello, %s' % some_bytes_var --> b'Hello, <whatever>'
> 
> whilst
> 
>   b'Hello, {}'.format(some_uni_var) --> u'Hello, <whatever>'

No, you're mixing up `format`, an explicit method on str that no one is suggesting adding to bytes, and `__format__`, a dunder method on every type that's used by `str.format` and `format`; the proposal is to extend `bytes.__format__` in some way that I don't think is entirely decided yet, but it would look something like this:

    u'Hello, {:a}'.format(some_bytes_var)  --> u'Hello, <whatever>'

Or:

    u'Hello, {:#x}'.format(some_bytes_var) --> u'Hello, \\x2d\\x78\\x68\\x61...'

> (Yes, I remember unicode == str, I was just being explicit ;)
> 
> That would certainly go along with the idea that `format` is for strings.
> 
> --
> ~Ethan~
> _______________________________________________
> Python-ideas mailing list
> Python-ideas at python.org
> https://mail.python.org/mailman/listinfo/python-ideas
> Code of Conduct: http://python.org/psf/codeofconduct/


More information about the Python-ideas mailing list