cpython/Objects/classobject.c

913 lines
18 KiB
C
Raw Normal View History

1991-02-19 12:39:46 +00:00
/***********************************************************
Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
Amsterdam, The Netherlands.
1991-02-19 12:39:46 +00:00
All Rights Reserved
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted,
provided that the above copyright notice appear in all copies and that
both that copyright notice and this permission notice appear in
supporting documentation, and that the names of Stichting Mathematisch
Centrum or CWI not be used in advertising or publicity pertaining to
distribution of the software without specific, written prior permission.
STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
******************************************************************/
1990-10-14 12:07:46 +00:00
/* Class object implementation */
1990-12-20 15:06:42 +00:00
#include "allobjects.h"
#include "modsupport.h"
1990-12-20 15:06:42 +00:00
#include "structmember.h"
#include "ceval.h"
extern typeobject MappingInstancetype;
extern typeobject SequenceInstancetype;
1990-10-14 12:07:46 +00:00
typedef struct {
OB_HEAD
object *cl_bases; /* A tuple */
object *cl_methods; /* A dictionary */
1991-10-20 20:11:48 +00:00
object *cl_name; /* A string */
1990-10-14 12:07:46 +00:00
} classobject;
object *
1991-10-20 20:11:48 +00:00
newclassobject(bases, methods, name)
1990-10-14 12:07:46 +00:00
object *bases; /* NULL or tuple of classobjects! */
object *methods;
1991-10-20 20:11:48 +00:00
object *name; /* String; NULL if unknown */
1990-10-14 12:07:46 +00:00
{
classobject *op;
if (bases == NULL) {
bases = newtupleobject(0);
if (bases == NULL)
return NULL;
}
else
INCREF(bases);
1990-10-14 12:07:46 +00:00
op = NEWOBJ(classobject, &Classtype);
if (op == NULL) {
DECREF(bases);
1990-10-14 12:07:46 +00:00
return NULL;
}
1990-10-14 12:07:46 +00:00
op->cl_bases = bases;
INCREF(methods);
op->cl_methods = methods;
1991-10-20 20:11:48 +00:00
XINCREF(name);
op->cl_name = name;
1990-10-14 12:07:46 +00:00
return (object *) op;
}
/* Class methods */
static void
class_dealloc(op)
classobject *op;
{
DECREF(op->cl_bases);
1990-10-14 12:07:46 +00:00
DECREF(op->cl_methods);
1991-10-20 20:11:48 +00:00
XDECREF(op->cl_name);
1990-10-14 12:07:46 +00:00
free((ANY *)op);
}
static object *
class_getattr(op, name)
register classobject *op;
register char *name;
{
register object *v;
1991-10-20 20:11:48 +00:00
if (strcmp(name, "__dict__") == 0) {
INCREF(op->cl_methods);
return op->cl_methods;
}
if (strcmp(name, "__bases__") == 0) {
INCREF(op->cl_bases);
return op->cl_bases;
}
if (strcmp(name, "__name__") == 0) {
if (op->cl_name == NULL)
v = None;
else
v = op->cl_name;
INCREF(v);
return v;
}
1990-10-14 12:07:46 +00:00
v = dictlookup(op->cl_methods, name);
if (v != NULL) {
INCREF(v);
return v;
}
{
1990-10-14 12:07:46 +00:00
int n = gettuplesize(op->cl_bases);
int i;
for (i = 0; i < n; i++) {
1992-03-27 17:23:48 +00:00
v = class_getattr((classobject *)
gettupleitem(op->cl_bases, i), name);
1990-10-14 12:07:46 +00:00
if (v != NULL)
return v;
1990-12-20 15:06:42 +00:00
err_clear();
1990-10-14 12:07:46 +00:00
}
}
err_setstr(AttributeError, name);
1990-10-14 12:07:46 +00:00
return NULL;
}
1991-10-20 20:11:48 +00:00
static int
class_setattr(op, name, v)
classobject *op;
char *name;
object *v;
{
if (name[0] == '_' && name[1] == '_') {
int n = strlen(name);
if (name[n-1] == '_' && name[n-2] == '_') {
err_setstr(TypeError, "read-only special attribute");
return -1;
}
}
if (v == NULL) {
int rv = dictremove(op->cl_methods, name);
if (rv < 0)
err_setstr(AttributeError,
"delete non-existing class attribute");
return rv;
}
1991-10-20 20:11:48 +00:00
else
return dictinsert(op->cl_methods, name, v);
}
1990-10-14 12:07:46 +00:00
typeobject Classtype = {
OB_HEAD_INIT(&Typetype)
0,
"class",
sizeof(classobject),
0,
class_dealloc, /*tp_dealloc*/
0, /*tp_print*/
class_getattr, /*tp_getattr*/
1991-10-20 20:11:48 +00:00
class_setattr, /*tp_setattr*/
1990-10-14 12:07:46 +00:00
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};
/* We're not done yet: next, we define instance objects... */
1990-10-14 12:07:46 +00:00
typedef struct {
OB_HEAD
classobject *in_class; /* The class object */
object *in_attr; /* A dictionary */
} instanceobject;
1990-10-14 12:07:46 +00:00
object *
newinstanceobject(class)
1990-10-14 12:07:46 +00:00
register object *class;
{
register instanceobject *inst;
object *v;
1990-10-14 12:07:46 +00:00
if (!is_classobject(class)) {
1990-10-21 22:15:08 +00:00
err_badcall();
1990-10-14 12:07:46 +00:00
return NULL;
}
inst = NEWOBJ(instanceobject, &Instancetype);
if (inst == NULL)
1990-10-14 12:07:46 +00:00
return NULL;
INCREF(class);
inst->in_class = (classobject *)class;
inst->in_attr = newdictobject();
if (inst->in_attr == NULL) {
DECREF(inst);
1990-10-14 12:07:46 +00:00
return NULL;
}
return (object *)inst;
1990-10-14 12:07:46 +00:00
}
/* Instance methods */
1990-10-14 12:07:46 +00:00
static void
instance_dealloc(inst)
register instanceobject *inst;
1990-10-14 12:07:46 +00:00
{
DECREF(inst->in_class);
if (inst->in_attr != NULL)
DECREF(inst->in_attr);
free((ANY *)inst);
1990-10-14 12:07:46 +00:00
}
static object *
instance_getattr(inst, name)
register instanceobject *inst;
1990-10-14 12:07:46 +00:00
register char *name;
{
1991-10-20 20:11:48 +00:00
register object *v;
if (strcmp(name, "__dict__") == 0) {
INCREF(inst->in_attr);
return inst->in_attr;
}
if (strcmp(name, "__class__") == 0) {
INCREF(inst->in_class);
return (object *)inst->in_class;
}
v = dictlookup(inst->in_attr, name);
1990-10-14 12:07:46 +00:00
if (v != NULL) {
INCREF(v);
return v;
}
v = class_getattr(inst->in_class, name);
1990-10-14 12:07:46 +00:00
if (v == NULL)
1990-10-21 22:15:08 +00:00
return v; /* class_getattr() has set the error */
1990-10-14 12:07:46 +00:00
if (is_funcobject(v)) {
object *w = newinstancemethodobject(v, (object *)inst);
1990-10-14 12:07:46 +00:00
DECREF(v);
return w;
}
DECREF(v);
err_setstr(AttributeError, name);
1990-10-14 12:07:46 +00:00
return NULL;
}
static int
instance_setattr(inst, name, v)
instanceobject *inst;
1990-10-14 12:07:46 +00:00
char *name;
object *v;
{
if (name[0] == '_' && name[1] == '_') {
int n = strlen(name);
if (name[n-1] == '_' && name[n-2] == '_') {
err_setstr(TypeError, "read-only special attribute");
return -1;
}
}
if (v == NULL) {
int rv = dictremove(inst->in_attr, name);
if (rv < 0)
err_setstr(AttributeError,
"delete non-existing instance attribute");
return rv;
}
1990-10-14 12:07:46 +00:00
else
return dictinsert(inst->in_attr, name, v);
1990-10-14 12:07:46 +00:00
}
static object *
instance_repr(inst)
instanceobject *inst;
{
object *func;
object *res;
func = instance_getattr(inst, "__repr__");
if (func == NULL) {
char buf[80];
err_clear();
sprintf(buf, "<instance object at %lx>", (long)inst);
return newstringobject(buf);
}
res = call_object(func, (object *)NULL);
DECREF(func);
return res;
}
static int
instance_compare(inst, other)
instanceobject *inst, *other;
{
object *func;
object *res;
int outcome;
func = instance_getattr(inst, "__cmp__");
if (func == NULL) {
err_clear();
if (inst < other)
return -1;
if (inst > other)
return 1;
return 0;
}
res = call_object(func, (object *)other);
DECREF(func);
if (res == NULL) {
err_clear(); /* XXX Should report the error, bot how...??? */
return 0;
}
if (is_intobject(res))
outcome = getintvalue(res);
else
outcome = 0; /* XXX Should report the error, bot how...??? */
DECREF(res);
return outcome;
}
static long
instance_hash(inst)
instanceobject *inst;
{
object *func;
object *res;
long outcome;
func = instance_getattr(inst, "__hash__");
if (func == NULL) {
/* If there is no __cmp__ method, we hash on the address.
If a __cmp__ method exists, there must be a __hash__. */
err_clear();
func = instance_getattr(inst, "__cmp__");
if (func == NULL) {
err_clear();
outcome = (long)inst;
if (outcome == -1)
outcome = -2;
return outcome;
}
err_setstr(TypeError, "unhashable instance");
return -1;
}
res = call_object(func, (object *)NULL);
DECREF(func);
if (res == NULL)
return -1;
if (is_intobject(res)) {
outcome = getintvalue(res);
if (outcome == -1)
outcome = -2;
}
else {
err_setstr(TypeError, "__hash__() should return an int");
outcome = -1;
}
DECREF(res);
return outcome;
}
static int
instance_length(inst)
instanceobject *inst;
{
object *func;
object *res;
int outcome;
func = instance_getattr(inst, "__len__");
if (func == NULL)
return -1;
res = call_object(func, (object *)NULL);
DECREF(func);
if (res == NULL)
return -1;
if (is_intobject(res)) {
outcome = getintvalue(res);
if (outcome < 0)
err_setstr(ValueError, "__len__() should return >= 0");
}
else {
err_setstr(TypeError, "__len__() should return an int");
outcome = -1;
}
DECREF(res);
return outcome;
}
static object *
instance_subscript(inst, key)
instanceobject *inst;
object *key;
{
object *func;
object *arg;
object *res;
func = instance_getattr(inst, "__getitem__");
if (func == NULL)
return NULL;
arg = mkvalue("(O)", key);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static int
instance_ass_subscript(inst, key, value)
instanceobject*inst;
object *key;
object *value;
{
object *func;
object *arg;
object *res;
if (value == NULL)
func = instance_getattr(inst, "__delitem__");
else
func = instance_getattr(inst, "__setitem__");
if (func == NULL)
return -1;
if (value == NULL)
arg = mkvalue("(O)", key);
else
arg = mkvalue("(OO)", key, value);
if (arg == NULL) {
DECREF(func);
return -1;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
if (res == NULL)
return -1;
DECREF(res);
return 0;
}
static mapping_methods instance_as_mapping = {
instance_length, /*mp_length*/
instance_subscript, /*mp_subscript*/
instance_ass_subscript, /*mp_ass_subscript*/
};
static object *
instance_concat(inst, other)
instanceobject *inst, *other;
{
object *func, *arg, *res;
func = instance_getattr(inst, "__add__");
if (func == NULL)
return NULL;
arg = mkvalue("(O)", other);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static object *
instance_repeat(inst, count)
instanceobject *inst;
int count;
{
object *func, *arg, *res;
func = instance_getattr(inst, "__mul__");
if (func == NULL)
return NULL;
arg = newintobject((long)count);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static object *
instance_item(inst, i)
instanceobject *inst;
int i;
{
object *func, *arg, *res;
func = instance_getattr(inst, "__getitem__");
if (func == NULL)
return NULL;
arg = newintobject((long)i);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static object *
instance_slice(inst, i, j)
instanceobject *inst;
int i, j;
{
object *func, *arg, *res;
func = instance_getattr(inst, "__getslice__");
if (func == NULL)
return NULL;
arg = mkvalue("(ii)", i, j);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static int
instance_ass_item(inst, i, item)
instanceobject *inst;
int i;
object *item;
{
object *func, *arg, *res;
if (item == NULL)
func = instance_getattr(inst, "__delitem__");
else
func = instance_getattr(inst, "__setitem__");
if (func == NULL)
return -1;
if (item == NULL)
arg = mkvalue("i", i);
else
arg = mkvalue("(iO)", i, item);
if (arg == NULL) {
DECREF(func);
return -1;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
if (res == NULL)
return -1;
DECREF(res);
return 0;
}
static int
instance_ass_slice(inst, i, j, value)
instanceobject *inst;
int i, j;
object *value;
{
object *func, *arg, *res;
if (value == NULL)
func = instance_getattr(inst, "__delslice__");
else
func = instance_getattr(inst, "__setslice__");
if (func == NULL)
return -1;
if (value == NULL)
arg = mkvalue("(ii)", i, j);
else
arg = mkvalue("(iiO)", i, j, value);
if (arg == NULL) {
DECREF(func);
return -1;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
if (res == NULL)
return -1;
DECREF(res);
return 0;
}
static sequence_methods instance_as_sequence = {
instance_length, /*sq_length*/
instance_concat, /*sq_concat*/
instance_repeat, /*sq_repeat*/
instance_item, /*sq_item*/
instance_slice, /*sq_slice*/
instance_ass_item, /*sq_ass_item*/
instance_ass_slice, /*sq_ass_slice*/
};
static object *
generic_binary_op(self, other, methodname)
instanceobject *self;
object *other;
char *methodname;
{
object *func, *arg, *res;
if ((func = instance_getattr(self, methodname)) == NULL)
return NULL;
arg = mkvalue("O", other);
if (arg == NULL) {
DECREF(func);
return NULL;
}
res = call_object(func, arg);
DECREF(func);
DECREF(arg);
return res;
}
static object *
generic_unary_op(self, methodname)
instanceobject *self;
char *methodname;
{
object *func, *res;
if ((func = instance_getattr(self, methodname)) == NULL)
return NULL;
res = call_object(func, (object *)NULL);
DECREF(func);
return res;
}
#define BINARY(funcname, methodname) \
static object * funcname(self, other) instanceobject *self; object *other; { \
return generic_binary_op(self, other, methodname); \
}
#define UNARY(funcname, methodname) \
static object *funcname(self) instanceobject *self; { \
return generic_unary_op(self, methodname); \
}
BINARY(instance_add, "__add__")
BINARY(instance_sub, "__sub__")
BINARY(instance_mul, "__mul__")
BINARY(instance_div, "__div__")
BINARY(instance_mod, "__mod__")
BINARY(instance_divmod, "__divmod__")
BINARY(instance_pow, "__pow__")
UNARY(instance_neg, "__neg__")
UNARY(instance_pos, "__pos__")
UNARY(instance_abs, "__abs__")
static int
instance_nonzero(self)
instanceobject *self;
{
object *func, *res;
long outcome;
if ((func = instance_getattr(self, "__nonzero__")) == NULL) {
err_clear();
if ((func = instance_getattr(self, "__len__")) == NULL) {
err_clear();
/* Fall back to the default behavior:
all instances are nonzero */
return 1;
}
}
res = call_object(func, (object *)NULL);
DECREF(func);
if (res == NULL)
return -1;
if (!is_intobject(res)) {
DECREF(res);
err_setstr(TypeError, "__nonzero__ should return an int");
return -1;
}
outcome = getintvalue(res);
DECREF(res);
if (outcome < 0) {
err_setstr(ValueError, "__nonzero__ should return >= 0");
return -1;
}
return outcome > 0;
}
UNARY(instance_invert, "__invert__")
BINARY(instance_lshift, "__lshift__")
BINARY(instance_rshift, "__rshift__")
BINARY(instance_and, "__and__")
BINARY(instance_xor, "__xor__")
BINARY(instance_or, "__or__")
static int
instance_coerce(pv, pw)
object **pv, **pw;
{
object *v = *pv;
object *w = *pw;
object *func;
object *res;
int outcome;
if (!is_instanceobject(v))
return 1; /* XXX shouldn't be possible */
func = instance_getattr((instanceobject *)v, "__coerce__");
if (func == NULL) {
err_clear();
return 1;
}
res = call_object(func, w);
if (res == NULL)
return -1;
if (res == None) {
DECREF(res);
return 1;
}
outcome = getargs(res, "(OO)", &v, &w);
if (!outcome || v->ob_type != w->ob_type ||
v->ob_type->tp_as_number == NULL) {
DECREF(res);
err_setstr(TypeError, "bad __coerce__ result");
return -1;
}
INCREF(v);
INCREF(w);
DECREF(res);
*pv = v;
*pw = w;
return 0;
}
UNARY(instance_int, "__int__")
UNARY(instance_long, "__long__")
UNARY(instance_float, "__float__")
UNARY(instance_oct, "__oct__")
UNARY(instance_hex, "__hex__")
static number_methods instance_as_number = {
instance_add, /*nb_add*/
instance_sub, /*nb_subtract*/
instance_mul, /*nb_multiply*/
instance_div, /*nb_divide*/
instance_mod, /*nb_remainder*/
instance_divmod, /*nb_divmod*/
instance_pow, /*nb_power*/
instance_neg, /*nb_negative*/
instance_pos, /*nb_positive*/
instance_abs, /*nb_absolute*/
instance_nonzero, /*nb_nonzero*/
instance_invert, /*nb_invert*/
instance_lshift, /*nb_lshift*/
instance_rshift, /*nb_rshift*/
instance_and, /*nb_and*/
instance_xor, /*nb_xor*/
instance_or, /*nb_or*/
instance_coerce, /*nb_coerce*/
instance_int, /*nb_int*/
instance_long, /*nb_long*/
instance_float, /*nb_float*/
instance_oct, /*nb_oct*/
instance_hex, /*nb_hex*/
};
typeobject Instancetype = {
1990-10-14 12:07:46 +00:00
OB_HEAD_INIT(&Typetype)
0,
"instance",
sizeof(instanceobject),
1990-10-14 12:07:46 +00:00
0,
instance_dealloc, /*tp_dealloc*/
0, /*tp_print*/
instance_getattr, /*tp_getattr*/
instance_setattr, /*tp_setattr*/
instance_compare, /*tp_compare*/
instance_repr, /*tp_repr*/
&instance_as_number, /*tp_as_number*/
&instance_as_sequence, /*tp_as_sequence*/
&instance_as_mapping, /*tp_as_mapping*/
instance_hash, /*tp_hash*/
1990-10-14 12:07:46 +00:00
};
static object *
instance_convert(inst, methodname)
object *inst;
char *methodname;
{
return generic_unary_op((instanceobject *)inst, methodname);
}
1990-10-14 12:07:46 +00:00
1991-10-20 20:11:48 +00:00
/* And finally, here are instance method objects */
1990-10-14 12:07:46 +00:00
typedef struct {
OB_HEAD
object *im_func; /* The method function */
object *im_self; /* The object to which this applies */
} instancemethodobject;
1990-10-14 12:07:46 +00:00
object *
newinstancemethodobject(func, self)
1990-10-14 12:07:46 +00:00
object *func;
object *self;
{
register instancemethodobject *im;
1990-10-14 12:07:46 +00:00
if (!is_funcobject(func)) {
1990-10-21 22:15:08 +00:00
err_badcall();
1990-10-14 12:07:46 +00:00
return NULL;
}
im = NEWOBJ(instancemethodobject, &Instancemethodtype);
if (im == NULL)
1990-10-14 12:07:46 +00:00
return NULL;
INCREF(func);
im->im_func = func;
1990-10-14 12:07:46 +00:00
INCREF(self);
im->im_self = self;
return (object *)im;
1990-10-14 12:07:46 +00:00
}
object *
instancemethodgetfunc(im)
register object *im;
1990-10-14 12:07:46 +00:00
{
if (!is_instancemethodobject(im)) {
1990-10-21 22:15:08 +00:00
err_badcall();
1990-10-14 12:07:46 +00:00
return NULL;
}
return ((instancemethodobject *)im)->im_func;
1990-10-14 12:07:46 +00:00
}
object *
instancemethodgetself(im)
register object *im;
1990-10-14 12:07:46 +00:00
{
if (!is_instancemethodobject(im)) {
1990-10-21 22:15:08 +00:00
err_badcall();
1990-10-14 12:07:46 +00:00
return NULL;
}
return ((instancemethodobject *)im)->im_self;
1990-10-14 12:07:46 +00:00
}
/* Class method methods */
#define OFF(x) offsetof(instancemethodobject, x)
1990-12-20 15:06:42 +00:00
static struct memberlist instancemethod_memberlist[] = {
{"im_func", T_OBJECT, OFF(im_func)},
{"im_self", T_OBJECT, OFF(im_self)},
1990-12-20 15:06:42 +00:00
{NULL} /* Sentinel */
};
static object *
instancemethod_getattr(im, name)
register instancemethodobject *im;
1990-12-20 15:06:42 +00:00
char *name;
{
return getmember((char *)im, instancemethod_memberlist, name);
1990-12-20 15:06:42 +00:00
}
1990-10-14 12:07:46 +00:00
static void
instancemethod_dealloc(im)
register instancemethodobject *im;
1990-10-14 12:07:46 +00:00
{
DECREF(im->im_func);
DECREF(im->im_self);
free((ANY *)im);
1990-10-14 12:07:46 +00:00
}
static int
instancemethod_compare(a, b)
instancemethodobject *a, *b;
{
int cmp = cmpobject(a->im_self, b->im_self);
if (cmp == 0)
cmp = cmpobject(a->im_func, b->im_func);
return cmp;
}
static long
instancemethod_hash(a)
instancemethodobject *a;
{
long x, y;
x = hashobject(a->im_self);
if (x == -1)
return -1;
y = hashobject(a->im_func);
if (y == -1)
return -1;
return x ^ y;
}
typeobject Instancemethodtype = {
1990-10-14 12:07:46 +00:00
OB_HEAD_INIT(&Typetype)
0,
"instance method",
sizeof(instancemethodobject),
1990-10-14 12:07:46 +00:00
0,
instancemethod_dealloc, /*tp_dealloc*/
1990-10-14 12:07:46 +00:00
0, /*tp_print*/
instancemethod_getattr, /*tp_getattr*/
1990-10-14 12:07:46 +00:00
0, /*tp_setattr*/
instancemethod_compare, /*tp_compare*/
1990-10-14 12:07:46 +00:00
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
instancemethod_hash, /*tp_hash*/
1990-10-14 12:07:46 +00:00
};