2019-05-17 11:55:34 +02:00
|
|
|
.. highlight:: c
|
2008-01-20 09:30:57 +00:00
|
|
|
|
|
|
|
|
.. _allocating-objects:
|
|
|
|
|
|
|
|
|
|
Allocating Objects on the Heap
|
|
|
|
|
==============================
|
|
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyObject* _PyObject_New(PyTypeObject *type)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyVarObject* _PyObject_NewVar(PyTypeObject *type, Py_ssize_t size)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyObject* PyObject_Init(PyObject *op, PyTypeObject *type)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2022-07-05 05:16:10 -04:00
|
|
|
Initialize a newly allocated object *op* with its type and initial
|
2024-11-05 11:01:16 +01:00
|
|
|
reference. Returns the initialized object. Other fields of the object are
|
|
|
|
|
not affected.
|
2008-01-20 09:30:57 +00:00
|
|
|
|
|
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
This does everything :c:func:`PyObject_Init` does, and also initializes the
|
2008-01-20 09:30:57 +00:00
|
|
|
length information for a variable-size object.
|
|
|
|
|
|
|
|
|
|
|
2023-07-27 02:52:40 +02:00
|
|
|
.. c:macro:: PyObject_New(TYPE, typeobj)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2023-08-07 15:40:59 -06:00
|
|
|
Allocate a new Python object using the C structure type *TYPE*
|
|
|
|
|
and the Python type object *typeobj* (``PyTypeObject*``).
|
|
|
|
|
Fields not defined by the Python object header are not initialized.
|
|
|
|
|
The caller will own the only reference to the object
|
|
|
|
|
(i.e. its reference count will be one).
|
|
|
|
|
The size of the memory allocation is determined from the
|
|
|
|
|
:c:member:`~PyTypeObject.tp_basicsize` field of the type object.
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2025-03-17 16:18:26 +01:00
|
|
|
Note that this function is unsuitable if *typeobj* has
|
|
|
|
|
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
|
|
|
|
|
use :c:func:`PyObject_GC_New` instead.
|
|
|
|
|
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2023-07-27 02:52:40 +02:00
|
|
|
.. c:macro:: PyObject_NewVar(TYPE, typeobj, size)
|
2008-01-20 09:30:57 +00:00
|
|
|
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 21:06:15 +00:00
|
|
|
Allocate a new Python object using the C structure type *TYPE* and the
|
2023-07-27 02:52:40 +02:00
|
|
|
Python type object *typeobj* (``PyTypeObject*``).
|
|
|
|
|
Fields not defined by the Python object header
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 21:06:15 +00:00
|
|
|
are not initialized. The allocated memory allows for the *TYPE* structure
|
2023-07-27 02:52:40 +02:00
|
|
|
plus *size* (``Py_ssize_t``) fields of the size
|
|
|
|
|
given by the :c:member:`~PyTypeObject.tp_itemsize` field of
|
|
|
|
|
*typeobj*. This is useful for implementing objects like tuples, which are
|
Merged revisions 71898-71900,71910,71914-71919 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r71898 | jeroen.ruigrok | 2009-04-25 16:24:30 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71899 | jeroen.ruigrok | 2009-04-25 16:27:00 +0200 (za, 25 apr 2009) | 3 lines
The type for ppos has been Py_ssize_t since 2.5, reflect this in the
documentation.
........
r71900 | jeroen.ruigrok | 2009-04-25 16:28:02 +0200 (za, 25 apr 2009) | 2 lines
Reformat paragraph.
........
r71910 | jeroen.ruigrok | 2009-04-25 19:59:03 +0200 (za, 25 apr 2009) | 4 lines
Issue #4129: Belatedly document which C API functions had their argument(s) or
return type changed from int or int * to Py_ssize_t or Py_ssize_t * as this
might cause problems on 64-bit platforms.
........
r71914 | jeroen.ruigrok | 2009-04-25 20:31:20 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71915 | jeroen.ruigrok | 2009-04-25 20:46:03 +0200 (za, 25 apr 2009) | 2 lines
Issue #4129: Document more int -> Py_ssize_t changes.
........
r71916 | jeroen.ruigrok | 2009-04-25 20:53:48 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
r71917 | jeroen.ruigrok | 2009-04-25 20:57:32 +0200 (za, 25 apr 2009) | 2 lines
Reference to an int type, whereas it's a Py_ssize_t as the synopsis states.
........
r71918 | jeroen.ruigrok | 2009-04-25 21:04:15 +0200 (za, 25 apr 2009) | 2 lines
Since I edited this file, reformat for future edits.
........
r71919 | jeroen.ruigrok | 2009-04-25 21:10:52 +0200 (za, 25 apr 2009) | 2 lines
Reformat prior to editing.
........
2009-04-26 21:06:15 +00:00
|
|
|
able to determine their size at construction time. Embedding the array of
|
|
|
|
|
fields into the same allocation decreases the number of allocations,
|
|
|
|
|
improving the memory management efficiency.
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2025-03-17 16:18:26 +01:00
|
|
|
Note that this function is unsuitable if *typeobj* has
|
|
|
|
|
:c:macro:`Py_TPFLAGS_HAVE_GC` set. For such objects,
|
|
|
|
|
use :c:func:`PyObject_GC_NewVar` instead.
|
|
|
|
|
|
2008-01-20 09:30:57 +00:00
|
|
|
|
2010-10-06 10:11:56 +00:00
|
|
|
.. c:var:: PyObject _Py_NoneStruct
|
2008-01-20 09:30:57 +00:00
|
|
|
|
|
|
|
|
Object which is visible in Python as ``None``. This should only be accessed
|
2010-10-06 10:11:56 +00:00
|
|
|
using the :c:macro:`Py_None` macro, which evaluates to a pointer to this
|
2008-01-20 09:30:57 +00:00
|
|
|
object.
|
2009-01-04 23:20:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
|
|
.. seealso::
|
|
|
|
|
|
2025-05-26 23:50:19 +02:00
|
|
|
:ref:`moduleobjects`
|
2009-01-04 23:20:14 +00:00
|
|
|
To allocate and create extension modules.
|
|
|
|
|
|
2025-11-09 10:37:13 -05:00
|
|
|
|
|
|
|
|
Deprecated aliases
|
|
|
|
|
^^^^^^^^^^^^^^^^^^
|
|
|
|
|
|
|
|
|
|
These are :term:`soft deprecated` aliases to existing functions and macros.
|
|
|
|
|
They exist solely for backwards compatibility.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
.. list-table::
|
|
|
|
|
:widths: auto
|
|
|
|
|
:header-rows: 1
|
|
|
|
|
|
|
|
|
|
* * Deprecated alias
|
|
|
|
|
* Function
|
|
|
|
|
* * .. c:macro:: PyObject_NEW(type, typeobj)
|
|
|
|
|
* :c:macro:`PyObject_New`
|
|
|
|
|
* * .. c:macro:: PyObject_NEW_VAR(type, typeobj, n)
|
|
|
|
|
* :c:macro:`PyObject_NewVar`
|
|
|
|
|
* * .. c:macro:: PyObject_INIT(op, typeobj)
|
|
|
|
|
* :c:func:`PyObject_Init`
|
|
|
|
|
* * .. c:macro:: PyObject_INIT_VAR(op, typeobj, n)
|
|
|
|
|
* :c:func:`PyObject_InitVar`
|
|
|
|
|
* * .. c:macro:: PyObject_MALLOC(n)
|
|
|
|
|
* :c:func:`PyObject_Malloc`
|
|
|
|
|
* * .. c:macro:: PyObject_REALLOC(p, n)
|
|
|
|
|
* :c:func:`PyObject_Realloc`
|
|
|
|
|
* * .. c:macro:: PyObject_FREE(p)
|
|
|
|
|
* :c:func:`PyObject_Free`
|
|
|
|
|
* * .. c:macro:: PyObject_DEL(p)
|
|
|
|
|
* :c:func:`PyObject_Free`
|
|
|
|
|
* * .. c:macro:: PyObject_Del(p)
|
|
|
|
|
* :c:func:`PyObject_Free`
|