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 | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 8 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 9 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 10 | |
| 11 | All Rights Reserved |
| 12 | |
| 13 | Permission to use, copy, modify, and distribute this software and its |
| 14 | documentation for any purpose and without fee is hereby granted, |
| 15 | provided that the above copyright notice appear in all copies and that |
| 16 | both that copyright notice and this permission notice appear in |
| 17 | supporting documentation, and that the names of Stichting Mathematisch |
| 18 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 19 | distribution of the software without specific, written prior permission. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 22 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 23 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 24 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 25 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 26 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 27 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 28 | |
| 29 | ******************************************************************/ |
| 30 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | /* Object and type object interface */ |
| 32 | |
| 33 | /* |
| 34 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 35 | |
| 36 | Objects are structures allocated on the heap. Special rules apply to |
| 37 | the use of objects to ensure they are properly garbage-collected. |
| 38 | Objects are never allocated statically or on the stack; they must be |
| 39 | accessed through special macros and functions only. (Type objects are |
| 40 | exceptions to the first rule; the standard types are represented by |
| 41 | statically initialized type objects.) |
| 42 | |
| 43 | An object has a 'reference count' that is increased or decreased when a |
| 44 | pointer to the object is copied or deleted; when the reference count |
| 45 | reaches zero there are no references to the object left and it can be |
| 46 | removed from the heap. |
| 47 | |
| 48 | An object has a 'type' that determines what it represents and what kind |
| 49 | of data it contains. An object's type is fixed when it is created. |
| 50 | Types themselves are represented as objects; an object contains a |
| 51 | pointer to the corresponding type object. The type itself has a type |
| 52 | pointer pointing to the object representing the type 'type', which |
| 53 | contains a pointer to itself!). |
| 54 | |
| 55 | Objects do not float around in memory; once allocated an object keeps |
| 56 | the same size and address. Objects that must hold variable-size data |
| 57 | can contain pointers to variable-size parts of the object. Not all |
| 58 | objects of the same type have the same size; but the size cannot change |
| 59 | after allocation. (These restrictions are made so a reference to an |
| 60 | object can be simply a pointer -- moving an object would require |
| 61 | updating all the pointers, and changing an object's size would require |
| 62 | moving it if there was another object right next to it.) |
| 63 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 64 | Objects are always accessed through pointers of the type 'PyObject *'. |
| 65 | 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] | 66 | and the type pointer. The actual memory allocated for an object |
| 67 | contains other data that can only be accessed after casting the pointer |
| 68 | 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] | 69 | with the reference count and type fields; the macro PyObject_HEAD should be |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 70 | used for this (to accomodate for future changes). The implementation |
| 71 | of a particular object type can cast the object pointer to the proper |
| 72 | type and back. |
| 73 | |
| 74 | A standard interface exists for objects that contain an array of items |
| 75 | whose size is determined when the object is allocated. |
| 76 | |
| 77 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 78 | */ |
| 79 | |
Guido van Rossum | e1cd6c1 | 1996-05-24 20:43:12 +0000 | [diff] [blame] | 80 | #ifdef DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 81 | |
| 82 | /* Turn on heavy reference debugging */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 83 | #define Py_TRACE_REFS |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | |
| 85 | /* Turn on reference counting */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 86 | #define Py_REF_DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 87 | |
Guido van Rossum | e1cd6c1 | 1996-05-24 20:43:12 +0000 | [diff] [blame] | 88 | #endif /* DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 90 | #ifdef Py_TRACE_REFS |
| 91 | #define PyObject_HEAD \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | struct _object *_ob_next, *_ob_prev; \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 93 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 95 | #define PyObject_HEAD_INIT(type) 0, 0, 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 96 | #else /* !Py_TRACE_REFS */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 97 | #define PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 98 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 99 | struct _typeobject *ob_type; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 100 | #define PyObject_HEAD_INIT(type) 1, type, |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 101 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 103 | #define PyObject_VAR_HEAD \ |
| 104 | PyObject_HEAD \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 105 | int ob_size; /* Number of items in variable part */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 106 | |
| 107 | typedef struct _object { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 108 | PyObject_HEAD |
| 109 | } PyObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | |
| 111 | typedef struct { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 112 | PyObject_VAR_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | } varobject; |
| 114 | |
| 115 | |
| 116 | /* |
| 117 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 118 | |
| 119 | Type objects contain a string containing the type name (to help somewhat |
| 120 | in debugging), the allocation parameters (see newobj() and newvarobj()), |
| 121 | and methods for accessing objects of the type. Methods are optional,a |
| 122 | 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] | 123 | 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] | 124 | checking for a nil pointer; it should always be implemented except if |
| 125 | the implementation can guarantee that the reference count will never |
| 126 | reach zero (e.g., for type objects). |
| 127 | |
| 128 | NB: the methods for certain type groups are now contained in separate |
| 129 | method blocks. |
| 130 | */ |
| 131 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 132 | typedef PyObject * (*unaryfunc) Py_PROTO((PyObject *)); |
| 133 | typedef PyObject * (*binaryfunc) Py_PROTO((PyObject *, PyObject *)); |
| 134 | typedef PyObject * (*ternaryfunc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
| 135 | typedef int (*inquiry) Py_PROTO((PyObject *)); |
| 136 | typedef int (*coercion) Py_PROTO((PyObject **, PyObject **)); |
| 137 | typedef PyObject *(*intargfunc) Py_PROTO((PyObject *, int)); |
| 138 | typedef PyObject *(*intintargfunc) Py_PROTO((PyObject *, int, int)); |
| 139 | typedef int(*intobjargproc) Py_PROTO((PyObject *, int, PyObject *)); |
| 140 | typedef int(*intintobjargproc) Py_PROTO((PyObject *, int, int, PyObject *)); |
| 141 | typedef int(*objobjargproc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 142 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 143 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 144 | binaryfunc nb_add; |
| 145 | binaryfunc nb_subtract; |
| 146 | binaryfunc nb_multiply; |
| 147 | binaryfunc nb_divide; |
| 148 | binaryfunc nb_remainder; |
| 149 | binaryfunc nb_divmod; |
Guido van Rossum | 75abc63 | 1994-08-09 13:21:54 +0000 | [diff] [blame] | 150 | ternaryfunc nb_power; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 151 | unaryfunc nb_negative; |
| 152 | unaryfunc nb_positive; |
| 153 | unaryfunc nb_absolute; |
| 154 | inquiry nb_nonzero; |
| 155 | unaryfunc nb_invert; |
| 156 | binaryfunc nb_lshift; |
| 157 | binaryfunc nb_rshift; |
| 158 | binaryfunc nb_and; |
| 159 | binaryfunc nb_xor; |
| 160 | binaryfunc nb_or; |
| 161 | coercion nb_coerce; |
| 162 | unaryfunc nb_int; |
| 163 | unaryfunc nb_long; |
| 164 | unaryfunc nb_float; |
| 165 | unaryfunc nb_oct; |
| 166 | unaryfunc nb_hex; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 167 | } PyNumberMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 168 | |
| 169 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 170 | inquiry sq_length; |
| 171 | binaryfunc sq_concat; |
| 172 | intargfunc sq_repeat; |
| 173 | intargfunc sq_item; |
| 174 | intintargfunc sq_slice; |
| 175 | intobjargproc sq_ass_item; |
| 176 | intintobjargproc sq_ass_slice; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 177 | } PySequenceMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 178 | |
| 179 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 180 | inquiry mp_length; |
| 181 | binaryfunc mp_subscript; |
| 182 | objobjargproc mp_ass_subscript; |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 183 | } PyMappingMethods; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 184 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 185 | typedef void (*destructor) Py_PROTO((PyObject *)); |
| 186 | typedef int (*printfunc) Py_PROTO((PyObject *, FILE *, int)); |
| 187 | typedef PyObject *(*getattrfunc) Py_PROTO((PyObject *, char *)); |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame^] | 188 | typedef PyObject *(*getattrofunc) Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 189 | typedef int (*setattrfunc) Py_PROTO((PyObject *, char *, PyObject *)); |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame^] | 190 | typedef int (*setattrofunc) Py_PROTO((PyObject *, PyObject *, PyObject *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 191 | typedef int (*cmpfunc) Py_PROTO((PyObject *, PyObject *)); |
| 192 | typedef PyObject *(*reprfunc) Py_PROTO((PyObject *)); |
| 193 | typedef long (*hashfunc) Py_PROTO((PyObject *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 194 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 195 | typedef struct _typeobject { |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 196 | PyObject_VAR_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | char *tp_name; /* For printing */ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 198 | int tp_basicsize, tp_itemsize; /* For allocation */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 199 | |
| 200 | /* Methods to implement standard operations */ |
| 201 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 202 | destructor tp_dealloc; |
| 203 | printfunc tp_print; |
| 204 | getattrfunc tp_getattr; |
| 205 | setattrfunc tp_setattr; |
| 206 | cmpfunc tp_compare; |
| 207 | reprfunc tp_repr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | |
| 209 | /* Method suites for standard classes */ |
| 210 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 211 | PyNumberMethods *tp_as_number; |
| 212 | PySequenceMethods *tp_as_sequence; |
| 213 | PyMappingMethods *tp_as_mapping; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 214 | |
| 215 | /* More standard operations (at end for binary compatibility) */ |
| 216 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 217 | hashfunc tp_hash; |
Guido van Rossum | 884afd6 | 1995-07-18 14:21:06 +0000 | [diff] [blame] | 218 | ternaryfunc tp_call; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 219 | reprfunc tp_str; |
Guido van Rossum | 0693dd2 | 1996-08-09 20:48:52 +0000 | [diff] [blame^] | 220 | getattrofunc tp_getattro; |
| 221 | setattrofunc tp_setattro; |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 222 | |
| 223 | /* Space for future expansion */ |
Guido van Rossum | 6fde390 | 1995-01-07 10:32:04 +0000 | [diff] [blame] | 224 | long tp_xxx3; |
| 225 | long tp_xxx4; |
| 226 | |
| 227 | char *tp_doc; /* Documentation string */ |
| 228 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 229 | #ifdef COUNT_ALLOCS |
| 230 | /* these must be last */ |
| 231 | int tp_alloc; |
| 232 | int tp_free; |
| 233 | int tp_maxalloc; |
| 234 | struct _typeobject *tp_next; |
| 235 | #endif |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 236 | } PyTypeObject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | |
Sjoerd Mullender | 107c747 | 1995-04-25 11:53:24 +0000 | [diff] [blame] | 238 | extern DL_IMPORT(PyTypeObject) PyType_Type; /* The type of type objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 239 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 240 | #define PyType_Check(op) ((op)->ob_type == &PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 241 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 242 | /* Generic operations on objects */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 243 | extern int PyObject_Print Py_PROTO((PyObject *, FILE *, int)); |
| 244 | extern PyObject * PyObject_Repr Py_PROTO((PyObject *)); |
Guido van Rossum | 9381782 | 1995-01-17 16:01:01 +0000 | [diff] [blame] | 245 | extern PyObject * PyObject_Str Py_PROTO((PyObject *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 246 | extern int PyObject_Compare Py_PROTO((PyObject *, PyObject *)); |
| 247 | extern PyObject *PyObject_GetAttrString Py_PROTO((PyObject *, char *)); |
Guido van Rossum | 454674d | 1995-07-26 17:53:29 +0000 | [diff] [blame] | 248 | extern int PyObject_SetAttrString Py_PROTO((PyObject *, char *, PyObject *)); |
Guido van Rossum | 9381782 | 1995-01-17 16:01:01 +0000 | [diff] [blame] | 249 | extern int PyObject_HasAttrString Py_PROTO((PyObject *, char *)); |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 250 | extern PyObject *PyObject_GetAttr Py_PROTO((PyObject *, PyObject *)); |
| 251 | extern int PyObject_SetAttr Py_PROTO((PyObject *, PyObject *, PyObject *)); |
| 252 | extern long PyObject_Hash Py_PROTO((PyObject *)); |
Guido van Rossum | b13afdd | 1995-02-17 15:01:21 +0000 | [diff] [blame] | 253 | extern int PyObject_IsTrue Py_PROTO((PyObject *)); |
| 254 | extern int PyCallable_Check Py_PROTO((PyObject *)); |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 255 | extern int PyNumber_Coerce Py_PROTO((PyObject **, PyObject **)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 256 | |
| 257 | /* Flag bits for printing: */ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 258 | #define Py_PRINT_RAW 1 /* No string quotes etc. */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 259 | |
| 260 | /* |
| 261 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 262 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 263 | The macros Py_INCREF(op) and Py_DECREF(op) are used to increment or decrement |
| 264 | reference counts. Py_DECREF calls the object's deallocator function; for |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | objects that don't contain references to other objects or heap memory |
| 266 | this can be the standard function free(). Both macros can be used |
| 267 | whereever a void expression is allowed. The argument shouldn't be a |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 268 | NIL pointer. The macro _Py_NewReference(op) is used only to initialize |
| 269 | reference counts to 1; it is defined here for convenience. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 270 | |
| 271 | We assume that the reference count field can never overflow; this can |
| 272 | be proven when the size of the field is the same as the pointer size |
| 273 | but even with a 16-bit reference count field it is pretty unlikely so |
| 274 | we ignore the possibility. (If you are paranoid, make it a long.) |
| 275 | |
| 276 | Type objects should never be deallocated; the type pointer in an object |
| 277 | is not considered to be a reference to the type object, to save |
| 278 | complications in the deallocation function. (This is actually a |
| 279 | decision that's up to the implementer of each new type so if you want, |
| 280 | you can count such references to the type object.) |
| 281 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 282 | *** 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] | 283 | since it may evaluate its argument multiple times. (The alternative |
| 284 | would be to mace it a proper function or assign it to a global temporary |
| 285 | variable first, both of which are slower; and in a multi-threaded |
| 286 | environment the global variable trick is not safe.) |
| 287 | */ |
| 288 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 289 | #ifdef Py_TRACE_REFS |
| 290 | #ifndef Py_REF_DEBUG |
| 291 | #define Py_REF_DEBUG |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 292 | #endif |
| 293 | #endif |
| 294 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 295 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 296 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 297 | #define _Py_Dealloc(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((PyObject *)(op))) |
Sjoerd Mullender | 91e7a0b | 1995-04-06 13:47:48 +0000 | [diff] [blame] | 298 | #define _Py_ForgetReference(op) ((op)->ob_type->tp_free++) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 299 | #else |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 300 | #define _Py_Dealloc(op) (*(op)->ob_type->tp_dealloc)((PyObject *)(op)) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 301 | #define _Py_ForgetReference(op) /*empty*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | #endif |
Sjoerd Mullender | 91e7a0b | 1995-04-06 13:47:48 +0000 | [diff] [blame] | 303 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 304 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 305 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 306 | extern void inc_count Py_PROTO((PyTypeObject *)); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 307 | #endif |
| 308 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 309 | #ifdef Py_REF_DEBUG |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 310 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 311 | extern long _Py_RefTotal; |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 313 | #ifndef Py_TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 314 | #ifdef COUNT_ALLOCS |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 315 | #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] | 316 | #else |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 317 | #define _Py_NewReference(op) (_Py_RefTotal++, (op)->ob_refcnt = 1) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 318 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 319 | #endif /* !Py_TRACE_REFS */ |
| 320 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 321 | #define Py_INCREF(op) (_Py_RefTotal++, (op)->ob_refcnt++) |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 322 | #define Py_DECREF(op) \ |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 323 | if (--_Py_RefTotal, --(op)->ob_refcnt != 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 324 | ; \ |
| 325 | else \ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 326 | _Py_Dealloc(op) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 327 | #else /* !Py_REF_DEBUG */ |
| 328 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 329 | #ifdef COUNT_ALLOCS |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 330 | #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] | 331 | #else |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 332 | #define _Py_NewReference(op) ((op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 333 | #endif |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 334 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 335 | #define Py_INCREF(op) ((op)->ob_refcnt++) |
| 336 | #define Py_DECREF(op) \ |
Guido van Rossum | 5799b52 | 1995-01-04 19:06:22 +0000 | [diff] [blame] | 337 | if (--(op)->ob_refcnt != 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 338 | ; \ |
| 339 | else \ |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 340 | _Py_Dealloc(op) |
Guido van Rossum | 60be1db | 1996-05-22 16:33:22 +0000 | [diff] [blame] | 341 | #endif /* !Py_REF_DEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 343 | /* Macros to use in case the object pointer may be NULL: */ |
| 344 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 345 | #define Py_XINCREF(op) if ((op) == NULL) ; else Py_INCREF(op) |
| 346 | #define Py_XDECREF(op) if ((op) == NULL) ; else Py_DECREF(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 347 | |
| 348 | /* Definition of NULL, so you don't have to include <stdio.h> */ |
| 349 | |
| 350 | #ifndef NULL |
| 351 | #define NULL 0 |
| 352 | #endif |
| 353 | |
| 354 | |
| 355 | /* |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 356 | _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] | 357 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 358 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 359 | Don't forget to apply Py_INCREF() when returning this value!!! |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 360 | */ |
| 361 | |
Sjoerd Mullender | 107c747 | 1995-04-25 11:53:24 +0000 | [diff] [blame] | 362 | extern DL_IMPORT(PyObject) _Py_NoneStruct; /* Don't use this directly */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 363 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 364 | #define Py_None (&_Py_NoneStruct) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 365 | |
| 366 | |
| 367 | /* |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 368 | A common programming style in Python requires the forward declaration |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 369 | 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] | 370 | by the functions whose address must be used in the initializer. |
| 371 | Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as |
| 372 | well) botch this if you use the static keyword for both declarations |
| 373 | (they allocate two objects, and use the first, uninitialized one until |
| 374 | the second declaration is encountered). Therefore, the forward |
| 375 | declaration should use the 'forwardstatic' keyword. This expands to |
| 376 | static on most systems, but to extern on a few. The actual storage |
| 377 | and name will still be static because the second declaration is |
| 378 | static, so no linker visible symbols will be generated. (Standard C |
| 379 | compilers take offense to the extern forward declaration of a static |
| 380 | object, so I can't just put extern in all cases. :-( ) |
| 381 | */ |
| 382 | |
| 383 | #ifdef BAD_STATIC_FORWARD |
| 384 | #define staticforward extern |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 385 | #ifdef __SC__ |
| 386 | #define statichere |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 387 | #else |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 388 | #define statichere static |
| 389 | #endif /* __SC__ */ |
| 390 | #else /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 391 | #define staticforward static |
Guido van Rossum | 57836fe | 1995-02-21 21:06:10 +0000 | [diff] [blame] | 392 | #define statichere static |
| 393 | #endif /* !BAD_STATIC_FORWARD */ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 394 | |
| 395 | |
| 396 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 397 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 398 | |
| 399 | More conventions |
| 400 | ================ |
| 401 | |
| 402 | Argument Checking |
| 403 | ----------------- |
| 404 | |
| 405 | Functions that take objects as arguments normally don't check for nil |
| 406 | arguments, but they do check the type of the argument, and return an |
| 407 | error if the function doesn't apply to the type. |
| 408 | |
| 409 | Failure Modes |
| 410 | ------------- |
| 411 | |
| 412 | 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] | 413 | memory. This is communicated to the caller in two ways: an error string |
| 414 | is set (see errors.h), and the function result differs: functions that |
| 415 | normally return a pointer return NULL for failure, functions returning |
| 416 | an integer return -1 (which could be a legal return value too!), and |
| 417 | other functions return 0 for success and -1 for failure. |
| 418 | Callers should always check for errors before using the result. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 419 | |
| 420 | Reference Counts |
| 421 | ---------------- |
| 422 | |
| 423 | It takes a while to get used to the proper usage of reference counts. |
| 424 | |
| 425 | 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] | 426 | objects must be stored somewhere or destroyed again with Py_DECREF(). |
| 427 | Functions that 'store' objects such as PyTuple_SetItem() and |
| 428 | PyDict_SetItemString() |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 429 | don't increment the reference count of the object, since the most |
| 430 | frequent use is to store a fresh object. Functions that 'retrieve' |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 431 | objects such as PyTuple_GetItem() and PyDict_GetItemString() also |
| 432 | don't increment |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | the reference count, since most frequently the object is only looked at |
| 434 | 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] | 435 | must call Py_INCREF() explicitly. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 437 | NOTE: functions that 'consume' a reference count like |
| 438 | PyDict_SetItemString() even |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | consume the reference if the object wasn't stored, to simplify error |
| 440 | handling. |
| 441 | |
| 442 | It seems attractive to make other functions that take an object as |
| 443 | argument consume a reference count; however this may quickly get |
| 444 | confusing (even the current practice is already confusing). Consider |
Guido van Rossum | caa6380 | 1995-01-12 11:45:45 +0000 | [diff] [blame] | 445 | 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] | 446 | times. |
| 447 | |
| 448 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 449 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 450 | |
| 451 | #ifdef __cplusplus |
| 452 | } |
| 453 | #endif |
| 454 | #endif /* !Py_OBJECT_H */ |