blob: a38f354b2a1e35ec8a1bb69fb2fc776790caf7e4 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Class object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Guido van Rossum3f5da241990-12-20 15:06:42 +00005#include "structmember.h"
Guido van Rossum04691fc1992-08-12 15:35:34 +00006
Neil Schemenauer29bfc072001-01-04 01:43:46 +00007
Guido van Rossum52ca98a1994-09-05 07:32:29 +00008/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +00009static PyObject *class_lookup(PyClassObject *, PyObject *,
10 PyClassObject **);
11static PyObject *instance_getattr1(PyInstanceObject *, PyObject *);
12static PyObject *instance_getattr2(PyInstanceObject *, PyObject *);
Guido van Rossum52ca98a1994-09-05 07:32:29 +000013
Guido van Rossuma63eff61998-05-29 21:37:21 +000014static PyObject *getattrstr, *setattrstr, *delattrstr;
15
Fred Drake79912472000-07-09 04:06:11 +000016
Guido van Rossumc0b618a1997-05-02 03:12:38 +000017PyObject *
Fred Drake79912472000-07-09 04:06:11 +000018PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
19 /* bases is NULL or tuple of classobjects! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021 PyClassObject *op, *dummy;
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000022 static PyObject *docstr, *modstr, *namestr;
Guido van Rossum019f4241996-08-21 14:54:28 +000023 if (docstr == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +000024 docstr= PyString_InternFromString("__doc__");
Guido van Rossum019f4241996-08-21 14:54:28 +000025 if (docstr == NULL)
26 return NULL;
27 }
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000028 if (modstr == NULL) {
29 modstr= PyString_InternFromString("__module__");
30 if (modstr == NULL)
31 return NULL;
32 }
33 if (namestr == NULL) {
34 namestr= PyString_InternFromString("__name__");
35 if (namestr == NULL)
36 return NULL;
37 }
Guido van Rossum04d73c41997-10-07 14:54:11 +000038 if (name == NULL || !PyString_Check(name)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000039 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000040 "PyClass_New: name must be a string");
41 return NULL;
42 }
43 if (dict == NULL || !PyDict_Check(dict)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000044 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000045 "PyClass_New: dict must be a dictionary");
46 return NULL;
47 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000048 if (PyDict_GetItem(dict, docstr) == NULL) {
49 if (PyDict_SetItem(dict, docstr, Py_None) < 0)
Guido van Rossume7d444f1995-01-07 12:35:18 +000050 return NULL;
51 }
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000052 if (PyDict_GetItem(dict, modstr) == NULL) {
53 PyObject *globals = PyEval_GetGlobals();
54 if (globals != NULL) {
Guido van Rossum04d73c41997-10-07 14:54:11 +000055 PyObject *modname = PyDict_GetItem(globals, namestr);
56 if (modname != NULL) {
57 if (PyDict_SetItem(dict, modstr, modname) < 0)
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000058 return NULL;
59 }
60 }
61 }
Guido van Rossume2966a61991-12-10 13:53:23 +000062 if (bases == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000063 bases = PyTuple_New(0);
Guido van Rossume2966a61991-12-10 13:53:23 +000064 if (bases == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +000065 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000066 }
Guido van Rossum04d73c41997-10-07 14:54:11 +000067 else {
68 int i;
69 if (!PyTuple_Check(bases)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000070 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000071 "PyClass_New: bases must be a tuple");
72 return NULL;
73 }
74 i = PyTuple_Size(bases);
75 while (--i >= 0) {
76 if (!PyClass_Check(PyTuple_GetItem(bases, i))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000077 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000078 "PyClass_New: base must be a class");
79 return NULL;
80 }
81 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000082 Py_INCREF(bases);
Guido van Rossum04d73c41997-10-07 14:54:11 +000083 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +000084 op = PyObject_GC_New(PyClassObject, &PyClass_Type);
Guido van Rossume2966a61991-12-10 13:53:23 +000085 if (op == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000086 Py_DECREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000087 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000088 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089 op->cl_bases = bases;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000090 Py_INCREF(dict);
Guido van Rossum81daa321993-05-20 14:24:46 +000091 op->cl_dict = dict;
Guido van Rossumc0b618a1997-05-02 03:12:38 +000092 Py_XINCREF(name);
Guido van Rossum94308391991-10-20 20:11:48 +000093 op->cl_name = name;
Guido van Rossum2878a691996-08-09 20:53:24 +000094 if (getattrstr == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +000095 getattrstr = PyString_InternFromString("__getattr__");
96 setattrstr = PyString_InternFromString("__setattr__");
97 delattrstr = PyString_InternFromString("__delattr__");
Guido van Rossum2878a691996-08-09 20:53:24 +000098 }
99 op->cl_getattr = class_lookup(op, getattrstr, &dummy);
100 op->cl_setattr = class_lookup(op, setattrstr, &dummy);
101 op->cl_delattr = class_lookup(op, delattrstr, &dummy);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000102 Py_XINCREF(op->cl_getattr);
103 Py_XINCREF(op->cl_setattr);
104 Py_XINCREF(op->cl_delattr);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000105 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000106 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000107}
108
Guido van Rossumb479dc52001-09-05 22:52:50 +0000109PyObject *
110PyMethod_Function(PyObject *im)
111{
112 if (!PyMethod_Check(im)) {
113 PyErr_BadInternalCall();
114 return NULL;
115 }
116 return ((PyMethodObject *)im)->im_func;
117}
118
119PyObject *
120PyMethod_Self(PyObject *im)
121{
122 if (!PyMethod_Check(im)) {
123 PyErr_BadInternalCall();
124 return NULL;
125 }
126 return ((PyMethodObject *)im)->im_self;
127}
128
129PyObject *
130PyMethod_Class(PyObject *im)
131{
132 if (!PyMethod_Check(im)) {
133 PyErr_BadInternalCall();
134 return NULL;
135 }
136 return ((PyMethodObject *)im)->im_class;
137}
138
Tim Peters6d6c1a32001-08-02 04:15:00 +0000139static PyObject *
140class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
141{
142 PyObject *name, *bases, *dict;
143 static char *kwlist[] = {"name", "bases", "dict", 0};
144
145 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
146 &name, &bases, &dict))
147 return NULL;
148 return PyClass_New(bases, dict, name);
149}
150
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000151/* Class methods */
152
153static void
Fred Drake79912472000-07-09 04:06:11 +0000154class_dealloc(PyClassObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000155{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000156 _PyObject_GC_UNTRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000157 Py_DECREF(op->cl_bases);
158 Py_DECREF(op->cl_dict);
159 Py_XDECREF(op->cl_name);
Guido van Rossum152d8171998-08-04 14:59:16 +0000160 Py_XDECREF(op->cl_getattr);
161 Py_XDECREF(op->cl_setattr);
162 Py_XDECREF(op->cl_delattr);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000163 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000166static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000167class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass)
Guido van Rossum81daa321993-05-20 14:24:46 +0000168{
169 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000170 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
Guido van Rossum81daa321993-05-20 14:24:46 +0000171 if (value != NULL) {
172 *pclass = cp;
173 return value;
174 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000175 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000176 for (i = 0; i < n; i++) {
Guido van Rossum7cc56eb1997-09-12 20:04:46 +0000177 /* XXX What if one of the bases is not a class? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000178 PyObject *v = class_lookup(
179 (PyClassObject *)
180 PyTuple_GetItem(cp->cl_bases, i), name, pclass);
Guido van Rossum81daa321993-05-20 14:24:46 +0000181 if (v != NULL)
182 return v;
183 }
184 return NULL;
185}
186
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000187static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000188class_getattr(register PyClassObject *op, PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000190 register PyObject *v;
191 register char *sname = PyString_AsString(name);
192 PyClassObject *class;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193 descrgetfunc f;
194
Guido van Rossum2878a691996-08-09 20:53:24 +0000195 if (sname[0] == '_' && sname[1] == '_') {
196 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000197 if (PyEval_GetRestricted()) {
198 PyErr_SetString(PyExc_RuntimeError,
199 "class.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000200 return NULL;
201 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000202 Py_INCREF(op->cl_dict);
Guido van Rossum10393b11995-01-10 10:39:49 +0000203 return op->cl_dict;
204 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000205 if (strcmp(sname, "__bases__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000206 Py_INCREF(op->cl_bases);
Guido van Rossum10393b11995-01-10 10:39:49 +0000207 return op->cl_bases;
208 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000209 if (strcmp(sname, "__name__") == 0) {
Guido van Rossum10393b11995-01-10 10:39:49 +0000210 if (op->cl_name == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000211 v = Py_None;
Guido van Rossum10393b11995-01-10 10:39:49 +0000212 else
213 v = op->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000214 Py_INCREF(v);
Guido van Rossum10393b11995-01-10 10:39:49 +0000215 return v;
216 }
Guido van Rossum94308391991-10-20 20:11:48 +0000217 }
Guido van Rossum81daa321993-05-20 14:24:46 +0000218 v = class_lookup(op, name, &class);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000219 if (v == NULL) {
Fred Drake661ea262000-10-24 19:57:45 +0000220 PyErr_Format(PyExc_AttributeError,
221 "class %.50s has no attribute '%.400s'",
222 PyString_AS_STRING(op->cl_name), sname);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000223 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000224 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000225 f = v->ob_type->tp_descr_get;
226 if (f == NULL)
227 Py_INCREF(v);
228 else
229 v = f(v, (PyObject *)NULL, (PyObject *)op);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000230 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000231}
232
Guido van Rossuma63eff61998-05-29 21:37:21 +0000233static void
Fred Drake79912472000-07-09 04:06:11 +0000234set_slot(PyObject **slot, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000235{
236 PyObject *temp = *slot;
237 Py_XINCREF(v);
238 *slot = v;
239 Py_XDECREF(temp);
240}
241
Guido van Rossum7ba30431998-07-08 13:34:48 +0000242static void
Fred Drake79912472000-07-09 04:06:11 +0000243set_attr_slots(PyClassObject *c)
Guido van Rossum7ba30431998-07-08 13:34:48 +0000244{
245 PyClassObject *dummy;
246
247 set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy));
248 set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy));
249 set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy));
250}
251
Guido van Rossuma63eff61998-05-29 21:37:21 +0000252static char *
Fred Drake79912472000-07-09 04:06:11 +0000253set_dict(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000254{
Guido van Rossuma63eff61998-05-29 21:37:21 +0000255 if (v == NULL || !PyDict_Check(v))
256 return "__dict__ must be a dictionary object";
257 set_slot(&c->cl_dict, v);
Guido van Rossum7ba30431998-07-08 13:34:48 +0000258 set_attr_slots(c);
Guido van Rossuma63eff61998-05-29 21:37:21 +0000259 return "";
260}
261
262static char *
Fred Drake79912472000-07-09 04:06:11 +0000263set_bases(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000264{
Guido van Rossuma63eff61998-05-29 21:37:21 +0000265 int i, n;
266
267 if (v == NULL || !PyTuple_Check(v))
268 return "__bases__ must be a tuple object";
269 n = PyTuple_Size(v);
270 for (i = 0; i < n; i++) {
271 PyObject *x = PyTuple_GET_ITEM(v, i);
272 if (!PyClass_Check(x))
273 return "__bases__ items must be classes";
274 if (PyClass_IsSubclass(x, (PyObject *)c))
275 return "a __bases__ item causes an inheritance cycle";
276 }
277 set_slot(&c->cl_bases, v);
Guido van Rossum7ba30431998-07-08 13:34:48 +0000278 set_attr_slots(c);
Guido van Rossuma63eff61998-05-29 21:37:21 +0000279 return "";
280}
281
282static char *
Fred Drake79912472000-07-09 04:06:11 +0000283set_name(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000284{
Guido van Rossuma63eff61998-05-29 21:37:21 +0000285 if (v == NULL || !PyString_Check(v))
286 return "__name__ must be a string object";
Guido van Rossumad89bbc2000-06-28 21:57:18 +0000287 if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v))
Guido van Rossuma63eff61998-05-29 21:37:21 +0000288 return "__name__ must not contain null bytes";
289 set_slot(&c->cl_name, v);
290 return "";
291}
292
Guido van Rossum94308391991-10-20 20:11:48 +0000293static int
Fred Drake79912472000-07-09 04:06:11 +0000294class_setattr(PyClassObject *op, PyObject *name, PyObject *v)
Guido van Rossum94308391991-10-20 20:11:48 +0000295{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000296 char *sname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000297 if (PyEval_GetRestricted()) {
298 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumac21f6a1995-08-04 04:05:31 +0000299 "classes are read-only in restricted mode");
300 return -1;
301 }
Guido van Rossumb2173c31997-08-25 21:23:56 +0000302 sname = PyString_AsString(name);
303 if (sname[0] == '_' && sname[1] == '_') {
304 int n = PyString_Size(name);
305 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossuma63eff61998-05-29 21:37:21 +0000306 char *err = NULL;
307 if (strcmp(sname, "__dict__") == 0)
308 err = set_dict(op, v);
309 else if (strcmp(sname, "__bases__") == 0)
310 err = set_bases(op, v);
311 else if (strcmp(sname, "__name__") == 0)
312 err = set_name(op, v);
313 else if (strcmp(sname, "__getattr__") == 0)
314 set_slot(&op->cl_getattr, v);
315 else if (strcmp(sname, "__setattr__") == 0)
316 set_slot(&op->cl_setattr, v);
317 else if (strcmp(sname, "__delattr__") == 0)
318 set_slot(&op->cl_delattr, v);
319 /* For the last three, we fall through to update the
320 dictionary as well. */
321 if (err != NULL) {
322 if (*err == '\0')
323 return 0;
324 PyErr_SetString(PyExc_TypeError, err);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000325 return -1;
326 }
327 }
328 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000329 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000330 int rv = PyDict_DelItem(op->cl_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000331 if (rv < 0)
Fred Drake661ea262000-10-24 19:57:45 +0000332 PyErr_Format(PyExc_AttributeError,
333 "class %.50s has no attribute '%.400s'",
334 PyString_AS_STRING(op->cl_name), sname);
Guido van Rossum94472a01992-09-04 09:45:18 +0000335 return rv;
336 }
Guido van Rossum94308391991-10-20 20:11:48 +0000337 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000338 return PyDict_SetItem(op->cl_dict, name, v);
Guido van Rossum94308391991-10-20 20:11:48 +0000339}
340
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000341static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000342class_repr(PyClassObject *op)
Guido van Rossum25831651993-05-19 14:50:45 +0000343{
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000344 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
Guido van Rossum25831651993-05-19 14:50:45 +0000345 char *name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000346 if (op->cl_name == NULL || !PyString_Check(op->cl_name))
Guido van Rossum25831651993-05-19 14:50:45 +0000347 name = "?";
348 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000349 name = PyString_AsString(op->cl_name);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000350 if (mod == NULL || !PyString_Check(mod))
Barry Warsaw7ce36942001-08-24 18:34:26 +0000351 return PyString_FromFormat("<class ?.s at %p>", name, op);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000352 else
Barry Warsaw7ce36942001-08-24 18:34:26 +0000353 return PyString_FromFormat("<class %s.%s at %p>",
354 PyString_AsString(mod),
355 name, op);
Guido van Rossum25831651993-05-19 14:50:45 +0000356}
357
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000358static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000359class_str(PyClassObject *op)
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000360{
361 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
362 PyObject *name = op->cl_name;
363 PyObject *res;
364 int m, n;
365
366 if (name == NULL || !PyString_Check(name))
367 return class_repr(op);
368 if (mod == NULL || !PyString_Check(mod)) {
369 Py_INCREF(name);
370 return name;
371 }
372 m = PyString_Size(mod);
373 n = PyString_Size(name);
374 res = PyString_FromStringAndSize((char *)NULL, m+1+n);
375 if (res != NULL) {
376 char *s = PyString_AsString(res);
377 memcpy(s, PyString_AsString(mod), m);
378 s += m;
379 *s++ = '.';
380 memcpy(s, PyString_AsString(name), n);
381 }
382 return res;
383}
384
Jeremy Hylton8caad492000-06-23 14:18:11 +0000385static int
386class_traverse(PyClassObject *o, visitproc visit, void *arg)
387{
388 int err;
389 if (o->cl_bases) {
390 err = visit(o->cl_bases, arg);
391 if (err)
392 return err;
393 }
394 if (o->cl_dict) {
395 err = visit(o->cl_dict, arg);
396 if (err)
397 return err;
398 }
399 if (o->cl_name) {
400 err = visit(o->cl_name, arg);
401 if (err)
402 return err;
403 }
404 if (o->cl_getattr) {
405 err = visit(o->cl_getattr, arg);
406 if (err)
407 return err;
408 }
409 if (o->cl_setattr) {
410 err = visit(o->cl_setattr, arg);
411 if (err)
412 return err;
413 }
414 if (o->cl_delattr) {
415 err = visit(o->cl_delattr, arg);
416 if (err)
417 return err;
418 }
419 return 0;
420}
421
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000422PyTypeObject PyClass_Type = {
423 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000424 0,
425 "class",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000426 sizeof(PyClassObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000427 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000428 (destructor)class_dealloc, /* tp_dealloc */
429 0, /* tp_print */
430 0, /* tp_getattr */
431 0, /* tp_setattr */
432 0, /* tp_compare */
433 (reprfunc)class_repr, /* tp_repr */
434 0, /* tp_as_number */
435 0, /* tp_as_sequence */
436 0, /* tp_as_mapping */
437 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000438 PyInstance_New, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000439 (reprfunc)class_str, /* tp_str */
440 (getattrofunc)class_getattr, /* tp_getattro */
441 (setattrofunc)class_setattr, /* tp_setattro */
442 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000443 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000444 0, /* tp_doc */
445 (traverseproc)class_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000446 0, /* tp_clear */
447 0, /* tp_richcompare */
448 0, /* tp_weaklistoffset */
449 0, /* tp_iter */
450 0, /* tp_iternext */
451 0, /* tp_methods */
452 0, /* tp_members */
453 0, /* tp_getset */
454 0, /* tp_base */
455 0, /* tp_dict */
456 0, /* tp_descr_get */
457 0, /* tp_descr_set */
458 0, /* tp_dictoffset */
459 0, /* tp_init */
460 0, /* tp_alloc */
461 class_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000462};
463
Guido van Rossum81daa321993-05-20 14:24:46 +0000464int
Fred Drake79912472000-07-09 04:06:11 +0000465PyClass_IsSubclass(PyObject *class, PyObject *base)
Guido van Rossum81daa321993-05-20 14:24:46 +0000466{
467 int i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000468 PyClassObject *cp;
Guido van Rossum81daa321993-05-20 14:24:46 +0000469 if (class == base)
470 return 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000471 if (class == NULL || !PyClass_Check(class))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000472 return 0;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000473 cp = (PyClassObject *)class;
474 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000475 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000476 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
Guido van Rossum81daa321993-05-20 14:24:46 +0000477 return 1;
478 }
479 return 0;
480}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000481
Guido van Rossum81daa321993-05-20 14:24:46 +0000482
483/* Instance objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000484
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000485PyObject *
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000486PyInstance_NewRaw(PyObject *klass, PyObject *dict)
487{
488 PyInstanceObject *inst;
489
490 if (!PyClass_Check(klass)) {
491 PyErr_BadInternalCall();
492 return NULL;
493 }
494 if (dict == NULL) {
495 dict = PyDict_New();
496 if (dict == NULL)
497 return NULL;
498 }
499 else {
500 if (!PyDict_Check(dict)) {
501 PyErr_BadInternalCall();
502 return NULL;
503 }
504 Py_INCREF(dict);
505 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000506 inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000507 if (inst == NULL) {
508 Py_DECREF(dict);
509 return NULL;
510 }
Fred Drake4e262a92001-03-22 18:26:47 +0000511 inst->in_weakreflist = NULL;
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000512 Py_INCREF(klass);
513 inst->in_class = (PyClassObject *)klass;
514 inst->in_dict = dict;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000515 _PyObject_GC_TRACK(inst);
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000516 return (PyObject *)inst;
517}
518
519PyObject *
520PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000521{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000522 register PyInstanceObject *inst;
523 PyObject *init;
524 static PyObject *initstr;
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000525
526 inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
Guido van Rossume8122f11991-05-05 20:03:07 +0000527 if (inst == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000528 return NULL;
Guido van Rossum2878a691996-08-09 20:53:24 +0000529 if (initstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000530 initstr = PyString_InternFromString("__init__");
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000531 init = instance_getattr2(inst, initstr);
Guido van Rossum25831651993-05-19 14:50:45 +0000532 if (init == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000533 if ((arg != NULL && (!PyTuple_Check(arg) ||
534 PyTuple_Size(arg) != 0))
535 || (kw != NULL && (!PyDict_Check(kw) ||
536 PyDict_Size(kw) != 0))) {
537 PyErr_SetString(PyExc_TypeError,
Guido van Rossuma83f2701995-07-26 18:07:32 +0000538 "this constructor takes no arguments");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000540 inst = NULL;
541 }
542 }
543 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000544 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
545 Py_DECREF(init);
Guido van Rossum25831651993-05-19 14:50:45 +0000546 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000547 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000548 inst = NULL;
549 }
550 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000551 if (res != Py_None) {
552 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +0000553 "__init__() should return None");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000554 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000555 inst = NULL;
556 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 Py_DECREF(res);
Guido van Rossum25831651993-05-19 14:50:45 +0000558 }
559 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000560 return (PyObject *)inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000561}
562
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000563/* Instance methods */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000564
565static void
Fred Drake79912472000-07-09 04:06:11 +0000566instance_dealloc(register PyInstanceObject *inst)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000567{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyObject *error_type, *error_value, *error_traceback;
569 PyObject *del;
570 static PyObject *delstr;
Tim Peters6b184912000-09-17 14:40:17 +0000571#ifdef Py_REF_DEBUG
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000572 extern long _Py_RefTotal;
Skip Montanaro4ca150b2000-07-08 12:04:57 +0000573#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000574 _PyObject_GC_UNTRACK(inst);
Fred Drakeb60654b2001-02-26 18:56:37 +0000575 PyObject_ClearWeakRefs((PyObject *) inst);
Fred Drake41deb1e2001-02-01 05:27:45 +0000576
Tim Peters6b184912000-09-17 14:40:17 +0000577 /* Temporarily resurrect the object. */
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000578#ifdef Py_TRACE_REFS
Tim Peters6b184912000-09-17 14:40:17 +0000579#ifndef Py_REF_DEBUG
580# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
581#endif
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000582 /* much too complicated if Py_TRACE_REFS defined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000583 inst->ob_type = &PyInstance_Type;
Guido van Rossumbffd6832000-01-20 22:32:56 +0000584 _Py_NewReference((PyObject *)inst);
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000585#ifdef COUNT_ALLOCS
Tim Peters6b184912000-09-17 14:40:17 +0000586 /* compensate for boost in _Py_NewReference; note that
587 * _Py_RefTotal was also boosted; we'll knock that down later.
588 */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000589 inst->ob_type->tp_allocs--;
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000590#endif
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000591#else /* !Py_TRACE_REFS */
Tim Peters6b184912000-09-17 14:40:17 +0000592 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000593 Py_INCREF(inst);
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000594#endif /* !Py_TRACE_REFS */
Tim Peters6b184912000-09-17 14:40:17 +0000595
596 /* Save the current exception, if any. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000597 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Tim Peters6b184912000-09-17 14:40:17 +0000598 /* Execute __del__ method, if any. */
Guido van Rossum2878a691996-08-09 20:53:24 +0000599 if (delstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000600 delstr = PyString_InternFromString("__del__");
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000601 if ((del = instance_getattr2(inst, delstr)) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000602 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
Tim Peters6b184912000-09-17 14:40:17 +0000603 if (res == NULL)
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000604 PyErr_WriteUnraisable(del);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000605 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000606 Py_DECREF(res);
607 Py_DECREF(del);
Guido van Rossum25831651993-05-19 14:50:45 +0000608 }
Tim Peters6b184912000-09-17 14:40:17 +0000609 /* Restore the saved exception. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000610 PyErr_Restore(error_type, error_value, error_traceback);
Tim Peters6b184912000-09-17 14:40:17 +0000611 /* Undo the temporary resurrection; can't use DECREF here, it would
612 * cause a recursive call.
613 */
614#ifdef Py_REF_DEBUG
615 /* _Py_RefTotal was boosted either by _Py_NewReference or
616 * Py_INCREF above.
617 */
618 _Py_RefTotal--;
619#endif
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000620 if (--inst->ob_refcnt > 0) {
621#ifdef COUNT_ALLOCS
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622 inst->ob_type->tp_frees--;
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000623#endif
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000624 _PyObject_GC_TRACK(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000625 return; /* __del__ added a reference; don't delete now */
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000626 }
Guido van Rossum441e4ab1996-05-23 22:46:51 +0000627#ifdef Py_TRACE_REFS
Guido van Rossumbffd6832000-01-20 22:32:56 +0000628 _Py_ForgetReference((PyObject *)inst);
Tim Peters6b184912000-09-17 14:40:17 +0000629#ifdef COUNT_ALLOCS
630 /* compensate for increment in _Py_ForgetReference */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631 inst->ob_type->tp_frees--;
Tim Peters6b184912000-09-17 14:40:17 +0000632#endif
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000633#ifndef WITH_CYCLE_GC
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000634 inst->ob_type = NULL;
Jeremy Hyltonc5007aa2000-06-30 05:02:53 +0000635#endif
Tim Peters6b184912000-09-17 14:40:17 +0000636#endif
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000637 Py_DECREF(inst->in_class);
638 Py_XDECREF(inst->in_dict);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000639 PyObject_GC_Del(inst);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000640}
641
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000643instance_getattr1(register PyInstanceObject *inst, PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000644{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000645 register PyObject *v;
646 register char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000647 if (sname[0] == '_' && sname[1] == '_') {
648 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000649 if (PyEval_GetRestricted()) {
650 PyErr_SetString(PyExc_RuntimeError,
651 "instance.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000652 return NULL;
653 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000654 Py_INCREF(inst->in_dict);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000655 return inst->in_dict;
656 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000657 if (strcmp(sname, "__class__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000658 Py_INCREF(inst->in_class);
659 return (PyObject *)inst->in_class;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000660 }
Guido van Rossum94308391991-10-20 20:11:48 +0000661 }
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000662 v = instance_getattr2(inst, name);
663 if (v == NULL) {
Fred Drake661ea262000-10-24 19:57:45 +0000664 PyErr_Format(PyExc_AttributeError,
665 "%.50s instance has no attribute '%.400s'",
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000666 PyString_AS_STRING(inst->in_class->cl_name), sname);
667 }
668 return v;
669}
670
671static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000672instance_getattr2(register PyInstanceObject *inst, PyObject *name)
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000673{
674 register PyObject *v;
675 PyClassObject *class;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676 descrgetfunc f;
677
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000678 v = PyDict_GetItem(inst->in_dict, name);
Guido van Rossumf23c41d2001-08-17 13:43:27 +0000679 if (v != NULL) {
680 Py_INCREF(v);
681 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000682 }
Guido van Rossumf23c41d2001-08-17 13:43:27 +0000683 v = class_lookup(inst->in_class, name, &class);
684 if (v != NULL) {
685 Py_INCREF(v);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000686 f = v->ob_type->tp_descr_get;
687 if (f != NULL) {
688 PyObject *w = f(v, (PyObject *)inst,
689 (PyObject *)(inst->in_class));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000690 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000691 v = w;
692 }
Guido van Rossumb3f72581993-05-21 19:56:10 +0000693 }
694 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000695}
696
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000697static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000698instance_getattr(register PyInstanceObject *inst, PyObject *name)
Guido van Rossume7737541994-09-05 07:31:41 +0000699{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000700 register PyObject *func, *res;
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000701 res = instance_getattr1(inst, name);
702 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000703 PyObject *args;
704 PyErr_Clear();
705 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000706 if (args == NULL)
Guido van Rossume7737541994-09-05 07:31:41 +0000707 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000708 res = PyEval_CallObject(func, args);
709 Py_DECREF(args);
Guido van Rossume7737541994-09-05 07:31:41 +0000710 }
711 return res;
712}
713
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000714static int
Fred Drake79912472000-07-09 04:06:11 +0000715instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000716{
Guido van Rossum94472a01992-09-04 09:45:18 +0000717 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000718 int rv = PyDict_DelItem(inst->in_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000719 if (rv < 0)
Fred Drake661ea262000-10-24 19:57:45 +0000720 PyErr_Format(PyExc_AttributeError,
721 "%.50s instance has no attribute '%.400s'",
722 PyString_AS_STRING(inst->in_class->cl_name),
723 PyString_AS_STRING(name));
Guido van Rossum94472a01992-09-04 09:45:18 +0000724 return rv;
725 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000726 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000727 return PyDict_SetItem(inst->in_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000728}
729
Guido van Rossume7737541994-09-05 07:31:41 +0000730static int
Fred Drake79912472000-07-09 04:06:11 +0000731instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
Guido van Rossume7737541994-09-05 07:31:41 +0000732{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000733 PyObject *func, *args, *res, *tmp;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000734 char *sname = PyString_AsString(name);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000735 if (sname[0] == '_' && sname[1] == '_') {
736 int n = PyString_Size(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000737 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossumb2173c31997-08-25 21:23:56 +0000738 if (strcmp(sname, "__dict__") == 0) {
739 if (PyEval_GetRestricted()) {
740 PyErr_SetString(PyExc_RuntimeError,
741 "__dict__ not accessible in restricted mode");
742 return -1;
743 }
744 if (v == NULL || !PyDict_Check(v)) {
745 PyErr_SetString(PyExc_TypeError,
746 "__dict__ must be set to a dictionary");
747 return -1;
748 }
749 tmp = inst->in_dict;
750 Py_INCREF(v);
751 inst->in_dict = v;
752 Py_DECREF(tmp);
753 return 0;
754 }
755 if (strcmp(sname, "__class__") == 0) {
756 if (PyEval_GetRestricted()) {
757 PyErr_SetString(PyExc_RuntimeError,
758 "__class__ not accessible in restricted mode");
759 return -1;
760 }
761 if (v == NULL || !PyClass_Check(v)) {
762 PyErr_SetString(PyExc_TypeError,
763 "__class__ must be set to a class");
764 return -1;
765 }
766 tmp = (PyObject *)(inst->in_class);
767 Py_INCREF(v);
768 inst->in_class = (PyClassObject *)v;
769 Py_DECREF(tmp);
770 return 0;
771 }
Guido van Rossume7737541994-09-05 07:31:41 +0000772 }
Guido van Rossume7737541994-09-05 07:31:41 +0000773 }
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000774 if (v == NULL)
775 func = inst->in_class->cl_delattr;
776 else
777 func = inst->in_class->cl_setattr;
778 if (func == NULL)
779 return instance_setattr1(inst, name, v);
780 if (v == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000781 args = Py_BuildValue("(OO)", inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000782 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000783 args = Py_BuildValue("(OOO)", inst, name, v);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000784 if (args == NULL)
785 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000786 res = PyEval_CallObject(func, args);
787 Py_DECREF(args);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000788 if (res == NULL)
789 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000790 Py_DECREF(res);
Guido van Rossume7737541994-09-05 07:31:41 +0000791 return 0;
792}
793
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000795instance_repr(PyInstanceObject *inst)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000796{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000797 PyObject *func;
798 PyObject *res;
799 static PyObject *reprstr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000800
Guido van Rossum2878a691996-08-09 20:53:24 +0000801 if (reprstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000802 reprstr = PyString_InternFromString("__repr__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000803 func = instance_getattr(inst, reprstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000804 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000805 PyObject *classname = inst->in_class->cl_name;
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000806 PyObject *mod = PyDict_GetItemString(
807 inst->in_class->cl_dict, "__module__");
Guido van Rossum25831651993-05-19 14:50:45 +0000808 char *cname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000809 if (classname != NULL && PyString_Check(classname))
810 cname = PyString_AsString(classname);
Guido van Rossum25831651993-05-19 14:50:45 +0000811 else
812 cname = "?";
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000813 PyErr_Clear();
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000814 if (mod == NULL || !PyString_Check(mod))
Barry Warsaw7ce36942001-08-24 18:34:26 +0000815 return PyString_FromFormat("<?.%s instance at %p>",
816 cname, inst);
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000817 else
Barry Warsaw7ce36942001-08-24 18:34:26 +0000818 return PyString_FromFormat("<%s.%s instance at %p>",
819 PyString_AsString(mod),
820 cname, inst);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000821 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000822 res = PyEval_CallObject(func, (PyObject *)NULL);
823 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000824 return res;
825}
826
Guido van Rossum82c690f2001-04-30 14:39:18 +0000827static PyObject *
828instance_str(PyInstanceObject *inst)
829{
830 PyObject *func;
831 PyObject *res;
832 static PyObject *strstr;
833
834 if (strstr == NULL)
835 strstr = PyString_InternFromString("__str__");
836 func = instance_getattr(inst, strstr);
837 if (func == NULL) {
838 PyErr_Clear();
839 return instance_repr(inst);
840 }
841 res = PyEval_CallObject(func, (PyObject *)NULL);
842 Py_DECREF(func);
843 return res;
844}
845
Guido van Rossum9bfef441993-03-29 10:43:31 +0000846static long
Fred Drake79912472000-07-09 04:06:11 +0000847instance_hash(PyInstanceObject *inst)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000848{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000849 PyObject *func;
850 PyObject *res;
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000851 long outcome;
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000852 static PyObject *hashstr, *eqstr, *cmpstr;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000853
Guido van Rossum2878a691996-08-09 20:53:24 +0000854 if (hashstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000855 hashstr = PyString_InternFromString("__hash__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000856 func = instance_getattr(inst, hashstr);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000857 if (func == NULL) {
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000858 /* If there is no __eq__ and no __cmp__ method, we hash on the
859 address. If an __eq__ or __cmp__ method exists, there must
860 be a __hash__. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000861 PyErr_Clear();
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000862 if (eqstr == NULL)
863 eqstr = PyString_InternFromString("__eq__");
864 func = instance_getattr(inst, eqstr);
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000865 if (func == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866 PyErr_Clear();
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000867 if (cmpstr == NULL)
868 cmpstr = PyString_InternFromString("__cmp__");
869 func = instance_getattr(inst, cmpstr);
870 if (func == NULL) {
871 PyErr_Clear();
872 return _Py_HashPointer(inst);
873 }
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000874 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000875 PyErr_SetString(PyExc_TypeError, "unhashable instance");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000876 return -1;
877 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000878 res = PyEval_CallObject(func, (PyObject *)NULL);
879 Py_DECREF(func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000880 if (res == NULL)
881 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000882 if (PyInt_Check(res)) {
883 outcome = PyInt_AsLong(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000884 if (outcome == -1)
885 outcome = -2;
886 }
887 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000888 PyErr_SetString(PyExc_TypeError,
889 "__hash__() should return an int");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000890 outcome = -1;
891 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000892 Py_DECREF(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000893 return outcome;
894}
895
Jeremy Hylton8caad492000-06-23 14:18:11 +0000896static int
897instance_traverse(PyInstanceObject *o, visitproc visit, void *arg)
898{
899 int err;
900 if (o->in_class) {
901 err = visit((PyObject *)(o->in_class), arg);
902 if (err)
903 return err;
904 }
905 if (o->in_dict) {
906 err = visit(o->in_dict, arg);
907 if (err)
908 return err;
909 }
Jeremy Hyltond22162b2000-06-23 17:14:56 +0000910 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +0000911}
912
Guido van Rossum213c7a62001-04-23 14:08:49 +0000913static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
914static PyObject *iterstr, *nextstr;
Guido van Rossum2878a691996-08-09 20:53:24 +0000915
Guido van Rossum9bfef441993-03-29 10:43:31 +0000916static int
Fred Drake79912472000-07-09 04:06:11 +0000917instance_length(PyInstanceObject *inst)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000918{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000919 PyObject *func;
920 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000921 int outcome;
922
Guido van Rossum2878a691996-08-09 20:53:24 +0000923 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000924 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000925 func = instance_getattr(inst, lenstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000926 if (func == NULL)
927 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000928 res = PyEval_CallObject(func, (PyObject *)NULL);
929 Py_DECREF(func);
Guido van Rossumd014ea61992-11-26 10:30:26 +0000930 if (res == NULL)
931 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000932 if (PyInt_Check(res)) {
933 outcome = PyInt_AsLong(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000934 if (outcome < 0)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000935 PyErr_SetString(PyExc_ValueError,
936 "__len__() should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000937 }
938 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000939 PyErr_SetString(PyExc_TypeError,
940 "__len__() should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +0000941 outcome = -1;
942 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000943 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000944 return outcome;
945}
946
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000947static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000948instance_subscript(PyInstanceObject *inst, PyObject *key)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000949{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000950 PyObject *func;
951 PyObject *arg;
952 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000953
Guido van Rossum2878a691996-08-09 20:53:24 +0000954 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000955 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000956 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000957 if (func == NULL)
958 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000959 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000960 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000961 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000962 return NULL;
963 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000964 res = PyEval_CallObject(func, arg);
965 Py_DECREF(func);
966 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000967 return res;
968}
969
Guido van Rossum9bfef441993-03-29 10:43:31 +0000970static int
Fred Drake79912472000-07-09 04:06:11 +0000971instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000972{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000973 PyObject *func;
974 PyObject *arg;
975 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000976
Guido van Rossum2878a691996-08-09 20:53:24 +0000977 if (value == NULL) {
978 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000979 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000980 func = instance_getattr(inst, delitemstr);
981 }
982 else {
983 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +0000984 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +0000985 func = instance_getattr(inst, setitemstr);
986 }
Guido van Rossum04691fc1992-08-12 15:35:34 +0000987 if (func == NULL)
988 return -1;
989 if (value == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 arg = Py_BuildValue("(O)", key);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000991 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000992 arg = Py_BuildValue("(OO)", key, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000993 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +0000995 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000996 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000997 res = PyEval_CallObject(func, arg);
998 Py_DECREF(func);
999 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001000 if (res == NULL)
1001 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001002 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001003 return 0;
1004}
1005
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001006static PyMappingMethods instance_as_mapping = {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001007 (inquiry)instance_length, /* mp_length */
1008 (binaryfunc)instance_subscript, /* mp_subscript */
1009 (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001010};
1011
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001013instance_item(PyInstanceObject *inst, int i)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001014{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001015 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001016
Guido van Rossum2878a691996-08-09 20:53:24 +00001017 if (getitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001018 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001019 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001020 if (func == NULL)
1021 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001022 arg = Py_BuildValue("(i)", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001023 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001025 return NULL;
1026 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001027 res = PyEval_CallObject(func, arg);
1028 Py_DECREF(func);
1029 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001030 return res;
1031}
1032
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001033static PyObject *
Thomas Wouters1d75a792000-08-17 22:37:32 +00001034sliceobj_from_intint(int i, int j)
1035{
1036 PyObject *start, *end, *res;
1037
1038 start = PyInt_FromLong((long)i);
1039 if (!start)
1040 return NULL;
1041
1042 end = PyInt_FromLong((long)j);
1043 if (!end) {
1044 Py_DECREF(start);
1045 return NULL;
1046 }
1047 res = PySlice_New(start, end, NULL);
1048 Py_DECREF(start);
1049 Py_DECREF(end);
1050 return res;
1051}
1052
1053
1054static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001055instance_slice(PyInstanceObject *inst, int i, int j)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001056{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001057 PyObject *func, *arg, *res;
1058 static PyObject *getslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001059
Guido van Rossum2878a691996-08-09 20:53:24 +00001060 if (getslicestr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001061 getslicestr = PyString_InternFromString("__getslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001062 func = instance_getattr(inst, getslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001063
1064 if (func == NULL) {
1065 PyErr_Clear();
1066
1067 if (getitemstr == NULL)
1068 getitemstr = PyString_InternFromString("__getitem__");
1069 func = instance_getattr(inst, getitemstr);
1070 if (func == NULL)
1071 return NULL;
1072 arg = Py_BuildValue("(N)", sliceobj_from_intint(i, j));
1073 } else
1074 arg = Py_BuildValue("(ii)", i, j);
1075
Guido van Rossum04691fc1992-08-12 15:35:34 +00001076 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001077 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001078 return NULL;
1079 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001080 res = PyEval_CallObject(func, arg);
1081 Py_DECREF(func);
1082 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001083 return res;
1084}
1085
1086static int
Fred Drake79912472000-07-09 04:06:11 +00001087instance_ass_item(PyInstanceObject *inst, int i, PyObject *item)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001088{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001089 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001090
Guido van Rossum2878a691996-08-09 20:53:24 +00001091 if (item == NULL) {
1092 if (delitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001093 delitemstr = PyString_InternFromString("__delitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001094 func = instance_getattr(inst, delitemstr);
1095 }
1096 else {
1097 if (setitemstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001098 setitemstr = PyString_InternFromString("__setitem__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001099 func = instance_getattr(inst, setitemstr);
1100 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001101 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +00001102 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001103 if (item == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001104 arg = Py_BuildValue("i", i);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001105 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001106 arg = Py_BuildValue("(iO)", i, item);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001107 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001108 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001109 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001110 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001111 res = PyEval_CallObject(func, arg);
1112 Py_DECREF(func);
1113 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001114 if (res == NULL)
1115 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001116 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001117 return 0;
1118}
1119
1120static int
Fred Drake79912472000-07-09 04:06:11 +00001121instance_ass_slice(PyInstanceObject *inst, int i, int j, PyObject *value)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001122{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001123 PyObject *func, *arg, *res;
1124 static PyObject *setslicestr, *delslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001125
Guido van Rossum2878a691996-08-09 20:53:24 +00001126 if (value == NULL) {
1127 if (delslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001128 delslicestr =
1129 PyString_InternFromString("__delslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001130 func = instance_getattr(inst, delslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001131 if (func == NULL) {
1132 PyErr_Clear();
1133 if (delitemstr == NULL)
1134 delitemstr =
1135 PyString_InternFromString("__delitem__");
1136 func = instance_getattr(inst, delitemstr);
1137 if (func == NULL)
1138 return -1;
1139
1140 arg = Py_BuildValue("(N)",
1141 sliceobj_from_intint(i, j));
1142 } else
1143 arg = Py_BuildValue("(ii)", i, j);
Guido van Rossum2878a691996-08-09 20:53:24 +00001144 }
1145 else {
1146 if (setslicestr == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001147 setslicestr =
1148 PyString_InternFromString("__setslice__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001149 func = instance_getattr(inst, setslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001150 if (func == NULL) {
1151 PyErr_Clear();
1152 if (setitemstr == NULL)
1153 setitemstr =
1154 PyString_InternFromString("__setitem__");
1155 func = instance_getattr(inst, setitemstr);
1156 if (func == NULL)
1157 return -1;
1158
1159 arg = Py_BuildValue("(NO)",
1160 sliceobj_from_intint(i, j), value);
1161 } else
1162 arg = Py_BuildValue("(iiO)", i, j, value);
Guido van Rossum2878a691996-08-09 20:53:24 +00001163 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001164 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001165 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001166 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001167 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001168 res = PyEval_CallObject(func, arg);
1169 Py_DECREF(func);
1170 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001171 if (res == NULL)
1172 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001173 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001174 return 0;
1175}
1176
Tim Peterscb8d3682001-05-05 21:05:01 +00001177static int
1178instance_contains(PyInstanceObject *inst, PyObject *member)
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001179{
1180 static PyObject *__contains__;
Tim Peterscb8d3682001-05-05 21:05:01 +00001181 PyObject *func;
1182
1183 /* Try __contains__ first.
1184 * If that can't be done, try iterator-based searching.
1185 */
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001186
1187 if(__contains__ == NULL) {
1188 __contains__ = PyString_InternFromString("__contains__");
1189 if(__contains__ == NULL)
1190 return -1;
1191 }
1192 func = instance_getattr(inst, __contains__);
Tim Peterscb8d3682001-05-05 21:05:01 +00001193 if (func) {
1194 PyObject *res;
1195 int ret;
1196 PyObject *arg = Py_BuildValue("(O)", member);
1197 if(arg == NULL) {
1198 Py_DECREF(func);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001199 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001200 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001201 res = PyEval_CallObject(func, arg);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001202 Py_DECREF(func);
Tim Peterscb8d3682001-05-05 21:05:01 +00001203 Py_DECREF(arg);
1204 if(res == NULL)
1205 return -1;
1206 ret = PyObject_IsTrue(res);
1207 Py_DECREF(res);
1208 return ret;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001209 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001210
1211 /* Couldn't find __contains__. */
1212 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
1213 /* Assume the failure was simply due to that there is no
1214 * __contains__ attribute, and try iterating instead.
1215 */
1216 PyErr_Clear();
1217 return _PySequence_IterContains((PyObject *)inst, member);
1218 }
1219 else
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001220 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001221}
1222
Fred Drake79912472000-07-09 04:06:11 +00001223static PySequenceMethods
1224instance_as_sequence = {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001225 (inquiry)instance_length, /* sq_length */
1226 0, /* sq_concat */
1227 0, /* sq_repeat */
1228 (intargfunc)instance_item, /* sq_item */
1229 (intintargfunc)instance_slice, /* sq_slice */
1230 (intobjargproc)instance_ass_item, /* sq_ass_item */
1231 (intintobjargproc)instance_ass_slice, /* sq_ass_slice */
1232 (objobjproc)instance_contains, /* sq_contains */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001233};
1234
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001235static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001236generic_unary_op(PyInstanceObject *self, PyObject *methodname)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001237{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001239
1240 if ((func = instance_getattr(self, methodname)) == NULL)
1241 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001242 res = PyEval_CallObject(func, (PyObject *)NULL);
1243 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001244 return res;
1245}
1246
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001247static PyObject *
1248generic_binary_op(PyObject *v, PyObject *w, char *opname)
Guido van Rossum03093a21994-09-28 15:51:32 +00001249{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001250 PyObject *result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001251 PyObject *args;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001252 PyObject *func = PyObject_GetAttrString(v, opname);
Guido van Rossume7d444f1995-01-07 12:35:18 +00001253 if (func == NULL) {
Guido van Rossum617c1b01998-05-28 19:50:02 +00001254 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001255 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001256 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001257 Py_INCREF(Py_NotImplemented);
1258 return Py_NotImplemented;
Guido van Rossume7d444f1995-01-07 12:35:18 +00001259 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001260 args = Py_BuildValue("(O)", w);
Guido van Rossum03093a21994-09-28 15:51:32 +00001261 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001262 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001263 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +00001264 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001265 result = PyEval_CallObject(func, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001266 Py_DECREF(args);
1267 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001268 return result;
1269}
1270
1271
1272static PyObject *coerce_obj;
1273
1274/* Try one half of a binary operator involving a class instance. */
1275static PyObject *
1276half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
1277 int swapped)
1278{
1279 PyObject *args;
1280 PyObject *coercefunc;
1281 PyObject *coerced = NULL;
1282 PyObject *v1;
1283 PyObject *result;
1284
1285 if (!PyInstance_Check(v)) {
1286 Py_INCREF(Py_NotImplemented);
1287 return Py_NotImplemented;
1288 }
1289
1290 if (coerce_obj == NULL) {
1291 coerce_obj = PyString_InternFromString("__coerce__");
1292 if (coerce_obj == NULL)
1293 return NULL;
1294 }
1295 coercefunc = PyObject_GetAttr(v, coerce_obj);
1296 if (coercefunc == NULL) {
1297 PyErr_Clear();
1298 return generic_binary_op(v, w, opname);
1299 }
1300
1301 args = Py_BuildValue("(O)", w);
1302 if (args == NULL) {
1303 return NULL;
1304 }
1305 coerced = PyEval_CallObject(coercefunc, args);
1306 Py_DECREF(args);
1307 Py_DECREF(coercefunc);
1308 if (coerced == NULL) {
1309 return NULL;
1310 }
1311 if (coerced == Py_None || coerced == Py_NotImplemented) {
1312 Py_DECREF(coerced);
1313 return generic_binary_op(v, w, opname);
1314 }
1315 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
1316 Py_DECREF(coerced);
1317 PyErr_SetString(PyExc_TypeError,
1318 "coercion should return None or 2-tuple");
1319 return NULL;
1320 }
1321 v1 = PyTuple_GetItem(coerced, 0);
1322 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001323 if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001324 /* prevent recursion if __coerce__ returns self as the first
1325 * argument */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001326 result = generic_binary_op(v1, w, opname);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001327 } else {
1328 if (swapped)
1329 result = (thisfunc)(w, v1);
1330 else
1331 result = (thisfunc)(v1, w);
1332 }
1333 Py_DECREF(coerced);
1334 return result;
1335}
1336
1337/* Implement a binary operator involving at least one class instance. */
1338static PyObject *
1339do_binop(PyObject *v, PyObject *w, char *opname, char *ropname,
1340 binaryfunc thisfunc)
1341{
1342 PyObject *result = half_binop(v, w, opname, thisfunc, 0);
1343 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001344 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001345 result = half_binop(w, v, ropname, thisfunc, 1);
1346 }
1347 return result;
1348}
1349
1350static PyObject *
1351do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname,
1352 char *ropname, binaryfunc thisfunc)
1353{
1354 PyObject *result = half_binop(v, w, iopname, thisfunc, 0);
1355 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001356 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001357 result = do_binop(v, w, opname, ropname, thisfunc);
1358 }
1359 return result;
Guido van Rossum03093a21994-09-28 15:51:32 +00001360}
1361
Guido van Rossum879c5811995-01-10 15:24:06 +00001362static int
Fred Drake79912472000-07-09 04:06:11 +00001363instance_coerce(PyObject **pv, PyObject **pw)
Guido van Rossum879c5811995-01-10 15:24:06 +00001364{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001365 PyObject *v = *pv;
1366 PyObject *w = *pw;
Guido van Rossum3931df91997-11-18 19:23:07 +00001367 PyObject *coercefunc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001368 PyObject *args;
1369 PyObject *coerced;
Guido van Rossum879c5811995-01-10 15:24:06 +00001370
Guido van Rossum2878a691996-08-09 20:53:24 +00001371 if (coerce_obj == NULL) {
Guido van Rossuma412d241997-01-18 07:59:12 +00001372 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001373 if (coerce_obj == NULL)
1374 return -1;
1375 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001376 coercefunc = PyObject_GetAttr(v, coerce_obj);
1377 if (coercefunc == NULL) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001378 /* No __coerce__ method */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001379 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001380 return 1;
Guido van Rossum879c5811995-01-10 15:24:06 +00001381 }
1382 /* Has __coerce__ method: call it */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001383 args = Py_BuildValue("(O)", w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001384 if (args == NULL) {
1385 return -1;
1386 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001387 coerced = PyEval_CallObject(coercefunc, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 Py_DECREF(args);
Guido van Rossum3931df91997-11-18 19:23:07 +00001389 Py_DECREF(coercefunc);
Guido van Rossum879c5811995-01-10 15:24:06 +00001390 if (coerced == NULL) {
1391 /* __coerce__ call raised an exception */
1392 return -1;
1393 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001394 if (coerced == Py_None || coerced == Py_NotImplemented) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001395 /* __coerce__ says "I can't do it" */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001396 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001397 return 1;
1398 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001399 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001400 /* __coerce__ return value is malformed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401 Py_DECREF(coerced);
1402 PyErr_SetString(PyExc_TypeError,
Guido van Rossum879c5811995-01-10 15:24:06 +00001403 "coercion should return None or 2-tuple");
1404 return -1;
1405 }
1406 /* __coerce__ returned two new values */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001407 *pv = PyTuple_GetItem(coerced, 0);
1408 *pw = PyTuple_GetItem(coerced, 1);
1409 Py_INCREF(*pv);
1410 Py_INCREF(*pw);
1411 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001412 return 0;
1413}
1414
Guido van Rossum04691fc1992-08-12 15:35:34 +00001415#define UNARY(funcname, methodname) \
Thomas Woutersc3073522000-07-23 22:09:59 +00001416static PyObject *funcname(PyInstanceObject *self) { \
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001417 static PyObject *o; \
Guido van Rossuma412d241997-01-18 07:59:12 +00001418 if (o == NULL) o = PyString_InternFromString(methodname); \
Guido van Rossum2878a691996-08-09 20:53:24 +00001419 return generic_unary_op(self, o); \
Guido van Rossum04691fc1992-08-12 15:35:34 +00001420}
1421
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001422#define BINARY(f, m, n) \
1423static PyObject *f(PyObject *v, PyObject *w) { \
1424 return do_binop(v, w, "__" m "__", "__r" m "__", n); \
1425}
1426
1427#define BINARY_INPLACE(f, m, n) \
1428static PyObject *f(PyObject *v, PyObject *w) { \
1429 return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \
1430 "__r" m "__", n); \
1431}
1432
Guido van Rossum04691fc1992-08-12 15:35:34 +00001433UNARY(instance_neg, "__neg__")
1434UNARY(instance_pos, "__pos__")
1435UNARY(instance_abs, "__abs__")
1436
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001437BINARY(instance_or, "or", PyNumber_Or)
1438BINARY(instance_and, "and", PyNumber_And)
1439BINARY(instance_xor, "xor", PyNumber_Xor)
1440BINARY(instance_lshift, "lshift", PyNumber_Lshift)
1441BINARY(instance_rshift, "rshift", PyNumber_Rshift)
1442BINARY(instance_add, "add", PyNumber_Add)
1443BINARY(instance_sub, "sub", PyNumber_Subtract)
1444BINARY(instance_mul, "mul", PyNumber_Multiply)
1445BINARY(instance_div, "div", PyNumber_Divide)
1446BINARY(instance_mod, "mod", PyNumber_Remainder)
1447BINARY(instance_divmod, "divmod", PyNumber_Divmod)
Guido van Rossum4668b002001-08-08 05:00:18 +00001448BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
1449BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001450
1451BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
1452BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
1453BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd)
1454BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift)
1455BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift)
1456BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd)
1457BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
1458BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
1459BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
1460BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
Guido van Rossum4668b002001-08-08 05:00:18 +00001461BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
1462BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001463
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001464/* Try a 3-way comparison, returning an int; v is an instance. Return:
1465 -2 for an exception;
1466 -1 if v < w;
1467 0 if v == w;
1468 1 if v > w;
1469 2 if this particular 3-way comparison is not implemented or undefined.
1470*/
1471static int
1472half_cmp(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001473{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001474 static PyObject *cmp_obj;
1475 PyObject *args;
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001476 PyObject *cmp_func;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001477 PyObject *result;
1478 long l;
1479
1480 assert(PyInstance_Check(v));
1481
1482 if (cmp_obj == NULL) {
1483 cmp_obj = PyString_InternFromString("__cmp__");
1484 if (cmp_obj == NULL)
1485 return -2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001486 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001487
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001488 cmp_func = PyObject_GetAttr(v, cmp_obj);
1489 if (cmp_func == NULL) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001490 PyErr_Clear();
1491 return 2;
1492 }
1493
1494 args = Py_BuildValue("(O)", w);
1495 if (args == NULL)
1496 return -2;
1497
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001498 result = PyEval_CallObject(cmp_func, args);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001499 Py_DECREF(args);
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001500 Py_DECREF(cmp_func);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001501
1502 if (result == NULL)
1503 return -2;
1504
1505 if (result == Py_NotImplemented) {
1506 Py_DECREF(result);
1507 return 2;
1508 }
1509
1510 l = PyInt_AsLong(result);
1511 Py_DECREF(result);
1512 if (l == -1 && PyErr_Occurred()) {
1513 PyErr_SetString(PyExc_TypeError,
1514 "comparison did not return an int");
1515 return -2;
1516 }
1517
1518 return l < 0 ? -1 : l > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001519}
1520
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001521/* Try a 3-way comparison, returning an int; either v or w is an instance.
1522 We first try a coercion. Return:
1523 -2 for an exception;
1524 -1 if v < w;
1525 0 if v == w;
1526 1 if v > w;
1527 2 if this particular 3-way comparison is not implemented or undefined.
1528 THIS IS ONLY CALLED FROM object.c!
1529*/
1530static int
1531instance_compare(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001532{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001533 int c;
1534
1535 c = PyNumber_CoerceEx(&v, &w);
1536 if (c < 0)
1537 return -2;
1538 if (c == 0) {
1539 /* If neither is now an instance, use regular comparison */
1540 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
1541 c = PyObject_Compare(v, w);
1542 Py_DECREF(v);
1543 Py_DECREF(w);
1544 if (PyErr_Occurred())
1545 return -2;
1546 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001547 }
1548 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001549 else {
1550 /* The coercion didn't do anything.
1551 Treat this the same as returning v and w unchanged. */
1552 Py_INCREF(v);
1553 Py_INCREF(w);
1554 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001555
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001556 if (PyInstance_Check(v)) {
1557 c = half_cmp(v, w);
1558 if (c <= 1) {
1559 Py_DECREF(v);
1560 Py_DECREF(w);
1561 return c;
1562 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001563 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001564 if (PyInstance_Check(w)) {
1565 c = half_cmp(w, v);
1566 if (c <= 1) {
1567 Py_DECREF(v);
1568 Py_DECREF(w);
1569 if (c >= -1)
1570 c = -c;
1571 return c;
1572 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001573 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001574 Py_DECREF(v);
1575 Py_DECREF(w);
1576 return 2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001577}
1578
Guido van Rossum9bfef441993-03-29 10:43:31 +00001579static int
Fred Drake79912472000-07-09 04:06:11 +00001580instance_nonzero(PyInstanceObject *self)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001581{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001582 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001583 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001584 static PyObject *nonzerostr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001585
Guido van Rossum2878a691996-08-09 20:53:24 +00001586 if (nonzerostr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001587 nonzerostr = PyString_InternFromString("__nonzero__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001588 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001589 PyErr_Clear();
Guido van Rossum2878a691996-08-09 20:53:24 +00001590 if (lenstr == NULL)
Guido van Rossuma412d241997-01-18 07:59:12 +00001591 lenstr = PyString_InternFromString("__len__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001592 if ((func = instance_getattr(self, lenstr)) == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001593 PyErr_Clear();
Guido van Rossum04691fc1992-08-12 15:35:34 +00001594 /* Fall back to the default behavior:
1595 all instances are nonzero */
1596 return 1;
1597 }
1598 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001599 res = PyEval_CallObject(func, (PyObject *)NULL);
1600 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001601 if (res == NULL)
1602 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001603 if (!PyInt_Check(res)) {
1604 Py_DECREF(res);
1605 PyErr_SetString(PyExc_TypeError,
1606 "__nonzero__ should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001607 return -1;
1608 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001609 outcome = PyInt_AsLong(res);
1610 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001611 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001612 PyErr_SetString(PyExc_ValueError,
1613 "__nonzero__ should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001614 return -1;
1615 }
1616 return outcome > 0;
1617}
1618
1619UNARY(instance_invert, "__invert__")
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001620UNARY(instance_int, "__int__")
1621UNARY(instance_long, "__long__")
1622UNARY(instance_float, "__float__")
1623UNARY(instance_oct, "__oct__")
1624UNARY(instance_hex, "__hex__")
1625
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001626static PyObject *
1627bin_power(PyObject *v, PyObject *w)
1628{
1629 return PyNumber_Power(v, w, Py_None);
1630}
1631
Guido van Rossum03093a21994-09-28 15:51:32 +00001632/* This version is for ternary calls only (z != None) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001633static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001634instance_pow(PyObject *v, PyObject *w, PyObject *z)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001635{
1636 if (z == Py_None) {
1637 return do_binop(v, w, "__pow__", "__rpow__", bin_power);
Guido van Rossum03093a21994-09-28 15:51:32 +00001638 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001639 else {
1640 PyObject *func;
1641 PyObject *args;
1642 PyObject *result;
1643
1644 /* XXX Doesn't do coercions... */
1645 func = PyObject_GetAttrString(v, "__pow__");
1646 if (func == NULL)
1647 return NULL;
1648 args = Py_BuildValue("(OO)", w, z);
1649 if (args == NULL) {
1650 Py_DECREF(func);
1651 return NULL;
1652 }
1653 result = PyEval_CallObject(func, args);
1654 Py_DECREF(func);
1655 Py_DECREF(args);
1656 return result;
1657 }
Guido van Rossum03093a21994-09-28 15:51:32 +00001658}
1659
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001660static PyObject *
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001661bin_inplace_power(PyObject *v, PyObject *w)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001662{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001663 return PyNumber_InPlacePower(v, w, Py_None);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001664}
1665
1666
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001667static PyObject *
1668instance_ipow(PyObject *v, PyObject *w, PyObject *z)
1669{
1670 if (z == Py_None) {
1671 return do_binop_inplace(v, w, "__ipow__", "__pow__",
1672 "__rpow__", bin_inplace_power);
1673 }
1674 else {
1675 /* XXX Doesn't do coercions... */
1676 PyObject *func;
1677 PyObject *args;
1678 PyObject *result;
1679
1680 func = PyObject_GetAttrString(v, "__ipow__");
1681 if (func == NULL) {
1682 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1683 return NULL;
1684 PyErr_Clear();
1685 return instance_pow(v, w, z);
1686 }
1687 args = Py_BuildValue("(OO)", w, z);
1688 if (args == NULL) {
1689 Py_DECREF(func);
1690 return NULL;
1691 }
1692 result = PyEval_CallObject(func, args);
1693 Py_DECREF(func);
1694 Py_DECREF(args);
1695 return result;
1696 }
1697}
1698
1699
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001700/* Map rich comparison operators to their __xx__ namesakes */
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001701#define NAME_OPS 6
1702static PyObject **name_op = NULL;
1703
1704static int
Guido van Rossum0ba9e3a2001-05-22 02:33:08 +00001705init_name_op(void)
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001706{
1707 int i;
1708 char *_name_op[] = {
1709 "__lt__",
1710 "__le__",
1711 "__eq__",
1712 "__ne__",
1713 "__gt__",
1714 "__ge__",
1715 };
1716
1717 name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS);
1718 if (name_op == NULL)
1719 return -1;
1720 for (i = 0; i < NAME_OPS; ++i) {
1721 name_op[i] = PyString_InternFromString(_name_op[i]);
1722 if (name_op[i] == NULL)
1723 return -1;
1724 }
1725 return 0;
1726}
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001727
1728static PyObject *
1729half_richcompare(PyObject *v, PyObject *w, int op)
1730{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001731 PyObject *method;
1732 PyObject *args;
1733 PyObject *res;
1734
1735 assert(PyInstance_Check(v));
1736
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001737 if (name_op == NULL) {
1738 if (init_name_op() < 0)
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001739 return NULL;
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001740 }
1741 /* If the instance doesn't define an __getattr__ method, use
1742 instance_getattr2 directly because it will not set an
1743 exception on failure. */
1744 if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) {
1745 method = instance_getattr2((PyInstanceObject *)v,
1746 name_op[op]);
1747 if (method == NULL) {
1748 assert(!PyErr_Occurred());
1749 res = Py_NotImplemented;
1750 Py_INCREF(res);
1751 return res;
1752 }
1753 } else {
1754 method = PyObject_GetAttr(v, name_op[op]);
1755 if (method == NULL) {
1756 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1757 return NULL;
1758 PyErr_Clear();
1759 res = Py_NotImplemented;
1760 Py_INCREF(res);
1761 return res;
1762 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001763 }
1764
1765 args = Py_BuildValue("(O)", w);
1766 if (args == NULL) {
1767 Py_DECREF(method);
1768 return NULL;
1769 }
1770
1771 res = PyEval_CallObject(method, args);
1772 Py_DECREF(args);
1773 Py_DECREF(method);
1774
1775 return res;
1776}
1777
1778/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
1779static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
1780
1781static PyObject *
1782instance_richcompare(PyObject *v, PyObject *w, int op)
1783{
1784 PyObject *res;
1785
1786 if (PyInstance_Check(v)) {
1787 res = half_richcompare(v, w, op);
1788 if (res != Py_NotImplemented)
1789 return res;
1790 Py_DECREF(res);
1791 }
1792
1793 if (PyInstance_Check(w)) {
1794 res = half_richcompare(w, v, swapped_op[op]);
1795 if (res != Py_NotImplemented)
1796 return res;
1797 Py_DECREF(res);
1798 }
1799
1800 Py_INCREF(Py_NotImplemented);
1801 return Py_NotImplemented;
1802}
1803
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001804
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001805/* Get the iterator */
1806static PyObject *
1807instance_getiter(PyInstanceObject *self)
1808{
1809 PyObject *func;
1810
1811 if (iterstr == NULL)
1812 iterstr = PyString_InternFromString("__iter__");
1813 if (getitemstr == NULL)
1814 getitemstr = PyString_InternFromString("__getitem__");
1815
1816 if ((func = instance_getattr(self, iterstr)) != NULL) {
1817 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
1818 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001819 if (res != NULL && !PyIter_Check(res)) {
1820 PyErr_Format(PyExc_TypeError,
1821 "__iter__ returned non-iterator "
1822 "of type '%.100s'",
1823 res->ob_type->tp_name);
1824 Py_DECREF(res);
1825 res = NULL;
1826 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001827 return res;
1828 }
1829 PyErr_Clear();
1830 if ((func = instance_getattr(self, getitemstr)) == NULL) {
1831 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
1832 return NULL;
1833 }
1834 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00001835 return PySeqIter_New((PyObject *)self);
1836}
1837
1838
1839/* Call the iterator's next */
1840static PyObject *
1841instance_iternext(PyInstanceObject *self)
1842{
1843 PyObject *func;
1844
1845 if (nextstr == NULL)
1846 nextstr = PyString_InternFromString("next");
1847
1848 if ((func = instance_getattr(self, nextstr)) != NULL) {
1849 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
1850 Py_DECREF(func);
1851 if (res != NULL) {
1852 return res;
1853 }
1854 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
1855 PyErr_Clear();
1856 return NULL;
1857 }
1858 return NULL;
1859 }
1860 PyErr_SetString(PyExc_TypeError, "instance has no next() method");
1861 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001862}
1863
Tim Peters6d6c1a32001-08-02 04:15:00 +00001864static PyObject *
1865instance_call(PyObject *func, PyObject *arg, PyObject *kw)
1866{
1867 PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
1868 if (call == NULL) {
1869 PyInstanceObject *inst = (PyInstanceObject*) func;
1870 PyErr_Clear();
1871 PyErr_Format(PyExc_AttributeError,
1872 "%.200s instance has no __call__ method",
1873 PyString_AsString(inst->in_class->cl_name));
1874 return NULL;
1875 }
1876 res = PyObject_Call(call, arg, kw);
1877 Py_DECREF(call);
1878 return res;
1879}
1880
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001881
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001882static PyNumberMethods instance_as_number = {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001883 (binaryfunc)instance_add, /* nb_add */
1884 (binaryfunc)instance_sub, /* nb_subtract */
1885 (binaryfunc)instance_mul, /* nb_multiply */
1886 (binaryfunc)instance_div, /* nb_divide */
1887 (binaryfunc)instance_mod, /* nb_remainder */
1888 (binaryfunc)instance_divmod, /* nb_divmod */
1889 (ternaryfunc)instance_pow, /* nb_power */
1890 (unaryfunc)instance_neg, /* nb_negative */
1891 (unaryfunc)instance_pos, /* nb_positive */
1892 (unaryfunc)instance_abs, /* nb_absolute */
1893 (inquiry)instance_nonzero, /* nb_nonzero */
1894 (unaryfunc)instance_invert, /* nb_invert */
1895 (binaryfunc)instance_lshift, /* nb_lshift */
1896 (binaryfunc)instance_rshift, /* nb_rshift */
1897 (binaryfunc)instance_and, /* nb_and */
1898 (binaryfunc)instance_xor, /* nb_xor */
1899 (binaryfunc)instance_or, /* nb_or */
1900 (coercion)instance_coerce, /* nb_coerce */
1901 (unaryfunc)instance_int, /* nb_int */
1902 (unaryfunc)instance_long, /* nb_long */
1903 (unaryfunc)instance_float, /* nb_float */
1904 (unaryfunc)instance_oct, /* nb_oct */
1905 (unaryfunc)instance_hex, /* nb_hex */
1906 (binaryfunc)instance_iadd, /* nb_inplace_add */
1907 (binaryfunc)instance_isub, /* nb_inplace_subtract */
1908 (binaryfunc)instance_imul, /* nb_inplace_multiply */
1909 (binaryfunc)instance_idiv, /* nb_inplace_divide */
1910 (binaryfunc)instance_imod, /* nb_inplace_remainder */
1911 (ternaryfunc)instance_ipow, /* nb_inplace_power */
1912 (binaryfunc)instance_ilshift, /* nb_inplace_lshift */
1913 (binaryfunc)instance_irshift, /* nb_inplace_rshift */
1914 (binaryfunc)instance_iand, /* nb_inplace_and */
1915 (binaryfunc)instance_ixor, /* nb_inplace_xor */
1916 (binaryfunc)instance_ior, /* nb_inplace_or */
Guido van Rossum4668b002001-08-08 05:00:18 +00001917 (binaryfunc)instance_floordiv, /* nb_floor_divide */
1918 (binaryfunc)instance_truediv, /* nb_true_divide */
1919 (binaryfunc)instance_ifloordiv, /* nb_inplace_floor_divide */
1920 (binaryfunc)instance_itruediv, /* nb_inplace_true_divide */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001921};
1922
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001923PyTypeObject PyInstance_Type = {
1924 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001925 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +00001926 "instance",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001927 sizeof(PyInstanceObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001928 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001929 (destructor)instance_dealloc, /* tp_dealloc */
1930 0, /* tp_print */
1931 0, /* tp_getattr */
1932 0, /* tp_setattr */
1933 instance_compare, /* tp_compare */
1934 (reprfunc)instance_repr, /* tp_repr */
1935 &instance_as_number, /* tp_as_number */
1936 &instance_as_sequence, /* tp_as_sequence */
1937 &instance_as_mapping, /* tp_as_mapping */
1938 (hashfunc)instance_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001939 instance_call, /* tp_call */
Guido van Rossum82c690f2001-04-30 14:39:18 +00001940 (reprfunc)instance_str, /* tp_str */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001941 (getattrofunc)instance_getattr, /* tp_getattro */
1942 (setattrofunc)instance_setattr, /* tp_setattro */
1943 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001944 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001945 0, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00001946 (traverseproc)instance_traverse, /* tp_traverse */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001947 0, /* tp_clear */
1948 instance_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00001949 offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
1950 (getiterfunc)instance_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00001951 (iternextfunc)instance_iternext, /* tp_iternext */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001952};
1953
1954
Guido van Rossum81daa321993-05-20 14:24:46 +00001955/* Instance method objects are used for two purposes:
1956 (a) as bound instance methods (returned by instancename.methodname)
1957 (b) as unbound methods (returned by ClassName.methodname)
1958 In case (b), im_self is NULL
1959*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001960
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001961static PyMethodObject *free_list;
1962
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001963PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001964PyMethod_New(PyObject *func, PyObject *self, PyObject *class)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001965{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001966 register PyMethodObject *im;
Guido van Rossum7859f871998-07-08 14:58:16 +00001967 if (!PyCallable_Check(func)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001968 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001969 return NULL;
1970 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001971 im = free_list;
1972 if (im != NULL) {
1973 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +00001974 PyObject_INIT(im, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001975 }
1976 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001977 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00001978 if (im == NULL)
1979 return NULL;
1980 }
Fred Drakedb81e8d2001-03-23 04:19:27 +00001981 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001982 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00001983 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001984 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +00001985 im->im_self = self;
Guido van Rossumcdf0d752001-08-17 12:07:34 +00001986 Py_XINCREF(class);
Guido van Rossum81daa321993-05-20 14:24:46 +00001987 im->im_class = class;
Neil Schemenauere83c00e2001-08-29 23:54:21 +00001988 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001989 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001990}
1991
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001992/* Class method methods */
1993
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001994#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +00001995
Guido van Rossume8122f11991-05-05 20:03:07 +00001996static struct memberlist instancemethod_memberlist[] = {
1997 {"im_func", T_OBJECT, OFF(im_func)},
1998 {"im_self", T_OBJECT, OFF(im_self)},
Guido van Rossum81daa321993-05-20 14:24:46 +00001999 {"im_class", T_OBJECT, OFF(im_class)},
Guido van Rossum6dabc981996-05-14 21:54:20 +00002000 /* Dummies that are not handled by getattr() except for __members__ */
2001 {"__doc__", T_INT, 0},
2002 {"__name__", T_INT, 0},
Barry Warsawd6a9e842001-01-15 20:40:19 +00002003 {"__dict__", T_OBJECT, 0},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002004 {NULL} /* Sentinel */
2005};
2006
Barry Warsawd6a9e842001-01-15 20:40:19 +00002007static int
2008instancemethod_setattro(register PyMethodObject *im, PyObject *name,
2009 PyObject *v)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002010{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002011 char *sname = PyString_AsString(name);
Barry Warsawd6a9e842001-01-15 20:40:19 +00002012
Barry Warsaw4f9b13b2001-02-26 18:09:15 +00002013 PyErr_Format(PyExc_TypeError, "read-only attribute: %s", sname);
2014 return -1;
Barry Warsawd6a9e842001-01-15 20:40:19 +00002015}
2016
2017
2018static PyObject *
2019instancemethod_getattro(register PyMethodObject *im, PyObject *name)
2020{
2021 PyObject *rtn;
2022 char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +00002023 if (sname[0] == '_') {
Guido van Rossum7859f871998-07-08 14:58:16 +00002024 /* Inherit __name__ and __doc__ from the callable object
2025 implementing the method */
2026 if (strcmp(sname, "__name__") == 0 ||
2027 strcmp(sname, "__doc__") == 0)
2028 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum6dabc981996-05-14 21:54:20 +00002029 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002030 if (PyEval_GetRestricted()) {
2031 PyErr_SetString(PyExc_RuntimeError,
2032 "instance-method attributes not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +00002033 return NULL;
2034 }
Barry Warsawd6a9e842001-01-15 20:40:19 +00002035 if (sname[0] == '_' && strcmp(sname, "__dict__") == 0)
2036 return PyObject_GetAttr(im->im_func, name);
2037
2038 rtn = PyMember_Get((char *)im, instancemethod_memberlist, sname);
2039 if (rtn == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
2040 PyErr_Clear();
2041 rtn = PyObject_GetAttr(im->im_func, name);
2042 }
2043 return rtn;
Guido van Rossum3f5da241990-12-20 15:06:42 +00002044}
2045
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002046static void
Fred Drake79912472000-07-09 04:06:11 +00002047instancemethod_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002048{
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002049 _PyObject_GC_UNTRACK(im);
Fred Drakedb81e8d2001-03-23 04:19:27 +00002050 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002051 Py_DECREF(im->im_func);
2052 Py_XDECREF(im->im_self);
Guido van Rossumcdf0d752001-08-17 12:07:34 +00002053 Py_XDECREF(im->im_class);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002054 im->im_self = (PyObject *)free_list;
2055 free_list = im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002056}
2057
Guido van Rossumebc8c511992-09-03 20:39:51 +00002058static int
Fred Drake79912472000-07-09 04:06:11 +00002059instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
Guido van Rossumebc8c511992-09-03 20:39:51 +00002060{
Guido van Rossume9df7271995-04-06 14:46:51 +00002061 if (a->im_self != b->im_self)
2062 return (a->im_self < b->im_self) ? -1 : 1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002063 return PyObject_Compare(a->im_func, b->im_func);
Guido van Rossumebc8c511992-09-03 20:39:51 +00002064}
2065
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002066static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002067instancemethod_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +00002068{
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +00002070 PyObject *func = a->im_func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002071 PyObject *klass = a->im_class;
2072 PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
2073 char *sfuncname = "?", *sklassname = "?";
2074
2075 funcname = PyObject_GetAttrString(func, "__name__");
2076 if (funcname == NULL)
2077 PyErr_Clear();
2078 else if (!PyString_Check(funcname)) {
2079 Py_DECREF(funcname);
2080 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +00002081 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002082 else
2083 sfuncname = PyString_AS_STRING(funcname);
Guido van Rossum40667692001-08-17 13:59:27 +00002084 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002085 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +00002086 else {
2087 klassname = PyObject_GetAttrString(klass, "__name__");
2088 if (klassname == NULL)
2089 PyErr_Clear();
2090 else if (!PyString_Check(klassname)) {
2091 Py_DECREF(klassname);
2092 klassname = NULL;
2093 }
2094 else
2095 sklassname = PyString_AS_STRING(klassname);
Guido van Rossum7859f871998-07-08 14:58:16 +00002096 }
Guido van Rossum81daa321993-05-20 14:24:46 +00002097 if (self == NULL)
Barry Warsaw7ce36942001-08-24 18:34:26 +00002098 result = PyString_FromFormat("<unbound method %s.%s>",
2099 sklassname, sfuncname);
Guido van Rossum81daa321993-05-20 14:24:46 +00002100 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002101 /* XXX Shouldn't use repr() here! */
2102 PyObject *selfrepr = PyObject_Repr(self);
2103 if (selfrepr == NULL)
2104 goto fail;
2105 if (!PyString_Check(selfrepr)) {
2106 Py_DECREF(selfrepr);
2107 goto fail;
2108 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002109 result = PyString_FromFormat("<bound method %s.%s of %s>",
2110 sklassname, sfuncname,
2111 PyString_AS_STRING(selfrepr));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002112 Py_DECREF(selfrepr);
Guido van Rossum81daa321993-05-20 14:24:46 +00002113 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002114 fail:
Guido van Rossum42636dc1999-10-11 14:03:12 +00002115 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116 Py_XDECREF(klassname);
2117 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00002118}
2119
Guido van Rossum9bfef441993-03-29 10:43:31 +00002120static long
Fred Drake79912472000-07-09 04:06:11 +00002121instancemethod_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002122{
2123 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +00002124 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002125 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +00002126 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002127 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002128 if (x == -1)
2129 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002130 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002131 if (y == -1)
2132 return -1;
2133 return x ^ y;
2134}
2135
Jeremy Hylton8caad492000-06-23 14:18:11 +00002136static int
2137instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
2138{
2139 int err;
2140 if (im->im_func) {
2141 err = visit(im->im_func, arg);
2142 if (err)
2143 return err;
2144 }
2145 if (im->im_self) {
2146 err = visit(im->im_self, arg);
2147 if (err)
2148 return err;
2149 }
2150 if (im->im_class) {
2151 err = visit(im->im_class, arg);
2152 if (err)
2153 return err;
2154 }
Jeremy Hyltond22162b2000-06-23 17:14:56 +00002155 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002156}
2157
Guido van Rossuma15dece2001-08-24 18:48:27 +00002158static char *
2159getclassname(PyObject *class)
2160{
2161 PyObject *name;
2162
2163 if (class == NULL)
2164 name = NULL;
2165 else
2166 name = PyObject_GetAttrString(class, "__name__");
2167 if (name == NULL) {
2168 PyErr_Clear();
2169 return "?";
2170 }
2171 if (!PyString_Check(name)) {
2172 Py_DECREF(name);
2173 return "?";
2174 }
2175 PyString_InternInPlace(&name);
2176 Py_DECREF(name);
2177 return PyString_AS_STRING(name);
2178}
2179
2180static char *
2181getinstclassname(PyObject *inst)
2182{
2183 PyObject *class;
2184 char *name;
2185
2186 if (inst == NULL)
2187 return "nothing";
2188
2189 class = PyObject_GetAttrString(inst, "__class__");
2190 if (class == NULL) {
2191 PyErr_Clear();
2192 class = (PyObject *)(inst->ob_type);
2193 Py_INCREF(class);
2194 }
2195 name = getclassname(class);
2196 Py_XDECREF(class);
2197 return name;
2198}
2199
Tim Peters6d6c1a32001-08-02 04:15:00 +00002200static PyObject *
2201instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
2202{
2203 PyObject *self = PyMethod_GET_SELF(func);
2204 PyObject *class = PyMethod_GET_CLASS(func);
2205 PyObject *result;
2206
2207 func = PyMethod_GET_FUNCTION(func);
2208 if (self == NULL) {
2209 /* Unbound methods must be called with an instance of
2210 the class (or a derived class) as first argument */
2211 int ok;
2212 if (PyTuple_Size(arg) >= 1)
2213 self = PyTuple_GET_ITEM(arg, 0);
2214 if (self == NULL)
2215 ok = 0;
2216 else {
2217 ok = PyObject_IsInstance(self, class);
2218 if (ok < 0)
2219 return NULL;
2220 }
2221 if (!ok) {
2222 PyErr_Format(PyExc_TypeError,
Guido van Rossuma15dece2001-08-24 18:48:27 +00002223 "unbound method %s%s must be called with "
2224 "%s instance as first argument "
2225 "(got %s%s instead)",
Tim Peters6d6c1a32001-08-02 04:15:00 +00002226 PyEval_GetFuncName(func),
Guido van Rossuma15dece2001-08-24 18:48:27 +00002227 PyEval_GetFuncDesc(func),
2228 getclassname(class),
2229 getinstclassname(self),
2230 self == NULL ? "" : " instance");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002231 return NULL;
2232 }
2233 Py_INCREF(arg);
2234 }
2235 else {
2236 int argcount = PyTuple_Size(arg);
2237 PyObject *newarg = PyTuple_New(argcount + 1);
2238 int i;
2239 if (newarg == NULL)
2240 return NULL;
2241 Py_INCREF(self);
2242 PyTuple_SET_ITEM(newarg, 0, self);
2243 for (i = 0; i < argcount; i++) {
2244 PyObject *v = PyTuple_GET_ITEM(arg, i);
2245 Py_XINCREF(v);
2246 PyTuple_SET_ITEM(newarg, i+1, v);
2247 }
2248 arg = newarg;
2249 }
2250 result = PyObject_Call((PyObject *)func, arg, kw);
2251 Py_DECREF(arg);
2252 return result;
2253}
2254
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002255static PyObject *
Guido van Rossumf23c41d2001-08-17 13:43:27 +00002256instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *class)
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002257{
Guido van Rossumf23c41d2001-08-17 13:43:27 +00002258 /* Don't rebind an already bound method, or an unbound method
2259 of a class that's not a base class of class */
2260 if (PyMethod_GET_SELF(meth) != NULL ||
2261 (PyMethod_GET_CLASS(meth) != NULL &&
2262 !PyObject_IsSubclass(class, PyMethod_GET_CLASS(meth)))) {
Guido van Rossum501c7c72001-08-16 20:41:56 +00002263 Py_INCREF(meth);
2264 return meth;
2265 }
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002266 if (obj == Py_None)
2267 obj = NULL;
Guido van Rossumf23c41d2001-08-17 13:43:27 +00002268 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, class);
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002269}
2270
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002271PyTypeObject PyMethod_Type = {
2272 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002273 0,
Guido van Rossum569fce71991-04-16 08:38:43 +00002274 "instance method",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002275 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002276 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002277 (destructor)instancemethod_dealloc, /* tp_dealloc */
2278 0, /* tp_print */
2279 0, /* tp_getattr */
2280 0, /* tp_setattr */
2281 (cmpfunc)instancemethod_compare, /* tp_compare */
2282 (reprfunc)instancemethod_repr, /* tp_repr */
2283 0, /* tp_as_number */
2284 0, /* tp_as_sequence */
2285 0, /* tp_as_mapping */
2286 (hashfunc)instancemethod_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002287 instancemethod_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002288 0, /* tp_str */
2289 (getattrofunc)instancemethod_getattro, /* tp_getattro */
2290 (setattrofunc)instancemethod_setattro, /* tp_setattro */
2291 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002292 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002293 0, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00002294 (traverseproc)instancemethod_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +00002295 0, /* tp_clear */
2296 0, /* tp_richcompare */
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002297 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
2298 0, /* tp_iter */
2299 0, /* tp_iternext */
2300 0, /* tp_methods */
2301 0, /* tp_members */
2302 0, /* tp_getset */
2303 0, /* tp_base */
2304 0, /* tp_dict */
2305 instancemethod_descr_get, /* tp_descr_get */
2306 0, /* tp_descr_set */
2307 0, /* tp_dictoffset */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002308};
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002309
2310/* Clear out the free list */
2311
2312void
Fred Drake79912472000-07-09 04:06:11 +00002313PyMethod_Fini(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002314{
2315 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00002316 PyMethodObject *im = free_list;
2317 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002318 PyObject_GC_Del(im);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002319 }
2320}