CoolFace
Apppublic

Aluode/PerceptionLabPortable

sourceHugging Faceupdated 9mo agoView on Hugging Face
0likes
pythoncapi_compat.h1697 linesDownload Raw Back to numba
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/0041177c4f348c8952b4c8980b2c90856e61c7c7/pythoncapi_compat.h
11//
12// SPDX-License-Identifier: 0BSD
13
14#ifndef PYTHONCAPI_COMPAT
15#define PYTHONCAPI_COMPAT
16
17#ifdef __cplusplus
18extern "C" {
19#endif
20
21#include <Python.h>
22
23// Python 3.11.0b4 added PyFrame_Back() to Python.h
24#if PY_VERSION_HEX < 0x030b00B4 && !defined(PYPY_VERSION)
25#  include "frameobject.h"        // PyFrameObject, PyFrame_GetBack()
26#endif
27
28
29#ifndef _Py_CAST
30#  define _Py_CAST(type, expr) ((type)(expr))
31#endif
32
33// Static inline functions should use _Py_NULL rather than using directly NULL
34// to prevent C++ compiler warnings. On C23 and newer and on C++11 and newer,
35// _Py_NULL is defined as nullptr.
36#if (defined (__STDC_VERSION__) && __STDC_VERSION__ > 201710L) \
37        || (defined(__cplusplus) && __cplusplus >= 201103)
38#  define _Py_NULL nullptr
39#else
40#  define _Py_NULL NULL
41#endif
42
43// Cast argument to PyObject* type.
44#ifndef _PyObject_CAST
45#  define _PyObject_CAST(op) _Py_CAST(PyObject*, op)
46#endif
47
48#ifndef Py_BUILD_ASSERT
49#  define Py_BUILD_ASSERT(cond) \
50        do { \
51            (void)sizeof(char [1 - 2 * !(cond)]); \
52        } while(0)
53#endif
54
55
56// bpo-42262 added Py_NewRef() to Python 3.10.0a3
57#if PY_VERSION_HEX < 0x030A00A3 && !defined(Py_NewRef)
58static inline PyObject* _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)
69static inline PyObject* _Py_XNewRef(PyObject *obj)
70{
71    Py_XINCREF(obj);
72    return obj;
73}
74#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
75#endif
76
77
78// bpo-39573 added Py_SET_REFCNT() to Python 3.9.0a4
79#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_REFCNT)
80static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt)
81{
82    ob->ob_refcnt = refcnt;
83}
84#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
85#endif
86
87
88// Py_SETREF() and Py_XSETREF() were added to Python 3.5.2.
89// It is excluded from the limited C API.
90#if (PY_VERSION_HEX < 0x03050200 && !defined(Py_SETREF)) && !defined(Py_LIMITED_API)
91#define Py_SETREF(dst, src)                                     \
92    do {                                                        \
93        PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
94        PyObject *_tmp_dst = (*_tmp_dst_ptr);                   \
95        *_tmp_dst_ptr = _PyObject_CAST(src);                    \
96        Py_DECREF(_tmp_dst);                                    \
97    } while (0)
98
99#define Py_XSETREF(dst, src)                                    \
100    do {                                                        \
101        PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
102        PyObject *_tmp_dst = (*_tmp_dst_ptr);                   \
103        *_tmp_dst_ptr = _PyObject_CAST(src);                    \
104        Py_XDECREF(_tmp_dst);                                   \
105    } while (0)
106#endif
107
108
109// bpo-43753 added Py_Is(), Py_IsNone(), Py_IsTrue() and Py_IsFalse()
110// to Python 3.10.0b1.
111#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_Is)
112#  define Py_Is(x, y) ((x) == (y))
113#endif
114#if PY_VERSION_HEX < 0x030A00B1 && !defined(Py_IsNone)
115#  define Py_IsNone(x) Py_Is(x, Py_None)
116#endif
117#if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsTrue)
118#  define Py_IsTrue(x) Py_Is(x, Py_True)
119#endif
120#if (PY_VERSION_HEX < 0x030A00B1 || defined(PYPY_VERSION)) && !defined(Py_IsFalse)
121#  define Py_IsFalse(x) Py_Is(x, Py_False)
122#endif
123
124
125// bpo-39573 added Py_SET_TYPE() to Python 3.9.0a4
126#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_TYPE)
127static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type)
128{
129    ob->ob_type = type;
130}
131#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
132#endif
133
134
135// bpo-39573 added Py_SET_SIZE() to Python 3.9.0a4
136#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_SET_SIZE)
137static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size)
138{
139    ob->ob_size = size;
140}
141#define Py_SET_SIZE(ob, size) _Py_SET_SIZE((PyVarObject*)(ob), size)
142#endif
143
144
145// bpo-40421 added PyFrame_GetCode() to Python 3.9.0b1
146#if PY_VERSION_HEX < 0x030900B1 || defined(PYPY_VERSION)
147static inline PyCodeObject* PyFrame_GetCode(PyFrameObject *frame)
148{
149    assert(frame != _Py_NULL);
150    assert(frame->f_code != _Py_NULL);
151    return _Py_CAST(PyCodeObject*, Py_NewRef(frame->f_code));
152}
153#endif
154
155static inline PyCodeObject* _PyFrame_GetCodeBorrow(PyFrameObject *frame)
156{
157    PyCodeObject *code = PyFrame_GetCode(frame);
158    Py_DECREF(code);
159    return code;
160}
161
162
163// bpo-40421 added PyFrame_GetBack() to Python 3.9.0b1
164#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
165static inline PyFrameObject* PyFrame_GetBack(PyFrameObject *frame)
166{
167    assert(frame != _Py_NULL);
168    return _Py_CAST(PyFrameObject*, Py_XNewRef(frame->f_back));
169}
170#endif
171
172#if !defined(PYPY_VERSION)
173static inline PyFrameObject* _PyFrame_GetBackBorrow(PyFrameObject *frame)
174{
175    PyFrameObject *back = PyFrame_GetBack(frame);
176    Py_XDECREF(back);
177    return back;
178}
179#endif
180
181
182// bpo-40421 added PyFrame_GetLocals() to Python 3.11.0a7
183#if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
184static inline PyObject* PyFrame_GetLocals(PyFrameObject *frame)
185{
186#if PY_VERSION_HEX >= 0x030400B1
187    if (PyFrame_FastToLocalsWithError(frame) < 0) {
188        return NULL;
189    }
190#else
191    PyFrame_FastToLocals(frame);
192#endif
193    return Py_NewRef(frame->f_locals);
194}
195#endif
196
197
198// bpo-40421 added PyFrame_GetGlobals() to Python 3.11.0a7
199#if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
200static inline PyObject* PyFrame_GetGlobals(PyFrameObject *frame)
201{
202    return Py_NewRef(frame->f_globals);
203}
204#endif
205
206
207// bpo-40421 added PyFrame_GetBuiltins() to Python 3.11.0a7
208#if PY_VERSION_HEX < 0x030B00A7 && !defined(PYPY_VERSION)
209static inline PyObject* PyFrame_GetBuiltins(PyFrameObject *frame)
210{
211    return Py_NewRef(frame->f_builtins);
212}
213#endif
214
215
216// bpo-40421 added PyFrame_GetLasti() to Python 3.11.0b1
217#if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
218static inline int PyFrame_GetLasti(PyFrameObject *frame)
219{
220#if PY_VERSION_HEX >= 0x030A00A7
221    // bpo-27129: Since Python 3.10.0a7, f_lasti is an instruction offset,
222    // not a bytes offset anymore. Python uses 16-bit "wordcode" (2 bytes)
223    // instructions.
224    if (frame->f_lasti < 0) {
225        return -1;
226    }
227    return frame->f_lasti * 2;
228#else
229    return frame->f_lasti;
230#endif
231}
232#endif
233
234
235// gh-91248 added PyFrame_GetVar() to Python 3.12.0a2
236#if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
237static inline PyObject* PyFrame_GetVar(PyFrameObject *frame, PyObject *name)
238{
239    PyObject *locals, *value;
240
241    locals = PyFrame_GetLocals(frame);
242    if (locals == NULL) {
243        return NULL;
244    }
245#if PY_VERSION_HEX >= 0x03000000
246    value = PyDict_GetItemWithError(locals, name);
247#else
248    value = _PyDict_GetItemWithError(locals, name);
249#endif
250    Py_DECREF(locals);
251
252    if (value == NULL) {
253        if (PyErr_Occurred()) {
254            return NULL;
255        }
256#if PY_VERSION_HEX >= 0x03000000
257        PyErr_Format(PyExc_NameError, "variable %R does not exist", name);
258#else
259        PyErr_SetString(PyExc_NameError, "variable does not exist");
260#endif
261        return NULL;
262    }
263    return Py_NewRef(value);
264}
265#endif
266
267
268// gh-91248 added PyFrame_GetVarString() to Python 3.12.0a2
269#if PY_VERSION_HEX < 0x030C00A2 && !defined(PYPY_VERSION)
270static inline PyObject*
271PyFrame_GetVarString(PyFrameObject *frame, const char *name)
272{
273    PyObject *name_obj, *value;
274#if PY_VERSION_HEX >= 0x03000000
275    name_obj = PyUnicode_FromString(name);
276#else
277    name_obj = PyString_FromString(name);
278#endif
279    if (name_obj == NULL) {
280        return NULL;
281    }
282    value = PyFrame_GetVar(frame, name_obj);
283    Py_DECREF(name_obj);
284    return value;
285}
286#endif
287
288
289// bpo-39947 added PyThreadState_GetInterpreter() to Python 3.9.0a5
290#if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
291static inline PyInterpreterState *
292PyThreadState_GetInterpreter(PyThreadState *tstate)
293{
294    assert(tstate != _Py_NULL);
295    return tstate->interp;
296}
297#endif
298
299
300// bpo-40429 added PyThreadState_GetFrame() to Python 3.9.0b1
301#if PY_VERSION_HEX < 0x030900B1 && !defined(PYPY_VERSION)
302static inline PyFrameObject* PyThreadState_GetFrame(PyThreadState *tstate)
303{
304    assert(tstate != _Py_NULL);
305    return _Py_CAST(PyFrameObject *, Py_XNewRef(tstate->frame));
306}
307#endif
308
309#if !defined(PYPY_VERSION)
310static inline PyFrameObject*
311_PyThreadState_GetFrameBorrow(PyThreadState *tstate)
312{
313    PyFrameObject *frame = PyThreadState_GetFrame(tstate);
314    Py_XDECREF(frame);
315    return frame;
316}
317#endif
318
319
320// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a5
321#if PY_VERSION_HEX < 0x030900A5 || defined(PYPY_VERSION)
322static inline PyInterpreterState* PyInterpreterState_Get(void)
323{
324    PyThreadState *tstate;
325    PyInterpreterState *interp;
326
327    tstate = PyThreadState_GET();
328    if (tstate == _Py_NULL) {
329        Py_FatalError("GIL released (tstate is NULL)");
330    }
331    interp = tstate->interp;
332    if (interp == _Py_NULL) {
333        Py_FatalError("no current interpreter");
334    }
335    return interp;
336}
337#endif
338
339
340// bpo-39947 added PyInterpreterState_Get() to Python 3.9.0a6
341#if 0x030700A1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
342static inline uint64_t PyThreadState_GetID(PyThreadState *tstate)
343{
344    assert(tstate != _Py_NULL);
345    return tstate->id;
346}
347#endif
348
349// bpo-43760 added PyThreadState_EnterTracing() to Python 3.11.0a2
350#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
351static inline void PyThreadState_EnterTracing(PyThreadState *tstate)
352{
353    tstate->tracing++;
354#if PY_VERSION_HEX >= 0x030A00A1
355    tstate->cframe->use_tracing = 0;
356#else
357    tstate->use_tracing = 0;
358#endif
359}
360#endif
361
362// bpo-43760 added PyThreadState_LeaveTracing() to Python 3.11.0a2
363#if PY_VERSION_HEX < 0x030B00A2 && !defined(PYPY_VERSION)
364static inline void PyThreadState_LeaveTracing(PyThreadState *tstate)
365{
366    int use_tracing = (tstate->c_tracefunc != _Py_NULL
367                       || tstate->c_profilefunc != _Py_NULL);
368    tstate->tracing--;
369#if PY_VERSION_HEX >= 0x030A00A1
370    tstate->cframe->use_tracing = use_tracing;
371#else
372    tstate->use_tracing = use_tracing;
373#endif
374}
375#endif
376
377
378// bpo-37194 added PyObject_CallNoArgs() to Python 3.9.0a1
379// PyObject_CallNoArgs() added to PyPy 3.9.16-v7.3.11
380#if !defined(PyObject_CallNoArgs) && PY_VERSION_HEX < 0x030900A1
381static inline PyObject* PyObject_CallNoArgs(PyObject *func)
382{
383    return PyObject_CallFunctionObjArgs(func, NULL);
384}
385#endif
386
387
388// bpo-39245 made PyObject_CallOneArg() public (previously called
389// _PyObject_CallOneArg) in Python 3.9.0a4
390// PyObject_CallOneArg() added to PyPy 3.9.16-v7.3.11
391#if !defined(PyObject_CallOneArg) && PY_VERSION_HEX < 0x030900A4
392static inline PyObject* PyObject_CallOneArg(PyObject *func, PyObject *arg)
393{
394    return PyObject_CallFunctionObjArgs(func, arg, NULL);
395}
396#endif
397
398
399// bpo-1635741 added PyModule_AddObjectRef() to Python 3.10.0a3
400#if PY_VERSION_HEX < 0x030A00A3
401static inline int
402PyModule_AddObjectRef(PyObject *module, const char *name, PyObject *value)
403{
404    int res;
405
406    if (!value && !PyErr_Occurred()) {
407        // PyModule_AddObject() raises TypeError in this case
408        PyErr_SetString(PyExc_SystemError,
409                        "PyModule_AddObjectRef() must be called "
410                        "with an exception raised if value is NULL");
411        return -1;
412    }
413
414    Py_XINCREF(value);
415    res = PyModule_AddObject(module, name, value);
416    if (res < 0) {
417        Py_XDECREF(value);
418    }
419    return res;
420}
421#endif
422
423
424// bpo-40024 added PyModule_AddType() to Python 3.9.0a5
425#if PY_VERSION_HEX < 0x030900A5
426static inline int PyModule_AddType(PyObject *module, PyTypeObject *type)
427{
428    const char *name, *dot;
429
430    if (PyType_Ready(type) < 0) {
431        return -1;
432    }
433
434    // inline _PyType_Name()
435    name = type->tp_name;
436    assert(name != _Py_NULL);
437    dot = strrchr(name, '.');
438    if (dot != _Py_NULL) {
439        name = dot + 1;
440    }
441
442    return PyModule_AddObjectRef(module, name, _PyObject_CAST(type));
443}
444#endif
445
446
447// bpo-40241 added PyObject_GC_IsTracked() to Python 3.9.0a6.
448// bpo-4688 added _PyObject_GC_IS_TRACKED() to Python 2.7.0a2.
449#if PY_VERSION_HEX < 0x030900A6 && !defined(PYPY_VERSION)
450static inline int PyObject_GC_IsTracked(PyObject* obj)
451{
452    return (PyObject_IS_GC(obj) && _PyObject_GC_IS_TRACKED(obj));
453}
454#endif
455
456// bpo-40241 added PyObject_GC_IsFinalized() to Python 3.9.0a6.
457// bpo-18112 added _PyGCHead_FINALIZED() to Python 3.4.0 final.
458#if PY_VERSION_HEX < 0x030900A6 && PY_VERSION_HEX >= 0x030400F0 && !defined(PYPY_VERSION)
459static inline int PyObject_GC_IsFinalized(PyObject *obj)
460{
461    PyGC_Head *gc = _Py_CAST(PyGC_Head*, obj) - 1;
462    return (PyObject_IS_GC(obj) && _PyGCHead_FINALIZED(gc));
463}
464#endif
465
466
467// bpo-39573 added Py_IS_TYPE() to Python 3.9.0a4
468#if PY_VERSION_HEX < 0x030900A4 && !defined(Py_IS_TYPE)
469static inline int _Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
470    return Py_TYPE(ob) == type;
471}
472#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST(ob), type)
473#endif
474
475
476// bpo-46906 added PyFloat_Pack2() and PyFloat_Unpack2() to Python 3.11a7.
477// bpo-11734 added _PyFloat_Pack2() and _PyFloat_Unpack2() to Python 3.6.0b1.
478// Python 3.11a2 moved _PyFloat_Pack2() and _PyFloat_Unpack2() to the internal
479// C API: Python 3.11a2-3.11a6 versions are not supported.
480#if 0x030600B1 <= PY_VERSION_HEX && PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
481static inline int PyFloat_Pack2(double x, char *p, int le)
482{ return _PyFloat_Pack2(x, (unsigned char*)p, le); }
483
484static inline double PyFloat_Unpack2(const char *p, int le)
485{ return _PyFloat_Unpack2((const unsigned char *)p, le); }
486#endif
487
488
489// bpo-46906 added PyFloat_Pack4(), PyFloat_Pack8(), PyFloat_Unpack4() and
490// PyFloat_Unpack8() to Python 3.11a7.
491// Python 3.11a2 moved _PyFloat_Pack4(), _PyFloat_Pack8(), _PyFloat_Unpack4()
492// and _PyFloat_Unpack8() to the internal C API: Python 3.11a2-3.11a6 versions
493// are not supported.
494#if PY_VERSION_HEX <= 0x030B00A1 && !defined(PYPY_VERSION)
495static inline int PyFloat_Pack4(double x, char *p, int le)
496{ return _PyFloat_Pack4(x, (unsigned char*)p, le); }
497
498static inline int PyFloat_Pack8(double x, char *p, int le)
499{ return _PyFloat_Pack8(x, (unsigned char*)p, le); }
500
501static inline double PyFloat_Unpack4(const char *p, int le)
502{ return _PyFloat_Unpack4((const unsigned char *)p, le); }
503
504static inline double PyFloat_Unpack8(const char *p, int le)
505{ return _PyFloat_Unpack8((const unsigned char *)p, le); }
506#endif
507
508
509// gh-92154 added PyCode_GetCode() to Python 3.11.0b1
510#if PY_VERSION_HEX < 0x030B00B1 && !defined(PYPY_VERSION)
511static inline PyObject* PyCode_GetCode(PyCodeObject *code)
512{
513    return Py_NewRef(code->co_code);
514}
515#endif
516
517
518// gh-95008 added PyCode_GetVarnames() to Python 3.11.0rc1
519#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
520static inline PyObject* PyCode_GetVarnames(PyCodeObject *code)
521{
522    return Py_NewRef(code->co_varnames);
523}
524#endif
525
526// gh-95008 added PyCode_GetFreevars() to Python 3.11.0rc1
527#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
528static inline PyObject* PyCode_GetFreevars(PyCodeObject *code)
529{
530    return Py_NewRef(code->co_freevars);
531}
532#endif
533
534// gh-95008 added PyCode_GetCellvars() to Python 3.11.0rc1
535#if PY_VERSION_HEX < 0x030B00C1 && !defined(PYPY_VERSION)
536static inline PyObject* PyCode_GetCellvars(PyCodeObject *code)
537{
538    return Py_NewRef(code->co_cellvars);
539}
540#endif
541
542
543// Py_UNUSED() was added to Python 3.4.0b2.
544#if PY_VERSION_HEX < 0x030400B2 && !defined(Py_UNUSED)
545#  if defined(__GNUC__) || defined(__clang__)
546#    define Py_UNUSED(name) _unused_ ## name __attribute__((unused))
547#  else
548#    define Py_UNUSED(name) _unused_ ## name
549#  endif
550#endif
551
552
553// gh-105922 added PyImport_AddModuleRef() to Python 3.13.0a1
554#if PY_VERSION_HEX < 0x030D00A0
555static inline PyObject* PyImport_AddModuleRef(const char *name)
556{
557    return Py_XNewRef(PyImport_AddModule(name));
558}
559#endif
560
561
562// gh-105927 added PyWeakref_GetRef() to Python 3.13.0a1
563#if PY_VERSION_HEX < 0x030D0000
564static inline int PyWeakref_GetRef(PyObject *ref, PyObject **pobj)
565{
566    PyObject *obj;
567    if (ref != NULL && !PyWeakref_Check(ref)) {
568        *pobj = NULL;
569        PyErr_SetString(PyExc_TypeError, "expected a weakref");
570        return -1;
571    }
572    obj = PyWeakref_GetObject(ref);
573    if (obj == NULL) {
574        // SystemError if ref is NULL
575        *pobj = NULL;
576        return -1;
577    }
578    if (obj == Py_None) {
579        *pobj = NULL;
580        return 0;
581    }
582    *pobj = Py_NewRef(obj);
583    return (*pobj != NULL);
584}
585#endif
586
587
588// bpo-36974 added PY_VECTORCALL_ARGUMENTS_OFFSET to Python 3.8b1
589#ifndef PY_VECTORCALL_ARGUMENTS_OFFSET
590#  define PY_VECTORCALL_ARGUMENTS_OFFSET (_Py_CAST(size_t, 1) << (8 * sizeof(size_t) - 1))
591#endif
592
593// bpo-36974 added PyVectorcall_NARGS() to Python 3.8b1
594#if PY_VERSION_HEX < 0x030800B1
595static inline Py_ssize_t PyVectorcall_NARGS(size_t n)
596{
597    return n & ~PY_VECTORCALL_ARGUMENTS_OFFSET;
598}
599#endif
600
601
602// gh-105922 added PyObject_Vectorcall() to Python 3.9.0a4
603#if PY_VERSION_HEX < 0x030900A4
604static inline PyObject*
605PyObject_Vectorcall(PyObject *callable, PyObject *const *args,
606                     size_t nargsf, PyObject *kwnames)
607{
608#if PY_VERSION_HEX >= 0x030800B1 && !defined(PYPY_VERSION)
609    // bpo-36974 added _PyObject_Vectorcall() to Python 3.8.0b1
610    return _PyObject_Vectorcall(callable, args, nargsf, kwnames);
611#else
612    PyObject *posargs = NULL, *kwargs = NULL;
613    PyObject *res;
614    Py_ssize_t nposargs, nkwargs, i;
615
616    if (nargsf != 0 && args == NULL) {
617        PyErr_BadInternalCall();
618        goto error;
619    }
620    if (kwnames != NULL && !PyTuple_Check(kwnames)) {
621        PyErr_BadInternalCall();
622        goto error;
623    }
624
625    nposargs = (Py_ssize_t)PyVectorcall_NARGS(nargsf);
626    if (kwnames) {
627        nkwargs = PyTuple_GET_SIZE(kwnames);
628    }
629    else {
630        nkwargs = 0;
631    }
632
633    posargs = PyTuple_New(nposargs);
634    if (posargs == NULL) {
635        goto error;
636    }
637    if (nposargs) {
638        for (i=0; i < nposargs; i++) {
639            PyTuple_SET_ITEM(posargs, i, Py_NewRef(*args));
640            args++;
641        }
642    }
643
644    if (nkwargs) {
645        kwargs = PyDict_New();
646        if (kwargs == NULL) {
647            goto error;
648        }
649
650        for (i = 0; i < nkwargs; i++) {
651            PyObject *key = PyTuple_GET_ITEM(kwnames, i);
652            PyObject *value = *args;
653            args++;
654            if (PyDict_SetItem(kwargs, key, value) < 0) {
655                goto error;
656            }
657        }
658    }
659    else {
660        kwargs = NULL;
661    }
662
663    res = PyObject_Call(callable, posargs, kwargs);
664    Py_DECREF(posargs);
665    Py_XDECREF(kwargs);
666    return res;
667
668error:
669    Py_DECREF(posargs);
670    Py_XDECREF(kwargs);
671    return NULL;
672#endif
673}
674#endif
675
676
677// gh-106521 added PyObject_GetOptionalAttr() and
678// PyObject_GetOptionalAttrString() to Python 3.13.0a1
679#if PY_VERSION_HEX < 0x030D00A1
680static inline int
681PyObject_GetOptionalAttr(PyObject *obj, PyObject *attr_name, PyObject **result)
682{
683    // bpo-32571 added _PyObject_LookupAttr() to Python 3.7.0b1
684#if PY_VERSION_HEX >= 0x030700B1 && !defined(PYPY_VERSION)
685    return _PyObject_LookupAttr(obj, attr_name, result);
686#else
687    *result = PyObject_GetAttr(obj, attr_name);
688    if (*result != NULL) {
689        return 1;
690    }
691    if (!PyErr_Occurred()) {
692        return 0;
693    }
694    if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
695        PyErr_Clear();
696        return 0;
697    }
698    return -1;
699#endif
700}
701
702static inline int
703PyObject_GetOptionalAttrString(PyObject *obj, const char *attr_name, PyObject **result)
704{
705    PyObject *name_obj;
706    int rc;
707#if PY_VERSION_HEX >= 0x03000000
708    name_obj = PyUnicode_FromString(attr_name);
709#else
710    name_obj = PyString_FromString(attr_name);
711#endif
712    if (name_obj == NULL) {
713        *result = NULL;
714        return -1;
715    }
716    rc = PyObject_GetOptionalAttr(obj, name_obj, result);
717    Py_DECREF(name_obj);
718    return rc;
719}
720#endif
721
722
723// gh-106307 added PyObject_GetOptionalAttr() and
724// PyMapping_GetOptionalItemString() to Python 3.13.0a1
725#if PY_VERSION_HEX < 0x030D00A1
726static inline int
727PyMapping_GetOptionalItem(PyObject *obj, PyObject *key, PyObject **result)
728{
729    *result = PyObject_GetItem(obj, key);
730    if (*result) {
731        return 1;
732    }
733    if (!PyErr_ExceptionMatches(PyExc_KeyError)) {
734        return -1;
735    }
736    PyErr_Clear();
737    return 0;
738}
739
740static inline int
741PyMapping_GetOptionalItemString(PyObject *obj, const char *key, PyObject **result)
742{
743    PyObject *key_obj;
744    int rc;
745#if PY_VERSION_HEX >= 0x03000000
746    key_obj = PyUnicode_FromString(key);
747#else
748    key_obj = PyString_FromString(key);
749#endif
750    if (key_obj == NULL) {
751        *result = NULL;
752        return -1;
753    }
754    rc = PyMapping_GetOptionalItem(obj, key_obj, result);
755    Py_DECREF(key_obj);
756    return rc;
757}
758#endif
759
760// gh-108511 added PyMapping_HasKeyWithError() and
761// PyMapping_HasKeyStringWithError() to Python 3.13.0a1
762#if PY_VERSION_HEX < 0x030D00A1
763static inline int
764PyMapping_HasKeyWithError(PyObject *obj, PyObject *key)
765{
766    PyObject *res;
767    int rc = PyMapping_GetOptionalItem(obj, key, &res);
768    Py_XDECREF(res);
769    return rc;
770}
771
772static inline int
773PyMapping_HasKeyStringWithError(PyObject *obj, const char *key)
774{
775    PyObject *res;
776    int rc = PyMapping_GetOptionalItemString(obj, key, &res);
777    Py_XDECREF(res);
778    return rc;
779}
780#endif
781
782
783// gh-108511 added PyObject_HasAttrWithError() and
784// PyObject_HasAttrStringWithError() to Python 3.13.0a1
785#if PY_VERSION_HEX < 0x030D00A1
786static inline int
787PyObject_HasAttrWithError(PyObject *obj, PyObject *attr)
788{
789    PyObject *res;
790    int rc = PyObject_GetOptionalAttr(obj, attr, &res);
791    Py_XDECREF(res);
792    return rc;
793}
794
795static inline int
796PyObject_HasAttrStringWithError(PyObject *obj, const char *attr)
797{
798    PyObject *res;
799    int rc = PyObject_GetOptionalAttrString(obj, attr, &res);
800    Py_XDECREF(res);
801    return rc;
802}
803#endif
804
805
806// gh-106004 added PyDict_GetItemRef() and PyDict_GetItemStringRef()
807// to Python 3.13.0a1
808#if PY_VERSION_HEX < 0x030D00A1
809static inline int
810PyDict_GetItemRef(PyObject *mp, PyObject *key, PyObject **result)
811{
812#if PY_VERSION_HEX >= 0x03000000
813    PyObject *item = PyDict_GetItemWithError(mp, key);
814#else
815    PyObject *item = _PyDict_GetItemWithError(mp, key);
816#endif
817    if (item != NULL) {
818        *result = Py_NewRef(item);
819        return 1;  // found
820    }
821    if (!PyErr_Occurred()) {
822        *result = NULL;
823        return 0;  // not found
824    }
825    *result = NULL;
826    return -1;
827}
828
829static inline int
830PyDict_GetItemStringRef(PyObject *mp, const char *key, PyObject **result)
831{
832    int res;
833#if PY_VERSION_HEX >= 0x03000000
834    PyObject *key_obj = PyUnicode_FromString(key);
835#else
836    PyObject *key_obj = PyString_FromString(key);
837#endif
838    if (key_obj == NULL) {
839        *result = NULL;
840        return -1;
841    }
842    res = PyDict_GetItemRef(mp, key_obj, result);
843    Py_DECREF(key_obj);
844    return res;
845}
846#endif
847
848
849// gh-106307 added PyModule_Add() to Python 3.13.0a1
850#if PY_VERSION_HEX < 0x030D00A1
851static inline int
852PyModule_Add(PyObject *mod, const char *name, PyObject *value)
853{
854    int res = PyModule_AddObjectRef(mod, name, value);
855    Py_XDECREF(value);
856    return res;
857}
858#endif
859
860
861// gh-108014 added Py_IsFinalizing() to Python 3.13.0a1
862// bpo-1856 added _Py_Finalizing to Python 3.2.1b1.
863// _Py_IsFinalizing() was added to PyPy 7.3.0.
864#if (0x030201B1 <= PY_VERSION_HEX && PY_VERSION_HEX < 0x030D00A1) \
865        && (!defined(PYPY_VERSION_NUM) || PYPY_VERSION_NUM >= 0x7030000)
866static inline int Py_IsFinalizing(void)
867{
868#if PY_VERSION_HEX >= 0x030700A1
869    // _Py_IsFinalizing() was added to Python 3.7.0a1.
870    return _Py_IsFinalizing();
871#else
872    return (_Py_Finalizing != NULL);
873#endif
874}
875#endif
876
877
878// gh-108323 added PyDict_ContainsString() to Python 3.13.0a1
879#if PY_VERSION_HEX < 0x030D00A1
880static inline int PyDict_ContainsString(PyObject *op, const char *key)
881{
882    PyObject *key_obj = PyUnicode_FromString(key);
883    if (key_obj == NULL) {
884        return -1;
885    }
886    int res = PyDict_Contains(op, key_obj);
887    Py_DECREF(key_obj);
888    return res;
889}
890#endif
891
892
893// gh-108445 added PyLong_AsInt() to Python 3.13.0a1
894#if PY_VERSION_HEX < 0x030D00A1
895static inline int PyLong_AsInt(PyObject *obj)
896{
897#ifdef PYPY_VERSION
898    long value = PyLong_AsLong(obj);
899    if (value == -1 && PyErr_Occurred()) {
900        return -1;
901    }
902    if (value < (long)INT_MIN || (long)INT_MAX < value) {
903        PyErr_SetString(PyExc_OverflowError,
904                        "Python int too large to convert to C int");
905        return -1;
906    }
907    return (int)value;
908#else
909    return _PyLong_AsInt(obj);
910#endif
911}
912#endif
913
914
915// gh-107073 added PyObject_VisitManagedDict() to Python 3.13.0a1
916#if PY_VERSION_HEX < 0x030D00A1
917static inline int
918PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg)
919{
920    PyObject **dict = _PyObject_GetDictPtr(obj);
921    if (*dict == NULL) {
922        return -1;
923    }
924    Py_VISIT(*dict);
925    return 0;
926}
927
928static inline void
929PyObject_ClearManagedDict(PyObject *obj)
930{
931    PyObject **dict = _PyObject_GetDictPtr(obj);
932    if (*dict == NULL) {
933        return;
934    }
935    Py_CLEAR(*dict);
936}
937#endif
938
939// gh-108867 added PyThreadState_GetUnchecked() to Python 3.13.0a1
940// Python 3.5.2 added _PyThreadState_UncheckedGet().
941#if PY_VERSION_HEX >= 0x03050200 && PY_VERSION_HEX < 0x030D00A1
942static inline PyThreadState*
943PyThreadState_GetUnchecked(void)
944{
945    return _PyThreadState_UncheckedGet();
946}
947#endif
948
949// gh-110289 added PyUnicode_EqualToUTF8() and PyUnicode_EqualToUTF8AndSize()
950// to Python 3.13.0a1
951#if PY_VERSION_HEX < 0x030D00A1
952static inline int
953PyUnicode_EqualToUTF8AndSize(PyObject *unicode, const char *str, Py_ssize_t str_len)
954{
955    Py_ssize_t len;
956    const void *utf8;
957    PyObject *exc_type, *exc_value, *exc_tb;
958    int res;
959
960    // API cannot report errors so save/restore the exception
961    PyErr_Fetch(&exc_type, &exc_value, &exc_tb);
962
963    // Python 3.3.0a1 added PyUnicode_AsUTF8AndSize()
964#if PY_VERSION_HEX >= 0x030300A1
965    if (PyUnicode_IS_ASCII(unicode)) {
966        utf8 = PyUnicode_DATA(unicode);
967        len = PyUnicode_GET_LENGTH(unicode);
968    }
969    else {
970        utf8 = PyUnicode_AsUTF8AndSize(unicode, &len);
971        if (utf8 == NULL) {
972            // Memory allocation failure. The API cannot report error,
973            // so ignore the exception and return 0.
974            res = 0;
975            goto done;
976        }
977    }
978
979    if (len != str_len) {
980        res = 0;
981        goto done;
982    }
983    res = (memcmp(utf8, str, (size_t)len) == 0);
984#else
985    PyObject *bytes = PyUnicode_AsUTF8String(unicode);
986    if (bytes == NULL) {
987        // Memory allocation failure. The API cannot report error,
988        // so ignore the exception and return 0.
989        res = 0;
990        goto done;
991    }
992
993#if PY_VERSION_HEX >= 0x03000000
994    len = PyBytes_GET_SIZE(bytes);
995    utf8 = PyBytes_AS_STRING(bytes);
996#else
997    len = PyString_GET_SIZE(bytes);
998    utf8 = PyString_AS_STRING(bytes);
999#endif
1000    if (len != str_len) {
1001        Py_DECREF(bytes);
1002        res = 0;
1003        goto done;
1004    }
1005
1006    res = (memcmp(utf8, str, (size_t)len) == 0);
1007    Py_DECREF(bytes);
1008#endif
1009
1010done:
1011    PyErr_Restore(exc_type, exc_value, exc_tb);
1012    return res;
1013}
1014
1015static inline int
1016PyUnicode_EqualToUTF8(PyObject *unicode, const char *str)
1017{
1018    return PyUnicode_EqualToUTF8AndSize(unicode, str, (Py_ssize_t)strlen(str));
1019}
1020#endif
1021
1022
1023// gh-111138 added PyList_Extend() and PyList_Clear() to Python 3.13.0a2
1024#if PY_VERSION_HEX < 0x030D00A2
1025static inline int
1026PyList_Extend(PyObject *list, PyObject *iterable)
1027{
1028    return PyList_SetSlice(list, PY_SSIZE_T_MAX, PY_SSIZE_T_MAX, iterable);
1029}
1030
1031static inline int
1032PyList_Clear(PyObject *list)
1033{
1034    return PyList_SetSlice(list, 0, PY_SSIZE_T_MAX, NULL);
1035}
1036#endif
1037
1038// gh-111262 added PyDict_Pop() and PyDict_PopString() to Python 3.13.0a2
1039#if PY_VERSION_HEX < 0x030D00A2
1040static inline int
1041PyDict_Pop(PyObject *dict, PyObject *key, PyObject **result)
1042{
1043    PyObject *value;
1044
1045    if (!PyDict_Check(dict)) {
1046        PyErr_BadInternalCall();
1047        if (result) {
1048            *result = NULL;
1049        }
1050        return -1;
1051    }
1052
1053    // bpo-16991 added _PyDict_Pop() to Python 3.5.0b2.
1054    // Python 3.6.0b3 changed _PyDict_Pop() first argument type to PyObject*.
1055    // Python 3.13.0a1 removed _PyDict_Pop().
1056#if defined(PYPY_VERSION) || PY_VERSION_HEX < 0x030500b2 || PY_VERSION_HEX >= 0x030D0000
1057    value = PyObject_CallMethod(dict, "pop", "O", key);
1058#elif PY_VERSION_HEX < 0x030600b3
1059    value = _PyDict_Pop(_Py_CAST(PyDictObject*, dict), key, NULL);
1060#else
1061    value = _PyDict_Pop(dict, key, NULL);
1062#endif
1063    if (value == NULL) {
1064        if (result) {
1065            *result = NULL;
1066        }
1067        if (PyErr_Occurred() && !PyErr_ExceptionMatches(PyExc_KeyError)) {
1068            return -1;
1069        }
1070        PyErr_Clear();
1071        return 0;
1072    }
1073    if (result) {
1074        *result = value;
1075    }
1076    else {
1077        Py_DECREF(value);
1078    }
1079    return 1;
1080}
1081
1082static inline int
1083PyDict_PopString(PyObject *dict, const char *key, PyObject **result)
1084{
1085    PyObject *key_obj = PyUnicode_FromString(key);
1086    if (key_obj == NULL) {
1087        if (result != NULL) {
1088            *result = NULL;
1089        }
1090        return -1;
1091    }
1092
1093    int res = PyDict_Pop(dict, key_obj, result);
1094    Py_DECREF(key_obj);
1095    return res;
1096}
1097#endif
1098
1099
1100#if PY_VERSION_HEX < 0x030200A4
1101// Python 3.2.0a4 added Py_hash_t type
1102typedef Py_ssize_t Py_hash_t;
1103#endif
1104
1105
1106// gh-111545 added Py_HashPointer() to Python 3.13.0a3
1107#if PY_VERSION_HEX < 0x030D00A3
1108static inline Py_hash_t Py_HashPointer(const void *ptr)
1109{
1110#if PY_VERSION_HEX >= 0x030900A4 && !defined(PYPY_VERSION)
1111    return _Py_HashPointer(ptr);
1112#else
1113    return _Py_HashPointer(_Py_CAST(void*, ptr));
1114#endif
1115}
1116#endif
1117
1118
1119// Python 3.13a4 added a PyTime API.
1120// Use the private API added to Python 3.5.
1121#if PY_VERSION_HEX < 0x030D00A4 && PY_VERSION_HEX  >= 0x03050000
1122typedef _PyTime_t PyTime_t;
1123#define PyTime_MIN _PyTime_MIN
1124#define PyTime_MAX _PyTime_MAX
1125
1126static inline double PyTime_AsSecondsDouble(PyTime_t t)
1127{ return _PyTime_AsSecondsDouble(t); }
1128
1129static inline int PyTime_Monotonic(PyTime_t *result)
1130{ return _PyTime_GetMonotonicClockWithInfo(result, NULL); }
1131
1132static inline int PyTime_Time(PyTime_t *result)
1133{ return _PyTime_GetSystemClockWithInfo(result, NULL); }
1134
1135static inline int PyTime_PerfCounter(PyTime_t *result)
1136{
1137#if PY_VERSION_HEX >= 0x03070000 && !defined(PYPY_VERSION)
1138    return _PyTime_GetPerfCounterWithInfo(result, NULL);
1139#elif PY_VERSION_HEX >= 0x03070000
1140    // Call time.perf_counter_ns() and convert Python int object to PyTime_t.
1141    // Cache time.perf_counter_ns() function for best performance.
1142    static PyObject *func = NULL;
1143    if (func == NULL) {
1144        PyObject *mod = PyImport_ImportModule("time");
1145        if (mod == NULL) {
1146            return -1;
1147        }
1148
1149        func = PyObject_GetAttrString(mod, "perf_counter_ns");
1150        Py_DECREF(mod);
1151        if (func == NULL) {
1152            return -1;
1153        }
1154    }
1155
1156    PyObject *res = PyObject_CallNoArgs(func);
1157    if (res == NULL) {
1158        return -1;
1159    }
1160    long long value = PyLong_AsLongLong(res);
1161    Py_DECREF(res);
1162
1163    if (value == -1 && PyErr_Occurred()) {
1164        return -1;
1165    }
1166
1167    Py_BUILD_ASSERT(sizeof(value) >= sizeof(PyTime_t));
1168    *result = (PyTime_t)value;
1169    return 0;
1170#else
1171    // Call time.perf_counter() and convert C double to PyTime_t.
1172    // Cache time.perf_counter() function for best performance.
1173    static PyObject *func = NULL;
1174    if (func == NULL) {
1175        PyObject *mod = PyImport_ImportModule("time");
1176        if (mod == NULL) {
1177            return -1;
1178        }
1179
1180        func = PyObject_GetAttrString(mod, "perf_counter");
1181        Py_DECREF(mod);
1182        if (func == NULL) {
1183            return -1;
1184        }
1185    }
1186
1187    PyObject *res = PyObject_CallNoArgs(func);
1188    if (res == NULL) {
1189        return -1;
1190    }
1191    double d = PyFloat_AsDouble(res);
1192    Py_DECREF(res);
1193
1194    if (d == -1.0 && PyErr_Occurred()) {
1195        return -1;
1196    }
1197
1198    // Avoid floor() to avoid having to link to libm
1199    *result = (PyTime_t)(d * 1e9);
1200    return 0;

Showing the first 1,200 of 1697 lines. Download the file for the rest.

Aluode/PerceptionLabPortable · CoolFace