Skip to content

Commit

Permalink
refactor: don't access frame structs directly
Browse files Browse the repository at this point in the history
They've been moved behind APIs since 3.10 and 3.11, but 3.13 forced our hand.
  • Loading branch information
nedbat committed Oct 1, 2023
1 parent 7b8dec9 commit 1ea3907
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
5 changes: 2 additions & 3 deletions coverage/ctracer/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,15 +508,14 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
Py_XDECREF(self->pcur_entry->file_data);
self->pcur_entry->file_data = NULL;
self->pcur_entry->file_tracer = Py_None;
frame->f_trace_lines = 0;
MyFrame_NoTraceLines(frame);
SHOWLOG(PyFrame_GetLineNumber(frame), filename, "skipped");
}

self->pcur_entry->disposition = disposition;

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

/* A call event is really a "start frame" event, and can happen for
* re-entering a generator also. How we tell the difference depends on
Expand Down
11 changes: 11 additions & 0 deletions coverage/ctracer/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@
// 3.11 moved f_lasti into an internal structure. This is totally the wrong way
// to make this work, but it's all I've got until https://bugs.python.org/issue40421
// is resolved.
#if PY_VERSION_HEX < 0x030D0000
#include <internal/pycore_frame.h>
#endif

#if PY_VERSION_HEX >= 0x030B00A7
#define MyFrame_GetLasti(f) (PyFrame_GetLasti(f))
#else
Expand All @@ -30,6 +33,14 @@
#define MyFrame_GetLasti(f) ((f)->f_lasti)
#endif

#if PY_VERSION_HEX >= 0x030D0000
#define MyFrame_NoTraceLines(f) (PyObject_SetAttrString((PyObject*)(f), "f_trace_lines", Py_False))
#define MyFrame_SetTrace(f, obj) (PyObject_SetAttrString((PyObject*)(f), "f_trace", (PyObject*)(obj)))
#else
#define MyFrame_NoTraceLines(f) ((f)->f_trace_lines = 0)
#define MyFrame_SetTrace(f, obj) {Py_INCREF(obj); Py_XSETREF((f)->f_trace, (PyObject*)(obj));}
#endif

// Access f_code should be done through a helper starting in 3.9.
#if PY_VERSION_HEX >= 0x03090000
#define MyFrame_GetCode(f) (PyFrame_GetCode(f))
Expand Down

0 comments on commit 1ea3907

Please sign in to comment.