Skip to content

Commit 9cd5427

Browse files
gh-141004: Document PyType_SUPPORTS_WEAKREFS (GH-141408)
Co-authored-by: Stan Ulbrych <[email protected]>
1 parent f1330b3 commit 9cd5427

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

Doc/c-api/type.rst

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,12 +195,14 @@ Type Objects
195195
before initialization) and should be paired with :c:func:`PyObject_Free` in
196196
:c:member:`~PyTypeObject.tp_free`.
197197
198+
198199
.. c:function:: PyObject* PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
199200
200201
Generic handler for the :c:member:`~PyTypeObject.tp_new` slot of a type
201202
object. Creates a new instance using the type's
202203
:c:member:`~PyTypeObject.tp_alloc` slot and returns the resulting object.
203204
205+
204206
.. c:function:: int PyType_Ready(PyTypeObject *type)
205207
206208
Finalize a type object. This should be called on all type objects to finish
@@ -217,13 +219,15 @@ Type Objects
217219
GC protocol itself by at least implementing the
218220
:c:member:`~PyTypeObject.tp_traverse` handle.
219221
222+
220223
.. c:function:: PyObject* PyType_GetName(PyTypeObject *type)
221224
222225
Return the type's name. Equivalent to getting the type's
223226
:attr:`~type.__name__` attribute.
224227
225228
.. versionadded:: 3.11
226229
230+
227231
.. c:function:: PyObject* PyType_GetQualName(PyTypeObject *type)
228232
229233
Return the type's qualified name. Equivalent to getting the
@@ -239,13 +243,15 @@ Type Objects
239243
240244
.. versionadded:: 3.13
241245
246+
242247
.. c:function:: PyObject* PyType_GetModuleName(PyTypeObject *type)
243248
244249
Return the type's module name. Equivalent to getting the
245250
:attr:`type.__module__` attribute.
246251
247252
.. versionadded:: 3.13
248253
254+
249255
.. c:function:: void* PyType_GetSlot(PyTypeObject *type, int slot)
250256
251257
Return the function pointer stored in the given slot. If the
@@ -262,6 +268,7 @@ Type Objects
262268
:c:func:`PyType_GetSlot` can now accept all types.
263269
Previously, it was limited to :ref:`heap types <heap-types>`.
264270
271+
265272
.. c:function:: PyObject* PyType_GetModule(PyTypeObject *type)
266273
267274
Return the module object associated with the given type when the type was
@@ -281,6 +288,7 @@ Type Objects
281288
282289
.. versionadded:: 3.9
283290
291+
284292
.. c:function:: void* PyType_GetModuleState(PyTypeObject *type)
285293
286294
Return the state of the module object associated with the given type.
@@ -295,6 +303,7 @@ Type Objects
295303
296304
.. versionadded:: 3.9
297305
306+
298307
.. c:function:: PyObject* PyType_GetModuleByDef(PyTypeObject *type, struct PyModuleDef *def)
299308
300309
Find the first superclass whose module was created from
@@ -314,6 +323,7 @@ Type Objects
314323
315324
.. versionadded:: 3.11
316325
326+
317327
.. c:function:: int PyType_GetBaseByToken(PyTypeObject *type, void *token, PyTypeObject **result)
318328
319329
Find the first superclass in *type*'s :term:`method resolution order` whose
@@ -332,6 +342,7 @@ Type Objects
332342
333343
.. versionadded:: 3.14
334344
345+
335346
.. c:function:: int PyUnstable_Type_AssignVersionTag(PyTypeObject *type)
336347
337348
Attempt to assign a version tag to the given type.
@@ -342,6 +353,16 @@ Type Objects
342353
.. versionadded:: 3.12
343354
344355
356+
.. c:function:: int PyType_SUPPORTS_WEAKREFS(PyTypeObject *type)
357+
358+
Return true if instances of *type* support creating weak references, false
359+
otherwise. This function always succeeds. *type* must not be ``NULL``.
360+
361+
.. seealso::
362+
* :ref:`weakrefobjects`
363+
* :py:mod:`weakref`
364+
365+
345366
Creating Heap-Allocated Types
346367
.............................
347368
@@ -390,6 +411,7 @@ The following functions and structs are used to create
390411
391412
.. versionadded:: 3.12
392413
414+
393415
.. c:function:: PyObject* PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
394416
395417
Equivalent to ``PyType_FromMetaclass(NULL, module, spec, bases)``.
@@ -416,6 +438,7 @@ The following functions and structs are used to create
416438
Creating classes whose metaclass overrides
417439
:c:member:`~PyTypeObject.tp_new` is no longer allowed.
418440
441+
419442
.. c:function:: PyObject* PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
420443
421444
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, bases)``.
@@ -437,6 +460,7 @@ The following functions and structs are used to create
437460
Creating classes whose metaclass overrides
438461
:c:member:`~PyTypeObject.tp_new` is no longer allowed.
439462
463+
440464
.. c:function:: PyObject* PyType_FromSpec(PyType_Spec *spec)
441465
442466
Equivalent to ``PyType_FromMetaclass(NULL, NULL, spec, NULL)``.
@@ -457,6 +481,7 @@ The following functions and structs are used to create
457481
Creating classes whose metaclass overrides
458482
:c:member:`~PyTypeObject.tp_new` is no longer allowed.
459483
484+
460485
.. c:function:: int PyType_Freeze(PyTypeObject *type)
461486
462487
Make a type immutable: set the :c:macro:`Py_TPFLAGS_IMMUTABLETYPE` flag.
@@ -628,6 +653,7 @@ The following functions and structs are used to create
628653
* :c:data:`Py_tp_token` (for clarity, prefer :c:data:`Py_TP_USE_SPEC`
629654
rather than ``NULL``)
630655
656+
631657
.. c:macro:: Py_tp_token
632658
633659
A :c:member:`~PyType_Slot.slot` that records a static memory layout ID

Doc/c-api/weakref.rst

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,10 @@ as much as it can.
4545
weakly referenceable object, or if *callback* is not callable, ``None``, or
4646
``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
4747
48+
.. seealso::
49+
:c:func:`PyType_SUPPORTS_WEAKREFS` for checking if *ob* is weakly
50+
referenceable.
51+
4852
4953
.. c:function:: PyObject* PyWeakref_NewProxy(PyObject *ob, PyObject *callback)
5054
@@ -57,6 +61,10 @@ as much as it can.
5761
is not a weakly referenceable object, or if *callback* is not callable,
5862
``None``, or ``NULL``, this will return ``NULL`` and raise :exc:`TypeError`.
5963
64+
.. seealso::
65+
:c:func:`PyType_SUPPORTS_WEAKREFS` for checking if *ob* is weakly
66+
referenceable.
67+
6068
6169
.. c:function:: int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
6270

0 commit comments

Comments
 (0)