17db96d56Sopenharmony_ci#ifndef Py_OBJECT_H 27db96d56Sopenharmony_ci#define Py_OBJECT_H 37db96d56Sopenharmony_ci#ifdef __cplusplus 47db96d56Sopenharmony_ciextern "C" { 57db96d56Sopenharmony_ci#endif 67db96d56Sopenharmony_ci 77db96d56Sopenharmony_ci 87db96d56Sopenharmony_ci/* Object and type object interface */ 97db96d56Sopenharmony_ci 107db96d56Sopenharmony_ci/* 117db96d56Sopenharmony_ciObjects are structures allocated on the heap. Special rules apply to 127db96d56Sopenharmony_cithe use of objects to ensure they are properly garbage-collected. 137db96d56Sopenharmony_ciObjects are never allocated statically or on the stack; they must be 147db96d56Sopenharmony_ciaccessed through special macros and functions only. (Type objects are 157db96d56Sopenharmony_ciexceptions to the first rule; the standard types are represented by 167db96d56Sopenharmony_cistatically initialized type objects, although work on type/class unification 177db96d56Sopenharmony_cifor Python 2.2 made it possible to have heap-allocated type objects too). 187db96d56Sopenharmony_ci 197db96d56Sopenharmony_ciAn object has a 'reference count' that is increased or decreased when a 207db96d56Sopenharmony_cipointer to the object is copied or deleted; when the reference count 217db96d56Sopenharmony_cireaches zero there are no references to the object left and it can be 227db96d56Sopenharmony_ciremoved from the heap. 237db96d56Sopenharmony_ci 247db96d56Sopenharmony_ciAn object has a 'type' that determines what it represents and what kind 257db96d56Sopenharmony_ciof data it contains. An object's type is fixed when it is created. 267db96d56Sopenharmony_ciTypes themselves are represented as objects; an object contains a 277db96d56Sopenharmony_cipointer to the corresponding type object. The type itself has a type 287db96d56Sopenharmony_cipointer pointing to the object representing the type 'type', which 297db96d56Sopenharmony_cicontains a pointer to itself!. 307db96d56Sopenharmony_ci 317db96d56Sopenharmony_ciObjects do not float around in memory; once allocated an object keeps 327db96d56Sopenharmony_cithe same size and address. Objects that must hold variable-size data 337db96d56Sopenharmony_cican contain pointers to variable-size parts of the object. Not all 347db96d56Sopenharmony_ciobjects of the same type have the same size; but the size cannot change 357db96d56Sopenharmony_ciafter allocation. (These restrictions are made so a reference to an 367db96d56Sopenharmony_ciobject can be simply a pointer -- moving an object would require 377db96d56Sopenharmony_ciupdating all the pointers, and changing an object's size would require 387db96d56Sopenharmony_cimoving it if there was another object right next to it.) 397db96d56Sopenharmony_ci 407db96d56Sopenharmony_ciObjects are always accessed through pointers of the type 'PyObject *'. 417db96d56Sopenharmony_ciThe type 'PyObject' is a structure that only contains the reference count 427db96d56Sopenharmony_ciand the type pointer. The actual memory allocated for an object 437db96d56Sopenharmony_cicontains other data that can only be accessed after casting the pointer 447db96d56Sopenharmony_cito a pointer to a longer structure type. This longer type must start 457db96d56Sopenharmony_ciwith the reference count and type fields; the macro PyObject_HEAD should be 467db96d56Sopenharmony_ciused for this (to accommodate for future changes). The implementation 477db96d56Sopenharmony_ciof a particular object type can cast the object pointer to the proper 487db96d56Sopenharmony_citype and back. 497db96d56Sopenharmony_ci 507db96d56Sopenharmony_ciA standard interface exists for objects that contain an array of items 517db96d56Sopenharmony_ciwhose size is determined when the object is allocated. 527db96d56Sopenharmony_ci*/ 537db96d56Sopenharmony_ci 547db96d56Sopenharmony_ci/* Py_DEBUG implies Py_REF_DEBUG. */ 557db96d56Sopenharmony_ci#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) 567db96d56Sopenharmony_ci# define Py_REF_DEBUG 577db96d56Sopenharmony_ci#endif 587db96d56Sopenharmony_ci 597db96d56Sopenharmony_ci#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS) 607db96d56Sopenharmony_ci# error Py_LIMITED_API is incompatible with Py_TRACE_REFS 617db96d56Sopenharmony_ci#endif 627db96d56Sopenharmony_ci 637db96d56Sopenharmony_ci#ifdef Py_TRACE_REFS 647db96d56Sopenharmony_ci/* Define pointers to support a doubly-linked list of all live heap objects. */ 657db96d56Sopenharmony_ci#define _PyObject_HEAD_EXTRA \ 667db96d56Sopenharmony_ci PyObject *_ob_next; \ 677db96d56Sopenharmony_ci PyObject *_ob_prev; 687db96d56Sopenharmony_ci 697db96d56Sopenharmony_ci#define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL, 707db96d56Sopenharmony_ci 717db96d56Sopenharmony_ci#else 727db96d56Sopenharmony_ci# define _PyObject_HEAD_EXTRA 737db96d56Sopenharmony_ci# define _PyObject_EXTRA_INIT 747db96d56Sopenharmony_ci#endif 757db96d56Sopenharmony_ci 767db96d56Sopenharmony_ci/* PyObject_HEAD defines the initial segment of every PyObject. */ 777db96d56Sopenharmony_ci#define PyObject_HEAD PyObject ob_base; 787db96d56Sopenharmony_ci 797db96d56Sopenharmony_ci#define PyObject_HEAD_INIT(type) \ 807db96d56Sopenharmony_ci { _PyObject_EXTRA_INIT \ 817db96d56Sopenharmony_ci 1, type }, 827db96d56Sopenharmony_ci 837db96d56Sopenharmony_ci#define PyVarObject_HEAD_INIT(type, size) \ 847db96d56Sopenharmony_ci { PyObject_HEAD_INIT(type) size }, 857db96d56Sopenharmony_ci 867db96d56Sopenharmony_ci/* PyObject_VAR_HEAD defines the initial segment of all variable-size 877db96d56Sopenharmony_ci * container objects. These end with a declaration of an array with 1 887db96d56Sopenharmony_ci * element, but enough space is malloc'ed so that the array actually 897db96d56Sopenharmony_ci * has room for ob_size elements. Note that ob_size is an element count, 907db96d56Sopenharmony_ci * not necessarily a byte count. 917db96d56Sopenharmony_ci */ 927db96d56Sopenharmony_ci#define PyObject_VAR_HEAD PyVarObject ob_base; 937db96d56Sopenharmony_ci#define Py_INVALID_SIZE (Py_ssize_t)-1 947db96d56Sopenharmony_ci 957db96d56Sopenharmony_ci/* Nothing is actually declared to be a PyObject, but every pointer to 967db96d56Sopenharmony_ci * a Python object can be cast to a PyObject*. This is inheritance built 977db96d56Sopenharmony_ci * by hand. Similarly every pointer to a variable-size Python object can, 987db96d56Sopenharmony_ci * in addition, be cast to PyVarObject*. 997db96d56Sopenharmony_ci */ 1007db96d56Sopenharmony_cistruct _object { 1017db96d56Sopenharmony_ci _PyObject_HEAD_EXTRA 1027db96d56Sopenharmony_ci Py_ssize_t ob_refcnt; 1037db96d56Sopenharmony_ci PyTypeObject *ob_type; 1047db96d56Sopenharmony_ci}; 1057db96d56Sopenharmony_ci 1067db96d56Sopenharmony_ci/* Cast argument to PyObject* type. */ 1077db96d56Sopenharmony_ci#define _PyObject_CAST(op) _Py_CAST(PyObject*, (op)) 1087db96d56Sopenharmony_ci 1097db96d56Sopenharmony_citypedef struct { 1107db96d56Sopenharmony_ci PyObject ob_base; 1117db96d56Sopenharmony_ci Py_ssize_t ob_size; /* Number of items in variable part */ 1127db96d56Sopenharmony_ci} PyVarObject; 1137db96d56Sopenharmony_ci 1147db96d56Sopenharmony_ci/* Cast argument to PyVarObject* type. */ 1157db96d56Sopenharmony_ci#define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op)) 1167db96d56Sopenharmony_ci 1177db96d56Sopenharmony_ci 1187db96d56Sopenharmony_ci// Test if the 'x' object is the 'y' object, the same as "x is y" in Python. 1197db96d56Sopenharmony_ciPyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y); 1207db96d56Sopenharmony_ci#define Py_Is(x, y) ((x) == (y)) 1217db96d56Sopenharmony_ci 1227db96d56Sopenharmony_ci 1237db96d56Sopenharmony_cistatic inline Py_ssize_t Py_REFCNT(PyObject *ob) { 1247db96d56Sopenharmony_ci return ob->ob_refcnt; 1257db96d56Sopenharmony_ci} 1267db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1277db96d56Sopenharmony_ci# define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob)) 1287db96d56Sopenharmony_ci#endif 1297db96d56Sopenharmony_ci 1307db96d56Sopenharmony_ci 1317db96d56Sopenharmony_ci// bpo-39573: The Py_SET_TYPE() function must be used to set an object type. 1327db96d56Sopenharmony_cistatic inline PyTypeObject* Py_TYPE(PyObject *ob) { 1337db96d56Sopenharmony_ci return ob->ob_type; 1347db96d56Sopenharmony_ci} 1357db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1367db96d56Sopenharmony_ci# define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob)) 1377db96d56Sopenharmony_ci#endif 1387db96d56Sopenharmony_ci 1397db96d56Sopenharmony_ci// bpo-39573: The Py_SET_SIZE() function must be used to set an object size. 1407db96d56Sopenharmony_cistatic inline Py_ssize_t Py_SIZE(PyObject *ob) { 1417db96d56Sopenharmony_ci PyVarObject *var_ob = _PyVarObject_CAST(ob); 1427db96d56Sopenharmony_ci return var_ob->ob_size; 1437db96d56Sopenharmony_ci} 1447db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1457db96d56Sopenharmony_ci# define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob)) 1467db96d56Sopenharmony_ci#endif 1477db96d56Sopenharmony_ci 1487db96d56Sopenharmony_ci 1497db96d56Sopenharmony_cistatic inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) { 1507db96d56Sopenharmony_ci return Py_TYPE(ob) == type; 1517db96d56Sopenharmony_ci} 1527db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1537db96d56Sopenharmony_ci# define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), type) 1547db96d56Sopenharmony_ci#endif 1557db96d56Sopenharmony_ci 1567db96d56Sopenharmony_ci 1577db96d56Sopenharmony_cistatic inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { 1587db96d56Sopenharmony_ci ob->ob_refcnt = refcnt; 1597db96d56Sopenharmony_ci} 1607db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1617db96d56Sopenharmony_ci# define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), refcnt) 1627db96d56Sopenharmony_ci#endif 1637db96d56Sopenharmony_ci 1647db96d56Sopenharmony_ci 1657db96d56Sopenharmony_cistatic inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { 1667db96d56Sopenharmony_ci ob->ob_type = type; 1677db96d56Sopenharmony_ci} 1687db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1697db96d56Sopenharmony_ci# define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type) 1707db96d56Sopenharmony_ci#endif 1717db96d56Sopenharmony_ci 1727db96d56Sopenharmony_ci 1737db96d56Sopenharmony_cistatic inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { 1747db96d56Sopenharmony_ci ob->ob_size = size; 1757db96d56Sopenharmony_ci} 1767db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 1777db96d56Sopenharmony_ci# define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), size) 1787db96d56Sopenharmony_ci#endif 1797db96d56Sopenharmony_ci 1807db96d56Sopenharmony_ci 1817db96d56Sopenharmony_ci/* 1827db96d56Sopenharmony_ciType objects contain a string containing the type name (to help somewhat 1837db96d56Sopenharmony_ciin debugging), the allocation parameters (see PyObject_New() and 1847db96d56Sopenharmony_ciPyObject_NewVar()), 1857db96d56Sopenharmony_ciand methods for accessing objects of the type. Methods are optional, a 1867db96d56Sopenharmony_cinil pointer meaning that particular kind of access is not available for 1877db96d56Sopenharmony_cithis type. The Py_DECREF() macro uses the tp_dealloc method without 1887db96d56Sopenharmony_cichecking for a nil pointer; it should always be implemented except if 1897db96d56Sopenharmony_cithe implementation can guarantee that the reference count will never 1907db96d56Sopenharmony_cireach zero (e.g., for statically allocated type objects). 1917db96d56Sopenharmony_ci 1927db96d56Sopenharmony_ciNB: the methods for certain type groups are now contained in separate 1937db96d56Sopenharmony_cimethod blocks. 1947db96d56Sopenharmony_ci*/ 1957db96d56Sopenharmony_ci 1967db96d56Sopenharmony_citypedef PyObject * (*unaryfunc)(PyObject *); 1977db96d56Sopenharmony_citypedef PyObject * (*binaryfunc)(PyObject *, PyObject *); 1987db96d56Sopenharmony_citypedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); 1997db96d56Sopenharmony_citypedef int (*inquiry)(PyObject *); 2007db96d56Sopenharmony_citypedef Py_ssize_t (*lenfunc)(PyObject *); 2017db96d56Sopenharmony_citypedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); 2027db96d56Sopenharmony_citypedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); 2037db96d56Sopenharmony_citypedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); 2047db96d56Sopenharmony_citypedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); 2057db96d56Sopenharmony_citypedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); 2067db96d56Sopenharmony_ci 2077db96d56Sopenharmony_citypedef int (*objobjproc)(PyObject *, PyObject *); 2087db96d56Sopenharmony_citypedef int (*visitproc)(PyObject *, void *); 2097db96d56Sopenharmony_citypedef int (*traverseproc)(PyObject *, visitproc, void *); 2107db96d56Sopenharmony_ci 2117db96d56Sopenharmony_ci 2127db96d56Sopenharmony_citypedef void (*freefunc)(void *); 2137db96d56Sopenharmony_citypedef void (*destructor)(PyObject *); 2147db96d56Sopenharmony_citypedef PyObject *(*getattrfunc)(PyObject *, char *); 2157db96d56Sopenharmony_citypedef PyObject *(*getattrofunc)(PyObject *, PyObject *); 2167db96d56Sopenharmony_citypedef int (*setattrfunc)(PyObject *, char *, PyObject *); 2177db96d56Sopenharmony_citypedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); 2187db96d56Sopenharmony_citypedef PyObject *(*reprfunc)(PyObject *); 2197db96d56Sopenharmony_citypedef Py_hash_t (*hashfunc)(PyObject *); 2207db96d56Sopenharmony_citypedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); 2217db96d56Sopenharmony_citypedef PyObject *(*getiterfunc) (PyObject *); 2227db96d56Sopenharmony_citypedef PyObject *(*iternextfunc) (PyObject *); 2237db96d56Sopenharmony_citypedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); 2247db96d56Sopenharmony_citypedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); 2257db96d56Sopenharmony_citypedef int (*initproc)(PyObject *, PyObject *, PyObject *); 2267db96d56Sopenharmony_citypedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *); 2277db96d56Sopenharmony_citypedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t); 2287db96d56Sopenharmony_ci 2297db96d56Sopenharmony_citypedef struct{ 2307db96d56Sopenharmony_ci int slot; /* slot id, see below */ 2317db96d56Sopenharmony_ci void *pfunc; /* function pointer */ 2327db96d56Sopenharmony_ci} PyType_Slot; 2337db96d56Sopenharmony_ci 2347db96d56Sopenharmony_citypedef struct{ 2357db96d56Sopenharmony_ci const char* name; 2367db96d56Sopenharmony_ci int basicsize; 2377db96d56Sopenharmony_ci int itemsize; 2387db96d56Sopenharmony_ci unsigned int flags; 2397db96d56Sopenharmony_ci PyType_Slot *slots; /* terminated by slot==0. */ 2407db96d56Sopenharmony_ci} PyType_Spec; 2417db96d56Sopenharmony_ci 2427db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*); 2437db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 2447db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); 2457db96d56Sopenharmony_ci#endif 2467db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 2477db96d56Sopenharmony_ciPyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int); 2487db96d56Sopenharmony_ci#endif 2497db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 2507db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *); 2517db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *); 2527db96d56Sopenharmony_ciPyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *); 2537db96d56Sopenharmony_ci#endif 2547db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000 2557db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *); 2567db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *); 2577db96d56Sopenharmony_ci#endif 2587db96d56Sopenharmony_ci 2597db96d56Sopenharmony_ci/* Generic type check */ 2607db96d56Sopenharmony_ciPyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); 2617db96d56Sopenharmony_ci 2627db96d56Sopenharmony_cistatic inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) { 2637db96d56Sopenharmony_ci return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type); 2647db96d56Sopenharmony_ci} 2657db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 2667db96d56Sopenharmony_ci# define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), type) 2677db96d56Sopenharmony_ci#endif 2687db96d56Sopenharmony_ci 2697db96d56Sopenharmony_ciPyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ 2707db96d56Sopenharmony_ciPyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ 2717db96d56Sopenharmony_ciPyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */ 2727db96d56Sopenharmony_ci 2737db96d56Sopenharmony_ciPyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*); 2747db96d56Sopenharmony_ci 2757db96d56Sopenharmony_ciPyAPI_FUNC(int) PyType_Ready(PyTypeObject *); 2767db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t); 2777db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *, 2787db96d56Sopenharmony_ci PyObject *, PyObject *); 2797db96d56Sopenharmony_ciPyAPI_FUNC(unsigned int) PyType_ClearCache(void); 2807db96d56Sopenharmony_ciPyAPI_FUNC(void) PyType_Modified(PyTypeObject *); 2817db96d56Sopenharmony_ci 2827db96d56Sopenharmony_ci/* Generic operations on objects */ 2837db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); 2847db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); 2857db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *); 2867db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *); 2877db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); 2887db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); 2897db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *); 2907db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); 2917db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); 2927db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); 2937db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); 2947db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); 2957db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); 2967db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); 2977db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *); 2987db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 2997db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *); 3007db96d56Sopenharmony_ci#endif 3017db96d56Sopenharmony_ciPyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *); 3027db96d56Sopenharmony_ciPyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *); 3037db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_IsTrue(PyObject *); 3047db96d56Sopenharmony_ciPyAPI_FUNC(int) PyObject_Not(PyObject *); 3057db96d56Sopenharmony_ciPyAPI_FUNC(int) PyCallable_Check(PyObject *); 3067db96d56Sopenharmony_ciPyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *); 3077db96d56Sopenharmony_ci 3087db96d56Sopenharmony_ci/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a 3097db96d56Sopenharmony_ci list of strings. PyObject_Dir(NULL) is like builtins.dir(), 3107db96d56Sopenharmony_ci returning the names of the current locals. In this case, if there are 3117db96d56Sopenharmony_ci no current locals, NULL is returned, and PyErr_Occurred() is false. 3127db96d56Sopenharmony_ci*/ 3137db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *); 3147db96d56Sopenharmony_ci 3157db96d56Sopenharmony_ci/* Pickle support. */ 3167db96d56Sopenharmony_ci#ifndef Py_LIMITED_API 3177db96d56Sopenharmony_ciPyAPI_FUNC(PyObject *) _PyObject_GetState(PyObject *); 3187db96d56Sopenharmony_ci#endif 3197db96d56Sopenharmony_ci 3207db96d56Sopenharmony_ci 3217db96d56Sopenharmony_ci/* Helpers for printing recursive container types */ 3227db96d56Sopenharmony_ciPyAPI_FUNC(int) Py_ReprEnter(PyObject *); 3237db96d56Sopenharmony_ciPyAPI_FUNC(void) Py_ReprLeave(PyObject *); 3247db96d56Sopenharmony_ci 3257db96d56Sopenharmony_ci/* Flag bits for printing: */ 3267db96d56Sopenharmony_ci#define Py_PRINT_RAW 1 /* No string quotes etc. */ 3277db96d56Sopenharmony_ci 3287db96d56Sopenharmony_ci/* 3297db96d56Sopenharmony_ciType flags (tp_flags) 3307db96d56Sopenharmony_ci 3317db96d56Sopenharmony_ciThese flags are used to change expected features and behavior for a 3327db96d56Sopenharmony_ciparticular type. 3337db96d56Sopenharmony_ci 3347db96d56Sopenharmony_ciArbitration of the flag bit positions will need to be coordinated among 3357db96d56Sopenharmony_ciall extension writers who publicly release their extensions (this will 3367db96d56Sopenharmony_cibe fewer than you might expect!). 3377db96d56Sopenharmony_ci 3387db96d56Sopenharmony_ciMost flags were removed as of Python 3.0 to make room for new flags. (Some 3397db96d56Sopenharmony_ciflags are not for backwards compatibility but to indicate the presence of an 3407db96d56Sopenharmony_cioptional feature; these flags remain of course.) 3417db96d56Sopenharmony_ci 3427db96d56Sopenharmony_ciType definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. 3437db96d56Sopenharmony_ci 3447db96d56Sopenharmony_ciCode can use PyType_HasFeature(type_ob, flag_value) to test whether the 3457db96d56Sopenharmony_cigiven type object has a specified feature. 3467db96d56Sopenharmony_ci*/ 3477db96d56Sopenharmony_ci 3487db96d56Sopenharmony_ci#ifndef Py_LIMITED_API 3497db96d56Sopenharmony_ci 3507db96d56Sopenharmony_ci/* Placement of dict (and values) pointers are managed by the VM, not by the type. 3517db96d56Sopenharmony_ci * The VM will automatically set tp_dictoffset. Should not be used for variable sized 3527db96d56Sopenharmony_ci * classes, such as classes that extend tuple. 3537db96d56Sopenharmony_ci */ 3547db96d56Sopenharmony_ci#define Py_TPFLAGS_MANAGED_DICT (1 << 4) 3557db96d56Sopenharmony_ci 3567db96d56Sopenharmony_ci/* Set if instances of the type object are treated as sequences for pattern matching */ 3577db96d56Sopenharmony_ci#define Py_TPFLAGS_SEQUENCE (1 << 5) 3587db96d56Sopenharmony_ci/* Set if instances of the type object are treated as mappings for pattern matching */ 3597db96d56Sopenharmony_ci#define Py_TPFLAGS_MAPPING (1 << 6) 3607db96d56Sopenharmony_ci#endif 3617db96d56Sopenharmony_ci 3627db96d56Sopenharmony_ci/* Disallow creating instances of the type: set tp_new to NULL and don't create 3637db96d56Sopenharmony_ci * the "__new__" key in the type dictionary. */ 3647db96d56Sopenharmony_ci#define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7) 3657db96d56Sopenharmony_ci 3667db96d56Sopenharmony_ci/* Set if the type object is immutable: type attributes cannot be set nor deleted */ 3677db96d56Sopenharmony_ci#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8) 3687db96d56Sopenharmony_ci 3697db96d56Sopenharmony_ci/* Set if the type object is dynamically allocated */ 3707db96d56Sopenharmony_ci#define Py_TPFLAGS_HEAPTYPE (1UL << 9) 3717db96d56Sopenharmony_ci 3727db96d56Sopenharmony_ci/* Set if the type allows subclassing */ 3737db96d56Sopenharmony_ci#define Py_TPFLAGS_BASETYPE (1UL << 10) 3747db96d56Sopenharmony_ci 3757db96d56Sopenharmony_ci/* Set if the type implements the vectorcall protocol (PEP 590) */ 3767db96d56Sopenharmony_ci#ifndef Py_LIMITED_API 3777db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11) 3787db96d56Sopenharmony_ci// Backwards compatibility alias for API that was provisional in Python 3.8 3797db96d56Sopenharmony_ci#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL 3807db96d56Sopenharmony_ci#endif 3817db96d56Sopenharmony_ci 3827db96d56Sopenharmony_ci/* Set if the type is 'ready' -- fully initialized */ 3837db96d56Sopenharmony_ci#define Py_TPFLAGS_READY (1UL << 12) 3847db96d56Sopenharmony_ci 3857db96d56Sopenharmony_ci/* Set while the type is being 'readied', to prevent recursive ready calls */ 3867db96d56Sopenharmony_ci#define Py_TPFLAGS_READYING (1UL << 13) 3877db96d56Sopenharmony_ci 3887db96d56Sopenharmony_ci/* Objects support garbage collection (see objimpl.h) */ 3897db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_GC (1UL << 14) 3907db96d56Sopenharmony_ci 3917db96d56Sopenharmony_ci/* These two bits are preserved for Stackless Python, next after this is 17 */ 3927db96d56Sopenharmony_ci#ifdef STACKLESS 3937db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15) 3947db96d56Sopenharmony_ci#else 3957db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0 3967db96d56Sopenharmony_ci#endif 3977db96d56Sopenharmony_ci 3987db96d56Sopenharmony_ci/* Objects behave like an unbound method */ 3997db96d56Sopenharmony_ci#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17) 4007db96d56Sopenharmony_ci 4017db96d56Sopenharmony_ci/* Object has up-to-date type attribute cache */ 4027db96d56Sopenharmony_ci#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19) 4037db96d56Sopenharmony_ci 4047db96d56Sopenharmony_ci/* Type is abstract and cannot be instantiated */ 4057db96d56Sopenharmony_ci#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20) 4067db96d56Sopenharmony_ci 4077db96d56Sopenharmony_ci// This undocumented flag gives certain built-ins their unique pattern-matching 4087db96d56Sopenharmony_ci// behavior, which allows a single positional subpattern to match against the 4097db96d56Sopenharmony_ci// subject itself (rather than a mapped attribute on it): 4107db96d56Sopenharmony_ci#define _Py_TPFLAGS_MATCH_SELF (1UL << 22) 4117db96d56Sopenharmony_ci 4127db96d56Sopenharmony_ci/* These flags are used to determine if a type is a subclass. */ 4137db96d56Sopenharmony_ci#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24) 4147db96d56Sopenharmony_ci#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25) 4157db96d56Sopenharmony_ci#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26) 4167db96d56Sopenharmony_ci#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27) 4177db96d56Sopenharmony_ci#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28) 4187db96d56Sopenharmony_ci#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29) 4197db96d56Sopenharmony_ci#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30) 4207db96d56Sopenharmony_ci#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31) 4217db96d56Sopenharmony_ci 4227db96d56Sopenharmony_ci#define Py_TPFLAGS_DEFAULT ( \ 4237db96d56Sopenharmony_ci Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \ 4247db96d56Sopenharmony_ci 0) 4257db96d56Sopenharmony_ci 4267db96d56Sopenharmony_ci/* NOTE: Some of the following flags reuse lower bits (removed as part of the 4277db96d56Sopenharmony_ci * Python 3.0 transition). */ 4287db96d56Sopenharmony_ci 4297db96d56Sopenharmony_ci/* The following flags are kept for compatibility; in previous 4307db96d56Sopenharmony_ci * versions they indicated presence of newer tp_* fields on the 4317db96d56Sopenharmony_ci * type struct. 4327db96d56Sopenharmony_ci * Starting with 3.8, binary compatibility of C extensions across 4337db96d56Sopenharmony_ci * feature releases of Python is not supported anymore (except when 4347db96d56Sopenharmony_ci * using the stable ABI, in which all classes are created dynamically, 4357db96d56Sopenharmony_ci * using the interpreter's memory layout.) 4367db96d56Sopenharmony_ci * Note that older extensions using the stable ABI set these flags, 4377db96d56Sopenharmony_ci * so the bits must not be repurposed. 4387db96d56Sopenharmony_ci */ 4397db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0) 4407db96d56Sopenharmony_ci#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18) 4417db96d56Sopenharmony_ci 4427db96d56Sopenharmony_ci 4437db96d56Sopenharmony_ci/* 4447db96d56Sopenharmony_ciThe macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement 4457db96d56Sopenharmony_cireference counts. Py_DECREF calls the object's deallocator function when 4467db96d56Sopenharmony_cithe refcount falls to 0; for 4477db96d56Sopenharmony_ciobjects that don't contain references to other objects or heap memory 4487db96d56Sopenharmony_cithis can be the standard function free(). Both macros can be used 4497db96d56Sopenharmony_ciwherever a void expression is allowed. The argument must not be a 4507db96d56Sopenharmony_ciNULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead. 4517db96d56Sopenharmony_ciThe macro _Py_NewReference(op) initialize reference counts to 1, and 4527db96d56Sopenharmony_ciin special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional 4537db96d56Sopenharmony_cibookkeeping appropriate to the special build. 4547db96d56Sopenharmony_ci 4557db96d56Sopenharmony_ciWe assume that the reference count field can never overflow; this can 4567db96d56Sopenharmony_cibe proven when the size of the field is the same as the pointer size, so 4577db96d56Sopenharmony_ciwe ignore the possibility. Provided a C int is at least 32 bits (which 4587db96d56Sopenharmony_ciis implicitly assumed in many parts of this code), that's enough for 4597db96d56Sopenharmony_ciabout 2**31 references to an object. 4607db96d56Sopenharmony_ci 4617db96d56Sopenharmony_ciXXX The following became out of date in Python 2.2, but I'm not sure 4627db96d56Sopenharmony_ciXXX what the full truth is now. Certainly, heap-allocated type objects 4637db96d56Sopenharmony_ciXXX can and should be deallocated. 4647db96d56Sopenharmony_ciType objects should never be deallocated; the type pointer in an object 4657db96d56Sopenharmony_ciis not considered to be a reference to the type object, to save 4667db96d56Sopenharmony_cicomplications in the deallocation function. (This is actually a 4677db96d56Sopenharmony_cidecision that's up to the implementer of each new type so if you want, 4687db96d56Sopenharmony_ciyou can count such references to the type object.) 4697db96d56Sopenharmony_ci*/ 4707db96d56Sopenharmony_ci 4717db96d56Sopenharmony_ci#ifdef Py_REF_DEBUG 4727db96d56Sopenharmony_ciPyAPI_DATA(Py_ssize_t) _Py_RefTotal; 4737db96d56Sopenharmony_ciPyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno, 4747db96d56Sopenharmony_ci PyObject *op); 4757db96d56Sopenharmony_ci#endif /* Py_REF_DEBUG */ 4767db96d56Sopenharmony_ci 4777db96d56Sopenharmony_ciPyAPI_FUNC(void) _Py_Dealloc(PyObject *); 4787db96d56Sopenharmony_ci 4797db96d56Sopenharmony_ci/* 4807db96d56Sopenharmony_ciThese are provided as conveniences to Python runtime embedders, so that 4817db96d56Sopenharmony_cithey can have object code that is not dependent on Python compilation flags. 4827db96d56Sopenharmony_ci*/ 4837db96d56Sopenharmony_ciPyAPI_FUNC(void) Py_IncRef(PyObject *); 4847db96d56Sopenharmony_ciPyAPI_FUNC(void) Py_DecRef(PyObject *); 4857db96d56Sopenharmony_ci 4867db96d56Sopenharmony_ci// Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL. 4877db96d56Sopenharmony_ci// Private functions used by Py_INCREF() and Py_DECREF(). 4887db96d56Sopenharmony_ciPyAPI_FUNC(void) _Py_IncRef(PyObject *); 4897db96d56Sopenharmony_ciPyAPI_FUNC(void) _Py_DecRef(PyObject *); 4907db96d56Sopenharmony_ci 4917db96d56Sopenharmony_cistatic inline void Py_INCREF(PyObject *op) 4927db96d56Sopenharmony_ci{ 4937db96d56Sopenharmony_ci#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000 4947db96d56Sopenharmony_ci // Stable ABI for Python 3.10 built in debug mode. 4957db96d56Sopenharmony_ci _Py_IncRef(op); 4967db96d56Sopenharmony_ci#else 4977db96d56Sopenharmony_ci // Non-limited C API and limited C API for Python 3.9 and older access 4987db96d56Sopenharmony_ci // directly PyObject.ob_refcnt. 4997db96d56Sopenharmony_ci#ifdef Py_REF_DEBUG 5007db96d56Sopenharmony_ci _Py_RefTotal++; 5017db96d56Sopenharmony_ci#endif 5027db96d56Sopenharmony_ci op->ob_refcnt++; 5037db96d56Sopenharmony_ci#endif 5047db96d56Sopenharmony_ci} 5057db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 5067db96d56Sopenharmony_ci# define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op)) 5077db96d56Sopenharmony_ci#endif 5087db96d56Sopenharmony_ci 5097db96d56Sopenharmony_ci 5107db96d56Sopenharmony_ci#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000 5117db96d56Sopenharmony_ci// Stable ABI for limited C API version 3.10 of Python debug build 5127db96d56Sopenharmony_cistatic inline void Py_DECREF(PyObject *op) { 5137db96d56Sopenharmony_ci _Py_DecRef(op); 5147db96d56Sopenharmony_ci} 5157db96d56Sopenharmony_ci#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) 5167db96d56Sopenharmony_ci 5177db96d56Sopenharmony_ci#elif defined(Py_REF_DEBUG) 5187db96d56Sopenharmony_cistatic inline void Py_DECREF(const char *filename, int lineno, PyObject *op) 5197db96d56Sopenharmony_ci{ 5207db96d56Sopenharmony_ci _Py_RefTotal--; 5217db96d56Sopenharmony_ci if (--op->ob_refcnt != 0) { 5227db96d56Sopenharmony_ci if (op->ob_refcnt < 0) { 5237db96d56Sopenharmony_ci _Py_NegativeRefcount(filename, lineno, op); 5247db96d56Sopenharmony_ci } 5257db96d56Sopenharmony_ci } 5267db96d56Sopenharmony_ci else { 5277db96d56Sopenharmony_ci _Py_Dealloc(op); 5287db96d56Sopenharmony_ci } 5297db96d56Sopenharmony_ci} 5307db96d56Sopenharmony_ci#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op)) 5317db96d56Sopenharmony_ci 5327db96d56Sopenharmony_ci#else 5337db96d56Sopenharmony_cistatic inline void Py_DECREF(PyObject *op) 5347db96d56Sopenharmony_ci{ 5357db96d56Sopenharmony_ci // Non-limited C API and limited C API for Python 3.9 and older access 5367db96d56Sopenharmony_ci // directly PyObject.ob_refcnt. 5377db96d56Sopenharmony_ci if (--op->ob_refcnt == 0) { 5387db96d56Sopenharmony_ci _Py_Dealloc(op); 5397db96d56Sopenharmony_ci } 5407db96d56Sopenharmony_ci} 5417db96d56Sopenharmony_ci#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op)) 5427db96d56Sopenharmony_ci#endif 5437db96d56Sopenharmony_ci 5447db96d56Sopenharmony_ci 5457db96d56Sopenharmony_ci/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear 5467db96d56Sopenharmony_ci * and tp_dealloc implementations. 5477db96d56Sopenharmony_ci * 5487db96d56Sopenharmony_ci * Note that "the obvious" code can be deadly: 5497db96d56Sopenharmony_ci * 5507db96d56Sopenharmony_ci * Py_XDECREF(op); 5517db96d56Sopenharmony_ci * op = NULL; 5527db96d56Sopenharmony_ci * 5537db96d56Sopenharmony_ci * Typically, `op` is something like self->containee, and `self` is done 5547db96d56Sopenharmony_ci * using its `containee` member. In the code sequence above, suppose 5557db96d56Sopenharmony_ci * `containee` is non-NULL with a refcount of 1. Its refcount falls to 5567db96d56Sopenharmony_ci * 0 on the first line, which can trigger an arbitrary amount of code, 5577db96d56Sopenharmony_ci * possibly including finalizers (like __del__ methods or weakref callbacks) 5587db96d56Sopenharmony_ci * coded in Python, which in turn can release the GIL and allow other threads 5597db96d56Sopenharmony_ci * to run, etc. Such code may even invoke methods of `self` again, or cause 5607db96d56Sopenharmony_ci * cyclic gc to trigger, but-- oops! --self->containee still points to the 5617db96d56Sopenharmony_ci * object being torn down, and it may be in an insane state while being torn 5627db96d56Sopenharmony_ci * down. This has in fact been a rich historic source of miserable (rare & 5637db96d56Sopenharmony_ci * hard-to-diagnose) segfaulting (and other) bugs. 5647db96d56Sopenharmony_ci * 5657db96d56Sopenharmony_ci * The safe way is: 5667db96d56Sopenharmony_ci * 5677db96d56Sopenharmony_ci * Py_CLEAR(op); 5687db96d56Sopenharmony_ci * 5697db96d56Sopenharmony_ci * That arranges to set `op` to NULL _before_ decref'ing, so that any code 5707db96d56Sopenharmony_ci * triggered as a side-effect of `op` getting torn down no longer believes 5717db96d56Sopenharmony_ci * `op` points to a valid object. 5727db96d56Sopenharmony_ci * 5737db96d56Sopenharmony_ci * There are cases where it's safe to use the naive code, but they're brittle. 5747db96d56Sopenharmony_ci * For example, if `op` points to a Python integer, you know that destroying 5757db96d56Sopenharmony_ci * one of those can't cause problems -- but in part that relies on that 5767db96d56Sopenharmony_ci * Python integers aren't currently weakly referencable. Best practice is 5777db96d56Sopenharmony_ci * to use Py_CLEAR() even if you can't think of a reason for why you need to. 5787db96d56Sopenharmony_ci */ 5797db96d56Sopenharmony_ci#define Py_CLEAR(op) \ 5807db96d56Sopenharmony_ci do { \ 5817db96d56Sopenharmony_ci PyObject *_py_tmp = _PyObject_CAST(op); \ 5827db96d56Sopenharmony_ci if (_py_tmp != NULL) { \ 5837db96d56Sopenharmony_ci (op) = NULL; \ 5847db96d56Sopenharmony_ci Py_DECREF(_py_tmp); \ 5857db96d56Sopenharmony_ci } \ 5867db96d56Sopenharmony_ci } while (0) 5877db96d56Sopenharmony_ci 5887db96d56Sopenharmony_ci/* Function to use in case the object pointer can be NULL: */ 5897db96d56Sopenharmony_cistatic inline void Py_XINCREF(PyObject *op) 5907db96d56Sopenharmony_ci{ 5917db96d56Sopenharmony_ci if (op != _Py_NULL) { 5927db96d56Sopenharmony_ci Py_INCREF(op); 5937db96d56Sopenharmony_ci } 5947db96d56Sopenharmony_ci} 5957db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 5967db96d56Sopenharmony_ci# define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op)) 5977db96d56Sopenharmony_ci#endif 5987db96d56Sopenharmony_ci 5997db96d56Sopenharmony_cistatic inline void Py_XDECREF(PyObject *op) 6007db96d56Sopenharmony_ci{ 6017db96d56Sopenharmony_ci if (op != _Py_NULL) { 6027db96d56Sopenharmony_ci Py_DECREF(op); 6037db96d56Sopenharmony_ci } 6047db96d56Sopenharmony_ci} 6057db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 6067db96d56Sopenharmony_ci# define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op)) 6077db96d56Sopenharmony_ci#endif 6087db96d56Sopenharmony_ci 6097db96d56Sopenharmony_ci// Create a new strong reference to an object: 6107db96d56Sopenharmony_ci// increment the reference count of the object and return the object. 6117db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj); 6127db96d56Sopenharmony_ci 6137db96d56Sopenharmony_ci// Similar to Py_NewRef(), but the object can be NULL. 6147db96d56Sopenharmony_ciPyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj); 6157db96d56Sopenharmony_ci 6167db96d56Sopenharmony_cistatic inline PyObject* _Py_NewRef(PyObject *obj) 6177db96d56Sopenharmony_ci{ 6187db96d56Sopenharmony_ci Py_INCREF(obj); 6197db96d56Sopenharmony_ci return obj; 6207db96d56Sopenharmony_ci} 6217db96d56Sopenharmony_ci 6227db96d56Sopenharmony_cistatic inline PyObject* _Py_XNewRef(PyObject *obj) 6237db96d56Sopenharmony_ci{ 6247db96d56Sopenharmony_ci Py_XINCREF(obj); 6257db96d56Sopenharmony_ci return obj; 6267db96d56Sopenharmony_ci} 6277db96d56Sopenharmony_ci 6287db96d56Sopenharmony_ci// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI. 6297db96d56Sopenharmony_ci// Names overridden with macros by static inline functions for best 6307db96d56Sopenharmony_ci// performances. 6317db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 6327db96d56Sopenharmony_ci# define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj)) 6337db96d56Sopenharmony_ci# define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj)) 6347db96d56Sopenharmony_ci#else 6357db96d56Sopenharmony_ci# define Py_NewRef(obj) _Py_NewRef(obj) 6367db96d56Sopenharmony_ci# define Py_XNewRef(obj) _Py_XNewRef(obj) 6377db96d56Sopenharmony_ci#endif 6387db96d56Sopenharmony_ci 6397db96d56Sopenharmony_ci 6407db96d56Sopenharmony_ci/* 6417db96d56Sopenharmony_ci_Py_NoneStruct is an object of undefined type which can be used in contexts 6427db96d56Sopenharmony_ciwhere NULL (nil) is not suitable (since NULL often means 'error'). 6437db96d56Sopenharmony_ci 6447db96d56Sopenharmony_ciDon't forget to apply Py_INCREF() when returning this value!!! 6457db96d56Sopenharmony_ci*/ 6467db96d56Sopenharmony_ciPyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ 6477db96d56Sopenharmony_ci#define Py_None (&_Py_NoneStruct) 6487db96d56Sopenharmony_ci 6497db96d56Sopenharmony_ci// Test if an object is the None singleton, the same as "x is None" in Python. 6507db96d56Sopenharmony_ciPyAPI_FUNC(int) Py_IsNone(PyObject *x); 6517db96d56Sopenharmony_ci#define Py_IsNone(x) Py_Is((x), Py_None) 6527db96d56Sopenharmony_ci 6537db96d56Sopenharmony_ci/* Macro for returning Py_None from a function */ 6547db96d56Sopenharmony_ci#define Py_RETURN_NONE return Py_NewRef(Py_None) 6557db96d56Sopenharmony_ci 6567db96d56Sopenharmony_ci/* 6577db96d56Sopenharmony_ciPy_NotImplemented is a singleton used to signal that an operation is 6587db96d56Sopenharmony_cinot implemented for a given type combination. 6597db96d56Sopenharmony_ci*/ 6607db96d56Sopenharmony_ciPyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ 6617db96d56Sopenharmony_ci#define Py_NotImplemented (&_Py_NotImplementedStruct) 6627db96d56Sopenharmony_ci 6637db96d56Sopenharmony_ci/* Macro for returning Py_NotImplemented from a function */ 6647db96d56Sopenharmony_ci#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented) 6657db96d56Sopenharmony_ci 6667db96d56Sopenharmony_ci/* Rich comparison opcodes */ 6677db96d56Sopenharmony_ci#define Py_LT 0 6687db96d56Sopenharmony_ci#define Py_LE 1 6697db96d56Sopenharmony_ci#define Py_EQ 2 6707db96d56Sopenharmony_ci#define Py_NE 3 6717db96d56Sopenharmony_ci#define Py_GT 4 6727db96d56Sopenharmony_ci#define Py_GE 5 6737db96d56Sopenharmony_ci 6747db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000 6757db96d56Sopenharmony_ci/* Result of calling PyIter_Send */ 6767db96d56Sopenharmony_citypedef enum { 6777db96d56Sopenharmony_ci PYGEN_RETURN = 0, 6787db96d56Sopenharmony_ci PYGEN_ERROR = -1, 6797db96d56Sopenharmony_ci PYGEN_NEXT = 1, 6807db96d56Sopenharmony_ci} PySendResult; 6817db96d56Sopenharmony_ci#endif 6827db96d56Sopenharmony_ci 6837db96d56Sopenharmony_ci/* 6847db96d56Sopenharmony_ci * Macro for implementing rich comparisons 6857db96d56Sopenharmony_ci * 6867db96d56Sopenharmony_ci * Needs to be a macro because any C-comparable type can be used. 6877db96d56Sopenharmony_ci */ 6887db96d56Sopenharmony_ci#define Py_RETURN_RICHCOMPARE(val1, val2, op) \ 6897db96d56Sopenharmony_ci do { \ 6907db96d56Sopenharmony_ci switch (op) { \ 6917db96d56Sopenharmony_ci case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6927db96d56Sopenharmony_ci case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6937db96d56Sopenharmony_ci case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6947db96d56Sopenharmony_ci case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6957db96d56Sopenharmony_ci case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6967db96d56Sopenharmony_ci case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 6977db96d56Sopenharmony_ci default: \ 6987db96d56Sopenharmony_ci Py_UNREACHABLE(); \ 6997db96d56Sopenharmony_ci } \ 7007db96d56Sopenharmony_ci } while (0) 7017db96d56Sopenharmony_ci 7027db96d56Sopenharmony_ci 7037db96d56Sopenharmony_ci/* 7047db96d56Sopenharmony_ciMore conventions 7057db96d56Sopenharmony_ci================ 7067db96d56Sopenharmony_ci 7077db96d56Sopenharmony_ciArgument Checking 7087db96d56Sopenharmony_ci----------------- 7097db96d56Sopenharmony_ci 7107db96d56Sopenharmony_ciFunctions that take objects as arguments normally don't check for nil 7117db96d56Sopenharmony_ciarguments, but they do check the type of the argument, and return an 7127db96d56Sopenharmony_cierror if the function doesn't apply to the type. 7137db96d56Sopenharmony_ci 7147db96d56Sopenharmony_ciFailure Modes 7157db96d56Sopenharmony_ci------------- 7167db96d56Sopenharmony_ci 7177db96d56Sopenharmony_ciFunctions may fail for a variety of reasons, including running out of 7187db96d56Sopenharmony_cimemory. This is communicated to the caller in two ways: an error string 7197db96d56Sopenharmony_ciis set (see errors.h), and the function result differs: functions that 7207db96d56Sopenharmony_cinormally return a pointer return NULL for failure, functions returning 7217db96d56Sopenharmony_cian integer return -1 (which could be a legal return value too!), and 7227db96d56Sopenharmony_ciother functions return 0 for success and -1 for failure. 7237db96d56Sopenharmony_ciCallers should always check for errors before using the result. If 7247db96d56Sopenharmony_cian error was set, the caller must either explicitly clear it, or pass 7257db96d56Sopenharmony_cithe error on to its caller. 7267db96d56Sopenharmony_ci 7277db96d56Sopenharmony_ciReference Counts 7287db96d56Sopenharmony_ci---------------- 7297db96d56Sopenharmony_ci 7307db96d56Sopenharmony_ciIt takes a while to get used to the proper usage of reference counts. 7317db96d56Sopenharmony_ci 7327db96d56Sopenharmony_ciFunctions that create an object set the reference count to 1; such new 7337db96d56Sopenharmony_ciobjects must be stored somewhere or destroyed again with Py_DECREF(). 7347db96d56Sopenharmony_ciSome functions that 'store' objects, such as PyTuple_SetItem() and 7357db96d56Sopenharmony_ciPyList_SetItem(), 7367db96d56Sopenharmony_cidon't increment the reference count of the object, since the most 7377db96d56Sopenharmony_cifrequent use is to store a fresh object. Functions that 'retrieve' 7387db96d56Sopenharmony_ciobjects, such as PyTuple_GetItem() and PyDict_GetItemString(), also 7397db96d56Sopenharmony_cidon't increment 7407db96d56Sopenharmony_cithe reference count, since most frequently the object is only looked at 7417db96d56Sopenharmony_ciquickly. Thus, to retrieve an object and store it again, the caller 7427db96d56Sopenharmony_cimust call Py_INCREF() explicitly. 7437db96d56Sopenharmony_ci 7447db96d56Sopenharmony_ciNOTE: functions that 'consume' a reference count, like 7457db96d56Sopenharmony_ciPyList_SetItem(), consume the reference even if the object wasn't 7467db96d56Sopenharmony_cisuccessfully stored, to simplify error handling. 7477db96d56Sopenharmony_ci 7487db96d56Sopenharmony_ciIt seems attractive to make other functions that take an object as 7497db96d56Sopenharmony_ciargument consume a reference count; however, this may quickly get 7507db96d56Sopenharmony_ciconfusing (even the current practice is already confusing). Consider 7517db96d56Sopenharmony_ciit carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at 7527db96d56Sopenharmony_citimes. 7537db96d56Sopenharmony_ci*/ 7547db96d56Sopenharmony_ci 7557db96d56Sopenharmony_ci#ifndef Py_LIMITED_API 7567db96d56Sopenharmony_ci# define Py_CPYTHON_OBJECT_H 7577db96d56Sopenharmony_ci# include "cpython/object.h" 7587db96d56Sopenharmony_ci# undef Py_CPYTHON_OBJECT_H 7597db96d56Sopenharmony_ci#endif 7607db96d56Sopenharmony_ci 7617db96d56Sopenharmony_ci 7627db96d56Sopenharmony_cistatic inline int 7637db96d56Sopenharmony_ciPyType_HasFeature(PyTypeObject *type, unsigned long feature) 7647db96d56Sopenharmony_ci{ 7657db96d56Sopenharmony_ci unsigned long flags; 7667db96d56Sopenharmony_ci#ifdef Py_LIMITED_API 7677db96d56Sopenharmony_ci // PyTypeObject is opaque in the limited C API 7687db96d56Sopenharmony_ci flags = PyType_GetFlags(type); 7697db96d56Sopenharmony_ci#else 7707db96d56Sopenharmony_ci flags = type->tp_flags; 7717db96d56Sopenharmony_ci#endif 7727db96d56Sopenharmony_ci return ((flags & feature) != 0); 7737db96d56Sopenharmony_ci} 7747db96d56Sopenharmony_ci 7757db96d56Sopenharmony_ci#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag) 7767db96d56Sopenharmony_ci 7777db96d56Sopenharmony_cistatic inline int PyType_Check(PyObject *op) { 7787db96d56Sopenharmony_ci return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS); 7797db96d56Sopenharmony_ci} 7807db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 7817db96d56Sopenharmony_ci# define PyType_Check(op) PyType_Check(_PyObject_CAST(op)) 7827db96d56Sopenharmony_ci#endif 7837db96d56Sopenharmony_ci 7847db96d56Sopenharmony_ci#define _PyType_CAST(op) \ 7857db96d56Sopenharmony_ci (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op))) 7867db96d56Sopenharmony_ci 7877db96d56Sopenharmony_cistatic inline int PyType_CheckExact(PyObject *op) { 7887db96d56Sopenharmony_ci return Py_IS_TYPE(op, &PyType_Type); 7897db96d56Sopenharmony_ci} 7907db96d56Sopenharmony_ci#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000 7917db96d56Sopenharmony_ci# define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op)) 7927db96d56Sopenharmony_ci#endif 7937db96d56Sopenharmony_ci 7947db96d56Sopenharmony_ci#ifdef __cplusplus 7957db96d56Sopenharmony_ci} 7967db96d56Sopenharmony_ci#endif 7977db96d56Sopenharmony_ci#endif // !Py_OBJECT_H 798