Skip to content

Commit 2c9a8e7

Browse files
Sync pythoncapi_compat (#12188)
1 parent 2d30dba commit 2c9a8e7

File tree

1 file changed

+76
-62
lines changed

1 file changed

+76
-62
lines changed

mypyc/lib-rt/pythoncapi_compat.h

Lines changed: 76 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
// Header file providing new functions of the Python C API to old Python
2-
// versions.
1+
// Header file providing new C API functions to old Python versions.
32
//
4-
// File distributed under the MIT license.
3+
// File distributed under the Zero Clause BSD (0BSD) license.
54
// Copyright Contributors to the pythoncapi_compat project.
65
//
76
// Homepage:
@@ -10,7 +9,7 @@
109
// Latest version:
1110
// https://github.com/raw/pythoncapi/pythoncapi_compat/master/pythoncapi_compat.h
1211
//
13-
// SPDX-License-Identifier: MIT
12+
// SPDX-License-Identifier: 0BSD
1413

1514
#ifndef PYTHONCAPI_COMPAT
1615
#define PYTHONCAPI_COMPAT
@@ -27,24 +26,34 @@ extern "C" {
2726
// the inline keyword in C (only in C++): use __inline instead.
2827
#if (defined(_MSC_VER) && _MSC_VER < 1900 \
2928
&& !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
29+
# define PYCAPI_COMPAT_INLINE(TYPE static __inline TYPE
30+
#else
31+
# define PYCAPI_COMPAT_STATIC_INLINE(TYPE) static inline TYPE
3332
#endif
3433

3534

35+
// C++ compatibility
36+
#ifdef __cplusplus
37+
# define PYCAPI_COMPAT_CAST(TYPE, EXPR) reinterpret_cast<TYPE>(EXPR)
38+
# define PYCAPI_COMPAT_NULL nullptr
39+
#else
40+
# define PYCAPI_COMPAT_CAST(TYPE, EXPR) (TYPE)(EXPR)
41+
# define PYCAPI_COMPAT_NULL NULL
42+
#endif
43+
3644
// Cast argument to PyObject* type.
3745
#ifndef _PyObject_CAST
38-
# define _PyObject_CAST(op) ((PyObject*)(op))
46+
# define _PyObject_CAST(op) PYCAPI_COMPAT_CAST(PyObject*, op)
3947
#endif
4048
#ifndef _PyObject_CAST_CONST
41-
# define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
49+
# define _PyObject_CAST_CONST(op) PYCAPI_COMPAT_CAST(const PyObject*, op)
4250
#endif
4351

4452

4553
// bpo-42262 added Py_NewRef() to Python 3.10.0a3
4654
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
47-
static inline PyObject* _Py_NewRef(PyObject *obj)
55+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
56+
_Py_NewRef(PyObject *obj)
4857
{
4958
Py_INCREF(obj);
5059
return obj;
@@ -55,7 +64,8 @@ static inline PyObject* _Py_NewRef(PyObject *obj)
5564

5665
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3
5766
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
58-
static inline PyObject* _Py_XNewRef(PyObject *obj)
67+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
68+
_Py_XNewRef(PyObject *obj)
5969
{
6070
Py_XINCREF(obj);
6171
return obj;
@@ -66,7 +76,8 @@ static inline PyObject* _Py_XNewRef(PyObject *obj)
6676

6777
// See https://bugs.python.org/issue42522
6878
#if !defined(_Py_StealRef)
69-
static inline PyObject* __Py_StealRef(PyObject *obj)
79+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
80+
__Py_StealRef(PyObject *obj)
7081
{
7182
Py_DECREF(obj);
7283
return obj;
@@ -77,7 +88,8 @@ static inline PyObject* __Py_StealRef(PyObject *obj)
7788

7889
// See https://bugs.python.org/issue42522
7990
#if !defined(_Py_XStealRef)
80-
static inline PyObject* __Py_XStealRef(PyObject *obj)
91+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
92+
__Py_XStealRef(PyObject *obj)
8193
{
8294
Py_XDECREF(obj);
8395
return obj;
@@ -88,7 +100,8 @@ static inline PyObject* __Py_XStealRef(PyObject *obj)
88100

89101
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
90102
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
91-
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
103+
PYCAPI_COMPAT_STATIC_INLINE(void)
104+
_Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
92105
{
93106
ob->ob_refcnt = refcnt;
94107
}
@@ -133,7 +146,7 @@ static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
133146

134147
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
135148
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
136-
static inline void
149+
PYCAPI_COMPAT_STATIC_INLINE(void)
137150
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
138151
{
139152
ob->ob_type = type;
@@ -144,7 +157,7 @@ _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
144157

145158
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
146159
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
147-
static inline void
160+
PYCAPI_COMPAT_STATIC_INLINE(void)
148161
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
149162
{
150163
ob->ob_size = size;
@@ -155,85 +168,88 @@ _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
155168

156169
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
157170
#if PY_VERSION_HEX < 0x030900B1
158-
static inline PyCodeObject*
171+
PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
159172
PyFrame_GetCode(PyFrameObject *frame)
160173
{
161-
assert(frame != NULL);
162-
assert(frame->f_code != NULL);
163-
return (PyCodeObject*)Py_NewRef(frame->f_code);
174+
assert(frame != PYCAPI_COMPAT_NULL);
175+
assert(frame->f_code != PYCAPI_COMPAT_NULL);
176+
return PYCAPI_COMPAT_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
164177
}
165178
#endif
166179

167-
static inline PyCodeObject*
180+
PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
168181
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
169182
{
170-
return (PyCodeObject *)_Py_StealRef(PyFrame_GetCode(frame));
183+
return PYCAPI_COMPAT_CAST(PyCodeObject *,
184+
_Py_StealRef(PyFrame_GetCode(frame)));
171185
}
172186

173187

174188
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
175189
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
176-
static inline PyFrameObject*
190+
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
177191
PyFrame_GetBack(PyFrameObject *frame)
178192
{
179-
assert(frame != NULL);
180-
return (PyFrameObject*)Py_XNewRef(frame->f_back);
193+
assert(frame != PYCAPI_COMPAT_NULL);
194+
return PYCAPI_COMPAT_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
181195
}
182196
#endif
183197

184198
#if !defined(PYPY_VERSION)
185-
static inline PyFrameObject*
199+
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
186200
_PyFrame_GetBackBorrow(PyFrameObject *frame)
187201
{
188-
return (PyFrameObject *)_Py_XStealRef(PyFrame_GetBack(frame));
202+
return PYCAPI_COMPAT_CAST(PyFrameObject *,
203+
_Py_XStealRef(PyFrame_GetBack(frame)));
189204
}
190205
#endif
191206

192207

193208
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
194209
#if PY_VERSION_HEX < 0x030900A5
195-
static inline PyInterpreterState *
210+
PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
196211
PyThreadState_GetInterpreter(PyThreadState *tstate)
197212
{
198-
assert(tstate != NULL);
213+
assert(tstate != PYCAPI_COMPAT_NULL);
199214
return tstate->interp;
200215
}
201216
#endif
202217

203218

204219
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
205220
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
206-
static inline PyFrameObject*
221+
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
207222
PyThreadState_GetFrame(PyThreadState *tstate)
208223
{
209-
assert(tstate != NULL);
210-
return (PyFrameObject *)Py_XNewRef(tstate->frame);
224+
assert(tstate != PYCAPI_COMPAT_NULL);
225+
return PYCAPI_COMPAT_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
211226
}
212227
#endif
213228

214229
#if !defined(PYPY_VERSION)
215-
static inline PyFrameObject*
230+
PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
216231
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
217232
{
218-
return (PyFrameObject *)_Py_XStealRef(PyThreadState_GetFrame(tstate));
233+
return PYCAPI_COMPAT_CAST(PyFrameObject*,
234+
_Py_XStealRef(PyThreadState_GetFrame(tstate)));
219235
}
220236
#endif
221237

222238

223239
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
224240
#if PY_VERSION_HEX < 0x030900A5
225-
static inline PyInterpreterState *
241+
PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState*)
226242
PyInterpreterState_Get(void)
227243
{
228244
PyThreadState *tstate;
229245
PyInterpreterState *interp;
230246

231247
tstate = PyThreadState_GET();
232-
if (tstate == NULL) {
248+
if (tstate == PYCAPI_COMPAT_NULL) {
233249
Py_FatalError("GIL released (tstate is NULL)");
234250
}
235251
interp = tstate->interp;
236-
if (interp == NULL) {
252+
if (interp == PYCAPI_COMPAT_NULL) {
237253
Py_FatalError("no current interpreter");
238254
}
239255
return interp;
@@ -243,17 +259,18 @@ PyInterpreterState_Get(void)
243259

244260
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
245261
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
246-
static inline uint64_t
262+
PYCAPI_COMPAT_STATIC_INLINE(uint64_t)
247263
PyThreadState_GetID(PyThreadState *tstate)
248264
{
249-
assert(tstate != NULL);
265+
assert(tstate != PYCAPI_COMPAT_NULL);
250266
return tstate->id;
251267
}
252268
#endif
253269

254270
// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
255271
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
256-
static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
272+
PYCAPI_COMPAT_STATIC_INLINE(void)
273+
PyThreadState_EnterTracing(PyThreadState *tstate)
257274
{
258275
tstate->tracing++;
259276
#if PY_VERSION_HEX >= 0x030A00A1
@@ -266,11 +283,12 @@ static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
266283

267284
// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
268285
#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
269-
static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
286+
PYCAPI_COMPAT_STATIC_INLINE(void)
287+
PyThreadState_LeaveTracing(PyThreadState *tstate)
270288
{
289+
int use_tracing = (tstate->c_tracefunc != PYCAPI_COMPAT_NULL
290+
|| tstate->c_profilefunc != PYCAPI_COMPAT_NULL);
271291
tstate->tracing--;
272-
int use_tracing = (tstate->c_tracefunc != NULL
273-
|| tstate->c_profilefunc != NULL);
274292
#if PY_VERSION_HEX >= 0x030A00A1
275293
tstate->cframe->use_tracing = use_tracing;
276294
#else
@@ -282,7 +300,7 @@ static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
282300

283301
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
284302
#if PY_VERSION_HEX < 0x030900A1
285-
static inline PyObject*
303+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
286304
PyObject_CallNoArgs(PyObject *func)
287305
{
288306
return PyObject_CallFunctionObjArgs(func, NULL);
@@ -293,7 +311,7 @@ PyObject_CallNoArgs(PyObject *func)
293311
// bpo-39245 made PyObject_CallOneArg() public (previously called
294312
// _PyObject_CallOneArg) in Python 3.9.0a4
295313
#if PY_VERSION_HEX < 0x030900A4
296-
static inline PyObject*
314+
PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
297315
PyObject_CallOneArg(PyObject *func, PyObject *arg)
298316
{
299317
return PyObject_CallFunctionObjArgs(func, arg, NULL);
@@ -303,12 +321,12 @@ PyObject_CallOneArg(PyObject *func, PyObject *arg)
303321

304322
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
305323
#if PY_VERSION_HEX < 0x030A00A3
306-
static inline int
307-
PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
324+
PYCAPI_COMPAT_STATIC_INLINE(int)
325+
PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
308326
{
309327
int res;
310328
Py_XINCREF(value);
311-
res = PyModule_AddObject(module, name, value);
329+
res = PyModule_AddObject(mod, name, value);
312330
if (res < 0) {
313331
Py_XDECREF(value);
314332
}
@@ -319,8 +337,8 @@ PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
319337

320338
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
321339
#if PY_VERSION_HEX < 0x030900A5
322-
static inline int
323-
PyModule_AddType(PyObject *module, PyTypeObject *type)
340+
PYCAPI_COMPAT_STATIC_INLINE(int)
341+
PyModule_AddType(PyObject *mod, PyTypeObject *type)
324342
{
325343
const char *name, *dot;
326344

@@ -330,21 +348,21 @@ PyModule_AddType(PyObject *module, PyTypeObject *type)
330348

331349
// inline _PyType_Name()
332350
name = type->tp_name;
333-
assert(name != NULL);
351+
assert(name != PYCAPI_COMPAT_NULL);
334352
dot = strrchr(name, '.');
335-
if (dot != NULL) {
353+
if (dot != PYCAPI_COMPAT_NULL) {
336354
name = dot + 1;
337355
}
338356

339-
return PyModule_AddObjectRef(module, name, (PyObject *)type);
357+
return PyModule_AddObjectRef(mod, name, _PyObject_CAST(type));
340358
}
341359
#endif
342360

343361

344362
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
345363
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
346364
#if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
347-
static inline int
365+
PYCAPI_COMPAT_STATIC_INLINE(int)
348366
PyObject_GC_IsTracked(PyObject* obj)
349367
{
350368
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
@@ -354,17 +372,18 @@ PyObject_GC_IsTracked(PyObject* obj)
354372
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
355373
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
356374
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
357-
static inline int
375+
PYCAPI_COMPAT_STATIC_INLINE(int)
358376
PyObject_GC_IsFinalized(PyObject *obj)
359377
{
360-
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
378+
PyGC_Head *gc = PYCAPI_COMPAT_CAST(PyGC_Head *, obj) - 1;
379+
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
361380
}
362381
#endif
363382

364383

365384
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
366385
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
367-
static inline int
386+
PYCAPI_COMPAT_STATIC_INLINE(int)
368387
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
369388
return ob->ob_type == type;
370389
}
@@ -382,11 +401,6 @@ _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
382401
#endif
383402

384403

385-
#ifdef PYTHONCAPI_COMPAT_MSC_INLINE
386-
# undef inline
387-
# undef PYTHONCAPI_COMPAT_MSC_INLINE
388-
#endif
389-
390404
#ifdef __cplusplus
391405
}
392406
#endif

0 commit comments

Comments
 (0)