1991-02-19 12:39:46 +00:00
|
|
|
/***********************************************************
|
1992-04-05 14:24:32 +00:00
|
|
|
Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
|
1991-02-19 12:39:46 +00: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 12:07:46 +00:00
|
|
|
/* Class object implementation */
|
|
|
|
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "allobjects.h"
|
1992-08-12 15:35:34 +00:00
|
|
|
#include "modsupport.h"
|
1990-12-20 15:06:42 +00:00
|
|
|
#include "structmember.h"
|
1992-08-12 15:35:34 +00:00
|
|
|
#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;
|
1991-12-10 13:53:23 +00:00
|
|
|
if (bases == NULL) {
|
|
|
|
bases = newtupleobject(0);
|
|
|
|
if (bases == NULL)
|
|
|
|
return err_nomem();
|
|
|
|
}
|
|
|
|
else
|
|
|
|
INCREF(bases);
|
1990-10-14 12:07:46 +00:00
|
|
|
op = NEWOBJ(classobject, &Classtype);
|
1991-12-10 13:53:23 +00:00
|
|
|
if (op == NULL) {
|
|
|
|
DECREF(bases);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
1991-12-10 13:53:23 +00:00
|
|
|
}
|
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;
|
|
|
|
{
|
1991-12-10 13:53:23 +00:00
|
|
|
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;
|
|
|
|
}
|
1991-12-10 13:53:23 +00:00
|
|
|
{
|
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 *)
|
1992-04-05 14:24:32 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
}
|
1991-12-10 13:53:23 +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;
|
|
|
|
{
|
1992-04-05 14:24:32 +00: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;
|
|
|
|
}
|
|
|
|
}
|
1992-09-04 09:45:18 +00:00
|
|
|
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*/
|
|
|
|
};
|
|
|
|
|
|
|
|
|
1991-04-04 10:42:10 +00:00
|
|
|
/* We're not done yet: next, we define instance objects... */
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
1991-05-05 20:03:07 +00:00
|
|
|
classobject *in_class; /* The class object */
|
|
|
|
object *in_attr; /* A dictionary */
|
1991-04-04 10:42:10 +00:00
|
|
|
} instanceobject;
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
object *
|
1991-04-04 10:42:10 +00:00
|
|
|
newinstanceobject(class)
|
1990-10-14 12:07:46 +00:00
|
|
|
register object *class;
|
|
|
|
{
|
1991-05-05 20:03:07 +00:00
|
|
|
register instanceobject *inst;
|
1992-08-12 15:35:34 +00:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
inst = NEWOBJ(instanceobject, &Instancetype);
|
|
|
|
if (inst == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
INCREF(class);
|
1991-05-05 20:03:07 +00:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
return (object *)inst;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1991-04-04 10:42:10 +00:00
|
|
|
/* Instance methods */
|
1990-10-14 12:07:46 +00:00
|
|
|
|
|
|
|
static void
|
1991-05-05 20:03:07 +00:00
|
|
|
instance_dealloc(inst)
|
|
|
|
register instanceobject *inst;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-05-05 20:03:07 +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 *
|
1991-05-05 20:03:07 +00:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
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)) {
|
1991-05-05 20:03:07 +00:00
|
|
|
object *w = newinstancemethodobject(v, (object *)inst);
|
1990-10-14 12:07:46 +00:00
|
|
|
DECREF(v);
|
|
|
|
return w;
|
|
|
|
}
|
|
|
|
DECREF(v);
|
1991-12-10 13:53:23 +00:00
|
|
|
err_setstr(AttributeError, name);
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
1991-05-05 20:03:07 +00:00
|
|
|
instance_setattr(inst, name, v)
|
|
|
|
instanceobject *inst;
|
1990-10-14 12:07:46 +00:00
|
|
|
char *name;
|
|
|
|
object *v;
|
|
|
|
{
|
1992-04-05 14:24:32 +00: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;
|
|
|
|
}
|
|
|
|
}
|
1992-09-04 09:45:18 +00:00
|
|
|
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
|
1991-05-05 20:03:07 +00:00
|
|
|
return dictinsert(inst->in_attr, name, v);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1992-08-12 15:35:34 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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 (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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
1992-08-14 13:49:30 +00:00
|
|
|
return -1;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
res = call_object(func, arg);
|
|
|
|
DECREF(func);
|
|
|
|
DECREF(arg);
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
{
|
1992-08-14 12:06:52 +00:00
|
|
|
object *func, *arg, *res;
|
1992-08-12 15:35:34 +00:00
|
|
|
|
|
|
|
func = instance_getattr(inst, "__add__");
|
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
1992-08-14 12:06:52 +00:00
|
|
|
arg = mkvalue("(O)", other);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = call_object(func, arg);
|
1992-08-12 15:35:34 +00:00
|
|
|
DECREF(func);
|
1992-08-14 12:06:52 +00:00
|
|
|
DECREF(arg);
|
1992-08-12 15:35:34 +00:00
|
|
|
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)
|
1992-08-14 13:49:30 +00:00
|
|
|
return -1;
|
1992-08-12 15:35:34 +00:00
|
|
|
if (item == NULL)
|
|
|
|
arg = mkvalue("i", i);
|
|
|
|
else
|
|
|
|
arg = mkvalue("(iO)", i, item);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
1992-08-14 13:49:30 +00:00
|
|
|
return -1;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
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)
|
1992-08-14 13:49:30 +00:00
|
|
|
return -1;
|
1992-08-12 15:35:34 +00:00
|
|
|
if (value == NULL)
|
|
|
|
arg = mkvalue("(ii)", i, j);
|
|
|
|
else
|
|
|
|
arg = mkvalue("(iiO)", i, j, value);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
1992-08-14 13:49:30 +00:00
|
|
|
return -1;
|
1992-08-12 15:35:34 +00:00
|
|
|
}
|
|
|
|
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;
|
|
|
|
{
|
1992-08-14 12:06:52 +00:00
|
|
|
object *func, *arg, *res;
|
1992-08-12 15:35:34 +00:00
|
|
|
|
|
|
|
if ((func = instance_getattr(self, methodname)) == NULL)
|
|
|
|
return NULL;
|
1992-08-14 12:06:52 +00:00
|
|
|
arg = mkvalue("O", other);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
res = call_object(func, arg);
|
1992-08-12 15:35:34 +00:00
|
|
|
DECREF(func);
|
1992-08-14 12:06:52 +00:00
|
|
|
DECREF(arg);
|
1992-08-12 15:35:34 +00:00
|
|
|
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__")
|
|
|
|
|
|
|
|
int
|
|
|
|
instance_nonzero(self)
|
|
|
|
instanceobject *self;
|
|
|
|
{
|
|
|
|
object *func, *res;
|
|
|
|
long outcome;
|
|
|
|
|
|
|
|
if ((func = instance_getattr(self, "__len__")) == NULL) {
|
|
|
|
err_clear();
|
|
|
|
if ((func = instance_getattr(self, "__nonzero__")) == 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__")
|
|
|
|
|
1992-08-14 12:06:52 +00:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1992-09-12 11:09:23 +00:00
|
|
|
UNARY(instance_int, "__int__")
|
|
|
|
UNARY(instance_long, "__long__")
|
|
|
|
UNARY(instance_float, "__float__")
|
|
|
|
UNARY(instance_oct, "__oct__")
|
|
|
|
UNARY(instance_hex, "__hex__")
|
|
|
|
|
1992-08-12 15:35:34 +00:00
|
|
|
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*/
|
1992-08-14 12:06:52 +00:00
|
|
|
instance_coerce, /*nb_coerce*/
|
1992-09-12 11:09:23 +00:00
|
|
|
instance_int, /*nb_int*/
|
|
|
|
instance_long, /*nb_long*/
|
|
|
|
instance_float, /*nb_float*/
|
|
|
|
instance_oct, /*nb_oct*/
|
|
|
|
instance_hex, /*nb_hex*/
|
1992-08-12 15:35:34 +00:00
|
|
|
};
|
|
|
|
|
1991-04-04 10:42:10 +00:00
|
|
|
typeobject Instancetype = {
|
1990-10-14 12:07:46 +00:00
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1991-04-04 10:42:10 +00:00
|
|
|
"instance",
|
|
|
|
sizeof(instanceobject),
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
1991-04-04 10:42:10 +00:00
|
|
|
instance_dealloc, /*tp_dealloc*/
|
1992-09-17 17:54:56 +00:00
|
|
|
0, /*tp_print*/
|
1991-04-04 10:42:10 +00:00
|
|
|
instance_getattr, /*tp_getattr*/
|
|
|
|
instance_setattr, /*tp_setattr*/
|
1992-08-12 15:35:34 +00:00
|
|
|
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*/
|
1990-10-14 12:07:46 +00:00
|
|
|
};
|
|
|
|
|
1992-08-12 15:35:34 +00:00
|
|
|
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
|
1991-05-05 20:03:07 +00:00
|
|
|
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 *
|
1991-05-05 20:03:07 +00:00
|
|
|
newinstancemethodobject(func, self)
|
1990-10-14 12:07:46 +00:00
|
|
|
object *func;
|
|
|
|
object *self;
|
|
|
|
{
|
1991-05-05 20:03:07 +00:00
|
|
|
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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
im = NEWOBJ(instancemethodobject, &Instancemethodtype);
|
|
|
|
if (im == NULL)
|
1990-10-14 12:07:46 +00:00
|
|
|
return NULL;
|
|
|
|
INCREF(func);
|
1991-05-05 20:03:07 +00:00
|
|
|
im->im_func = func;
|
1990-10-14 12:07:46 +00:00
|
|
|
INCREF(self);
|
1991-05-05 20:03:07 +00:00
|
|
|
im->im_self = self;
|
|
|
|
return (object *)im;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethodgetfunc(im)
|
|
|
|
register object *im;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-05-05 20:03:07 +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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
return ((instancemethodobject *)im)->im_func;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
object *
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethodgetself(im)
|
|
|
|
register object *im;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-05-05 20:03:07 +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;
|
|
|
|
}
|
1991-05-05 20:03:07 +00:00
|
|
|
return ((instancemethodobject *)im)->im_self;
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Class method methods */
|
|
|
|
|
1991-05-05 20:03:07 +00:00
|
|
|
#define OFF(x) offsetof(instancemethodobject, x)
|
1990-12-20 15:06:42 +00:00
|
|
|
|
1991-05-05 20:03:07 +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 *
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethod_getattr(im, name)
|
|
|
|
register instancemethodobject *im;
|
1990-12-20 15:06:42 +00:00
|
|
|
char *name;
|
|
|
|
{
|
1991-05-05 20:03:07 +00:00
|
|
|
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
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethod_dealloc(im)
|
|
|
|
register instancemethodobject *im;
|
1990-10-14 12:07:46 +00:00
|
|
|
{
|
1991-05-05 20:03:07 +00:00
|
|
|
DECREF(im->im_func);
|
|
|
|
DECREF(im->im_self);
|
|
|
|
free((ANY *)im);
|
1990-10-14 12:07:46 +00:00
|
|
|
}
|
|
|
|
|
1992-09-03 20:39:51 +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;
|
|
|
|
}
|
|
|
|
|
1991-05-05 20:03:07 +00:00
|
|
|
typeobject Instancemethodtype = {
|
1990-10-14 12:07:46 +00:00
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
1991-04-16 08:38:43 +00:00
|
|
|
"instance method",
|
1991-05-05 20:03:07 +00:00
|
|
|
sizeof(instancemethodobject),
|
1990-10-14 12:07:46 +00:00
|
|
|
0,
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethod_dealloc, /*tp_dealloc*/
|
1990-10-14 12:07:46 +00:00
|
|
|
0, /*tp_print*/
|
1991-05-05 20:03:07 +00:00
|
|
|
instancemethod_getattr, /*tp_getattr*/
|
1990-10-14 12:07:46 +00:00
|
|
|
0, /*tp_setattr*/
|
1992-09-03 20:39:51 +00:00
|
|
|
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*/
|
|
|
|
};
|