Skip to content

Commit 6a6d61c

Browse files
committed
bpo-40241: Add PyObject_GC_IsTracked and PyObject_GC_IsFinalized to the public C-API
1 parent 0361556 commit 6a6d61c

File tree

6 files changed

+54
-1
lines changed

6 files changed

+54
-1
lines changed

Doc/c-api/gcsupport.rst

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,26 @@ Constructors for container types must conform to two rules:
6060
followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
6161
end of the constructor.
6262
63+
.. c:function:: int PyObject_GC_IsTracked(PyObject *op)
64+
65+
Returns 1 if the object *op* is being currently tracked by the garbage
66+
collector and 0 otherwise.
67+
68+
This function will return 0 for objects that do not have implemented support
69+
for the garbage collector.
70+
71+
.. versionadded:: 3.9
72+
73+
74+
.. c:function:: int PyObject_GC_IsFinalized(PyObject *op)
75+
76+
Returns 1 if the object *op* has been already finalized by the garbage
77+
collector and 0 otherwise.
78+
79+
This function will return 0 for objects that do not have implemented support
80+
for the garbage collector.
81+
82+
.. versionadded:: 3.9
6383
6484
Similarly, the deallocator for the object must conform to a similar pair of
6585
rules:

Doc/whatsnew/3.9.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -564,6 +564,13 @@ Build and C API Changes
564564
Windows.
565565
(Contributed by Zackery Spytz in :issue:`8901`.)
566566

567+
* Add the functions :c:func:`PyObject_GC_IsTracked` and
568+
:c:func:`PyObject_GC_IsFinalized` to the public API to allow to query if
569+
Python objects are being currently tracked or have been already finalized by
570+
the garbage collector respectively. (Contributed by Pablo Galindo in
571+
:issue:`40241`.)
572+
573+
567574
Deprecated
568575
==========
569576

Include/objimpl.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,8 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
186186
#define PyObject_GC_NewVar(type, typeobj, n) \
187187
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
188188

189+
PyAPI_FUNC(int) PyObject_GC_IsTracked(void *);
190+
PyAPI_FUNC(int) PyObject_GC_IsFinalized(void *);
189191

190192
/* Utility macro to help write tp_traverse functions.
191193
* To use this macro, the tp_traverse function must name its arguments
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Add the functions :c:func:`PyObject_GC_IsTracked` and
2+
:c:func:`PyObject_GC_IsFinalized` to the public API to allow to query if
3+
Python objects are being currently tracked or have been already finalized by
4+
the garbage collector respectively. Patch by Pablo Galindo.

Modules/_testcapimodule.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3588,7 +3588,7 @@ slot_tp_del(PyObject *self)
35883588
_Py_NewReference(self);
35893589
Py_SET_REFCNT(self, refcnt);
35903590
}
3591-
assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
3591+
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
35923592
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
35933593
_Py_RefTotal, so we need to undo that. */
35943594
#ifdef Py_REF_DEBUG

Modules/gcmodule.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2312,3 +2312,23 @@ PyObject_GC_Del(void *op)
23122312
}
23132313
PyObject_FREE(g);
23142314
}
2315+
2316+
int
2317+
PyObject_GC_IsTracked(void* op_raw)
2318+
{
2319+
PyObject *obj = _PyObject_CAST(op_raw);
2320+
if (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
2321+
return 1;
2322+
}
2323+
return 0;
2324+
}
2325+
2326+
int
2327+
PyObject_GC_IsFinalized(void *op_raw)
2328+
{
2329+
PyObject *obj = _PyObject_CAST(op_raw);
2330+
if (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
2331+
return 1;
2332+
}
2333+
return 0;
2334+
}

0 commit comments

Comments
 (0)