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