|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#ifndef PYTHONCAPI_COMPAT |
|
|
#define PYTHONCAPI_COMPAT |
|
|
|
|
|
#ifdef __cplusplus |
|
|
extern "C" { |
|
|
#endif |
|
|
|
|
|
#include <Python.h> |
|
|
#include "frameobject.h" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if (defined(_MSC_VER) && _MSC_VER < 1900 \ |
|
|
&& !defined(__cplusplus) && !defined(inline)) |
|
|
# define inline __inline |
|
|
# define PYTHONCAPI_COMPAT_MSC_INLINE |
|
|
|
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#ifndef _PyObject_CAST |
|
|
# define _PyObject_CAST(op) ((PyObject*)(op)) |
|
|
#endif |
|
|
#ifndef _PyObject_CAST_CONST |
|
|
# define _PyObject_CAST_CONST(op) ((const PyObject*)(op)) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef) |
|
|
static inline PyObject* _Py_NewRef(PyObject *obj) |
|
|
{ |
|
|
Py_INCREF(obj); |
|
|
return obj; |
|
|
} |
|
|
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef) |
|
|
static inline PyObject* _Py_XNewRef(PyObject *obj) |
|
|
{ |
|
|
Py_XINCREF(obj); |
|
|
return obj; |
|
|
} |
|
|
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if !defined(_Py_StealRef) |
|
|
static inline PyObject* __Py_StealRef(PyObject *obj) |
|
|
{ |
|
|
Py_DECREF(obj); |
|
|
return obj; |
|
|
} |
|
|
#define _Py_StealRef(obj) __Py_StealRef(_PyObject_CAST(obj)) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if !defined(_Py_XStealRef) |
|
|
static inline PyObject* __Py_XStealRef(PyObject *obj) |
|
|
{ |
|
|
Py_XDECREF(obj); |
|
|
return obj; |
|
|
} |
|
|
#define _Py_XStealRef(obj) __Py_XStealRef(_PyObject_CAST(obj)) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT) |
|
|
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) |
|
|
{ |
|
|
ob->ob_refcnt = refcnt; |
|
|
} |
|
|
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API) |
|
|
#define Py_SETREF(op, op2) \ |
|
|
do { \ |
|
|
PyObject *_py_tmp = _PyObject_CAST(op); \ |
|
|
(op) = (op2); \ |
|
|
Py_DECREF(_py_tmp); \ |
|
|
} while (0) |
|
|
|
|
|
#define Py_XSETREF(op, op2) \ |
|
|
do { \ |
|
|
PyObject *_py_tmp = _PyObject_CAST(op); \ |
|
|
(op) = (op2); \ |
|
|
Py_XDECREF(_py_tmp); \ |
|
|
} while (0) |
|
|
#endif |
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE) |
|
|
static inline void |
|
|
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type) |
|
|
{ |
|
|
ob->ob_type = type; |
|
|
} |
|
|
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE) |
|
|
static inline void |
|
|
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) |
|
|
{ |
|
|
ob->ob_size = size; |
|
|
} |
|
|
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900B1 |
|
|
static inline PyCodeObject* |
|
|
PyFrame_GetCode(PyFrameObject *frame) |
|
|
{ |
|
|
assert(frame != NULL); |
|
|
assert(frame->f_code != NULL); |
|
|
return (PyCodeObject*)Py_NewRef(frame->f_code); |
|
|
} |
|
|
#endif |
|
|
|
|
|
static inline PyCodeObject* |
|
|
_PyFrame_GetCodeBorrow(PyFrameObject *frame) |
|
|
{ |
|
|
return (PyCodeObject *)_Py_StealRef(PyFrame_GetCode(frame)); |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION) |
|
|
static inline PyFrameObject* |
|
|
PyFrame_GetBack(PyFrameObject *frame) |
|
|
{ |
|
|
assert(frame != NULL); |
|
|
return (PyFrameObject*)Py_XNewRef(frame->f_back); |
|
|
} |
|
|
#endif |
|
|
|
|
|
#if !defined(PYPY_VERSION) |
|
|
static inline PyFrameObject* |
|
|
_PyFrame_GetBackBorrow(PyFrameObject *frame) |
|
|
{ |
|
|
return (PyFrameObject *)_Py_XStealRef(PyFrame_GetBack(frame)); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A5 |
|
|
static inline PyInterpreterState * |
|
|
PyThreadState_GetInterpreter(PyThreadState *tstate) |
|
|
{ |
|
|
assert(tstate != NULL); |
|
|
return tstate->interp; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION) |
|
|
static inline PyFrameObject* |
|
|
PyThreadState_GetFrame(PyThreadState *tstate) |
|
|
{ |
|
|
assert(tstate != NULL); |
|
|
return (PyFrameObject *)Py_XNewRef(tstate->frame); |
|
|
} |
|
|
#endif |
|
|
|
|
|
#if !defined(PYPY_VERSION) |
|
|
static inline PyFrameObject* |
|
|
_PyThreadState_GetFrameBorrow(PyThreadState *tstate) |
|
|
{ |
|
|
return (PyFrameObject *)_Py_XStealRef(PyThreadState_GetFrame(tstate)); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A5 |
|
|
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; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION) |
|
|
static inline uint64_t |
|
|
PyThreadState_GetID(PyThreadState *tstate) |
|
|
{ |
|
|
assert(tstate != NULL); |
|
|
return tstate->id; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A1 |
|
|
static inline PyObject* |
|
|
PyObject_CallNoArgs(PyObject *func) |
|
|
{ |
|
|
return PyObject_CallFunctionObjArgs(func, NULL); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A4 |
|
|
static inline PyObject* |
|
|
PyObject_CallOneArg(PyObject *func, PyObject *arg) |
|
|
{ |
|
|
return PyObject_CallFunctionObjArgs(func, arg, NULL); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030A00A3 |
|
|
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; |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A5 |
|
|
static inline int |
|
|
PyModule_AddType(PyObject *module, PyTypeObject *type) |
|
|
{ |
|
|
const char *name, *dot; |
|
|
|
|
|
if (PyType_Ready(type) < 0) { |
|
|
return -1; |
|
|
} |
|
|
|
|
|
|
|
|
name = type->tp_name; |
|
|
assert(name != NULL); |
|
|
dot = strrchr(name, '.'); |
|
|
if (dot != NULL) { |
|
|
name = dot + 1; |
|
|
} |
|
|
|
|
|
return PyModule_AddObjectRef(module, name, (PyObject *)type); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION) |
|
|
static inline int |
|
|
PyObject_GC_IsTracked(PyObject* obj) |
|
|
{ |
|
|
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION) |
|
|
static inline int |
|
|
PyObject_GC_IsFinalized(PyObject *obj) |
|
|
{ |
|
|
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1)); |
|
|
} |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE) |
|
|
static inline int |
|
|
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) { |
|
|
return ob->ob_type == type; |
|
|
} |
|
|
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type) |
|
|
#endif |
|
|
|
|
|
|
|
|
|
|
|
#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED) |
|
|
# if defined(__GNUC__) || defined(__clang__) |
|
|
# define Py_UNUSED(name) _unused_ ## name __attribute__((unused)) |
|
|
# else |
|
|
# define Py_UNUSED(name) _unused_ ## name |
|
|
# endif |
|
|
#endif |
|
|
|
|
|
|
|
|
#ifdef PYTHONCAPI_COMPAT_MSC_INLINE |
|
|
# undef inline |
|
|
# undef PYTHONCAPI_COMPAT_MSC_INLINE |
|
|
#endif |
|
|
|
|
|
#ifdef __cplusplus |
|
|
} |
|
|
#endif |
|
|
#endif |
|
|
|