[Python-Dev] PEP 460: allowing %d and %f and mojibake (original) (raw)

Greg Ewing greg.ewing at canterbury.ac.nz
Sun Jan 12 22:25:47 CET 2014


Nick Coghlan wrote:

On 13 Jan 2014 01:22, "Kristján Valur Jónsson" <kristjan at ccpgames.com_ _<mailto:kristjan at ccpgames.com>> wrote: > Well, my suggestion would that we should make it work, by having the %s format specifyer on bytes objects mean: str(arg).encode('ascii', 'strict')

It is not explicit, it is implicit - whether or not the resulting string assumes ASCII compatibility or not depends on whether you pass a binary value (no assumption) or a string value (assumes ASCII compatibility).

How do you make that out? As far as I can see, Kristjan's proposal will always call str() on the argument of a %s format, regardless of its type. The result of that str() is then required (not assumed) to be encodable as ascii. I don't see any type-dependent changes in behaviour here.

Interpolating a bytes object as-is, without a conversion to text, should be done by a different format specifier, such as %b. All text/bytes conversions are then explicit: if you write %s, then you're encoding something as ascii, but if you write %b, you're just inserting something that's already binary.

-- Greg



More information about the Python-Dev mailing list