Coverage Report

Created: 2026-05-30 06:18

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/cpython/Modules/_tracemalloc.c
Line
Count
Source
1
#include "Python.h"
2
#include "pycore_tracemalloc.h"  // _PyTraceMalloc_IsTracing
3
4
#include "clinic/_tracemalloc.c.h"
5
6
7
/*[clinic input]
8
module _tracemalloc
9
[clinic start generated code]*/
10
/*[clinic end generated code: output=da39a3ee5e6b4b0d input=708a98302fc46e5f]*/
11
12
13
/*[clinic input]
14
@permit_long_summary
15
_tracemalloc.is_tracing
16
17
Return True if the tracemalloc module is tracing Python memory allocations.
18
[clinic start generated code]*/
19
20
static PyObject *
21
_tracemalloc_is_tracing_impl(PyObject *module)
22
/*[clinic end generated code: output=2d763b42601cd3ef input=cac4fc9096babeac]*/
23
0
{
24
0
    return PyBool_FromLong(_PyTraceMalloc_IsTracing());
25
0
}
26
27
28
/*[clinic input]
29
_tracemalloc.clear_traces
30
31
Clear traces of memory blocks allocated by Python.
32
[clinic start generated code]*/
33
34
static PyObject *
35
_tracemalloc_clear_traces_impl(PyObject *module)
36
/*[clinic end generated code: output=a86080ee41b84197 input=0dab5b6c785183a5]*/
37
0
{
38
0
    _PyTraceMalloc_ClearTraces();
39
0
    Py_RETURN_NONE;
40
0
}
41
42
43
/*[clinic input]
44
_tracemalloc._get_traces
45
46
Get traces of all memory blocks allocated by Python.
47
48
Return a list of (size: int, traceback: tuple) tuples.
49
traceback is a tuple of (filename: str, lineno: int) tuples.
50
51
Return an empty list if the tracemalloc module is disabled.
52
[clinic start generated code]*/
53
54
static PyObject *
55
_tracemalloc__get_traces_impl(PyObject *module)
56
/*[clinic end generated code: output=e9929876ced4b5cc input=6c7d2230b24255aa]*/
57
0
{
58
0
    return _PyTraceMalloc_GetTraces();
59
0
}
60
61
62
63
/*[clinic input]
64
_tracemalloc._get_object_traceback
65
66
    obj: object
67
    /
68
69
Get the traceback where the Python object obj was allocated.
70
71
Return a tuple of (filename: str, lineno: int) tuples.
72
Return None if the tracemalloc module is disabled or did not
73
trace the allocation of the object.
74
[clinic start generated code]*/
75
76
static PyObject *
77
_tracemalloc__get_object_traceback(PyObject *module, PyObject *obj)
78
/*[clinic end generated code: output=41ee0553a658b0aa input=29495f1b21c53212]*/
79
0
{
80
0
    return _PyTraceMalloc_GetObjectTraceback(obj);
81
0
}
82
83
84
/*[clinic input]
85
_tracemalloc.start
86
87
    nframe: int = 1
88
    /
89
90
Start tracing Python memory allocations.
91
92
Also set the maximum number of frames stored in the traceback of a
93
trace to nframe.
94
[clinic start generated code]*/
95
96
static PyObject *
97
_tracemalloc_start_impl(PyObject *module, int nframe)
98
/*[clinic end generated code: output=caae05c23c159d3c input=40d849b5b29d1933]*/
99
0
{
100
0
    if (_PyTraceMalloc_Start(nframe) < 0) {
101
0
        return NULL;
102
0
    }
103
0
    Py_RETURN_NONE;
104
0
}
105
106
107
/*[clinic input]
108
_tracemalloc.stop
109
110
Stop tracing Python memory allocations.
111
112
Also clear traces of memory blocks allocated by Python.
113
[clinic start generated code]*/
114
115
static PyObject *
116
_tracemalloc_stop_impl(PyObject *module)
117
/*[clinic end generated code: output=c3c42ae03e3955cd input=7478f075e51dae18]*/
118
0
{
119
0
    _PyTraceMalloc_Stop();
120
0
    Py_RETURN_NONE;
121
0
}
122
123
124
/*[clinic input]
125
_tracemalloc.get_traceback_limit
126
127
Get the maximum number of frames stored in the traceback of a trace.
128
129
By default, a trace of an allocated memory block only stores
130
the most recent frame: the limit is 1.
131
[clinic start generated code]*/
132
133
static PyObject *
134
_tracemalloc_get_traceback_limit_impl(PyObject *module)
135
/*[clinic end generated code: output=d556d9306ba95567 input=da3cd977fc68ae3b]*/
136
0
{
137
0
    return PyLong_FromLong(_PyTraceMalloc_GetTracebackLimit());
138
0
}
139
140
/*[clinic input]
141
_tracemalloc.get_tracemalloc_memory
142
143
Get the memory usage in bytes of the tracemalloc module.
144
145
This memory is used internally to trace memory allocations.
146
[clinic start generated code]*/
147
148
static PyObject *
149
_tracemalloc_get_tracemalloc_memory_impl(PyObject *module)
150
/*[clinic end generated code: output=e3f14e280a55f5aa input=5d919c0f4d5132ad]*/
151
0
{
152
0
    return PyLong_FromSize_t(_PyTraceMalloc_GetMemory());
153
0
}
154
155
156
/*[clinic input]
157
@permit_long_summary
158
_tracemalloc.get_traced_memory
159
160
Get the current size and peak size of memory blocks traced by tracemalloc.
161
162
Returns a tuple: (current: int, peak: int).
163
[clinic start generated code]*/
164
165
static PyObject *
166
_tracemalloc_get_traced_memory_impl(PyObject *module)
167
/*[clinic end generated code: output=5b167189adb9e782 input=b06e7a1a4914fc21]*/
168
0
{
169
0
    return _PyTraceMalloc_GetTracedMemory();
170
0
}
171
172
/*[clinic input]
173
@permit_long_summary
174
_tracemalloc.reset_peak
175
176
Set the peak size of memory blocks traced by tracemalloc to the current size.
177
178
Do nothing if the tracemalloc module is not tracing memory allocations.
179
180
[clinic start generated code]*/
181
182
static PyObject *
183
_tracemalloc_reset_peak_impl(PyObject *module)
184
/*[clinic end generated code: output=140c2870f691dbb2 input=4103319210f46286]*/
185
0
{
186
0
    _PyTraceMalloc_ResetPeak();
187
0
    Py_RETURN_NONE;
188
0
}
189
190
191
static PyMethodDef module_methods[] = {
192
    _TRACEMALLOC_IS_TRACING_METHODDEF
193
    _TRACEMALLOC_CLEAR_TRACES_METHODDEF
194
    _TRACEMALLOC__GET_TRACES_METHODDEF
195
    _TRACEMALLOC__GET_OBJECT_TRACEBACK_METHODDEF
196
    _TRACEMALLOC_START_METHODDEF
197
    _TRACEMALLOC_STOP_METHODDEF
198
    _TRACEMALLOC_GET_TRACEBACK_LIMIT_METHODDEF
199
    _TRACEMALLOC_GET_TRACEMALLOC_MEMORY_METHODDEF
200
    _TRACEMALLOC_GET_TRACED_MEMORY_METHODDEF
201
    _TRACEMALLOC_RESET_PEAK_METHODDEF
202
    /* sentinel */
203
    {NULL, NULL}
204
};
205
206
PyDoc_STRVAR(module_doc,
207
"Debug module to trace memory blocks allocated by Python.");
208
209
static struct PyModuleDef module_def = {
210
    PyModuleDef_HEAD_INIT,
211
    "_tracemalloc",
212
    module_doc,
213
    0, /* non-negative size to be able to unload the module */
214
    module_methods,
215
    NULL,
216
};
217
218
PyMODINIT_FUNC
219
PyInit__tracemalloc(void)
220
0
{
221
0
    PyABIInfo_VAR(abi_info);
222
0
    if (PyABIInfo_Check(&abi_info, "_tracemalloc") < 0) {
223
0
        return NULL;
224
0
    }
225
226
0
    PyObject *mod = PyModule_Create(&module_def);
227
0
    if (mod == NULL) {
228
0
        return NULL;
229
0
    }
230
231
#ifdef Py_GIL_DISABLED
232
    PyUnstable_Module_SetGIL(mod, Py_MOD_GIL_NOT_USED);
233
#endif
234
235
0
    return mod;
236
0
}