Issue 10832: Add support of bytes objects in PyBytes_FromFormatV() (original) (raw)
It would be very practical use have a format, eg. '%y', to accept bytes object in PyBytes_FromFormatV().
Example (extracted from posixmodule.c):
k = PyBytes_AsString(key2);
v = PyBytes_AsString(val2);
len = PyBytes_GET_SIZE(key2) + PyBytes_GET_SIZE(val2) + 2;
p = PyMem_NEW(char, len);
if (p == NULL) { PyErr_NoMemory(); ... }
PyOS_snprintf(p, len, "%s=%s", k, v);
With %y, it can be written:
p = PyBytes_FromFormat("%y=%y", key2, val2);
if (p == NULL) { PyErr_NoMemory(); ... }
The '%y' may also accept bytearray and any object with the buffer interface (as the 'y' format of PyArg_Parse*() functions).