summaryrefslogtreecommitdiffstats
path: root/Modules/gcmodule.c
diff options
context:
space:
mode:
authorJeremy Hylton <jeremy@alum.mit.edu>2003-04-03 16:28:38 (GMT)
committerJeremy Hylton <jeremy@alum.mit.edu>2003-04-03 16:28:38 (GMT)
commit5bd378bfcaeff7b4373f6f9abbe8c53b0a333b7b (patch)
tree7f8bb828aa3f52c36661fd378759bca084a1ae26 /Modules/gcmodule.c
parent7fb697b5d26b1c9a379978653db97a84d7270141 (diff)
downloadcpython-5bd378bfcaeff7b4373f6f9abbe8c53b0a333b7b.zip
cpython-5bd378bfcaeff7b4373f6f9abbe8c53b0a333b7b.tar.gz
cpython-5bd378bfcaeff7b4373f6f9abbe8c53b0a333b7b.tar.bz2
Add get_referrents() helper function.
Diffstat (limited to 'Modules/gcmodule.c')
-rw-r--r--Modules/gcmodule.c33
1 files changed, 32 insertions, 1 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c
index c0c091e..fc834f2 100644
--- a/Modules/gcmodule.c
+++ b/Modules/gcmodule.c
@@ -828,6 +828,34 @@ gc_get_referrers(PyObject *self, PyObject *args)
return result;
}
+static int
+referrentsvisit(PyObject *obj, PyObject *list)
+{
+ if (PyList_Append(list, obj) < 0)
+ return 1;
+ return 0;
+}
+
+PyDoc_STRVAR(gc_get_referrents__doc__,
+"get_referrents(*objs) -> list\n\
+Return the list of objects that directly refer to any of objs.");
+
+static PyObject *
+gc_get_referrents(PyObject *self, PyObject *args)
+{
+ int i;
+ PyObject *result = PyList_New(0);
+ for (i = 0; i < PyTuple_GET_SIZE(args); i++) {
+ PyObject *obj = PyTuple_GET_ITEM(args, i);
+ traverseproc traverse = obj->ob_type->tp_traverse;
+ if (!traverse)
+ continue;
+ if (traverse(obj, (visitproc)referrentsvisit, result))
+ return NULL;
+ }
+ return result;
+}
+
PyDoc_STRVAR(gc_get_objects__doc__,
"get_objects() -> [...]\n"
"\n"
@@ -884,7 +912,8 @@ PyDoc_STRVAR(gc__doc__,
"set_threshold() -- Set the collection thresholds.\n"
"get_threshold() -- Return the current the collection thresholds.\n"
"get_objects() -- Return a list of all objects tracked by the collector.\n"
-"get_referrers() -- Return the list of objects that refer to an object.\n");
+"get_referrers() -- Return the list of objects that refer to an object.\n"
+"get_referrents() -- Return the list of objects that an object refers to.\n");
static PyMethodDef GcMethods[] = {
{"enable", gc_enable, METH_VARARGS, gc_enable__doc__},
@@ -898,6 +927,8 @@ static PyMethodDef GcMethods[] = {
{"get_objects", gc_get_objects,METH_VARARGS, gc_get_objects__doc__},
{"get_referrers", gc_get_referrers, METH_VARARGS,
gc_get_referrers__doc__},
+ {"get_referrents", gc_get_referrents, METH_VARARGS,
+ gc_get_referrents__doc__},
{NULL, NULL} /* Sentinel */
};