Debian Codegraph @

Doc/c-api/allocation.rst

raw
1 .. highlight:: c
2
3 .. _allocating-objects:
4
5 Allocating Objects on the Heap
6 ==============================
7
8
9 .. c:function:: PyObject* _PyObject_New(PyTypeObject *type)
10
11
12 .. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
13
14
15 .. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
16
17 Initialize a newly allocated object *op* with its type and initial
18 reference. Returns the initialized object. Other fields of the object are
19 not affected.
20
21
22 .. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
23
24 This does everything :c:func:`PyObject_Init` does, and also initializes the
25 length information for a variable-size object.
26
27
28 .. c:macro:: PyObject_New(TYPE, typeobj)
29
30 Allocate a new Python object using the C structure type *TYPE*
31 and the Python type object *typeobj* (``PyTypeObject*``).
32 Fields not defined by the Python object header are not initialized.
33 The caller will own the only reference to the object
34 (i.e. its reference count will be one).
35 The size of the memory allocation is determined from the
36 :c:member:`~PyTypeObject.tp_basicsize` field of the type object.
37
38 Note that this function is unsuitable if *typeobj* has
39 :c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
40 use :c:func:`PyObject_GC_New` instead.
41
42
43 .. c:macro:: PyObject_NewVar(TYPE, typeobj, size)
44
45 Allocate a new Python object using the C structure type *TYPE* and the
46 Python type object *typeobj* (``PyTypeObject*``).
47 Fields not defined by the Python object header
48 are not initialized. The allocated memory allows for the *TYPE* structure
49 plus *size* (``Py_ssize_t``) fields of the size
50 given by the :c:member:`~PyTypeObject.tp_itemsize` field of
51 *typeobj*. This is useful for implementing objects like tuples, which are
52 able to determine their size at construction time. Embedding the array of
53 fields into the same allocation decreases the number of allocations,
54 improving the memory management efficiency.
55
56 Note that this function is unsuitable if *typeobj* has
57 :c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
58 use :c:func:`PyObject_GC_NewVar` instead.
59
60
61 .. c:var:: PyObject _Py_NoneStruct
62
63 Object which is visible in Python as ``None``. This should only be accessed
64 using the :c:macro:`Py_None` macro, which evaluates to a pointer to this
65 object.
66
67
68 .. seealso::
69
70 :ref:`moduleobjects`
71 To allocate and create extension modules.
72
73
74 Deprecated aliases
75 ^^^^^^^^^^^^^^^^^^
76
77 These are :term:`soft deprecated` aliases to existing functions and macros.
78 They exist solely for backwards compatibility.
79
80
81 .. list-table::
82 :widths: auto
83 :header-rows: 1
84
85 * * Deprecated alias
86 * Function
87 * * .. c:macro:: PyObject_NEW(type, typeobj)
88 * :c:macro:`PyObject_New`
89 * * .. c:macro:: PyObject_NEW_VAR(type, typeobj, n)
90 * :c:macro:`PyObject_NewVar`
91 * * .. c:macro:: PyObject_INIT(op, typeobj)
92 * :c:func:`PyObject_Init`
93 * * .. c:macro:: PyObject_INIT_VAR(op, typeobj, n)
94 * :c:func:`PyObject_InitVar`
95 * * .. c:macro:: PyObject_MALLOC(n)
96 * :c:func:`PyObject_Malloc`
97 * * .. c:macro:: PyObject_REALLOC(p, n)
98 * :c:func:`PyObject_Realloc`
99 * * .. c:macro:: PyObject_FREE(p)
100 * :c:func:`PyObject_Free`
101 * * .. c:macro:: PyObject_DEL(p)
102 * :c:func:`PyObject_Free`
103 * * .. c:macro:: PyObject_Del(p)
104 * :c:func:`PyObject_Free`