Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_OBJECT_H |
| 2 | #define Py_OBJECT_H |
Victor Stinner | 6279c1c | 2018-10-25 15:54:13 +0200 | [diff] [blame] | 3 | |
| 4 | #include "pymem.h" /* _Py_tracemalloc_config */ |
| 5 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 6 | #ifdef __cplusplus |
| 7 | extern "C" { |
| 8 | #endif |
| 9 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 11 | /* Object and type object interface */ |
| 12 | |
| 13 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 14 | Objects are structures allocated on the heap. Special rules apply to |
| 15 | the use of objects to ensure they are properly garbage-collected. |
| 16 | Objects are never allocated statically or on the stack; they must be |
| 17 | accessed through special macros and functions only. (Type objects are |
| 18 | exceptions to the first rule; the standard types are represented by |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 19 | statically initialized type objects, although work on type/class unification |
| 20 | for Python 2.2 made it possible to have heap-allocated type objects too). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
| 22 | An object has a 'reference count' that is increased or decreased when a |
| 23 | pointer to the object is copied or deleted; when the reference count |
| 24 | reaches zero there are no references to the object left and it can be |
| 25 | removed from the heap. |
| 26 | |
| 27 | An object has a 'type' that determines what it represents and what kind |
| 28 | of data it contains. An object's type is fixed when it is created. |
| 29 | Types themselves are represented as objects; an object contains a |
| 30 | pointer to the corresponding type object. The type itself has a type |
| 31 | pointer pointing to the object representing the type 'type', which |
| 32 | contains a pointer to itself!). |
| 33 | |
| 34 | Objects do not float around in memory; once allocated an object keeps |
| 35 | the same size and address. Objects that must hold variable-size data |
| 36 | can contain pointers to variable-size parts of the object. Not all |
| 37 | objects of the same type have the same size; but the size cannot change |
| 38 | after allocation. (These restrictions are made so a reference to an |
| 39 | object can be simply a pointer -- moving an object would require |
| 40 | updating all the pointers, and changing an object's size would require |
| 41 | moving it if there was another object right next to it.) |
| 42 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 43 | Objects are always accessed through pointers of the type 'PyObject *'. |
| 44 | The type 'PyObject' is a structure that only contains the reference count |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 45 | and the type pointer. The actual memory allocated for an object |
| 46 | contains other data that can only be accessed after casting the pointer |
| 47 | to a pointer to a longer structure type. This longer type must start |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 48 | with the reference count and type fields; the macro PyObject_HEAD should be |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 49 | used for this (to accommodate for future changes). The implementation |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | of a particular object type can cast the object pointer to the proper |
| 51 | type and back. |
| 52 | |
| 53 | A standard interface exists for objects that contain an array of items |
| 54 | whose size is determined when the object is allocated. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | */ |
| 56 | |
Victor Stinner | f4e4703 | 2019-04-25 00:56:28 +0200 | [diff] [blame] | 57 | /* Py_DEBUG implies Py_REF_DEBUG. */ |
| 58 | #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 59 | #define Py_REF_DEBUG |
| 60 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 62 | #if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG) |
| 63 | #error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG |
| 64 | #endif |
| 65 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 66 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 67 | #ifdef Py_TRACE_REFS |
| 68 | /* Define pointers to support a doubly-linked list of all live heap objects. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 69 | #define _PyObject_HEAD_EXTRA \ |
| 70 | struct _object *_ob_next; \ |
| 71 | struct _object *_ob_prev; |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 72 | |
| 73 | #define _PyObject_EXTRA_INIT 0, 0, |
| 74 | |
| 75 | #else |
| 76 | #define _PyObject_HEAD_EXTRA |
| 77 | #define _PyObject_EXTRA_INIT |
| 78 | #endif |
| 79 | |
| 80 | /* PyObject_HEAD defines the initial segment of every PyObject. */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 81 | #define PyObject_HEAD PyObject ob_base; |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 82 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 83 | #define PyObject_HEAD_INIT(type) \ |
| 84 | { _PyObject_EXTRA_INIT \ |
| 85 | 1, type }, |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 86 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 87 | #define PyVarObject_HEAD_INIT(type, size) \ |
| 88 | { PyObject_HEAD_INIT(type) size }, |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 89 | |
| 90 | /* PyObject_VAR_HEAD defines the initial segment of all variable-size |
| 91 | * container objects. These end with a declaration of an array with 1 |
| 92 | * element, but enough space is malloc'ed so that the array actually |
| 93 | * has room for ob_size elements. Note that ob_size is an element count, |
| 94 | * not necessarily a byte count. |
| 95 | */ |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 96 | #define PyObject_VAR_HEAD PyVarObject ob_base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 97 | #define Py_INVALID_SIZE (Py_ssize_t)-1 |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 98 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 99 | /* Nothing is actually declared to be a PyObject, but every pointer to |
| 100 | * a Python object can be cast to a PyObject*. This is inheritance built |
| 101 | * by hand. Similarly every pointer to a variable-size Python object can, |
| 102 | * in addition, be cast to PyVarObject*. |
| 103 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | typedef struct _object { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 105 | _PyObject_HEAD_EXTRA |
| 106 | Py_ssize_t ob_refcnt; |
| 107 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 108 | } PyObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 110 | /* Cast argument to PyObject* type. */ |
| 111 | #define _PyObject_CAST(op) ((PyObject*)(op)) |
| 112 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 114 | PyObject ob_base; |
| 115 | Py_ssize_t ob_size; /* Number of items in variable part */ |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 116 | } PyVarObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 118 | /* Cast argument to PyVarObject* type. */ |
| 119 | #define _PyVarObject_CAST(op) ((PyVarObject*)(op)) |
| 120 | |
| 121 | #define Py_REFCNT(ob) (_PyObject_CAST(ob)->ob_refcnt) |
| 122 | #define Py_TYPE(ob) (_PyObject_CAST(ob)->ob_type) |
| 123 | #define Py_SIZE(ob) (_PyVarObject_CAST(ob)->ob_size) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 124 | |
| 125 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 126 | Type objects contain a string containing the type name (to help somewhat |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 127 | in debugging), the allocation parameters (see PyObject_New() and |
| 128 | PyObject_NewVar()), |
| 129 | and methods for accessing objects of the type. Methods are optional, a |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | nil pointer meaning that particular kind of access is not available for |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 131 | this type. The Py_DECREF() macro uses the tp_dealloc method without |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 132 | checking for a nil pointer; it should always be implemented except if |
| 133 | the implementation can guarantee that the reference count will never |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 134 | reach zero (e.g., for statically allocated type objects). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | |
| 136 | NB: the methods for certain type groups are now contained in separate |
| 137 | method blocks. |
| 138 | */ |
| 139 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 140 | typedef PyObject * (*unaryfunc)(PyObject *); |
| 141 | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); |
| 142 | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); |
| 143 | typedef int (*inquiry)(PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 144 | typedef Py_ssize_t (*lenfunc)(PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 145 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); |
| 146 | typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 147 | typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); |
| 148 | typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 149 | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 150 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 151 | typedef int (*objobjproc)(PyObject *, PyObject *); |
| 152 | typedef int (*visitproc)(PyObject *, void *); |
| 153 | typedef int (*traverseproc)(PyObject *, visitproc, void *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 154 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 155 | |
Neil Schemenauer | f6d1ea1 | 2002-04-12 01:57:06 +0000 | [diff] [blame] | 156 | typedef void (*freefunc)(void *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 157 | typedef void (*destructor)(PyObject *); |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 158 | typedef PyObject *(*getattrfunc)(PyObject *, char *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 159 | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 160 | typedef int (*setattrfunc)(PyObject *, char *, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 161 | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 162 | typedef PyObject *(*reprfunc)(PyObject *); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 163 | typedef Py_hash_t (*hashfunc)(PyObject *); |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 164 | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 165 | typedef PyObject *(*getiterfunc) (PyObject *); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 166 | typedef PyObject *(*iternextfunc) (PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 167 | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); |
| 168 | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); |
| 169 | typedef int (*initproc)(PyObject *, PyObject *, PyObject *); |
| 170 | typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 171 | typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 172 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 173 | #ifdef Py_LIMITED_API |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 174 | /* In Py_LIMITED_API, PyTypeObject is an opaque structure. */ |
| 175 | typedef struct _typeobject PyTypeObject; |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 176 | #else |
| 177 | /* PyTypeObject is defined in cpython/object.h */ |
| 178 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 180 | typedef struct{ |
| 181 | int slot; /* slot id, see below */ |
| 182 | void *pfunc; /* function pointer */ |
| 183 | } PyType_Slot; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 185 | typedef struct{ |
| 186 | const char* name; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 187 | int basicsize; |
| 188 | int itemsize; |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 189 | unsigned int flags; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 190 | PyType_Slot *slots; /* terminated by slot==0. */ |
| 191 | } PyType_Spec; |
| 192 | |
| 193 | PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 194 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
| 195 | PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); |
| 196 | #endif |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 197 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 198 | PyAPI_FUNC(void*) PyType_GetSlot(struct _typeobject*, int); |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 199 | #endif |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 200 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 201 | /* Generic type check */ |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 202 | PyAPI_FUNC(int) PyType_IsSubtype(struct _typeobject *, struct _typeobject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 203 | #define PyObject_TypeCheck(ob, tp) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 204 | (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp))) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 205 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 206 | PyAPI_DATA(struct _typeobject) PyType_Type; /* built-in 'type' */ |
| 207 | PyAPI_DATA(struct _typeobject) PyBaseObject_Type; /* built-in 'object' */ |
| 208 | PyAPI_DATA(struct _typeobject) PySuper_Type; /* built-in 'super' */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 209 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 210 | PyAPI_FUNC(unsigned long) PyType_GetFlags(struct _typeobject*); |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 211 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 212 | #define PyType_Check(op) \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 213 | PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS) |
Christian Heimes | 90aa764 | 2007-12-19 02:45:37 +0000 | [diff] [blame] | 214 | #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 215 | |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 216 | PyAPI_FUNC(int) PyType_Ready(struct _typeobject *); |
| 217 | PyAPI_FUNC(PyObject *) PyType_GenericAlloc(struct _typeobject *, Py_ssize_t); |
| 218 | PyAPI_FUNC(PyObject *) PyType_GenericNew(struct _typeobject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 219 | PyObject *, PyObject *); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 220 | PyAPI_FUNC(unsigned int) PyType_ClearCache(void); |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 221 | PyAPI_FUNC(void) PyType_Modified(struct _typeobject *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 223 | /* Generic operations on objects */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 224 | PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); |
| 225 | PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 226 | PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 227 | PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 228 | PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); |
| 229 | PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 230 | PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *); |
| 231 | PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); |
| 232 | PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 233 | PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); |
| 234 | PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); |
| 235 | PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 236 | PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 237 | PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); |
| 238 | PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 239 | PyObject *, PyObject *); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 240 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 241 | PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 242 | #endif |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 243 | PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *); |
| 244 | PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 245 | PyAPI_FUNC(int) PyObject_IsTrue(PyObject *); |
| 246 | PyAPI_FUNC(int) PyObject_Not(PyObject *); |
| 247 | PyAPI_FUNC(int) PyCallable_Check(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 248 | PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *); |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 249 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 250 | /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a |
| 251 | list of strings. PyObject_Dir(NULL) is like builtins.dir(), |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 252 | 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 Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 255 | PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 256 | |
| 257 | |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 258 | /* Helpers for printing recursive container types */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 259 | PyAPI_FUNC(int) Py_ReprEnter(PyObject *); |
| 260 | PyAPI_FUNC(void) Py_ReprLeave(PyObject *); |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 261 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | /* Flag bits for printing: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 263 | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 264 | |
| 265 | /* |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 266 | Type flags (tp_flags) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 267 | |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 268 | These flags are used to change expected features and behavior for a |
| 269 | particular type. |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 270 | |
| 271 | Arbitration of the flag bit positions will need to be coordinated among |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 272 | all extension writers who publicly release their extensions (this will |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 273 | be fewer than you might expect!). |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 274 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 275 | Most flags were removed as of Python 3.0 to make room for new flags. (Some |
| 276 | flags are not for backwards compatibility but to indicate the presence of an |
| 277 | optional feature; these flags remain of course.) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 278 | |
| 279 | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. |
| 280 | |
| 281 | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the |
| 282 | given type object has a specified feature. |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 283 | */ |
| 284 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 285 | /* Set if the type object is dynamically allocated */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 286 | #define Py_TPFLAGS_HEAPTYPE (1UL << 9) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 287 | |
| 288 | /* Set if the type allows subclassing */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 289 | #define Py_TPFLAGS_BASETYPE (1UL << 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 290 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 291 | /* Set if the type implements the vectorcall protocol (PEP 590) */ |
| 292 | #ifndef Py_LIMITED_API |
| 293 | #define _Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11) |
| 294 | #endif |
| 295 | |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 296 | /* Set if the type is 'ready' -- fully initialized */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 297 | #define Py_TPFLAGS_READY (1UL << 12) |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 298 | |
| 299 | /* Set while the type is being 'readied', to prevent recursive ready calls */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 300 | #define Py_TPFLAGS_READYING (1UL << 13) |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 301 | |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 302 | /* Objects support garbage collection (see objimpl.h) */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 303 | #define Py_TPFLAGS_HAVE_GC (1UL << 14) |
Neil Schemenauer | 31ec142 | 2001-08-29 23:46:35 +0000 | [diff] [blame] | 304 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 305 | /* These two bits are preserved for Stackless Python, next after this is 17 */ |
Christian Tismer | 6695ba8 | 2003-05-20 15:14:31 +0000 | [diff] [blame] | 306 | #ifdef STACKLESS |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 307 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15) |
Christian Tismer | 6695ba8 | 2003-05-20 15:14:31 +0000 | [diff] [blame] | 308 | #else |
Christian Tismer | c26ff41 | 2003-05-23 03:33:35 +0000 | [diff] [blame] | 309 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0 |
Christian Tismer | 6695ba8 | 2003-05-20 15:14:31 +0000 | [diff] [blame] | 310 | #endif |
| 311 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 312 | /* Objects behave like an unbound method */ |
| 313 | #define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17) |
| 314 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 315 | /* Objects support type attribute cache */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 316 | #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18) |
| 317 | #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 318 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 319 | /* Type is abstract and cannot be instantiated */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 320 | #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 321 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 322 | /* These flags are used to determine if a type is a subclass. */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 323 | #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24) |
| 324 | #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25) |
| 325 | #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26) |
| 326 | #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27) |
| 327 | #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28) |
| 328 | #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29) |
| 329 | #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30) |
| 330 | #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31) |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 331 | |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 332 | #define Py_TPFLAGS_DEFAULT ( \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 333 | Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \ |
| 334 | Py_TPFLAGS_HAVE_VERSION_TAG | \ |
| 335 | 0) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 336 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 337 | /* NOTE: The following flags reuse lower bits (removed as part of the |
| 338 | * Python 3.0 transition). */ |
| 339 | |
Hansraj Das | 10e5c66 | 2019-07-06 03:07:15 +0530 | [diff] [blame] | 340 | /* The following flag is kept for compatibility. Starting with 3.8, |
| 341 | * binary compatibility of C extensions across feature releases of |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 342 | * Python is not supported anymore, except when using the stable ABI. |
| 343 | */ |
| 344 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 345 | /* Type structure has tp_finalize member (3.4) */ |
| 346 | #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0) |
| 347 | |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 348 | #ifdef Py_LIMITED_API |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 349 | # define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0) |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 350 | #endif |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 351 | #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 352 | |
| 353 | |
| 354 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 355 | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 356 | reference counts. Py_DECREF calls the object's deallocator function when |
| 357 | the refcount falls to 0; for |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 358 | objects that don't contain references to other objects or heap memory |
| 359 | this can be the standard function free(). Both macros can be used |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 360 | wherever a void expression is allowed. The argument must not be a |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 361 | NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead. |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 362 | The macro _Py_NewReference(op) initialize reference counts to 1, and |
| 363 | in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional |
| 364 | bookkeeping appropriate to the special build. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | |
| 366 | We assume that the reference count field can never overflow; this can |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 367 | be proven when the size of the field is the same as the pointer size, so |
| 368 | we ignore the possibility. Provided a C int is at least 32 bits (which |
| 369 | is implicitly assumed in many parts of this code), that's enough for |
| 370 | about 2**31 references to an object. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 371 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 372 | XXX The following became out of date in Python 2.2, but I'm not sure |
| 373 | XXX what the full truth is now. Certainly, heap-allocated type objects |
| 374 | XXX can and should be deallocated. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 375 | Type objects should never be deallocated; the type pointer in an object |
| 376 | is not considered to be a reference to the type object, to save |
| 377 | complications in the deallocation function. (This is actually a |
| 378 | decision that's up to the implementer of each new type so if you want, |
| 379 | you can count such references to the type object.) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 380 | */ |
| 381 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 382 | /* First define a pile of simple helper macros, one set per special |
| 383 | * build symbol. These either expand to the obvious things, or to |
| 384 | * nothing at all when the special mode isn't in effect. The main |
| 385 | * macros can later be defined just once then, yet expand to different |
| 386 | * things depending on which special build options are and aren't in effect. |
| 387 | * Trust me <wink>: while painful, this is 20x easier to understand than, |
| 388 | * e.g, defining _Py_NewReference five different times in a maze of nested |
| 389 | * #ifdefs (we used to do that -- it was impenetrable). |
| 390 | */ |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 391 | #ifdef Py_REF_DEBUG |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 392 | PyAPI_DATA(Py_ssize_t) _Py_RefTotal; |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 393 | PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno, |
| 394 | PyObject *op); |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 395 | PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void); |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 396 | #define _Py_INC_REFTOTAL _Py_RefTotal++ |
| 397 | #define _Py_DEC_REFTOTAL _Py_RefTotal-- |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 398 | |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 399 | /* Py_REF_DEBUG also controls the display of refcounts and memory block |
| 400 | * allocations at the interactive prompt and at interpreter shutdown |
| 401 | */ |
| 402 | PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void); |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 403 | #else |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 404 | #define _Py_INC_REFTOTAL |
| 405 | #define _Py_DEC_REFTOTAL |
Tim Peters | 57f4ddd | 2002-07-10 06:34:15 +0000 | [diff] [blame] | 406 | #endif /* Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 408 | #ifdef COUNT_ALLOCS |
Victor Stinner | 9bdd2de | 2018-11-27 23:54:59 +0100 | [diff] [blame] | 409 | PyAPI_FUNC(void) _Py_inc_count(struct _typeobject *); |
| 410 | PyAPI_FUNC(void) _Py_dec_count(struct _typeobject *); |
Pablo Galindo | 49c75a8 | 2018-10-28 15:02:17 +0000 | [diff] [blame] | 411 | #define _Py_INC_TPALLOCS(OP) _Py_inc_count(Py_TYPE(OP)) |
| 412 | #define _Py_INC_TPFREES(OP) _Py_dec_count(Py_TYPE(OP)) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 413 | #define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees-- |
| 414 | #define _Py_COUNT_ALLOCS_COMMA , |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 415 | #else |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 416 | #define _Py_INC_TPALLOCS(OP) |
| 417 | #define _Py_INC_TPFREES(OP) |
| 418 | #define _Py_DEC_TPFREES(OP) |
| 419 | #define _Py_COUNT_ALLOCS_COMMA |
Tim Peters | 57f4ddd | 2002-07-10 06:34:15 +0000 | [diff] [blame] | 420 | #endif /* COUNT_ALLOCS */ |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 421 | |
Victor Stinner | c89a932 | 2018-10-26 00:01:56 +0200 | [diff] [blame] | 422 | /* Update the Python traceback of an object. This function must be called |
| 423 | when a memory block is reused from a free list. */ |
| 424 | PyAPI_FUNC(int) _PyTraceMalloc_NewReference(PyObject *op); |
| 425 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 426 | #ifdef Py_TRACE_REFS |
| 427 | /* Py_TRACE_REFS is such major surgery that we call external routines. */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 428 | PyAPI_FUNC(void) _Py_NewReference(PyObject *); |
| 429 | PyAPI_FUNC(void) _Py_ForgetReference(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 430 | PyAPI_FUNC(void) _Py_PrintReferences(FILE *); |
Tim Peters | 269b2a6 | 2003-04-17 19:52:29 +0000 | [diff] [blame] | 431 | PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *); |
Tim Peters | 7571a0f | 2003-03-23 17:52:28 +0000 | [diff] [blame] | 432 | PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force); |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 433 | #else |
| 434 | /* Without Py_TRACE_REFS, there's little enough to do that we expand code |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 435 | inline. */ |
| 436 | static inline void _Py_NewReference(PyObject *op) |
| 437 | { |
| 438 | if (_Py_tracemalloc_config.tracing) { |
| 439 | _PyTraceMalloc_NewReference(op); |
| 440 | } |
| 441 | _Py_INC_TPALLOCS(op); |
| 442 | _Py_INC_REFTOTAL; |
| 443 | Py_REFCNT(op) = 1; |
| 444 | } |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 445 | |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 446 | static inline void _Py_ForgetReference(PyObject *op) |
| 447 | { |
Dmitry Marakasov | a0da131 | 2019-04-06 12:04:47 +0300 | [diff] [blame] | 448 | (void)op; /* may be unused, shut up -Wunused-parameter */ |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 449 | _Py_INC_TPFREES(op); |
| 450 | } |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 451 | #endif /* !Py_TRACE_REFS */ |
| 452 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 453 | |
Victor Stinner | 3c09dca | 2018-10-30 14:48:26 +0100 | [diff] [blame] | 454 | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
| 455 | |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 456 | static inline void _Py_INCREF(PyObject *op) |
| 457 | { |
| 458 | _Py_INC_REFTOTAL; |
| 459 | op->ob_refcnt++; |
| 460 | } |
| 461 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 462 | #define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op)) |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 463 | |
| 464 | static inline void _Py_DECREF(const char *filename, int lineno, |
| 465 | PyObject *op) |
| 466 | { |
Dmitry Marakasov | a0da131 | 2019-04-06 12:04:47 +0300 | [diff] [blame] | 467 | (void)filename; /* may be unused, shut up -Wunused-parameter */ |
| 468 | (void)lineno; /* may be unused, shut up -Wunused-parameter */ |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 469 | _Py_DEC_REFTOTAL; |
| 470 | if (--op->ob_refcnt != 0) { |
| 471 | #ifdef Py_REF_DEBUG |
| 472 | if (op->ob_refcnt < 0) { |
| 473 | _Py_NegativeRefcount(filename, lineno, op); |
| 474 | } |
| 475 | #endif |
| 476 | } |
| 477 | else { |
| 478 | _Py_Dealloc(op); |
| 479 | } |
| 480 | } |
| 481 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 482 | #define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op)) |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 483 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 484 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 485 | /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear |
Berker Peksag | 4882cac | 2015-04-14 09:30:01 +0300 | [diff] [blame] | 486 | * and tp_dealloc implementations. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 487 | * |
| 488 | * Note that "the obvious" code can be deadly: |
| 489 | * |
| 490 | * Py_XDECREF(op); |
| 491 | * op = NULL; |
| 492 | * |
| 493 | * Typically, `op` is something like self->containee, and `self` is done |
| 494 | * using its `containee` member. In the code sequence above, suppose |
| 495 | * `containee` is non-NULL with a refcount of 1. Its refcount falls to |
| 496 | * 0 on the first line, which can trigger an arbitrary amount of code, |
| 497 | * possibly including finalizers (like __del__ methods or weakref callbacks) |
| 498 | * coded in Python, which in turn can release the GIL and allow other threads |
| 499 | * to run, etc. Such code may even invoke methods of `self` again, or cause |
| 500 | * cyclic gc to trigger, but-- oops! --self->containee still points to the |
| 501 | * object being torn down, and it may be in an insane state while being torn |
| 502 | * down. This has in fact been a rich historic source of miserable (rare & |
| 503 | * hard-to-diagnose) segfaulting (and other) bugs. |
| 504 | * |
| 505 | * The safe way is: |
| 506 | * |
| 507 | * Py_CLEAR(op); |
| 508 | * |
| 509 | * That arranges to set `op` to NULL _before_ decref'ing, so that any code |
| 510 | * triggered as a side-effect of `op` getting torn down no longer believes |
| 511 | * `op` points to a valid object. |
| 512 | * |
| 513 | * There are cases where it's safe to use the naive code, but they're brittle. |
| 514 | * For example, if `op` points to a Python integer, you know that destroying |
| 515 | * one of those can't cause problems -- but in part that relies on that |
| 516 | * Python integers aren't currently weakly referencable. Best practice is |
| 517 | * to use Py_CLEAR() even if you can't think of a reason for why you need to. |
| 518 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 519 | #define Py_CLEAR(op) \ |
| 520 | do { \ |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 521 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 522 | if (_py_tmp != NULL) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 523 | (op) = NULL; \ |
| 524 | Py_DECREF(_py_tmp); \ |
| 525 | } \ |
| 526 | } while (0) |
Jim Fulton | 8c5aeaa | 2004-07-14 19:07:35 +0000 | [diff] [blame] | 527 | |
Victor Stinner | 541497e | 2018-10-29 20:52:41 +0100 | [diff] [blame] | 528 | /* Function to use in case the object pointer can be NULL: */ |
| 529 | static inline void _Py_XINCREF(PyObject *op) |
| 530 | { |
| 531 | if (op != NULL) { |
| 532 | Py_INCREF(op); |
| 533 | } |
| 534 | } |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 535 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 536 | #define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op)) |
Victor Stinner | 541497e | 2018-10-29 20:52:41 +0100 | [diff] [blame] | 537 | |
| 538 | static inline void _Py_XDECREF(PyObject *op) |
| 539 | { |
| 540 | if (op != NULL) { |
| 541 | Py_DECREF(op); |
| 542 | } |
| 543 | } |
| 544 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 545 | #define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | /* |
Thomas Heller | 1328b52 | 2004-04-22 17:23:49 +0000 | [diff] [blame] | 548 | These are provided as conveniences to Python runtime embedders, so that |
| 549 | they can have object code that is not dependent on Python compilation flags. |
| 550 | */ |
| 551 | PyAPI_FUNC(void) Py_IncRef(PyObject *); |
| 552 | PyAPI_FUNC(void) Py_DecRef(PyObject *); |
| 553 | |
| 554 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 555 | _Py_NoneStruct is an object of undefined type which can be used in contexts |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 556 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 557 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 558 | Don't forget to apply Py_INCREF() when returning this value!!! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 559 | */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 560 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 561 | #define Py_None (&_Py_NoneStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | |
Brett Cannon | d05235e | 2003-10-19 21:19:40 +0000 | [diff] [blame] | 563 | /* Macro for returning Py_None from a function */ |
Tim Peters | 5980ff2 | 2004-07-22 01:46:43 +0000 | [diff] [blame] | 564 | #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None |
Brett Cannon | d05235e | 2003-10-19 21:19:40 +0000 | [diff] [blame] | 565 | |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 566 | /* |
| 567 | Py_NotImplemented is a singleton used to signal that an operation is |
| 568 | not implemented for a given type combination. |
| 569 | */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 570 | PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 571 | #define Py_NotImplemented (&_Py_NotImplementedStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 572 | |
Brian Curtin | 7d2f9e1 | 2011-08-10 20:05:21 -0500 | [diff] [blame] | 573 | /* Macro for returning Py_NotImplemented from a function */ |
| 574 | #define Py_RETURN_NOTIMPLEMENTED \ |
| 575 | return Py_INCREF(Py_NotImplemented), Py_NotImplemented |
| 576 | |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 577 | /* Rich comparison opcodes */ |
| 578 | #define Py_LT 0 |
| 579 | #define Py_LE 1 |
| 580 | #define Py_EQ 2 |
| 581 | #define Py_NE 3 |
| 582 | #define Py_GT 4 |
| 583 | #define Py_GE 5 |
| 584 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 585 | /* |
| 586 | * Macro for implementing rich comparisons |
| 587 | * |
| 588 | * Needs to be a macro because any C-comparable type can be used. |
| 589 | */ |
| 590 | #define Py_RETURN_RICHCOMPARE(val1, val2, op) \ |
| 591 | do { \ |
| 592 | switch (op) { \ |
| 593 | case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 594 | case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 595 | case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 596 | case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 597 | case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 598 | case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 599 | default: \ |
| 600 | Py_UNREACHABLE(); \ |
| 601 | } \ |
| 602 | } while (0) |
| 603 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 604 | |
| 605 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 606 | More conventions |
| 607 | ================ |
| 608 | |
| 609 | Argument Checking |
| 610 | ----------------- |
| 611 | |
| 612 | Functions that take objects as arguments normally don't check for nil |
| 613 | arguments, but they do check the type of the argument, and return an |
| 614 | error if the function doesn't apply to the type. |
| 615 | |
| 616 | Failure Modes |
| 617 | ------------- |
| 618 | |
| 619 | Functions may fail for a variety of reasons, including running out of |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 620 | memory. This is communicated to the caller in two ways: an error string |
| 621 | is set (see errors.h), and the function result differs: functions that |
| 622 | normally return a pointer return NULL for failure, functions returning |
| 623 | an integer return -1 (which could be a legal return value too!), and |
| 624 | other functions return 0 for success and -1 for failure. |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 625 | Callers should always check for errors before using the result. If |
| 626 | an error was set, the caller must either explicitly clear it, or pass |
| 627 | the error on to its caller. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 628 | |
| 629 | Reference Counts |
| 630 | ---------------- |
| 631 | |
| 632 | It takes a while to get used to the proper usage of reference counts. |
| 633 | |
| 634 | Functions that create an object set the reference count to 1; such new |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 635 | objects must be stored somewhere or destroyed again with Py_DECREF(). |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 636 | Some functions that 'store' objects, such as PyTuple_SetItem() and |
| 637 | PyList_SetItem(), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 638 | don't increment the reference count of the object, since the most |
| 639 | frequent use is to store a fresh object. Functions that 'retrieve' |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 640 | objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 641 | don't increment |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 642 | the reference count, since most frequently the object is only looked at |
| 643 | quickly. Thus, to retrieve an object and store it again, the caller |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 644 | must call Py_INCREF() explicitly. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 645 | |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 646 | NOTE: functions that 'consume' a reference count, like |
| 647 | PyList_SetItem(), consume the reference even if the object wasn't |
| 648 | successfully stored, to simplify error handling. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 649 | |
| 650 | It seems attractive to make other functions that take an object as |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 651 | argument consume a reference count; however, this may quickly get |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | confusing (even the current practice is already confusing). Consider |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 653 | it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 654 | times. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 655 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 656 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 657 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 658 | /* Trashcan mechanism, thanks to Christian Tismer. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 659 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 660 | When deallocating a container object, it's possible to trigger an unbounded |
| 661 | chain of deallocations, as each Py_DECREF in turn drops the refcount on "the |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 662 | next" object in the chain to 0. This can easily lead to stack overflows, |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 663 | especially in threads (which typically have less stack space to work with). |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 664 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 665 | A container object can avoid this by bracketing the body of its tp_dealloc |
| 666 | function with a pair of macros: |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 667 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 668 | static void |
| 669 | mytype_dealloc(mytype *p) |
| 670 | { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 671 | ... declarations go here ... |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 672 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 673 | PyObject_GC_UnTrack(p); // must untrack first |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 674 | Py_TRASHCAN_BEGIN(p, mytype_dealloc) |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 675 | ... The body of the deallocator goes here, including all calls ... |
| 676 | ... to Py_DECREF on contained objects. ... |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 677 | Py_TRASHCAN_END // there should be no code after this |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 678 | } |
| 679 | |
Tim Peters | 808eb59 | 2002-08-07 20:53:05 +0000 | [diff] [blame] | 680 | CAUTION: Never return from the middle of the body! If the body needs to |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 681 | "get out early", put a label immediately before the Py_TRASHCAN_END |
Tim Peters | 808eb59 | 2002-08-07 20:53:05 +0000 | [diff] [blame] | 682 | call, and goto it. Else the call-depth counter (see below) will stay |
| 683 | above 0 forever, and the trashcan will never get emptied. |
| 684 | |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 685 | How it works: The BEGIN macro increments a call-depth counter. So long |
| 686 | as this counter is small, the body of the deallocator is run directly without |
| 687 | further ado. But if the counter gets large, it instead adds p to a list of |
| 688 | objects to be deallocated later, skips the body of the deallocator, and |
| 689 | resumes execution after the END macro. The tp_dealloc routine then returns |
| 690 | without deallocating anything (and so unbounded call-stack depth is avoided). |
| 691 | |
| 692 | When the call stack finishes unwinding again, code generated by the END macro |
| 693 | notices this, and calls another routine to deallocate all the objects that |
| 694 | may have been added to the list of deferred deallocations. In effect, a |
Xiang Zhang | a66f9c6 | 2017-05-13 13:36:14 +0800 | [diff] [blame] | 695 | chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces, |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 696 | with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL. |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 697 | |
| 698 | Since the tp_dealloc of a subclass typically calls the tp_dealloc of the base |
| 699 | class, we need to ensure that the trashcan is only triggered on the tp_dealloc |
| 700 | of the actual class being deallocated. Otherwise we might end up with a |
| 701 | partially-deallocated object. To check this, the tp_dealloc function must be |
| 702 | passed as second argument to Py_TRASHCAN_BEGIN(). |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 703 | */ |
| 704 | |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 705 | /* The new thread-safe private API, invoked by the macros below. */ |
| 706 | PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*); |
| 707 | PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void); |
| 708 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 709 | #define PyTrash_UNWIND_LEVEL 50 |
| 710 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 711 | #define Py_TRASHCAN_BEGIN_CONDITION(op, cond) \ |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 712 | do { \ |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 713 | PyThreadState *_tstate = NULL; \ |
| 714 | /* If "cond" is false, then _tstate remains NULL and the deallocator \ |
| 715 | * is run normally without involving the trashcan */ \ |
| 716 | if (cond) { \ |
| 717 | _tstate = PyThreadState_GET(); \ |
| 718 | if (_tstate->trash_delete_nesting >= PyTrash_UNWIND_LEVEL) { \ |
| 719 | /* Store the object (to be deallocated later) and jump past \ |
| 720 | * Py_TRASHCAN_END, skipping the body of the deallocator */ \ |
| 721 | _PyTrash_thread_deposit_object(_PyObject_CAST(op)); \ |
| 722 | break; \ |
| 723 | } \ |
| 724 | ++_tstate->trash_delete_nesting; \ |
| 725 | } |
| 726 | /* The body of the deallocator is here. */ |
| 727 | #define Py_TRASHCAN_END \ |
| 728 | if (_tstate) { \ |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 729 | --_tstate->trash_delete_nesting; \ |
| 730 | if (_tstate->trash_delete_later && _tstate->trash_delete_nesting <= 0) \ |
| 731 | _PyTrash_thread_destroy_chain(); \ |
| 732 | } \ |
Antoine Pitrou | 56cd62c | 2012-09-06 00:59:49 +0200 | [diff] [blame] | 733 | } while (0); |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 734 | |
Jeroen Demeyer | 351c674 | 2019-05-10 19:21:11 +0200 | [diff] [blame] | 735 | #define Py_TRASHCAN_BEGIN(op, dealloc) Py_TRASHCAN_BEGIN_CONDITION(op, \ |
| 736 | Py_TYPE(op)->tp_dealloc == (destructor)(dealloc)) |
| 737 | |
| 738 | /* For backwards compatibility, these macros enable the trashcan |
| 739 | * unconditionally */ |
| 740 | #define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1) |
| 741 | #define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END |
| 742 | |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 743 | |
| 744 | #ifndef Py_LIMITED_API |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 745 | # define Py_CPYTHON_OBJECT_H |
| 746 | # include "cpython/object.h" |
| 747 | # undef Py_CPYTHON_OBJECT_H |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 748 | #endif |
| 749 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 750 | #ifdef __cplusplus |
| 751 | } |
| 752 | #endif |
| 753 | #endif /* !Py_OBJECT_H */ |