forked from python/pythoncapi-compat
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpythoncapi_compat.h
More file actions
223 lines (180 loc) · 4.48 KB
/
pythoncapi_compat.h
File metadata and controls
223 lines (180 loc) · 4.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
// Header file providing new functions of the Python C API to Python 3.6.
//
// 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
#ifndef PYTHONCAPI_COMPAT
#define PYTHONCAPI_COMPAT
#ifdef __cplusplus
extern "C" {
#endif
#include <Python.h>
#include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
// bpo-42262 added Py_NewRef() and Py_XNewRef() to Python 3.10.0a3
#if PY_VERSION_HEX < 0x030a00A3
static inline PyObject* Py_NewRef(PyObject *obj)
{
Py_INCREF(obj);
return obj;
}
static inline PyObject* Py_XNewRef(PyObject *obj)
{
Py_XINCREF(obj);
return obj;
}
#endif
// bpo-42522
static inline PyObject* _Py_Borrow(PyObject *obj)
{
Py_DECREF(obj);
return obj;
}
static inline PyObject* _Py_XBorrow(PyObject *obj)
{
Py_XDECREF(obj);
return obj;
}
// bpo-39573: Py_TYPE(), Py_REFCNT() and Py_SIZE() can no longer be used
// as l-value in Python 3.10.
#if PY_VERSION_HEX < 0x030900A4
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*)(ob), refcnt)
static inline void
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
{
ob->ob_type = type;
}
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject*)(ob), type)
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 // PY_VERSION_HEX < 0x030900A4
#if PY_VERSION_HEX < 0x030900B1
static inline PyCodeObject*
PyFrame_GetCode(PyFrameObject *frame)
{
assert(frame != NULL);
PyCodeObject *code = frame->f_code;
assert(code != NULL);
Py_INCREF(code);
return code;
}
#endif
#if PY_VERSION_HEX < 0x030900B1
static inline PyFrameObject*
PyFrame_GetBack(PyFrameObject *frame)
{
assert(frame != NULL);
PyFrameObject *back = frame->f_back;
Py_XINCREF(back);
return back;
}
#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
static inline PyFrameObject*
PyThreadState_GetFrame(PyThreadState *tstate)
{
assert(tstate != NULL);
PyFrameObject *frame = tstate->frame;
Py_XINCREF(frame);
return frame;
}
#endif
#if PY_VERSION_HEX < 0x030900A5
static inline PyInterpreterState *
PyInterpreterState_Get(void)
{
PyThreadState *tstate = PyThreadState_GET();
if (tstate == NULL) {
Py_FatalError("GIL released (tstate is NULL)");
}
PyInterpreterState *interp = tstate->interp;
if (interp == NULL) {
Py_FatalError("no current interpreter");
}
return interp;
}
#endif
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6
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 < 0x030900A5
static inline int
PyModule_AddType(PyObject *module, PyTypeObject *type)
{
if (PyType_Ready(type) < 0) {
return -1;
}
// inline _PyType_Name()
const char *name = type->tp_name;
assert(name != NULL);
const char *dot = strrchr(name, '.');
if (dot != NULL) {
name = dot + 1;
}
Py_INCREF(type);
if (PyModule_AddObject(module, name, (PyObject *)type) < 0) {
Py_DECREF(type);
return -1;
}
return 0;
}
#endif
#if PY_VERSION_HEX < 0x030900A6
static inline int
PyObject_GC_IsTracked(PyObject* obj)
{
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
}
static inline int
PyObject_GC_IsFinalized(PyObject *obj)
{
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
}
#endif // PY_VERSION_HEX < 0x030900A6
#if PY_VERSION_HEX < 0x030900A4
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((const PyObject*)(ob), type)
#endif
#ifdef __cplusplus
}
#endif
#endif // PYTHONCAPI_COMPAT