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 | |
| 5 | #ifdef __cplusplus |
| 6 | extern "C" { |
| 7 | #endif |
| 8 | |
| 9 | /********************* String Literals ****************************************/ |
| 10 | /* This structure helps managing static strings. The basic usage goes like this: |
| 11 | Instead of doing |
| 12 | |
| 13 | r = PyObject_CallMethod(o, "foo", "args", ...); |
| 14 | |
| 15 | do |
| 16 | |
| 17 | _Py_IDENTIFIER(foo); |
| 18 | ... |
| 19 | r = _PyObject_CallMethodId(o, &PyId_foo, "args", ...); |
| 20 | |
| 21 | PyId_foo is a static variable, either on block level or file level. On first |
| 22 | usage, the string "foo" is interned, and the structures are linked. On interpreter |
| 23 | shutdown, all strings are released (through _PyUnicode_ClearStaticStrings). |
| 24 | |
| 25 | Alternatively, _Py_static_string allows choosing the variable name. |
| 26 | _PyUnicode_FromId returns a borrowed reference to the interned string. |
| 27 | _PyObject_{Get,Set,Has}AttrId are __getattr__ versions using _Py_Identifier*. |
| 28 | */ |
| 29 | typedef struct _Py_Identifier { |
| 30 | struct _Py_Identifier *next; |
| 31 | const char* string; |
| 32 | PyObject *object; |
| 33 | } _Py_Identifier; |
| 34 | |
| 35 | #define _Py_static_string_init(value) { .next = NULL, .string = value, .object = NULL } |
| 36 | #define _Py_static_string(varname, value) static _Py_Identifier varname = _Py_static_string_init(value) |
| 37 | #define _Py_IDENTIFIER(varname) _Py_static_string(PyId_##varname, #varname) |
| 38 | |
| 39 | /* buffer interface */ |
| 40 | typedef struct bufferinfo { |
| 41 | void *buf; |
| 42 | PyObject *obj; /* owned reference */ |
| 43 | Py_ssize_t len; |
| 44 | Py_ssize_t itemsize; /* This is Py_ssize_t so it can be |
| 45 | pointed to by strides in simple case.*/ |
| 46 | int readonly; |
| 47 | int ndim; |
| 48 | char *format; |
| 49 | Py_ssize_t *shape; |
| 50 | Py_ssize_t *strides; |
| 51 | Py_ssize_t *suboffsets; |
| 52 | void *internal; |
| 53 | } Py_buffer; |
| 54 | |
| 55 | typedef int (*getbufferproc)(PyObject *, Py_buffer *, int); |
| 56 | typedef void (*releasebufferproc)(PyObject *, Py_buffer *); |
| 57 | |
| 58 | /* Maximum number of dimensions */ |
| 59 | #define PyBUF_MAX_NDIM 64 |
| 60 | |
| 61 | /* Flags for getting buffers */ |
| 62 | #define PyBUF_SIMPLE 0 |
| 63 | #define PyBUF_WRITABLE 0x0001 |
| 64 | /* we used to include an E, backwards compatible alias */ |
| 65 | #define PyBUF_WRITEABLE PyBUF_WRITABLE |
| 66 | #define PyBUF_FORMAT 0x0004 |
| 67 | #define PyBUF_ND 0x0008 |
| 68 | #define PyBUF_STRIDES (0x0010 | PyBUF_ND) |
| 69 | #define PyBUF_C_CONTIGUOUS (0x0020 | PyBUF_STRIDES) |
| 70 | #define PyBUF_F_CONTIGUOUS (0x0040 | PyBUF_STRIDES) |
| 71 | #define PyBUF_ANY_CONTIGUOUS (0x0080 | PyBUF_STRIDES) |
| 72 | #define PyBUF_INDIRECT (0x0100 | PyBUF_STRIDES) |
| 73 | |
| 74 | #define PyBUF_CONTIG (PyBUF_ND | PyBUF_WRITABLE) |
| 75 | #define PyBUF_CONTIG_RO (PyBUF_ND) |
| 76 | |
| 77 | #define PyBUF_STRIDED (PyBUF_STRIDES | PyBUF_WRITABLE) |
| 78 | #define PyBUF_STRIDED_RO (PyBUF_STRIDES) |
| 79 | |
| 80 | #define PyBUF_RECORDS (PyBUF_STRIDES | PyBUF_WRITABLE | PyBUF_FORMAT) |
| 81 | #define PyBUF_RECORDS_RO (PyBUF_STRIDES | PyBUF_FORMAT) |
| 82 | |
| 83 | #define PyBUF_FULL (PyBUF_INDIRECT | PyBUF_WRITABLE | PyBUF_FORMAT) |
| 84 | #define PyBUF_FULL_RO (PyBUF_INDIRECT | PyBUF_FORMAT) |
| 85 | |
| 86 | |
| 87 | #define PyBUF_READ 0x100 |
| 88 | #define PyBUF_WRITE 0x200 |
| 89 | /* End buffer interface */ |
| 90 | |
| 91 | |
| 92 | typedef struct { |
| 93 | /* Number implementations must check *both* |
| 94 | arguments for proper type and implement the necessary conversions |
| 95 | in the slot functions themselves. */ |
| 96 | |
| 97 | binaryfunc nb_add; |
| 98 | binaryfunc nb_subtract; |
| 99 | binaryfunc nb_multiply; |
| 100 | binaryfunc nb_remainder; |
| 101 | binaryfunc nb_divmod; |
| 102 | ternaryfunc nb_power; |
| 103 | unaryfunc nb_negative; |
| 104 | unaryfunc nb_positive; |
| 105 | unaryfunc nb_absolute; |
| 106 | inquiry nb_bool; |
| 107 | unaryfunc nb_invert; |
| 108 | binaryfunc nb_lshift; |
| 109 | binaryfunc nb_rshift; |
| 110 | binaryfunc nb_and; |
| 111 | binaryfunc nb_xor; |
| 112 | binaryfunc nb_or; |
| 113 | unaryfunc nb_int; |
| 114 | void *nb_reserved; /* the slot formerly known as nb_long */ |
| 115 | unaryfunc nb_float; |
| 116 | |
| 117 | binaryfunc nb_inplace_add; |
| 118 | binaryfunc nb_inplace_subtract; |
| 119 | binaryfunc nb_inplace_multiply; |
| 120 | binaryfunc nb_inplace_remainder; |
| 121 | ternaryfunc nb_inplace_power; |
| 122 | binaryfunc nb_inplace_lshift; |
| 123 | binaryfunc nb_inplace_rshift; |
| 124 | binaryfunc nb_inplace_and; |
| 125 | binaryfunc nb_inplace_xor; |
| 126 | binaryfunc nb_inplace_or; |
| 127 | |
| 128 | binaryfunc nb_floor_divide; |
| 129 | binaryfunc nb_true_divide; |
| 130 | binaryfunc nb_inplace_floor_divide; |
| 131 | binaryfunc nb_inplace_true_divide; |
| 132 | |
| 133 | unaryfunc nb_index; |
| 134 | |
| 135 | binaryfunc nb_matrix_multiply; |
| 136 | binaryfunc nb_inplace_matrix_multiply; |
| 137 | } PyNumberMethods; |
| 138 | |
| 139 | typedef struct { |
| 140 | lenfunc sq_length; |
| 141 | binaryfunc sq_concat; |
| 142 | ssizeargfunc sq_repeat; |
| 143 | ssizeargfunc sq_item; |
| 144 | void *was_sq_slice; |
| 145 | ssizeobjargproc sq_ass_item; |
| 146 | void *was_sq_ass_slice; |
| 147 | objobjproc sq_contains; |
| 148 | |
| 149 | binaryfunc sq_inplace_concat; |
| 150 | ssizeargfunc sq_inplace_repeat; |
| 151 | } PySequenceMethods; |
| 152 | |
| 153 | typedef struct { |
| 154 | lenfunc mp_length; |
| 155 | binaryfunc mp_subscript; |
| 156 | objobjargproc mp_ass_subscript; |
| 157 | } PyMappingMethods; |
| 158 | |
| 159 | typedef struct { |
| 160 | unaryfunc am_await; |
| 161 | unaryfunc am_aiter; |
| 162 | unaryfunc am_anext; |
| 163 | } PyAsyncMethods; |
| 164 | |
| 165 | typedef struct { |
| 166 | getbufferproc bf_getbuffer; |
| 167 | releasebufferproc bf_releasebuffer; |
| 168 | } PyBufferProcs; |
| 169 | |
| 170 | /* We can't provide a full compile-time check that limited-API |
| 171 | users won't implement tp_print. However, not defining printfunc |
| 172 | and making tp_print of a different function pointer type |
| 173 | if Py_LIMITED_API is set should at least cause a warning |
| 174 | in most cases. */ |
| 175 | typedef int (*printfunc)(PyObject *, FILE *, int); |
| 176 | |
| 177 | typedef struct _typeobject { |
| 178 | PyObject_VAR_HEAD |
| 179 | const char *tp_name; /* For printing, in format "<module>.<name>" */ |
| 180 | Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */ |
| 181 | |
| 182 | /* Methods to implement standard operations */ |
| 183 | |
| 184 | destructor tp_dealloc; |
| 185 | printfunc tp_print; |
| 186 | getattrfunc tp_getattr; |
| 187 | setattrfunc tp_setattr; |
| 188 | PyAsyncMethods *tp_as_async; /* formerly known as tp_compare (Python 2) |
| 189 | or tp_reserved (Python 3) */ |
| 190 | reprfunc tp_repr; |
| 191 | |
| 192 | /* Method suites for standard classes */ |
| 193 | |
| 194 | PyNumberMethods *tp_as_number; |
| 195 | PySequenceMethods *tp_as_sequence; |
| 196 | PyMappingMethods *tp_as_mapping; |
| 197 | |
| 198 | /* More standard operations (here for binary compatibility) */ |
| 199 | |
| 200 | hashfunc tp_hash; |
| 201 | ternaryfunc tp_call; |
| 202 | reprfunc tp_str; |
| 203 | getattrofunc tp_getattro; |
| 204 | setattrofunc tp_setattro; |
| 205 | |
| 206 | /* Functions to access object as input/output buffer */ |
| 207 | PyBufferProcs *tp_as_buffer; |
| 208 | |
| 209 | /* Flags to define presence of optional/expanded features */ |
| 210 | unsigned long tp_flags; |
| 211 | |
| 212 | const char *tp_doc; /* Documentation string */ |
| 213 | |
| 214 | /* Assigned meaning in release 2.0 */ |
| 215 | /* call function for all accessible objects */ |
| 216 | traverseproc tp_traverse; |
| 217 | |
| 218 | /* delete references to contained objects */ |
| 219 | inquiry tp_clear; |
| 220 | |
| 221 | /* Assigned meaning in release 2.1 */ |
| 222 | /* rich comparisons */ |
| 223 | richcmpfunc tp_richcompare; |
| 224 | |
| 225 | /* weak reference enabler */ |
| 226 | Py_ssize_t tp_weaklistoffset; |
| 227 | |
| 228 | /* Iterators */ |
| 229 | getiterfunc tp_iter; |
| 230 | iternextfunc tp_iternext; |
| 231 | |
| 232 | /* Attribute descriptor and subclassing stuff */ |
| 233 | struct PyMethodDef *tp_methods; |
| 234 | struct PyMemberDef *tp_members; |
| 235 | struct PyGetSetDef *tp_getset; |
| 236 | struct _typeobject *tp_base; |
| 237 | PyObject *tp_dict; |
| 238 | descrgetfunc tp_descr_get; |
| 239 | descrsetfunc tp_descr_set; |
| 240 | Py_ssize_t tp_dictoffset; |
| 241 | initproc tp_init; |
| 242 | allocfunc tp_alloc; |
| 243 | newfunc tp_new; |
| 244 | freefunc tp_free; /* Low-level free-memory routine */ |
| 245 | inquiry tp_is_gc; /* For PyObject_IS_GC */ |
| 246 | PyObject *tp_bases; |
| 247 | PyObject *tp_mro; /* method resolution order */ |
| 248 | PyObject *tp_cache; |
| 249 | PyObject *tp_subclasses; |
| 250 | PyObject *tp_weaklist; |
| 251 | destructor tp_del; |
| 252 | |
| 253 | /* Type attribute cache version tag. Added in version 2.6 */ |
| 254 | unsigned int tp_version_tag; |
| 255 | |
| 256 | destructor tp_finalize; |
| 257 | |
| 258 | #ifdef COUNT_ALLOCS |
| 259 | /* these must be last and never explicitly initialized */ |
| 260 | Py_ssize_t tp_allocs; |
| 261 | Py_ssize_t tp_frees; |
| 262 | Py_ssize_t tp_maxalloc; |
| 263 | struct _typeobject *tp_prev; |
| 264 | struct _typeobject *tp_next; |
| 265 | #endif |
| 266 | } PyTypeObject; |
| 267 | |
| 268 | /* The *real* layout of a type object when allocated on the heap */ |
| 269 | typedef struct _heaptypeobject { |
| 270 | /* Note: there's a dependency on the order of these members |
| 271 | in slotptr() in typeobject.c . */ |
| 272 | PyTypeObject ht_type; |
| 273 | PyAsyncMethods as_async; |
| 274 | PyNumberMethods as_number; |
| 275 | PyMappingMethods as_mapping; |
| 276 | PySequenceMethods as_sequence; /* as_sequence comes after as_mapping, |
| 277 | so that the mapping wins when both |
| 278 | the mapping and the sequence define |
| 279 | a given operator (e.g. __getitem__). |
| 280 | see add_operators() in typeobject.c . */ |
| 281 | PyBufferProcs as_buffer; |
| 282 | PyObject *ht_name, *ht_slots, *ht_qualname; |
| 283 | struct _dictkeysobject *ht_cached_keys; |
| 284 | /* here are optional user slots, followed by the members. */ |
| 285 | } PyHeapTypeObject; |
| 286 | |
| 287 | /* access macro to the members which are floating "behind" the object */ |
| 288 | #define PyHeapType_GET_MEMBERS(etype) \ |
| 289 | ((PyMemberDef *)(((char *)etype) + Py_TYPE(etype)->tp_basicsize)) |
| 290 | |
| 291 | PyAPI_FUNC(const char *) _PyType_Name(PyTypeObject *); |
| 292 | PyAPI_FUNC(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *); |
| 293 | PyAPI_FUNC(PyObject *) _PyType_LookupId(PyTypeObject *, _Py_Identifier *); |
| 294 | PyAPI_FUNC(PyObject *) _PyObject_LookupSpecial(PyObject *, _Py_Identifier *); |
| 295 | PyAPI_FUNC(PyTypeObject *) _PyType_CalculateMetaclass(PyTypeObject *, PyObject *); |
| 296 | PyAPI_FUNC(PyObject *) _PyType_GetDocFromInternalDoc(const char *, const char *); |
| 297 | PyAPI_FUNC(PyObject *) _PyType_GetTextSignatureFromInternalDoc(const char *, const char *); |
| 298 | |
| 299 | struct _Py_Identifier; |
| 300 | PyAPI_FUNC(int) PyObject_Print(PyObject *, FILE *, int); |
| 301 | PyAPI_FUNC(void) _Py_BreakPoint(void); |
| 302 | PyAPI_FUNC(void) _PyObject_Dump(PyObject *); |
| 303 | PyAPI_FUNC(int) _PyObject_IsFreed(PyObject *); |
| 304 | |
| 305 | PyAPI_FUNC(int) _PyObject_IsAbstract(PyObject *); |
| 306 | PyAPI_FUNC(PyObject *) _PyObject_GetAttrId(PyObject *, struct _Py_Identifier *); |
| 307 | PyAPI_FUNC(int) _PyObject_SetAttrId(PyObject *, struct _Py_Identifier *, PyObject *); |
| 308 | PyAPI_FUNC(int) _PyObject_HasAttrId(PyObject *, struct _Py_Identifier *); |
| 309 | /* Replacements of PyObject_GetAttr() and _PyObject_GetAttrId() which |
| 310 | don't raise AttributeError. |
| 311 | |
| 312 | Return 1 and set *result != NULL if an attribute is found. |
| 313 | Return 0 and set *result == NULL if an attribute is not found; |
| 314 | an AttributeError is silenced. |
| 315 | Return -1 and set *result == NULL if an error other than AttributeError |
| 316 | is raised. |
| 317 | */ |
| 318 | PyAPI_FUNC(int) _PyObject_LookupAttr(PyObject *, PyObject *, PyObject **); |
| 319 | PyAPI_FUNC(int) _PyObject_LookupAttrId(PyObject *, struct _Py_Identifier *, PyObject **); |
| 320 | PyAPI_FUNC(PyObject **) _PyObject_GetDictPtr(PyObject *); |
| 321 | PyAPI_FUNC(PyObject *) _PyObject_NextNotImplemented(PyObject *); |
| 322 | PyAPI_FUNC(void) PyObject_CallFinalizer(PyObject *); |
| 323 | PyAPI_FUNC(int) PyObject_CallFinalizerFromDealloc(PyObject *); |
| 324 | |
| 325 | /* Same as PyObject_Generic{Get,Set}Attr, but passing the attributes |
| 326 | dict as the last parameter. */ |
| 327 | PyAPI_FUNC(PyObject *) |
| 328 | _PyObject_GenericGetAttrWithDict(PyObject *, PyObject *, PyObject *, int); |
| 329 | PyAPI_FUNC(int) |
| 330 | _PyObject_GenericSetAttrWithDict(PyObject *, PyObject *, |
| 331 | PyObject *, PyObject *); |
| 332 | |
Victor Stinner | 6eb9966 | 2018-11-26 17:09:16 +0100 | [diff] [blame] | 333 | #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0) |
| 334 | |
| 335 | static inline void _Py_Dealloc_inline(PyObject *op) |
| 336 | { |
| 337 | destructor dealloc = Py_TYPE(op)->tp_dealloc; |
| 338 | #ifdef Py_TRACE_REFS |
| 339 | _Py_ForgetReference(op); |
| 340 | #else |
| 341 | _Py_INC_TPFREES(op); |
| 342 | #endif |
| 343 | (*dealloc)(op); |
| 344 | } |
| 345 | #define _Py_Dealloc(op) _Py_Dealloc_inline(op) |
| 346 | |
| 347 | |
| 348 | /* Safely decref `op` and set `op` to `op2`. |
| 349 | * |
| 350 | * As in case of Py_CLEAR "the obvious" code can be deadly: |
| 351 | * |
| 352 | * Py_DECREF(op); |
| 353 | * op = op2; |
| 354 | * |
| 355 | * The safe way is: |
| 356 | * |
| 357 | * Py_SETREF(op, op2); |
| 358 | * |
| 359 | * That arranges to set `op` to `op2` _before_ decref'ing, so that any code |
| 360 | * triggered as a side-effect of `op` getting torn down no longer believes |
| 361 | * `op` points to a valid object. |
| 362 | * |
| 363 | * Py_XSETREF is a variant of Py_SETREF that uses Py_XDECREF instead of |
| 364 | * Py_DECREF. |
| 365 | */ |
| 366 | |
| 367 | #define Py_SETREF(op, op2) \ |
| 368 | do { \ |
| 369 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
| 370 | (op) = (op2); \ |
| 371 | Py_DECREF(_py_tmp); \ |
| 372 | } while (0) |
| 373 | |
| 374 | #define Py_XSETREF(op, op2) \ |
| 375 | do { \ |
| 376 | PyObject *_py_tmp = _PyObject_CAST(op); \ |
| 377 | (op) = (op2); \ |
| 378 | Py_XDECREF(_py_tmp); \ |
| 379 | } while (0) |
| 380 | |
| 381 | |
| 382 | PyAPI_DATA(PyTypeObject) _PyNone_Type; |
| 383 | PyAPI_DATA(PyTypeObject) _PyNotImplemented_Type; |
| 384 | |
| 385 | /* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE. |
| 386 | * Defined in object.c. |
| 387 | */ |
| 388 | PyAPI_DATA(int) _Py_SwappedOp[]; |
| 389 | |
| 390 | /* This is the old private API, invoked by the macros before 3.2.4. |
| 391 | Kept for binary compatibility of extensions using the stable ABI. */ |
| 392 | PyAPI_FUNC(void) _PyTrash_deposit_object(PyObject*); |
| 393 | PyAPI_FUNC(void) _PyTrash_destroy_chain(void); |
| 394 | |
| 395 | PyAPI_FUNC(void) |
| 396 | _PyDebugAllocatorStats(FILE *out, const char *block_name, int num_blocks, |
| 397 | size_t sizeof_block); |
| 398 | PyAPI_FUNC(void) |
| 399 | _PyObject_DebugTypeStats(FILE *out); |
| 400 | |
| 401 | /* Define a pair of assertion macros: |
| 402 | _PyObject_ASSERT_FROM(), _PyObject_ASSERT_WITH_MSG() and _PyObject_ASSERT(). |
| 403 | |
| 404 | These work like the regular C assert(), in that they will abort the |
| 405 | process with a message on stderr if the given condition fails to hold, |
| 406 | but compile away to nothing if NDEBUG is defined. |
| 407 | |
| 408 | However, before aborting, Python will also try to call _PyObject_Dump() on |
| 409 | the given object. This may be of use when investigating bugs in which a |
| 410 | particular object is corrupt (e.g. buggy a tp_visit method in an extension |
| 411 | module breaking the garbage collector), to help locate the broken objects. |
| 412 | |
| 413 | The WITH_MSG variant allows you to supply an additional message that Python |
| 414 | will attempt to print to stderr, after the object dump. */ |
| 415 | #ifdef NDEBUG |
| 416 | /* No debugging: compile away the assertions: */ |
| 417 | # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \ |
| 418 | ((void)0) |
| 419 | #else |
| 420 | /* With debugging: generate checks: */ |
| 421 | # define _PyObject_ASSERT_FROM(obj, expr, msg, filename, lineno, func) \ |
| 422 | ((expr) \ |
| 423 | ? (void)(0) \ |
| 424 | : _PyObject_AssertFailed((obj), Py_STRINGIFY(expr), \ |
| 425 | (msg), (filename), (lineno), (func))) |
| 426 | #endif |
| 427 | |
| 428 | #define _PyObject_ASSERT_WITH_MSG(obj, expr, msg) \ |
| 429 | _PyObject_ASSERT_FROM(obj, expr, msg, __FILE__, __LINE__, __func__) |
| 430 | #define _PyObject_ASSERT(obj, expr) \ |
| 431 | _PyObject_ASSERT_WITH_MSG(obj, expr, NULL) |
| 432 | |
| 433 | #define _PyObject_ASSERT_FAILED_MSG(obj, msg) \ |
| 434 | _PyObject_AssertFailed((obj), NULL, (msg), __FILE__, __LINE__, __func__) |
| 435 | |
| 436 | /* Declare and define _PyObject_AssertFailed() even when NDEBUG is defined, |
| 437 | to avoid causing compiler/linker errors when building extensions without |
| 438 | NDEBUG against a Python built with NDEBUG defined. |
| 439 | |
| 440 | msg, expr and function can be NULL. */ |
| 441 | PyAPI_FUNC(void) _PyObject_AssertFailed( |
| 442 | PyObject *obj, |
| 443 | const char *expr, |
| 444 | const char *msg, |
| 445 | const char *file, |
| 446 | int line, |
| 447 | const char *function); |
| 448 | |
| 449 | #ifdef __cplusplus |
| 450 | } |
| 451 | #endif |