File tree Expand file tree Collapse file tree 6 files changed +54
-1
lines changed Expand file tree Collapse file tree 6 files changed +54
-1
lines changed Original file line number Diff line number Diff line change @@ -60,6 +60,26 @@ 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 *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
63
83
64
84
Similarly, the deallocator for the object must conform to a similar pair of
65
85
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 (void * );
190
+ PyAPI_FUNC (int ) PyObject_GC_IsFinalized (void * );
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,23 @@ PyObject_GC_Del(void *op)
2312
2312
}
2313
2313
PyObject_FREE (g );
2314
2314
}
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
+ }
You can’t perform that action at this time.
0 commit comments