diff options
author | Barry Warsaw <barry@python.org> | 2006-03-07 09:46:03 (GMT) |
---|---|---|
committer | Barry Warsaw <barry@python.org> | 2006-03-07 09:46:03 (GMT) |
commit | d3c38ff7f832f60830532524e07a5e11be749d7e (patch) | |
tree | c1bb3343d60f4c646a8933057b19206040c2b633 /Modules/gcmodule.c | |
parent | 995acdf308919b61dd94170925e511a926279dbc (diff) | |
download | cpython-d3c38ff7f832f60830532524e07a5e11be749d7e.zip cpython-d3c38ff7f832f60830532524e07a5e11be749d7e.tar.gz cpython-d3c38ff7f832f60830532524e07a5e11be749d7e.tar.bz2 |
SF patch #1443865; gc.get_count() added and optional argument 'generation'
added to gc.collect(). Updated docs, unit test, and NEWS entry.
(Also, fixed a typo in NEWS.)
Diffstat (limited to 'Modules/gcmodule.c')
-rw-r--r-- | Modules/gcmodule.c | 39 |
1 files changed, 34 insertions, 5 deletions
diff --git a/Modules/gcmodule.c b/Modules/gcmodule.c index a3655d6..7e3f95a 100644 --- a/Modules/gcmodule.c +++ b/Modules/gcmodule.c @@ -925,20 +925,33 @@ gc_isenabled(PyObject *self, PyObject *noargs) } PyDoc_STRVAR(gc_collect__doc__, -"collect() -> n\n" +"collect([generation]) -> n\n" "\n" -"Run a full collection. The number of unreachable objects is returned.\n"); +"With no arguments, run a full collection. The optional argument\n" +"may be an integer specifying which generation to collect. A ValueError\n" +"is raised if the generation number is invalid.\n\n" +"The number of unreachable objects is returned.\n"); static PyObject * -gc_collect(PyObject *self, PyObject *noargs) +gc_collect(PyObject *self, PyObject *args, PyObject *kws) { + static char *keywords[] = {"generation", NULL}; + int genarg = NUM_GENERATIONS - 1; Py_ssize_t n; + if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg)) + return NULL; + + else if (genarg < 0 || genarg >= NUM_GENERATIONS) { + PyErr_SetString(PyExc_ValueError, "invalid generation"); + return NULL; + } + if (collecting) n = 0; /* already collecting, don't do anything */ else { collecting = 1; - n = collect(NUM_GENERATIONS - 1); + n = collect(genarg); collecting = 0; } @@ -1020,6 +1033,20 @@ gc_get_thresh(PyObject *self, PyObject *noargs) generations[2].threshold); } +PyDoc_STRVAR(gc_get_count__doc__, +"get_count() -> (count0, count1, count2)\n" +"\n" +"Return the current collection counts\n"); + +static PyObject * +gc_get_count(PyObject *self, PyObject *noargs) +{ + return Py_BuildValue("(iii)", + generations[0].count, + generations[1].count, + generations[2].count); +} + static int referrersvisit(PyObject* obj, PyObject *objs) { @@ -1150,9 +1177,11 @@ static PyMethodDef GcMethods[] = { {"isenabled", gc_isenabled, METH_NOARGS, gc_isenabled__doc__}, {"set_debug", gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, {"get_debug", gc_get_debug, METH_NOARGS, gc_get_debug__doc__}, + {"get_count", gc_get_count, METH_NOARGS, gc_get_count__doc__}, {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__}, - {"collect", gc_collect, METH_NOARGS, gc_collect__doc__}, + {"collect", (PyCFunction)gc_collect, + METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, {"get_referrers", gc_get_referrers, METH_VARARGS, gc_get_referrers__doc__}, |