/Doc/c-api/allocation.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 122 lines · 79 code · 43 blank · 0 comment · 0 complexity · 6650af81d1b3c891dd144461676dda6a MD5 · raw file

  1. .. highlightlang:: c
  2. .. _allocating-objects:
  3. Allocating Objects on the Heap
  4. ==============================
  5. .. cfunction:: PyObject* _PyObject_New(PyTypeObject *type)
  6. .. cfunction:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
  7. .. versionchanged:: 2.5
  8. This function used an :ctype:`int` type for *size*. This might require
  9. changes in your code for properly supporting 64-bit systems.
  10. .. cfunction:: void _PyObject_Del(PyObject *op)
  11. .. cfunction:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
  12. Initialize a newly-allocated object *op* with its type and initial
  13. reference. Returns the initialized object. If *type* indicates that the
  14. object participates in the cyclic garbage detector, it is added to the
  15. detector's set of observed objects. Other fields of the object are not
  16. affected.
  17. .. cfunction:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
  18. This does everything :cfunc:`PyObject_Init` does, and also initializes the
  19. length information for a variable-size object.
  20. .. versionchanged:: 2.5
  21. This function used an :ctype:`int` type for *size*. This might require
  22. changes in your code for properly supporting 64-bit systems.
  23. .. cfunction:: TYPE* PyObject_New(TYPE, PyTypeObject *type)
  24. Allocate a new Python object using the C structure type *TYPE* and the
  25. Python type object *type*. Fields not defined by the Python object header
  26. are not initialized; the object's reference count will be one. The size of
  27. the memory allocation is determined from the :attr:`tp_basicsize` field of
  28. the type object.
  29. .. cfunction:: TYPE* PyObject_NewVar(TYPE, PyTypeObject *type, Py_ssize_t size)
  30. Allocate a new Python object using the C structure type *TYPE* and the
  31. Python type object *type*. Fields not defined by the Python object header
  32. are not initialized. The allocated memory allows for the *TYPE* structure
  33. plus *size* fields of the size given by the :attr:`tp_itemsize` field of
  34. *type*. This is useful for implementing objects like tuples, which are
  35. able to determine their size at construction time. Embedding the array of
  36. fields into the same allocation decreases the number of allocations,
  37. improving the memory management efficiency.
  38. .. versionchanged:: 2.5
  39. This function used an :ctype:`int` type for *size*. This might require
  40. changes in your code for properly supporting 64-bit systems.
  41. .. cfunction:: void PyObject_Del(PyObject *op)
  42. Releases memory allocated to an object using :cfunc:`PyObject_New` or
  43. :cfunc:`PyObject_NewVar`. This is normally called from the
  44. :attr:`tp_dealloc` handler specified in the object's type. The fields of
  45. the object should not be accessed after this call as the memory is no
  46. longer a valid Python object.
  47. .. cfunction:: PyObject* Py_InitModule(char *name, PyMethodDef *methods)
  48. Create a new module object based on a name and table of functions,
  49. returning the new module object.
  50. .. versionchanged:: 2.3
  51. Older versions of Python did not support *NULL* as the value for the
  52. *methods* argument.
  53. .. cfunction:: PyObject* Py_InitModule3(char *name, PyMethodDef *methods, char *doc)
  54. Create a new module object based on a name and table of functions,
  55. returning the new module object. If *doc* is non-*NULL*, it will be used
  56. to define the docstring for the module.
  57. .. versionchanged:: 2.3
  58. Older versions of Python did not support *NULL* as the value for the
  59. *methods* argument.
  60. .. cfunction:: PyObject* Py_InitModule4(char *name, PyMethodDef *methods, char *doc, PyObject *self, int apiver)
  61. Create a new module object based on a name and table of functions,
  62. returning the new module object. If *doc* is non-*NULL*, it will be used
  63. to define the docstring for the module. If *self* is non-*NULL*, it will
  64. passed to the functions of the module as their (otherwise *NULL*) first
  65. parameter. (This was added as an experimental feature, and there are no
  66. known uses in the current version of Python.) For *apiver*, the only value
  67. which should be passed is defined by the constant
  68. :const:`PYTHON_API_VERSION`.
  69. .. note::
  70. Most uses of this function should probably be using the
  71. :cfunc:`Py_InitModule3` instead; only use this if you are sure you need
  72. it.
  73. .. versionchanged:: 2.3
  74. Older versions of Python did not support *NULL* as the value for the
  75. *methods* argument.
  76. .. cvar:: PyObject _Py_NoneStruct
  77. Object which is visible in Python as ``None``. This should only be
  78. accessed using the ``Py_None`` macro, which evaluates to a pointer to this
  79. object.