|
| 1 | +/* Header file providing new functions of the Python C API |
| 2 | + for old Python versions. |
| 3 | +
|
| 4 | + File distributed under the MIT license. |
| 5 | + Homepage: https://github.com/pythoncapi/pythoncapi_compat. |
| 6 | +*/ |
| 7 | + |
| 8 | +#ifndef PYTHONCAPI_COMPAT |
| 9 | +#define PYTHONCAPI_COMPAT |
| 10 | + |
| 11 | +#ifdef __cplusplus |
| 12 | +extern "C" { |
| 13 | +#endif |
| 14 | + |
| 15 | +#include <Python.h> |
| 16 | +#include "frameobject.h" // PyFrameObject |
| 17 | + |
| 18 | + |
| 19 | +// bpo-39573: Py_TYPE(), Py_REFCNT() and Py_SIZE() can no longer be used |
| 20 | +// as l-value in Python 3.10. |
| 21 | +#if PY_VERSION_HEX < 0x030900A4 |
| 22 | +static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) |
| 23 | +{ |
| 24 | + ob->ob_refcnt = refcnt; |
| 25 | +} |
| 26 | +#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT((PyObject*)(ob), refcnt) |
| 27 | + |
| 28 | + |
| 29 | +static inline void |
| 30 | +_Py_SET_TYPE(PyObject *ob, PyTypeObject *type) |
| 31 | +{ |
| 32 | + ob->ob_type = type; |
| 33 | +} |
| 34 | +#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type) |
| 35 | + |
| 36 | +static inline void |
| 37 | +_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) |
| 38 | +{ |
| 39 | + ob->ob_size = size; |
| 40 | +} |
| 41 | +#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size) |
| 42 | + |
| 43 | +#endif // PY_VERSION_HEX < 0x030900A4 |
| 44 | + |
| 45 | + |
| 46 | +#if PY_VERSION_HEX < 0x030900B1 |
| 47 | +static inline PyCodeObject* |
| 48 | +PyFrame_GetCode(PyFrameObject *frame) |
| 49 | +{ |
| 50 | + assert(frame != NULL); |
| 51 | + PyCodeObject *code = frame->f_code; |
| 52 | + assert(code != NULL); |
| 53 | + Py_INCREF(code); |
| 54 | + return code; |
| 55 | +} |
| 56 | +#endif |
| 57 | + |
| 58 | + |
| 59 | +#if PY_VERSION_HEX < 0x030900B1 |
| 60 | +static inline PyFrameObject* |
| 61 | +PyFrame_GetBack(PyFrameObject *frame) |
| 62 | +{ |
| 63 | + assert(frame != NULL); |
| 64 | + PyFrameObject *back = frame->f_back; |
| 65 | + Py_XINCREF(back); |
| 66 | + return back; |
| 67 | +} |
| 68 | +#endif |
| 69 | + |
| 70 | + |
| 71 | +#if PY_VERSION_HEX < 0x030900A5 |
| 72 | +static inline PyInterpreterState * |
| 73 | +PyThreadState_GetInterpreter(PyThreadState *tstate) |
| 74 | +{ |
| 75 | + assert(tstate != NULL); |
| 76 | + return tstate->interp; |
| 77 | +} |
| 78 | +#endif |
| 79 | + |
| 80 | + |
| 81 | +#if PY_VERSION_HEX < 0x030900B1 |
| 82 | +static inline PyFrameObject* |
| 83 | +PyThreadState_GetFrame(PyThreadState *tstate) |
| 84 | +{ |
| 85 | + assert(tstate != NULL); |
| 86 | + PyFrameObject *frame = tstate->frame; |
| 87 | + Py_XINCREF(frame); |
| 88 | + return frame; |
| 89 | +} |
| 90 | +#endif |
| 91 | + |
| 92 | + |
| 93 | +#if PY_VERSION_HEX < 0x030900A5 |
| 94 | +static inline PyInterpreterState * |
| 95 | +PyInterpreterState_Get(void) |
| 96 | +{ |
| 97 | + PyThreadState *tstate = PyThreadState_GET(); |
| 98 | + if (tstate == NULL) { |
| 99 | + Py_FatalError("GIL released (tstate is NULL)"); |
| 100 | + } |
| 101 | + PyInterpreterState *interp = tstate->interp; |
| 102 | + if (interp == NULL) { |
| 103 | + Py_FatalError("no current interpreter"); |
| 104 | + } |
| 105 | + return interp; |
| 106 | +} |
| 107 | +#endif |
| 108 | + |
| 109 | + |
| 110 | +#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 |
| 111 | +static inline uint64_t |
| 112 | +PyThreadState_GetID(PyThreadState *tstate) |
| 113 | +{ |
| 114 | + assert(tstate != NULL); |
| 115 | + return tstate->id; |
| 116 | +} |
| 117 | +#endif |
| 118 | + |
| 119 | + |
| 120 | +#if PY_VERSION_HEX < 0x030900A1 |
| 121 | +static inline PyObject* |
| 122 | +PyObject_CallNoArgs(PyObject *func) |
| 123 | +{ |
| 124 | + return PyObject_CallFunctionObjArgs(func, NULL); |
| 125 | +} |
| 126 | +#endif |
| 127 | + |
| 128 | + |
| 129 | +#if PY_VERSION_HEX < 0x030900A5 |
| 130 | +static inline int |
| 131 | +PyModule_AddType(PyObject *module, PyTypeObject *type) |
| 132 | +{ |
| 133 | + if (PyType_Ready(type) < 0) { |
| 134 | + return -1; |
| 135 | + } |
| 136 | + |
| 137 | + // inline _PyType_Name() |
| 138 | + const char *name = type->tp_name; |
| 139 | + assert(name != NULL); |
| 140 | + const char *dot = strrchr(name, '.'); |
| 141 | + if (dot != NULL) { |
| 142 | + name = dot + 1; |
| 143 | + } |
| 144 | + |
| 145 | + Py_INCREF(type); |
| 146 | + if (PyModule_AddObject(module, name, (PyObject *)type) < 0) { |
| 147 | + Py_DECREF(type); |
| 148 | + return -1; |
| 149 | + } |
| 150 | + |
| 151 | + return 0; |
| 152 | +} |
| 153 | +#endif |
| 154 | + |
| 155 | + |
| 156 | +#if PY_VERSION_HEX < 0x030900A6 |
| 157 | +static inline int |
| 158 | +PyObject_GC_IsTracked(PyObject* obj) |
| 159 | +{ |
| 160 | + return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)); |
| 161 | +} |
| 162 | + |
| 163 | +static inline int |
| 164 | +PyObject_GC_IsFinalized(PyObject *obj) |
| 165 | +{ |
| 166 | + return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1)); |
| 167 | +} |
| 168 | +#endif // PY_VERSION_HEX < 0x030900A6 |
| 169 | + |
| 170 | + |
| 171 | +#ifdef __cplusplus |
| 172 | +} |
| 173 | +#endif |
| 174 | +#endif // PYTHONCAPI_COMPAT |
0 commit comments