Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
| 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
| 9 | provided that the above copyright notice appear in all copies and that |
| 10 | both that copyright notice and this permission notice appear in |
| 11 | supporting documentation, and that the names of Stichting Mathematisch |
| 12 | Centrum or CWI not be used in advertising or publicity pertaining to |
| 13 | distribution of the software without specific, written prior permission. |
| 14 | |
| 15 | STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO |
| 16 | THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 17 | FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE |
| 18 | FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 19 | WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN |
| 20 | ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT |
| 21 | OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. |
| 22 | |
| 23 | ******************************************************************/ |
| 24 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 25 | /* Generic object operations; and implementation of None (NoObject) */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 6f9e433 | 1995-03-29 16:57:48 +0000 | [diff] [blame] | 29 | #if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 30 | long ref_total; |
| 31 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 33 | /* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. |
| 34 | These are used by the individual routines for object creation. |
| 35 | Do not call them otherwise, they do not initialize the object! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 36 | |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 37 | #ifdef COUNT_ALLOCS |
| 38 | static typeobject *type_list; |
Sjoerd Mullender | 842d2cc | 1993-10-15 16:18:48 +0000 | [diff] [blame] | 39 | extern int tuple_zero_allocs, fast_tuple_allocs; |
| 40 | extern int quick_int_allocs, quick_neg_int_allocs; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 41 | extern int null_strings, one_strings; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 42 | void |
| 43 | dump_counts() |
| 44 | { |
| 45 | typeobject *tp; |
| 46 | |
| 47 | for (tp = type_list; tp; tp = tp->tp_next) |
Sjoerd Mullender | 52c1f51 | 1993-10-25 08:40:52 +0000 | [diff] [blame] | 48 | fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", |
| 49 | tp->tp_name, tp->tp_alloc, tp->tp_free, |
| 50 | tp->tp_maxalloc); |
| 51 | fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", |
| 52 | fast_tuple_allocs, tuple_zero_allocs); |
| 53 | fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", |
| 54 | quick_int_allocs, quick_neg_int_allocs); |
| 55 | fprintf(stderr, "null strings: %d, 1-strings: %d\n", |
| 56 | null_strings, one_strings); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 57 | } |
| 58 | |
| 59 | void |
| 60 | inc_count(tp) |
| 61 | typeobject *tp; |
| 62 | { |
| 63 | if (tp->tp_alloc == 0) { |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 64 | /* first time; insert in linked list */ |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 65 | if (tp->tp_next != NULL) /* sanity check */ |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 66 | fatal("XXX inc_count sanity check"); |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 67 | tp->tp_next = type_list; |
| 68 | type_list = tp; |
| 69 | } |
| 70 | tp->tp_alloc++; |
| 71 | if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc) |
| 72 | tp->tp_maxalloc = tp->tp_alloc - tp->tp_free; |
| 73 | } |
| 74 | #endif |
| 75 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | object * |
| 77 | newobject(tp) |
| 78 | typeobject *tp; |
| 79 | { |
| 80 | object *op = (object *) malloc(tp->tp_basicsize); |
| 81 | if (op == NULL) |
| 82 | return err_nomem(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 83 | op->ob_type = tp; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 84 | NEWREF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | return op; |
| 86 | } |
| 87 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 88 | varobject * |
| 89 | newvarobject(tp, size) |
| 90 | typeobject *tp; |
Guido van Rossum | 2497ead | 1995-02-10 17:00:27 +0000 | [diff] [blame] | 91 | int size; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 92 | { |
| 93 | varobject *op = (varobject *) |
| 94 | malloc(tp->tp_basicsize + size * tp->tp_itemsize); |
| 95 | if (op == NULL) |
Guido van Rossum | 05ab111 | 1991-05-05 20:10:41 +0000 | [diff] [blame] | 96 | return (varobject *)err_nomem(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 97 | op->ob_type = tp; |
| 98 | op->ob_size = size; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 99 | NEWREF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | return op; |
| 101 | } |
| 102 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 103 | int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 104 | printobject(op, fp, flags) |
| 105 | object *op; |
| 106 | FILE *fp; |
| 107 | int flags; |
| 108 | { |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 109 | int ret = 0; |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 110 | if (sigcheck()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 111 | return -1; |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 112 | if (op == NULL) { |
| 113 | fprintf(fp, "<nil>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 115 | else { |
| 116 | if (op->ob_refcnt <= 0) |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 117 | fprintf(fp, "<refcnt %u at %lx>", |
| 118 | op->ob_refcnt, (long)op); |
| 119 | else if (op->ob_type->tp_print == NULL) { |
| 120 | if (op->ob_type->tp_repr == NULL) { |
| 121 | fprintf(fp, "<%s object at %lx>", |
| 122 | op->ob_type->tp_name, (long)op); |
| 123 | } |
| 124 | else { |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 125 | object *s; |
| 126 | if (flags & PRINT_RAW) |
| 127 | s = strobject(op); |
| 128 | else |
| 129 | s = reprobject(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 130 | if (s == NULL) |
| 131 | ret = -1; |
| 132 | else if (!is_stringobject(s)) { |
| 133 | err_setstr(TypeError, |
| 134 | "repr not string"); |
| 135 | ret = -1; |
| 136 | } |
| 137 | else { |
| 138 | fprintf(fp, "%s", getstringvalue(s)); |
| 139 | } |
| 140 | XDECREF(s); |
| 141 | } |
| 142 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 143 | else |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 144 | ret = (*op->ob_type->tp_print)(op, fp, flags); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 145 | } |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 146 | if (ret == 0) { |
| 147 | if (ferror(fp)) { |
Guido van Rossum | 2912f22 | 1991-12-10 13:59:09 +0000 | [diff] [blame] | 148 | err_errno(IOError); |
Guido van Rossum | 278ef59 | 1991-07-27 21:40:24 +0000 | [diff] [blame] | 149 | clearerr(fp); |
| 150 | ret = -1; |
| 151 | } |
| 152 | } |
| 153 | return ret; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | object * |
| 157 | reprobject(v) |
| 158 | object *v; |
| 159 | { |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 160 | if (sigcheck()) |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 161 | return NULL; |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 162 | if (v == NULL) |
| 163 | return newstringobject("<NULL>"); |
| 164 | else if (v->ob_type->tp_repr == NULL) { |
| 165 | char buf[120]; |
| 166 | sprintf(buf, "<%.80s object at %lx>", |
| 167 | v->ob_type->tp_name, (long)v); |
| 168 | return newstringobject(buf); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 169 | } |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 170 | else |
| 171 | return (*v->ob_type->tp_repr)(v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | } |
| 173 | |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 174 | object * |
| 175 | strobject(v) |
| 176 | object *v; |
| 177 | { |
| 178 | if (v == NULL) |
| 179 | return newstringobject("<NULL>"); |
| 180 | if (is_stringobject(v)) { |
| 181 | INCREF(v); |
| 182 | return v; |
| 183 | } |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 184 | else if (v->ob_type->tp_str != NULL) |
| 185 | return (*v->ob_type->tp_str)(v); |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 186 | else { |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 187 | object *func; |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 188 | object *args; |
| 189 | object *res; |
Guido van Rossum | 32b582b | 1995-01-17 16:35:13 +0000 | [diff] [blame] | 190 | if (!is_instanceobject(v) || (func = getattr(v, "__str__")) == NULL) { |
Guido van Rossum | c600411 | 1993-11-05 10:22:19 +0000 | [diff] [blame] | 191 | err_clear(); |
| 192 | return reprobject(v); |
| 193 | } |
| 194 | args = newtupleobject(0); |
| 195 | if (args == NULL) |
| 196 | res = NULL; |
| 197 | else { |
| 198 | res = call_object(func, args); |
| 199 | DECREF(args); |
| 200 | } |
| 201 | DECREF(func); |
| 202 | return res; |
| 203 | } |
| 204 | } |
| 205 | |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 206 | static object * |
| 207 | do_cmp(v, w) |
| 208 | object *v, *w; |
| 209 | { |
| 210 | /* __rcmp__ actually won't be called unless __cmp__ isn't defined, |
| 211 | because the check in cmpobject() reverses the objects first. |
| 212 | This is intentional -- it makes no sense to define cmp(x,y) different |
| 213 | than -cmp(y,x). */ |
| 214 | if (is_instanceobject(v) || is_instanceobject(w)) |
| 215 | return instancebinop(v, w, "__cmp__", "__rcmp__", do_cmp); |
| 216 | return newintobject((long)cmpobject(v, w)); |
| 217 | } |
| 218 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 219 | int |
| 220 | cmpobject(v, w) |
| 221 | object *v, *w; |
| 222 | { |
| 223 | typeobject *tp; |
| 224 | if (v == w) |
| 225 | return 0; |
| 226 | if (v == NULL) |
| 227 | return -1; |
| 228 | if (w == NULL) |
| 229 | return 1; |
Guido van Rossum | 2056684 | 1995-01-12 11:26:10 +0000 | [diff] [blame] | 230 | if (is_instanceobject(v) || is_instanceobject(w)) { |
| 231 | object *res; |
| 232 | int c; |
| 233 | if (!is_instanceobject(v)) |
| 234 | return -cmpobject(w, v); |
| 235 | res = do_cmp(v, w); |
| 236 | if (res == NULL) { |
| 237 | err_clear(); |
| 238 | return (v < w) ? -1 : 1; |
| 239 | } |
| 240 | if (!is_intobject(res)) { |
| 241 | DECREF(res); |
| 242 | return (v < w) ? -1 : 1; |
| 243 | } |
| 244 | c = getintvalue(res); |
| 245 | DECREF(res); |
| 246 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 247 | } |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 248 | if ((tp = v->ob_type) != w->ob_type) { |
| 249 | if (tp->tp_as_number != NULL && |
| 250 | w->ob_type->tp_as_number != NULL) { |
| 251 | if (coerce(&v, &w) != 0) { |
| 252 | err_clear(); |
| 253 | /* XXX Should report the error, |
| 254 | XXX but the interface isn't there... */ |
| 255 | } |
| 256 | else { |
| 257 | int cmp = (*v->ob_type->tp_compare)(v, w); |
| 258 | DECREF(v); |
| 259 | DECREF(w); |
| 260 | return cmp; |
| 261 | } |
| 262 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | return strcmp(tp->tp_name, w->ob_type->tp_name); |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 264 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | if (tp->tp_compare == NULL) |
| 266 | return (v < w) ? -1 : 1; |
Guido van Rossum | 9fb0368 | 1991-07-01 18:48:04 +0000 | [diff] [blame] | 267 | return (*tp->tp_compare)(v, w); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 268 | } |
| 269 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 270 | long |
| 271 | hashobject(v) |
| 272 | object *v; |
| 273 | { |
| 274 | typeobject *tp = v->ob_type; |
| 275 | if (tp->tp_hash != NULL) |
| 276 | return (*tp->tp_hash)(v); |
| 277 | if (tp->tp_compare == NULL) |
| 278 | return (long) v; /* Use address as hash value */ |
| 279 | /* If there's a cmp but no hash defined, the object can't be hashed */ |
| 280 | err_setstr(TypeError, "unhashable type"); |
| 281 | return -1; |
| 282 | } |
| 283 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 284 | object * |
| 285 | getattr(v, name) |
| 286 | object *v; |
| 287 | char *name; |
| 288 | { |
| 289 | if (v->ob_type->tp_getattr == NULL) { |
Guido van Rossum | 016564a | 1995-01-07 11:54:44 +0000 | [diff] [blame] | 290 | err_setstr(AttributeError, "attribute-less object"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 291 | return NULL; |
| 292 | } |
| 293 | else { |
| 294 | return (*v->ob_type->tp_getattr)(v, name); |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | int |
Guido van Rossum | ed18fdc | 1993-07-11 19:55:34 +0000 | [diff] [blame] | 299 | hasattr(v, name) |
| 300 | object *v; |
| 301 | char *name; |
| 302 | { |
| 303 | object *res = getattr(v, name); |
| 304 | if (res != NULL) { |
| 305 | DECREF(res); |
| 306 | return 1; |
| 307 | } |
| 308 | err_clear(); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | int |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 313 | setattr(v, name, w) |
| 314 | object *v; |
| 315 | char *name; |
| 316 | object *w; |
| 317 | { |
| 318 | if (v->ob_type->tp_setattr == NULL) { |
| 319 | if (v->ob_type->tp_getattr == NULL) |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 320 | err_setstr(TypeError, |
| 321 | "attribute-less object (assign or del)"); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 322 | else |
Guido van Rossum | 3ea7412 | 1991-12-24 13:28:03 +0000 | [diff] [blame] | 323 | err_setstr(TypeError, |
| 324 | "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 325 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 326 | } |
| 327 | else { |
| 328 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 329 | } |
| 330 | } |
| 331 | |
Guido van Rossum | 6ac258d | 1993-05-12 08:24:20 +0000 | [diff] [blame] | 332 | /* Test a value used as condition, e.g., in a for or if statement. |
| 333 | Return -1 if an error occurred */ |
| 334 | |
| 335 | int |
| 336 | testbool(v) |
| 337 | object *v; |
| 338 | { |
| 339 | int res; |
| 340 | if (v == None) |
| 341 | res = 0; |
| 342 | else if (v->ob_type->tp_as_number != NULL) |
| 343 | res = (*v->ob_type->tp_as_number->nb_nonzero)(v); |
| 344 | else if (v->ob_type->tp_as_mapping != NULL) |
| 345 | res = (*v->ob_type->tp_as_mapping->mp_length)(v); |
| 346 | else if (v->ob_type->tp_as_sequence != NULL) |
| 347 | res = (*v->ob_type->tp_as_sequence->sq_length)(v); |
| 348 | else |
| 349 | res = 1; |
| 350 | if (res > 0) |
| 351 | res = 1; |
| 352 | return res; |
| 353 | } |
| 354 | |
Guido van Rossum | 5524a59 | 1995-01-10 15:26:20 +0000 | [diff] [blame] | 355 | /* Coerce two numeric types to the "larger" one. |
| 356 | Increment the reference count on each argument. |
| 357 | Return -1 and raise an exception if no coercion is possible |
| 358 | (and then no reference count is incremented). |
| 359 | */ |
| 360 | |
| 361 | int |
| 362 | coerce(pv, pw) |
| 363 | object **pv, **pw; |
| 364 | { |
| 365 | register object *v = *pv; |
| 366 | register object *w = *pw; |
| 367 | int res; |
| 368 | |
| 369 | if (v->ob_type == w->ob_type && !is_instanceobject(v)) { |
| 370 | INCREF(v); |
| 371 | INCREF(w); |
| 372 | return 0; |
| 373 | } |
| 374 | if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { |
| 375 | res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); |
| 376 | if (res <= 0) |
| 377 | return res; |
| 378 | } |
| 379 | if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { |
| 380 | res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); |
| 381 | if (res <= 0) |
| 382 | return res; |
| 383 | } |
| 384 | err_setstr(TypeError, "number coercion failed"); |
| 385 | return -1; |
| 386 | } |
| 387 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 388 | |
Guido van Rossum | 49b11fe | 1995-01-26 00:38:22 +0000 | [diff] [blame] | 389 | /* Test whether an object can be called */ |
| 390 | |
| 391 | int |
| 392 | callable(x) |
| 393 | object *x; |
| 394 | { |
| 395 | if (x == NULL) |
| 396 | return 0; |
| 397 | if (x->ob_type->tp_call != NULL || |
| 398 | is_funcobject(x) || |
| 399 | is_instancemethodobject(x) || |
| 400 | is_methodobject(x) || |
| 401 | is_classobject(x)) |
| 402 | return 1; |
| 403 | if (is_instanceobject(x)) { |
| 404 | object *call = getattr(x, "__call__"); |
| 405 | if (call == NULL) { |
| 406 | err_clear(); |
| 407 | return 0; |
| 408 | } |
| 409 | /* Could test recursively but don't, for fear of endless |
| 410 | recursion if some joker sets self.__call__ = self */ |
| 411 | DECREF(call); |
| 412 | return 1; |
| 413 | } |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 418 | /* |
| 419 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 420 | There is (and should be!) no way to create other objects of this type, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 421 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 422 | */ |
| 423 | |
Guido van Rossum | 0c182a1 | 1992-03-27 17:26:13 +0000 | [diff] [blame] | 424 | /* ARGSUSED */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 425 | static object * |
| 426 | none_repr(op) |
| 427 | object *op; |
| 428 | { |
| 429 | return newstringobject("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | static typeobject Notype = { |
| 433 | OB_HEAD_INIT(&Typetype) |
| 434 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 435 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | 0, |
| 437 | 0, |
| 438 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 439 | 0, /*tp_print*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 440 | 0, /*tp_getattr*/ |
| 441 | 0, /*tp_setattr*/ |
| 442 | 0, /*tp_compare*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 443 | (reprfunc)none_repr, /*tp_repr*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 444 | 0, /*tp_as_number*/ |
| 445 | 0, /*tp_as_sequence*/ |
| 446 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 447 | 0, /*tp_hash */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 448 | }; |
| 449 | |
| 450 | object NoObject = { |
| 451 | OB_HEAD_INIT(&Notype) |
| 452 | }; |
| 453 | |
| 454 | |
| 455 | #ifdef TRACE_REFS |
| 456 | |
| 457 | static object refchain = {&refchain, &refchain}; |
| 458 | |
| 459 | NEWREF(op) |
| 460 | object *op; |
| 461 | { |
| 462 | ref_total++; |
| 463 | op->ob_refcnt = 1; |
| 464 | op->_ob_next = refchain._ob_next; |
| 465 | op->_ob_prev = &refchain; |
| 466 | refchain._ob_next->_ob_prev = op; |
| 467 | refchain._ob_next = op; |
Sjoerd Mullender | a9c3c22 | 1993-10-11 12:54:31 +0000 | [diff] [blame] | 468 | #ifdef COUNT_ALLOCS |
| 469 | inc_count(op->ob_type); |
| 470 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | } |
| 472 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 473 | UNREF(op) |
| 474 | register object *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 475 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 476 | register object *p; |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 477 | if (op->ob_refcnt < 0) |
| 478 | fatal("UNREF negative refcnt"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 479 | if (op == &refchain || |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 480 | op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) |
| 481 | fatal("UNREF invalid object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 482 | #ifdef SLOW_UNREF_CHECK |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 483 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 484 | if (p == op) |
| 485 | break; |
| 486 | } |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 487 | if (p == &refchain) /* Not found */ |
| 488 | fatal("UNREF unknown object"); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 489 | #endif |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 490 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 491 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 492 | op->_ob_next = op->_ob_prev = NULL; |
Guido van Rossum | d8953cb | 1995-04-06 14:46:26 +0000 | [diff] [blame] | 493 | #ifdef COUNT_ALLOCS |
| 494 | op->ob_type->tp_free++; |
| 495 | #endif |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 496 | } |
| 497 | |
| 498 | DELREF(op) |
| 499 | object *op; |
| 500 | { |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 501 | destructor dealloc = op->ob_type->tp_dealloc; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 502 | UNREF(op); |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 503 | op->ob_type = NULL; |
Guido van Rossum | 9776adf | 1994-09-07 14:36:45 +0000 | [diff] [blame] | 504 | (*dealloc)(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 505 | } |
| 506 | |
| 507 | printrefs(fp) |
| 508 | FILE *fp; |
| 509 | { |
| 510 | object *op; |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 511 | fprintf(fp, "Remaining objects (except strings referenced once):\n"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 512 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
Guido van Rossum | 2e8f614 | 1992-09-03 20:32:55 +0000 | [diff] [blame] | 513 | if (op->ob_refcnt == 1 && is_stringobject(op)) |
| 514 | continue; /* Will be printed elsewhere */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 515 | fprintf(fp, "[%d] ", op->ob_refcnt); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 516 | if (printobject(op, fp, 0) != 0) |
| 517 | err_clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 518 | putc('\n', fp); |
| 519 | } |
| 520 | } |
| 521 | |
| 522 | #endif |