1 #ifndef Py_OBJECT_H 2 #define Py_OBJECT_H 3 #ifdef __cplusplus 4 extern "C" { 5 #endif 6 7 8 /* Object and type object interface */ 9 10 /* 11 Objects are structures allocated on the heap. Special rules apply to 12 the use of objects to ensure they are properly garbage-collected. 13 Objects are never allocated statically or on the stack; they must be 14 accessed through special macros and functions only. (Type objects are 15 exceptions to the first rule; the standard types are represented by 16 statically initialized type objects, although work on type/class unification 17 for Python 2.2 made it possible to have heap-allocated type objects too). 18 19 An object has a 'reference count' that is increased or decreased when a 20 pointer to the object is copied or deleted; when the reference count 21 reaches zero there are no references to the object left and it can be 22 removed from the heap. 23 24 An object has a 'type' that determines what it represents and what kind 25 of data it contains. An object's type is fixed when it is created. 26 Types themselves are represented as objects; an object contains a 27 pointer to the corresponding type object. The type itself has a type 28 pointer pointing to the object representing the type 'type', which 29 contains a pointer to itself!). 30 31 Objects do not float around in memory; once allocated an object keeps 32 the same size and address. Objects that must hold variable-size data 33 can contain pointers to variable-size parts of the object. Not all 34 objects of the same type have the same size; but the size cannot change 35 after allocation. (These restrictions are made so a reference to an 36 object can be simply a pointer -- moving an object would require 37 updating all the pointers, and changing an object's size would require 38 moving it if there was another object right next to it.) 39 40 Objects are always accessed through pointers of the type 'PyObject *'. 41 The type 'PyObject' is a structure that only contains the reference count 42 and the type pointer. The actual memory allocated for an object 43 contains other data that can only be accessed after casting the pointer 44 to a pointer to a longer structure type. This longer type must start 45 with the reference count and type fields; the macro PyObject_HEAD should be 46 used for this (to accommodate for future changes). The implementation 47 of a particular object type can cast the object pointer to the proper 48 type and back. 49 50 A standard interface exists for objects that contain an array of items 51 whose size is determined when the object is allocated. 52 */ 53 54 /* Py_DEBUG implies Py_TRACE_REFS. */ 55 #if defined(Py_DEBUG) && !defined(Py_TRACE_REFS) 56 #define Py_TRACE_REFS 57 #endif 58 59 /* Py_TRACE_REFS implies Py_REF_DEBUG. */ 60 #if defined(Py_TRACE_REFS) && !defined(Py_REF_DEBUG) 61 #define Py_REF_DEBUG 62 #endif 63 64 #if defined(Py_LIMITED_API) && defined(Py_REF_DEBUG) 65 #error Py_LIMITED_API is incompatible with Py_DEBUG, Py_TRACE_REFS, and Py_REF_DEBUG 66 #endif 67 68 69 #ifdef Py_TRACE_REFS 70 /* Define pointers to support a doubly-linked list of all live heap objects. */ 71 #define _PyObject_HEAD_EXTRA \ 72 struct _object *_ob_next; \ 73 struct _object *_ob_prev; 74 75 #define _PyObject_EXTRA_INIT 0, 0, 76 77 #else 78 #define _PyObject_HEAD_EXTRA 79 #define _PyObject_EXTRA_INIT 80 #endif 81 82 /* PyObject_HEAD defines the initial segment of every PyObject. */ 83 #define PyObject_HEAD PyObject ob_base; 84 85 #define PyObject_HEAD_INIT(type) \ 86 { _PyObject_EXTRA_INIT \ 87 1, type }, 88 89 #define PyVarObject_HEAD_INIT(type, size) \ 90 { PyObject_HEAD_INIT(type) size }, 91 92 /* PyObject_VAR_HEAD defines the initial segment of all variable-size 93 * container objects. These end with a declaration of an array with 1 94 * element, but enough space is malloc'ed so that the array actually 95 * has room for ob_size elements. Note that ob_size is an element count, 96 * not necessarily a byte count. 97 */ 98 #define PyObject_VAR_HEAD PyVarObject ob_base; 99 #define Py_INVALID_SIZE (Py_ssize_t)-1 100 101 /* Nothing is actually declared to be a PyObject, but every pointer to 102 * a Python object can be cast to a PyObject*. This is inheritance built 103 * by hand. Similarly every pointer to a variable-size Python object can, 104 * in addition, be cast to PyVarObject*. 105 */ 106 typedef struct _object { 107 _PyObject_HEAD_EXTRA 108 Py_ssize_t ob_refcnt; 109 struct _typeobject *ob_type; 110 } PyObject; 111 112 typedef struct { 113 PyObject ob_base; 114 Py_ssize_t ob_size; /* Number of items in variable part */ 115 } PyVarObject; 116 117 #define Py_REFCNT(ob) (((PyObject*)(ob))->ob_refcnt) 118 #define Py_TYPE(ob) (((PyObject*)(ob))->ob_type) 119 #define Py_SIZE(ob) (((PyVarObject*)(ob))->ob_size) 120 121 #ifndef Py_LIMITED_API 122 /********************* String Literals ****************************************/ 123 /* This structure helps managing static strings. The basic usage goes like this: 124 Instead of doing 125 126 r = PyObject_CallMethod(o, "foo", "args", ...); 127 128 do 129 130 _Py_IDENTIFIER(foo); 131 ... 132 r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...); 133 134 PyId_foo is a static variable, either on block level or file level. On first 135 usage, the string "foo" is interned, and the structures are linked. On interpreter 136 shutdown, all strings are released (through _PyUnicode_ClearStaticStrings). 137 138 Alternatively, _Py_static_string allows choosing the variable name. 139 _PyUnicode_FromId returns a borrowed reference to the interned string. 140 _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*. 141 */ 142 typedef struct _Py_Identifier { 143 struct _Py_Identifier *next; 144 const char* string; 145 PyObject *object; 146 } _Py_Identifier; 147 148 #define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL } 149 #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value) 150 #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) 151 152 #endif /* !Py_LIMITED_API */ 153 154 /* 155 Type objects contain a string containing the type name (to help somewhat 156 in debugging), the allocation parameters (see PyObject_New() and 157 PyObject_NewVar()), 158 and methods for accessing objects of the type. Methods are optional, a 159 nil pointer meaning that particular kind of access is not available for 160 this type. The Py_DECREF() macro uses the tp_dealloc method without 161 checking for a nil pointer; it should always be implemented except if 162 the implementation can guarantee that the reference count will never 163 reach zero (e.g., for statically allocated type objects). 164 165 NB: the methods for certain type groups are now contained in separate 166 method blocks. 167 */ 168 169 typedef PyObject * (*unaryfunc)(PyObject *); 170 typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); 171 typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); 172 typedef int (*inquiry)(PyObject *); 173 typedef Py_ssize_t (*lenfunc)(PyObject *); 174 typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t); 175 typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t); 176 typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *); 177 typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *); 178 typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); 179 180 #ifndef Py_LIMITED_API 181 /* buffer interface */ 182 typedef struct bufferinfo { 183 void *buf; 184 PyObject *obj; /* owned reference */ 185 Py_ssize_t len; 186 Py_ssize_t itemsize; /* This is Py_ssize_t so it can be 187 pointed to by strides in simple case.*/ 188 int readonly; 189 int ndim; 190 char *format; 191 Py_ssize_t *shape; 192 Py_ssize_t *strides; 193 Py_ssize_t *suboffsets; 194 void *internal; 195 } Py_buffer; 196 197 typedef int (*getbufferproc)(PyObject *, Py_buffer *, int); 198 typedef void (*releasebufferproc)(PyObject *, Py_buffer *); 199 200 /* Maximum number of dimensions */ 201 #define PyBUF_MAX_NDIM 64 202 203 /* Flags for getting buffers */ 204 #define PyBUF_SIMPLE 0 205 #define PyBUF_WRITABLE 0x0001 206 /* we used to include an E, backwards compatible alias */ 207 #define PyBUF_WRITEABLE PyBUF_WRITABLE 208 #define PyBUF_FORMAT 0x0004 209 #define PyBUF_ND 0x0008 210 #define PyBUF_STRIDES (0x0010 | PyBUF_ND) 211 #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) 212 #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) 213 #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) 214 #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) 215 216 #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE) 217 #define PyBUF_CONTIG_RO (PyBUF_ND) 218 219 #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE) 220 #define PyBUF_STRIDED_RO (PyBUF_STRIDES) 221 222 #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT) 223 #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT) 224 225 #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT) 226 #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT) 227 228 229 #define PyBUF_READ 0x100 230 #define PyBUF_WRITE 0x200 231 232 /* End buffer interface */ 233 #endif /* Py_LIMITED_API */ 234 235 typedef int (*objobjproc)(PyObject *, PyObject *); 236 typedef int (*visitproc)(PyObject *, void *); 237 typedef int (*traverseproc)(PyObject *, visitproc, void *); 238 239 #ifndef Py_LIMITED_API 240 typedef struct { 241 /* Number implementations must check *both* 242 arguments for proper type and implement the necessary conversions 243 in the slot functions themselves. */ 244 245 binaryfunc nb_add; 246 binaryfunc nb_subtract; 247 binaryfunc nb_multiply; 248 binaryfunc nb_remainder; 249 binaryfunc nb_divmod; 250 ternaryfunc nb_power; 251 unaryfunc nb_negative; 252 unaryfunc nb_positive; 253 unaryfunc nb_absolute; 254 inquiry nb_bool; 255 unaryfunc nb_invert; 256 binaryfunc nb_lshift; 257 binaryfunc nb_rshift; 258 binaryfunc nb_and; 259 binaryfunc nb_xor; 260 binaryfunc nb_or; 261 unaryfunc nb_int; 262 void *nb_reserved; /* the slot formerly known as nb_long */ 263 unaryfunc nb_float; 264 265 binaryfunc nb_inplace_add; 266 binaryfunc nb_inplace_subtract; 267 binaryfunc nb_inplace_multiply; 268 binaryfunc nb_inplace_remainder; 269 ternaryfunc nb_inplace_power; 270 binaryfunc nb_inplace_lshift; 271 binaryfunc nb_inplace_rshift; 272 binaryfunc nb_inplace_and; 273 binaryfunc nb_inplace_xor; 274 binaryfunc nb_inplace_or; 275 276 binaryfunc nb_floor_divide; 277 binaryfunc nb_true_divide; 278 binaryfunc nb_inplace_floor_divide; 279 binaryfunc nb_inplace_true_divide; 280 281 unaryfunc nb_index; 282 283 binaryfunc nb_matrix_multiply; 284 binaryfunc nb_inplace_matrix_multiply; 285 } PyNumberMethods; 286 287 typedef struct { 288 lenfunc sq_length; 289 binaryfunc sq_concat; 290 ssizeargfunc sq_repeat; 291 ssizeargfunc sq_item; 292 void *was_sq_slice; 293 ssizeobjargproc sq_ass_item; 294 void *was_sq_ass_slice; 295 objobjproc sq_contains; 296 297 binaryfunc sq_inplace_concat; 298 ssizeargfunc sq_inplace_repeat; 299 } PySequenceMethods; 300 301 typedef struct { 302 lenfunc mp_length; 303 binaryfunc mp_subscript; 304 objobjargproc mp_ass_subscript; 305 } PyMappingMethods; 306 307 typedef struct { 308 unaryfunc am_await; 309 unaryfunc am_aiter; 310 unaryfunc am_anext; 311 } PyAsyncMethods; 312 313 typedef struct { 314 getbufferproc bf_getbuffer; 315 releasebufferproc bf_releasebuffer; 316 } PyBufferProcs; 317 #endif /* Py_LIMITED_API */ 318 319 typedef void (*freefunc)(void *); 320 typedef void (*destructor)(PyObject *); 321 #ifndef Py_LIMITED_API 322 /* We can't provide a full compile-time check that limited-API 323 users won't implement tp_print. However, not defining printfunc 324 and making tp_print of a different function pointer type 325 should at least cause a warning in most cases. */ 326 typedef int (*printfunc)(PyObject *, FILE *, int); 327 #endif 328 typedef PyObject *(*getattrfunc)(PyObject *, char *); 329 typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); 330 typedef int (*setattrfunc)(PyObject *, char *, PyObject *); 331 typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); 332 typedef PyObject *(*reprfunc)(PyObject *); 333 typedef Py_hash_t (*hashfunc)(PyObject *); 334 typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); 335 typedef PyObject *(*getiterfunc) (PyObject *); 336 typedef PyObject *(*iternextfunc) (PyObject *); 337 typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); 338 typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); 339 typedef int (*initproc)(PyObject *, PyObject *, PyObject *); 340 typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *); 341 typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t); 342 343 #ifdef Py_LIMITED_API 344 typedef struct _typeobject PyTypeObject; /* opaque */ 345 #else 346 typedef struct _typeobject { 347 PyObject_VAR_HEAD 348 const char *tp_name; /* For printing, in format "<module>.<name>" */ 349 Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ 350 351 /* Methods to implement standard operations */ 352 353 destructor tp_dealloc; 354 printfunc tp_print; 355 getattrfunc tp_getattr; 356 setattrfunc tp_setattr; 357 PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2) 358 or tp_reserved (Python 3) */ 359 reprfunc tp_repr; 360 361 /* Method suites for standard classes */ 362 363 PyNumberMethods *tp_as_number; 364 PySequenceMethods *tp_as_sequence; 365 PyMappingMethods *tp_as_mapping; 366 367 /* More standard operations (here for binary compatibility) */ 368 369 hashfunc tp_hash; 370 ternaryfunc tp_call; 371 reprfunc tp_str; 372 getattrofunc tp_getattro; 373 setattrofunc tp_setattro; 374 375 /* Functions to access object as input/output buffer */ 376 PyBufferProcs *tp_as_buffer; 377 378 /* Flags to define presence of optional/expanded features */ 379 unsigned long tp_flags; 380 381 const char *tp_doc; /* Documentation string */ 382 383 /* Assigned meaning in release 2.0 */ 384 /* call function for all accessible objects */ 385 traverseproc tp_traverse; 386 387 /* delete references to contained objects */ 388 inquiry tp_clear; 389 390 /* Assigned meaning in release 2.1 */ 391 /* rich comparisons */ 392 richcmpfunc tp_richcompare; 393 394 /* weak reference enabler */ 395 Py_ssize_t tp_weaklistoffset; 396 397 /* Iterators */ 398 getiterfunc tp_iter; 399 iternextfunc tp_iternext; 400 401 /* Attribute descriptor and subclassing stuff */ 402 struct PyMethodDef *tp_methods; 403 struct PyMemberDef *tp_members; 404 struct PyGetSetDef *tp_getset; 405 struct _typeobject *tp_base; 406 PyObject *tp_dict; 407 descrgetfunc tp_descr_get; 408 descrsetfunc tp_descr_set; 409 Py_ssize_t tp_dictoffset; 410 initproc tp_init; 411 allocfunc tp_alloc; 412 newfunc tp_new; 413 freefunc tp_free; /* Low-level free-memory routine */ 414 inquiry tp_is_gc; /* For PyObject_IS_GC */ 415 PyObject *tp_bases; 416 PyObject *tp_mro; /* method resolution order */ 417 PyObject *tp_cache; 418 PyObject *tp_subclasses; 419 PyObject *tp_weaklist; 420 destructor tp_del; 421 422 /* Type attribute cache version tag. Added in version 2.6 */ 423 unsigned int tp_version_tag; 424 425 destructor tp_finalize; 426 427 #ifdef COUNT_ALLOCS 428 /* these must be last and never explicitly initialized */ 429 Py_ssize_t tp_allocs; 430 Py_ssize_t tp_frees; 431 Py_ssize_t tp_maxalloc; 432 struct _typeobject *tp_prev; 433 struct _typeobject *tp_next; 434 #endif 435 } PyTypeObject; 436 #endif 437 438 typedef struct{ 439 int slot; /* slot id, see below */ 440 void *pfunc; /* function pointer */ 441 } PyType_Slot; 442 443 typedef struct{ 444 const char* name; 445 int basicsize; 446 int itemsize; 447 unsigned int flags; 448 PyType_Slot *slots; /* terminated by slot==0. */ 449 } PyType_Spec; 450 451 PyAPI_FUNC(PyObject*) PyType_FromSpec(PyType_Spec*); 452 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 453 PyAPI_FUNC(PyObject*) PyType_FromSpecWithBases(PyType_Spec*, PyObject*); 454 #endif 455 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03040000 456 PyAPI_FUNC(void*) PyType_GetSlot(PyTypeObject*, int); 457 #endif 458 459 #ifndef Py_LIMITED_API 460 /* The *real* layout of a type object when allocated on the heap */ 461 typedef struct _heaptypeobject { 462 /* Note: there's a dependency on the order of these members 463 in slotptr() in typeobject.c . */ 464 PyTypeObject ht_type; 465 PyAsyncMethods as_async; 466 PyNumberMethods as_number; 467 PyMappingMethods as_mapping; 468 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, 469 so that the mapping wins when both 470 the mapping and the sequence define 471 a given operator (e.g. __getitem__). 472 see add_operators() in typeobject.c . */ 473 PyBufferProcs as_buffer; 474 PyObject *ht_name, *ht_slots, *ht_qualname; 475 struct _dictkeysobject *ht_cached_keys; 476 /* here are optional user slots, followed by the members. */ 477 } PyHeapTypeObject; 478 479 /* access macro to the members which are floating "behind" the object */ 480 #define PyHeapType_GET_MEMBERS(etype) \ 481 ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize)) 482 #endif 483 484 /* Generic type check */ 485 PyAPI_FUNC(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); 486 #define PyObject_TypeCheck(ob, tp) \ 487 (Py_TYPE(ob) == (tp) || PyType_IsSubtype(Py_TYPE(ob), (tp))) 488 489 PyAPI_DATA(PyTypeObject) PyType_Type; /* built-in 'type' */ 490 PyAPI_DATA(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ 491 PyAPI_DATA(PyTypeObject) PySuper_Type; /* built-in 'super' */ 492 493 PyAPI_FUNC(unsigned long) PyType_GetFlags(PyTypeObject*); 494 495 #define PyType_Check(op) \ 496 PyType_FastSubclass(Py_TYPE(op), Py_TPFLAGS_TYPE_SUBCLASS) 497 #define PyType_CheckExact(op) (Py_TYPE(op) == &PyType_Type) 498 499 PyAPI_FUNC(int) PyType_Ready(PyTypeObject *); 500 PyAPI_FUNC(PyObject *) PyType_GenericAlloc(PyTypeObject *, Py_ssize_t); 501 PyAPI_FUNC(PyObject *) PyType_GenericNew(PyTypeObject *, 502 PyObject *, PyObject *); 503 #ifndef Py_LIMITED_API 504 PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *); 505 PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *); 506 PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *); 507 PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *); 508 PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *); 509 #endif 510 PyAPI_FUNC(unsigned int) PyType_ClearCache(void); 511 PyAPI_FUNC(void) PyType_Modified(PyTypeObject *); 512 513 #ifndef Py_LIMITED_API 514 PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *); 515 PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *); 516 #endif 517 518 /* Generic operations on objects */ 519 #ifndef Py_LIMITED_API 520 struct _Py_Identifier; 521 PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int); 522 PyAPI_FUNC(void) _Py_BreakPoint(void); 523 PyAPI_FUNC(void) _PyObject_Dump(PyObject *); 524 PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *); 525 #endif 526 PyAPI_FUNC(PyObject *) PyObject_Repr(PyObject *); 527 PyAPI_FUNC(PyObject *) PyObject_Str(PyObject *); 528 PyAPI_FUNC(PyObject *) PyObject_ASCII(PyObject *); 529 PyAPI_FUNC(PyObject *) PyObject_Bytes(PyObject *); 530 PyAPI_FUNC(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); 531 PyAPI_FUNC(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); 532 PyAPI_FUNC(PyObject *) PyObject_GetAttrString(PyObject *, const char *); 533 PyAPI_FUNC(int) PyObject_SetAttrString(PyObject *, const char *, PyObject *); 534 PyAPI_FUNC(int) PyObject_HasAttrString(PyObject *, const char *); 535 PyAPI_FUNC(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); 536 PyAPI_FUNC(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); 537 PyAPI_FUNC(int) PyObject_HasAttr(PyObject *, PyObject *); 538 #ifndef Py_LIMITED_API 539 PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); 540 PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); 541 PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); 542 PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *); 543 /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which 544 don't raise AttributeError. 545 546 Return 1 and set *result != NULL if an attribute is found. 547 Return 0 and set *result == NULL if an attribute is not found; 548 an AttributeError is silenced. 549 Return -1 and set *result == NULL if an error other than AttributeError 550 is raised. 551 */ 552 PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **); 553 PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **); 554 PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); 555 #endif 556 PyAPI_FUNC(PyObject *) PyObject_SelfIter(PyObject *); 557 #ifndef Py_LIMITED_API 558 PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *); 559 #endif 560 PyAPI_FUNC(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); 561 PyAPI_FUNC(int) PyObject_GenericSetAttr(PyObject *, 562 PyObject *, PyObject *); 563 #if !defined(Py_LIMITED_API) || Py_LIMITED_API+0 >= 0x03030000 564 PyAPI_FUNC(int) PyObject_GenericSetDict(PyObject *, PyObject *, void *); 565 #endif 566 PyAPI_FUNC(Py_hash_t) PyObject_Hash(PyObject *); 567 PyAPI_FUNC(Py_hash_t) PyObject_HashNotImplemented(PyObject *); 568 PyAPI_FUNC(int) PyObject_IsTrue(PyObject *); 569 PyAPI_FUNC(int) PyObject_Not(PyObject *); 570 PyAPI_FUNC(int) PyCallable_Check(PyObject *); 571 572 PyAPI_FUNC(void) PyObject_ClearWeakRefs(PyObject *); 573 #ifndef Py_LIMITED_API 574 PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *); 575 PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *); 576 #endif 577 578 #ifndef Py_LIMITED_API 579 /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes 580 dict as the last parameter. */ 581 PyAPI_FUNC(PyObject *) 582 _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int); 583 PyAPI_FUNC(int) 584 _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *, 585 PyObject *, PyObject *); 586 #endif /* !Py_LIMITED_API */ 587 588 /* Helper to look up a builtin object */ 589 #ifndef Py_LIMITED_API 590 PyAPI_FUNC(PyObject *) 591 _PyObject_GetBuiltin(const char *name); 592 #endif 593 594 /* PyObject_Dir(obj) acts like Python builtins.dir(obj), returning a 595 list of strings. PyObject_Dir(NULL) is like builtins.dir(), 596 returning the names of the current locals. In this case, if there are 597 no current locals, NULL is returned, and PyErr_Occurred() is false. 598 */ 599 PyAPI_FUNC(PyObject *) PyObject_Dir(PyObject *); 600 601 602 /* Helpers for printing recursive container types */ 603 PyAPI_FUNC(int) Py_ReprEnter(PyObject *); 604 PyAPI_FUNC(void) Py_ReprLeave(PyObject *); 605 606 /* Flag bits for printing: */ 607 #define Py_PRINT_RAW 1 /* No string quotes etc. */ 608 609 /* 610 `Type flags (tp_flags) 611 612 These flags are used to extend the type structure in a backwards-compatible 613 fashion. Extensions can use the flags to indicate (and test) when a given 614 type structure contains a new feature. The Python core will use these when 615 introducing new functionality between major revisions (to avoid mid-version 616 changes in the PYTHON_API_VERSION). 617 618 Arbitration of the flag bit positions will need to be coordinated among 619 all extension writers who publicly release their extensions (this will 620 be fewer than you might expect!).. 621 622 Most flags were removed as of Python 3.0 to make room for new flags. (Some 623 flags are not for backwards compatibility but to indicate the presence of an 624 optional feature; these flags remain of course.) 625 626 Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. 627 628 Code can use PyType_HasFeature(type_ob, flag_value) to test whether the 629 given type object has a specified feature. 630 */ 631 632 /* Set if the type object is dynamically allocated */ 633 #define Py_TPFLAGS_HEAPTYPE (1UL << 9) 634 635 /* Set if the type allows subclassing */ 636 #define Py_TPFLAGS_BASETYPE (1UL << 10) 637 638 /* Set if the type is 'ready' -- fully initialized */ 639 #define Py_TPFLAGS_READY (1UL << 12) 640 641 /* Set while the type is being 'readied', to prevent recursive ready calls */ 642 #define Py_TPFLAGS_READYING (1UL << 13) 643 644 /* Objects support garbage collection (see objimp.h) */ 645 #define Py_TPFLAGS_HAVE_GC (1UL << 14) 646 647 /* These two bits are preserved for Stackless Python, next after this is 17 */ 648 #ifdef STACKLESS 649 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION (3UL << 15) 650 #else 651 #define Py_TPFLAGS_HAVE_STACKLESS_EXTENSION 0 652 #endif 653 654 /* Objects support type attribute cache */ 655 #define Py_TPFLAGS_HAVE_VERSION_TAG (1UL << 18) 656 #define Py_TPFLAGS_VALID_VERSION_TAG (1UL << 19) 657 658 /* Type is abstract and cannot be instantiated */ 659 #define Py_TPFLAGS_IS_ABSTRACT (1UL << 20) 660 661 /* These flags are used to determine if a type is a subclass. */ 662 #define Py_TPFLAGS_LONG_SUBCLASS (1UL << 24) 663 #define Py_TPFLAGS_LIST_SUBCLASS (1UL << 25) 664 #define Py_TPFLAGS_TUPLE_SUBCLASS (1UL << 26) 665 #define Py_TPFLAGS_BYTES_SUBCLASS (1UL << 27) 666 #define Py_TPFLAGS_UNICODE_SUBCLASS (1UL << 28) 667 #define Py_TPFLAGS_DICT_SUBCLASS (1UL << 29) 668 #define Py_TPFLAGS_BASE_EXC_SUBCLASS (1UL << 30) 669 #define Py_TPFLAGS_TYPE_SUBCLASS (1UL << 31) 670 671 #define Py_TPFLAGS_DEFAULT ( \ 672 Py_TPFLAGS_HAVE_STACKLESS_EXTENSION | \ 673 Py_TPFLAGS_HAVE_VERSION_TAG | \ 674 0) 675 676 /* NOTE: The following flags reuse lower bits (removed as part of the 677 * Python 3.0 transition). */ 678 679 /* Type structure has tp_finalize member (3.4) */ 680 #define Py_TPFLAGS_HAVE_FINALIZE (1UL << 0) 681 682 #ifdef Py_LIMITED_API 683 #define PyType_HasFeature(t,f) ((PyType_GetFlags(t) & (f)) != 0) 684 #else 685 #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0) 686 #endif 687 #define PyType_FastSubclass(t,f) PyType_HasFeature(t,f) 688 689 690 /* 691 The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement 692 reference counts. Py_DECREF calls the object's deallocator function when 693 the refcount falls to 0; for 694 objects that don't contain references to other objects or heap memory 695 this can be the standard function free(). Both macros can be used 696 wherever a void expression is allowed. The argument must not be a 697 NULL pointer. If it may be NULL, use Py_XINCREF/Py_XDECREF instead. 698 The macro _Py_NewReference(op) initialize reference counts to 1, and 699 in special builds (Py_REF_DEBUG, Py_TRACE_REFS) performs additional 700 bookkeeping appropriate to the special build. 701 702 We assume that the reference count field can never overflow; this can 703 be proven when the size of the field is the same as the pointer size, so 704 we ignore the possibility. Provided a C int is at least 32 bits (which 705 is implicitly assumed in many parts of this code), that's enough for 706 about 2**31 references to an object. 707 708 XXX The following became out of date in Python 2.2, but I'm not sure 709 XXX what the full truth is now. Certainly, heap-allocated type objects 710 XXX can and should be deallocated. 711 Type objects should never be deallocated; the type pointer in an object 712 is not considered to be a reference to the type object, to save 713 complications in the deallocation function. (This is actually a 714 decision that's up to the implementer of each new type so if you want, 715 you can count such references to the type object.) 716 */ 717 718 /* First define a pile of simple helper macros, one set per special 719 * build symbol. These either expand to the obvious things, or to 720 * nothing at all when the special mode isn't in effect. The main 721 * macros can later be defined just once then, yet expand to different 722 * things depending on which special build options are and aren't in effect. 723 * Trust me <wink>: while painful, this is 20x easier to understand than, 724 * e.g, defining _Py_NewReference five different times in a maze of nested 725 * #ifdefs (we used to do that -- it was impenetrable). 726 */ 727 #ifdef Py_REF_DEBUG 728 PyAPI_DATA(Py_ssize_t) _Py_RefTotal; 729 PyAPI_FUNC(void) _Py_NegativeRefcount(const char *fname, 730 int lineno, PyObject *op); 731 PyAPI_FUNC(Py_ssize_t) _Py_GetRefTotal(void); 732 #define _Py_INC_REFTOTAL _Py_RefTotal++ 733 #define _Py_DEC_REFTOTAL _Py_RefTotal-- 734 #define _Py_REF_DEBUG_COMMA , 735 #define _Py_CHECK_REFCNT(OP) \ 736 { if (((PyObject*)OP)->ob_refcnt < 0) \ 737 _Py_NegativeRefcount(__FILE__, __LINE__, \ 738 (PyObject *)(OP)); \ 739 } 740 /* Py_REF_DEBUG also controls the display of refcounts and memory block 741 * allocations at the interactive prompt and at interpreter shutdown 742 */ 743 PyAPI_FUNC(void) _PyDebug_PrintTotalRefs(void); 744 #else 745 #define _Py_INC_REFTOTAL 746 #define _Py_DEC_REFTOTAL 747 #define _Py_REF_DEBUG_COMMA 748 #define _Py_CHECK_REFCNT(OP) /* a semicolon */; 749 #endif /* Py_REF_DEBUG */ 750 751 #ifdef COUNT_ALLOCS 752 PyAPI_FUNC(void) inc_count(PyTypeObject *); 753 PyAPI_FUNC(void) dec_count(PyTypeObject *); 754 #define _Py_INC_TPALLOCS(OP) inc_count(Py_TYPE(OP)) 755 #define _Py_INC_TPFREES(OP) dec_count(Py_TYPE(OP)) 756 #define _Py_DEC_TPFREES(OP) Py_TYPE(OP)->tp_frees-- 757 #define _Py_COUNT_ALLOCS_COMMA , 758 #else 759 #define _Py_INC_TPALLOCS(OP) 760 #define _Py_INC_TPFREES(OP) 761 #define _Py_DEC_TPFREES(OP) 762 #define _Py_COUNT_ALLOCS_COMMA 763 #endif /* COUNT_ALLOCS */ 764 765 #ifdef Py_TRACE_REFS 766 /* Py_TRACE_REFS is such major surgery that we call external routines. */ 767 PyAPI_FUNC(void) _Py_NewReference(PyObject *); 768 PyAPI_FUNC(void) _Py_ForgetReference(PyObject *); 769 PyAPI_FUNC(void) _Py_Dealloc(PyObject *); 770 PyAPI_FUNC(void) _Py_PrintReferences(FILE *); 771 PyAPI_FUNC(void) _Py_PrintReferenceAddresses(FILE *); 772 PyAPI_FUNC(void) _Py_AddToAllObjects(PyObject *, int force); 773 774 #else 775 /* Without Py_TRACE_REFS, there's little enough to do that we expand code 776 * inline. 777 */ 778 #define _Py_NewReference(op) ( \ 779 _Py_INC_TPALLOCS(op) _Py_COUNT_ALLOCS_COMMA \ 780 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \ 781 Py_REFCNT(op) = 1) 782 783 #define _Py_ForgetReference(op) _Py_INC_TPFREES(op) 784 785 #ifdef Py_LIMITED_API 786 PyAPI_FUNC(void) _Py_Dealloc(PyObject *); 787 #else 788 #define _Py_Dealloc(op) ( \ 789 _Py_INC_TPFREES(op) _Py_COUNT_ALLOCS_COMMA \ 790 (*Py_TYPE(op)->tp_dealloc)((PyObject *)(op))) 791 #endif 792 #endif /* !Py_TRACE_REFS */ 793 794 #define Py_INCREF(op) ( \ 795 _Py_INC_REFTOTAL _Py_REF_DEBUG_COMMA \ 796 ((PyObject *)(op))->ob_refcnt++) 797 798 #define Py_DECREF(op) \ 799 do { \ 800 PyObject *_py_decref_tmp = (PyObject *)(op); \ 801 if (_Py_DEC_REFTOTAL _Py_REF_DEBUG_COMMA \ 802 --(_py_decref_tmp)->ob_refcnt != 0) \ 803 _Py_CHECK_REFCNT(_py_decref_tmp) \ 804 else \ 805 _Py_Dealloc(_py_decref_tmp); \ 806 } while (0) 807 808 /* Safely decref `op` and set `op` to NULL, especially useful in tp_clear 809 * and tp_dealloc implementations. 810 * 811 * Note that "the obvious" code can be deadly: 812 * 813 * Py_XDECREF(op); 814 * op = NULL; 815 * 816 * Typically, `op` is something like self->containee, and `self` is done 817 * using its `containee` member. In the code sequence above, suppose 818 * `containee` is non-NULL with a refcount of 1. Its refcount falls to 819 * 0 on the first line, which can trigger an arbitrary amount of code, 820 * possibly including finalizers (like __del__ methods or weakref callbacks) 821 * coded in Python, which in turn can release the GIL and allow other threads 822 * to run, etc. Such code may even invoke methods of `self` again, or cause 823 * cyclic gc to trigger, but-- oops! --self->containee still points to the 824 * object being torn down, and it may be in an insane state while being torn 825 * down. This has in fact been a rich historic source of miserable (rare & 826 * hard-to-diagnose) segfaulting (and other) bugs. 827 * 828 * The safe way is: 829 * 830 * Py_CLEAR(op); 831 * 832 * That arranges to set `op` to NULL _before_ decref'ing, so that any code 833 * triggered as a side-effect of `op` getting torn down no longer believes 834 * `op` points to a valid object. 835 * 836 * There are cases where it's safe to use the naive code, but they're brittle. 837 * For example, if `op` points to a Python integer, you know that destroying 838 * one of those can't cause problems -- but in part that relies on that 839 * Python integers aren't currently weakly referencable. Best practice is 840 * to use Py_CLEAR() even if you can't think of a reason for why you need to. 841 */ 842 #define Py_CLEAR(op) \ 843 do { \ 844 PyObject *_py_tmp = (PyObject *)(op); \ 845 if (_py_tmp != NULL) { \ 846 (op) = NULL; \ 847 Py_DECREF(_py_tmp); \ 848 } \ 849 } while (0) 850 851 /* Macros to use in case the object pointer may be NULL: */ 852 #define Py_XINCREF(op) \ 853 do { \ 854 PyObject *_py_xincref_tmp = (PyObject *)(op); \ 855 if (_py_xincref_tmp != NULL) \ 856 Py_INCREF(_py_xincref_tmp); \ 857 } while (0) 858 859 #define Py_XDECREF(op) \ 860 do { \ 861 PyObject *_py_xdecref_tmp = (PyObject *)(op); \ 862 if (_py_xdecref_tmp != NULL) \ 863 Py_DECREF(_py_xdecref_tmp); \ 864 } while (0) 865 866 #ifndef Py_LIMITED_API 867 /* Safely decref `op` and set `op` to `op2`. 868 * 869 * As in case of Py_CLEAR "the obvious" code can be deadly: 870 * 871 * Py_DECREF(op); 872 * op = op2; 873 * 874 * The safe way is: 875 * 876 * Py_SETREF(op, op2); 877 * 878 * That arranges to set `op` to `op2` _before_ decref'ing, so that any code 879 * triggered as a side-effect of `op` getting torn down no longer believes 880 * `op` points to a valid object. 881 * 882 * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of 883 * Py_DECREF. 884 */ 885 886 #define Py_SETREF(op, op2) \ 887 do { \ 888 PyObject *_py_tmp = (PyObject *)(op); \ 889 (op) = (op2); \ 890 Py_DECREF(_py_tmp); \ 891 } while (0) 892 893 #define Py_XSETREF(op, op2) \ 894 do { \ 895 PyObject *_py_tmp = (PyObject *)(op); \ 896 (op) = (op2); \ 897 Py_XDECREF(_py_tmp); \ 898 } while (0) 899 900 #endif /* ifndef Py_LIMITED_API */ 901 902 /* 903 These are provided as conveniences to Python runtime embedders, so that 904 they can have object code that is not dependent on Python compilation flags. 905 */ 906 PyAPI_FUNC(void) Py_IncRef(PyObject *); 907 PyAPI_FUNC(void) Py_DecRef(PyObject *); 908 909 #ifndef Py_LIMITED_API 910 PyAPI_DATA(PyTypeObject) _PyNone_Type; 911 PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type; 912 #endif /* !Py_LIMITED_API */ 913 914 /* 915 _Py_NoneStruct is an object of undefined type which can be used in contexts 916 where NULL (nil) is not suitable (since NULL often means 'error'). 917 918 Don't forget to apply Py_INCREF() when returning this value!!! 919 */ 920 PyAPI_DATA(PyObject) _Py_NoneStruct; /* Don't use this directly */ 921 #define Py_None (&_Py_NoneStruct) 922 923 /* Macro for returning Py_None from a function */ 924 #define Py_RETURN_NONE return Py_INCREF(Py_None), Py_None 925 926 /* 927 Py_NotImplemented is a singleton used to signal that an operation is 928 not implemented for a given type combination. 929 */ 930 PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ 931 #define Py_NotImplemented (&_Py_NotImplementedStruct) 932 933 /* Macro for returning Py_NotImplemented from a function */ 934 #define Py_RETURN_NOTIMPLEMENTED \ 935 return Py_INCREF(Py_NotImplemented), Py_NotImplemented 936 937 /* Rich comparison opcodes */ 938 #define Py_LT 0 939 #define Py_LE 1 940 #define Py_EQ 2 941 #define Py_NE 3 942 #define Py_GT 4 943 #define Py_GE 5 944 945 /* 946 * Macro for implementing rich comparisons 947 * 948 * Needs to be a macro because any C-comparable type can be used. 949 */ 950 #define Py_RETURN_RICHCOMPARE(val1, val2, op) \ 951 do { \ 952 switch (op) { \ 953 case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 954 case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 955 case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 956 case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 957 case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 958 case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \ 959 default: \ 960 Py_UNREACHABLE(); \ 961 } \ 962 } while (0) 963 964 #ifndef Py_LIMITED_API 965 /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE. 966 * Defined in object.c. 967 */ 968 PyAPI_DATA(int) _Py_SwappedOp[]; 969 #endif /* !Py_LIMITED_API */ 970 971 972 /* 973 More conventions 974 ================ 975 976 Argument Checking 977 ----------------- 978 979 Functions that take objects as arguments normally don't check for nil 980 arguments, but they do check the type of the argument, and return an 981 error if the function doesn't apply to the type. 982 983 Failure Modes 984 ------------- 985 986 Functions may fail for a variety of reasons, including running out of 987 memory. This is communicated to the caller in two ways: an error string 988 is set (see errors.h), and the function result differs: functions that 989 normally return a pointer return NULL for failure, functions returning 990 an integer return -1 (which could be a legal return value too!), and 991 other functions return 0 for success and -1 for failure. 992 Callers should always check for errors before using the result. If 993 an error was set, the caller must either explicitly clear it, or pass 994 the error on to its caller. 995 996 Reference Counts 997 ---------------- 998 999 It takes a while to get used to the proper usage of reference counts. 1000 1001 Functions that create an object set the reference count to 1; such new 1002 objects must be stored somewhere or destroyed again with Py_DECREF(). 1003 Some functions that 'store' objects, such as PyTuple_SetItem() and 1004 PyList_SetItem(), 1005 don't increment the reference count of the object, since the most 1006 frequent use is to store a fresh object. Functions that 'retrieve' 1007 objects, such as PyTuple_GetItem() and PyDict_GetItemString(), also 1008 don't increment 1009 the reference count, since most frequently the object is only looked at 1010 quickly. Thus, to retrieve an object and store it again, the caller 1011 must call Py_INCREF() explicitly. 1012 1013 NOTE: functions that 'consume' a reference count, like 1014 PyList_SetItem(), consume the reference even if the object wasn't 1015 successfully stored, to simplify error handling. 1016 1017 It seems attractive to make other functions that take an object as 1018 argument consume a reference count; however, this may quickly get 1019 confusing (even the current practice is already confusing). Consider 1020 it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at 1021 times. 1022 */ 1023 1024 1025 /* Trashcan mechanism, thanks to Christian Tismer. 1026 1027 When deallocating a container object, it's possible to trigger an unbounded 1028 chain of deallocations, as each Py_DECREF in turn drops the refcount on "the 1029 next" object in the chain to 0. This can easily lead to stack faults, and 1030 especially in threads (which typically have less stack space to work with). 1031 1032 A container object that participates in cyclic gc can avoid this by 1033 bracketing the body of its tp_dealloc function with a pair of macros: 1034 1035 static void 1036 mytype_dealloc(mytype *p) 1037 { 1038 ... declarations go here ... 1039 1040 PyObject_GC_UnTrack(p); // must untrack first 1041 Py_TRASHCAN_SAFE_BEGIN(p) 1042 ... The body of the deallocator goes here, including all calls ... 1043 ... to Py_DECREF on contained objects. ... 1044 Py_TRASHCAN_SAFE_END(p) 1045 } 1046 1047 CAUTION: Never return from the middle of the body! If the body needs to 1048 "get out early", put a label immediately before the Py_TRASHCAN_SAFE_END 1049 call, and goto it. Else the call-depth counter (see below) will stay 1050 above 0 forever, and the trashcan will never get emptied. 1051 1052 How it works: The BEGIN macro increments a call-depth counter. So long 1053 as this counter is small, the body of the deallocator is run directly without 1054 further ado. But if the counter gets large, it instead adds p to a list of 1055 objects to be deallocated later, skips the body of the deallocator, and 1056 resumes execution after the END macro. The tp_dealloc routine then returns 1057 without deallocating anything (and so unbounded call-stack depth is avoided). 1058 1059 When the call stack finishes unwinding again, code generated by the END macro 1060 notices this, and calls another routine to deallocate all the objects that 1061 may have been added to the list of deferred deallocations. In effect, a 1062 chain of N deallocations is broken into (N-1)/(PyTrash_UNWIND_LEVEL-1) pieces, 1063 with the call stack never exceeding a depth of PyTrash_UNWIND_LEVEL. 1064 */ 1065 1066 #ifndef Py_LIMITED_API 1067 /* This is the old private API, invoked by the macros before 3.2.4. 1068 Kept for binary compatibility of extensions using the stable ABI. */ 1069 PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*); 1070 PyAPI_FUNC(void) _PyTrash_destroy_chain(void); 1071 #endif /* !Py_LIMITED_API */ 1072 1073 /* The new thread-safe private API, invoked by the macros below. */ 1074 PyAPI_FUNC(void) _PyTrash_thread_deposit_object(PyObject*); 1075 PyAPI_FUNC(void) _PyTrash_thread_destroy_chain(void); 1076 1077 #define PyTrash_UNWIND_LEVEL 50 1078 1079 #define Py_TRASHCAN_SAFE_BEGIN(op) \ 1080 do { \ 1081 PyThreadState *_tstate = PyThreadState_GET(); \ 1082 if (_tstate->trash_delete_nesting < PyTrash_UNWIND_LEVEL) { \ 1083 ++_tstate->trash_delete_nesting; 1084 /* The body of the deallocator is here. */ 1085 #define Py_TRASHCAN_SAFE_END(op) \ 1086 --_tstate->trash_delete_nesting; \ 1087 if (_tstate->trash_delete_later && _tstate->trash_delete_nesting <= 0) \ 1088 _PyTrash_thread_destroy_chain(); \ 1089 } \ 1090 else \ 1091 _PyTrash_thread_deposit_object((PyObject*)op); \ 1092 } while (0); 1093 1094 #ifndef Py_LIMITED_API 1095 PyAPI_FUNC(void) 1096 _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks, 1097 size_t sizeof_block); 1098 PyAPI_FUNC(void) 1099 _PyObject_DebugTypeStats(FILE *out); 1100 #endif /* ifndef Py_LIMITED_API */ 1101 1102 #ifdef __cplusplus 1103 } 1104 #endif 1105 #endif /* !Py_OBJECT_H */ 1106