blob: 823e397f7955e4aa0aeee4432047260da5d9592d [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossume5372401993-03-16 12:15:04 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Class object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +000028#include "modsupport.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000029#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +000030#include "ceval.h"
31
32extern typeobject MappingInstancetype;
33extern typeobject SequenceInstancetype;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000034
35typedef struct {
36 OB_HEAD
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037 object *cl_bases; /* A tuple */
38 object *cl_methods; /* A dictionary */
Guido van Rossum94308391991-10-20 20:11:48 +000039 object *cl_name; /* A string */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000040} classobject;
41
42object *
Guido van Rossum94308391991-10-20 20:11:48 +000043newclassobject(bases, methods, name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 object *bases; /* NULL or tuple of classobjects! */
45 object *methods;
Guido van Rossum94308391991-10-20 20:11:48 +000046 object *name; /* String; NULL if unknown */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
48 classobject *op;
Guido van Rossume2966a61991-12-10 13:53:23 +000049 if (bases == NULL) {
50 bases = newtupleobject(0);
51 if (bases == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +000052 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000053 }
54 else
55 INCREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000056 op = NEWOBJ(classobject, &Classtype);
Guido van Rossume2966a61991-12-10 13:53:23 +000057 if (op == NULL) {
58 DECREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000059 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000060 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 op->cl_bases = bases;
62 INCREF(methods);
63 op->cl_methods = methods;
Guido van Rossum94308391991-10-20 20:11:48 +000064 XINCREF(name);
65 op->cl_name = name;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000066 return (object *) op;
67}
68
69/* Class methods */
70
71static void
72class_dealloc(op)
73 classobject *op;
74{
Guido van Rossume2966a61991-12-10 13:53:23 +000075 DECREF(op->cl_bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000076 DECREF(op->cl_methods);
Guido van Rossum94308391991-10-20 20:11:48 +000077 XDECREF(op->cl_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000078 free((ANY *)op);
79}
80
81static object *
82class_getattr(op, name)
83 register classobject *op;
84 register char *name;
85{
86 register object *v;
Guido van Rossum94308391991-10-20 20:11:48 +000087 if (strcmp(name, "__dict__") == 0) {
88 INCREF(op->cl_methods);
89 return op->cl_methods;
90 }
91 if (strcmp(name, "__bases__") == 0) {
Guido van Rossum94308391991-10-20 20:11:48 +000092 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 Rossum85a5fbb1990-10-14 12:07:46 +0000103 v = dictlookup(op->cl_methods, name);
104 if (v != NULL) {
105 INCREF(v);
106 return v;
107 }
Guido van Rossume2966a61991-12-10 13:53:23 +0000108 {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 int n = gettuplesize(op->cl_bases);
110 int i;
111 for (i = 0; i < n; i++) {
Guido van Rossum85998fa1992-03-27 17:23:48 +0000112 v = class_getattr((classobject *)
Guido van Rossum8dd79cf1992-04-05 14:24:32 +0000113 gettupleitem(op->cl_bases, i), name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 if (v != NULL)
115 return v;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000116 err_clear();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 }
118 }
Guido van Rossume2966a61991-12-10 13:53:23 +0000119 err_setstr(AttributeError, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 return NULL;
121}
122
Guido van Rossum94308391991-10-20 20:11:48 +0000123static int
124class_setattr(op, name, v)
125 classobject *op;
126 char *name;
127 object *v;
128{
Guido van Rossum8dd79cf1992-04-05 14:24:32 +0000129 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 Rossum94472a01992-09-04 09:45:18 +0000136 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 Rossum94308391991-10-20 20:11:48 +0000143 else
144 return dictinsert(op->cl_methods, name, v);
145}
146
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000147typeobject 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 Rossum94308391991-10-20 20:11:48 +0000156 class_setattr, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 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 Rossum21ed88c1991-04-04 10:42:10 +0000165/* We're not done yet: next, we define instance objects... */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000166
167typedef struct {
168 OB_HEAD
Guido van Rossume8122f11991-05-05 20:03:07 +0000169 classobject *in_class; /* The class object */
170 object *in_attr; /* A dictionary */
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000171} instanceobject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000172
173object *
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000174newinstanceobject(class)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000175 register object *class;
176{
Guido van Rossume8122f11991-05-05 20:03:07 +0000177 register instanceobject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000178 object *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000179 if (!is_classobject(class)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000180 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000181 return NULL;
182 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000183 inst = NEWOBJ(instanceobject, &Instancetype);
184 if (inst == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185 return NULL;
186 INCREF(class);
Guido van Rossume8122f11991-05-05 20:03:07 +0000187 inst->in_class = (classobject *)class;
188 inst->in_attr = newdictobject();
189 if (inst->in_attr == NULL) {
190 DECREF(inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000191 return NULL;
192 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000193 return (object *)inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000194}
195
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000196/* Instance methods */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000197
198static void
Guido van Rossume8122f11991-05-05 20:03:07 +0000199instance_dealloc(inst)
200 register instanceobject *inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000201{
Guido van Rossume8122f11991-05-05 20:03:07 +0000202 DECREF(inst->in_class);
203 if (inst->in_attr != NULL)
204 DECREF(inst->in_attr);
205 free((ANY *)inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000206}
207
208static object *
Guido van Rossume8122f11991-05-05 20:03:07 +0000209instance_getattr(inst, name)
210 register instanceobject *inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000211 register char *name;
212{
Guido van Rossum94308391991-10-20 20:11:48 +0000213 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 Rossum85a5fbb1990-10-14 12:07:46 +0000223 if (v != NULL) {
224 INCREF(v);
225 return v;
226 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000227 v = class_getattr(inst->in_class, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000228 if (v == NULL)
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000229 return v; /* class_getattr() has set the error */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000230 if (is_funcobject(v)) {
Guido van Rossume8122f11991-05-05 20:03:07 +0000231 object *w = newinstancemethodobject(v, (object *)inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000232 DECREF(v);
233 return w;
234 }
235 DECREF(v);
Guido van Rossume2966a61991-12-10 13:53:23 +0000236 err_setstr(AttributeError, name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237 return NULL;
238}
239
240static int
Guido van Rossume8122f11991-05-05 20:03:07 +0000241instance_setattr(inst, name, v)
242 instanceobject *inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000243 char *name;
244 object *v;
245{
Guido van Rossum8dd79cf1992-04-05 14:24:32 +0000246 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 Rossum94472a01992-09-04 09:45:18 +0000253 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 Rossum85a5fbb1990-10-14 12:07:46 +0000260 else
Guido van Rossume8122f11991-05-05 20:03:07 +0000261 return dictinsert(inst->in_attr, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000262}
263
Guido van Rossum9bfef441993-03-29 10:43:31 +0000264static object *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000265instance_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 Rossum9bfef441993-03-29 10:43:31 +0000283static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000284instance_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 Rossum9bfef441993-03-29 10:43:31 +0000314static long
315instance_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
350static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000351instance_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 Rossumd014ea61992-11-26 10:30:26 +0000363 if (res == NULL)
364 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000365 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 Rossum9bfef441993-03-29 10:43:31 +0000378static object *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000379instance_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 Rossum9bfef441993-03-29 10:43:31 +0000401static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000402instance_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 Rossum6d946f91992-08-14 13:49:30 +0000423 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000424 }
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 Rossum9bfef441993-03-29 10:43:31 +0000434static mapping_methods instance_as_mapping = {
Guido van Rossum04691fc1992-08-12 15:35:34 +0000435 instance_length, /*mp_length*/
436 instance_subscript, /*mp_subscript*/
437 instance_ass_subscript, /*mp_ass_subscript*/
438};
439
440static object *
441instance_concat(inst, other)
442 instanceobject *inst, *other;
443{
Guido van Rossume6eefc21992-08-14 12:06:52 +0000444 object *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000445
446 func = instance_getattr(inst, "__add__");
447 if (func == NULL)
448 return NULL;
Guido van Rossume6eefc21992-08-14 12:06:52 +0000449 arg = mkvalue("(O)", other);
450 if (arg == NULL) {
451 DECREF(func);
452 return NULL;
453 }
454 res = call_object(func, arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000455 DECREF(func);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000456 DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000457 return res;
458}
459
460static object *
461instance_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
481static object *
482instance_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
502static object *
503instance_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
523static int
524instance_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 Rossum6d946f91992-08-14 13:49:30 +0000536 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000537 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 Rossum6d946f91992-08-14 13:49:30 +0000543 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000544 }
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
554static int
555instance_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 Rossum6d946f91992-08-14 13:49:30 +0000567 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000568 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 Rossum6d946f91992-08-14 13:49:30 +0000574 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000575 }
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
585static 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
595static object *
596generic_binary_op(self, other, methodname)
597 instanceobject *self;
598 object *other;
599 char *methodname;
600{
Guido van Rossume6eefc21992-08-14 12:06:52 +0000601 object *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000602
603 if ((func = instance_getattr(self, methodname)) == NULL)
604 return NULL;
Guido van Rossume6eefc21992-08-14 12:06:52 +0000605 arg = mkvalue("O", other);
606 if (arg == NULL) {
607 DECREF(func);
608 return NULL;
609 }
610 res = call_object(func, arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000611 DECREF(func);
Guido van Rossume6eefc21992-08-14 12:06:52 +0000612 DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000613 return res;
614}
615
616static object *
617generic_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) \
631static object * funcname(self, other) instanceobject *self; object *other; { \
632 return generic_binary_op(self, other, methodname); \
633}
634
635#define UNARY(funcname, methodname) \
636static object *funcname(self) instanceobject *self; { \
637 return generic_unary_op(self, methodname); \
638}
639
640BINARY(instance_add, "__add__")
641BINARY(instance_sub, "__sub__")
642BINARY(instance_mul, "__mul__")
643BINARY(instance_div, "__div__")
644BINARY(instance_mod, "__mod__")
645BINARY(instance_divmod, "__divmod__")
646BINARY(instance_pow, "__pow__")
647UNARY(instance_neg, "__neg__")
648UNARY(instance_pos, "__pos__")
649UNARY(instance_abs, "__abs__")
650
Guido van Rossum9bfef441993-03-29 10:43:31 +0000651static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000652instance_nonzero(self)
653 instanceobject *self;
654{
655 object *func, *res;
656 long outcome;
657
Guido van Rossum9e51f9b1993-02-12 16:29:05 +0000658 if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
Guido van Rossum04691fc1992-08-12 15:35:34 +0000659 err_clear();
Guido van Rossum9e51f9b1993-02-12 16:29:05 +0000660 if ((func = instance_getattr(self, "__len__")) == NULL) {
Guido van Rossum04691fc1992-08-12 15:35:34 +0000661 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
685UNARY(instance_invert, "__invert__")
686BINARY(instance_lshift, "__lshift__")
687BINARY(instance_rshift, "__rshift__")
688BINARY(instance_and, "__and__")
689BINARY(instance_xor, "__xor__")
690BINARY(instance_or, "__or__")
691
Guido van Rossume6eefc21992-08-14 12:06:52 +0000692static int
693instance_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 Rossum1899c2e1992-09-12 11:09:23 +0000731UNARY(instance_int, "__int__")
732UNARY(instance_long, "__long__")
733UNARY(instance_float, "__float__")
734UNARY(instance_oct, "__oct__")
735UNARY(instance_hex, "__hex__")
736
Guido van Rossum04691fc1992-08-12 15:35:34 +0000737static 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 Rossume6eefc21992-08-14 12:06:52 +0000755 instance_coerce, /*nb_coerce*/
Guido van Rossum1899c2e1992-09-12 11:09:23 +0000756 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 Rossum04691fc1992-08-12 15:35:34 +0000761};
762
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000763typeobject Instancetype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000764 OB_HEAD_INIT(&Typetype)
765 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000766 "instance",
767 sizeof(instanceobject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000768 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000769 instance_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000770 0, /*tp_print*/
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000771 instance_getattr, /*tp_getattr*/
772 instance_setattr, /*tp_setattr*/
Guido van Rossum04691fc1992-08-12 15:35:34 +0000773 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 Rossum9bfef441993-03-29 10:43:31 +0000778 instance_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000779};
780
Guido van Rossum9bfef441993-03-29 10:43:31 +0000781static object *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000782instance_convert(inst, methodname)
783 object *inst;
784 char *methodname;
785{
786 return generic_unary_op((instanceobject *)inst, methodname);
787}
788
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000789
Guido van Rossum94308391991-10-20 20:11:48 +0000790/* And finally, here are instance method objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000791
792typedef struct {
793 OB_HEAD
Guido van Rossume8122f11991-05-05 20:03:07 +0000794 object *im_func; /* The method function */
795 object *im_self; /* The object to which this applies */
796} instancemethodobject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000797
798object *
Guido van Rossume8122f11991-05-05 20:03:07 +0000799newinstancemethodobject(func, self)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000800 object *func;
801 object *self;
802{
Guido van Rossume8122f11991-05-05 20:03:07 +0000803 register instancemethodobject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804 if (!is_funcobject(func)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000805 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000806 return NULL;
807 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000808 im = NEWOBJ(instancemethodobject, &Instancemethodtype);
809 if (im == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000810 return NULL;
811 INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +0000812 im->im_func = func;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000813 INCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +0000814 im->im_self = self;
815 return (object *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000816}
817
818object *
Guido van Rossume8122f11991-05-05 20:03:07 +0000819instancemethodgetfunc(im)
820 register object *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000821{
Guido van Rossume8122f11991-05-05 20:03:07 +0000822 if (!is_instancemethodobject(im)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000823 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000824 return NULL;
825 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000826 return ((instancemethodobject *)im)->im_func;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000827}
828
829object *
Guido van Rossume8122f11991-05-05 20:03:07 +0000830instancemethodgetself(im)
831 register object *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000832{
Guido van Rossume8122f11991-05-05 20:03:07 +0000833 if (!is_instancemethodobject(im)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +0000834 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000835 return NULL;
836 }
Guido van Rossume8122f11991-05-05 20:03:07 +0000837 return ((instancemethodobject *)im)->im_self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000838}
839
840/* Class method methods */
841
Guido van Rossume8122f11991-05-05 20:03:07 +0000842#define OFF(x) offsetof(instancemethodobject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000843
Guido van Rossume8122f11991-05-05 20:03:07 +0000844static struct memberlist instancemethod_memberlist[] = {
845 {"im_func", T_OBJECT, OFF(im_func)},
846 {"im_self", T_OBJECT, OFF(im_self)},
Guido van Rossum3f5da241990-12-20 15:06:42 +0000847 {NULL} /* Sentinel */
848};
849
850static object *
Guido van Rossume8122f11991-05-05 20:03:07 +0000851instancemethod_getattr(im, name)
852 register instancemethodobject *im;
Guido van Rossum3f5da241990-12-20 15:06:42 +0000853 char *name;
854{
Guido van Rossume8122f11991-05-05 20:03:07 +0000855 return getmember((char *)im, instancemethod_memberlist, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000856}
857
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000858static void
Guido van Rossume8122f11991-05-05 20:03:07 +0000859instancemethod_dealloc(im)
860 register instancemethodobject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000861{
Guido van Rossume8122f11991-05-05 20:03:07 +0000862 DECREF(im->im_func);
863 DECREF(im->im_self);
864 free((ANY *)im);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000865}
866
Guido van Rossumebc8c511992-09-03 20:39:51 +0000867static int
868instancemethod_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 Rossum9bfef441993-03-29 10:43:31 +0000877static long
878instancemethod_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 Rossume8122f11991-05-05 20:03:07 +0000891typeobject Instancemethodtype = {
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000892 OB_HEAD_INIT(&Typetype)
893 0,
Guido van Rossum569fce71991-04-16 08:38:43 +0000894 "instance method",
Guido van Rossume8122f11991-05-05 20:03:07 +0000895 sizeof(instancemethodobject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000896 0,
Guido van Rossume8122f11991-05-05 20:03:07 +0000897 instancemethod_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000898 0, /*tp_print*/
Guido van Rossume8122f11991-05-05 20:03:07 +0000899 instancemethod_getattr, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000900 0, /*tp_setattr*/
Guido van Rossumebc8c511992-09-03 20:39:51 +0000901 instancemethod_compare, /*tp_compare*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000902 0, /*tp_repr*/
903 0, /*tp_as_number*/
904 0, /*tp_as_sequence*/
905 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000906 instancemethod_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000907};