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 | |
| 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 Werven | 089c5cd | 2009-04-25 17:59:03 +0000 | [diff] [blame^] | 14 | .. 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 Brandl | f684272 | 2008-01-19 22:08:21 +0000 | [diff] [blame] | 18 | |
| 19 | .. cfunction:: void _PyObject_Del(PyObject *op) |
| 20 | |
| 21 | |
| 22 | .. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type) |
| 23 | |
| 24 | Initialize a newly-allocated object *op* with its type and initial reference. |
| 25 | Returns the initialized object. If *type* indicates that the object |
| 26 | participates in the cyclic garbage detector, it is added to the detector's set |
| 27 | of observed objects. Other fields of the object are not affected. |
| 28 | |
| 29 | |
| 30 | .. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size) |
| 31 | |
| 32 | This does everything :cfunc:`PyObject_Init` does, and also initializes the |
| 33 | length information for a variable-size object. |
| 34 | |
| 35 | |
| 36 | .. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type) |
| 37 | |
| 38 | Allocate a new Python object using the C structure type *TYPE* and the Python |
| 39 | type object *type*. Fields not defined by the Python object header are not |
| 40 | initialized; the object's reference count will be one. The size of the memory |
| 41 | allocation is determined from the :attr:`tp_basicsize` field of the type object. |
| 42 | |
| 43 | |
| 44 | .. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size) |
| 45 | |
| 46 | Allocate a new Python object using the C structure type *TYPE* and the Python |
| 47 | type object *type*. Fields not defined by the Python object header are not |
| 48 | initialized. The allocated memory allows for the *TYPE* structure plus *size* |
| 49 | fields of the size given by the :attr:`tp_itemsize` field of *type*. This is |
| 50 | useful for implementing objects like tuples, which are able to determine their |
| 51 | size at construction time. Embedding the array of fields into the same |
| 52 | allocation decreases the number of allocations, improving the memory management |
| 53 | efficiency. |
| 54 | |
| 55 | |
| 56 | .. cfunction:: void PyObject_Del(PyObject *op) |
| 57 | |
| 58 | Releases memory allocated to an object using :cfunc:`PyObject_New` or |
| 59 | :cfunc:`PyObject_NewVar`. This is normally called from the :attr:`tp_dealloc` |
| 60 | handler specified in the object's type. The fields of the object should not be |
| 61 | accessed after this call as the memory is no longer a valid Python object. |
| 62 | |
| 63 | |
| 64 | .. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods) |
| 65 | |
| 66 | Create a new module object based on a name and table of functions, returning the |
| 67 | new module object. |
| 68 | |
| 69 | .. versionchanged:: 2.3 |
| 70 | Older versions of Python did not support *NULL* as the value for the *methods* |
| 71 | argument. |
| 72 | |
| 73 | |
| 74 | .. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc) |
| 75 | |
| 76 | Create a new module object based on a name and table of functions, returning the |
| 77 | new module object. If *doc* is non-*NULL*, it will be used to define the |
| 78 | docstring for the module. |
| 79 | |
| 80 | .. versionchanged:: 2.3 |
| 81 | Older versions of Python did not support *NULL* as the value for the *methods* |
| 82 | argument. |
| 83 | |
| 84 | |
| 85 | .. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver) |
| 86 | |
| 87 | Create a new module object based on a name and table of functions, returning the |
| 88 | new module object. If *doc* is non-*NULL*, it will be used to define the |
| 89 | docstring for the module. If *self* is non-*NULL*, it will passed to the |
| 90 | functions of the module as their (otherwise *NULL*) first parameter. (This was |
| 91 | added as an experimental feature, and there are no known uses in the current |
| 92 | version of Python.) For *apiver*, the only value which should be passed is |
| 93 | defined by the constant :const:`PYTHON_API_VERSION`. |
| 94 | |
| 95 | .. note:: |
| 96 | |
| 97 | Most uses of this function should probably be using the :cfunc:`Py_InitModule3` |
| 98 | instead; only use this if you are sure you need it. |
| 99 | |
| 100 | .. versionchanged:: 2.3 |
| 101 | Older versions of Python did not support *NULL* as the value for the *methods* |
| 102 | argument. |
| 103 | |
| 104 | |
| 105 | .. cvar:: PyObject _Py_NoneStruct |
| 106 | |
| 107 | Object which is visible in Python as ``None``. This should only be accessed |
| 108 | using the ``Py_None`` macro, which evaluates to a pointer to this object. |