| 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 | #ifdef Py_TRACE_REFS
|
|---|
| 65 | /* Define pointers to support a doubly-linked list of all live heap objects. */
|
|---|
| 66 | #define _PyObject_HEAD_EXTRA \
|
|---|
| 67 | struct _object *_ob_next; \
|
|---|
| 68 | struct _object *_ob_prev;
|
|---|
| 69 |
|
|---|
| 70 | #define _PyObject_EXTRA_INIT 0, 0,
|
|---|
| 71 |
|
|---|
| 72 | #else
|
|---|
| 73 | #define _PyObject_HEAD_EXTRA
|
|---|
| 74 | #define _PyObject_EXTRA_INIT
|
|---|
| 75 | #endif
|
|---|
| 76 |
|
|---|
| 77 | /* PyObject_HEAD defines the initial segment of every PyObject. */
|
|---|
| 78 | #define PyObject_HEAD \
|
|---|
| 79 | _PyObject_HEAD_EXTRA \
|
|---|
| 80 | Py_ssize_t ob_refcnt; \
|
|---|
| 81 | struct _typeobject *ob_type;
|
|---|
| 82 |
|
|---|
| 83 | #define PyObject_HEAD_INIT(type) \
|
|---|
| 84 | _PyObject_EXTRA_INIT \
|
|---|
| 85 | 1, type,
|
|---|
| 86 |
|
|---|
| 87 | /* PyObject_VAR_HEAD defines the initial segment of all variable-size
|
|---|
| 88 | * container objects. These end with a declaration of an array with 1
|
|---|
| 89 | * element, but enough space is malloc'ed so that the array actually
|
|---|
| 90 | * has room for ob_size elements. Note that ob_size is an element count,
|
|---|
| 91 | * not necessarily a byte count.
|
|---|
| 92 | */
|
|---|
| 93 | #define PyObject_VAR_HEAD \
|
|---|
| 94 | PyObject_HEAD \
|
|---|
| 95 | Py_ssize_t ob_size; /* Number of items in variable part */
|
|---|
| 96 | #define Py_INVALID_SIZE (Py_ssize_t)-1
|
|---|
| 97 |
|
|---|
| 98 | /* Nothing is actually declared to be a PyObject, but every pointer to
|
|---|
| 99 | * a Python object can be cast to a PyObject*. This is inheritance built
|
|---|
| 100 | * by hand. Similarly every pointer to a variable-size Python object can,
|
|---|
| 101 | * in addition, be cast to PyVarObject*.
|
|---|
| 102 | */
|
|---|
| 103 | typedef struct _object {
|
|---|
| 104 | PyObject_HEAD
|
|---|
| 105 | } PyObject;
|
|---|
| 106 |
|
|---|
| 107 | typedef struct {
|
|---|
| 108 | PyObject_VAR_HEAD
|
|---|
| 109 | } PyVarObject;
|
|---|
| 110 |
|
|---|
| 111 |
|
|---|
| 112 | /*
|
|---|
| 113 | Type objects contain a string containing the type name (to help somewhat
|
|---|
| 114 | in debugging), the allocation parameters (see PyObject_New() and
|
|---|
| 115 | PyObject_NewVar()),
|
|---|
| 116 | and methods for accessing objects of the type. Methods are optional, a
|
|---|
| 117 | nil pointer meaning that particular kind of access is not available for
|
|---|
| 118 | this type. The Py_DECREF() macro uses the tp_dealloc method without
|
|---|
| 119 | checking for a nil pointer; it should always be implemented except if
|
|---|
| 120 | the implementation can guarantee that the reference count will never
|
|---|
| 121 | reach zero (e.g., for statically allocated type objects).
|
|---|
| 122 |
|
|---|
| 123 | NB: the methods for certain type groups are now contained in separate
|
|---|
| 124 | method blocks.
|
|---|
| 125 | */
|
|---|
| 126 |
|
|---|
| 127 | typedef PyObject * (*unaryfunc)(PyObject *);
|
|---|
| 128 | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *);
|
|---|
| 129 | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *);
|
|---|
| 130 | typedef int (*inquiry)(PyObject *);
|
|---|
| 131 | typedef Py_ssize_t (*lenfunc)(PyObject *);
|
|---|
| 132 | typedef int (*coercion)(PyObject **, PyObject **);
|
|---|
| 133 | typedef PyObject *(*intargfunc)(PyObject *, int) Py_DEPRECATED(2.5);
|
|---|
| 134 | typedef PyObject *(*intintargfunc)(PyObject *, int, int) Py_DEPRECATED(2.5);
|
|---|
| 135 | typedef PyObject *(*ssizeargfunc)(PyObject *, Py_ssize_t);
|
|---|
| 136 | typedef PyObject *(*ssizessizeargfunc)(PyObject *, Py_ssize_t, Py_ssize_t);
|
|---|
| 137 | typedef int(*intobjargproc)(PyObject *, int, PyObject *);
|
|---|
| 138 | typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *);
|
|---|
| 139 | typedef int(*ssizeobjargproc)(PyObject *, Py_ssize_t, PyObject *);
|
|---|
| 140 | typedef int(*ssizessizeobjargproc)(PyObject *, Py_ssize_t, Py_ssize_t, PyObject *);
|
|---|
| 141 | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *);
|
|---|
| 142 |
|
|---|
| 143 | /* int-based buffer interface */
|
|---|
| 144 | typedef int (*getreadbufferproc)(PyObject *, int, void **);
|
|---|
| 145 | typedef int (*getwritebufferproc)(PyObject *, int, void **);
|
|---|
| 146 | typedef int (*getsegcountproc)(PyObject *, int *);
|
|---|
| 147 | typedef int (*getcharbufferproc)(PyObject *, int, char **);
|
|---|
| 148 | /* ssize_t-based buffer interface */
|
|---|
| 149 | typedef Py_ssize_t (*readbufferproc)(PyObject *, Py_ssize_t, void **);
|
|---|
| 150 | typedef Py_ssize_t (*writebufferproc)(PyObject *, Py_ssize_t, void **);
|
|---|
| 151 | typedef Py_ssize_t (*segcountproc)(PyObject *, Py_ssize_t *);
|
|---|
| 152 | typedef Py_ssize_t (*charbufferproc)(PyObject *, Py_ssize_t, char **);
|
|---|
| 153 |
|
|---|
| 154 | typedef int (*objobjproc)(PyObject *, PyObject *);
|
|---|
| 155 | typedef int (*visitproc)(PyObject *, void *);
|
|---|
| 156 | typedef int (*traverseproc)(PyObject *, visitproc, void *);
|
|---|
| 157 |
|
|---|
| 158 | typedef struct {
|
|---|
| 159 | /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
|
|---|
| 160 | arguments are guaranteed to be of the object's type (modulo
|
|---|
| 161 | coercion hacks -- i.e. if the type's coercion function
|
|---|
| 162 | returns other types, then these are allowed as well). Numbers that
|
|---|
| 163 | have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
|
|---|
| 164 | arguments for proper type and implement the necessary conversions
|
|---|
| 165 | in the slot functions themselves. */
|
|---|
| 166 |
|
|---|
| 167 | binaryfunc nb_add;
|
|---|
| 168 | binaryfunc nb_subtract;
|
|---|
| 169 | binaryfunc nb_multiply;
|
|---|
| 170 | binaryfunc nb_divide;
|
|---|
| 171 | binaryfunc nb_remainder;
|
|---|
| 172 | binaryfunc nb_divmod;
|
|---|
| 173 | ternaryfunc nb_power;
|
|---|
| 174 | unaryfunc nb_negative;
|
|---|
| 175 | unaryfunc nb_positive;
|
|---|
| 176 | unaryfunc nb_absolute;
|
|---|
| 177 | inquiry nb_nonzero;
|
|---|
| 178 | unaryfunc nb_invert;
|
|---|
| 179 | binaryfunc nb_lshift;
|
|---|
| 180 | binaryfunc nb_rshift;
|
|---|
| 181 | binaryfunc nb_and;
|
|---|
| 182 | binaryfunc nb_xor;
|
|---|
| 183 | binaryfunc nb_or;
|
|---|
| 184 | coercion nb_coerce;
|
|---|
| 185 | unaryfunc nb_int;
|
|---|
| 186 | unaryfunc nb_long;
|
|---|
| 187 | unaryfunc nb_float;
|
|---|
| 188 | unaryfunc nb_oct;
|
|---|
| 189 | unaryfunc nb_hex;
|
|---|
| 190 | /* Added in release 2.0 */
|
|---|
| 191 | binaryfunc nb_inplace_add;
|
|---|
| 192 | binaryfunc nb_inplace_subtract;
|
|---|
| 193 | binaryfunc nb_inplace_multiply;
|
|---|
| 194 | binaryfunc nb_inplace_divide;
|
|---|
| 195 | binaryfunc nb_inplace_remainder;
|
|---|
| 196 | ternaryfunc nb_inplace_power;
|
|---|
| 197 | binaryfunc nb_inplace_lshift;
|
|---|
| 198 | binaryfunc nb_inplace_rshift;
|
|---|
| 199 | binaryfunc nb_inplace_and;
|
|---|
| 200 | binaryfunc nb_inplace_xor;
|
|---|
| 201 | binaryfunc nb_inplace_or;
|
|---|
| 202 |
|
|---|
| 203 | /* Added in release 2.2 */
|
|---|
| 204 | /* The following require the Py_TPFLAGS_HAVE_CLASS flag */
|
|---|
| 205 | binaryfunc nb_floor_divide;
|
|---|
| 206 | binaryfunc nb_true_divide;
|
|---|
| 207 | binaryfunc nb_inplace_floor_divide;
|
|---|
| 208 | binaryfunc nb_inplace_true_divide;
|
|---|
| 209 |
|
|---|
| 210 | /* Added in release 2.5 */
|
|---|
| 211 | unaryfunc nb_index;
|
|---|
| 212 | } PyNumberMethods;
|
|---|
| 213 |
|
|---|
| 214 | typedef struct {
|
|---|
| 215 | lenfunc sq_length;
|
|---|
| 216 | binaryfunc sq_concat;
|
|---|
| 217 | ssizeargfunc sq_repeat;
|
|---|
| 218 | ssizeargfunc sq_item;
|
|---|
| 219 | ssizessizeargfunc sq_slice;
|
|---|
| 220 | ssizeobjargproc sq_ass_item;
|
|---|
| 221 | ssizessizeobjargproc sq_ass_slice;
|
|---|
| 222 | objobjproc sq_contains;
|
|---|
| 223 | /* Added in release 2.0 */
|
|---|
| 224 | binaryfunc sq_inplace_concat;
|
|---|
| 225 | ssizeargfunc sq_inplace_repeat;
|
|---|
| 226 | } PySequenceMethods;
|
|---|
| 227 |
|
|---|
| 228 | typedef struct {
|
|---|
| 229 | lenfunc mp_length;
|
|---|
| 230 | binaryfunc mp_subscript;
|
|---|
| 231 | objobjargproc mp_ass_subscript;
|
|---|
| 232 | } PyMappingMethods;
|
|---|
| 233 |
|
|---|
| 234 | typedef struct {
|
|---|
| 235 | readbufferproc bf_getreadbuffer;
|
|---|
| 236 | writebufferproc bf_getwritebuffer;
|
|---|
| 237 | segcountproc bf_getsegcount;
|
|---|
| 238 | charbufferproc bf_getcharbuffer;
|
|---|
| 239 | } PyBufferProcs;
|
|---|
| 240 |
|
|---|
| 241 |
|
|---|
| 242 | typedef void (*freefunc)(void *);
|
|---|
| 243 | typedef void (*destructor)(PyObject *);
|
|---|
| 244 | typedef int (*printfunc)(PyObject *, FILE *, int);
|
|---|
| 245 | typedef PyObject *(*getattrfunc)(PyObject *, char *);
|
|---|
| 246 | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *);
|
|---|
| 247 | typedef int (*setattrfunc)(PyObject *, char *, PyObject *);
|
|---|
| 248 | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
|
|---|
| 249 | typedef int (*cmpfunc)(PyObject *, PyObject *);
|
|---|
| 250 | typedef PyObject *(*reprfunc)(PyObject *);
|
|---|
| 251 | typedef long (*hashfunc)(PyObject *);
|
|---|
| 252 | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
|
|---|
| 253 | typedef PyObject *(*getiterfunc) (PyObject *);
|
|---|
| 254 | typedef PyObject *(*iternextfunc) (PyObject *);
|
|---|
| 255 | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *);
|
|---|
| 256 | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *);
|
|---|
| 257 | typedef int (*initproc)(PyObject *, PyObject *, PyObject *);
|
|---|
| 258 | typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *);
|
|---|
| 259 | typedef PyObject *(*allocfunc)(struct _typeobject *, Py_ssize_t);
|
|---|
| 260 |
|
|---|
| 261 | typedef struct _typeobject {
|
|---|
| 262 | PyObject_VAR_HEAD
|
|---|
| 263 | const char *tp_name; /* For printing, in format "<module>.<name>" */
|
|---|
| 264 | Py_ssize_t tp_basicsize, tp_itemsize; /* For allocation */
|
|---|
| 265 |
|
|---|
| 266 | /* Methods to implement standard operations */
|
|---|
| 267 |
|
|---|
| 268 | destructor tp_dealloc;
|
|---|
| 269 | printfunc tp_print;
|
|---|
| 270 | getattrfunc tp_getattr;
|
|---|
| 271 | setattrfunc tp_setattr;
|
|---|
| 272 | cmpfunc tp_compare;
|
|---|
| 273 | reprfunc tp_repr;
|
|---|
| 274 |
|
|---|
| 275 | /* Method suites for standard classes */
|
|---|
| 276 |
|
|---|
| 277 | PyNumberMethods *tp_as_number;
|
|---|
| 278 | PySequenceMethods *tp_as_sequence;
|
|---|
| 279 | PyMappingMethods *tp_as_mapping;
|
|---|
| 280 |
|
|---|
| 281 | /* More standard operations (here for binary compatibility) */
|
|---|
| 282 |
|
|---|
| 283 | hashfunc tp_hash;
|
|---|
| 284 | ternaryfunc tp_call;
|
|---|
| 285 | reprfunc tp_str;
|
|---|
| 286 | getattrofunc tp_getattro;
|
|---|
| 287 | setattrofunc tp_setattro;
|
|---|
| 288 |
|
|---|
| 289 | /* Functions to access object as input/output buffer */
|
|---|
| 290 | PyBufferProcs *tp_as_buffer;
|
|---|
| 291 |
|
|---|
| 292 | /* Flags to define presence of optional/expanded features */
|
|---|
| 293 | long tp_flags;
|
|---|
| 294 |
|
|---|
| 295 | const char *tp_doc; /* Documentation string */
|
|---|
| 296 |
|
|---|
| 297 | /* Assigned meaning in release 2.0 */
|
|---|
| 298 | /* call function for all accessible objects */
|
|---|
| 299 | traverseproc tp_traverse;
|
|---|
| 300 |
|
|---|
| 301 | /* delete references to contained objects */
|
|---|
| 302 | inquiry tp_clear;
|
|---|
| 303 |
|
|---|
| 304 | /* Assigned meaning in release 2.1 */
|
|---|
| 305 | /* rich comparisons */
|
|---|
| 306 | richcmpfunc tp_richcompare;
|
|---|
| 307 |
|
|---|
| 308 | /* weak reference enabler */
|
|---|
| 309 | Py_ssize_t tp_weaklistoffset;
|
|---|
| 310 |
|
|---|
| 311 | /* Added in release 2.2 */
|
|---|
| 312 | /* Iterators */
|
|---|
| 313 | getiterfunc tp_iter;
|
|---|
| 314 | iternextfunc tp_iternext;
|
|---|
| 315 |
|
|---|
| 316 | /* Attribute descriptor and subclassing stuff */
|
|---|
| 317 | struct PyMethodDef *tp_methods;
|
|---|
| 318 | struct PyMemberDef *tp_members;
|
|---|
| 319 | struct PyGetSetDef *tp_getset;
|
|---|
| 320 | struct _typeobject *tp_base;
|
|---|
| 321 | PyObject *tp_dict;
|
|---|
| 322 | descrgetfunc tp_descr_get;
|
|---|
| 323 | descrsetfunc tp_descr_set;
|
|---|
| 324 | Py_ssize_t tp_dictoffset;
|
|---|
| 325 | initproc tp_init;
|
|---|
| 326 | allocfunc tp_alloc;
|
|---|
| 327 | newfunc tp_new;
|
|---|
| 328 | freefunc tp_free; /* Low-level free-memory routine */
|
|---|
| 329 | inquiry tp_is_gc; /* For PyObject_IS_GC */
|
|---|
| 330 | PyObject *tp_bases;
|
|---|
| 331 | PyObject *tp_mro; /* method resolution order */
|
|---|
| 332 | PyObject *tp_cache;
|
|---|
| 333 | PyObject *tp_subclasses;
|
|---|
| 334 | PyObject *tp_weaklist;
|
|---|
| 335 | destructor tp_del;
|
|---|
| 336 |
|
|---|
| 337 | #ifdef COUNT_ALLOCS
|
|---|
| 338 | /* these must be last and never explicitly initialized */
|
|---|
| 339 | Py_ssize_t tp_allocs;
|
|---|
| 340 | Py_ssize_t tp_frees;
|
|---|
| 341 | Py_ssize_t tp_maxalloc;
|
|---|
| 342 | struct _typeobject *tp_prev;
|
|---|
| 343 | struct _typeobject *tp_next;
|
|---|
| 344 | #endif
|
|---|
| 345 | } PyTypeObject;
|
|---|
| 346 |
|
|---|
| 347 |
|
|---|
|
|---|