Skip to content

Nedbat/bug1184 #1185

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 2 commits into from
Jul 8, 2021
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
22 changes: 11 additions & 11 deletions coverage/ctracer/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ CTracer_check_missing_return(CTracer *self, PyFrameObject *frame)
goto error;
}
}
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "missedreturn");
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "missedreturn");
self->pdata_stack->depth--;
self->pcur_entry = &self->pdata_stack->stack[self->pdata_stack->depth];
}
Expand Down Expand Up @@ -384,7 +384,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
}

/* Check if we should trace this line. */
filename = frame->f_code->co_filename;
filename = MyFrame_GetCode(frame)->co_filename;
disposition = PyDict_GetItem(self->should_trace_cache, filename);
if (disposition == NULL) {
if (PyErr_Occurred()) {
Expand Down Expand Up @@ -549,7 +549,7 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
* real byte offset for a generator re-entry.
*/
if (frame->f_lasti < 0) {
self->pcur_entry->last_line = -frame->f_code->co_firstlineno;
self->pcur_entry->last_line = -MyFrame_GetCode(frame)->co_firstlineno;
}
else {
self->pcur_entry->last_line = PyFrame_GetLineNumber(frame);
Expand Down Expand Up @@ -633,7 +633,7 @@ CTracer_handle_line(CTracer *self, PyFrameObject *frame)

STATS( self->stats.lines++; )
if (self->pdata_stack->depth >= 0) {
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "line");
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "line");
if (self->pcur_entry->file_data) {
int lineno_from = -1;
int lineno_to = -1;
Expand Down Expand Up @@ -714,14 +714,14 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
* f_lasti before reading the byte.
*/
int bytecode = RETURN_VALUE;
PyObject * pCode = frame->f_code->co_code;
PyObject * pCode = MyFrame_GetCode(frame)->co_code;
int lasti = MyFrame_lasti(frame);

if (lasti < PyBytes_GET_SIZE(pCode)) {
bytecode = PyBytes_AS_STRING(pCode)[lasti];
}
if (bytecode != YIELD_VALUE) {
int first = frame->f_code->co_firstlineno;
int first = MyFrame_GetCode(frame)->co_firstlineno;
if (CTracer_record_pair(self, self->pcur_entry->last_line, -first) < 0) {
goto error;
}
Expand All @@ -744,7 +744,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
}

/* Pop the stack. */
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), frame->f_code->co_filename, "return");
SHOWLOG(self->pdata_stack->depth, PyFrame_GetLineNumber(frame), MyFrame_GetCode(frame)->co_filename, "return");
self->pdata_stack->depth--;
self->pcur_entry = &self->pdata_stack->stack[self->pdata_stack->depth];
}
Expand Down Expand Up @@ -775,7 +775,7 @@ CTracer_handle_exception(CTracer *self, PyFrameObject *frame)
*/
STATS( self->stats.exceptions++; )
self->last_exc_back = frame->f_back;
self->last_exc_firstlineno = frame->f_code->co_firstlineno;
self->last_exc_firstlineno = MyFrame_GetCode(frame)->co_firstlineno;

return RET_OK;
}
Expand Down Expand Up @@ -806,14 +806,14 @@ CTracer_trace(CTracer *self, PyFrameObject *frame, int what, PyObject *arg_unuse

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

#if TRACE_LOG
ascii = PyUnicode_AsASCIIString(frame->f_code->co_filename);
ascii = PyUnicode_AsASCIIString(MyFrame_GetCode(frame)->co_filename);
if (strstr(PyBytes_AS_STRING(ascii), start_file) && PyFrame_GetLineNumber(frame) == start_line) {
logging = TRUE;
}
Expand Down Expand Up @@ -930,7 +930,7 @@ CTracer_call(CTracer *self, PyObject *args, PyObject *kwds)
}

#if WHAT_LOG
ascii = PyUnicode_AsASCIIString(frame->f_code->co_filename);
ascii = PyUnicode_AsASCIIString(MyFrame_GetCode(frame)->co_filename);
printf("pytrace: %s @ %s %d\n", what_sym[what], PyBytes_AS_STRING(ascii), PyFrame_GetLineNumber(frame));
Py_DECREF(ascii);
#endif
Expand Down
7 changes: 7 additions & 0 deletions coverage/ctracer/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,13 @@
#define MyFrame_lasti(f) f->f_lasti
#endif // 3.10.0a7

// 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))
#else
#define MyFrame_GetCode(f) ((f)->f_code)
#endif // 3.11

/* The values returned to indicate ok or error. */
#define RET_OK 0
#define RET_ERROR -1
Expand Down
17 changes: 17 additions & 0 deletions tests/test_arcs.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,23 @@ def test_what_is_the_sound_of_no_lines_clapping(self):
arcz_missing=arcz_missing,
)

def test_bug_1184(self):
self.check_coverage("""\
def foo(x):
if x:
try:
1/(x - 1)
except ZeroDivisionError:
pass
return x # 7

for i in range(3): # 9
foo(i)
""",
arcz=".1 19 9-1 .2 23 27 34 47 56 67 7-1 9A A9",
arcz_unpredicted="45",
)


class WithTest(CoverageTest):
"""Arc-measuring tests involving context managers."""
Expand Down