blob: 749a06a0eb238fa4ab8d2dc8281c40749c193853 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum6610ad91995-01-04 19:07:38 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossumf70e43a1991-02-19 12:39:46 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossumf70e43a1991-02-19 12:39:46 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossumf70e43a1991-02-19 12:39:46 +000029
30******************************************************************/
31
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000032/* Class object implementation */
33
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +000035#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +000036
Guido van Rossum52ca98a1994-09-05 07:32:29 +000037/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +000038static PyObject *class_lookup
39 Py_PROTO((PyClassObject *, PyObject *, PyClassObject **));
40static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *));
Guido van Rossum52ca98a1994-09-05 07:32:29 +000041
Guido van Rossumc0b618a1997-05-02 03:12:38 +000042PyObject *
43PyClass_New(bases, dict, name)
44 PyObject *bases; /* NULL or tuple of classobjects! */
45 PyObject *dict;
46 PyObject *name; /* String; NULL if unknown */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000047{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 PyClassObject *op, *dummy;
49 static PyObject *getattrstr, *setattrstr, *delattrstr;
50 static PyObject *docstr;
Guido van Rossum019f4241996-08-21 14:54:28 +000051 if (docstr == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +000052 docstr= PyString_InternFromString("__doc__");
Guido van Rossum019f4241996-08-21 14:54:28 +000053 if (docstr == NULL)
54 return NULL;
55 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000056 if (PyDict_GetItem(dict, docstr) == NULL) {
57 if (PyDict_SetItem(dict, docstr, Py_None) < 0)
Guido van Rossume7d444f1995-01-07 12:35:18 +000058 return NULL;
59 }
Guido van Rossume2966a61991-12-10 13:53:23 +000060 if (bases == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000061 bases = PyTuple_New(0);
Guido van Rossume2966a61991-12-10 13:53:23 +000062 if (bases == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +000063 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000064 }
65 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +000066 Py_INCREF(bases);
67 op = PyObject_NEW(PyClassObject, &PyClass_Type);
Guido van Rossume2966a61991-12-10 13:53:23 +000068 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000069 Py_DECREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000070 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000071 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 op->cl_bases = bases;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000073 Py_INCREF(dict);
Guido van Rossum81daa321993-05-20 14:24:46 +000074 op->cl_dict = dict;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 Py_XINCREF(name);
Guido van Rossum94308391991-10-20 20:11:48 +000076 op->cl_name = name;
Guido van Rossum2878a691996-08-09 20:53:24 +000077 if (getattrstr == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +000078 getattrstr = PyString_InternFromString("__getattr__");
79 setattrstr = PyString_InternFromString("__setattr__");
80 delattrstr = PyString_InternFromString("__delattr__");
Guido van Rossum2878a691996-08-09 20:53:24 +000081 }
82 op->cl_getattr = class_lookup(op, getattrstr, &dummy);
83 op->cl_setattr = class_lookup(op, setattrstr, &dummy);
84 op->cl_delattr = class_lookup(op, delattrstr, &dummy);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000085 Py_XINCREF(op->cl_getattr);
86 Py_XINCREF(op->cl_setattr);
87 Py_XINCREF(op->cl_delattr);
Guido van Rossumc0b618a1997-05-02 03:12:38 +000088 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089}
90
91/* Class methods */
92
93static void
94class_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 PyClassObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000097 Py_DECREF(op->cl_bases);
98 Py_DECREF(op->cl_dict);
99 Py_XDECREF(op->cl_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100 free((ANY *)op);
101}
102
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000103static PyObject *
Guido van Rossum81daa321993-05-20 14:24:46 +0000104class_lookup(cp, name, pclass)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000105 PyClassObject *cp;
106 PyObject *name;
107 PyClassObject **pclass;
Guido van Rossum81daa321993-05-20 14:24:46 +0000108{
109 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
Guido van Rossum81daa321993-05-20 14:24:46 +0000111 if (value != NULL) {
112 *pclass = cp;
113 return value;
114 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000116 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000117 PyObject *v = class_lookup(
118 (PyClassObject *)
119 PyTuple_GetItem(cp->cl_bases, i), name, pclass);
Guido van Rossum81daa321993-05-20 14:24:46 +0000120 if (v != NULL)
121 return v;
122 }
123 return NULL;
124}
125
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000126static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127class_getattr(op, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000128 register PyClassObject *op;
129 PyObject *name;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000130{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000131 register PyObject *v;
132 register char *sname = PyString_AsString(name);
133 PyClassObject *class;
Guido van Rossum2878a691996-08-09 20:53:24 +0000134 if (sname[0] == '_' && sname[1] == '_') {
135 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000136 if (PyEval_GetRestricted()) {
137 PyErr_SetString(PyExc_RuntimeError,
138 "class.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000139 return NULL;
140 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000141 Py_INCREF(op->cl_dict);
Guido van Rossum10393b11995-01-10 10:39:49 +0000142 return op->cl_dict;
143 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000144 if (strcmp(sname, "__bases__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000145 Py_INCREF(op->cl_bases);
Guido van Rossum10393b11995-01-10 10:39:49 +0000146 return op->cl_bases;
147 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000148 if (strcmp(sname, "__name__") == 0) {
Guido van Rossum10393b11995-01-10 10:39:49 +0000149 if (op->cl_name == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 v = Py_None;
Guido van Rossum10393b11995-01-10 10:39:49 +0000151 else
152 v = op->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000153 Py_INCREF(v);
Guido van Rossum10393b11995-01-10 10:39:49 +0000154 return v;
155 }
Guido van Rossum94308391991-10-20 20:11:48 +0000156 }
Guido van Rossum81daa321993-05-20 14:24:46 +0000157 v = class_lookup(op, name, &class);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000158 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000159 PyErr_SetObject(PyExc_AttributeError, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000160 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000161 }
Guido van Rossum0add15f1997-05-09 01:07:15 +0000162 Py_INCREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 if (PyFunction_Check(v)) {
164 PyObject *w = PyMethod_New(v, (PyObject *)NULL,
165 (PyObject *)class);
166 Py_DECREF(v);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000167 v = w;
168 }
169 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000170}
171
Guido van Rossum94308391991-10-20 20:11:48 +0000172static int
173class_setattr(op, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 PyClassObject *op;
175 PyObject *name;
176 PyObject *v;
Guido van Rossum94308391991-10-20 20:11:48 +0000177{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000179 if (sname[0] == '_' && sname[1] == '_') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000180 int n = PyString_Size(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000181 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 PyErr_SetString(PyExc_TypeError,
183 "read-only special attribute");
Guido van Rossum8dd79cf1992-04-05 14:24:32 +0000184 return -1;
185 }
186 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 if (PyEval_GetRestricted()) {
188 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumac21f6a1995-08-04 04:05:31 +0000189 "classes are read-only in restricted mode");
190 return -1;
191 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000192 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000193 int rv = PyDict_DelItem(op->cl_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000194 if (rv < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000195 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000196 "delete non-existing class attribute");
197 return rv;
198 }
Guido van Rossum94308391991-10-20 20:11:48 +0000199 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200 return PyDict_SetItem(op->cl_dict, name, v);
Guido van Rossum94308391991-10-20 20:11:48 +0000201}
202
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000203static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +0000204class_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyClassObject *op;
Guido van Rossum25831651993-05-19 14:50:45 +0000206{
207 char buf[140];
208 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 if (op->cl_name == NULL || !PyString_Check(op->cl_name))
Guido van Rossum25831651993-05-19 14:50:45 +0000210 name = "?";
211 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 name = PyString_AsString(op->cl_name);
Guido van Rossum25831651993-05-19 14:50:45 +0000213 sprintf(buf, "<class %.100s at %lx>", name, (long)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 return PyString_FromString(buf);
Guido van Rossum25831651993-05-19 14:50:45 +0000215}
216
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000217PyTypeObject PyClass_Type = {
218 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000219 0,
220 "class",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221 sizeof(PyClassObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000222 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000223 (destructor)class_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +0000225 0, /*tp_getattr*/
226 0, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000227 0, /*tp_compare*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000228 (reprfunc)class_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000229 0, /*tp_as_number*/
230 0, /*tp_as_sequence*/
231 0, /*tp_as_mapping*/
Guido van Rossum2878a691996-08-09 20:53:24 +0000232 0, /*tp_hash*/
233 0, /*tp_call*/
234 0, /*tp_str*/
235 (getattrofunc)class_getattr, /*tp_getattro*/
236 (setattrofunc)class_setattr, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000237};
238
Guido van Rossum81daa321993-05-20 14:24:46 +0000239int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240PyClass_IsSubclass(class, base)
241 PyObject *class;
242 PyObject *base;
Guido van Rossum81daa321993-05-20 14:24:46 +0000243{
244 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245 PyClassObject *cp;
Guido van Rossum81daa321993-05-20 14:24:46 +0000246 if (class == base)
247 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 if (class == NULL || !PyClass_Check(class))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000249 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 cp = (PyClassObject *)class;
251 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000252 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000253 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
Guido van Rossum81daa321993-05-20 14:24:46 +0000254 return 1;
255 }
256 return 0;
257}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258
Guido van Rossum81daa321993-05-20 14:24:46 +0000259
260/* Instance objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000261
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262PyObject *
263PyInstance_New(class, arg, kw)
264 PyObject *class;
265 PyObject *arg;
266 PyObject *kw;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000267{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000268 register PyInstanceObject *inst;
269 PyObject *init;
270 static PyObject *initstr;
271 if (!PyClass_Check(class)) {
272 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000273 return NULL;
274 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000275 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
Guido van Rossume8122f11991-05-05 20:03:07 +0000276 if (inst == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000277 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000278 Py_INCREF(class);
279 inst->in_class = (PyClassObject *)class;
280 inst->in_dict = PyDict_New();
Guido van Rossum0add15f1997-05-09 01:07:15 +0000281 if (inst->in_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000282 Py_DECREF(inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000283 return NULL;
284 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000285 if (initstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000286 initstr = PyString_InternFromString("__init__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000287 init = instance_getattr1(inst, initstr);
Guido van Rossum25831651993-05-19 14:50:45 +0000288 if (init == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000289 PyErr_Clear();
290 if ((arg != NULL && (!PyTuple_Check(arg) ||
291 PyTuple_Size(arg) != 0))
292 || (kw != NULL && (!PyDict_Check(kw) ||
293 PyDict_Size(kw) != 0))) {
294 PyErr_SetString(PyExc_TypeError,
Guido van Rossuma83f2701995-07-26 18:07:32 +0000295 "this constructor takes no arguments");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000296 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000297 inst = NULL;
298 }
299 }
300 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000301 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
302 Py_DECREF(init);
Guido van Rossum25831651993-05-19 14:50:45 +0000303 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000304 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000305 inst = NULL;
306 }
307 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308 if (res != Py_None) {
309 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +0000310 "__init__() should return None");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000311 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000312 inst = NULL;
313 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000314 Py_DECREF(res);
Guido van Rossum25831651993-05-19 14:50:45 +0000315 }
316 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000317 return (PyObject *)inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318}
319
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000320/* Instance methods */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000321
322static void
Guido van Rossume8122f11991-05-05 20:03:07 +0000323instance_dealloc(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000324 register PyInstanceObject *inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000325{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000326 PyObject *error_type, *error_value, *error_traceback;
327 PyObject *del;
328 static PyObject *delstr;
Guido van Rossum25831651993-05-19 14:50:45 +0000329 /* Call the __del__ method if it exists. First temporarily
330 revive the object and save the current exception, if any. */
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000331#ifdef Py_TRACE_REFS
332 /* much too complicated if Py_TRACE_REFS defined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000333 extern long _Py_RefTotal;
334 inst->ob_type = &PyInstance_Type;
335 _Py_NewReference(inst);
336 _Py_RefTotal--; /* compensate for increment in NEWREF */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000337#ifdef COUNT_ALLOCS
338 inst->ob_type->tp_alloc--; /* ditto */
339#endif
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000340#else /* !Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 Py_INCREF(inst);
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000342#endif /* !Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000343 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum2878a691996-08-09 20:53:24 +0000344 if (delstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000345 delstr = PyString_InternFromString("__del__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000346 if ((del = instance_getattr1(inst, delstr)) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000347 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000348 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 PyObject *f, *t, *v, *tb;
350 PyErr_Fetch(&t, &v, &tb);
351 f = PySys_GetObject("stderr");
Guido van Rossum22a85e51996-09-11 22:51:57 +0000352 if (f != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353 PyFile_WriteString("Exception ", f);
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000354 if (t) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000355 PyFile_WriteObject(t, f, Py_PRINT_RAW);
356 if (v && v != Py_None) {
357 PyFile_WriteString(": ", f);
358 PyFile_WriteObject(v, f, 0);
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000359 }
360 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000361 PyFile_WriteString(" in ", f);
362 PyFile_WriteObject(del, f, 0);
363 PyFile_WriteString(" ignored\n", f);
364 PyErr_Clear(); /* Just in case */
Guido van Rossum22a85e51996-09-11 22:51:57 +0000365 }
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000366 Py_XDECREF(t);
367 Py_XDECREF(v);
368 Py_XDECREF(tb);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000369 }
370 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000371 Py_DECREF(res);
372 Py_DECREF(del);
Guido van Rossum25831651993-05-19 14:50:45 +0000373 }
374 /* Restore the saved exception and undo the temporary revival */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum25831651993-05-19 14:50:45 +0000376 /* Can't use DECREF here, it would cause a recursive call */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000377 if (--inst->ob_refcnt > 0) {
378#ifdef COUNT_ALLOCS
379 inst->ob_type->tp_free--;
380#endif
Guido van Rossum25831651993-05-19 14:50:45 +0000381 return; /* __del__ added a reference; don't delete now */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000382 }
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000383#ifdef Py_TRACE_REFS
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000384#ifdef COUNT_ALLOCS
385 inst->ob_type->tp_free--; /* compensate for increment in UNREF */
386#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387 _Py_ForgetReference(inst);
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000388 inst->ob_type = NULL;
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000389#endif /* Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000390 Py_DECREF(inst->in_class);
391 Py_XDECREF(inst->in_dict);
Guido van Rossume8122f11991-05-05 20:03:07 +0000392 free((ANY *)inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000393}
394
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395static PyObject *
Guido van Rossume7737541994-09-05 07:31:41 +0000396instance_getattr1(inst, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000397 register PyInstanceObject *inst;
398 PyObject *name;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000399{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000400 register PyObject *v;
401 register char *sname = PyString_AsString(name);
402 PyClassObject *class;
Guido van Rossum2878a691996-08-09 20:53:24 +0000403 if (sname[0] == '_' && sname[1] == '_') {
404 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 if (PyEval_GetRestricted()) {
406 PyErr_SetString(PyExc_RuntimeError,
407 "instance.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000408 return NULL;
409 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000410 Py_INCREF(inst->in_dict);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000411 return inst->in_dict;
412 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000413 if (strcmp(sname, "__class__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000414 Py_INCREF(inst->in_class);
415 return (PyObject *)inst->in_class;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000416 }
Guido van Rossum94308391991-10-20 20:11:48 +0000417 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000418 class = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000419 v = PyDict_GetItem(inst->in_dict, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000420 if (v == NULL) {
421 v = class_lookup(inst->in_class, name, &class);
422 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000423 PyErr_SetObject(PyExc_AttributeError, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000424 return NULL;
425 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000426 }
Guido van Rossum0add15f1997-05-09 01:07:15 +0000427 Py_INCREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000428 if (class != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000429 if (PyFunction_Check(v)) {
430 PyObject *w = PyMethod_New(v, (PyObject *)inst,
431 (PyObject *)class);
432 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000433 v = w;
434 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000435 else if (PyMethod_Check(v)) {
436 PyObject *im_class = PyMethod_Class(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000437 /* Only if classes are compatible */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 if (PyClass_IsSubclass((PyObject *)class, im_class)) {
439 PyObject *im_func = PyMethod_Function(v);
440 PyObject *w = PyMethod_New(im_func,
441 (PyObject *)inst, im_class);
442 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000443 v = w;
444 }
445 }
Guido van Rossumb3f72581993-05-21 19:56:10 +0000446 }
447 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000448}
449
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000450static PyObject *
Guido van Rossume7737541994-09-05 07:31:41 +0000451instance_getattr(inst, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 register PyInstanceObject *inst;
453 PyObject *name;
Guido van Rossume7737541994-09-05 07:31:41 +0000454{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000455 register PyObject *func, *res;
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000456 res = instance_getattr1(inst, name);
457 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000458 PyObject *args;
459 PyErr_Clear();
460 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000461 if (args == NULL)
Guido van Rossume7737541994-09-05 07:31:41 +0000462 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000463 res = PyEval_CallObject(func, args);
464 Py_DECREF(args);
Guido van Rossume7737541994-09-05 07:31:41 +0000465 }
466 return res;
467}
468
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000469static int
Guido van Rossume7737541994-09-05 07:31:41 +0000470instance_setattr1(inst, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 PyInstanceObject *inst;
472 PyObject *name;
473 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000474{
Guido van Rossum94472a01992-09-04 09:45:18 +0000475 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 int rv = PyDict_DelItem(inst->in_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000477 if (rv < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000479 "delete non-existing instance attribute");
480 return rv;
481 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000482 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000483 return PyDict_SetItem(inst->in_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484}
485
Guido van Rossume7737541994-09-05 07:31:41 +0000486static int
487instance_setattr(inst, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 PyInstanceObject *inst;
489 PyObject *name;
490 PyObject *v;
Guido van Rossume7737541994-09-05 07:31:41 +0000491{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000492 PyObject *func, *args, *res;
493 char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000494 if (sname[0] == '_' && sname[1] == '_'
495 && (strcmp(sname, "__dict__") == 0 ||
496 strcmp(sname, "__class__") == 0)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000497 int n = PyString_Size(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000498 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000499 PyErr_SetString(PyExc_TypeError,
500 "read-only special attribute");
Guido van Rossume7737541994-09-05 07:31:41 +0000501 return -1;
502 }
Guido van Rossume7737541994-09-05 07:31:41 +0000503 }
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000504 if (v == NULL)
505 func = inst->in_class->cl_delattr;
506 else
507 func = inst->in_class->cl_setattr;
508 if (func == NULL)
509 return instance_setattr1(inst, name, v);
510 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000511 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000512 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000513 args = Py_BuildValue("(OOO)", inst, name, v);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000514 if (args == NULL)
515 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000516 res = PyEval_CallObject(func, args);
517 Py_DECREF(args);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000518 if (res == NULL)
519 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 Py_DECREF(res);
Guido van Rossume7737541994-09-05 07:31:41 +0000521 return 0;
522}
523
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000524static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000525instance_repr(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000527{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000528 PyObject *func;
529 PyObject *res;
530 static PyObject *reprstr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000531
Guido van Rossum2878a691996-08-09 20:53:24 +0000532 if (reprstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000533 reprstr = PyString_InternFromString("__repr__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000534 func = instance_getattr(inst, reprstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000535 if (func == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +0000536 char buf[140];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000537 PyObject *classname = inst->in_class->cl_name;
Guido van Rossum25831651993-05-19 14:50:45 +0000538 char *cname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 if (classname != NULL && PyString_Check(classname))
540 cname = PyString_AsString(classname);
Guido van Rossum25831651993-05-19 14:50:45 +0000541 else
542 cname = "?";
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 PyErr_Clear();
Guido van Rossum25831651993-05-19 14:50:45 +0000544 sprintf(buf, "<%.100s instance at %lx>", cname, (long)inst);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000545 return PyString_FromString(buf);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000546 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 res = PyEval_CallObject(func, (PyObject *)NULL);
548 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000549 return res;
550}
551
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000552static PyObject *
Guido van Rossume7d444f1995-01-07 12:35:18 +0000553instance_compare1(inst, other)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 PyObject *inst, *other;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000555{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000556 return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__",
Guido van Rossume7d444f1995-01-07 12:35:18 +0000557 instance_compare1);
558}
559
Guido van Rossum9bfef441993-03-29 10:43:31 +0000560static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000561instance_compare(inst, other)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 PyObject *inst, *other;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000563{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000564 PyObject *result;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000565 long outcome;
566 result = instance_compare1(inst, other);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000567 if (result == NULL)
568 return -1;
569 if (!PyInt_Check(result)) {
570 Py_DECREF(result);
571 PyErr_SetString(PyExc_TypeError,
572 "comparison did not return an int");
573 return -1;
Sjoerd Mullenderb9a6d121994-10-19 15:11:52 +0000574 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 outcome = PyInt_AsLong(result);
576 Py_DECREF(result);
Guido van Rossum03093a21994-09-28 15:51:32 +0000577 if (outcome < 0)
578 return -1;
579 else if (outcome > 0)
580 return 1;
581 return 0;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000582}
583
Guido van Rossum9bfef441993-03-29 10:43:31 +0000584static long
585instance_hash(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000586 PyInstanceObject *inst;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000587{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000588 PyObject *func;
589 PyObject *res;
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000590 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000591 static PyObject *hashstr, *cmpstr;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000592
Guido van Rossum2878a691996-08-09 20:53:24 +0000593 if (hashstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000594 hashstr = PyString_InternFromString("__hash__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000595 func = instance_getattr(inst, hashstr);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000596 if (func == NULL) {
597 /* If there is no __cmp__ method, we hash on the address.
598 If a __cmp__ method exists, there must be a __hash__. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000599 PyErr_Clear();
Guido van Rossum2878a691996-08-09 20:53:24 +0000600 if (cmpstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000601 cmpstr = PyString_InternFromString("__cmp__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000602 func = instance_getattr(inst, cmpstr);
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000603 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000604 PyErr_Clear();
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000605 outcome = (long)inst;
606 if (outcome == -1)
607 outcome = -2;
608 return outcome;
609 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 PyErr_SetString(PyExc_TypeError, "unhashable instance");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000611 return -1;
612 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000613 res = PyEval_CallObject(func, (PyObject *)NULL);
614 Py_DECREF(func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000615 if (res == NULL)
616 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000617 if (PyInt_Check(res)) {
618 outcome = PyInt_AsLong(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000619 if (outcome == -1)
620 outcome = -2;
621 }
622 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000623 PyErr_SetString(PyExc_TypeError,
624 "__hash__() should return an int");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000625 outcome = -1;
626 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000627 Py_DECREF(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000628 return outcome;
629}
630
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
Guido van Rossum2878a691996-08-09 20:53:24 +0000632
Guido van Rossum9bfef441993-03-29 10:43:31 +0000633static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000634instance_length(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000635 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000636{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 PyObject *func;
638 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000639 int outcome;
640
Guido van Rossum2878a691996-08-09 20:53:24 +0000641 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000642 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000643 func = instance_getattr(inst, lenstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000644 if (func == NULL)
645 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 res = PyEval_CallObject(func, (PyObject *)NULL);
647 Py_DECREF(func);
Guido van Rossumd014ea61992-11-26 10:30:26 +0000648 if (res == NULL)
649 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 if (PyInt_Check(res)) {
651 outcome = PyInt_AsLong(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000652 if (outcome < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000653 PyErr_SetString(PyExc_ValueError,
654 "__len__() should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000655 }
656 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 PyErr_SetString(PyExc_TypeError,
658 "__len__() should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000659 outcome = -1;
660 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000662 return outcome;
663}
664
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000666instance_subscript(inst, key)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000667 PyInstanceObject *inst;
668 PyObject *key;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000669{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000670 PyObject *func;
671 PyObject *arg;
672 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000673
Guido van Rossum2878a691996-08-09 20:53:24 +0000674 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000675 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000676 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000677 if (func == NULL)
678 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000679 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000680 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000681 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000682 return NULL;
683 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 res = PyEval_CallObject(func, arg);
685 Py_DECREF(func);
686 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000687 return res;
688}
689
Guido van Rossum9bfef441993-03-29 10:43:31 +0000690static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000691instance_ass_subscript(inst, key, value)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 PyInstanceObject*inst;
693 PyObject *key;
694 PyObject *value;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000695{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000696 PyObject *func;
697 PyObject *arg;
698 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000699
Guido van Rossum2878a691996-08-09 20:53:24 +0000700 if (value == NULL) {
701 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000702 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000703 func = instance_getattr(inst, delitemstr);
704 }
705 else {
706 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000707 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000708 func = instance_getattr(inst, setitemstr);
709 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000710 if (func == NULL)
711 return -1;
712 if (value == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000713 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000714 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000715 arg = Py_BuildValue("(OO)", key, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000716 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000717 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000718 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000719 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000720 res = PyEval_CallObject(func, arg);
721 Py_DECREF(func);
722 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000723 if (res == NULL)
724 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000725 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000726 return 0;
727}
728
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000729static PyMappingMethods instance_as_mapping = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000730 (inquiry)instance_length, /*mp_length*/
731 (binaryfunc)instance_subscript, /*mp_subscript*/
732 (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/
Guido van Rossum04691fc1992-08-12 15:35:34 +0000733};
734
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000735static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000736instance_item(inst, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000737 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000738 int i;
739{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000740 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000741
Guido van Rossum2878a691996-08-09 20:53:24 +0000742 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000743 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000744 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000745 if (func == NULL)
746 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000747 arg = Py_BuildValue("(i)", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000748 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000749 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000750 return NULL;
751 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000752 res = PyEval_CallObject(func, arg);
753 Py_DECREF(func);
754 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000755 return res;
756}
757
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000758static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000759instance_slice(inst, i, j)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000760 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000761 int i, j;
762{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 PyObject *func, *arg, *res;
764 static PyObject *getslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000765
Guido van Rossum2878a691996-08-09 20:53:24 +0000766 if (getslicestr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000767 getslicestr = PyString_InternFromString("__getslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000768 func = instance_getattr(inst, getslicestr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000769 if (func == NULL)
770 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000771 arg = Py_BuildValue("(ii)", i, j);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000772 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000773 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000774 return NULL;
775 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000776 res = PyEval_CallObject(func, arg);
777 Py_DECREF(func);
778 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000779 return res;
780}
781
782static int
783instance_ass_item(inst, i, item)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000784 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000785 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 PyObject *item;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000787{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000788 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000789
Guido van Rossum2878a691996-08-09 20:53:24 +0000790 if (item == NULL) {
791 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000792 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000793 func = instance_getattr(inst, delitemstr);
794 }
795 else {
796 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000797 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000798 func = instance_getattr(inst, setitemstr);
799 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000800 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +0000801 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000802 if (item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 arg = Py_BuildValue("i", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000804 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 arg = Py_BuildValue("(iO)", i, item);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000806 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000808 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000809 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000810 res = PyEval_CallObject(func, arg);
811 Py_DECREF(func);
812 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000813 if (res == NULL)
814 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000815 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000816 return 0;
817}
818
819static int
820instance_ass_slice(inst, i, j, value)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000821 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000822 int i, j;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000823 PyObject *value;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000824{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000825 PyObject *func, *arg, *res;
826 static PyObject *setslicestr, *delslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000827
Guido van Rossum2878a691996-08-09 20:53:24 +0000828 if (value == NULL) {
829 if (delslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000830 delslicestr =
831 PyString_InternFromString("__delslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000832 func = instance_getattr(inst, delslicestr);
833 }
834 else {
835 if (setslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000836 setslicestr =
837 PyString_InternFromString("__setslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000838 func = instance_getattr(inst, setslicestr);
839 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000840 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +0000841 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000842 if (value == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 arg = Py_BuildValue("(ii)", i, j);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000844 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 arg = Py_BuildValue("(iiO)", i, j, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000846 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000847 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000848 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000849 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000850 res = PyEval_CallObject(func, arg);
851 Py_DECREF(func);
852 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000853 if (res == NULL)
854 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000856 return 0;
857}
858
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859static PySequenceMethods instance_as_sequence = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000860 (inquiry)instance_length, /*sq_length*/
Guido van Rossum03093a21994-09-28 15:51:32 +0000861 0, /*sq_concat*/
862 0, /*sq_repeat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000863 (intargfunc)instance_item, /*sq_item*/
864 (intintargfunc)instance_slice, /*sq_slice*/
865 (intobjargproc)instance_ass_item, /*sq_ass_item*/
866 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
Guido van Rossum04691fc1992-08-12 15:35:34 +0000867};
868
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000869static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000870generic_unary_op(self, methodname)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000871 PyInstanceObject *self;
872 PyObject *methodname;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000873{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000874 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000875
876 if ((func = instance_getattr(self, methodname)) == NULL)
877 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 res = PyEval_CallObject(func, (PyObject *)NULL);
879 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000880 return res;
881}
882
Guido van Rossum03093a21994-09-28 15:51:32 +0000883
884/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000885static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **,
886 PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int ));
Guido van Rossum03093a21994-09-28 15:51:32 +0000887
888
889/* Implement a binary operator involving at least one class instance. */
890
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891PyObject *
892PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
893 PyObject *v;
894 PyObject *w;
Guido van Rossum03093a21994-09-28 15:51:32 +0000895 char *opname;
896 char *ropname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000897 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossum03093a21994-09-28 15:51:32 +0000898{
899 char buf[256];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000900 PyObject *result = NULL;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000901 if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000902 return result;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000903 if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0)
Guido van Rossum03093a21994-09-28 15:51:32 +0000904 return result;
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000905 /* Sigh -- special case for comnparisons */
906 if (strcmp(opname, "__cmp__") == 0) {
907 long c = (v < w) ? -1 : (v > w) ? 1 : 0;
908 return PyInt_FromLong(c);
909 }
Guido van Rossum03093a21994-09-28 15:51:32 +0000910 sprintf(buf, "%s nor %s defined for these operands", opname, ropname);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000911 PyErr_SetString(PyExc_TypeError, buf);
Guido van Rossum03093a21994-09-28 15:51:32 +0000912 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000913}
914
Guido van Rossum03093a21994-09-28 15:51:32 +0000915
916/* Try one half of a binary operator involving a class instance.
917 Return value:
918 -1 if an exception is to be reported right away
919 0 if we have a valid result
920 1 if we could try another operation
921*/
922
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000923static PyObject *coerce_obj;
Guido van Rossum2878a691996-08-09 20:53:24 +0000924
Guido van Rossum03093a21994-09-28 15:51:32 +0000925static int
Guido van Rossume7d444f1995-01-07 12:35:18 +0000926halfbinop(v, w, opname, r_result, thisfunc, swapped)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000927 PyObject *v;
928 PyObject *w;
Guido van Rossum03093a21994-09-28 15:51:32 +0000929 char *opname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000930 PyObject **r_result;
931 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossume7d444f1995-01-07 12:35:18 +0000932 int swapped;
Guido van Rossum03093a21994-09-28 15:51:32 +0000933{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000934 PyObject *func;
935 PyObject *args;
936 PyObject *PyNumber_Coerce;
937 PyObject *coerced = NULL;
938 PyObject *v1;
Guido van Rossum03093a21994-09-28 15:51:32 +0000939
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000940 if (!PyInstance_Check(v))
Guido van Rossum03093a21994-09-28 15:51:32 +0000941 return 1;
Guido van Rossum2878a691996-08-09 20:53:24 +0000942 if (coerce_obj == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +0000943 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000944 if (coerce_obj == NULL)
945 return -1;
946 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947 PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj);
948 if (PyNumber_Coerce == NULL) {
949 PyErr_Clear();
Guido van Rossum03093a21994-09-28 15:51:32 +0000950 }
951 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000952 args = Py_BuildValue("(O)", w);
Guido van Rossum03093a21994-09-28 15:51:32 +0000953 if (args == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000954 return -1;
955 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000956 coerced = PyEval_CallObject(PyNumber_Coerce, args);
957 Py_DECREF(args);
958 Py_DECREF(PyNumber_Coerce);
Guido van Rossum03093a21994-09-28 15:51:32 +0000959 if (coerced == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +0000960 return -1;
961 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000962 if (coerced == Py_None) {
963 Py_DECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +0000964 return 1;
965 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000966 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
967 Py_DECREF(coerced);
968 PyErr_SetString(PyExc_TypeError,
Guido van Rossume7d444f1995-01-07 12:35:18 +0000969 "coercion should return None or 2-tuple");
Guido van Rossum03093a21994-09-28 15:51:32 +0000970 return -1;
971 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000972 v1 = PyTuple_GetItem(coerced, 0);
973 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum03093a21994-09-28 15:51:32 +0000974 if (v1 != v) {
975 v = v1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000976 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
Guido van Rossume7d444f1995-01-07 12:35:18 +0000977 if (swapped)
978 *r_result = (*thisfunc)(w, v);
979 else
980 *r_result = (*thisfunc)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000981 Py_DECREF(coerced);
Guido van Rossume7d444f1995-01-07 12:35:18 +0000982 return *r_result == NULL ? -1 : 0;
Guido van Rossum03093a21994-09-28 15:51:32 +0000983 }
984 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000985 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum03093a21994-09-28 15:51:32 +0000986 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987 func = PyObject_GetAttrString(v, opname);
Guido van Rossume7d444f1995-01-07 12:35:18 +0000988 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000989 Py_XDECREF(coerced);
990 if (PyErr_Occurred() != PyExc_AttributeError)
Guido van Rossume7d444f1995-01-07 12:35:18 +0000991 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000992 PyErr_Clear();
Guido van Rossume7d444f1995-01-07 12:35:18 +0000993 return 1;
994 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000995 args = Py_BuildValue("(O)", w);
Guido van Rossum03093a21994-09-28 15:51:32 +0000996 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 Py_DECREF(func);
998 Py_XDECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +0000999 return -1;
1000 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001001 *r_result = PyEval_CallObject(func, args);
1002 Py_DECREF(args);
1003 Py_DECREF(func);
1004 Py_XDECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +00001005 return *r_result == NULL ? -1 : 0;
1006}
1007
Guido van Rossum879c5811995-01-10 15:24:06 +00001008static int
1009instance_coerce(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001010 PyObject **pv;
1011 PyObject **pw;
Guido van Rossum879c5811995-01-10 15:24:06 +00001012{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013 PyObject *v = *pv;
1014 PyObject *w = *pw;
1015 PyObject *PyNumber_Coerce;
1016 PyObject *args;
1017 PyObject *coerced;
Guido van Rossum879c5811995-01-10 15:24:06 +00001018
Guido van Rossum2878a691996-08-09 20:53:24 +00001019 if (coerce_obj == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +00001020 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001021 if (coerce_obj == NULL)
1022 return -1;
1023 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024 PyNumber_Coerce = PyObject_GetAttr(v, coerce_obj);
1025 if (PyNumber_Coerce == NULL) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001026 /* No __coerce__ method: always OK */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 PyErr_Clear();
1028 Py_INCREF(v);
1029 Py_INCREF(w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001030 return 0;
1031 }
1032 /* Has __coerce__ method: call it */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033 args = Py_BuildValue("(O)", w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001034 if (args == NULL) {
1035 return -1;
1036 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001037 coerced = PyEval_CallObject(PyNumber_Coerce, args);
1038 Py_DECREF(args);
1039 Py_DECREF(PyNumber_Coerce);
Guido van Rossum879c5811995-01-10 15:24:06 +00001040 if (coerced == NULL) {
1041 /* __coerce__ call raised an exception */
1042 return -1;
1043 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 if (coerced == Py_None) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001045 /* __coerce__ says "I can't do it" */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001046 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001047 return 1;
1048 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001049 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001050 /* __coerce__ return value is malformed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051 Py_DECREF(coerced);
1052 PyErr_SetString(PyExc_TypeError,
Guido van Rossum879c5811995-01-10 15:24:06 +00001053 "coercion should return None or 2-tuple");
1054 return -1;
1055 }
1056 /* __coerce__ returned two new values */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001057 *pv = PyTuple_GetItem(coerced, 0);
1058 *pw = PyTuple_GetItem(coerced, 1);
1059 Py_INCREF(*pv);
1060 Py_INCREF(*pw);
1061 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001062 return 0;
1063}
1064
Guido van Rossum03093a21994-09-28 15:51:32 +00001065
Guido van Rossum04691fc1992-08-12 15:35:34 +00001066#define UNARY(funcname, methodname) \
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001067static PyObject *funcname(self) PyInstanceObject *self; { \
1068 static PyObject *o; \
Guido van Rossuma412d241997-01-18 07:59:12 +00001069 if (o == NULL) o = PyString_InternFromString(methodname); \
Guido van Rossum2878a691996-08-09 20:53:24 +00001070 return generic_unary_op(self, o); \
Guido van Rossum04691fc1992-08-12 15:35:34 +00001071}
1072
Guido van Rossum04691fc1992-08-12 15:35:34 +00001073UNARY(instance_neg, "__neg__")
1074UNARY(instance_pos, "__pos__")
1075UNARY(instance_abs, "__abs__")
1076
Guido van Rossum9bfef441993-03-29 10:43:31 +00001077static int
Guido van Rossum04691fc1992-08-12 15:35:34 +00001078instance_nonzero(self)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001079 PyInstanceObject *self;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001080{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001081 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001082 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001083 static PyObject *nonzerostr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001084
Guido van Rossum2878a691996-08-09 20:53:24 +00001085 if (nonzerostr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001086 nonzerostr = PyString_InternFromString("__nonzero__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001087 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001088 PyErr_Clear();
Guido van Rossum2878a691996-08-09 20:53:24 +00001089 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001090 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001091 if ((func = instance_getattr(self, lenstr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001092 PyErr_Clear();
Guido van Rossum04691fc1992-08-12 15:35:34 +00001093 /* Fall back to the default behavior:
1094 all instances are nonzero */
1095 return 1;
1096 }
1097 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001098 res = PyEval_CallObject(func, (PyObject *)NULL);
1099 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001100 if (res == NULL)
1101 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001102 if (!PyInt_Check(res)) {
1103 Py_DECREF(res);
1104 PyErr_SetString(PyExc_TypeError,
1105 "__nonzero__ should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001106 return -1;
1107 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001108 outcome = PyInt_AsLong(res);
1109 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001110 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111 PyErr_SetString(PyExc_ValueError,
1112 "__nonzero__ should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001113 return -1;
1114 }
1115 return outcome > 0;
1116}
1117
1118UNARY(instance_invert, "__invert__")
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001119UNARY(instance_int, "__int__")
1120UNARY(instance_long, "__long__")
1121UNARY(instance_float, "__float__")
1122UNARY(instance_oct, "__oct__")
1123UNARY(instance_hex, "__hex__")
1124
Guido van Rossum03093a21994-09-28 15:51:32 +00001125/* This version is for ternary calls only (z != None) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001126static PyObject *
Guido van Rossum03093a21994-09-28 15:51:32 +00001127instance_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001128 PyObject *v;
1129 PyObject *w;
1130 PyObject *z;
Guido van Rossum03093a21994-09-28 15:51:32 +00001131{
1132 /* XXX Doesn't do coercions... */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001133 PyObject *func;
1134 PyObject *args;
1135 PyObject *result;
1136 static PyObject *powstr;
Guido van Rossum2878a691996-08-09 20:53:24 +00001137
1138 if (powstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001139 powstr = PyString_InternFromString("__pow__");
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001140 func = PyObject_GetAttr(v, powstr);
Guido van Rossum03093a21994-09-28 15:51:32 +00001141 if (func == NULL)
1142 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001143 args = Py_BuildValue("(OO)", w, z);
Guido van Rossum03093a21994-09-28 15:51:32 +00001144 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001145 Py_DECREF(func);
Guido van Rossum03093a21994-09-28 15:51:32 +00001146 return NULL;
1147 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001148 result = PyEval_CallObject(func, args);
1149 Py_DECREF(func);
1150 Py_DECREF(args);
Guido van Rossum03093a21994-09-28 15:51:32 +00001151 return result;
1152}
1153
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001154static PyNumberMethods instance_as_number = {
Guido van Rossum03093a21994-09-28 15:51:32 +00001155 0, /*nb_add*/
1156 0, /*nb_subtract*/
1157 0, /*nb_multiply*/
1158 0, /*nb_divide*/
1159 0, /*nb_remainder*/
1160 0, /*nb_divmod*/
Guido van Rossum52ca98a1994-09-05 07:32:29 +00001161 (ternaryfunc)instance_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001162 (unaryfunc)instance_neg, /*nb_negative*/
1163 (unaryfunc)instance_pos, /*nb_positive*/
1164 (unaryfunc)instance_abs, /*nb_absolute*/
1165 (inquiry)instance_nonzero, /*nb_nonzero*/
1166 (unaryfunc)instance_invert, /*nb_invert*/
Guido van Rossum03093a21994-09-28 15:51:32 +00001167 0, /*nb_lshift*/
1168 0, /*nb_rshift*/
1169 0, /*nb_and*/
1170 0, /*nb_xor*/
1171 0, /*nb_or*/
Guido van Rossum879c5811995-01-10 15:24:06 +00001172 (coercion)instance_coerce, /*nb_coerce*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001173 (unaryfunc)instance_int, /*nb_int*/
1174 (unaryfunc)instance_long, /*nb_long*/
1175 (unaryfunc)instance_float, /*nb_float*/
1176 (unaryfunc)instance_oct, /*nb_oct*/
1177 (unaryfunc)instance_hex, /*nb_hex*/
Guido van Rossum04691fc1992-08-12 15:35:34 +00001178};
1179
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001180PyTypeObject PyInstance_Type = {
1181 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001182 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +00001183 "instance",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 sizeof(PyInstanceObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001185 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +00001186 (destructor)instance_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001187 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001188 0, /*tp_getattr*/
1189 0, /*tp_setattr*/
1190 instance_compare, /*tp_compare*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001191 (reprfunc)instance_repr, /*tp_repr*/
Guido van Rossum04691fc1992-08-12 15:35:34 +00001192 &instance_as_number, /*tp_as_number*/
1193 &instance_as_sequence, /*tp_as_sequence*/
1194 &instance_as_mapping, /*tp_as_mapping*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001195 (hashfunc)instance_hash, /*tp_hash*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001196 0, /*tp_call*/
1197 0, /*tp_str*/
1198 (getattrofunc)instance_getattr, /*tp_getattro*/
1199 (setattrofunc)instance_setattr, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001200};
1201
1202
Guido van Rossum81daa321993-05-20 14:24:46 +00001203/* Instance method objects are used for two purposes:
1204 (a) as bound instance methods (returned by instancename.methodname)
1205 (b) as unbound methods (returned by ClassName.methodname)
1206 In case (b), im_self is NULL
1207*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001208
1209typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001210 PyObject_HEAD
1211 PyObject *im_func; /* The function implementing the method */
1212 PyObject *im_self; /* The instance it is bound to, or NULL */
1213 PyObject *im_class; /* The class that defined the method */
1214} PyMethodObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001215
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001216PyObject *
1217PyMethod_New(func, self, class)
1218 PyObject *func;
1219 PyObject *self;
1220 PyObject *class;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001221{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001222 register PyMethodObject *im;
1223 if (!PyFunction_Check(func)) {
1224 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001225 return NULL;
1226 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227 im = PyObject_NEW(PyMethodObject, &PyMethod_Type);
Guido van Rossume8122f11991-05-05 20:03:07 +00001228 if (im == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001229 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00001231 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001232 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +00001233 im->im_self = self;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001234 Py_INCREF(class);
Guido van Rossum81daa321993-05-20 14:24:46 +00001235 im->im_class = class;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001237}
1238
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239PyObject *
1240PyMethod_Function(im)
1241 register PyObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001243 if (!PyMethod_Check(im)) {
1244 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001245 return NULL;
1246 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001247 return ((PyMethodObject *)im)->im_func;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001248}
1249
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001250PyObject *
1251PyMethod_Self(im)
1252 register PyObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001253{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254 if (!PyMethod_Check(im)) {
1255 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001256 return NULL;
1257 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001258 return ((PyMethodObject *)im)->im_self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001259}
1260
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261PyObject *
1262PyMethod_Class(im)
1263 register PyObject *im;
Guido van Rossum81daa321993-05-20 14:24:46 +00001264{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001265 if (!PyMethod_Check(im)) {
1266 PyErr_BadInternalCall();
Guido van Rossum81daa321993-05-20 14:24:46 +00001267 return NULL;
1268 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001269 return ((PyMethodObject *)im)->im_class;
Guido van Rossum81daa321993-05-20 14:24:46 +00001270}
1271
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001272/* Class method methods */
1273
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001274#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001275
Guido van Rossume8122f11991-05-05 20:03:07 +00001276static struct memberlist instancemethod_memberlist[] = {
1277 {"im_func", T_OBJECT, OFF(im_func)},
1278 {"im_self", T_OBJECT, OFF(im_self)},
Guido van Rossum81daa321993-05-20 14:24:46 +00001279 {"im_class", T_OBJECT, OFF(im_class)},
Guido van Rossum6dabc981996-05-14 21:54:20 +00001280 /* Dummies that are not handled by getattr() except for __members__ */
1281 {"__doc__", T_INT, 0},
1282 {"__name__", T_INT, 0},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001283 {NULL} /* Sentinel */
1284};
1285
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001286static PyObject *
Guido van Rossume8122f11991-05-05 20:03:07 +00001287instancemethod_getattr(im, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001288 register PyMethodObject *im;
1289 PyObject *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001290{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291 char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +00001292 if (sname[0] == '_') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001293 PyFunctionObject *func = (PyFunctionObject *)(im->im_func);
Guido van Rossum2878a691996-08-09 20:53:24 +00001294 if (strcmp(sname, "__name__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001295 Py_INCREF(func->func_name);
Guido van Rossum6dabc981996-05-14 21:54:20 +00001296 return func->func_name;
1297 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001298 if (strcmp(sname, "__doc__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 Py_INCREF(func->func_doc);
Guido van Rossum6dabc981996-05-14 21:54:20 +00001300 return func->func_doc;
1301 }
1302 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001303 if (PyEval_GetRestricted()) {
1304 PyErr_SetString(PyExc_RuntimeError,
1305 "instance-method attributes not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +00001306 return NULL;
1307 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308 return PyMember_Get((char *)im, instancemethod_memberlist, sname);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001309}
1310
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001311static void
Guido van Rossume8122f11991-05-05 20:03:07 +00001312instancemethod_dealloc(im)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001313 register PyMethodObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001314{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 Py_DECREF(im->im_func);
1316 Py_XDECREF(im->im_self);
1317 Py_DECREF(im->im_class);
Guido van Rossume8122f11991-05-05 20:03:07 +00001318 free((ANY *)im);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001319}
1320
Guido van Rossumebc8c511992-09-03 20:39:51 +00001321static int
1322instancemethod_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001323 PyMethodObject *a, *b;
Guido van Rossumebc8c511992-09-03 20:39:51 +00001324{
Guido van Rossume9df7271995-04-06 14:46:51 +00001325 if (a->im_self != b->im_self)
1326 return (a->im_self < b->im_self) ? -1 : 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001327 return PyObject_Compare(a->im_func, b->im_func);
Guido van Rossumebc8c511992-09-03 20:39:51 +00001328}
1329
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001330static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00001331instancemethod_repr(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001332 PyMethodObject *a;
Guido van Rossum25831651993-05-19 14:50:45 +00001333{
1334 char buf[240];
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001335 PyInstanceObject *self = (PyInstanceObject *)(a->im_self);
1336 PyFunctionObject *func = (PyFunctionObject *)(a->im_func);
1337 PyClassObject *class = (PyClassObject *)(a->im_class);
1338 PyObject *fclassname, *iclassname, *funcname;
Guido van Rossum81daa321993-05-20 14:24:46 +00001339 char *fcname, *icname, *fname;
1340 fclassname = class->cl_name;
1341 funcname = func->func_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001342 if (fclassname != NULL && PyString_Check(fclassname))
1343 fcname = PyString_AsString(fclassname);
Guido van Rossum25831651993-05-19 14:50:45 +00001344 else
Guido van Rossum81daa321993-05-20 14:24:46 +00001345 fcname = "?";
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001346 if (funcname != NULL && PyString_Check(funcname))
1347 fname = PyString_AsString(funcname);
Guido van Rossum25831651993-05-19 14:50:45 +00001348 else
1349 fname = "?";
Guido van Rossum81daa321993-05-20 14:24:46 +00001350 if (self == NULL)
1351 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname);
1352 else {
1353 iclassname = self->in_class->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001354 if (iclassname != NULL && PyString_Check(iclassname))
1355 icname = PyString_AsString(iclassname);
Guido van Rossum81daa321993-05-20 14:24:46 +00001356 else
1357 icname = "?";
1358 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
1359 fcname, fname, icname, (long)self);
1360 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001361 return PyString_FromString(buf);
Guido van Rossum25831651993-05-19 14:50:45 +00001362}
1363
Guido van Rossum9bfef441993-03-29 10:43:31 +00001364static long
1365instancemethod_hash(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001366 PyMethodObject *a;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001367{
1368 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +00001369 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001370 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +00001371 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001372 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001373 if (x == -1)
1374 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001376 if (y == -1)
1377 return -1;
1378 return x ^ y;
1379}
1380
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001381PyTypeObject PyMethod_Type = {
1382 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001383 0,
Guido van Rossum569fce71991-04-16 08:38:43 +00001384 "instance method",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001386 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +00001387 (destructor)instancemethod_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001388 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001389 0, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001390 0, /*tp_setattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001391 (cmpfunc)instancemethod_compare, /*tp_compare*/
1392 (reprfunc)instancemethod_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001393 0, /*tp_as_number*/
1394 0, /*tp_as_sequence*/
1395 0, /*tp_as_mapping*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001396 (hashfunc)instancemethod_hash, /*tp_hash*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001397 0, /*tp_call*/
1398 0, /*tp_str*/
1399 (getattrofunc)instancemethod_getattr, /*tp_getattro*/
1400 0, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001401};