Mercurial > cpython-lru_cache-weakref
annotate Include/object.h @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | aac8231a715c |
| children | 1940892d2fe3 |
| rev | line source |
|---|---|
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
1 #ifndef Py_OBJECT_H |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
2 #define Py_OBJECT_H |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
3 #ifdef __cplusplus |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
4 extern "C" { |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
5 #endif |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
6 |
| 145 | 7 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
8 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
9 Amsterdam, The Netherlands. |
| 145 | 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 | |
|
1018
3945f3712354
AOnly define NDEDBUG if DEBUG is not defined
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
31 #ifndef DEBUG |
| 86 | 32 #define NDEBUG |
|
1018
3945f3712354
AOnly define NDEDBUG if DEBUG is not defined
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
33 #endif |
|
3945f3712354
AOnly define NDEDBUG if DEBUG is not defined
Guido van Rossum <guido@python.org>
parents:
955
diff
changeset
|
34 |
| 6 | 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 | |
| 86 | 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 */ | |
| 6 | 93 |
| 94 #ifdef TRACE_REFS | |
| 95 #define OB_HEAD \ | |
| 96 struct _object *_ob_next, *_ob_prev; \ | |
|
62
fffd1286c882
Be more careful with negative reference counts.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
97 int ob_refcnt; \ |
| 6 | 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 | |
| 136 typedef struct { | |
| 137 object *(*nb_add) FPROTO((object *, object *)); | |
| 138 object *(*nb_subtract) FPROTO((object *, object *)); | |
| 139 object *(*nb_multiply) FPROTO((object *, object *)); | |
| 140 object *(*nb_divide) FPROTO((object *, object *)); | |
| 141 object *(*nb_remainder) FPROTO((object *, object *)); | |
|
232
af2e64007698
Added divmod and abs methods for numbers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
142 object *(*nb_divmod) FPROTO((object *, object *)); |
| 6 | 143 object *(*nb_power) FPROTO((object *, object *)); |
| 144 object *(*nb_negative) FPROTO((object *)); | |
| 145 object *(*nb_positive) FPROTO((object *)); | |
|
232
af2e64007698
Added divmod and abs methods for numbers
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
146 object *(*nb_absolute) FPROTO((object *)); |
|
243
5f7c24b3953b
Added nonzero to number methods
Guido van Rossum <guido@python.org>
parents:
232
diff
changeset
|
147 int (*nb_nonzero) FPROTO((object *)); |
|
408
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
148 object *(*nb_invert) FPROTO((object *)); |
|
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
149 object *(*nb_lshift) FPROTO((object *, object *)); |
|
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
150 object *(*nb_rshift) FPROTO((object *, object *)); |
|
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
151 object *(*nb_and) FPROTO((object *, object *)); |
|
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
152 object *(*nb_xor) FPROTO((object *, object *)); |
|
952133e89bc7
Added shifting and masking ops to as_number struct
Guido van Rossum <guido@python.org>
parents:
285
diff
changeset
|
153 object *(*nb_or) FPROTO((object *, object *)); |
|
955
03de67080c95
* classobject.[ch], {float,long,int}object.c, bltinmodule.c:
Guido van Rossum <guido@python.org>
parents:
790
diff
changeset
|
154 int (*nb_coerce) FPROTO((object **, object **)); |
|
1049
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1018
diff
changeset
|
155 object *(*nb_int) FPROTO((object *)); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1018
diff
changeset
|
156 object *(*nb_long) FPROTO((object *)); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1018
diff
changeset
|
157 object *(*nb_float) FPROTO((object *)); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1018
diff
changeset
|
158 object *(*nb_oct) FPROTO((object *)); |
|
2ed511171809
Made builtins int(), long(), float(), oct() and hex() more generic.
Guido van Rossum <guido@python.org>
parents:
1018
diff
changeset
|
159 object *(*nb_hex) FPROTO((object *)); |
| 6 | 160 } number_methods; |
| 161 | |
| 162 typedef struct { | |
| 163 int (*sq_length) FPROTO((object *)); | |
| 164 object *(*sq_concat) FPROTO((object *, object *)); | |
| 165 object *(*sq_repeat) FPROTO((object *, int)); | |
| 166 object *(*sq_item) FPROTO((object *, int)); | |
| 167 object *(*sq_slice) FPROTO((object *, int, int)); | |
| 168 int (*sq_ass_item) FPROTO((object *, int, object *)); | |
| 169 int (*sq_ass_slice) FPROTO((object *, int, int, object *)); | |
| 170 } sequence_methods; | |
| 171 | |
| 172 typedef struct { | |
| 173 int (*mp_length) FPROTO((object *)); | |
| 174 object *(*mp_subscript) FPROTO((object *, object *)); | |
| 175 int (*mp_ass_subscript) FPROTO((object *, object *, object *)); | |
| 176 } mapping_methods; | |
| 177 | |
| 178 typedef struct _typeobject { | |
| 179 OB_VARHEAD | |
| 180 char *tp_name; /* For printing */ | |
| 181 unsigned int tp_basicsize, tp_itemsize; /* For allocation */ | |
| 182 | |
| 183 /* Methods to implement standard operations */ | |
| 184 | |
| 185 void (*tp_dealloc) FPROTO((object *)); | |
|
285
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
243
diff
changeset
|
186 int (*tp_print) FPROTO((object *, FILE *, int)); |
| 6 | 187 object *(*tp_getattr) FPROTO((object *, char *)); |
| 188 int (*tp_setattr) FPROTO((object *, char *, object *)); | |
| 189 int (*tp_compare) FPROTO((object *, object *)); | |
| 190 object *(*tp_repr) FPROTO((object *)); | |
| 191 | |
| 192 /* Method suites for standard classes */ | |
| 193 | |
| 194 number_methods *tp_as_number; | |
| 195 sequence_methods *tp_as_sequence; | |
| 196 mapping_methods *tp_as_mapping; | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
197 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
198 /* More standard operations (at end for binary compatibility) */ |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
199 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
200 long (*tp_hash) FPROTO((object *)); |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
201 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
202 /* these must be last */ |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
203 int tp_alloc; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
204 int tp_free; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
205 int tp_maxalloc; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
206 struct _typeobject *tp_next; |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
207 #endif |
| 6 | 208 } typeobject; |
| 209 | |
| 210 extern typeobject Typetype; /* The type of type objects */ | |
| 211 | |
| 212 #define is_typeobject(op) ((op)->ob_type == &Typetype) | |
| 213 | |
| 86 | 214 /* Generic operations on objects */ |
|
285
85bd7dac13f3
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
243
diff
changeset
|
215 extern int printobject PROTO((object *, FILE *, int)); |
| 6 | 216 extern object * reprobject PROTO((object *)); |
|
1400
aac8231a715c
* mpzmodule.c: removed redundant mpz_print function.
Guido van Rossum <guido@python.org>
parents:
1347
diff
changeset
|
217 extern object * strobject PROTO((object *)); |
| 6 | 218 extern int cmpobject PROTO((object *, object *)); |
| 86 | 219 extern object *getattr PROTO((object *, char *)); |
|
1324
716be765a79b
* accessobject.c (ownercheck): allow a base class access to protected
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
220 extern int hasattr PROTO((object *, char *)); |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
221 extern object *getattro PROTO((object *, object *)); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
222 extern int setattro PROTO((object *, object *, object *)); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1049
diff
changeset
|
223 extern long hashobject PROTO((object *)); |
| 6 | 224 |
| 225 /* Flag bits for printing: */ | |
| 226 #define PRINT_RAW 1 /* No string quotes etc. */ | |
| 227 | |
| 228 /* | |
| 229 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 | |
| 230 | |
| 231 The macros INCREF(op) and DECREF(op) are used to increment or decrement | |
| 232 reference counts. DECREF calls the object's deallocator function; for | |
| 233 objects that don't contain references to other objects or heap memory | |
| 234 this can be the standard function free(). Both macros can be used | |
| 235 whereever a void expression is allowed. The argument shouldn't be a | |
| 236 NIL pointer. The macro NEWREF(op) is used only to initialize reference | |
| 237 counts to 1; it is defined here for convenience. | |
| 238 | |
| 239 We assume that the reference count field can never overflow; this can | |
| 240 be proven when the size of the field is the same as the pointer size | |
| 241 but even with a 16-bit reference count field it is pretty unlikely so | |
| 242 we ignore the possibility. (If you are paranoid, make it a long.) | |
| 243 | |
| 244 Type objects should never be deallocated; the type pointer in an object | |
| 245 is not considered to be a reference to the type object, to save | |
| 246 complications in the deallocation function. (This is actually a | |
| 247 decision that's up to the implementer of each new type so if you want, | |
| 248 you can count such references to the type object.) | |
| 249 | |
| 250 *** WARNING*** The DECREF macro must have a side-effect-free argument | |
| 251 since it may evaluate its argument multiple times. (The alternative | |
| 252 would be to mace it a proper function or assign it to a global temporary | |
| 253 variable first, both of which are slower; and in a multi-threaded | |
| 254 environment the global variable trick is not safe.) | |
| 255 */ | |
| 256 | |
| 257 #ifdef TRACE_REFS | |
| 258 #ifndef REF_DEBUG | |
| 259 #define REF_DEBUG | |
| 260 #endif | |
| 261 #endif | |
| 262 | |
| 263 #ifndef TRACE_REFS | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
264 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
265 #define DELREF(op) ((op)->ob_type->tp_free++, (*(op)->ob_type->tp_dealloc)((object *)(op))) |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
266 #else |
| 6 | 267 #define DELREF(op) (*(op)->ob_type->tp_dealloc)((object *)(op)) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
268 #endif |
|
68
36503736fdc2
Add UNREF macro if not tracing refs (see UNREF function in object.c).
Guido van Rossum <guido@python.org>
parents:
62
diff
changeset
|
269 #define UNREF(op) /*empty*/ |
| 6 | 270 #endif |
| 271 | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
272 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
273 extern void inc_count PROTO((typeobject *)); |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
274 #endif |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
275 |
| 6 | 276 #ifdef REF_DEBUG |
| 277 extern long ref_total; | |
| 278 #ifndef TRACE_REFS | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
279 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
280 #define NEWREF(op) (inc_count((op)->ob_type), ref_total++, (op)->ob_refcnt = 1) |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
281 #else |
| 6 | 282 #define NEWREF(op) (ref_total++, (op)->ob_refcnt = 1) |
| 283 #endif | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
284 #endif |
| 6 | 285 #define INCREF(op) (ref_total++, (op)->ob_refcnt++) |
| 286 #define DECREF(op) \ | |
|
62
fffd1286c882
Be more careful with negative reference counts.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
287 if (--ref_total, --(op)->ob_refcnt > 0) \ |
| 6 | 288 ; \ |
| 289 else \ | |
| 290 DELREF(op) | |
| 291 #else | |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
292 #ifdef COUNT_ALLOCS |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
293 #define NEWREF(op) (inc_count((op)->ob_type), (op)->ob_refcnt = 1) |
|
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
294 #else |
| 6 | 295 #define NEWREF(op) ((op)->ob_refcnt = 1) |
|
1347
a22a350d3a5d
* Extended X interface: pixmap objects, colormap objects visual objects,
Sjoerd Mullender <sjoerd@acm.org>
parents:
1329
diff
changeset
|
296 #endif |
| 6 | 297 #define INCREF(op) ((op)->ob_refcnt++) |
| 298 #define DECREF(op) \ | |
|
62
fffd1286c882
Be more careful with negative reference counts.
Guido van Rossum <guido@python.org>
parents:
6
diff
changeset
|
299 if (--(op)->ob_refcnt > 0) \ |
| 6 | 300 ; \ |
| 301 else \ | |
| 302 DELREF(op) | |
| 303 #endif | |
| 304 | |
| 86 | 305 /* Macros to use in case the object pointer may be NULL: */ |
| 306 | |
| 307 #define XINCREF(op) if ((op) == NULL) ; else INCREF(op) | |
| 308 #define XDECREF(op) if ((op) == NULL) ; else DECREF(op) | |
| 6 | 309 |
| 310 /* Definition of NULL, so you don't have to include <stdio.h> */ | |
| 311 | |
| 312 #ifndef NULL | |
| 313 #define NULL 0 | |
| 314 #endif | |
| 315 | |
| 316 | |
| 317 /* | |
| 318 NoObject is an object of undefined type which can be used in contexts | |
| 319 where NULL (nil) is not suitable (since NULL often means 'error'). | |
| 320 | |
| 321 Don't forget to apply INCREF() when returning this value!!! | |
| 322 */ | |
| 323 | |
| 324 extern object NoObject; /* Don't use this directly */ | |
| 325 | |
| 326 #define None (&NoObject) | |
| 327 | |
| 328 | |
| 329 /* | |
| 330 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 | |
| 331 | |
| 332 More conventions | |
| 333 ================ | |
| 334 | |
| 335 Argument Checking | |
| 336 ----------------- | |
| 337 | |
| 338 Functions that take objects as arguments normally don't check for nil | |
| 339 arguments, but they do check the type of the argument, and return an | |
| 340 error if the function doesn't apply to the type. | |
| 341 | |
| 342 Failure Modes | |
| 343 ------------- | |
| 344 | |
| 345 Functions may fail for a variety of reasons, including running out of | |
| 86 | 346 memory. This is communicated to the caller in two ways: an error string |
| 347 is set (see errors.h), and the function result differs: functions that | |
| 348 normally return a pointer return NULL for failure, functions returning | |
| 349 an integer return -1 (which could be a legal return value too!), and | |
| 350 other functions return 0 for success and -1 for failure. | |
| 351 Callers should always check for errors before using the result. | |
| 6 | 352 |
| 353 Reference Counts | |
| 354 ---------------- | |
| 355 | |
| 356 It takes a while to get used to the proper usage of reference counts. | |
| 357 | |
| 358 Functions that create an object set the reference count to 1; such new | |
| 359 objects must be stored somewhere or destroyed again with DECREF(). | |
| 360 Functions that 'store' objects such as settupleitem() and dictinsert() | |
| 361 don't increment the reference count of the object, since the most | |
| 362 frequent use is to store a fresh object. Functions that 'retrieve' | |
| 363 objects such as gettupleitem() and dictlookup() also don't increment | |
| 364 the reference count, since most frequently the object is only looked at | |
| 365 quickly. Thus, to retrieve an object and store it again, the caller | |
| 366 must call INCREF() explicitly. | |
| 367 | |
| 368 NOTE: functions that 'consume' a reference count like dictinsert() even | |
| 369 consume the reference if the object wasn't stored, to simplify error | |
| 370 handling. | |
| 371 | |
| 372 It seems attractive to make other functions that take an object as | |
| 373 argument consume a reference count; however this may quickly get | |
| 374 confusing (even the current practice is already confusing). Consider | |
| 375 it carefully, it may safe lots of calls to INCREF() and DECREF() at | |
| 376 times. | |
| 377 | |
| 378 123456789-123456789-123456789-123456789-123456789-123456789-123456789-12 | |
| 379 */ | |
|
1329
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
380 |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
381 #ifdef __cplusplus |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
382 } |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
383 #endif |
|
519c25039c6a
* Added support for X11 modules.
Guido van Rossum <guido@python.org>
parents:
1324
diff
changeset
|
384 #endif /* !Py_OBJECT_H */ |
