Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 1 | .. highlightlang:: c |
| 2 | |
| 3 | .. _allocating-objects: |
| 4 | |
| 5 | Allocating Objects on the Heap |
| 6 | ============================== |
| 7 | |
| 8 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 9 | .. c:function:: PyObject* _PyObject_New(PyTypeObject *type) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 10 | |
| 11 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 12 | .. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 13 | |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 14 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 15 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame] | 16 | changes in your code for properly supporting 64-bit systems. |
| 17 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 18 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 19 | .. c:function:: void _PyObject_Del(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 20 | |
| 21 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 22 | .. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 23 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 24 | 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 29 | |
| 30 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 31 | .. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 32 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 33 | This does everything :c:func:`PyObject_Init` does, and also initializes the |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 34 | length information for a variable-size object. |
| 35 | |
Jeroen Ruigrok van der Werven | 2aa7840 | 2009-04-25 19:46:19 +0000 | [diff] [blame] | 36 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 37 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 2aa7840 | 2009-04-25 19:46:19 +0000 | [diff] [blame] | 38 | changes in your code for properly supporting 64-bit systems. |
| 39 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 40 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 41 | .. c:function:: TYPE* PyObject_New(TYPE, PyTypeObject *type) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 42 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 43 | Allocate a new Python object using the C structure type *TYPE* and the |
| 44 | Python type object *type*. Fields not defined by the Python object header |
| 45 | are not initialized; the object's reference count will be one. The size of |
Antoine Pitrou | 92fae55 | 2013-08-01 21:17:24 +0200 | [diff] [blame] | 46 | the memory allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize` field of |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 47 | the type object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 48 | |
| 49 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 50 | .. c:function:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 51 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 52 | Allocate a new Python object using the C structure type *TYPE* and the |
| 53 | Python type object *type*. Fields not defined by the Python object header |
| 54 | are not initialized. The allocated memory allows for the *TYPE* structure |
Antoine Pitrou | 92fae55 | 2013-08-01 21:17:24 +0200 | [diff] [blame] | 55 | plus *size* fields of the size given by the :c:member:`~PyTypeObject.tp_itemsize` field of |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 56 | *type*. This is useful for implementing objects like tuples, which are |
| 57 | able to determine their size at construction time. Embedding the array of |
| 58 | fields into the same allocation decreases the number of allocations, |
| 59 | improving the memory management efficiency. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 60 | |
Jeroen Ruigrok van der Werven | 2aa7840 | 2009-04-25 19:46:19 +0000 | [diff] [blame] | 61 | .. versionchanged:: 2.5 |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 62 | This function used an :c:type:`int` type for *size*. This might require |
Jeroen Ruigrok van der Werven | 2aa7840 | 2009-04-25 19:46:19 +0000 | [diff] [blame] | 63 | changes in your code for properly supporting 64-bit systems. |
| 64 | |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 65 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 66 | .. c:function:: void PyObject_Del(PyObject *op) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 67 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 68 | Releases memory allocated to an object using :c:func:`PyObject_New` or |
| 69 | :c:func:`PyObject_NewVar`. This is normally called from the |
Antoine Pitrou | 92fae55 | 2013-08-01 21:17:24 +0200 | [diff] [blame] | 70 | :c:member:`~PyTypeObject.tp_dealloc` handler specified in the object's type. The fields of |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 71 | the object should not be accessed after this call as the memory is no |
| 72 | longer a valid Python object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 73 | |
| 74 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 75 | .. c:function:: PyObject* Py_InitModule(char *name, PyMethodDef *methods) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 76 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 77 | Create a new module object based on a name and table of functions, |
| 78 | returning the new module object. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 79 | |
| 80 | .. versionchanged:: 2.3 |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 81 | Older versions of Python did not support *NULL* as the value for the |
| 82 | *methods* argument. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 83 | |
| 84 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 85 | .. c:function:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 86 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 87 | Create a new module object based on a name and table of functions, |
| 88 | returning the new module object. If *doc* is non-*NULL*, it will be used |
| 89 | to define the docstring for the module. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 90 | |
| 91 | .. versionchanged:: 2.3 |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 92 | Older versions of Python did not support *NULL* as the value for the |
| 93 | *methods* argument. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 94 | |
| 95 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 96 | .. c:function:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver) |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 97 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 98 | Create a new module object based on a name and table of functions, |
| 99 | returning the new module object. If *doc* is non-*NULL*, it will be used |
| 100 | to define the docstring for the module. If *self* is non-*NULL*, it will |
| 101 | passed to the functions of the module as their (otherwise *NULL*) first |
| 102 | parameter. (This was added as an experimental feature, and there are no |
| 103 | known uses in the current version of Python.) For *apiver*, the only value |
| 104 | which should be passed is defined by the constant |
| 105 | :const:`PYTHON_API_VERSION`. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 106 | |
| 107 | .. note:: |
| 108 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 109 | Most uses of this function should probably be using the |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 110 | :c:func:`Py_InitModule3` instead; only use this if you are sure you need |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 111 | it. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 112 | |
| 113 | .. versionchanged:: 2.3 |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 114 | Older versions of Python did not support *NULL* as the value for the |
| 115 | *methods* argument. |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 116 | |
| 117 | |
Sandro Tosi | 98ed08f | 2012-01-14 16:42:02 +0100 | [diff] [blame] | 118 | .. c:var:: PyObject _Py_NoneStruct |
Georg Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 119 | |
Jeroen Ruigrok van der Werven | 28c81e0 | 2009-04-25 18:53:48 +0000 | [diff] [blame] | 120 | Object which is visible in Python as ``None``. This should only be |
| 121 | accessed using the ``Py_None`` macro, which evaluates to a pointer to this |
| 122 | object. |