Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
gh-93649: Split tracemalloc tests from _testcapimodule.c
  • Loading branch information
erlend-aasland committed Nov 16, 2022
commit 5de1dc4d945c672b8a9c4869b4e3d09199491c3d
81 changes: 81 additions & 0 deletions Modules/_testcapi/mem.c
Original file line number Diff line number Diff line change
Expand Up @@ -596,6 +596,82 @@ check_pyobject_freed_is_freed(PyObject *self, PyObject *Py_UNUSED(args))
#endif
}

// Tracemalloc tests
static PyObject *
tracemalloc_track(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;
Py_ssize_t size;
int release_gil = 0;

if (!PyArg_ParseTuple(args, "IOn|i",
&domain, &ptr_obj, &size, &release_gil))
{
return NULL;
}
void *ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred()) {
return NULL;
}

int res;
if (release_gil) {
Py_BEGIN_ALLOW_THREADS
res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Py_END_ALLOW_THREADS
}
else {
res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
}
if (res < 0) {
PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
return NULL;
}

Py_RETURN_NONE;
}

static PyObject *
tracemalloc_untrack(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;

if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) {
return NULL;
}
void *ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred()) {
return NULL;
}

int res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
if (res < 0) {
PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
return NULL;
}

Py_RETURN_NONE;
}

static PyObject *
tracemalloc_get_traceback(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;

if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj)) {
return NULL;
}
void *ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred()) {
return NULL;
}

return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
}

static PyMethodDef test_methods[] = {
{"check_pyobject_forbidden_bytes_is_freed",
check_pyobject_forbidden_bytes_is_freed, METH_NOARGS},
Expand All @@ -617,6 +693,11 @@ static PyMethodDef test_methods[] = {
{"test_pymem_setrawallocators", test_pymem_setrawallocators, METH_NOARGS},
{"test_pyobject_new", test_pyobject_new, METH_NOARGS},
{"test_pyobject_setallocators", test_pyobject_setallocators, METH_NOARGS},

// Tracemalloc tests
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},
{"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
{"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
{NULL},
};

Expand Down
75 changes: 0 additions & 75 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2597,78 +2597,6 @@ getitem_with_error(PyObject *self, PyObject *args)
return PyObject_GetItem(map, key);
}

static PyObject *
tracemalloc_track(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;
void *ptr;
Py_ssize_t size;
int release_gil = 0;
int res;

if (!PyArg_ParseTuple(args, "IOn|i", &domain, &ptr_obj, &size, &release_gil))
return NULL;
ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred())
return NULL;

if (release_gil) {
Py_BEGIN_ALLOW_THREADS
res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
Py_END_ALLOW_THREADS
}
else {
res = PyTraceMalloc_Track(domain, (uintptr_t)ptr, size);
}

if (res < 0) {
PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Track error");
return NULL;
}

Py_RETURN_NONE;
}

static PyObject *
tracemalloc_untrack(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;
void *ptr;
int res;

if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
return NULL;
ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred())
return NULL;

res = PyTraceMalloc_Untrack(domain, (uintptr_t)ptr);
if (res < 0) {
PyErr_SetString(PyExc_RuntimeError, "PyTraceMalloc_Untrack error");
return NULL;
}

Py_RETURN_NONE;
}

static PyObject *
tracemalloc_get_traceback(PyObject *self, PyObject *args)
{
unsigned int domain;
PyObject *ptr_obj;
void *ptr;

if (!PyArg_ParseTuple(args, "IO", &domain, &ptr_obj))
return NULL;
ptr = PyLong_AsVoidPtr(ptr_obj);
if (PyErr_Occurred())
return NULL;

return _PyTraceMalloc_GetTraceback(domain, (uintptr_t)ptr);
}

static PyObject *
dict_get_version(PyObject *self, PyObject *args)
{
Expand Down Expand Up @@ -3922,9 +3850,6 @@ static PyMethodDef TestMethods[] = {
{"return_result_with_error", return_result_with_error, METH_NOARGS},
{"getitem_with_error", getitem_with_error, METH_VARARGS},
{"Py_CompileString", pycompilestring, METH_O},
{"tracemalloc_track", tracemalloc_track, METH_VARARGS},
{"tracemalloc_untrack", tracemalloc_untrack, METH_VARARGS},
{"tracemalloc_get_traceback", tracemalloc_get_traceback, METH_VARARGS},
{"dict_get_version", dict_get_version, METH_VARARGS},
{"raise_SIGINT_then_send_None", raise_SIGINT_then_send_None, METH_VARARGS},
{"stack_pointer", stack_pointer, METH_NOARGS},
Expand Down