summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorPablo Galindo <Pablogsal@gmail.com>2020-01-14 12:06:45 (GMT)
committerGitHub <noreply@github.com>2020-01-14 12:06:45 (GMT)
commita2ec3f07f7f028ff6229d6be2a7cfbda1f4efaeb (patch)
tree2ca30015db1da7016d3f0eff8e253dcfc298452a /Modules
parent1d1b97ae643dd8b22d87785ed7bd2599c6c8dc8d (diff)
downloadcpython-a2ec3f07f7f028ff6229d6be2a7cfbda1f4efaeb.zip
cpython-a2ec3f07f7f028ff6229d6be2a7cfbda1f4efaeb.tar.gz
cpython-a2ec3f07f7f028ff6229d6be2a7cfbda1f4efaeb.tar.bz2
bpo-39322: Add gc.is_finalized to check if an object has been finalised by the gc (GH-17989)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/clinic/gcmodule.c.h11
-rw-r--r--Modules/gcmodule.c20
2 files changed, 30 insertions, 1 deletions
diff --git a/Modules/clinic/gcmodule.c.h b/Modules/clinic/gcmodule.c.h
index 22d2aa4..72795c6 100644
--- a/Modules/clinic/gcmodule.c.h
+++ b/Modules/clinic/gcmodule.c.h
@@ -304,6 +304,15 @@ PyDoc_STRVAR(gc_is_tracked__doc__,
#define GC_IS_TRACKED_METHODDEF \
{"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__},
+PyDoc_STRVAR(gc_is_finalized__doc__,
+"is_finalized($module, obj, /)\n"
+"--\n"
+"\n"
+"Returns true if the object has been already finalized by the GC.");
+
+#define GC_IS_FINALIZED_METHODDEF \
+ {"is_finalized", (PyCFunction)gc_is_finalized, METH_O, gc_is_finalized__doc__},
+
PyDoc_STRVAR(gc_freeze__doc__,
"freeze($module, /)\n"
"--\n"
@@ -373,4 +382,4 @@ gc_get_freeze_count(PyObject *module, PyObject *Py_UNUSED(ignored))
exit:
return return_value;
}
-/*[clinic end generated code: output=e40d384b1f0d513c input=a9049054013a1b77]*/
+/*[clinic end generated code: output=bd6a8056989e2e69 input=a9049054013a1b77]*/
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index 5fef114..4ad9d22 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -1870,6 +1870,25 @@ gc_is_tracked(PyObject *module, PyObject *obj)
}
/*[clinic input]
+gc.is_finalized
+
+ obj: object
+ /
+
+Returns true if the object has been already finalized by the GC.
+[clinic start generated code]*/
+
+static PyObject *
+gc_is_finalized(PyObject *module, PyObject *obj)
+/*[clinic end generated code: output=e1516ac119a918ed input=201d0c58f69ae390]*/
+{
+ if (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(AS_GC(obj))) {
+ Py_RETURN_TRUE;
+ }
+ Py_RETURN_FALSE;
+}
+
+/*[clinic input]
gc.freeze
Freeze all current tracked objects and ignore them for future collections.
@@ -1961,6 +1980,7 @@ static PyMethodDef GcMethods[] = {
GC_GET_OBJECTS_METHODDEF
GC_GET_STATS_METHODDEF
GC_IS_TRACKED_METHODDEF
+ GC_IS_FINALIZED_METHODDEF
{"get_referrers", gc_get_referrers, METH_VARARGS,
gc_get_referrers__doc__},
{"get_referents", gc_get_referents, METH_VARARGS,