Message 91556 - Python tracker (original) (raw)

+1 from me too; I agree the current API of PyBytes_FromObject is ugly.

Is there still a need for a separate C function for creating a zero- initialized bytes object from a Py_ssize_t or a Python integer? It's a fairly simple operation (PyBytes_FromStringAndSize + memset), so perhaps it doesn't need its own public function.

I'm not sure about the extra PyUnicode_Check: this seems to go against Python's philosophy of duck-typing. After all, the empty string is an iterable all of whose elements are integers. And this check doesn't cover other, similar, cases: for example, list('') will still be converted by PyBytes_FromObject, while list('123') won't. What's the benefit?

If this check is going to stay, there should probably also be a unit test for this behaviour.

Apart from the reservation about the PyUnicode_Check, the patch looks good to me. All tests pass on my machine (OS X 10.5) with this patch applied.