8000 gh-93502: Add new C-API functions to trace object creation and destruction by pablogsal · Pull Request #115945 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

gh-93502: Add new C-API functions to trace object creation and destruction #115945

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
May 2, 2024
Merged
Prev Previous commit
Next Next commit
Address feedback
  • Loading branch information
pablogsal committed May 1, 2024
commit be2380d0fe3f158592d5c62a918b9572444fc220
25 changes: 16 additions & 9 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3227,7 +3227,7 @@ struct simpletracer_data {

static int _simpletracer(PyObject *obj, PyRefTracerEvent event, void* data) {
struct simpletracer_data* the_data = (struct simpletracer_data*)data;
assert(the_data->create_count + the_data->destroy_count < Py_ARRAY_LENGTH(the_data->addresses));
assert(the_data->create_count + the_data->destroy_count < (int)Py_ARRAY_LENGTH(the_data->addresses));
the_data->addresses[the_data->create_count + the_data->destroy_count] = obj;
if (event == PyRefTracer_CREATE) {
the_data->create_count++;
Expand All @@ -3240,30 +3240,34 @@ static int _simpletracer(PyObject *obj, PyRefTracerEvent event, void* data) {
static PyObject *
test_reftracer(PyObject *ob, PyObject *Py_UNUSED(ignored))
{
// Save the current tracer and data to restore it later
void* current_data;
PyRefTracer current_tracer = PyRefTracer_GetTracer(&current_data);

struct simpletracer_data tracer_data = {0};
void* the_data = &tracer_data;
// Install a simple tracer function
if (PyRefTracer_SetTracer(_simpletracer, the_data) != 0) {
return NULL;
goto failed;
}

// Check that the tracer was correctly installed
void* data;
if (PyRefTracer_GetTracer(&data) != _simpletracer || data != the_data) {
PyErr_SetString(PyExc_AssertionError, "The reftracer not correctly installed");
PyRefTracer_SetTracer(NULL, NULL);
return NULL;
goto failed;
}

// Create a bunch of objects
PyObject* obj = PyList_New(0);
if (obj == NULL) {
return NULL;
goto failed;
}
PyObject* obj2 = PyDict_New();
if (obj2 == NULL) {
Py_DECREF(obj);
return NULL;
goto failed;
}

// Kill all objects
Expand All @@ -3276,24 +3280,27 @@ test_reftracer(PyObject *ob, PyObject *Py_UNUSED(ignored))
// Check that the tracer was removed
if (PyRefTracer_GetTracer(&data) != NULL || data != NULL) {
PyErr_SetString(PyExc_ValueError, "The reftracer was not correctly removed");
return NULL;
goto failed;
}

if (tracer_data.create_count != 2 ||
tracer_data.addresses[0] != obj ||
tracer_data.addresses[1] != obj2) {
PyErr_SetString(PyExc_ValueError, "The object creation was not correctly traced");
return NULL;
goto failed;
}

if (tracer_data.destroy_count != 2 ||
tracer_data.addresses[2] != obj ||
tracer_data.addresses[3] != obj2) {
PyErr_SetString(PyExc_ValueError, "The object destruction was not correctly traced");
return NULL;
goto failed;
}

PyRefTracer_SetTracer(current_tracer, current_data);
Py_RETURN_NONE;
failed:
PyRefTracer_SetTracer(current_tracer, current_data);
return NULL;
}

static PyMethodDef TestMethods[] = {
Expand Down
0