| 1 |
// Header file providing new functions of the Python C API to old Python |
| 2 |
// versions. |
| 3 |
// |
| 4 |
// File distributed under the MIT license. |
| 5 |
// |
| 6 |
// Homepage: |
| 7 |
// https://github.com/pythoncapi/pythoncapi_compat |
| 8 |
// |
| 9 |
// Latest version: |
| 10 |
// https://raw.githubusercontent.com/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h |
| 11 |
// |
| 12 |
// SPDX-License-Identifier: MIT |
| 13 |
|
| 14 |
#ifndef PYTHONCAPI_COMPAT |
| 15 |
#define PYTHONCAPI_COMPAT |
| 16 |
|
| 17 |
#ifdef __cplusplus |
| 18 |
extern "C" { |
| 19 |
#endif |
| 20 |
|
| 21 |
#include <Python.h> |
| 22 |
#include "frameobject.h" // PyFrameObject, PyFrame_GetBack() |
| 23 |
|
| 24 |
|
| 25 |
// Compatibility with Visual Studio 2013 and older which don't support |
| 26 |
// the inline keyword in C (only in C++): use __inline instead. |
| 27 |
#if (defined(_MSC_VER) && _MSC_VER < 1900 \ |
| 28 |
&& !defined(__cplusplus) && !defined(inline)) |
| 29 |
# define inline __inline |
| 30 |
# define PYTHONCAPI_COMPAT_MSC_INLINE |
| 31 |
// These two macros are undefined at the end of this file |
| 32 |
#endif |
| 33 |
|
| 34 |
|
| 35 |
// Cast argument to PyObject* type. |
| 36 |
#ifndef _PyObject_CAST |
| 37 |
# define _PyObject_CAST(op) ((PyObject*)(op)) |
| 38 |
#endif |
| 39 |
#ifndef _PyObject_CAST_CONST |
| 40 |
# define _PyObject_CAST_CONST(op) ((const PyObject*)(op)) |
| 41 |
#endif |
| 42 |
|
| 43 |
|
| 44 |
// bpo-42262 added Py_NewRef() to Python 3.10.0a3 |
| 45 |
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef) |
| 46 |
static inline PyObject* _Py_NewRef(PyObject *obj) |
| 47 |
{ |
| 48 |
Py_INCREF(obj); |
| 49 |
return obj; |
| 50 |
} |
| 51 |
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) |
| 52 |
#endif |
| 53 |
|
| 54 |
|
| 55 |
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3 |
| 56 |
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef) |
| 57 |
static inline PyObject* _Py_XNewRef(PyObject *obj) |
| 58 |
{ |
| 59 |
Py_XINCREF(obj); |
| 60 |
return obj; |
| 61 |
} |
| 62 |
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) |
| 63 |
#endif |
| 64 |
|
| 65 |
|
| 66 |
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4 |
| 67 |
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT) |
| 68 |
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) |
| 69 |
{ |
| 70 |
ob->ob_refcnt = refcnt; |
| 71 |
} |
| 72 |
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt) |
| 73 |
#endif |
| 74 |
|
| 75 |
|
| 76 |
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4 |
| 77 |
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE) |
| 78 |
static inline void |
| 79 |
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type) |
| 80 |
{ |
| 81 |
ob->ob_type = type; |
| 82 |
} |
| 83 |
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type) |
| 84 |
#endif |
| 85 |
|
| 86 |
|
| 87 |
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4 |
| 88 |
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE) |
| 89 |
static inline void |
| 90 |
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) |
| 91 |
{ |
| 92 |
ob->ob_size = size; |
| 93 |
} |
| 94 |
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size) |
| 95 |
#endif |
| 96 |
|
| 97 |
|
| 98 |
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1 |
| 99 |
#if PY_VERSION_HEX < 0x030900B1 |
| 100 |
static inline PyCodeObject* |
| 101 |
PyFrame_GetCode(PyFrameObject *frame) |
| 102 |
{ |
| 103 |
PyCodeObject *code; |
| 104 |
assert(frame != NULL); |
| 105 |
code = frame->f_code; |
| 106 |
assert(code != NULL); |
| 107 |
Py_INCREF(code); |
| 108 |
return code; |
| 109 |
} |
| 110 |
#endif |
| 111 |
|
| 112 |
static inline PyCodeObject* |
| 113 |
_PyFrame_GetCodeBorrow(PyFrameObject *frame) |
| 114 |
{ |
| 115 |
PyCodeObject *code = PyFrame_GetCode(frame); |
| 116 |
Py_DECREF(code); |
| 117 |
return code; // borrowed reference |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1 |
| 122 |
#if PY_VERSION_HEX < 0x030900B1 |
| 123 |
static inline PyFrameObject* |
| 124 |
PyFrame_GetBack(PyFrameObject *frame) |
| 125 |
{ |
| 126 |
PyFrameObject *back; |
| 127 |
assert(frame != NULL); |
| 128 |
back = frame->f_back; |
| 129 |
Py_XINCREF(back); |
| 130 |
return back; |
| 131 |
} |
| 132 |
#endif |
| 133 |
|
| 134 |
static inline PyFrameObject* |
| 135 |
_PyFrame_GetBackBorrow(PyFrameObject *frame) |
| 136 |
{ |
| 137 |
PyFrameObject *back = PyFrame_GetBack(frame); |
| 138 |
Py_XDECREF(back); |
| 139 |
return back; // borrowed reference |
| 140 |
} |
| 141 |
|
| 142 |
|
| 143 |
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5 |
| 144 |
#if PY_VERSION_HEX < 0x030900A5 |
| 145 |
static inline PyInterpreterState * |
| 146 |
PyThreadState_GetInterpreter(PyThreadState *tstate) |
| 147 |
{ |
| 148 |
assert(tstate != NULL); |
| 149 |
return tstate->interp; |
| 150 |
} |
| 151 |
#endif |
| 152 |
|
| 153 |
|
| 154 |
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1 |
| 155 |
#if PY_VERSION_HEX < 0x030900B1 |
| 156 |
static inline PyFrameObject* |
| 157 |
PyThreadState_GetFrame(PyThreadState *tstate) |
| 158 |
{ |
| 159 |
PyFrameObject *frame; |
| 160 |
assert(tstate != NULL); |
| 161 |
frame = tstate->frame; |
| 162 |
Py_XINCREF(frame); |
| 163 |
return frame; |
| 164 |
} |
| 165 |
#endif |
| 166 |
|
| 167 |
static inline PyFrameObject* |
| 168 |
_PyThreadState_GetFrameBorrow(PyThreadState *tstate) |
| 169 |
{ |
| 170 |
PyFrameObject *frame = PyThreadState_GetFrame(tstate); |
| 171 |
Py_XDECREF(frame); |
| 172 |
return frame; // borrowed reference |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5 |
| 177 |
#if PY_VERSION_HEX < 0x030900A5 |
| 178 |
static inline PyInterpreterState * |
| 179 |
PyInterpreterState_Get(void) |
| 180 |
{ |
| 181 |
PyThreadState *tstate; |
| 182 |
PyInterpreterState *interp; |
| 183 |
|
| 184 |
tstate = PyThreadState_GET(); |
| 185 |
if (tstate == NULL) { |
| 186 |
Py_FatalError("GIL released (tstate is NULL)"); |
| 187 |
} |
| 188 |
interp = tstate->interp; |
| 189 |
if (interp == NULL) { |
| 190 |
Py_FatalError("no current interpreter"); |
| 191 |
} |
| 192 |
return interp; |
| 193 |
} |
| 194 |
#endif |
| 195 |
|
| 196 |
|
| 197 |
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6 |
| 198 |
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 |
| 199 |
static inline uint64_t |
| 200 |
PyThreadState_GetID(PyThreadState *tstate) |
| 201 |
{ |
| 202 |
assert(tstate != NULL); |
| 203 |
return tstate->id; |
| 204 |
} |
| 205 |
#endif |
| 206 |
|
| 207 |
|
| 208 |
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1 |
| 209 |
#if PY_VERSION_HEX < 0x030900A1 |
| 210 |
static inline PyObject* |
| 211 |
PyObject_CallNoArgs(PyObject *func) |
| 212 |
{ |
| 213 |
return PyObject_CallFunctionObjArgs(func, NULL); |
| 214 |
} |
| 215 |
#endif |
| 216 |
|
| 217 |
|
| 218 |
// bpo-39245 made PyObject_CallOneArg() public (previously called |
| 219 |
// _PyObject_CallOneArg) in Python 3.9.0a4 |
| 220 |
#if PY_VERSION_HEX < 0x030900A4 |
| 221 |
static inline PyObject* |
| 222 |
PyObject_CallOneArg(PyObject *func, PyObject *arg) |
| 223 |
{ |
| 224 |
return PyObject_CallFunctionObjArgs(func, arg, NULL); |
| 225 |
} |
| 226 |
#endif |
| 227 |
|
| 228 |
|
| 229 |
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3 |
| 230 |
#if PY_VERSION_HEX < 0x030A00A3 |
| 231 |
static inline int |
| 232 |
PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value) |
| 233 |
{ |
| 234 |
int res; |
| 235 |
Py_XINCREF(value); |
| 236 |
res = PyModule_AddObject(module, name, value); |
| 237 |
if (res < 0) { |
| 238 |
Py_XDECREF(value); |
| 239 |
} |
| 240 |
return res; |
| 241 |
} |
| 242 |
#endif |
| 243 |
|
| 244 |
|
| 245 |
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5 |
| 246 |
#if PY_VERSION_HEX < 0x030900A5 |
| 247 |
static inline int |
| 248 |
PyModule_AddType(PyObject *module, PyTypeObject *type) |
| 249 |
{ |
| 250 |
const char *name, *dot; |
| 251 |
|
| 252 |
if (PyType_Ready(type) < 0) { |
| 253 |
return -1; |
| 254 |
} |
| 255 |
|
| 256 |
// inline _PyType_Name() |
| 257 |
name = type->tp_name; |
| 258 |
assert(name != NULL); |
| 259 |
dot = strrchr(name, '.'); |
| 260 |
if (dot != NULL) { |
| 261 |
name = dot + 1; |
| 262 |
} |
| 263 |
|
| 264 |
return PyModule_AddObjectRef(module, name, (PyObject *)type); |
| 265 |
} |
| 266 |
#endif |
| 267 |
|
| 268 |
|
| 269 |
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6. |
| 270 |
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2. |
| 271 |
#if PY_VERSION_HEX < 0x030900A6 |
| 272 |
static inline int |
| 273 |
PyObject_GC_IsTracked(PyObject* obj) |
| 274 |
{ |
| 275 |
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj)); |
| 276 |
} |
| 277 |
#endif |
| 278 |
|
| 279 |
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6. |
| 280 |
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final. |
| 281 |
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 |
| 282 |
static inline int |
| 283 |
PyObject_GC_IsFinalized(PyObject *obj) |
| 284 |
{ |
| 285 |
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1)); |
| 286 |
} |
| 287 |
#endif |
| 288 |
|
| 289 |
|
| 290 |
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4 |
| 291 |
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE) |
| 292 |
static inline int |
| 293 |
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) { |
| 294 |
return ob->ob_type == type; |
| 295 |
} |
| 296 |
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type) |
| 297 |
#endif |
| 298 |
|
| 299 |
|
| 300 |
#ifdef PYTHONCAPI_COMPAT_MSC_INLINE |
| 301 |
# undef inline |
| 302 |
# undef PYTHONCAPI_COMPAT_MSC_INLINE |
| 303 |
#endif |
| 304 |
|
| 305 |
#ifdef __cplusplus |
| 306 |
} |
| 307 |
#endif |
| 308 |
#endif // PYTHONCAPI_COMPAT |