blob: 9e6a8f4656af0e687e259e94cb439727cfa98ab5 [file] [log] [blame]
Guido van Rossuma3309961993-07-28 09:05:47 +00001#ifndef Py_OBJECT_H
2#define Py_OBJECT_H
Victor Stinner6279c1c2018-10-25 15:54:13 +02003
Guido van Rossuma3309961993-07-28 09:05:47 +00004#ifdef __cplusplus
5extern "C" {
6#endif
7
Guido van Rossumf70e43a1991-02-19 12:39:46 +00008
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00009/* Object and type object interface */
10
11/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000012Objects are structures allocated on the heap. Special rules apply to
13the use of objects to ensure they are properly garbage-collected.
14Objects are never allocated statically or on the stack; they must be
15accessed through special macros and functions only. (Type objects are
16exceptions to the first rule; the standard types are represented by
Tim Peters4be93d02002-07-07 19:59:50 +000017statically initialized type objects, although work on type/class unification
18for Python 2.2 made it possible to have heap-allocated type objects too).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019
20An object has a 'reference count' that is increased or decreased when a
21pointer to the object is copied or deleted; when the reference count
22reaches zero there are no references to the object left and it can be
23removed from the heap.
24
25An object has a 'type' that determines what it represents and what kind
26of data it contains. An object's type is fixed when it is created.
27Types themselves are represented as objects; an object contains a
28pointer to the corresponding type object. The type itself has a type
29pointer pointing to the object representing the type 'type', which
Jimmy Yangac2cfe62020-04-08 02:28:59 -070030contains a pointer to itself!.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031
32Objects do not float around in memory; once allocated an object keeps
33the same size and address. Objects that must hold variable-size data
34can contain pointers to variable-size parts of the object. Not all
35objects of the same type have the same size; but the size cannot change
36after allocation. (These restrictions are made so a reference to an
37object can be simply a pointer -- moving an object would require
38updating all the pointers, and changing an object's size would require
39moving it if there was another object right next to it.)
40
Guido van Rossumcaa63801995-01-12 11:45:45 +000041Objects are always accessed through pointers of the type 'PyObject *'.
42The type 'PyObject' is a structure that only contains the reference count
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000043and the type pointer. The actual memory allocated for an object
44contains other data that can only be accessed after casting the pointer
45to a pointer to a longer structure type. This longer type must start
Guido van Rossumcaa63801995-01-12 11:45:45 +000046with the reference count and type fields; the macro PyObject_HEAD should be
Thomas Wouters7e474022000-07-16 12:04:32 +000047used for this (to accommodate for future changes). The implementation
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048of a particular object type can cast the object pointer to the proper
49type and back.
50
51A standard interface exists for objects that contain an array of items
52whose size is determined when the object is allocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000053*/
54
Victor Stinnerf4e47032019-04-25 00:56:28 +020055/* Py_DEBUG implies Py_REF_DEBUG. */
56#if defined(Py_DEBUG) && !defined(Py_REF_DEBUG)
Victor Stinner3359cab2021-04-02 15:45:37 +020057# define Py_REF_DEBUG
Tim Peters4be93d02002-07-07 19:59:50 +000058#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
Victor Stinner3359cab2021-04-02 15:45:37 +020060#if defined(Py_LIMITED_API) && defined(Py_TRACE_REFS)
61# error Py_LIMITED_API is incompatible with Py_TRACE_REFS
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000062#endif
63
Victor Stinner0e4e7352020-02-05 15:10:39 +010064/* PyTypeObject structure is defined in cpython/object.h.
65 In Py_LIMITED_API, PyTypeObject is an opaque structure. */
66typedef struct _typeobject PyTypeObject;
Nick Coghland6009512014-11-20 21:39:37 +100067
Tim Peters4be93d02002-07-07 19:59:50 +000068#ifdef Py_TRACE_REFS
69/* Define pointers to support a doubly-linked list of all live heap objects. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000070#define _PyObject_HEAD_EXTRA \
71 struct _object *_ob_next; \
72 struct _object *_ob_prev;
Tim Peters4be93d02002-07-07 19:59:50 +000073
74#define _PyObject_EXTRA_INIT 0, 0,
75
76#else
Victor Stinner3359cab2021-04-02 15:45:37 +020077# define _PyObject_HEAD_EXTRA
78# define _PyObject_EXTRA_INIT
Tim Peters4be93d02002-07-07 19:59:50 +000079#endif
80
81/* PyObject_HEAD defines the initial segment of every PyObject. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000082#define PyObject_HEAD PyObject ob_base;
Tim Peters4be93d02002-07-07 19:59:50 +000083
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000084#define PyObject_HEAD_INIT(type) \
85 { _PyObject_EXTRA_INIT \
86 1, type },
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000087
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000088#define PyVarObject_HEAD_INIT(type, size) \
89 { PyObject_HEAD_INIT(type) size },
Tim Peters4be93d02002-07-07 19:59:50 +000090
91/* PyObject_VAR_HEAD defines the initial segment of all variable-size
92 * container objects. These end with a declaration of an array with 1
93 * element, but enough space is malloc'ed so that the array actually
94 * has room for ob_size elements. Note that ob_size is an element count,
95 * not necessarily a byte count.
96 */
Martin v. Löwis9f2e3462007-07-21 17:22:18 +000097#define PyObject_VAR_HEAD PyVarObject ob_base;
Martin v. Löwis18e16552006-02-15 17:27:45 +000098#define Py_INVALID_SIZE (Py_ssize_t)-1
Tim Peters803526b2002-07-07 05:13:56 +000099
Tim Peters4be93d02002-07-07 19:59:50 +0000100/* Nothing is actually declared to be a PyObject, but every pointer to
101 * a Python object can be cast to a PyObject*. This is inheritance built
102 * by hand. Similarly every pointer to a variable-size Python object can,
103 * in addition, be cast to PyVarObject*.
104 */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000105typedef struct _object {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000106 _PyObject_HEAD_EXTRA
107 Py_ssize_t ob_refcnt;
Victor Stinner0e4e7352020-02-05 15:10:39 +0100108 PyTypeObject *ob_type;
Guido van Rossumcaa63801995-01-12 11:45:45 +0000109} PyObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100111/* Cast argument to PyObject* type. */
112#define _PyObject_CAST(op) ((PyObject*)(op))
Andy Lester8767ce92020-03-06 02:03:58 -0600113#define _PyObject_CAST_CONST(op) ((const PyObject*)(op))
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100114
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000116 PyObject ob_base;
117 Py_ssize_t ob_size; /* Number of items in variable part */
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000118} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100120/* Cast argument to PyVarObject* type. */
121#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200122#define _PyVarObject_CAST_CONST(op) ((const PyVarObject*)(op))
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100123
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200124
Victor Stinner09bbebe2021-04-11 00:17:39 +0200125// Test if the 'x' object is the 'y' object, the same as "x is y" in Python.
126PyAPI_FUNC(int) Py_Is(PyObject *x, PyObject *y);
127#define Py_Is(x, y) ((x) == (y))
128
129
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200130static inline Py_ssize_t _Py_REFCNT(const PyObject *ob) {
131 return ob->ob_refcnt;
132}
133#define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST_CONST(ob))
134
135
Victor Stinner0e2ac212020-11-18 18:48:06 +0100136// bpo-39573: The Py_SET_TYPE() function must be used to set an object type.
137#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200138
Victor Stinner0e2ac212020-11-18 18:48:06 +0100139// bpo-39573: The Py_SET_SIZE() function must be used to set an object size.
140#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
Dong-hee Naad3252b2020-05-26 01:52:54 +0900141
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200142
Andy Lester8767ce92020-03-06 02:03:58 -0600143static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) {
Miss Islington (bot)e6d28a12021-06-11 01:57:16 -0700144 // bpo-44378: Don't use Py_TYPE() since Py_TYPE() requires a non-const
145 // object.
146 return ob->ob_type == type;
Dong-hee Nad905df72020-02-14 02:37:17 +0900147}
Andy Lester8767ce92020-03-06 02:03:58 -0600148#define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type)
Dong-hee Nad905df72020-02-14 02:37:17 +0900149
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200150
Victor Stinnerc86a1122020-02-07 01:24:29 +0100151static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
152 ob->ob_refcnt = refcnt;
153}
154#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
155
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200156
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100157static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
158 ob->ob_type = type;
159}
160#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
161
Victor Stinnerfe2978b2020-05-27 14:55:10 +0200162
Brandt Bucher968dcd92020-02-13 09:34:45 -0800163static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) {
164 ob->ob_size = size;
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100165}
Brandt Bucher968dcd92020-02-13 09:34:45 -0800166#define Py_SET_SIZE(ob, size) _Py_SET_SIZE(_PyVarObject_CAST(ob), size)
Victor Stinnerb10dc3e2020-02-07 12:05:12 +0100167
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100168
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000169/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170Type objects contain a string containing the type name (to help somewhat
Tim Peters4be93d02002-07-07 19:59:50 +0000171in debugging), the allocation parameters (see PyObject_New() and
172PyObject_NewVar()),
173and methods for accessing objects of the type. Methods are optional, a
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000175this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176checking for a nil pointer; it should always be implemented except if
177the implementation can guarantee that the reference count will never
Tim Peters4be93d02002-07-07 19:59:50 +0000178reach zero (e.g., for statically allocated type objects).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179
180NB: the methods for certain type groups are now contained in separate
181method blocks.
182*/
183
Tim Peters9ace6bc2000-07-08 00:32:04 +0000184typedef PyObject * (*unaryfunc)(PyObject *);
185typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
186typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
187typedef int (*inquiry)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000188typedef Py_ssize_t (*lenfunc)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000189typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
190typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000191typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
192typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000193typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000194
Tim Peters9ace6bc2000-07-08 00:32:04 +0000195typedef int (*objobjproc)(PyObject *, PyObject *);
196typedef int (*visitproc)(PyObject *, void *);
197typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000198
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000199
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000200typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000201typedef void (*destructor)(PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000202typedef PyObject *(*getattrfunc)(PyObject *, char *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000203typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000204typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000205typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000206typedef PyObject *(*reprfunc)(PyObject *);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000207typedef Py_hash_t (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000208typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000209typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000210typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000211typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
212typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
213typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
Victor Stinner0e4e7352020-02-05 15:10:39 +0100214typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
215typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000217typedef struct{
218 int slot; /* slot id, see below */
219 void *pfunc; /* function pointer */
220} PyType_Slot;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000221
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000222typedef struct{
223 const char* name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000224 int basicsize;
225 int itemsize;
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100226 unsigned int flags;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000227 PyType_Slot *slots; /* terminated by slot==0. */
228} PyType_Spec;
229
230PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
Martin v. Löwis9c564092012-06-23 23:20:45 +0200231#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
232PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
233#endif
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100234#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
Victor Stinner0e4e7352020-02-05 15:10:39 +0100235PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100236#endif
Petr Viktorine1becf42020-05-07 15:39:59 +0200237#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000
238PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *);
239PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *);
240PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *);
241#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000242
Tim Peters6d6c1a32001-08-02 04:15:00 +0000243/* Generic type check */
Victor Stinner0e4e7352020-02-05 15:10:39 +0100244PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Erlend Egeberg Aasland4bb2a1e2021-02-15 17:19:24 +0100245
246static inline int _PyObject_TypeCheck(PyObject *ob, PyTypeObject *type) {
247 return Py_IS_TYPE(ob, type) || PyType_IsSubtype(Py_TYPE(ob), type);
248}
249#define PyObject_TypeCheck(ob, type) _PyObject_TypeCheck(_PyObject_CAST(ob), type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000250
Victor Stinner0e4e7352020-02-05 15:10:39 +0100251PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
252PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
253PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000254
Victor Stinner0e4e7352020-02-05 15:10:39 +0100255PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
Martin v. Löwis738236d2011-02-05 20:35:29 +0000256
Victor Stinner0e4e7352020-02-05 15:10:39 +0100257PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
258PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
259PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000260 PyObject *, PyObject *);
Christian Heimes26855632008-01-27 23:50:43 +0000261PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
Victor Stinner0e4e7352020-02-05 15:10:39 +0100262PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000263
Guido van Rossum3f5da241990-12-20 15:06:42 +0000264/* Generic operations on objects */
Mark Hammonda2905272002-07-29 13:42:14 +0000265PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
266PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
Georg Brandl559e5d72008-06-11 18:37:52 +0000267PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000268PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000269PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
270PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000271PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
272PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
273PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
Mark Hammonda2905272002-07-29 13:42:14 +0000274PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
275PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
276PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000277PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000278PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100279PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200280#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500281PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200282#endif
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000283PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
284PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000285PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
286PyAPI_FUNC(int) PyObject_Not(PyObject *);
287PyAPI_FUNC(int) PyCallable_Check(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000288PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
Guido van Rossumab3b0342001-09-18 20:38:53 +0000289
Georg Brandl1a3284e2007-12-02 09:40:06 +0000290/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
291 list of strings. PyObject_Dir(NULL) is like builtins.dir(),
Tim Peters7eea37e2001-09-04 22:08:56 +0000292 returning the names of the current locals. In this case, if there are
293 no current locals, NULL is returned, and PyErr_Occurred() is false.
294*/
Mark Hammonda2905272002-07-29 13:42:14 +0000295PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
Tim Peters7eea37e2001-09-04 22:08:56 +0000296
297
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000298/* Helpers for printing recursive container types */
Mark Hammonda2905272002-07-29 13:42:14 +0000299PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
300PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000301
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000302/* Flag bits for printing: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000303#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000304
305/*
Antoine Pitrouada319b2019-05-29 22:12:38 +0200306Type flags (tp_flags)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000307
Antoine Pitrouada319b2019-05-29 22:12:38 +0200308These flags are used to change expected features and behavior for a
309particular type.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000310
311Arbitration of the flag bit positions will need to be coordinated among
luzpaza5293b42017-11-05 07:37:50 -0600312all extension writers who publicly release their extensions (this will
Antoine Pitrouada319b2019-05-29 22:12:38 +0200313be fewer than you might expect!).
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000314
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000315Most flags were removed as of Python 3.0 to make room for new flags. (Some
316flags are not for backwards compatibility but to indicate the presence of an
317optional feature; these flags remain of course.)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000318
319Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
320
321Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
322given type object has a specified feature.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000323*/
324
Mark Shannon069e81a2021-04-30 09:50:28 +0100325#ifndef Py_LIMITED_API
326/* Set if instances of the type object are treated as sequences for pattern matching */
327#define Py_TPFLAGS_SEQUENCE (1 << 5)
328/* Set if instances of the type object are treated as mappings for pattern matching */
329#define Py_TPFLAGS_MAPPING (1 << 6)
330#endif
331
Victor Stinner3bb09942021-04-30 12:46:15 +0200332/* Disallow creating instances of the type: set tp_new to NULL and don't create
333 * the "__new__" key in the type dictionary. */
334#define Py_TPFLAGS_DISALLOW_INSTANTIATION (1UL << 7)
335
Erlend Egeberg Aasland3b52c8d2021-04-28 19:02:42 +0200336/* Set if the type object is immutable: type attributes cannot be set nor deleted */
337#define Py_TPFLAGS_IMMUTABLETYPE (1UL << 8)
338
Tim Peters6d6c1a32001-08-02 04:15:00 +0000339/* Set if the type object is dynamically allocated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100340#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000341
342/* Set if the type allows subclassing */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100343#define Py_TPFLAGS_BASETYPE (1UL << 10)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000344
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200345/* Set if the type implements the vectorcall protocol (PEP 590) */
346#ifndef Py_LIMITED_API
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100347#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
348// Backwards compatibility alias for API that was provisional in Python 3.8
349#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200350#endif
351
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000352/* Set if the type is 'ready' -- fully initialized */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100353#define Py_TPFLAGS_READY (1UL << 12)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000354
355/* Set while the type is being 'readied', to prevent recursive ready calls */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100356#define Py_TPFLAGS_READYING (1UL << 13)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000357
Antoine Pitrouada319b2019-05-29 22:12:38 +0200358/* Objects support garbage collection (see objimpl.h) */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100359#define Py_TPFLAGS_HAVE_GC (1UL << 14)
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000360
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000361/* These two bits are preserved for Stackless Python, next after this is 17 */
Christian Tismer6695ba82003-05-20 15:14:31 +0000362#ifdef STACKLESS
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100363#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
Christian Tismer6695ba82003-05-20 15:14:31 +0000364#else
Christian Tismerc26ff412003-05-23 03:33:35 +0000365#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
Christian Tismer6695ba82003-05-20 15:14:31 +0000366#endif
367
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200368/* Objects behave like an unbound method */
369#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
370
Miss Islington (bot)632e8a62021-07-23 07:56:53 -0700371/* Object has up-to-date type attribute cache */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100372#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
Christian Heimesa62da1d2008-01-12 19:39:10 +0000373
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000374/* Type is abstract and cannot be instantiated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100375#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000376
Brandt Bucher145bf262021-02-26 14:51:55 -0800377// This undocumented flag gives certain built-ins their unique pattern-matching
378// behavior, which allows a single positional subpattern to match against the
379// subject itself (rather than a mapped attribute on it):
380#define _Py_TPFLAGS_MATCH_SELF (1UL << 22)
381
Thomas Wouters27d517b2007-02-25 20:39:11 +0000382/* These flags are used to determine if a type is a subclass. */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100383#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
384#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
385#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
386#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
387#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
388#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
389#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
390#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
Thomas Wouters27d517b2007-02-25 20:39:11 +0000391
Guido van Rossumbacca542001-01-24 22:13:48 +0000392#define Py_TPFLAGS_DEFAULT ( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000393 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000394 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000395
Miss Islington (bot)632e8a62021-07-23 07:56:53 -0700396/* NOTE: Some of the following flags reuse lower bits (removed as part of the
Antoine Pitrou796564c2013-07-30 19:59:21 +0200397 * Python 3.0 transition). */
398
Miss Islington (bot)632e8a62021-07-23 07:56:53 -0700399/* The following flags are kept for compatibility; in previous
400 * versions they indicated presence of newer tp_* fields on the
401 * type struct.
402 * Starting with 3.8, binary compatibility of C extensions across
403 * feature releases of Python is not supported anymore (except when
404 * using the stable ABI, in which all classes are created dynamically,
405 * using the interpreter's memory layout.)
406 * Note that older extensions using the stable ABI set these flags,
407 * so the bits must not be repurposed.
Antoine Pitrouada319b2019-05-29 22:12:38 +0200408 */
Antoine Pitrou796564c2013-07-30 19:59:21 +0200409#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
Miss Islington (bot)632e8a62021-07-23 07:56:53 -0700410#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
Antoine Pitrou796564c2013-07-30 19:59:21 +0200411
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000412
413/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000414The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Tim Peters4be93d02002-07-07 19:59:50 +0000415reference counts. Py_DECREF calls the object's deallocator function when
416the refcount falls to 0; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000417objects that don't contain references to other objects or heap memory
418this can be the standard function free(). Both macros can be used
Tim Peters4be93d02002-07-07 19:59:50 +0000419wherever a void expression is allowed. The argument must not be a
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000420NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
Tim Peters4be93d02002-07-07 19:59:50 +0000421The macro _Py_NewReference(op) initialize reference counts to 1, and
422in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
423bookkeeping appropriate to the special build.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424
425We assume that the reference count field can never overflow; this can
Tim Peters4be93d02002-07-07 19:59:50 +0000426be proven when the size of the field is the same as the pointer size, so
427we ignore the possibility. Provided a C int is at least 32 bits (which
428is implicitly assumed in many parts of this code), that's enough for
429about 2**31 references to an object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000430
Tim Peters4be93d02002-07-07 19:59:50 +0000431XXX The following became out of date in Python 2.2, but I'm not sure
432XXX what the full truth is now. Certainly, heap-allocated type objects
433XXX can and should be deallocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000434Type objects should never be deallocated; the type pointer in an object
435is not considered to be a reference to the type object, to save
436complications in the deallocation function. (This is actually a
437decision that's up to the implementer of each new type so if you want,
438you can count such references to the type object.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000439*/
440
Tim Peters4be93d02002-07-07 19:59:50 +0000441#ifdef Py_REF_DEBUG
Neal Norwitz4281cef2006-03-04 19:58:13 +0000442PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
Victor Stinner18618e652018-10-25 17:28:11 +0200443PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
444 PyObject *op);
Tim Peters57f4ddd2002-07-10 06:34:15 +0000445#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000446
Victor Stinner3c09dca2018-10-30 14:48:26 +0100447PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
448
Victor Stinner3359cab2021-04-02 15:45:37 +0200449/*
450These are provided as conveniences to Python runtime embedders, so that
451they can have object code that is not dependent on Python compilation flags.
452*/
453PyAPI_FUNC(void) Py_IncRef(PyObject *);
454PyAPI_FUNC(void) Py_DecRef(PyObject *);
455
456// Similar to Py_IncRef() and Py_DecRef() but the argument must be non-NULL.
457// Private functions used by Py_INCREF() and Py_DECREF().
458PyAPI_FUNC(void) _Py_IncRef(PyObject *);
459PyAPI_FUNC(void) _Py_DecRef(PyObject *);
460
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100461static inline void _Py_INCREF(PyObject *op)
462{
Victor Stinner3359cab2021-04-02 15:45:37 +0200463#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
464 // Stable ABI for Python 3.10 built in debug mode.
465 _Py_IncRef(op);
466#else
467 // Non-limited C API and limited C API for Python 3.9 and older access
468 // directly PyObject.ob_refcnt.
Victor Stinner49932fe2020-02-03 17:55:05 +0100469#ifdef Py_REF_DEBUG
470 _Py_RefTotal++;
471#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100472 op->ob_refcnt++;
Victor Stinner3359cab2021-04-02 15:45:37 +0200473#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100474}
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100475#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100476
Victor Stinnerf3a0a6b2020-01-08 21:03:45 +0100477static inline void _Py_DECREF(
Victor Stinner3359cab2021-04-02 15:45:37 +0200478#if defined(Py_REF_DEBUG) && !(defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000)
Victor Stinnerf3a0a6b2020-01-08 21:03:45 +0100479 const char *filename, int lineno,
480#endif
481 PyObject *op)
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100482{
Victor Stinner3359cab2021-04-02 15:45:37 +0200483#if defined(Py_REF_DEBUG) && defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000
484 // Stable ABI for Python 3.10 built in debug mode.
485 _Py_DecRef(op);
486#else
487 // Non-limited C API and limited C API for Python 3.9 and older access
488 // directly PyObject.ob_refcnt.
Victor Stinner49932fe2020-02-03 17:55:05 +0100489#ifdef Py_REF_DEBUG
490 _Py_RefTotal--;
491#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100492 if (--op->ob_refcnt != 0) {
493#ifdef Py_REF_DEBUG
494 if (op->ob_refcnt < 0) {
495 _Py_NegativeRefcount(filename, lineno, op);
496 }
497#endif
498 }
499 else {
500 _Py_Dealloc(op);
501 }
Victor Stinner3359cab2021-04-02 15:45:37 +0200502#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100503}
Victor Stinner3359cab2021-04-02 15:45:37 +0200504#if defined(Py_REF_DEBUG) && !(defined(Py_LIMITED_API) && Py_LIMITED_API+0 >= 0x030A0000)
Victor Stinnerf3a0a6b2020-01-08 21:03:45 +0100505# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
506#else
507# define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op))
508#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100509
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000510
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000511/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Berker Peksag4882cac2015-04-14 09:30:01 +0300512 * and tp_dealloc implementations.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000513 *
514 * Note that "the obvious" code can be deadly:
515 *
516 * Py_XDECREF(op);
517 * op = NULL;
518 *
519 * Typically, `op` is something like self->containee, and `self` is done
520 * using its `containee` member. In the code sequence above, suppose
521 * `containee` is non-NULL with a refcount of 1. Its refcount falls to
522 * 0 on the first line, which can trigger an arbitrary amount of code,
523 * possibly including finalizers (like __del__ methods or weakref callbacks)
524 * coded in Python, which in turn can release the GIL and allow other threads
525 * to run, etc. Such code may even invoke methods of `self` again, or cause
526 * cyclic gc to trigger, but-- oops! --self->containee still points to the
527 * object being torn down, and it may be in an insane state while being torn
528 * down. This has in fact been a rich historic source of miserable (rare &
529 * hard-to-diagnose) segfaulting (and other) bugs.
530 *
531 * The safe way is:
532 *
533 * Py_CLEAR(op);
534 *
535 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
536 * triggered as a side-effect of `op` getting torn down no longer believes
537 * `op` points to a valid object.
538 *
539 * There are cases where it's safe to use the naive code, but they're brittle.
540 * For example, if `op` points to a Python integer, you know that destroying
541 * one of those can't cause problems -- but in part that relies on that
542 * Python integers aren't currently weakly referencable. Best practice is
543 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
544 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000545#define Py_CLEAR(op) \
546 do { \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100547 PyObject *_py_tmp = _PyObject_CAST(op); \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700548 if (_py_tmp != NULL) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000549 (op) = NULL; \
550 Py_DECREF(_py_tmp); \
551 } \
552 } while (0)
Jim Fulton8c5aeaa2004-07-14 19:07:35 +0000553
Victor Stinner541497e2018-10-29 20:52:41 +0100554/* Function to use in case the object pointer can be NULL: */
555static inline void _Py_XINCREF(PyObject *op)
556{
557 if (op != NULL) {
558 Py_INCREF(op);
559 }
560}
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700561
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100562#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
Victor Stinner541497e2018-10-29 20:52:41 +0100563
564static inline void _Py_XDECREF(PyObject *op)
565{
566 if (op != NULL) {
567 Py_DECREF(op);
568 }
569}
570
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100571#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000572
Victor Stinner23c5f932020-11-09 13:40:47 +0100573// Create a new strong reference to an object:
574// increment the reference count of the object and return the object.
Victor Stinner53a03aa2020-11-05 15:02:12 +0100575PyAPI_FUNC(PyObject*) Py_NewRef(PyObject *obj);
576
Victor Stinner23c5f932020-11-09 13:40:47 +0100577// Similar to Py_NewRef(), but the object can be NULL.
Victor Stinner53a03aa2020-11-05 15:02:12 +0100578PyAPI_FUNC(PyObject*) Py_XNewRef(PyObject *obj);
579
580static inline PyObject* _Py_NewRef(PyObject *obj)
581{
582 Py_INCREF(obj);
583 return obj;
584}
585
586static inline PyObject* _Py_XNewRef(PyObject *obj)
587{
588 Py_XINCREF(obj);
589 return obj;
590}
591
592// Py_NewRef() and Py_XNewRef() are exported as functions for the stable ABI.
593// Names overriden with macros by static inline functions for best
594// performances.
Victor Stinner8b6c4a92020-12-03 14:01:10 +0100595#define Py_NewRef(obj) _Py_NewRef(_PyObject_CAST(obj))
596#define Py_XNewRef(obj) _Py_XNewRef(_PyObject_CAST(obj))
Victor Stinner53a03aa2020-11-05 15:02:12 +0100597
598
Thomas Heller1328b522004-04-22 17:23:49 +0000599/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000600_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000601where NULL (nil) is not suitable (since NULL often means 'error').
602
Guido van Rossumcaa63801995-01-12 11:45:45 +0000603Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000604*/
Mark Hammonda2905272002-07-29 13:42:14 +0000605PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000606#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000607
Victor Stinner09bbebe2021-04-11 00:17:39 +0200608// Test if an object is the None singleton, the same as "x is None" in Python.
609PyAPI_FUNC(int) Py_IsNone(PyObject *x);
610#define Py_IsNone(x) Py_Is((x), Py_None)
611
Brett Cannond05235e2003-10-19 21:19:40 +0000612/* Macro for returning Py_None from a function */
Victor Stinner53a03aa2020-11-05 15:02:12 +0100613#define Py_RETURN_NONE return Py_NewRef(Py_None)
Brett Cannond05235e2003-10-19 21:19:40 +0000614
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000615/*
616Py_NotImplemented is a singleton used to signal that an operation is
617not implemented for a given type combination.
618*/
Mark Hammonda2905272002-07-29 13:42:14 +0000619PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000620#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000621
Brian Curtin7d2f9e12011-08-10 20:05:21 -0500622/* Macro for returning Py_NotImplemented from a function */
Victor Stinner53a03aa2020-11-05 15:02:12 +0100623#define Py_RETURN_NOTIMPLEMENTED return Py_NewRef(Py_NotImplemented)
Brian Curtin7d2f9e12011-08-10 20:05:21 -0500624
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000625/* Rich comparison opcodes */
626#define Py_LT 0
627#define Py_LE 1
628#define Py_EQ 2
629#define Py_NE 3
630#define Py_GT 4
631#define Py_GE 5
632
Vladimir Matveev1e996c32020-11-10 12:09:55 -0800633#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x030A0000
634/* Result of calling PyIter_Send */
635typedef enum {
636 PYGEN_RETURN = 0,
637 PYGEN_ERROR = -1,
638 PYGEN_NEXT = 1,
639} PySendResult;
640#endif
641
stratakise8b19652017-11-02 11:32:54 +0100642/*
643 * Macro for implementing rich comparisons
644 *
645 * Needs to be a macro because any C-comparable type can be used.
646 */
647#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
648 do { \
649 switch (op) { \
650 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
651 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
652 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
653 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
654 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
655 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
656 default: \
657 Py_UNREACHABLE(); \
658 } \
659 } while (0)
660
Guido van Rossumb6775db1994-08-01 11:34:53 +0000661
662/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000663More conventions
664================
665
666Argument Checking
667-----------------
668
669Functions that take objects as arguments normally don't check for nil
670arguments, but they do check the type of the argument, and return an
671error if the function doesn't apply to the type.
672
673Failure Modes
674-------------
675
676Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000677memory. This is communicated to the caller in two ways: an error string
678is set (see errors.h), and the function result differs: functions that
679normally return a pointer return NULL for failure, functions returning
680an integer return -1 (which could be a legal return value too!), and
681other functions return 0 for success and -1 for failure.
Tim Peters4be93d02002-07-07 19:59:50 +0000682Callers should always check for errors before using the result. If
683an error was set, the caller must either explicitly clear it, or pass
684the error on to its caller.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000685
686Reference Counts
687----------------
688
689It takes a while to get used to the proper usage of reference counts.
690
691Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000692objects must be stored somewhere or destroyed again with Py_DECREF().
Alex Martelli721b7762003-11-09 16:38:39 +0000693Some functions that 'store' objects, such as PyTuple_SetItem() and
694PyList_SetItem(),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695don't increment the reference count of the object, since the most
696frequent use is to store a fresh object. Functions that 'retrieve'
Alex Martelli721b7762003-11-09 16:38:39 +0000697objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
Guido van Rossumcaa63801995-01-12 11:45:45 +0000698don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000699the reference count, since most frequently the object is only looked at
700quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000701must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000702
Alex Martelli721b7762003-11-09 16:38:39 +0000703NOTE: functions that 'consume' a reference count, like
704PyList_SetItem(), consume the reference even if the object wasn't
705successfully stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000706
707It seems attractive to make other functions that take an object as
Alex Martelli721b7762003-11-09 16:38:39 +0000708argument consume a reference count; however, this may quickly get
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000709confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000710it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000711times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000712*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000713
Victor Stinner626bff82018-10-25 17:31:10 +0200714#ifndef Py_LIMITED_API
Victor Stinner6eb99662018-11-26 17:09:16 +0100715# define Py_CPYTHON_OBJECT_H
716# include "cpython/object.h"
717# undef Py_CPYTHON_OBJECT_H
Victor Stinner626bff82018-10-25 17:31:10 +0200718#endif
719
Victor Stinner509dd902020-02-05 14:24:17 +0100720
721static inline int
Victor Stinnerb26a0db2020-07-08 11:02:23 +0200722PyType_HasFeature(PyTypeObject *type, unsigned long feature)
723{
724 unsigned long flags;
725#ifdef Py_LIMITED_API
726 // PyTypeObject is opaque in the limited C API
727 flags = PyType_GetFlags(type);
728#else
729 flags = type->tp_flags;
730#endif
731 return ((flags & feature) != 0);
Victor Stinner509dd902020-02-05 14:24:17 +0100732}
733
734#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)
735
736static inline int _PyType_Check(PyObject *op) {
737 return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
738}
739#define PyType_Check(op) _PyType_Check(_PyObject_CAST(op))
740
741static inline int _PyType_CheckExact(PyObject *op) {
Dong-hee Nad905df72020-02-14 02:37:17 +0900742 return Py_IS_TYPE(op, &PyType_Type);
Victor Stinner509dd902020-02-05 14:24:17 +0100743}
744#define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op))
745
Guido van Rossuma3309961993-07-28 09:05:47 +0000746#ifdef __cplusplus
747}
748#endif
749#endif /* !Py_OBJECT_H */