xref: /aosp_15_r20/external/pytorch/torch/csrc/utils/pythoncapi_compat.h (revision da0073e96a02ea20f0ac840b70461e3646d07c45)
1 // Header file providing new C API functions to old Python versions.
2 //
3 // File distributed under the Zero Clause BSD (0BSD) license.
4 // Copyright Contributors to the pythoncapi_compat project.
5 //
6 // Homepage:
7 // https://github.com/python/pythoncapi_compat
8 //
9 // Latest version:
10 // https://raw.githubusercontent.com/python/pythoncapi_compat/master/pythoncapi_compat.h
11 //
12 // SPDX-License-Identifier: 0BSD
13 
14 #ifndef PYTHONCAPI_COMPAT
15 #define PYTHONCAPI_COMPAT
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif
20 
21 #include <Python.h>
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 PYCAPI_COMPAT_STATIC_INLINE(TYPE) static __inline TYPE
30 #else
31 #  define PYCAPI_COMPAT_STATIC_INLINE(TYPE) static inline TYPE
32 #endif
33 
34 
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
47 #endif
48 
49 // Cast argument to PyObject* type.
50 #ifndef _PyObject_CAST
51 #  define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
52 #endif
53 
54 
55 // bpo-42262 added Py_NewRef() to Python 3.10.0a3
56 #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
57 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
_Py_NewRef(PyObject * obj)58 _Py_NewRef(PyObject *obj)
59 {
60     Py_INCREF(obj);
61     return obj;
62 }
63 #define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
64 #endif
65 
66 
67 // bpo-42262 added Py_XNewRef() to Python 3.10.0a3
68 #if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_XNewRef)
69 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
_Py_XNewRef(PyObject * obj)70 _Py_XNewRef(PyObject *obj)
71 {
72     Py_XINCREF(obj);
73     return obj;
74 }
75 #define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
76 #endif
77 
78 
79 // bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
80 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
81 PYCAPI_COMPAT_STATIC_INLINE(void)
_Py_SET_REFCNT(PyObject * ob,Py_ssize_t refcnt)82 _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
83 {
84     ob->ob_refcnt = refcnt;
85 }
86 #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
87 #endif
88 
89 
90 // Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
91 // It is excluded from the limited C API.
92 #if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
93 #define Py_SETREF(dst, src)                                     \
94     do {                                                        \
95         PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
96         PyObject *_tmp_dst = (*_tmp_dst_ptr);                   \
97         *_tmp_dst_ptr = _PyObject_CAST(src);                    \
98         Py_DECREF(_tmp_dst);                                    \
99     } while (0)
100 
101 #define Py_XSETREF(dst, src)                                    \
102     do {                                                        \
103         PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
104         PyObject *_tmp_dst = (*_tmp_dst_ptr);                   \
105         *_tmp_dst_ptr = _PyObject_CAST(src);                    \
106         Py_XDECREF(_tmp_dst);                                   \
107     } while (0)
108 #endif
109 
110 
111 // bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
112 // to Python 3.10.0b1.
113 #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
114 #  define Py_Is(x, y) ((x) == (y))
115 #endif
116 #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
117 #  define Py_IsNone(x) Py_Is(x, Py_None)
118 #endif
119 #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsTrue)
120 #  define Py_IsTrue(x) Py_Is(x, Py_True)
121 #endif
122 #if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsFalse)
123 #  define Py_IsFalse(x) Py_Is(x, Py_False)
124 #endif
125 
126 
127 // bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
128 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
129 PYCAPI_COMPAT_STATIC_INLINE(void)
_Py_SET_TYPE(PyObject * ob,PyTypeObject * type)130 _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
131 {
132     ob->ob_type = type;
133 }
134 #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
135 #endif
136 
137 
138 // bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
139 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
140 PYCAPI_COMPAT_STATIC_INLINE(void)
_Py_SET_SIZE(PyVarObject * ob,Py_ssize_t size)141 _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
142 {
143     ob->ob_size = size;
144 }
145 #define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
146 #endif
147 
148 
149 // bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
150 #if PY_VERSION_HEX < 0x030900B1 || defined(PYPY_VERSION)
151 PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
PyFrame_GetCode(PyFrameObject * frame)152 PyFrame_GetCode(PyFrameObject *frame)
153 {
154     assert(frame != _Py_NULL);
155     assert(frame->f_code != _Py_NULL);
156     return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
157 }
158 #endif
159 
160 PYCAPI_COMPAT_STATIC_INLINE(PyCodeObject*)
_PyFrame_GetCodeBorrow(PyFrameObject * frame)161 _PyFrame_GetCodeBorrow(PyFrameObject *frame)
162 {
163     PyCodeObject *code = PyFrame_GetCode(frame);
164     Py_DECREF(code);
165     return code;
166 }
167 
168 
169 // bpo-40421 added PyFrame_GetBack() to Python 3.9.0b1
170 #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
171 PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyFrame_GetBack(PyFrameObject * frame)172 PyFrame_GetBack(PyFrameObject *frame)
173 {
174     assert(frame != _Py_NULL);
175     return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
176 }
177 #endif
178 
179 #if !defined(PYPY_VERSION)
180 PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
_PyFrame_GetBackBorrow(PyFrameObject * frame)181 _PyFrame_GetBackBorrow(PyFrameObject *frame)
182 {
183     PyFrameObject *back = PyFrame_GetBack(frame);
184     Py_XDECREF(back);
185     return back;
186 }
187 #endif
188 
189 
190 // bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
191 #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
192 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetLocals(PyFrameObject * frame)193 PyFrame_GetLocals(PyFrameObject *frame)
194 {
195 #if PY_VERSION_HEX >= 0x030400B1
196     if (PyFrame_FastToLocalsWithError(frame) < 0) {
197         return NULL;
198     }
199 #else
200     PyFrame_FastToLocals(frame);
201 #endif
202     return Py_NewRef(frame->f_locals);
203 }
204 #endif
205 
206 
207 // bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
208 #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
209 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetGlobals(PyFrameObject * frame)210 PyFrame_GetGlobals(PyFrameObject *frame)
211 {
212     return Py_NewRef(frame->f_globals);
213 }
214 #endif
215 
216 
217 // bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
218 #if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
219 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetBuiltins(PyFrameObject * frame)220 PyFrame_GetBuiltins(PyFrameObject *frame)
221 {
222     return Py_NewRef(frame->f_builtins);
223 }
224 #endif
225 
226 
227 // bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
228 #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
229 PYCAPI_COMPAT_STATIC_INLINE(int)
PyFrame_GetLasti(PyFrameObject * frame)230 PyFrame_GetLasti(PyFrameObject *frame)
231 {
232 #if PY_VERSION_HEX >= 0x030A00A7
233     // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
234     // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
235     // instructions.
236     if (frame->f_lasti < 0) {
237         return -1;
238     }
239     return frame->f_lasti * 2;
240 #else
241     return frame->f_lasti;
242 #endif
243 }
244 #endif
245 
246 
247 // gh-91248 added PyFrame_GetVar() to Python 3.12.0a2
248 #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
249 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetVar(PyFrameObject * frame,PyObject * name)250 PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
251 {
252     PyObject *locals, *value;
253 
254     locals = PyFrame_GetLocals(frame);
255     if (locals == NULL) {
256         return NULL;
257     }
258 #if PY_VERSION_HEX >= 0x03000000
259     value = PyDict_GetItemWithError(locals, name);
260 #else
261     value = PyDict_GetItem(locals, name);
262 #endif
263     Py_DECREF(locals);
264 
265     if (value == NULL) {
266         if (PyErr_Occurred()) {
267             return NULL;
268         }
269 #if PY_VERSION_HEX >= 0x03000000
270         PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
271 #else
272         PyErr_SetString(PyExc_NameError, "variable does not exist");
273 #endif
274         return NULL;
275     }
276     return Py_NewRef(value);
277 }
278 #endif
279 
280 
281 // gh-91248 added PyFrame_GetVarString() to Python 3.12.0a2
282 #if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
283 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyFrame_GetVarString(PyFrameObject * frame,const char * name)284 PyFrame_GetVarString(PyFrameObject *frame, const char *name)
285 {
286     PyObject *name_obj, *value;
287     name_obj = PyUnicode_FromString(name);
288     if (name_obj == NULL) {
289         return NULL;
290     }
291     value = PyFrame_GetVar(frame, name_obj);
292     Py_DECREF(name_obj);
293     return value;
294 }
295 #endif
296 
297 
298 // bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
299 #if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
300 PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState *)
PyThreadState_GetInterpreter(PyThreadState * tstate)301 PyThreadState_GetInterpreter(PyThreadState *tstate)
302 {
303     assert(tstate != _Py_NULL);
304     return tstate->interp;
305 }
306 #endif
307 
308 
309 // bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
310 #if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
311 PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
PyThreadState_GetFrame(PyThreadState * tstate)312 PyThreadState_GetFrame(PyThreadState *tstate)
313 {
314     assert(tstate != _Py_NULL);
315     return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
316 }
317 #endif
318 
319 #if !defined(PYPY_VERSION)
320 PYCAPI_COMPAT_STATIC_INLINE(PyFrameObject*)
_PyThreadState_GetFrameBorrow(PyThreadState * tstate)321 _PyThreadState_GetFrameBorrow(PyThreadState *tstate)
322 {
323     PyFrameObject *frame = PyThreadState_GetFrame(tstate);
324     Py_XDECREF(frame);
325     return frame;
326 }
327 #endif
328 
329 
330 // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
331 #if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
332 PYCAPI_COMPAT_STATIC_INLINE(PyInterpreterState*)
PyInterpreterState_Get(void)333 PyInterpreterState_Get(void)
334 {
335     PyThreadState *tstate;
336     PyInterpreterState *interp;
337 
338     tstate = PyThreadState_GET();
339     if (tstate == _Py_NULL) {
340         Py_FatalError("GIL released (tstate is NULL)");
341     }
342     interp = tstate->interp;
343     if (interp == _Py_NULL) {
344         Py_FatalError("no current interpreter");
345     }
346     return interp;
347 }
348 #endif
349 
350 
351 // bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
352 #if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
353 PYCAPI_COMPAT_STATIC_INLINE(uint64_t)
354 PyThreadState_GetID(PyThreadState *tstate)
355 {
356     assert(tstate != _Py_NULL);
357     return tstate->id;
358 }
359 #endif
360 
361 // bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
362 #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
363 PYCAPI_COMPAT_STATIC_INLINE(void)
PyThreadState_EnterTracing(PyThreadState * tstate)364 PyThreadState_EnterTracing(PyThreadState *tstate)
365 {
366     tstate->tracing++;
367 #if PY_VERSION_HEX >= 0x030A00A1
368     tstate->cframe->use_tracing = 0;
369 #else
370     tstate->use_tracing = 0;
371 #endif
372 }
373 #endif
374 
375 // bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
376 #if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
377 PYCAPI_COMPAT_STATIC_INLINE(void)
PyThreadState_LeaveTracing(PyThreadState * tstate)378 PyThreadState_LeaveTracing(PyThreadState *tstate)
379 {
380     int use_tracing = (tstate->c_tracefunc != _Py_NULL
381                        || tstate->c_profilefunc != _Py_NULL);
382     tstate->tracing--;
383 #if PY_VERSION_HEX >= 0x030A00A1
384     tstate->cframe->use_tracing = use_tracing;
385 #else
386     tstate->use_tracing = use_tracing;
387 #endif
388 }
389 #endif
390 
391 
392 // bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
393 // PyObject_CallNoArgs() added to PyPy 3.9.16-v7.3.11
394 #if !defined(PyObject_CallNoArgs) && PY_VERSION_HEX < 0x030900A1
395 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyObject_CallNoArgs(PyObject * func)396 PyObject_CallNoArgs(PyObject *func)
397 {
398     return PyObject_CallFunctionObjArgs(func, NULL);
399 }
400 
401 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyObject_CallMethodNoArgs(PyObject * obj,PyObject * name)402 PyObject_CallMethodNoArgs(PyObject *obj, PyObject *name)
403 {
404     return PyObject_CallMethodObjArgs(obj, name, NULL);
405 }
406 #endif
407 
408 
409 // bpo-39245 made PyObject_CallOneArg() public (previously called
410 // _PyObject_CallOneArg) in Python 3.9.0a4
411 // PyObject_CallOneArg() added to PyPy 3.9.16-v7.3.11
412 #if !defined(PyObject_CallOneArg) && PY_VERSION_HEX < 0x030900A4
413 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyObject_CallOneArg(PyObject * func,PyObject * arg)414 PyObject_CallOneArg(PyObject *func, PyObject *arg)
415 {
416     return PyObject_CallFunctionObjArgs(func, arg, NULL);
417 }
418 
419 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyObject_CallMethodOneArg(PyObject * obj,PyObject * name,PyObject * arg)420 PyObject_CallMethodOneArg(PyObject *obj, PyObject *name, PyObject *arg)
421 {
422     return PyObject_CallMethodObjArgs(obj, name, arg, NULL);
423 }
424 #endif
425 
426 
427 // bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
428 #if PY_VERSION_HEX < 0x030A00A3
429 PYCAPI_COMPAT_STATIC_INLINE(int)
PyModule_AddObjectRef(PyObject * module,const char * name,PyObject * value)430 PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
431 {
432     int res;
433     Py_XINCREF(value);
434     res = PyModule_AddObject(module, name, value);
435     if (res < 0) {
436         Py_XDECREF(value);
437     }
438     return res;
439 }
440 #endif
441 
442 
443 // bpo-40024 added PyModule_AddType() to Python 3.9.0a5
444 #if PY_VERSION_HEX < 0x030900A5
445 PYCAPI_COMPAT_STATIC_INLINE(int)
PyModule_AddType(PyObject * module,PyTypeObject * type)446 PyModule_AddType(PyObject *module, PyTypeObject *type)
447 {
448     const char *name, *dot;
449 
450     if (PyType_Ready(type) < 0) {
451         return -1;
452     }
453 
454     // inline _PyType_Name()
455     name = type->tp_name;
456     assert(name != _Py_NULL);
457     dot = strrchr(name, '.');
458     if (dot != _Py_NULL) {
459         name = dot + 1;
460     }
461 
462     return PyModule_AddObjectRef(module, name, _PyObject_CAST(type));
463 }
464 #endif
465 
466 
467 // bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
468 // bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
469 #if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
470 PYCAPI_COMPAT_STATIC_INLINE(int)
PyObject_GC_IsTracked(PyObject * obj)471 PyObject_GC_IsTracked(PyObject* obj)
472 {
473     return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
474 }
475 #endif
476 
477 // bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
478 // bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
479 #if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
480 PYCAPI_COMPAT_STATIC_INLINE(int)
PyObject_GC_IsFinalized(PyObject * obj)481 PyObject_GC_IsFinalized(PyObject *obj)
482 {
483     PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
484     return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
485 }
486 #endif
487 
488 
489 // bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
490 #if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
491 PYCAPI_COMPAT_STATIC_INLINE(int)
_Py_IS_TYPE(PyObject * ob,PyTypeObject * type)492 _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
493     return Py_TYPE(ob) == type;
494 }
495 #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
496 #endif
497 
498 
499 // bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7.
500 // bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.
501 // Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal
502 // C API: Python 3.11a2-3.11a6 versions are not supported.
503 #if 0x030600B1 <= PY_VERSION_HEX && PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
504 PYCAPI_COMPAT_STATIC_INLINE(int)
505 PyFloat_Pack2(double x, char *p, int le)
506 { return _PyFloat_Pack2(x, (unsigned char*)p, le); }
507 
508 PYCAPI_COMPAT_STATIC_INLINE(double)
509 PyFloat_Unpack2(const char *p, int le)
510 { return _PyFloat_Unpack2((const unsigned char *)p, le); }
511 #endif
512 
513 
514 // bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and
515 // PyFloat_Unpack8() to Python 3.11a7.
516 // Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4()
517 // and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions
518 // are not supported.
519 #if PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
520 PYCAPI_COMPAT_STATIC_INLINE(int)
PyFloat_Pack4(double x,char * p,int le)521 PyFloat_Pack4(double x, char *p, int le)
522 { return _PyFloat_Pack4(x, (unsigned char*)p, le); }
523 
524 PYCAPI_COMPAT_STATIC_INLINE(int)
PyFloat_Pack8(double x,char * p,int le)525 PyFloat_Pack8(double x, char *p, int le)
526 { return _PyFloat_Pack8(x, (unsigned char*)p, le); }
527 
528 PYCAPI_COMPAT_STATIC_INLINE(double)
PyFloat_Unpack4(const char * p,int le)529 PyFloat_Unpack4(const char *p, int le)
530 { return _PyFloat_Unpack4((const unsigned char *)p, le); }
531 
532 PYCAPI_COMPAT_STATIC_INLINE(double)
PyFloat_Unpack8(const char * p,int le)533 PyFloat_Unpack8(const char *p, int le)
534 { return _PyFloat_Unpack8((const unsigned char *)p, le); }
535 #endif
536 
537 
538 // gh-92154 added PyCode_GetCode() to Python 3.11.0b1
539 #if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
540 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetCode(PyCodeObject * code)541 PyCode_GetCode(PyCodeObject *code)
542 {
543     return Py_NewRef(code->co_code);
544 }
545 #endif
546 
547 
548 // gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
549 #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
550 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetVarnames(PyCodeObject * code)551 PyCode_GetVarnames(PyCodeObject *code)
552 {
553     return Py_NewRef(code->co_varnames);
554 }
555 #endif
556 
557 // gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
558 #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
559 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetFreevars(PyCodeObject * code)560 PyCode_GetFreevars(PyCodeObject *code)
561 {
562     return Py_NewRef(code->co_freevars);
563 }
564 #endif
565 
566 // gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
567 #if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
568 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyCode_GetCellvars(PyCodeObject * code)569 PyCode_GetCellvars(PyCodeObject *code)
570 {
571     return Py_NewRef(code->co_cellvars);
572 }
573 #endif
574 
575 
576 // Py_UNUSED() was added to Python 3.4.0b2.
577 #if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
578 #  if defined(__GNUC__) || defined(__clang__)
579 #    define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
580 #  else
581 #    define Py_UNUSED(name) _unused_ ## name
582 #  endif
583 #endif
584 
585 
586 // gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
587 #if PY_VERSION_HEX < 0x030D00A0
588 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyImport_AddModuleRef(const char * name)589 PyImport_AddModuleRef(const char *name)
590 {
591     return Py_XNewRef(PyImport_AddModule(name));
592 }
593 #endif
594 
595 
596 // gh-105927 added PyWeakref_GetRef() to Python 3.13.0a1
597 #if PY_VERSION_HEX < 0x030D0000
598 PYCAPI_COMPAT_STATIC_INLINE(int)
PyWeakref_GetRef(PyObject * ref,PyObject ** pobj)599 PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
600 {
601     PyObject *obj;
602     if (ref != NULL && !PyWeakref_Check(ref)) {
603         *pobj = NULL;
604         PyErr_SetString(PyExc_TypeError, "expected a weakref");
605         return -1;
606     }
607     obj = PyWeakref_GetObject(ref);
608     if (obj == NULL) {
609         // SystemError if ref is NULL
610         *pobj = NULL;
611         return -1;
612     }
613     if (obj == Py_None) {
614         *pobj = NULL;
615         return 0;
616     }
617     *pobj = Py_NewRef(obj);
618     return (*pobj != NULL);
619 }
620 #endif
621 
622 
623 // bpo-36974 added PY_VECTORCALL_ARGUMENTS_OFFSET to Python 3.8b1
624 #ifndef PY_VECTORCALL_ARGUMENTS_OFFSET
625 #  define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
626 #endif
627 
628 // bpo-36974 added PyVectorcall_NARGS() to Python 3.8b1
629 #if PY_VERSION_HEX < 0x030800B1
630 static inline Py_ssize_t
PyVectorcall_NARGS(size_t n)631 PyVectorcall_NARGS(size_t n)
632 {
633     return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
634 }
635 #endif
636 
637 
638 // gh-105922 added PyObject_Vectorcall() to Python 3.9.0a4
639 #if PY_VERSION_HEX < 0x030900A4
640 PYCAPI_COMPAT_STATIC_INLINE(PyObject*)
PyObject_Vectorcall(PyObject * callable,PyObject * const * args,size_t nargsf,PyObject * kwnames)641 PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
642                      size_t nargsf, PyObject *kwnames)
643 {
644 #if PY_VERSION_HEX >= 0x030800B1 && !defined(PYPY_VERSION)
645     // bpo-36974 added _PyObject_Vectorcall() to Python 3.8.0b1
646     return _PyObject_Vectorcall(callable, args, nargsf, kwnames);
647 #else
648     PyObject *posargs = NULL, *kwargs = NULL;
649     PyObject *res;
650     Py_ssize_t nposargs, nkwargs, i;
651 
652     if (nargsf != 0 && args == NULL) {
653         PyErr_BadInternalCall();
654         goto error;
655     }
656     if (kwnames != NULL && !PyTuple_Check(kwnames)) {
657         PyErr_BadInternalCall();
658         goto error;
659     }
660 
661     nposargs = (Py_ssize_t)PyVectorcall_NARGS(nargsf);
662     if (kwnames) {
663         nkwargs = PyTuple_GET_SIZE(kwnames);
664     }
665     else {
666         nkwargs = 0;
667     }
668 
669     posargs = PyTuple_New(nposargs);
670     if (posargs == NULL) {
671         goto error;
672     }
673     if (nposargs) {
674         for (i=0; i < nposargs; i++) {
675             PyTuple_SET_ITEM(posargs, i, Py_NewRef(*args));
676             args++;
677         }
678     }
679 
680     if (nkwargs) {
681         kwargs = PyDict_New();
682         if (kwargs == NULL) {
683             goto error;
684         }
685 
686         for (i = 0; i < nkwargs; i++) {
687             PyObject *key = PyTuple_GET_ITEM(kwnames, i);
688             PyObject *value = *args;
689             args++;
690             if (PyDict_SetItem(kwargs, key, value) < 0) {
691                 goto error;
692             }
693         }
694     }
695     else {
696         kwargs = NULL;
697     }
698 
699     res = PyObject_Call(callable, posargs, kwargs);
700     Py_DECREF(posargs);
701     Py_XDECREF(kwargs);
702     return res;
703 
704 error:
705     Py_DECREF(posargs);
706     Py_XDECREF(kwargs);
707     return NULL;
708 #endif
709 }
710 #endif
711 
712 
713 #ifdef __cplusplus
714 }
715 #endif
716 #endif  // PYTHONCAPI_COMPAT
717