summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSerhiy Storchaka <storchaka@gmail.com>2017-02-04 12:18:38 +0200
committerSerhiy Storchaka <storchaka@gmail.com>2017-02-04 12:18:38 +0200
commitb97948eadf46cef282f6978e3caefd7c7a365f13 (patch)
tree8c0c0c8181e7573a96dd04fec68cbcabdef5ece9
parent8fa02ec21b3087ebc0754fbf05b72c3647b2dbba (diff)
downloadcpython-b97948eadf46cef282f6978e3caefd7c7a365f13.tar.gz
Issue #20186: Converted the tracemalloc module to Argument Clinic.
Based on patch by Georg Brandl.
-rw-r--r--Modules/_tracemalloc.c225
-rw-r--r--Modules/clinic/_tracemalloc.c.h192
2 files changed, 315 insertions, 102 deletions
diff --git a/Modules/_tracemalloc.c b/Modules/_tracemalloc.c
index ec8bd960a7..43cf51df5d 100644
--- a/Modules/_tracemalloc.c
+++ b/Modules/_tracemalloc.c
@@ -4,6 +4,12 @@
#include "pythread.h"
#include "osdefs.h"
+#include "clinic/_tracemalloc.c.h"
+/*[clinic input]
+module _tracemalloc
+[clinic start generated code]*/
+/*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
+
/* Trace memory blocks allocated by PyMem_RawMalloc() */
#define TRACE_RAW_MALLOC
@@ -1161,27 +1167,31 @@ tracemalloc_stop(void)
tracemalloc_traceback = NULL;
}
-PyDoc_STRVAR(tracemalloc_is_tracing_doc,
- "is_tracing()->bool\n"
- "\n"
- "True if the tracemalloc module is tracing Python memory allocations,\n"
- "False otherwise.");
-static PyObject*
-py_tracemalloc_is_tracing(PyObject *self)
+/*[clinic input]
+_tracemalloc.is_tracing
+
+Return True if the tracemalloc module is tracing Python memory allocations.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_is_tracing_impl(PyObject *module)
+/*[clinic end generated code: output=2d763b42601cd3ef input=8beb4fb5446813be]*/
{
return PyBool_FromLong(tracemalloc_config.tracing);
}
-PyDoc_STRVAR(tracemalloc_clear_traces_doc,
- "clear_traces()\n"
- "\n"
- "Clear traces of memory blocks allocated by Python.");
+/*[clinic input]
+_tracemalloc.clear_traces
-static PyObject*
-py_tracemalloc_clear_traces(PyObject *self)
+Clear traces of memory blocks allocated by Python.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_clear_traces_impl(PyObject *module)
+/*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/
{
if (!tracemalloc_config.tracing)
Py_RETURN_NONE;
@@ -1343,17 +1353,21 @@ tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks,
}
-PyDoc_STRVAR(tracemalloc_get_traces_doc,
- "_get_traces() -> list\n"
- "\n"
- "Get traces of all memory blocks allocated by Python.\n"
- "Return a list of (size: int, traceback: tuple) tuples.\n"
- "traceback is a tuple of (filename: str, lineno: int) tuples.\n"
- "\n"
- "Return an empty list if the tracemalloc module is disabled.");
-static PyObject*
-py_tracemalloc_get_traces(PyObject *self, PyObject *obj)
+/*[clinic input]
+_tracemalloc._get_traces
+
+Get traces of all memory blocks allocated by Python.
+
+Return a list of (size: int, traceback: tuple) tuples.
+traceback is a tuple of (filename: str, lineno: int) tuples.
+
+Return an empty list if the tracemalloc module is disabled.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc__get_traces_impl(PyObject *module)
+/*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/
{
get_traces_t get_traces;
int err;
@@ -1439,17 +1453,23 @@ tracemalloc_get_traceback(_PyTraceMalloc_domain_t domain, uintptr_t ptr)
}
-PyDoc_STRVAR(tracemalloc_get_object_traceback_doc,
- "_get_object_traceback(obj)\n"
- "\n"
- "Get the traceback where the Python object obj was allocated.\n"
- "Return a tuple of (filename: str, lineno: int) tuples.\n"
- "\n"
- "Return None if the tracemalloc module is disabled or did not\n"
- "trace the allocation of the object.");
-static PyObject*
-py_tracemalloc_get_object_traceback(PyObject *self, PyObject *obj)
+/*[clinic input]
+_tracemalloc._get_object_traceback
+
+ obj: object
+ /
+
+Get the traceback where the Python object obj was allocated.
+
+Return a tuple of (filename: str, lineno: int) tuples.
+Return None if the tracemalloc module is disabled or did not
+trace the allocation of the object.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
+/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
{
PyTypeObject *type;
void *ptr;
@@ -1503,25 +1523,29 @@ _PyMem_DumpTraceback(int fd, const void *ptr)
#undef PUTS
-PyDoc_STRVAR(tracemalloc_start_doc,
- "start(nframe: int=1)\n"
- "\n"
- "Start tracing Python memory allocations. Set also the maximum number \n"
- "of frames stored in the traceback of a trace to nframe.");
-static PyObject*
-py_tracemalloc_start(PyObject *self, PyObject *args)
+/*[clinic input]
+_tracemalloc.start
+
+ nframe: Py_ssize_t = 1
+ /
+
+Start tracing Python memory allocations.
+
+Also set the maximum number of frames stored in the traceback of a
+trace to nframe.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_start_impl(PyObject *module, Py_ssize_t nframe)
+/*[clinic end generated code: output=0f558d2079511553 input=997841629cc441cb]*/
{
- Py_ssize_t nframe = 1;
int nframe_int;
- if (!PyArg_ParseTuple(args, "|n:start", &nframe))
- return NULL;
-
if (nframe < 1 || nframe > MAX_NFRAME) {
PyErr_Format(PyExc_ValueError,
"the number of frames must be in range [1; %i]",
- MAX_NFRAME);
+ (int)MAX_NFRAME);
return NULL;
}
nframe_int = Py_SAFE_DOWNCAST(nframe, Py_ssize_t, int);
@@ -1532,48 +1556,55 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
Py_RETURN_NONE;
}
-PyDoc_STRVAR(tracemalloc_stop_doc,
- "stop()\n"
- "\n"
- "Stop tracing Python memory allocations and clear traces\n"
- "of memory blocks allocated by Python.");
+/*[clinic input]
+_tracemalloc.stop
-static PyObject*
-py_tracemalloc_stop(PyObject *self)
+Stop tracing Python memory allocations.
+
+Also clear traces of memory blocks allocated by Python.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_stop_impl(PyObject *module)
+/*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/
{
tracemalloc_stop();
Py_RETURN_NONE;
}
-PyDoc_STRVAR(tracemalloc_get_traceback_limit_doc,
- "get_traceback_limit() -> int\n"
- "\n"
- "Get the maximum number of frames stored in the traceback\n"
- "of a trace.\n"
- "\n"
- "By default, a trace of an allocated memory block only stores\n"
- "the most recent frame: the limit is 1.");
+/*[clinic input]
+_tracemalloc.get_traceback_limit
-static PyObject*
-py_tracemalloc_get_traceback_limit(PyObject *self)
+Get the maximum number of frames stored in the traceback of a trace.
+
+By default, a trace of an allocated memory block only stores
+the most recent frame: the limit is 1.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_get_traceback_limit_impl(PyObject *module)
+/*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/
{
return PyLong_FromLong(tracemalloc_config.max_nframe);
}
-PyDoc_STRVAR(tracemalloc_get_tracemalloc_memory_doc,
- "get_tracemalloc_memory() -> int\n"
- "\n"
- "Get the memory usage in bytes of the tracemalloc module\n"
- "used internally to trace memory allocations.");
-static PyObject*
-tracemalloc_get_tracemalloc_memory(PyObject *self)
+/*[clinic input]
+_tracemalloc.get_tracemalloc_memory
+
+Get the memory usage in bytes of the tracemalloc module.
+
+This memory is used internally to trace memory allocations.
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_get_tracemalloc_memory_impl(PyObject *module)
+/*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/
{
size_t size;
- PyObject *size_obj;
size = _Py_hashtable_size(tracemalloc_tracebacks);
size += _Py_hashtable_size(tracemalloc_filenames);
@@ -1582,22 +1613,24 @@ tracemalloc_get_tracemalloc_memory(PyObject *self)
size += _Py_hashtable_size(tracemalloc_traces);
TABLES_UNLOCK();
- size_obj = PyLong_FromSize_t(size);
- return Py_BuildValue("N", size_obj);
+ return PyLong_FromSize_t(size);
}
-PyDoc_STRVAR(tracemalloc_get_traced_memory_doc,
- "get_traced_memory() -> (int, int)\n"
- "\n"
- "Get the current size and peak size of memory blocks traced\n"
- "by the tracemalloc module as a tuple: (current: int, peak: int).");
-static PyObject*
-tracemalloc_get_traced_memory(PyObject *self)
+/*[clinic input]
+_tracemalloc.get_traced_memory
+
+Get the current size and peak size of memory blocks traced by tracemalloc.
+
+Returns a tuple: (current: int, peak: int).
+[clinic start generated code]*/
+
+static PyObject *
+_tracemalloc_get_traced_memory_impl(PyObject *module)
+/*[clinic end generated code: output=5b167189adb9e782 input=61ddb5478400ff66]*/
{
Py_ssize_t size, peak_size;
- PyObject *size_obj, *peak_size_obj;
if (!tracemalloc_config.tracing)
return Py_BuildValue("ii", 0, 0);
@@ -1607,32 +1640,20 @@ tracemalloc_get_traced_memory(PyObject *self)
peak_size = tracemalloc_peak_traced_memory;
TABLES_UNLOCK();
- size_obj = PyLong_FromSize_t(size);
- peak_size_obj = PyLong_FromSize_t(peak_size);
- return Py_BuildValue("NN", size_obj, peak_size_obj);
+ return Py_BuildValue("nn", size, peak_size);
}
static PyMethodDef module_methods[] = {
- {"is_tracing", (PyCFunction)py_tracemalloc_is_tracing,
- METH_NOARGS, tracemalloc_is_tracing_doc},
- {"clear_traces", (PyCFunction)py_tracemalloc_clear_traces,
- METH_NOARGS, tracemalloc_clear_traces_doc},
- {"_get_traces", (PyCFunction)py_tracemalloc_get_traces,
- METH_NOARGS, tracemalloc_get_traces_doc},
- {"_get_object_traceback", (PyCFunction)py_tracemalloc_get_object_traceback,
- METH_O, tracemalloc_get_object_traceback_doc},
- {"start", (PyCFunction)py_tracemalloc_start,
- METH_VARARGS, tracemalloc_start_doc},
- {"stop", (PyCFunction)py_tracemalloc_stop,
- METH_NOARGS, tracemalloc_stop_doc},
- {"get_traceback_limit", (PyCFunction)py_tracemalloc_get_traceback_limit,
- METH_NOARGS, tracemalloc_get_traceback_limit_doc},
- {"get_tracemalloc_memory", (PyCFunction)tracemalloc_get_tracemalloc_memory,
- METH_NOARGS, tracemalloc_get_tracemalloc_memory_doc},
- {"get_traced_memory", (PyCFunction)tracemalloc_get_traced_memory,
- METH_NOARGS, tracemalloc_get_traced_memory_doc},
-
+ _TRACEMALLOC_IS_TRACING_METHODDEF
+ _TRACEMALLOC_CLEAR_TRACES_METHODDEF
+ _TRACEMALLOC__GET_TRACES_METHODDEF
+ _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF
+ _TRACEMALLOC_START_METHODDEF
+ _TRACEMALLOC_STOP_METHODDEF
+ _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
+ _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
+ _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
/* sentinel */
{NULL, NULL}
};
diff --git a/Modules/clinic/_tracemalloc.c.h b/Modules/clinic/_tracemalloc.c.h
new file mode 100644
index 0000000000..6592dad378
--- /dev/null
+++ b/Modules/clinic/_tracemalloc.c.h
@@ -0,0 +1,192 @@
+/*[clinic input]
+preserve
+[clinic start generated code]*/
+
+PyDoc_STRVAR(_tracemalloc_is_tracing__doc__,
+"is_tracing($module, /)\n"
+"--\n"
+"\n"
+"True if the tracemalloc module is tracing Python memory allocations, False otherwise.");
+
+#define _TRACEMALLOC_IS_TRACING_METHODDEF \
+ {"is_tracing", (PyCFunction)_tracemalloc_is_tracing, METH_NOARGS, _tracemalloc_is_tracing__doc__},
+
+static PyObject *
+_tracemalloc_is_tracing_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_is_tracing(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_is_tracing_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc_clear_traces__doc__,
+"clear_traces($module, /)\n"
+"--\n"
+"\n"
+"Clear traces of memory blocks allocated by Python.");
+
+#define _TRACEMALLOC_CLEAR_TRACES_METHODDEF \
+ {"clear_traces", (PyCFunction)_tracemalloc_clear_traces, METH_NOARGS, _tracemalloc_clear_traces__doc__},
+
+static PyObject *
+_tracemalloc_clear_traces_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_clear_traces(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_clear_traces_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc__get_traces__doc__,
+"_get_traces($module, /)\n"
+"--\n"
+"\n"
+"Get traces of all memory blocks allocated by Python.\n"
+"\n"
+"Return a list of (size: int, traceback: tuple) tuples.\n"
+"traceback is a tuple of (filename: str, lineno: int) tuples.\n"
+"\n"
+"Return an empty list if the tracemalloc module is disabled.");
+
+#define _TRACEMALLOC__GET_TRACES_METHODDEF \
+ {"_get_traces", (PyCFunction)_tracemalloc__get_traces, METH_NOARGS, _tracemalloc__get_traces__doc__},
+
+static PyObject *
+_tracemalloc__get_traces_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc__get_traces(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc__get_traces_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc__get_object_traceback__doc__,
+"_get_object_traceback($module, obj, /)\n"
+"--\n"
+"\n"
+"Get the traceback where the Python object obj was allocated.\n"
+"\n"
+"Return a tuple of (filename: str, lineno: int) tuples.\n"
+"Return None if the tracemalloc module is disabled or did not\n"
+"trace the allocation of the object.");
+
+#define _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF \
+ {"_get_object_traceback", (PyCFunction)_tracemalloc__get_object_traceback, METH_O, _tracemalloc__get_object_traceback__doc__},
+
+PyDoc_STRVAR(_tracemalloc_start__doc__,
+"start($module, nframe=1, /)\n"
+"--\n"
+"\n"
+"Start tracing Python memory allocations.\n"
+"\n"
+"Also set the maximum number of frames stored in the traceback of a\n"
+"trace to nframe.");
+
+#define _TRACEMALLOC_START_METHODDEF \
+ {"start", (PyCFunction)_tracemalloc_start, METH_FASTCALL, _tracemalloc_start__doc__},
+
+static PyObject *
+_tracemalloc_start_impl(PyObject *module, Py_ssize_t nframe);
+
+static PyObject *
+_tracemalloc_start(PyObject *module, PyObject **args, Py_ssize_t nargs, PyObject *kwnames)
+{
+ PyObject *return_value = NULL;
+ Py_ssize_t nframe = 1;
+
+ if (!_PyArg_ParseStack(args, nargs, "|n:start",
+ &nframe)) {
+ goto exit;
+ }
+
+ if (!_PyArg_NoStackKeywords("start", kwnames)) {
+ goto exit;
+ }
+ return_value = _tracemalloc_start_impl(module, nframe);
+
+exit:
+ return return_value;
+}
+
+PyDoc_STRVAR(_tracemalloc_stop__doc__,
+"stop($module, /)\n"
+"--\n"
+"\n"
+"Stop tracing Python memory allocations.\n"
+"\n"
+"Also clear traces of memory blocks allocated by Python.");
+
+#define _TRACEMALLOC_STOP_METHODDEF \
+ {"stop", (PyCFunction)_tracemalloc_stop, METH_NOARGS, _tracemalloc_stop__doc__},
+
+static PyObject *
+_tracemalloc_stop_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_stop(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_stop_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc_get_traceback_limit__doc__,
+"get_traceback_limit($module, /)\n"
+"--\n"
+"\n"
+"Get the maximum number of frames stored in the traceback of a trace.\n"
+"\n"
+"By default, a trace of an allocated memory block only stores\n"
+"the most recent frame: the limit is 1.");
+
+#define _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF \
+ {"get_traceback_limit", (PyCFunction)_tracemalloc_get_traceback_limit, METH_NOARGS, _tracemalloc_get_traceback_limit__doc__},
+
+static PyObject *
+_tracemalloc_get_traceback_limit_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_get_traceback_limit(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_get_traceback_limit_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc_get_tracemalloc_memory__doc__,
+"get_tracemalloc_memory($module, /)\n"
+"--\n"
+"\n"
+"Get the memory usage in bytes of the tracemalloc module.\n"
+"\n"
+"This memory is used internally to trace memory allocations.");
+
+#define _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF \
+ {"get_tracemalloc_memory", (PyCFunction)_tracemalloc_get_tracemalloc_memory, METH_NOARGS, _tracemalloc_get_tracemalloc_memory__doc__},
+
+static PyObject *
+_tracemalloc_get_tracemalloc_memory_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_get_tracemalloc_memory(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_get_tracemalloc_memory_impl(module);
+}
+
+PyDoc_STRVAR(_tracemalloc_get_traced_memory__doc__,
+"get_traced_memory($module, /)\n"
+"--\n"
+"\n"
+"Get the current size and peak size of memory blocks traced by tracemalloc.\n"
+"\n"
+"Returns a tuple: (current: int, peak: int).");
+
+#define _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF \
+ {"get_traced_memory", (PyCFunction)_tracemalloc_get_traced_memory, METH_NOARGS, _tracemalloc_get_traced_memory__doc__},
+
+static PyObject *
+_tracemalloc_get_traced_memory_impl(PyObject *module);
+
+static PyObject *
+_tracemalloc_get_traced_memory(PyObject *module, PyObject *Py_UNUSED(ignored))
+{
+ return _tracemalloc_get_traced_memory_impl(module);
+}
+/*[clinic end generated code: output=1e059f24619e23f9 input=a9049054013a1b77]*/