xref: /third_party/python/Include/object.h (revision 7db96d56)
1#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
3#ifdef __cplusplus
4extern "C" {
5#endif
6
7
8/* Object and type object interface */
9
10/*
11Objects are structures allocated on the heap.  Special rules apply to
12the use of objects to ensure they are properly garbage-collected.
13Objects are never allocated statically or on the stack; they must be
14accessed through special macros and functions only.  (Type objects are
15exceptions to the first rule; the standard types are represented by
16statically initialized type objects, although work on type/class unification
17for Python 2.2 made it possible to have heap-allocated type objects too).
18
19An object has a 'reference count' that is increased or decreased when a
20pointer to the object is copied or deleted; when the reference count
21reaches zero there are no references to the object left and it can be
22removed from the heap.
23
24An object has a 'type' that determines what it represents and what kind
25of data it contains.  An object's type is fixed when it is created.
26Types themselves are represented as objects; an object contains a
27pointer to the corresponding type object.  The type itself has a type
28pointer pointing to the object representing the type 'type', which
29contains a pointer to itself!.
30
31Objects do not float around in memory; once allocated an object keeps
32the same size and address.  Objects that must hold variable-size data
33can contain pointers to variable-size parts of the object.  Not all
34objects of the same type have the same size; but the size cannot change
35after allocation.  (These restrictions are made so a reference to an
36object can be simply a pointer -- moving an object would require
37updating all the pointers, and changing an object's size would require
38moving it if there was another object right next to it.)
39
40Objects are always accessed through pointers of the type 'PyObject *'.
41The type 'PyObject' is a structure that only contains the reference count
42and the type pointer.  The actual memory allocated for an object
43contains other data that can only be accessed after casting the pointer
44to a pointer to a longer structure type.  This longer type must start
45with the reference count and type fields; the macro PyObject_HEAD should be
46used for this (to accommodate for future changes).  The implementation
47of a particular object type can cast the object pointer to the proper
48type and back.
49
50A standard interface exists for objects that contain an array of items
51whose size is determined when the object is allocated.
52*/
53
54/* Py_DEBUG implies Py_REF_DEBUG. */
55#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
56#  define Py_REF_DEBUG
57#endif
58
59#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
60#  error Py_LIMITED_API is incompatible with Py_TRACE_REFS
61#endif
62
63#ifdef Py_TRACE_REFS
64/* Define pointers to support a doubly-linked list of all live heap objects. */
65#define _PyObject_HEAD_EXTRA            \
66    PyObject *_ob_next;           \
67    PyObject *_ob_prev;
68
69#define _PyObject_EXTRA_INIT _Py_NULL, _Py_NULL,
70
71#else
72#  define _PyObject_HEAD_EXTRA
73#  define _PyObject_EXTRA_INIT
74#endif
75
76/* PyObject_HEAD defines the initial segment of every PyObject. */
77#define PyObject_HEAD                   PyObject ob_base;
78
79#define PyObject_HEAD_INIT(type)        \
80    { _PyObject_EXTRA_INIT              \
81    1, type },
82
83#define PyVarObject_HEAD_INIT(type, size)       \
84    { PyObject_HEAD_INIT(type) size },
85
86/* PyObject_VAR_HEAD defines the initial segment of all variable-size
87 * container objects.  These end with a declaration of an array with 1
88 * element, but enough space is malloc'ed so that the array actually
89 * has room for ob_size elements.  Note that ob_size is an element count,
90 * not necessarily a byte count.
91 */
92#define PyObject_VAR_HEAD      PyVarObject ob_base;
93#define Py_INVALID_SIZE (Py_ssize_t)-1
94
95/* Nothing is actually declared to be a PyObject, but every pointer to
96 * a Python object can be cast to a PyObject*.  This is inheritance built
97 * by hand.  Similarly every pointer to a variable-size Python object can,
98 * in addition, be cast to PyVarObject*.
99 */
100struct _object {
101    _PyObject_HEAD_EXTRA
102    Py_ssize_t ob_refcnt;
103    PyTypeObject *ob_type;
104};
105
106/* Cast argument to PyObject* type. */
107#define _PyObject_CAST(op) _Py_CAST(PyObject*, (op))
108
109typedef struct {
110    PyObject ob_base;
111    Py_ssize_t ob_size; /* Number of items in variable part */
112} PyVarObject;
113
114/* Cast argument to PyVarObject* type. */
115#define _PyVarObject_CAST(op) _Py_CAST(PyVarObject*, (op))
116
117
118// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
119PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
120#define Py_Is(x, y) ((x) == (y))
121
122
123static inline Py_ssize_t Py_REFCNT(PyObject *ob) {
124    return ob->ob_refcnt;
125}
126#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
127#  define Py_REFCNT(ob) Py_REFCNT(_PyObject_CAST(ob))
128#endif
129
130
131// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
132static inline PyTypeObject* Py_TYPE(PyObject *ob) {
133    return ob->ob_type;
134}
135#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
136#  define Py_TYPE(ob) Py_TYPE(_PyObject_CAST(ob))
137#endif
138
139// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
140static inline Py_ssize_t Py_SIZE(PyObject *ob) {
141    PyVarObject *var_ob = _PyVarObject_CAST(ob);
142    return var_ob->ob_size;
143}
144#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
145#  define Py_SIZE(ob) Py_SIZE(_PyObject_CAST(ob))
146#endif
147
148
149static inline int Py_IS_TYPE(PyObject *ob, PyTypeObject *type) {
150    return Py_TYPE(ob) == type;
151}
152#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
153#  define Py_IS_TYPE(ob, type) Py_IS_TYPE(_PyObject_CAST(ob), type)
154#endif
155
156
157static inline void Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
158    ob->ob_refcnt = refcnt;
159}
160#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
161#  define Py_SET_REFCNT(ob, refcnt) Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
162#endif
163
164
165static inline void Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
166    ob->ob_type = type;
167}
168#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
169#  define Py_SET_TYPE(ob, type) Py_SET_TYPE(_PyObject_CAST(ob), type)
170#endif
171
172
173static inline void Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
174    ob->ob_size = size;
175}
176#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
177#  define Py_SET_SIZE(ob, size) Py_SET_SIZE(_PyVarObject_CAST(ob), size)
178#endif
179
180
181/*
182Type objects contain a string containing the type name (to help somewhat
183in debugging), the allocation parameters (see PyObject_New() and
184PyObject_NewVar()),
185and methods for accessing objects of the type.  Methods are optional, a
186nil pointer meaning that particular kind of access is not available for
187this type.  The Py_DECREF() macro uses the tp_dealloc method without
188checking for a nil pointer; it should always be implemented except if
189the implementation can guarantee that the reference count will never
190reach zero (e.g., for statically allocated type objects).
191
192NB: the methods for certain type groups are now contained in separate
193method blocks.
194*/
195
196typedef PyObject * (*unaryfunc)(PyObject *);
197typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
198typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
199typedef int (*inquiry)(PyObject *);
200typedef Py_ssize_t (*lenfunc)(PyObject *);
201typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
202typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
203typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
204typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
205typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
206
207typedef int (*objobjproc)(PyObject *, PyObject *);
208typedef int (*visitproc)(PyObject *, void *);
209typedef int (*traverseproc)(PyObject *, visitproc, void *);
210
211
212typedef void (*freefunc)(void *);
213typedef void (*destructor)(PyObject *);
214typedef PyObject *(*getattrfunc)(PyObject *, char *);
215typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
216typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
217typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
218typedef PyObject *(*reprfunc)(PyObject *);
219typedef Py_hash_t (*hashfunc)(PyObject *);
220typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
221typedef PyObject *(*getiterfunc) (PyObject *);
222typedef PyObject *(*iternextfunc) (PyObject *);
223typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
224typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
225typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
226typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
227typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
228
229typedef struct{
230    int slot;    /* slot id, see below */
231    void *pfunc; /* function pointer */
232} PyType_Slot;
233
234typedef struct{
235    const char* name;
236    int basicsize;
237    int itemsize;
238    unsigned int flags;
239    PyType_Slot *slots; /* terminated by slot==0. */
240} PyType_Spec;
241
242PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
243#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
244PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
245#endif
246#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
247PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
248#endif
249#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
250PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
251PyAPI_FUNC(PyObject *) PyType_GetModule(PyTypeObject *);
252PyAPI_FUNC(void *) PyType_GetModuleState(PyTypeObject *);
253#endif
254#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030B0000
255PyAPI_FUNC(PyObject *) PyType_GetName(PyTypeObject *);
256PyAPI_FUNC(PyObject *) PyType_GetQualName(PyTypeObject *);
257#endif
258
259/* Generic type check */
260PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
261
262static inline int PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
263    return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
264}
265#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
266#  define PyObject_TypeCheck(ob, type) PyObject_TypeCheck(_PyObject_CAST(ob), type)
267#endif
268
269PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
270PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
271PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
272
273PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
274
275PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
276PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
277PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
278                                               PyObject *, PyObject *);
279PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
280PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
281
282/* Generic operations on objects */
283PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
284PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
285PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
286PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
287PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
288PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
289PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
290PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
291PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
292PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
293PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
294PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
295PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
296PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
297PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
298#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
299PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
300#endif
301PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
302PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
303PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
304PyAPI_FUNC(int) PyObject_Not(PyObject *);
305PyAPI_FUNC(int) PyCallable_Check(PyObject *);
306PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
307
308/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
309   list of strings.  PyObject_Dir(NULL) is like builtins.dir(),
310   returning the names of the current locals.  In this case, if there are
311   no current locals, NULL is returned, and PyErr_Occurred() is false.
312*/
313PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
314
315/* Pickle support. */
316#ifndef Py_LIMITED_API
317PyAPI_FUNC(PyObject *) _PyObject_GetState(PyObject *);
318#endif
319
320
321/* Helpers for printing recursive container types */
322PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
323PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
324
325/* Flag bits for printing: */
326#define Py_PRINT_RAW    1       /* No string quotes etc. */
327
328/*
329Type flags (tp_flags)
330
331These flags are used to change expected features and behavior for a
332particular type.
333
334Arbitration of the flag bit positions will need to be coordinated among
335all extension writers who publicly release their extensions (this will
336be fewer than you might expect!).
337
338Most flags were removed as of Python 3.0 to make room for new flags.  (Some
339flags are not for backwards compatibility but to indicate the presence of an
340optional feature; these flags remain of course.)
341
342Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
343
344Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
345given type object has a specified feature.
346*/
347
348#ifndef Py_LIMITED_API
349
350/* Placement of dict (and values) pointers are managed by the VM, not by the type.
351 * The VM will automatically set tp_dictoffset. Should not be used for variable sized
352 * classes, such as classes that extend tuple.
353 */
354#define Py_TPFLAGS_MANAGED_DICT (1 << 4)
355
356/* Set if instances of the type object are treated as sequences for pattern matching */
357#define Py_TPFLAGS_SEQUENCE (1 << 5)
358/* Set if instances of the type object are treated as mappings for pattern matching */
359#define Py_TPFLAGS_MAPPING (1 << 6)
360#endif
361
362/* Disallow creating instances of the type: set tp_new to NULL and don't create
363 * the "__new__" key in the type dictionary. */
364#define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
365
366/* Set if the type object is immutable: type attributes cannot be set nor deleted */
367#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
368
369/* Set if the type object is dynamically allocated */
370#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
371
372/* Set if the type allows subclassing */
373#define Py_TPFLAGS_BASETYPE (1UL << 10)
374
375/* Set if the type implements the vectorcall protocol (PEP 590) */
376#ifndef Py_LIMITED_API
377#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
378// Backwards compatibility alias for API that was provisional in Python 3.8
379#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
380#endif
381
382/* Set if the type is 'ready' -- fully initialized */
383#define Py_TPFLAGS_READY (1UL << 12)
384
385/* Set while the type is being 'readied', to prevent recursive ready calls */
386#define Py_TPFLAGS_READYING (1UL << 13)
387
388/* Objects support garbage collection (see objimpl.h) */
389#define Py_TPFLAGS_HAVE_GC (1UL << 14)
390
391/* These two bits are preserved for Stackless Python, next after this is 17 */
392#ifdef STACKLESS
393#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
394#else
395#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
396#endif
397
398/* Objects behave like an unbound method */
399#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
400
401/* Object has up-to-date type attribute cache */
402#define Py_TPFLAGS_VALID_VERSION_TAG  (1UL << 19)
403
404/* Type is abstract and cannot be instantiated */
405#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
406
407// This undocumented flag gives certain built-ins their unique pattern-matching
408// behavior, which allows a single positional subpattern to match against the
409// subject itself (rather than a mapped attribute on it):
410#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
411
412/* These flags are used to determine if a type is a subclass. */
413#define Py_TPFLAGS_LONG_SUBCLASS        (1UL << 24)
414#define Py_TPFLAGS_LIST_SUBCLASS        (1UL << 25)
415#define Py_TPFLAGS_TUPLE_SUBCLASS       (1UL << 26)
416#define Py_TPFLAGS_BYTES_SUBCLASS       (1UL << 27)
417#define Py_TPFLAGS_UNICODE_SUBCLASS     (1UL << 28)
418#define Py_TPFLAGS_DICT_SUBCLASS        (1UL << 29)
419#define Py_TPFLAGS_BASE_EXC_SUBCLASS    (1UL << 30)
420#define Py_TPFLAGS_TYPE_SUBCLASS        (1UL << 31)
421
422#define Py_TPFLAGS_DEFAULT  ( \
423                 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
424                0)
425
426/* NOTE: Some of the following flags reuse lower bits (removed as part of the
427 * Python 3.0 transition). */
428
429/* The following flags are kept for compatibility; in previous
430 * versions they indicated presence of newer tp_* fields on the
431 * type struct.
432 * Starting with 3.8, binary compatibility of C extensions across
433 * feature releases of Python is not supported anymore (except when
434 * using the stable ABI, in which all classes are created dynamically,
435 * using the interpreter's memory layout.)
436 * Note that older extensions using the stable ABI set these flags,
437 * so the bits must not be repurposed.
438 */
439#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
440#define Py_TPFLAGS_HAVE_VERSION_TAG   (1UL << 18)
441
442
443/*
444The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
445reference counts.  Py_DECREF calls the object's deallocator function when
446the refcount falls to 0; for
447objects that don't contain references to other objects or heap memory
448this can be the standard function free().  Both macros can be used
449wherever a void expression is allowed.  The argument must not be a
450NULL pointer.  If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
451The macro _Py_NewReference(op) initialize reference counts to 1, and
452in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
453bookkeeping appropriate to the special build.
454
455We assume that the reference count field can never overflow; this can
456be proven when the size of the field is the same as the pointer size, so
457we ignore the possibility.  Provided a C int is at least 32 bits (which
458is implicitly assumed in many parts of this code), that's enough for
459about 2**31 references to an object.
460
461XXX The following became out of date in Python 2.2, but I'm not sure
462XXX what the full truth is now.  Certainly, heap-allocated type objects
463XXX can and should be deallocated.
464Type objects should never be deallocated; the type pointer in an object
465is not considered to be a reference to the type object, to save
466complications in the deallocation function.  (This is actually a
467decision that's up to the implementer of each new type so if you want,
468you can count such references to the type object.)
469*/
470
471#ifdef Py_REF_DEBUG
472PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
473PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
474                                      PyObject *op);
475#endif /* Py_REF_DEBUG */
476
477PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
478
479/*
480These are provided as conveniences to Python runtime embedders, so that
481they can have object code that is not dependent on Python compilation flags.
482*/
483PyAPI_FUNC(void) Py_IncRef(PyObject *);
484PyAPI_FUNC(void) Py_DecRef(PyObject *);
485
486// Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
487// Private functions used by Py_INCREF() and Py_DECREF().
488PyAPI_FUNC(void) _Py_IncRef(PyObject *);
489PyAPI_FUNC(void) _Py_DecRef(PyObject *);
490
491static inline void Py_INCREF(PyObject *op)
492{
493#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
494    // Stable ABI for Python 3.10 built in debug mode.
495    _Py_IncRef(op);
496#else
497    // Non-limited C API and limited C API for Python 3.9 and older access
498    // directly PyObject.ob_refcnt.
499#ifdef Py_REF_DEBUG
500    _Py_RefTotal++;
501#endif
502    op->ob_refcnt++;
503#endif
504}
505#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
506#  define Py_INCREF(op) Py_INCREF(_PyObject_CAST(op))
507#endif
508
509
510#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
511// Stable ABI for limited C API version 3.10 of Python debug build
512static inline void Py_DECREF(PyObject *op) {
513    _Py_DecRef(op);
514}
515#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
516
517#elif defined(Py_REF_DEBUG)
518static inline void Py_DECREF(const char *filename, int lineno, PyObject *op)
519{
520    _Py_RefTotal--;
521    if (--op->ob_refcnt != 0) {
522        if (op->ob_refcnt < 0) {
523            _Py_NegativeRefcount(filename, lineno, op);
524        }
525    }
526    else {
527        _Py_Dealloc(op);
528    }
529}
530#define Py_DECREF(op) Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
531
532#else
533static inline void Py_DECREF(PyObject *op)
534{
535    // Non-limited C API and limited C API for Python 3.9 and older access
536    // directly PyObject.ob_refcnt.
537    if (--op->ob_refcnt == 0) {
538        _Py_Dealloc(op);
539    }
540}
541#define Py_DECREF(op) Py_DECREF(_PyObject_CAST(op))
542#endif
543
544
545/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
546 * and tp_dealloc implementations.
547 *
548 * Note that "the obvious" code can be deadly:
549 *
550 *     Py_XDECREF(op);
551 *     op = NULL;
552 *
553 * Typically, `op` is something like self->containee, and `self` is done
554 * using its `containee` member.  In the code sequence above, suppose
555 * `containee` is non-NULL with a refcount of 1.  Its refcount falls to
556 * 0 on the first line, which can trigger an arbitrary amount of code,
557 * possibly including finalizers (like __del__ methods or weakref callbacks)
558 * coded in Python, which in turn can release the GIL and allow other threads
559 * to run, etc.  Such code may even invoke methods of `self` again, or cause
560 * cyclic gc to trigger, but-- oops! --self->containee still points to the
561 * object being torn down, and it may be in an insane state while being torn
562 * down.  This has in fact been a rich historic source of miserable (rare &
563 * hard-to-diagnose) segfaulting (and other) bugs.
564 *
565 * The safe way is:
566 *
567 *      Py_CLEAR(op);
568 *
569 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
570 * triggered as a side-effect of `op` getting torn down no longer believes
571 * `op` points to a valid object.
572 *
573 * There are cases where it's safe to use the naive code, but they're brittle.
574 * For example, if `op` points to a Python integer, you know that destroying
575 * one of those can't cause problems -- but in part that relies on that
576 * Python integers aren't currently weakly referencable.  Best practice is
577 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
578 */
579#define Py_CLEAR(op)                            \
580    do {                                        \
581        PyObject *_py_tmp = _PyObject_CAST(op); \
582        if (_py_tmp != NULL) {                  \
583            (op) = NULL;                        \
584            Py_DECREF(_py_tmp);                 \
585        }                                       \
586    } while (0)
587
588/* Function to use in case the object pointer can be NULL: */
589static inline void Py_XINCREF(PyObject *op)
590{
591    if (op != _Py_NULL) {
592        Py_INCREF(op);
593    }
594}
595#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
596#  define Py_XINCREF(op) Py_XINCREF(_PyObject_CAST(op))
597#endif
598
599static inline void Py_XDECREF(PyObject *op)
600{
601    if (op != _Py_NULL) {
602        Py_DECREF(op);
603    }
604}
605#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
606#  define Py_XDECREF(op) Py_XDECREF(_PyObject_CAST(op))
607#endif
608
609// Create a new strong reference to an object:
610// increment the reference count of the object and return the object.
611PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
612
613// Similar to Py_NewRef(), but the object can be NULL.
614PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
615
616static inline PyObject* _Py_NewRef(PyObject *obj)
617{
618    Py_INCREF(obj);
619    return obj;
620}
621
622static inline PyObject* _Py_XNewRef(PyObject *obj)
623{
624    Py_XINCREF(obj);
625    return obj;
626}
627
628// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
629// Names overridden with macros by static inline functions for best
630// performances.
631#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
632#  define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
633#  define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
634#else
635#  define Py_NewRef(obj) _Py_NewRef(obj)
636#  define Py_XNewRef(obj) _Py_XNewRef(obj)
637#endif
638
639
640/*
641_Py_NoneStruct is an object of undefined type which can be used in contexts
642where NULL (nil) is not suitable (since NULL often means 'error').
643
644Don't forget to apply Py_INCREF() when returning this value!!!
645*/
646PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
647#define Py_None (&_Py_NoneStruct)
648
649// Test if an object is the None singleton, the same as "x is None" in Python.
650PyAPI_FUNC(int) Py_IsNone(PyObject *x);
651#define Py_IsNone(x) Py_Is((x), Py_None)
652
653/* Macro for returning Py_None from a function */
654#define Py_RETURN_NONE return Py_NewRef(Py_None)
655
656/*
657Py_NotImplemented is a singleton used to signal that an operation is
658not implemented for a given type combination.
659*/
660PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
661#define Py_NotImplemented (&_Py_NotImplementedStruct)
662
663/* Macro for returning Py_NotImplemented from a function */
664#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
665
666/* Rich comparison opcodes */
667#define Py_LT 0
668#define Py_LE 1
669#define Py_EQ 2
670#define Py_NE 3
671#define Py_GT 4
672#define Py_GE 5
673
674#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
675/* Result of calling PyIter_Send */
676typedef enum {
677    PYGEN_RETURN = 0,
678    PYGEN_ERROR = -1,
679    PYGEN_NEXT = 1,
680} PySendResult;
681#endif
682
683/*
684 * Macro for implementing rich comparisons
685 *
686 * Needs to be a macro because any C-comparable type can be used.
687 */
688#define Py_RETURN_RICHCOMPARE(val1, val2, op)                               \
689    do {                                                                    \
690        switch (op) {                                                       \
691        case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
692        case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
693        case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
694        case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;   \
695        case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
696        case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE;  \
697        default:                                                            \
698            Py_UNREACHABLE();                                               \
699        }                                                                   \
700    } while (0)
701
702
703/*
704More conventions
705================
706
707Argument Checking
708-----------------
709
710Functions that take objects as arguments normally don't check for nil
711arguments, but they do check the type of the argument, and return an
712error if the function doesn't apply to the type.
713
714Failure Modes
715-------------
716
717Functions may fail for a variety of reasons, including running out of
718memory.  This is communicated to the caller in two ways: an error string
719is set (see errors.h), and the function result differs: functions that
720normally return a pointer return NULL for failure, functions returning
721an integer return -1 (which could be a legal return value too!), and
722other functions return 0 for success and -1 for failure.
723Callers should always check for errors before using the result.  If
724an error was set, the caller must either explicitly clear it, or pass
725the error on to its caller.
726
727Reference Counts
728----------------
729
730It takes a while to get used to the proper usage of reference counts.
731
732Functions that create an object set the reference count to 1; such new
733objects must be stored somewhere or destroyed again with Py_DECREF().
734Some functions that 'store' objects, such as PyTuple_SetItem() and
735PyList_SetItem(),
736don't increment the reference count of the object, since the most
737frequent use is to store a fresh object.  Functions that 'retrieve'
738objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
739don't increment
740the reference count, since most frequently the object is only looked at
741quickly.  Thus, to retrieve an object and store it again, the caller
742must call Py_INCREF() explicitly.
743
744NOTE: functions that 'consume' a reference count, like
745PyList_SetItem(), consume the reference even if the object wasn't
746successfully stored, to simplify error handling.
747
748It seems attractive to make other functions that take an object as
749argument consume a reference count; however, this may quickly get
750confusing (even the current practice is already confusing).  Consider
751it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
752times.
753*/
754
755#ifndef Py_LIMITED_API
756#  define Py_CPYTHON_OBJECT_H
757#  include "cpython/object.h"
758#  undef Py_CPYTHON_OBJECT_H
759#endif
760
761
762static inline int
763PyType_HasFeature(PyTypeObject *type, unsigned long feature)
764{
765    unsigned long flags;
766#ifdef Py_LIMITED_API
767    // PyTypeObject is opaque in the limited C API
768    flags = PyType_GetFlags(type);
769#else
770    flags = type->tp_flags;
771#endif
772    return ((flags & feature) != 0);
773}
774
775#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)
776
777static inline int PyType_Check(PyObject *op) {
778    return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
779}
780#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
781#  define PyType_Check(op) PyType_Check(_PyObject_CAST(op))
782#endif
783
784#define _PyType_CAST(op) \
785    (assert(PyType_Check(op)), _Py_CAST(PyTypeObject*, (op)))
786
787static inline int PyType_CheckExact(PyObject *op) {
788    return Py_IS_TYPE(op, &PyType_Type);
789}
790#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 < 0x030b0000
791#  define PyType_CheckExact(op) PyType_CheckExact(_PyObject_CAST(op))
792#endif
793
794#ifdef __cplusplus
795}
796#endif
797#endif   // !Py_OBJECT_H
798