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