diff -r b541ecd32115 Modules/gcmodule.c --- a/Modules/gcmodule.c Fri Feb 07 16:11:17 2014 -0800 +++ b/Modules/gcmodule.c Sat Feb 08 16:25:21 2014 +0800 @@ -26,6 +26,11 @@ #include "Python.h" #include "frameobject.h" /* for PyFrame_ClearFreeList */ +/*[clinic input] +module gc +[clinic start generated code]*/ +/*[clinic end generated code: output=da39a3ee5e6b4b0d input=b5c9690ecc842d79]*/ + /* Get an object's GC head */ #define AS_GC(o) ((PyGC_Head *)(o)-1) @@ -41,6 +46,8 @@ generations */ }; +/* If we change this, we need to change the default value in the signature of + gc.collect. */ #define NUM_GENERATIONS 3 #define GEN_HEAD(n) (&generations[n].head) @@ -1155,110 +1162,275 @@ return n; } +/*[clinic input] +gc.enable + +Enable automatic garbage collection. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_enable__doc__, -"enable() -> None\n" -"\n" -"Enable automatic garbage collection.\n"); +"sig=($module)\n" +"Enable automatic garbage collection."); + +#define GC_ENABLE_METHODDEF \ + {"enable", (PyCFunction)gc_enable, METH_NOARGS, gc_enable__doc__}, static PyObject * -gc_enable(PyObject *self, PyObject *noargs) +gc_enable_impl(PyModuleDef *module); + +static PyObject * +gc_enable(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_enable_impl(module); +} + +static PyObject * +gc_enable_impl(PyModuleDef *module) +/*[clinic end generated code: output=476c0b07b0098d24 input=81ac4940ca579707]*/ { enabled = 1; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +gc.disable + +Disable automatic garbage collection. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_disable__doc__, -"disable() -> None\n" -"\n" -"Disable automatic garbage collection.\n"); +"sig=($module)\n" +"Disable automatic garbage collection."); + +#define GC_DISABLE_METHODDEF \ + {"disable", (PyCFunction)gc_disable, METH_NOARGS, gc_disable__doc__}, static PyObject * -gc_disable(PyObject *self, PyObject *noargs) +gc_disable_impl(PyModuleDef *module); + +static PyObject * +gc_disable(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_disable_impl(module); +} + +static PyObject * +gc_disable_impl(PyModuleDef *module) +/*[clinic end generated code: output=b5f25c21f0d61be1 input=8c2e5a14e800d83b]*/ { enabled = 0; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } +/*[clinic input] +gc.isenabled -> bool + +Returns true if automatic garbage collection is enabled. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_isenabled__doc__, -"isenabled() -> status\n" -"\n" -"Returns true if automatic garbage collection is enabled.\n"); +"sig=($module)\n" +"Returns true if automatic garbage collection is enabled."); + +#define GC_ISENABLED_METHODDEF \ + {"isenabled", (PyCFunction)gc_isenabled, METH_NOARGS, gc_isenabled__doc__}, + +static int +gc_isenabled_impl(PyModuleDef *module); static PyObject * -gc_isenabled(PyObject *self, PyObject *noargs) +gc_isenabled(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - return PyBool_FromLong((long)enabled); + PyObject *return_value = NULL; + int _return_value; + + _return_value = gc_isenabled_impl(module); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyBool_FromLong((long)_return_value); + +exit: + return return_value; } +static int +gc_isenabled_impl(PyModuleDef *module) +/*[clinic end generated code: output=14abdb39851f4e4e input=30005e0422373b31]*/ +{ + return enabled; +} + +/*[clinic input] +gc.collect -> Py_ssize_t + + generation: int(c_default="NUM_GENERATIONS - 1") = 2 + An integer specifying which generation to collect. + +Collect generations. With no arguments, run a full collection. + +A ValueError is raised if the generation number is invalid. The number of +unreachable objects is returned. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_collect__doc__, -"collect([generation]) -> n\n" +"sig=($module, generation=2)\n" +"Collect generations. With no arguments, run a full collection.\n" "\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"); +" generation\n" +" An integer specifying which generation to collect.\n" +"\n" +"A ValueError is raised if the generation number is invalid. The number of\n" +"unreachable objects is returned."); + +#define GC_COLLECT_METHODDEF \ + {"collect", (PyCFunction)gc_collect, METH_VARARGS|METH_KEYWORDS, gc_collect__doc__}, + +static Py_ssize_t +gc_collect_impl(PyModuleDef *module, int generation); static PyObject * -gc_collect(PyObject *self, PyObject *args, PyObject *kws) +gc_collect(PyModuleDef *module, PyObject *args, PyObject *kwargs) { - static char *keywords[] = {"generation", NULL}; - int genarg = NUM_GENERATIONS - 1; + PyObject *return_value = NULL; + static char *_keywords[] = {"generation", NULL}; + int generation = NUM_GENERATIONS - 1; + Py_ssize_t _return_value; + + if (!PyArg_ParseTupleAndKeywords(args, kwargs, + "|i:collect", _keywords, + &generation)) + goto exit; + _return_value = gc_collect_impl(module, generation); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromSsize_t(_return_value); + +exit: + return return_value; +} + +static Py_ssize_t +gc_collect_impl(PyModuleDef *module, int generation) +/*[clinic end generated code: output=d2cb4868cd8abb25 input=fe9331fbdc55d611]*/ +{ Py_ssize_t n; - if (!PyArg_ParseTupleAndKeywords(args, kws, "|i", keywords, &genarg)) - return NULL; - - else if (genarg < 0 || genarg >= NUM_GENERATIONS) { + if (generation < 0 || generation >= NUM_GENERATIONS) { PyErr_SetString(PyExc_ValueError, "invalid generation"); - return NULL; + return -1; } if (collecting) n = 0; /* already collecting, don't do anything */ else { collecting = 1; - n = collect_with_callback(genarg); + n = collect_with_callback(generation); collecting = 0; } - return PyLong_FromSsize_t(n); + return n; } +/*[clinic input] +gc.set_debug + + flags: int + An integer that can have the following bits turned on: + DEBUG_STATS - Print statistics during collection. + DEBUG_COLLECTABLE - Print collectable objects found. + DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects + found. + DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them. + DEBUG_LEAK - Debug leaking programs (everything but STATS). + / + +Set the garbage collection debugging flags. + +Debugging information is written to sys.stderr. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_set_debug__doc__, -"set_debug(flags) -> None\n" +"sig=($module, flags)\n" +"Set the garbage collection debugging flags.\n" "\n" -"Set the garbage collection debugging flags. Debugging information is\n" -"written to sys.stderr.\n" +" flags\n" +" An integer that can have the following bits turned on:\n" +" DEBUG_STATS - Print statistics during collection.\n" +" DEBUG_COLLECTABLE - Print collectable objects found.\n" +" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects\n" +" found.\n" +" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" +" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n" "\n" -"flags is an integer and can have the following bits turned on:\n" -"\n" -" DEBUG_STATS - Print statistics during collection.\n" -" DEBUG_COLLECTABLE - Print collectable objects found.\n" -" DEBUG_UNCOLLECTABLE - Print unreachable but uncollectable objects found.\n" -" DEBUG_SAVEALL - Save objects to gc.garbage rather than freeing them.\n" -" DEBUG_LEAK - Debug leaking programs (everything but STATS).\n"); +"Debugging information is written to sys.stderr."); + +#define GC_SET_DEBUG_METHODDEF \ + {"set_debug", (PyCFunction)gc_set_debug, METH_VARARGS, gc_set_debug__doc__}, static PyObject * -gc_set_debug(PyObject *self, PyObject *args) +gc_set_debug_impl(PyModuleDef *module, int flags); + +static PyObject * +gc_set_debug(PyModuleDef *module, PyObject *args) { - if (!PyArg_ParseTuple(args, "i:set_debug", &debug)) - return NULL; + PyObject *return_value = NULL; + int flags; - Py_INCREF(Py_None); - return Py_None; + if (!PyArg_ParseTuple(args, + "i:set_debug", + &flags)) + goto exit; + return_value = gc_set_debug_impl(module, flags); + +exit: + return return_value; } +static PyObject * +gc_set_debug_impl(PyModuleDef *module, int flags) +/*[clinic end generated code: output=e513ab4f525bbb06 input=5e5ce15e84fbed15]*/ +{ + debug = flags; + + Py_RETURN_NONE; +} + +/*[clinic input] +gc.get_debug -> int + +Get the garbage collection debugging flags. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_get_debug__doc__, -"get_debug() -> flags\n" -"\n" -"Get the garbage collection debugging flags.\n"); +"sig=($module)\n" +"Get the garbage collection debugging flags."); + +#define GC_GET_DEBUG_METHODDEF \ + {"get_debug", (PyCFunction)gc_get_debug, METH_NOARGS, gc_get_debug__doc__}, + +static int +gc_get_debug_impl(PyModuleDef *module); static PyObject * -gc_get_debug(PyObject *self, PyObject *noargs) +gc_get_debug(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) { - return Py_BuildValue("i", debug); + PyObject *return_value = NULL; + int _return_value; + + _return_value = gc_get_debug_impl(module); + if ((_return_value == -1) && PyErr_Occurred()) + goto exit; + return_value = PyLong_FromLong((long)_return_value); + +exit: + return return_value; +} + +static int +gc_get_debug_impl(PyModuleDef *module) +/*[clinic end generated code: output=26923c7652716fe0 input=91a101e1c3b98366]*/ +{ + return debug; } PyDoc_STRVAR(gc_set_thresh__doc__, @@ -1281,17 +1453,34 @@ generations[i].threshold = generations[2].threshold; } - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(gc_get_thresh__doc__, -"get_threshold() -> (threshold0, threshold1, threshold2)\n" -"\n" -"Return the current collection thresholds\n"); +/*[clinic input] +gc.get_threshold + +Return the current collection thresholds. +[clinic start generated code]*/ + +PyDoc_STRVAR(gc_get_threshold__doc__, +"sig=($module)\n" +"Return the current collection thresholds."); + +#define GC_GET_THRESHOLD_METHODDEF \ + {"get_threshold", (PyCFunction)gc_get_threshold, METH_NOARGS, gc_get_threshold__doc__}, static PyObject * -gc_get_thresh(PyObject *self, PyObject *noargs) +gc_get_threshold_impl(PyModuleDef *module); + +static PyObject * +gc_get_threshold(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_get_threshold_impl(module); +} + +static PyObject * +gc_get_threshold_impl(PyModuleDef *module) +/*[clinic end generated code: output=6d46e093f94d36f1 input=286d79918034d6e6]*/ { return Py_BuildValue("(iii)", generations[0].threshold, @@ -1299,13 +1488,31 @@ generations[2].threshold); } +/*[clinic input] +gc.get_count + +Return the current collection counts. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_get_count__doc__, -"get_count() -> (count0, count1, count2)\n" -"\n" -"Return the current collection counts\n"); +"sig=($module)\n" +"Return the current collection counts."); + +#define GC_GET_COUNT_METHODDEF \ + {"get_count", (PyCFunction)gc_get_count, METH_NOARGS, gc_get_count__doc__}, static PyObject * -gc_get_count(PyObject *self, PyObject *noargs) +gc_get_count_impl(PyModuleDef *module); + +static PyObject * +gc_get_count(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_get_count_impl(module); +} + +static PyObject * +gc_get_count_impl(PyModuleDef *module) +/*[clinic end generated code: output=5be1308251cb4610 input=85a958f5505cbd23]*/ { return Py_BuildValue("(iii)", generations[0].count, @@ -1399,14 +1606,31 @@ return result; } +/*[clinic input] +gc.get_objects + +Return a list of objects tracked by the collector (excluding the list returned). +[clinic start generated code]*/ + PyDoc_STRVAR(gc_get_objects__doc__, -"get_objects() -> [...]\n" -"\n" -"Return a list of objects tracked by the collector (excluding the list\n" -"returned).\n"); +"sig=($module)\n" +"Return a list of objects tracked by the collector (excluding the list returned)."); + +#define GC_GET_OBJECTS_METHODDEF \ + {"get_objects", (PyCFunction)gc_get_objects, METH_NOARGS, gc_get_objects__doc__}, static PyObject * -gc_get_objects(PyObject *self, PyObject *noargs) +gc_get_objects_impl(PyModuleDef *module); + +static PyObject * +gc_get_objects(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_get_objects_impl(module); +} + +static PyObject * +gc_get_objects_impl(PyModuleDef *module) +/*[clinic end generated code: output=d617facc6fc6f61f input=9439fe8170bf35d8]*/ { int i; PyObject* result; @@ -1423,13 +1647,31 @@ return result; } +/*[clinic input] +gc.get_stats + +Return a list of dictionaries containing per-generation statistics. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_get_stats__doc__, -"get_stats() -> [...]\n" -"\n" -"Return a list of dictionaries containing per-generation statistics.\n"); +"sig=($module)\n" +"Return a list of dictionaries containing per-generation statistics."); + +#define GC_GET_STATS_METHODDEF \ + {"get_stats", (PyCFunction)gc_get_stats, METH_NOARGS, gc_get_stats__doc__}, static PyObject * -gc_get_stats(PyObject *self, PyObject *noargs) +gc_get_stats_impl(PyModuleDef *module); + +static PyObject * +gc_get_stats(PyModuleDef *module, PyObject *Py_UNUSED(ignored)) +{ + return gc_get_stats_impl(module); +} + +static PyObject * +gc_get_stats_impl(PyModuleDef *module) +/*[clinic end generated code: output=728b4e278f4e69dd input=1ef4ed9d17b1a470]*/ { int i; PyObject *result; @@ -1469,15 +1711,29 @@ } +/*[clinic input] +gc.is_tracked + + obj: object + / + +Returns true if the object is tracked by the garbage collector. + +Simple atomic objects will return false. +[clinic start generated code]*/ + PyDoc_STRVAR(gc_is_tracked__doc__, -"is_tracked(obj) -> bool\n" +"sig=($module, obj)\n" +"Returns true if the object is tracked by the garbage collector.\n" "\n" -"Returns true if the object is tracked by the garbage collector.\n" -"Simple atomic objects will return false.\n" -); +"Simple atomic objects will return false."); + +#define GC_IS_TRACKED_METHODDEF \ + {"is_tracked", (PyCFunction)gc_is_tracked, METH_O, gc_is_tracked__doc__}, static PyObject * -gc_is_tracked(PyObject *self, PyObject *obj) +gc_is_tracked(PyModuleDef *module, PyObject *obj) +/*[clinic end generated code: output=c1ce9399e6a88ec6 input=d83057f170ea2723]*/ { PyObject *result; @@ -1509,19 +1765,18 @@ "get_referents() -- Return the list of objects that an object refers to.\n"); static PyMethodDef GcMethods[] = { - {"enable", gc_enable, METH_NOARGS, gc_enable__doc__}, - {"disable", gc_disable, METH_NOARGS, gc_disable__doc__}, - {"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__}, + GC_ENABLE_METHODDEF + GC_DISABLE_METHODDEF + GC_ISENABLED_METHODDEF + GC_SET_DEBUG_METHODDEF + GC_GET_DEBUG_METHODDEF + GC_GET_COUNT_METHODDEF {"set_threshold", gc_set_thresh, METH_VARARGS, gc_set_thresh__doc__}, - {"get_threshold", gc_get_thresh, METH_NOARGS, gc_get_thresh__doc__}, - {"collect", (PyCFunction)gc_collect, - METH_VARARGS | METH_KEYWORDS, gc_collect__doc__}, - {"get_objects", gc_get_objects,METH_NOARGS, gc_get_objects__doc__}, - {"get_stats", gc_get_stats, METH_NOARGS, gc_get_stats__doc__}, - {"is_tracked", gc_is_tracked, METH_O, gc_is_tracked__doc__}, + GC_GET_THRESHOLD_METHODDEF + GC_COLLECT_METHODDEF + GC_GET_OBJECTS_METHODDEF + GC_GET_STATS_METHODDEF + GC_IS_TRACKED_METHODDEF {"get_referrers", gc_get_referrers, METH_VARARGS, gc_get_referrers__doc__}, {"get_referents", gc_get_referents, METH_VARARGS,