@@ -19,6 +19,13 @@ Allocating Objects on the Heap
1919 not initialized. Specifically, this function does **not ** call the object's
2020 :meth: `~object.__init__ ` method (:c:member: `~PyTypeObject.tp_init ` slot).
2121
22+ .. warning::
23+
24+ This function does not guarantee that the memory will be completely
25+ zeroed before it is initialized. Fields that are not initialized by this
26+ function will have indeterminate values, which might include sensitive
27+ data from previously destroyed objects (e.g., secret keys).
28+
2229
2330.. c:function:: PyVarObject* PyObject_InitVar(PyVarObject *op, PyTypeObject *type, Py_ssize_t size)
2431
@@ -28,23 +35,44 @@ Allocating Objects on the Heap
2835
2936.. c :macro :: PyObject_New(TYPE, typeobj)
3037
31- Calls :c:func: ` PyObject_Malloc ` to allocate memory for a new Python object
32- using the C structure type * TYPE * and the Python type object *typeobj *
33- (`` PyTypeObject* ``), then initializes the memory like
38+ Allocates a new Python object using the C structure type * TYPE * and the
39+ Python type object *typeobj * (`` PyTypeObject* ``) by calling
40+ :c:func:`PyObject_Malloc` to allocate memory and initializing it like
3441 :c:func:`PyObject_Init`. The caller will own the only reference to the
3542 object (i.e. its reference count will be one). The size of the memory
3643 allocation is determined from the :c:member:`~PyTypeObject.tp_basicsize`
3744 field of the type object.
3845
39- This does not call :c:member:`~PyTypeObject.tp_alloc`,
46+ For a type's :c:member:`~PyTypeObject.tp_alloc` slot,
47+ :c:func:`PyType_GenericAlloc` is generally preferred over a custom function
48+ that simply calls this macro.
49+
50+ This macro does not call :c:member:`~PyTypeObject.tp_alloc`,
4051 :c:member:`~PyTypeObject.tp_new` (:meth: `~object.__new__ `), or
4152 :c:member:`~PyTypeObject.tp_init` (:meth: `~object.__init__ `).
4253
43- This should not be used for objects with :c:macro:`Py_TPFLAGS_HAVE_GC` set
44- in :c:member:`~PyTypeObject.tp_flags`; use :c:macro:`PyObject_GC_New`
54+ This macro should not be used for objects with :c:macro:`Py_TPFLAGS_HAVE_GC`
55+ set in :c:member:`~PyTypeObject.tp_flags`; use :c:macro:`PyObject_GC_New`
4556 instead.
4657
47- Memory allocated by this function must be freed with :c:func: `PyObject_Free `.
58+ Memory allocated by this function must be freed with
59+ :c:func: `PyObject_Free `.
60+
61+ .. warning ::
62+
63+ The returned memory is not guaranteed to have been completely zeroed
64+ before it was initialized. Fields that were not initialized by this
65+ function will have indeterminate values, which might include sensitive
66+ data from previously destroyed objects (e.g., secret keys).
67+
68+ .. warning::
69+
70+ This macro does not construct a fully initialized object of the given
71+ type; it merely allocates memory and prepares it for further
72+ initialization by :c:member:`~PyTypeObject.tp_init`. To construct a
73+ fully initialized object, call *typeobj* instead. For example::
74+
75+ PyObject *foo = PyObject_CallNoArgs((PyObject *)&PyFoo_Type);
4876
4977
5078 .. c :macro :: PyObject_NewVar(TYPE, typeobj, size)
@@ -65,7 +93,8 @@ Allocating Objects on the Heap
6593 in :c:member:`~PyTypeObject.tp_flags`; use :c:macro:`PyObject_GC_NewVar`
6694 instead.
6795
68- Memory allocated by this function must be freed with :c:func: `PyObject_Free `.
96+ Memory allocated by this function must be freed with
97+ :c:func: `PyObject_Free `.
6998
7099
71100.. c :function :: void PyObject_Del (void *op)
0 commit comments