Georg Brandl | 54a3faa | 2008-01-20 09:30:57 +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 | |
| 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 | |
| 57 | .. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods) |
| 58 | |
| 59 | Create a new module object based on a name and table of functions, returning |
| 60 | the new module object; the *methods* argument can be *NULL* if no methods are |
| 61 | to be defined for the module. |
| 62 | |
| 63 | |
| 64 | .. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc) |
| 65 | |
| 66 | Create a new module object based on a name and table of functions, returning |
| 67 | the new module object. The *methods* argument can be *NULL* if no methods |
| 68 | are to be defined for the module. If *doc* is non-*NULL*, it will be used to |
| 69 | define the docstring for the module. |
| 70 | |
| 71 | |
| 72 | .. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver) |
| 73 | |
| 74 | Create a new module object based on a name and table of functions, returning |
| 75 | the new module object. The *methods* argument can be *NULL* if no methods |
| 76 | are to be defined for the module. If *doc* is non-*NULL*, it will be used to |
| 77 | define the docstring for the module. If *self* is non-*NULL*, it will passed |
| 78 | to the functions of the module as their (otherwise *NULL*) first parameter. |
| 79 | (This was added as an experimental feature, and there are no known uses in |
| 80 | the current version of Python.) For *apiver*, the only value which should be |
| 81 | passed is defined by the constant :const:`PYTHON_API_VERSION`. |
| 82 | |
| 83 | .. note:: |
| 84 | |
| 85 | Most uses of this function should probably be using the :cfunc:`Py_InitModule3` |
| 86 | instead; only use this if you are sure you need it. |
| 87 | |
| 88 | |
| 89 | .. cvar:: PyObject _Py_NoneStruct |
| 90 | |
| 91 | Object which is visible in Python as ``None``. This should only be accessed |
| 92 | using the :cmacro:`Py_None` macro, which evaluates to a pointer to this |
| 93 | object. |