blob: b64381bf39d47e72eb8fdf18c27a3c0d125bf17f [file] [log] [blame]
Georg Brandl54a3faa2008-01-20 09:30:57 +00001.. highlightlang:: c
2
3.. _allocating-objects:
4
5Allocating Objects on the Heap
6==============================
7
8
9.. cfunction:: PyObject* _PyObject_New(PyTypeObject *type)
10
11
12.. cfunction:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
13
14
15.. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
16
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000017 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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000022
23
24.. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
25
26 This does everything :cfunc:`PyObject_Init` does, and also initializes the
27 length information for a variable-size object.
28
29
30.. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
31
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000032 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 :attr:`tp_basicsize` field of
36 the type object.
Georg Brandl54a3faa2008-01-20 09:30:57 +000037
38
39.. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
40
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000041 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 :attr:`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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000049
50
51.. cfunction:: void PyObject_Del(PyObject *op)
52
53 Releases memory allocated to an object using :cfunc:`PyObject_New` or
Jeroen Ruigrok van der Wervenbd875522009-04-26 21:06:15 +000054 :cfunc:`PyObject_NewVar`. This is normally called from the
55 :attr:`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.
Georg Brandl54a3faa2008-01-20 09:30:57 +000058
59
Georg Brandl54a3faa2008-01-20 09:30:57 +000060.. cvar:: PyObject _Py_NoneStruct
61
62 Object which is visible in Python as ``None``. This should only be accessed
63 using the :cmacro:`Py_None` macro, which evaluates to a pointer to this
64 object.
Georg Brandle69cdf92009-01-04 23:20:14 +000065
66
67.. seealso::
68
69 :cfunc:`PyModule_Create`
70 To allocate and create extension modules.
71