gh-138794: Communicate to PyRefTracer when they are being replaced (#138797)

This commit is contained in:
Pablo Galindo Salgado 2025-09-15 11:12:09 +01:00 committed by GitHub
parent baf7470515
commit f01181b595
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 54 additions and 9 deletions

View file

@ -2319,6 +2319,7 @@ test_weakref_capi(PyObject *Py_UNUSED(module), PyObject *Py_UNUSED(args))
struct simpletracer_data {
int create_count;
int destroy_count;
int tracker_removed;
void* addresses[10];
};
@ -2326,10 +2327,18 @@ 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 < (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++;
} else {
the_data->destroy_count++;
switch (event) {
case PyRefTracer_CREATE:
the_data->create_count++;
break;
case PyRefTracer_DESTROY:
the_data->destroy_count++;
break;
case PyRefTracer_TRACKER_REMOVED:
the_data->tracker_removed++;
break;
default:
return -1;
}
return 0;
}
@ -2393,6 +2402,10 @@ test_reftracer(PyObject *ob, PyObject *Py_UNUSED(ignored))
PyErr_SetString(PyExc_ValueError, "The object destruction was not correctly traced");
goto failed;
}
if (tracer_data.tracker_removed != 1) {
PyErr_SetString(PyExc_ValueError, "The tracker removal was not correctly traced");
goto failed;
}
PyRefTracer_SetTracer(current_tracer, current_data);
Py_RETURN_NONE;
failed:
@ -2533,11 +2546,15 @@ code_offset_to_line(PyObject* self, PyObject* const* args, Py_ssize_t nargsf)
static int
_reftrace_printer(PyObject *obj, PyRefTracerEvent event, void *counter_data)
{
if (event == PyRefTracer_CREATE) {
printf("CREATE %s\n", Py_TYPE(obj)->tp_name);
}
else { // PyRefTracer_DESTROY
printf("DESTROY %s\n", Py_TYPE(obj)->tp_name);
switch (event) {
case PyRefTracer_CREATE:
printf("CREATE %s\n", Py_TYPE(obj)->tp_name);
break;
case PyRefTracer_DESTROY:
printf("DESTROY %s\n", Py_TYPE(obj)->tp_name);
break;
case PyRefTracer_TRACKER_REMOVED:
return 0;
}
return 0;
}