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 | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 8 | Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 9 | Amsterdam, 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 | f2c8beb | 1992-09-03 20:34:07 +0000 | [diff] [blame] | 31 | #ifndef DEBUG |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 32 | #define NDEBUG |
Guido van Rossum | f2c8beb | 1992-09-03 20:34:07 +0000 | [diff] [blame] | 33 | #endif |
| 34 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | /* Object and type object interface */ |
| 36 | |
| 37 | /* |
| 38 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 39 | |
| 40 | Objects are structures allocated on the heap. Special rules apply to |
| 41 | the use of objects to ensure they are properly garbage-collected. |
| 42 | Objects are never allocated statically or on the stack; they must be |
| 43 | accessed through special macros and functions only. (Type objects are |
| 44 | exceptions to the first rule; the standard types are represented by |
| 45 | statically initialized type objects.) |
| 46 | |
| 47 | An object has a 'reference count' that is increased or decreased when a |
| 48 | pointer to the object is copied or deleted; when the reference count |
| 49 | reaches zero there are no references to the object left and it can be |
| 50 | removed from the heap. |
| 51 | |
| 52 | An object has a 'type' that determines what it represents and what kind |
| 53 | of data it contains. An object's type is fixed when it is created. |
| 54 | Types themselves are represented as objects; an object contains a |
| 55 | pointer to the corresponding type object. The type itself has a type |
| 56 | pointer pointing to the object representing the type 'type', which |
| 57 | contains a pointer to itself!). |
| 58 | |
| 59 | Objects do not float around in memory; once allocated an object keeps |
| 60 | the same size and address. Objects that must hold variable-size data |
| 61 | can contain pointers to variable-size parts of the object. Not all |
| 62 | objects of the same type have the same size; but the size cannot change |
| 63 | after allocation. (These restrictions are made so a reference to an |
| 64 | object can be simply a pointer -- moving an object would require |
| 65 | updating all the pointers, and changing an object's size would require |
| 66 | moving it if there was another object right next to it.) |
| 67 | |
| 68 | Objects are always accessed through pointers of the type 'object *'. |
| 69 | The type 'object' is a structure that only contains the reference count |
| 70 | and the type pointer. The actual memory allocated for an object |
| 71 | contains other data that can only be accessed after casting the pointer |
| 72 | to a pointer to a longer structure type. This longer type must start |
| 73 | with the reference count and type fields; the macro OB_HEAD should be |
| 74 | used for this (to accomodate for future changes). The implementation |
| 75 | of a particular object type can cast the object pointer to the proper |
| 76 | type and back. |
| 77 | |
| 78 | A standard interface exists for objects that contain an array of items |
| 79 | whose size is determined when the object is allocated. |
| 80 | |
| 81 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 82 | */ |
| 83 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 84 | #ifndef NDEBUG |
| 85 | |
| 86 | /* Turn on heavy reference debugging */ |
| 87 | #define TRACE_REFS |
| 88 | |
| 89 | /* Turn on reference counting */ |
| 90 | #define REF_DEBUG |
| 91 | |
| 92 | #endif /* NDEBUG */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 93 | |
| 94 | #ifdef TRACE_REFS |
| 95 | #define OB_HEAD \ |
| 96 | struct _object *_ob_next, *_ob_prev; \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 97 | int ob_refcnt; \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 98 | struct _typeobject *ob_type; |
| 99 | #define OB_HEAD_INIT(type) 0, 0, 1, type, |
| 100 | #else |
| 101 | #define OB_HEAD \ |
| 102 | unsigned int ob_refcnt; \ |
| 103 | struct _typeobject *ob_type; |
| 104 | #define OB_HEAD_INIT(type) 1, type, |
| 105 | #endif |
| 106 | |
| 107 | #define OB_VARHEAD \ |
| 108 | OB_HEAD \ |
| 109 | unsigned int ob_size; /* Number of items in variable part */ |
| 110 | |
| 111 | typedef struct _object { |
| 112 | OB_HEAD |
| 113 | } object; |
| 114 | |
| 115 | typedef struct { |
| 116 | OB_VARHEAD |
| 117 | } varobject; |
| 118 | |
| 119 | |
| 120 | /* |
| 121 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 122 | |
| 123 | Type objects contain a string containing the type name (to help somewhat |
| 124 | in debugging), the allocation parameters (see newobj() and newvarobj()), |
| 125 | and methods for accessing objects of the type. Methods are optional,a |
| 126 | nil pointer meaning that particular kind of access is not available for |
| 127 | this type. The DECREF() macro uses the tp_dealloc method without |
| 128 | checking for a nil pointer; it should always be implemented except if |
| 129 | the implementation can guarantee that the reference count will never |
| 130 | reach zero (e.g., for type objects). |
| 131 | |
| 132 | NB: the methods for certain type groups are now contained in separate |
| 133 | method blocks. |
| 134 | */ |
| 135 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 136 | typedef object * (*unaryfunc) PROTO((object *)); |
| 137 | typedef object * (*binaryfunc) PROTO((object *, object *)); |
Guido van Rossum | 75abc63 | 1994-08-09 13:21:54 +0000 | [diff] [blame] | 138 | typedef object * (*ternaryfunc) PROTO((object *, object *, object *)); |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 139 | typedef int (*inquiry) PROTO((object *)); |
| 140 | typedef int (*coercion) PROTO((object **, object **)); |
| 141 | typedef object *(*intargfunc) PROTO((object *, int)); |
| 142 | typedef object *(*intintargfunc) PROTO((object *, int, int)); |
| 143 | typedef int(*intobjargproc) PROTO((object *, int, object *)); |
| 144 | typedef int(*intintobjargproc) PROTO((object *, int, int, object *)); |
| 145 | typedef int(*objobjargproc) PROTO((object *, object *, object *)); |
| 146 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 148 | binaryfunc nb_add; |
| 149 | binaryfunc nb_subtract; |
| 150 | binaryfunc nb_multiply; |
| 151 | binaryfunc nb_divide; |
| 152 | binaryfunc nb_remainder; |
| 153 | binaryfunc nb_divmod; |
Guido van Rossum | 75abc63 | 1994-08-09 13:21:54 +0000 | [diff] [blame] | 154 | ternaryfunc nb_power; |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 155 | unaryfunc nb_negative; |
| 156 | unaryfunc nb_positive; |
| 157 | unaryfunc nb_absolute; |
| 158 | inquiry nb_nonzero; |
| 159 | unaryfunc nb_invert; |
| 160 | binaryfunc nb_lshift; |
| 161 | binaryfunc nb_rshift; |
| 162 | binaryfunc nb_and; |
| 163 | binaryfunc nb_xor; |
| 164 | binaryfunc nb_or; |
| 165 | coercion nb_coerce; |
| 166 | unaryfunc nb_int; |
| 167 | unaryfunc nb_long; |
| 168 | unaryfunc nb_float; |
| 169 | unaryfunc nb_oct; |
| 170 | unaryfunc nb_hex; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 171 | } number_methods; |
| 172 | |
| 173 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 174 | inquiry sq_length; |
| 175 | binaryfunc sq_concat; |
| 176 | intargfunc sq_repeat; |
| 177 | intargfunc sq_item; |
| 178 | intintargfunc sq_slice; |
| 179 | intobjargproc sq_ass_item; |
| 180 | intintobjargproc sq_ass_slice; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | } sequence_methods; |
| 182 | |
| 183 | typedef struct { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 184 | inquiry mp_length; |
| 185 | binaryfunc mp_subscript; |
| 186 | objobjargproc mp_ass_subscript; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | } mapping_methods; |
| 188 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 189 | typedef void (*destructor) PROTO((object *)); |
| 190 | typedef int (*printfunc) PROTO((object *, FILE *, int)); |
| 191 | typedef object *(*getattrfunc) PROTO((object *, char *)); |
| 192 | typedef int (*setattrfunc) PROTO((object *, char *, object *)); |
| 193 | typedef int (*cmpfunc) PROTO((object *, object *)); |
| 194 | typedef object *(*reprfunc) PROTO((object *)); |
| 195 | typedef long (*hashfunc) PROTO((object *)); |
| 196 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | typedef struct _typeobject { |
| 198 | OB_VARHEAD |
| 199 | char *tp_name; /* For printing */ |
| 200 | unsigned int tp_basicsize, tp_itemsize; /* For allocation */ |
| 201 | |
| 202 | /* Methods to implement standard operations */ |
| 203 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 204 | destructor tp_dealloc; |
| 205 | printfunc tp_print; |
| 206 | getattrfunc tp_getattr; |
| 207 | setattrfunc tp_setattr; |
| 208 | cmpfunc tp_compare; |
| 209 | reprfunc tp_repr; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 210 | |
| 211 | /* Method suites for standard classes */ |
| 212 | |
| 213 | number_methods *tp_as_number; |
| 214 | sequence_methods *tp_as_sequence; |
| 215 | mapping_methods *tp_as_mapping; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 216 | |
| 217 | /* More standard operations (at end for binary compatibility) */ |
| 218 | |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 219 | hashfunc tp_hash; |
| 220 | binaryfunc tp_call; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 221 | #ifdef COUNT_ALLOCS |
| 222 | /* these must be last */ |
| 223 | int tp_alloc; |
| 224 | int tp_free; |
| 225 | int tp_maxalloc; |
| 226 | struct _typeobject *tp_next; |
| 227 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | } typeobject; |
| 229 | |
Guido van Rossum | e89bc75 | 1994-08-18 16:18:13 +0000 | [diff] [blame] | 230 | extern DL_IMPORT typeobject Typetype; /* The type of type objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 231 | |
| 232 | #define is_typeobject(op) ((op)->ob_type == &Typetype) |
| 233 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 234 | /* Generic operations on objects */ |
Guido van Rossum | d783a46 | 1991-06-07 22:35:42 +0000 | [diff] [blame] | 235 | extern int printobject PROTO((object *, FILE *, int)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 236 | extern object * reprobject PROTO((object *)); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 237 | extern object * strobject PROTO((object *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 238 | extern int cmpobject PROTO((object *, object *)); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 239 | extern object *getattr PROTO((object *, char *)); |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 240 | extern int hasattr PROTO((object *, char *)); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 241 | extern object *getattro PROTO((object *, object *)); |
| 242 | extern int setattro PROTO((object *, object *, object *)); |
| 243 | extern long hashobject PROTO((object *)); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 244 | |
| 245 | /* Flag bits for printing: */ |
| 246 | #define PRINT_RAW 1 /* No string quotes etc. */ |
| 247 | |
| 248 | /* |
| 249 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 250 | |
| 251 | The macros INCREF(op) and DECREF(op) are used to increment or decrement |
| 252 | reference counts. DECREF calls the object's deallocator function; for |
| 253 | objects that don't contain references to other objects or heap memory |
| 254 | this can be the standard function free(). Both macros can be used |
| 255 | whereever a void expression is allowed. The argument shouldn't be a |
| 256 | NIL pointer. The macro NEWREF(op) is used only to initialize reference |
| 257 | counts to 1; it is defined here for convenience. |
| 258 | |
| 259 | We assume that the reference count field can never overflow; this can |
| 260 | be proven when the size of the field is the same as the pointer size |
| 261 | but even with a 16-bit reference count field it is pretty unlikely so |
| 262 | we ignore the possibility. (If you are paranoid, make it a long.) |
| 263 | |
| 264 | Type objects should never be deallocated; the type pointer in an object |
| 265 | is not considered to be a reference to the type object, to save |
| 266 | complications in the deallocation function. (This is actually a |
| 267 | decision that's up to the implementer of each new type so if you want, |
| 268 | you can count such references to the type object.) |
| 269 | |
| 270 | *** WARNING*** The DECREF macro must have a side-effect-free argument |
| 271 | since it may evaluate its argument multiple times. (The alternative |
| 272 | would be to mace it a proper function or assign it to a global temporary |
| 273 | variable first, both of which are slower; and in a multi-threaded |
| 274 | environment the global variable trick is not safe.) |
| 275 | */ |
| 276 | |
| 277 | #ifdef TRACE_REFS |
| 278 | #ifndef REF_DEBUG |
| 279 | #define REF_DEBUG |
| 280 | #endif |
| 281 | #endif |
| 282 | |
| 283 | #ifndef TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 284 | #ifdef COUNT_ALLOCS |
| 285 | #define DELREF(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((object *)(op))) |
| 286 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 287 | #define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op)) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 288 | #endif |
Guido van Rossum | d5b70f5 | 1990-11-18 17:27:10 +0000 | [diff] [blame] | 289 | #define UNREF(op) /*empty*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 290 | #endif |
| 291 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 292 | #ifdef COUNT_ALLOCS |
| 293 | extern void inc_count PROTO((typeobject *)); |
| 294 | #endif |
| 295 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 296 | #ifdef REF_DEBUG |
| 297 | extern long ref_total; |
| 298 | #ifndef TRACE_REFS |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 299 | #ifdef COUNT_ALLOCS |
| 300 | #define NEWREF(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1) |
| 301 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 302 | #define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1) |
| 303 | #endif |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 304 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 305 | #define INCREF(op) (ref_total++, (op)->ob_refcnt++) |
| 306 | #define DECREF(op) \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 307 | if (--ref_total, --(op)->ob_refcnt > 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 308 | ; \ |
| 309 | else \ |
| 310 | DELREF(op) |
| 311 | #else |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 312 | #ifdef COUNT_ALLOCS |
| 313 | #define NEWREF(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1) |
| 314 | #else |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 315 | #define NEWREF(op) ((op)->ob_refcnt = 1) |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 316 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 317 | #define INCREF(op) ((op)->ob_refcnt++) |
| 318 | #define DECREF(op) \ |
Guido van Rossum | c8564cd | 1990-11-02 17:51:56 +0000 | [diff] [blame] | 319 | if (--(op)->ob_refcnt > 0) \ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 320 | ; \ |
| 321 | else \ |
| 322 | DELREF(op) |
| 323 | #endif |
| 324 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 325 | /* Macros to use in case the object pointer may be NULL: */ |
| 326 | |
| 327 | #define XINCREF(op) if ((op) == NULL) ; else INCREF(op) |
| 328 | #define XDECREF(op) if ((op) == NULL) ; else DECREF(op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 329 | |
| 330 | /* Definition of NULL, so you don't have to include <stdio.h> */ |
| 331 | |
| 332 | #ifndef NULL |
| 333 | #define NULL 0 |
| 334 | #endif |
| 335 | |
| 336 | |
| 337 | /* |
| 338 | NoObject is an object of undefined type which can be used in contexts |
| 339 | where NULL (nil) is not suitable (since NULL often means 'error'). |
| 340 | |
| 341 | Don't forget to apply INCREF() when returning this value!!! |
| 342 | */ |
| 343 | |
Guido van Rossum | e89bc75 | 1994-08-18 16:18:13 +0000 | [diff] [blame] | 344 | extern DL_IMPORT object NoObject; /* Don't use this directly */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 345 | |
| 346 | #define None (&NoObject) |
| 347 | |
| 348 | |
| 349 | /* |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 350 | A common programming style in Python requires the forward declaration |
| 351 | of static, initialized structures, e.g. for a typeobject that is used |
| 352 | by the functions whose address must be used in the initializer. |
| 353 | Some compilers (notably SCO ODT 3.0, I seem to remember early AIX as |
| 354 | well) botch this if you use the static keyword for both declarations |
| 355 | (they allocate two objects, and use the first, uninitialized one until |
| 356 | the second declaration is encountered). Therefore, the forward |
| 357 | declaration should use the 'forwardstatic' keyword. This expands to |
| 358 | static on most systems, but to extern on a few. The actual storage |
| 359 | and name will still be static because the second declaration is |
| 360 | static, so no linker visible symbols will be generated. (Standard C |
| 361 | compilers take offense to the extern forward declaration of a static |
| 362 | object, so I can't just put extern in all cases. :-( ) |
| 363 | */ |
| 364 | |
| 365 | #ifdef BAD_STATIC_FORWARD |
| 366 | #define staticforward extern |
| 367 | #else |
| 368 | #define staticforward static |
| 369 | #endif /* BAD_STATIC_FORWARD */ |
| 370 | |
| 371 | |
| 372 | /* |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 373 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 374 | |
| 375 | More conventions |
| 376 | ================ |
| 377 | |
| 378 | Argument Checking |
| 379 | ----------------- |
| 380 | |
| 381 | Functions that take objects as arguments normally don't check for nil |
| 382 | arguments, but they do check the type of the argument, and return an |
| 383 | error if the function doesn't apply to the type. |
| 384 | |
| 385 | Failure Modes |
| 386 | ------------- |
| 387 | |
| 388 | 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] | 389 | memory. This is communicated to the caller in two ways: an error string |
| 390 | is set (see errors.h), and the function result differs: functions that |
| 391 | normally return a pointer return NULL for failure, functions returning |
| 392 | an integer return -1 (which could be a legal return value too!), and |
| 393 | other functions return 0 for success and -1 for failure. |
| 394 | Callers should always check for errors before using the result. |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 395 | |
| 396 | Reference Counts |
| 397 | ---------------- |
| 398 | |
| 399 | It takes a while to get used to the proper usage of reference counts. |
| 400 | |
| 401 | Functions that create an object set the reference count to 1; such new |
| 402 | objects must be stored somewhere or destroyed again with DECREF(). |
| 403 | Functions that 'store' objects such as settupleitem() and dictinsert() |
| 404 | don't increment the reference count of the object, since the most |
| 405 | frequent use is to store a fresh object. Functions that 'retrieve' |
| 406 | objects such as gettupleitem() and dictlookup() also don't increment |
| 407 | the reference count, since most frequently the object is only looked at |
| 408 | quickly. Thus, to retrieve an object and store it again, the caller |
| 409 | must call INCREF() explicitly. |
| 410 | |
| 411 | NOTE: functions that 'consume' a reference count like dictinsert() even |
| 412 | consume the reference if the object wasn't stored, to simplify error |
| 413 | handling. |
| 414 | |
| 415 | It seems attractive to make other functions that take an object as |
| 416 | argument consume a reference count; however this may quickly get |
| 417 | confusing (even the current practice is already confusing). Consider |
| 418 | it carefully, it may safe lots of calls to INCREF() and DECREF() at |
| 419 | times. |
| 420 | |
| 421 | 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 |
| 422 | */ |
Guido van Rossum | a330996 | 1993-07-28 09:05:47 +0000 | [diff] [blame] | 423 | |
| 424 | #ifdef __cplusplus |
| 425 | } |
| 426 | #endif |
| 427 | #endif /* !Py_OBJECT_H */ |