1991-02-19 08:39:46 -04:00
|
|
|
/***********************************************************
|
1995-01-04 15:07:38 -04:00
|
|
|
Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
|
|
|
|
The Netherlands.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
All Rights Reserved
|
|
|
|
|
1996-10-25 11:44:06 -03:00
|
|
|
Permission to use, copy, modify, and distribute this software and its
|
|
|
|
documentation for any purpose and without fee is hereby granted,
|
1991-02-19 08:39:46 -04:00
|
|
|
provided that the above copyright notice appear in all copies and that
|
1996-10-25 11:44:06 -03:00
|
|
|
both that copyright notice and this permission notice appear in
|
1991-02-19 08:39:46 -04:00
|
|
|
supporting documentation, and that the names of Stichting Mathematisch
|
1996-10-25 11:44:06 -03:00
|
|
|
Centrum or CWI or Corporation for National Research Initiatives or
|
|
|
|
CNRI not be used in advertising or publicity pertaining to
|
|
|
|
distribution of the software without specific, written prior
|
|
|
|
permission.
|
|
|
|
|
|
|
|
While CWI is the initial source for this software, a modified version
|
|
|
|
is made available by the Corporation for National Research Initiatives
|
|
|
|
(CNRI) at the Internet address ftp://ftp.python.org.
|
|
|
|
|
|
|
|
STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
|
|
|
|
REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
|
|
|
|
CENTRUM OR CNRI 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.
|
1991-02-19 08:39:46 -04:00
|
|
|
|
|
|
|
******************************************************************/
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Class object implementation */
|
|
|
|
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "allobjects.h"
|
|
|
|
#include "structmember.h"
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1994-09-05 04:32:29 -03:00
|
|
|
/* Forward */
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *class_lookup PROTO((classobject *, object *, classobject **));
|
|
|
|
static object *instance_getattr1 PROTO((instanceobject *, object *));
|
1994-09-05 04:32:29 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
object *
|
1993-05-20 11:24:46 -03:00
|
|
|
newclassobject(bases, dict, name)
|
1990-10-14 09:07:46 -03:00
|
|
|
object *bases; /* NULL or tuple of classobjects! */
|
1993-05-20 11:24:46 -03:00
|
|
|
object *dict;
|
1991-10-20 17:11:48 -03:00
|
|
|
object *name; /* String; NULL if unknown */
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-20 11:24:46 -03:00
|
|
|
int pos;
|
|
|
|
object *key, *value;
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1994-09-05 04:32:29 -03:00
|
|
|
classobject *op, *dummy;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *getattrstr, *setattrstr, *delattrstr;
|
1996-08-21 11:54:28 -03:00
|
|
|
static object *docstr;
|
|
|
|
if (docstr == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
docstr= PyString_InternFromString("__doc__");
|
1996-08-21 11:54:28 -03:00
|
|
|
if (docstr == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
if (mappinglookup(dict, docstr) == NULL) {
|
|
|
|
if (mappinginsert(dict, docstr, None) < 0)
|
1995-01-07 08:35:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-12-10 09:53:23 -04:00
|
|
|
if (bases == NULL) {
|
|
|
|
bases = newtupleobject(0);
|
|
|
|
if (bases == NULL)
|
1993-03-16 08:15:04 -04:00
|
|
|
return NULL;
|
1991-12-10 09:53:23 -04:00
|
|
|
}
|
|
|
|
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;
|
1993-05-20 11:24:46 -03:00
|
|
|
INCREF(dict);
|
|
|
|
op->cl_dict = dict;
|
1991-10-20 17:11:48 -03:00
|
|
|
XINCREF(name);
|
|
|
|
op->cl_name = name;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getattrstr == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
getattrstr = PyString_InternFromString("__getattr__");
|
|
|
|
setattrstr = PyString_InternFromString("__setattr__");
|
|
|
|
delattrstr = PyString_InternFromString("__delattr__");
|
1996-08-09 17:53:24 -03:00
|
|
|
}
|
|
|
|
op->cl_getattr = class_lookup(op, getattrstr, &dummy);
|
|
|
|
op->cl_setattr = class_lookup(op, setattrstr, &dummy);
|
|
|
|
op->cl_delattr = class_lookup(op, delattrstr, &dummy);
|
1994-09-05 04:32:29 -03:00
|
|
|
XINCREF(op->cl_getattr);
|
|
|
|
XINCREF(op->cl_setattr);
|
|
|
|
XINCREF(op->cl_delattr);
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-20 11:24:46 -03:00
|
|
|
pos = 0;
|
|
|
|
while (mappinggetnext(dict, &pos, &key, &value)) {
|
|
|
|
if (is_accessobject(value))
|
|
|
|
setaccessowner(value, (object *)op);
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
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);
|
1993-05-20 11:24:46 -03:00
|
|
|
DECREF(op->cl_dict);
|
1991-10-20 17:11:48 -03:00
|
|
|
XDECREF(op->cl_name);
|
1990-10-14 09:07:46 -03:00
|
|
|
free((ANY *)op);
|
|
|
|
}
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
static object *
|
|
|
|
class_lookup(cp, name, pclass)
|
|
|
|
classobject *cp;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1993-05-20 11:24:46 -03:00
|
|
|
classobject **pclass;
|
|
|
|
{
|
|
|
|
int i, n;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *value = mappinglookup(cp->cl_dict, name);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (value != NULL) {
|
|
|
|
*pclass = cp;
|
|
|
|
return value;
|
|
|
|
}
|
|
|
|
n = gettuplesize(cp->cl_bases);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
object *v = class_lookup((classobject *)
|
|
|
|
gettupleitem(cp->cl_bases, i), name, pclass);
|
|
|
|
if (v != NULL)
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static object *
|
|
|
|
class_getattr(op, name)
|
|
|
|
register classobject *op;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
|
|
|
register object *v;
|
1996-08-09 17:53:24 -03:00
|
|
|
register char *sname = getstringvalue(name);
|
1994-08-01 08:34:53 -03:00
|
|
|
classobject *class;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1995-01-10 06:39:49 -04:00
|
|
|
if (getrestricted()) {
|
|
|
|
err_setstr(RuntimeError,
|
|
|
|
"class.__dict__ not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
INCREF(op->cl_dict);
|
|
|
|
return op->cl_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__bases__") == 0) {
|
1995-01-10 06:39:49 -04:00
|
|
|
INCREF(op->cl_bases);
|
|
|
|
return op->cl_bases;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__name__") == 0) {
|
1995-01-10 06:39:49 -04:00
|
|
|
if (op->cl_name == NULL)
|
|
|
|
v = None;
|
|
|
|
else
|
|
|
|
v = op->cl_name;
|
|
|
|
INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
1993-05-20 11:24:46 -03:00
|
|
|
v = class_lookup(op, name, &class);
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL) {
|
1996-08-09 17:53:24 -03:00
|
|
|
err_setval(AttributeError, name);
|
1993-05-21 16:56:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-21 16:56:10 -03:00
|
|
|
if (is_accessobject(v)) {
|
1993-05-25 06:38:27 -03:00
|
|
|
v = getaccessvalue(v, getowner());
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1993-05-21 16:56:10 -03:00
|
|
|
INCREF(v);
|
|
|
|
if (is_funcobject(v)) {
|
|
|
|
object *w = newinstancemethodobject(v, (object *)NULL,
|
1993-05-20 11:24:46 -03:00
|
|
|
(object *)class);
|
1993-05-21 16:56:10 -03:00
|
|
|
DECREF(v);
|
|
|
|
v = w;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1993-05-21 16:56:10 -03:00
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1991-10-20 17:11:48 -03:00
|
|
|
static int
|
|
|
|
class_setattr(op, name, v)
|
|
|
|
classobject *op;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1991-10-20 17:11:48 -03:00
|
|
|
object *v;
|
|
|
|
{
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-19 11:50:45 -03:00
|
|
|
object *ac;
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1996-08-09 17:53:24 -03:00
|
|
|
char *sname = getstringvalue(name);
|
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
int n = getstringsize(name);
|
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1992-04-05 11:24:32 -03:00
|
|
|
err_setstr(TypeError, "read-only special attribute");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1995-08-04 01:05:31 -03:00
|
|
|
if (getrestricted()) {
|
|
|
|
err_setstr(RuntimeError,
|
|
|
|
"classes are read-only in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1996-08-09 17:53:24 -03:00
|
|
|
ac = mappinglookup(op->cl_dict, name);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (ac != NULL && is_accessobject(ac))
|
1993-05-25 06:38:27 -03:00
|
|
|
return setaccessvalue(ac, getowner(), v);
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1996-08-09 17:53:24 -03:00
|
|
|
int rv = mappingremove(op->cl_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
|
|
|
err_setstr(AttributeError,
|
|
|
|
"delete non-existing class attribute");
|
|
|
|
return rv;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
else
|
1996-08-09 17:53:24 -03:00
|
|
|
return mappinginsert(op->cl_dict, name, v);
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
|
|
|
|
1993-05-19 11:50:45 -03:00
|
|
|
static object *
|
|
|
|
class_repr(op)
|
|
|
|
classobject *op;
|
|
|
|
{
|
|
|
|
char buf[140];
|
|
|
|
char *name;
|
|
|
|
if (op->cl_name == NULL || !is_stringobject(op->cl_name))
|
|
|
|
name = "?";
|
|
|
|
else
|
|
|
|
name = getstringvalue(op->cl_name);
|
|
|
|
sprintf(buf, "<class %.100s at %lx>", name, (long)op);
|
|
|
|
return newstringobject(buf);
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
typeobject Classtype = {
|
|
|
|
OB_HEAD_INIT(&Typetype)
|
|
|
|
0,
|
|
|
|
"class",
|
|
|
|
sizeof(classobject),
|
|
|
|
0,
|
1994-08-01 08:34:53 -03:00
|
|
|
(destructor)class_dealloc, /*tp_dealloc*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_print*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_compare*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(reprfunc)class_repr, /*tp_repr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_hash*/
|
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
(getattrofunc)class_getattr, /*tp_getattro*/
|
|
|
|
(setattrofunc)class_setattr, /*tp_setattro*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
int
|
|
|
|
issubclass(class, base)
|
|
|
|
object *class;
|
|
|
|
object *base;
|
|
|
|
{
|
|
|
|
int i, n;
|
|
|
|
classobject *cp;
|
|
|
|
if (class == base)
|
|
|
|
return 1;
|
1993-05-25 06:38:27 -03:00
|
|
|
if (class == NULL || !is_classobject(class))
|
|
|
|
return 0;
|
1993-05-20 11:24:46 -03:00
|
|
|
cp = (classobject *)class;
|
|
|
|
n = gettuplesize(cp->cl_bases);
|
|
|
|
for (i = 0; i < n; i++) {
|
|
|
|
if (issubclass(gettupleitem(cp->cl_bases, i), base))
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
/* Instance objects */
|
1990-10-14 09:07:46 -03:00
|
|
|
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-20 11:24:46 -03:00
|
|
|
static int
|
|
|
|
addaccess(class, inst)
|
|
|
|
classobject *class;
|
|
|
|
instanceobject *inst;
|
|
|
|
{
|
|
|
|
int i, n, pos, ret;
|
|
|
|
object *key, *value, *ac;
|
|
|
|
|
|
|
|
n = gettuplesize(class->cl_bases);
|
|
|
|
for (i = 0; i < n; i++) {
|
1994-08-01 08:34:53 -03:00
|
|
|
if (addaccess((classobject *)gettupleitem(class->cl_bases, i), inst) < 0)
|
1993-05-20 11:24:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
|
|
|
while (mappinggetnext(class->cl_dict, &pos, &key, &value)) {
|
|
|
|
if (!is_accessobject(value))
|
|
|
|
continue;
|
1993-05-21 16:56:10 -03:00
|
|
|
if (hasaccessvalue(value))
|
|
|
|
continue;
|
1993-05-20 11:24:46 -03:00
|
|
|
ac = dict2lookup(inst->in_dict, key);
|
|
|
|
if (ac != NULL && is_accessobject(ac)) {
|
|
|
|
err_setval(ConflictError, key);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
ac = cloneaccessobject(value);
|
|
|
|
if (ac == NULL)
|
|
|
|
return -1;
|
|
|
|
ret = dict2insert(inst->in_dict, key, ac);
|
|
|
|
DECREF(ac);
|
|
|
|
if (ret != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1993-05-20 11:24:46 -03:00
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
object *
|
1995-07-26 15:07:32 -03:00
|
|
|
newinstanceobject(class, arg, kw)
|
1993-05-19 11:50:45 -03:00
|
|
|
object *class;
|
|
|
|
object *arg;
|
1995-07-26 15:07:32 -03:00
|
|
|
object *kw;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-05-05 17:03:07 -03:00
|
|
|
register instanceobject *inst;
|
1993-05-19 11:50:45 -03:00
|
|
|
object *init;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *initstr;
|
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;
|
1993-05-20 11:24:46 -03:00
|
|
|
inst->in_dict = newdictobject();
|
1996-08-12 19:00:53 -03:00
|
|
|
if (inst->in_dict == NULL
|
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
|
|
|
|| addaccess((classobject *)class, inst) != 0
|
|
|
|
#endif
|
|
|
|
) {
|
1991-05-05 17:03:07 -03:00
|
|
|
DECREF(inst);
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (initstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
initstr = PyString_InternFromString("__init__");
|
1996-08-09 17:53:24 -03:00
|
|
|
init = instance_getattr1(inst, initstr);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (init == NULL) {
|
|
|
|
err_clear();
|
1996-12-05 17:52:32 -04:00
|
|
|
if ((arg != NULL && (!is_tupleobject(arg) ||
|
|
|
|
gettuplesize(arg) != 0))
|
|
|
|
|| (kw != NULL && (!is_dictobject(kw) ||
|
|
|
|
getdictsize(kw) != 0))) {
|
1993-05-19 11:50:45 -03:00
|
|
|
err_setstr(TypeError,
|
1995-07-26 15:07:32 -03:00
|
|
|
"this constructor takes no arguments");
|
1993-05-19 11:50:45 -03:00
|
|
|
DECREF(inst);
|
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1995-07-26 15:07:32 -03:00
|
|
|
object *res = PyEval_CallObjectWithKeywords(init, arg, kw);
|
1993-05-19 11:50:45 -03:00
|
|
|
DECREF(init);
|
|
|
|
if (res == NULL) {
|
|
|
|
DECREF(inst);
|
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (res != None) {
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"__init__() should return None");
|
|
|
|
DECREF(inst);
|
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
DECREF(res);
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
{
|
1995-01-02 15:07:15 -04:00
|
|
|
object *error_type, *error_value, *error_traceback;
|
1993-05-19 11:50:45 -03:00
|
|
|
object *del;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *delstr;
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Call the __del__ method if it exists. First temporarily
|
|
|
|
revive the object and save the current exception, if any. */
|
1996-05-23 19:46:51 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
|
|
|
/* much too complicated if Py_TRACE_REFS defined */
|
1995-08-28 06:00:43 -03:00
|
|
|
extern long ref_total;
|
|
|
|
inst->ob_type = &Instancetype;
|
1995-09-18 07:49:04 -03:00
|
|
|
NEWREF(inst);
|
1995-08-28 06:00:43 -03:00
|
|
|
ref_total--; /* compensate for increment in NEWREF */
|
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inst->ob_type->tp_alloc--; /* ditto */
|
|
|
|
#endif
|
1996-05-23 19:46:51 -03:00
|
|
|
#else /* !Py_TRACE_REFS */
|
1993-05-19 11:50:45 -03:00
|
|
|
INCREF(inst);
|
1996-05-23 19:46:51 -03:00
|
|
|
#endif /* !Py_TRACE_REFS */
|
1995-01-02 15:07:15 -04:00
|
|
|
err_fetch(&error_type, &error_value, &error_traceback);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (delstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delstr = PyString_InternFromString("__del__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if ((del = instance_getattr1(inst, delstr)) != NULL) {
|
1995-07-11 23:22:06 -03:00
|
|
|
object *res = call_object(del, (object *)NULL);
|
1996-08-12 19:00:53 -03:00
|
|
|
if (res == NULL) {
|
1996-12-05 17:52:32 -04:00
|
|
|
object *f, *t, *v, *tb;
|
|
|
|
err_fetch(&t, &v, &tb);
|
|
|
|
f = sysget("stderr");
|
1996-08-22 20:15:42 -03:00
|
|
|
err_clear();
|
1996-09-11 19:51:57 -03:00
|
|
|
if (f != NULL) {
|
1996-12-05 17:52:32 -04:00
|
|
|
writestring("Exception ", f);
|
|
|
|
if (t) {
|
|
|
|
writeobject(t, f, Py_PRINT_RAW);
|
|
|
|
if (v && v != None) {
|
|
|
|
writestring(": ", f);
|
|
|
|
writeobject(v, f, 0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
writestring(" in ", f);
|
|
|
|
writeobject(del, f, 0);
|
|
|
|
writestring(" ignored\n", f);
|
|
|
|
err_clear(); /* Just in case */
|
1996-09-11 19:51:57 -03:00
|
|
|
}
|
1996-12-05 17:52:32 -04:00
|
|
|
Py_XDECREF(t);
|
|
|
|
Py_XDECREF(v);
|
|
|
|
Py_XDECREF(tb);
|
1996-08-12 19:00:53 -03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
DECREF(res);
|
1996-12-05 17:52:32 -04:00
|
|
|
DECREF(del);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
/* Restore the saved exception and undo the temporary revival */
|
1995-01-02 15:07:15 -04:00
|
|
|
err_restore(error_type, error_value, error_traceback);
|
1993-05-19 11:50:45 -03:00
|
|
|
/* Can't use DECREF here, it would cause a recursive call */
|
1995-08-28 06:00:43 -03:00
|
|
|
if (--inst->ob_refcnt > 0) {
|
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inst->ob_type->tp_free--;
|
|
|
|
#endif
|
1993-05-19 11:50:45 -03:00
|
|
|
return; /* __del__ added a reference; don't delete now */
|
1995-08-28 06:00:43 -03:00
|
|
|
}
|
1996-05-23 19:46:51 -03:00
|
|
|
#ifdef Py_TRACE_REFS
|
1995-08-28 06:00:43 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inst->ob_type->tp_free--; /* compensate for increment in UNREF */
|
|
|
|
#endif
|
|
|
|
UNREF(inst);
|
|
|
|
inst->ob_type = NULL;
|
1996-05-23 19:46:51 -03:00
|
|
|
#endif /* Py_TRACE_REFS */
|
1991-05-05 17:03:07 -03:00
|
|
|
DECREF(inst->in_class);
|
1993-05-20 11:24:46 -03:00
|
|
|
XDECREF(inst->in_dict);
|
1991-05-05 17:03:07 -03:00
|
|
|
free((ANY *)inst);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-09-05 04:31:41 -03:00
|
|
|
static object *
|
|
|
|
instance_getattr1(inst, name)
|
1991-05-05 17:03:07 -03:00
|
|
|
register instanceobject *inst;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1991-10-20 17:11:48 -03:00
|
|
|
register object *v;
|
1996-08-09 17:53:24 -03:00
|
|
|
register char *sname = getstringvalue(name);
|
1993-05-20 11:24:46 -03:00
|
|
|
classobject *class;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1995-01-10 06:39:49 -04:00
|
|
|
if (getrestricted()) {
|
|
|
|
err_setstr(RuntimeError,
|
|
|
|
"instance.__dict__ not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
1993-10-22 09:04:32 -03:00
|
|
|
INCREF(inst->in_dict);
|
|
|
|
return inst->in_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__class__") == 0) {
|
1993-10-22 09:04:32 -03:00
|
|
|
INCREF(inst->in_class);
|
|
|
|
return (object *)inst->in_class;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
1993-05-25 06:38:27 -03:00
|
|
|
class = NULL;
|
1996-08-09 17:53:24 -03:00
|
|
|
v = mappinglookup(inst->in_dict, name);
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL) {
|
|
|
|
v = class_lookup(inst->in_class, name, &class);
|
|
|
|
if (v == NULL) {
|
1996-08-09 17:53:24 -03:00
|
|
|
err_setval(AttributeError, name);
|
1993-05-21 16:56:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-21 16:56:10 -03:00
|
|
|
if (is_accessobject(v)) {
|
1993-05-25 06:38:27 -03:00
|
|
|
v = getaccessvalue(v, getowner());
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1993-05-21 16:56:10 -03:00
|
|
|
INCREF(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
if (class != NULL) {
|
|
|
|
if (is_funcobject(v)) {
|
|
|
|
object *w = newinstancemethodobject(v, (object *)inst,
|
|
|
|
(object *)class);
|
|
|
|
DECREF(v);
|
|
|
|
v = w;
|
|
|
|
}
|
|
|
|
else if (is_instancemethodobject(v)) {
|
|
|
|
object *im_class = instancemethodgetclass(v);
|
|
|
|
/* Only if classes are compatible */
|
|
|
|
if (issubclass((object *)class, im_class)) {
|
|
|
|
object *im_func = instancemethodgetfunc(v);
|
|
|
|
object *w = newinstancemethodobject(im_func,
|
|
|
|
(object *)inst, im_class);
|
|
|
|
DECREF(v);
|
|
|
|
v = w;
|
|
|
|
}
|
|
|
|
}
|
1993-05-21 16:56:10 -03:00
|
|
|
}
|
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-09-05 04:31:41 -03:00
|
|
|
static object *
|
|
|
|
instance_getattr(inst, name)
|
|
|
|
register instanceobject *inst;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1994-09-05 04:31:41 -03:00
|
|
|
{
|
|
|
|
register object *func, *res;
|
1994-09-05 04:32:29 -03:00
|
|
|
res = instance_getattr1(inst, name);
|
|
|
|
if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
|
|
|
|
object *args;
|
1995-01-02 15:07:15 -04:00
|
|
|
err_clear();
|
1996-08-09 17:53:24 -03:00
|
|
|
args = mkvalue("(OO)", inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
1994-09-05 04:31:41 -03:00
|
|
|
return NULL;
|
1994-09-05 04:32:29 -03:00
|
|
|
res = call_object(func, args);
|
|
|
|
DECREF(args);
|
1994-09-05 04:31:41 -03:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
static int
|
1994-09-05 04:31:41 -03:00
|
|
|
instance_setattr1(inst, name, v)
|
1991-05-05 17:03:07 -03:00
|
|
|
instanceobject *inst;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1990-10-14 09:07:46 -03:00
|
|
|
object *v;
|
|
|
|
{
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1993-05-19 11:50:45 -03:00
|
|
|
object *ac;
|
1996-08-09 17:53:24 -03:00
|
|
|
ac = mappinglookup(inst->in_dict, name);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (ac != NULL && is_accessobject(ac))
|
1993-05-25 06:38:27 -03:00
|
|
|
return setaccessvalue(ac, getowner(), v);
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1996-08-09 17:53:24 -03:00
|
|
|
int rv = mappingremove(inst->in_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
|
|
|
err_setstr(AttributeError,
|
|
|
|
"delete non-existing instance attribute");
|
|
|
|
return rv;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
else
|
1996-08-09 17:53:24 -03:00
|
|
|
return mappinginsert(inst->in_dict, name, v);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1994-09-05 04:31:41 -03:00
|
|
|
static int
|
|
|
|
instance_setattr(inst, name, v)
|
|
|
|
instanceobject *inst;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1994-09-05 04:31:41 -03:00
|
|
|
object *v;
|
|
|
|
{
|
1994-09-05 04:32:29 -03:00
|
|
|
object *func, *args, *res;
|
1996-08-09 17:53:24 -03:00
|
|
|
char *sname = getstringvalue(name);
|
|
|
|
if (sname[0] == '_' && sname[1] == '_'
|
|
|
|
&& (strcmp(sname, "__dict__") == 0 ||
|
|
|
|
strcmp(sname, "__class__") == 0)) {
|
1996-08-26 11:58:03 -03:00
|
|
|
int n = getstringsize(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1994-09-05 04:32:29 -03:00
|
|
|
err_setstr(TypeError, "read-only special attribute");
|
1994-09-05 04:31:41 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1994-09-05 04:32:29 -03:00
|
|
|
if (v == NULL)
|
|
|
|
func = inst->in_class->cl_delattr;
|
|
|
|
else
|
|
|
|
func = inst->in_class->cl_setattr;
|
|
|
|
if (func == NULL)
|
|
|
|
return instance_setattr1(inst, name, v);
|
|
|
|
if (v == NULL)
|
1996-08-09 17:53:24 -03:00
|
|
|
args = mkvalue("(OO)", inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
else
|
1996-08-09 17:53:24 -03:00
|
|
|
args = mkvalue("(OOO)", inst, name, v);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
|
|
|
return -1;
|
|
|
|
res = call_object(func, args);
|
|
|
|
DECREF(args);
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(res);
|
1994-09-05 04:31:41 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static object *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_repr(inst)
|
|
|
|
instanceobject *inst;
|
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *res;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *reprstr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (reprstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
reprstr = PyString_InternFromString("__repr__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, reprstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL) {
|
1993-05-19 11:50:45 -03:00
|
|
|
char buf[140];
|
|
|
|
object *classname = inst->in_class->cl_name;
|
|
|
|
char *cname;
|
|
|
|
if (classname != NULL && is_stringobject(classname))
|
|
|
|
cname = getstringvalue(classname);
|
|
|
|
else
|
|
|
|
cname = "?";
|
1992-08-12 12:35:34 -03:00
|
|
|
err_clear();
|
1993-05-19 11:50:45 -03:00
|
|
|
sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
|
1992-08-12 12:35:34 -03:00
|
|
|
return newstringobject(buf);
|
|
|
|
}
|
|
|
|
res = call_object(func, (object *)NULL);
|
|
|
|
DECREF(func);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1995-01-07 08:35:18 -04:00
|
|
|
static object *
|
|
|
|
instance_compare1(inst, other)
|
|
|
|
object *inst, *other;
|
|
|
|
{
|
|
|
|
return instancebinop(inst, other, "__cmp__", "__rcmp__",
|
|
|
|
instance_compare1);
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_compare(inst, other)
|
1994-09-28 12:51:32 -03:00
|
|
|
object *inst, *other;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1994-09-28 12:51:32 -03:00
|
|
|
object *result;
|
1995-01-07 08:35:18 -04:00
|
|
|
long outcome;
|
|
|
|
result = instance_compare1(inst, other);
|
|
|
|
if (result == NULL || !is_intobject(result)) {
|
1994-10-19 12:11:52 -03:00
|
|
|
err_clear();
|
1994-11-10 18:31:02 -04:00
|
|
|
return (inst < other) ? -1 : 1;
|
1994-10-19 12:11:52 -03:00
|
|
|
}
|
1994-09-28 12:51:32 -03:00
|
|
|
outcome = getintvalue(result);
|
|
|
|
DECREF(result);
|
|
|
|
if (outcome < 0)
|
|
|
|
return -1;
|
|
|
|
else if (outcome > 0)
|
|
|
|
return 1;
|
|
|
|
return 0;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
|
|
|
instance_hash(inst)
|
|
|
|
instanceobject *inst;
|
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *res;
|
1993-04-08 09:56:19 -03:00
|
|
|
long outcome;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *hashstr, *cmpstr;
|
1993-03-29 06:43:31 -04:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (hashstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
hashstr = PyString_InternFromString("__hash__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, hashstr);
|
1993-03-29 06:43:31 -04:00
|
|
|
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();
|
1996-08-09 17:53:24 -03:00
|
|
|
if (cmpstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
cmpstr = PyString_InternFromString("__cmp__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, cmpstr);
|
1993-04-08 09:56:19 -03:00
|
|
|
if (func == NULL) {
|
|
|
|
err_clear();
|
|
|
|
outcome = (long)inst;
|
|
|
|
if (outcome == -1)
|
|
|
|
outcome = -2;
|
|
|
|
return outcome;
|
|
|
|
}
|
1993-03-29 06:43:31 -04:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *getitemstr, *setitemstr, *delitemstr, *lenstr;
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_length(inst)
|
|
|
|
instanceobject *inst;
|
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *res;
|
|
|
|
int outcome;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (lenstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
lenstr = PyString_InternFromString("__len__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, lenstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return -1;
|
|
|
|
res = call_object(func, (object *)NULL);
|
|
|
|
DECREF(func);
|
1992-11-26 06:30:26 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static object *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_subscript(inst, key)
|
|
|
|
instanceobject *inst;
|
|
|
|
object *key;
|
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *arg;
|
|
|
|
object *res;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getitemstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_ass_subscript(inst, key, value)
|
|
|
|
instanceobject*inst;
|
|
|
|
object *key;
|
|
|
|
object *value;
|
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *arg;
|
|
|
|
object *res;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
if (delitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delitemstr = PyString_InternFromString("__delitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delitemstr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
setitemstr = PyString_InternFromString("__setitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setitemstr);
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
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 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
res = call_object(func, arg);
|
|
|
|
DECREF(func);
|
|
|
|
DECREF(arg);
|
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
|
|
|
DECREF(res);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static mapping_methods instance_as_mapping = {
|
1994-08-01 08:34:53 -03:00
|
|
|
(inquiry)instance_length, /*mp_length*/
|
|
|
|
(binaryfunc)instance_subscript, /*mp_subscript*/
|
|
|
|
(objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/
|
1992-08-12 12:35:34 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
|
|
|
instance_item(inst, i)
|
|
|
|
instanceobject *inst;
|
|
|
|
int i;
|
|
|
|
{
|
|
|
|
object *func, *arg, *res;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getitemstr = PyString_InternFromString("__getitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getitemstr);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
1995-07-11 23:22:06 -03:00
|
|
|
arg = mkvalue("(i)", i);
|
1992-08-12 12:35:34 -03:00
|
|
|
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;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *getslicestr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (getslicestr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
getslicestr = PyString_InternFromString("__getslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, getslicestr);
|
1992-08-12 12:35:34 -03:00
|
|
|
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;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (item == NULL) {
|
|
|
|
if (delitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delitemstr = PyString_InternFromString("__delitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delitemstr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setitemstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
setitemstr = PyString_InternFromString("__setitem__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setitemstr);
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
if (item == NULL)
|
|
|
|
arg = mkvalue("i", i);
|
|
|
|
else
|
|
|
|
arg = mkvalue("(iO)", i, item);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03: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;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *setslicestr, *delslicestr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (value == NULL) {
|
|
|
|
if (delslicestr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
delslicestr = PyString_InternFromString("__delslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delslicestr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setslicestr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
setslicestr = PyString_InternFromString("__setslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, setslicestr);
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
if (func == NULL)
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
if (value == NULL)
|
|
|
|
arg = mkvalue("(ii)", i, j);
|
|
|
|
else
|
|
|
|
arg = mkvalue("(iiO)", i, j, value);
|
|
|
|
if (arg == NULL) {
|
|
|
|
DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03: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 = {
|
1994-08-01 08:34:53 -03:00
|
|
|
(inquiry)instance_length, /*sq_length*/
|
1994-09-28 12:51:32 -03:00
|
|
|
0, /*sq_concat*/
|
|
|
|
0, /*sq_repeat*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(intargfunc)instance_item, /*sq_item*/
|
|
|
|
(intintargfunc)instance_slice, /*sq_slice*/
|
|
|
|
(intobjargproc)instance_ass_item, /*sq_ass_item*/
|
|
|
|
(intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
|
1992-08-12 12:35:34 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
static object *
|
1994-09-28 12:51:32 -03:00
|
|
|
generic_unary_op(self, methodname)
|
1992-08-12 12:35:34 -03:00
|
|
|
instanceobject *self;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *methodname;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1994-09-28 12:51:32 -03:00
|
|
|
object *func, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
|
|
|
if ((func = instance_getattr(self, methodname)) == NULL)
|
|
|
|
return NULL;
|
1994-09-28 12:51:32 -03:00
|
|
|
res = call_object(func, (object *)NULL);
|
1992-08-12 12:35:34 -03:00
|
|
|
DECREF(func);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
|
|
|
|
/* Forward */
|
1996-08-09 17:53:24 -03:00
|
|
|
static int halfbinop Py_PROTO((object *, object *, char *, object **,
|
|
|
|
object * (*) Py_PROTO((object *, object *)), int ));
|
1994-09-28 12:51:32 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Implement a binary operator involving at least one class instance. */
|
|
|
|
|
|
|
|
object *
|
1995-01-07 08:35:18 -04:00
|
|
|
instancebinop(v, w, opname, ropname, thisfunc)
|
1994-09-28 12:51:32 -03:00
|
|
|
object *v;
|
|
|
|
object *w;
|
|
|
|
char *opname;
|
|
|
|
char *ropname;
|
1995-01-07 08:35:18 -04:00
|
|
|
object * (*thisfunc) PROTO((object *, object *));
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1994-09-28 12:51:32 -03:00
|
|
|
char buf[256];
|
|
|
|
object *result = NULL;
|
1995-01-07 08:35:18 -04:00
|
|
|
if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0)
|
1994-09-28 12:51:32 -03:00
|
|
|
return result;
|
1995-01-07 08:35:18 -04:00
|
|
|
if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0)
|
1994-09-28 12:51:32 -03:00
|
|
|
return result;
|
|
|
|
sprintf(buf, "%s nor %s defined for these operands", opname, ropname);
|
|
|
|
err_setstr(TypeError, buf);
|
|
|
|
return NULL;
|
|
|
|
}
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
|
|
|
|
/* Try one half of a binary operator involving a class instance.
|
|
|
|
Return value:
|
|
|
|
-1 if an exception is to be reported right away
|
|
|
|
0 if we have a valid result
|
|
|
|
1 if we could try another operation
|
|
|
|
*/
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *coerce_obj;
|
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
static int
|
1995-01-07 08:35:18 -04:00
|
|
|
halfbinop(v, w, opname, r_result, thisfunc, swapped)
|
1994-09-28 12:51:32 -03:00
|
|
|
object *v;
|
|
|
|
object *w;
|
|
|
|
char *opname;
|
|
|
|
object **r_result;
|
1995-01-07 08:35:18 -04:00
|
|
|
object * (*thisfunc) PROTO((object *, object *));
|
|
|
|
int swapped;
|
1994-09-28 12:51:32 -03:00
|
|
|
{
|
|
|
|
object *func;
|
|
|
|
object *args;
|
|
|
|
object *coerce;
|
|
|
|
object *coerced = NULL;
|
|
|
|
object *v1;
|
|
|
|
|
|
|
|
if (!is_instanceobject(v))
|
|
|
|
return 1;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
coerce_obj = PyString_InternFromString("__coerce__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
coerce = getattro(v, coerce_obj);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (coerce == NULL) {
|
|
|
|
err_clear();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
args = mkvalue("(O)", w);
|
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
coerced = call_object(coerce, args);
|
|
|
|
DECREF(args);
|
|
|
|
DECREF(coerce);
|
|
|
|
if (coerced == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (coerced == None) {
|
|
|
|
DECREF(coerced);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) {
|
|
|
|
DECREF(coerced);
|
1995-01-07 08:35:18 -04:00
|
|
|
err_setstr(TypeError,
|
|
|
|
"coercion should return None or 2-tuple");
|
1994-09-28 12:51:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
v1 = gettupleitem(coerced, 0);
|
1995-01-07 08:35:18 -04:00
|
|
|
w = gettupleitem(coerced, 1);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (v1 != v) {
|
|
|
|
v = v1;
|
1995-01-07 08:35:18 -04:00
|
|
|
if (!is_instanceobject(v) && !is_instanceobject(w)) {
|
|
|
|
if (swapped)
|
|
|
|
*r_result = (*thisfunc)(w, v);
|
|
|
|
else
|
|
|
|
*r_result = (*thisfunc)(v, w);
|
|
|
|
DECREF(coerced);
|
|
|
|
return *r_result == NULL ? -1 : 0;
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
w = gettupleitem(coerced, 1);
|
|
|
|
}
|
1995-01-07 08:35:18 -04:00
|
|
|
func = getattr(v, opname);
|
|
|
|
if (func == NULL) {
|
|
|
|
XDECREF(coerced);
|
|
|
|
if (err_occurred() != AttributeError)
|
|
|
|
return -1;
|
|
|
|
err_clear();
|
|
|
|
return 1;
|
|
|
|
}
|
1994-09-28 12:51:32 -03:00
|
|
|
args = mkvalue("(O)", w);
|
|
|
|
if (args == NULL) {
|
|
|
|
DECREF(func);
|
|
|
|
XDECREF(coerced);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
*r_result = call_object(func, args);
|
|
|
|
DECREF(args);
|
1992-08-12 12:35:34 -03:00
|
|
|
DECREF(func);
|
1994-09-28 12:51:32 -03:00
|
|
|
XDECREF(coerced);
|
|
|
|
return *r_result == NULL ? -1 : 0;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
|
1995-01-10 11:24:06 -04:00
|
|
|
static int
|
|
|
|
instance_coerce(pv, pw)
|
|
|
|
object **pv;
|
|
|
|
object **pw;
|
|
|
|
{
|
|
|
|
object *v = *pv;
|
|
|
|
object *w = *pw;
|
|
|
|
object *coerce;
|
|
|
|
object *args;
|
|
|
|
object *coerced;
|
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL) {
|
1997-01-18 03:59:12 -04:00
|
|
|
coerce_obj = PyString_InternFromString("__coerce__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if (coerce_obj == NULL)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
coerce = getattro(v, coerce_obj);
|
1995-01-10 11:24:06 -04:00
|
|
|
if (coerce == NULL) {
|
|
|
|
/* No __coerce__ method: always OK */
|
|
|
|
err_clear();
|
|
|
|
INCREF(v);
|
|
|
|
INCREF(w);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Has __coerce__ method: call it */
|
|
|
|
args = mkvalue("(O)", w);
|
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
coerced = call_object(coerce, args);
|
|
|
|
DECREF(args);
|
|
|
|
DECREF(coerce);
|
|
|
|
if (coerced == NULL) {
|
|
|
|
/* __coerce__ call raised an exception */
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (coerced == None) {
|
|
|
|
/* __coerce__ says "I can't do it" */
|
|
|
|
DECREF(coerced);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (!is_tupleobject(coerced) || gettuplesize(coerced) != 2) {
|
|
|
|
/* __coerce__ return value is malformed */
|
|
|
|
DECREF(coerced);
|
|
|
|
err_setstr(TypeError,
|
|
|
|
"coercion should return None or 2-tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* __coerce__ returned two new values */
|
|
|
|
*pv = gettupleitem(coerced, 0);
|
|
|
|
*pw = gettupleitem(coerced, 1);
|
|
|
|
INCREF(*pv);
|
|
|
|
INCREF(*pw);
|
|
|
|
DECREF(coerced);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
|
|
|
|
#define UNARY(funcname, methodname) \
|
|
|
|
static object *funcname(self) instanceobject *self; { \
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *o; \
|
1997-01-18 03:59:12 -04:00
|
|
|
if (o == NULL) o = PyString_InternFromString(methodname); \
|
1996-08-09 17:53:24 -03:00
|
|
|
return generic_unary_op(self, o); \
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
UNARY(instance_neg, "__neg__")
|
|
|
|
UNARY(instance_pos, "__pos__")
|
|
|
|
UNARY(instance_abs, "__abs__")
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_nonzero(self)
|
|
|
|
instanceobject *self;
|
|
|
|
{
|
|
|
|
object *func, *res;
|
|
|
|
long outcome;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *nonzerostr;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1996-08-09 17:53:24 -03:00
|
|
|
if (nonzerostr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
nonzerostr = PyString_InternFromString("__nonzero__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if ((func = instance_getattr(self, nonzerostr)) == NULL) {
|
1992-08-12 12:35:34 -03:00
|
|
|
err_clear();
|
1996-08-09 17:53:24 -03:00
|
|
|
if (lenstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
lenstr = PyString_InternFromString("__len__");
|
1996-08-09 17:53:24 -03:00
|
|
|
if ((func = instance_getattr(self, lenstr)) == NULL) {
|
1992-08-12 12:35:34 -03:00
|
|
|
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__")
|
1992-09-12 08:09:23 -03:00
|
|
|
UNARY(instance_int, "__int__")
|
|
|
|
UNARY(instance_long, "__long__")
|
|
|
|
UNARY(instance_float, "__float__")
|
|
|
|
UNARY(instance_oct, "__oct__")
|
|
|
|
UNARY(instance_hex, "__hex__")
|
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
/* This version is for ternary calls only (z != None) */
|
|
|
|
static object *
|
|
|
|
instance_pow(v, w, z)
|
|
|
|
object *v;
|
|
|
|
object *w;
|
|
|
|
object *z;
|
|
|
|
{
|
|
|
|
/* XXX Doesn't do coercions... */
|
|
|
|
object *func;
|
|
|
|
object *args;
|
|
|
|
object *result;
|
1996-08-09 17:53:24 -03:00
|
|
|
static object *powstr;
|
|
|
|
|
|
|
|
if (powstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
powstr = PyString_InternFromString("__pow__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = getattro(v, powstr);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
|
|
|
args = mkvalue("(OO)", w, z);
|
|
|
|
if (args == NULL) {
|
|
|
|
DECREF(func);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
result = call_object(func, args);
|
|
|
|
DECREF(func);
|
|
|
|
DECREF(args);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
static number_methods instance_as_number = {
|
1994-09-28 12:51:32 -03:00
|
|
|
0, /*nb_add*/
|
|
|
|
0, /*nb_subtract*/
|
|
|
|
0, /*nb_multiply*/
|
|
|
|
0, /*nb_divide*/
|
|
|
|
0, /*nb_remainder*/
|
|
|
|
0, /*nb_divmod*/
|
1994-09-05 04:32:29 -03:00
|
|
|
(ternaryfunc)instance_pow, /*nb_power*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(unaryfunc)instance_neg, /*nb_negative*/
|
|
|
|
(unaryfunc)instance_pos, /*nb_positive*/
|
|
|
|
(unaryfunc)instance_abs, /*nb_absolute*/
|
|
|
|
(inquiry)instance_nonzero, /*nb_nonzero*/
|
|
|
|
(unaryfunc)instance_invert, /*nb_invert*/
|
1994-09-28 12:51:32 -03:00
|
|
|
0, /*nb_lshift*/
|
|
|
|
0, /*nb_rshift*/
|
|
|
|
0, /*nb_and*/
|
|
|
|
0, /*nb_xor*/
|
|
|
|
0, /*nb_or*/
|
1995-01-10 11:24:06 -04:00
|
|
|
(coercion)instance_coerce, /*nb_coerce*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(unaryfunc)instance_int, /*nb_int*/
|
|
|
|
(unaryfunc)instance_long, /*nb_long*/
|
|
|
|
(unaryfunc)instance_float, /*nb_float*/
|
|
|
|
(unaryfunc)instance_oct, /*nb_oct*/
|
|
|
|
(unaryfunc)instance_hex, /*nb_hex*/
|
1992-08-12 12:35:34 -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,
|
1994-08-01 08:34:53 -03:00
|
|
|
(destructor)instance_dealloc, /*tp_dealloc*/
|
1992-09-17 14:54:56 -03:00
|
|
|
0, /*tp_print*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_getattr*/
|
|
|
|
0, /*tp_setattr*/
|
|
|
|
instance_compare, /*tp_compare*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(reprfunc)instance_repr, /*tp_repr*/
|
1992-08-12 12:35:34 -03:00
|
|
|
&instance_as_number, /*tp_as_number*/
|
|
|
|
&instance_as_sequence, /*tp_as_sequence*/
|
|
|
|
&instance_as_mapping, /*tp_as_mapping*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(hashfunc)instance_hash, /*tp_hash*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
(getattrofunc)instance_getattr, /*tp_getattro*/
|
|
|
|
(setattrofunc)instance_setattr, /*tp_setattro*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|
|
|
|
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
/* Instance method objects are used for two purposes:
|
|
|
|
(a) as bound instance methods (returned by instancename.methodname)
|
|
|
|
(b) as unbound methods (returned by ClassName.methodname)
|
|
|
|
In case (b), im_self is NULL
|
|
|
|
*/
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
typedef struct {
|
|
|
|
OB_HEAD
|
1993-05-20 11:24:46 -03:00
|
|
|
object *im_func; /* The function implementing the method */
|
|
|
|
object *im_self; /* The instance it is bound to, or NULL */
|
|
|
|
object *im_class; /* The class that defined the method */
|
1991-05-05 17:03:07 -03:00
|
|
|
} instancemethodobject;
|
1990-10-14 09:07:46 -03:00
|
|
|
|
|
|
|
object *
|
1993-05-20 11:24:46 -03:00
|
|
|
newinstancemethodobject(func, self, class)
|
1990-10-14 09:07:46 -03:00
|
|
|
object *func;
|
|
|
|
object *self;
|
1993-05-20 11:24:46 -03:00
|
|
|
object *class;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
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;
|
1993-05-20 11:24:46 -03:00
|
|
|
XINCREF(self);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_self = self;
|
1993-05-20 11:24:46 -03:00
|
|
|
INCREF(class);
|
|
|
|
im->im_class = class;
|
1991-05-05 17:03:07 -03:00
|
|
|
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
|
|
|
}
|
|
|
|
|
1993-05-20 11:24:46 -03:00
|
|
|
object *
|
|
|
|
instancemethodgetclass(im)
|
|
|
|
register object *im;
|
|
|
|
{
|
|
|
|
if (!is_instancemethodobject(im)) {
|
|
|
|
err_badcall();
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return ((instancemethodobject *)im)->im_class;
|
|
|
|
}
|
|
|
|
|
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)},
|
1993-05-20 11:24:46 -03:00
|
|
|
{"im_class", T_OBJECT, OFF(im_class)},
|
1996-05-14 18:54:20 -03:00
|
|
|
/* Dummies that are not handled by getattr() except for __members__ */
|
|
|
|
{"__doc__", T_INT, 0},
|
|
|
|
{"__name__", T_INT, 0},
|
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;
|
1996-08-09 17:53:24 -03:00
|
|
|
object *name;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1996-08-09 17:53:24 -03:00
|
|
|
char *sname = getstringvalue(name);
|
|
|
|
if (sname[0] == '_') {
|
1996-05-14 18:54:20 -03:00
|
|
|
funcobject *func = (funcobject *)(im->im_func);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__name__") == 0) {
|
1996-05-14 18:54:20 -03:00
|
|
|
INCREF(func->func_name);
|
|
|
|
return func->func_name;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__doc__") == 0) {
|
1996-05-14 18:54:20 -03:00
|
|
|
INCREF(func->func_doc);
|
|
|
|
return func->func_doc;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (getrestricted()) {
|
1995-01-10 06:39:49 -04:00
|
|
|
err_setstr(RuntimeError,
|
|
|
|
"instance-method attributes not accessible in restricted mode");
|
|
|
|
return NULL;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
return getmember((char *)im, instancemethod_memberlist, sname);
|
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);
|
1993-05-20 11:24:46 -03:00
|
|
|
XDECREF(im->im_self);
|
|
|
|
DECREF(im->im_class);
|
1991-05-05 17:03:07 -03:00
|
|
|
free((ANY *)im);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1992-09-03 17:39:51 -03:00
|
|
|
static int
|
|
|
|
instancemethod_compare(a, b)
|
|
|
|
instancemethodobject *a, *b;
|
|
|
|
{
|
1995-04-06 11:46:51 -03:00
|
|
|
if (a->im_self != b->im_self)
|
|
|
|
return (a->im_self < b->im_self) ? -1 : 1;
|
|
|
|
return cmpobject(a->im_func, b->im_func);
|
1992-09-03 17:39:51 -03:00
|
|
|
}
|
|
|
|
|
1993-05-19 11:50:45 -03:00
|
|
|
static object *
|
|
|
|
instancemethod_repr(a)
|
|
|
|
instancemethodobject *a;
|
|
|
|
{
|
|
|
|
char buf[240];
|
1993-05-20 11:24:46 -03:00
|
|
|
instanceobject *self = (instanceobject *)(a->im_self);
|
|
|
|
funcobject *func = (funcobject *)(a->im_func);
|
|
|
|
classobject *class = (classobject *)(a->im_class);
|
|
|
|
object *fclassname, *iclassname, *funcname;
|
|
|
|
char *fcname, *icname, *fname;
|
|
|
|
fclassname = class->cl_name;
|
|
|
|
funcname = func->func_name;
|
|
|
|
if (fclassname != NULL && is_stringobject(fclassname))
|
|
|
|
fcname = getstringvalue(fclassname);
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
1993-05-20 11:24:46 -03:00
|
|
|
fcname = "?";
|
1993-05-19 11:50:45 -03:00
|
|
|
if (funcname != NULL && is_stringobject(funcname))
|
|
|
|
fname = getstringvalue(funcname);
|
|
|
|
else
|
|
|
|
fname = "?";
|
1993-05-20 11:24:46 -03:00
|
|
|
if (self == NULL)
|
|
|
|
sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname);
|
|
|
|
else {
|
|
|
|
iclassname = self->in_class->cl_name;
|
|
|
|
if (iclassname != NULL && is_stringobject(iclassname))
|
|
|
|
icname = getstringvalue(iclassname);
|
|
|
|
else
|
|
|
|
icname = "?";
|
|
|
|
sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
|
|
|
|
fcname, fname, icname, (long)self);
|
|
|
|
}
|
1993-05-19 11:50:45 -03:00
|
|
|
return newstringobject(buf);
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
|
|
|
instancemethod_hash(a)
|
|
|
|
instancemethodobject *a;
|
|
|
|
{
|
|
|
|
long x, y;
|
1993-05-20 11:24:46 -03:00
|
|
|
if (a->im_self == NULL)
|
|
|
|
x = hashobject(None);
|
|
|
|
else
|
|
|
|
x = hashobject(a->im_self);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (x == -1)
|
|
|
|
return -1;
|
|
|
|
y = hashobject(a->im_func);
|
|
|
|
if (y == -1)
|
|
|
|
return -1;
|
|
|
|
return x ^ y;
|
|
|
|
}
|
|
|
|
|
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,
|
1994-08-01 08:34:53 -03:00
|
|
|
(destructor)instancemethod_dealloc, /*tp_dealloc*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_print*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_getattr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_setattr*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(cmpfunc)instancemethod_compare, /*tp_compare*/
|
|
|
|
(reprfunc)instancemethod_repr, /*tp_repr*/
|
1990-10-14 09:07:46 -03:00
|
|
|
0, /*tp_as_number*/
|
|
|
|
0, /*tp_as_sequence*/
|
|
|
|
0, /*tp_as_mapping*/
|
1994-08-01 08:34:53 -03:00
|
|
|
(hashfunc)instancemethod_hash, /*tp_hash*/
|
1996-08-09 17:53:24 -03:00
|
|
|
0, /*tp_call*/
|
|
|
|
0, /*tp_str*/
|
|
|
|
(getattrofunc)instancemethod_getattr, /*tp_getattro*/
|
|
|
|
0, /*tp_setattro*/
|
1990-10-14 09:07:46 -03:00
|
|
|
};
|