diff -r 1638360eea41 Modules/_lsprof.c --- a/Modules/_lsprof.c Sat Jan 11 22:22:21 2014 -0800 +++ b/Modules/_lsprof.c Sun Jan 12 10:16:36 2014 +0100 @@ -2,6 +2,12 @@ #include "frameobject.h" #include "rotatingtree.h" +/*[clinic input] +module _lsprof +class _lsprof.Profiler +[clinic start generated code]*/ +/*[clinic end generated code: checksum=da39a3ee5e6b4b0d3255bfef95601890afd80709]*/ + #if !defined(HAVE_LONG_LONG) #error "This module requires long longs!" #endif @@ -600,46 +606,86 @@ static int statsForEntry(rotating_node_t return err; } -PyDoc_STRVAR(getstats_doc, "\ -getstats() -> list of profiler_entry objects\n\ -\n\ -Return all information collected by the profiler.\n\ -Each profiler_entry is a tuple-like object with the\n\ -following attributes:\n\ -\n\ - code code object\n\ - callcount how many times this was called\n\ - reccallcount how many times called recursively\n\ - totaltime total time in this entry\n\ - inlinetime inline time in this entry (not in subcalls)\n\ - calls details of the calls\n\ -\n\ -The calls attribute is either None or a list of\n\ -profiler_subentry objects:\n\ -\n\ - code called code object\n\ - callcount how many times this is called\n\ - reccallcount how many times this is called recursively\n\ - totaltime total time spent in this call\n\ - inlinetime inline time (not in further subcalls)\n\ -"); -static PyObject* -profiler_getstats(ProfilerObject *pObj, PyObject* noarg) +/*[clinic input] +_lsprof.Profiler.getstats + + self: self(type='ProfilerObject *') + +Return all information collected by the profiler. + +Each profiler_entry is a tuple-like object with the following attributes: + + code code object + callcount how many times this was called + reccallcount how many times called recursively + totaltime total time in this entry + inlinetime inline time in this entry (not in subcalls) + calls details of the calls + +The calls attribute is either None or a list of profiler_subentry objects: + + code called code object + callcount how many times this is called + reccallcount how many times this is called recursively + totaltime total time spent in this call + inlinetime inline time (not in further subcalls) +[clinic start generated code]*/ + +PyDoc_STRVAR(_lsprof_Profiler_getstats__doc__, +"getstats()\n" +"Return all information collected by the profiler.\n" +"\n" +"Each profiler_entry is a tuple-like object with the following attributes:\n" +"\n" +" code code object\n" +" callcount how many times this was called\n" +" reccallcount how many times called recursively\n" +" totaltime total time in this entry\n" +" inlinetime inline time in this entry (not in subcalls)\n" +" calls details of the calls\n" +"\n" +"The calls attribute is either None or a list of profiler_subentry objects:\n" +"\n" +" code called code object\n" +" callcount how many times this is called\n" +" reccallcount how many times this is called recursively\n" +" totaltime total time spent in this call\n" +" inlinetime inline time (not in further subcalls)"); + +#define _LSPROF_PROFILER_GETSTATS_METHODDEF \ + {"getstats", (PyCFunction)_lsprof_Profiler_getstats, METH_NOARGS, _lsprof_Profiler_getstats__doc__}, + +static PyObject * +_lsprof_Profiler_getstats_impl(ProfilerObject *self); + +static PyObject * +_lsprof_Profiler_getstats(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _lsprof_Profiler_getstats_impl((ProfilerObject *)self); + + return return_value; +} + +static PyObject * +_lsprof_Profiler_getstats_impl(ProfilerObject *self) +/*[clinic end generated code: checksum=1f784e81fe6a6021c585f94eaff88367926b26d1]*/ { statscollector_t collect; - if (pending_exception(pObj)) + if (pending_exception(self)) return NULL; - if (!pObj->externalTimer) + if (!self->externalTimer) collect.factor = hpTimerUnit(); - else if (pObj->externalTimerUnit > 0.0) - collect.factor = pObj->externalTimerUnit; + else if (self->externalTimerUnit > 0.0) + collect.factor = self->externalTimerUnit; else collect.factor = 1.0 / DOUBLE_TIMER_PRECISION; collect.list = PyList_New(0); if (collect.list == NULL) return NULL; - if (RotatingTree_Enum(pObj->profilerEntries, statsForEntry, &collect) + if (RotatingTree_Enum(self->profilerEntries, statsForEntry, &collect) != 0) { Py_DECREF(collect.list); return NULL; @@ -674,31 +720,64 @@ setBuiltins(ProfilerObject *pObj, int nv return 0; } -PyDoc_STRVAR(enable_doc, "\ -enable(subcalls=True, builtins=True)\n\ -\n\ -Start collecting profiling information.\n\ -If 'subcalls' is True, also records for each function\n\ -statistics separated according to its current caller.\n\ -If 'builtins' is True, records the time spent in\n\ -built-in functions separately from their caller.\n\ -"); -static PyObject* -profiler_enable(ProfilerObject *self, PyObject *args, PyObject *kwds) +/*[clinic input] +_lsprof.Profiler.enable + + self: self(type='ProfilerObject *') + subcalls: bool = True + builtins: bool = True + / + +Start collecting profiling information. + +If 'subcalls' is True, also records for each function +statistics separated according to its current caller. +If 'builtins' is True, records the time spent in +built-in functions separately from their caller. +[clinic start generated code]*/ + +PyDoc_STRVAR(_lsprof_Profiler_enable__doc__, +"enable(subcalls=True, builtins=True)\n" +"Start collecting profiling information.\n" +"\n" +"If \'subcalls\' is True, also records for each function\n" +"statistics separated according to its current caller.\n" +"If \'builtins\' is True, records the time spent in\n" +"built-in functions separately from their caller."); + +#define _LSPROF_PROFILER_ENABLE_METHODDEF \ + {"enable", (PyCFunction)_lsprof_Profiler_enable, METH_VARARGS, _lsprof_Profiler_enable__doc__}, + +static PyObject * +_lsprof_Profiler_enable_impl(ProfilerObject *self, int subcalls, int builtins); + +static PyObject * +_lsprof_Profiler_enable(PyObject *self, PyObject *args) { - int subcalls = -1; - int builtins = -1; - static char *kwlist[] = {"subcalls", "builtins", 0}; - if (!PyArg_ParseTupleAndKeywords(args, kwds, "|ii:enable", - kwlist, &subcalls, &builtins)) - return NULL; + PyObject *return_value = NULL; + int subcalls = 1; + int builtins = 1; + + if (!PyArg_ParseTuple(args, + "|pp:enable", + &subcalls, &builtins)) + goto exit; + return_value = _lsprof_Profiler_enable_impl((ProfilerObject *)self, subcalls, builtins); + +exit: + return return_value; +} + +static PyObject * +_lsprof_Profiler_enable_impl(ProfilerObject *self, int subcalls, int builtins) +/*[clinic end generated code: checksum=6034ac8a3c5d15647227efd5e162b325d035f098]*/ +{ if (setSubcalls(self, subcalls) < 0 || setBuiltins(self, builtins) < 0) return NULL; PyEval_SetProfile(profiler_callback, (PyObject*)self); self->flags |= POF_ENABLED; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } static void @@ -717,36 +796,82 @@ flush_unmatched(ProfilerObject *pObj) } -PyDoc_STRVAR(disable_doc, "\ -disable()\n\ -\n\ -Stop collecting profiling information.\n\ -"); -static PyObject* -profiler_disable(ProfilerObject *self, PyObject* noarg) +/*[clinic input] +_lsprof.Profiler.disable + + self: self(type='ProfilerObject *') + +Stop collecting profiling information. +[clinic start generated code]*/ + +PyDoc_STRVAR(_lsprof_Profiler_disable__doc__, +"disable()\n" +"Stop collecting profiling information."); + +#define _LSPROF_PROFILER_DISABLE_METHODDEF \ + {"disable", (PyCFunction)_lsprof_Profiler_disable, METH_NOARGS, _lsprof_Profiler_disable__doc__}, + +static PyObject * +_lsprof_Profiler_disable_impl(ProfilerObject *self); + +static PyObject * +_lsprof_Profiler_disable(PyObject *self, PyObject *Py_UNUSED(ignored)) +{ + PyObject *return_value = NULL; + + return_value = _lsprof_Profiler_disable_impl((ProfilerObject *)self); + + return return_value; +} + +static PyObject * +_lsprof_Profiler_disable_impl(ProfilerObject *self) +/*[clinic end generated code: checksum=1bbc0d91532655cc397d7cb0c204d07ea179f18f]*/ { self->flags &= ~POF_ENABLED; PyEval_SetProfile(NULL, NULL); flush_unmatched(self); if (pending_exception(self)) return NULL; - Py_INCREF(Py_None); - return Py_None; + Py_RETURN_NONE; } -PyDoc_STRVAR(clear_doc, "\ -clear()\n\ -\n\ -Clear all profiling information collected so far.\n\ -"); -static PyObject* -profiler_clear(ProfilerObject *pObj, PyObject* noarg) +/*[clinic input] +_lsprof.Profiler.clear + + self: self(type='ProfilerObject *') + +Clear all profiling information collected so far. +[clinic start generated code]*/ + +PyDoc_STRVAR(_lsprof_Profiler_clear__doc__, +"clear()\n" +"Clear all profiling information collected so far."); + +#define _LSPROF_PROFILER_CLEAR_METHODDEF \ + {"clear", (PyCFunction)_lsprof_Profiler_clear, METH_NOARGS, _lsprof_Profiler_clear__doc__}, + +static PyObject * +_lsprof_Profiler_clear_impl(ProfilerObject *self); + +static PyObject * +_lsprof_Profiler_clear(PyObject *self, PyObject *Py_UNUSED(ignored)) { - clearEntries(pObj); - Py_INCREF(Py_None); - return Py_None; + PyObject *return_value = NULL; + + return_value = _lsprof_Profiler_clear_impl((ProfilerObject *)self); + + return return_value; +} + +static PyObject * +_lsprof_Profiler_clear_impl(ProfilerObject *self) +/*[clinic end generated code: checksum=a355d43a71ac5c247f34e46f2801a072ff22cb77]*/ +{ + clearEntries(self); + Py_RETURN_NONE; } static void @@ -791,14 +916,10 @@ profiler_init(ProfilerObject *pObj, PyOb } static PyMethodDef profiler_methods[] = { - {"getstats", (PyCFunction)profiler_getstats, - METH_NOARGS, getstats_doc}, - {"enable", (PyCFunction)profiler_enable, - METH_VARARGS | METH_KEYWORDS, enable_doc}, - {"disable", (PyCFunction)profiler_disable, - METH_NOARGS, disable_doc}, - {"clear", (PyCFunction)profiler_clear, - METH_NOARGS, clear_doc}, + _LSPROF_PROFILER_GETSTATS_METHODDEF + _LSPROF_PROFILER_ENABLE_METHODDEF + _LSPROF_PROFILER_DISABLE_METHODDEF + _LSPROF_PROFILER_CLEAR_METHODDEF {NULL, NULL} };