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 */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#include "Python.h"
|
1990-12-20 11:06:42 -04:00
|
|
|
#include "structmember.h"
|
1992-08-12 12:35:34 -03:00
|
|
|
|
1994-09-05 04:32:29 -03:00
|
|
|
/* Forward */
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *class_lookup
|
|
|
|
Py_PROTO((PyClassObject *, PyObject *, PyClassObject **));
|
|
|
|
static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *));
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyClass_New(bases, dict, name)
|
|
|
|
PyObject *bases; /* NULL or tuple of classobjects! */
|
|
|
|
PyObject *dict;
|
|
|
|
PyObject *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;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *key, *value;
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *op, *dummy;
|
|
|
|
static PyObject *getattrstr, *setattrstr, *delattrstr;
|
|
|
|
static PyObject *docstr;
|
1996-08-21 11:54:28 -03:00
|
|
|
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;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyDict_GetItem(dict, docstr) == NULL) {
|
|
|
|
if (PyDict_SetItem(dict, docstr, Py_None) < 0)
|
1995-01-07 08:35:18 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1991-12-10 09:53:23 -04:00
|
|
|
if (bases == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
bases = PyTuple_New(0);
|
1991-12-10 09:53:23 -04:00
|
|
|
if (bases == NULL)
|
1993-03-16 08:15:04 -04:00
|
|
|
return NULL;
|
1991-12-10 09:53:23 -04:00
|
|
|
}
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(bases);
|
|
|
|
op = PyObject_NEW(PyClassObject, &PyClass_Type);
|
1991-12-10 09:53:23 -04:00
|
|
|
if (op == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_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;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(dict);
|
1993-05-20 11:24:46 -03:00
|
|
|
op->cl_dict = dict;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(name);
|
1991-10-20 17:11:48 -03:00
|
|
|
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);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(op->cl_getattr);
|
|
|
|
Py_XINCREF(op->cl_setattr);
|
|
|
|
Py_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;
|
1997-05-02 00:12:38 -03:00
|
|
|
while (PyDict_Next(dict, &pos, &key, &value)) {
|
|
|
|
if (PyAccess_Check(value))
|
|
|
|
PyAccess_SetOwner(value, (PyObject *)op);
|
1993-05-20 11:24:46 -03:00
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *) op;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Class methods */
|
|
|
|
|
|
|
|
static void
|
|
|
|
class_dealloc(op)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *op;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(op->cl_bases);
|
|
|
|
Py_DECREF(op->cl_dict);
|
|
|
|
Py_XDECREF(op->cl_name);
|
1990-10-14 09:07:46 -03:00
|
|
|
free((ANY *)op);
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1993-05-20 11:24:46 -03:00
|
|
|
class_lookup(cp, name, pclass)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *cp;
|
|
|
|
PyObject *name;
|
|
|
|
PyClassObject **pclass;
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
|
|
|
int i, n;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *value = PyDict_GetItem(cp->cl_dict, name);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (value != NULL) {
|
|
|
|
*pclass = cp;
|
|
|
|
return value;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
n = PyTuple_Size(cp->cl_bases);
|
1993-05-20 11:24:46 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = class_lookup(
|
|
|
|
(PyClassObject *)
|
|
|
|
PyTuple_GetItem(cp->cl_bases, i), name, pclass);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (v != NULL)
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1990-10-14 09:07:46 -03:00
|
|
|
class_getattr(op, name)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyClassObject *op;
|
|
|
|
PyObject *name;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *v;
|
|
|
|
register char *sname = PyString_AsString(name);
|
|
|
|
PyClassObject *class;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"class.__dict__ not accessible in restricted mode");
|
1995-01-10 06:39:49 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(op->cl_dict);
|
1995-01-10 06:39:49 -04:00
|
|
|
return op->cl_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__bases__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(op->cl_bases);
|
1995-01-10 06:39:49 -04:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
v = Py_None;
|
1995-01-10 06:39:49 -04:00
|
|
|
else
|
|
|
|
v = op->cl_name;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1995-01-10 06:39:49 -04:00
|
|
|
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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetObject(PyExc_AttributeError, name);
|
1993-05-21 16:56:10 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyAccess_Check(v)) {
|
|
|
|
v = PyAccess_AsValue(v, PyEval_GetOwner());
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
|
|
|
if (PyFunction_Check(v)) {
|
|
|
|
PyObject *w = PyMethod_New(v, (PyObject *)NULL,
|
|
|
|
(PyObject *)class);
|
|
|
|
Py_DECREF(v);
|
1993-05-21 16:56:10 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *op;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *v;
|
1991-10-20 17:11:48 -03:00
|
|
|
{
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *ac;
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
char *sname = PyString_AsString(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
1997-05-02 00:12:38 -03:00
|
|
|
int n = PyString_Size(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"read-only special attribute");
|
1992-04-05 11:24:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
1995-08-04 01:05:31 -03:00
|
|
|
"classes are read-only in restricted mode");
|
|
|
|
return -1;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1997-05-02 00:12:38 -03:00
|
|
|
ac = PyDict_GetItem(op->cl_dict, name);
|
|
|
|
if (ac != NULL && PyAccess_Check(ac))
|
|
|
|
return PyAccess_SetValue(ac, PyEval_GetOwner(), v);
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int rv = PyDict_DelItem(op->cl_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
1992-09-04 06:45:18 -03:00
|
|
|
"delete non-existing class attribute");
|
|
|
|
return rv;
|
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyDict_SetItem(op->cl_dict, name, v);
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1993-05-19 11:50:45 -03:00
|
|
|
class_repr(op)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *op;
|
1993-05-19 11:50:45 -03:00
|
|
|
{
|
|
|
|
char buf[140];
|
|
|
|
char *name;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (op->cl_name == NULL || !PyString_Check(op->cl_name))
|
1993-05-19 11:50:45 -03:00
|
|
|
name = "?";
|
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
name = PyString_AsString(op->cl_name);
|
1993-05-19 11:50:45 -03:00
|
|
|
sprintf(buf, "<class %.100s at %lx>", name, (long)op);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyClass_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
|
|
|
"class",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyClassObject),
|
1990-10-14 09:07:46 -03:00
|
|
|
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
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClass_IsSubclass(class, base)
|
|
|
|
PyObject *class;
|
|
|
|
PyObject *base;
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
|
|
|
int i, n;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *cp;
|
1993-05-20 11:24:46 -03:00
|
|
|
if (class == base)
|
|
|
|
return 1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (class == NULL || !PyClass_Check(class))
|
1993-05-25 06:38:27 -03:00
|
|
|
return 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
cp = (PyClassObject *)class;
|
|
|
|
n = PyTuple_Size(cp->cl_bases);
|
1993-05-20 11:24:46 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
|
1993-05-20 11:24:46 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyClassObject *class;
|
|
|
|
PyInstanceObject *inst;
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
|
|
|
int i, n, pos, ret;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *key, *value, *ac;
|
1993-05-20 11:24:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
n = PyTuple_Size(class->cl_bases);
|
1993-05-20 11:24:46 -03:00
|
|
|
for (i = 0; i < n; i++) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (addaccess((PyClassObject *)PyTuple_GetItem(
|
|
|
|
class->cl_bases, i), inst) < 0)
|
1993-05-20 11:24:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
pos = 0;
|
1997-05-02 00:12:38 -03:00
|
|
|
while (PyDict_Next(class->cl_dict, &pos, &key, &value)) {
|
|
|
|
if (!PyAccess_Check(value))
|
1993-05-20 11:24:46 -03:00
|
|
|
continue;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyAccess_HasValue(value))
|
1993-05-21 16:56:10 -03:00
|
|
|
continue;
|
1997-05-02 00:12:38 -03:00
|
|
|
ac = PyDict_GetItem(inst->in_dict, key);
|
|
|
|
if (ac != NULL && PyAccess_Check(ac)) {
|
|
|
|
PyErr_SetObject(PyExc_ConflictError, key);
|
1993-05-20 11:24:46 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
ac = PyAccess_Clone(value);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (ac == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
ret = PyDict_SetItem(inst->in_dict, key, ac);
|
|
|
|
Py_DECREF(ac);
|
1993-05-20 11:24:46 -03:00
|
|
|
if (ret != 0)
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1993-05-20 11:24:46 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyInstance_New(class, arg, kw)
|
|
|
|
PyObject *class;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *kw;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyInstanceObject *inst;
|
|
|
|
PyObject *init;
|
|
|
|
static PyObject *initstr;
|
|
|
|
if (!PyClass_Check(class)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
|
1991-05-05 17:03:07 -03:00
|
|
|
if (inst == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(class);
|
|
|
|
inst->in_class = (PyClassObject *)class;
|
|
|
|
inst->in_dict = PyDict_New();
|
1996-08-12 19:00:53 -03:00
|
|
|
if (inst->in_dict == NULL
|
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1997-05-02 00:12:38 -03:00
|
|
|
|| addaccess((PyClassObject *)class, inst) != 0
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
|
|
|
) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
if ((arg != NULL && (!PyTuple_Check(arg) ||
|
|
|
|
PyTuple_Size(arg) != 0))
|
|
|
|
|| (kw != NULL && (!PyDict_Check(kw) ||
|
|
|
|
PyDict_Size(kw) != 0))) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-07-26 15:07:32 -03:00
|
|
|
"this constructor takes no arguments");
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
|
|
|
|
Py_DECREF(init);
|
1993-05-19 11:50:45 -03:00
|
|
|
if (res == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (res != Py_None) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1993-05-19 11:50:45 -03:00
|
|
|
"__init__() should return None");
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst);
|
1993-05-19 11:50:45 -03:00
|
|
|
inst = NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)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)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyInstanceObject *inst;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *error_type, *error_value, *error_traceback;
|
|
|
|
PyObject *del;
|
|
|
|
static PyObject *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 */
|
1997-05-02 00:12:38 -03:00
|
|
|
extern long _Py_RefTotal;
|
|
|
|
inst->ob_type = &PyInstance_Type;
|
|
|
|
_Py_NewReference(inst);
|
|
|
|
_Py_RefTotal--; /* compensate for increment in NEWREF */
|
1995-08-28 06:00:43 -03:00
|
|
|
#ifdef COUNT_ALLOCS
|
|
|
|
inst->ob_type->tp_alloc--; /* ditto */
|
|
|
|
#endif
|
1996-05-23 19:46:51 -03:00
|
|
|
#else /* !Py_TRACE_REFS */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(inst);
|
1996-05-23 19:46:51 -03:00
|
|
|
#endif /* !Py_TRACE_REFS */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
|
1996-08-12 19:00:53 -03:00
|
|
|
if (res == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *f, *t, *v, *tb;
|
|
|
|
PyErr_Fetch(&t, &v, &tb);
|
|
|
|
f = PySys_GetObject("stderr");
|
|
|
|
PyErr_Clear();
|
1996-09-11 19:51:57 -03:00
|
|
|
if (f != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFile_WriteString("Exception ", f);
|
1996-12-05 17:52:32 -04:00
|
|
|
if (t) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFile_WriteObject(t, f, Py_PRINT_RAW);
|
|
|
|
if (v && v != Py_None) {
|
|
|
|
PyFile_WriteString(": ", f);
|
|
|
|
PyFile_WriteObject(v, f, 0);
|
1996-12-05 17:52:32 -04:00
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFile_WriteString(" in ", f);
|
|
|
|
PyFile_WriteObject(del, f, 0);
|
|
|
|
PyFile_WriteString(" ignored\n", f);
|
|
|
|
PyErr_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
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
|
|
|
Py_DECREF(del);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
/* Restore the saved exception and undo the temporary revival */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_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
|
1997-05-02 00:12:38 -03:00
|
|
|
_Py_ForgetReference(inst);
|
1995-08-28 06:00:43 -03:00
|
|
|
inst->ob_type = NULL;
|
1996-05-23 19:46:51 -03:00
|
|
|
#endif /* Py_TRACE_REFS */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(inst->in_class);
|
|
|
|
Py_XDECREF(inst->in_dict);
|
1991-05-05 17:03:07 -03:00
|
|
|
free((ANY *)inst);
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1994-09-05 04:31:41 -03:00
|
|
|
instance_getattr1(inst, name)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyInstanceObject *inst;
|
|
|
|
PyObject *name;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *v;
|
|
|
|
register char *sname = PyString_AsString(name);
|
|
|
|
PyClassObject *class;
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_') {
|
|
|
|
if (strcmp(sname, "__dict__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"instance.__dict__ not accessible in restricted mode");
|
1995-01-10 06:39:49 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(inst->in_dict);
|
1993-10-22 09:04:32 -03:00
|
|
|
return inst->in_dict;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__class__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(inst->in_class);
|
|
|
|
return (PyObject *)inst->in_class;
|
1993-10-22 09:04:32 -03:00
|
|
|
}
|
1991-10-20 17:11:48 -03:00
|
|
|
}
|
1993-05-25 06:38:27 -03:00
|
|
|
class = NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
v = PyDict_GetItem(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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetObject(PyExc_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
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyAccess_Check(v)) {
|
|
|
|
v = PyAccess_AsValue(v, PyEval_GetOwner());
|
1993-05-21 16:56:10 -03:00
|
|
|
if (v == NULL)
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
else
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
if (class != NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyFunction_Check(v)) {
|
|
|
|
PyObject *w = PyMethod_New(v, (PyObject *)inst,
|
|
|
|
(PyObject *)class);
|
|
|
|
Py_DECREF(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
v = w;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
else if (PyMethod_Check(v)) {
|
|
|
|
PyObject *im_class = PyMethod_Class(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
/* Only if classes are compatible */
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyClass_IsSubclass((PyObject *)class, im_class)) {
|
|
|
|
PyObject *im_func = PyMethod_Function(v);
|
|
|
|
PyObject *w = PyMethod_New(im_func,
|
|
|
|
(PyObject *)inst, im_class);
|
|
|
|
Py_DECREF(v);
|
1993-10-15 10:01:11 -03:00
|
|
|
v = w;
|
|
|
|
}
|
|
|
|
}
|
1993-05-21 16:56:10 -03:00
|
|
|
}
|
|
|
|
return v;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1994-09-05 04:31:41 -03:00
|
|
|
instance_getattr(inst, name)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyInstanceObject *inst;
|
|
|
|
PyObject *name;
|
1994-09-05 04:31:41 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyObject *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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *args;
|
|
|
|
PyErr_Clear();
|
|
|
|
args = Py_BuildValue("(OO)", inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
1994-09-05 04:31:41 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, args);
|
|
|
|
Py_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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *v;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1996-08-12 19:00:53 -03:00
|
|
|
#ifdef SUPPORT_OBSOLETE_ACCESS
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *ac;
|
|
|
|
ac = PyDict_GetItem(inst->in_dict, name);
|
|
|
|
if (ac != NULL && PyAccess_Check(ac))
|
|
|
|
return PyAccess_SetValue(ac, PyEval_GetOwner(), v);
|
1996-08-12 19:00:53 -03:00
|
|
|
#endif
|
1992-09-04 06:45:18 -03:00
|
|
|
if (v == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int rv = PyDict_DelItem(inst->in_dict, name);
|
1992-09-04 06:45:18 -03:00
|
|
|
if (rv < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_AttributeError,
|
1992-09-04 06:45:18 -03:00
|
|
|
"delete non-existing instance attribute");
|
|
|
|
return rv;
|
|
|
|
}
|
1990-10-14 09:07:46 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyDict_SetItem(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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
|
|
|
PyObject *name;
|
|
|
|
PyObject *v;
|
1994-09-05 04:31:41 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *args, *res;
|
|
|
|
char *sname = PyString_AsString(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_' && sname[1] == '_'
|
|
|
|
&& (strcmp(sname, "__dict__") == 0 ||
|
|
|
|
strcmp(sname, "__class__") == 0)) {
|
1997-05-02 00:12:38 -03:00
|
|
|
int n = PyString_Size(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[n-1] == '_' && sname[n-2] == '_') {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_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)
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(OO)", inst, name);
|
1994-09-05 04:32:29 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(OOO)", inst, name, v);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (args == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(args);
|
1994-09-05 04:32:29 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1994-09-05 04:31:41 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_repr(inst)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
|
|
|
static PyObject *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];
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *classname = inst->in_class->cl_name;
|
1993-05-19 11:50:45 -03:00
|
|
|
char *cname;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (classname != NULL && PyString_Check(classname))
|
|
|
|
cname = PyString_AsString(classname);
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
|
|
|
cname = "?";
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1993-05-19 11:50:45 -03:00
|
|
|
sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1995-01-07 08:35:18 -04:00
|
|
|
instance_compare1(inst, other)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *inst, *other;
|
1995-01-07 08:35:18 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__",
|
1995-01-07 08:35:18 -04:00
|
|
|
instance_compare1);
|
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_compare(inst, other)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *inst, *other;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *result;
|
1995-01-07 08:35:18 -04:00
|
|
|
long outcome;
|
|
|
|
result = instance_compare1(inst, other);
|
1997-05-02 00:12:38 -03:00
|
|
|
if (result == NULL || !PyInt_Check(result)) {
|
|
|
|
PyErr_Clear();
|
1994-11-10 18:31:02 -04:00
|
|
|
return (inst < other) ? -1 : 1;
|
1994-10-19 12:11:52 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
outcome = PyInt_AsLong(result);
|
|
|
|
Py_DECREF(result);
|
1994-09-28 12:51:32 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
1993-04-08 09:56:19 -03:00
|
|
|
long outcome;
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *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__. */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1993-04-08 09:56:19 -03:00
|
|
|
outcome = (long)inst;
|
|
|
|
if (outcome == -1)
|
|
|
|
outcome = -2;
|
|
|
|
return outcome;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "unhashable instance");
|
1993-03-29 06:43:31 -04:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInt_Check(res)) {
|
|
|
|
outcome = PyInt_AsLong(res);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (outcome == -1)
|
|
|
|
outcome = -2;
|
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__hash__() should return an int");
|
1993-03-29 06:43:31 -04:00
|
|
|
outcome = -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1993-03-29 06:43:31 -04:00
|
|
|
return outcome;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static int
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_length(inst)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
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;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-11-26 06:30:26 -04:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyInt_Check(res)) {
|
|
|
|
outcome = PyInt_AsLong(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (outcome < 0)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"__len__() should return >= 0");
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__len__() should return an int");
|
1992-08-12 12:35:34 -03:00
|
|
|
outcome = -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return outcome;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_subscript(inst, key)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
|
|
|
PyObject *key;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
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;
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(O)", key);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject*inst;
|
|
|
|
PyObject *key;
|
|
|
|
PyObject *value;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *arg;
|
|
|
|
PyObject *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(O)", key);
|
1992-08-12 12:35:34 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(OO)", key, value);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyMappingMethods 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
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_item(inst, i)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
int i;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
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;
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(i)", i);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1992-08-12 12:35:34 -03:00
|
|
|
instance_slice(inst, i, j)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
int i, j;
|
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
|
|
|
static PyObject *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;
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(ii)", i, j);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
instance_ass_item(inst, i, item)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
int i;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *item;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("i", i);
|
1992-08-12 12:35:34 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(iO)", i, item);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
instance_ass_slice(inst, i, j, value)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *inst;
|
1992-08-12 12:35:34 -03:00
|
|
|
int i, j;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *value;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *arg, *res;
|
|
|
|
static PyObject *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-05-02 00:12:38 -03:00
|
|
|
delslicestr =
|
|
|
|
PyString_InternFromString("__delslice__");
|
1996-08-09 17:53:24 -03:00
|
|
|
func = instance_getattr(inst, delslicestr);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
if (setslicestr == NULL)
|
1997-05-02 00:12:38 -03: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)
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(ii)", i, j);
|
1992-08-12 12:35:34 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
arg = Py_BuildValue("(iiO)", i, j, value);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (arg == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1992-08-14 10:49:30 -03:00
|
|
|
return -1;
|
1992-08-12 12:35:34 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, arg);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(arg);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PySequenceMethods 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
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1994-09-28 12:51:32 -03:00
|
|
|
generic_unary_op(self, methodname)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *self;
|
|
|
|
PyObject *methodname;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
|
|
|
|
if ((func = instance_getattr(self, methodname)) == NULL)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
1994-09-28 12:51:32 -03:00
|
|
|
|
|
|
|
/* Forward */
|
1997-05-02 00:12:38 -03:00
|
|
|
static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **,
|
|
|
|
PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int ));
|
1994-09-28 12:51:32 -03:00
|
|
|
|
|
|
|
|
|
|
|
/* Implement a binary operator involving at least one class instance. */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
|
|
|
|
PyObject *v;
|
|
|
|
PyObject *w;
|
1994-09-28 12:51:32 -03:00
|
|
|
char *opname;
|
|
|
|
char *ropname;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1994-09-28 12:51:32 -03:00
|
|
|
char buf[256];
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *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);
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, buf);
|
1994-09-28 12:51:32 -03:00
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *coerce_obj;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *w;
|
1994-09-28 12:51:32 -03:00
|
|
|
char *opname;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **r_result;
|
|
|
|
PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
|
1995-01-07 08:35:18 -04:00
|
|
|
int swapped;
|
1994-09-28 12:51:32 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *PyNumber_Coerce;
|
|
|
|
PyObject *coerced = NULL;
|
|
|
|
PyObject *v1;
|
1994-09-28 12:51:32 -03:00
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInstance_Check(v))
|
1994-09-28 12:51:32 -03:00
|
|
|
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;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj);
|
|
|
|
if (PyNumber_Coerce == NULL) {
|
|
|
|
PyErr_Clear();
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
|
|
|
else {
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(O)", w);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
coerced = PyEval_CallObject(PyNumber_Coerce, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(PyNumber_Coerce);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (coerced == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (coerced == Py_None) {
|
|
|
|
Py_DECREF(coerced);
|
1994-09-28 12:51:32 -03:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
|
|
|
|
Py_DECREF(coerced);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-01-07 08:35:18 -04:00
|
|
|
"coercion should return None or 2-tuple");
|
1994-09-28 12:51:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
v1 = PyTuple_GetItem(coerced, 0);
|
|
|
|
w = PyTuple_GetItem(coerced, 1);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (v1 != v) {
|
|
|
|
v = v1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
|
1995-01-07 08:35:18 -04:00
|
|
|
if (swapped)
|
|
|
|
*r_result = (*thisfunc)(w, v);
|
|
|
|
else
|
|
|
|
*r_result = (*thisfunc)(v, w);
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(coerced);
|
1995-01-07 08:35:18 -04:00
|
|
|
return *r_result == NULL ? -1 : 0;
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
w = PyTuple_GetItem(coerced, 1);
|
1994-09-28 12:51:32 -03:00
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
func = PyObject_GetAttrString(v, opname);
|
1995-01-07 08:35:18 -04:00
|
|
|
if (func == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XDECREF(coerced);
|
|
|
|
if (PyErr_Occurred() != PyExc_AttributeError)
|
1995-01-07 08:35:18 -04:00
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1995-01-07 08:35:18 -04:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(O)", w);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
|
|
|
Py_XDECREF(coerced);
|
1994-09-28 12:51:32 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
*r_result = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_XDECREF(coerced);
|
1994-09-28 12:51:32 -03:00
|
|
|
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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject **pv;
|
|
|
|
PyObject **pw;
|
1995-01-10 11:24:06 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v = *pv;
|
|
|
|
PyObject *w = *pw;
|
|
|
|
PyObject *PyNumber_Coerce;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *coerced;
|
1995-01-10 11:24:06 -04:00
|
|
|
|
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;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj);
|
|
|
|
if (PyNumber_Coerce == NULL) {
|
1995-01-10 11:24:06 -04:00
|
|
|
/* No __coerce__ method: always OK */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
|
|
|
Py_INCREF(v);
|
|
|
|
Py_INCREF(w);
|
1995-01-10 11:24:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
/* Has __coerce__ method: call it */
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(O)", w);
|
1995-01-10 11:24:06 -04:00
|
|
|
if (args == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
coerced = PyEval_CallObject(PyNumber_Coerce, args);
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(PyNumber_Coerce);
|
1995-01-10 11:24:06 -04:00
|
|
|
if (coerced == NULL) {
|
|
|
|
/* __coerce__ call raised an exception */
|
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (coerced == Py_None) {
|
1995-01-10 11:24:06 -04:00
|
|
|
/* __coerce__ says "I can't do it" */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(coerced);
|
1995-01-10 11:24:06 -04:00
|
|
|
return 1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
|
1995-01-10 11:24:06 -04:00
|
|
|
/* __coerce__ return value is malformed */
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(coerced);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
1995-01-10 11:24:06 -04:00
|
|
|
"coercion should return None or 2-tuple");
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* __coerce__ returned two new values */
|
1997-05-02 00:12:38 -03:00
|
|
|
*pv = PyTuple_GetItem(coerced, 0);
|
|
|
|
*pw = PyTuple_GetItem(coerced, 1);
|
|
|
|
Py_INCREF(*pv);
|
|
|
|
Py_INCREF(*pw);
|
|
|
|
Py_DECREF(coerced);
|
1995-01-10 11:24:06 -04:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
1992-08-12 12:35:34 -03:00
|
|
|
|
|
|
|
#define UNARY(funcname, methodname) \
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *funcname(self) PyInstanceObject *self; { \
|
|
|
|
static PyObject *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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *self;
|
1992-08-12 12:35:34 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func, *res;
|
1992-08-12 12:35:34 -03:00
|
|
|
long outcome;
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_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) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_Clear();
|
1992-08-12 12:35:34 -03:00
|
|
|
/* Fall back to the default behavior:
|
|
|
|
all instances are nonzero */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
res = PyEval_CallObject(func, (PyObject *)NULL);
|
|
|
|
Py_DECREF(func);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (res == NULL)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyInt_Check(res)) {
|
|
|
|
Py_DECREF(res);
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"__nonzero__ should return an int");
|
1992-08-12 12:35:34 -03:00
|
|
|
return -1;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
outcome = PyInt_AsLong(res);
|
|
|
|
Py_DECREF(res);
|
1992-08-12 12:35:34 -03:00
|
|
|
if (outcome < 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyErr_SetString(PyExc_ValueError,
|
|
|
|
"__nonzero__ should return >= 0");
|
1992-08-12 12:35:34 -03:00
|
|
|
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) */
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1994-09-28 12:51:32 -03:00
|
|
|
instance_pow(v, w, z)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *v;
|
|
|
|
PyObject *w;
|
|
|
|
PyObject *z;
|
1994-09-28 12:51:32 -03:00
|
|
|
{
|
|
|
|
/* XXX Doesn't do coercions... */
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *func;
|
|
|
|
PyObject *args;
|
|
|
|
PyObject *result;
|
|
|
|
static PyObject *powstr;
|
1996-08-09 17:53:24 -03:00
|
|
|
|
|
|
|
if (powstr == NULL)
|
1997-01-18 03:59:12 -04:00
|
|
|
powstr = PyString_InternFromString("__pow__");
|
1997-05-02 00:12:38 -03:00
|
|
|
func = PyObject_GetAttr(v, powstr);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (func == NULL)
|
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
args = Py_BuildValue("(OO)", w, z);
|
1994-09-28 12:51:32 -03:00
|
|
|
if (args == NULL) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(func);
|
1994-09-28 12:51:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
result = PyEval_CallObject(func, args);
|
|
|
|
Py_DECREF(func);
|
|
|
|
Py_DECREF(args);
|
1994-09-28 12:51:32 -03:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyNumberMethods 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
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyInstance_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-04-04 06:42:10 -04:00
|
|
|
"instance",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyInstanceObject),
|
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 {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject_HEAD
|
|
|
|
PyObject *im_func; /* The function implementing the method */
|
|
|
|
PyObject *im_self; /* The instance it is bound to, or NULL */
|
|
|
|
PyObject *im_class; /* The class that defined the method */
|
|
|
|
} PyMethodObject;
|
|
|
|
|
|
|
|
PyObject *
|
|
|
|
PyMethod_New(func, self, class)
|
|
|
|
PyObject *func;
|
|
|
|
PyObject *self;
|
|
|
|
PyObject *class;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyMethodObject *im;
|
|
|
|
if (!PyFunction_Check(func)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
im = PyObject_NEW(PyMethodObject, &PyMethod_Type);
|
1991-05-05 17:03:07 -03:00
|
|
|
if (im == NULL)
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(func);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_func = func;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_XINCREF(self);
|
1991-05-05 17:03:07 -03:00
|
|
|
im->im_self = self;
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(class);
|
1993-05-20 11:24:46 -03:00
|
|
|
im->im_class = class;
|
1997-05-02 00:12:38 -03:00
|
|
|
return (PyObject *)im;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyMethod_Function(im)
|
|
|
|
register PyObject *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyMethodObject *)im)->im_func;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyMethod_Self(im)
|
|
|
|
register PyObject *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
1990-10-14 09:07:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyMethodObject *)im)->im_self;
|
1990-10-14 09:07:46 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyObject *
|
|
|
|
PyMethod_Class(im)
|
|
|
|
register PyObject *im;
|
1993-05-20 11:24:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
if (!PyMethod_Check(im)) {
|
|
|
|
PyErr_BadInternalCall();
|
1993-05-20 11:24:46 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return ((PyMethodObject *)im)->im_class;
|
1993-05-20 11:24:46 -03:00
|
|
|
}
|
|
|
|
|
1990-10-14 09:07:46 -03:00
|
|
|
/* Class method methods */
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
#define OFF(x) offsetof(PyMethodObject, 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 */
|
|
|
|
};
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1991-05-05 17:03:07 -03:00
|
|
|
instancemethod_getattr(im, name)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyMethodObject *im;
|
|
|
|
PyObject *name;
|
1990-12-20 11:06:42 -04:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
char *sname = PyString_AsString(name);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (sname[0] == '_') {
|
1997-05-02 00:12:38 -03:00
|
|
|
PyFunctionObject *func = (PyFunctionObject *)(im->im_func);
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__name__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(func->func_name);
|
1996-05-14 18:54:20 -03:00
|
|
|
return func->func_name;
|
|
|
|
}
|
1996-08-09 17:53:24 -03:00
|
|
|
if (strcmp(sname, "__doc__") == 0) {
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_INCREF(func->func_doc);
|
1996-05-14 18:54:20 -03:00
|
|
|
return func->func_doc;
|
|
|
|
}
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
if (PyEval_GetRestricted()) {
|
|
|
|
PyErr_SetString(PyExc_RuntimeError,
|
|
|
|
"instance-method attributes not accessible in restricted mode");
|
1995-01-10 06:39:49 -04:00
|
|
|
return NULL;
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyMember_Get((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)
|
1997-05-02 00:12:38 -03:00
|
|
|
register PyMethodObject *im;
|
1990-10-14 09:07:46 -03:00
|
|
|
{
|
1997-05-02 00:12:38 -03:00
|
|
|
Py_DECREF(im->im_func);
|
|
|
|
Py_XDECREF(im->im_self);
|
|
|
|
Py_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)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyMethodObject *a, *b;
|
1992-09-03 17:39:51 -03:00
|
|
|
{
|
1995-04-06 11:46:51 -03:00
|
|
|
if (a->im_self != b->im_self)
|
|
|
|
return (a->im_self < b->im_self) ? -1 : 1;
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyObject_Compare(a->im_func, b->im_func);
|
1992-09-03 17:39:51 -03:00
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
static PyObject *
|
1993-05-19 11:50:45 -03:00
|
|
|
instancemethod_repr(a)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyMethodObject *a;
|
1993-05-19 11:50:45 -03:00
|
|
|
{
|
|
|
|
char buf[240];
|
1997-05-02 00:12:38 -03:00
|
|
|
PyInstanceObject *self = (PyInstanceObject *)(a->im_self);
|
|
|
|
PyFunctionObject *func = (PyFunctionObject *)(a->im_func);
|
|
|
|
PyClassObject *class = (PyClassObject *)(a->im_class);
|
|
|
|
PyObject *fclassname, *iclassname, *funcname;
|
1993-05-20 11:24:46 -03:00
|
|
|
char *fcname, *icname, *fname;
|
|
|
|
fclassname = class->cl_name;
|
|
|
|
funcname = func->func_name;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (fclassname != NULL && PyString_Check(fclassname))
|
|
|
|
fcname = PyString_AsString(fclassname);
|
1993-05-19 11:50:45 -03:00
|
|
|
else
|
1993-05-20 11:24:46 -03:00
|
|
|
fcname = "?";
|
1997-05-02 00:12:38 -03:00
|
|
|
if (funcname != NULL && PyString_Check(funcname))
|
|
|
|
fname = PyString_AsString(funcname);
|
1993-05-19 11:50:45 -03:00
|
|
|
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;
|
1997-05-02 00:12:38 -03:00
|
|
|
if (iclassname != NULL && PyString_Check(iclassname))
|
|
|
|
icname = PyString_AsString(iclassname);
|
1993-05-20 11:24:46 -03:00
|
|
|
else
|
|
|
|
icname = "?";
|
|
|
|
sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
|
|
|
|
fcname, fname, icname, (long)self);
|
|
|
|
}
|
1997-05-02 00:12:38 -03:00
|
|
|
return PyString_FromString(buf);
|
1993-05-19 11:50:45 -03:00
|
|
|
}
|
|
|
|
|
1993-03-29 06:43:31 -04:00
|
|
|
static long
|
|
|
|
instancemethod_hash(a)
|
1997-05-02 00:12:38 -03:00
|
|
|
PyMethodObject *a;
|
1993-03-29 06:43:31 -04:00
|
|
|
{
|
|
|
|
long x, y;
|
1993-05-20 11:24:46 -03:00
|
|
|
if (a->im_self == NULL)
|
1997-05-02 00:12:38 -03:00
|
|
|
x = PyObject_Hash(Py_None);
|
1993-05-20 11:24:46 -03:00
|
|
|
else
|
1997-05-02 00:12:38 -03:00
|
|
|
x = PyObject_Hash(a->im_self);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (x == -1)
|
|
|
|
return -1;
|
1997-05-02 00:12:38 -03:00
|
|
|
y = PyObject_Hash(a->im_func);
|
1993-03-29 06:43:31 -04:00
|
|
|
if (y == -1)
|
|
|
|
return -1;
|
|
|
|
return x ^ y;
|
|
|
|
}
|
|
|
|
|
1997-05-02 00:12:38 -03:00
|
|
|
PyTypeObject PyMethod_Type = {
|
|
|
|
PyObject_HEAD_INIT(&PyType_Type)
|
1990-10-14 09:07:46 -03:00
|
|
|
0,
|
1991-04-16 05:38:43 -03:00
|
|
|
"instance method",
|
1997-05-02 00:12:38 -03:00
|
|
|
sizeof(PyMethodObject),
|
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
|
|
|
};
|