Implicit and explicit resource management¶
The Python C API has multiple APIs which implicit resource managements:
These functions return a pointer to data. The pointer becomes a dangling pointer once the object is deleted.
This API makes the assumption that the memory address of Python objects are fixed. Python implementations moving objects in memory have to pin these objects in memory to implement the API, and pinning memory is inefficient in this case.
Py_buffer API has a
PyBuffer_Release() function which can execute
code when a buffer is no longer needed:
- Free memory if the buffer was a memory copy
- Close files
Array of objects¶
array = &PyTuple_GET_ITEM(tuple, 0) is a common code pattern to access a
Python tuple as a C array of
PyObject*: array of
PyObject** type. It
makes the assumption that a tuple stores
becomes a dangling pointer once the tuple is destroyed.