Skip to content

Commit 1ea3907

Browse files
committed
refactor: don't access frame structs directly
They've been moved behind APIs since 3.10 and 3.11, but 3.13 forced our hand.
1 parent 7b8dec9 commit 1ea3907

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

coverage/ctracer/tracer.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -508,15 +508,14 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
508508
Py_XDECREF(self->pcur_entry->file_data);
509509
self->pcur_entry->file_data = NULL;
510510
self->pcur_entry->file_tracer = Py_None;
511-
frame->f_trace_lines = 0;
511+
MyFrame_NoTraceLines(frame);
512512
SHOWLOG(PyFrame_GetLineNumber(frame), filename, "skipped");
513513
}
514514

515515
self->pcur_entry->disposition = disposition;
516516

517517
/* Make the frame right in case settrace(gettrace()) happens. */
518-
Py_INCREF(self);
519-
Py_XSETREF(frame->f_trace, (PyObject*)self);
518+
MyFrame_SetTrace(frame, self);
520519

521520
/* A call event is really a "start frame" event, and can happen for
522521
* re-entering a generator also. How we tell the difference depends on

coverage/ctracer/util.h

+11
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@
1616
// 3.11 moved f_lasti into an internal structure. This is totally the wrong way
1717
// to make this work, but it's all I've got until https://bugs.python.org/issue40421
1818
// is resolved.
19+
#if PY_VERSION_HEX < 0x030D0000
1920
#include <internal/pycore_frame.h>
21+
#endif
22+
2023
#if PY_VERSION_HEX >= 0x030B00A7
2124
#define MyFrame_GetLasti(f) (PyFrame_GetLasti(f))
2225
#else
@@ -30,6 +33,14 @@
3033
#define MyFrame_GetLasti(f) ((f)->f_lasti)
3134
#endif
3235

36+
#if PY_VERSION_HEX >= 0x030D0000
37+
#define MyFrame_NoTraceLines(f) (PyObject_SetAttrString((PyObject*)(f), "f_trace_lines", Py_False))
38+
#define MyFrame_SetTrace(f, obj) (PyObject_SetAttrString((PyObject*)(f), "f_trace", (PyObject*)(obj)))
39+
#else
40+
#define MyFrame_NoTraceLines(f) ((f)->f_trace_lines = 0)
41+
#define MyFrame_SetTrace(f, obj) {Py_INCREF(obj); Py_XSETREF((f)->f_trace, (PyObject*)(obj));}
42+
#endif
43+
3344
// Access f_code should be done through a helper starting in 3.9.
3445
#if PY_VERSION_HEX >= 0x03090000
3546
#define MyFrame_GetCode(f) (PyFrame_GetCode(f))

0 commit comments

Comments
 (0)