1 /***********************************************************
2 Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
3 Amsterdam, The Netherlands.
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"
33 newclassobject(bases
, dict
, name
)
34 object
*bases
; /* NULL or tuple of classobjects! */
36 object
*name
; /* String; NULL if unknown */
42 bases
= newtupleobject(0);
48 op
= NEWOBJ(classobject
, &Classtype
);
59 while (mappinggetnext(dict
, &pos
, &key
, &value
)) {
60 if (is_accessobject(value
))
61 setaccessowner(value
, (object
*)op
);
79 class_lookup(cp
, name
, pclass
)
85 object
*value
= dictlookup(cp
->cl_dict
, name
);
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
);
101 class_getattr(op
, name
)
102 register classobject
*op
;
107 if (strcmp(name
, "__dict__") == 0) {
111 if (strcmp(name
, "__bases__") == 0) {
112 INCREF(op
->cl_bases
);
115 if (strcmp(name
, "__name__") == 0) {
116 if (op
->cl_name
== NULL
)
123 v
= class_lookup(op
, name
, &class);
125 err_setstr(AttributeError
, name
);
128 if (is_accessobject(v
)) {
129 v
= getaccessvalue(v
, getowner());
135 if (is_funcobject(v
)) {
136 object
*w
= newinstancemethodobject(v
, (object
*)NULL
,
145 class_setattr(op
, name
, v
)
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");
158 ac
= dictlookup(op
->cl_dict
, name
);
159 if (ac
!= NULL
&& is_accessobject(ac
))
160 return setaccessvalue(ac
, getowner(), v
);
162 int rv
= dictremove(op
->cl_dict
, name
);
164 err_setstr(AttributeError
,
165 "delete non-existing class attribute");
169 return dictinsert(op
->cl_dict
, name
, v
);
178 if (op
->cl_name
== NULL
|| !is_stringobject(op
->cl_name
))
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
)
192 (destructor
)class_dealloc
, /*tp_dealloc*/
194 (getattrfunc
)class_getattr
, /*tp_getattr*/
195 (setattrfunc
)class_setattr
, /*tp_setattr*/
197 (reprfunc
)class_repr
, /*tp_repr*/
199 0, /*tp_as_sequence*/
204 issubclass(class, base
)
212 if (class == NULL
|| !is_classobject(class))
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
))
224 /* Instance objects */
226 static object
*instance_getattr
PROTO((instanceobject
*, char *));
229 addaccess(class, inst
)
231 instanceobject
*inst
;
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)
243 while (mappinggetnext(class->cl_dict
, &pos
, &key
, &value
)) {
245 if (!is_accessobject(value
))
247 if (hasaccessvalue(value
))
249 ac
= dict2lookup(inst
->in_dict
, key
);
250 if (ac
!= NULL
&& is_accessobject(ac
)) {
251 err_setval(ConflictError
, key
);
254 ac
= cloneaccessobject(value
);
257 ret
= dict2insert(inst
->in_dict
, key
, ac
);
266 newinstanceobject(class, arg
)
270 register instanceobject
*inst
;
273 if (!is_classobject(class)) {
277 inst
= NEWOBJ(instanceobject
, &Instancetype
);
281 inst
->in_class
= (classobject
*)class;
282 inst
->in_dict
= newdictobject();
283 if (inst
->in_dict
== NULL
||
284 addaccess((classobject
*)class, inst
) != 0) {
288 init
= instance_getattr(inst
, "__init__");
291 if (arg
!= NULL
&& !(is_tupleobject(arg
) &&
292 gettuplesize(arg
) == 0)) {
293 err_setstr(TypeError
,
294 "this classobject() takes no arguments");
300 object
*res
= call_object(init
, arg
);
308 err_setstr(TypeError
,
309 "__init__() should return None");
316 return (object
*)inst
;
319 /* Instance methods */
322 instance_dealloc(inst
)
323 register instanceobject
*inst
;
325 object
*error_type
, *error_value
;
327 /* Call the __del__ method if it exists. First temporarily
328 revive the object and save the current exception, if any. */
330 err_get(&error_type
, &error_value
);
331 if ((del
= instance_getattr(inst
, "__del__")) != NULL
) {
332 object
*args
= newtupleobject(0);
335 res
= call_object(del
, args
);
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
);
352 instance_getattr(inst
, name
)
353 register instanceobject
*inst
;
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
;
369 v
= dictlookup(inst
->in_dict
, name
);
371 v
= class_lookup(inst
->in_class
, name
, &class);
373 err_setstr(AttributeError
, name
);
377 if (is_accessobject(v
)) {
378 v
= getaccessvalue(v
, getowner());
385 if (is_funcobject(v
)) {
386 object
*w
= newinstancemethodobject(v
, (object
*)inst
,
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
);
407 instance_setattr(inst
, name
, v
)
408 instanceobject
*inst
;
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");
420 ac
= dictlookup(inst
->in_dict
, name
);
421 if (ac
!= NULL
&& is_accessobject(ac
))
422 return setaccessvalue(ac
, getowner(), v
);
424 int rv
= dictremove(inst
->in_dict
, name
);
426 err_setstr(AttributeError
,
427 "delete non-existing instance attribute");
431 return dictinsert(inst
->in_dict
, name
, v
);
436 instanceobject
*inst
;
441 func
= instance_getattr(inst
, "__repr__");
444 object
*classname
= inst
->in_class
->cl_name
;
446 if (classname
!= NULL
&& is_stringobject(classname
))
447 cname
= getstringvalue(classname
);
451 sprintf(buf
, "<%.100s instance at %lx>", cname
, (long)inst
);
452 return newstringobject(buf
);
454 res
= call_object(func
, (object
*)NULL
);
460 instance_compare(inst
, other
)
461 instanceobject
*inst
, *other
;
467 func
= instance_getattr(inst
, "__cmp__");
476 res
= call_object(func
, (object
*)other
);
479 err_clear(); /* XXX Should report the error, bot how...??? */
482 if (is_intobject(res
))
483 outcome
= getintvalue(res
);
485 outcome
= 0; /* XXX Should report the error, bot how...??? */
492 instanceobject
*inst
;
498 func
= instance_getattr(inst
, "__hash__");
500 /* If there is no __cmp__ method, we hash on the address.
501 If a __cmp__ method exists, there must be a __hash__. */
503 func
= instance_getattr(inst
, "__cmp__");
506 outcome
= (long)inst
;
511 err_setstr(TypeError
, "unhashable instance");
514 res
= call_object(func
, (object
*)NULL
);
518 if (is_intobject(res
)) {
519 outcome
= getintvalue(res
);
524 err_setstr(TypeError
, "__hash__() should return an int");
532 instance_length(inst
)
533 instanceobject
*inst
;
539 func
= instance_getattr(inst
, "__len__");
542 res
= call_object(func
, (object
*)NULL
);
546 if (is_intobject(res
)) {
547 outcome
= getintvalue(res
);
549 err_setstr(ValueError
, "__len__() should return >= 0");
552 err_setstr(TypeError
, "__len__() should return an int");
560 instance_subscript(inst
, key
)
561 instanceobject
*inst
;
568 func
= instance_getattr(inst
, "__getitem__");
571 arg
= mkvalue("(O)", key
);
576 res
= call_object(func
, arg
);
583 instance_ass_subscript(inst
, key
, value
)
593 func
= instance_getattr(inst
, "__delitem__");
595 func
= instance_getattr(inst
, "__setitem__");
599 arg
= mkvalue("(O)", key
);
601 arg
= mkvalue("(OO)", key
, value
);
606 res
= call_object(func
, arg
);
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*/
622 instance_concat(inst
, other
)
623 instanceobject
*inst
, *other
;
625 object
*func
, *arg
, *res
;
627 func
= instance_getattr(inst
, "__add__");
630 arg
= mkvalue("(O)", other
);
635 res
= call_object(func
, arg
);
642 instance_repeat(inst
, count
)
643 instanceobject
*inst
;
646 object
*func
, *arg
, *res
;
648 func
= instance_getattr(inst
, "__mul__");
651 arg
= newintobject((long)count
);
656 res
= call_object(func
, arg
);
663 instance_item(inst
, i
)
664 instanceobject
*inst
;
667 object
*func
, *arg
, *res
;
669 func
= instance_getattr(inst
, "__getitem__");
672 arg
= newintobject((long)i
);
677 res
= call_object(func
, arg
);
684 instance_slice(inst
, i
, j
)
685 instanceobject
*inst
;
688 object
*func
, *arg
, *res
;
690 func
= instance_getattr(inst
, "__getslice__");
693 arg
= mkvalue("(ii)", i
, j
);
698 res
= call_object(func
, arg
);
705 instance_ass_item(inst
, i
, item
)
706 instanceobject
*inst
;
710 object
*func
, *arg
, *res
;
713 func
= instance_getattr(inst
, "__delitem__");
715 func
= instance_getattr(inst
, "__setitem__");
719 arg
= mkvalue("i", i
);
721 arg
= mkvalue("(iO)", i
, item
);
726 res
= call_object(func
, arg
);
736 instance_ass_slice(inst
, i
, j
, value
)
737 instanceobject
*inst
;
741 object
*func
, *arg
, *res
;
744 func
= instance_getattr(inst
, "__delslice__");
746 func
= instance_getattr(inst
, "__setslice__");
750 arg
= mkvalue("(ii)", i
, j
);
752 arg
= mkvalue("(iiO)", i
, j
, value
);
757 res
= call_object(func
, arg
);
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*/
777 generic_binary_op(self
, other
, methodname
)
778 instanceobject
*self
;
782 object
*func
, *arg
, *res
;
784 if ((func
= instance_getattr(self
, methodname
)) == NULL
)
786 arg
= mkvalue("O", other
);
791 res
= call_object(func
, arg
);
798 generic_unary_op(self
, methodname
)
799 instanceobject
*self
;
804 if ((func
= instance_getattr(self
, methodname
)) == NULL
)
806 res
= call_object(func
, (object
*)NULL
);
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__")
833 instance_nonzero(self
)
834 instanceobject
*self
;
839 if ((func
= instance_getattr(self
, "__nonzero__")) == NULL
) {
841 if ((func
= instance_getattr(self
, "__len__")) == NULL
) {
843 /* Fall back to the default behavior:
844 all instances are nonzero */
848 res
= call_object(func
, (object
*)NULL
);
852 if (!is_intobject(res
)) {
854 err_setstr(TypeError
, "__nonzero__ should return an int");
857 outcome
= getintvalue(res
);
860 err_setstr(ValueError
, "__nonzero__ should return >= 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__")
874 instance_coerce(pv
, pw
)
883 if (!is_instanceobject(v
))
884 return 1; /* XXX shouldn't be possible */
885 func
= instance_getattr((instanceobject
*)v
, "__coerce__");
890 res
= call_object(func
, w
);
897 outcome
= getargs(res
, "(OO)", &v
, &w
);
898 if (!outcome
|| v
->ob_type
!= w
->ob_type
||
899 v
->ob_type
->tp_as_number
== NULL
) {
901 err_setstr(TypeError
, "bad __coerce__ result");
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
)
948 sizeof(instanceobject
),
950 (destructor
)instance_dealloc
, /*tp_dealloc*/
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
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
;
978 newinstancemethodobject(func
, self
, class)
983 register instancemethodobject
*im
;
984 if (!is_funcobject(func
)) {
988 im
= NEWOBJ(instancemethodobject
, &Instancemethodtype
);
996 im
->im_class
= class;
1001 instancemethodgetfunc(im
)
1002 register object
*im
;
1004 if (!is_instancemethodobject(im
)) {
1008 return ((instancemethodobject
*)im
)->im_func
;
1012 instancemethodgetself(im
)
1013 register object
*im
;
1015 if (!is_instancemethodobject(im
)) {
1019 return ((instancemethodobject
*)im
)->im_self
;
1023 instancemethodgetclass(im
)
1024 register object
*im
;
1026 if (!is_instancemethodobject(im
)) {
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 */
1045 instancemethod_getattr(im
, name
)
1046 register instancemethodobject
*im
;
1049 return getmember((char *)im
, instancemethod_memberlist
, name
);
1053 instancemethod_dealloc(im
)
1054 register instancemethodobject
*im
;
1056 DECREF(im
->im_func
);
1057 XDECREF(im
->im_self
);
1058 DECREF(im
->im_class
);
1063 instancemethod_compare(a
, b
)
1064 instancemethodobject
*a
, *b
;
1066 int cmp
= cmpobject(a
->im_self
, b
->im_self
);
1068 cmp
= cmpobject(a
->im_func
, b
->im_func
);
1073 instancemethod_repr(a
)
1074 instancemethodobject
*a
;
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
);
1088 if (funcname
!= NULL
&& is_stringobject(funcname
))
1089 fname
= getstringvalue(funcname
);
1093 sprintf(buf
, "<unbound method %.100s.%.100s>", fcname
, fname
);
1095 iclassname
= self
->in_class
->cl_name
;
1096 if (iclassname
!= NULL
&& is_stringobject(iclassname
))
1097 icname
= getstringvalue(iclassname
);
1100 sprintf(buf
, "<method %.60s.%.60s of %.60s instance at %lx>",
1101 fcname
, fname
, icname
, (long)self
);
1103 return newstringobject(buf
);
1107 instancemethod_hash(a
)
1108 instancemethodobject
*a
;
1111 if (a
->im_self
== NULL
)
1112 x
= hashobject(None
);
1114 x
= hashobject(a
->im_self
);
1117 y
= hashobject(a
->im_func
);
1123 typeobject Instancemethodtype
= {
1124 OB_HEAD_INIT(&Typetype
)
1127 sizeof(instancemethodobject
),
1129 (destructor
)instancemethod_dealloc
, /*tp_dealloc*/
1131 (getattrfunc
)instancemethod_getattr
, /*tp_getattr*/
1133 (cmpfunc
)instancemethod_compare
, /*tp_compare*/
1134 (reprfunc
)instancemethod_repr
, /*tp_repr*/
1136 0, /*tp_as_sequence*/
1137 0, /*tp_as_mapping*/
1138 (hashfunc
)instancemethod_hash
, /*tp_hash*/