blob: dc9f5a4d54e3d3809a84d2b8b3e80291cc4b6c72 [file] [log] [blame]
Georg Brandlf6842722008-01-19 22:08:21 +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
Jeroen Ruigrok van der Werven089c5cd2009-04-25 17:59:03 +000014 .. versionchanged:: 2.5
15 This function used an :ctype:`int` type for *size*. This might require
16 changes in your code for properly supporting 64-bit systems.
17
Georg Brandlf6842722008-01-19 22:08:21 +000018
19.. cfunction:: void _PyObject_Del(PyObject *op)
20
21
22.. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
23
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000024 Initialize a newly-allocated object *op* with its type and initial
25 reference. Returns the initialized object. If *type* indicates that the
26 object participates in the cyclic garbage detector, it is added to the
27 detector's set of observed objects. Other fields of the object are not
28 affected.
Georg Brandlf6842722008-01-19 22:08:21 +000029
30
31.. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
32
33 This does everything :cfunc:`PyObject_Init` does, and also initializes the
34 length information for a variable-size object.
35
36
37.. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
38
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000039 Allocate a new Python object using the C structure type *TYPE* and the
40 Python type object *type*. Fields not defined by the Python object header
41 are not initialized; the object's reference count will be one. The size of
42 the memory allocation is determined from the :attr:`tp_basicsize` field of
43 the type object.
Georg Brandlf6842722008-01-19 22:08:21 +000044
45
46.. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
47
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000048 Allocate a new Python object using the C structure type *TYPE* and the
49 Python type object *type*. Fields not defined by the Python object header
50 are not initialized. The allocated memory allows for the *TYPE* structure
51 plus *size* fields of the size given by the :attr:`tp_itemsize` field of
52 *type*. This is useful for implementing objects like tuples, which are
53 able to determine their size at construction time. Embedding the array of
54 fields into the same allocation decreases the number of allocations,
55 improving the memory management efficiency.
Georg Brandlf6842722008-01-19 22:08:21 +000056
57
58.. cfunction:: void PyObject_Del(PyObject *op)
59
60 Releases memory allocated to an object using :cfunc:`PyObject_New` or
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000061 :cfunc:`PyObject_NewVar`. This is normally called from the
62 :attr:`tp_dealloc` handler specified in the object's type. The fields of
63 the object should not be accessed after this call as the memory is no
64 longer a valid Python object.
Georg Brandlf6842722008-01-19 22:08:21 +000065
66
67.. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods)
68
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000069 Create a new module object based on a name and table of functions,
70 returning the new module object.
Georg Brandlf6842722008-01-19 22:08:21 +000071
72 .. versionchanged:: 2.3
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000073 Older versions of Python did not support *NULL* as the value for the
74 *methods* argument.
Georg Brandlf6842722008-01-19 22:08:21 +000075
76
77.. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
78
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000079 Create a new module object based on a name and table of functions,
80 returning the new module object. If *doc* is non-*NULL*, it will be used
81 to define the docstring for the module.
Georg Brandlf6842722008-01-19 22:08:21 +000082
83 .. versionchanged:: 2.3
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000084 Older versions of Python did not support *NULL* as the value for the
85 *methods* argument.
Georg Brandlf6842722008-01-19 22:08:21 +000086
87
88.. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
89
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +000090 Create a new module object based on a name and table of functions,
91 returning the new module object. If *doc* is non-*NULL*, it will be used
92 to define the docstring for the module. If *self* is non-*NULL*, it will
93 passed to the functions of the module as their (otherwise *NULL*) first
94 parameter. (This was added as an experimental feature, and there are no
95 known uses in the current version of Python.) For *apiver*, the only value
96 which should be passed is defined by the constant
97 :const:`PYTHON_API_VERSION`.
Georg Brandlf6842722008-01-19 22:08:21 +000098
99 .. note::
100
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +0000101 Most uses of this function should probably be using the
102 :cfunc:`Py_InitModule3` instead; only use this if you are sure you need
103 it.
Georg Brandlf6842722008-01-19 22:08:21 +0000104
105 .. versionchanged:: 2.3
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +0000106 Older versions of Python did not support *NULL* as the value for the
107 *methods* argument.
Georg Brandlf6842722008-01-19 22:08:21 +0000108
109
110.. cvar:: PyObject _Py_NoneStruct
111
Jeroen Ruigrok van der Werven28c81e02009-04-25 18:53:48 +0000112 Object which is visible in Python as ``None``. This should only be
113 accessed using the ``Py_None`` macro, which evaluates to a pointer to this
114 object.