Skip to content

Commit ab48a7b

Browse files
committed
refactor: Python 3.9 added an accessor for frame->f_code
This accessor is now required in 3.11, so let's use it.
1 parent 4e75bc6 commit ab48a7b

File tree

2 files changed

+18
-11
lines changed

2 files changed

+18
-11
lines changed

coverage/ctracer/tracer.c

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ CTracer_check_missing_return(CTracer *self, PyFrameObject *frame)
305305
goto error;
306306
}
307307
}
308-
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "missedreturn");
308+
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "missedreturn");
309309
self->pdata_stack->depth--;
310310
self->pcur_entry = &self->pdata_stack->stack[self->pdata_stack->depth];
311311
}
@@ -384,7 +384,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
384384
}
385385

386386
/* Check if we should trace this line. */
387-
filename = frame->f_code->co_filename;
387+
filename = MyFrame_GetCode(frame)->co_filename;
388388
disposition = PyDict_GetItem(self->should_trace_cache, filename);
389389
if (disposition == NULL) {
390390
if (PyErr_Occurred()) {
@@ -549,7 +549,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
549549
* real byte offset for a generator re-entry.
550550
*/
551551
if (frame->f_lasti < 0) {
552-
self->pcur_entry->last_line = -frame->f_code->co_firstlineno;
552+
self->pcur_entry->last_line = -MyFrame_GetCode(frame)->co_firstlineno;
553553
}
554554
else {
555555
self->pcur_entry->last_line = PyFrame_GetLineNumber(frame);
@@ -633,7 +633,7 @@ CTracer_handle_line(CTracer *self, PyFrameObject *frame)
633633

634634
STATS( self->stats.lines++; )
635635
if (self->pdata_stack->depth >= 0) {
636-
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "line");
636+
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "line");
637637
if (self->pcur_entry->file_data) {
638638
int lineno_from = -1;
639639
int lineno_to = -1;
@@ -714,14 +714,14 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
714714
* f_lasti before reading the byte.
715715
*/
716716
int bytecode = RETURN_VALUE;
717-
PyObject * pCode = frame->f_code->co_code;
717+
PyObject * pCode = MyFrame_GetCode(frame)->co_code;
718718
int lasti = MyFrame_lasti(frame);
719719

720720
if (lasti < PyBytes_GET_SIZE(pCode)) {
721721
bytecode = PyBytes_AS_STRING(pCode)[lasti];
722722
}
723723
if (bytecode != YIELD_VALUE) {
724-
int first = frame->f_code->co_firstlineno;
724+
int first = MyFrame_GetCode(frame)->co_firstlineno;
725725
if (CTracer_record_pair(self, self->pcur_entry->last_line, -first) < 0) {
726726
goto error;
727727
}
@@ -744,7 +744,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
744744
}
745745

746746
/* Pop the stack. */
747-
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "return");
747+
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "return");
748748
self->pdata_stack->depth--;
749749
self->pcur_entry = &self->pdata_stack->stack[self->pdata_stack->depth];
750750
}
@@ -775,7 +775,7 @@ CTracer_handle_exception(CTracer *self, PyFrameObject *frame)
775775
*/
776776
STATS( self->stats.exceptions++; )
777777
self->last_exc_back = frame->f_back;
778-
self->last_exc_firstlineno = frame->f_code->co_firstlineno;
778+
self->last_exc_firstlineno = MyFrame_GetCode(frame)->co_firstlineno;
779779

780780
return RET_OK;
781781
}
@@ -806,14 +806,14 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse
806806

807807
#if WHAT_LOG
808808
if (what <= (int)(sizeof(what_sym)/sizeof(const char *))) {
809-
ascii = PyUnicode_AsASCIIString(frame->f_code->co_filename);
809+
ascii = PyUnicode_AsASCIIString(MyFrame_GetCode(frame)->co_filename);
810810
printf("trace: %s @ %s %d\n", what_sym[what], PyBytes_AS_STRING(ascii), PyFrame_GetLineNumber(frame));
811811
Py_DECREF(ascii);
812812
}
813813
#endif
814814

815815
#if TRACE_LOG
816-
ascii = PyUnicode_AsASCIIString(frame->f_code->co_filename);
816+
ascii = PyUnicode_AsASCIIString(MyFrame_GetCode(frame)->co_filename);
817817
if (strstr(PyBytes_AS_STRING(ascii), start_file) && PyFrame_GetLineNumber(frame) == start_line) {
818818
logging = TRUE;
819819
}
@@ -930,7 +930,7 @@ CTracer_call(CTracer *self, PyObject *args, PyObject *kwds)
930930
}
931931

932932
#if WHAT_LOG
933-
ascii = PyUnicode_AsASCIIString(frame->f_code->co_filename);
933+
ascii = PyUnicode_AsASCIIString(MyFrame_GetCode(frame)->co_filename);
934934
printf("pytrace: %s @ %s %d\n", what_sym[what], PyBytes_AS_STRING(ascii), PyFrame_GetLineNumber(frame));
935935
Py_DECREF(ascii);
936936
#endif

coverage/ctracer/util.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@
2020
#define MyFrame_lasti(f) f->f_lasti
2121
#endif // 3.10.0a7
2222

23+
// Access f_code should be done through a helper starting in 3.9.
24+
#if PY_VERSION_HEX >= 0x03090000
25+
#define MyFrame_GetCode(f) (PyFrame_GetCode(f))
26+
#else
27+
#define MyFrame_GetCode(f) ((f)->f_code)
28+
#endif // 3.11
29+
2330
/* The values returned to indicate ok or error. */
2431
#define RET_OK 0
2532
#define RET_ERROR -1

0 commit comments

Comments
 (0)