@@ -27,24 +27,34 @@ extern "C" {
27
27
// the inline keyword in C (only in C++): use __inline instead.
28
28
#if (defined(_MSC_VER ) && _MSC_VER < 1900 \
29
29
&& !defined(__cplusplus ) && !defined(inline ))
30
- # define inline __inline
31
- # define PYTHONCAPI_COMPAT_MSC_INLINE
32
- // These two macros are undefined at the end of this file
30
+ # define PYCAPI_COMPAT_INLINE ( TYPE static __inline TYPE
31
+ #else
32
+ # define PYCAPI_COMPAT_STATIC_INLINE ( TYPE ) static inline TYPE
33
33
#endif
34
34
35
35
36
+ // C++ compatibility
37
+ #ifdef __cplusplus
38
+ # define PYCAPI_COMPAT_CAST (TYPE , EXPR ) reinterpret_cast<TYPE>(EXPR)
39
+ # define PYCAPI_COMPAT_NULL nullptr
40
+ #else
41
+ # define PYCAPI_COMPAT_CAST (TYPE , EXPR ) (TYPE)(EXPR)
42
+ # define PYCAPI_COMPAT_NULL NULL
43
+ #endif
44
+
36
45
// Cast argument to PyObject* type.
37
46
#ifndef _PyObject_CAST
38
- # define _PyObject_CAST (op ) (( PyObject*)(op) )
47
+ # define _PyObject_CAST (op ) PYCAPI_COMPAT_CAST( PyObject*, op )
39
48
#endif
40
49
#ifndef _PyObject_CAST_CONST
41
- # define _PyObject_CAST_CONST (op ) (( const PyObject*)(op) )
50
+ # define _PyObject_CAST_CONST (op ) PYCAPI_COMPAT_CAST( const PyObject*, op )
42
51
#endif
43
52
44
53
45
54
// bpo-42262 added Py_NewRef() to Python 3.10.0a3
46
55
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef )
47
- static inline PyObject * _Py_NewRef (PyObject * obj )
56
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
57
+ _Py_NewRef (PyObject * obj )
48
58
{
49
59
Py_INCREF (obj );
50
60
return obj ;
@@ -55,7 +65,8 @@ static inline PyObject* _Py_NewRef(PyObject *obj)
55
65
56
66
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3
57
67
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef )
58
- static inline PyObject * _Py_XNewRef (PyObject * obj )
68
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
69
+ _Py_XNewRef (PyObject * obj )
59
70
{
60
71
Py_XINCREF (obj );
61
72
return obj ;
@@ -66,7 +77,8 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
66
77
67
78
// See https://bugs.python.org/issue42522
68
79
#if !defined(_Py_StealRef )
69
- static inline PyObject * __Py_StealRef (PyObject * obj )
80
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
81
+ __Py_StealRef (PyObject * obj )
70
82
{
71
83
Py_DECREF (obj );
72
84
return obj ;
@@ -77,7 +89,8 @@ static inline PyObject* __Py_StealRef(PyObject *obj)
77
89
78
90
// See https://bugs.python.org/issue42522
79
91
#if !defined(_Py_XStealRef )
80
- static inline PyObject * __Py_XStealRef (PyObject * obj )
92
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
93
+ __Py_XStealRef (PyObject * obj )
81
94
{
82
95
Py_XDECREF (obj );
83
96
return obj ;
@@ -88,7 +101,8 @@ static inline PyObject* __Py_XStealRef(PyObject *obj)
88
101
89
102
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
90
103
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT )
91
- static inline void _Py_SET_REFCNT (PyObject * ob , Py_ssize_t refcnt )
104
+ PYCAPI_COMPAT_STATIC_INLINE (void )
105
+ _Py_SET_REFCNT (PyObject * ob , Py_ssize_t refcnt )
92
106
{
93
107
ob -> ob_refcnt = refcnt ;
94
108
}
@@ -133,7 +147,7 @@ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
133
147
134
148
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
135
149
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE )
136
- static inline void
150
+ PYCAPI_COMPAT_STATIC_INLINE ( void )
137
151
_Py_SET_TYPE (PyObject * ob , PyTypeObject * type )
138
152
{
139
153
ob -> ob_type = type ;
@@ -144,7 +158,7 @@ _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
144
158
145
159
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
146
160
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE )
147
- static inline void
161
+ PYCAPI_COMPAT_STATIC_INLINE ( void )
148
162
_Py_SET_SIZE (PyVarObject * ob , Py_ssize_t size )
149
163
{
150
164
ob -> ob_size = size ;
@@ -155,85 +169,88 @@ _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
155
169
156
170
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
157
171
#if PY_VERSION_HEX < 0x030900B1
158
- static inline PyCodeObject *
172
+ PYCAPI_COMPAT_STATIC_INLINE ( PyCodeObject * )
159
173
PyFrame_GetCode (PyFrameObject * frame )
160
174
{
161
- assert (frame != NULL );
162
- assert (frame -> f_code != NULL );
163
- return (PyCodeObject * ) Py_NewRef (frame -> f_code );
175
+ assert (frame != PYCAPI_COMPAT_NULL );
176
+ assert (frame -> f_code != PYCAPI_COMPAT_NULL );
177
+ return PYCAPI_COMPAT_CAST (PyCodeObject * , Py_NewRef (frame -> f_code ) );
164
178
}
165
179
#endif
166
180
167
- static inline PyCodeObject *
181
+ PYCAPI_COMPAT_STATIC_INLINE ( PyCodeObject * )
168
182
_PyFrame_GetCodeBorrow (PyFrameObject * frame )
169
183
{
170
- return (PyCodeObject * )_Py_StealRef (PyFrame_GetCode (frame ));
184
+ return PYCAPI_COMPAT_CAST (PyCodeObject * ,
185
+ _Py_StealRef (PyFrame_GetCode (frame )));
171
186
}
172
187
173
188
174
189
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
175
190
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION )
176
- static inline PyFrameObject *
191
+ PYCAPI_COMPAT_STATIC_INLINE ( PyFrameObject * )
177
192
PyFrame_GetBack (PyFrameObject * frame )
178
193
{
179
- assert (frame != NULL );
180
- return (PyFrameObject * ) Py_XNewRef (frame -> f_back );
194
+ assert (frame != PYCAPI_COMPAT_NULL );
195
+ return PYCAPI_COMPAT_CAST (PyFrameObject * , Py_XNewRef (frame -> f_back ) );
181
196
}
182
197
#endif
183
198
184
199
#if !defined(PYPY_VERSION )
185
- static inline PyFrameObject *
200
+ PYCAPI_COMPAT_STATIC_INLINE ( PyFrameObject * )
186
201
_PyFrame_GetBackBorrow (PyFrameObject * frame )
187
202
{
188
- return (PyFrameObject * )_Py_XStealRef (PyFrame_GetBack (frame ));
203
+ return PYCAPI_COMPAT_CAST (PyFrameObject * ,
204
+ _Py_XStealRef (PyFrame_GetBack (frame )));
189
205
}
190
206
#endif
191
207
192
208
193
209
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
194
210
#if PY_VERSION_HEX < 0x030900A5
195
- static inline PyInterpreterState *
211
+ PYCAPI_COMPAT_STATIC_INLINE ( PyInterpreterState * )
196
212
PyThreadState_GetInterpreter (PyThreadState * tstate )
197
213
{
198
- assert (tstate != NULL );
214
+ assert (tstate != PYCAPI_COMPAT_NULL );
199
215
return tstate -> interp ;
200
216
}
201
217
#endif
202
218
203
219
204
220
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
205
221
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION )
206
- static inline PyFrameObject *
222
+ PYCAPI_COMPAT_STATIC_INLINE ( PyFrameObject * )
207
223
PyThreadState_GetFrame (PyThreadState * tstate )
208
224
{
209
- assert (tstate != NULL );
210
- return (PyFrameObject * ) Py_XNewRef (tstate -> frame );
225
+ assert (tstate != PYCAPI_COMPAT_NULL );
226
+ return PYCAPI_COMPAT_CAST (PyFrameObject * , Py_XNewRef (tstate -> frame ) );
211
227
}
212
228
#endif
213
229
214
230
#if !defined(PYPY_VERSION )
215
- static inline PyFrameObject *
231
+ PYCAPI_COMPAT_STATIC_INLINE ( PyFrameObject * )
216
232
_PyThreadState_GetFrameBorrow (PyThreadState * tstate )
217
233
{
218
- return (PyFrameObject * )_Py_XStealRef (PyThreadState_GetFrame (tstate ));
234
+ return PYCAPI_COMPAT_CAST (PyFrameObject * ,
235
+ _Py_XStealRef (PyThreadState_GetFrame (tstate )));
219
236
}
220
237
#endif
221
238
222
239
223
240
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
224
241
#if PY_VERSION_HEX < 0x030900A5
225
- static inline PyInterpreterState *
242
+ PYCAPI_COMPAT_STATIC_INLINE ( PyInterpreterState * )
226
243
PyInterpreterState_Get (void )
227
244
{
228
245
PyThreadState * tstate ;
229
246
PyInterpreterState * interp ;
230
247
231
248
tstate = PyThreadState_GET ();
232
- if (tstate == NULL ) {
249
+ if (tstate == PYCAPI_COMPAT_NULL ) {
233
250
Py_FatalError ("GIL released (tstate is NULL)" );
234
251
}
235
252
interp = tstate -> interp ;
236
- if (interp == NULL ) {
253
+ if (interp == PYCAPI_COMPAT_NULL ) {
237
254
Py_FatalError ("no current interpreter" );
238
255
}
239
256
return interp ;
@@ -243,17 +260,18 @@ PyInterpreterState_Get(void)
243
260
244
261
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
245
262
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION )
246
- static inline uint64_t
263
+ PYCAPI_COMPAT_STATIC_INLINE ( uint64_t )
247
264
PyThreadState_GetID (PyThreadState * tstate )
248
265
{
249
- assert (tstate != NULL );
266
+ assert (tstate != PYCAPI_COMPAT_NULL );
250
267
return tstate -> id ;
251
268
}
252
269
#endif
253
270
254
271
// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
255
272
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION )
256
- static inline void PyThreadState_EnterTracing (PyThreadState * tstate )
273
+ PYCAPI_COMPAT_STATIC_INLINE (void )
274
+ PyThreadState_EnterTracing (PyThreadState * tstate )
257
275
{
258
276
tstate -> tracing ++ ;
259
277
#if PY_VERSION_HEX >= 0x030A00A1
@@ -266,10 +284,11 @@ static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
266
284
267
285
// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
268
286
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION )
269
- static inline void PyThreadState_LeaveTracing (PyThreadState * tstate )
287
+ PYCAPI_COMPAT_STATIC_INLINE (void )
288
+ PyThreadState_LeaveTracing (PyThreadState * tstate )
270
289
{
271
- int use_tracing = (tstate -> c_tracefunc != NULL
272
- || tstate -> c_profilefunc != NULL );
290
+ int use_tracing = (tstate -> c_tracefunc != PYCAPI_COMPAT_NULL
291
+ || tstate -> c_profilefunc != PYCAPI_COMPAT_NULL );
273
292
tstate -> tracing -- ;
274
293
#if PY_VERSION_HEX >= 0x030A00A1
275
294
tstate -> cframe -> use_tracing = use_tracing ;
@@ -282,7 +301,7 @@ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
282
301
283
302
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
284
303
#if PY_VERSION_HEX < 0x030900A1
285
- static inline PyObject *
304
+ PYCAPI_COMPAT_STATIC_INLINE ( PyObject * )
286
305
PyObject_CallNoArgs (PyObject * func )
287
306
{
288
307
return PyObject_CallFunctionObjArgs (func , NULL );
@@ -293,7 +312,7 @@ PyObject_CallNoArgs(PyObject *func)
293
312
// bpo-39245 made PyObject_CallOneArg() public (previously called
294
313
// _PyObject_CallOneArg) in Python 3.9.0a4
295
314
#if PY_VERSION_HEX < 0x030900A4
296
- static inline PyObject *
315
+ PYCAPI_COMPAT_STATIC_INLINE ( PyObject * )
297
316
PyObject_CallOneArg (PyObject * func , PyObject * arg )
298
317
{
299
318
return PyObject_CallFunctionObjArgs (func , arg , NULL );
@@ -303,7 +322,7 @@ PyObject_CallOneArg(PyObject *func, PyObject *arg)
303
322
304
323
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
305
324
#if PY_VERSION_HEX < 0x030A00A3
306
- static inline int
325
+ PYCAPI_COMPAT_STATIC_INLINE ( int )
307
326
PyModule_AddObjectRef (PyObject * module , const char * name , PyObject * value )
308
327
{
309
328
int res ;
@@ -319,7 +338,7 @@ PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
319
338
320
339
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
321
340
#if PY_VERSION_HEX < 0x030900A5
322
- static inline int
341
+ PYCAPI_COMPAT_STATIC_INLINE ( int )
323
342
PyModule_AddType (PyObject * module , PyTypeObject * type )
324
343
{
325
344
const char * name , * dot ;
@@ -330,21 +349,21 @@ PyModule_AddType(PyObject *module, PyTypeObject *type)
330
349
331
350
// inline _PyType_Name()
332
351
name = type -> tp_name ;
333
- assert (name != NULL );
352
+ assert (name != PYCAPI_COMPAT_NULL );
334
353
dot = strrchr (name , '.' );
335
- if (dot != NULL ) {
354
+ if (dot != PYCAPI_COMPAT_NULL ) {
336
355
name = dot + 1 ;
337
356
}
338
357
339
- return PyModule_AddObjectRef (module , name , ( PyObject * ) type );
358
+ return PyModule_AddObjectRef (module , name , _PyObject_CAST ( type ) );
340
359
}
341
360
#endif
342
361
343
362
344
363
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
345
364
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
346
365
#if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION )
347
- static inline int
366
+ PYCAPI_COMPAT_STATIC_INLINE ( int )
348
367
PyObject_GC_IsTracked (PyObject * obj )
349
368
{
350
369
return (PyObject_IS_GC (obj ) && _PyObject_GC_IS_TRACKED (obj ));
@@ -354,17 +373,18 @@ PyObject_GC_IsTracked(PyObject* obj)
354
373
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
355
374
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
356
375
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION )
357
- static inline int
376
+ PYCAPI_COMPAT_STATIC_INLINE ( int )
358
377
PyObject_GC_IsFinalized (PyObject * obj )
359
378
{
360
- return (PyObject_IS_GC (obj ) && _PyGCHead_FINALIZED ((PyGC_Head * )(obj )- 1 ));
379
+ PyGC_Head * gc = PYCAPI_COMPAT_CAST (PyGC_Head * , obj ) - 1 ;
380
+ return (PyObject_IS_GC (obj ) && _PyGCHead_FINALIZED (gc ));
361
381
}
362
382
#endif
363
383
364
384
365
385
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
366
386
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE )
367
- static inline int
387
+ PYCAPI_COMPAT_STATIC_INLINE ( int )
368
388
_Py_IS_TYPE (const PyObject * ob , const PyTypeObject * type ) {
369
389
return ob -> ob_type == type ;
370
390
}
@@ -382,11 +402,6 @@ _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
382
402
#endif
383
403
384
404
385
- #ifdef PYTHONCAPI_COMPAT_MSC_INLINE
386
- # undef inline
387
- # undef PYTHONCAPI_COMPAT_MSC_INLINE
388
- #endif
389
-
390
405
#ifdef __cplusplus
391
406
}
392
407
#endif
0 commit comments