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