Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 2 | Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
| 3 | Amsterdam, 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 | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Class object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 28 | #include "modsupport.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 29 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 30 | #include "ceval.h" |
| 31 | |
| 32 | extern typeobject MappingInstancetype; |
| 33 | extern typeobject SequenceInstancetype; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 34 | |
| 35 | typedef struct { |
| 36 | OB_HEAD |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | object *cl_bases; /* A tuple */ |
| 38 | object *cl_methods; /* A dictionary */ |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 39 | object *cl_name; /* A string */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | } classobject; |
| 41 | |
| 42 | object * |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 43 | newclassobject(bases, methods, name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | object *bases; /* NULL or tuple of classobjects! */ |
| 45 | object *methods; |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 46 | object *name; /* String; NULL if unknown */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 47 | { |
| 48 | classobject *op; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 49 | if (bases == NULL) { |
| 50 | bases = newtupleobject(0); |
| 51 | if (bases == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 52 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 53 | } |
| 54 | else |
| 55 | INCREF(bases); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 56 | op = NEWOBJ(classobject, &Classtype); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 57 | if (op == NULL) { |
| 58 | DECREF(bases); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 59 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 60 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | op->cl_bases = bases; |
| 62 | INCREF(methods); |
| 63 | op->cl_methods = methods; |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 64 | XINCREF(name); |
| 65 | op->cl_name = name; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 66 | return (object *) op; |
| 67 | } |
| 68 | |
| 69 | /* Class methods */ |
| 70 | |
| 71 | static void |
| 72 | class_dealloc(op) |
| 73 | classobject *op; |
| 74 | { |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 75 | DECREF(op->cl_bases); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 76 | DECREF(op->cl_methods); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 77 | XDECREF(op->cl_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 78 | free((ANY *)op); |
| 79 | } |
| 80 | |
| 81 | static object * |
| 82 | class_getattr(op, name) |
| 83 | register classobject *op; |
| 84 | register char *name; |
| 85 | { |
| 86 | register object *v; |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 87 | if (strcmp(name, "__dict__") == 0) { |
| 88 | INCREF(op->cl_methods); |
| 89 | return op->cl_methods; |
| 90 | } |
| 91 | if (strcmp(name, "__bases__") == 0) { |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 92 | INCREF(op->cl_bases); |
| 93 | return op->cl_bases; |
| 94 | } |
| 95 | if (strcmp(name, "__name__") == 0) { |
| 96 | if (op->cl_name == NULL) |
| 97 | v = None; |
| 98 | else |
| 99 | v = op->cl_name; |
| 100 | INCREF(v); |
| 101 | return v; |
| 102 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | v = dictlookup(op->cl_methods, name); |
| 104 | if (v != NULL) { |
| 105 | INCREF(v); |
| 106 | return v; |
| 107 | } |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 108 | { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | int n = gettuplesize(op->cl_bases); |
| 110 | int i; |
| 111 | for (i = 0; i < n; i++) { |
Guido van Rossum | 85998fa | 1992-03-27 17:23:48 +0000 | [diff] [blame] | 112 | v = class_getattr((classobject *) |
Guido van Rossum | 8dd79cf | 1992-04-05 14:24:32 +0000 | [diff] [blame] | 113 | gettupleitem(op->cl_bases, i), name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | if (v != NULL) |
| 115 | return v; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 116 | err_clear(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | } |
| 118 | } |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 119 | err_setstr(AttributeError, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 120 | return NULL; |
| 121 | } |
| 122 | |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 123 | static int |
| 124 | class_setattr(op, name, v) |
| 125 | classobject *op; |
| 126 | char *name; |
| 127 | object *v; |
| 128 | { |
Guido van Rossum | 8dd79cf | 1992-04-05 14:24:32 +0000 | [diff] [blame] | 129 | if (name[0] == '_' && name[1] == '_') { |
| 130 | int n = strlen(name); |
| 131 | if (name[n-1] == '_' && name[n-2] == '_') { |
| 132 | err_setstr(TypeError, "read-only special attribute"); |
| 133 | return -1; |
| 134 | } |
| 135 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 136 | if (v == NULL) { |
| 137 | int rv = dictremove(op->cl_methods, name); |
| 138 | if (rv < 0) |
| 139 | err_setstr(AttributeError, |
| 140 | "delete non-existing class attribute"); |
| 141 | return rv; |
| 142 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 143 | else |
| 144 | return dictinsert(op->cl_methods, name, v); |
| 145 | } |
| 146 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 147 | typeobject Classtype = { |
| 148 | OB_HEAD_INIT(&Typetype) |
| 149 | 0, |
| 150 | "class", |
| 151 | sizeof(classobject), |
| 152 | 0, |
| 153 | class_dealloc, /*tp_dealloc*/ |
| 154 | 0, /*tp_print*/ |
| 155 | class_getattr, /*tp_getattr*/ |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 156 | class_setattr, /*tp_setattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 157 | 0, /*tp_compare*/ |
| 158 | 0, /*tp_repr*/ |
| 159 | 0, /*tp_as_number*/ |
| 160 | 0, /*tp_as_sequence*/ |
| 161 | 0, /*tp_as_mapping*/ |
| 162 | }; |
| 163 | |
| 164 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 165 | /* We're not done yet: next, we define instance objects... */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 166 | |
| 167 | typedef struct { |
| 168 | OB_HEAD |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 169 | classobject *in_class; /* The class object */ |
| 170 | object *in_attr; /* A dictionary */ |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 171 | } instanceobject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 172 | |
| 173 | object * |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 174 | newinstanceobject(class) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 175 | register object *class; |
| 176 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 177 | register instanceobject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 178 | object *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 179 | if (!is_classobject(class)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 180 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 181 | return NULL; |
| 182 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 183 | inst = NEWOBJ(instanceobject, &Instancetype); |
| 184 | if (inst == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | return NULL; |
| 186 | INCREF(class); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 187 | inst->in_class = (classobject *)class; |
| 188 | inst->in_attr = newdictobject(); |
| 189 | if (inst->in_attr == NULL) { |
| 190 | DECREF(inst); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 191 | return NULL; |
| 192 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 193 | return (object *)inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 194 | } |
| 195 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 196 | /* Instance methods */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 197 | |
| 198 | static void |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 199 | instance_dealloc(inst) |
| 200 | register instanceobject *inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 202 | DECREF(inst->in_class); |
| 203 | if (inst->in_attr != NULL) |
| 204 | DECREF(inst->in_attr); |
| 205 | free((ANY *)inst); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 206 | } |
| 207 | |
| 208 | static object * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 209 | instance_getattr(inst, name) |
| 210 | register instanceobject *inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 211 | register char *name; |
| 212 | { |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 213 | register object *v; |
| 214 | if (strcmp(name, "__dict__") == 0) { |
| 215 | INCREF(inst->in_attr); |
| 216 | return inst->in_attr; |
| 217 | } |
| 218 | if (strcmp(name, "__class__") == 0) { |
| 219 | INCREF(inst->in_class); |
| 220 | return (object *)inst->in_class; |
| 221 | } |
| 222 | v = dictlookup(inst->in_attr, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | if (v != NULL) { |
| 224 | INCREF(v); |
| 225 | return v; |
| 226 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 227 | v = class_getattr(inst->in_class, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 228 | if (v == NULL) |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 229 | return v; /* class_getattr() has set the error */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 230 | if (is_funcobject(v)) { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 231 | object *w = newinstancemethodobject(v, (object *)inst); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 232 | DECREF(v); |
| 233 | return w; |
| 234 | } |
| 235 | DECREF(v); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 236 | err_setstr(AttributeError, name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 237 | return NULL; |
| 238 | } |
| 239 | |
| 240 | static int |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 241 | instance_setattr(inst, name, v) |
| 242 | instanceobject *inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 243 | char *name; |
| 244 | object *v; |
| 245 | { |
Guido van Rossum | 8dd79cf | 1992-04-05 14:24:32 +0000 | [diff] [blame] | 246 | if (name[0] == '_' && name[1] == '_') { |
| 247 | int n = strlen(name); |
| 248 | if (name[n-1] == '_' && name[n-2] == '_') { |
| 249 | err_setstr(TypeError, "read-only special attribute"); |
| 250 | return -1; |
| 251 | } |
| 252 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 253 | if (v == NULL) { |
| 254 | int rv = dictremove(inst->in_attr, name); |
| 255 | if (rv < 0) |
| 256 | err_setstr(AttributeError, |
| 257 | "delete non-existing instance attribute"); |
| 258 | return rv; |
| 259 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 260 | else |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 261 | return dictinsert(inst->in_attr, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 262 | } |
| 263 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 264 | static object * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 265 | instance_repr(inst) |
| 266 | instanceobject *inst; |
| 267 | { |
| 268 | object *func; |
| 269 | object *res; |
| 270 | |
| 271 | func = instance_getattr(inst, "__repr__"); |
| 272 | if (func == NULL) { |
| 273 | char buf[80]; |
| 274 | err_clear(); |
| 275 | sprintf(buf, "<instance object at %lx>", (long)inst); |
| 276 | return newstringobject(buf); |
| 277 | } |
| 278 | res = call_object(func, (object *)NULL); |
| 279 | DECREF(func); |
| 280 | return res; |
| 281 | } |
| 282 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 283 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 284 | instance_compare(inst, other) |
| 285 | instanceobject *inst, *other; |
| 286 | { |
| 287 | object *func; |
| 288 | object *res; |
| 289 | int outcome; |
| 290 | |
| 291 | func = instance_getattr(inst, "__cmp__"); |
| 292 | if (func == NULL) { |
| 293 | err_clear(); |
| 294 | if (inst < other) |
| 295 | return -1; |
| 296 | if (inst > other) |
| 297 | return 1; |
| 298 | return 0; |
| 299 | } |
| 300 | res = call_object(func, (object *)other); |
| 301 | DECREF(func); |
| 302 | if (res == NULL) { |
| 303 | err_clear(); /* XXX Should report the error, bot how...??? */ |
| 304 | return 0; |
| 305 | } |
| 306 | if (is_intobject(res)) |
| 307 | outcome = getintvalue(res); |
| 308 | else |
| 309 | outcome = 0; /* XXX Should report the error, bot how...??? */ |
| 310 | DECREF(res); |
| 311 | return outcome; |
| 312 | } |
| 313 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 314 | static long |
| 315 | instance_hash(inst) |
| 316 | instanceobject *inst; |
| 317 | { |
| 318 | object *func; |
| 319 | object *res; |
| 320 | int outcome; |
| 321 | |
| 322 | func = instance_getattr(inst, "__hash__"); |
| 323 | if (func == NULL) { |
| 324 | /* If there is no __cmp__ method, we hash on the address. |
| 325 | If a __cmp__ method exists, there must be a __hash__. */ |
| 326 | err_clear(); |
| 327 | func = instance_getattr(inst, "__cmp__"); |
| 328 | if (func == NULL) |
| 329 | return (long)inst; |
| 330 | err_setstr(TypeError, "unhashable instance"); |
| 331 | return -1; |
| 332 | } |
| 333 | res = call_object(func, (object *)NULL); |
| 334 | DECREF(func); |
| 335 | if (res == NULL) |
| 336 | return -1; |
| 337 | if (is_intobject(res)) { |
| 338 | outcome = getintvalue(res); |
| 339 | if (outcome == -1) |
| 340 | outcome = -2; |
| 341 | } |
| 342 | else { |
| 343 | err_setstr(TypeError, "__hash__() should return an int"); |
| 344 | outcome = -1; |
| 345 | } |
| 346 | DECREF(res); |
| 347 | return outcome; |
| 348 | } |
| 349 | |
| 350 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 351 | instance_length(inst) |
| 352 | instanceobject *inst; |
| 353 | { |
| 354 | object *func; |
| 355 | object *res; |
| 356 | int outcome; |
| 357 | |
| 358 | func = instance_getattr(inst, "__len__"); |
| 359 | if (func == NULL) |
| 360 | return -1; |
| 361 | res = call_object(func, (object *)NULL); |
| 362 | DECREF(func); |
Guido van Rossum | d014ea6 | 1992-11-26 10:30:26 +0000 | [diff] [blame] | 363 | if (res == NULL) |
| 364 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 365 | if (is_intobject(res)) { |
| 366 | outcome = getintvalue(res); |
| 367 | if (outcome < 0) |
| 368 | err_setstr(ValueError, "__len__() should return >= 0"); |
| 369 | } |
| 370 | else { |
| 371 | err_setstr(TypeError, "__len__() should return an int"); |
| 372 | outcome = -1; |
| 373 | } |
| 374 | DECREF(res); |
| 375 | return outcome; |
| 376 | } |
| 377 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 378 | static object * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 379 | instance_subscript(inst, key) |
| 380 | instanceobject *inst; |
| 381 | object *key; |
| 382 | { |
| 383 | object *func; |
| 384 | object *arg; |
| 385 | object *res; |
| 386 | |
| 387 | func = instance_getattr(inst, "__getitem__"); |
| 388 | if (func == NULL) |
| 389 | return NULL; |
| 390 | arg = mkvalue("(O)", key); |
| 391 | if (arg == NULL) { |
| 392 | DECREF(func); |
| 393 | return NULL; |
| 394 | } |
| 395 | res = call_object(func, arg); |
| 396 | DECREF(func); |
| 397 | DECREF(arg); |
| 398 | return res; |
| 399 | } |
| 400 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 401 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 402 | instance_ass_subscript(inst, key, value) |
| 403 | instanceobject*inst; |
| 404 | object *key; |
| 405 | object *value; |
| 406 | { |
| 407 | object *func; |
| 408 | object *arg; |
| 409 | object *res; |
| 410 | |
| 411 | if (value == NULL) |
| 412 | func = instance_getattr(inst, "__delitem__"); |
| 413 | else |
| 414 | func = instance_getattr(inst, "__setitem__"); |
| 415 | if (func == NULL) |
| 416 | return -1; |
| 417 | if (value == NULL) |
| 418 | arg = mkvalue("(O)", key); |
| 419 | else |
| 420 | arg = mkvalue("(OO)", key, value); |
| 421 | if (arg == NULL) { |
| 422 | DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 423 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 424 | } |
| 425 | res = call_object(func, arg); |
| 426 | DECREF(func); |
| 427 | DECREF(arg); |
| 428 | if (res == NULL) |
| 429 | return -1; |
| 430 | DECREF(res); |
| 431 | return 0; |
| 432 | } |
| 433 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 434 | static mapping_methods instance_as_mapping = { |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 435 | instance_length, /*mp_length*/ |
| 436 | instance_subscript, /*mp_subscript*/ |
| 437 | instance_ass_subscript, /*mp_ass_subscript*/ |
| 438 | }; |
| 439 | |
| 440 | static object * |
| 441 | instance_concat(inst, other) |
| 442 | instanceobject *inst, *other; |
| 443 | { |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 444 | object *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 445 | |
| 446 | func = instance_getattr(inst, "__add__"); |
| 447 | if (func == NULL) |
| 448 | return NULL; |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 449 | arg = mkvalue("(O)", other); |
| 450 | if (arg == NULL) { |
| 451 | DECREF(func); |
| 452 | return NULL; |
| 453 | } |
| 454 | res = call_object(func, arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 455 | DECREF(func); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 456 | DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 457 | return res; |
| 458 | } |
| 459 | |
| 460 | static object * |
| 461 | instance_repeat(inst, count) |
| 462 | instanceobject *inst; |
| 463 | int count; |
| 464 | { |
| 465 | object *func, *arg, *res; |
| 466 | |
| 467 | func = instance_getattr(inst, "__mul__"); |
| 468 | if (func == NULL) |
| 469 | return NULL; |
| 470 | arg = newintobject((long)count); |
| 471 | if (arg == NULL) { |
| 472 | DECREF(func); |
| 473 | return NULL; |
| 474 | } |
| 475 | res = call_object(func, arg); |
| 476 | DECREF(func); |
| 477 | DECREF(arg); |
| 478 | return res; |
| 479 | } |
| 480 | |
| 481 | static object * |
| 482 | instance_item(inst, i) |
| 483 | instanceobject *inst; |
| 484 | int i; |
| 485 | { |
| 486 | object *func, *arg, *res; |
| 487 | |
| 488 | func = instance_getattr(inst, "__getitem__"); |
| 489 | if (func == NULL) |
| 490 | return NULL; |
| 491 | arg = newintobject((long)i); |
| 492 | if (arg == NULL) { |
| 493 | DECREF(func); |
| 494 | return NULL; |
| 495 | } |
| 496 | res = call_object(func, arg); |
| 497 | DECREF(func); |
| 498 | DECREF(arg); |
| 499 | return res; |
| 500 | } |
| 501 | |
| 502 | static object * |
| 503 | instance_slice(inst, i, j) |
| 504 | instanceobject *inst; |
| 505 | int i, j; |
| 506 | { |
| 507 | object *func, *arg, *res; |
| 508 | |
| 509 | func = instance_getattr(inst, "__getslice__"); |
| 510 | if (func == NULL) |
| 511 | return NULL; |
| 512 | arg = mkvalue("(ii)", i, j); |
| 513 | if (arg == NULL) { |
| 514 | DECREF(func); |
| 515 | return NULL; |
| 516 | } |
| 517 | res = call_object(func, arg); |
| 518 | DECREF(func); |
| 519 | DECREF(arg); |
| 520 | return res; |
| 521 | } |
| 522 | |
| 523 | static int |
| 524 | instance_ass_item(inst, i, item) |
| 525 | instanceobject *inst; |
| 526 | int i; |
| 527 | object *item; |
| 528 | { |
| 529 | object *func, *arg, *res; |
| 530 | |
| 531 | if (item == NULL) |
| 532 | func = instance_getattr(inst, "__delitem__"); |
| 533 | else |
| 534 | func = instance_getattr(inst, "__setitem__"); |
| 535 | if (func == NULL) |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 536 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 537 | if (item == NULL) |
| 538 | arg = mkvalue("i", i); |
| 539 | else |
| 540 | arg = mkvalue("(iO)", i, item); |
| 541 | if (arg == NULL) { |
| 542 | DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 543 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 544 | } |
| 545 | res = call_object(func, arg); |
| 546 | DECREF(func); |
| 547 | DECREF(arg); |
| 548 | if (res == NULL) |
| 549 | return -1; |
| 550 | DECREF(res); |
| 551 | return 0; |
| 552 | } |
| 553 | |
| 554 | static int |
| 555 | instance_ass_slice(inst, i, j, value) |
| 556 | instanceobject *inst; |
| 557 | int i, j; |
| 558 | object *value; |
| 559 | { |
| 560 | object *func, *arg, *res; |
| 561 | |
| 562 | if (value == NULL) |
| 563 | func = instance_getattr(inst, "__delslice__"); |
| 564 | else |
| 565 | func = instance_getattr(inst, "__setslice__"); |
| 566 | if (func == NULL) |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 567 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 568 | if (value == NULL) |
| 569 | arg = mkvalue("(ii)", i, j); |
| 570 | else |
| 571 | arg = mkvalue("(iiO)", i, j, value); |
| 572 | if (arg == NULL) { |
| 573 | DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 574 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 575 | } |
| 576 | res = call_object(func, arg); |
| 577 | DECREF(func); |
| 578 | DECREF(arg); |
| 579 | if (res == NULL) |
| 580 | return -1; |
| 581 | DECREF(res); |
| 582 | return 0; |
| 583 | } |
| 584 | |
| 585 | static sequence_methods instance_as_sequence = { |
| 586 | instance_length, /*sq_length*/ |
| 587 | instance_concat, /*sq_concat*/ |
| 588 | instance_repeat, /*sq_repeat*/ |
| 589 | instance_item, /*sq_item*/ |
| 590 | instance_slice, /*sq_slice*/ |
| 591 | instance_ass_item, /*sq_ass_item*/ |
| 592 | instance_ass_slice, /*sq_ass_slice*/ |
| 593 | }; |
| 594 | |
| 595 | static object * |
| 596 | generic_binary_op(self, other, methodname) |
| 597 | instanceobject *self; |
| 598 | object *other; |
| 599 | char *methodname; |
| 600 | { |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 601 | object *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 602 | |
| 603 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 604 | return NULL; |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 605 | arg = mkvalue("O", other); |
| 606 | if (arg == NULL) { |
| 607 | DECREF(func); |
| 608 | return NULL; |
| 609 | } |
| 610 | res = call_object(func, arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 611 | DECREF(func); |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 612 | DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 613 | return res; |
| 614 | } |
| 615 | |
| 616 | static object * |
| 617 | generic_unary_op(self, methodname) |
| 618 | instanceobject *self; |
| 619 | char *methodname; |
| 620 | { |
| 621 | object *func, *res; |
| 622 | |
| 623 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 624 | return NULL; |
| 625 | res = call_object(func, (object *)NULL); |
| 626 | DECREF(func); |
| 627 | return res; |
| 628 | } |
| 629 | |
| 630 | #define BINARY(funcname, methodname) \ |
| 631 | static object * funcname(self, other) instanceobject *self; object *other; { \ |
| 632 | return generic_binary_op(self, other, methodname); \ |
| 633 | } |
| 634 | |
| 635 | #define UNARY(funcname, methodname) \ |
| 636 | static object *funcname(self) instanceobject *self; { \ |
| 637 | return generic_unary_op(self, methodname); \ |
| 638 | } |
| 639 | |
| 640 | BINARY(instance_add, "__add__") |
| 641 | BINARY(instance_sub, "__sub__") |
| 642 | BINARY(instance_mul, "__mul__") |
| 643 | BINARY(instance_div, "__div__") |
| 644 | BINARY(instance_mod, "__mod__") |
| 645 | BINARY(instance_divmod, "__divmod__") |
| 646 | BINARY(instance_pow, "__pow__") |
| 647 | UNARY(instance_neg, "__neg__") |
| 648 | UNARY(instance_pos, "__pos__") |
| 649 | UNARY(instance_abs, "__abs__") |
| 650 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 651 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 652 | instance_nonzero(self) |
| 653 | instanceobject *self; |
| 654 | { |
| 655 | object *func, *res; |
| 656 | long outcome; |
| 657 | |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 658 | if ((func = instance_getattr(self, "__nonzero__")) == NULL) { |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 659 | err_clear(); |
Guido van Rossum | 9e51f9b | 1993-02-12 16:29:05 +0000 | [diff] [blame] | 660 | if ((func = instance_getattr(self, "__len__")) == NULL) { |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 661 | err_clear(); |
| 662 | /* Fall back to the default behavior: |
| 663 | all instances are nonzero */ |
| 664 | return 1; |
| 665 | } |
| 666 | } |
| 667 | res = call_object(func, (object *)NULL); |
| 668 | DECREF(func); |
| 669 | if (res == NULL) |
| 670 | return -1; |
| 671 | if (!is_intobject(res)) { |
| 672 | DECREF(res); |
| 673 | err_setstr(TypeError, "__nonzero__ should return an int"); |
| 674 | return -1; |
| 675 | } |
| 676 | outcome = getintvalue(res); |
| 677 | DECREF(res); |
| 678 | if (outcome < 0) { |
| 679 | err_setstr(ValueError, "__nonzero__ should return >= 0"); |
| 680 | return -1; |
| 681 | } |
| 682 | return outcome > 0; |
| 683 | } |
| 684 | |
| 685 | UNARY(instance_invert, "__invert__") |
| 686 | BINARY(instance_lshift, "__lshift__") |
| 687 | BINARY(instance_rshift, "__rshift__") |
| 688 | BINARY(instance_and, "__and__") |
| 689 | BINARY(instance_xor, "__xor__") |
| 690 | BINARY(instance_or, "__or__") |
| 691 | |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 692 | static int |
| 693 | instance_coerce(pv, pw) |
| 694 | object **pv, **pw; |
| 695 | { |
| 696 | object *v = *pv; |
| 697 | object *w = *pw; |
| 698 | object *func; |
| 699 | object *res; |
| 700 | int outcome; |
| 701 | |
| 702 | if (!is_instanceobject(v)) |
| 703 | return 1; /* XXX shouldn't be possible */ |
| 704 | func = instance_getattr((instanceobject *)v, "__coerce__"); |
| 705 | if (func == NULL) { |
| 706 | err_clear(); |
| 707 | return 1; |
| 708 | } |
| 709 | res = call_object(func, w); |
| 710 | if (res == NULL) |
| 711 | return -1; |
| 712 | if (res == None) { |
| 713 | DECREF(res); |
| 714 | return 1; |
| 715 | } |
| 716 | outcome = getargs(res, "(OO)", &v, &w); |
| 717 | if (!outcome || v->ob_type != w->ob_type || |
| 718 | v->ob_type->tp_as_number == NULL) { |
| 719 | DECREF(res); |
| 720 | err_setstr(TypeError, "bad __coerce__ result"); |
| 721 | return -1; |
| 722 | } |
| 723 | INCREF(v); |
| 724 | INCREF(w); |
| 725 | DECREF(res); |
| 726 | *pv = v; |
| 727 | *pw = w; |
| 728 | return 0; |
| 729 | } |
| 730 | |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 731 | UNARY(instance_int, "__int__") |
| 732 | UNARY(instance_long, "__long__") |
| 733 | UNARY(instance_float, "__float__") |
| 734 | UNARY(instance_oct, "__oct__") |
| 735 | UNARY(instance_hex, "__hex__") |
| 736 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 737 | static number_methods instance_as_number = { |
| 738 | instance_add, /*nb_add*/ |
| 739 | instance_sub, /*nb_subtract*/ |
| 740 | instance_mul, /*nb_multiply*/ |
| 741 | instance_div, /*nb_divide*/ |
| 742 | instance_mod, /*nb_remainder*/ |
| 743 | instance_divmod, /*nb_divmod*/ |
| 744 | instance_pow, /*nb_power*/ |
| 745 | instance_neg, /*nb_negative*/ |
| 746 | instance_pos, /*nb_positive*/ |
| 747 | instance_abs, /*nb_absolute*/ |
| 748 | instance_nonzero, /*nb_nonzero*/ |
| 749 | instance_invert, /*nb_invert*/ |
| 750 | instance_lshift, /*nb_lshift*/ |
| 751 | instance_rshift, /*nb_rshift*/ |
| 752 | instance_and, /*nb_and*/ |
| 753 | instance_xor, /*nb_xor*/ |
| 754 | instance_or, /*nb_or*/ |
Guido van Rossum | e6eefc2 | 1992-08-14 12:06:52 +0000 | [diff] [blame] | 755 | instance_coerce, /*nb_coerce*/ |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 756 | instance_int, /*nb_int*/ |
| 757 | instance_long, /*nb_long*/ |
| 758 | instance_float, /*nb_float*/ |
| 759 | instance_oct, /*nb_oct*/ |
| 760 | instance_hex, /*nb_hex*/ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 761 | }; |
| 762 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 763 | typeobject Instancetype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 764 | OB_HEAD_INIT(&Typetype) |
| 765 | 0, |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 766 | "instance", |
| 767 | sizeof(instanceobject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 768 | 0, |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 769 | instance_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 770 | 0, /*tp_print*/ |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 771 | instance_getattr, /*tp_getattr*/ |
| 772 | instance_setattr, /*tp_setattr*/ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 773 | instance_compare, /*tp_compare*/ |
| 774 | instance_repr, /*tp_repr*/ |
| 775 | &instance_as_number, /*tp_as_number*/ |
| 776 | &instance_as_sequence, /*tp_as_sequence*/ |
| 777 | &instance_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 778 | instance_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 779 | }; |
| 780 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 781 | static object * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 782 | instance_convert(inst, methodname) |
| 783 | object *inst; |
| 784 | char *methodname; |
| 785 | { |
| 786 | return generic_unary_op((instanceobject *)inst, methodname); |
| 787 | } |
| 788 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 789 | |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 790 | /* And finally, here are instance method objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 791 | |
| 792 | typedef struct { |
| 793 | OB_HEAD |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 794 | object *im_func; /* The method function */ |
| 795 | object *im_self; /* The object to which this applies */ |
| 796 | } instancemethodobject; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 797 | |
| 798 | object * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 799 | newinstancemethodobject(func, self) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 800 | object *func; |
| 801 | object *self; |
| 802 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 803 | register instancemethodobject *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | if (!is_funcobject(func)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 805 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 806 | return NULL; |
| 807 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 808 | im = NEWOBJ(instancemethodobject, &Instancemethodtype); |
| 809 | if (im == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 810 | return NULL; |
| 811 | INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 812 | im->im_func = func; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 813 | INCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 814 | im->im_self = self; |
| 815 | return (object *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 816 | } |
| 817 | |
| 818 | object * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 819 | instancemethodgetfunc(im) |
| 820 | register object *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 821 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 822 | if (!is_instancemethodobject(im)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 823 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 824 | return NULL; |
| 825 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 826 | return ((instancemethodobject *)im)->im_func; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 827 | } |
| 828 | |
| 829 | object * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 830 | instancemethodgetself(im) |
| 831 | register object *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 832 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 833 | if (!is_instancemethodobject(im)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 834 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 835 | return NULL; |
| 836 | } |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 837 | return ((instancemethodobject *)im)->im_self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 838 | } |
| 839 | |
| 840 | /* Class method methods */ |
| 841 | |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 842 | #define OFF(x) offsetof(instancemethodobject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 843 | |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 844 | static struct memberlist instancemethod_memberlist[] = { |
| 845 | {"im_func", T_OBJECT, OFF(im_func)}, |
| 846 | {"im_self", T_OBJECT, OFF(im_self)}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 847 | {NULL} /* Sentinel */ |
| 848 | }; |
| 849 | |
| 850 | static object * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 851 | instancemethod_getattr(im, name) |
| 852 | register instancemethodobject *im; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 853 | char *name; |
| 854 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 855 | return getmember((char *)im, instancemethod_memberlist, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 856 | } |
| 857 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 858 | static void |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 859 | instancemethod_dealloc(im) |
| 860 | register instancemethodobject *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 861 | { |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 862 | DECREF(im->im_func); |
| 863 | DECREF(im->im_self); |
| 864 | free((ANY *)im); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 865 | } |
| 866 | |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 867 | static int |
| 868 | instancemethod_compare(a, b) |
| 869 | instancemethodobject *a, *b; |
| 870 | { |
| 871 | int cmp = cmpobject(a->im_self, b->im_self); |
| 872 | if (cmp == 0) |
| 873 | cmp = cmpobject(a->im_func, b->im_func); |
| 874 | return cmp; |
| 875 | } |
| 876 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 877 | static long |
| 878 | instancemethod_hash(a) |
| 879 | instancemethodobject *a; |
| 880 | { |
| 881 | long x, y; |
| 882 | x = hashobject(a->im_self); |
| 883 | if (x == -1) |
| 884 | return -1; |
| 885 | y = hashobject(a->im_func); |
| 886 | if (y == -1) |
| 887 | return -1; |
| 888 | return x ^ y; |
| 889 | } |
| 890 | |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 891 | typeobject Instancemethodtype = { |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 892 | OB_HEAD_INIT(&Typetype) |
| 893 | 0, |
Guido van Rossum | 569fce7 | 1991-04-16 08:38:43 +0000 | [diff] [blame] | 894 | "instance method", |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 895 | sizeof(instancemethodobject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 896 | 0, |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 897 | instancemethod_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 898 | 0, /*tp_print*/ |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 899 | instancemethod_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 900 | 0, /*tp_setattr*/ |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 901 | instancemethod_compare, /*tp_compare*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 902 | 0, /*tp_repr*/ |
| 903 | 0, /*tp_as_number*/ |
| 904 | 0, /*tp_as_sequence*/ |
| 905 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame^] | 906 | instancemethod_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 907 | }; |