Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 1 | #ifndef Py_CPYTHON_OBJECT_H |
| 2 | # error "this header file must not be included directly" |
| 3 | #endif |
| 4 | |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 5 | PyAPI_FUNC(void) _Py_NewReference(PyObject *op); |
| 6 | |
| 7 | #ifdef Py_TRACE_REFS |
| 8 | /* Py_TRACE_REFS is such major surgery that we call external routines. */ |
| 9 | PyAPI_FUNC(void) _Py_ForgetReference(PyObject *); |
| 10 | #endif |
| 11 | |
Victor Stinner | f58bd7c | 2020-02-05 13:12:19 +0100 | [diff] [blame] | 12 | #ifdef Py_REF_DEBUG |
| 13 | PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void); |
| 14 | #endif |
| 15 | |
| 16 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 17 | /********************* String Literals ****************************************/ |
| 18 | /* This structure helps managing static strings. The basic usage goes like this: |
| 19 | Instead of doing |
| 20 | |
| 21 | r = PyObject_CallMethod(o, "foo", "args", ...); |
| 22 | |
| 23 | do |
| 24 | |
| 25 | _Py_IDENTIFIER(foo); |
| 26 | ... |
| 27 | r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...); |
| 28 | |
| 29 | PyId_foo is a static variable, either on block level or file level. On first |
| 30 | usage, the string "foo" is interned, and the structures are linked. On interpreter |
Victor Stinner | d6fb53f | 2020-05-14 01:11:54 +0200 | [diff] [blame] | 31 | shutdown, all strings are released. |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 32 | |
| 33 | Alternatively, _Py_static_string allows choosing the variable name. |
| 34 | _PyUnicode_FromId returns a borrowed reference to the interned string. |
| 35 | _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*. |
| 36 | */ |
| 37 | typedef struct _Py_Identifier { |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 38 | const char* string; |
Victor Stinner | ba3d67c | 2020-12-26 00:41:46 +0100 | [diff] [blame] | 39 | // Index in PyInterpreterState.unicode.ids.array. It is process-wide |
| 40 | // unique and must be initialized to -1. |
| 41 | Py_ssize_t index; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 42 | } _Py_Identifier; |
| 43 | |
Victor Stinner | ba3d67c | 2020-12-26 00:41:46 +0100 | [diff] [blame] | 44 | #define _Py_static_string_init(value) { .string = value, .index = -1 } |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 45 | #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value) |
| 46 | #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) |
| 47 | |
| 48 | /* buffer interface */ |
| 49 | typedef struct bufferinfo { |
| 50 | void *buf; |
| 51 | PyObject *obj; /* owned reference */ |
| 52 | Py_ssize_t len; |
| 53 | Py_ssize_t itemsize; /* This is Py_ssize_t so it can be |
| 54 | pointed to by strides in simple case.*/ |
| 55 | int readonly; |
| 56 | int ndim; |
| 57 | char *format; |
| 58 | Py_ssize_t *shape; |
| 59 | Py_ssize_t *strides; |
| 60 | Py_ssize_t *suboffsets; |
| 61 | void *internal; |
| 62 | } Py_buffer; |
| 63 | |
| 64 | typedef int (*getbufferproc)(PyObject *, Py_buffer *, int); |
| 65 | typedef void (*releasebufferproc)(PyObject *, Py_buffer *); |
| 66 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 67 | typedef PyObject *(*vectorcallfunc)(PyObject *callable, PyObject *const *args, |
| 68 | size_t nargsf, PyObject *kwnames); |
| 69 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 70 | /* Maximum number of dimensions */ |
| 71 | #define PyBUF_MAX_NDIM 64 |
| 72 | |
| 73 | /* Flags for getting buffers */ |
| 74 | #define PyBUF_SIMPLE 0 |
| 75 | #define PyBUF_WRITABLE 0x0001 |
| 76 | /* we used to include an E, backwards compatible alias */ |
| 77 | #define PyBUF_WRITEABLE PyBUF_WRITABLE |
| 78 | #define PyBUF_FORMAT 0x0004 |
| 79 | #define PyBUF_ND 0x0008 |
| 80 | #define PyBUF_STRIDES (0x0010 | PyBUF_ND) |
| 81 | #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) |
| 82 | #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) |
| 83 | #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) |
| 84 | #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) |
| 85 | |
| 86 | #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE) |
| 87 | #define PyBUF_CONTIG_RO (PyBUF_ND) |
| 88 | |
| 89 | #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE) |
| 90 | #define PyBUF_STRIDED_RO (PyBUF_STRIDES) |
| 91 | |
| 92 | #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT) |
| 93 | #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT) |
| 94 | |
| 95 | #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT) |
| 96 | #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT) |
| 97 | |
| 98 | |
| 99 | #define PyBUF_READ 0x100 |
| 100 | #define PyBUF_WRITE 0x200 |
| 101 | /* End buffer interface */ |
| 102 | |
| 103 | |
| 104 | typedef struct { |
| 105 | /* Number implementations must check *both* |
| 106 | arguments for proper type and implement the necessary conversions |
| 107 | in the slot functions themselves. */ |
| 108 | |
| 109 | binaryfunc nb_add; |
| 110 | binaryfunc nb_subtract; |
| 111 | binaryfunc nb_multiply; |
| 112 | binaryfunc nb_remainder; |
| 113 | binaryfunc nb_divmod; |
| 114 | ternaryfunc nb_power; |
| 115 | unaryfunc nb_negative; |
| 116 | unaryfunc nb_positive; |
| 117 | unaryfunc nb_absolute; |
| 118 | inquiry nb_bool; |
| 119 | unaryfunc nb_invert; |
| 120 | binaryfunc nb_lshift; |
| 121 | binaryfunc nb_rshift; |
| 122 | binaryfunc nb_and; |
| 123 | binaryfunc nb_xor; |
| 124 | binaryfunc nb_or; |
| 125 | unaryfunc nb_int; |
| 126 | void *nb_reserved; /* the slot formerly known as nb_long */ |
| 127 | unaryfunc nb_float; |
| 128 | |
| 129 | binaryfunc nb_inplace_add; |
| 130 | binaryfunc nb_inplace_subtract; |
| 131 | binaryfunc nb_inplace_multiply; |
| 132 | binaryfunc nb_inplace_remainder; |
| 133 | ternaryfunc nb_inplace_power; |
| 134 | binaryfunc nb_inplace_lshift; |
| 135 | binaryfunc nb_inplace_rshift; |
| 136 | binaryfunc nb_inplace_and; |
| 137 | binaryfunc nb_inplace_xor; |
| 138 | binaryfunc nb_inplace_or; |
| 139 | |
| 140 | binaryfunc nb_floor_divide; |
| 141 | binaryfunc nb_true_divide; |
| 142 | binaryfunc nb_inplace_floor_divide; |
| 143 | binaryfunc nb_inplace_true_divide; |
| 144 | |
| 145 | unaryfunc nb_index; |
| 146 | |
| 147 | binaryfunc nb_matrix_multiply; |
| 148 | binaryfunc nb_inplace_matrix_multiply; |
| 149 | } PyNumberMethods; |
| 150 | |
| 151 | typedef struct { |
| 152 | lenfunc sq_length; |
| 153 | binaryfunc sq_concat; |
| 154 | ssizeargfunc sq_repeat; |
| 155 | ssizeargfunc sq_item; |
| 156 | void *was_sq_slice; |
| 157 | ssizeobjargproc sq_ass_item; |
| 158 | void *was_sq_ass_slice; |
| 159 | objobjproc sq_contains; |
| 160 | |
| 161 | binaryfunc sq_inplace_concat; |
| 162 | ssizeargfunc sq_inplace_repeat; |
| 163 | } PySequenceMethods; |
| 164 | |
| 165 | typedef struct { |
| 166 | lenfunc mp_length; |
| 167 | binaryfunc mp_subscript; |
| 168 | objobjargproc mp_ass_subscript; |
| 169 | } PyMappingMethods; |
| 170 | |
Vladimir Matveev | 1e996c3 | 2020-11-10 12:09:55 -0800 | [diff] [blame] | 171 | typedef PySendResult (*sendfunc)(PyObject *iter, PyObject *value, PyObject **result); |
| 172 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 173 | typedef struct { |
| 174 | unaryfunc am_await; |
| 175 | unaryfunc am_aiter; |
| 176 | unaryfunc am_anext; |
Vladimir Matveev | 1e996c3 | 2020-11-10 12:09:55 -0800 | [diff] [blame] | 177 | sendfunc am_send; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 178 | } PyAsyncMethods; |
| 179 | |
| 180 | typedef struct { |
| 181 | getbufferproc bf_getbuffer; |
| 182 | releasebufferproc bf_releasebuffer; |
| 183 | } PyBufferProcs; |
| 184 | |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 185 | /* Allow printfunc in the tp_vectorcall_offset slot for |
| 186 | * backwards-compatibility */ |
| 187 | typedef Py_ssize_t printfunc; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 188 | |
Victor Stinner | f95cd19 | 2020-02-07 01:43:25 +0100 | [diff] [blame] | 189 | struct _typeobject { |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 190 | PyObject_VAR_HEAD |
| 191 | const char *tp_name; /* For printing, in format "<module>.<name>" */ |
| 192 | Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ |
| 193 | |
| 194 | /* Methods to implement standard operations */ |
| 195 | |
| 196 | destructor tp_dealloc; |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 197 | Py_ssize_t tp_vectorcall_offset; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 198 | getattrfunc tp_getattr; |
| 199 | setattrfunc tp_setattr; |
| 200 | PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2) |
| 201 | or tp_reserved (Python 3) */ |
| 202 | reprfunc tp_repr; |
| 203 | |
| 204 | /* Method suites for standard classes */ |
| 205 | |
| 206 | PyNumberMethods *tp_as_number; |
| 207 | PySequenceMethods *tp_as_sequence; |
| 208 | PyMappingMethods *tp_as_mapping; |
| 209 | |
| 210 | /* More standard operations (here for binary compatibility) */ |
| 211 | |
| 212 | hashfunc tp_hash; |
| 213 | ternaryfunc tp_call; |
| 214 | reprfunc tp_str; |
| 215 | getattrofunc tp_getattro; |
| 216 | setattrofunc tp_setattro; |
| 217 | |
| 218 | /* Functions to access object as input/output buffer */ |
| 219 | PyBufferProcs *tp_as_buffer; |
| 220 | |
| 221 | /* Flags to define presence of optional/expanded features */ |
| 222 | unsigned long tp_flags; |
| 223 | |
| 224 | const char *tp_doc; /* Documentation string */ |
| 225 | |
| 226 | /* Assigned meaning in release 2.0 */ |
| 227 | /* call function for all accessible objects */ |
| 228 | traverseproc tp_traverse; |
| 229 | |
| 230 | /* delete references to contained objects */ |
| 231 | inquiry tp_clear; |
| 232 | |
| 233 | /* Assigned meaning in release 2.1 */ |
| 234 | /* rich comparisons */ |
| 235 | richcmpfunc tp_richcompare; |
| 236 | |
| 237 | /* weak reference enabler */ |
| 238 | Py_ssize_t tp_weaklistoffset; |
| 239 | |
| 240 | /* Iterators */ |
| 241 | getiterfunc tp_iter; |
| 242 | iternextfunc tp_iternext; |
| 243 | |
| 244 | /* Attribute descriptor and subclassing stuff */ |
| 245 | struct PyMethodDef *tp_methods; |
| 246 | struct PyMemberDef *tp_members; |
| 247 | struct PyGetSetDef *tp_getset; |
Victor Stinner | ba2958e | 2020-11-11 14:27:32 +0100 | [diff] [blame] | 248 | // Strong reference on a heap type, borrowed reference on a static type |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 249 | struct _typeobject *tp_base; |
| 250 | PyObject *tp_dict; |
| 251 | descrgetfunc tp_descr_get; |
| 252 | descrsetfunc tp_descr_set; |
| 253 | Py_ssize_t tp_dictoffset; |
| 254 | initproc tp_init; |
| 255 | allocfunc tp_alloc; |
| 256 | newfunc tp_new; |
| 257 | freefunc tp_free; /* Low-level free-memory routine */ |
| 258 | inquiry tp_is_gc; /* For PyObject_IS_GC */ |
| 259 | PyObject *tp_bases; |
| 260 | PyObject *tp_mro; /* method resolution order */ |
| 261 | PyObject *tp_cache; |
| 262 | PyObject *tp_subclasses; |
| 263 | PyObject *tp_weaklist; |
| 264 | destructor tp_del; |
| 265 | |
| 266 | /* Type attribute cache version tag. Added in version 2.6 */ |
| 267 | unsigned int tp_version_tag; |
| 268 | |
| 269 | destructor tp_finalize; |
Jeroen Demeyer | aacc77f | 2019-05-29 20:31:52 +0200 | [diff] [blame] | 270 | vectorcallfunc tp_vectorcall; |
Victor Stinner | f95cd19 | 2020-02-07 01:43:25 +0100 | [diff] [blame] | 271 | }; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 272 | |
| 273 | /* The *real* layout of a type object when allocated on the heap */ |
| 274 | typedef struct _heaptypeobject { |
| 275 | /* Note: there's a dependency on the order of these members |
| 276 | in slotptr() in typeobject.c . */ |
| 277 | PyTypeObject ht_type; |
| 278 | PyAsyncMethods as_async; |
| 279 | PyNumberMethods as_number; |
| 280 | PyMappingMethods as_mapping; |
| 281 | PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, |
| 282 | so that the mapping wins when both |
| 283 | the mapping and the sequence define |
| 284 | a given operator (e.g. __getitem__). |
| 285 | see add_operators() in typeobject.c . */ |
| 286 | PyBufferProcs as_buffer; |
| 287 | PyObject *ht_name, *ht_slots, *ht_qualname; |
| 288 | struct _dictkeysobject *ht_cached_keys; |
Petr Viktorin | e1becf4 | 2020-05-07 15:39:59 +0200 | [diff] [blame] | 289 | PyObject *ht_module; |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 290 | /* here are optional user slots, followed by the members. */ |
| 291 | } PyHeapTypeObject; |
| 292 | |
| 293 | /* access macro to the members which are floating "behind" the object */ |
| 294 | #define PyHeapType_GET_MEMBERS(etype) \ |
| 295 | ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize)) |
| 296 | |
| 297 | PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *); |
| 298 | PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *); |
| 299 | PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *); |
| 300 | PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *); |
| 301 | PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *); |
| 302 | PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *); |
| 303 | PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *); |
Petr Viktorin | 57aaaa8 | 2020-11-03 22:27:12 +0100 | [diff] [blame] | 304 | struct PyModuleDef; |
| 305 | PyAPI_FUNC(PyObject *) _PyType_GetModuleByDef(PyTypeObject *, struct PyModuleDef *); |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 306 | |
| 307 | struct _Py_Identifier; |
| 308 | PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int); |
| 309 | PyAPI_FUNC(void) _Py_BreakPoint(void); |
| 310 | PyAPI_FUNC(void) _PyObject_Dump(PyObject *); |
| 311 | PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *); |
| 312 | |
| 313 | PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); |
| 314 | PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); |
| 315 | PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 316 | /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which |
| 317 | don't raise AttributeError. |
| 318 | |
| 319 | Return 1 and set *result != NULL if an attribute is found. |
| 320 | Return 0 and set *result == NULL if an attribute is not found; |
| 321 | an AttributeError is silenced. |
| 322 | Return -1 and set *result == NULL if an error other than AttributeError |
| 323 | is raised. |
| 324 | */ |
| 325 | PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **); |
| 326 | PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **); |
Jeroen Demeyer | b2f9473 | 2019-06-14 12:37:15 +0200 | [diff] [blame] | 327 | |
| 328 | PyAPI_FUNC(int) _PyObject_GetMethod(PyObject *obj, PyObject *name, PyObject **method); |
| 329 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 330 | PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); |
| 331 | PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *); |
| 332 | PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *); |
| 333 | PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *); |
| 334 | |
| 335 | /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes |
| 336 | dict as the last parameter. */ |
| 337 | PyAPI_FUNC(PyObject *) |
| 338 | _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int); |
| 339 | PyAPI_FUNC(int) |
| 340 | _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *, |
| 341 | PyObject *, PyObject *); |
| 342 | |
Jeroen Demeyer | bf17d41 | 2019-11-05 16:48:04 +0100 | [diff] [blame] | 343 | PyAPI_FUNC(PyObject *) _PyObject_FunctionStr(PyObject *); |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 344 | |
| 345 | /* Safely decref `op` and set `op` to `op2`. |
| 346 | * |
| 347 | * As in case of Py_CLEAR "the obvious" code can be deadly: |
| 348 | * |
| 349 | * Py_DECREF(op); |
| 350 | * op = op2; |
| 351 | * |
| 352 | * The safe way is: |
| 353 | * |
| 354 | * Py_SETREF(op, op2); |
| 355 | * |
| 356 | * That arranges to set `op` to `op2` _before_ decref'ing, so that any code |
| 357 | * triggered as a side-effect of `op` getting torn down no longer believes |
| 358 | * `op` points to a valid object. |
| 359 | * |
| 360 | * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of |
| 361 | * Py_DECREF. |
| 362 | */ |
| 363 | |
| 364 | #define Py_SETREF(op, op2) \ |
| 365 | do { \ |
| 366 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
| 367 | (op) = (op2); \ |
| 368 | Py_DECREF(_py_tmp); \ |
| 369 | } while (0) |
| 370 | |
| 371 | #define Py_XSETREF(op, op2) \ |
| 372 | do { \ |
| 373 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
| 374 | (op) = (op2); \ |
| 375 | Py_XDECREF(_py_tmp); \ |
| 376 | } while (0) |
| 377 | |
| 378 | |
| 379 | PyAPI_DATA(PyTypeObject) _PyNone_Type; |
| 380 | PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type; |
| 381 | |
| 382 | /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE. |
| 383 | * Defined in object.c. |
| 384 | */ |
| 385 | PyAPI_DATA(int) _Py_SwappedOp[]; |
| 386 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 387 | PyAPI_FUNC(void) |
| 388 | _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks, |
| 389 | size_t sizeof_block); |
| 390 | PyAPI_FUNC(void) |
| 391 | _PyObject_DebugTypeStats(FILE *out); |
| 392 | |
| 393 | /* Define a pair of assertion macros: |
| 394 | _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT(). |
| 395 | |
| 396 | These work like the regular C assert(), in that they will abort the |
| 397 | process with a message on stderr if the given condition fails to hold, |
| 398 | but compile away to nothing if NDEBUG is defined. |
| 399 | |
| 400 | However, before aborting, Python will also try to call _PyObject_Dump() on |
| 401 | the given object. This may be of use when investigating bugs in which a |
| 402 | particular object is corrupt (e.g. buggy a tp_visit method in an extension |
| 403 | module breaking the garbage collector), to help locate the broken objects. |
| 404 | |
| 405 | The WITH_MSG variant allows you to supply an additional message that Python |
| 406 | will attempt to print to stderr, after the object dump. */ |
| 407 | #ifdef NDEBUG |
| 408 | /* No debugging: compile away the assertions: */ |
| 409 | # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \ |
| 410 | ((void)0) |
| 411 | #else |
| 412 | /* With debugging: generate checks: */ |
| 413 | # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \ |
| 414 | ((expr) \ |
| 415 | ? (void)(0) \ |
| 416 | : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \ |
| 417 | (msg), (filename), (lineno), (func))) |
| 418 | #endif |
| 419 | |
| 420 | #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \ |
| 421 | _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__) |
| 422 | #define _PyObject_ASSERT(obj, expr) \ |
| 423 | _PyObject_ASSERT_WITH_MSG(obj, expr, NULL) |
| 424 | |
| 425 | #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \ |
| 426 | _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__) |
| 427 | |
| 428 | /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined, |
| 429 | to avoid causing compiler/linker errors when building extensions without |
| 430 | NDEBUG against a Python built with NDEBUG defined. |
| 431 | |
| 432 | msg, expr and function can be NULL. */ |
Victor Stinner | 2a4903f | 2020-01-30 13:09:11 +0100 | [diff] [blame] | 433 | PyAPI_FUNC(void) _Py_NO_RETURN _PyObject_AssertFailed( |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 434 | PyObject *obj, |
| 435 | const char *expr, |
| 436 | const char *msg, |
| 437 | const char *file, |
| 438 | int line, |
| 439 | const char *function); |
| 440 | |
Victor Stinner | 0fc91ee | 2019-04-12 21:51:34 +0200 | [diff] [blame] | 441 | /* Check if an object is consistent. For example, ensure that the reference |
| 442 | counter is greater than or equal to 1, and ensure that ob_type is not NULL. |
| 443 | |
| 444 | Call _PyObject_AssertFailed() if the object is inconsistent. |
| 445 | |
| 446 | If check_content is zero, only check header fields: reduce the overhead. |
| 447 | |
| 448 | The function always return 1. The return value is just here to be able to |
| 449 | write: |
| 450 | |
| 451 | assert(_PyObject_CheckConsistency(obj, 1)); */ |
| 452 | PyAPI_FUNC(int) _PyObject_CheckConsistency( |
| 453 | PyObject *op, |
| 454 | int check_content); |
| 455 | |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 456 | |
| 457 | /* Trashcan mechanism, thanks to Christian Tismer. |
| 458 | |
| 459 | When deallocating a container object, it's possible to trigger an unbounded |
| 460 | chain of deallocations, as each Py_DECREF in turn drops the refcount on "the |
| 461 | next" object in the chain to 0. This can easily lead to stack overflows, |
| 462 | especially in threads (which typically have less stack space to work with). |
| 463 | |
| 464 | A container object can avoid this by bracketing the body of its tp_dealloc |
| 465 | function with a pair of macros: |
| 466 | |
| 467 | static void |
| 468 | mytype_dealloc(mytype *p) |
| 469 | { |
| 470 | ... declarations go here ... |
| 471 | |
| 472 | PyObject_GC_UnTrack(p); // must untrack first |
| 473 | Py_TRASHCAN_BEGIN(p, mytype_dealloc) |
| 474 | ... The body of the deallocator goes here, including all calls ... |
| 475 | ... to Py_DECREF on contained objects. ... |
| 476 | Py_TRASHCAN_END // there should be no code after this |
| 477 | } |
| 478 | |
| 479 | CAUTION: Never return from the middle of the body! If the body needs to |
| 480 | "get out early", put a label immediately before the Py_TRASHCAN_END |
| 481 | call, and goto it. Else the call-depth counter (see below) will stay |
| 482 | above 0 forever, and the trashcan will never get emptied. |
| 483 | |
| 484 | How it works: The BEGIN macro increments a call-depth counter. So long |
| 485 | as this counter is small, the body of the deallocator is run directly without |
| 486 | further ado. But if the counter gets large, it instead adds p to a list of |
| 487 | objects to be deallocated later, skips the body of the deallocator, and |
| 488 | resumes execution after the END macro. The tp_dealloc routine then returns |
| 489 | without deallocating anything (and so unbounded call-stack depth is avoided). |
| 490 | |
| 491 | When the call stack finishes unwinding again, code generated by the END macro |
| 492 | notices this, and calls another routine to deallocate all the objects that |
| 493 | may have been added to the list of deferred deallocations. In effect, a |
| 494 | chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces, |
| 495 | with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL. |
| 496 | |
| 497 | Since the tp_dealloc of a subclass typically calls the tp_dealloc of the base |
| 498 | class, we need to ensure that the trashcan is only triggered on the tp_dealloc |
| 499 | of the actual class being deallocated. Otherwise we might end up with a |
| 500 | partially-deallocated object. To check this, the tp_dealloc function must be |
| 501 | passed as second argument to Py_TRASHCAN_BEGIN(). |
| 502 | */ |
| 503 | |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 504 | /* This is the old private API, invoked by the macros before 3.2.4. |
| 505 | Kept for binary compatibility of extensions using the stable ABI. */ |
| 506 | PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*); |
| 507 | PyAPI_FUNC(void) _PyTrash_destroy_chain(void); |
| 508 | |
| 509 | /* This is the old private API, invoked by the macros before 3.9. |
| 510 | Kept for binary compatibility of extensions using the stable ABI. */ |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 511 | PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*); |
| 512 | PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void); |
| 513 | |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 514 | /* Forward declarations for PyThreadState */ |
| 515 | struct _ts; |
| 516 | |
| 517 | /* Python 3.9 private API, invoked by the macros below. */ |
| 518 | PyAPI_FUNC(int) _PyTrash_begin(struct _ts *tstate, PyObject *op); |
| 519 | PyAPI_FUNC(void) _PyTrash_end(struct _ts *tstate); |
Hai Shi | ed1a5a5 | 2020-11-25 06:03:31 +0800 | [diff] [blame] | 520 | /* Python 3.10 private API, invoked by the Py_TRASHCAN_BEGIN(). */ |
| 521 | PyAPI_FUNC(int) _PyTrash_cond(PyObject *op, destructor dealloc); |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 522 | |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 523 | #define PyTrash_UNWIND_LEVEL 50 |
| 524 | |
| 525 | #define Py_TRASHCAN_BEGIN_CONDITION(op, cond) \ |
| 526 | do { \ |
| 527 | PyThreadState *_tstate = NULL; \ |
| 528 | /* If "cond" is false, then _tstate remains NULL and the deallocator \ |
| 529 | * is run normally without involving the trashcan */ \ |
| 530 | if (cond) { \ |
Victor Stinner | 6207810 | 2021-02-19 13:21:51 +0100 | [diff] [blame^] | 531 | _tstate = PyThreadState_Get(); \ |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 532 | if (_PyTrash_begin(_tstate, _PyObject_CAST(op))) { \ |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 533 | break; \ |
| 534 | } \ |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 535 | } |
| 536 | /* The body of the deallocator is here. */ |
| 537 | #define Py_TRASHCAN_END \ |
| 538 | if (_tstate) { \ |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 539 | _PyTrash_end(_tstate); \ |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 540 | } \ |
| 541 | } while (0); |
| 542 | |
Victor Stinner | 38965ec | 2020-03-13 16:51:52 +0100 | [diff] [blame] | 543 | #define Py_TRASHCAN_BEGIN(op, dealloc) \ |
| 544 | Py_TRASHCAN_BEGIN_CONDITION(op, \ |
Hai Shi | ed1a5a5 | 2020-11-25 06:03:31 +0800 | [diff] [blame] | 545 | _PyTrash_cond(_PyObject_CAST(op), (destructor)dealloc)) |
Victor Stinner | 0fa4f43 | 2020-02-05 12:23:27 +0100 | [diff] [blame] | 546 | |
| 547 | /* For backwards compatibility, these macros enable the trashcan |
| 548 | * unconditionally */ |
| 549 | #define Py_TRASHCAN_SAFE_BEGIN(op) Py_TRASHCAN_BEGIN_CONDITION(op, 1) |
| 550 | #define Py_TRASHCAN_SAFE_END(op) Py_TRASHCAN_END |