Spaces:
Running
Running
// Header file providing new functions of the Python C API to old Python | |
// versions. | |
// | |
// File distributed under the MIT license. | |
// | |
// Homepage: | |
// https://github.com/pythoncapi/pythoncapi_compat | |
// | |
// Latest version: | |
// https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h | |
// | |
// SPDX-License-Identifier: MIT | |
extern "C" { | |
// Compatibility with Visual Studio 2013 and older which don't support | |
// the inline keyword in C (only in C++): use __inline instead. | |
// These two macros are undefined at the end of this file | |
// Cast argument to PyObject* type. | |
// bpo-42262 added Py_NewRef() to Python 3.10.0a3 | |
static inline PyObject* _Py_NewRef(PyObject *obj) | |
{ | |
Py_INCREF(obj); | |
return obj; | |
} | |
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3 | |
static inline PyObject* _Py_XNewRef(PyObject *obj) | |
{ | |
Py_XINCREF(obj); | |
return obj; | |
} | |
// See https://bugs.python.org/issue42522 | |
static inline PyObject* __Py_StealRef(PyObject *obj) | |
{ | |
Py_DECREF(obj); | |
return obj; | |
} | |
// See https://bugs.python.org/issue42522 | |
static inline PyObject* __Py_XStealRef(PyObject *obj) | |
{ | |
Py_XDECREF(obj); | |
return obj; | |
} | |
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4 | |
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) | |
{ | |
ob->ob_refcnt = refcnt; | |
} | |
// Py_SETREF() and Py_XSETREF() were added to Python 3.5.2. | |
// It is excluded from the limited C API. | |
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4 | |
static inline void | |
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type) | |
{ | |
ob->ob_type = type; | |
} | |
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4 | |
static inline void | |
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) | |
{ | |
ob->ob_size = size; | |
} | |
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1 | |
static inline PyCodeObject* | |
PyFrame_GetCode(PyFrameObject *frame) | |
{ | |
assert(frame != NULL); | |
assert(frame->f_code != NULL); | |
return (PyCodeObject*)Py_NewRef(frame->f_code); | |
} | |
static inline PyCodeObject* | |
_PyFrame_GetCodeBorrow(PyFrameObject *frame) | |
{ | |
return (PyCodeObject *)_Py_StealRef(PyFrame_GetCode(frame)); | |
} | |
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1 | |
static inline PyFrameObject* | |
PyFrame_GetBack(PyFrameObject *frame) | |
{ | |
assert(frame != NULL); | |
return (PyFrameObject*)Py_XNewRef(frame->f_back); | |
} | |
static inline PyFrameObject* | |
_PyFrame_GetBackBorrow(PyFrameObject *frame) | |
{ | |
return (PyFrameObject *)_Py_XStealRef(PyFrame_GetBack(frame)); | |
} | |
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5 | |
static inline PyInterpreterState * | |
PyThreadState_GetInterpreter(PyThreadState *tstate) | |
{ | |
assert(tstate != NULL); | |
return tstate->interp; | |
} | |
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1 | |
static inline PyFrameObject* | |
PyThreadState_GetFrame(PyThreadState *tstate) | |
{ | |
assert(tstate != NULL); | |
return (PyFrameObject *)Py_XNewRef(tstate->frame); | |
} | |
static inline PyFrameObject* | |
_PyThreadState_GetFrameBorrow(PyThreadState *tstate) | |
{ | |
return (PyFrameObject *)_Py_XStealRef(PyThreadState_GetFrame(tstate)); | |
} | |
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5 | |
static inline PyInterpreterState * | |
PyInterpreterState_Get(void) | |
{ | |
PyThreadState *tstate; | |
PyInterpreterState *interp; | |
tstate = PyThreadState_GET(); | |
if (tstate == NULL) { | |
Py_FatalError("GIL released (tstate is NULL)"); | |
} | |
interp = tstate->interp; | |
if (interp == NULL) { | |
Py_FatalError("no current interpreter"); | |
} | |
return interp; | |
} | |
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6 | |
static inline uint64_t | |
PyThreadState_GetID(PyThreadState *tstate) | |
{ | |
assert(tstate != NULL); | |
return tstate->id; | |
} | |
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1 | |
static inline PyObject* | |
PyObject_CallNoArgs(PyObject *func) | |
{ | |
return PyObject_CallFunctionObjArgs(func, NULL); | |
} | |
// bpo-39245 made PyObject_CallOneArg() public (previously called | |
// _PyObject_CallOneArg) in Python 3.9.0a4 | |
static inline PyObject* | |
PyObject_CallOneArg(PyObject *func, PyObject *arg) | |
{ | |
return PyObject_CallFunctionObjArgs(func, arg, NULL); | |
} | |
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3 | |
static inline int | |
PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value) | |
{ | |
int res; | |
Py_XINCREF(value); | |
res = PyModule_AddObject(module, name, value); | |
if (res < 0) { | |
Py_XDECREF(value); | |
} | |
return res; | |
} | |
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5 | |
static inline int | |
PyModule_AddType(PyObject *module, PyTypeObject *type) | |
{ | |
const char *name, *dot; | |
if (PyType_Ready(type) < 0) { | |
return -1; | |
} | |
// inline _PyType_Name() | |
name = type->tp_name; | |
assert(name != NULL); | |
dot = strrchr(name, '.'); | |
if (dot != NULL) { | |
name = dot + 1; | |
} | |
return PyModule_AddObjectRef(module, name, (PyObject *)type); | |
} | |
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6. | |
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2. | |
static inline int | |
PyObject_GC_IsTracked(PyObject* obj) | |
{ | |
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)); | |
} | |
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6. | |
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final. | |
static inline int | |
PyObject_GC_IsFinalized(PyObject *obj) | |
{ | |
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1)); | |
} | |
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4 | |
static inline int | |
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) { | |
return ob->ob_type == type; | |
} | |
// Py_UNUSED() was added to Python 3.4.0b2. | |
} | |