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