Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 10:06:49

0001 #ifndef Py_OBJECT_H
0002 #define Py_OBJECT_H
0003 #ifdef __cplusplus
0004 extern "C" {
0005 #endif
0006 
0007 
0008 /* Object and type object interface */
0009 
0010 /*
0011 Objects are structures allocated on the heap.  Special rules apply to
0012 the use of objects to ensure they are properly garbage-collected.
0013 Objects are never allocated statically or on the stack; they must be
0014 accessed through special macros and functions only.  (Type objects are
0015 exceptions to the first rule; the standard types are represented by
0016 statically initialized type objects, although work on type/class unification
0017 for Python 2.2 made it possible to have heap-allocated type objects too).
0018 
0019 An object has a 'reference count' that is increased or decreased when a
0020 pointer to the object is copied or deleted; when the reference count
0021 reaches zero there are no references to the object left and it can be
0022 removed from the heap.
0023 
0024 An object has a 'type' that determines what it represents and what kind
0025 of data it contains.  An object's type is fixed when it is created.
0026 Types themselves are represented as objects; an object contains a
0027 pointer to the corresponding type object.  The type itself has a type
0028 pointer pointing to the object representing the type 'type', which
0029 contains a pointer to itself!.
0030 
0031 Objects do not float around in memory; once allocated an object keeps
0032 the same size and address.  Objects that must hold variable-size data
0033 can contain pointers to variable-size parts of the object.  Not all
0034 objects of the same type have the same size; but the size cannot change
0035 after allocation.  (These restrictions are made so a reference to an
0036 object can be simply a pointer -- moving an object would require
0037 updating all the pointers, and changing an object's size would require
0038 moving it if there was another object right next to it.)
0039 
0040 Objects are always accessed through pointers of the type 'PyObject *'.
0041 The type 'PyObject' is a structure that only contains the reference count
0042 and the type pointer.  The actual memory allocated for an object
0043 contains other data that can only be accessed after casting the pointer
0044 to a pointer to a longer structure type.  This longer type must start
0045 with the reference count and type fields; the macro PyObject_HEAD should be
0046 used for this (to accommodate for future changes).  The implementation
0047 of a particular object type can cast the object pointer to the proper
0048 type and back.
0049 
0050 A standard interface exists for objects that contain an array of items
0051 whose size is determined when the object is allocated.
0052 */
0053 
0054 #include "pystats.h"
0055 
0056 /* Py_DEBUG implies Py_REF_DEBUG. */
0057 #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
0058 #  define Py_REF_DEBUG
0059 #endif
0060 
0061 #if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
0062 #  error Py_LIMITED_API is incompatible with Py_TRACE_REFS
0063 #endif
0064 
0065 #ifdef Py_TRACE_REFS
0066 /* Define pointers to support a doubly-linked list of all live heap objects. */
0067 #define _PyObject_HEAD_EXTRA            \
0068     PyObject *_ob_next;           \
0069     PyObject *_ob_prev;
0070 
0071 #define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL,
0072 
0073 #else
0074 #  define _PyObject_HEAD_EXTRA
0075 #  define _PyObject_EXTRA_INIT
0076 #endif
0077 
0078 /* PyObject_HEAD defines the initial segment of every PyObject. */
0079 #define PyObject_HEAD                   PyObject ob_base;
0080 
0081 /*
0082 Immortalization:
0083 
0084 The following indicates the immortalization strategy depending on the amount
0085 of available bits in the reference count field. All strategies are backwards
0086 compatible but the specific reference count value or immortalization check
0087 might change depending on the specializations for the underlying system.
0088 
0089 Proper deallocation of immortal instances requires distinguishing between
0090 statically allocated immortal instances vs those promoted by the runtime to be
0091 immortal. The latter should be the only instances that require
0092 cleanup during runtime finalization.
0093 */
0094 
0095 #if SIZEOF_VOID_P > 4
0096 /*
0097 In 64+ bit systems, an object will be marked as immortal by setting all of the
0098 lower 32 bits of the reference count field, which is equal to: 0xFFFFFFFF
0099 
0100 Using the lower 32 bits makes the value backwards compatible by allowing
0101 C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely
0102 increase and decrease the objects reference count. The object would lose its
0103 immortality, but the execution would still be correct.
0104 
0105 Reference count increases will use saturated arithmetic, taking advantage of
0106 having all the lower 32 bits set, which will avoid the reference count to go
0107 beyond the refcount limit. Immortality checks for reference count decreases will
0108 be done by checking the bit sign flag in the lower 32 bits.
0109 */
0110 #define _Py_IMMORTAL_REFCNT UINT_MAX
0111 
0112 #else
0113 /*
0114 In 32 bit systems, an object will be marked as immortal by setting all of the
0115 lower 30 bits of the reference count field, which is equal to: 0x3FFFFFFF
0116 
0117 Using the lower 30 bits makes the value backwards compatible by allowing
0118 C-Extensions without the updated checks in Py_INCREF and Py_DECREF to safely
0119 increase and decrease the objects reference count. The object would lose its
0120 immortality, but the execution would still be correct.
0121 
0122 Reference count increases and decreases will first go through an immortality
0123 check by comparing the reference count field to the immortality reference count.
0124 */
0125 #define _Py_IMMORTAL_REFCNT (UINT_MAX >> 2)
0126 #endif
0127 
0128 // Make all internal uses of PyObject_HEAD_INIT immortal while preserving the
0129 // C-API expectation that the refcnt will be set to 1.
0130 #ifdef Py_BUILD_CORE
0131 #define PyObject_HEAD_INIT(type)    \
0132     {                               \
0133         _PyObject_EXTRA_INIT        \
0134         { _Py_IMMORTAL_REFCNT },    \
0135         (type)                      \
0136     },
0137 #else
0138 #define PyObject_HEAD_INIT(type) \
0139     {                            \
0140         _PyObject_EXTRA_INIT     \
0141         { 1 },                   \
0142         (type)                   \
0143     },
0144 #endif /* Py_BUILD_CORE */
0145 
0146 #define PyVarObject_HEAD_INIT(type, size) \
0147     {                                     \
0148         PyObject_HEAD_INIT(type)          \
0149         (size)                            \
0150     },
0151 
0152 /* PyObject_VAR_HEAD defines the initial segment of all variable-size
0153  * container objects.  These end with a declaration of an array with 1
0154  * element, but enough space is malloc'ed so that the array actually
0155  * has room for ob_size elements.  Note that ob_size is an element count,
0156  * not necessarily a byte count.
0157  */
0158 #define PyObject_VAR_HEAD      PyVarObject ob_base;
0159 #define Py_INVALID_SIZE (Py_ssize_t)-1
0160 
0161 /* Nothing is actually declared to be a PyObject, but every pointer to
0162  * a Python object can be cast to a PyObject*.  This is inheritance built
0163  * by hand.  Similarly every pointer to a variable-size Python object can,
0164  * in addition, be cast to PyVarObject*.
0165  */
0166 struct _object {
0167     _PyObject_HEAD_EXTRA
0168 
0169 #if (defined(__GNUC__) || defined(__clang__)) \
0170         && !(defined __STDC_VERSION__ && __STDC_VERSION__ >= 201112L)
0171     // On C99 and older, anonymous union is a GCC and clang extension
0172     __extension__
0173 #endif
0174 #ifdef _MSC_VER
0175     // Ignore MSC warning C4201: "nonstandard extension used:
0176     // nameless struct/union"
0177     __pragma(warning(push))
0178     __pragma(warning(disable: 4201))
0179 #endif
0180     union {
0181        Py_ssize_t ob_refcnt;
0182 #if SIZEOF_VOID_P > 4
0183        PY_UINT32_T ob_refcnt_split[2];
0184 #endif
0185     };
0186 #ifdef _MSC_VER
0187     __pragma(warning(pop))
0188 #endif
0189 
0190     PyTypeObject *ob_type;
0191 };
0192 
0193 /* Cast argument to PyObject* type. */
0194 #define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
0195 
0196 typedef struct {
0197     PyObject ob_base;
0198     Py_ssize_t ob_size; /* Number of items in variable part */
0199 } PyVarObject;
0200 
0201 /* Cast argument to PyVarObject* type. */
0202 #define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op))
0203 
0204 
0205 // Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
0206 PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
0207 #define Py_Is(x, y) ((x) == (y))
0208 
0209 
0210 static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
0211     return ob->ob_refcnt;
0212 }
0213 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0214 #  define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
0215 #endif
0216 
0217 
0218 // bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
0219 static inline PyTypeObject* Py_TYPE(PyObject *ob) {
0220     return ob->ob_type;
0221 }
0222 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0223 #  define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
0224 #endif
0225 
0226 PyAPI_DATA(PyTypeObject) PyLong_Type;
0227 PyAPI_DATA(PyTypeObject) PyBool_Type;
0228 
0229 // bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
0230 static inline Py_ssize_t Py_SIZE(PyObject *ob) {
0231     assert(ob->ob_type != &PyLong_Type);
0232     assert(ob->ob_type != &PyBool_Type);
0233     return  _PyVarObject_CAST(ob)->ob_size;
0234 }
0235 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0236 #  define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
0237 #endif
0238 
0239 static inline Py_ALWAYS_INLINE int _Py_IsImmortal(PyObject *op)
0240 {
0241 #if SIZEOF_VOID_P > 4
0242     return _Py_CAST(PY_INT32_T, op->ob_refcnt) < 0;
0243 #else
0244     return op->ob_refcnt == _Py_IMMORTAL_REFCNT;
0245 #endif
0246 }
0247 #define _Py_IsImmortal(op) _Py_IsImmortal(_PyObject_CAST(op))
0248 
0249 static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
0250     return Py_TYPE(ob) == type;
0251 }
0252 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0253 #  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), (type))
0254 #endif
0255 
0256 
0257 static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
0258     // This immortal check is for code that is unaware of immortal objects.
0259     // The runtime tracks these objects and we should avoid as much
0260     // as possible having extensions inadvertently change the refcnt
0261     // of an immortalized object.
0262     if (_Py_IsImmortal(ob)) {
0263         return;
0264     }
0265     ob->ob_refcnt = refcnt;
0266 }
0267 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0268 #  define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), (refcnt))
0269 #endif
0270 
0271 
0272 static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
0273     ob->ob_type = type;
0274 }
0275 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0276 #  define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
0277 #endif
0278 
0279 static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
0280     assert(ob->ob_base.ob_type != &PyLong_Type);
0281     assert(ob->ob_base.ob_type != &PyBool_Type);
0282     ob->ob_size = size;
0283 }
0284 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0285 #  define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), (size))
0286 #endif
0287 
0288 
0289 /*
0290 Type objects contain a string containing the type name (to help somewhat
0291 in debugging), the allocation parameters (see PyObject_New() and
0292 PyObject_NewVar()),
0293 and methods for accessing objects of the type.  Methods are optional, a
0294 nil pointer meaning that particular kind of access is not available for
0295 this type.  The Py_DECREF() macro uses the tp_dealloc method without
0296 checking for a nil pointer; it should always be implemented except if
0297 the implementation can guarantee that the reference count will never
0298 reach zero (e.g., for statically allocated type objects).
0299 
0300 NB: the methods for certain type groups are now contained in separate
0301 method blocks.
0302 */
0303 
0304 typedef PyObject * (*unaryfunc)(PyObject *);
0305 typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
0306 typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
0307 typedef int (*inquiry)(PyObject *);
0308 typedef Py_ssize_t (*lenfunc)(PyObject *);
0309 typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
0310 typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
0311 typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
0312 typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
0313 typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
0314 
0315 typedef int (*objobjproc)(PyObject *, PyObject *);
0316 typedef int (*visitproc)(PyObject *, void *);
0317 typedef int (*traverseproc)(PyObject *, visitproc, void *);
0318 
0319 
0320 typedef void (*freefunc)(void *);
0321 typedef void (*destructor)(PyObject *);
0322 typedef PyObject *(*getattrfunc)(PyObject *, char *);
0323 typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
0324 typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
0325 typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
0326 typedef PyObject *(*reprfunc)(PyObject *);
0327 typedef Py_hash_t (*hashfunc)(PyObject *);
0328 typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
0329 typedef PyObject *(*getiterfunc) (PyObject *);
0330 typedef PyObject *(*iternextfunc) (PyObject *);
0331 typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
0332 typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
0333 typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
0334 typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
0335 typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
0336 
0337 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030c0000 // 3.12
0338 typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args,
0339                                     size_t nargsf, PyObject *kwnames);
0340 #endif
0341 
0342 typedef struct{
0343     int slot;    /* slot id, see below */
0344     void *pfunc; /* function pointer */
0345 } PyType_Slot;
0346 
0347 typedef struct{
0348     const char* name;
0349     int basicsize;
0350     int itemsize;
0351     unsigned int flags;
0352     PyType_Slot *slots; /* terminated by slot==0. */
0353 } PyType_Spec;
0354 
0355 PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
0356 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
0357 PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
0358 #endif
0359 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
0360 PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
0361 #endif
0362 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
0363 PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
0364 PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *);
0365 PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
0366 #endif
0367 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
0368 PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
0369 PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
0370 #endif
0371 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
0372 PyAPI_FUNC(PyObject *) PyType_FromMetaclass(PyTypeObject*, PyObject*, PyType_Spec*, PyObject*);
0373 PyAPI_FUNC(void *) PyObject_GetTypeData(PyObject *obj, PyTypeObject *cls);
0374 PyAPI_FUNC(Py_ssize_t) PyType_GetTypeDataSize(PyTypeObject *cls);
0375 #endif
0376 
0377 /* Generic type check */
0378 PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
0379 
0380 static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
0381     return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
0382 }
0383 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0384 #  define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), (type))
0385 #endif
0386 
0387 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
0388 PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
0389 PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
0390 
0391 PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
0392 
0393 PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
0394 PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
0395 PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
0396                                                PyObject *, PyObject *);
0397 PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
0398 PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
0399 
0400 /* Generic operations on objects */
0401 PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
0402 PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
0403 PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
0404 PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
0405 PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
0406 PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
0407 PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
0408 PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
0409 PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
0410 PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
0411 PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
0412 PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
0413 PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
0414 PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
0415 PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
0416 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
0417 PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
0418 #endif
0419 PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
0420 PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
0421 PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
0422 PyAPI_FUNC(int) PyObject_Not(PyObject *);
0423 PyAPI_FUNC(int) PyCallable_Check(PyObject *);
0424 PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
0425 
0426 /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
0427    list of strings.  PyObject_Dir(NULL) is like builtins.dir(),
0428    returning the names of the current locals.  In this case, if there are
0429    no current locals, NULL is returned, and PyErr_Occurred() is false.
0430 */
0431 PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
0432 
0433 /* Pickle support. */
0434 #ifndef Py_LIMITED_API
0435 PyAPI_FUNC(PyObject *) _PyObject_GetState(PyObject *);
0436 #endif
0437 
0438 
0439 /* Helpers for printing recursive container types */
0440 PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
0441 PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
0442 
0443 /* Flag bits for printing: */
0444 #define Py_PRINT_RAW    1       /* No string quotes etc. */
0445 
0446 /*
0447 Type flags (tp_flags)
0448 
0449 These flags are used to change expected features and behavior for a
0450 particular type.
0451 
0452 Arbitration of the flag bit positions will need to be coordinated among
0453 all extension writers who publicly release their extensions (this will
0454 be fewer than you might expect!).
0455 
0456 Most flags were removed as of Python 3.0 to make room for new flags.  (Some
0457 flags are not for backwards compatibility but to indicate the presence of an
0458 optional feature; these flags remain of course.)
0459 
0460 Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
0461 
0462 Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
0463 given type object has a specified feature.
0464 */
0465 
0466 #ifndef Py_LIMITED_API
0467 
0468 /* Track types initialized using _PyStaticType_InitBuiltin(). */
0469 #define _Py_TPFLAGS_STATIC_BUILTIN (1 << 1)
0470 
0471 /* Placement of weakref pointers are managed by the VM, not by the type.
0472  * The VM will automatically set tp_weaklistoffset.
0473  */
0474 #define Py_TPFLAGS_MANAGED_WEAKREF (1 << 3)
0475 
0476 /* Placement of dict (and values) pointers are managed by the VM, not by the type.
0477  * The VM will automatically set tp_dictoffset.
0478  */
0479 #define Py_TPFLAGS_MANAGED_DICT (1 << 4)
0480 
0481 #define Py_TPFLAGS_PREHEADER (Py_TPFLAGS_MANAGED_WEAKREF | Py_TPFLAGS_MANAGED_DICT)
0482 
0483 /* Set if instances of the type object are treated as sequences for pattern matching */
0484 #define Py_TPFLAGS_SEQUENCE (1 << 5)
0485 /* Set if instances of the type object are treated as mappings for pattern matching */
0486 #define Py_TPFLAGS_MAPPING (1 << 6)
0487 #endif
0488 
0489 /* Disallow creating instances of the type: set tp_new to NULL and don't create
0490  * the "__new__" key in the type dictionary. */
0491 #define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
0492 
0493 /* Set if the type object is immutable: type attributes cannot be set nor deleted */
0494 #define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
0495 
0496 /* Set if the type object is dynamically allocated */
0497 #define Py_TPFLAGS_HEAPTYPE (1UL << 9)
0498 
0499 /* Set if the type allows subclassing */
0500 #define Py_TPFLAGS_BASETYPE (1UL << 10)
0501 
0502 /* Set if the type implements the vectorcall protocol (PEP 590) */
0503 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030C0000
0504 #define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
0505 #ifndef Py_LIMITED_API
0506 // Backwards compatibility alias for API that was provisional in Python 3.8
0507 #define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
0508 #endif
0509 #endif
0510 
0511 /* Set if the type is 'ready' -- fully initialized */
0512 #define Py_TPFLAGS_READY (1UL << 12)
0513 
0514 /* Set while the type is being 'readied', to prevent recursive ready calls */
0515 #define Py_TPFLAGS_READYING (1UL << 13)
0516 
0517 /* Objects support garbage collection (see objimpl.h) */
0518 #define Py_TPFLAGS_HAVE_GC (1UL << 14)
0519 
0520 /* These two bits are preserved for Stackless Python, next after this is 17 */
0521 #ifdef STACKLESS
0522 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
0523 #else
0524 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
0525 #endif
0526 
0527 /* Objects behave like an unbound method */
0528 #define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
0529 
0530 /* Object has up-to-date type attribute cache */
0531 #define Py_TPFLAGS_VALID_VERSION_TAG  (1UL << 19)
0532 
0533 /* Type is abstract and cannot be instantiated */
0534 #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
0535 
0536 // This undocumented flag gives certain built-ins their unique pattern-matching
0537 // behavior, which allows a single positional subpattern to match against the
0538 // subject itself (rather than a mapped attribute on it):
0539 #define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
0540 
0541 /* Items (ob_size*tp_itemsize) are found at the end of an instance's memory */
0542 #define Py_TPFLAGS_ITEMS_AT_END (1UL << 23)
0543 
0544 /* These flags are used to determine if a type is a subclass. */
0545 #define Py_TPFLAGS_LONG_SUBCLASS        (1UL << 24)
0546 #define Py_TPFLAGS_LIST_SUBCLASS        (1UL << 25)
0547 #define Py_TPFLAGS_TUPLE_SUBCLASS       (1UL << 26)
0548 #define Py_TPFLAGS_BYTES_SUBCLASS       (1UL << 27)
0549 #define Py_TPFLAGS_UNICODE_SUBCLASS     (1UL << 28)
0550 #define Py_TPFLAGS_DICT_SUBCLASS        (1UL << 29)
0551 #define Py_TPFLAGS_BASE_EXC_SUBCLASS    (1UL << 30)
0552 #define Py_TPFLAGS_TYPE_SUBCLASS        (1UL << 31)
0553 
0554 #define Py_TPFLAGS_DEFAULT  ( \
0555                  Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
0556                 0)
0557 
0558 /* NOTE: Some of the following flags reuse lower bits (removed as part of the
0559  * Python 3.0 transition). */
0560 
0561 /* The following flags are kept for compatibility; in previous
0562  * versions they indicated presence of newer tp_* fields on the
0563  * type struct.
0564  * Starting with 3.8, binary compatibility of C extensions across
0565  * feature releases of Python is not supported anymore (except when
0566  * using the stable ABI, in which all classes are created dynamically,
0567  * using the interpreter's memory layout.)
0568  * Note that older extensions using the stable ABI set these flags,
0569  * so the bits must not be repurposed.
0570  */
0571 #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
0572 #define Py_TPFLAGS_HAVE_VERSION_TAG   (1UL << 18)
0573 
0574 
0575 /*
0576 The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
0577 reference counts.  Py_DECREF calls the object's deallocator function when
0578 the refcount falls to 0; for
0579 objects that don't contain references to other objects or heap memory
0580 this can be the standard function free().  Both macros can be used
0581 wherever a void expression is allowed.  The argument must not be a
0582 NULL pointer.  If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
0583 The macro _Py_NewReference(op) initialize reference counts to 1, and
0584 in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
0585 bookkeeping appropriate to the special build.
0586 
0587 We assume that the reference count field can never overflow; this can
0588 be proven when the size of the field is the same as the pointer size, so
0589 we ignore the possibility.  Provided a C int is at least 32 bits (which
0590 is implicitly assumed in many parts of this code), that's enough for
0591 about 2**31 references to an object.
0592 
0593 XXX The following became out of date in Python 2.2, but I'm not sure
0594 XXX what the full truth is now.  Certainly, heap-allocated type objects
0595 XXX can and should be deallocated.
0596 Type objects should never be deallocated; the type pointer in an object
0597 is not considered to be a reference to the type object, to save
0598 complications in the deallocation function.  (This is actually a
0599 decision that's up to the implementer of each new type so if you want,
0600 you can count such references to the type object.)
0601 */
0602 
0603 #if defined(Py_REF_DEBUG) && !defined(Py_LIMITED_API)
0604 PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
0605                                       PyObject *op);
0606 PyAPI_FUNC(void) _Py_INCREF_IncRefTotal(void);
0607 PyAPI_FUNC(void) _Py_DECREF_DecRefTotal(void);
0608 #endif  // Py_REF_DEBUG && !Py_LIMITED_API
0609 
0610 PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
0611 
0612 /*
0613 These are provided as conveniences to Python runtime embedders, so that
0614 they can have object code that is not dependent on Python compilation flags.
0615 */
0616 PyAPI_FUNC(void) Py_IncRef(PyObject *);
0617 PyAPI_FUNC(void) Py_DecRef(PyObject *);
0618 
0619 // Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
0620 // Private functions used by Py_INCREF() and Py_DECREF().
0621 PyAPI_FUNC(void) _Py_IncRef(PyObject *);
0622 PyAPI_FUNC(void) _Py_DecRef(PyObject *);
0623 
0624 static inline Py_ALWAYS_INLINE void Py_INCREF(PyObject *op)
0625 {
0626 #if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
0627     // Stable ABI implements Py_INCREF() as a function call on limited C API
0628     // version 3.12 and newer, and on Python built in debug mode. _Py_IncRef()
0629     // was added to Python 3.10.0a7, use Py_IncRef() on older Python versions.
0630     // Py_IncRef() accepts NULL whereas _Py_IncRef() doesn't.
0631 #  if Py_LIMITED_API+0 >= 0x030a00A7
0632     _Py_IncRef(op);
0633 #  else
0634     Py_IncRef(op);
0635 #  endif
0636 #else
0637     // Non-limited C API and limited C API for Python 3.9 and older access
0638     // directly PyObject.ob_refcnt.
0639 #if SIZEOF_VOID_P > 4
0640     // Portable saturated add, branching on the carry flag and set low bits
0641     PY_UINT32_T cur_refcnt = op->ob_refcnt_split[PY_BIG_ENDIAN];
0642     PY_UINT32_T new_refcnt = cur_refcnt + 1;
0643     if (new_refcnt == 0) {
0644         return;
0645     }
0646     op->ob_refcnt_split[PY_BIG_ENDIAN] = new_refcnt;
0647 #else
0648     // Explicitly check immortality against the immortal value
0649     if (_Py_IsImmortal(op)) {
0650         return;
0651     }
0652     op->ob_refcnt++;
0653 #endif
0654     _Py_INCREF_STAT_INC();
0655 #ifdef Py_REF_DEBUG
0656     _Py_INCREF_IncRefTotal();
0657 #endif
0658 #endif
0659 }
0660 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0661 #  define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
0662 #endif
0663 
0664 #if defined(Py_LIMITED_API) && (Py_LIMITED_API+0 >= 0x030c0000 || defined(Py_REF_DEBUG))
0665 // Stable ABI implements Py_DECREF() as a function call on limited C API
0666 // version 3.12 and newer, and on Python built in debug mode. _Py_DecRef() was
0667 // added to Python 3.10.0a7, use Py_DecRef() on older Python versions.
0668 // Py_DecRef() accepts NULL whereas _Py_IncRef() doesn't.
0669 static inline void Py_DECREF(PyObject *op) {
0670 #  if Py_LIMITED_API+0 >= 0x030a00A7
0671     _Py_DecRef(op);
0672 #  else
0673     Py_DecRef(op);
0674 #  endif
0675 }
0676 #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
0677 
0678 #elif defined(Py_REF_DEBUG)
0679 static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
0680 {
0681     if (op->ob_refcnt <= 0) {
0682         _Py_NegativeRefcount(filename, lineno, op);
0683     }
0684     if (_Py_IsImmortal(op)) {
0685         return;
0686     }
0687     _Py_DECREF_STAT_INC();
0688     _Py_DECREF_DecRefTotal();
0689     if (--op->ob_refcnt == 0) {
0690         _Py_Dealloc(op);
0691     }
0692 }
0693 #define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
0694 
0695 #else
0696 static inline Py_ALWAYS_INLINE void Py_DECREF(PyObject *op)
0697 {
0698     // Non-limited C API and limited C API for Python 3.9 and older access
0699     // directly PyObject.ob_refcnt.
0700     if (_Py_IsImmortal(op)) {
0701         return;
0702     }
0703     _Py_DECREF_STAT_INC();
0704     if (--op->ob_refcnt == 0) {
0705         _Py_Dealloc(op);
0706     }
0707 }
0708 #define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
0709 #endif
0710 
0711 
0712 /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
0713  * and tp_dealloc implementations.
0714  *
0715  * Note that "the obvious" code can be deadly:
0716  *
0717  *     Py_XDECREF(op);
0718  *     op = NULL;
0719  *
0720  * Typically, `op` is something like self->containee, and `self` is done
0721  * using its `containee` member.  In the code sequence above, suppose
0722  * `containee` is non-NULL with a refcount of 1.  Its refcount falls to
0723  * 0 on the first line, which can trigger an arbitrary amount of code,
0724  * possibly including finalizers (like __del__ methods or weakref callbacks)
0725  * coded in Python, which in turn can release the GIL and allow other threads
0726  * to run, etc.  Such code may even invoke methods of `self` again, or cause
0727  * cyclic gc to trigger, but-- oops! --self->containee still points to the
0728  * object being torn down, and it may be in an insane state while being torn
0729  * down.  This has in fact been a rich historic source of miserable (rare &
0730  * hard-to-diagnose) segfaulting (and other) bugs.
0731  *
0732  * The safe way is:
0733  *
0734  *      Py_CLEAR(op);
0735  *
0736  * That arranges to set `op` to NULL _before_ decref'ing, so that any code
0737  * triggered as a side-effect of `op` getting torn down no longer believes
0738  * `op` points to a valid object.
0739  *
0740  * There are cases where it's safe to use the naive code, but they're brittle.
0741  * For example, if `op` points to a Python integer, you know that destroying
0742  * one of those can't cause problems -- but in part that relies on that
0743  * Python integers aren't currently weakly referencable.  Best practice is
0744  * to use Py_CLEAR() even if you can't think of a reason for why you need to.
0745  *
0746  * gh-98724: Use a temporary variable to only evaluate the macro argument once,
0747  * to avoid the duplication of side effects if the argument has side effects.
0748  *
0749  * gh-99701: If the PyObject* type is used with casting arguments to PyObject*,
0750  * the code can be miscompiled with strict aliasing because of type punning.
0751  * With strict aliasing, a compiler considers that two pointers of different
0752  * types cannot read or write the same memory which enables optimization
0753  * opportunities.
0754  *
0755  * If available, use _Py_TYPEOF() to use the 'op' type for temporary variables,
0756  * and so avoid type punning. Otherwise, use memcpy() which causes type erasure
0757  * and so prevents the compiler to reuse an old cached 'op' value after
0758  * Py_CLEAR().
0759  */
0760 #ifdef _Py_TYPEOF
0761 #define Py_CLEAR(op) \
0762     do { \
0763         _Py_TYPEOF(op)* _tmp_op_ptr = &(op); \
0764         _Py_TYPEOF(op) _tmp_old_op = (*_tmp_op_ptr); \
0765         if (_tmp_old_op != NULL) { \
0766             *_tmp_op_ptr = _Py_NULL; \
0767             Py_DECREF(_tmp_old_op); \
0768         } \
0769     } while (0)
0770 #else
0771 #define Py_CLEAR(op) \
0772     do { \
0773         PyObject **_tmp_op_ptr = _Py_CAST(PyObject**, &(op)); \
0774         PyObject *_tmp_old_op = (*_tmp_op_ptr); \
0775         if (_tmp_old_op != NULL) { \
0776             PyObject *_null_ptr = _Py_NULL; \
0777             memcpy(_tmp_op_ptr, &_null_ptr, sizeof(PyObject*)); \
0778             Py_DECREF(_tmp_old_op); \
0779         } \
0780     } while (0)
0781 #endif
0782 
0783 
0784 /* Function to use in case the object pointer can be NULL: */
0785 static inline void Py_XINCREF(PyObject *op)
0786 {
0787     if (op != _Py_NULL) {
0788         Py_INCREF(op);
0789     }
0790 }
0791 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0792 #  define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op))
0793 #endif
0794 
0795 static inline void Py_XDECREF(PyObject *op)
0796 {
0797     if (op != _Py_NULL) {
0798         Py_DECREF(op);
0799     }
0800 }
0801 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0802 #  define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op))
0803 #endif
0804 
0805 // Create a new strong reference to an object:
0806 // increment the reference count of the object and return the object.
0807 PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
0808 
0809 // Similar to Py_NewRef(), but the object can be NULL.
0810 PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
0811 
0812 static inline PyObject* _Py_NewRef(PyObject *obj)
0813 {
0814     Py_INCREF(obj);
0815     return obj;
0816 }
0817 
0818 static inline PyObject* _Py_XNewRef(PyObject *obj)
0819 {
0820     Py_XINCREF(obj);
0821     return obj;
0822 }
0823 
0824 // Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
0825 // Names overridden with macros by static inline functions for best
0826 // performances.
0827 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0828 #  define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
0829 #  define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
0830 #else
0831 #  define Py_NewRef(obj) _Py_NewRef(obj)
0832 #  define Py_XNewRef(obj) _Py_XNewRef(obj)
0833 #endif
0834 
0835 
0836 /*
0837 _Py_NoneStruct is an object of undefined type which can be used in contexts
0838 where NULL (nil) is not suitable (since NULL often means 'error').
0839 
0840 Don't forget to apply Py_INCREF() when returning this value!!!
0841 */
0842 PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
0843 #define Py_None (&_Py_NoneStruct)
0844 
0845 // Test if an object is the None singleton, the same as "x is None" in Python.
0846 PyAPI_FUNC(int) Py_IsNone(PyObject *x);
0847 #define Py_IsNone(x) Py_Is((x), Py_None)
0848 
0849 /* Macro for returning Py_None from a function */
0850 #define Py_RETURN_NONE return Py_None
0851 
0852 /*
0853 Py_NotImplemented is a singleton used to signal that an operation is
0854 not implemented for a given type combination.
0855 */
0856 PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
0857 #define Py_NotImplemented (&_Py_NotImplementedStruct)
0858 
0859 /* Macro for returning Py_NotImplemented from a function */
0860 #define Py_RETURN_NOTIMPLEMENTED return Py_NotImplemented
0861 
0862 /* Rich comparison opcodes */
0863 #define Py_LT 0
0864 #define Py_LE 1
0865 #define Py_EQ 2
0866 #define Py_NE 3
0867 #define Py_GT 4
0868 #define Py_GE 5
0869 
0870 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
0871 /* Result of calling PyIter_Send */
0872 typedef enum {
0873     PYGEN_RETURN = 0,
0874     PYGEN_ERROR = -1,
0875     PYGEN_NEXT = 1,
0876 } PySendResult;
0877 #endif
0878 
0879 /*
0880  * Macro for implementing rich comparisons
0881  *
0882  * Needs to be a macro because any C-comparable type can be used.
0883  */
0884 #define Py_RETURN_RICHCOMPARE(val1, val2, op)                               \
0885     do {                                                                    \
0886         switch (op) {                                                       \
0887         case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
0888         case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
0889         case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
0890         case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
0891         case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
0892         case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
0893         default:                                                            \
0894             Py_UNREACHABLE();                                               \
0895         }                                                                   \
0896     } while (0)
0897 
0898 
0899 /*
0900 More conventions
0901 ================
0902 
0903 Argument Checking
0904 -----------------
0905 
0906 Functions that take objects as arguments normally don't check for nil
0907 arguments, but they do check the type of the argument, and return an
0908 error if the function doesn't apply to the type.
0909 
0910 Failure Modes
0911 -------------
0912 
0913 Functions may fail for a variety of reasons, including running out of
0914 memory.  This is communicated to the caller in two ways: an error string
0915 is set (see errors.h), and the function result differs: functions that
0916 normally return a pointer return NULL for failure, functions returning
0917 an integer return -1 (which could be a legal return value too!), and
0918 other functions return 0 for success and -1 for failure.
0919 Callers should always check for errors before using the result.  If
0920 an error was set, the caller must either explicitly clear it, or pass
0921 the error on to its caller.
0922 
0923 Reference Counts
0924 ----------------
0925 
0926 It takes a while to get used to the proper usage of reference counts.
0927 
0928 Functions that create an object set the reference count to 1; such new
0929 objects must be stored somewhere or destroyed again with Py_DECREF().
0930 Some functions that 'store' objects, such as PyTuple_SetItem() and
0931 PyList_SetItem(),
0932 don't increment the reference count of the object, since the most
0933 frequent use is to store a fresh object.  Functions that 'retrieve'
0934 objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
0935 don't increment
0936 the reference count, since most frequently the object is only looked at
0937 quickly.  Thus, to retrieve an object and store it again, the caller
0938 must call Py_INCREF() explicitly.
0939 
0940 NOTE: functions that 'consume' a reference count, like
0941 PyList_SetItem(), consume the reference even if the object wasn't
0942 successfully stored, to simplify error handling.
0943 
0944 It seems attractive to make other functions that take an object as
0945 argument consume a reference count; however, this may quickly get
0946 confusing (even the current practice is already confusing).  Consider
0947 it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
0948 times.
0949 */
0950 
0951 #ifndef Py_LIMITED_API
0952 #  define Py_CPYTHON_OBJECT_H
0953 #  include "cpython/object.h"
0954 #  undef Py_CPYTHON_OBJECT_H
0955 #endif
0956 
0957 
0958 static inline int
0959 PyType_HasFeature(PyTypeObject *type, unsigned long feature)
0960 {
0961     unsigned long flags;
0962 #ifdef Py_LIMITED_API
0963     // PyTypeObject is opaque in the limited C API
0964     flags = PyType_GetFlags(type);
0965 #else
0966     flags = type->tp_flags;
0967 #endif
0968     return ((flags & feature) != 0);
0969 }
0970 
0971 #define PyType_FastSubclass(type, flag) PyType_HasFeature((type), (flag))
0972 
0973 static inline int PyType_Check(PyObject *op) {
0974     return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
0975 }
0976 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0977 #  define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
0978 #endif
0979 
0980 #define _PyType_CAST(op) \
0981     (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op)))
0982 
0983 static inline int PyType_CheckExact(PyObject *op) {
0984     return Py_IS_TYPE(op, &PyType_Type);
0985 }
0986 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
0987 #  define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
0988 #endif
0989 
0990 #ifdef __cplusplus
0991 }
0992 #endif
0993 #endif   // !Py_OBJECT_H