cpython/Objects/classobject.c

360 lines
7.5 KiB
C
Raw Normal View History

1991-02-19 08:39:46 -04:00
/***********************************************************
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
1991-02-19 08:39:46 -04:00
Netherlands.
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 09:07:46 -03:00
/* Class object implementation */
1990-12-20 11:06:42 -04:00
#include "allobjects.h"
1990-10-14 09:07:46 -03:00
1990-12-20 11:06:42 -04:00
#include "structmember.h"
1990-10-14 09:07:46 -03:00
typedef struct {
OB_HEAD
object *cl_bases; /* A tuple */
object *cl_methods; /* A dictionary */
1991-10-20 17:11:48 -03:00
object *cl_name; /* A string */
1990-10-14 09:07:46 -03:00
} classobject;
object *
1991-10-20 17:11:48 -03:00
newclassobject(bases, methods, name)
1990-10-14 09:07:46 -03:00
object *bases; /* NULL or tuple of classobjects! */
object *methods;
1991-10-20 17:11:48 -03:00
object *name; /* String; NULL if unknown */
1990-10-14 09:07:46 -03:00
{
classobject *op;
if (bases == NULL) {
bases = newtupleobject(0);
if (bases == NULL)
return err_nomem();
}
else
INCREF(bases);
1990-10-14 09:07:46 -03:00
op = NEWOBJ(classobject, &Classtype);
if (op == NULL) {
DECREF(bases);
1990-10-14 09:07:46 -03:00
return NULL;
}
1990-10-14 09:07:46 -03:00
op->cl_bases = bases;
INCREF(methods);
op->cl_methods = methods;
1991-10-20 17:11:48 -03:00
XINCREF(name);
op->cl_name = name;
1990-10-14 09:07:46 -03:00
return (object *) op;
}
/* Class methods */
static void
class_dealloc(op)
classobject *op;
{
DECREF(op->cl_bases);
1990-10-14 09:07:46 -03:00
DECREF(op->cl_methods);
1991-10-20 17:11:48 -03:00
XDECREF(op->cl_name);
1990-10-14 09:07:46 -03:00
free((ANY *)op);
}
static object *
class_getattr(op, name)
register classobject *op;
register char *name;
{
register object *v;
1991-10-20 17:11:48 -03: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 09:07:46 -03:00
v = dictlookup(op->cl_methods, name);
if (v != NULL) {
INCREF(v);
return v;
}
{
1990-10-14 09:07:46 -03:00
int n = gettuplesize(op->cl_bases);
int i;
for (i = 0; i < n; i++) {
1992-03-27 13:23:48 -04:00
v = class_getattr((classobject *)
gettupleitem(op->cl_bases, i), name);
1990-10-14 09:07:46 -03:00
if (v != NULL)
return v;
1990-12-20 11:06:42 -04:00
err_clear();
1990-10-14 09:07:46 -03:00
}
}
err_setstr(AttributeError, name);
1990-10-14 09:07:46 -03:00
return NULL;
}
1991-10-20 17:11:48 -03: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;
}
}
1991-10-20 17:11:48 -03:00
if (v == NULL)
return dictremove(op->cl_methods, name);
else
return dictinsert(op->cl_methods, name, v);
}
1990-10-14 09:07:46 -03: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 17:11:48 -03:00
class_setattr, /*tp_setattr*/
1990-10-14 09:07:46 -03: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 09:07:46 -03:00
typedef struct {
OB_HEAD
classobject *in_class; /* The class object */
object *in_attr; /* A dictionary */
} instanceobject;
1990-10-14 09:07:46 -03:00
object *
newinstanceobject(class)
1990-10-14 09:07:46 -03:00
register object *class;
{
register instanceobject *inst;
1990-10-14 09:07:46 -03:00
if (!is_classobject(class)) {
1990-10-21 19:15:08 -03:00
err_badcall();
1990-10-14 09:07:46 -03:00
return NULL;
}
inst = NEWOBJ(instanceobject, &Instancetype);
if (inst == NULL)
1990-10-14 09:07:46 -03:00
return NULL;
INCREF(class);
inst->in_class = (classobject *)class;
inst->in_attr = newdictobject();
if (inst->in_attr == NULL) {
DECREF(inst);
1990-10-14 09:07:46 -03:00
return NULL;
}
return (object *)inst;
1990-10-14 09:07:46 -03:00
}
/* Instance methods */
1990-10-14 09:07:46 -03:00
static void
instance_dealloc(inst)
register instanceobject *inst;
1990-10-14 09:07:46 -03:00
{
DECREF(inst->in_class);
if (inst->in_attr != NULL)
DECREF(inst->in_attr);
free((ANY *)inst);
1990-10-14 09:07:46 -03:00
}
static object *
instance_getattr(inst, name)
register instanceobject *inst;
1990-10-14 09:07:46 -03:00
register char *name;
{
1991-10-20 17:11:48 -03: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 09:07:46 -03:00
if (v != NULL) {
INCREF(v);
return v;
}
v = class_getattr(inst->in_class, name);
1990-10-14 09:07:46 -03:00
if (v == NULL)
1990-10-21 19:15:08 -03:00
return v; /* class_getattr() has set the error */
1990-10-14 09:07:46 -03:00
if (is_funcobject(v)) {
object *w = newinstancemethodobject(v, (object *)inst);
1990-10-14 09:07:46 -03:00
DECREF(v);
return w;
}
DECREF(v);
err_setstr(AttributeError, name);
1990-10-14 09:07:46 -03:00
return NULL;
}
static int
instance_setattr(inst, name, v)
instanceobject *inst;
1990-10-14 09:07:46 -03: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;
}
}
1990-10-14 09:07:46 -03:00
if (v == NULL)
return dictremove(inst->in_attr, name);
1990-10-14 09:07:46 -03:00
else
return dictinsert(inst->in_attr, name, v);
1990-10-14 09:07:46 -03:00
}
typeobject Instancetype = {
1990-10-14 09:07:46 -03:00
OB_HEAD_INIT(&Typetype)
0,
"instance",
sizeof(instanceobject),
1990-10-14 09:07:46 -03:00
0,
instance_dealloc, /*tp_dealloc*/
1990-10-14 09:07:46 -03:00
0, /*tp_print*/
instance_getattr, /*tp_getattr*/
instance_setattr, /*tp_setattr*/
1990-10-14 09:07:46 -03:00
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};
1991-10-20 17:11:48 -03:00
/* And finally, here are instance method objects */
1990-10-14 09:07:46 -03:00
typedef struct {
OB_HEAD
object *im_func; /* The method function */
object *im_self; /* The object to which this applies */
} instancemethodobject;
1990-10-14 09:07:46 -03:00
object *
newinstancemethodobject(func, self)
1990-10-14 09:07:46 -03:00
object *func;
object *self;
{
register instancemethodobject *im;
1990-10-14 09:07:46 -03:00
if (!is_funcobject(func)) {
1990-10-21 19:15:08 -03:00
err_badcall();
1990-10-14 09:07:46 -03:00
return NULL;
}
im = NEWOBJ(instancemethodobject, &Instancemethodtype);
if (im == NULL)
1990-10-14 09:07:46 -03:00
return NULL;
INCREF(func);
im->im_func = func;
1990-10-14 09:07:46 -03:00
INCREF(self);
im->im_self = self;
return (object *)im;
1990-10-14 09:07:46 -03:00
}
object *
instancemethodgetfunc(im)
register object *im;
1990-10-14 09:07:46 -03:00
{
if (!is_instancemethodobject(im)) {
1990-10-21 19:15:08 -03:00
err_badcall();
1990-10-14 09:07:46 -03:00
return NULL;
}
return ((instancemethodobject *)im)->im_func;
1990-10-14 09:07:46 -03:00
}
object *
instancemethodgetself(im)
register object *im;
1990-10-14 09:07:46 -03:00
{
if (!is_instancemethodobject(im)) {
1990-10-21 19:15:08 -03:00
err_badcall();
1990-10-14 09:07:46 -03:00
return NULL;
}
return ((instancemethodobject *)im)->im_self;
1990-10-14 09:07:46 -03:00
}
/* Class method methods */
#define OFF(x) offsetof(instancemethodobject, x)
1990-12-20 11:06:42 -04:00
static struct memberlist instancemethod_memberlist[] = {
{"im_func", T_OBJECT, OFF(im_func)},
{"im_self", T_OBJECT, OFF(im_self)},
1990-12-20 11:06:42 -04:00
{NULL} /* Sentinel */
};
static object *
instancemethod_getattr(im, name)
register instancemethodobject *im;
1990-12-20 11:06:42 -04:00
char *name;
{
return getmember((char *)im, instancemethod_memberlist, name);
1990-12-20 11:06:42 -04:00
}
1990-10-14 09:07:46 -03:00
static void
instancemethod_dealloc(im)
register instancemethodobject *im;
1990-10-14 09:07:46 -03:00
{
DECREF(im->im_func);
DECREF(im->im_self);
free((ANY *)im);
1990-10-14 09:07:46 -03:00
}
typeobject Instancemethodtype = {
1990-10-14 09:07:46 -03:00
OB_HEAD_INIT(&Typetype)
0,
"instance method",
sizeof(instancemethodobject),
1990-10-14 09:07:46 -03:00
0,
instancemethod_dealloc, /*tp_dealloc*/
1990-10-14 09:07:46 -03:00
0, /*tp_print*/
instancemethod_getattr, /*tp_getattr*/
1990-10-14 09:07:46 -03:00
0, /*tp_setattr*/
0, /*tp_compare*/
0, /*tp_repr*/
0, /*tp_as_number*/
0, /*tp_as_sequence*/
0, /*tp_as_mapping*/
};