Python C Extension - Access to PyInterpreterState Internal Data Structure

42 views Asked by At

I recently found that I could get the Python interpreter state by PyInterpreterState_Get() C-API. However, when I tried to access its members defined by struct _is in the extension code, during the setup stage, the compiler threw "incomplete definition of type 'struct _is'" error. I noticed that this data structure is defined in an internal header file internal/pycore_interp.h, which cannot be included in a C extension module. I wonder if there is any workaround for accessing PyInterpreterState structure in C extensions?

My code looks like this

#define PY_SSIZE_T_CLEAN
#include <Python.h>
// #include <pycore_interp.h> // Wouldn't work: file not found
// #include <internal/pycore_interp.h> // Wouldn't work: this header requires Py_BUILD_CORE define

static PyObject *
mymodule_get_details(PyObject *self, PyObject *args)
{
    PyInterpreterState *interp = PyInterpreterState_Get();
    void *obmalloc = interp->obmalloc; // Compilation Error: incomplete definition of type 'struct _is'

    ...
}

... // other stuff
0

There are 0 answers