[Python-Dev] PEP 460: allowing %d and %f and mojibake

Victor Stinner victor.stinner at gmail.com
Sun Jan 12 00:11:23 CET 2014


2014/1/11 Ethan Furman <ethan at stoneleaf.us>:
>>> b'x=%s' % 10 is well defined, it's pure bytes.
>>
>> It is well-defined? Then please explain me what the general case of
>>    b'%s' % x
>> is supposed to call:
>
> This is the key question, isn't it?

Python 2 and Python 3 are very different here.

In Python 2, the "s" format of PyArg_Parse may call the __str__()
method of an object.

In Python 3, the "y*" format of PyArg_Parse uses the Py_buffer API
which has no slot (there is no protocol like a __getbuffer__()
method).  The Py_buffer can only be implemented in C. For example,
bytes, bytearray and memoryview implement it. PyArg_Parse requires
also the buffer to be C-contiguous and has a single segment (use
PyBUF_SIMPLE flag).

Said differently, bytes%args and bytes.format() would *not* call any method.

Victor


More information about the Python-Dev mailing list