Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-30 10:18:04

0001 #ifndef Py_CPYTHON_OBJECT_H
0002 #  error "this header file must not be included directly"
0003 #endif
0004 
0005 PyAPI_FUNC(void) _Py_NewReference(PyObject *op);
0006 PyAPI_FUNC(void) _Py_NewReferenceNoTotal(PyObject *op);
0007 
0008 #ifdef Py_TRACE_REFS
0009 /* Py_TRACE_REFS is such major surgery that we call external routines. */
0010 PyAPI_FUNC(void) _Py_ForgetReference(PyObject *);
0011 #endif
0012 
0013 #ifdef Py_REF_DEBUG
0014 /* These are useful as debugging aids when chasing down refleaks. */
0015 PyAPI_FUNC(Py_ssize_t) _Py_GetGlobalRefTotal(void);
0016 #  define _Py_GetRefTotal() _Py_GetGlobalRefTotal()
0017 PyAPI_FUNC(Py_ssize_t) _Py_GetLegacyRefTotal(void);
0018 PyAPI_FUNC(Py_ssize_t) _PyInterpreterState_GetRefTotal(PyInterpreterState *);
0019 #endif
0020 
0021 
0022 /********************* String Literals ****************************************/
0023 /* This structure helps managing static strings. The basic usage goes like this:
0024    Instead of doing
0025 
0026        r = PyObject_CallMethod(o, "foo", "args", ...);
0027 
0028    do
0029 
0030        _Py_IDENTIFIER(foo);
0031        ...
0032        r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...);
0033 
0034    PyId_foo is a static variable, either on block level or file level. On first
0035    usage, the string "foo" is interned, and the structures are linked. On interpreter
0036    shutdown, all strings are released.
0037 
0038    Alternatively, _Py_static_string allows choosing the variable name.
0039    _PyUnicode_FromId returns a borrowed reference to the interned string.
0040    _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*.
0041 */
0042 typedef struct _Py_Identifier {
0043     const char* string;
0044     // Index in PyInterpreterState.unicode.ids.array. It is process-wide
0045     // unique and must be initialized to -1.
0046     Py_ssize_t index;
0047 } _Py_Identifier;
0048 
0049 #ifndef Py_BUILD_CORE
0050 // For now we are keeping _Py_IDENTIFIER for continued use
0051 // in non-builtin extensions (and naughty PyPI modules).
0052 
0053 #define _Py_static_string_init(value) { .string = (value), .index = -1 }
0054 #define _Py_static_string(varname, value)  static _Py_Identifier varname = _Py_static_string_init(value)
0055 #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname)
0056 
0057 #endif /* !Py_BUILD_CORE */
0058 
0059 typedef struct {
0060     /* Number implementations must check *both*
0061        arguments for proper type and implement the necessary conversions
0062        in the slot functions themselves. */
0063 
0064     binaryfunc nb_add;
0065     binaryfunc nb_subtract;
0066     binaryfunc nb_multiply;
0067     binaryfunc nb_remainder;
0068     binaryfunc nb_divmod;
0069     ternaryfunc nb_power;
0070     unaryfunc nb_negative;
0071     unaryfunc nb_positive;
0072     unaryfunc nb_absolute;
0073     inquiry nb_bool;
0074     unaryfunc nb_invert;
0075     binaryfunc nb_lshift;
0076     binaryfunc nb_rshift;
0077     binaryfunc nb_and;
0078     binaryfunc nb_xor;
0079     binaryfunc nb_or;
0080     unaryfunc nb_int;
0081     void *nb_reserved;  /* the slot formerly known as nb_long */
0082     unaryfunc nb_float;
0083 
0084     binaryfunc nb_inplace_add;
0085     binaryfunc nb_inplace_subtract;
0086     binaryfunc nb_inplace_multiply;
0087     binaryfunc nb_inplace_remainder;
0088     ternaryfunc nb_inplace_power;
0089     binaryfunc nb_inplace_lshift;
0090     binaryfunc nb_inplace_rshift;
0091     binaryfunc nb_inplace_and;
0092     binaryfunc nb_inplace_xor;
0093     binaryfunc nb_inplace_or;
0094 
0095     binaryfunc nb_floor_divide;
0096     binaryfunc nb_true_divide;
0097     binaryfunc nb_inplace_floor_divide;
0098     binaryfunc nb_inplace_true_divide;
0099 
0100     unaryfunc nb_index;
0101 
0102     binaryfunc nb_matrix_multiply;
0103     binaryfunc nb_inplace_matrix_multiply;
0104 } PyNumberMethods;
0105 
0106 typedef struct {
0107     lenfunc sq_length;
0108     binaryfunc sq_concat;
0109     ssizeargfunc sq_repeat;
0110     ssizeargfunc sq_item;
0111     void *was_sq_slice;
0112     ssizeobjargproc sq_ass_item;
0113     void *was_sq_ass_slice;
0114     objobjproc sq_contains;
0115 
0116     binaryfunc sq_inplace_concat;
0117     ssizeargfunc sq_inplace_repeat;
0118 } PySequenceMethods;
0119 
0120 typedef struct {
0121     lenfunc mp_length;
0122     binaryfunc mp_subscript;
0123     objobjargproc mp_ass_subscript;
0124 } PyMappingMethods;
0125 
0126 typedef PySendResult (*sendfunc)(PyObject *iter, PyObject *value, PyObject **result);
0127 
0128 typedef struct {
0129     unaryfunc am_await;
0130     unaryfunc am_aiter;
0131     unaryfunc am_anext;
0132     sendfunc am_send;
0133 } PyAsyncMethods;
0134 
0135 typedef struct {
0136      getbufferproc bf_getbuffer;
0137      releasebufferproc bf_releasebuffer;
0138 } PyBufferProcs;
0139 
0140 /* Allow printfunc in the tp_vectorcall_offset slot for
0141  * backwards-compatibility */
0142 typedef Py_ssize_t printfunc;
0143 
0144 // If this structure is modified, Doc/includes/typestruct.h should be updated
0145 // as well.
0146 struct _typeobject {
0147     PyObject_VAR_HEAD
0148     const char *tp_name; /* For printing, in format "<module>.<name>" */
0149     Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
0150 
0151     /* Methods to implement standard operations */
0152 
0153     destructor tp_dealloc;
0154     Py_ssize_t tp_vectorcall_offset;
0155     getattrfunc tp_getattr;
0156     setattrfunc tp_setattr;
0157     PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2)
0158                                     or tp_reserved (Python 3) */
0159     reprfunc tp_repr;
0160 
0161     /* Method suites for standard classes */
0162 
0163     PyNumberMethods *tp_as_number;
0164     PySequenceMethods *tp_as_sequence;
0165     PyMappingMethods *tp_as_mapping;
0166 
0167     /* More standard operations (here for binary compatibility) */
0168 
0169     hashfunc tp_hash;
0170     ternaryfunc tp_call;
0171     reprfunc tp_str;
0172     getattrofunc tp_getattro;
0173     setattrofunc tp_setattro;
0174 
0175     /* Functions to access object as input/output buffer */
0176     PyBufferProcs *tp_as_buffer;
0177 
0178     /* Flags to define presence of optional/expanded features */
0179     unsigned long tp_flags;
0180 
0181     const char *tp_doc; /* Documentation string */
0182 
0183     /* Assigned meaning in release 2.0 */
0184     /* call function for all accessible objects */
0185     traverseproc tp_traverse;
0186 
0187     /* delete references to contained objects */
0188     inquiry tp_clear;
0189 
0190     /* Assigned meaning in release 2.1 */
0191     /* rich comparisons */
0192     richcmpfunc tp_richcompare;
0193 
0194     /* weak reference enabler */
0195     Py_ssize_t tp_weaklistoffset;
0196 
0197     /* Iterators */
0198     getiterfunc tp_iter;
0199     iternextfunc tp_iternext;
0200 
0201     /* Attribute descriptor and subclassing stuff */
0202     PyMethodDef *tp_methods;
0203     PyMemberDef *tp_members;
0204     PyGetSetDef *tp_getset;
0205     // Strong reference on a heap type, borrowed reference on a static type
0206     PyTypeObject *tp_base;
0207     PyObject *tp_dict;
0208     descrgetfunc tp_descr_get;
0209     descrsetfunc tp_descr_set;
0210     Py_ssize_t tp_dictoffset;
0211     initproc tp_init;
0212     allocfunc tp_alloc;
0213     newfunc tp_new;
0214     freefunc tp_free; /* Low-level free-memory routine */
0215     inquiry tp_is_gc; /* For PyObject_IS_GC */
0216     PyObject *tp_bases;
0217     PyObject *tp_mro; /* method resolution order */
0218     PyObject *tp_cache; /* no longer used */
0219     void *tp_subclasses;  /* for static builtin types this is an index */
0220     PyObject *tp_weaklist; /* not used for static builtin types */
0221     destructor tp_del;
0222 
0223     /* Type attribute cache version tag. Added in version 2.6 */
0224     unsigned int tp_version_tag;
0225 
0226     destructor tp_finalize;
0227     vectorcallfunc tp_vectorcall;
0228 
0229     /* bitset of which type-watchers care about this type */
0230     unsigned char tp_watched;
0231 };
0232 
0233 /* This struct is used by the specializer
0234  * It should should be treated as an opaque blob
0235  * by code other than the specializer and interpreter. */
0236 struct _specialization_cache {
0237     // In order to avoid bloating the bytecode with lots of inline caches, the
0238     // members of this structure have a somewhat unique contract. They are set
0239     // by the specialization machinery, and are invalidated by PyType_Modified.
0240     // The rules for using them are as follows:
0241     // - If getitem is non-NULL, then it is the same Python function that
0242     //   PyType_Lookup(cls, "__getitem__") would return.
0243     // - If getitem is NULL, then getitem_version is meaningless.
0244     // - If getitem->func_version == getitem_version, then getitem can be called
0245     //   with two positional arguments and no keyword arguments, and has neither
0246     //   *args nor **kwargs (as required by BINARY_SUBSCR_GETITEM):
0247     PyObject *getitem;
0248     uint32_t getitem_version;
0249 };
0250 
0251 /* The *real* layout of a type object when allocated on the heap */
0252 typedef struct _heaptypeobject {
0253     /* Note: there's a dependency on the order of these members
0254        in slotptr() in typeobject.c . */
0255     PyTypeObject ht_type;
0256     PyAsyncMethods as_async;
0257     PyNumberMethods as_number;
0258     PyMappingMethods as_mapping;
0259     PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
0260                                       so that the mapping wins when both
0261                                       the mapping and the sequence define
0262                                       a given operator (e.g. __getitem__).
0263                                       see add_operators() in typeobject.c . */
0264     PyBufferProcs as_buffer;
0265     PyObject *ht_name, *ht_slots, *ht_qualname;
0266     struct _dictkeysobject *ht_cached_keys;
0267     PyObject *ht_module;
0268     char *_ht_tpname;  // Storage for "tp_name"; see PyType_FromModuleAndSpec
0269     struct _specialization_cache _spec_cache; // For use by the specializer.
0270     /* here are optional user slots, followed by the members. */
0271 } PyHeapTypeObject;
0272 
0273 PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *);
0274 PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *);
0275 PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *);
0276 PyAPI_FUNC(PyObject *) _PyObject_LookupSpecialId(PyObject *, _Py_Identifier *);
0277 #ifndef Py_BUILD_CORE
0278 // Backward compatibility for 3rd-party extensions
0279 // that may be using the old name.
0280 #define _PyObject_LookupSpecial _PyObject_LookupSpecialId
0281 #endif
0282 PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *);
0283 PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *);
0284 PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *);
0285 PyAPI_FUNC(PyObject *) PyType_GetModuleByDef(PyTypeObject *, PyModuleDef *);
0286 PyAPI_FUNC(PyObject *) PyType_GetDict(PyTypeObject *);
0287 
0288 PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int);
0289 PyAPI_FUNC(void) _Py_BreakPoint(void);
0290 PyAPI_FUNC(void) _PyObject_Dump(PyObject *);
0291 PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *);
0292 
0293 PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *);
0294 PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, _Py_Identifier *);
0295 PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, _Py_Identifier *, PyObject *);
0296 /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which
0297    don't raise AttributeError.
0298 
0299    Return 1 and set *result != NULL if an attribute is found.
0300    Return 0 and set *result == NULL if an attribute is not found;
0301    an AttributeError is silenced.
0302    Return -1 and set *result == NULL if an error other than AttributeError
0303    is raised.
0304 */
0305 PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **);
0306 PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, _Py_Identifier *, PyObject **);
0307 
0308 PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method);
0309 
0310 PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *);
0311 PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *);
0312 PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *);
0313 PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *);
0314 
0315 /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes
0316    dict as the last parameter. */
0317 PyAPI_FUNC(PyObject *)
0318 _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int);
0319 PyAPI_FUNC(int)
0320 _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *,
0321                                  PyObject *, PyObject *);
0322 
0323 PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *);
0324 
0325 /* Safely decref `dst` and set `dst` to `src`.
0326  *
0327  * As in case of Py_CLEAR "the obvious" code can be deadly:
0328  *
0329  *     Py_DECREF(dst);
0330  *     dst = src;
0331  *
0332  * The safe way is:
0333  *
0334  *      Py_SETREF(dst, src);
0335  *
0336  * That arranges to set `dst` to `src` _before_ decref'ing, so that any code
0337  * triggered as a side-effect of `dst` getting torn down no longer believes
0338  * `dst` points to a valid object.
0339  *
0340  * Temporary variables are used to only evalutate macro arguments once and so
0341  * avoid the duplication of side effects. _Py_TYPEOF() or memcpy() is used to
0342  * avoid a miscompilation caused by type punning. See Py_CLEAR() comment for
0343  * implementation details about type punning.
0344  *
0345  * The memcpy() implementation does not emit a compiler warning if 'src' has
0346  * not the same type than 'src': any pointer type is accepted for 'src'.
0347  */
0348 #ifdef _Py_TYPEOF
0349 #define Py_SETREF(dst, src) \
0350     do { \
0351         _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
0352         _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
0353         *_tmp_dst_ptr = (src); \
0354         Py_DECREF(_tmp_old_dst); \
0355     } while (0)
0356 #else
0357 #define Py_SETREF(dst, src) \
0358     do { \
0359         PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
0360         PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
0361         PyObject *_tmp_src = _PyObject_CAST(src); \
0362         memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
0363         Py_DECREF(_tmp_old_dst); \
0364     } while (0)
0365 #endif
0366 
0367 /* Py_XSETREF() is a variant of Py_SETREF() that uses Py_XDECREF() instead of
0368  * Py_DECREF().
0369  */
0370 #ifdef _Py_TYPEOF
0371 #define Py_XSETREF(dst, src) \
0372     do { \
0373         _Py_TYPEOF(dst)* _tmp_dst_ptr = &(dst); \
0374         _Py_TYPEOF(dst) _tmp_old_dst = (*_tmp_dst_ptr); \
0375         *_tmp_dst_ptr = (src); \
0376         Py_XDECREF(_tmp_old_dst); \
0377     } while (0)
0378 #else
0379 #define Py_XSETREF(dst, src) \
0380     do { \
0381         PyObject **_tmp_dst_ptr = _Py_CAST(PyObject**, &(dst)); \
0382         PyObject *_tmp_old_dst = (*_tmp_dst_ptr); \
0383         PyObject *_tmp_src = _PyObject_CAST(src); \
0384         memcpy(_tmp_dst_ptr, &_tmp_src, sizeof(PyObject*)); \
0385         Py_XDECREF(_tmp_old_dst); \
0386     } while (0)
0387 #endif
0388 
0389 
0390 PyAPI_DATA(PyTypeObject) _PyNone_Type;
0391 PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type;
0392 
0393 /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
0394  * Defined in object.c.
0395  */
0396 PyAPI_DATA(int) _Py_SwappedOp[];
0397 
0398 PyAPI_FUNC(void)
0399 _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks,
0400                        size_t sizeof_block);
0401 PyAPI_FUNC(void)
0402 _PyObject_DebugTypeStats(FILE *out);
0403 
0404 /* Define a pair of assertion macros:
0405    _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT().
0406 
0407    These work like the regular C assert(), in that they will abort the
0408    process with a message on stderr if the given condition fails to hold,
0409    but compile away to nothing if NDEBUG is defined.
0410 
0411    However, before aborting, Python will also try to call _PyObject_Dump() on
0412    the given object.  This may be of use when investigating bugs in which a
0413    particular object is corrupt (e.g. buggy a tp_visit method in an extension
0414    module breaking the garbage collector), to help locate the broken objects.
0415 
0416    The WITH_MSG variant allows you to supply an additional message that Python
0417    will attempt to print to stderr, after the object dump. */
0418 #ifdef NDEBUG
0419    /* No debugging: compile away the assertions: */
0420 #  define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
0421     ((void)0)
0422 #else
0423    /* With debugging: generate checks: */
0424 #  define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \
0425     ((expr) \
0426       ? (void)(0) \
0427       : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \
0428                                (msg), (filename), (lineno), (func)))
0429 #endif
0430 
0431 #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \
0432     _PyObject_ASSERT_FROM((obj), expr, (msg), __FILE__, __LINE__, __func__)
0433 #define _PyObject_ASSERT(obj, expr) \
0434     _PyObject_ASSERT_WITH_MSG((obj), expr, NULL)
0435 
0436 #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \
0437     _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__)
0438 
0439 /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined,
0440    to avoid causing compiler/linker errors when building extensions without
0441    NDEBUG against a Python built with NDEBUG defined.
0442 
0443    msg, expr and function can be NULL. */
0444 PyAPI_FUNC(void) _Py_NO_RETURN _PyObject_AssertFailed(
0445     PyObject *obj,
0446     const char *expr,
0447     const char *msg,
0448     const char *file,
0449     int line,
0450     const char *function);
0451 
0452 /* Check if an object is consistent. For example, ensure that the reference
0453    counter is greater than or equal to 1, and ensure that ob_type is not NULL.
0454 
0455    Call _PyObject_AssertFailed() if the object is inconsistent.
0456 
0457    If check_content is zero, only check header fields: reduce the overhead.
0458 
0459    The function always return 1. The return value is just here to be able to
0460    write:
0461 
0462    assert(_PyObject_CheckConsistency(obj, 1)); */
0463 PyAPI_FUNC(int) _PyObject_CheckConsistency(
0464     PyObject *op,
0465     int check_content);
0466 
0467 
0468 /* Trashcan mechanism, thanks to Christian Tismer.
0469 
0470 When deallocating a container object, it's possible to trigger an unbounded
0471 chain of deallocations, as each Py_DECREF in turn drops the refcount on "the
0472 next" object in the chain to 0.  This can easily lead to stack overflows,
0473 especially in threads (which typically have less stack space to work with).
0474 
0475 A container object can avoid this by bracketing the body of its tp_dealloc
0476 function with a pair of macros:
0477 
0478 static void
0479 mytype_dealloc(mytype *p)
0480 {
0481     ... declarations go here ...
0482 
0483     PyObject_GC_UnTrack(p);        // must untrack first
0484     Py_TRASHCAN_BEGIN(p, mytype_dealloc)
0485     ... The body of the deallocator goes here, including all calls ...
0486     ... to Py_DECREF on contained objects.                         ...
0487     Py_TRASHCAN_END                // there should be no code after this
0488 }
0489 
0490 CAUTION:  Never return from the middle of the body!  If the body needs to
0491 "get out early", put a label immediately before the Py_TRASHCAN_END
0492 call, and goto it.  Else the call-depth counter (see below) will stay
0493 above 0 forever, and the trashcan will never get emptied.
0494 
0495 How it works:  The BEGIN macro increments a call-depth counter.  So long
0496 as this counter is small, the body of the deallocator is run directly without
0497 further ado.  But if the counter gets large, it instead adds p to a list of
0498 objects to be deallocated later, skips the body of the deallocator, and
0499 resumes execution after the END macro.  The tp_dealloc routine then returns
0500 without deallocating anything (and so unbounded call-stack depth is avoided).
0501 
0502 When the call stack finishes unwinding again, code generated by the END macro
0503 notices this, and calls another routine to deallocate all the objects that
0504 may have been added to the list of deferred deallocations.  In effect, a
0505 chain of N deallocations is broken into (N-1)/(_PyTrash_UNWIND_LEVEL-1) pieces,
0506 with the call stack never exceeding a depth of _PyTrash_UNWIND_LEVEL.
0507 
0508 Since the tp_dealloc of a subclass typically calls the tp_dealloc of the base
0509 class, we need to ensure that the trashcan is only triggered on the tp_dealloc
0510 of the actual class being deallocated. Otherwise we might end up with a
0511 partially-deallocated object. To check this, the tp_dealloc function must be
0512 passed as second argument to Py_TRASHCAN_BEGIN().
0513 */
0514 
0515 /* Python 3.9 private API, invoked by the macros below. */
0516 PyAPI_FUNC(int) _PyTrash_begin(PyThreadState *tstate, PyObject *op);
0517 PyAPI_FUNC(void) _PyTrash_end(PyThreadState *tstate);
0518 /* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */
0519 PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc);
0520 
0521 #define Py_TRASHCAN_BEGIN_CONDITION(op, cond) \
0522     do { \
0523         PyThreadState *_tstate = NULL; \
0524         /* If "cond" is false, then _tstate remains NULL and the deallocator \
0525          * is run normally without involving the trashcan */ \
0526         if (cond) { \
0527             _tstate = _PyThreadState_UncheckedGet(); \
0528             if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \
0529                 break; \
0530             } \
0531         }
0532         /* The body of the deallocator is here. */
0533 #define Py_TRASHCAN_END \
0534         if (_tstate) { \
0535             _PyTrash_end(_tstate); \
0536         } \
0537     } while (0);
0538 
0539 #define Py_TRASHCAN_BEGIN(op, dealloc) \
0540     Py_TRASHCAN_BEGIN_CONDITION((op), \
0541         _PyTrash_cond(_PyObject_CAST(op), (destructor)(dealloc)))
0542 
0543 /* The following two macros, Py_TRASHCAN_SAFE_BEGIN and
0544  * Py_TRASHCAN_SAFE_END, are deprecated since version 3.11 and
0545  * will be removed in the future.
0546  * Use Py_TRASHCAN_BEGIN and Py_TRASHCAN_END instead.
0547  */
0548 Py_DEPRECATED(3.11) typedef int UsingDeprecatedTrashcanMacro;
0549 #define Py_TRASHCAN_SAFE_BEGIN(op) \
0550     do { \
0551         UsingDeprecatedTrashcanMacro cond=1; \
0552         Py_TRASHCAN_BEGIN_CONDITION((op), cond);
0553 #define Py_TRASHCAN_SAFE_END(op) \
0554         Py_TRASHCAN_END; \
0555     } while(0);
0556 
0557 PyAPI_FUNC(void *) PyObject_GetItemData(PyObject *obj);
0558 
0559 PyAPI_FUNC(int) _PyObject_VisitManagedDict(PyObject *obj, visitproc visit, void *arg);
0560 PyAPI_FUNC(void) _PyObject_ClearManagedDict(PyObject *obj);
0561 
0562 #define TYPE_MAX_WATCHERS 8
0563 
0564 typedef int(*PyType_WatchCallback)(PyTypeObject *);
0565 PyAPI_FUNC(int) PyType_AddWatcher(PyType_WatchCallback callback);
0566 PyAPI_FUNC(int) PyType_ClearWatcher(int watcher_id);
0567 PyAPI_FUNC(int) PyType_Watch(int watcher_id, PyObject *type);
0568 PyAPI_FUNC(int) PyType_Unwatch(int watcher_id, PyObject *type);
0569 
0570 /* Attempt to assign a version tag to the given type.
0571  *
0572  * Returns 1 if the type already had a valid version tag or a new one was
0573  * assigned, or 0 if a new tag could not be assigned.
0574  */
0575 PyAPI_FUNC(int) PyUnstable_Type_AssignVersionTag(PyTypeObject *type);