[Python-Dev] PEP 461: Adding % formatting to bytes and bytearray -- Final, Take 2

Nick Coghlan ncoghlan at gmail.com
Mon Feb 24 03:45:42 CET 2014


On 24 February 2014 08:56, Cameron Simpson <cs at zip.com.au> wrote:
> On 23Feb2014 22:56, Victor Stinner <victor.stinner at gmail.com> wrote:
>> > An aid to debugging -- need to see what's what at that moment?  Toss it
>> > into %a.  It is not intended for production code, but is included to
>> > hopefully circumvent the inappropriate use of __bytes__ methods on classes.
>>
>> How do you plan to use this output? Write it into a socket or a file?
>> When I debug, I use print & logging which both expect text string. So I
>> think that b'%a' is useless.
>
> The case from the email thread, which I support at +0.5 or maybe
> only +0.1, is printing to a binary log. The classic example that
> comes to mind is syslog packets.

We actually hit a bug related to that in Beaker not that long ago - we
were interpolating (Python 2) 8-bit strings directly into the syslog
data, and it corrupted the log message when one of those strings
contained a NULL value.

Would leaving %a out destroy the utility of the PEP? No. Is leaving it
in useful? I think so, yes, as it provides OOWTD interpolation of pure
ASCII representations into binary formats that contain ASCII
compatible segments, and it's directly analogous to the handling of
the numeric formatting codes with (b"%a" % obj) being a shorthand for
(b"%s" % ("%a" % obj).encode("ascii")). (Note that invoking repr() or
ascii() on a bytes instance is perfectly legal, even under -b and -bb
- it's only str() that triggers a warning or error)

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia


More information about the Python-Dev mailing list