This commit was manufactured by cvs2svn to create tag 'mac102'.
[python/dscho.git] / Objects / classobject.c
blob35ce0b1710621099e5e266555a12e55b81feb82e
1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
5 All Rights Reserved
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.
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.
23 ******************************************************************/
25 /* Class object implementation */
27 #include "allobjects.h"
28 #include "modsupport.h"
29 #include "structmember.h"
30 #include "ceval.h"
32 object *
33 newclassobject(bases, dict, name)
34 object *bases; /* NULL or tuple of classobjects! */
35 object *dict;
36 object *name; /* String; NULL if unknown */
38 int pos;
39 object *key, *value;
40 classobject *op;
41 if (bases == NULL) {
42 bases = newtupleobject(0);
43 if (bases == NULL)
44 return NULL;
46 else
47 INCREF(bases);
48 op = NEWOBJ(classobject, &Classtype);
49 if (op == NULL) {
50 DECREF(bases);
51 return NULL;
53 op->cl_bases = bases;
54 INCREF(dict);
55 op->cl_dict = dict;
56 XINCREF(name);
57 op->cl_name = name;
58 pos = 0;
59 while (mappinggetnext(dict, &pos, &key, &value)) {
60 if (is_accessobject(value))
61 setaccessowner(value, (object *)op);
63 return (object *) op;
66 /* Class methods */
68 static void
69 class_dealloc(op)
70 classobject *op;
72 DECREF(op->cl_bases);
73 DECREF(op->cl_dict);
74 XDECREF(op->cl_name);
75 free((ANY *)op);
78 static object *
79 class_lookup(cp, name, pclass)
80 classobject *cp;
81 char *name;
82 classobject **pclass;
84 int i, n;
85 object *value = dictlookup(cp->cl_dict, name);
86 if (value != NULL) {
87 *pclass = cp;
88 return value;
90 n = gettuplesize(cp->cl_bases);
91 for (i = 0; i < n; i++) {
92 object *v = class_lookup((classobject *)
93 gettupleitem(cp->cl_bases, i), name, pclass);
94 if (v != NULL)
95 return v;
97 return NULL;
100 static object *
101 class_getattr(op, name)
102 register classobject *op;
103 register char *name;
105 register object *v;
106 classobject *class;
107 if (strcmp(name, "__dict__") == 0) {
108 INCREF(op->cl_dict);
109 return op->cl_dict;
111 if (strcmp(name, "__bases__") == 0) {
112 INCREF(op->cl_bases);
113 return op->cl_bases;
115 if (strcmp(name, "__name__") == 0) {
116 if (op->cl_name == NULL)
117 v = None;
118 else
119 v = op->cl_name;
120 INCREF(v);
121 return v;
123 v = class_lookup(op, name, &class);
124 if (v == NULL) {
125 err_setstr(AttributeError, name);
126 return NULL;
128 if (is_accessobject(v)) {
129 v = getaccessvalue(v, getowner());
130 if (v == NULL)
131 return NULL;
133 else
134 INCREF(v);
135 if (is_funcobject(v)) {
136 object *w = newinstancemethodobject(v, (object *)NULL,
137 (object *)class);
138 DECREF(v);
139 v = w;
141 return v;
144 static int
145 class_setattr(op, name, v)
146 classobject *op;
147 char *name;
148 object *v;
150 object *ac;
151 if (name[0] == '_' && name[1] == '_') {
152 int n = strlen(name);
153 if (name[n-1] == '_' && name[n-2] == '_') {
154 err_setstr(TypeError, "read-only special attribute");
155 return -1;
158 ac = dictlookup(op->cl_dict, name);
159 if (ac != NULL && is_accessobject(ac))
160 return setaccessvalue(ac, getowner(), v);
161 if (v == NULL) {
162 int rv = dictremove(op->cl_dict, name);
163 if (rv < 0)
164 err_setstr(AttributeError,
165 "delete non-existing class attribute");
166 return rv;
168 else
169 return dictinsert(op->cl_dict, name, v);
172 static object *
173 class_repr(op)
174 classobject *op;
176 char buf[140];
177 char *name;
178 if (op->cl_name == NULL || !is_stringobject(op->cl_name))
179 name = "?";
180 else
181 name = getstringvalue(op->cl_name);
182 sprintf(buf, "<class %.100s at %lx>", name, (long)op);
183 return newstringobject(buf);
186 typeobject Classtype = {
187 OB_HEAD_INIT(&Typetype)
189 "class",
190 sizeof(classobject),
192 (destructor)class_dealloc, /*tp_dealloc*/
193 0, /*tp_print*/
194 (getattrfunc)class_getattr, /*tp_getattr*/
195 (setattrfunc)class_setattr, /*tp_setattr*/
196 0, /*tp_compare*/
197 (reprfunc)class_repr, /*tp_repr*/
198 0, /*tp_as_number*/
199 0, /*tp_as_sequence*/
200 0, /*tp_as_mapping*/
204 issubclass(class, base)
205 object *class;
206 object *base;
208 int i, n;
209 classobject *cp;
210 if (class == base)
211 return 1;
212 if (class == NULL || !is_classobject(class))
213 return 0;
214 cp = (classobject *)class;
215 n = gettuplesize(cp->cl_bases);
216 for (i = 0; i < n; i++) {
217 if (issubclass(gettupleitem(cp->cl_bases, i), base))
218 return 1;
220 return 0;
224 /* Instance objects */
226 static object *instance_getattr PROTO((instanceobject *, char *));
228 static int
229 addaccess(class, inst)
230 classobject *class;
231 instanceobject *inst;
233 int i, n, pos, ret;
234 object *key, *value, *ac;
236 n = gettuplesize(class->cl_bases);
237 for (i = 0; i < n; i++) {
238 if (addaccess((classobject *)gettupleitem(class->cl_bases, i), inst) < 0)
239 return -1;
242 pos = 0;
243 while (mappinggetnext(class->cl_dict, &pos, &key, &value)) {
244 object *v;
245 if (!is_accessobject(value))
246 continue;
247 if (hasaccessvalue(value))
248 continue;
249 ac = dict2lookup(inst->in_dict, key);
250 if (ac != NULL && is_accessobject(ac)) {
251 err_setval(ConflictError, key);
252 return -1;
254 ac = cloneaccessobject(value);
255 if (ac == NULL)
256 return -1;
257 ret = dict2insert(inst->in_dict, key, ac);
258 DECREF(ac);
259 if (ret != 0)
260 return -1;
262 return 0;
265 object *
266 newinstanceobject(class, arg)
267 object *class;
268 object *arg;
270 register instanceobject *inst;
271 object *v;
272 object *init;
273 if (!is_classobject(class)) {
274 err_badcall();
275 return NULL;
277 inst = NEWOBJ(instanceobject, &Instancetype);
278 if (inst == NULL)
279 return NULL;
280 INCREF(class);
281 inst->in_class = (classobject *)class;
282 inst->in_dict = newdictobject();
283 if (inst->in_dict == NULL ||
284 addaccess((classobject *)class, inst) != 0) {
285 DECREF(inst);
286 return NULL;
288 init = instance_getattr(inst, "__init__");
289 if (init == NULL) {
290 err_clear();
291 if (arg != NULL && !(is_tupleobject(arg) &&
292 gettuplesize(arg) == 0)) {
293 err_setstr(TypeError,
294 "this classobject() takes no arguments");
295 DECREF(inst);
296 inst = NULL;
299 else {
300 object *res = call_object(init, arg);
301 DECREF(init);
302 if (res == NULL) {
303 DECREF(inst);
304 inst = NULL;
306 else {
307 if (res != None) {
308 err_setstr(TypeError,
309 "__init__() should return None");
310 DECREF(inst);
311 inst = NULL;
313 DECREF(res);
316 return (object *)inst;
319 /* Instance methods */
321 static void
322 instance_dealloc(inst)
323 register instanceobject *inst;
325 object *error_type, *error_value;
326 object *del;
327 /* Call the __del__ method if it exists. First temporarily
328 revive the object and save the current exception, if any. */
329 INCREF(inst);
330 err_get(&error_type, &error_value);
331 if ((del = instance_getattr(inst, "__del__")) != NULL) {
332 object *args = newtupleobject(0);
333 object *res = args;
334 if (res != NULL)
335 res = call_object(del, args);
336 XDECREF(args);
337 DECREF(del);
338 XDECREF(res);
339 /* XXX If __del__ raised an exception, it is ignored! */
341 /* Restore the saved exception and undo the temporary revival */
342 err_setval(error_type, error_value);
343 /* Can't use DECREF here, it would cause a recursive call */
344 if (--inst->ob_refcnt > 0)
345 return; /* __del__ added a reference; don't delete now */
346 DECREF(inst->in_class);
347 XDECREF(inst->in_dict);
348 free((ANY *)inst);
351 static object *
352 instance_getattr(inst, name)
353 register instanceobject *inst;
354 register char *name;
356 register object *v;
357 classobject *class;
358 if (name[0] == '_' && name[1] == '_') {
359 if (strcmp(name, "__dict__") == 0) {
360 INCREF(inst->in_dict);
361 return inst->in_dict;
363 if (strcmp(name, "__class__") == 0) {
364 INCREF(inst->in_class);
365 return (object *)inst->in_class;
368 class = NULL;
369 v = dictlookup(inst->in_dict, name);
370 if (v == NULL) {
371 v = class_lookup(inst->in_class, name, &class);
372 if (v == NULL) {
373 err_setstr(AttributeError, name);
374 return NULL;
377 if (is_accessobject(v)) {
378 v = getaccessvalue(v, getowner());
379 if (v == NULL)
380 return NULL;
382 else
383 INCREF(v);
384 if (class != NULL) {
385 if (is_funcobject(v)) {
386 object *w = newinstancemethodobject(v, (object *)inst,
387 (object *)class);
388 DECREF(v);
389 v = w;
391 else if (is_instancemethodobject(v)) {
392 object *im_class = instancemethodgetclass(v);
393 /* Only if classes are compatible */
394 if (issubclass((object *)class, im_class)) {
395 object *im_func = instancemethodgetfunc(v);
396 object *w = newinstancemethodobject(im_func,
397 (object *)inst, im_class);
398 DECREF(v);
399 v = w;
403 return v;
406 static int
407 instance_setattr(inst, name, v)
408 instanceobject *inst;
409 char *name;
410 object *v;
412 object *ac;
413 if (name[0] == '_' && name[1] == '_') {
414 int n = strlen(name);
415 if (name[n-1] == '_' && name[n-2] == '_') {
416 err_setstr(TypeError, "read-only special attribute");
417 return -1;
420 ac = dictlookup(inst->in_dict, name);
421 if (ac != NULL && is_accessobject(ac))
422 return setaccessvalue(ac, getowner(), v);
423 if (v == NULL) {
424 int rv = dictremove(inst->in_dict, name);
425 if (rv < 0)
426 err_setstr(AttributeError,
427 "delete non-existing instance attribute");
428 return rv;
430 else
431 return dictinsert(inst->in_dict, name, v);
434 static object *
435 instance_repr(inst)
436 instanceobject *inst;
438 object *func;
439 object *res;
441 func = instance_getattr(inst, "__repr__");
442 if (func == NULL) {
443 char buf[140];
444 object *classname = inst->in_class->cl_name;
445 char *cname;
446 if (classname != NULL && is_stringobject(classname))
447 cname = getstringvalue(classname);
448 else
449 cname = "?";
450 err_clear();
451 sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
452 return newstringobject(buf);
454 res = call_object(func, (object *)NULL);
455 DECREF(func);
456 return res;
459 static int
460 instance_compare(inst, other)
461 instanceobject *inst, *other;
463 object *func;
464 object *res;
465 int outcome;
467 func = instance_getattr(inst, "__cmp__");
468 if (func == NULL) {
469 err_clear();
470 if (inst < other)
471 return -1;
472 if (inst > other)
473 return 1;
474 return 0;
476 res = call_object(func, (object *)other);
477 DECREF(func);
478 if (res == NULL) {
479 err_clear(); /* XXX Should report the error, bot how...??? */
480 return 0;
482 if (is_intobject(res))
483 outcome = getintvalue(res);
484 else
485 outcome = 0; /* XXX Should report the error, bot how...??? */
486 DECREF(res);
487 return outcome;
490 static long
491 instance_hash(inst)
492 instanceobject *inst;
494 object *func;
495 object *res;
496 long outcome;
498 func = instance_getattr(inst, "__hash__");
499 if (func == NULL) {
500 /* If there is no __cmp__ method, we hash on the address.
501 If a __cmp__ method exists, there must be a __hash__. */
502 err_clear();
503 func = instance_getattr(inst, "__cmp__");
504 if (func == NULL) {
505 err_clear();
506 outcome = (long)inst;
507 if (outcome == -1)
508 outcome = -2;
509 return outcome;
511 err_setstr(TypeError, "unhashable instance");
512 return -1;
514 res = call_object(func, (object *)NULL);
515 DECREF(func);
516 if (res == NULL)
517 return -1;
518 if (is_intobject(res)) {
519 outcome = getintvalue(res);
520 if (outcome == -1)
521 outcome = -2;
523 else {
524 err_setstr(TypeError, "__hash__() should return an int");
525 outcome = -1;
527 DECREF(res);
528 return outcome;
531 static int
532 instance_length(inst)
533 instanceobject *inst;
535 object *func;
536 object *res;
537 int outcome;
539 func = instance_getattr(inst, "__len__");
540 if (func == NULL)
541 return -1;
542 res = call_object(func, (object *)NULL);
543 DECREF(func);
544 if (res == NULL)
545 return -1;
546 if (is_intobject(res)) {
547 outcome = getintvalue(res);
548 if (outcome < 0)
549 err_setstr(ValueError, "__len__() should return >= 0");
551 else {
552 err_setstr(TypeError, "__len__() should return an int");
553 outcome = -1;
555 DECREF(res);
556 return outcome;
559 static object *
560 instance_subscript(inst, key)
561 instanceobject *inst;
562 object *key;
564 object *func;
565 object *arg;
566 object *res;
568 func = instance_getattr(inst, "__getitem__");
569 if (func == NULL)
570 return NULL;
571 arg = mkvalue("(O)", key);
572 if (arg == NULL) {
573 DECREF(func);
574 return NULL;
576 res = call_object(func, arg);
577 DECREF(func);
578 DECREF(arg);
579 return res;
582 static int
583 instance_ass_subscript(inst, key, value)
584 instanceobject*inst;
585 object *key;
586 object *value;
588 object *func;
589 object *arg;
590 object *res;
592 if (value == NULL)
593 func = instance_getattr(inst, "__delitem__");
594 else
595 func = instance_getattr(inst, "__setitem__");
596 if (func == NULL)
597 return -1;
598 if (value == NULL)
599 arg = mkvalue("(O)", key);
600 else
601 arg = mkvalue("(OO)", key, value);
602 if (arg == NULL) {
603 DECREF(func);
604 return -1;
606 res = call_object(func, arg);
607 DECREF(func);
608 DECREF(arg);
609 if (res == NULL)
610 return -1;
611 DECREF(res);
612 return 0;
615 static mapping_methods instance_as_mapping = {
616 (inquiry)instance_length, /*mp_length*/
617 (binaryfunc)instance_subscript, /*mp_subscript*/
618 (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/
621 static object *
622 instance_concat(inst, other)
623 instanceobject *inst, *other;
625 object *func, *arg, *res;
627 func = instance_getattr(inst, "__add__");
628 if (func == NULL)
629 return NULL;
630 arg = mkvalue("(O)", other);
631 if (arg == NULL) {
632 DECREF(func);
633 return NULL;
635 res = call_object(func, arg);
636 DECREF(func);
637 DECREF(arg);
638 return res;
641 static object *
642 instance_repeat(inst, count)
643 instanceobject *inst;
644 int count;
646 object *func, *arg, *res;
648 func = instance_getattr(inst, "__mul__");
649 if (func == NULL)
650 return NULL;
651 arg = newintobject((long)count);
652 if (arg == NULL) {
653 DECREF(func);
654 return NULL;
656 res = call_object(func, arg);
657 DECREF(func);
658 DECREF(arg);
659 return res;
662 static object *
663 instance_item(inst, i)
664 instanceobject *inst;
665 int i;
667 object *func, *arg, *res;
669 func = instance_getattr(inst, "__getitem__");
670 if (func == NULL)
671 return NULL;
672 arg = newintobject((long)i);
673 if (arg == NULL) {
674 DECREF(func);
675 return NULL;
677 res = call_object(func, arg);
678 DECREF(func);
679 DECREF(arg);
680 return res;
683 static object *
684 instance_slice(inst, i, j)
685 instanceobject *inst;
686 int i, j;
688 object *func, *arg, *res;
690 func = instance_getattr(inst, "__getslice__");
691 if (func == NULL)
692 return NULL;
693 arg = mkvalue("(ii)", i, j);
694 if (arg == NULL) {
695 DECREF(func);
696 return NULL;
698 res = call_object(func, arg);
699 DECREF(func);
700 DECREF(arg);
701 return res;
704 static int
705 instance_ass_item(inst, i, item)
706 instanceobject *inst;
707 int i;
708 object *item;
710 object *func, *arg, *res;
712 if (item == NULL)
713 func = instance_getattr(inst, "__delitem__");
714 else
715 func = instance_getattr(inst, "__setitem__");
716 if (func == NULL)
717 return -1;
718 if (item == NULL)
719 arg = mkvalue("i", i);
720 else
721 arg = mkvalue("(iO)", i, item);
722 if (arg == NULL) {
723 DECREF(func);
724 return -1;
726 res = call_object(func, arg);
727 DECREF(func);
728 DECREF(arg);
729 if (res == NULL)
730 return -1;
731 DECREF(res);
732 return 0;
735 static int
736 instance_ass_slice(inst, i, j, value)
737 instanceobject *inst;
738 int i, j;
739 object *value;
741 object *func, *arg, *res;
743 if (value == NULL)
744 func = instance_getattr(inst, "__delslice__");
745 else
746 func = instance_getattr(inst, "__setslice__");
747 if (func == NULL)
748 return -1;
749 if (value == NULL)
750 arg = mkvalue("(ii)", i, j);
751 else
752 arg = mkvalue("(iiO)", i, j, value);
753 if (arg == NULL) {
754 DECREF(func);
755 return -1;
757 res = call_object(func, arg);
758 DECREF(func);
759 DECREF(arg);
760 if (res == NULL)
761 return -1;
762 DECREF(res);
763 return 0;
766 static sequence_methods instance_as_sequence = {
767 (inquiry)instance_length, /*sq_length*/
768 (binaryfunc)instance_concat, /*sq_concat*/
769 (intargfunc)instance_repeat, /*sq_repeat*/
770 (intargfunc)instance_item, /*sq_item*/
771 (intintargfunc)instance_slice, /*sq_slice*/
772 (intobjargproc)instance_ass_item, /*sq_ass_item*/
773 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
776 static object *
777 generic_binary_op(self, other, methodname)
778 instanceobject *self;
779 object *other;
780 char *methodname;
782 object *func, *arg, *res;
784 if ((func = instance_getattr(self, methodname)) == NULL)
785 return NULL;
786 arg = mkvalue("O", other);
787 if (arg == NULL) {
788 DECREF(func);
789 return NULL;
791 res = call_object(func, arg);
792 DECREF(func);
793 DECREF(arg);
794 return res;
797 static object *
798 generic_unary_op(self, methodname)
799 instanceobject *self;
800 char *methodname;
802 object *func, *res;
804 if ((func = instance_getattr(self, methodname)) == NULL)
805 return NULL;
806 res = call_object(func, (object *)NULL);
807 DECREF(func);
808 return res;
811 #define BINARY(funcname, methodname) \
812 static object * funcname(self, other) instanceobject *self; object *other; { \
813 return generic_binary_op(self, other, methodname); \
816 #define UNARY(funcname, methodname) \
817 static object *funcname(self) instanceobject *self; { \
818 return generic_unary_op(self, methodname); \
821 BINARY(instance_add, "__add__")
822 BINARY(instance_sub, "__sub__")
823 BINARY(instance_mul, "__mul__")
824 BINARY(instance_div, "__div__")
825 BINARY(instance_mod, "__mod__")
826 BINARY(instance_divmod, "__divmod__")
827 BINARY(instance_pow, "__pow__")
828 UNARY(instance_neg, "__neg__")
829 UNARY(instance_pos, "__pos__")
830 UNARY(instance_abs, "__abs__")
832 static int
833 instance_nonzero(self)
834 instanceobject *self;
836 object *func, *res;
837 long outcome;
839 if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
840 err_clear();
841 if ((func = instance_getattr(self, "__len__")) == NULL) {
842 err_clear();
843 /* Fall back to the default behavior:
844 all instances are nonzero */
845 return 1;
848 res = call_object(func, (object *)NULL);
849 DECREF(func);
850 if (res == NULL)
851 return -1;
852 if (!is_intobject(res)) {
853 DECREF(res);
854 err_setstr(TypeError, "__nonzero__ should return an int");
855 return -1;
857 outcome = getintvalue(res);
858 DECREF(res);
859 if (outcome < 0) {
860 err_setstr(ValueError, "__nonzero__ should return >= 0");
861 return -1;
863 return outcome > 0;
866 UNARY(instance_invert, "__invert__")
867 BINARY(instance_lshift, "__lshift__")
868 BINARY(instance_rshift, "__rshift__")
869 BINARY(instance_and, "__and__")
870 BINARY(instance_xor, "__xor__")
871 BINARY(instance_or, "__or__")
873 static int
874 instance_coerce(pv, pw)
875 object **pv, **pw;
877 object *v = *pv;
878 object *w = *pw;
879 object *func;
880 object *res;
881 int outcome;
883 if (!is_instanceobject(v))
884 return 1; /* XXX shouldn't be possible */
885 func = instance_getattr((instanceobject *)v, "__coerce__");
886 if (func == NULL) {
887 err_clear();
888 return 1;
890 res = call_object(func, w);
891 if (res == NULL)
892 return -1;
893 if (res == None) {
894 DECREF(res);
895 return 1;
897 outcome = getargs(res, "(OO)", &v, &w);
898 if (!outcome || v->ob_type != w->ob_type ||
899 v->ob_type->tp_as_number == NULL) {
900 DECREF(res);
901 err_setstr(TypeError, "bad __coerce__ result");
902 return -1;
904 INCREF(v);
905 INCREF(w);
906 DECREF(res);
907 *pv = v;
908 *pw = w;
909 return 0;
912 UNARY(instance_int, "__int__")
913 UNARY(instance_long, "__long__")
914 UNARY(instance_float, "__float__")
915 UNARY(instance_oct, "__oct__")
916 UNARY(instance_hex, "__hex__")
918 static number_methods instance_as_number = {
919 (binaryfunc)instance_add, /*nb_add*/
920 (binaryfunc)instance_sub, /*nb_subtract*/
921 (binaryfunc)instance_mul, /*nb_multiply*/
922 (binaryfunc)instance_div, /*nb_divide*/
923 (binaryfunc)instance_mod, /*nb_remainder*/
924 (binaryfunc)instance_divmod, /*nb_divmod*/
925 (binaryfunc)instance_pow, /*nb_power*/
926 (unaryfunc)instance_neg, /*nb_negative*/
927 (unaryfunc)instance_pos, /*nb_positive*/
928 (unaryfunc)instance_abs, /*nb_absolute*/
929 (inquiry)instance_nonzero, /*nb_nonzero*/
930 (unaryfunc)instance_invert, /*nb_invert*/
931 (binaryfunc)instance_lshift, /*nb_lshift*/
932 (binaryfunc)instance_rshift, /*nb_rshift*/
933 (binaryfunc)instance_and, /*nb_and*/
934 (binaryfunc)instance_xor, /*nb_xor*/
935 (binaryfunc)instance_or, /*nb_or*/
936 (coercion)instance_coerce, /*nb_coerce*/
937 (unaryfunc)instance_int, /*nb_int*/
938 (unaryfunc)instance_long, /*nb_long*/
939 (unaryfunc)instance_float, /*nb_float*/
940 (unaryfunc)instance_oct, /*nb_oct*/
941 (unaryfunc)instance_hex, /*nb_hex*/
944 typeobject Instancetype = {
945 OB_HEAD_INIT(&Typetype)
947 "instance",
948 sizeof(instanceobject),
950 (destructor)instance_dealloc, /*tp_dealloc*/
951 0, /*tp_print*/
952 (object * (*) FPROTO((object *, char *)))
953 (getattrfunc)instance_getattr, /*tp_getattr*/
954 (setattrfunc)instance_setattr, /*tp_setattr*/
955 (cmpfunc)instance_compare, /*tp_compare*/
956 (reprfunc)instance_repr, /*tp_repr*/
957 &instance_as_number, /*tp_as_number*/
958 &instance_as_sequence, /*tp_as_sequence*/
959 &instance_as_mapping, /*tp_as_mapping*/
960 (hashfunc)instance_hash, /*tp_hash*/
964 /* Instance method objects are used for two purposes:
965 (a) as bound instance methods (returned by instancename.methodname)
966 (b) as unbound methods (returned by ClassName.methodname)
967 In case (b), im_self is NULL
970 typedef struct {
971 OB_HEAD
972 object *im_func; /* The function implementing the method */
973 object *im_self; /* The instance it is bound to, or NULL */
974 object *im_class; /* The class that defined the method */
975 } instancemethodobject;
977 object *
978 newinstancemethodobject(func, self, class)
979 object *func;
980 object *self;
981 object *class;
983 register instancemethodobject *im;
984 if (!is_funcobject(func)) {
985 err_badcall();
986 return NULL;
988 im = NEWOBJ(instancemethodobject, &Instancemethodtype);
989 if (im == NULL)
990 return NULL;
991 INCREF(func);
992 im->im_func = func;
993 XINCREF(self);
994 im->im_self = self;
995 INCREF(class);
996 im->im_class = class;
997 return (object *)im;
1000 object *
1001 instancemethodgetfunc(im)
1002 register object *im;
1004 if (!is_instancemethodobject(im)) {
1005 err_badcall();
1006 return NULL;
1008 return ((instancemethodobject *)im)->im_func;
1011 object *
1012 instancemethodgetself(im)
1013 register object *im;
1015 if (!is_instancemethodobject(im)) {
1016 err_badcall();
1017 return NULL;
1019 return ((instancemethodobject *)im)->im_self;
1022 object *
1023 instancemethodgetclass(im)
1024 register object *im;
1026 if (!is_instancemethodobject(im)) {
1027 err_badcall();
1028 return NULL;
1030 return ((instancemethodobject *)im)->im_class;
1033 /* Class method methods */
1035 #define OFF(x) offsetof(instancemethodobject, x)
1037 static struct memberlist instancemethod_memberlist[] = {
1038 {"im_func", T_OBJECT, OFF(im_func)},
1039 {"im_self", T_OBJECT, OFF(im_self)},
1040 {"im_class", T_OBJECT, OFF(im_class)},
1041 {NULL} /* Sentinel */
1044 static object *
1045 instancemethod_getattr(im, name)
1046 register instancemethodobject *im;
1047 char *name;
1049 return getmember((char *)im, instancemethod_memberlist, name);
1052 static void
1053 instancemethod_dealloc(im)
1054 register instancemethodobject *im;
1056 DECREF(im->im_func);
1057 XDECREF(im->im_self);
1058 DECREF(im->im_class);
1059 free((ANY *)im);
1062 static int
1063 instancemethod_compare(a, b)
1064 instancemethodobject *a, *b;
1066 int cmp = cmpobject(a->im_self, b->im_self);
1067 if (cmp == 0)
1068 cmp = cmpobject(a->im_func, b->im_func);
1069 return cmp;
1072 static object *
1073 instancemethod_repr(a)
1074 instancemethodobject *a;
1076 char buf[240];
1077 instanceobject *self = (instanceobject *)(a->im_self);
1078 funcobject *func = (funcobject *)(a->im_func);
1079 classobject *class = (classobject *)(a->im_class);
1080 object *fclassname, *iclassname, *funcname;
1081 char *fcname, *icname, *fname;
1082 fclassname = class->cl_name;
1083 funcname = func->func_name;
1084 if (fclassname != NULL && is_stringobject(fclassname))
1085 fcname = getstringvalue(fclassname);
1086 else
1087 fcname = "?";
1088 if (funcname != NULL && is_stringobject(funcname))
1089 fname = getstringvalue(funcname);
1090 else
1091 fname = "?";
1092 if (self == NULL)
1093 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname);
1094 else {
1095 iclassname = self->in_class->cl_name;
1096 if (iclassname != NULL && is_stringobject(iclassname))
1097 icname = getstringvalue(iclassname);
1098 else
1099 icname = "?";
1100 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
1101 fcname, fname, icname, (long)self);
1103 return newstringobject(buf);
1106 static long
1107 instancemethod_hash(a)
1108 instancemethodobject *a;
1110 long x, y;
1111 if (a->im_self == NULL)
1112 x = hashobject(None);
1113 else
1114 x = hashobject(a->im_self);
1115 if (x == -1)
1116 return -1;
1117 y = hashobject(a->im_func);
1118 if (y == -1)
1119 return -1;
1120 return x ^ y;
1123 typeobject Instancemethodtype = {
1124 OB_HEAD_INIT(&Typetype)
1126 "instance method",
1127 sizeof(instancemethodobject),
1129 (destructor)instancemethod_dealloc, /*tp_dealloc*/
1130 0, /*tp_print*/
1131 (getattrfunc)instancemethod_getattr, /*tp_getattr*/
1132 0, /*tp_setattr*/
1133 (cmpfunc)instancemethod_compare, /*tp_compare*/
1134 (reprfunc)instancemethod_repr, /*tp_repr*/
1135 0, /*tp_as_number*/
1136 0, /*tp_as_sequence*/
1137 0, /*tp_as_mapping*/
1138 (hashfunc)instancemethod_hash, /*tp_hash*/