@@ -26,27 +26,29 @@ extern "C" {
26
26
// the inline keyword in C (only in C++): use __inline instead.
27
27
#if (defined(_MSC_VER) && _MSC_VER < 1900 \
28
28
&& !defined(__cplusplus) && !defined(inline ))
29
- # define PYCAPI_COMPAT_INLINE (TYPE static __inline TYPE
29
+ # define PYCAPI_COMPAT_STATIC_INLINE (TYPE ) static __inline TYPE
30
30
#else
31
31
# define PYCAPI_COMPAT_STATIC_INLINE (TYPE ) static inline TYPE
32
32
#endif
33
33
34
34
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
35
+ #ifndef _Py_CAST
36
+ # define _Py_CAST (type, expr ) ((type)(expr))
37
+ #endif
38
+
39
+ // On C++11 and newer, _Py_NULL is defined as nullptr on C++11,
40
+ // otherwise it is defined as NULL.
41
+ #ifndef _Py_NULL
42
+ # if defined(__cplusplus) && __cplusplus >= 201103
43
+ # define _Py_NULL nullptr
44
+ # else
45
+ # define _Py_NULL NULL
46
+ # endif
42
47
#endif
43
48
44
49
// Cast argument to PyObject* type.
45
50
#ifndef _PyObject_CAST
46
- # define _PyObject_CAST (op ) PYCAPI_COMPAT_CAST(PyObject*, op)
47
- #endif
48
- #ifndef _PyObject_CAST_CONST
49
- # define _PyObject_CAST_CONST (op ) PYCAPI_COMPAT_CAST(const PyObject*, op)
51
+ # define _PyObject_CAST (op ) _Py_CAST(PyObject*, op)
50
52
#endif
51
53
52
54
@@ -74,30 +76,6 @@ _Py_XNewRef(PyObject *obj)
74
76
#endif
75
77
76
78
77
- // See https://bugs.python.org/issue42522
78
- #if !defined(_Py_StealRef )
79
- PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
80
- __Py_StealRef (PyObject * obj )
81
- {
82
- Py_DECREF (obj );
83
- return obj ;
84
- }
85
- #define _Py_StealRef (obj ) __Py_StealRef(_PyObject_CAST(obj))
86
- #endif
87
-
88
-
89
- // See https://bugs.python.org/issue42522
90
- #if !defined(_Py_XStealRef )
91
- PYCAPI_COMPAT_STATIC_INLINE (PyObject * )
92
- __Py_XStealRef (PyObject * obj )
93
- {
94
- Py_XDECREF (obj );
95
- return obj ;
96
- }
97
- #define _Py_XStealRef (obj ) __Py_XStealRef(_PyObject_CAST(obj))
98
- #endif
99
-
100
-
101
79
// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
102
80
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
103
81
PYCAPI_COMPAT_STATIC_INLINE (void )
@@ -171,36 +149,95 @@ _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
171
149
PYCAPI_COMPAT_STATIC_INLINE (PyCodeObject*)
172
150
PyFrame_GetCode(PyFrameObject *frame)
173
151
{
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 ));
152
+ assert (frame != _Py_NULL );
153
+ assert (frame->f_code != _Py_NULL );
154
+ return _Py_CAST (PyCodeObject*, Py_NewRef (frame->f_code ));
177
155
}
178
156
#endif
179
157
180
158
PYCAPI_COMPAT_STATIC_INLINE (PyCodeObject*)
181
159
_PyFrame_GetCodeBorrow(PyFrameObject *frame)
182
160
{
183
- return PYCAPI_COMPAT_CAST (PyCodeObject * ,
184
- _Py_StealRef (PyFrame_GetCode (frame )));
161
+ PyCodeObject *code = PyFrame_GetCode (frame);
162
+ Py_DECREF (code);
163
+ return code;
185
164
}
186
165
187
166
188
- // bpo-40421 added PyFrame_GetCode () to Python 3.9.0b1
167
+ // bpo-40421 added PyFrame_GetBack () to Python 3.9.0b1
189
168
#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
190
169
PYCAPI_COMPAT_STATIC_INLINE (PyFrameObject*)
191
170
PyFrame_GetBack(PyFrameObject *frame)
192
171
{
193
- assert (frame != PYCAPI_COMPAT_NULL );
194
- return PYCAPI_COMPAT_CAST (PyFrameObject * , Py_XNewRef (frame -> f_back ));
172
+ assert (frame != _Py_NULL );
173
+ return _Py_CAST (PyFrameObject*, Py_XNewRef (frame->f_back ));
195
174
}
196
175
#endif
197
176
198
177
#if !defined(PYPY_VERSION)
199
178
PYCAPI_COMPAT_STATIC_INLINE (PyFrameObject*)
200
179
_PyFrame_GetBackBorrow(PyFrameObject *frame)
201
180
{
202
- return PYCAPI_COMPAT_CAST (PyFrameObject * ,
203
- _Py_XStealRef (PyFrame_GetBack (frame )));
181
+ PyFrameObject *back = PyFrame_GetBack (frame);
182
+ Py_XDECREF (back);
183
+ return back;
184
+ }
185
+ #endif
186
+
187
+
188
+ // bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
189
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
190
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
191
+ PyFrame_GetLocals(PyFrameObject *frame)
192
+ {
193
+ #if PY_VERSION_HEX >= 0x030400B1
194
+ if (PyFrame_FastToLocalsWithError (frame) < 0 ) {
195
+ return NULL ;
196
+ }
197
+ #else
198
+ PyFrame_FastToLocals (frame);
199
+ #endif
200
+ return Py_NewRef (frame->f_locals );
201
+ }
202
+ #endif
203
+
204
+
205
+ // bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
206
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
207
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
208
+ PyFrame_GetGlobals(PyFrameObject *frame)
209
+ {
210
+ return Py_NewRef (frame->f_globals );
211
+ }
212
+ #endif
213
+
214
+
215
+ // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
216
+ #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
217
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
218
+ PyFrame_GetBuiltins(PyFrameObject *frame)
219
+ {
220
+ return Py_NewRef (frame->f_builtins );
221
+ }
222
+ #endif
223
+
224
+
225
+ // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
226
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
227
+ PYCAPI_COMPAT_STATIC_INLINE (int )
228
+ PyFrame_GetLasti(PyFrameObject *frame)
229
+ {
230
+ #if PY_VERSION_HEX >= 0x030A00A7
231
+ // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
232
+ // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
233
+ // instructions.
234
+ if (frame->f_lasti < 0 ) {
235
+ return -1 ;
236
+ }
237
+ return frame->f_lasti * 2 ;
238
+ #else
239
+ return frame->f_lasti ;
240
+ #endif
204
241
}
205
242
#endif
206
243
@@ -210,7 +247,7 @@ _PyFrame_GetBackBorrow(PyFrameObject *frame)
210
247
PYCAPI_COMPAT_STATIC_INLINE (PyInterpreterState *)
211
248
PyThreadState_GetInterpreter(PyThreadState *tstate)
212
249
{
213
- assert (tstate != PYCAPI_COMPAT_NULL );
250
+ assert (tstate != _Py_NULL );
214
251
return tstate->interp ;
215
252
}
216
253
#endif
@@ -221,17 +258,18 @@ PyThreadState_GetInterpreter(PyThreadState *tstate)
221
258
PYCAPI_COMPAT_STATIC_INLINE (PyFrameObject*)
222
259
PyThreadState_GetFrame(PyThreadState *tstate)
223
260
{
224
- assert (tstate != PYCAPI_COMPAT_NULL );
225
- return PYCAPI_COMPAT_CAST (PyFrameObject * , Py_XNewRef (tstate -> frame ));
261
+ assert (tstate != _Py_NULL );
262
+ return _Py_CAST (PyFrameObject *, Py_XNewRef (tstate->frame ));
226
263
}
227
264
#endif
228
265
229
266
#if !defined(PYPY_VERSION)
230
267
PYCAPI_COMPAT_STATIC_INLINE (PyFrameObject*)
231
268
_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
232
269
{
233
- return PYCAPI_COMPAT_CAST (PyFrameObject * ,
234
- _Py_XStealRef (PyThreadState_GetFrame (tstate )));
270
+ PyFrameObject *frame = PyThreadState_GetFrame (tstate);
271
+ Py_XDECREF (frame);
272
+ return frame;
235
273
}
236
274
#endif
237
275
@@ -245,11 +283,11 @@ PyInterpreterState_Get(void)
245
283
PyInterpreterState *interp;
246
284
247
285
tstate = PyThreadState_GET ();
248
- if (tstate == PYCAPI_COMPAT_NULL ) {
286
+ if (tstate == _Py_NULL ) {
249
287
Py_FatalError (" GIL released (tstate is NULL)" );
250
288
}
251
289
interp = tstate->interp ;
252
- if (interp == PYCAPI_COMPAT_NULL ) {
290
+ if (interp == _Py_NULL ) {
253
291
Py_FatalError (" no current interpreter" );
254
292
}
255
293
return interp;
@@ -262,7 +300,7 @@ PyInterpreterState_Get(void)
262
300
PYCAPI_COMPAT_STATIC_INLINE (uint64_t )
263
301
PyThreadState_GetID(PyThreadState *tstate)
264
302
{
265
- assert (tstate != PYCAPI_COMPAT_NULL );
303
+ assert (tstate != _Py_NULL );
266
304
return tstate->id ;
267
305
}
268
306
#endif
@@ -286,8 +324,8 @@ PyThreadState_EnterTracing(PyThreadState *tstate)
286
324
PYCAPI_COMPAT_STATIC_INLINE (void )
287
325
PyThreadState_LeaveTracing(PyThreadState *tstate)
288
326
{
289
- int use_tracing = (tstate -> c_tracefunc != PYCAPI_COMPAT_NULL
290
- || tstate -> c_profilefunc != PYCAPI_COMPAT_NULL );
327
+ int use_tracing = (tstate->c_tracefunc != _Py_NULL
328
+ || tstate->c_profilefunc != _Py_NULL );
291
329
tstate->tracing --;
292
330
#if PY_VERSION_HEX >= 0x030A00A1
293
331
tstate->cframe ->use_tracing = use_tracing;
@@ -322,11 +360,11 @@ PyObject_CallOneArg(PyObject *func, PyObject *arg)
322
360
// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
323
361
#if PY_VERSION_HEX < 0x030A00A3
324
362
PYCAPI_COMPAT_STATIC_INLINE (int )
325
- PyModule_AddObjectRef (PyObject * mod , const char * name , PyObject * value )
363
+ PyModule_AddObjectRef(PyObject *module , const char *name, PyObject *value)
326
364
{
327
365
int res;
328
366
Py_XINCREF (value);
329
- res = PyModule_AddObject (mod , name , value );
367
+ res = PyModule_AddObject (module , name, value);
330
368
if (res < 0 ) {
331
369
Py_XDECREF (value);
332
370
}
@@ -338,7 +376,7 @@ PyModule_AddObjectRef(PyObject *mod, const char *name, PyObject *value)
338
376
// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
339
377
#if PY_VERSION_HEX < 0x030900A5
340
378
PYCAPI_COMPAT_STATIC_INLINE (int )
341
- PyModule_AddType (PyObject * mod , PyTypeObject * type )
379
+ PyModule_AddType(PyObject *module , PyTypeObject *type)
342
380
{
343
381
const char *name, *dot;
344
382
@@ -348,13 +386,13 @@ PyModule_AddType(PyObject *mod, PyTypeObject *type)
348
386
349
387
// inline _PyType_Name()
350
388
name = type->tp_name ;
351
- assert (name != PYCAPI_COMPAT_NULL );
389
+ assert (name != _Py_NULL );
352
390
dot = strrchr (name, ' .' );
353
- if (dot != PYCAPI_COMPAT_NULL ) {
391
+ if (dot != _Py_NULL ) {
354
392
name = dot + 1 ;
355
393
}
356
394
357
- return PyModule_AddObjectRef (mod , name , _PyObject_CAST (type ));
395
+ return PyModule_AddObjectRef (module , name, _PyObject_CAST (type));
358
396
}
359
397
#endif
360
398
@@ -375,7 +413,7 @@ PyObject_GC_IsTracked(PyObject* obj)
375
413
PYCAPI_COMPAT_STATIC_INLINE (int )
376
414
PyObject_GC_IsFinalized(PyObject *obj)
377
415
{
378
- PyGC_Head * gc = PYCAPI_COMPAT_CAST (PyGC_Head * , obj ) - 1 ;
416
+ PyGC_Head *gc = _Py_CAST (PyGC_Head*, obj) - 1 ;
379
417
return (PyObject_IS_GC (obj) && _PyGCHead_FINALIZED (gc));
380
418
}
381
419
#endif
@@ -384,10 +422,10 @@ PyObject_GC_IsFinalized(PyObject *obj)
384
422
// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
385
423
#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
386
424
PYCAPI_COMPAT_STATIC_INLINE (int )
387
- _Py_IS_TYPE (const PyObject * ob , const PyTypeObject * type ) {
388
- return ob -> ob_type == type ;
425
+ _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
426
+ return Py_TYPE (ob) == type;
389
427
}
390
- #define Py_IS_TYPE (ob , type ) _Py_IS_TYPE(_PyObject_CAST_CONST (ob), type)
428
+ #define Py_IS_TYPE (ob, type ) _Py_IS_TYPE(_PyObject_CAST (ob), type)
391
429
#endif
392
430
393
431
@@ -430,6 +468,44 @@ PyFloat_Unpack8(const char *p, int le)
430
468
#endif
431
469
432
470
471
+ // gh-92154 added PyCode_GetCode() to Python 3.11.0b1
472
+ #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
473
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
474
+ PyCode_GetCode(PyCodeObject *code)
475
+ {
476
+ return Py_NewRef (code->co_code );
477
+ }
478
+ #endif
479
+
480
+
481
+ // gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
482
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
483
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
484
+ PyCode_GetVarnames(PyCodeObject *code)
485
+ {
486
+ return Py_NewRef (code->co_varnames );
487
+ }
488
+ #endif
489
+
490
+ // gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
491
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
492
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
493
+ PyCode_GetFreevars(PyCodeObject *code)
494
+ {
495
+ return Py_NewRef (code->co_freevars );
496
+ }
497
+ #endif
498
+
499
+ // gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
500
+ #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
501
+ PYCAPI_COMPAT_STATIC_INLINE (PyObject*)
502
+ PyCode_GetCellvars(PyCodeObject *code)
503
+ {
504
+ return Py_NewRef (code->co_cellvars );
505
+ }
506
+ #endif
507
+
508
+
433
509
// Py_UNUSED() was added to Python 3.4.0b2.
434
510
#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
435
511
# if defined(__GNUC__) || defined(__clang__)
0 commit comments