Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 1 | #ifndef Py_OBJECT_H |
| 2 | #define Py_OBJECT_H |
| 3 | #ifdef __cplusplus |
| 4 | extern "C" { |
| 5 | #endif |
| 6 | |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 7 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 8 | /* Object and type object interface */ |
| 9 | |
| 10 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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.) |
| 17 | |
| 18 | An object has a 'reference count' that is increased or decreased when a |
| 19 | pointer to the object is copied or deleted; when the reference count |
| 20 | reaches zero there are no references to the object left and it can be |
| 21 | removed from the heap. |
| 22 | |
| 23 | An object has a 'type' that determines what it represents and what kind |
| 24 | of data it contains. An object's type is fixed when it is created. |
| 25 | Types themselves are represented as objects; an object contains a |
| 26 | pointer to the corresponding type object. The type itself has a type |
| 27 | pointer pointing to the object representing the type 'type', which |
| 28 | contains a pointer to itself!). |
| 29 | |
| 30 | Objects do not float around in memory; once allocated an object keeps |
| 31 | the same size and address. Objects that must hold variable-size data |
| 32 | can contain pointers to variable-size parts of the object. Not all |
| 33 | objects of the same type have the same size; but the size cannot change |
| 34 | after allocation. (These restrictions are made so a reference to an |
| 35 | object can be simply a pointer -- moving an object would require |
| 36 | updating all the pointers, and changing an object's size would require |
| 37 | moving it if there was another object right next to it.) |
| 38 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 39 | Objects are always accessed through pointers of the type 'PyObject *'. |
| 40 | The type 'PyObject' is a structure that only contains the reference count |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | and the type pointer. The actual memory allocated for an object |
| 42 | contains other data that can only be accessed after casting the pointer |
| 43 | to a pointer to a longer structure type. This longer type must start |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 44 | with the reference count and type fields; the macro PyObject_HEAD should be |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 45 | used for this (to accommodate for future changes). The implementation |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | of a particular object type can cast the object pointer to the proper |
| 47 | type and back. |
| 48 | |
| 49 | A standard interface exists for objects that contain an array of items |
| 50 | whose size is determined when the object is allocated. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 51 | */ |
| 52 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 53 | #ifdef Py_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 54 | |
| 55 | /* Turn on heavy reference debugging */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 56 | #define Py_TRACE_REFS |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 57 | |
| 58 | /* Turn on reference counting */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 59 | #define Py_REF_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 60 | |
Guido van Rossum | 408027e | 1996-12-30 16:17:54 +0000 | [diff] [blame] | 61 | #endif /* Py_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 62 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 63 | #ifdef Py_TRACE_REFS |
| 64 | #define PyObject_HEAD \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 65 | struct _object *_ob_next, *_ob_prev; \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 66 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 67 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 68 | #define PyObject_HEAD_INIT(type) 0, 0, 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 69 | #else /* !Py_TRACE_REFS */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 70 | #define PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 71 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 73 | #define PyObject_HEAD_INIT(type) 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 74 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 76 | #define PyObject_VAR_HEAD \ |
| 77 | PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 78 | int ob_size; /* Number of items in variable part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 79 | |
| 80 | typedef struct _object { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 81 | PyObject_HEAD |
| 82 | } PyObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | |
| 84 | typedef struct { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 85 | PyObject_VAR_HEAD |
Guido van Rossum | d0c87ee | 1997-05-15 21:31:03 +0000 | [diff] [blame] | 86 | } PyVarObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 87 | |
| 88 | |
| 89 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 90 | Type objects contain a string containing the type name (to help somewhat |
| 91 | in debugging), the allocation parameters (see newobj() and newvarobj()), |
| 92 | and methods for accessing objects of the type. Methods are optional,a |
| 93 | nil pointer meaning that particular kind of access is not available for |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 94 | this type. The Py_DECREF() macro uses the tp_dealloc method without |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | checking for a nil pointer; it should always be implemented except if |
| 96 | the implementation can guarantee that the reference count will never |
| 97 | reach zero (e.g., for type objects). |
| 98 | |
| 99 | NB: the methods for certain type groups are now contained in separate |
| 100 | method blocks. |
| 101 | */ |
| 102 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 103 | typedef PyObject * (*unaryfunc)(PyObject *); |
| 104 | typedef PyObject * (*binaryfunc)(PyObject *, PyObject *); |
| 105 | typedef PyObject * (*ternaryfunc)(PyObject *, PyObject *, PyObject *); |
| 106 | typedef int (*inquiry)(PyObject *); |
| 107 | typedef int (*coercion)(PyObject **, PyObject **); |
| 108 | typedef PyObject *(*intargfunc)(PyObject *, int); |
| 109 | typedef PyObject *(*intintargfunc)(PyObject *, int, int); |
| 110 | typedef int(*intobjargproc)(PyObject *, int, PyObject *); |
| 111 | typedef int(*intintobjargproc)(PyObject *, int, int, PyObject *); |
| 112 | typedef int(*objobjargproc)(PyObject *, PyObject *, PyObject *); |
| 113 | typedef int (*getreadbufferproc)(PyObject *, int, void **); |
| 114 | typedef int (*getwritebufferproc)(PyObject *, int, void **); |
| 115 | typedef int (*getsegcountproc)(PyObject *, int *); |
| 116 | typedef int (*getcharbufferproc)(PyObject *, int, const char **); |
| 117 | typedef int (*objobjproc)(PyObject *, PyObject *); |
| 118 | typedef int (*visitproc)(PyObject *, void *); |
| 119 | typedef int (*traverseproc)(PyObject *, visitproc, void *); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 120 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | typedef struct { |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 122 | /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all |
| 123 | arguments are guaranteed to be of the object's type (modulo |
| 124 | coercion hacks that is -- i.e. if the type's coercion function |
| 125 | returns other types, then these are allowed as well). Numbers that |
| 126 | have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both* |
| 127 | arguments for proper type and implement the necessary conversions |
| 128 | in the slot functions themselves. */ |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 129 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 130 | binaryfunc nb_add; |
| 131 | binaryfunc nb_subtract; |
| 132 | binaryfunc nb_multiply; |
| 133 | binaryfunc nb_divide; |
| 134 | binaryfunc nb_remainder; |
| 135 | binaryfunc nb_divmod; |
Guido van Rossum | 75abc63 | 1994-08-09 13:21:54 +0000 | [diff] [blame] | 136 | ternaryfunc nb_power; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 137 | unaryfunc nb_negative; |
| 138 | unaryfunc nb_positive; |
| 139 | unaryfunc nb_absolute; |
| 140 | inquiry nb_nonzero; |
| 141 | unaryfunc nb_invert; |
| 142 | binaryfunc nb_lshift; |
| 143 | binaryfunc nb_rshift; |
| 144 | binaryfunc nb_and; |
| 145 | binaryfunc nb_xor; |
| 146 | binaryfunc nb_or; |
| 147 | coercion nb_coerce; |
| 148 | unaryfunc nb_int; |
| 149 | unaryfunc nb_long; |
| 150 | unaryfunc nb_float; |
| 151 | unaryfunc nb_oct; |
| 152 | unaryfunc nb_hex; |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 153 | /* Added in release 2.0 */ |
Thomas Wouters | dd8dbdb | 2000-08-24 20:09:45 +0000 | [diff] [blame] | 154 | binaryfunc nb_inplace_add; |
| 155 | binaryfunc nb_inplace_subtract; |
| 156 | binaryfunc nb_inplace_multiply; |
| 157 | binaryfunc nb_inplace_divide; |
| 158 | binaryfunc nb_inplace_remainder; |
| 159 | ternaryfunc nb_inplace_power; |
| 160 | binaryfunc nb_inplace_lshift; |
| 161 | binaryfunc nb_inplace_rshift; |
| 162 | binaryfunc nb_inplace_and; |
| 163 | binaryfunc nb_inplace_xor; |
| 164 | binaryfunc nb_inplace_or; |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 165 | |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 166 | /* Added in release 2.2 */ |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 167 | /* The following require the Py_TPFLAGS_HAVE_CLASS flag */ |
| 168 | binaryfunc nb_floor_divide; |
| 169 | binaryfunc nb_true_divide; |
| 170 | binaryfunc nb_inplace_floor_divide; |
| 171 | binaryfunc nb_inplace_true_divide; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 172 | } PyNumberMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | |
| 174 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 175 | inquiry sq_length; |
| 176 | binaryfunc sq_concat; |
| 177 | intargfunc sq_repeat; |
| 178 | intargfunc sq_item; |
| 179 | intintargfunc sq_slice; |
| 180 | intobjargproc sq_ass_item; |
| 181 | intintobjargproc sq_ass_slice; |
Guido van Rossum | cecb27a | 2000-02-28 15:00:40 +0000 | [diff] [blame] | 182 | objobjproc sq_contains; |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 183 | /* Added in release 2.0 */ |
Thomas Wouters | dd8dbdb | 2000-08-24 20:09:45 +0000 | [diff] [blame] | 184 | binaryfunc sq_inplace_concat; |
| 185 | intargfunc sq_inplace_repeat; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 186 | } PySequenceMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | |
| 188 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | inquiry mp_length; |
| 190 | binaryfunc mp_subscript; |
| 191 | objobjargproc mp_ass_subscript; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 192 | } PyMappingMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 193 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 194 | typedef struct { |
| 195 | getreadbufferproc bf_getreadbuffer; |
| 196 | getwritebufferproc bf_getwritebuffer; |
| 197 | getsegcountproc bf_getsegcount; |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 198 | getcharbufferproc bf_getcharbuffer; |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 199 | } PyBufferProcs; |
| 200 | |
| 201 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 202 | typedef void (*destructor)(PyObject *); |
| 203 | typedef int (*printfunc)(PyObject *, FILE *, int); |
| 204 | typedef PyObject *(*getattrfunc)(PyObject *, char *); |
| 205 | typedef PyObject *(*getattrofunc)(PyObject *, PyObject *); |
| 206 | typedef int (*setattrfunc)(PyObject *, char *, PyObject *); |
| 207 | typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); |
| 208 | typedef int (*cmpfunc)(PyObject *, PyObject *); |
| 209 | typedef PyObject *(*reprfunc)(PyObject *); |
| 210 | typedef long (*hashfunc)(PyObject *); |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 211 | typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 212 | typedef PyObject *(*getiterfunc) (PyObject *); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 213 | typedef PyObject *(*iternextfunc) (PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 214 | typedef PyObject *(*descrgetfunc) (PyObject *, PyObject *, PyObject *); |
| 215 | typedef int (*descrsetfunc) (PyObject *, PyObject *, PyObject *); |
| 216 | typedef int (*initproc)(PyObject *, PyObject *, PyObject *); |
| 217 | typedef PyObject *(*newfunc)(struct _typeobject *, PyObject *, PyObject *); |
| 218 | typedef PyObject *(*allocfunc)(struct _typeobject *, int); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 220 | typedef struct _typeobject { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 221 | PyObject_VAR_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 222 | char *tp_name; /* For printing */ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 223 | int tp_basicsize, tp_itemsize; /* For allocation */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 224 | |
| 225 | /* Methods to implement standard operations */ |
| 226 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 227 | destructor tp_dealloc; |
| 228 | printfunc tp_print; |
| 229 | getattrfunc tp_getattr; |
| 230 | setattrfunc tp_setattr; |
| 231 | cmpfunc tp_compare; |
| 232 | reprfunc tp_repr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 233 | |
| 234 | /* Method suites for standard classes */ |
| 235 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 236 | PyNumberMethods *tp_as_number; |
| 237 | PySequenceMethods *tp_as_sequence; |
| 238 | PyMappingMethods *tp_as_mapping; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 239 | |
Fred Drake | 0e12bcd | 2000-03-21 16:14:47 +0000 | [diff] [blame] | 240 | /* More standard operations (here for binary compatibility) */ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 242 | hashfunc tp_hash; |
Guido van Rossum | 884afd6 | 1995-07-18 14:21:06 +0000 | [diff] [blame] | 243 | ternaryfunc tp_call; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 244 | reprfunc tp_str; |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame] | 245 | getattrofunc tp_getattro; |
| 246 | setattrofunc tp_setattro; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 247 | |
Guido van Rossum | fdf95dd | 1997-05-05 22:15:02 +0000 | [diff] [blame] | 248 | /* Functions to access object as input/output buffer */ |
| 249 | PyBufferProcs *tp_as_buffer; |
| 250 | |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 251 | /* Flags to define presence of optional/expanded features */ |
| 252 | long tp_flags; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 253 | |
| 254 | char *tp_doc; /* Documentation string */ |
| 255 | |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 256 | /* Assigned meaning in release 2.0 */ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 257 | /* call function for all accessible objects */ |
| 258 | traverseproc tp_traverse; |
| 259 | |
| 260 | /* delete references to contained objects */ |
| 261 | inquiry tp_clear; |
| 262 | |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 263 | /* Assigned meaning in release 2.1 */ |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 264 | /* rich comparisons */ |
| 265 | richcmpfunc tp_richcompare; |
| 266 | |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 267 | /* weak reference enabler */ |
| 268 | long tp_weaklistoffset; |
Guido van Rossum | a9c2d7a | 1998-04-23 19:16:44 +0000 | [diff] [blame] | 269 | |
Fred Drake | d55657b | 2001-08-15 18:32:33 +0000 | [diff] [blame] | 270 | /* Added in release 2.2 */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 271 | /* Iterators */ |
| 272 | getiterfunc tp_iter; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 273 | iternextfunc tp_iternext; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 274 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 275 | /* Attribute descriptor and subclassing stuff */ |
| 276 | struct PyMethodDef *tp_methods; |
| 277 | struct memberlist *tp_members; |
| 278 | struct getsetlist *tp_getset; |
| 279 | struct _typeobject *tp_base; |
| 280 | PyObject *tp_dict; |
| 281 | descrgetfunc tp_descr_get; |
| 282 | descrsetfunc tp_descr_set; |
| 283 | long tp_dictoffset; |
| 284 | initproc tp_init; |
| 285 | allocfunc tp_alloc; |
| 286 | newfunc tp_new; |
| 287 | destructor tp_free; /* Low-level free-memory routine */ |
| 288 | PyObject *tp_bases; |
| 289 | PyObject *tp_mro; /* method resolution order */ |
| 290 | PyObject *tp_defined; |
| 291 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 292 | #ifdef COUNT_ALLOCS |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 293 | /* these must be last and never explicitly initialized */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 294 | int tp_allocs; |
| 295 | int tp_frees; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 296 | int tp_maxalloc; |
| 297 | struct _typeobject *tp_next; |
| 298 | #endif |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 299 | } PyTypeObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 300 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 301 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 302 | /* Generic type check */ |
| 303 | extern DL_IMPORT(int) PyType_IsSubtype(PyTypeObject *, PyTypeObject *); |
| 304 | #define PyObject_TypeCheck(ob, tp) \ |
| 305 | ((ob)->ob_type == (tp) || PyType_IsSubtype((ob)->ob_type, (tp))) |
| 306 | |
Guido van Rossum | 609c7c8 | 2001-08-24 16:51:42 +0000 | [diff] [blame] | 307 | extern DL_IMPORT(PyTypeObject) PyType_Type; /* built-in 'type' */ |
| 308 | extern DL_IMPORT(PyTypeObject) PyBaseObject_Type; /* built-in 'object' */ |
| 309 | extern DL_IMPORT(PyTypeObject) PySuper_Type; /* built-in 'super' */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 310 | |
| 311 | #define PyType_Check(op) PyObject_TypeCheck(op, &PyType_Type) |
| 312 | |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 313 | extern DL_IMPORT(int) PyType_Ready(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 314 | extern DL_IMPORT(PyObject *) PyType_GenericAlloc(PyTypeObject *, int); |
| 315 | extern DL_IMPORT(PyObject *) PyType_GenericNew(PyTypeObject *, |
| 316 | PyObject *, PyObject *); |
| 317 | extern DL_IMPORT(PyObject *) _PyType_Lookup(PyTypeObject *, PyObject *); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 319 | /* Generic operations on objects */ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 320 | extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int); |
Barry Warsaw | 10418eb | 2001-01-24 04:16:59 +0000 | [diff] [blame] | 321 | extern DL_IMPORT(void) _PyObject_Dump(PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 322 | extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *); |
| 323 | extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 324 | #ifdef Py_USING_UNICODE |
Marc-André Lemburg | ad7c98e | 2001-01-17 17:09:53 +0000 | [diff] [blame] | 325 | extern DL_IMPORT(PyObject *) PyObject_Unicode(PyObject *); |
Martin v. Löwis | 339d0f7 | 2001-08-17 18:39:25 +0000 | [diff] [blame] | 326 | #endif |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 327 | extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *); |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 328 | extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); |
| 329 | extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 330 | extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *); |
| 331 | extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *); |
| 332 | extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *); |
| 333 | extern DL_IMPORT(PyObject *) PyObject_GetAttr(PyObject *, PyObject *); |
| 334 | extern DL_IMPORT(int) PyObject_SetAttr(PyObject *, PyObject *, PyObject *); |
| 335 | extern DL_IMPORT(int) PyObject_HasAttr(PyObject *, PyObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 336 | extern DL_IMPORT(PyObject **) _PyObject_GetDictPtr(PyObject *); |
| 337 | extern DL_IMPORT(PyObject *) PyObject_GenericGetAttr(PyObject *, PyObject *); |
| 338 | extern DL_IMPORT(int) PyObject_GenericSetAttr(PyObject *, |
| 339 | PyObject *, PyObject *); |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 340 | extern DL_IMPORT(long) PyObject_Hash(PyObject *); |
| 341 | extern DL_IMPORT(int) PyObject_IsTrue(PyObject *); |
| 342 | extern DL_IMPORT(int) PyObject_Not(PyObject *); |
| 343 | extern DL_IMPORT(int) PyCallable_Check(PyObject *); |
| 344 | extern DL_IMPORT(int) PyNumber_Coerce(PyObject **, PyObject **); |
| 345 | extern DL_IMPORT(int) PyNumber_CoerceEx(PyObject **, PyObject **); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 346 | |
Fred Drake | b60654b | 2001-02-26 18:56:37 +0000 | [diff] [blame] | 347 | extern DL_IMPORT(void) (*PyObject_ClearWeakRefs)(PyObject *); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 348 | |
Tim Peters | 7eea37e | 2001-09-04 22:08:56 +0000 | [diff] [blame] | 349 | /* PyObject_Dir(obj) acts like Python __builtin__.dir(obj), returning a |
| 350 | list of strings. PyObject_Dir(NULL) is like __builtin__.dir(), |
| 351 | returning the names of the current locals. In this case, if there are |
| 352 | no current locals, NULL is returned, and PyErr_Occurred() is false. |
| 353 | */ |
| 354 | extern DL_IMPORT(PyObject *) PyObject_Dir(PyObject *); |
| 355 | |
| 356 | |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 357 | /* Helpers for printing recursive container types */ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 358 | extern DL_IMPORT(int) Py_ReprEnter(PyObject *); |
| 359 | extern DL_IMPORT(void) Py_ReprLeave(PyObject *); |
Guido van Rossum | 26d4ac3 | 1998-04-10 22:32:24 +0000 | [diff] [blame] | 360 | |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 361 | /* Helpers for hash functions */ |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 362 | extern DL_IMPORT(long) _Py_HashDouble(double); |
| 363 | extern DL_IMPORT(long) _Py_HashPointer(void*); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 364 | |
Jeremy Hylton | 483638c | 2001-02-01 20:20:45 +0000 | [diff] [blame] | 365 | /* Helper for passing objects to printf and the like */ |
| 366 | #define PyObject_REPR(obj) PyString_AS_STRING(PyObject_Repr(obj)) |
| 367 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 368 | /* Flag bits for printing: */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 369 | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 370 | |
| 371 | /* |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 372 | |
| 373 | Type flags (tp_flags) |
| 374 | |
| 375 | These flags are used to extend the type structure in a backwards-compatible |
| 376 | fashion. Extensions can use the flags to indicate (and test) when a given |
| 377 | type structure contains a new feature. The Python core will use these when |
| 378 | introducing new functionality between major revisions (to avoid mid-version |
| 379 | changes in the PYTHON_API_VERSION). |
| 380 | |
| 381 | Arbitration of the flag bit positions will need to be coordinated among |
| 382 | all extension writers who publically release their extensions (this will |
| 383 | be fewer than you might expect!).. |
| 384 | |
| 385 | Python 1.5.2 introduced the bf_getcharbuffer slot into PyBufferProcs. |
| 386 | |
| 387 | Type definitions should use Py_TPFLAGS_DEFAULT for their tp_flags value. |
| 388 | |
| 389 | Code can use PyType_HasFeature(type_ob, flag_value) to test whether the |
| 390 | given type object has a specified feature. |
| 391 | |
| 392 | */ |
| 393 | |
| 394 | /* PyBufferProcs contains bf_getcharbuffer */ |
| 395 | #define Py_TPFLAGS_HAVE_GETCHARBUFFER (1L<<0) |
| 396 | |
Guido van Rossum | cecb27a | 2000-02-28 15:00:40 +0000 | [diff] [blame] | 397 | /* PySequenceMethods contains sq_contains */ |
| 398 | #define Py_TPFLAGS_HAVE_SEQUENCE_IN (1L<<1) |
| 399 | |
Neil Schemenauer | 31ec142 | 2001-08-29 23:46:35 +0000 | [diff] [blame] | 400 | /* This is here for backwards compatibility. Extensions that use the old GC |
| 401 | * API will still compile but the objects will not be tracked by the GC. */ |
| 402 | #define Py_TPFLAGS_GC 0 /* used to be (1L<<2) */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 403 | |
Thomas Wouters | dd8dbdb | 2000-08-24 20:09:45 +0000 | [diff] [blame] | 404 | /* PySequenceMethods and PyNumberMethods contain in-place operators */ |
| 405 | #define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3) |
| 406 | |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 407 | /* PyNumberMethods do their own coercion */ |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 408 | #define Py_TPFLAGS_CHECKTYPES (1L<<4) |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 410 | /* tp_richcompare is defined */ |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 411 | #define Py_TPFLAGS_HAVE_RICHCOMPARE (1L<<5) |
| 412 | |
Fred Drake | 033f312 | 2001-02-02 18:17:30 +0000 | [diff] [blame] | 413 | /* Objects which are weakly referencable if their tp_weaklistoffset is >0 */ |
Fred Drake | 033f312 | 2001-02-02 18:17:30 +0000 | [diff] [blame] | 414 | #define Py_TPFLAGS_HAVE_WEAKREFS (1L<<6) |
| 415 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 416 | /* tp_iter is defined */ |
| 417 | #define Py_TPFLAGS_HAVE_ITER (1L<<7) |
| 418 | |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 419 | /* New members introduced by Python 2.2 exist */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 420 | #define Py_TPFLAGS_HAVE_CLASS (1L<<8) |
| 421 | |
| 422 | /* Set if the type object is dynamically allocated */ |
| 423 | #define Py_TPFLAGS_HEAPTYPE (1L<<9) |
| 424 | |
| 425 | /* Set if the type allows subclassing */ |
| 426 | #define Py_TPFLAGS_BASETYPE (1L<<10) |
| 427 | |
| 428 | /* Set if the type's __dict__ may change */ |
| 429 | #define Py_TPFLAGS_DYNAMICTYPE (1L<<11) |
| 430 | |
Guido van Rossum | 9b9c972 | 2001-08-10 17:37:02 +0000 | [diff] [blame] | 431 | /* Set if the type is 'ready' -- fully initialized */ |
| 432 | #define Py_TPFLAGS_READY (1L<<12) |
| 433 | |
| 434 | /* Set while the type is being 'readied', to prevent recursive ready calls */ |
| 435 | #define Py_TPFLAGS_READYING (1L<<13) |
| 436 | |
Neil Schemenauer | 31ec142 | 2001-08-29 23:46:35 +0000 | [diff] [blame] | 437 | /* Objects support garbage collection (see objimp.h) */ |
| 438 | #ifdef WITH_CYCLE_GC |
| 439 | #define Py_TPFLAGS_HAVE_GC (1L<<14) |
| 440 | #else |
| 441 | #define Py_TPFLAGS_HAVE_GC 0 |
| 442 | #endif |
| 443 | |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 444 | #define Py_TPFLAGS_DEFAULT ( \ |
| 445 | Py_TPFLAGS_HAVE_GETCHARBUFFER | \ |
Thomas Wouters | dd8dbdb | 2000-08-24 20:09:45 +0000 | [diff] [blame] | 446 | Py_TPFLAGS_HAVE_SEQUENCE_IN | \ |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 447 | Py_TPFLAGS_HAVE_INPLACEOPS | \ |
| 448 | Py_TPFLAGS_HAVE_RICHCOMPARE | \ |
Fred Drake | 033f312 | 2001-02-02 18:17:30 +0000 | [diff] [blame] | 449 | Py_TPFLAGS_HAVE_WEAKREFS | \ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 450 | Py_TPFLAGS_HAVE_ITER | \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 451 | Py_TPFLAGS_HAVE_CLASS | \ |
Guido van Rossum | bacca54 | 2001-01-24 22:13:48 +0000 | [diff] [blame] | 452 | 0) |
Guido van Rossum | 36eef3c | 1998-10-08 02:10:56 +0000 | [diff] [blame] | 453 | |
| 454 | #define PyType_HasFeature(t,f) (((t)->tp_flags & (f)) != 0) |
| 455 | |
| 456 | |
| 457 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 458 | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement |
| 459 | reference counts. Py_DECREF calls the object's deallocator function; for |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 460 | objects that don't contain references to other objects or heap memory |
| 461 | this can be the standard function free(). Both macros can be used |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 462 | wherever a void expression is allowed. The argument shouldn't be a |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 463 | NIL pointer. The macro _Py_NewReference(op) is used only to initialize |
| 464 | reference counts to 1; it is defined here for convenience. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 465 | |
| 466 | We assume that the reference count field can never overflow; this can |
| 467 | be proven when the size of the field is the same as the pointer size |
| 468 | but even with a 16-bit reference count field it is pretty unlikely so |
| 469 | we ignore the possibility. (If you are paranoid, make it a long.) |
| 470 | |
| 471 | Type objects should never be deallocated; the type pointer in an object |
| 472 | is not considered to be a reference to the type object, to save |
| 473 | complications in the deallocation function. (This is actually a |
| 474 | decision that's up to the implementer of each new type so if you want, |
| 475 | you can count such references to the type object.) |
| 476 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 477 | *** WARNING*** The Py_DECREF macro must have a side-effect-free argument |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 478 | since it may evaluate its argument multiple times. (The alternative |
| 479 | would be to mace it a proper function or assign it to a global temporary |
| 480 | variable first, both of which are slower; and in a multi-threaded |
| 481 | environment the global variable trick is not safe.) |
| 482 | */ |
| 483 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 484 | #ifdef Py_TRACE_REFS |
| 485 | #ifndef Py_REF_DEBUG |
| 486 | #define Py_REF_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 487 | #endif |
| 488 | #endif |
| 489 | |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 490 | #ifdef Py_TRACE_REFS |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 491 | extern DL_IMPORT(void) _Py_Dealloc(PyObject *); |
| 492 | extern DL_IMPORT(void) _Py_NewReference(PyObject *); |
| 493 | extern DL_IMPORT(void) _Py_ForgetReference(PyObject *); |
| 494 | extern DL_IMPORT(void) _Py_PrintReferences(FILE *); |
| 495 | extern DL_IMPORT(void) _Py_ResetReferences(void); |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 496 | #endif |
| 497 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 498 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 499 | #ifdef COUNT_ALLOCS |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 500 | #define _Py_Dealloc(op) ((op)->ob_type->tp_frees++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op))) |
| 501 | #define _Py_ForgetReference(op) ((op)->ob_type->tp_frees++) |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 502 | #else /* !COUNT_ALLOCS */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 503 | #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op)) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 504 | #define _Py_ForgetReference(op) /*empty*/ |
Guido van Rossum | d86b380 | 1996-08-12 21:31:32 +0000 | [diff] [blame] | 505 | #endif /* !COUNT_ALLOCS */ |
| 506 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 507 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 508 | #ifdef COUNT_ALLOCS |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 509 | extern DL_IMPORT(void) inc_count(PyTypeObject *); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 510 | #endif |
| 511 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 512 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 513 | |
Guido van Rossum | 43466ec | 1998-12-04 18:48:25 +0000 | [diff] [blame] | 514 | extern DL_IMPORT(long) _Py_RefTotal; |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 515 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 516 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 517 | #ifdef COUNT_ALLOCS |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 518 | #define _Py_NewReference(op) (inc_count((op)->ob_type), _Py_RefTotal++, (op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 519 | #else |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 520 | #define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 521 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 522 | #endif /* !Py_TRACE_REFS */ |
| 523 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 524 | #define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++) |
Martin v. Löwis | 64fbb33 | 2001-08-05 21:23:03 +0000 | [diff] [blame] | 525 | /* under Py_REF_DEBUG: also log negative ref counts after Py_DECREF() !! */ |
| 526 | #define Py_DECREF(op) \ |
| 527 | if (--_Py_RefTotal, 0 < (--((op)->ob_refcnt))) ; \ |
| 528 | else if (0 == (op)->ob_refcnt) _Py_Dealloc( (PyObject*)(op)); \ |
| 529 | else (void)fprintf( stderr, "%s:%i negative ref count %i\n", \ |
| 530 | __FILE__, __LINE__, (op)->ob_refcnt) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 531 | #else /* !Py_REF_DEBUG */ |
| 532 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 533 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 534 | #define _Py_NewReference(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 535 | #else |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 536 | #define _Py_NewReference(op) ((op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 537 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 538 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 539 | #define Py_INCREF(op) ((op)->ob_refcnt++) |
| 540 | #define Py_DECREF(op) \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 541 | if (--(op)->ob_refcnt != 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 542 | ; \ |
| 543 | else \ |
Guido van Rossum | 1d529d1 | 1997-08-05 02:30:44 +0000 | [diff] [blame] | 544 | _Py_Dealloc((PyObject *)(op)) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 545 | #endif /* !Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 546 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 547 | /* Macros to use in case the object pointer may be NULL: */ |
| 548 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 549 | #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op) |
| 550 | #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 552 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 553 | _Py_NoneStruct is an object of undefined type which can be used in contexts |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 554 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 555 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 556 | Don't forget to apply Py_INCREF() when returning this value!!! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 557 | */ |
| 558 | |
Sjoerd Mullender | 107c747 | 1995-04-25 11:53:24 +0000 | [diff] [blame] | 559 | extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 560 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 561 | #define Py_None (&_Py_NoneStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 562 | |
Neil Schemenauer | a7ed694 | 2001-01-04 01:31:50 +0000 | [diff] [blame] | 563 | /* |
| 564 | Py_NotImplemented is a singleton used to signal that an operation is |
| 565 | not implemented for a given type combination. |
| 566 | */ |
| 567 | |
| 568 | extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */ |
| 569 | |
| 570 | #define Py_NotImplemented (&_Py_NotImplementedStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 571 | |
Guido van Rossum | 5f284ce | 2001-01-17 15:20:39 +0000 | [diff] [blame] | 572 | /* Rich comparison opcodes */ |
| 573 | #define Py_LT 0 |
| 574 | #define Py_LE 1 |
| 575 | #define Py_EQ 2 |
| 576 | #define Py_NE 3 |
| 577 | #define Py_GT 4 |
| 578 | #define Py_GE 5 |
| 579 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 580 | /* |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 581 | A common programming style in Python requires the forward declaration |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 582 | of static, initialized structures, e.g. for a type object that is used |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 583 | by the functions whose address must be used in the initializer. |
| 584 | Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as |
| 585 | well) botch this if you use the static keyword for both declarations |
| 586 | (they allocate two objects, and use the first, uninitialized one until |
| 587 | the second declaration is encountered). Therefore, the forward |
| 588 | declaration should use the 'forwardstatic' keyword. This expands to |
| 589 | static on most systems, but to extern on a few. The actual storage |
| 590 | and name will still be static because the second declaration is |
| 591 | static, so no linker visible symbols will be generated. (Standard C |
| 592 | compilers take offense to the extern forward declaration of a static |
| 593 | object, so I can't just put extern in all cases. :-( ) |
| 594 | */ |
| 595 | |
| 596 | #ifdef BAD_STATIC_FORWARD |
| 597 | #define staticforward extern |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 598 | #define statichere static |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 599 | #else /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 600 | #define staticforward static |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 601 | #define statichere static |
| 602 | #endif /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 603 | |
| 604 | |
| 605 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 606 | More conventions |
| 607 | ================ |
| 608 | |
| 609 | Argument Checking |
| 610 | ----------------- |
| 611 | |
| 612 | Functions that take objects as arguments normally don't check for nil |
| 613 | arguments, but they do check the type of the argument, and return an |
| 614 | error if the function doesn't apply to the type. |
| 615 | |
| 616 | Failure Modes |
| 617 | ------------- |
| 618 | |
| 619 | Functions may fail for a variety of reasons, including running out of |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 620 | memory. This is communicated to the caller in two ways: an error string |
| 621 | is set (see errors.h), and the function result differs: functions that |
| 622 | normally return a pointer return NULL for failure, functions returning |
| 623 | an integer return -1 (which could be a legal return value too!), and |
| 624 | other functions return 0 for success and -1 for failure. |
| 625 | Callers should always check for errors before using the result. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 626 | |
| 627 | Reference Counts |
| 628 | ---------------- |
| 629 | |
| 630 | It takes a while to get used to the proper usage of reference counts. |
| 631 | |
| 632 | Functions that create an object set the reference count to 1; such new |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 633 | objects must be stored somewhere or destroyed again with Py_DECREF(). |
| 634 | Functions that 'store' objects such as PyTuple_SetItem() and |
| 635 | PyDict_SetItemString() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 636 | don't increment the reference count of the object, since the most |
| 637 | frequent use is to store a fresh object. Functions that 'retrieve' |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 638 | objects such as PyTuple_GetItem() and PyDict_GetItemString() also |
| 639 | don't increment |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 640 | the reference count, since most frequently the object is only looked at |
| 641 | quickly. Thus, to retrieve an object and store it again, the caller |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 642 | must call Py_INCREF() explicitly. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 643 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 644 | NOTE: functions that 'consume' a reference count like |
Fred Drake | 49bb0e3 | 1997-09-05 17:53:53 +0000 | [diff] [blame] | 645 | PyList_SetItemString() even consume the reference if the object wasn't |
| 646 | stored, to simplify error handling. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 647 | |
| 648 | It seems attractive to make other functions that take an object as |
| 649 | argument consume a reference count; however this may quickly get |
| 650 | confusing (even the current practice is already confusing). Consider |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 651 | it carefully, it may save lots of calls to Py_INCREF() and Py_DECREF() at |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 652 | times. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 653 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 654 | |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 655 | /* |
| 656 | trashcan |
| 657 | CT 2k0130 |
| 658 | non-recursively destroy nested objects |
| 659 | |
| 660 | CT 2k0223 |
| 661 | redefinition for better locality and less overhead. |
| 662 | |
| 663 | Objects that want to be recursion safe need to use |
Thomas Wouters | 7e47402 | 2000-07-16 12:04:32 +0000 | [diff] [blame] | 664 | the macro's |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 665 | Py_TRASHCAN_SAFE_BEGIN(name) |
| 666 | and |
| 667 | Py_TRASHCAN_SAFE_END(name) |
| 668 | surrounding their actual deallocation code. |
| 669 | |
| 670 | It would be nice to do this using the thread state. |
| 671 | Also, we could do an exact stack measure then. |
| 672 | Unfortunately, deallocations also take place when |
| 673 | the thread state is undefined. |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 674 | |
| 675 | CT 2k0422 complete rewrite. |
| 676 | There is no need to allocate new objects. |
| 677 | Everything is done vialob_refcnt and ob_type now. |
| 678 | Adding support for free-threading should be easy, too. |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 679 | */ |
| 680 | |
| 681 | #define PyTrash_UNWIND_LEVEL 50 |
| 682 | |
| 683 | #define Py_TRASHCAN_SAFE_BEGIN(op) \ |
| 684 | { \ |
| 685 | ++_PyTrash_delete_nesting; \ |
| 686 | if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL) { \ |
| 687 | |
| 688 | #define Py_TRASHCAN_SAFE_END(op) \ |
| 689 | ;} \ |
| 690 | else \ |
| 691 | _PyTrash_deposit_object((PyObject*)op);\ |
| 692 | --_PyTrash_delete_nesting; \ |
| 693 | if (_PyTrash_delete_later && _PyTrash_delete_nesting <= 0) \ |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 694 | _PyTrash_destroy_chain(); \ |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 695 | } \ |
| 696 | |
Tim Peters | 9ace6bc | 2000-07-08 00:32:04 +0000 | [diff] [blame] | 697 | extern DL_IMPORT(void) _PyTrash_deposit_object(PyObject*); |
Greg Stein | a90b23c | 2000-07-08 00:46:19 +0000 | [diff] [blame] | 698 | extern DL_IMPORT(void) _PyTrash_destroy_chain(void); |
Guido van Rossum | d724b23 | 2000-03-13 16:01:29 +0000 | [diff] [blame] | 699 | |
| 700 | extern DL_IMPORT(int) _PyTrash_delete_nesting; |
| 701 | extern DL_IMPORT(PyObject *) _PyTrash_delete_later; |
| 702 | |
| 703 | /* swap the "xx" to check the speed loss */ |
| 704 | |
| 705 | #define xxPy_TRASHCAN_SAFE_BEGIN(op) |
| 706 | #define xxPy_TRASHCAN_SAFE_END(op) ; |
Guido van Rossum | e92e610 | 2000-04-24 15:40:53 +0000 | [diff] [blame] | 707 | |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 708 | #ifdef __cplusplus |
| 709 | } |
| 710 | #endif |
| 711 | #endif /* !Py_OBJECT_H */ |