Skip to content

Commit feb51f3

Browse files
authored
gh-106320: Remove private _PyTraceMalloc C API functions (#106324)
* Remove private _PyTraceMalloc C API functions: move them to the internal C API. * Don't export most of these functions anymore, but still export _PyTraceMalloc_GetTraceback() used by tests. * Rename Include/tracemalloc.h to Include/cpython/tracemalloc.h
1 parent 18b1fde commit feb51f3

File tree

10 files changed

+99
-98
lines changed

10 files changed

+99
-98
lines changed

Include/Python.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@
103103
#include "pystrcmp.h"
104104
#include "fileutils.h"
105105
#include "cpython/pyfpe.h"
106-
#include "tracemalloc.h"
106+
#include "cpython/tracemalloc.h"
107107
#include "cpython/optimizer.h"
108108

109109
#endif /* !Py_PYTHON_H */

Include/cpython/tracemalloc.h

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
#ifndef Py_LIMITED_API
2+
#ifndef Py_TRACEMALLOC_H
3+
#define Py_TRACEMALLOC_H
4+
5+
/* Track an allocated memory block in the tracemalloc module.
6+
Return 0 on success, return -1 on error (failed to allocate memory to store
7+
the trace).
8+
9+
Return -2 if tracemalloc is disabled.
10+
11+
If memory block is already tracked, update the existing trace. */
12+
PyAPI_FUNC(int) PyTraceMalloc_Track(
13+
unsigned int domain,
14+
uintptr_t ptr,
15+
size_t size);
16+
17+
/* Untrack an allocated memory block in the tracemalloc module.
18+
Do nothing if the block was not tracked.
19+
20+
Return -2 if tracemalloc is disabled, otherwise return 0. */
21+
PyAPI_FUNC(int) PyTraceMalloc_Untrack(
22+
unsigned int domain,
23+
uintptr_t ptr);
24+
25+
#endif // !Py_TRACEMALLOC_H
26+
#endif // !Py_LIMITED_API

Include/internal/pycore_tracemalloc.h

+45
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,51 @@ struct _tracemalloc_runtime_state {
117117
}
118118

119119

120+
/* Get the traceback where a memory block was allocated.
121+
122+
Return a tuple of (filename: str, lineno: int) tuples.
123+
124+
Return None if the tracemalloc module is disabled or if the memory block
125+
is not tracked by tracemalloc.
126+
127+
Raise an exception and return NULL on error. */
128+
PyAPI_FUNC(PyObject*) _PyTraceMalloc_GetTraceback(
129+
unsigned int domain,
130+
uintptr_t ptr);
131+
132+
/* Return non-zero if tracemalloc is tracing */
133+
extern int _PyTraceMalloc_IsTracing(void);
134+
135+
/* Clear the tracemalloc traces */
136+
extern void _PyTraceMalloc_ClearTraces(void);
137+
138+
/* Clear the tracemalloc traces */
139+
extern PyObject* _PyTraceMalloc_GetTraces(void);
140+
141+
/* Clear tracemalloc traceback for an object */
142+
extern PyObject* _PyTraceMalloc_GetObjectTraceback(PyObject *obj);
143+
144+
/* Initialize tracemalloc */
145+
extern int _PyTraceMalloc_Init(void);
146+
147+
/* Start tracemalloc */
148+
extern int _PyTraceMalloc_Start(int max_nframe);
149+
150+
/* Stop tracemalloc */
151+
extern void _PyTraceMalloc_Stop(void);
152+
153+
/* Get the tracemalloc traceback limit */
154+
extern int _PyTraceMalloc_GetTracebackLimit(void);
155+
156+
/* Get the memory usage of tracemalloc in bytes */
157+
extern size_t _PyTraceMalloc_GetMemory(void);
158+
159+
/* Get the current size and peak size of traced memory blocks as a 2-tuple */
160+
extern PyObject* _PyTraceMalloc_GetTracedMemory(void);
161+
162+
/* Set the peak size of traced memory blocks to the current size */
163+
extern void _PyTraceMalloc_ResetPeak(void);
164+
120165
#ifdef __cplusplus
121166
}
122167
#endif

Include/tracemalloc.h

-72
This file was deleted.

Lib/test/test_tracemalloc.py

+3-1
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,10 @@
1111

1212
try:
1313
import _testcapi
14+
import _testinternalcapi
1415
except ImportError:
1516
_testcapi = None
17+
_testinternalcapi = None
1618

1719

1820
EMPTY_STRING_SIZE = sys.getsizeof(b'')
@@ -1008,7 +1010,7 @@ def tearDown(self):
10081010
tracemalloc.stop()
10091011

10101012
def get_traceback(self):
1011-
frames = _testcapi.tracemalloc_get_traceback(self.domain, self.ptr)
1013+
frames = _testinternalcapi._PyTraceMalloc_GetTraceback(self.domain, self.ptr)
10121014
if frames is not None:
10131015
return tracemalloc.Traceback(frames)
10141016
else:

Makefile.pre.in

+1-1
Original file line numberDiff line numberDiff line change
@@ -1658,7 +1658,6 @@ PYTHON_HEADERS= \
16581658
$(srcdir)/Include/structseq.h \
16591659
$(srcdir)/Include/sysmodule.h \
16601660
$(srcdir)/Include/traceback.h \
1661-
$(srcdir)/Include/tracemalloc.h \
16621661
$(srcdir)/Include/tupleobject.h \
16631662
$(srcdir)/Include/unicodeobject.h \
16641663
$(srcdir)/Include/warnings.h \
@@ -1713,6 +1712,7 @@ PYTHON_HEADERS= \
17131712
$(srcdir)/Include/cpython/setobject.h \
17141713
$(srcdir)/Include/cpython/sysmodule.h \
17151714
$(srcdir)/Include/cpython/traceback.h \
1715+
$(srcdir)/Include/cpython/tracemalloc.h \
17161716
$(srcdir)/Include/cpython/tupleobject.h \
17171717
$(srcdir)/Include/cpython/unicodeobject.h \
17181718
$(srcdir)/Include/cpython/warnings.h \

Modules/_testcapi/mem.c

-18
Original file line numberDiff line numberDiff line change
@@ -655,23 +655,6 @@ tracemalloc_untrack(PyObject *self, PyObject *args)
655655
Py_RETURN_NONE;
656656
}
657657

658-
static PyObject *
659-
tracemalloc_get_traceback(PyObject *self, PyObject *args)
660-
{
661-
unsigned int domain;
662-
PyObject *ptr_obj;
663-
664-
if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) {
665-
return NULL;
666-
}
667-
void *ptr = PyLong_AsVoidPtr(ptr_obj);
668-
if (PyErr_Occurred()) {
669-
return NULL;
670-
}
671-
672-
return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
673-
}
674-
675658
static PyMethodDef test_methods[] = {
676659
{"check_pyobject_forbidden_bytes_is_freed",
677660
check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
@@ -697,7 +680,6 @@ static PyMethodDef test_methods[] = {
697680
// Tracemalloc tests
698681
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},
699682
{"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
700-
{"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
701683
{NULL},
702684
};
703685

Modules/_testinternalcapi.c

+19-1
Original file line numberDiff line numberDiff line change
@@ -1216,6 +1216,24 @@ test_pytime_object_to_timespec(PyObject *self, PyObject *args)
12161216
}
12171217

12181218

1219+
static PyObject *
1220+
tracemalloc_get_traceback(PyObject *self, PyObject *args)
1221+
{
1222+
unsigned int domain;
1223+
PyObject *ptr_obj;
1224+
1225+
if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) {
1226+
return NULL;
1227+
}
1228+
void *ptr = PyLong_AsVoidPtr(ptr_obj);
1229+
if (PyErr_Occurred()) {
1230+
return NULL;
1231+
}
1232+
1233+
return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
1234+
}
1235+
1236+
12191237
static PyMethodDef module_functions[] = {
12201238
{"get_configs", get_configs, METH_NOARGS},
12211239
{"get_recursion_depth", get_recursion_depth, METH_NOARGS},
@@ -1250,7 +1268,6 @@ static PyMethodDef module_functions[] = {
12501268
{"get_uop_optimizer", get_uop_optimizer, METH_NOARGS, NULL},
12511269
{"pending_threadfunc", _PyCFunction_CAST(pending_threadfunc),
12521270
METH_VARARGS | METH_KEYWORDS},
1253-
// {"pending_fd_identify", pending_fd_identify, METH_VARARGS, NULL},
12541271
{"pending_identify", pending_identify, METH_VARARGS, NULL},
12551272
{"_PyTime_AsMicroseconds", test_PyTime_AsMicroseconds, METH_VARARGS},
12561273
{"_PyTime_AsMilliseconds", test_PyTime_AsMilliseconds, METH_VARARGS},
@@ -1266,6 +1283,7 @@ static PyMethodDef module_functions[] = {
12661283
{"_PyTime_ObjectToTime_t", test_pytime_object_to_time_t, METH_VARARGS},
12671284
{"_PyTime_ObjectToTimespec", test_pytime_object_to_timespec, METH_VARARGS},
12681285
{"_PyTime_ObjectToTimeval", test_pytime_object_to_timeval, METH_VARARGS},
1286+
{"_PyTraceMalloc_GetTraceback", tracemalloc_get_traceback, METH_VARARGS},
12691287
{NULL, NULL} /* sentinel */
12701288
};
12711289

PCbuild/pythoncore.vcxproj

+1-1
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@
179179
<ClInclude Include="..\Include\cpython\setobject.h" />
180180
<ClInclude Include="..\Include\cpython\sysmodule.h" />
181181
<ClInclude Include="..\Include\cpython\traceback.h" />
182+
<ClInclude Include="..\Include\cpython\tracemalloc.h" />
182183
<ClInclude Include="..\Include\cpython\tupleobject.h" />
183184
<ClInclude Include="..\Include\cpython\unicodeobject.h" />
184185
<ClInclude Include="..\Include\cpython\warnings.h" />
@@ -320,7 +321,6 @@
320321
<ClInclude Include="..\Include\symtable.h" />
321322
<ClInclude Include="..\Include\sysmodule.h" />
322323
<ClInclude Include="..\Include\traceback.h" />
323-
<ClInclude Include="..\Include\tracemalloc.h" />
324324
<ClInclude Include="..\Include\tupleobject.h" />
325325
<ClInclude Include="..\Include\unicodeobject.h" />
326326
<ClInclude Include="..\Include\weakrefobject.h" />

PCbuild/pythoncore.vcxproj.filters

+3-3
Original file line numberDiff line numberDiff line change
@@ -219,9 +219,6 @@
219219
<ClInclude Include="..\Include\traceback.h">
220220
<Filter>Include</Filter>
221221
</ClInclude>
222-
<ClInclude Include="..\Include\tracemalloc.h">
223-
<Filter>Include</Filter>
224-
</ClInclude>
225222
<ClInclude Include="..\Include\tupleobject.h">
226223
<Filter>Include</Filter>
227224
</ClInclude>
@@ -453,6 +450,9 @@
453450
<ClInclude Include="..\Include\cpython\traceback.h">
454451
<Filter>Include\cpython</Filter>
455452
</ClInclude>
453+
<ClInclude Include="..\Include\cpython\tracemalloc.h">
454+
<Filter>Include</Filter>
455+
</ClInclude>
456456
<ClInclude Include="..\Include\cpython\frameobject.h">
457457
<Filter>Include\cpython</Filter>
458458
</ClInclude>

0 commit comments

Comments
 (0)