Skip to content

Commit 8719c78

Browse files
tom-pytelcolesbury
authored andcommitted
[3.13] pythongh-130382: add missing _PyReftracerTrack to ceval Py_DECREF (pythonGH-130689)
(cherry picked from commit c5abded) Co-authored-by: Tomasz Pytel <[email protected]>
1 parent 050813c commit 8719c78

File tree

4 files changed

+45
-0
lines changed

4 files changed

+45
-0
lines changed

Lib/test/test_capi/test_misc.py

+17
Original file line numberDiff line numberDiff line change
@@ -3186,5 +3186,22 @@ def run(self):
31863186
py_thread_ids)
31873187

31883188

3189+
class TestCEval(unittest.TestCase):
3190+
def test_ceval_decref(self):
3191+
code = textwrap.dedent("""
3192+
import _testcapi
3193+
_testcapi.toggle_reftrace_printer(True)
3194+
l1 = []
3195+
l2 = []
3196+
del l1
3197+
del l2
3198+
_testcapi.toggle_reftrace_printer(False)
3199+
""")
3200+
_, out, _ = assert_python_ok("-c", code)
3201+
lines = out.decode("utf-8").splitlines()
3202+
self.assertEqual(lines.count("CREATE list"), 2)
3203+
self.assertEqual(lines.count("DESTROY list"), 2)
3204+
3205+
31893206
if __name__ == "__main__":
31903207
unittest.main()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix ``PyRefTracer_DESTROY`` not being sent from :file:`Python/ceval.c` ``Py_DECREF()``.

Modules/_testcapimodule.c

+26
Original file line numberDiff line numberDiff line change
@@ -3421,6 +3421,31 @@ tracemalloc_track_race(PyObject *self, PyObject *args)
34213421
#undef NTHREAD
34223422
}
34233423

3424+
static int
3425+
_reftrace_printer(PyObject *obj, PyRefTracerEvent event, void *counter_data)
3426+
{
3427+
if (event == PyRefTracer_CREATE) {
3428+
printf("CREATE %s\n", Py_TYPE(obj)->tp_name);
3429+
}
3430+
else { // PyRefTracer_DESTROY
3431+
printf("DESTROY %s\n", Py_TYPE(obj)->tp_name);
3432+
}
3433+
return 0;
3434+
}
3435+
3436+
// A simple reftrace printer for very simple tests
3437+
static PyObject *
3438+
toggle_reftrace_printer(PyObject *ob, PyObject *arg)
3439+
{
3440+
if (arg == Py_True) {
3441+
PyRefTracer_SetTracer(_reftrace_printer, NULL);
3442+
}
3443+
else {
3444+
PyRefTracer_SetTracer(NULL, NULL);
3445+
}
3446+
Py_RETURN_NONE;
3447+
}
3448+
34243449
static PyMethodDef TestMethods[] = {
34253450
{"set_errno", set_errno, METH_VARARGS},
34263451
{"test_config", test_config, METH_NOARGS},
@@ -3564,6 +3589,7 @@ static PyMethodDef TestMethods[] = {
35643589
{"test_critical_sections", test_critical_sections, METH_NOARGS},
35653590
{"test_atexit", test_atexit, METH_NOARGS},
35663591
{"tracemalloc_track_race", tracemalloc_track_race, METH_NOARGS},
3592+
{"toggle_reftrace_printer", toggle_reftrace_printer, METH_O},
35673593
{NULL, NULL} /* sentinel */
35683594
};
35693595

Python/ceval.c

+1
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
} \
7171
_Py_DECREF_STAT_INC(); \
7272
if (--op->ob_refcnt == 0) { \
73+
_PyReftracerTrack(op, PyRefTracer_DESTROY); \
7374
destructor dealloc = Py_TYPE(op)->tp_dealloc; \
7475
(*dealloc)(op); \
7576
} \

0 commit comments

Comments
 (0)