在 heap 上分配物件

PyObject *_PyObject_New(PyTypeObject *type)
回傳值:新的參照。
PyVarObject *_PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
回傳值:新的參照。
PyObject *PyObject_Init(PyObject *op, PyTypeObject *type)
回傳值:借用參照。 Part of the Stable ABI.

用它的型別和初始參照來初始化新分配物件 op。已初始化的物件會被回傳。如果 type 表示了該物件參與迴圈垃圾檢查器,則將其新增到檢查器的觀察物件集合中。物件的其他欄位不受影響。

PyVarObject *PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
回傳值:借用參照。 Part of the Stable ABI.

它會做到 PyObject_Init() 的所有功能,並且會初始化一個大小可變物件的長度資訊。

PyObject_New(TYPE, typeobj)

Allocate a new Python object using the C structure type TYPE and the Python type object typeobj (PyTypeObject*). Fields not defined by the Python object header are not initialized. The caller will own the only reference to the object (i.e. its reference count will be one). The size of the memory allocation is determined from the tp_basicsize field of the type object.

PyObject_NewVar(TYPE, typeobj, size)

Allocate a new Python object using the C structure type TYPE and the Python type object typeobj (PyTypeObject*). Fields not defined by the Python object header are not initialized. The allocated memory allows for the TYPE structure plus size (Py_ssize_t) fields of the size given by the tp_itemsize field of typeobj. This is useful for implementing objects like tuples, which are able to determine their size at construction time. Embedding the array of fields into the same allocation decreases the number of allocations, improving the memory management efficiency.

void PyObject_Del(void *op)

Releases memory allocated to an object using PyObject_New or PyObject_NewVar. This is normally called from the tp_dealloc handler specified in the object's type. The fields of the object should not be accessed after this call as the memory is no longer a valid Python object.

PyObject _Py_NoneStruct

這個物件像是 Python 中的 None。它只應該透過 Py_None 巨集來存取,該巨集的拿到指向該物件的指標。

也參考

PyModule_Create()

分配記憶體和建立擴充模組。