blob: eb887f4c6eb50d258d1cd2dc5de8db5d958fd8a6 [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
30contains a pointer to itself!).
31
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)
Tim Peters4be93d02002-07-07 19:59:50 +000057#define Py_REF_DEBUG
58#endif
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059
Martin v. Löwis4d0d4712010-12-03 20:14:31 +000060#if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG)
61#error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG
62#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
77#define _PyObject_HEAD_EXTRA
78#define _PyObject_EXTRA_INIT
79#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))
113
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000115 PyObject ob_base;
116 Py_ssize_t ob_size; /* Number of items in variable part */
Guido van Rossumd0c87ee1997-05-15 21:31:03 +0000117} PyVarObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000118
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100119/* Cast argument to PyVarObject* type. */
120#define _PyVarObject_CAST(op) ((PyVarObject*)(op))
121
122#define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt)
123#define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type)
124#define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000125
Victor Stinnerc86a1122020-02-07 01:24:29 +0100126static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) {
127 ob->ob_refcnt = refcnt;
128}
129#define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt)
130
Victor Stinnerd2ec81a2020-02-07 09:17:07 +0100131static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) {
132 ob->ob_type = type;
133}
134#define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type)
135
136
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000137/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138Type objects contain a string containing the type name (to help somewhat
Tim Peters4be93d02002-07-07 19:59:50 +0000139in debugging), the allocation parameters (see PyObject_New() and
140PyObject_NewVar()),
141and methods for accessing objects of the type. Methods are optional, a
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142nil pointer meaning that particular kind of access is not available for
Guido van Rossumcaa63801995-01-12 11:45:45 +0000143this type. The Py_DECREF() macro uses the tp_dealloc method without
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000144checking for a nil pointer; it should always be implemented except if
145the implementation can guarantee that the reference count will never
Tim Peters4be93d02002-07-07 19:59:50 +0000146reach zero (e.g., for statically allocated type objects).
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147
148NB: the methods for certain type groups are now contained in separate
149method blocks.
150*/
151
Tim Peters9ace6bc2000-07-08 00:32:04 +0000152typedef PyObject * (*unaryfunc)(PyObject *);
153typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
154typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
155typedef int (*inquiry)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000156typedef Py_ssize_t (*lenfunc)(PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000157typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
158typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000159typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
160typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000161typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
Martin v. Löwis18e16552006-02-15 17:27:45 +0000162
Tim Peters9ace6bc2000-07-08 00:32:04 +0000163typedef int (*objobjproc)(PyObject *, PyObject *);
164typedef int (*visitproc)(PyObject *, void *);
165typedef int (*traverseproc)(PyObject *, visitproc, void *);
Guido van Rossumb6775db1994-08-01 11:34:53 +0000166
Guido van Rossumfdf95dd1997-05-05 22:15:02 +0000167
Neil Schemenauerf6d1ea12002-04-12 01:57:06 +0000168typedef void (*freefunc)(void *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000169typedef void (*destructor)(PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000170typedef PyObject *(*getattrfunc)(PyObject *, char *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000171typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
Martin v. Löwis15e62742006-02-27 16:46:16 +0000172typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000173typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
Tim Peters9ace6bc2000-07-08 00:32:04 +0000174typedef PyObject *(*reprfunc)(PyObject *);
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000175typedef Py_hash_t (*hashfunc)(PyObject *);
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000176typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
Guido van Rossum59d1d2b2001-04-20 19:13:02 +0000177typedef PyObject *(*getiterfunc) (PyObject *);
Guido van Rossum213c7a62001-04-23 14:08:49 +0000178typedef PyObject *(*iternextfunc) (PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000179typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
180typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
181typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
Victor Stinner0e4e7352020-02-05 15:10:39 +0100182typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *);
183typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000184
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000185typedef struct{
186 int slot; /* slot id, see below */
187 void *pfunc; /* function pointer */
188} PyType_Slot;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000190typedef struct{
191 const char* name;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000192 int basicsize;
193 int itemsize;
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100194 unsigned int flags;
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000195 PyType_Slot *slots; /* terminated by slot==0. */
196} PyType_Spec;
197
198PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*);
Martin v. Löwis9c564092012-06-23 23:20:45 +0200199#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
200PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*);
201#endif
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100202#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000
Victor Stinner0e4e7352020-02-05 15:10:39 +0100203PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int);
Martin v. Löwisca7b0462014-02-04 09:33:05 +0100204#endif
Martin v. Löwis4d0d4712010-12-03 20:14:31 +0000205
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206/* Generic type check */
Victor Stinner0e4e7352020-02-05 15:10:39 +0100207PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208#define PyObject_TypeCheck(ob, tp) \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000209 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp)))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210
Victor Stinner0e4e7352020-02-05 15:10:39 +0100211PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */
212PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */
213PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000214
Victor Stinner0e4e7352020-02-05 15:10:39 +0100215PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*);
Martin v. Löwis738236d2011-02-05 20:35:29 +0000216
Victor Stinner0e4e7352020-02-05 15:10:39 +0100217PyAPI_FUNC(int) PyType_Ready(PyTypeObject *);
218PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t);
219PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000220 PyObject *, PyObject *);
Christian Heimes26855632008-01-27 23:50:43 +0000221PyAPI_FUNC(unsigned int) PyType_ClearCache(void);
Victor Stinner0e4e7352020-02-05 15:10:39 +0100222PyAPI_FUNC(void) PyType_Modified(PyTypeObject *);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223
Guido van Rossum3f5da241990-12-20 15:06:42 +0000224/* Generic operations on objects */
Mark Hammonda2905272002-07-29 13:42:14 +0000225PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *);
226PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *);
Georg Brandl559e5d72008-06-11 18:37:52 +0000227PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *);
Benjamin Petersonc15a0732008-08-26 16:46:47 +0000228PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000229PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
230PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
Jeremy Hyltonaf68c872005-12-10 18:50:16 +0000231PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *);
232PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *);
233PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *);
Mark Hammonda2905272002-07-29 13:42:14 +0000234PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *);
235PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *);
236PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *);
Raymond Hettinger1da1dbf2003-03-17 19:46:11 +0000237PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000238PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *);
Victor Stinnerf58bd7c2020-02-05 13:12:19 +0100239PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200240#if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000
Benjamin Peterson8eb12692012-02-19 19:59:10 -0500241PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *);
Serhiy Storchaka34d0ac82016-12-27 14:57:39 +0200242#endif
Benjamin Peterson8f67d082010-10-17 20:54:53 +0000243PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *);
244PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000245PyAPI_FUNC(int) PyObject_IsTrue(PyObject *);
246PyAPI_FUNC(int) PyObject_Not(PyObject *);
247PyAPI_FUNC(int) PyCallable_Check(PyObject *);
Mark Hammonda2905272002-07-29 13:42:14 +0000248PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *);
Guido van Rossumab3b0342001-09-18 20:38:53 +0000249
Georg Brandl1a3284e2007-12-02 09:40:06 +0000250/* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a
251 list of strings. PyObject_Dir(NULL) is like builtins.dir(),
Tim Peters7eea37e2001-09-04 22:08:56 +0000252 returning the names of the current locals. In this case, if there are
253 no current locals, NULL is returned, and PyErr_Occurred() is false.
254*/
Mark Hammonda2905272002-07-29 13:42:14 +0000255PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *);
Tim Peters7eea37e2001-09-04 22:08:56 +0000256
257
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000258/* Helpers for printing recursive container types */
Mark Hammonda2905272002-07-29 13:42:14 +0000259PyAPI_FUNC(int) Py_ReprEnter(PyObject *);
260PyAPI_FUNC(void) Py_ReprLeave(PyObject *);
Guido van Rossum26d4ac31998-04-10 22:32:24 +0000261
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262/* Flag bits for printing: */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263#define Py_PRINT_RAW 1 /* No string quotes etc. */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000264
265/*
Antoine Pitrouada319b2019-05-29 22:12:38 +0200266Type flags (tp_flags)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000267
Antoine Pitrouada319b2019-05-29 22:12:38 +0200268These flags are used to change expected features and behavior for a
269particular type.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000270
271Arbitration of the flag bit positions will need to be coordinated among
luzpaza5293b42017-11-05 07:37:50 -0600272all extension writers who publicly release their extensions (this will
Antoine Pitrouada319b2019-05-29 22:12:38 +0200273be fewer than you might expect!).
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000274
Guido van Rossum3cf5b1e2006-07-27 21:53:35 +0000275Most flags were removed as of Python 3.0 to make room for new flags. (Some
276flags are not for backwards compatibility but to indicate the presence of an
277optional feature; these flags remain of course.)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000278
279Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value.
280
281Code can use PyType_HasFeature(type_ob, flag_value) to test whether the
282given type object has a specified feature.
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000283*/
284
Tim Peters6d6c1a32001-08-02 04:15:00 +0000285/* Set if the type object is dynamically allocated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100286#define Py_TPFLAGS_HEAPTYPE (1UL << 9)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000287
288/* Set if the type allows subclassing */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100289#define Py_TPFLAGS_BASETYPE (1UL << 10)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000290
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200291/* Set if the type implements the vectorcall protocol (PEP 590) */
292#ifndef Py_LIMITED_API
Petr Viktorin3f563ce2020-02-06 15:48:27 +0100293#define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11)
294// Backwards compatibility alias for API that was provisional in Python 3.8
295#define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL
Jeroen Demeyeraacc77f2019-05-29 20:31:52 +0200296#endif
297
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000298/* Set if the type is 'ready' -- fully initialized */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100299#define Py_TPFLAGS_READY (1UL << 12)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000300
301/* Set while the type is being 'readied', to prevent recursive ready calls */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100302#define Py_TPFLAGS_READYING (1UL << 13)
Guido van Rossum9b9c9722001-08-10 17:37:02 +0000303
Antoine Pitrouada319b2019-05-29 22:12:38 +0200304/* Objects support garbage collection (see objimpl.h) */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100305#define Py_TPFLAGS_HAVE_GC (1UL << 14)
Neil Schemenauer31ec1422001-08-29 23:46:35 +0000306
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000307/* These two bits are preserved for Stackless Python, next after this is 17 */
Christian Tismer6695ba82003-05-20 15:14:31 +0000308#ifdef STACKLESS
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100309#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15)
Christian Tismer6695ba82003-05-20 15:14:31 +0000310#else
Christian Tismerc26ff412003-05-23 03:33:35 +0000311#define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0
Christian Tismer6695ba82003-05-20 15:14:31 +0000312#endif
313
Jeroen Demeyereb65e242019-05-28 14:42:53 +0200314/* Objects behave like an unbound method */
315#define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17)
316
Christian Heimesa62da1d2008-01-12 19:39:10 +0000317/* Objects support type attribute cache */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100318#define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18)
319#define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19)
Christian Heimesa62da1d2008-01-12 19:39:10 +0000320
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000321/* Type is abstract and cannot be instantiated */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100322#define Py_TPFLAGS_IS_ABSTRACT (1UL << 20)
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000323
Thomas Wouters27d517b2007-02-25 20:39:11 +0000324/* These flags are used to determine if a type is a subclass. */
Victor Stinner4ca1cf32012-10-30 23:40:45 +0100325#define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24)
326#define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25)
327#define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26)
328#define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27)
329#define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28)
330#define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29)
331#define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30)
332#define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31)
Thomas Wouters27d517b2007-02-25 20:39:11 +0000333
Guido van Rossumbacca542001-01-24 22:13:48 +0000334#define Py_TPFLAGS_DEFAULT ( \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000335 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \
336 Py_TPFLAGS_HAVE_VERSION_TAG | \
337 0)
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000338
Antoine Pitrou796564c2013-07-30 19:59:21 +0200339/* NOTE: The following flags reuse lower bits (removed as part of the
340 * Python 3.0 transition). */
341
Hansraj Das10e5c662019-07-06 03:07:15 +0530342/* The following flag is kept for compatibility. Starting with 3.8,
343 * binary compatibility of C extensions across feature releases of
Antoine Pitrouada319b2019-05-29 22:12:38 +0200344 * Python is not supported anymore, except when using the stable ABI.
345 */
346
Antoine Pitrou796564c2013-07-30 19:59:21 +0200347/* Type structure has tp_finalize member (3.4) */
348#define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0)
349
Guido van Rossum36eef3c1998-10-08 02:10:56 +0000350
351/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000352The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement
Tim Peters4be93d02002-07-07 19:59:50 +0000353reference counts. Py_DECREF calls the object's deallocator function when
354the refcount falls to 0; for
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000355objects that don't contain references to other objects or heap memory
356this can be the standard function free(). Both macros can be used
Tim Peters4be93d02002-07-07 19:59:50 +0000357wherever a void expression is allowed. The argument must not be a
Benjamin Peterson2a691a82008-03-31 01:51:45 +0000358NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead.
Tim Peters4be93d02002-07-07 19:59:50 +0000359The macro _Py_NewReference(op) initialize reference counts to 1, and
360in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional
361bookkeeping appropriate to the special build.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000362
363We assume that the reference count field can never overflow; this can
Tim Peters4be93d02002-07-07 19:59:50 +0000364be proven when the size of the field is the same as the pointer size, so
365we ignore the possibility. Provided a C int is at least 32 bits (which
366is implicitly assumed in many parts of this code), that's enough for
367about 2**31 references to an object.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368
Tim Peters4be93d02002-07-07 19:59:50 +0000369XXX The following became out of date in Python 2.2, but I'm not sure
370XXX what the full truth is now. Certainly, heap-allocated type objects
371XXX can and should be deallocated.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000372Type objects should never be deallocated; the type pointer in an object
373is not considered to be a reference to the type object, to save
374complications in the deallocation function. (This is actually a
375decision that's up to the implementer of each new type so if you want,
376you can count such references to the type object.)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000377*/
378
Tim Peters4be93d02002-07-07 19:59:50 +0000379#ifdef Py_REF_DEBUG
Neal Norwitz4281cef2006-03-04 19:58:13 +0000380PyAPI_DATA(Py_ssize_t) _Py_RefTotal;
Victor Stinner18618e652018-10-25 17:28:11 +0200381PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno,
382 PyObject *op);
Tim Peters57f4ddd2002-07-10 06:34:15 +0000383#endif /* Py_REF_DEBUG */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000384
Victor Stinner3c09dca2018-10-30 14:48:26 +0100385PyAPI_FUNC(void) _Py_Dealloc(PyObject *);
386
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100387static inline void _Py_INCREF(PyObject *op)
388{
Victor Stinner49932fe2020-02-03 17:55:05 +0100389#ifdef Py_REF_DEBUG
390 _Py_RefTotal++;
391#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100392 op->ob_refcnt++;
393}
394
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100395#define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op))
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100396
Victor Stinnerf3a0a6b2020-01-08 21:03:45 +0100397static inline void _Py_DECREF(
398#ifdef Py_REF_DEBUG
399 const char *filename, int lineno,
400#endif
401 PyObject *op)
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100402{
Victor Stinner49932fe2020-02-03 17:55:05 +0100403#ifdef Py_REF_DEBUG
404 _Py_RefTotal--;
405#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100406 if (--op->ob_refcnt != 0) {
407#ifdef Py_REF_DEBUG
408 if (op->ob_refcnt < 0) {
409 _Py_NegativeRefcount(filename, lineno, op);
410 }
411#endif
412 }
413 else {
414 _Py_Dealloc(op);
415 }
416}
417
Victor Stinnerf3a0a6b2020-01-08 21:03:45 +0100418#ifdef Py_REF_DEBUG
419# define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op))
420#else
421# define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op))
422#endif
Victor Stinner2aaf0c12018-10-29 13:43:07 +0100423
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000425/* Safely decref `op` and set `op` to NULL, especially useful in tp_clear
Berker Peksag4882cac2015-04-14 09:30:01 +0300426 * and tp_dealloc implementations.
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000427 *
428 * Note that "the obvious" code can be deadly:
429 *
430 * Py_XDECREF(op);
431 * op = NULL;
432 *
433 * Typically, `op` is something like self->containee, and `self` is done
434 * using its `containee` member. In the code sequence above, suppose
435 * `containee` is non-NULL with a refcount of 1. Its refcount falls to
436 * 0 on the first line, which can trigger an arbitrary amount of code,
437 * possibly including finalizers (like __del__ methods or weakref callbacks)
438 * coded in Python, which in turn can release the GIL and allow other threads
439 * to run, etc. Such code may even invoke methods of `self` again, or cause
440 * cyclic gc to trigger, but-- oops! --self->containee still points to the
441 * object being torn down, and it may be in an insane state while being torn
442 * down. This has in fact been a rich historic source of miserable (rare &
443 * hard-to-diagnose) segfaulting (and other) bugs.
444 *
445 * The safe way is:
446 *
447 * Py_CLEAR(op);
448 *
449 * That arranges to set `op` to NULL _before_ decref'ing, so that any code
450 * triggered as a side-effect of `op` getting torn down no longer believes
451 * `op` points to a valid object.
452 *
453 * There are cases where it's safe to use the naive code, but they're brittle.
454 * For example, if `op` points to a Python integer, you know that destroying
455 * one of those can't cause problems -- but in part that relies on that
456 * Python integers aren't currently weakly referencable. Best practice is
457 * to use Py_CLEAR() even if you can't think of a reason for why you need to.
458 */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459#define Py_CLEAR(op) \
460 do { \
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100461 PyObject *_py_tmp = _PyObject_CAST(op); \
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700462 if (_py_tmp != NULL) { \
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000463 (op) = NULL; \
464 Py_DECREF(_py_tmp); \
465 } \
466 } while (0)
Jim Fulton8c5aeaa2004-07-14 19:07:35 +0000467
Victor Stinner541497e2018-10-29 20:52:41 +0100468/* Function to use in case the object pointer can be NULL: */
469static inline void _Py_XINCREF(PyObject *op)
470{
471 if (op != NULL) {
472 Py_INCREF(op);
473 }
474}
Benjamin Petersonda5eb5a2013-05-27 14:46:14 -0700475
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100476#define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op))
Victor Stinner541497e2018-10-29 20:52:41 +0100477
478static inline void _Py_XDECREF(PyObject *op)
479{
480 if (op != NULL) {
481 Py_DECREF(op);
482 }
483}
484
Victor Stinner2ff8fb72018-11-22 02:57:29 +0100485#define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op))
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000486
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000487/*
Thomas Heller1328b522004-04-22 17:23:49 +0000488These are provided as conveniences to Python runtime embedders, so that
489they can have object code that is not dependent on Python compilation flags.
490*/
491PyAPI_FUNC(void) Py_IncRef(PyObject *);
492PyAPI_FUNC(void) Py_DecRef(PyObject *);
493
494/*
Guido van Rossumcaa63801995-01-12 11:45:45 +0000495_Py_NoneStruct is an object of undefined type which can be used in contexts
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000496where NULL (nil) is not suitable (since NULL often means 'error').
497
Guido van Rossumcaa63801995-01-12 11:45:45 +0000498Don't forget to apply Py_INCREF() when returning this value!!!
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000499*/
Mark Hammonda2905272002-07-29 13:42:14 +0000500PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */
Guido van Rossumcaa63801995-01-12 11:45:45 +0000501#define Py_None (&_Py_NoneStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000502
Brett Cannond05235e2003-10-19 21:19:40 +0000503/* Macro for returning Py_None from a function */
Tim Peters5980ff22004-07-22 01:46:43 +0000504#define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None
Brett Cannond05235e2003-10-19 21:19:40 +0000505
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000506/*
507Py_NotImplemented is a singleton used to signal that an operation is
508not implemented for a given type combination.
509*/
Mark Hammonda2905272002-07-29 13:42:14 +0000510PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
Neil Schemenauera7ed6942001-01-04 01:31:50 +0000511#define Py_NotImplemented (&_Py_NotImplementedStruct)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000512
Brian Curtin7d2f9e12011-08-10 20:05:21 -0500513/* Macro for returning Py_NotImplemented from a function */
514#define Py_RETURN_NOTIMPLEMENTED \
515 return Py_INCREF(Py_NotImplemented), Py_NotImplemented
516
Guido van Rossum5f284ce2001-01-17 15:20:39 +0000517/* Rich comparison opcodes */
518#define Py_LT 0
519#define Py_LE 1
520#define Py_EQ 2
521#define Py_NE 3
522#define Py_GT 4
523#define Py_GE 5
524
stratakise8b19652017-11-02 11:32:54 +0100525/*
526 * Macro for implementing rich comparisons
527 *
528 * Needs to be a macro because any C-comparable type can be used.
529 */
530#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
531 do { \
532 switch (op) { \
533 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
534 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
535 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
536 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
537 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
538 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
539 default: \
540 Py_UNREACHABLE(); \
541 } \
542 } while (0)
543
Guido van Rossumb6775db1994-08-01 11:34:53 +0000544
545/*
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000546More conventions
547================
548
549Argument Checking
550-----------------
551
552Functions that take objects as arguments normally don't check for nil
553arguments, but they do check the type of the argument, and return an
554error if the function doesn't apply to the type.
555
556Failure Modes
557-------------
558
559Functions may fail for a variety of reasons, including running out of
Guido van Rossum3f5da241990-12-20 15:06:42 +0000560memory. This is communicated to the caller in two ways: an error string
561is set (see errors.h), and the function result differs: functions that
562normally return a pointer return NULL for failure, functions returning
563an integer return -1 (which could be a legal return value too!), and
564other functions return 0 for success and -1 for failure.
Tim Peters4be93d02002-07-07 19:59:50 +0000565Callers should always check for errors before using the result. If
566an error was set, the caller must either explicitly clear it, or pass
567the error on to its caller.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000568
569Reference Counts
570----------------
571
572It takes a while to get used to the proper usage of reference counts.
573
574Functions that create an object set the reference count to 1; such new
Guido van Rossumcaa63801995-01-12 11:45:45 +0000575objects must be stored somewhere or destroyed again with Py_DECREF().
Alex Martelli721b7762003-11-09 16:38:39 +0000576Some functions that 'store' objects, such as PyTuple_SetItem() and
577PyList_SetItem(),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000578don't increment the reference count of the object, since the most
579frequent use is to store a fresh object. Functions that 'retrieve'
Alex Martelli721b7762003-11-09 16:38:39 +0000580objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also
Guido van Rossumcaa63801995-01-12 11:45:45 +0000581don't increment
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000582the reference count, since most frequently the object is only looked at
583quickly. Thus, to retrieve an object and store it again, the caller
Guido van Rossumcaa63801995-01-12 11:45:45 +0000584must call Py_INCREF() explicitly.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585
Alex Martelli721b7762003-11-09 16:38:39 +0000586NOTE: functions that 'consume' a reference count, like
587PyList_SetItem(), consume the reference even if the object wasn't
588successfully stored, to simplify error handling.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000589
590It seems attractive to make other functions that take an object as
Alex Martelli721b7762003-11-09 16:38:39 +0000591argument consume a reference count; however, this may quickly get
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000592confusing (even the current practice is already confusing). Consider
Guido van Rossumcaa63801995-01-12 11:45:45 +0000593it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000594times.
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000595*/
Guido van Rossuma3309961993-07-28 09:05:47 +0000596
Victor Stinner626bff82018-10-25 17:31:10 +0200597#ifndef Py_LIMITED_API
Victor Stinner6eb99662018-11-26 17:09:16 +0100598# define Py_CPYTHON_OBJECT_H
599# include "cpython/object.h"
600# undef Py_CPYTHON_OBJECT_H
Victor Stinner626bff82018-10-25 17:31:10 +0200601#endif
602
Victor Stinner509dd902020-02-05 14:24:17 +0100603
604static inline int
605PyType_HasFeature(PyTypeObject *type, unsigned long feature) {
606#ifdef Py_LIMITED_API
607 return ((PyType_GetFlags(type) & feature) != 0);
608#else
609 return ((type->tp_flags & feature) != 0);
610#endif
611}
612
613#define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag)
614
615static inline int _PyType_Check(PyObject *op) {
616 return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS);
617}
618#define PyType_Check(op) _PyType_Check(_PyObject_CAST(op))
619
620static inline int _PyType_CheckExact(PyObject *op) {
621 return (Py_TYPE(op) == &PyType_Type);
622}
623#define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op))
624
Guido van Rossuma3309961993-07-28 09:05:47 +0000625#ifdef __cplusplus
626}
627#endif
628#endif /* !Py_OBJECT_H */