Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | Netherlands. |
| 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 | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 29 | #ifdef REF_DEBUG |
| 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 | |
| 37 | object * |
| 38 | newobject(tp) |
| 39 | typeobject *tp; |
| 40 | { |
| 41 | object *op = (object *) malloc(tp->tp_basicsize); |
| 42 | if (op == NULL) |
| 43 | return err_nomem(); |
| 44 | NEWREF(op); |
| 45 | op->ob_type = tp; |
| 46 | return op; |
| 47 | } |
| 48 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 49 | varobject * |
| 50 | newvarobject(tp, size) |
| 51 | typeobject *tp; |
| 52 | unsigned int size; |
| 53 | { |
| 54 | varobject *op = (varobject *) |
| 55 | malloc(tp->tp_basicsize + size * tp->tp_itemsize); |
| 56 | if (op == NULL) |
Guido van Rossum | 05ab111 | 1991-05-05 20:10:41 +0000 | [diff] [blame] | 57 | return (varobject *)err_nomem(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 58 | NEWREF(op); |
| 59 | op->ob_type = tp; |
| 60 | op->ob_size = size; |
| 61 | return op; |
| 62 | } |
| 63 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 64 | int StopPrint; /* Flag to indicate printing must be stopped */ |
| 65 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | static int prlevel; |
| 67 | |
| 68 | void |
| 69 | printobject(op, fp, flags) |
| 70 | object *op; |
| 71 | FILE *fp; |
| 72 | int flags; |
| 73 | { |
| 74 | /* Hacks to make printing a long or recursive object interruptible */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 75 | /* XXX Interrupts should leave a more permanent error */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | prlevel++; |
| 77 | if (!StopPrint && intrcheck()) { |
| 78 | fprintf(fp, "\n[print interrupted]\n"); |
| 79 | StopPrint = 1; |
| 80 | } |
| 81 | if (!StopPrint) { |
| 82 | if (op == NULL) { |
| 83 | fprintf(fp, "<nil>"); |
| 84 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 85 | else { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 86 | if (op->ob_refcnt <= 0) |
| 87 | fprintf(fp, "(refcnt %d):", op->ob_refcnt); |
| 88 | if (op->ob_type->tp_print == NULL) { |
| 89 | fprintf(fp, "<%s object at %lx>", |
| 90 | op->ob_type->tp_name, (long)op); |
| 91 | } |
| 92 | else { |
| 93 | (*op->ob_type->tp_print)(op, fp, flags); |
| 94 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | prlevel--; |
| 98 | if (prlevel == 0) |
| 99 | StopPrint = 0; |
| 100 | } |
| 101 | |
| 102 | object * |
| 103 | reprobject(v) |
| 104 | object *v; |
| 105 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 106 | object *w = NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 107 | /* Hacks to make converting a long or recursive object interruptible */ |
| 108 | prlevel++; |
| 109 | if (!StopPrint && intrcheck()) { |
| 110 | StopPrint = 1; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 111 | err_set(KeyboardInterrupt); |
| 112 | } |
| 113 | if (!StopPrint) { |
| 114 | if (v == NULL) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 115 | w = newstringobject("<NULL>"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 116 | } |
| 117 | else if (v->ob_type->tp_repr == NULL) { |
| 118 | char buf[100]; |
| 119 | sprintf(buf, "<%.80s object at %lx>", |
| 120 | v->ob_type->tp_name, (long)v); |
| 121 | w = newstringobject(buf); |
| 122 | } |
| 123 | else { |
| 124 | w = (*v->ob_type->tp_repr)(v); |
| 125 | } |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 126 | if (StopPrint) { |
| 127 | XDECREF(w); |
| 128 | w = NULL; |
| 129 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | } |
| 131 | prlevel--; |
| 132 | if (prlevel == 0) |
| 133 | StopPrint = 0; |
| 134 | return w; |
| 135 | } |
| 136 | |
| 137 | int |
| 138 | cmpobject(v, w) |
| 139 | object *v, *w; |
| 140 | { |
| 141 | typeobject *tp; |
| 142 | if (v == w) |
| 143 | return 0; |
| 144 | if (v == NULL) |
| 145 | return -1; |
| 146 | if (w == NULL) |
| 147 | return 1; |
| 148 | if ((tp = v->ob_type) != w->ob_type) |
| 149 | return strcmp(tp->tp_name, w->ob_type->tp_name); |
| 150 | if (tp->tp_compare == NULL) |
| 151 | return (v < w) ? -1 : 1; |
| 152 | return ((*tp->tp_compare)(v, w)); |
| 153 | } |
| 154 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 155 | object * |
| 156 | getattr(v, name) |
| 157 | object *v; |
| 158 | char *name; |
| 159 | { |
| 160 | if (v->ob_type->tp_getattr == NULL) { |
| 161 | err_setstr(TypeError, "attribute-less object"); |
| 162 | return NULL; |
| 163 | } |
| 164 | else { |
| 165 | return (*v->ob_type->tp_getattr)(v, name); |
| 166 | } |
| 167 | } |
| 168 | |
| 169 | int |
| 170 | setattr(v, name, w) |
| 171 | object *v; |
| 172 | char *name; |
| 173 | object *w; |
| 174 | { |
| 175 | if (v->ob_type->tp_setattr == NULL) { |
| 176 | if (v->ob_type->tp_getattr == NULL) |
| 177 | err_setstr(TypeError, "attribute-less object"); |
| 178 | else |
| 179 | err_setstr(TypeError, "object has read-only attributes"); |
Guido van Rossum | 73531a3 | 1990-12-20 23:12:40 +0000 | [diff] [blame] | 180 | return -1; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 181 | } |
| 182 | else { |
| 183 | return (*v->ob_type->tp_setattr)(v, name, w); |
| 184 | } |
| 185 | } |
| 186 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 187 | |
| 188 | /* |
| 189 | NoObject is usable as a non-NULL undefined value, used by the macro None. |
| 190 | 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] | 191 | so there is exactly one (which is indestructible, by the way). |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 192 | */ |
| 193 | |
| 194 | static void |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 195 | none_print(op, fp, flags) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 196 | object *op; |
| 197 | FILE *fp; |
| 198 | int flags; |
| 199 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 200 | fprintf(fp, "None"); |
| 201 | } |
| 202 | |
| 203 | static object * |
| 204 | none_repr(op) |
| 205 | object *op; |
| 206 | { |
| 207 | return newstringobject("None"); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | static typeobject Notype = { |
| 211 | OB_HEAD_INIT(&Typetype) |
| 212 | 0, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 213 | "None", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | 0, |
| 215 | 0, |
| 216 | 0, /*tp_dealloc*/ /*never called*/ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 217 | none_print, /*tp_print*/ |
| 218 | 0, /*tp_getattr*/ |
| 219 | 0, /*tp_setattr*/ |
| 220 | 0, /*tp_compare*/ |
| 221 | none_repr, /*tp_repr*/ |
| 222 | 0, /*tp_as_number*/ |
| 223 | 0, /*tp_as_sequence*/ |
| 224 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 225 | }; |
| 226 | |
| 227 | object NoObject = { |
| 228 | OB_HEAD_INIT(&Notype) |
| 229 | }; |
| 230 | |
| 231 | |
| 232 | #ifdef TRACE_REFS |
| 233 | |
| 234 | static object refchain = {&refchain, &refchain}; |
| 235 | |
| 236 | NEWREF(op) |
| 237 | object *op; |
| 238 | { |
| 239 | ref_total++; |
| 240 | op->ob_refcnt = 1; |
| 241 | op->_ob_next = refchain._ob_next; |
| 242 | op->_ob_prev = &refchain; |
| 243 | refchain._ob_next->_ob_prev = op; |
| 244 | refchain._ob_next = op; |
| 245 | } |
| 246 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 247 | UNREF(op) |
| 248 | register object *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 249 | { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 250 | register object *p; |
Guido van Rossum | bd3edc8 | 1990-11-02 17:49:51 +0000 | [diff] [blame] | 251 | if (op->ob_refcnt < 0) { |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 252 | fprintf(stderr, "UNREF negative refcnt\n"); |
| 253 | abort(); |
| 254 | } |
| 255 | for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { |
| 256 | if (p == op) |
| 257 | break; |
| 258 | } |
| 259 | if (p == &refchain) { /* Not found */ |
| 260 | fprintf(stderr, "UNREF unknown object\n"); |
Guido van Rossum | bd3edc8 | 1990-11-02 17:49:51 +0000 | [diff] [blame] | 261 | abort(); |
| 262 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 263 | op->_ob_next->_ob_prev = op->_ob_prev; |
| 264 | op->_ob_prev->_ob_next = op->_ob_next; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | DELREF(op) |
| 268 | object *op; |
| 269 | { |
| 270 | UNREF(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 271 | (*(op)->ob_type->tp_dealloc)(op); |
| 272 | } |
| 273 | |
| 274 | printrefs(fp) |
| 275 | FILE *fp; |
| 276 | { |
| 277 | object *op; |
| 278 | fprintf(fp, "Remaining objects:\n"); |
| 279 | for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { |
| 280 | fprintf(fp, "[%d] ", op->ob_refcnt); |
| 281 | printobject(op, fp, 0); |
| 282 | putc('\n', fp); |
| 283 | } |
| 284 | } |
| 285 | |
| 286 | #endif |