blob: b6052a07583b6de55d7c0f2002cb4c241d974eda [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;
Guido van Rossum04d73c41997-10-07 14:54:11 +000046 PyObject *name;
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;
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000050 static PyObject *docstr, *modstr, *namestr;
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 Rossum7cc56eb1997-09-12 20:04:46 +000056 if (modstr == NULL) {
57 modstr= PyString_InternFromString("__module__");
58 if (modstr == NULL)
59 return NULL;
60 }
61 if (namestr == NULL) {
62 namestr= PyString_InternFromString("__name__");
63 if (namestr == NULL)
64 return NULL;
65 }
Guido van Rossum04d73c41997-10-07 14:54:11 +000066 if (name == NULL || !PyString_Check(name)) {
67 PyErr_SetString(PyExc_SystemError,
68 "PyClass_New: name must be a string");
69 return NULL;
70 }
71 if (dict == NULL || !PyDict_Check(dict)) {
72 PyErr_SetString(PyExc_SystemError,
73 "PyClass_New: dict must be a dictionary");
74 return NULL;
75 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076 if (PyDict_GetItem(dict, docstr) == NULL) {
77 if (PyDict_SetItem(dict, docstr, Py_None) < 0)
Guido van Rossume7d444f1995-01-07 12:35:18 +000078 return NULL;
79 }
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000080 if (PyDict_GetItem(dict, modstr) == NULL) {
81 PyObject *globals = PyEval_GetGlobals();
82 if (globals != NULL) {
Guido van Rossum04d73c41997-10-07 14:54:11 +000083 PyObject *modname = PyDict_GetItem(globals, namestr);
84 if (modname != NULL) {
85 if (PyDict_SetItem(dict, modstr, modname) < 0)
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000086 return NULL;
87 }
88 }
89 }
Guido van Rossume2966a61991-12-10 13:53:23 +000090 if (bases == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000091 bases = PyTuple_New(0);
Guido van Rossume2966a61991-12-10 13:53:23 +000092 if (bases == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +000093 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000094 }
Guido van Rossum04d73c41997-10-07 14:54:11 +000095 else {
96 int i;
97 if (!PyTuple_Check(bases)) {
98 PyErr_SetString(PyExc_SystemError,
99 "PyClass_New: bases must be a tuple");
100 return NULL;
101 }
102 i = PyTuple_Size(bases);
103 while (--i >= 0) {
104 if (!PyClass_Check(PyTuple_GetItem(bases, i))) {
105 PyErr_SetString(PyExc_SystemError,
106 "PyClass_New: base must be a class");
107 return NULL;
108 }
109 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000110 Py_INCREF(bases);
Guido van Rossum04d73c41997-10-07 14:54:11 +0000111 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000112 op = PyObject_NEW(PyClassObject, &PyClass_Type);
Guido van Rossume2966a61991-12-10 13:53:23 +0000113 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000114 Py_DECREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +0000116 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117 op->cl_bases = bases;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 Py_INCREF(dict);
Guido van Rossum81daa321993-05-20 14:24:46 +0000119 op->cl_dict = dict;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120 Py_XINCREF(name);
Guido van Rossum94308391991-10-20 20:11:48 +0000121 op->cl_name = name;
Guido van Rossum2878a691996-08-09 20:53:24 +0000122 if (getattrstr == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +0000123 getattrstr = PyString_InternFromString("__getattr__");
124 setattrstr = PyString_InternFromString("__setattr__");
125 delattrstr = PyString_InternFromString("__delattr__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000126 }
127 op->cl_getattr = class_lookup(op, getattrstr, &dummy);
128 op->cl_setattr = class_lookup(op, setattrstr, &dummy);
129 op->cl_delattr = class_lookup(op, delattrstr, &dummy);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 Py_XINCREF(op->cl_getattr);
131 Py_XINCREF(op->cl_setattr);
132 Py_XINCREF(op->cl_delattr);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000133 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
136/* Class methods */
137
138static void
139class_dealloc(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140 PyClassObject *op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000141{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000142 Py_DECREF(op->cl_bases);
143 Py_DECREF(op->cl_dict);
144 Py_XDECREF(op->cl_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000145 free((ANY *)op);
146}
147
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000148static PyObject *
Guido van Rossum81daa321993-05-20 14:24:46 +0000149class_lookup(cp, name, pclass)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000150 PyClassObject *cp;
151 PyObject *name;
152 PyClassObject **pclass;
Guido van Rossum81daa321993-05-20 14:24:46 +0000153{
154 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000155 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
Guido van Rossum81daa321993-05-20 14:24:46 +0000156 if (value != NULL) {
157 *pclass = cp;
158 return value;
159 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000160 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000161 for (i = 0; i < n; i++) {
Guido van Rossum7cc56eb1997-09-12 20:04:46 +0000162 /* XXX What if one of the bases is not a class? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000163 PyObject *v = class_lookup(
164 (PyClassObject *)
165 PyTuple_GetItem(cp->cl_bases, i), name, pclass);
Guido van Rossum81daa321993-05-20 14:24:46 +0000166 if (v != NULL)
167 return v;
168 }
169 return NULL;
170}
171
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000172static PyObject *
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000173class_getattr(op, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000174 register PyClassObject *op;
175 PyObject *name;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000176{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000177 register PyObject *v;
178 register char *sname = PyString_AsString(name);
179 PyClassObject *class;
Guido van Rossum2878a691996-08-09 20:53:24 +0000180 if (sname[0] == '_' && sname[1] == '_') {
181 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000182 if (PyEval_GetRestricted()) {
183 PyErr_SetString(PyExc_RuntimeError,
184 "class.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000185 return NULL;
186 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187 Py_INCREF(op->cl_dict);
Guido van Rossum10393b11995-01-10 10:39:49 +0000188 return op->cl_dict;
189 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000190 if (strcmp(sname, "__bases__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 Py_INCREF(op->cl_bases);
Guido van Rossum10393b11995-01-10 10:39:49 +0000192 return op->cl_bases;
193 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000194 if (strcmp(sname, "__name__") == 0) {
Guido van Rossum10393b11995-01-10 10:39:49 +0000195 if (op->cl_name == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000196 v = Py_None;
Guido van Rossum10393b11995-01-10 10:39:49 +0000197 else
198 v = op->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000199 Py_INCREF(v);
Guido van Rossum10393b11995-01-10 10:39:49 +0000200 return v;
201 }
Guido van Rossum94308391991-10-20 20:11:48 +0000202 }
Guido van Rossum81daa321993-05-20 14:24:46 +0000203 v = class_lookup(op, name, &class);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000204 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000205 PyErr_SetObject(PyExc_AttributeError, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000206 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000207 }
Guido van Rossum0add15f1997-05-09 01:07:15 +0000208 Py_INCREF(v);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 if (PyFunction_Check(v)) {
210 PyObject *w = PyMethod_New(v, (PyObject *)NULL,
211 (PyObject *)class);
212 Py_DECREF(v);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000213 v = w;
214 }
215 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000216}
217
Guido van Rossum94308391991-10-20 20:11:48 +0000218static int
219class_setattr(op, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000220 PyClassObject *op;
221 PyObject *name;
222 PyObject *v;
Guido van Rossum94308391991-10-20 20:11:48 +0000223{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000224 char *sname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000225 if (PyEval_GetRestricted()) {
226 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumac21f6a1995-08-04 04:05:31 +0000227 "classes are read-only in restricted mode");
228 return -1;
229 }
Guido van Rossumb2173c31997-08-25 21:23:56 +0000230 sname = PyString_AsString(name);
231 if (sname[0] == '_' && sname[1] == '_') {
232 int n = PyString_Size(name);
233 if (sname[n-1] == '_' && sname[n-2] == '_') {
234 if (strcmp(sname, "__dict__") == 0 ||
235 strcmp(sname, "__bases__") == 0 ||
236 strcmp(sname, "__name__") == 0 ||
237 strcmp(sname, "__getattr__") == 0 ||
238 strcmp(sname, "__setattr__") == 0 ||
239 strcmp(sname, "__delattr__") == 0)
240 {
241 /* XXX In unrestricted mode, we should
242 XXX allow this -- with a type check */
243 PyErr_SetString(PyExc_TypeError,
244 "read-only special attribute");
245 return -1;
246 }
247 }
248 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000249 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000250 int rv = PyDict_DelItem(op->cl_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000251 if (rv < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000252 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000253 "delete non-existing class attribute");
254 return rv;
255 }
Guido van Rossum94308391991-10-20 20:11:48 +0000256 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000257 return PyDict_SetItem(op->cl_dict, name, v);
Guido van Rossum94308391991-10-20 20:11:48 +0000258}
259
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000260static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +0000261class_repr(op)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000262 PyClassObject *op;
Guido van Rossum25831651993-05-19 14:50:45 +0000263{
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000264 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
Guido van Rossum25831651993-05-19 14:50:45 +0000265 char buf[140];
266 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000267 if (op->cl_name == NULL || !PyString_Check(op->cl_name))
Guido van Rossum25831651993-05-19 14:50:45 +0000268 name = "?";
269 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000270 name = PyString_AsString(op->cl_name);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000271 if (mod == NULL || !PyString_Check(mod))
272 sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op);
273 else
274 sprintf(buf, "<class %.50s.%.50s at %lx>",
275 PyString_AsString(mod),
276 name, (long)op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000277 return PyString_FromString(buf);
Guido van Rossum25831651993-05-19 14:50:45 +0000278}
279
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000280static PyObject *
281class_str(op)
282 PyClassObject *op;
283{
284 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
285 PyObject *name = op->cl_name;
286 PyObject *res;
287 int m, n;
288
289 if (name == NULL || !PyString_Check(name))
290 return class_repr(op);
291 if (mod == NULL || !PyString_Check(mod)) {
292 Py_INCREF(name);
293 return name;
294 }
295 m = PyString_Size(mod);
296 n = PyString_Size(name);
297 res = PyString_FromStringAndSize((char *)NULL, m+1+n);
298 if (res != NULL) {
299 char *s = PyString_AsString(res);
300 memcpy(s, PyString_AsString(mod), m);
301 s += m;
302 *s++ = '.';
303 memcpy(s, PyString_AsString(name), n);
304 }
305 return res;
306}
307
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000308PyTypeObject PyClass_Type = {
309 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000310 0,
311 "class",
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000312 sizeof(PyClassObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000313 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +0000314 (destructor)class_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000315 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +0000316 0, /*tp_getattr*/
317 0, /*tp_setattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000318 0, /*tp_compare*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000319 (reprfunc)class_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000320 0, /*tp_as_number*/
321 0, /*tp_as_sequence*/
322 0, /*tp_as_mapping*/
Guido van Rossum2878a691996-08-09 20:53:24 +0000323 0, /*tp_hash*/
324 0, /*tp_call*/
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000325 (reprfunc)class_str, /*tp_str*/
Guido van Rossum2878a691996-08-09 20:53:24 +0000326 (getattrofunc)class_getattr, /*tp_getattro*/
327 (setattrofunc)class_setattr, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000328};
329
Guido van Rossum81daa321993-05-20 14:24:46 +0000330int
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331PyClass_IsSubclass(class, base)
332 PyObject *class;
333 PyObject *base;
Guido van Rossum81daa321993-05-20 14:24:46 +0000334{
335 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000336 PyClassObject *cp;
Guido van Rossum81daa321993-05-20 14:24:46 +0000337 if (class == base)
338 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000339 if (class == NULL || !PyClass_Check(class))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000340 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341 cp = (PyClassObject *)class;
342 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000343 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000344 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
Guido van Rossum81daa321993-05-20 14:24:46 +0000345 return 1;
346 }
347 return 0;
348}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000349
Guido van Rossum81daa321993-05-20 14:24:46 +0000350
351/* Instance objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000352
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000353PyObject *
354PyInstance_New(class, arg, kw)
355 PyObject *class;
356 PyObject *arg;
357 PyObject *kw;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000358{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000359 register PyInstanceObject *inst;
360 PyObject *init;
361 static PyObject *initstr;
362 if (!PyClass_Check(class)) {
363 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000364 return NULL;
365 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000366 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type);
Guido van Rossume8122f11991-05-05 20:03:07 +0000367 if (inst == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000368 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000369 Py_INCREF(class);
370 inst->in_class = (PyClassObject *)class;
371 inst->in_dict = PyDict_New();
Guido van Rossum0add15f1997-05-09 01:07:15 +0000372 if (inst->in_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000373 Py_DECREF(inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000374 return NULL;
375 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000376 if (initstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000377 initstr = PyString_InternFromString("__init__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000378 init = instance_getattr1(inst, initstr);
Guido van Rossum25831651993-05-19 14:50:45 +0000379 if (init == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000380 PyErr_Clear();
381 if ((arg != NULL && (!PyTuple_Check(arg) ||
382 PyTuple_Size(arg) != 0))
383 || (kw != NULL && (!PyDict_Check(kw) ||
384 PyDict_Size(kw) != 0))) {
385 PyErr_SetString(PyExc_TypeError,
Guido van Rossuma83f2701995-07-26 18:07:32 +0000386 "this constructor takes no arguments");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000387 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000388 inst = NULL;
389 }
390 }
391 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000392 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
393 Py_DECREF(init);
Guido van Rossum25831651993-05-19 14:50:45 +0000394 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000395 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000396 inst = NULL;
397 }
398 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000399 if (res != Py_None) {
400 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +0000401 "__init__() should return None");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000402 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000403 inst = NULL;
404 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000405 Py_DECREF(res);
Guido van Rossum25831651993-05-19 14:50:45 +0000406 }
407 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000408 return (PyObject *)inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000409}
410
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000411/* Instance methods */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000412
413static void
Guido van Rossume8122f11991-05-05 20:03:07 +0000414instance_dealloc(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000415 register PyInstanceObject *inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000416{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000417 PyObject *error_type, *error_value, *error_traceback;
418 PyObject *del;
419 static PyObject *delstr;
Guido van Rossum25831651993-05-19 14:50:45 +0000420 /* Call the __del__ method if it exists. First temporarily
421 revive the object and save the current exception, if any. */
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000422#ifdef Py_TRACE_REFS
423 /* much too complicated if Py_TRACE_REFS defined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000424 extern long _Py_RefTotal;
425 inst->ob_type = &PyInstance_Type;
426 _Py_NewReference(inst);
427 _Py_RefTotal--; /* compensate for increment in NEWREF */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000428#ifdef COUNT_ALLOCS
429 inst->ob_type->tp_alloc--; /* ditto */
430#endif
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000431#else /* !Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000432 Py_INCREF(inst);
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000433#endif /* !Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000434 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Guido van Rossum2878a691996-08-09 20:53:24 +0000435 if (delstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000436 delstr = PyString_InternFromString("__del__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000437 if ((del = instance_getattr1(inst, delstr)) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000438 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000439 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000440 PyObject *f, *t, *v, *tb;
441 PyErr_Fetch(&t, &v, &tb);
442 f = PySys_GetObject("stderr");
Guido van Rossum22a85e51996-09-11 22:51:57 +0000443 if (f != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000444 PyFile_WriteString("Exception ", f);
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000445 if (t) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000446 PyFile_WriteObject(t, f, Py_PRINT_RAW);
447 if (v && v != Py_None) {
448 PyFile_WriteString(": ", f);
449 PyFile_WriteObject(v, f, 0);
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000450 }
451 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000452 PyFile_WriteString(" in ", f);
453 PyFile_WriteObject(del, f, 0);
454 PyFile_WriteString(" ignored\n", f);
455 PyErr_Clear(); /* Just in case */
Guido van Rossum22a85e51996-09-11 22:51:57 +0000456 }
Guido van Rossumcbd1e4e1996-12-05 21:52:32 +0000457 Py_XDECREF(t);
458 Py_XDECREF(v);
459 Py_XDECREF(tb);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000460 }
461 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000462 Py_DECREF(res);
463 Py_DECREF(del);
Guido van Rossum25831651993-05-19 14:50:45 +0000464 }
465 /* Restore the saved exception and undo the temporary revival */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000466 PyErr_Restore(error_type, error_value, error_traceback);
Guido van Rossum25831651993-05-19 14:50:45 +0000467 /* Can't use DECREF here, it would cause a recursive call */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000468 if (--inst->ob_refcnt > 0) {
469#ifdef COUNT_ALLOCS
470 inst->ob_type->tp_free--;
471#endif
Guido van Rossum25831651993-05-19 14:50:45 +0000472 return; /* __del__ added a reference; don't delete now */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000473 }
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000474#ifdef Py_TRACE_REFS
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000475#ifdef COUNT_ALLOCS
476 inst->ob_type->tp_free--; /* compensate for increment in UNREF */
477#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000478 _Py_ForgetReference(inst);
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000479 inst->ob_type = NULL;
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000480#endif /* Py_TRACE_REFS */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000481 Py_DECREF(inst->in_class);
482 Py_XDECREF(inst->in_dict);
Guido van Rossume8122f11991-05-05 20:03:07 +0000483 free((ANY *)inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484}
485
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000486static PyObject *
Guido van Rossume7737541994-09-05 07:31:41 +0000487instance_getattr1(inst, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000488 register PyInstanceObject *inst;
489 PyObject *name;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000490{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 register PyObject *v;
492 register char *sname = PyString_AsString(name);
493 PyClassObject *class;
Guido van Rossum2878a691996-08-09 20:53:24 +0000494 if (sname[0] == '_' && sname[1] == '_') {
495 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000496 if (PyEval_GetRestricted()) {
497 PyErr_SetString(PyExc_RuntimeError,
498 "instance.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000499 return NULL;
500 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000501 Py_INCREF(inst->in_dict);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000502 return inst->in_dict;
503 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000504 if (strcmp(sname, "__class__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000505 Py_INCREF(inst->in_class);
506 return (PyObject *)inst->in_class;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000507 }
Guido van Rossum94308391991-10-20 20:11:48 +0000508 }
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000509 class = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000510 v = PyDict_GetItem(inst->in_dict, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000511 if (v == NULL) {
512 v = class_lookup(inst->in_class, name, &class);
513 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000514 PyErr_SetObject(PyExc_AttributeError, name);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000515 return NULL;
516 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000517 }
Guido van Rossum0add15f1997-05-09 01:07:15 +0000518 Py_INCREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000519 if (class != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000520 if (PyFunction_Check(v)) {
521 PyObject *w = PyMethod_New(v, (PyObject *)inst,
522 (PyObject *)class);
523 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000524 v = w;
525 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000526 else if (PyMethod_Check(v)) {
527 PyObject *im_class = PyMethod_Class(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000528 /* Only if classes are compatible */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000529 if (PyClass_IsSubclass((PyObject *)class, im_class)) {
530 PyObject *im_func = PyMethod_Function(v);
531 PyObject *w = PyMethod_New(im_func,
532 (PyObject *)inst, im_class);
533 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000534 v = w;
535 }
536 }
Guido van Rossumb3f72581993-05-21 19:56:10 +0000537 }
538 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000539}
540
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000541static PyObject *
Guido van Rossume7737541994-09-05 07:31:41 +0000542instance_getattr(inst, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000543 register PyInstanceObject *inst;
544 PyObject *name;
Guido van Rossume7737541994-09-05 07:31:41 +0000545{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000546 register PyObject *func, *res;
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000547 res = instance_getattr1(inst, name);
548 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000549 PyObject *args;
550 PyErr_Clear();
551 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000552 if (args == NULL)
Guido van Rossume7737541994-09-05 07:31:41 +0000553 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 res = PyEval_CallObject(func, args);
555 Py_DECREF(args);
Guido van Rossume7737541994-09-05 07:31:41 +0000556 }
557 return res;
558}
559
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000560static int
Guido van Rossume7737541994-09-05 07:31:41 +0000561instance_setattr1(inst, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000562 PyInstanceObject *inst;
563 PyObject *name;
564 PyObject *v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000565{
Guido van Rossum94472a01992-09-04 09:45:18 +0000566 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000567 int rv = PyDict_DelItem(inst->in_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000568 if (rv < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000569 PyErr_SetString(PyExc_AttributeError,
Guido van Rossum94472a01992-09-04 09:45:18 +0000570 "delete non-existing instance attribute");
571 return rv;
572 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000573 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000574 return PyDict_SetItem(inst->in_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000575}
576
Guido van Rossume7737541994-09-05 07:31:41 +0000577static int
578instance_setattr(inst, name, v)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000579 PyInstanceObject *inst;
580 PyObject *name;
581 PyObject *v;
Guido van Rossume7737541994-09-05 07:31:41 +0000582{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000583 PyObject *func, *args, *res, *tmp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 char *sname = PyString_AsString(name);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000585 if (sname[0] == '_' && sname[1] == '_') {
586 int n = PyString_Size(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000587 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossumb2173c31997-08-25 21:23:56 +0000588 if (strcmp(sname, "__dict__") == 0) {
589 if (PyEval_GetRestricted()) {
590 PyErr_SetString(PyExc_RuntimeError,
591 "__dict__ not accessible in restricted mode");
592 return -1;
593 }
594 if (v == NULL || !PyDict_Check(v)) {
595 PyErr_SetString(PyExc_TypeError,
596 "__dict__ must be set to a dictionary");
597 return -1;
598 }
599 tmp = inst->in_dict;
600 Py_INCREF(v);
601 inst->in_dict = v;
602 Py_DECREF(tmp);
603 return 0;
604 }
605 if (strcmp(sname, "__class__") == 0) {
606 if (PyEval_GetRestricted()) {
607 PyErr_SetString(PyExc_RuntimeError,
608 "__class__ not accessible in restricted mode");
609 return -1;
610 }
611 if (v == NULL || !PyClass_Check(v)) {
612 PyErr_SetString(PyExc_TypeError,
613 "__class__ must be set to a class");
614 return -1;
615 }
616 tmp = (PyObject *)(inst->in_class);
617 Py_INCREF(v);
618 inst->in_class = (PyClassObject *)v;
619 Py_DECREF(tmp);
620 return 0;
621 }
Guido van Rossume7737541994-09-05 07:31:41 +0000622 }
Guido van Rossume7737541994-09-05 07:31:41 +0000623 }
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000624 if (v == NULL)
625 func = inst->in_class->cl_delattr;
626 else
627 func = inst->in_class->cl_setattr;
628 if (func == NULL)
629 return instance_setattr1(inst, name, v);
630 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000631 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000632 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000633 args = Py_BuildValue("(OOO)", inst, name, v);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000634 if (args == NULL)
635 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000636 res = PyEval_CallObject(func, args);
637 Py_DECREF(args);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000638 if (res == NULL)
639 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000640 Py_DECREF(res);
Guido van Rossume7737541994-09-05 07:31:41 +0000641 return 0;
642}
643
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000644static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000645instance_repr(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000647{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000648 PyObject *func;
649 PyObject *res;
650 static PyObject *reprstr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000651
Guido van Rossum2878a691996-08-09 20:53:24 +0000652 if (reprstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000653 reprstr = PyString_InternFromString("__repr__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000654 func = instance_getattr(inst, reprstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000655 if (func == NULL) {
Guido van Rossum25831651993-05-19 14:50:45 +0000656 char buf[140];
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000657 PyObject *classname = inst->in_class->cl_name;
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000658 PyObject *mod = PyDict_GetItemString(
659 inst->in_class->cl_dict, "__module__");
Guido van Rossum25831651993-05-19 14:50:45 +0000660 char *cname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000661 if (classname != NULL && PyString_Check(classname))
662 cname = PyString_AsString(classname);
Guido van Rossum25831651993-05-19 14:50:45 +0000663 else
664 cname = "?";
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000665 PyErr_Clear();
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000666 if (mod == NULL || !PyString_Check(mod))
667 sprintf(buf, "<?.%.100s instance at %lx>",
668 cname, (long)inst);
669 else
670 sprintf(buf, "<%.50s.%.50s instance at %lx>",
671 PyString_AsString(mod),
672 cname, (long)inst);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000673 return PyString_FromString(buf);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000674 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000675 res = PyEval_CallObject(func, (PyObject *)NULL);
676 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000677 return res;
678}
679
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000680static PyObject *
Guido van Rossume7d444f1995-01-07 12:35:18 +0000681instance_compare1(inst, other)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000682 PyObject *inst, *other;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000683{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000684 return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__",
Guido van Rossume7d444f1995-01-07 12:35:18 +0000685 instance_compare1);
686}
687
Guido van Rossum9bfef441993-03-29 10:43:31 +0000688static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000689instance_compare(inst, other)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 PyObject *inst, *other;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000691{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000692 PyObject *result;
Guido van Rossume7d444f1995-01-07 12:35:18 +0000693 long outcome;
694 result = instance_compare1(inst, other);
Guido van Rossumc8b6df91997-05-23 00:06:51 +0000695 if (result == NULL)
696 return -1;
697 if (!PyInt_Check(result)) {
698 Py_DECREF(result);
699 PyErr_SetString(PyExc_TypeError,
700 "comparison did not return an int");
701 return -1;
Sjoerd Mullenderb9a6d121994-10-19 15:11:52 +0000702 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 outcome = PyInt_AsLong(result);
704 Py_DECREF(result);
Guido van Rossum03093a21994-09-28 15:51:32 +0000705 if (outcome < 0)
706 return -1;
707 else if (outcome > 0)
708 return 1;
709 return 0;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000710}
711
Guido van Rossum9bfef441993-03-29 10:43:31 +0000712static long
713instance_hash(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000714 PyInstanceObject *inst;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000715{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000716 PyObject *func;
717 PyObject *res;
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000718 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000719 static PyObject *hashstr, *cmpstr;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000720
Guido van Rossum2878a691996-08-09 20:53:24 +0000721 if (hashstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000722 hashstr = PyString_InternFromString("__hash__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000723 func = instance_getattr(inst, hashstr);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000724 if (func == NULL) {
725 /* If there is no __cmp__ method, we hash on the address.
726 If a __cmp__ method exists, there must be a __hash__. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 PyErr_Clear();
Guido van Rossum2878a691996-08-09 20:53:24 +0000728 if (cmpstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000729 cmpstr = PyString_InternFromString("__cmp__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000730 func = instance_getattr(inst, cmpstr);
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000731 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000732 PyErr_Clear();
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000733 outcome = (long)inst;
734 if (outcome == -1)
735 outcome = -2;
736 return outcome;
737 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000738 PyErr_SetString(PyExc_TypeError, "unhashable instance");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000739 return -1;
740 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000741 res = PyEval_CallObject(func, (PyObject *)NULL);
742 Py_DECREF(func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000743 if (res == NULL)
744 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000745 if (PyInt_Check(res)) {
746 outcome = PyInt_AsLong(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000747 if (outcome == -1)
748 outcome = -2;
749 }
750 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000751 PyErr_SetString(PyExc_TypeError,
752 "__hash__() should return an int");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000753 outcome = -1;
754 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000755 Py_DECREF(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000756 return outcome;
757}
758
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
Guido van Rossum2878a691996-08-09 20:53:24 +0000760
Guido van Rossum9bfef441993-03-29 10:43:31 +0000761static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000762instance_length(inst)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000764{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000765 PyObject *func;
766 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000767 int outcome;
768
Guido van Rossum2878a691996-08-09 20:53:24 +0000769 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000770 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000771 func = instance_getattr(inst, lenstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000772 if (func == NULL)
773 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000774 res = PyEval_CallObject(func, (PyObject *)NULL);
775 Py_DECREF(func);
Guido van Rossumd014ea61992-11-26 10:30:26 +0000776 if (res == NULL)
777 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000778 if (PyInt_Check(res)) {
779 outcome = PyInt_AsLong(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000780 if (outcome < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781 PyErr_SetString(PyExc_ValueError,
782 "__len__() should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000783 }
784 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000785 PyErr_SetString(PyExc_TypeError,
786 "__len__() should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000787 outcome = -1;
788 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000789 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000790 return outcome;
791}
792
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000793static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000794instance_subscript(inst, key)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000795 PyInstanceObject *inst;
796 PyObject *key;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000797{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000798 PyObject *func;
799 PyObject *arg;
800 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000801
Guido van Rossum2878a691996-08-09 20:53:24 +0000802 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000803 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000804 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000805 if (func == NULL)
806 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000807 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000808 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000810 return NULL;
811 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000812 res = PyEval_CallObject(func, arg);
813 Py_DECREF(func);
814 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000815 return res;
816}
817
Guido van Rossum9bfef441993-03-29 10:43:31 +0000818static int
Guido van Rossum04691fc1992-08-12 15:35:34 +0000819instance_ass_subscript(inst, key, value)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000820 PyInstanceObject*inst;
821 PyObject *key;
822 PyObject *value;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000823{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000824 PyObject *func;
825 PyObject *arg;
826 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000827
Guido van Rossum2878a691996-08-09 20:53:24 +0000828 if (value == NULL) {
829 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000830 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000831 func = instance_getattr(inst, delitemstr);
832 }
833 else {
834 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000835 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000836 func = instance_getattr(inst, setitemstr);
837 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000838 if (func == NULL)
839 return -1;
840 if (value == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000841 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000842 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000843 arg = Py_BuildValue("(OO)", key, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000844 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000845 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000846 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000847 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000848 res = PyEval_CallObject(func, arg);
849 Py_DECREF(func);
850 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000851 if (res == NULL)
852 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000853 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000854 return 0;
855}
856
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857static PyMappingMethods instance_as_mapping = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000858 (inquiry)instance_length, /*mp_length*/
859 (binaryfunc)instance_subscript, /*mp_subscript*/
860 (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/
Guido van Rossum04691fc1992-08-12 15:35:34 +0000861};
862
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000863static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000864instance_item(inst, i)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000865 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000866 int i;
867{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000868 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000869
Guido van Rossum2878a691996-08-09 20:53:24 +0000870 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000871 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000872 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000873 if (func == NULL)
874 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 arg = Py_BuildValue("(i)", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000876 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000877 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000878 return NULL;
879 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880 res = PyEval_CallObject(func, arg);
881 Py_DECREF(func);
882 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000883 return res;
884}
885
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000886static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000887instance_slice(inst, i, j)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000889 int i, j;
890{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000891 PyObject *func, *arg, *res;
892 static PyObject *getslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000893
Guido van Rossum2878a691996-08-09 20:53:24 +0000894 if (getslicestr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000895 getslicestr = PyString_InternFromString("__getslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000896 func = instance_getattr(inst, getslicestr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000897 if (func == NULL)
898 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000899 arg = Py_BuildValue("(ii)", i, j);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000900 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000901 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000902 return NULL;
903 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 res = PyEval_CallObject(func, arg);
905 Py_DECREF(func);
906 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000907 return res;
908}
909
910static int
911instance_ass_item(inst, i, item)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000912 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000913 int i;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000914 PyObject *item;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000915{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000916 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000917
Guido van Rossum2878a691996-08-09 20:53:24 +0000918 if (item == NULL) {
919 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000920 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000921 func = instance_getattr(inst, delitemstr);
922 }
923 else {
924 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000925 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000926 func = instance_getattr(inst, setitemstr);
927 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000928 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +0000929 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000930 if (item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000931 arg = Py_BuildValue("i", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000932 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000933 arg = Py_BuildValue("(iO)", i, item);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000934 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000936 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000937 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000938 res = PyEval_CallObject(func, arg);
939 Py_DECREF(func);
940 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000941 if (res == NULL)
942 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000944 return 0;
945}
946
947static int
948instance_ass_slice(inst, i, j, value)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000949 PyInstanceObject *inst;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000950 int i, j;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000951 PyObject *value;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000952{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000953 PyObject *func, *arg, *res;
954 static PyObject *setslicestr, *delslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000955
Guido van Rossum2878a691996-08-09 20:53:24 +0000956 if (value == NULL) {
957 if (delslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000958 delslicestr =
959 PyString_InternFromString("__delslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000960 func = instance_getattr(inst, delslicestr);
961 }
962 else {
963 if (setslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 setslicestr =
965 PyString_InternFromString("__setslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000966 func = instance_getattr(inst, setslicestr);
967 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000968 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +0000969 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000970 if (value == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000971 arg = Py_BuildValue("(ii)", i, j);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000972 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973 arg = Py_BuildValue("(iiO)", i, j, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000974 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000975 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000976 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000977 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000978 res = PyEval_CallObject(func, arg);
979 Py_DECREF(func);
980 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000981 if (res == NULL)
982 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000983 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000984 return 0;
985}
986
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000987static PySequenceMethods instance_as_sequence = {
Guido van Rossumb6775db1994-08-01 11:34:53 +0000988 (inquiry)instance_length, /*sq_length*/
Guido van Rossum03093a21994-09-28 15:51:32 +0000989 0, /*sq_concat*/
990 0, /*sq_repeat*/
Guido van Rossumb6775db1994-08-01 11:34:53 +0000991 (intargfunc)instance_item, /*sq_item*/
992 (intintargfunc)instance_slice, /*sq_slice*/
993 (intobjargproc)instance_ass_item, /*sq_ass_item*/
994 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/
Guido van Rossum04691fc1992-08-12 15:35:34 +0000995};
996
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997static PyObject *
Guido van Rossum04691fc1992-08-12 15:35:34 +0000998generic_unary_op(self, methodname)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000999 PyInstanceObject *self;
1000 PyObject *methodname;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001001{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001003
1004 if ((func = instance_getattr(self, methodname)) == NULL)
1005 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001006 res = PyEval_CallObject(func, (PyObject *)NULL);
1007 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001008 return res;
1009}
1010
Guido van Rossum03093a21994-09-28 15:51:32 +00001011
1012/* Forward */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001013static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **,
1014 PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int ));
Guido van Rossum03093a21994-09-28 15:51:32 +00001015
1016
1017/* Implement a binary operator involving at least one class instance. */
1018
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001019PyObject *
1020PyInstance_DoBinOp(v, w, opname, ropname, thisfunc)
1021 PyObject *v;
1022 PyObject *w;
Guido van Rossum03093a21994-09-28 15:51:32 +00001023 char *opname;
1024 char *ropname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001025 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossum03093a21994-09-28 15:51:32 +00001026{
1027 char buf[256];
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 PyObject *result = NULL;
Guido van Rossume7d444f1995-01-07 12:35:18 +00001029 if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0)
Guido van Rossum03093a21994-09-28 15:51:32 +00001030 return result;
Guido van Rossume7d444f1995-01-07 12:35:18 +00001031 if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0)
Guido van Rossum03093a21994-09-28 15:51:32 +00001032 return result;
Guido van Rossumc8b6df91997-05-23 00:06:51 +00001033 /* Sigh -- special case for comnparisons */
1034 if (strcmp(opname, "__cmp__") == 0) {
1035 long c = (v < w) ? -1 : (v > w) ? 1 : 0;
1036 return PyInt_FromLong(c);
1037 }
Guido van Rossum03093a21994-09-28 15:51:32 +00001038 sprintf(buf, "%s nor %s defined for these operands", opname, ropname);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001039 PyErr_SetString(PyExc_TypeError, buf);
Guido van Rossum03093a21994-09-28 15:51:32 +00001040 return NULL;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001041}
1042
Guido van Rossum03093a21994-09-28 15:51:32 +00001043
1044/* Try one half of a binary operator involving a class instance.
1045 Return value:
1046 -1 if an exception is to be reported right away
1047 0 if we have a valid result
1048 1 if we could try another operation
1049*/
1050
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001051static PyObject *coerce_obj;
Guido van Rossum2878a691996-08-09 20:53:24 +00001052
Guido van Rossum03093a21994-09-28 15:51:32 +00001053static int
Guido van Rossume7d444f1995-01-07 12:35:18 +00001054halfbinop(v, w, opname, r_result, thisfunc, swapped)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001055 PyObject *v;
1056 PyObject *w;
Guido van Rossum03093a21994-09-28 15:51:32 +00001057 char *opname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058 PyObject **r_result;
1059 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *));
Guido van Rossume7d444f1995-01-07 12:35:18 +00001060 int swapped;
Guido van Rossum03093a21994-09-28 15:51:32 +00001061{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001062 PyObject *func;
1063 PyObject *args;
Guido van Rossum3931df91997-11-18 19:23:07 +00001064 PyObject *coercefunc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001065 PyObject *coerced = NULL;
1066 PyObject *v1;
Guido van Rossum03093a21994-09-28 15:51:32 +00001067
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001068 if (!PyInstance_Check(v))
Guido van Rossum03093a21994-09-28 15:51:32 +00001069 return 1;
Guido van Rossum2878a691996-08-09 20:53:24 +00001070 if (coerce_obj == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +00001071 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001072 if (coerce_obj == NULL)
1073 return -1;
1074 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001075 coercefunc = PyObject_GetAttr(v, coerce_obj);
1076 if (coercefunc == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001077 PyErr_Clear();
Guido van Rossum03093a21994-09-28 15:51:32 +00001078 }
1079 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 args = Py_BuildValue("(O)", w);
Guido van Rossum03093a21994-09-28 15:51:32 +00001081 if (args == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +00001082 return -1;
1083 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001084 coerced = PyEval_CallObject(coercefunc, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001085 Py_DECREF(args);
Guido van Rossum3931df91997-11-18 19:23:07 +00001086 Py_DECREF(coercefunc);
Guido van Rossum03093a21994-09-28 15:51:32 +00001087 if (coerced == NULL) {
Guido van Rossum03093a21994-09-28 15:51:32 +00001088 return -1;
1089 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001090 if (coerced == Py_None) {
1091 Py_DECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +00001092 return 1;
1093 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001094 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
1095 Py_DECREF(coerced);
1096 PyErr_SetString(PyExc_TypeError,
Guido van Rossume7d444f1995-01-07 12:35:18 +00001097 "coercion should return None or 2-tuple");
Guido van Rossum03093a21994-09-28 15:51:32 +00001098 return -1;
1099 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001100 v1 = PyTuple_GetItem(coerced, 0);
1101 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum03093a21994-09-28 15:51:32 +00001102 if (v1 != v) {
1103 v = v1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
Guido van Rossume7d444f1995-01-07 12:35:18 +00001105 if (swapped)
1106 *r_result = (*thisfunc)(w, v);
1107 else
1108 *r_result = (*thisfunc)(v, w);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001109 Py_DECREF(coerced);
Guido van Rossume7d444f1995-01-07 12:35:18 +00001110 return *r_result == NULL ? -1 : 0;
Guido van Rossum03093a21994-09-28 15:51:32 +00001111 }
1112 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001113 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum03093a21994-09-28 15:51:32 +00001114 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001115 func = PyObject_GetAttrString(v, opname);
Guido van Rossume7d444f1995-01-07 12:35:18 +00001116 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117 Py_XDECREF(coerced);
1118 if (PyErr_Occurred() != PyExc_AttributeError)
Guido van Rossume7d444f1995-01-07 12:35:18 +00001119 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001120 PyErr_Clear();
Guido van Rossume7d444f1995-01-07 12:35:18 +00001121 return 1;
1122 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123 args = Py_BuildValue("(O)", w);
Guido van Rossum03093a21994-09-28 15:51:32 +00001124 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001125 Py_DECREF(func);
1126 Py_XDECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +00001127 return -1;
1128 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001129 *r_result = PyEval_CallObject(func, args);
1130 Py_DECREF(args);
1131 Py_DECREF(func);
1132 Py_XDECREF(coerced);
Guido van Rossum03093a21994-09-28 15:51:32 +00001133 return *r_result == NULL ? -1 : 0;
1134}
1135
Guido van Rossum879c5811995-01-10 15:24:06 +00001136static int
1137instance_coerce(pv, pw)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001138 PyObject **pv;
1139 PyObject **pw;
Guido van Rossum879c5811995-01-10 15:24:06 +00001140{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001141 PyObject *v = *pv;
1142 PyObject *w = *pw;
Guido van Rossum3931df91997-11-18 19:23:07 +00001143 PyObject *coercefunc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001144 PyObject *args;
1145 PyObject *coerced;
Guido van Rossum879c5811995-01-10 15:24:06 +00001146
Guido van Rossum2878a691996-08-09 20:53:24 +00001147 if (coerce_obj == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +00001148 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001149 if (coerce_obj == NULL)
1150 return -1;
1151 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001152 coercefunc = PyObject_GetAttr(v, coerce_obj);
1153 if (coercefunc == NULL) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001154 /* No __coerce__ method: always OK */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001155 PyErr_Clear();
1156 Py_INCREF(v);
1157 Py_INCREF(w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001158 return 0;
1159 }
1160 /* Has __coerce__ method: call it */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001161 args = Py_BuildValue("(O)", w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001162 if (args == NULL) {
1163 return -1;
1164 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001165 coerced = PyEval_CallObject(coercefunc, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001166 Py_DECREF(args);
Guido van Rossum3931df91997-11-18 19:23:07 +00001167 Py_DECREF(coercefunc);
Guido van Rossum879c5811995-01-10 15:24:06 +00001168 if (coerced == NULL) {
1169 /* __coerce__ call raised an exception */
1170 return -1;
1171 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001172 if (coerced == Py_None) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001173 /* __coerce__ says "I can't do it" */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001174 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001175 return 1;
1176 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001177 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001178 /* __coerce__ return value is malformed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001179 Py_DECREF(coerced);
1180 PyErr_SetString(PyExc_TypeError,
Guido van Rossum879c5811995-01-10 15:24:06 +00001181 "coercion should return None or 2-tuple");
1182 return -1;
1183 }
1184 /* __coerce__ returned two new values */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001185 *pv = PyTuple_GetItem(coerced, 0);
1186 *pw = PyTuple_GetItem(coerced, 1);
1187 Py_INCREF(*pv);
1188 Py_INCREF(*pw);
1189 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001190 return 0;
1191}
1192
Guido van Rossum03093a21994-09-28 15:51:32 +00001193
Guido van Rossum04691fc1992-08-12 15:35:34 +00001194#define UNARY(funcname, methodname) \
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001195static PyObject *funcname(self) PyInstanceObject *self; { \
1196 static PyObject *o; \
Guido van Rossuma412d241997-01-18 07:59:12 +00001197 if (o == NULL) o = PyString_InternFromString(methodname); \
Guido van Rossum2878a691996-08-09 20:53:24 +00001198 return generic_unary_op(self, o); \
Guido van Rossum04691fc1992-08-12 15:35:34 +00001199}
1200
Guido van Rossum04691fc1992-08-12 15:35:34 +00001201UNARY(instance_neg, "__neg__")
1202UNARY(instance_pos, "__pos__")
1203UNARY(instance_abs, "__abs__")
1204
Guido van Rossum9bfef441993-03-29 10:43:31 +00001205static int
Guido van Rossum04691fc1992-08-12 15:35:34 +00001206instance_nonzero(self)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001207 PyInstanceObject *self;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001208{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001209 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001210 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001211 static PyObject *nonzerostr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001212
Guido van Rossum2878a691996-08-09 20:53:24 +00001213 if (nonzerostr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001214 nonzerostr = PyString_InternFromString("__nonzero__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001215 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001216 PyErr_Clear();
Guido van Rossum2878a691996-08-09 20:53:24 +00001217 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001218 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001219 if ((func = instance_getattr(self, lenstr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001220 PyErr_Clear();
Guido van Rossum04691fc1992-08-12 15:35:34 +00001221 /* Fall back to the default behavior:
1222 all instances are nonzero */
1223 return 1;
1224 }
1225 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 res = PyEval_CallObject(func, (PyObject *)NULL);
1227 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001228 if (res == NULL)
1229 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001230 if (!PyInt_Check(res)) {
1231 Py_DECREF(res);
1232 PyErr_SetString(PyExc_TypeError,
1233 "__nonzero__ should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001234 return -1;
1235 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001236 outcome = PyInt_AsLong(res);
1237 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001238 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001239 PyErr_SetString(PyExc_ValueError,
1240 "__nonzero__ should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001241 return -1;
1242 }
1243 return outcome > 0;
1244}
1245
1246UNARY(instance_invert, "__invert__")
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001247UNARY(instance_int, "__int__")
1248UNARY(instance_long, "__long__")
1249UNARY(instance_float, "__float__")
1250UNARY(instance_oct, "__oct__")
1251UNARY(instance_hex, "__hex__")
1252
Guido van Rossum03093a21994-09-28 15:51:32 +00001253/* This version is for ternary calls only (z != None) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001254static PyObject *
Guido van Rossum03093a21994-09-28 15:51:32 +00001255instance_pow(v, w, z)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 PyObject *v;
1257 PyObject *w;
1258 PyObject *z;
Guido van Rossum03093a21994-09-28 15:51:32 +00001259{
1260 /* XXX Doesn't do coercions... */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001261 PyObject *func;
1262 PyObject *args;
1263 PyObject *result;
1264 static PyObject *powstr;
Guido van Rossum2878a691996-08-09 20:53:24 +00001265
1266 if (powstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001267 powstr = PyString_InternFromString("__pow__");
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001268 func = PyObject_GetAttr(v, powstr);
Guido van Rossum03093a21994-09-28 15:51:32 +00001269 if (func == NULL)
1270 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001271 args = Py_BuildValue("(OO)", w, z);
Guido van Rossum03093a21994-09-28 15:51:32 +00001272 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001273 Py_DECREF(func);
Guido van Rossum03093a21994-09-28 15:51:32 +00001274 return NULL;
1275 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001276 result = PyEval_CallObject(func, args);
1277 Py_DECREF(func);
1278 Py_DECREF(args);
Guido van Rossum03093a21994-09-28 15:51:32 +00001279 return result;
1280}
1281
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001282static PyNumberMethods instance_as_number = {
Guido van Rossum03093a21994-09-28 15:51:32 +00001283 0, /*nb_add*/
1284 0, /*nb_subtract*/
1285 0, /*nb_multiply*/
1286 0, /*nb_divide*/
1287 0, /*nb_remainder*/
1288 0, /*nb_divmod*/
Guido van Rossum52ca98a1994-09-05 07:32:29 +00001289 (ternaryfunc)instance_pow, /*nb_power*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001290 (unaryfunc)instance_neg, /*nb_negative*/
1291 (unaryfunc)instance_pos, /*nb_positive*/
1292 (unaryfunc)instance_abs, /*nb_absolute*/
1293 (inquiry)instance_nonzero, /*nb_nonzero*/
1294 (unaryfunc)instance_invert, /*nb_invert*/
Guido van Rossum03093a21994-09-28 15:51:32 +00001295 0, /*nb_lshift*/
1296 0, /*nb_rshift*/
1297 0, /*nb_and*/
1298 0, /*nb_xor*/
1299 0, /*nb_or*/
Guido van Rossum879c5811995-01-10 15:24:06 +00001300 (coercion)instance_coerce, /*nb_coerce*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001301 (unaryfunc)instance_int, /*nb_int*/
1302 (unaryfunc)instance_long, /*nb_long*/
1303 (unaryfunc)instance_float, /*nb_float*/
1304 (unaryfunc)instance_oct, /*nb_oct*/
1305 (unaryfunc)instance_hex, /*nb_hex*/
Guido van Rossum04691fc1992-08-12 15:35:34 +00001306};
1307
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001308PyTypeObject PyInstance_Type = {
1309 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001310 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +00001311 "instance",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 sizeof(PyInstanceObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001313 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +00001314 (destructor)instance_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +00001315 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001316 0, /*tp_getattr*/
1317 0, /*tp_setattr*/
1318 instance_compare, /*tp_compare*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001319 (reprfunc)instance_repr, /*tp_repr*/
Guido van Rossum04691fc1992-08-12 15:35:34 +00001320 &instance_as_number, /*tp_as_number*/
1321 &instance_as_sequence, /*tp_as_sequence*/
1322 &instance_as_mapping, /*tp_as_mapping*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001323 (hashfunc)instance_hash, /*tp_hash*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001324 0, /*tp_call*/
1325 0, /*tp_str*/
1326 (getattrofunc)instance_getattr, /*tp_getattro*/
1327 (setattrofunc)instance_setattr, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001328};
1329
1330
Guido van Rossum81daa321993-05-20 14:24:46 +00001331/* Instance method objects are used for two purposes:
1332 (a) as bound instance methods (returned by instancename.methodname)
1333 (b) as unbound methods (returned by ClassName.methodname)
1334 In case (b), im_self is NULL
1335*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001336
1337typedef struct {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001338 PyObject_HEAD
1339 PyObject *im_func; /* The function implementing the method */
1340 PyObject *im_self; /* The instance it is bound to, or NULL */
1341 PyObject *im_class; /* The class that defined the method */
1342} PyMethodObject;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001343
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001344
1345static PyMethodObject *free_list;
1346
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001347PyObject *
1348PyMethod_New(func, self, class)
1349 PyObject *func;
1350 PyObject *self;
1351 PyObject *class;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001352{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001353 register PyMethodObject *im;
1354 if (!PyFunction_Check(func)) {
1355 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001356 return NULL;
1357 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001358 im = free_list;
1359 if (im != NULL) {
1360 free_list = (PyMethodObject *)(im->im_self);
1361 im->ob_type = &PyMethod_Type;
1362 _Py_NewReference(im);
1363 }
1364 else {
1365 im = PyObject_NEW(PyMethodObject, &PyMethod_Type);
1366 if (im == NULL)
1367 return NULL;
1368 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001369 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00001370 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001371 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +00001372 im->im_self = self;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001373 Py_INCREF(class);
Guido van Rossum81daa321993-05-20 14:24:46 +00001374 im->im_class = class;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001375 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001376}
1377
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001378PyObject *
1379PyMethod_Function(im)
1380 register PyObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001381{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001382 if (!PyMethod_Check(im)) {
1383 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001384 return NULL;
1385 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001386 return ((PyMethodObject *)im)->im_func;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001387}
1388
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001389PyObject *
1390PyMethod_Self(im)
1391 register PyObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001392{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001393 if (!PyMethod_Check(im)) {
1394 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001395 return NULL;
1396 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001397 return ((PyMethodObject *)im)->im_self;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001398}
1399
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001400PyObject *
1401PyMethod_Class(im)
1402 register PyObject *im;
Guido van Rossum81daa321993-05-20 14:24:46 +00001403{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001404 if (!PyMethod_Check(im)) {
1405 PyErr_BadInternalCall();
Guido van Rossum81daa321993-05-20 14:24:46 +00001406 return NULL;
1407 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001408 return ((PyMethodObject *)im)->im_class;
Guido van Rossum81daa321993-05-20 14:24:46 +00001409}
1410
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001411/* Class method methods */
1412
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001413#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001414
Guido van Rossume8122f11991-05-05 20:03:07 +00001415static struct memberlist instancemethod_memberlist[] = {
1416 {"im_func", T_OBJECT, OFF(im_func)},
1417 {"im_self", T_OBJECT, OFF(im_self)},
Guido van Rossum81daa321993-05-20 14:24:46 +00001418 {"im_class", T_OBJECT, OFF(im_class)},
Guido van Rossum6dabc981996-05-14 21:54:20 +00001419 /* Dummies that are not handled by getattr() except for __members__ */
1420 {"__doc__", T_INT, 0},
1421 {"__name__", T_INT, 0},
Guido van Rossum3f5da241990-12-20 15:06:42 +00001422 {NULL} /* Sentinel */
1423};
1424
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001425static PyObject *
Guido van Rossume8122f11991-05-05 20:03:07 +00001426instancemethod_getattr(im, name)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001427 register PyMethodObject *im;
1428 PyObject *name;
Guido van Rossum3f5da241990-12-20 15:06:42 +00001429{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001430 char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +00001431 if (sname[0] == '_') {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001432 PyFunctionObject *func = (PyFunctionObject *)(im->im_func);
Guido van Rossum2878a691996-08-09 20:53:24 +00001433 if (strcmp(sname, "__name__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434 Py_INCREF(func->func_name);
Guido van Rossum6dabc981996-05-14 21:54:20 +00001435 return func->func_name;
1436 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001437 if (strcmp(sname, "__doc__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001438 Py_INCREF(func->func_doc);
Guido van Rossum6dabc981996-05-14 21:54:20 +00001439 return func->func_doc;
1440 }
1441 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001442 if (PyEval_GetRestricted()) {
1443 PyErr_SetString(PyExc_RuntimeError,
1444 "instance-method attributes not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +00001445 return NULL;
1446 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001447 return PyMember_Get((char *)im, instancemethod_memberlist, sname);
Guido van Rossum3f5da241990-12-20 15:06:42 +00001448}
1449
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001450static void
Guido van Rossume8122f11991-05-05 20:03:07 +00001451instancemethod_dealloc(im)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001452 register PyMethodObject *im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001453{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001454 Py_DECREF(im->im_func);
1455 Py_XDECREF(im->im_self);
1456 Py_DECREF(im->im_class);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001457 im->im_self = (PyObject *)free_list;
1458 free_list = im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001459}
1460
Guido van Rossumebc8c511992-09-03 20:39:51 +00001461static int
1462instancemethod_compare(a, b)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001463 PyMethodObject *a, *b;
Guido van Rossumebc8c511992-09-03 20:39:51 +00001464{
Guido van Rossume9df7271995-04-06 14:46:51 +00001465 if (a->im_self != b->im_self)
1466 return (a->im_self < b->im_self) ? -1 : 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001467 return PyObject_Compare(a->im_func, b->im_func);
Guido van Rossumebc8c511992-09-03 20:39:51 +00001468}
1469
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001470static PyObject *
Guido van Rossum25831651993-05-19 14:50:45 +00001471instancemethod_repr(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001472 PyMethodObject *a;
Guido van Rossum25831651993-05-19 14:50:45 +00001473{
1474 char buf[240];
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001475 PyInstanceObject *self = (PyInstanceObject *)(a->im_self);
1476 PyFunctionObject *func = (PyFunctionObject *)(a->im_func);
1477 PyClassObject *class = (PyClassObject *)(a->im_class);
1478 PyObject *fclassname, *iclassname, *funcname;
Guido van Rossum81daa321993-05-20 14:24:46 +00001479 char *fcname, *icname, *fname;
1480 fclassname = class->cl_name;
1481 funcname = func->func_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001482 if (fclassname != NULL && PyString_Check(fclassname))
1483 fcname = PyString_AsString(fclassname);
Guido van Rossum25831651993-05-19 14:50:45 +00001484 else
Guido van Rossum81daa321993-05-20 14:24:46 +00001485 fcname = "?";
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001486 if (funcname != NULL && PyString_Check(funcname))
1487 fname = PyString_AsString(funcname);
Guido van Rossum25831651993-05-19 14:50:45 +00001488 else
1489 fname = "?";
Guido van Rossum81daa321993-05-20 14:24:46 +00001490 if (self == NULL)
1491 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname);
1492 else {
1493 iclassname = self->in_class->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001494 if (iclassname != NULL && PyString_Check(iclassname))
1495 icname = PyString_AsString(iclassname);
Guido van Rossum81daa321993-05-20 14:24:46 +00001496 else
1497 icname = "?";
1498 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>",
1499 fcname, fname, icname, (long)self);
1500 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001501 return PyString_FromString(buf);
Guido van Rossum25831651993-05-19 14:50:45 +00001502}
1503
Guido van Rossum9bfef441993-03-29 10:43:31 +00001504static long
1505instancemethod_hash(a)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001506 PyMethodObject *a;
Guido van Rossum9bfef441993-03-29 10:43:31 +00001507{
1508 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +00001509 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001510 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +00001511 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001512 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001513 if (x == -1)
1514 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001515 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +00001516 if (y == -1)
1517 return -1;
1518 return x ^ y;
1519}
1520
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521PyTypeObject PyMethod_Type = {
1522 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001523 0,
Guido van Rossum569fce71991-04-16 08:38:43 +00001524 "instance method",
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001525 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001526 0,
Guido van Rossumb6775db1994-08-01 11:34:53 +00001527 (destructor)instancemethod_dealloc, /*tp_dealloc*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001528 0, /*tp_print*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001529 0, /*tp_getattr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001530 0, /*tp_setattr*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001531 (cmpfunc)instancemethod_compare, /*tp_compare*/
1532 (reprfunc)instancemethod_repr, /*tp_repr*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001533 0, /*tp_as_number*/
1534 0, /*tp_as_sequence*/
1535 0, /*tp_as_mapping*/
Guido van Rossumb6775db1994-08-01 11:34:53 +00001536 (hashfunc)instancemethod_hash, /*tp_hash*/
Guido van Rossum2878a691996-08-09 20:53:24 +00001537 0, /*tp_call*/
1538 0, /*tp_str*/
1539 (getattrofunc)instancemethod_getattr, /*tp_getattro*/
1540 0, /*tp_setattro*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001541};
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001542
1543/* Clear out the free list */
1544
1545void
1546PyMethod_Fini()
1547{
1548 while (free_list) {
1549 PyMethodObject *v = free_list;
1550 free_list = (PyMethodObject *)(v->im_self);
1551 PyMem_DEL(v);
1552 }
1553}