Le ven. 26 juin 2020 à 07:36, Carl Shapiro firstname.lastname@example.org a écrit :
It would be very helpful to broaden the objective of avoiding functions returning PyObject** to other types of pointers. I have in mind several functions in the C-API that return a char* pointer to the contents of an object. While these functions are easy to implement on top of the CPython object model they are challenging for alternative Python implementations.
Consider PyBytes_AsString: it returns a mutable char* pointing to the contents of a byte instance. This presents several obvious problems. For starters, it burdens a relocating garbage collector to pin objects or create a temporary copy of an object's contents in non-moving memory. It also has implications for treating PyObejct* as a handle, using tagged pointers (and tagged immediates), and multi-threading.
To eliminate C-API functions such as PyBytes_AsString, PyUnicode_AsUTF8, etc., new functions should be added to the C-API that copy the contents of objects out into a buffer, similar to PyUnicode_AsUCS4 or to return the contents in an dynamically allocated buffer like PyUnicode_AsUCS4Copy.
Well, the general problem is to track when the caller ends using a resource. Borrowed references are a variant of this problem, PySequence_Fast_ITEMS() is another variant.
For PyUnicode_AsUTF8, INADA-san added PyUnicode_GetUTF8Buffer() which should be used wit PyBuffer_Release():
* https://github.com/python/cpython/commit/c7ad974d341d3edb6b9d2a2dcae4d3d4794... * https://github.com/python/cpython/pull/17659 * https://discuss.python.org/t/better-api-for-encoding-unicode-objects-with-ut...
... but it was reverted soon after its addition:
See also the "(PEP 620) C API for efficient loop iterating on a sequence of PyObject** or other C types" thread.
Victor -- Night gathers, and now my watch begins. It shall not end until my death.