Skip to content

[3.13] gh-130382: add missing _PyReftracerTrack to ceval Py_DECREF (GH-130689) #131195

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 1 commit into from
Mar 14, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
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
17 changes: 17 additions & 0 deletions Lib/test/test_capi/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3186,5 +3186,22 @@ def run(self):
py_thread_ids)


class TestCEval(unittest.TestCase):
def test_ceval_decref(self):
code = textwrap.dedent("""
import _testcapi
_testcapi.toggle_reftrace_printer(True)
l1 = []
l2 = []
del l1
del l2
_testcapi.toggle_reftrace_printer(False)
""")
_, out, _ = assert_python_ok("-c", code)
lines = out.decode("utf-8").splitlines()
self.assertEqual(lines.count("CREATE list"), 2)
self.assertEqual(lines.count("DESTROY list"), 2)


if __name__ == "__main__":
unittest.main()
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix ``PyRefTracer_DESTROY`` not being sent from :file:`Python/ceval.c` ``Py_DECREF()``.
26 changes: 26 additions & 0 deletions Modules/_testcapimodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3421,6 +3421,31 @@ tracemalloc_track_race(PyObject *self, PyObject *args)
#undef NTHREAD
}

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);
}
return 0;
}

// A simple reftrace printer for very simple tests
static PyObject *
toggle_reftrace_printer(PyObject *ob, PyObject *arg)
{
if (arg == Py_True) {
PyRefTracer_SetTracer(_reftrace_printer, NULL);
}
else {
PyRefTracer_SetTracer(NULL, NULL);
}
Py_RETURN_NONE;
}

static PyMethodDef TestMethods[] = {
{"set_errno", set_errno, METH_VARARGS},
{"test_config", test_config, METH_NOARGS},
Expand Down Expand Up @@ -3564,6 +3589,7 @@ static PyMethodDef TestMethods[] = {
{"test_critical_sections", test_critical_sections, METH_NOARGS},
{"test_atexit", test_atexit, METH_NOARGS},
{"tracemalloc_track_race", tracemalloc_track_race, METH_NOARGS},
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
{NULL, NULL} /* sentinel */
};

Expand Down
1 change: 1 addition & 0 deletions Python/ceval.c
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@
} \
_Py_DECREF_STAT_INC(); \
if (--op->ob_refcnt == 0) { \
_PyReftracerTrack(op, PyRefTracer_DESTROY); \
destructor dealloc = Py_TYPE(op)->tp_dealloc; \
(*dealloc)(op); \
} \
Expand Down
Loading