From f20972630c4dc0284e4ec9b2f50ce37dac9ad5ba Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 10:32:57 -0500 Subject: [PATCH 1/4] Document PyType_SUPPORTS_WEAKREFS() --- Doc/c-api/type.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst index 479ede70b01f5d..330b3f9ab5ffd4 100644 --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -327,6 +327,14 @@ Type Objects .. versionadded:: 3.12 +.. c:function:: int PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) + + Return true if instances of *type* supports creating weak references, false + otherwise. This function always succeeds. *type* must not be ``NULL``. + + .. seealso:: + * :ref:`weakrefobjects` + * :py:mod:`weakref` Creating Heap-Allocated Types ............................. From 6ef3a9611304f19207584ab9f1c7d616940b8259 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 10:34:03 -0500 Subject: [PATCH 2/4] Add seealso note to PyWeakref_NewRef() and PyWeakref_NewProxy() docs. --- Doc/c-api/weakref.rst | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/Doc/c-api/weakref.rst b/Doc/c-api/weakref.rst index 39e4febd3ef0f2..db6ae0a9d4ea3d 100644 --- a/Doc/c-api/weakref.rst +++ b/Doc/c-api/weakref.rst @@ -45,6 +45,10 @@ as much as it can. weakly referenceable object, or if *callback* is not callable, ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. + .. seealso:: + :c:func:`PyType_SUPPORTS_WEAKREFS` for checking if *ob* is weakly + referenceable. + .. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback) @@ -57,6 +61,10 @@ as much as it can. is not a weakly referenceable object, or if *callback* is not callable, ``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`. + .. seealso:: + :c:func:`PyType_SUPPORTS_WEAKREFS` for checking if *ob* is weakly + referenceable. + .. c:function:: int PyWeakref_GetRef(PyObject *ref, PyObject **pobj) From da0a26571ce35c04b3402088c24d119982823fbe Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 11:54:09 -0500 Subject: [PATCH 3/4] Update type.rst Co-authored-by: Stan Ulbrych <89152624+StanFromIreland@users.noreply.github.com> --- Doc/c-api/type.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst index 330b3f9ab5ffd4..175897f141c0a3 100644 --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -329,7 +329,7 @@ Type Objects .. c:function:: int PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) - Return true if instances of *type* supports creating weak references, false + Return true if instances of *type* support creating weak references, false otherwise. This function always succeeds. *type* must not be ``NULL``. .. seealso:: From dc405c23acbb663b2588b35d701097ccd814a921 Mon Sep 17 00:00:00 2001 From: Peter Bierma Date: Tue, 11 Nov 2025 12:25:17 -0500 Subject: [PATCH 4/4] Ensure there are two lines between each definition. --- Doc/c-api/type.rst | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/Doc/c-api/type.rst b/Doc/c-api/type.rst index 330b3f9ab5ffd4..a6d3be1d45d3cf 100644 --- a/Doc/c-api/type.rst +++ b/Doc/c-api/type.rst @@ -181,12 +181,14 @@ Type Objects before initialization) and should be paired with :c:func:`PyObject_Free` in :c:member:`~PyTypeObject.tp_free`. + .. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) Generic handler for the :c:member:`~PyTypeObject.tp_new` slot of a type object. Creates a new instance using the type's :c:member:`~PyTypeObject.tp_alloc` slot and returns the resulting object. + .. c:function:: int PyType_Ready(PyTypeObject *type) Finalize a type object. This should be called on all type objects to finish @@ -203,6 +205,7 @@ Type Objects GC protocol itself by at least implementing the :c:member:`~PyTypeObject.tp_traverse` handle. + .. c:function:: PyObject* PyType_GetName(PyTypeObject *type) Return the type's name. Equivalent to getting the type's @@ -210,6 +213,7 @@ Type Objects .. versionadded:: 3.11 + .. c:function:: PyObject* PyType_GetQualName(PyTypeObject *type) Return the type's qualified name. Equivalent to getting the @@ -225,6 +229,7 @@ Type Objects .. versionadded:: 3.13 + .. c:function:: PyObject* PyType_GetModuleName(PyTypeObject *type) Return the type's module name. Equivalent to getting the @@ -232,6 +237,7 @@ Type Objects .. versionadded:: 3.13 + .. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot) Return the function pointer stored in the given slot. If the @@ -248,6 +254,7 @@ Type Objects :c:func:`PyType_GetSlot` can now accept all types. Previously, it was limited to :ref:`heap types `. + .. c:function:: PyObject* PyType_GetModule(PyTypeObject *type) Return the module object associated with the given type when the type was @@ -267,6 +274,7 @@ Type Objects .. versionadded:: 3.9 + .. c:function:: void* PyType_GetModuleState(PyTypeObject *type) Return the state of the module object associated with the given type. @@ -281,6 +289,7 @@ Type Objects .. versionadded:: 3.9 + .. c:function:: PyObject* PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def) Find the first superclass whose module was created from @@ -300,6 +309,7 @@ Type Objects .. versionadded:: 3.11 + .. c:function:: int PyType_GetBaseByToken(PyTypeObject *type, void *token, PyTypeObject **result) Find the first superclass in *type*'s :term:`method resolution order` whose @@ -318,6 +328,7 @@ Type Objects .. versionadded:: 3.14 + .. c:function:: int PyUnstable_Type_AssignVersionTag(PyTypeObject *type) Attempt to assign a version tag to the given type. @@ -327,6 +338,7 @@ Type Objects .. versionadded:: 3.12 + .. c:function:: int PyType_SUPPORTS_WEAKREFS(PyTypeObject *type) Return true if instances of *type* supports creating weak references, false @@ -336,6 +348,7 @@ Type Objects * :ref:`weakrefobjects` * :py:mod:`weakref` + Creating Heap-Allocated Types ............................. @@ -384,6 +397,7 @@ The following functions and structs are used to create .. versionadded:: 3.12 + .. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases) Equivalent to ``PyType_FromMetaclass(NULL, module, spec, bases)``. @@ -410,6 +424,7 @@ The following functions and structs are used to create Creating classes whose metaclass overrides :c:member:`~PyTypeObject.tp_new` is no longer allowed. + .. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases) Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, bases)``. @@ -431,6 +446,7 @@ The following functions and structs are used to create Creating classes whose metaclass overrides :c:member:`~PyTypeObject.tp_new` is no longer allowed. + .. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec) Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, NULL)``. @@ -451,6 +467,7 @@ The following functions and structs are used to create Creating classes whose metaclass overrides :c:member:`~PyTypeObject.tp_new` is no longer allowed. + .. c:function:: int PyType_Freeze(PyTypeObject *type) Make a type immutable: set the :c:macro:`Py_TPFLAGS_IMMUTABLETYPE` flag. @@ -622,6 +639,7 @@ The following functions and structs are used to create * :c:data:`Py_tp_token` (for clarity, prefer :c:data:`Py_TP_USE_SPEC` rather than ``NULL``) + .. c:macro:: Py_tp_token A :c:member:`~PyType_Slot.slot` that records a static memory layout ID