Skip to content

Commit 477776e

Browse files
vstinnerindygreg
authored andcommitted
Update pythoncapi_compat.h
Fix compatibility with Visual Studio 2008 for Python 2.7: * https://phab.mercurial-scm.org/D9867 * python/pythoncapi-compat#3
1 parent 060d41d commit 477776e

1 file changed

Lines changed: 119 additions & 40 deletions

File tree

c-ext/pythoncapi_compat.h

Lines changed: 119 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -19,58 +19,87 @@ extern "C" {
1919
#endif
2020

2121
#include <Python.h>
22-
#include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
22+
#include "frameobject.h" // PyFrameObject, PyFrame_GetBack()
23+
24+
25+
// Compatibility with Visual Studio 2013 and older which don't support
26+
// the inline keyword in C (only in C++): use __inline instead.
27+
#if (defined(_MSC_VER) && _MSC_VER < 1900 \
28+
&& !defined(__cplusplus) && !defined(inline))
29+
# define inline __inline
30+
# define PYTHONCAPI_COMPAT_MSC_INLINE
31+
// These two macros are undefined at the end of this file
32+
#endif
33+
2334

2435
// Cast argument to PyObject* type.
2536
#ifndef _PyObject_CAST
26-
#define _PyObject_CAST(op) ((PyObject *)(op))
37+
# define _PyObject_CAST(op) ((PyObject*)(op))
38+
#endif
39+
#ifndef _PyObject_CAST_CONST
40+
# define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
2741
#endif
2842

43+
2944
// bpo-42262 added Py_NewRef() to Python 3.10.0a3
30-
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_NewRef)
31-
static inline PyObject *_Py_NewRef(PyObject *obj) {
45+
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
46+
static inline PyObject* _Py_NewRef(PyObject *obj)
47+
{
3248
Py_INCREF(obj);
3349
return obj;
3450
}
3551
#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
3652
#endif
3753

54+
3855
// bpo-42262 added Py_XNewRef() to Python 3.10.0a3
39-
#if PY_VERSION_HEX < 0x030a00A3 && !defined(Py_XNewRef)
40-
static inline PyObject *_Py_XNewRef(PyObject *obj) {
56+
#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
57+
static inline PyObject* _Py_XNewRef(PyObject *obj)
58+
{
4159
Py_XINCREF(obj);
4260
return obj;
4361
}
4462
#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
4563
#endif
4664

65+
4766
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
4867
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
49-
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
68+
static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
69+
{
5070
ob->ob_refcnt = refcnt;
5171
}
52-
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT((PyObject *)(ob), refcnt)
72+
#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
5373
#endif
5474

75+
5576
// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
5677
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
57-
static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
78+
static inline void
79+
_Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
80+
{
5881
ob->ob_type = type;
5982
}
60-
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE((PyObject *)(ob), type)
83+
#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
6184
#endif
6285

86+
6387
// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
6488
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
65-
static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
89+
static inline void
90+
_Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
91+
{
6692
ob->ob_size = size;
6793
}
68-
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject *)(ob), size)
94+
#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
6995
#endif
7096

97+
7198
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
7299
#if PY_VERSION_HEX < 0x030900B1
73-
static inline PyCodeObject *PyFrame_GetCode(PyFrameObject *frame) {
100+
static inline PyCodeObject*
101+
PyFrame_GetCode(PyFrameObject *frame)
102+
{
74103
PyCodeObject *code;
75104
assert(frame != NULL);
76105
code = frame->f_code;
@@ -80,15 +109,20 @@ static inline PyCodeObject *PyFrame_GetCode(PyFrameObject *frame) {
80109
}
81110
#endif
82111

83-
static inline PyCodeObject *_PyFrame_GetCodeBorrow(PyFrameObject *frame) {
112+
static inline PyCodeObject*
113+
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
114+
{
84115
PyCodeObject *code = PyFrame_GetCode(frame);
85116
Py_DECREF(code);
86-
return code; // borrowed reference
117+
return code; // borrowed reference
87118
}
88119

120+
89121
// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
90122
#if PY_VERSION_HEX < 0x030900B1
91-
static inline PyFrameObject *PyFrame_GetBack(PyFrameObject *frame) {
123+
static inline PyFrameObject*
124+
PyFrame_GetBack(PyFrameObject *frame)
125+
{
92126
PyFrameObject *back;
93127
assert(frame != NULL);
94128
back = frame->f_back;
@@ -97,24 +131,31 @@ static inline PyFrameObject *PyFrame_GetBack(PyFrameObject *frame) {
97131
}
98132
#endif
99133

100-
static inline PyFrameObject *_PyFrame_GetBackBorrow(PyFrameObject *frame) {
134+
static inline PyFrameObject*
135+
_PyFrame_GetBackBorrow(PyFrameObject *frame)
136+
{
101137
PyFrameObject *back = PyFrame_GetBack(frame);
102138
Py_XDECREF(back);
103-
return back; // borrowed reference
139+
return back; // borrowed reference
104140
}
105141

142+
106143
// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
107144
#if PY_VERSION_HEX < 0x030900A5
108145
static inline PyInterpreterState *
109-
PyThreadState_GetInterpreter(PyThreadState *tstate) {
146+
PyThreadState_GetInterpreter(PyThreadState *tstate)
147+
{
110148
assert(tstate != NULL);
111149
return tstate->interp;
112150
}
113151
#endif
114152

153+
115154
// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
116155
#if PY_VERSION_HEX < 0x030900B1
117-
static inline PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate) {
156+
static inline PyFrameObject*
157+
PyThreadState_GetFrame(PyThreadState *tstate)
158+
{
118159
PyFrameObject *frame;
119160
assert(tstate != NULL);
120161
frame = tstate->frame;
@@ -123,16 +164,20 @@ static inline PyFrameObject *PyThreadState_GetFrame(PyThreadState *tstate) {
123164
}
124165
#endif
125166

126-
static inline PyFrameObject *
127-
_PyThreadState_GetFrameBorrow(PyThreadState *tstate) {
167+
static inline PyFrameObject*
168+
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
169+
{
128170
PyFrameObject *frame = PyThreadState_GetFrame(tstate);
129171
Py_XDECREF(frame);
130-
return frame; // borrowed reference
172+
return frame; // borrowed reference
131173
}
132174

175+
133176
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
134177
#if PY_VERSION_HEX < 0x030900A5
135-
static inline PyInterpreterState *PyInterpreterState_Get(void) {
178+
static inline PyInterpreterState *
179+
PyInterpreterState_Get(void)
180+
{
136181
PyThreadState *tstate;
137182
PyInterpreterState *interp;
138183

@@ -148,32 +193,59 @@ static inline PyInterpreterState *PyInterpreterState_Get(void) {
148193
}
149194
#endif
150195

196+
151197
// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
152198
#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6
153-
static inline uint64_t PyThreadState_GetID(PyThreadState *tstate) {
199+
static inline uint64_t
200+
PyThreadState_GetID(PyThreadState *tstate)
201+
{
154202
assert(tstate != NULL);
155203
return tstate->id;
156204
}
157205
#endif
158206

207+
159208
// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
160209
#if PY_VERSION_HEX < 0x030900A1
161-
static inline PyObject *PyObject_CallNoArgs(PyObject *func) {
210+
static inline PyObject*
211+
PyObject_CallNoArgs(PyObject *func)
212+
{
162213
return PyObject_CallFunctionObjArgs(func, NULL);
163214
}
164215
#endif
165216

217+
166218
// bpo-39245 made PyObject_CallOneArg() public (previously called
167219
// _PyObject_CallOneArg) in Python 3.9.0a4
168220
#if PY_VERSION_HEX < 0x030900A4
169-
static inline PyObject *PyObject_CallOneArg(PyObject *func, PyObject *arg) {
221+
static inline PyObject*
222+
PyObject_CallOneArg(PyObject *func, PyObject *arg)
223+
{
170224
return PyObject_CallFunctionObjArgs(func, arg, NULL);
171225
}
172226
#endif
173227

228+
229+
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
230+
#if PY_VERSION_HEX < 0x030A00A3
231+
static inline int
232+
PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
233+
{
234+
Py_XINCREF(value);
235+
int res = PyModule_AddObject(module, name, value);
236+
if (res < 0) {
237+
Py_XDECREF(value);
238+
}
239+
return res;
240+
}
241+
#endif
242+
243+
174244
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
175245
#if PY_VERSION_HEX < 0x030900A5
176-
static inline int PyModule_AddType(PyObject *module, PyTypeObject *type) {
246+
static inline int
247+
PyModule_AddType(PyObject *module, PyTypeObject *type)
248+
{
177249
const char *name, *dot;
178250

179251
if (PyType_Ready(type) < 0) {
@@ -188,41 +260,48 @@ static inline int PyModule_AddType(PyObject *module, PyTypeObject *type) {
188260
name = dot + 1;
189261
}
190262

191-
Py_INCREF(type);
192-
if (PyModule_AddObject(module, name, (PyObject *)type) < 0) {
193-
Py_DECREF(type);
194-
return -1;
195-
}
196-
197-
return 0;
263+
return PyModule_AddObjectRef(module, name, (PyObject *)type);
198264
}
199265
#endif
200266

267+
201268
// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
202269
// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
203270
#if PY_VERSION_HEX < 0x030900A6
204-
static inline int PyObject_GC_IsTracked(PyObject *obj) {
271+
static inline int
272+
PyObject_GC_IsTracked(PyObject* obj)
273+
{
205274
return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
206275
}
207276
#endif
208277

209278
// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
210279
// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
211280
#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0
212-
static inline int PyObject_GC_IsFinalized(PyObject *obj) {
281+
static inline int
282+
PyObject_GC_IsFinalized(PyObject *obj)
283+
{
213284
return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED((PyGC_Head *)(obj)-1));
214285
}
215286
#endif
216287

288+
217289
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
218290
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
219-
static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
291+
static inline int
292+
_Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
220293
return ob->ob_type == type;
221294
}
222-
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE((const PyObject *)(ob), type)
295+
#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type)
296+
#endif
297+
298+
299+
#ifdef PYTHONCAPI_COMPAT_MSC_INLINE
300+
# undef inline
301+
# undef PYTHONCAPI_COMPAT_MSC_INLINE
223302
#endif
224303

225304
#ifdef __cplusplus
226305
}
227306
#endif
228-
#endif // PYTHONCAPI_COMPAT
307+
#endif // PYTHONCAPI_COMPAT

0 commit comments

Comments
 (0)