44#include "pythread.h"
55#include "osdefs.h"
66
7+ #include "clinic/_tracemalloc.c.h"
8+ /*[clinic input]
9+ module _tracemalloc
10+ [clinic start generated code]*/
11+ /*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
12+
713/* Trace memory blocks allocated by PyMem_RawMalloc() */
814#define TRACE_RAW_MALLOC
915
@@ -1161,27 +1167,31 @@ tracemalloc_stop(void)
11611167 tracemalloc_traceback = NULL ;
11621168}
11631169
1164- PyDoc_STRVAR (tracemalloc_is_tracing_doc ,
1165- "is_tracing()->bool\n"
1166- "\n"
1167- "True if the tracemalloc module is tracing Python memory allocations,\n"
1168- "False otherwise." );
11691170
11701171
1171- static PyObject *
1172- py_tracemalloc_is_tracing (PyObject * self )
1172+ /*[clinic input]
1173+ _tracemalloc.is_tracing
1174+
1175+ Return True if the tracemalloc module is tracing Python memory allocations.
1176+ [clinic start generated code]*/
1177+
1178+ static PyObject *
1179+ _tracemalloc_is_tracing_impl (PyObject * module )
1180+ /*[clinic end generated code: output=2d763b42601cd3ef input=8beb4fb5446813be]*/
11731181{
11741182 return PyBool_FromLong (tracemalloc_config .tracing );
11751183}
11761184
1177- PyDoc_STRVAR (tracemalloc_clear_traces_doc ,
1178- "clear_traces()\n"
1179- "\n"
1180- "Clear traces of memory blocks allocated by Python." );
11811185
1186+ /*[clinic input]
1187+ _tracemalloc.clear_traces
11821188
1183- static PyObject *
1184- py_tracemalloc_clear_traces (PyObject * self )
1189+ Clear traces of memory blocks allocated by Python.
1190+ [clinic start generated code]*/
1191+
1192+ static PyObject *
1193+ _tracemalloc_clear_traces_impl (PyObject * module )
1194+ /*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/
11851195{
11861196 if (!tracemalloc_config .tracing )
11871197 Py_RETURN_NONE ;
@@ -1343,17 +1353,21 @@ tracemalloc_pyobject_decref_cb(_Py_hashtable_t *tracebacks,
13431353}
13441354
13451355
1346- PyDoc_STRVAR (tracemalloc_get_traces_doc ,
1347- "_get_traces() -> list\n"
1348- "\n"
1349- "Get traces of all memory blocks allocated by Python.\n"
1350- "Return a list of (size: int, traceback: tuple) tuples.\n"
1351- "traceback is a tuple of (filename: str, lineno: int) tuples.\n"
1352- "\n"
1353- "Return an empty list if the tracemalloc module is disabled." );
13541356
1355- static PyObject *
1356- py_tracemalloc_get_traces (PyObject * self , PyObject * obj )
1357+ /*[clinic input]
1358+ _tracemalloc._get_traces
1359+
1360+ Get traces of all memory blocks allocated by Python.
1361+
1362+ Return a list of (size: int, traceback: tuple) tuples.
1363+ traceback is a tuple of (filename: str, lineno: int) tuples.
1364+
1365+ Return an empty list if the tracemalloc module is disabled.
1366+ [clinic start generated code]*/
1367+
1368+ static PyObject *
1369+ _tracemalloc__get_traces_impl (PyObject * module )
1370+ /*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/
13571371{
13581372 get_traces_t get_traces ;
13591373 int err ;
@@ -1439,17 +1453,23 @@ tracemalloc_get_traceback(_PyTraceMalloc_domain_t domain, uintptr_t ptr)
14391453}
14401454
14411455
1442- PyDoc_STRVAR (tracemalloc_get_object_traceback_doc ,
1443- "_get_object_traceback(obj)\n"
1444- "\n"
1445- "Get the traceback where the Python object obj was allocated.\n"
1446- "Return a tuple of (filename: str, lineno: int) tuples.\n"
1447- "\n"
1448- "Return None if the tracemalloc module is disabled or did not\n"
1449- "trace the allocation of the object." );
14501456
1451- static PyObject *
1452- py_tracemalloc_get_object_traceback (PyObject * self , PyObject * obj )
1457+ /*[clinic input]
1458+ _tracemalloc._get_object_traceback
1459+
1460+ obj: object
1461+ /
1462+
1463+ Get the traceback where the Python object obj was allocated.
1464+
1465+ Return a tuple of (filename: str, lineno: int) tuples.
1466+ Return None if the tracemalloc module is disabled or did not
1467+ trace the allocation of the object.
1468+ [clinic start generated code]*/
1469+
1470+ static PyObject *
1471+ _tracemalloc__get_object_traceback (PyObject * module , PyObject * obj )
1472+ /*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
14531473{
14541474 PyTypeObject * type ;
14551475 void * ptr ;
@@ -1503,25 +1523,29 @@ _PyMem_DumpTraceback(int fd, const void *ptr)
15031523#undef PUTS
15041524
15051525
1506- PyDoc_STRVAR (tracemalloc_start_doc ,
1507- "start(nframe: int=1)\n"
1508- "\n"
1509- "Start tracing Python memory allocations. Set also the maximum number \n"
1510- "of frames stored in the traceback of a trace to nframe." );
15111526
1512- static PyObject *
1513- py_tracemalloc_start (PyObject * self , PyObject * args )
1527+ /*[clinic input]
1528+ _tracemalloc.start
1529+
1530+ nframe: Py_ssize_t = 1
1531+ /
1532+
1533+ Start tracing Python memory allocations.
1534+
1535+ Also set the maximum number of frames stored in the traceback of a
1536+ trace to nframe.
1537+ [clinic start generated code]*/
1538+
1539+ static PyObject *
1540+ _tracemalloc_start_impl (PyObject * module , Py_ssize_t nframe )
1541+ /*[clinic end generated code: output=0f558d2079511553 input=997841629cc441cb]*/
15141542{
1515- Py_ssize_t nframe = 1 ;
15161543 int nframe_int ;
15171544
1518- if (!PyArg_ParseTuple (args , "|n:start" , & nframe ))
1519- return NULL ;
1520-
15211545 if (nframe < 1 || nframe > MAX_NFRAME ) {
15221546 PyErr_Format (PyExc_ValueError ,
15231547 "the number of frames must be in range [1; %i]" ,
1524- MAX_NFRAME );
1548+ ( int ) MAX_NFRAME );
15251549 return NULL ;
15261550 }
15271551 nframe_int = Py_SAFE_DOWNCAST (nframe , Py_ssize_t , int );
@@ -1532,48 +1556,55 @@ py_tracemalloc_start(PyObject *self, PyObject *args)
15321556 Py_RETURN_NONE ;
15331557}
15341558
1535- PyDoc_STRVAR (tracemalloc_stop_doc ,
1536- "stop()\n"
1537- "\n"
1538- "Stop tracing Python memory allocations and clear traces\n"
1539- "of memory blocks allocated by Python." );
15401559
1560+ /*[clinic input]
1561+ _tracemalloc.stop
15411562
1542- static PyObject *
1543- py_tracemalloc_stop (PyObject * self )
1563+ Stop tracing Python memory allocations.
1564+
1565+ Also clear traces of memory blocks allocated by Python.
1566+ [clinic start generated code]*/
1567+
1568+ static PyObject *
1569+ _tracemalloc_stop_impl (PyObject * module )
1570+ /*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/
15441571{
15451572 tracemalloc_stop ();
15461573 Py_RETURN_NONE ;
15471574}
15481575
15491576
1550- PyDoc_STRVAR (tracemalloc_get_traceback_limit_doc ,
1551- "get_traceback_limit() -> int\n"
1552- "\n"
1553- "Get the maximum number of frames stored in the traceback\n"
1554- "of a trace.\n"
1555- "\n"
1556- "By default, a trace of an allocated memory block only stores\n"
1557- "the most recent frame: the limit is 1." );
1577+ /*[clinic input]
1578+ _tracemalloc.get_traceback_limit
15581579
1559- static PyObject *
1560- py_tracemalloc_get_traceback_limit (PyObject * self )
1580+ Get the maximum number of frames stored in the traceback of a trace.
1581+
1582+ By default, a trace of an allocated memory block only stores
1583+ the most recent frame: the limit is 1.
1584+ [clinic start generated code]*/
1585+
1586+ static PyObject *
1587+ _tracemalloc_get_traceback_limit_impl (PyObject * module )
1588+ /*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/
15611589{
15621590 return PyLong_FromLong (tracemalloc_config .max_nframe );
15631591}
15641592
15651593
1566- PyDoc_STRVAR (tracemalloc_get_tracemalloc_memory_doc ,
1567- "get_tracemalloc_memory() -> int\n"
1568- "\n"
1569- "Get the memory usage in bytes of the tracemalloc module\n"
1570- "used internally to trace memory allocations." );
15711594
1572- static PyObject *
1573- tracemalloc_get_tracemalloc_memory (PyObject * self )
1595+ /*[clinic input]
1596+ _tracemalloc.get_tracemalloc_memory
1597+
1598+ Get the memory usage in bytes of the tracemalloc module.
1599+
1600+ This memory is used internally to trace memory allocations.
1601+ [clinic start generated code]*/
1602+
1603+ static PyObject *
1604+ _tracemalloc_get_tracemalloc_memory_impl (PyObject * module )
1605+ /*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/
15741606{
15751607 size_t size ;
1576- PyObject * size_obj ;
15771608
15781609 size = _Py_hashtable_size (tracemalloc_tracebacks );
15791610 size += _Py_hashtable_size (tracemalloc_filenames );
@@ -1582,22 +1613,24 @@ tracemalloc_get_tracemalloc_memory(PyObject *self)
15821613 size += _Py_hashtable_size (tracemalloc_traces );
15831614 TABLES_UNLOCK ();
15841615
1585- size_obj = PyLong_FromSize_t (size );
1586- return Py_BuildValue ("N" , size_obj );
1616+ return PyLong_FromSize_t (size );
15871617}
15881618
15891619
1590- PyDoc_STRVAR (tracemalloc_get_traced_memory_doc ,
1591- "get_traced_memory() -> (int, int)\n"
1592- "\n"
1593- "Get the current size and peak size of memory blocks traced\n"
1594- "by the tracemalloc module as a tuple: (current: int, peak: int)." );
15951620
1596- static PyObject *
1597- tracemalloc_get_traced_memory (PyObject * self )
1621+ /*[clinic input]
1622+ _tracemalloc.get_traced_memory
1623+
1624+ Get the current size and peak size of memory blocks traced by tracemalloc.
1625+
1626+ Returns a tuple: (current: int, peak: int).
1627+ [clinic start generated code]*/
1628+
1629+ static PyObject *
1630+ _tracemalloc_get_traced_memory_impl (PyObject * module )
1631+ /*[clinic end generated code: output=5b167189adb9e782 input=61ddb5478400ff66]*/
15981632{
15991633 Py_ssize_t size , peak_size ;
1600- PyObject * size_obj , * peak_size_obj ;
16011634
16021635 if (!tracemalloc_config .tracing )
16031636 return Py_BuildValue ("ii" , 0 , 0 );
@@ -1607,32 +1640,20 @@ tracemalloc_get_traced_memory(PyObject *self)
16071640 peak_size = tracemalloc_peak_traced_memory ;
16081641 TABLES_UNLOCK ();
16091642
1610- size_obj = PyLong_FromSize_t (size );
1611- peak_size_obj = PyLong_FromSize_t (peak_size );
1612- return Py_BuildValue ("NN" , size_obj , peak_size_obj );
1643+ return Py_BuildValue ("nn" , size , peak_size );
16131644}
16141645
16151646
16161647static PyMethodDef module_methods [] = {
1617- {"is_tracing" , (PyCFunction )py_tracemalloc_is_tracing ,
1618- METH_NOARGS , tracemalloc_is_tracing_doc },
1619- {"clear_traces" , (PyCFunction )py_tracemalloc_clear_traces ,
1620- METH_NOARGS , tracemalloc_clear_traces_doc },
1621- {"_get_traces" , (PyCFunction )py_tracemalloc_get_traces ,
1622- METH_NOARGS , tracemalloc_get_traces_doc },
1623- {"_get_object_traceback" , (PyCFunction )py_tracemalloc_get_object_traceback ,
1624- METH_O , tracemalloc_get_object_traceback_doc },
1625- {"start" , (PyCFunction )py_tracemalloc_start ,
1626- METH_VARARGS , tracemalloc_start_doc },
1627- {"stop" , (PyCFunction )py_tracemalloc_stop ,
1628- METH_NOARGS , tracemalloc_stop_doc },
1629- {"get_traceback_limit" , (PyCFunction )py_tracemalloc_get_traceback_limit ,
1630- METH_NOARGS , tracemalloc_get_traceback_limit_doc },
1631- {"get_tracemalloc_memory" , (PyCFunction )tracemalloc_get_tracemalloc_memory ,
1632- METH_NOARGS , tracemalloc_get_tracemalloc_memory_doc },
1633- {"get_traced_memory" , (PyCFunction )tracemalloc_get_traced_memory ,
1634- METH_NOARGS , tracemalloc_get_traced_memory_doc },
1635-
1648+ _TRACEMALLOC_IS_TRACING_METHODDEF
1649+ _TRACEMALLOC_CLEAR_TRACES_METHODDEF
1650+ _TRACEMALLOC__GET_TRACES_METHODDEF
1651+ _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF
1652+ _TRACEMALLOC_START_METHODDEF
1653+ _TRACEMALLOC_STOP_METHODDEF
1654+ _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
1655+ _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
1656+ _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
16361657 /* sentinel */
16371658 {NULL , NULL }
16381659};
0 commit comments