在堆上分配对象¶
-
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 thetp_basicsize
field of the type object.
-
PyObject_NewVar(TYPE, typeobj, size)¶
-
使用 C 结构类型 TYPE 和 Python 类型对象 typeobj (
PyTypeObject*
) 分配一个新的 Python 对象。 未在该 Python 对象标头中定义的字段不会被初始化。 被分配的内存允许 TYPE 结构加 typeobj 的tp_itemsize
字段所给出的 size (Py_ssize_t
) 个字段。 这对于实现像元组这样能够在构造时确定其大小的对象来说很有用。 将字段数组嵌入到相同的内在分配中可减少内存分配的次数,这提高了内存管理效率。
-
void PyObject_Del(void
*op)¶
-
释放使用
PyObject_New
或PyObject_NewVar
分配给一个对象的内存。 这通常由在对象的类型中指定的tp_dealloc
处理句柄来调用。 在此调用之后该对象中的字段不应再被访问因为原来的内存已不再是一个有效的 Python 对象。
参见
PyModule_Create()
-
分配内存和创建扩展模块