1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1992-04-05 11:24:32 -03: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;
|
1991-12-10 09:53:23 -04:00
|
|
|
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);
|
1991-12-10 09:53:23 -04:00
|
|
|
if (op == NULL) {
|
|
|
|
DECREF(bases);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1991-12-10 09:53:23 -04:00
|
|
|
}
|
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;
|
|
|
|
{
|
1991-12-10 09:53:23 -04:00
|
|
|
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;
|
|
|
|
}
|
1991-12-10 09:53:23 -04:00
|
|
|
{
|
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 *)
|
1992-04-05 11:24:32 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
1991-12-10 09:53:23 -04: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;
|
|
|
|
{
|
1992-04-05 11:24:32 -03:00
|
|
|
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*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
/* We're not done yet: next, we define instance objects... */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
1991-05-05 17:03:07 -03:00
|
|
|
classobject *in_class; /* The class object */
|
|
|
|
object *in_attr; /* A dictionary */
|
1991-04-04 06:42:10 -04:00
|
|
|
} instanceobject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
object *
|
1991-04-04 06:42:10 -04:00
|
|
|
newinstanceobject(class)
|
1990-10-14 09:07:46 -03:00
|
|
|
register object *class;
|
|
|
|
{
|
1991-05-05 17:03:07 -03:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
inst = NEWOBJ(instanceobject, &Instancetype);
|
|
|
|
if (inst == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
INCREF(class);
|
1991-05-05 17:03:07 -03:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
return (object *)inst;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
/* Instance methods */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
static void
|
1991-05-05 17:03:07 -03:00
|
|
|
instance_dealloc(inst)
|
|
|
|
register instanceobject *inst;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-05-05 17:03:07 -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 *
|
1991-05-05 17:03:07 -03:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
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)) {
|
1991-05-05 17:03:07 -03:00
|
|
|
object *w = newinstancemethodobject(v, (object *)inst);
|
1990-10-14 09:07:46 -03:00
|
|
|
DECREF(v);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
DECREF(v);
|
1991-12-10 09:53:23 -04:00
|
|
|
err_setstr(AttributeError, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1991-05-05 17:03:07 -03:00
|
|
|
instance_setattr(inst, name, v)
|
|
|
|
instanceobject *inst;
|
1990-10-14 09:07:46 -03:00
|
|
|
char *name;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-04-05 11:24:32 -03:00
|
|
|
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)
|
1991-05-05 17:03:07 -03:00
|
|
|
return dictremove(inst->in_attr, name);
|
1990-10-14 09:07:46 -03:00
|
|
|
else
|
1991-05-05 17:03:07 -03:00
|
|
|
return dictinsert(inst->in_attr, name, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-04-04 06:42:10 -04:00
|
|
|
typeobject Instancetype = {
|
1990-10-14 09:07:46 -03:00
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
"instance",
|
|
|
|
sizeof(instanceobject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
instance_dealloc, /*tp_dealloc*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_print*/
|
1991-04-04 06:42:10 -04:00
|
|
|
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
|
1991-05-05 17:03:07 -03:00
|
|
|
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 *
|
1991-05-05 17:03:07 -03:00
|
|
|
newinstancemethodobject(func, self)
|
1990-10-14 09:07:46 -03:00
|
|
|
object *func;
|
|
|
|
object *self;
|
|
|
|
{
|
1991-05-05 17:03:07 -03:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
im = NEWOBJ(instancemethodobject, &Instancemethodtype);
|
|
|
|
if (im == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
INCREF(func);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_func = func;
|
1990-10-14 09:07:46 -03:00
|
|
|
INCREF(self);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_self = self;
|
|
|
|
return (object *)im;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethodgetfunc(im)
|
|
|
|
register object *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-05-05 17:03:07 -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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
return ((instancemethodobject *)im)->im_func;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethodgetself(im)
|
|
|
|
register object *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-05-05 17:03:07 -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;
|
|
|
|
}
|
1991-05-05 17:03:07 -03:00
|
|
|
return ((instancemethodobject *)im)->im_self;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Class method methods */
|
|
|
|
|
1991-05-05 17:03:07 -03:00
|
|
|
#define OFF(x) offsetof(instancemethodobject, x)
|
1990-12-20 11:06:42 -04:00
|
|
|
|
1991-05-05 17:03:07 -03: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 *
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethod_getattr(im, name)
|
|
|
|
register instancemethodobject *im;
|
1990-12-20 11:06:42 -04:00
|
|
|
char *name;
|
|
|
|
{
|
1991-05-05 17:03:07 -03:00
|
|
|
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
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethod_dealloc(im)
|
|
|
|
register instancemethodobject *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-05-05 17:03:07 -03:00
|
|
|
DECREF(im->im_func);
|
|
|
|
DECREF(im->im_self);
|
|
|
|
free((ANY *)im);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-05-05 17:03:07 -03:00
|
|
|
typeobject Instancemethodtype = {
|
1990-10-14 09:07:46 -03:00
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1991-04-16 05:38:43 -03:00
|
|
|
"instance method",
|
1991-05-05 17:03:07 -03:00
|
|
|
sizeof(instancemethodobject),
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethod_dealloc, /*tp_dealloc*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_print*/
|
1991-05-05 17:03:07 -03:00
|
|
|
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*/
|
|
|
|
};
|