File tree Expand file tree Collapse file tree 6 files changed +50
-1
lines changed Expand file tree Collapse file tree 6 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,24 @@ Constructors for container types must conform to two rules:
60
60
followed by the :c:member: `~PyTypeObject.tp_traverse ` handler become valid, usually near the
61
61
end of the constructor.
62
62
63
+ .. c :function :: int PyObject_GC_IsTracked (PyObject *op)
64
+
65
+ Returns 1 if the object type of *op * implements the GC protocol and *op * is being
66
+ currently tracked by the garbage collector and 0 otherwise.
67
+
68
+ This is analogous to the Python function :func: `gc.is_tracked `.
69
+
70
+ .. versionadded :: 3.9
71
+
72
+
73
+ .. c :function :: int PyObject_GC_IsFinalized (PyObject *op)
74
+
75
+ Returns 1 if the object type of *op * implements the GC protocol and *op * has been
76
+ already finalized by the garbage collector and 0 otherwise.
77
+
78
+ This is analogous to the Python function :func: `gc.is_finalized `.
79
+
80
+ .. versionadded :: 3.9
63
81
64
82
Similarly, the deallocator for the object must conform to a similar pair of
65
83
rules:
Original file line number Diff line number Diff line change @@ -564,6 +564,13 @@ Build and C API Changes
564
564
Windows.
565
565
(Contributed by Zackery Spytz in :issue: `8901 `.)
566
566
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
+
567
574
Deprecated
568
575
==========
569
576
Original file line number Diff line number Diff line change @@ -186,6 +186,8 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
186
186
#define PyObject_GC_NewVar (type , typeobj , n ) \
187
187
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )
188
188
189
+ PyAPI_FUNC (int ) PyObject_GC_IsTracked (PyObject * );
190
+ PyAPI_FUNC (int ) PyObject_GC_IsFinalized (PyObject * );
189
191
190
192
/* Utility macro to help write tp_traverse functions.
191
193
* To use this macro, the tp_traverse function must name its arguments
Original file line number Diff line number Diff line change
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.
Original file line number Diff line number Diff line change @@ -3588,7 +3588,7 @@ slot_tp_del(PyObject *self)
3588
3588
_Py_NewReference (self );
3589
3589
Py_SET_REFCNT (self , refcnt );
3590
3590
}
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 ));
3592
3592
/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
3593
3593
_Py_RefTotal, so we need to undo that. */
3594
3594
#ifdef Py_REF_DEBUG
Original file line number Diff line number Diff line change @@ -2312,3 +2312,21 @@ PyObject_GC_Del(void *op)
2312
2312
}
2313
2313
PyObject_FREE (g );
2314
2314
}
2315
+
2316
+ int
2317
+ PyObject_GC_IsTracked (PyObject * obj )
2318
+ {
2319
+ if (PyObject_IS_GC (obj ) && _PyObject_GC_IS_TRACKED (obj )) {
2320
+ return 1 ;
2321
+ }
2322
+ return 0 ;
2323
+ }
2324
+
2325
+ int
2326
+ PyObject_GC_IsFinalized (PyObject * obj )
2327
+ {
2328
+ if (PyObject_IS_GC (obj ) && _PyGCHead_FINALIZED (AS_GC (obj ))) {
2329
+ return 1 ;
2330
+ }
2331
+ return 0 ;
2332
+ }
You can’t perform that action at this time.
0 commit comments