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 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 4 | #ifdef __cplusplus |
| 5 | extern "C" { |
| 6 | #endif |
| 7 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 9 | /* Object and type object interface */ |
| 10 | |
| 11 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 12 | Objects are structures allocated on the heap. Special rules apply to |
| 13 | the use of objects to ensure they are properly garbage-collected. |
| 14 | Objects are never allocated statically or on the stack; they must be |
| 15 | accessed through special macros and functions only. (Type objects are |
| 16 | exceptions to the first rule; the standard types are represented by |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 17 | statically initialized type objects, although work on type/class unification |
| 18 | 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] | 19 | |
| 20 | An object has a 'reference count' that is increased or decreased when a |
| 21 | pointer to the object is copied or deleted; when the reference count |
| 22 | reaches zero there are no references to the object left and it can be |
| 23 | removed from the heap. |
| 24 | |
| 25 | An object has a 'type' that determines what it represents and what kind |
| 26 | of data it contains. An object's type is fixed when it is created. |
| 27 | Types themselves are represented as objects; an object contains a |
| 28 | pointer to the corresponding type object. The type itself has a type |
| 29 | pointer pointing to the object representing the type 'type', which |
Jimmy Yang | ac2cfe6 | 2020-04-08 02:28:59 -0700 | [diff] [blame] | 30 | contains a pointer to itself!. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | |
| 32 | Objects do not float around in memory; once allocated an object keeps |
| 33 | the same size and address. Objects that must hold variable-size data |
| 34 | can contain pointers to variable-size parts of the object. Not all |
| 35 | objects of the same type have the same size; but the size cannot change |
| 36 | after allocation. (These restrictions are made so a reference to an |
| 37 | object can be simply a pointer -- moving an object would require |
| 38 | updating all the pointers, and changing an object's size would require |
| 39 | moving it if there was another object right next to it.) |
| 40 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 41 | Objects are always accessed through pointers of the type 'PyObject *'. |
| 42 | 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] | 43 | and the type pointer. The actual memory allocated for an object |
| 44 | contains other data that can only be accessed after casting the pointer |
| 45 | 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] | 46 | 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] | 47 | used for this (to accommodate for future changes). The implementation |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 48 | of a particular object type can cast the object pointer to the proper |
| 49 | type and back. |
| 50 | |
| 51 | A standard interface exists for objects that contain an array of items |
| 52 | whose size is determined when the object is allocated. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 53 | */ |
| 54 | |
Victor Stinner | f4e4703 | 2019-04-25 00:56:28 +0200 | [diff] [blame] | 55 | /* Py_DEBUG implies Py_REF_DEBUG. */ |
| 56 | #if defined(Py_DEBUG) && !defined(Py_REF_DEBUG) |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 57 | #define Py_REF_DEBUG |
| 58 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 60 | #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 Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 64 | /* PyTypeObject structure is defined in cpython/object.h. |
| 65 | In Py_LIMITED_API, PyTypeObject is an opaque structure. */ |
| 66 | typedef struct _typeobject PyTypeObject; |
Nick Coghlan | d600951 | 2014-11-20 21:39:37 +1000 | [diff] [blame] | 67 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 68 | #ifdef Py_TRACE_REFS |
| 69 | /* 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] | 70 | #define _PyObject_HEAD_EXTRA \ |
| 71 | struct _object *_ob_next; \ |
| 72 | struct _object *_ob_prev; |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 73 | |
| 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 Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 82 | #define PyObject_HEAD PyObject ob_base; |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 83 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 84 | #define PyObject_HEAD_INIT(type) \ |
| 85 | { _PyObject_EXTRA_INIT \ |
| 86 | 1, type }, |
Martin v. Löwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 87 | |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 88 | #define PyVarObject_HEAD_INIT(type, size) \ |
| 89 | { PyObject_HEAD_INIT(type) size }, |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 90 | |
| 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öwis | 9f2e346 | 2007-07-21 17:22:18 +0000 | [diff] [blame] | 97 | #define PyObject_VAR_HEAD PyVarObject ob_base; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 98 | #define Py_INVALID_SIZE (Py_ssize_t)-1 |
Tim Peters | 803526b | 2002-07-07 05:13:56 +0000 | [diff] [blame] | 99 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 100 | /* 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 105 | typedef struct _object { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 106 | _PyObject_HEAD_EXTRA |
| 107 | Py_ssize_t ob_refcnt; |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 108 | PyTypeObject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 109 | } PyObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 111 | /* Cast argument to PyObject* type. */ |
| 112 | #define _PyObject_CAST(op) ((PyObject*)(op)) |
Andy Lester | 8767ce9 | 2020-03-06 02:03:58 -0600 | [diff] [blame] | 113 | #define _PyObject_CAST_CONST(op) ((const PyObject*)(op)) |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 114 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 115 | typedef struct { |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 116 | PyObject ob_base; |
| 117 | Py_ssize_t ob_size; /* Number of items in variable part */ |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 118 | } PyVarObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 120 | /* Cast argument to PyVarObject* type. */ |
| 121 | #define _PyVarObject_CAST(op) ((PyVarObject*)(op)) |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 122 | #define _PyVarObject_CAST_CONST(op) ((const PyVarObject*)(op)) |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 123 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 124 | |
| 125 | static inline Py_ssize_t _Py_REFCNT(const PyObject *ob) { |
| 126 | return ob->ob_refcnt; |
| 127 | } |
| 128 | #define Py_REFCNT(ob) _Py_REFCNT(_PyObject_CAST_CONST(ob)) |
| 129 | |
| 130 | |
| 131 | static inline Py_ssize_t _Py_SIZE(const PyVarObject *ob) { |
| 132 | return ob->ob_size; |
| 133 | } |
| 134 | #define Py_SIZE(ob) _Py_SIZE(_PyVarObject_CAST_CONST(ob)) |
| 135 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | |
Dong-hee Na | ad3252b | 2020-05-26 01:52:54 +0900 | [diff] [blame] | 137 | static inline PyTypeObject* _Py_TYPE(const PyObject *ob) { |
| 138 | return ob->ob_type; |
| 139 | } |
| 140 | #define Py_TYPE(ob) _Py_TYPE(_PyObject_CAST_CONST(ob)) |
| 141 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 142 | |
Andy Lester | 8767ce9 | 2020-03-06 02:03:58 -0600 | [diff] [blame] | 143 | static inline int _Py_IS_TYPE(const PyObject *ob, const PyTypeObject *type) { |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 144 | return ob->ob_type == type; |
| 145 | } |
Andy Lester | 8767ce9 | 2020-03-06 02:03:58 -0600 | [diff] [blame] | 146 | #define Py_IS_TYPE(ob, type) _Py_IS_TYPE(_PyObject_CAST_CONST(ob), type) |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 147 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 148 | |
Victor Stinner | c86a112 | 2020-02-07 01:24:29 +0100 | [diff] [blame] | 149 | static inline void _Py_SET_REFCNT(PyObject *ob, Py_ssize_t refcnt) { |
| 150 | ob->ob_refcnt = refcnt; |
| 151 | } |
| 152 | #define Py_SET_REFCNT(ob, refcnt) _Py_SET_REFCNT(_PyObject_CAST(ob), refcnt) |
| 153 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 154 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 155 | static inline void _Py_SET_TYPE(PyObject *ob, PyTypeObject *type) { |
| 156 | ob->ob_type = type; |
| 157 | } |
| 158 | #define Py_SET_TYPE(ob, type) _Py_SET_TYPE(_PyObject_CAST(ob), type) |
| 159 | |
Victor Stinner | fe2978b | 2020-05-27 14:55:10 +0200 | [diff] [blame] | 160 | |
Brandt Bucher | 968dcd9 | 2020-02-13 09:34:45 -0800 | [diff] [blame] | 161 | static inline void _Py_SET_SIZE(PyVarObject *ob, Py_ssize_t size) { |
| 162 | ob->ob_size = size; |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 163 | } |
Brandt Bucher | 968dcd9 | 2020-02-13 09:34:45 -0800 | [diff] [blame] | 164 | #define Py_SET_SIZE(ob, size) _Py_SET_SIZE(_PyVarObject_CAST(ob), size) |
Victor Stinner | b10dc3e | 2020-02-07 12:05:12 +0100 | [diff] [blame] | 165 | |
Victor Stinner | d2ec81a | 2020-02-07 09:17:07 +0100 | [diff] [blame] | 166 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 167 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | Type objects contain a string containing the type name (to help somewhat |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 169 | in debugging), the allocation parameters (see PyObject_New() and |
| 170 | PyObject_NewVar()), |
| 171 | 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] | 172 | 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] | 173 | 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] | 174 | checking for a nil pointer; it should always be implemented except if |
| 175 | the implementation can guarantee that the reference count will never |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 176 | reach zero (e.g., for statically allocated type objects). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | |
| 178 | NB: the methods for certain type groups are now contained in separate |
| 179 | method blocks. |
| 180 | */ |
| 181 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 182 | typedef PyObject * (*unaryfunc)(PyObject *); |
| 183 | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); |
| 184 | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); |
| 185 | typedef int (*inquiry)(PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 186 | typedef Py_ssize_t (*lenfunc)(PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 187 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); |
| 188 | typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 189 | typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); |
| 190 | typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 191 | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 192 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 193 | typedef int (*objobjproc)(PyObject *, PyObject *); |
| 194 | typedef int (*visitproc)(PyObject *, void *); |
| 195 | typedef int (*traverseproc)(PyObject *, visitproc, void *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 196 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 197 | |
Neil Schemenauer | f6d1ea1 | 2002-04-12 01:57:06 +0000 | [diff] [blame] | 198 | typedef void (*freefunc)(void *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 199 | typedef void (*destructor)(PyObject *); |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 200 | typedef PyObject *(*getattrfunc)(PyObject *, char *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 201 | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 202 | typedef int (*setattrfunc)(PyObject *, char *, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 203 | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 204 | typedef PyObject *(*reprfunc)(PyObject *); |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 205 | typedef Py_hash_t (*hashfunc)(PyObject *); |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 206 | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 207 | typedef PyObject *(*getiterfunc) (PyObject *); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 208 | typedef PyObject *(*iternextfunc) (PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 209 | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); |
| 210 | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); |
| 211 | typedef int (*initproc)(PyObject *, PyObject *, PyObject *); |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 212 | typedef PyObject *(*newfunc)(PyTypeObject *, PyObject *, PyObject *); |
| 213 | typedef PyObject *(*allocfunc)(PyTypeObject *, Py_ssize_t); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 215 | typedef struct{ |
| 216 | int slot; /* slot id, see below */ |
| 217 | void *pfunc; /* function pointer */ |
| 218 | } PyType_Slot; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 220 | typedef struct{ |
| 221 | const char* name; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 222 | int basicsize; |
| 223 | int itemsize; |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 224 | unsigned int flags; |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 225 | PyType_Slot *slots; /* terminated by slot==0. */ |
| 226 | } PyType_Spec; |
| 227 | |
| 228 | PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*); |
Martin v. Löwis | 9c56409 | 2012-06-23 23:20:45 +0200 | [diff] [blame] | 229 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
| 230 | PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); |
| 231 | #endif |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 232 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 233 | PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int); |
Martin v. Löwis | ca7b046 | 2014-02-04 09:33:05 +0100 | [diff] [blame] | 234 | #endif |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 235 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03090000 |
| 236 | PyAPI_FUNC(PyObject*) PyType_FromModuleAndSpec(PyObject *, PyType_Spec *, PyObject *); |
| 237 | PyAPI_FUNC(PyObject *) PyType_GetModule(struct _typeobject *); |
| 238 | PyAPI_FUNC(void *) PyType_GetModuleState(struct _typeobject *); |
| 239 | #endif |
Martin v. Löwis | 4d0d471 | 2010-12-03 20:14:31 +0000 | [diff] [blame] | 240 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 241 | /* Generic type check */ |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 242 | PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 243 | #define PyObject_TypeCheck(ob, tp) \ |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 244 | (Py_IS_TYPE(ob, tp) || PyType_IsSubtype(Py_TYPE(ob), (tp))) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 245 | |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 246 | PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ |
| 247 | PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ |
| 248 | PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 249 | |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 250 | PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*); |
Martin v. Löwis | 738236d | 2011-02-05 20:35:29 +0000 | [diff] [blame] | 251 | |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 252 | PyAPI_FUNC(int) PyType_Ready(PyTypeObject *); |
| 253 | PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t); |
| 254 | PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *, |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 255 | PyObject *, PyObject *); |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 256 | PyAPI_FUNC(unsigned int) PyType_ClearCache(void); |
Victor Stinner | 0e4e735 | 2020-02-05 15:10:39 +0100 | [diff] [blame] | 257 | PyAPI_FUNC(void) PyType_Modified(PyTypeObject *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 259 | /* Generic operations on objects */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 260 | PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); |
| 261 | PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); |
Georg Brandl | 559e5d7 | 2008-06-11 18:37:52 +0000 | [diff] [blame] | 262 | PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *); |
Benjamin Peterson | c15a073 | 2008-08-26 16:46:47 +0000 | [diff] [blame] | 263 | PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 264 | PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); |
| 265 | PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); |
Jeremy Hylton | af68c87 | 2005-12-10 18:50:16 +0000 | [diff] [blame] | 266 | PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *); |
| 267 | PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); |
| 268 | PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 269 | PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); |
| 270 | PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); |
| 271 | PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); |
Raymond Hettinger | 1da1dbf | 2003-03-17 19:46:11 +0000 | [diff] [blame] | 272 | PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 273 | PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 274 | PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, PyObject *, PyObject *); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 275 | #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 |
Benjamin Peterson | 8eb1269 | 2012-02-19 19:59:10 -0500 | [diff] [blame] | 276 | PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *); |
Serhiy Storchaka | 34d0ac8 | 2016-12-27 14:57:39 +0200 | [diff] [blame] | 277 | #endif |
Benjamin Peterson | 8f67d08 | 2010-10-17 20:54:53 +0000 | [diff] [blame] | 278 | PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *); |
| 279 | PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 280 | PyAPI_FUNC(int) PyObject_IsTrue(PyObject *); |
| 281 | PyAPI_FUNC(int) PyObject_Not(PyObject *); |
| 282 | PyAPI_FUNC(int) PyCallable_Check(PyObject *); |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 283 | PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *); |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 284 | |
Georg Brandl | 1a3284e | 2007-12-02 09:40:06 +0000 | [diff] [blame] | 285 | /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a |
| 286 | list of strings. PyObject_Dir(NULL) is like builtins.dir(), |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 287 | returning the names of the current locals. In this case, if there are |
| 288 | no current locals, NULL is returned, and PyErr_Occurred() is false. |
| 289 | */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 290 | PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *); |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 291 | |
| 292 | |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 293 | /* Helpers for printing recursive container types */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 294 | PyAPI_FUNC(int) Py_ReprEnter(PyObject *); |
| 295 | PyAPI_FUNC(void) Py_ReprLeave(PyObject *); |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 296 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 297 | /* Flag bits for printing: */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 298 | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 299 | |
| 300 | /* |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 301 | Type flags (tp_flags) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 302 | |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 303 | These flags are used to change expected features and behavior for a |
| 304 | particular type. |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 305 | |
| 306 | Arbitration of the flag bit positions will need to be coordinated among |
luzpaz | a5293b4 | 2017-11-05 07:37:50 -0600 | [diff] [blame] | 307 | all extension writers who publicly release their extensions (this will |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 308 | be fewer than you might expect!). |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 309 | |
Guido van Rossum | 3cf5b1e | 2006-07-27 21:53:35 +0000 | [diff] [blame] | 310 | Most flags were removed as of Python 3.0 to make room for new flags. (Some |
| 311 | flags are not for backwards compatibility but to indicate the presence of an |
| 312 | optional feature; these flags remain of course.) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 313 | |
| 314 | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. |
| 315 | |
| 316 | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the |
| 317 | given type object has a specified feature. |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 318 | */ |
| 319 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 320 | /* Set if the type object is dynamically allocated */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 321 | #define Py_TPFLAGS_HEAPTYPE (1UL << 9) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 322 | |
| 323 | /* Set if the type allows subclassing */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 324 | #define Py_TPFLAGS_BASETYPE (1UL << 10) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 325 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 326 | /* Set if the type implements the vectorcall protocol (PEP 590) */ |
| 327 | #ifndef Py_LIMITED_API |
Petr Viktorin | 3f563ce | 2020-02-06 15:48:27 +0100 | [diff] [blame] | 328 | #define Py_TPFLAGS_HAVE_VECTORCALL (1UL << 11) |
| 329 | // Backwards compatibility alias for API that was provisional in Python 3.8 |
| 330 | #define _Py_TPFLAGS_HAVE_VECTORCALL Py_TPFLAGS_HAVE_VECTORCALL |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 331 | #endif |
| 332 | |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 333 | /* Set if the type is 'ready' -- fully initialized */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 334 | #define Py_TPFLAGS_READY (1UL << 12) |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 335 | |
| 336 | /* Set while the type is being 'readied', to prevent recursive ready calls */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 337 | #define Py_TPFLAGS_READYING (1UL << 13) |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 338 | |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 339 | /* Objects support garbage collection (see objimpl.h) */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 340 | #define Py_TPFLAGS_HAVE_GC (1UL << 14) |
Neil Schemenauer | 31ec142 | 2001-08-29 23:46:35 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 342 | /* 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] | 343 | #ifdef STACKLESS |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 344 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15) |
Christian Tismer | 6695ba8 | 2003-05-20 15:14:31 +0000 | [diff] [blame] | 345 | #else |
Christian Tismer | c26ff41 | 2003-05-23 03:33:35 +0000 | [diff] [blame] | 346 | #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0 |
Christian Tismer | 6695ba8 | 2003-05-20 15:14:31 +0000 | [diff] [blame] | 347 | #endif |
| 348 | |
Jeroen Demeyer | eb65e24 | 2019-05-28 14:42:53 +0200 | [diff] [blame] | 349 | /* Objects behave like an unbound method */ |
| 350 | #define Py_TPFLAGS_METHOD_DESCRIPTOR (1UL << 17) |
| 351 | |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 352 | /* Objects support type attribute cache */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 353 | #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18) |
| 354 | #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19) |
Christian Heimes | a62da1d | 2008-01-12 19:39:10 +0000 | [diff] [blame] | 355 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 356 | /* Type is abstract and cannot be instantiated */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 357 | #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20) |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 358 | |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 359 | /* These flags are used to determine if a type is a subclass. */ |
Victor Stinner | 4ca1cf3 | 2012-10-30 23:40:45 +0100 | [diff] [blame] | 360 | #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24) |
| 361 | #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25) |
| 362 | #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26) |
| 363 | #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27) |
| 364 | #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28) |
| 365 | #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29) |
| 366 | #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30) |
| 367 | #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31) |
Thomas Wouters | 27d517b | 2007-02-25 20:39:11 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 369 | #define Py_TPFLAGS_DEFAULT ( \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 370 | Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \ |
| 371 | Py_TPFLAGS_HAVE_VERSION_TAG | \ |
| 372 | 0) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 373 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 374 | /* NOTE: The following flags reuse lower bits (removed as part of the |
| 375 | * Python 3.0 transition). */ |
| 376 | |
Hansraj Das | 10e5c66 | 2019-07-06 03:07:15 +0530 | [diff] [blame] | 377 | /* The following flag is kept for compatibility. Starting with 3.8, |
| 378 | * binary compatibility of C extensions across feature releases of |
Antoine Pitrou | ada319b | 2019-05-29 22:12:38 +0200 | [diff] [blame] | 379 | * Python is not supported anymore, except when using the stable ABI. |
| 380 | */ |
| 381 | |
Antoine Pitrou | 796564c | 2013-07-30 19:59:21 +0200 | [diff] [blame] | 382 | /* Type structure has tp_finalize member (3.4) */ |
| 383 | #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0) |
| 384 | |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 385 | |
| 386 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 387 | 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] | 388 | reference counts. Py_DECREF calls the object's deallocator function when |
| 389 | the refcount falls to 0; for |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 390 | objects that don't contain references to other objects or heap memory |
| 391 | this can be the standard function free(). Both macros can be used |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 392 | wherever a void expression is allowed. The argument must not be a |
Benjamin Peterson | 2a691a8 | 2008-03-31 01:51:45 +0000 | [diff] [blame] | 393 | 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] | 394 | The macro _Py_NewReference(op) initialize reference counts to 1, and |
| 395 | in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional |
| 396 | bookkeeping appropriate to the special build. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | |
| 398 | We assume that the reference count field can never overflow; this can |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 399 | be proven when the size of the field is the same as the pointer size, so |
| 400 | we ignore the possibility. Provided a C int is at least 32 bits (which |
| 401 | is implicitly assumed in many parts of this code), that's enough for |
| 402 | about 2**31 references to an object. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 403 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 404 | XXX The following became out of date in Python 2.2, but I'm not sure |
| 405 | XXX what the full truth is now. Certainly, heap-allocated type objects |
| 406 | XXX can and should be deallocated. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 407 | Type objects should never be deallocated; the type pointer in an object |
| 408 | is not considered to be a reference to the type object, to save |
| 409 | complications in the deallocation function. (This is actually a |
| 410 | decision that's up to the implementer of each new type so if you want, |
| 411 | you can count such references to the type object.) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 412 | */ |
| 413 | |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 414 | #ifdef Py_REF_DEBUG |
Neal Norwitz | 4281cef | 2006-03-04 19:58:13 +0000 | [diff] [blame] | 415 | PyAPI_DATA(Py_ssize_t) _Py_RefTotal; |
Victor Stinner | 18618e65 | 2018-10-25 17:28:11 +0200 | [diff] [blame] | 416 | PyAPI_FUNC(void) _Py_NegativeRefcount(const char *filename, int lineno, |
| 417 | PyObject *op); |
Tim Peters | 57f4ddd | 2002-07-10 06:34:15 +0000 | [diff] [blame] | 418 | #endif /* Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | |
Victor Stinner | 3c09dca | 2018-10-30 14:48:26 +0100 | [diff] [blame] | 420 | PyAPI_FUNC(void) _Py_Dealloc(PyObject *); |
| 421 | |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 422 | static inline void _Py_INCREF(PyObject *op) |
| 423 | { |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 424 | #ifdef Py_REF_DEBUG |
| 425 | _Py_RefTotal++; |
| 426 | #endif |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 427 | op->ob_refcnt++; |
| 428 | } |
| 429 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 430 | #define Py_INCREF(op) _Py_INCREF(_PyObject_CAST(op)) |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 431 | |
Victor Stinner | f3a0a6b | 2020-01-08 21:03:45 +0100 | [diff] [blame] | 432 | static inline void _Py_DECREF( |
| 433 | #ifdef Py_REF_DEBUG |
| 434 | const char *filename, int lineno, |
| 435 | #endif |
| 436 | PyObject *op) |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 437 | { |
Victor Stinner | 49932fe | 2020-02-03 17:55:05 +0100 | [diff] [blame] | 438 | #ifdef Py_REF_DEBUG |
| 439 | _Py_RefTotal--; |
| 440 | #endif |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 441 | if (--op->ob_refcnt != 0) { |
| 442 | #ifdef Py_REF_DEBUG |
| 443 | if (op->ob_refcnt < 0) { |
| 444 | _Py_NegativeRefcount(filename, lineno, op); |
| 445 | } |
| 446 | #endif |
| 447 | } |
| 448 | else { |
| 449 | _Py_Dealloc(op); |
| 450 | } |
| 451 | } |
| 452 | |
Victor Stinner | f3a0a6b | 2020-01-08 21:03:45 +0100 | [diff] [blame] | 453 | #ifdef Py_REF_DEBUG |
| 454 | # define Py_DECREF(op) _Py_DECREF(__FILE__, __LINE__, _PyObject_CAST(op)) |
| 455 | #else |
| 456 | # define Py_DECREF(op) _Py_DECREF(_PyObject_CAST(op)) |
| 457 | #endif |
Victor Stinner | 2aaf0c1 | 2018-10-29 13:43:07 +0100 | [diff] [blame] | 458 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 459 | |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 460 | /* 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] | 461 | * and tp_dealloc implementations. |
Thomas Wouters | 49fd7fa | 2006-04-21 10:40:58 +0000 | [diff] [blame] | 462 | * |
| 463 | * Note that "the obvious" code can be deadly: |
| 464 | * |
| 465 | * Py_XDECREF(op); |
| 466 | * op = NULL; |
| 467 | * |
| 468 | * Typically, `op` is something like self->containee, and `self` is done |
| 469 | * using its `containee` member. In the code sequence above, suppose |
| 470 | * `containee` is non-NULL with a refcount of 1. Its refcount falls to |
| 471 | * 0 on the first line, which can trigger an arbitrary amount of code, |
| 472 | * possibly including finalizers (like __del__ methods or weakref callbacks) |
| 473 | * coded in Python, which in turn can release the GIL and allow other threads |
| 474 | * to run, etc. Such code may even invoke methods of `self` again, or cause |
| 475 | * cyclic gc to trigger, but-- oops! --self->containee still points to the |
| 476 | * object being torn down, and it may be in an insane state while being torn |
| 477 | * down. This has in fact been a rich historic source of miserable (rare & |
| 478 | * hard-to-diagnose) segfaulting (and other) bugs. |
| 479 | * |
| 480 | * The safe way is: |
| 481 | * |
| 482 | * Py_CLEAR(op); |
| 483 | * |
| 484 | * That arranges to set `op` to NULL _before_ decref'ing, so that any code |
| 485 | * triggered as a side-effect of `op` getting torn down no longer believes |
| 486 | * `op` points to a valid object. |
| 487 | * |
| 488 | * There are cases where it's safe to use the naive code, but they're brittle. |
| 489 | * For example, if `op` points to a Python integer, you know that destroying |
| 490 | * one of those can't cause problems -- but in part that relies on that |
| 491 | * Python integers aren't currently weakly referencable. Best practice is |
| 492 | * to use Py_CLEAR() even if you can't think of a reason for why you need to. |
| 493 | */ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 494 | #define Py_CLEAR(op) \ |
| 495 | do { \ |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 496 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 497 | if (_py_tmp != NULL) { \ |
Antoine Pitrou | f95a1b3 | 2010-05-09 15:52:27 +0000 | [diff] [blame] | 498 | (op) = NULL; \ |
| 499 | Py_DECREF(_py_tmp); \ |
| 500 | } \ |
| 501 | } while (0) |
Jim Fulton | 8c5aeaa | 2004-07-14 19:07:35 +0000 | [diff] [blame] | 502 | |
Victor Stinner | 541497e | 2018-10-29 20:52:41 +0100 | [diff] [blame] | 503 | /* Function to use in case the object pointer can be NULL: */ |
| 504 | static inline void _Py_XINCREF(PyObject *op) |
| 505 | { |
| 506 | if (op != NULL) { |
| 507 | Py_INCREF(op); |
| 508 | } |
| 509 | } |
Benjamin Peterson | da5eb5a | 2013-05-27 14:46:14 -0700 | [diff] [blame] | 510 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 511 | #define Py_XINCREF(op) _Py_XINCREF(_PyObject_CAST(op)) |
Victor Stinner | 541497e | 2018-10-29 20:52:41 +0100 | [diff] [blame] | 512 | |
| 513 | static inline void _Py_XDECREF(PyObject *op) |
| 514 | { |
| 515 | if (op != NULL) { |
| 516 | Py_DECREF(op); |
| 517 | } |
| 518 | } |
| 519 | |
Victor Stinner | 2ff8fb7 | 2018-11-22 02:57:29 +0100 | [diff] [blame] | 520 | #define Py_XDECREF(op) _Py_XDECREF(_PyObject_CAST(op)) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 522 | /* |
Thomas Heller | 1328b52 | 2004-04-22 17:23:49 +0000 | [diff] [blame] | 523 | These are provided as conveniences to Python runtime embedders, so that |
| 524 | they can have object code that is not dependent on Python compilation flags. |
| 525 | */ |
| 526 | PyAPI_FUNC(void) Py_IncRef(PyObject *); |
| 527 | PyAPI_FUNC(void) Py_DecRef(PyObject *); |
| 528 | |
| 529 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 530 | _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] | 531 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 532 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 533 | Don't forget to apply Py_INCREF() when returning this value!!! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 534 | */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 535 | PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 536 | #define Py_None (&_Py_NoneStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | |
Brett Cannon | d05235e | 2003-10-19 21:19:40 +0000 | [diff] [blame] | 538 | /* Macro for returning Py_None from a function */ |
Tim Peters | 5980ff2 | 2004-07-22 01:46:43 +0000 | [diff] [blame] | 539 | #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None |
Brett Cannon | d05235e | 2003-10-19 21:19:40 +0000 | [diff] [blame] | 540 | |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 541 | /* |
| 542 | Py_NotImplemented is a singleton used to signal that an operation is |
| 543 | not implemented for a given type combination. |
| 544 | */ |
Mark Hammond | a290527 | 2002-07-29 13:42:14 +0000 | [diff] [blame] | 545 | PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 546 | #define Py_NotImplemented (&_Py_NotImplementedStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 547 | |
Brian Curtin | 7d2f9e1 | 2011-08-10 20:05:21 -0500 | [diff] [blame] | 548 | /* Macro for returning Py_NotImplemented from a function */ |
| 549 | #define Py_RETURN_NOTIMPLEMENTED \ |
| 550 | return Py_INCREF(Py_NotImplemented), Py_NotImplemented |
| 551 | |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 552 | /* Rich comparison opcodes */ |
| 553 | #define Py_LT 0 |
| 554 | #define Py_LE 1 |
| 555 | #define Py_EQ 2 |
| 556 | #define Py_NE 3 |
| 557 | #define Py_GT 4 |
| 558 | #define Py_GE 5 |
| 559 | |
stratakis | e8b1965 | 2017-11-02 11:32:54 +0100 | [diff] [blame] | 560 | /* |
| 561 | * Macro for implementing rich comparisons |
| 562 | * |
| 563 | * Needs to be a macro because any C-comparable type can be used. |
| 564 | */ |
| 565 | #define Py_RETURN_RICHCOMPARE(val1, val2, op) \ |
| 566 | do { \ |
| 567 | switch (op) { \ |
| 568 | case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 569 | case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 570 | case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 571 | case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 572 | case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 573 | case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ |
| 574 | default: \ |
| 575 | Py_UNREACHABLE(); \ |
| 576 | } \ |
| 577 | } while (0) |
| 578 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 579 | |
| 580 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 581 | More conventions |
| 582 | ================ |
| 583 | |
| 584 | Argument Checking |
| 585 | ----------------- |
| 586 | |
| 587 | Functions that take objects as arguments normally don't check for nil |
| 588 | arguments, but they do check the type of the argument, and return an |
| 589 | error if the function doesn't apply to the type. |
| 590 | |
| 591 | Failure Modes |
| 592 | ------------- |
| 593 | |
| 594 | 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] | 595 | memory. This is communicated to the caller in two ways: an error string |
| 596 | is set (see errors.h), and the function result differs: functions that |
| 597 | normally return a pointer return NULL for failure, functions returning |
| 598 | an integer return -1 (which could be a legal return value too!), and |
| 599 | other functions return 0 for success and -1 for failure. |
Tim Peters | 4be93d0 | 2002-07-07 19:59:50 +0000 | [diff] [blame] | 600 | Callers should always check for errors before using the result. If |
| 601 | an error was set, the caller must either explicitly clear it, or pass |
| 602 | the error on to its caller. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 603 | |
| 604 | Reference Counts |
| 605 | ---------------- |
| 606 | |
| 607 | It takes a while to get used to the proper usage of reference counts. |
| 608 | |
| 609 | 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] | 610 | objects must be stored somewhere or destroyed again with Py_DECREF(). |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 611 | Some functions that 'store' objects, such as PyTuple_SetItem() and |
| 612 | PyList_SetItem(), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 613 | don't increment the reference count of the object, since the most |
| 614 | frequent use is to store a fresh object. Functions that 'retrieve' |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 615 | objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 616 | don't increment |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | the reference count, since most frequently the object is only looked at |
| 618 | 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] | 619 | must call Py_INCREF() explicitly. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 620 | |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 621 | NOTE: functions that 'consume' a reference count, like |
| 622 | PyList_SetItem(), consume the reference even if the object wasn't |
| 623 | successfully stored, to simplify error handling. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 624 | |
| 625 | It seems attractive to make other functions that take an object as |
Alex Martelli | 721b776 | 2003-11-09 16:38:39 +0000 | [diff] [blame] | 626 | argument consume a reference count; however, this may quickly get |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 627 | confusing (even the current practice is already confusing). Consider |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 628 | 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] | 629 | times. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 630 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 631 | |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 632 | #ifndef Py_LIMITED_API |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 633 | # define Py_CPYTHON_OBJECT_H |
| 634 | # include "cpython/object.h" |
| 635 | # undef Py_CPYTHON_OBJECT_H |
Victor Stinner | 626bff8 | 2018-10-25 17:31:10 +0200 | [diff] [blame] | 636 | #endif |
| 637 | |
Victor Stinner | 509dd90 | 2020-02-05 14:24:17 +0100 | [diff] [blame] | 638 | |
| 639 | static inline int |
| 640 | PyType_HasFeature(PyTypeObject *type, unsigned long feature) { |
Victor Stinner | 509dd90 | 2020-02-05 14:24:17 +0100 | [diff] [blame] | 641 | return ((PyType_GetFlags(type) & feature) != 0); |
Victor Stinner | 509dd90 | 2020-02-05 14:24:17 +0100 | [diff] [blame] | 642 | } |
| 643 | |
| 644 | #define PyType_FastSubclass(type, flag) PyType_HasFeature(type, flag) |
| 645 | |
| 646 | static inline int _PyType_Check(PyObject *op) { |
| 647 | return PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS); |
| 648 | } |
| 649 | #define PyType_Check(op) _PyType_Check(_PyObject_CAST(op)) |
| 650 | |
| 651 | static inline int _PyType_CheckExact(PyObject *op) { |
Dong-hee Na | d905df7 | 2020-02-14 02:37:17 +0900 | [diff] [blame] | 652 | return Py_IS_TYPE(op, &PyType_Type); |
Victor Stinner | 509dd90 | 2020-02-05 14:24:17 +0100 | [diff] [blame] | 653 | } |
| 654 | #define PyType_CheckExact(op) _PyType_CheckExact(_PyObject_CAST(op)) |
| 655 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 656 | #ifdef __cplusplus |
| 657 | } |
| 658 | #endif |
| 659 | #endif /* !Py_OBJECT_H */ |