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