Skip to content

bpo-40241: Add PyObject_GC_IsTracked and PyObject_GC_IsFinalized to the public C-API #19461

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Apr 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions Doc/c-api/gcsupport.rst
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,24 @@ Constructors for container types must conform to two rules:
followed by the :c:member:`~PyTypeObject.tp_traverse` handler become valid, usually near the
end of the constructor.

.. c:function:: int PyObject_GC_IsTracked(PyObject *op)

Returns 1 if the object type of *op* implements the GC protocol and *op* is being
currently tracked by the garbage collector and 0 otherwise.

This is analogous to the Python function :func:`gc.is_tracked`.

.. versionadded:: 3.9


.. c:function:: int PyObject_GC_IsFinalized(PyObject *op)

Returns 1 if the object type of *op* implements the GC protocol and *op* has been
already finalized by the garbage collector and 0 otherwise.

This is analogous to the Python function :func:`gc.is_finalized`.

.. versionadded:: 3.9

Similarly, the deallocator for the object must conform to a similar pair of
rules:
Expand Down
7 changes: 7 additions & 0 deletions Doc/whatsnew/3.9.rst
Original file line number Diff line number Diff line change
Expand Up @@ -564,6 +564,13 @@ Build and C API Changes
Windows.
(Contributed by Zackery Spytz in :issue:`8901`.)

* Add the functions :c:func:`PyObject_GC_IsTracked` and
:c:func:`PyObject_GC_IsFinalized` to the public API to allow to query if
Python objects are being currently tracked or have been already finalized by
the garbage collector respectively. (Contributed by Pablo Galindo in
:issue:`40241`.)


Deprecated
==========

Expand Down
2 changes: 2 additions & 0 deletions Include/objimpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,8 @@ PyAPI_FUNC(void) PyObject_GC_Del(void *);
#define PyObject_GC_NewVar(type, typeobj, n) \
( (type *) _PyObject_GC_NewVar((typeobj), (n)) )

PyAPI_FUNC(int) PyObject_GC_IsTracked(PyObject *);
PyAPI_FUNC(int) PyObject_GC_IsFinalized(PyObject *);

/* Utility macro to help write tp_traverse functions.
* To use this macro, the tp_traverse function must name its arguments
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Add the functions :c:func:`PyObject_GC_IsTracked` and
:c:func:`PyObject_GC_IsFinalized` to the public API to allow to query if
Python objects are being currently tracked or have been already finalized by
the garbage collector respectively. Patch by Pablo Galindo.
2 changes: 1 addition & 1 deletion Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3588,7 +3588,7 @@ slot_tp_del(PyObject *self)
_Py_NewReference(self);
Py_SET_REFCNT(self, refcnt);
}
assert(!PyType_IS_GC(Py_TYPE(self)) || _PyObject_GC_IS_TRACKED(self));
assert(!PyType_IS_GC(Py_TYPE(self)) || PyObject_GC_IsTracked(self));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for this change :-)

/* If Py_REF_DEBUG macro is defined, _Py_NewReference() increased
_Py_RefTotal, so we need to undo that. */
#ifdef Py_REF_DEBUG
Expand Down
18 changes: 18 additions & 0 deletions Modules/gcmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2312,3 +2312,21 @@ PyObject_GC_Del(void *op)
}
PyObject_FREE(g);
}

int
PyObject_GC_IsTracked(PyObject* obj)
{
if (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)) {
return 1;
}
return 0;
}

int
PyObject_GC_IsFinalized(PyObject *obj)
{
if (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
return 1;
}
return 0;
}