Skip to content

Commit

Permalink
fix: fix compilation errors on latest 3.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nedbat committed May 12, 2022
1 parent 9097c0d commit 956f0fd
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 14 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ jobs:
- "3.8"
- "3.9"
- "3.10"
- "3.11.0-alpha.5"
- "3.11.0-beta.1"
- "pypy-3.7"
exclude:
# Windows PyPy doesn't seem to work?
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/kit.yml
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ jobs:
py:
# PYVERSIONS. Available versions:
# https://github.com/actions/python-versions/blob/main/versions-manifest.json
- "3.11.0-alpha.5"
- "3.11.0-beta.1"
fail-fast: false

steps:
Expand Down
5 changes: 4 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,10 @@ development at the same time, such as 4.5.x and 5.0.
Unreleased
----------

Nothing yet.
- Fix: Coverage.py now builds successfully on CPython 3.11 (3.11.0b1) again.
Closes `issue 1367`_. Some results for generators may have changed.

.. _issue 1367: https://github.com/nedbat/coveragepy/issues/1367


.. _changes_632:
Expand Down
2 changes: 1 addition & 1 deletion README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ Coverage.py runs on these versions of Python:

.. PYVERSIONS
* CPython 3.7 through 3.11.0a5.
* CPython 3.7 through 3.11.0b1.
* PyPy3 7.3.8.

Documentation is on `Read the Docs`_. Code repository and issue tracker are on
Expand Down
21 changes: 15 additions & 6 deletions coverage/ctracer/tracer.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,6 +307,9 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
PyObject * plugin = NULL;
PyObject * plugin_name = NULL;
PyObject * next_tracename = NULL;
#ifdef RESUME
PyObject * pCode = NULL;
#endif

/* Borrowed references. */
PyObject * filename = NULL;
Expand Down Expand Up @@ -526,16 +529,16 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
*/
BOOL real_call = FALSE;

#ifdef RESUME // 3.11.0a4
#ifdef RESUME
/*
* The current opcode is guaranteed to be RESUME. The argument
* determines what kind of resume it is.
*/
PyObject * pCode = MyFrame_GetCode(frame)->co_code;
real_call = (PyBytes_AS_STRING(pCode)[MyFrame_lasti(frame) + 1] == 0);
pCode = MyCode_GetCode(MyFrame_GetCode(frame));
real_call = (PyBytes_AS_STRING(pCode)[MyFrame_GetLasti(frame) + 1] == 0);
#else
// f_lasti is -1 for a true call, and a real byte offset for a generator re-entry.
real_call = (MyFrame_lasti(frame) < 0);
real_call = (MyFrame_GetLasti(frame) < 0);
#endif

if (real_call) {
Expand All @@ -549,6 +552,9 @@ CTracer_handle_call(CTracer *self, PyFrameObject *frame)
ret = RET_OK;

error:
#ifdef RESUME
MyCode_FreeCode(pCode);
#endif
Py_XDECREF(next_tracename);
Py_XDECREF(disposition);
Py_XDECREF(plugin);
Expand Down Expand Up @@ -689,6 +695,8 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
{
int ret = RET_ERROR;

PyObject * pCode = NULL;

STATS( self->stats.returns++; )
/* A near-copy of this code is above in the missing-return handler. */
if (CTracer_set_pdata_stack(self) < 0) {
Expand All @@ -699,8 +707,8 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)
if (self->pdata_stack->depth >= 0) {
if (self->tracing_arcs && self->pcur_entry->file_data) {
BOOL real_return = FALSE;
PyObject * pCode = MyFrame_GetCode(frame)->co_code;
int lasti = MyFrame_lasti(frame);
pCode = MyCode_GetCode(MyFrame_GetCode(frame));
int lasti = MyFrame_GetLasti(frame);
Py_ssize_t code_size = PyBytes_GET_SIZE(pCode);
unsigned char * code_bytes = (unsigned char *)PyBytes_AS_STRING(pCode);
#ifdef RESUME
Expand Down Expand Up @@ -760,6 +768,7 @@ CTracer_handle_return(CTracer *self, PyFrameObject *frame)

error:

MyCode_FreeCode(pCode);
return ret;
}

Expand Down
18 changes: 15 additions & 3 deletions coverage/ctracer/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,17 @@
// to make this work, but it's all I've got until https://bugs.python.org/issue40421
// is resolved.
#include <internal/pycore_frame.h>
#define MyFrame_lasti(f) ((f)->f_frame->f_lasti * 2)
#if PY_VERSION_HEX >= 0x030B00A7
#define MyFrame_GetLasti(f) (PyFrame_GetLasti(f))
#else
#define MyFrame_GetLasti(f) ((f)->f_frame->f_lasti * 2)
#endif
#elif PY_VERSION_HEX >= 0x030A00A7
// The f_lasti field changed meaning in 3.10.0a7. It had been bytes, but
// now is instructions, so we need to adjust it to use it as a byte index.
#define MyFrame_lasti(f) ((f)->f_lasti * 2)
#define MyFrame_GetLasti(f) ((f)->f_lasti * 2)
#else
#define MyFrame_lasti(f) ((f)->f_lasti)
#define MyFrame_GetLasti(f) ((f)->f_lasti)
#endif

// Access f_code should be done through a helper starting in 3.9.
Expand All @@ -33,6 +37,14 @@
#define MyFrame_GetCode(f) ((f)->f_code)
#endif

#if PY_VERSION_HEX >= 0x030B00A7
#define MyCode_GetCode(co) (PyObject_GetAttrString((PyObject *)(co), "co_code"))
#define MyCode_FreeCode(code) Py_XDECREF(code)
#else
#define MyCode_GetCode(co) ((co)->co_code)
#define MyCode_FreeCode(code)
#endif

/* The values returned to indicate ok or error. */
#define RET_OK 0
#define RET_ERROR -1
Expand Down
2 changes: 1 addition & 1 deletion doc/index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ supported on:

.. PYVERSIONS
* Python versions 3.7 through 3.11.0a5.
* Python versions 3.7 through 3.11.0b1.

* PyPy3 7.3.8.

Expand Down

0 comments on commit 956f0fd

Please sign in to comment.