blob: 3f51c0fc55cc4aa5d6596e92913764a53c290cd3 [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
Christian Heimes6075a822008-02-06 12:44:34 +00007/* Free list for method objects to safe malloc/free overhead
8 * The im_self element is used to chain the elements.
9 */
10static PyMethodObject *free_list;
11static int numfree = 0;
Christian Heimes5b970ad2008-02-06 13:33:44 +000012#ifndef PyMethod_MAXFREELIST
13#define PyMethod_MAXFREELIST 256
14#endif
Christian Heimes6075a822008-02-06 12:44:34 +000015
Guido van Rossum915f0eb2001-10-17 20:26:38 +000016#define TP_DESCR_GET(t) \
17 (PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL)
18
Guido van Rossum52ca98a1994-09-05 07:32:29 +000019/* Forward */
Tim Petersdbd9ba62000-07-09 03:09:57 +000020static PyObject *class_lookup(PyClassObject *, PyObject *,
21 PyClassObject **);
22static PyObject *instance_getattr1(PyInstanceObject *, PyObject *);
23static PyObject *instance_getattr2(PyInstanceObject *, PyObject *);
Guido van Rossum52ca98a1994-09-05 07:32:29 +000024
Guido van Rossuma63eff61998-05-29 21:37:21 +000025static PyObject *getattrstr, *setattrstr, *delattrstr;
26
Fred Drake79912472000-07-09 04:06:11 +000027
Guido van Rossumc0b618a1997-05-02 03:12:38 +000028PyObject *
Fred Drake79912472000-07-09 04:06:11 +000029PyClass_New(PyObject *bases, PyObject *dict, PyObject *name)
30 /* bases is NULL or tuple of classobjects! */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000031{
Guido van Rossumc0b618a1997-05-02 03:12:38 +000032 PyClassObject *op, *dummy;
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000033 static PyObject *docstr, *modstr, *namestr;
Guido van Rossum019f4241996-08-21 14:54:28 +000034 if (docstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000035 docstr= PyString_InternFromString("__doc__");
Guido van Rossum019f4241996-08-21 14:54:28 +000036 if (docstr == NULL)
37 return NULL;
38 }
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000039 if (modstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000040 modstr= PyString_InternFromString("__module__");
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000041 if (modstr == NULL)
42 return NULL;
43 }
44 if (namestr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +000045 namestr= PyString_InternFromString("__name__");
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000046 if (namestr == NULL)
47 return NULL;
48 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +000049 if (name == NULL || !PyString_Check(name)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000050 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000051 "PyClass_New: name must be a string");
52 return NULL;
53 }
54 if (dict == NULL || !PyDict_Check(dict)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000055 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000056 "PyClass_New: dict must be a dictionary");
57 return NULL;
58 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +000059 if (PyDict_GetItem(dict, docstr) == NULL) {
60 if (PyDict_SetItem(dict, docstr, Py_None) < 0)
Guido van Rossume7d444f1995-01-07 12:35:18 +000061 return NULL;
62 }
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000063 if (PyDict_GetItem(dict, modstr) == NULL) {
64 PyObject *globals = PyEval_GetGlobals();
65 if (globals != NULL) {
Guido van Rossum04d73c41997-10-07 14:54:11 +000066 PyObject *modname = PyDict_GetItem(globals, namestr);
67 if (modname != NULL) {
68 if (PyDict_SetItem(dict, modstr, modname) < 0)
Guido van Rossum7cc56eb1997-09-12 20:04:46 +000069 return NULL;
70 }
71 }
72 }
Guido van Rossume2966a61991-12-10 13:53:23 +000073 if (bases == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000074 bases = PyTuple_New(0);
Guido van Rossume2966a61991-12-10 13:53:23 +000075 if (bases == NULL)
Guido van Rossume5372401993-03-16 12:15:04 +000076 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +000077 }
Guido van Rossum04d73c41997-10-07 14:54:11 +000078 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +000079 Py_ssize_t i, n;
Guido van Rossum28d80b12001-09-07 21:08:32 +000080 PyObject *base;
Guido van Rossum04d73c41997-10-07 14:54:11 +000081 if (!PyTuple_Check(bases)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +000082 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000083 "PyClass_New: bases must be a tuple");
84 return NULL;
85 }
Guido van Rossum28d80b12001-09-07 21:08:32 +000086 n = PyTuple_Size(bases);
87 for (i = 0; i < n; i++) {
88 base = PyTuple_GET_ITEM(bases, i);
89 if (!PyClass_Check(base)) {
90 if (PyCallable_Check(
91 (PyObject *) base->ob_type))
Georg Brandl684fd0c2006-05-25 19:15:31 +000092 return PyObject_CallFunctionObjArgs(
Guido van Rossum28d80b12001-09-07 21:08:32 +000093 (PyObject *) base->ob_type,
Georg Brandl684fd0c2006-05-25 19:15:31 +000094 name, bases, dict, NULL);
Tim Peters6d6c1a32001-08-02 04:15:00 +000095 PyErr_SetString(PyExc_TypeError,
Guido van Rossum04d73c41997-10-07 14:54:11 +000096 "PyClass_New: base must be a class");
97 return NULL;
98 }
99 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100 Py_INCREF(bases);
Guido van Rossum04d73c41997-10-07 14:54:11 +0000101 }
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000102
103 if (getattrstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000104 getattrstr = PyString_InternFromString("__getattr__");
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000105 if (getattrstr == NULL)
106 goto alloc_error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000107 setattrstr = PyString_InternFromString("__setattr__");
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000108 if (setattrstr == NULL)
109 goto alloc_error;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000110 delattrstr = PyString_InternFromString("__delattr__");
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000111 if (delattrstr == NULL)
112 goto alloc_error;
113 }
114
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000115 op = PyObject_GC_New(PyClassObject, &PyClass_Type);
Guido van Rossume2966a61991-12-10 13:53:23 +0000116 if (op == NULL) {
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000117alloc_error:
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000118 Py_DECREF(bases);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000119 return NULL;
Guido van Rossume2966a61991-12-10 13:53:23 +0000120 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000121 op->cl_bases = bases;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000122 Py_INCREF(dict);
Guido van Rossum81daa321993-05-20 14:24:46 +0000123 op->cl_dict = dict;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000124 Py_XINCREF(name);
Guido van Rossum94308391991-10-20 20:11:48 +0000125 op->cl_name = name;
Neal Norwitz6cbb7262006-08-19 04:22:33 +0000126
Guido van Rossum2878a691996-08-09 20:53:24 +0000127 op->cl_getattr = class_lookup(op, getattrstr, &dummy);
128 op->cl_setattr = class_lookup(op, setattrstr, &dummy);
129 op->cl_delattr = class_lookup(op, delattrstr, &dummy);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130 Py_XINCREF(op->cl_getattr);
131 Py_XINCREF(op->cl_setattr);
132 Py_XINCREF(op->cl_delattr);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000133 _PyObject_GC_TRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134 return (PyObject *) op;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000135}
136
Guido van Rossumb479dc52001-09-05 22:52:50 +0000137PyObject *
138PyMethod_Function(PyObject *im)
139{
140 if (!PyMethod_Check(im)) {
141 PyErr_BadInternalCall();
142 return NULL;
143 }
144 return ((PyMethodObject *)im)->im_func;
145}
146
147PyObject *
148PyMethod_Self(PyObject *im)
149{
150 if (!PyMethod_Check(im)) {
151 PyErr_BadInternalCall();
152 return NULL;
153 }
154 return ((PyMethodObject *)im)->im_self;
155}
156
157PyObject *
158PyMethod_Class(PyObject *im)
159{
160 if (!PyMethod_Check(im)) {
161 PyErr_BadInternalCall();
162 return NULL;
163 }
164 return ((PyMethodObject *)im)->im_class;
165}
166
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000167PyDoc_STRVAR(class_doc,
168"classobj(name, bases, dict)\n\
169\n\
170Create a class object. The name must be a string; the second argument\n\
171a tuple of classes, and the third a dictionary.");
172
173static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000174class_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
175{
176 PyObject *name, *bases, *dict;
Martin v. Löwis15e62742006-02-27 16:46:16 +0000177 static char *kwlist[] = {"name", "bases", "dict", 0};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000178
179 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist,
180 &name, &bases, &dict))
181 return NULL;
182 return PyClass_New(bases, dict, name);
183}
184
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000185/* Class methods */
186
187static void
Fred Drake79912472000-07-09 04:06:11 +0000188class_dealloc(PyClassObject *op)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000189{
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000190 _PyObject_GC_UNTRACK(op);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000191 Py_DECREF(op->cl_bases);
192 Py_DECREF(op->cl_dict);
193 Py_XDECREF(op->cl_name);
Guido van Rossum152d8171998-08-04 14:59:16 +0000194 Py_XDECREF(op->cl_getattr);
195 Py_XDECREF(op->cl_setattr);
196 Py_XDECREF(op->cl_delattr);
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000197 PyObject_GC_Del(op);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000198}
199
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000200static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000201class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass)
Guido van Rossum81daa321993-05-20 14:24:46 +0000202{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000203 Py_ssize_t i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000204 PyObject *value = PyDict_GetItem(cp->cl_dict, name);
Guido van Rossum81daa321993-05-20 14:24:46 +0000205 if (value != NULL) {
206 *pclass = cp;
207 return value;
208 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000209 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000210 for (i = 0; i < n; i++) {
Guido van Rossum7cc56eb1997-09-12 20:04:46 +0000211 /* XXX What if one of the bases is not a class? */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000212 PyObject *v = class_lookup(
213 (PyClassObject *)
214 PyTuple_GetItem(cp->cl_bases, i), name, pclass);
Guido van Rossum81daa321993-05-20 14:24:46 +0000215 if (v != NULL)
216 return v;
217 }
218 return NULL;
219}
220
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000221static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000222class_getattr(register PyClassObject *op, PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000223{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000224 register PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000225 register char *sname = PyString_AsString(name);
Anthony Baxter377be112006-04-11 06:54:30 +0000226 PyClassObject *klass;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000227 descrgetfunc f;
228
Guido van Rossum2878a691996-08-09 20:53:24 +0000229 if (sname[0] == '_' && sname[1] == '_') {
230 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000231 if (PyEval_GetRestricted()) {
232 PyErr_SetString(PyExc_RuntimeError,
233 "class.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000234 return NULL;
235 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000236 Py_INCREF(op->cl_dict);
Guido van Rossum10393b11995-01-10 10:39:49 +0000237 return op->cl_dict;
238 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000239 if (strcmp(sname, "__bases__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000240 Py_INCREF(op->cl_bases);
Guido van Rossum10393b11995-01-10 10:39:49 +0000241 return op->cl_bases;
242 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000243 if (strcmp(sname, "__name__") == 0) {
Guido van Rossum10393b11995-01-10 10:39:49 +0000244 if (op->cl_name == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000245 v = Py_None;
Guido van Rossum10393b11995-01-10 10:39:49 +0000246 else
247 v = op->cl_name;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000248 Py_INCREF(v);
Guido van Rossum10393b11995-01-10 10:39:49 +0000249 return v;
250 }
Guido van Rossum94308391991-10-20 20:11:48 +0000251 }
Anthony Baxter377be112006-04-11 06:54:30 +0000252 v = class_lookup(op, name, &klass);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000253 if (v == NULL) {
Fred Drake661ea262000-10-24 19:57:45 +0000254 PyErr_Format(PyExc_AttributeError,
255 "class %.50s has no attribute '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000256 PyString_AS_STRING(op->cl_name), sname);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000257 return NULL;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000258 }
Guido van Rossum915f0eb2001-10-17 20:26:38 +0000259 f = TP_DESCR_GET(v->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000260 if (f == NULL)
261 Py_INCREF(v);
262 else
263 v = f(v, (PyObject *)NULL, (PyObject *)op);
Guido van Rossumb3f72581993-05-21 19:56:10 +0000264 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000265}
266
Guido van Rossuma63eff61998-05-29 21:37:21 +0000267static void
Fred Drake79912472000-07-09 04:06:11 +0000268set_slot(PyObject **slot, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000269{
270 PyObject *temp = *slot;
271 Py_XINCREF(v);
272 *slot = v;
273 Py_XDECREF(temp);
274}
275
Guido van Rossum7ba30431998-07-08 13:34:48 +0000276static void
Fred Drake79912472000-07-09 04:06:11 +0000277set_attr_slots(PyClassObject *c)
Guido van Rossum7ba30431998-07-08 13:34:48 +0000278{
279 PyClassObject *dummy;
280
281 set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy));
282 set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy));
283 set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy));
284}
285
Guido van Rossuma63eff61998-05-29 21:37:21 +0000286static char *
Fred Drake79912472000-07-09 04:06:11 +0000287set_dict(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000288{
Guido van Rossuma63eff61998-05-29 21:37:21 +0000289 if (v == NULL || !PyDict_Check(v))
290 return "__dict__ must be a dictionary object";
291 set_slot(&c->cl_dict, v);
Guido van Rossum7ba30431998-07-08 13:34:48 +0000292 set_attr_slots(c);
Guido van Rossuma63eff61998-05-29 21:37:21 +0000293 return "";
294}
295
296static char *
Fred Drake79912472000-07-09 04:06:11 +0000297set_bases(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000298{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000299 Py_ssize_t i, n;
Guido van Rossuma63eff61998-05-29 21:37:21 +0000300
301 if (v == NULL || !PyTuple_Check(v))
302 return "__bases__ must be a tuple object";
303 n = PyTuple_Size(v);
304 for (i = 0; i < n; i++) {
305 PyObject *x = PyTuple_GET_ITEM(v, i);
306 if (!PyClass_Check(x))
307 return "__bases__ items must be classes";
308 if (PyClass_IsSubclass(x, (PyObject *)c))
309 return "a __bases__ item causes an inheritance cycle";
310 }
311 set_slot(&c->cl_bases, v);
Guido van Rossum7ba30431998-07-08 13:34:48 +0000312 set_attr_slots(c);
Guido van Rossuma63eff61998-05-29 21:37:21 +0000313 return "";
314}
315
316static char *
Fred Drake79912472000-07-09 04:06:11 +0000317set_name(PyClassObject *c, PyObject *v)
Guido van Rossuma63eff61998-05-29 21:37:21 +0000318{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000319 if (v == NULL || !PyString_Check(v))
Guido van Rossuma63eff61998-05-29 21:37:21 +0000320 return "__name__ must be a string object";
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000321 if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v))
Guido van Rossuma63eff61998-05-29 21:37:21 +0000322 return "__name__ must not contain null bytes";
323 set_slot(&c->cl_name, v);
324 return "";
325}
326
Guido van Rossum94308391991-10-20 20:11:48 +0000327static int
Fred Drake79912472000-07-09 04:06:11 +0000328class_setattr(PyClassObject *op, PyObject *name, PyObject *v)
Guido van Rossum94308391991-10-20 20:11:48 +0000329{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000330 char *sname;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000331 if (PyEval_GetRestricted()) {
332 PyErr_SetString(PyExc_RuntimeError,
Guido van Rossumac21f6a1995-08-04 04:05:31 +0000333 "classes are read-only in restricted mode");
334 return -1;
335 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000336 sname = PyString_AsString(name);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000337 if (sname[0] == '_' && sname[1] == '_') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000338 Py_ssize_t n = PyString_Size(name);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000339 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossuma63eff61998-05-29 21:37:21 +0000340 char *err = NULL;
341 if (strcmp(sname, "__dict__") == 0)
342 err = set_dict(op, v);
343 else if (strcmp(sname, "__bases__") == 0)
344 err = set_bases(op, v);
345 else if (strcmp(sname, "__name__") == 0)
346 err = set_name(op, v);
347 else if (strcmp(sname, "__getattr__") == 0)
348 set_slot(&op->cl_getattr, v);
349 else if (strcmp(sname, "__setattr__") == 0)
350 set_slot(&op->cl_setattr, v);
351 else if (strcmp(sname, "__delattr__") == 0)
352 set_slot(&op->cl_delattr, v);
353 /* For the last three, we fall through to update the
354 dictionary as well. */
355 if (err != NULL) {
356 if (*err == '\0')
357 return 0;
358 PyErr_SetString(PyExc_TypeError, err);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000359 return -1;
360 }
361 }
362 }
Guido van Rossum94472a01992-09-04 09:45:18 +0000363 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000364 int rv = PyDict_DelItem(op->cl_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000365 if (rv < 0)
Fred Drake661ea262000-10-24 19:57:45 +0000366 PyErr_Format(PyExc_AttributeError,
367 "class %.50s has no attribute '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000368 PyString_AS_STRING(op->cl_name), sname);
Guido van Rossum94472a01992-09-04 09:45:18 +0000369 return rv;
370 }
Guido van Rossum94308391991-10-20 20:11:48 +0000371 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000372 return PyDict_SetItem(op->cl_dict, name, v);
Guido van Rossum94308391991-10-20 20:11:48 +0000373}
374
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000375static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000376class_repr(PyClassObject *op)
Guido van Rossum25831651993-05-19 14:50:45 +0000377{
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000378 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
Guido van Rossum25831651993-05-19 14:50:45 +0000379 char *name;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000380 if (op->cl_name == NULL || !PyString_Check(op->cl_name))
Guido van Rossum25831651993-05-19 14:50:45 +0000381 name = "?";
382 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000383 name = PyString_AsString(op->cl_name);
384 if (mod == NULL || !PyString_Check(mod))
385 return PyString_FromFormat("<class ?.%s at %p>", name, op);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000386 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000387 return PyString_FromFormat("<class %s.%s at %p>",
388 PyString_AsString(mod),
Barry Warsaw7ce36942001-08-24 18:34:26 +0000389 name, op);
Guido van Rossum25831651993-05-19 14:50:45 +0000390}
391
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000392static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000393class_str(PyClassObject *op)
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000394{
395 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__");
396 PyObject *name = op->cl_name;
397 PyObject *res;
Martin v. Löwis66851282006-04-22 11:40:03 +0000398 Py_ssize_t m, n;
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000399
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000400 if (name == NULL || !PyString_Check(name))
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000401 return class_repr(op);
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000402 if (mod == NULL || !PyString_Check(mod)) {
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000403 Py_INCREF(name);
404 return name;
405 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000406 m = PyString_GET_SIZE(mod);
407 n = PyString_GET_SIZE(name);
408 res = PyString_FromStringAndSize((char *)NULL, m+1+n);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000409 if (res != NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000410 char *s = PyString_AS_STRING(res);
411 memcpy(s, PyString_AS_STRING(mod), m);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000412 s += m;
413 *s++ = '.';
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000414 memcpy(s, PyString_AS_STRING(name), n);
Guido van Rossum4a2a6211997-10-20 23:26:11 +0000415 }
416 return res;
417}
418
Jeremy Hylton8caad492000-06-23 14:18:11 +0000419static int
420class_traverse(PyClassObject *o, visitproc visit, void *arg)
421{
Thomas Woutersc6e55062006-04-15 21:47:09 +0000422 Py_VISIT(o->cl_bases);
423 Py_VISIT(o->cl_dict);
424 Py_VISIT(o->cl_name);
425 Py_VISIT(o->cl_getattr);
426 Py_VISIT(o->cl_setattr);
427 Py_VISIT(o->cl_delattr);
Jeremy Hylton8caad492000-06-23 14:18:11 +0000428 return 0;
429}
430
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000431PyTypeObject PyClass_Type = {
432 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000433 0,
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000434 "classobj",
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000435 sizeof(PyClassObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000436 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000437 (destructor)class_dealloc, /* tp_dealloc */
438 0, /* tp_print */
439 0, /* tp_getattr */
440 0, /* tp_setattr */
441 0, /* tp_compare */
442 (reprfunc)class_repr, /* tp_repr */
443 0, /* tp_as_number */
444 0, /* tp_as_sequence */
445 0, /* tp_as_mapping */
446 0, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447 PyInstance_New, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000448 (reprfunc)class_str, /* tp_str */
449 (getattrofunc)class_getattr, /* tp_getattro */
450 (setattrofunc)class_setattr, /* tp_setattro */
451 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000452 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000453 class_doc, /* tp_doc */
Guido van Rossum8998b4f2001-01-17 15:28:20 +0000454 (traverseproc)class_traverse, /* tp_traverse */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000455 0, /* tp_clear */
456 0, /* tp_richcompare */
457 0, /* tp_weaklistoffset */
458 0, /* tp_iter */
459 0, /* tp_iternext */
460 0, /* tp_methods */
461 0, /* tp_members */
462 0, /* tp_getset */
463 0, /* tp_base */
464 0, /* tp_dict */
465 0, /* tp_descr_get */
466 0, /* tp_descr_set */
467 0, /* tp_dictoffset */
468 0, /* tp_init */
469 0, /* tp_alloc */
470 class_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000471};
472
Guido van Rossum81daa321993-05-20 14:24:46 +0000473int
Anthony Baxter377be112006-04-11 06:54:30 +0000474PyClass_IsSubclass(PyObject *klass, PyObject *base)
Guido van Rossum81daa321993-05-20 14:24:46 +0000475{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000476 Py_ssize_t i, n;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000477 PyClassObject *cp;
Anthony Baxter377be112006-04-11 06:54:30 +0000478 if (klass == base)
Guido van Rossum81daa321993-05-20 14:24:46 +0000479 return 1;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000480 if (PyTuple_Check(base)) {
481 n = PyTuple_GET_SIZE(base);
482 for (i = 0; i < n; i++) {
Anthony Baxter377be112006-04-11 06:54:30 +0000483 if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i)))
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000484 return 1;
485 }
Walter Dörwald7e5c6a02002-12-12 19:14:08 +0000486 return 0;
Walter Dörwaldd9a6ad32002-12-12 16:41:44 +0000487 }
Anthony Baxter377be112006-04-11 06:54:30 +0000488 if (klass == NULL || !PyClass_Check(klass))
Guido van Rossumeb6b33a1993-05-25 09:38:27 +0000489 return 0;
Anthony Baxter377be112006-04-11 06:54:30 +0000490 cp = (PyClassObject *)klass;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000491 n = PyTuple_Size(cp->cl_bases);
Guido van Rossum81daa321993-05-20 14:24:46 +0000492 for (i = 0; i < n; i++) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000493 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base))
Guido van Rossum81daa321993-05-20 14:24:46 +0000494 return 1;
495 }
496 return 0;
497}
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000498
Guido van Rossum81daa321993-05-20 14:24:46 +0000499
500/* Instance objects */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000501
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000502PyObject *
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000503PyInstance_NewRaw(PyObject *klass, PyObject *dict)
504{
505 PyInstanceObject *inst;
506
507 if (!PyClass_Check(klass)) {
508 PyErr_BadInternalCall();
509 return NULL;
510 }
511 if (dict == NULL) {
512 dict = PyDict_New();
513 if (dict == NULL)
514 return NULL;
515 }
516 else {
517 if (!PyDict_Check(dict)) {
518 PyErr_BadInternalCall();
519 return NULL;
520 }
521 Py_INCREF(dict);
522 }
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000523 inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type);
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000524 if (inst == NULL) {
525 Py_DECREF(dict);
526 return NULL;
527 }
Fred Drake4e262a92001-03-22 18:26:47 +0000528 inst->in_weakreflist = NULL;
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000529 Py_INCREF(klass);
530 inst->in_class = (PyClassObject *)klass;
531 inst->in_dict = dict;
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000532 _PyObject_GC_TRACK(inst);
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000533 return (PyObject *)inst;
534}
535
536PyObject *
537PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000538{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000539 register PyInstanceObject *inst;
540 PyObject *init;
541 static PyObject *initstr;
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000542
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000543 if (initstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000544 initstr = PyString_InternFromString("__init__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000545 if (initstr == NULL)
546 return NULL;
547 }
Fred Drake5cc2c8c2001-01-28 03:53:08 +0000548 inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL);
Guido van Rossume8122f11991-05-05 20:03:07 +0000549 if (inst == NULL)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000550 return NULL;
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000551 init = instance_getattr2(inst, initstr);
Guido van Rossum25831651993-05-19 14:50:45 +0000552 if (init == NULL) {
Guido van Rossumf740bdf2002-10-29 18:36:40 +0000553 if (PyErr_Occurred()) {
554 Py_DECREF(inst);
555 return NULL;
556 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000557 if ((arg != NULL && (!PyTuple_Check(arg) ||
558 PyTuple_Size(arg) != 0))
559 || (kw != NULL && (!PyDict_Check(kw) ||
560 PyDict_Size(kw) != 0))) {
561 PyErr_SetString(PyExc_TypeError,
Guido van Rossuma83f2701995-07-26 18:07:32 +0000562 "this constructor takes no arguments");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000563 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000564 inst = NULL;
565 }
566 }
567 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000568 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw);
569 Py_DECREF(init);
Guido van Rossum25831651993-05-19 14:50:45 +0000570 if (res == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000571 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000572 inst = NULL;
573 }
574 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000575 if (res != Py_None) {
576 PyErr_SetString(PyExc_TypeError,
Guido van Rossum25831651993-05-19 14:50:45 +0000577 "__init__() should return None");
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000578 Py_DECREF(inst);
Guido van Rossum25831651993-05-19 14:50:45 +0000579 inst = NULL;
580 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000581 Py_DECREF(res);
Guido van Rossum25831651993-05-19 14:50:45 +0000582 }
583 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000584 return (PyObject *)inst;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000585}
586
Guido van Rossum21ed88c1991-04-04 10:42:10 +0000587/* Instance methods */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000588
Guido van Rossumbea18cc2002-06-14 20:41:17 +0000589PyDoc_STRVAR(instance_doc,
590"instance(class[, dict])\n\
591\n\
592Create an instance without calling its __init__() method.\n\
593The class must be a classic class.\n\
594If present, dict must be a dictionary or None.");
595
596static PyObject *
597instance_new(PyTypeObject* type, PyObject* args, PyObject *kw)
598{
599 PyObject *klass;
600 PyObject *dict = Py_None;
601
602 if (!PyArg_ParseTuple(args, "O!|O:instance",
603 &PyClass_Type, &klass, &dict))
604 return NULL;
605
606 if (dict == Py_None)
607 dict = NULL;
608 else if (!PyDict_Check(dict)) {
609 PyErr_SetString(PyExc_TypeError,
610 "instance() second arg must be dictionary or None");
611 return NULL;
612 }
613 return PyInstance_NewRaw(klass, dict);
614}
615
616
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000617static void
Fred Drake79912472000-07-09 04:06:11 +0000618instance_dealloc(register PyInstanceObject *inst)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000619{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000620 PyObject *error_type, *error_value, *error_traceback;
621 PyObject *del;
622 static PyObject *delstr;
Tim Peters34592512002-07-11 06:23:50 +0000623
Neil Schemenauere83c00e2001-08-29 23:54:21 +0000624 _PyObject_GC_UNTRACK(inst);
Fred Drakec916f5a2001-10-26 17:56:51 +0000625 if (inst->in_weakreflist != NULL)
626 PyObject_ClearWeakRefs((PyObject *) inst);
Fred Drake41deb1e2001-02-01 05:27:45 +0000627
Tim Peters6b184912000-09-17 14:40:17 +0000628 /* Temporarily resurrect the object. */
Tim Peters34592512002-07-11 06:23:50 +0000629 assert(inst->ob_type == &PyInstance_Type);
630 assert(inst->ob_refcnt == 0);
631 inst->ob_refcnt = 1;
Tim Peters6b184912000-09-17 14:40:17 +0000632
633 /* Save the current exception, if any. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000634 PyErr_Fetch(&error_type, &error_value, &error_traceback);
Tim Peters6b184912000-09-17 14:40:17 +0000635 /* Execute __del__ method, if any. */
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000636 if (delstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000637 delstr = PyString_InternFromString("__del__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000638 if (delstr == NULL)
Neal Norwitzaf33f2d2006-08-14 00:59:03 +0000639 PyErr_WriteUnraisable((PyObject*)inst);
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000640 }
Neal Norwitzaf33f2d2006-08-14 00:59:03 +0000641 if (delstr && (del = instance_getattr2(inst, delstr)) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000642 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL);
Tim Peters6b184912000-09-17 14:40:17 +0000643 if (res == NULL)
Jeremy Hyltonb709df32000-09-01 02:47:25 +0000644 PyErr_WriteUnraisable(del);
Guido van Rossum0dfcf751996-08-12 22:00:53 +0000645 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000646 Py_DECREF(res);
647 Py_DECREF(del);
Guido van Rossum25831651993-05-19 14:50:45 +0000648 }
Tim Peters6b184912000-09-17 14:40:17 +0000649 /* Restore the saved exception. */
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000650 PyErr_Restore(error_type, error_value, error_traceback);
Tim Peters34592512002-07-11 06:23:50 +0000651
Tim Peters6b184912000-09-17 14:40:17 +0000652 /* Undo the temporary resurrection; can't use DECREF here, it would
653 * cause a recursive call.
654 */
Tim Peters34592512002-07-11 06:23:50 +0000655 assert(inst->ob_refcnt > 0);
656 if (--inst->ob_refcnt == 0) {
Guido van Rossum9ff1a442008-01-18 20:56:30 +0000657
658 /* New weakrefs could be created during the finalizer call.
659 If this occurs, clear them out without calling their
660 finalizers since they might rely on part of the object
661 being finalized that has already been destroyed. */
662 while (inst->in_weakreflist != NULL) {
663 _PyWeakref_ClearRef((PyWeakReference *)
664 (inst->in_weakreflist));
665 }
666
Tim Peters34592512002-07-11 06:23:50 +0000667 Py_DECREF(inst->in_class);
668 Py_XDECREF(inst->in_dict);
669 PyObject_GC_Del(inst);
Sjoerd Mullender740f3571995-08-28 09:00:43 +0000670 }
Tim Peters34592512002-07-11 06:23:50 +0000671 else {
Martin v. Löwis66851282006-04-22 11:40:03 +0000672 Py_ssize_t refcnt = inst->ob_refcnt;
Tim Peters34592512002-07-11 06:23:50 +0000673 /* __del__ resurrected it! Make it look like the original
674 * Py_DECREF never happened.
675 */
676 _Py_NewReference((PyObject *)inst);
677 inst->ob_refcnt = refcnt;
678 _PyObject_GC_TRACK(inst);
Michael W. Hudson3f3b6682004-08-03 10:21:03 +0000679 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
680 * we need to undo that. */
681 _Py_DEC_REFTOTAL;
682 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the
683 * object chain, so no more to do there.
Tim Peters34592512002-07-11 06:23:50 +0000684 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
Michael W. Hudson3f3b6682004-08-03 10:21:03 +0000685 * _Py_NewReference bumped tp_allocs: both of those need to be
686 * undone.
Tim Peters34592512002-07-11 06:23:50 +0000687 */
Tim Peters6b184912000-09-17 14:40:17 +0000688#ifdef COUNT_ALLOCS
Tim Peters34592512002-07-11 06:23:50 +0000689 --inst->ob_type->tp_frees;
690 --inst->ob_type->tp_allocs;
Tim Peters6b184912000-09-17 14:40:17 +0000691#endif
Tim Peters34592512002-07-11 06:23:50 +0000692 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000693}
694
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000695static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000696instance_getattr1(register PyInstanceObject *inst, PyObject *name)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000697{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000698 register PyObject *v;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000699 register char *sname = PyString_AsString(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000700 if (sname[0] == '_' && sname[1] == '_') {
701 if (strcmp(sname, "__dict__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000702 if (PyEval_GetRestricted()) {
703 PyErr_SetString(PyExc_RuntimeError,
704 "instance.__dict__ not accessible in restricted mode");
Guido van Rossum10393b11995-01-10 10:39:49 +0000705 return NULL;
706 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000707 Py_INCREF(inst->in_dict);
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000708 return inst->in_dict;
709 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000710 if (strcmp(sname, "__class__") == 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000711 Py_INCREF(inst->in_class);
712 return (PyObject *)inst->in_class;
Sjoerd Mullender3bb8a051993-10-22 12:04:32 +0000713 }
Guido van Rossum94308391991-10-20 20:11:48 +0000714 }
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000715 v = instance_getattr2(inst, name);
Guido van Rossumf740bdf2002-10-29 18:36:40 +0000716 if (v == NULL && !PyErr_Occurred()) {
Fred Drake661ea262000-10-24 19:57:45 +0000717 PyErr_Format(PyExc_AttributeError,
718 "%.50s instance has no attribute '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000719 PyString_AS_STRING(inst->in_class->cl_name), sname);
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000720 }
721 return v;
722}
723
724static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000725instance_getattr2(register PyInstanceObject *inst, PyObject *name)
Jeremy Hylton9e392e22000-04-26 20:39:20 +0000726{
727 register PyObject *v;
Anthony Baxter377be112006-04-11 06:54:30 +0000728 PyClassObject *klass;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000729 descrgetfunc f;
730
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000731 v = PyDict_GetItem(inst->in_dict, name);
Guido van Rossumf23c41d2001-08-17 13:43:27 +0000732 if (v != NULL) {
733 Py_INCREF(v);
734 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000735 }
Anthony Baxter377be112006-04-11 06:54:30 +0000736 v = class_lookup(inst->in_class, name, &klass);
Guido van Rossumf23c41d2001-08-17 13:43:27 +0000737 if (v != NULL) {
738 Py_INCREF(v);
Guido van Rossum915f0eb2001-10-17 20:26:38 +0000739 f = TP_DESCR_GET(v->ob_type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000740 if (f != NULL) {
741 PyObject *w = f(v, (PyObject *)inst,
742 (PyObject *)(inst->in_class));
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000743 Py_DECREF(v);
Guido van Rossum21d335e1993-10-15 13:01:11 +0000744 v = w;
745 }
Guido van Rossumb3f72581993-05-21 19:56:10 +0000746 }
747 return v;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000748}
749
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000750static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000751instance_getattr(register PyInstanceObject *inst, PyObject *name)
Guido van Rossume7737541994-09-05 07:31:41 +0000752{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000753 register PyObject *func, *res;
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000754 res = instance_getattr1(inst, name);
755 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000756 PyObject *args;
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000757 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
758 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000759 PyErr_Clear();
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000760 args = PyTuple_Pack(2, inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000761 if (args == NULL)
Guido van Rossume7737541994-09-05 07:31:41 +0000762 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000763 res = PyEval_CallObject(func, args);
764 Py_DECREF(args);
Guido van Rossume7737541994-09-05 07:31:41 +0000765 }
766 return res;
767}
768
Tim Petersdf875b92003-04-07 17:51:59 +0000769/* See classobject.h comments: this only does dict lookups, and is always
770 * safe to call.
771 */
772PyObject *
773_PyInstance_Lookup(PyObject *pinst, PyObject *name)
774{
775 PyObject *v;
Anthony Baxter377be112006-04-11 06:54:30 +0000776 PyClassObject *klass;
Tim Petersdf875b92003-04-07 17:51:59 +0000777 PyInstanceObject *inst; /* pinst cast to the right type */
778
779 assert(PyInstance_Check(pinst));
780 inst = (PyInstanceObject *)pinst;
781
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000782 assert(PyString_Check(name));
Tim Petersdf875b92003-04-07 17:51:59 +0000783
784 v = PyDict_GetItem(inst->in_dict, name);
785 if (v == NULL)
Anthony Baxter377be112006-04-11 06:54:30 +0000786 v = class_lookup(inst->in_class, name, &klass);
Tim Petersdf875b92003-04-07 17:51:59 +0000787 return v;
788}
789
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000790static int
Fred Drake79912472000-07-09 04:06:11 +0000791instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000792{
Guido van Rossum94472a01992-09-04 09:45:18 +0000793 if (v == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000794 int rv = PyDict_DelItem(inst->in_dict, name);
Guido van Rossum94472a01992-09-04 09:45:18 +0000795 if (rv < 0)
Fred Drake661ea262000-10-24 19:57:45 +0000796 PyErr_Format(PyExc_AttributeError,
797 "%.50s instance has no attribute '%.400s'",
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000798 PyString_AS_STRING(inst->in_class->cl_name),
799 PyString_AS_STRING(name));
Guido van Rossum94472a01992-09-04 09:45:18 +0000800 return rv;
801 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000802 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000803 return PyDict_SetItem(inst->in_dict, name, v);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000804}
805
Guido van Rossume7737541994-09-05 07:31:41 +0000806static int
Fred Drake79912472000-07-09 04:06:11 +0000807instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v)
Guido van Rossume7737541994-09-05 07:31:41 +0000808{
Guido van Rossumb2173c31997-08-25 21:23:56 +0000809 PyObject *func, *args, *res, *tmp;
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000810 char *sname = PyString_AsString(name);
Guido van Rossumb2173c31997-08-25 21:23:56 +0000811 if (sname[0] == '_' && sname[1] == '_') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000812 Py_ssize_t n = PyString_Size(name);
Guido van Rossum2878a691996-08-09 20:53:24 +0000813 if (sname[n-1] == '_' && sname[n-2] == '_') {
Guido van Rossumb2173c31997-08-25 21:23:56 +0000814 if (strcmp(sname, "__dict__") == 0) {
815 if (PyEval_GetRestricted()) {
816 PyErr_SetString(PyExc_RuntimeError,
817 "__dict__ not accessible in restricted mode");
818 return -1;
819 }
820 if (v == NULL || !PyDict_Check(v)) {
821 PyErr_SetString(PyExc_TypeError,
822 "__dict__ must be set to a dictionary");
823 return -1;
824 }
825 tmp = inst->in_dict;
826 Py_INCREF(v);
827 inst->in_dict = v;
828 Py_DECREF(tmp);
829 return 0;
830 }
831 if (strcmp(sname, "__class__") == 0) {
832 if (PyEval_GetRestricted()) {
833 PyErr_SetString(PyExc_RuntimeError,
834 "__class__ not accessible in restricted mode");
835 return -1;
836 }
837 if (v == NULL || !PyClass_Check(v)) {
838 PyErr_SetString(PyExc_TypeError,
839 "__class__ must be set to a class");
840 return -1;
841 }
842 tmp = (PyObject *)(inst->in_class);
843 Py_INCREF(v);
844 inst->in_class = (PyClassObject *)v;
845 Py_DECREF(tmp);
846 return 0;
847 }
Guido van Rossume7737541994-09-05 07:31:41 +0000848 }
Guido van Rossume7737541994-09-05 07:31:41 +0000849 }
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000850 if (v == NULL)
851 func = inst->in_class->cl_delattr;
852 else
853 func = inst->in_class->cl_setattr;
854 if (func == NULL)
855 return instance_setattr1(inst, name, v);
856 if (v == NULL)
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000857 args = PyTuple_Pack(2, inst, name);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000858 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +0000859 args = PyTuple_Pack(3, inst, name, v);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000860 if (args == NULL)
861 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000862 res = PyEval_CallObject(func, args);
863 Py_DECREF(args);
Guido van Rossum52ca98a1994-09-05 07:32:29 +0000864 if (res == NULL)
865 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000866 Py_DECREF(res);
Guido van Rossume7737541994-09-05 07:31:41 +0000867 return 0;
868}
869
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000870static PyObject *
Fred Drake79912472000-07-09 04:06:11 +0000871instance_repr(PyInstanceObject *inst)
Guido van Rossum04691fc1992-08-12 15:35:34 +0000872{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000873 PyObject *func;
874 PyObject *res;
875 static PyObject *reprstr;
Guido van Rossum04691fc1992-08-12 15:35:34 +0000876
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000877 if (reprstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000878 reprstr = PyString_InternFromString("__repr__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000879 if (reprstr == NULL)
880 return NULL;
881 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000882 func = instance_getattr(inst, reprstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000883 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000884 PyObject *classname, *mod;
Guido van Rossum25831651993-05-19 14:50:45 +0000885 char *cname;
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000886 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
887 return NULL;
888 PyErr_Clear();
889 classname = inst->in_class->cl_name;
890 mod = PyDict_GetItemString(inst->in_class->cl_dict,
891 "__module__");
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000892 if (classname != NULL && PyString_Check(classname))
893 cname = PyString_AsString(classname);
Guido van Rossum25831651993-05-19 14:50:45 +0000894 else
895 cname = "?";
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000896 if (mod == NULL || !PyString_Check(mod))
897 return PyString_FromFormat("<?.%s instance at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +0000898 cname, inst);
Guido van Rossumb7f1afe1997-12-03 00:06:02 +0000899 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000900 return PyString_FromFormat("<%s.%s instance at %p>",
901 PyString_AsString(mod),
Barry Warsaw7ce36942001-08-24 18:34:26 +0000902 cname, inst);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000903 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000904 res = PyEval_CallObject(func, (PyObject *)NULL);
905 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +0000906 return res;
907}
908
Guido van Rossum82c690f2001-04-30 14:39:18 +0000909static PyObject *
910instance_str(PyInstanceObject *inst)
911{
912 PyObject *func;
913 PyObject *res;
914 static PyObject *strstr;
915
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000916 if (strstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000917 strstr = PyString_InternFromString("__str__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000918 if (strstr == NULL)
919 return NULL;
920 }
Guido van Rossum82c690f2001-04-30 14:39:18 +0000921 func = instance_getattr(inst, strstr);
922 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000923 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
924 return NULL;
Guido van Rossum82c690f2001-04-30 14:39:18 +0000925 PyErr_Clear();
926 return instance_repr(inst);
927 }
928 res = PyEval_CallObject(func, (PyObject *)NULL);
929 Py_DECREF(func);
930 return res;
931}
932
Guido van Rossum9bfef441993-03-29 10:43:31 +0000933static long
Fred Drake79912472000-07-09 04:06:11 +0000934instance_hash(PyInstanceObject *inst)
Guido van Rossum9bfef441993-03-29 10:43:31 +0000935{
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000936 PyObject *func;
937 PyObject *res;
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000938 long outcome;
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000939 static PyObject *hashstr, *eqstr, *cmpstr;
Guido van Rossum9bfef441993-03-29 10:43:31 +0000940
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000941 if (hashstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000942 hashstr = PyString_InternFromString("__hash__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000943 if (hashstr == NULL)
944 return -1;
945 }
Guido van Rossum2878a691996-08-09 20:53:24 +0000946 func = instance_getattr(inst, hashstr);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000947 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000948 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
949 return -1;
950 PyErr_Clear();
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000951 /* If there is no __eq__ and no __cmp__ method, we hash on the
952 address. If an __eq__ or __cmp__ method exists, there must
953 be a __hash__. */
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000954 if (eqstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000955 eqstr = PyString_InternFromString("__eq__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000956 if (eqstr == NULL)
957 return -1;
958 }
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000959 func = instance_getattr(inst, eqstr);
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000960 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000961 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
962 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000963 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000964 if (cmpstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000965 cmpstr = PyString_InternFromString("__cmp__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +0000966 if (cmpstr == NULL)
967 return -1;
968 }
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000969 func = instance_getattr(inst, cmpstr);
970 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +0000971 if (!PyErr_ExceptionMatches(
972 PyExc_AttributeError))
973 return -1;
Guido van Rossum65e8bd72001-01-18 23:46:31 +0000974 PyErr_Clear();
975 return _Py_HashPointer(inst);
976 }
Guido van Rossum8a0c3451993-04-08 12:56:19 +0000977 }
Raymond Hettingera9e14b72003-09-16 07:11:46 +0000978 Py_XDECREF(func);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000979 PyErr_SetString(PyExc_TypeError, "unhashable instance");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000980 return -1;
981 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000982 res = PyEval_CallObject(func, (PyObject *)NULL);
983 Py_DECREF(func);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000984 if (res == NULL)
985 return -1;
Martin v. Löwisab2f8f72006-08-09 07:57:39 +0000986 if (PyInt_Check(res) || PyLong_Check(res))
987 /* This already converts a -1 result to -2. */
988 outcome = res->ob_type->tp_hash(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000989 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000990 PyErr_SetString(PyExc_TypeError,
991 "__hash__() should return an int");
Guido van Rossum9bfef441993-03-29 10:43:31 +0000992 outcome = -1;
993 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000994 Py_DECREF(res);
Guido van Rossum9bfef441993-03-29 10:43:31 +0000995 return outcome;
996}
997
Jeremy Hylton8caad492000-06-23 14:18:11 +0000998static int
999instance_traverse(PyInstanceObject *o, visitproc visit, void *arg)
1000{
Thomas Woutersc6e55062006-04-15 21:47:09 +00001001 Py_VISIT(o->in_class);
1002 Py_VISIT(o->in_dict);
Jeremy Hyltond22162b2000-06-23 17:14:56 +00001003 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00001004}
1005
Guido van Rossum213c7a62001-04-23 14:08:49 +00001006static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr;
1007static PyObject *iterstr, *nextstr;
Guido van Rossum2878a691996-08-09 20:53:24 +00001008
Martin v. Löwis18e16552006-02-15 17:27:45 +00001009static Py_ssize_t
Fred Drake79912472000-07-09 04:06:11 +00001010instance_length(PyInstanceObject *inst)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001011{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001012 PyObject *func;
1013 PyObject *res;
Martin v. Löwis18e16552006-02-15 17:27:45 +00001014 Py_ssize_t outcome;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001015
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001016 if (lenstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001017 lenstr = PyString_InternFromString("__len__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001018 if (lenstr == NULL)
1019 return -1;
1020 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001021 func = instance_getattr(inst, lenstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001022 if (func == NULL)
1023 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001024 res = PyEval_CallObject(func, (PyObject *)NULL);
1025 Py_DECREF(func);
Guido van Rossumd014ea61992-11-26 10:30:26 +00001026 if (res == NULL)
1027 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028 if (PyInt_Check(res)) {
Neal Norwitz1872b1c2006-08-12 18:44:06 +00001029 outcome = PyInt_AsSsize_t(res);
1030 if (outcome == -1 && PyErr_Occurred()) {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001031 Py_DECREF(res);
1032 return -1;
1033 }
Neal Norwitz1872b1c2006-08-12 18:44:06 +00001034#if SIZEOF_SIZE_T < SIZEOF_INT
Guido van Rossumba3e6ec2005-09-19 22:42:41 +00001035 /* Overflow check -- range of PyInt is more than C int */
Neal Norwitz1872b1c2006-08-12 18:44:06 +00001036 if (outcome != (int)outcome) {
Guido van Rossumba3e6ec2005-09-19 22:42:41 +00001037 PyErr_SetString(PyExc_OverflowError,
Guido van Rossum630db602005-09-20 18:49:54 +00001038 "__len__() should return 0 <= outcome < 2**31");
Guido van Rossumba3e6ec2005-09-19 22:42:41 +00001039 outcome = -1;
1040 }
1041 else
1042#endif
Neal Norwitz1872b1c2006-08-12 18:44:06 +00001043 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001044 PyErr_SetString(PyExc_ValueError,
1045 "__len__() should return >= 0");
Neal Norwitz1872b1c2006-08-12 18:44:06 +00001046 outcome = -1;
1047 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001048 }
1049 else {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001050 PyErr_SetString(PyExc_TypeError,
1051 "__len__() should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001052 outcome = -1;
1053 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001054 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001055 return outcome;
1056}
1057
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001058static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001059instance_subscript(PyInstanceObject *inst, PyObject *key)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001060{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001061 PyObject *func;
1062 PyObject *arg;
1063 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001064
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001065 if (getitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001066 getitemstr = PyString_InternFromString("__getitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001067 if (getitemstr == NULL)
1068 return NULL;
1069 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001070 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001071 if (func == NULL)
1072 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001073 arg = PyTuple_Pack(1, key);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001074 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001075 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001076 return NULL;
1077 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001078 res = PyEval_CallObject(func, arg);
1079 Py_DECREF(func);
1080 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001081 return res;
1082}
1083
Guido van Rossum9bfef441993-03-29 10:43:31 +00001084static int
Fred Drake79912472000-07-09 04:06:11 +00001085instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001086{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001087 PyObject *func;
1088 PyObject *arg;
1089 PyObject *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001090
Guido van Rossum2878a691996-08-09 20:53:24 +00001091 if (value == NULL) {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001092 if (delitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001093 delitemstr = PyString_InternFromString("__delitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001094 if (delitemstr == NULL)
1095 return -1;
1096 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001097 func = instance_getattr(inst, delitemstr);
1098 }
1099 else {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001100 if (setitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001101 setitemstr = PyString_InternFromString("__setitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001102 if (setitemstr == NULL)
1103 return -1;
1104 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001105 func = instance_getattr(inst, setitemstr);
1106 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001107 if (func == NULL)
1108 return -1;
1109 if (value == NULL)
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001110 arg = PyTuple_Pack(1, key);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001111 else
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001112 arg = PyTuple_Pack(2, key, value);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001113 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001114 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001115 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001116 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001117 res = PyEval_CallObject(func, arg);
1118 Py_DECREF(func);
1119 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001120 if (res == NULL)
1121 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001122 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001123 return 0;
1124}
1125
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001126static PyMappingMethods instance_as_mapping = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001127 (lenfunc)instance_length, /* mp_length */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001128 (binaryfunc)instance_subscript, /* mp_subscript */
1129 (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001130};
1131
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001132static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001133instance_item(PyInstanceObject *inst, Py_ssize_t i)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001134{
Georg Brandle4e023c2006-05-26 20:22:50 +00001135 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001136
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001137 if (getitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001138 getitemstr = PyString_InternFromString("__getitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001139 if (getitemstr == NULL)
1140 return NULL;
1141 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001142 func = instance_getattr(inst, getitemstr);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001143 if (func == NULL)
1144 return NULL;
Georg Brandle4e023c2006-05-26 20:22:50 +00001145 res = PyObject_CallFunction(func, "n", i);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001146 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001147 return res;
1148}
1149
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001150static PyObject *
Martin v. Löwis18e16552006-02-15 17:27:45 +00001151instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001152{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001153 PyObject *func, *arg, *res;
1154 static PyObject *getslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001155
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001156 if (getslicestr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001157 getslicestr = PyString_InternFromString("__getslice__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001158 if (getslicestr == NULL)
1159 return NULL;
1160 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001161 func = instance_getattr(inst, getslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001162
1163 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001164 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1165 return NULL;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001166 PyErr_Clear();
1167
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001168 if (getitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001169 getitemstr = PyString_InternFromString("__getitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001170 if (getitemstr == NULL)
1171 return NULL;
1172 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001173 func = instance_getattr(inst, getitemstr);
1174 if (func == NULL)
1175 return NULL;
Neal Norwitzbadc0862006-03-23 06:03:08 +00001176 arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j));
Benjamin Peterson712ee922008-08-24 18:10:20 +00001177 }
1178 else {
1179 if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "
1180 "use __getitem__", 1) < 0) {
1181 Py_DECREF(func);
1182 return NULL;
1183 }
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001184 arg = Py_BuildValue("(nn)", i, j);
Benjamin Peterson712ee922008-08-24 18:10:20 +00001185 }
Tim Peters34592512002-07-11 06:23:50 +00001186
Guido van Rossum04691fc1992-08-12 15:35:34 +00001187 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001188 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001189 return NULL;
1190 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001191 res = PyEval_CallObject(func, arg);
1192 Py_DECREF(func);
1193 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001194 return res;
1195}
1196
1197static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001198instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001199{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001200 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001201
Guido van Rossum2878a691996-08-09 20:53:24 +00001202 if (item == NULL) {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001203 if (delitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001204 delitemstr = PyString_InternFromString("__delitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001205 if (delitemstr == NULL)
1206 return -1;
1207 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001208 func = instance_getattr(inst, delitemstr);
1209 }
1210 else {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001211 if (setitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001212 setitemstr = PyString_InternFromString("__setitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001213 if (setitemstr == NULL)
1214 return -1;
1215 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001216 func = instance_getattr(inst, setitemstr);
1217 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001218 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +00001219 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001220 if (item == NULL)
Georg Brandl2cfaa342006-05-29 19:39:45 +00001221 arg = PyInt_FromSsize_t(i);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001222 else
Georg Brandl2cfaa342006-05-29 19:39:45 +00001223 arg = Py_BuildValue("(nO)", i, item);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001224 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001225 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001226 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001227 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001228 res = PyEval_CallObject(func, arg);
1229 Py_DECREF(func);
1230 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001231 if (res == NULL)
1232 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001233 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001234 return 0;
1235}
1236
1237static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001238instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001239{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001240 PyObject *func, *arg, *res;
1241 static PyObject *setslicestr, *delslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001242
Guido van Rossum2878a691996-08-09 20:53:24 +00001243 if (value == NULL) {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001244 if (delslicestr == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001245 delslicestr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001246 PyString_InternFromString("__delslice__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001247 if (delslicestr == NULL)
1248 return -1;
1249 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001250 func = instance_getattr(inst, delslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001251 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001252 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1253 return -1;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001254 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001255 if (delitemstr == NULL) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001256 delitemstr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001257 PyString_InternFromString("__delitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001258 if (delitemstr == NULL)
1259 return -1;
1260 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001261 func = instance_getattr(inst, delitemstr);
1262 if (func == NULL)
1263 return -1;
1264
1265 arg = Py_BuildValue("(N)",
Neal Norwitzbadc0862006-03-23 06:03:08 +00001266 _PySlice_FromIndices(i, j));
Benjamin Peterson712ee922008-08-24 18:10:20 +00001267 }
1268 else {
1269 if (PyErr_WarnPy3k("in 3.x, __delslice__ has been "
1270 "removed; use __delitem__", 1) < 0) {
1271 Py_DECREF(func);
1272 return -1;
1273 }
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001274 arg = Py_BuildValue("(nn)", i, j);
Benjamin Peterson712ee922008-08-24 18:10:20 +00001275 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001276 }
1277 else {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001278 if (setslicestr == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001279 setslicestr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001280 PyString_InternFromString("__setslice__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001281 if (setslicestr == NULL)
1282 return -1;
1283 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001284 func = instance_getattr(inst, setslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001285 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001286 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1287 return -1;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001288 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001289 if (setitemstr == NULL) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001290 setitemstr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001291 PyString_InternFromString("__setitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001292 if (setitemstr == NULL)
1293 return -1;
1294 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001295 func = instance_getattr(inst, setitemstr);
1296 if (func == NULL)
1297 return -1;
1298
1299 arg = Py_BuildValue("(NO)",
Neal Norwitzbadc0862006-03-23 06:03:08 +00001300 _PySlice_FromIndices(i, j), value);
Benjamin Peterson712ee922008-08-24 18:10:20 +00001301 }
1302 else {
1303 if (PyErr_WarnPy3k("in 3.x, __setslice__ has been "
1304 "removed; use __setitem__", 1) < 0) {
1305 Py_DECREF(func);
1306 return -1;
1307 }
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001308 arg = Py_BuildValue("(nnO)", i, j, value);
Benjamin Peterson712ee922008-08-24 18:10:20 +00001309 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001310 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001311 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001312 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001313 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001314 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001315 res = PyEval_CallObject(func, arg);
1316 Py_DECREF(func);
1317 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001318 if (res == NULL)
1319 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001320 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001321 return 0;
1322}
1323
Tim Peterscb8d3682001-05-05 21:05:01 +00001324static int
1325instance_contains(PyInstanceObject *inst, PyObject *member)
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001326{
1327 static PyObject *__contains__;
Tim Peterscb8d3682001-05-05 21:05:01 +00001328 PyObject *func;
1329
1330 /* Try __contains__ first.
1331 * If that can't be done, try iterator-based searching.
1332 */
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001333
1334 if(__contains__ == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001335 __contains__ = PyString_InternFromString("__contains__");
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001336 if(__contains__ == NULL)
1337 return -1;
1338 }
1339 func = instance_getattr(inst, __contains__);
Tim Peterscb8d3682001-05-05 21:05:01 +00001340 if (func) {
1341 PyObject *res;
1342 int ret;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001343 PyObject *arg = PyTuple_Pack(1, member);
Tim Peterscb8d3682001-05-05 21:05:01 +00001344 if(arg == NULL) {
1345 Py_DECREF(func);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001346 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001347 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001348 res = PyEval_CallObject(func, arg);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001349 Py_DECREF(func);
Tim Peterscb8d3682001-05-05 21:05:01 +00001350 Py_DECREF(arg);
Tim Peters34592512002-07-11 06:23:50 +00001351 if(res == NULL)
Tim Peterscb8d3682001-05-05 21:05:01 +00001352 return -1;
1353 ret = PyObject_IsTrue(res);
1354 Py_DECREF(res);
1355 return ret;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001356 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001357
1358 /* Couldn't find __contains__. */
1359 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Martin v. Löwis3a624042006-11-08 06:46:37 +00001360 Py_ssize_t rc;
Tim Peterscb8d3682001-05-05 21:05:01 +00001361 /* Assume the failure was simply due to that there is no
1362 * __contains__ attribute, and try iterating instead.
1363 */
1364 PyErr_Clear();
Martin v. Löwis3a624042006-11-08 06:46:37 +00001365 rc = _PySequence_IterSearch((PyObject *)inst, member,
1366 PY_ITERSEARCH_CONTAINS);
1367 if (rc >= 0)
1368 return rc > 0;
Tim Peterscb8d3682001-05-05 21:05:01 +00001369 }
Martin v. Löwis3a624042006-11-08 06:46:37 +00001370 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001371}
1372
Fred Drake79912472000-07-09 04:06:11 +00001373static PySequenceMethods
1374instance_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001375 (lenfunc)instance_length, /* sq_length */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001376 0, /* sq_concat */
1377 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001378 (ssizeargfunc)instance_item, /* sq_item */
1379 (ssizessizeargfunc)instance_slice, /* sq_slice */
1380 (ssizeobjargproc)instance_ass_item, /* sq_ass_item */
1381 (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001382 (objobjproc)instance_contains, /* sq_contains */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001383};
1384
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001386generic_unary_op(PyInstanceObject *self, PyObject *methodname)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001387{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001388 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001389
1390 if ((func = instance_getattr(self, methodname)) == NULL)
1391 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001392 res = PyEval_CallObject(func, (PyObject *)NULL);
1393 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001394 return res;
1395}
1396
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001397static PyObject *
1398generic_binary_op(PyObject *v, PyObject *w, char *opname)
Guido van Rossum03093a21994-09-28 15:51:32 +00001399{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001400 PyObject *result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001401 PyObject *args;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001402 PyObject *func = PyObject_GetAttrString(v, opname);
Guido van Rossume7d444f1995-01-07 12:35:18 +00001403 if (func == NULL) {
Guido van Rossum617c1b01998-05-28 19:50:02 +00001404 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001405 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001406 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001407 Py_INCREF(Py_NotImplemented);
1408 return Py_NotImplemented;
Guido van Rossume7d444f1995-01-07 12:35:18 +00001409 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001410 args = PyTuple_Pack(1, w);
Guido van Rossum03093a21994-09-28 15:51:32 +00001411 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001412 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001413 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +00001414 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001415 result = PyEval_CallObject(func, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001416 Py_DECREF(args);
1417 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001418 return result;
1419}
1420
1421
1422static PyObject *coerce_obj;
1423
1424/* Try one half of a binary operator involving a class instance. */
1425static PyObject *
Tim Peters34592512002-07-11 06:23:50 +00001426half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001427 int swapped)
1428{
1429 PyObject *args;
1430 PyObject *coercefunc;
1431 PyObject *coerced = NULL;
1432 PyObject *v1;
1433 PyObject *result;
Tim Peters34592512002-07-11 06:23:50 +00001434
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001435 if (!PyInstance_Check(v)) {
1436 Py_INCREF(Py_NotImplemented);
1437 return Py_NotImplemented;
1438 }
1439
1440 if (coerce_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001441 coerce_obj = PyString_InternFromString("__coerce__");
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001442 if (coerce_obj == NULL)
1443 return NULL;
1444 }
1445 coercefunc = PyObject_GetAttr(v, coerce_obj);
1446 if (coercefunc == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001447 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1448 return NULL;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001449 PyErr_Clear();
1450 return generic_binary_op(v, w, opname);
1451 }
1452
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001453 args = PyTuple_Pack(1, w);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001454 if (args == NULL) {
Guido van Rossum617080b2002-10-18 14:15:33 +00001455 Py_DECREF(coercefunc);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001456 return NULL;
1457 }
1458 coerced = PyEval_CallObject(coercefunc, args);
1459 Py_DECREF(args);
1460 Py_DECREF(coercefunc);
1461 if (coerced == NULL) {
1462 return NULL;
1463 }
1464 if (coerced == Py_None || coerced == Py_NotImplemented) {
1465 Py_DECREF(coerced);
1466 return generic_binary_op(v, w, opname);
1467 }
1468 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
1469 Py_DECREF(coerced);
1470 PyErr_SetString(PyExc_TypeError,
1471 "coercion should return None or 2-tuple");
1472 return NULL;
1473 }
1474 v1 = PyTuple_GetItem(coerced, 0);
1475 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001476 if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001477 /* prevent recursion if __coerce__ returns self as the first
1478 * argument */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001479 result = generic_binary_op(v1, w, opname);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001480 } else {
Brett Cannonea3912b2006-06-13 21:46:41 +00001481 if (Py_EnterRecursiveCall(" after coercion"))
1482 return NULL;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001483 if (swapped)
1484 result = (thisfunc)(w, v1);
1485 else
1486 result = (thisfunc)(v1, w);
Brett Cannonea3912b2006-06-13 21:46:41 +00001487 Py_LeaveRecursiveCall();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001488 }
1489 Py_DECREF(coerced);
1490 return result;
1491}
1492
1493/* Implement a binary operator involving at least one class instance. */
1494static PyObject *
1495do_binop(PyObject *v, PyObject *w, char *opname, char *ropname,
1496 binaryfunc thisfunc)
1497{
1498 PyObject *result = half_binop(v, w, opname, thisfunc, 0);
1499 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001500 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001501 result = half_binop(w, v, ropname, thisfunc, 1);
1502 }
1503 return result;
1504}
1505
1506static PyObject *
1507do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname,
1508 char *ropname, binaryfunc thisfunc)
1509{
1510 PyObject *result = half_binop(v, w, iopname, thisfunc, 0);
1511 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001512 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001513 result = do_binop(v, w, opname, ropname, thisfunc);
1514 }
1515 return result;
Guido van Rossum03093a21994-09-28 15:51:32 +00001516}
1517
Guido van Rossum879c5811995-01-10 15:24:06 +00001518static int
Fred Drake79912472000-07-09 04:06:11 +00001519instance_coerce(PyObject **pv, PyObject **pw)
Guido van Rossum879c5811995-01-10 15:24:06 +00001520{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001521 PyObject *v = *pv;
1522 PyObject *w = *pw;
Guido van Rossum3931df91997-11-18 19:23:07 +00001523 PyObject *coercefunc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001524 PyObject *args;
1525 PyObject *coerced;
Guido van Rossum879c5811995-01-10 15:24:06 +00001526
Guido van Rossum2878a691996-08-09 20:53:24 +00001527 if (coerce_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001528 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001529 if (coerce_obj == NULL)
1530 return -1;
1531 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001532 coercefunc = PyObject_GetAttr(v, coerce_obj);
1533 if (coercefunc == NULL) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001534 /* No __coerce__ method */
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001535 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1536 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001537 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001538 return 1;
Guido van Rossum879c5811995-01-10 15:24:06 +00001539 }
1540 /* Has __coerce__ method: call it */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001541 args = PyTuple_Pack(1, w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001542 if (args == NULL) {
1543 return -1;
1544 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001545 coerced = PyEval_CallObject(coercefunc, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001546 Py_DECREF(args);
Guido van Rossum3931df91997-11-18 19:23:07 +00001547 Py_DECREF(coercefunc);
Guido van Rossum879c5811995-01-10 15:24:06 +00001548 if (coerced == NULL) {
1549 /* __coerce__ call raised an exception */
1550 return -1;
1551 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001552 if (coerced == Py_None || coerced == Py_NotImplemented) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001553 /* __coerce__ says "I can't do it" */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001554 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001555 return 1;
1556 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001557 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001558 /* __coerce__ return value is malformed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001559 Py_DECREF(coerced);
1560 PyErr_SetString(PyExc_TypeError,
Guido van Rossum879c5811995-01-10 15:24:06 +00001561 "coercion should return None or 2-tuple");
1562 return -1;
1563 }
1564 /* __coerce__ returned two new values */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565 *pv = PyTuple_GetItem(coerced, 0);
1566 *pw = PyTuple_GetItem(coerced, 1);
1567 Py_INCREF(*pv);
1568 Py_INCREF(*pw);
1569 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001570 return 0;
1571}
1572
Guido van Rossum04691fc1992-08-12 15:35:34 +00001573#define UNARY(funcname, methodname) \
Thomas Woutersc3073522000-07-23 22:09:59 +00001574static PyObject *funcname(PyInstanceObject *self) { \
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001575 static PyObject *o; \
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001576 if (o == NULL) { o = PyString_InternFromString(methodname); \
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001577 if (o == NULL) return NULL; } \
Guido van Rossum2878a691996-08-09 20:53:24 +00001578 return generic_unary_op(self, o); \
Guido van Rossum04691fc1992-08-12 15:35:34 +00001579}
1580
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001581/* unary function with a fallback */
1582#define UNARY_FB(funcname, methodname, funcname_fb) \
1583static PyObject *funcname(PyInstanceObject *self) { \
1584 static PyObject *o; \
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001585 if (o == NULL) { o = PyString_InternFromString(methodname); \
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001586 if (o == NULL) return NULL; } \
1587 if (PyObject_HasAttr((PyObject*)self, o)) \
1588 return generic_unary_op(self, o); \
1589 else \
1590 return funcname_fb(self); \
1591}
1592
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001593#define BINARY(f, m, n) \
1594static PyObject *f(PyObject *v, PyObject *w) { \
1595 return do_binop(v, w, "__" m "__", "__r" m "__", n); \
1596}
1597
1598#define BINARY_INPLACE(f, m, n) \
1599static PyObject *f(PyObject *v, PyObject *w) { \
1600 return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \
1601 "__r" m "__", n); \
1602}
1603
Guido van Rossum04691fc1992-08-12 15:35:34 +00001604UNARY(instance_neg, "__neg__")
1605UNARY(instance_pos, "__pos__")
1606UNARY(instance_abs, "__abs__")
1607
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001608BINARY(instance_or, "or", PyNumber_Or)
1609BINARY(instance_and, "and", PyNumber_And)
1610BINARY(instance_xor, "xor", PyNumber_Xor)
1611BINARY(instance_lshift, "lshift", PyNumber_Lshift)
1612BINARY(instance_rshift, "rshift", PyNumber_Rshift)
1613BINARY(instance_add, "add", PyNumber_Add)
1614BINARY(instance_sub, "sub", PyNumber_Subtract)
1615BINARY(instance_mul, "mul", PyNumber_Multiply)
1616BINARY(instance_div, "div", PyNumber_Divide)
1617BINARY(instance_mod, "mod", PyNumber_Remainder)
1618BINARY(instance_divmod, "divmod", PyNumber_Divmod)
Guido van Rossum4668b002001-08-08 05:00:18 +00001619BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
1620BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001621
1622BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
1623BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
1624BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd)
1625BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift)
1626BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift)
1627BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd)
1628BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
1629BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
1630BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
1631BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
Guido van Rossum4668b002001-08-08 05:00:18 +00001632BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
1633BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001634
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001635/* Try a 3-way comparison, returning an int; v is an instance. Return:
1636 -2 for an exception;
1637 -1 if v < w;
1638 0 if v == w;
1639 1 if v > w;
1640 2 if this particular 3-way comparison is not implemented or undefined.
1641*/
1642static int
1643half_cmp(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001644{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001645 static PyObject *cmp_obj;
1646 PyObject *args;
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001647 PyObject *cmp_func;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001648 PyObject *result;
1649 long l;
1650
1651 assert(PyInstance_Check(v));
1652
1653 if (cmp_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001654 cmp_obj = PyString_InternFromString("__cmp__");
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001655 if (cmp_obj == NULL)
1656 return -2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001657 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001658
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001659 cmp_func = PyObject_GetAttr(v, cmp_obj);
1660 if (cmp_func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001661 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1662 return -2;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001663 PyErr_Clear();
1664 return 2;
1665 }
1666
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001667 args = PyTuple_Pack(1, w);
Guido van Rossum617080b2002-10-18 14:15:33 +00001668 if (args == NULL) {
1669 Py_DECREF(cmp_func);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001670 return -2;
Guido van Rossum617080b2002-10-18 14:15:33 +00001671 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001672
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001673 result = PyEval_CallObject(cmp_func, args);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001674 Py_DECREF(args);
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001675 Py_DECREF(cmp_func);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001676
1677 if (result == NULL)
1678 return -2;
1679
1680 if (result == Py_NotImplemented) {
1681 Py_DECREF(result);
1682 return 2;
1683 }
1684
1685 l = PyInt_AsLong(result);
1686 Py_DECREF(result);
1687 if (l == -1 && PyErr_Occurred()) {
1688 PyErr_SetString(PyExc_TypeError,
1689 "comparison did not return an int");
1690 return -2;
1691 }
1692
1693 return l < 0 ? -1 : l > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001694}
1695
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001696/* Try a 3-way comparison, returning an int; either v or w is an instance.
1697 We first try a coercion. Return:
1698 -2 for an exception;
1699 -1 if v < w;
1700 0 if v == w;
1701 1 if v > w;
1702 2 if this particular 3-way comparison is not implemented or undefined.
1703 THIS IS ONLY CALLED FROM object.c!
1704*/
1705static int
1706instance_compare(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001707{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001708 int c;
1709
1710 c = PyNumber_CoerceEx(&v, &w);
1711 if (c < 0)
1712 return -2;
1713 if (c == 0) {
1714 /* If neither is now an instance, use regular comparison */
1715 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
1716 c = PyObject_Compare(v, w);
1717 Py_DECREF(v);
1718 Py_DECREF(w);
1719 if (PyErr_Occurred())
1720 return -2;
1721 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001722 }
1723 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001724 else {
1725 /* The coercion didn't do anything.
1726 Treat this the same as returning v and w unchanged. */
1727 Py_INCREF(v);
1728 Py_INCREF(w);
1729 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001730
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001731 if (PyInstance_Check(v)) {
1732 c = half_cmp(v, w);
1733 if (c <= 1) {
1734 Py_DECREF(v);
1735 Py_DECREF(w);
1736 return c;
1737 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001738 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001739 if (PyInstance_Check(w)) {
1740 c = half_cmp(w, v);
1741 if (c <= 1) {
1742 Py_DECREF(v);
1743 Py_DECREF(w);
1744 if (c >= -1)
1745 c = -c;
1746 return c;
1747 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001748 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001749 Py_DECREF(v);
1750 Py_DECREF(w);
1751 return 2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001752}
1753
Guido van Rossum9bfef441993-03-29 10:43:31 +00001754static int
Fred Drake79912472000-07-09 04:06:11 +00001755instance_nonzero(PyInstanceObject *self)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001756{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001758 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001759 static PyObject *nonzerostr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001760
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001761 if (nonzerostr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001762 nonzerostr = PyString_InternFromString("__nonzero__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001763 if (nonzerostr == NULL)
1764 return -1;
1765 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001766 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001767 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1768 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001769 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001770 if (lenstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001771 lenstr = PyString_InternFromString("__len__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001772 if (lenstr == NULL)
1773 return -1;
1774 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001775 if ((func = instance_getattr(self, lenstr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001776 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1777 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001778 PyErr_Clear();
Guido van Rossum04691fc1992-08-12 15:35:34 +00001779 /* Fall back to the default behavior:
1780 all instances are nonzero */
1781 return 1;
1782 }
1783 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001784 res = PyEval_CallObject(func, (PyObject *)NULL);
1785 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001786 if (res == NULL)
1787 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001788 if (!PyInt_Check(res)) {
1789 Py_DECREF(res);
1790 PyErr_SetString(PyExc_TypeError,
1791 "__nonzero__ should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001792 return -1;
1793 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001794 outcome = PyInt_AsLong(res);
1795 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001796 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001797 PyErr_SetString(PyExc_ValueError,
1798 "__nonzero__ should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001799 return -1;
1800 }
1801 return outcome > 0;
1802}
1803
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001804static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001805instance_index(PyInstanceObject *self)
1806{
1807 PyObject *func, *res;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001808 static PyObject *indexstr = NULL;
1809
1810 if (indexstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001811 indexstr = PyString_InternFromString("__index__");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001812 if (indexstr == NULL)
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001813 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001814 }
1815 if ((func = instance_getattr(self, indexstr)) == NULL) {
1816 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001817 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001818 PyErr_Clear();
1819 PyErr_SetString(PyExc_TypeError,
1820 "object cannot be interpreted as an index");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001821 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001822 }
1823 res = PyEval_CallObject(func, (PyObject *)NULL);
1824 Py_DECREF(func);
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001825 return res;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001826}
1827
1828
Guido van Rossum04691fc1992-08-12 15:35:34 +00001829UNARY(instance_invert, "__invert__")
Jeffrey Yasskina26cf9b2008-02-04 01:04:35 +00001830UNARY(_instance_trunc, "__trunc__")
1831
1832static PyObject *
1833instance_int(PyInstanceObject *self)
1834{
1835 PyObject *truncated;
1836 static PyObject *int_name;
1837 if (int_name == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001838 int_name = PyString_InternFromString("__int__");
Jeffrey Yasskina26cf9b2008-02-04 01:04:35 +00001839 if (int_name == NULL)
1840 return NULL;
1841 }
1842 if (PyObject_HasAttr((PyObject*)self, int_name))
1843 return generic_unary_op(self, int_name);
1844
1845 truncated = _instance_trunc(self);
1846 /* __trunc__ is specified to return an Integral type, but
1847 int() needs to return an int. */
1848 return _PyNumber_ConvertIntegralToInt(
1849 truncated,
1850 "__trunc__ returned non-Integral (type %.200s)");
1851}
1852
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001853UNARY_FB(instance_long, "__long__", instance_int)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001854UNARY(instance_float, "__float__")
1855UNARY(instance_oct, "__oct__")
1856UNARY(instance_hex, "__hex__")
1857
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001858static PyObject *
1859bin_power(PyObject *v, PyObject *w)
1860{
1861 return PyNumber_Power(v, w, Py_None);
1862}
1863
Guido van Rossum03093a21994-09-28 15:51:32 +00001864/* This version is for ternary calls only (z != None) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001865static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001866instance_pow(PyObject *v, PyObject *w, PyObject *z)
Tim Peters34592512002-07-11 06:23:50 +00001867{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001868 if (z == Py_None) {
1869 return do_binop(v, w, "__pow__", "__rpow__", bin_power);
Guido van Rossum03093a21994-09-28 15:51:32 +00001870 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001871 else {
1872 PyObject *func;
1873 PyObject *args;
1874 PyObject *result;
1875
1876 /* XXX Doesn't do coercions... */
1877 func = PyObject_GetAttrString(v, "__pow__");
1878 if (func == NULL)
1879 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001880 args = PyTuple_Pack(2, w, z);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001881 if (args == NULL) {
1882 Py_DECREF(func);
1883 return NULL;
1884 }
1885 result = PyEval_CallObject(func, args);
1886 Py_DECREF(func);
1887 Py_DECREF(args);
1888 return result;
1889 }
Guido van Rossum03093a21994-09-28 15:51:32 +00001890}
1891
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001892static PyObject *
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001893bin_inplace_power(PyObject *v, PyObject *w)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001894{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001895 return PyNumber_InPlacePower(v, w, Py_None);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001896}
1897
1898
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001899static PyObject *
1900instance_ipow(PyObject *v, PyObject *w, PyObject *z)
1901{
1902 if (z == Py_None) {
1903 return do_binop_inplace(v, w, "__ipow__", "__pow__",
1904 "__rpow__", bin_inplace_power);
1905 }
1906 else {
1907 /* XXX Doesn't do coercions... */
1908 PyObject *func;
1909 PyObject *args;
1910 PyObject *result;
1911
1912 func = PyObject_GetAttrString(v, "__ipow__");
1913 if (func == NULL) {
1914 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1915 return NULL;
1916 PyErr_Clear();
1917 return instance_pow(v, w, z);
1918 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001919 args = PyTuple_Pack(2, w, z);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001920 if (args == NULL) {
1921 Py_DECREF(func);
1922 return NULL;
1923 }
1924 result = PyEval_CallObject(func, args);
1925 Py_DECREF(func);
1926 Py_DECREF(args);
1927 return result;
1928 }
1929}
1930
1931
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001932/* Map rich comparison operators to their __xx__ namesakes */
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001933#define NAME_OPS 6
1934static PyObject **name_op = NULL;
1935
Tim Peters34592512002-07-11 06:23:50 +00001936static int
Guido van Rossum0ba9e3a2001-05-22 02:33:08 +00001937init_name_op(void)
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001938{
1939 int i;
1940 char *_name_op[] = {
1941 "__lt__",
1942 "__le__",
1943 "__eq__",
1944 "__ne__",
1945 "__gt__",
1946 "__ge__",
1947 };
1948
1949 name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS);
1950 if (name_op == NULL)
1951 return -1;
1952 for (i = 0; i < NAME_OPS; ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001953 name_op[i] = PyString_InternFromString(_name_op[i]);
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001954 if (name_op[i] == NULL)
1955 return -1;
1956 }
1957 return 0;
1958}
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001959
1960static PyObject *
1961half_richcompare(PyObject *v, PyObject *w, int op)
1962{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001963 PyObject *method;
1964 PyObject *args;
1965 PyObject *res;
1966
1967 assert(PyInstance_Check(v));
1968
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001969 if (name_op == NULL) {
1970 if (init_name_op() < 0)
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001971 return NULL;
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001972 }
1973 /* If the instance doesn't define an __getattr__ method, use
1974 instance_getattr2 directly because it will not set an
1975 exception on failure. */
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001976 if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL)
Tim Peters34592512002-07-11 06:23:50 +00001977 method = instance_getattr2((PyInstanceObject *)v,
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001978 name_op[op]);
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001979 else
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001980 method = PyObject_GetAttr(v, name_op[op]);
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001981 if (method == NULL) {
1982 if (PyErr_Occurred()) {
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001983 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1984 return NULL;
1985 PyErr_Clear();
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001986 }
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001987 res = Py_NotImplemented;
1988 Py_INCREF(res);
1989 return res;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001990 }
1991
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001992 args = PyTuple_Pack(1, w);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001993 if (args == NULL) {
1994 Py_DECREF(method);
1995 return NULL;
1996 }
1997
1998 res = PyEval_CallObject(method, args);
1999 Py_DECREF(args);
2000 Py_DECREF(method);
2001
2002 return res;
2003}
2004
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002005static PyObject *
2006instance_richcompare(PyObject *v, PyObject *w, int op)
2007{
2008 PyObject *res;
2009
2010 if (PyInstance_Check(v)) {
2011 res = half_richcompare(v, w, op);
2012 if (res != Py_NotImplemented)
2013 return res;
2014 Py_DECREF(res);
2015 }
2016
2017 if (PyInstance_Check(w)) {
Tim Petersf4aca752004-09-23 02:39:37 +00002018 res = half_richcompare(w, v, _Py_SwappedOp[op]);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002019 if (res != Py_NotImplemented)
2020 return res;
2021 Py_DECREF(res);
2022 }
2023
2024 Py_INCREF(Py_NotImplemented);
2025 return Py_NotImplemented;
2026}
2027
Neil Schemenauer29bfc072001-01-04 01:43:46 +00002028
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002029/* Get the iterator */
2030static PyObject *
2031instance_getiter(PyInstanceObject *self)
2032{
2033 PyObject *func;
2034
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002035 if (iterstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002036 iterstr = PyString_InternFromString("__iter__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002037 if (iterstr == NULL)
2038 return NULL;
2039 }
2040 if (getitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002041 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002042 if (getitemstr == NULL)
2043 return NULL;
2044 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002045
2046 if ((func = instance_getattr(self, iterstr)) != NULL) {
2047 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
2048 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002049 if (res != NULL && !PyIter_Check(res)) {
2050 PyErr_Format(PyExc_TypeError,
2051 "__iter__ returned non-iterator "
2052 "of type '%.100s'",
2053 res->ob_type->tp_name);
2054 Py_DECREF(res);
2055 res = NULL;
2056 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002057 return res;
2058 }
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002059 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2060 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002061 PyErr_Clear();
2062 if ((func = instance_getattr(self, getitemstr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002063 PyErr_SetString(PyExc_TypeError,
2064 "iteration over non-sequence");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002065 return NULL;
2066 }
2067 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002068 return PySeqIter_New((PyObject *)self);
2069}
2070
2071
2072/* Call the iterator's next */
2073static PyObject *
2074instance_iternext(PyInstanceObject *self)
2075{
2076 PyObject *func;
2077
Neal Norwitzb09f4f52006-08-13 18:10:28 +00002078 if (nextstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002079 nextstr = PyString_InternFromString("next");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00002080 if (nextstr == NULL)
2081 return NULL;
2082 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002083
2084 if ((func = instance_getattr(self, nextstr)) != NULL) {
2085 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
2086 Py_DECREF(func);
2087 if (res != NULL) {
2088 return res;
2089 }
2090 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
2091 PyErr_Clear();
2092 return NULL;
2093 }
2094 return NULL;
2095 }
2096 PyErr_SetString(PyExc_TypeError, "instance has no next() method");
2097 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002098}
2099
Tim Peters6d6c1a32001-08-02 04:15:00 +00002100static PyObject *
2101instance_call(PyObject *func, PyObject *arg, PyObject *kw)
2102{
2103 PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
2104 if (call == NULL) {
2105 PyInstanceObject *inst = (PyInstanceObject*) func;
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002106 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2107 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002108 PyErr_Clear();
2109 PyErr_Format(PyExc_AttributeError,
2110 "%.200s instance has no __call__ method",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002111 PyString_AsString(inst->in_class->cl_name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002112 return NULL;
2113 }
Guido van Rossum16b93b32002-06-13 21:32:51 +00002114 /* We must check and increment the recursion depth here. Scenario:
2115 class A:
2116 pass
2117 A.__call__ = A() # that's right
2118 a = A() # ok
2119 a() # infinite recursion
2120 This bounces between instance_call() and PyObject_Call() without
2121 ever hitting eval_frame() (which has the main recursion check). */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002122 if (Py_EnterRecursiveCall(" in __call__")) {
Guido van Rossum16b93b32002-06-13 21:32:51 +00002123 res = NULL;
2124 }
Armin Rigo2b3eb402003-10-28 12:05:48 +00002125 else {
Guido van Rossum16b93b32002-06-13 21:32:51 +00002126 res = PyObject_Call(call, arg, kw);
Armin Rigo2b3eb402003-10-28 12:05:48 +00002127 Py_LeaveRecursiveCall();
2128 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002129 Py_DECREF(call);
2130 return res;
2131}
2132
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002133
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002134static PyNumberMethods instance_as_number = {
Georg Brandl347b3002006-03-30 11:57:00 +00002135 instance_add, /* nb_add */
2136 instance_sub, /* nb_subtract */
2137 instance_mul, /* nb_multiply */
2138 instance_div, /* nb_divide */
2139 instance_mod, /* nb_remainder */
2140 instance_divmod, /* nb_divmod */
2141 instance_pow, /* nb_power */
2142 (unaryfunc)instance_neg, /* nb_negative */
2143 (unaryfunc)instance_pos, /* nb_positive */
2144 (unaryfunc)instance_abs, /* nb_absolute */
2145 (inquiry)instance_nonzero, /* nb_nonzero */
2146 (unaryfunc)instance_invert, /* nb_invert */
2147 instance_lshift, /* nb_lshift */
2148 instance_rshift, /* nb_rshift */
2149 instance_and, /* nb_and */
2150 instance_xor, /* nb_xor */
2151 instance_or, /* nb_or */
2152 instance_coerce, /* nb_coerce */
2153 (unaryfunc)instance_int, /* nb_int */
2154 (unaryfunc)instance_long, /* nb_long */
2155 (unaryfunc)instance_float, /* nb_float */
2156 (unaryfunc)instance_oct, /* nb_oct */
2157 (unaryfunc)instance_hex, /* nb_hex */
2158 instance_iadd, /* nb_inplace_add */
2159 instance_isub, /* nb_inplace_subtract */
2160 instance_imul, /* nb_inplace_multiply */
2161 instance_idiv, /* nb_inplace_divide */
2162 instance_imod, /* nb_inplace_remainder */
2163 instance_ipow, /* nb_inplace_power */
2164 instance_ilshift, /* nb_inplace_lshift */
2165 instance_irshift, /* nb_inplace_rshift */
2166 instance_iand, /* nb_inplace_and */
2167 instance_ixor, /* nb_inplace_xor */
2168 instance_ior, /* nb_inplace_or */
2169 instance_floordiv, /* nb_floor_divide */
2170 instance_truediv, /* nb_true_divide */
2171 instance_ifloordiv, /* nb_inplace_floor_divide */
2172 instance_itruediv, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00002173 (unaryfunc)instance_index, /* nb_index */
Guido van Rossum04691fc1992-08-12 15:35:34 +00002174};
2175
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002176PyTypeObject PyInstance_Type = {
2177 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002178 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +00002179 "instance",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002180 sizeof(PyInstanceObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002181 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002182 (destructor)instance_dealloc, /* tp_dealloc */
2183 0, /* tp_print */
2184 0, /* tp_getattr */
2185 0, /* tp_setattr */
2186 instance_compare, /* tp_compare */
2187 (reprfunc)instance_repr, /* tp_repr */
2188 &instance_as_number, /* tp_as_number */
2189 &instance_as_sequence, /* tp_as_sequence */
2190 &instance_as_mapping, /* tp_as_mapping */
2191 (hashfunc)instance_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002192 instance_call, /* tp_call */
Guido van Rossum82c690f2001-04-30 14:39:18 +00002193 (reprfunc)instance_str, /* tp_str */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002194 (getattrofunc)instance_getattr, /* tp_getattro */
2195 (setattrofunc)instance_setattr, /* tp_setattro */
2196 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002197 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002198 instance_doc, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00002199 (traverseproc)instance_traverse, /* tp_traverse */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002200 0, /* tp_clear */
2201 instance_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002202 offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
2203 (getiterfunc)instance_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002204 (iternextfunc)instance_iternext, /* tp_iternext */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002205 0, /* tp_methods */
2206 0, /* tp_members */
2207 0, /* tp_getset */
2208 0, /* tp_base */
2209 0, /* tp_dict */
2210 0, /* tp_descr_get */
2211 0, /* tp_descr_set */
2212 0, /* tp_dictoffset */
2213 0, /* tp_init */
2214 0, /* tp_alloc */
2215 instance_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002216};
2217
2218
Guido van Rossum81daa321993-05-20 14:24:46 +00002219/* Instance method objects are used for two purposes:
2220 (a) as bound instance methods (returned by instancename.methodname)
2221 (b) as unbound methods (returned by ClassName.methodname)
2222 In case (b), im_self is NULL
2223*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002224
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002225PyObject *
Anthony Baxter377be112006-04-11 06:54:30 +00002226PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002227{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002228 register PyMethodObject *im;
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002229 im = free_list;
2230 if (im != NULL) {
2231 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002232 PyObject_INIT(im, &PyMethod_Type);
Christian Heimes6075a822008-02-06 12:44:34 +00002233 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002234 }
2235 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002236 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002237 if (im == NULL)
2238 return NULL;
2239 }
Fred Drakedb81e8d2001-03-23 04:19:27 +00002240 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002241 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00002242 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002243 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +00002244 im->im_self = self;
Anthony Baxter377be112006-04-11 06:54:30 +00002245 Py_XINCREF(klass);
2246 im->im_class = klass;
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002247 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002248 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002249}
2250
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002251/* Descriptors for PyMethod attributes */
2252
2253/* im_class, im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002254
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002255#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002256
Guido van Rossum6f799372001-09-20 20:46:19 +00002257static PyMemberDef instancemethod_memberlist[] = {
2258 {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
2259 "the class associated with a method"},
2260 {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
2261 "the function (or other callable) implementing a method"},
Neal Norwitzb1d3d962008-03-18 04:46:00 +00002262 {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
2263 "the function (or other callable) implementing a method"},
Guido van Rossum6f799372001-09-20 20:46:19 +00002264 {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
2265 "the instance to which a method is bound; None for unbound methods"},
Neal Norwitzb1d3d962008-03-18 04:46:00 +00002266 {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
2267 "the instance to which a method is bound; None for unbound methods"},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002268 {NULL} /* Sentinel */
2269};
2270
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002271/* Christian Tismer argued convincingly that method attributes should
2272 (nearly) always override function attributes.
2273 The one exception is __doc__; there's a default __doc__ which
2274 should only be used for the class, not for instances */
2275
2276static PyObject *
2277instancemethod_get_doc(PyMethodObject *im, void *context)
2278{
2279 static PyObject *docstr;
2280 if (docstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002281 docstr= PyString_InternFromString("__doc__");
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002282 if (docstr == NULL)
2283 return NULL;
2284 }
2285 return PyObject_GetAttr(im->im_func, docstr);
2286}
2287
2288static PyGetSetDef instancemethod_getset[] = {
2289 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
2290 {0}
2291};
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002292
2293static PyObject *
2294instancemethod_getattro(PyObject *obj, PyObject *name)
2295{
2296 PyMethodObject *im = (PyMethodObject *)obj;
2297 PyTypeObject *tp = obj->ob_type;
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002298 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002299
Guido van Rossum915f0eb2001-10-17 20:26:38 +00002300 if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) {
2301 if (tp->tp_dict == NULL) {
2302 if (PyType_Ready(tp) < 0)
2303 return NULL;
2304 }
2305 descr = _PyType_Lookup(tp, name);
Barry Warsawd6a9e842001-01-15 20:40:19 +00002306 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002307
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002308 if (descr != NULL) {
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002309 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
2310 if (f != NULL)
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002311 return f(descr, obj, (PyObject *)obj->ob_type);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002312 else {
2313 Py_INCREF(descr);
2314 return descr;
2315 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002316 }
2317
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002318 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002319}
2320
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002321PyDoc_STRVAR(instancemethod_doc,
2322"instancemethod(function, instance, class)\n\
2323\n\
2324Create an instance method object.");
2325
2326static PyObject *
2327instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
2328{
2329 PyObject *func;
2330 PyObject *self;
Guido van Rossum2fb9fdc2003-04-09 19:35:08 +00002331 PyObject *classObj = NULL;
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002332
Georg Brandl5d59c092006-09-30 08:43:30 +00002333 if (!_PyArg_NoKeywords("instancemethod", kw))
2334 return NULL;
Guido van Rossum2fb9fdc2003-04-09 19:35:08 +00002335 if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3,
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002336 &func, &self, &classObj))
2337 return NULL;
2338 if (!PyCallable_Check(func)) {
2339 PyErr_SetString(PyExc_TypeError,
2340 "first argument must be callable");
2341 return NULL;
2342 }
2343 if (self == Py_None)
2344 self = NULL;
Michael W. Hudsone2749cb2005-03-30 16:32:10 +00002345 if (self == NULL && classObj == NULL) {
2346 PyErr_SetString(PyExc_TypeError,
2347 "unbound methods must have non-NULL im_class");
2348 return NULL;
2349 }
2350
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002351 return PyMethod_New(func, self, classObj);
2352}
2353
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002354static void
Fred Drake79912472000-07-09 04:06:11 +00002355instancemethod_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002356{
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002357 _PyObject_GC_UNTRACK(im);
Fred Drakec916f5a2001-10-26 17:56:51 +00002358 if (im->im_weakreflist != NULL)
2359 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002360 Py_DECREF(im->im_func);
2361 Py_XDECREF(im->im_self);
Guido van Rossumcdf0d752001-08-17 12:07:34 +00002362 Py_XDECREF(im->im_class);
Christian Heimes5b970ad2008-02-06 13:33:44 +00002363 if (numfree < PyMethod_MAXFREELIST) {
Christian Heimes6075a822008-02-06 12:44:34 +00002364 im->im_self = (PyObject *)free_list;
2365 free_list = im;
2366 numfree++;
2367 }
2368 else {
2369 PyObject_GC_Del(im);
2370 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002371}
2372
Guido van Rossumebc8c511992-09-03 20:39:51 +00002373static int
Fred Drake79912472000-07-09 04:06:11 +00002374instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
Guido van Rossumebc8c511992-09-03 20:39:51 +00002375{
Armin Rigofd01d792006-06-08 10:56:24 +00002376 int cmp;
2377 cmp = PyObject_Compare(a->im_func, b->im_func);
2378 if (cmp)
2379 return cmp;
2380
2381 if (a->im_self == b->im_self)
2382 return 0;
2383 if (a->im_self == NULL || b->im_self == NULL)
Guido van Rossume9df7271995-04-06 14:46:51 +00002384 return (a->im_self < b->im_self) ? -1 : 1;
Armin Rigofd01d792006-06-08 10:56:24 +00002385 else
2386 return PyObject_Compare(a->im_self, b->im_self);
Guido van Rossumebc8c511992-09-03 20:39:51 +00002387}
2388
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002389static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002390instancemethod_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +00002391{
Tim Peters6d6c1a32001-08-02 04:15:00 +00002392 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +00002393 PyObject *func = a->im_func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394 PyObject *klass = a->im_class;
2395 PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
2396 char *sfuncname = "?", *sklassname = "?";
2397
2398 funcname = PyObject_GetAttrString(func, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002399 if (funcname == NULL) {
2400 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2401 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002402 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002403 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002404 else if (!PyString_Check(funcname)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002405 Py_DECREF(funcname);
2406 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +00002407 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002409 sfuncname = PyString_AS_STRING(funcname);
Guido van Rossum40667692001-08-17 13:59:27 +00002410 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002411 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +00002412 else {
2413 klassname = PyObject_GetAttrString(klass, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002414 if (klassname == NULL) {
2415 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2416 return NULL;
Guido van Rossum40667692001-08-17 13:59:27 +00002417 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002418 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002419 else if (!PyString_Check(klassname)) {
Guido van Rossum40667692001-08-17 13:59:27 +00002420 Py_DECREF(klassname);
2421 klassname = NULL;
2422 }
2423 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002424 sklassname = PyString_AS_STRING(klassname);
Guido van Rossum7859f871998-07-08 14:58:16 +00002425 }
Guido van Rossum81daa321993-05-20 14:24:46 +00002426 if (self == NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002427 result = PyString_FromFormat("<unbound method %s.%s>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002428 sklassname, sfuncname);
Guido van Rossum81daa321993-05-20 14:24:46 +00002429 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002430 /* XXX Shouldn't use repr() here! */
2431 PyObject *selfrepr = PyObject_Repr(self);
2432 if (selfrepr == NULL)
2433 goto fail;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002434 if (!PyString_Check(selfrepr)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002435 Py_DECREF(selfrepr);
2436 goto fail;
2437 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002438 result = PyString_FromFormat("<bound method %s.%s of %s>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002439 sklassname, sfuncname,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002440 PyString_AS_STRING(selfrepr));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002441 Py_DECREF(selfrepr);
Guido van Rossum81daa321993-05-20 14:24:46 +00002442 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002443 fail:
Guido van Rossum42636dc1999-10-11 14:03:12 +00002444 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002445 Py_XDECREF(klassname);
2446 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00002447}
2448
Guido van Rossum9bfef441993-03-29 10:43:31 +00002449static long
Fred Drake79912472000-07-09 04:06:11 +00002450instancemethod_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002451{
2452 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +00002453 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002454 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +00002455 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002456 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002457 if (x == -1)
2458 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002459 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002460 if (y == -1)
2461 return -1;
Armin Rigofd01d792006-06-08 10:56:24 +00002462 x = x ^ y;
2463 if (x == -1)
2464 x = -2;
2465 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002466}
2467
Jeremy Hylton8caad492000-06-23 14:18:11 +00002468static int
2469instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
2470{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002471 Py_VISIT(im->im_func);
2472 Py_VISIT(im->im_self);
2473 Py_VISIT(im->im_class);
Jeremy Hyltond22162b2000-06-23 17:14:56 +00002474 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002475}
2476
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002477static void
Anthony Baxter377be112006-04-11 06:54:30 +00002478getclassname(PyObject *klass, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +00002479{
2480 PyObject *name;
2481
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002482 assert(bufsize > 1);
2483 strcpy(buf, "?"); /* Default outcome */
Anthony Baxter377be112006-04-11 06:54:30 +00002484 if (klass == NULL)
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002485 return;
Anthony Baxter377be112006-04-11 06:54:30 +00002486 name = PyObject_GetAttrString(klass, "__name__");
Guido van Rossuma15dece2001-08-24 18:48:27 +00002487 if (name == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002488 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +00002489 PyErr_Clear();
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002490 return;
Guido van Rossuma15dece2001-08-24 18:48:27 +00002491 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002492 if (PyString_Check(name)) {
2493 strncpy(buf, PyString_AS_STRING(name), bufsize);
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002494 buf[bufsize-1] = '\0';
Guido van Rossuma15dece2001-08-24 18:48:27 +00002495 }
Guido van Rossuma15dece2001-08-24 18:48:27 +00002496 Py_DECREF(name);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002497}
2498
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002499static void
2500getinstclassname(PyObject *inst, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +00002501{
Anthony Baxter377be112006-04-11 06:54:30 +00002502 PyObject *klass;
Guido van Rossuma15dece2001-08-24 18:48:27 +00002503
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002504 if (inst == NULL) {
Tim Peters75585d42002-08-20 14:31:35 +00002505 assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002506 strcpy(buf, "nothing");
2507 return;
2508 }
Guido van Rossuma15dece2001-08-24 18:48:27 +00002509
Anthony Baxter377be112006-04-11 06:54:30 +00002510 klass = PyObject_GetAttrString(inst, "__class__");
2511 if (klass == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002512 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +00002513 PyErr_Clear();
Anthony Baxter377be112006-04-11 06:54:30 +00002514 klass = (PyObject *)(inst->ob_type);
2515 Py_INCREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002516 }
Anthony Baxter377be112006-04-11 06:54:30 +00002517 getclassname(klass, buf, bufsize);
2518 Py_XDECREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002519}
2520
Tim Peters6d6c1a32001-08-02 04:15:00 +00002521static PyObject *
2522instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
2523{
2524 PyObject *self = PyMethod_GET_SELF(func);
Anthony Baxter377be112006-04-11 06:54:30 +00002525 PyObject *klass = PyMethod_GET_CLASS(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002526 PyObject *result;
2527
2528 func = PyMethod_GET_FUNCTION(func);
2529 if (self == NULL) {
2530 /* Unbound methods must be called with an instance of
2531 the class (or a derived class) as first argument */
2532 int ok;
2533 if (PyTuple_Size(arg) >= 1)
2534 self = PyTuple_GET_ITEM(arg, 0);
2535 if (self == NULL)
2536 ok = 0;
2537 else {
Anthony Baxter377be112006-04-11 06:54:30 +00002538 ok = PyObject_IsInstance(self, klass);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002539 if (ok < 0)
2540 return NULL;
2541 }
2542 if (!ok) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002543 char clsbuf[256];
2544 char instbuf[256];
Anthony Baxter377be112006-04-11 06:54:30 +00002545 getclassname(klass, clsbuf, sizeof(clsbuf));
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002546 getinstclassname(self, instbuf, sizeof(instbuf));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002547 PyErr_Format(PyExc_TypeError,
Guido van Rossuma15dece2001-08-24 18:48:27 +00002548 "unbound method %s%s must be called with "
2549 "%s instance as first argument "
2550 "(got %s%s instead)",
Tim Peters6d6c1a32001-08-02 04:15:00 +00002551 PyEval_GetFuncName(func),
Guido van Rossuma15dece2001-08-24 18:48:27 +00002552 PyEval_GetFuncDesc(func),
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002553 clsbuf,
2554 instbuf,
Guido van Rossuma15dece2001-08-24 18:48:27 +00002555 self == NULL ? "" : " instance");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002556 return NULL;
2557 }
2558 Py_INCREF(arg);
2559 }
2560 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002561 Py_ssize_t argcount = PyTuple_Size(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002562 PyObject *newarg = PyTuple_New(argcount + 1);
2563 int i;
2564 if (newarg == NULL)
2565 return NULL;
2566 Py_INCREF(self);
2567 PyTuple_SET_ITEM(newarg, 0, self);
2568 for (i = 0; i < argcount; i++) {
2569 PyObject *v = PyTuple_GET_ITEM(arg, i);
2570 Py_XINCREF(v);
2571 PyTuple_SET_ITEM(newarg, i+1, v);
2572 }
2573 arg = newarg;
2574 }
2575 result = PyObject_Call((PyObject *)func, arg, kw);
2576 Py_DECREF(arg);
2577 return result;
2578}
2579
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002580static PyObject *
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002581instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002582{
Guido van Rossumf23c41d2001-08-17 13:43:27 +00002583 /* Don't rebind an already bound method, or an unbound method
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002584 of a class that's not a base class of cls. */
2585
2586 if (PyMethod_GET_SELF(meth) != NULL) {
2587 /* Already bound */
Guido van Rossum501c7c72001-08-16 20:41:56 +00002588 Py_INCREF(meth);
2589 return meth;
2590 }
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002591 /* No, it is an unbound method */
2592 if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) {
2593 /* Do subclass test. If it fails, return meth unchanged. */
2594 int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth));
2595 if (ok < 0)
2596 return NULL;
2597 if (!ok) {
2598 Py_INCREF(meth);
2599 return meth;
2600 }
2601 }
2602 /* Bind it to obj */
2603 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls);
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002604}
2605
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002606PyTypeObject PyMethod_Type = {
2607 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002608 0,
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002609 "instancemethod",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002610 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002611 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002612 (destructor)instancemethod_dealloc, /* tp_dealloc */
2613 0, /* tp_print */
2614 0, /* tp_getattr */
2615 0, /* tp_setattr */
2616 (cmpfunc)instancemethod_compare, /* tp_compare */
2617 (reprfunc)instancemethod_repr, /* tp_repr */
2618 0, /* tp_as_number */
2619 0, /* tp_as_sequence */
2620 0, /* tp_as_mapping */
2621 (hashfunc)instancemethod_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002622 instancemethod_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002623 0, /* tp_str */
Georg Brandl347b3002006-03-30 11:57:00 +00002624 instancemethod_getattro, /* tp_getattro */
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002625 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002626 0, /* tp_as_buffer */
Raymond Hettingerbff60ae2005-06-19 08:42:20 +00002627 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002628 instancemethod_doc, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00002629 (traverseproc)instancemethod_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +00002630 0, /* tp_clear */
2631 0, /* tp_richcompare */
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002632 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
2633 0, /* tp_iter */
2634 0, /* tp_iternext */
2635 0, /* tp_methods */
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002636 instancemethod_memberlist, /* tp_members */
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002637 instancemethod_getset, /* tp_getset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002638 0, /* tp_base */
2639 0, /* tp_dict */
2640 instancemethod_descr_get, /* tp_descr_get */
2641 0, /* tp_descr_set */
2642 0, /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002643 0, /* tp_init */
2644 0, /* tp_alloc */
2645 instancemethod_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002646};
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002647
2648/* Clear out the free list */
2649
Christian Heimes3b718a72008-02-14 12:47:33 +00002650int
2651PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002652{
Christian Heimes3b718a72008-02-14 12:47:33 +00002653 int freelist_size = numfree;
2654
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002655 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00002656 PyMethodObject *im = free_list;
2657 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002658 PyObject_GC_Del(im);
Christian Heimes6075a822008-02-06 12:44:34 +00002659 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002660 }
Christian Heimes6075a822008-02-06 12:44:34 +00002661 assert(numfree == 0);
Christian Heimes3b718a72008-02-14 12:47:33 +00002662 return freelist_size;
2663}
2664
2665void
2666PyMethod_Fini(void)
2667{
2668 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002669}