1.. highlight:: c
2
3.. _allocating-objects:
4
5Allocating 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.  If *type* indicates that the
19   object participates in the cyclic garbage detector, it is added to the
20   detector's set of observed objects. Other fields of the object are not
21   affected.
22
23
24.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
25
26   This does everything :c:func:`PyObject_Init` does, and also initializes the
27   length information for a variable-size object.
28
29
30.. c:function:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
31
32   Allocate a new Python object using the C structure type *TYPE* and the
33   Python type object *type*.  Fields not defined by the Python object header
34   are not initialized; the object's reference count will be one.  The size of
35   the memory allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize` field of
36   the type object.
37
38
39.. c:function:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
40
41   Allocate a new Python object using the C structure type *TYPE* and the
42   Python type object *type*.  Fields not defined by the Python object header
43   are not initialized.  The allocated memory allows for the *TYPE* structure
44   plus *size* fields of the size given by the :c:member:`~PyTypeObject.tp_itemsize` field of
45   *type*.  This is useful for implementing objects like tuples, which are
46   able to determine their size at construction time.  Embedding the array of
47   fields into the same allocation decreases the number of allocations,
48   improving the memory management efficiency.
49
50
51.. c:function:: void PyObject_Del(void *op)
52
53   Releases memory allocated to an object using :c:func:`PyObject_New` or
54   :c:func:`PyObject_NewVar`.  This is normally called from the
55   :c:member:`~PyTypeObject.tp_dealloc` handler specified in the object's type.  The fields of
56   the object should not be accessed after this call as the memory is no
57   longer a valid Python object.
58
59
60.. c:var:: PyObject _Py_NoneStruct
61
62   Object which is visible in Python as ``None``.  This should only be accessed
63   using the :c:macro:`Py_None` macro, which evaluates to a pointer to this
64   object.
65
66
67.. seealso::
68
69   :c:func:`PyModule_Create`
70      To allocate and create extension modules.
71
72