blob: caf6b3e7b0de0a5e82a0f255a5a27d4dfca2e658 [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));
Tim Peters34592512002-07-11 06:23:50 +00001177 } else
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001178 arg = Py_BuildValue("(nn)", i, j);
Tim Peters34592512002-07-11 06:23:50 +00001179
Guido van Rossum04691fc1992-08-12 15:35:34 +00001180 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001181 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001182 return NULL;
1183 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001184 res = PyEval_CallObject(func, arg);
1185 Py_DECREF(func);
1186 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001187 return res;
1188}
1189
1190static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001191instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001192{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001193 PyObject *func, *arg, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001194
Guido van Rossum2878a691996-08-09 20:53:24 +00001195 if (item == NULL) {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001196 if (delitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001197 delitemstr = PyString_InternFromString("__delitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001198 if (delitemstr == NULL)
1199 return -1;
1200 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001201 func = instance_getattr(inst, delitemstr);
1202 }
1203 else {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001204 if (setitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001205 setitemstr = PyString_InternFromString("__setitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001206 if (setitemstr == NULL)
1207 return -1;
1208 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001209 func = instance_getattr(inst, setitemstr);
1210 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001211 if (func == NULL)
Guido van Rossum6d946f91992-08-14 13:49:30 +00001212 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001213 if (item == NULL)
Georg Brandl2cfaa342006-05-29 19:39:45 +00001214 arg = PyInt_FromSsize_t(i);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001215 else
Georg Brandl2cfaa342006-05-29 19:39:45 +00001216 arg = Py_BuildValue("(nO)", i, item);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001217 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001218 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001219 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001220 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001221 res = PyEval_CallObject(func, arg);
1222 Py_DECREF(func);
1223 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001224 if (res == NULL)
1225 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001226 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001227 return 0;
1228}
1229
1230static int
Martin v. Löwis18e16552006-02-15 17:27:45 +00001231instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001232{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001233 PyObject *func, *arg, *res;
1234 static PyObject *setslicestr, *delslicestr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001235
Guido van Rossum2878a691996-08-09 20:53:24 +00001236 if (value == NULL) {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001237 if (delslicestr == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001238 delslicestr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001239 PyString_InternFromString("__delslice__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001240 if (delslicestr == NULL)
1241 return -1;
1242 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001243 func = instance_getattr(inst, delslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001244 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001245 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1246 return -1;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001247 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001248 if (delitemstr == NULL) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001249 delitemstr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001250 PyString_InternFromString("__delitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001251 if (delitemstr == NULL)
1252 return -1;
1253 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001254 func = instance_getattr(inst, delitemstr);
1255 if (func == NULL)
1256 return -1;
1257
1258 arg = Py_BuildValue("(N)",
Neal Norwitzbadc0862006-03-23 06:03:08 +00001259 _PySlice_FromIndices(i, j));
Thomas Wouters1d75a792000-08-17 22:37:32 +00001260 } else
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001261 arg = Py_BuildValue("(nn)", i, j);
Guido van Rossum2878a691996-08-09 20:53:24 +00001262 }
1263 else {
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001264 if (setslicestr == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001265 setslicestr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001266 PyString_InternFromString("__setslice__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001267 if (setslicestr == NULL)
1268 return -1;
1269 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001270 func = instance_getattr(inst, setslicestr);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001271 if (func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001272 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1273 return -1;
Thomas Wouters1d75a792000-08-17 22:37:32 +00001274 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001275 if (setitemstr == NULL) {
Thomas Wouters1d75a792000-08-17 22:37:32 +00001276 setitemstr =
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001277 PyString_InternFromString("__setitem__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001278 if (setitemstr == NULL)
1279 return -1;
1280 }
Thomas Wouters1d75a792000-08-17 22:37:32 +00001281 func = instance_getattr(inst, setitemstr);
1282 if (func == NULL)
1283 return -1;
1284
1285 arg = Py_BuildValue("(NO)",
Neal Norwitzbadc0862006-03-23 06:03:08 +00001286 _PySlice_FromIndices(i, j), value);
Thomas Wouters1d75a792000-08-17 22:37:32 +00001287 } else
Martin v. Löwisdde99d22006-02-17 15:57:41 +00001288 arg = Py_BuildValue("(nnO)", i, j, value);
Guido van Rossum2878a691996-08-09 20:53:24 +00001289 }
Guido van Rossum04691fc1992-08-12 15:35:34 +00001290 if (arg == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001291 Py_DECREF(func);
Guido van Rossum6d946f91992-08-14 13:49:30 +00001292 return -1;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001293 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001294 res = PyEval_CallObject(func, arg);
1295 Py_DECREF(func);
1296 Py_DECREF(arg);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001297 if (res == NULL)
1298 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001299 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001300 return 0;
1301}
1302
Tim Peterscb8d3682001-05-05 21:05:01 +00001303static int
1304instance_contains(PyInstanceObject *inst, PyObject *member)
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001305{
1306 static PyObject *__contains__;
Tim Peterscb8d3682001-05-05 21:05:01 +00001307 PyObject *func;
1308
1309 /* Try __contains__ first.
1310 * If that can't be done, try iterator-based searching.
1311 */
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001312
1313 if(__contains__ == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001314 __contains__ = PyString_InternFromString("__contains__");
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001315 if(__contains__ == NULL)
1316 return -1;
1317 }
1318 func = instance_getattr(inst, __contains__);
Tim Peterscb8d3682001-05-05 21:05:01 +00001319 if (func) {
1320 PyObject *res;
1321 int ret;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001322 PyObject *arg = PyTuple_Pack(1, member);
Tim Peterscb8d3682001-05-05 21:05:01 +00001323 if(arg == NULL) {
1324 Py_DECREF(func);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001325 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001326 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001327 res = PyEval_CallObject(func, arg);
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001328 Py_DECREF(func);
Tim Peterscb8d3682001-05-05 21:05:01 +00001329 Py_DECREF(arg);
Tim Peters34592512002-07-11 06:23:50 +00001330 if(res == NULL)
Tim Peterscb8d3682001-05-05 21:05:01 +00001331 return -1;
1332 ret = PyObject_IsTrue(res);
1333 Py_DECREF(res);
1334 return ret;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001335 }
Tim Peterscb8d3682001-05-05 21:05:01 +00001336
1337 /* Couldn't find __contains__. */
1338 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
Martin v. Löwis3a624042006-11-08 06:46:37 +00001339 Py_ssize_t rc;
Tim Peterscb8d3682001-05-05 21:05:01 +00001340 /* Assume the failure was simply due to that there is no
1341 * __contains__ attribute, and try iterating instead.
1342 */
1343 PyErr_Clear();
Martin v. Löwis3a624042006-11-08 06:46:37 +00001344 rc = _PySequence_IterSearch((PyObject *)inst, member,
1345 PY_ITERSEARCH_CONTAINS);
1346 if (rc >= 0)
1347 return rc > 0;
Tim Peterscb8d3682001-05-05 21:05:01 +00001348 }
Martin v. Löwis3a624042006-11-08 06:46:37 +00001349 return -1;
Guido van Rossumee28c3a2000-02-28 15:03:15 +00001350}
1351
Fred Drake79912472000-07-09 04:06:11 +00001352static PySequenceMethods
1353instance_as_sequence = {
Martin v. Löwis18e16552006-02-15 17:27:45 +00001354 (lenfunc)instance_length, /* sq_length */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001355 0, /* sq_concat */
1356 0, /* sq_repeat */
Martin v. Löwis18e16552006-02-15 17:27:45 +00001357 (ssizeargfunc)instance_item, /* sq_item */
1358 (ssizessizeargfunc)instance_slice, /* sq_slice */
1359 (ssizeobjargproc)instance_ass_item, /* sq_ass_item */
1360 (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001361 (objobjproc)instance_contains, /* sq_contains */
Guido van Rossum04691fc1992-08-12 15:35:34 +00001362};
1363
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001364static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001365generic_unary_op(PyInstanceObject *self, PyObject *methodname)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001366{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001367 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001368
1369 if ((func = instance_getattr(self, methodname)) == NULL)
1370 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001371 res = PyEval_CallObject(func, (PyObject *)NULL);
1372 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001373 return res;
1374}
1375
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001376static PyObject *
1377generic_binary_op(PyObject *v, PyObject *w, char *opname)
Guido van Rossum03093a21994-09-28 15:51:32 +00001378{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001379 PyObject *result;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001380 PyObject *args;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001381 PyObject *func = PyObject_GetAttrString(v, opname);
Guido van Rossume7d444f1995-01-07 12:35:18 +00001382 if (func == NULL) {
Guido van Rossum617c1b01998-05-28 19:50:02 +00001383 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001384 return NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001385 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001386 Py_INCREF(Py_NotImplemented);
1387 return Py_NotImplemented;
Guido van Rossume7d444f1995-01-07 12:35:18 +00001388 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001389 args = PyTuple_Pack(1, w);
Guido van Rossum03093a21994-09-28 15:51:32 +00001390 if (args == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001391 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001392 return NULL;
Guido van Rossum03093a21994-09-28 15:51:32 +00001393 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001394 result = PyEval_CallObject(func, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001395 Py_DECREF(args);
1396 Py_DECREF(func);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001397 return result;
1398}
1399
1400
1401static PyObject *coerce_obj;
1402
1403/* Try one half of a binary operator involving a class instance. */
1404static PyObject *
Tim Peters34592512002-07-11 06:23:50 +00001405half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc,
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001406 int swapped)
1407{
1408 PyObject *args;
1409 PyObject *coercefunc;
1410 PyObject *coerced = NULL;
1411 PyObject *v1;
1412 PyObject *result;
Tim Peters34592512002-07-11 06:23:50 +00001413
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001414 if (!PyInstance_Check(v)) {
1415 Py_INCREF(Py_NotImplemented);
1416 return Py_NotImplemented;
1417 }
1418
1419 if (coerce_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001420 coerce_obj = PyString_InternFromString("__coerce__");
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001421 if (coerce_obj == NULL)
1422 return NULL;
1423 }
1424 coercefunc = PyObject_GetAttr(v, coerce_obj);
1425 if (coercefunc == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001426 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1427 return NULL;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001428 PyErr_Clear();
1429 return generic_binary_op(v, w, opname);
1430 }
1431
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001432 args = PyTuple_Pack(1, w);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001433 if (args == NULL) {
Guido van Rossum617080b2002-10-18 14:15:33 +00001434 Py_DECREF(coercefunc);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001435 return NULL;
1436 }
1437 coerced = PyEval_CallObject(coercefunc, args);
1438 Py_DECREF(args);
1439 Py_DECREF(coercefunc);
1440 if (coerced == NULL) {
1441 return NULL;
1442 }
1443 if (coerced == Py_None || coerced == Py_NotImplemented) {
1444 Py_DECREF(coerced);
1445 return generic_binary_op(v, w, opname);
1446 }
1447 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
1448 Py_DECREF(coerced);
1449 PyErr_SetString(PyExc_TypeError,
1450 "coercion should return None or 2-tuple");
1451 return NULL;
1452 }
1453 v1 = PyTuple_GetItem(coerced, 0);
1454 w = PyTuple_GetItem(coerced, 1);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001455 if (v1->ob_type == v->ob_type && PyInstance_Check(v)) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001456 /* prevent recursion if __coerce__ returns self as the first
1457 * argument */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001458 result = generic_binary_op(v1, w, opname);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001459 } else {
Brett Cannonea3912b2006-06-13 21:46:41 +00001460 if (Py_EnterRecursiveCall(" after coercion"))
1461 return NULL;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001462 if (swapped)
1463 result = (thisfunc)(w, v1);
1464 else
1465 result = (thisfunc)(v1, w);
Brett Cannonea3912b2006-06-13 21:46:41 +00001466 Py_LeaveRecursiveCall();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001467 }
1468 Py_DECREF(coerced);
1469 return result;
1470}
1471
1472/* Implement a binary operator involving at least one class instance. */
1473static PyObject *
1474do_binop(PyObject *v, PyObject *w, char *opname, char *ropname,
1475 binaryfunc thisfunc)
1476{
1477 PyObject *result = half_binop(v, w, opname, thisfunc, 0);
1478 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001479 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001480 result = half_binop(w, v, ropname, thisfunc, 1);
1481 }
1482 return result;
1483}
1484
1485static PyObject *
1486do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname,
1487 char *ropname, binaryfunc thisfunc)
1488{
1489 PyObject *result = half_binop(v, w, iopname, thisfunc, 0);
1490 if (result == Py_NotImplemented) {
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001491 Py_DECREF(result);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001492 result = do_binop(v, w, opname, ropname, thisfunc);
1493 }
1494 return result;
Guido van Rossum03093a21994-09-28 15:51:32 +00001495}
1496
Guido van Rossum879c5811995-01-10 15:24:06 +00001497static int
Fred Drake79912472000-07-09 04:06:11 +00001498instance_coerce(PyObject **pv, PyObject **pw)
Guido van Rossum879c5811995-01-10 15:24:06 +00001499{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001500 PyObject *v = *pv;
1501 PyObject *w = *pw;
Guido van Rossum3931df91997-11-18 19:23:07 +00001502 PyObject *coercefunc;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001503 PyObject *args;
1504 PyObject *coerced;
Guido van Rossum879c5811995-01-10 15:24:06 +00001505
Guido van Rossum2878a691996-08-09 20:53:24 +00001506 if (coerce_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001507 coerce_obj = PyString_InternFromString("__coerce__");
Guido van Rossum2878a691996-08-09 20:53:24 +00001508 if (coerce_obj == NULL)
1509 return -1;
1510 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001511 coercefunc = PyObject_GetAttr(v, coerce_obj);
1512 if (coercefunc == NULL) {
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001513 /* No __coerce__ method */
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001514 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1515 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001516 PyErr_Clear();
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001517 return 1;
Guido van Rossum879c5811995-01-10 15:24:06 +00001518 }
1519 /* Has __coerce__ method: call it */
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001520 args = PyTuple_Pack(1, w);
Guido van Rossum879c5811995-01-10 15:24:06 +00001521 if (args == NULL) {
1522 return -1;
1523 }
Guido van Rossum3931df91997-11-18 19:23:07 +00001524 coerced = PyEval_CallObject(coercefunc, args);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001525 Py_DECREF(args);
Guido van Rossum3931df91997-11-18 19:23:07 +00001526 Py_DECREF(coercefunc);
Guido van Rossum879c5811995-01-10 15:24:06 +00001527 if (coerced == NULL) {
1528 /* __coerce__ call raised an exception */
1529 return -1;
1530 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001531 if (coerced == Py_None || coerced == Py_NotImplemented) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001532 /* __coerce__ says "I can't do it" */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001533 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001534 return 1;
1535 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001536 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) {
Guido van Rossum879c5811995-01-10 15:24:06 +00001537 /* __coerce__ return value is malformed */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001538 Py_DECREF(coerced);
1539 PyErr_SetString(PyExc_TypeError,
Guido van Rossum879c5811995-01-10 15:24:06 +00001540 "coercion should return None or 2-tuple");
1541 return -1;
1542 }
1543 /* __coerce__ returned two new values */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001544 *pv = PyTuple_GetItem(coerced, 0);
1545 *pw = PyTuple_GetItem(coerced, 1);
1546 Py_INCREF(*pv);
1547 Py_INCREF(*pw);
1548 Py_DECREF(coerced);
Guido van Rossum879c5811995-01-10 15:24:06 +00001549 return 0;
1550}
1551
Guido van Rossum04691fc1992-08-12 15:35:34 +00001552#define UNARY(funcname, methodname) \
Thomas Woutersc3073522000-07-23 22:09:59 +00001553static PyObject *funcname(PyInstanceObject *self) { \
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001554 static PyObject *o; \
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001555 if (o == NULL) { o = PyString_InternFromString(methodname); \
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001556 if (o == NULL) return NULL; } \
Guido van Rossum2878a691996-08-09 20:53:24 +00001557 return generic_unary_op(self, o); \
Guido van Rossum04691fc1992-08-12 15:35:34 +00001558}
1559
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001560/* unary function with a fallback */
1561#define UNARY_FB(funcname, methodname, funcname_fb) \
1562static PyObject *funcname(PyInstanceObject *self) { \
1563 static PyObject *o; \
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001564 if (o == NULL) { o = PyString_InternFromString(methodname); \
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001565 if (o == NULL) return NULL; } \
1566 if (PyObject_HasAttr((PyObject*)self, o)) \
1567 return generic_unary_op(self, o); \
1568 else \
1569 return funcname_fb(self); \
1570}
1571
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001572#define BINARY(f, m, n) \
1573static PyObject *f(PyObject *v, PyObject *w) { \
1574 return do_binop(v, w, "__" m "__", "__r" m "__", n); \
1575}
1576
1577#define BINARY_INPLACE(f, m, n) \
1578static PyObject *f(PyObject *v, PyObject *w) { \
1579 return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \
1580 "__r" m "__", n); \
1581}
1582
Guido van Rossum04691fc1992-08-12 15:35:34 +00001583UNARY(instance_neg, "__neg__")
1584UNARY(instance_pos, "__pos__")
1585UNARY(instance_abs, "__abs__")
1586
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001587BINARY(instance_or, "or", PyNumber_Or)
1588BINARY(instance_and, "and", PyNumber_And)
1589BINARY(instance_xor, "xor", PyNumber_Xor)
1590BINARY(instance_lshift, "lshift", PyNumber_Lshift)
1591BINARY(instance_rshift, "rshift", PyNumber_Rshift)
1592BINARY(instance_add, "add", PyNumber_Add)
1593BINARY(instance_sub, "sub", PyNumber_Subtract)
1594BINARY(instance_mul, "mul", PyNumber_Multiply)
1595BINARY(instance_div, "div", PyNumber_Divide)
1596BINARY(instance_mod, "mod", PyNumber_Remainder)
1597BINARY(instance_divmod, "divmod", PyNumber_Divmod)
Guido van Rossum4668b002001-08-08 05:00:18 +00001598BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide)
1599BINARY(instance_truediv, "truediv", PyNumber_TrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001600
1601BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr)
1602BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor)
1603BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd)
1604BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift)
1605BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift)
1606BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd)
1607BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract)
1608BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply)
1609BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide)
1610BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder)
Guido van Rossum4668b002001-08-08 05:00:18 +00001611BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide)
1612BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001613
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001614/* Try a 3-way comparison, returning an int; v is an instance. Return:
1615 -2 for an exception;
1616 -1 if v < w;
1617 0 if v == w;
1618 1 if v > w;
1619 2 if this particular 3-way comparison is not implemented or undefined.
1620*/
1621static int
1622half_cmp(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001623{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001624 static PyObject *cmp_obj;
1625 PyObject *args;
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001626 PyObject *cmp_func;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001627 PyObject *result;
1628 long l;
1629
1630 assert(PyInstance_Check(v));
1631
1632 if (cmp_obj == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001633 cmp_obj = PyString_InternFromString("__cmp__");
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001634 if (cmp_obj == NULL)
1635 return -2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001636 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001637
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001638 cmp_func = PyObject_GetAttr(v, cmp_obj);
1639 if (cmp_func == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001640 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1641 return -2;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001642 PyErr_Clear();
1643 return 2;
1644 }
1645
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001646 args = PyTuple_Pack(1, w);
Guido van Rossum617080b2002-10-18 14:15:33 +00001647 if (args == NULL) {
1648 Py_DECREF(cmp_func);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001649 return -2;
Guido van Rossum617080b2002-10-18 14:15:33 +00001650 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001651
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001652 result = PyEval_CallObject(cmp_func, args);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001653 Py_DECREF(args);
Guido van Rossum3202c6f2001-01-29 23:50:25 +00001654 Py_DECREF(cmp_func);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001655
1656 if (result == NULL)
1657 return -2;
1658
1659 if (result == Py_NotImplemented) {
1660 Py_DECREF(result);
1661 return 2;
1662 }
1663
1664 l = PyInt_AsLong(result);
1665 Py_DECREF(result);
1666 if (l == -1 && PyErr_Occurred()) {
1667 PyErr_SetString(PyExc_TypeError,
1668 "comparison did not return an int");
1669 return -2;
1670 }
1671
1672 return l < 0 ? -1 : l > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001673}
1674
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001675/* Try a 3-way comparison, returning an int; either v or w is an instance.
1676 We first try a coercion. Return:
1677 -2 for an exception;
1678 -1 if v < w;
1679 0 if v == w;
1680 1 if v > w;
1681 2 if this particular 3-way comparison is not implemented or undefined.
1682 THIS IS ONLY CALLED FROM object.c!
1683*/
1684static int
1685instance_compare(PyObject *v, PyObject *w)
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001686{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001687 int c;
1688
1689 c = PyNumber_CoerceEx(&v, &w);
1690 if (c < 0)
1691 return -2;
1692 if (c == 0) {
1693 /* If neither is now an instance, use regular comparison */
1694 if (!PyInstance_Check(v) && !PyInstance_Check(w)) {
1695 c = PyObject_Compare(v, w);
1696 Py_DECREF(v);
1697 Py_DECREF(w);
1698 if (PyErr_Occurred())
1699 return -2;
1700 return c < 0 ? -1 : c > 0 ? 1 : 0;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001701 }
1702 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001703 else {
1704 /* The coercion didn't do anything.
1705 Treat this the same as returning v and w unchanged. */
1706 Py_INCREF(v);
1707 Py_INCREF(w);
1708 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001709
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001710 if (PyInstance_Check(v)) {
1711 c = half_cmp(v, w);
1712 if (c <= 1) {
1713 Py_DECREF(v);
1714 Py_DECREF(w);
1715 return c;
1716 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001717 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001718 if (PyInstance_Check(w)) {
1719 c = half_cmp(w, v);
1720 if (c <= 1) {
1721 Py_DECREF(v);
1722 Py_DECREF(w);
1723 if (c >= -1)
1724 c = -c;
1725 return c;
1726 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001727 }
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001728 Py_DECREF(v);
1729 Py_DECREF(w);
1730 return 2;
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001731}
1732
Guido van Rossum9bfef441993-03-29 10:43:31 +00001733static int
Fred Drake79912472000-07-09 04:06:11 +00001734instance_nonzero(PyInstanceObject *self)
Guido van Rossum04691fc1992-08-12 15:35:34 +00001735{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001736 PyObject *func, *res;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001737 long outcome;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001738 static PyObject *nonzerostr;
Guido van Rossum04691fc1992-08-12 15:35:34 +00001739
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001740 if (nonzerostr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001741 nonzerostr = PyString_InternFromString("__nonzero__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001742 if (nonzerostr == NULL)
1743 return -1;
1744 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001745 if ((func = instance_getattr(self, nonzerostr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001746 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1747 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001748 PyErr_Clear();
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001749 if (lenstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001750 lenstr = PyString_InternFromString("__len__");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00001751 if (lenstr == NULL)
1752 return -1;
1753 }
Guido van Rossum2878a691996-08-09 20:53:24 +00001754 if ((func = instance_getattr(self, lenstr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00001755 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1756 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001757 PyErr_Clear();
Guido van Rossum04691fc1992-08-12 15:35:34 +00001758 /* Fall back to the default behavior:
1759 all instances are nonzero */
1760 return 1;
1761 }
1762 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001763 res = PyEval_CallObject(func, (PyObject *)NULL);
1764 Py_DECREF(func);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001765 if (res == NULL)
1766 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001767 if (!PyInt_Check(res)) {
1768 Py_DECREF(res);
1769 PyErr_SetString(PyExc_TypeError,
1770 "__nonzero__ should return an int");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001771 return -1;
1772 }
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001773 outcome = PyInt_AsLong(res);
1774 Py_DECREF(res);
Guido van Rossum04691fc1992-08-12 15:35:34 +00001775 if (outcome < 0) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001776 PyErr_SetString(PyExc_ValueError,
1777 "__nonzero__ should return >= 0");
Guido van Rossum04691fc1992-08-12 15:35:34 +00001778 return -1;
1779 }
1780 return outcome > 0;
1781}
1782
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001783static PyObject *
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001784instance_index(PyInstanceObject *self)
1785{
1786 PyObject *func, *res;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001787 static PyObject *indexstr = NULL;
1788
1789 if (indexstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001790 indexstr = PyString_InternFromString("__index__");
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001791 if (indexstr == NULL)
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001792 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001793 }
1794 if ((func = instance_getattr(self, indexstr)) == NULL) {
1795 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001796 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001797 PyErr_Clear();
1798 PyErr_SetString(PyExc_TypeError,
1799 "object cannot be interpreted as an index");
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001800 return NULL;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001801 }
1802 res = PyEval_CallObject(func, (PyObject *)NULL);
1803 Py_DECREF(func);
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00001804 return res;
Guido van Rossum38fff8c2006-03-07 18:50:55 +00001805}
1806
1807
Guido van Rossum04691fc1992-08-12 15:35:34 +00001808UNARY(instance_invert, "__invert__")
Jeffrey Yasskina26cf9b2008-02-04 01:04:35 +00001809UNARY(_instance_trunc, "__trunc__")
1810
1811static PyObject *
1812instance_int(PyInstanceObject *self)
1813{
1814 PyObject *truncated;
1815 static PyObject *int_name;
1816 if (int_name == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001817 int_name = PyString_InternFromString("__int__");
Jeffrey Yasskina26cf9b2008-02-04 01:04:35 +00001818 if (int_name == NULL)
1819 return NULL;
1820 }
1821 if (PyObject_HasAttr((PyObject*)self, int_name))
1822 return generic_unary_op(self, int_name);
1823
1824 truncated = _instance_trunc(self);
1825 /* __trunc__ is specified to return an Integral type, but
1826 int() needs to return an int. */
1827 return _PyNumber_ConvertIntegralToInt(
1828 truncated,
1829 "__trunc__ returned non-Integral (type %.200s)");
1830}
1831
Kristján Valur Jónssonabe1d482007-05-07 16:46:54 +00001832UNARY_FB(instance_long, "__long__", instance_int)
Guido van Rossum1899c2e1992-09-12 11:09:23 +00001833UNARY(instance_float, "__float__")
1834UNARY(instance_oct, "__oct__")
1835UNARY(instance_hex, "__hex__")
1836
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001837static PyObject *
1838bin_power(PyObject *v, PyObject *w)
1839{
1840 return PyNumber_Power(v, w, Py_None);
1841}
1842
Guido van Rossum03093a21994-09-28 15:51:32 +00001843/* This version is for ternary calls only (z != None) */
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001844static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00001845instance_pow(PyObject *v, PyObject *w, PyObject *z)
Tim Peters34592512002-07-11 06:23:50 +00001846{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001847 if (z == Py_None) {
1848 return do_binop(v, w, "__pow__", "__rpow__", bin_power);
Guido van Rossum03093a21994-09-28 15:51:32 +00001849 }
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001850 else {
1851 PyObject *func;
1852 PyObject *args;
1853 PyObject *result;
1854
1855 /* XXX Doesn't do coercions... */
1856 func = PyObject_GetAttrString(v, "__pow__");
1857 if (func == NULL)
1858 return NULL;
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001859 args = PyTuple_Pack(2, w, z);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001860 if (args == NULL) {
1861 Py_DECREF(func);
1862 return NULL;
1863 }
1864 result = PyEval_CallObject(func, args);
1865 Py_DECREF(func);
1866 Py_DECREF(args);
1867 return result;
1868 }
Guido van Rossum03093a21994-09-28 15:51:32 +00001869}
1870
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001871static PyObject *
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001872bin_inplace_power(PyObject *v, PyObject *w)
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001873{
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001874 return PyNumber_InPlacePower(v, w, Py_None);
Thomas Wouterse289e0b2000-08-24 20:08:19 +00001875}
1876
1877
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001878static PyObject *
1879instance_ipow(PyObject *v, PyObject *w, PyObject *z)
1880{
1881 if (z == Py_None) {
1882 return do_binop_inplace(v, w, "__ipow__", "__pow__",
1883 "__rpow__", bin_inplace_power);
1884 }
1885 else {
1886 /* XXX Doesn't do coercions... */
1887 PyObject *func;
1888 PyObject *args;
1889 PyObject *result;
1890
1891 func = PyObject_GetAttrString(v, "__ipow__");
1892 if (func == NULL) {
1893 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1894 return NULL;
1895 PyErr_Clear();
1896 return instance_pow(v, w, z);
1897 }
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001898 args = PyTuple_Pack(2, w, z);
Neil Schemenauer29bfc072001-01-04 01:43:46 +00001899 if (args == NULL) {
1900 Py_DECREF(func);
1901 return NULL;
1902 }
1903 result = PyEval_CallObject(func, args);
1904 Py_DECREF(func);
1905 Py_DECREF(args);
1906 return result;
1907 }
1908}
1909
1910
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001911/* Map rich comparison operators to their __xx__ namesakes */
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001912#define NAME_OPS 6
1913static PyObject **name_op = NULL;
1914
Tim Peters34592512002-07-11 06:23:50 +00001915static int
Guido van Rossum0ba9e3a2001-05-22 02:33:08 +00001916init_name_op(void)
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001917{
1918 int i;
1919 char *_name_op[] = {
1920 "__lt__",
1921 "__le__",
1922 "__eq__",
1923 "__ne__",
1924 "__gt__",
1925 "__ge__",
1926 };
1927
1928 name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS);
1929 if (name_op == NULL)
1930 return -1;
1931 for (i = 0; i < NAME_OPS; ++i) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001932 name_op[i] = PyString_InternFromString(_name_op[i]);
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001933 if (name_op[i] == NULL)
1934 return -1;
1935 }
1936 return 0;
1937}
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001938
1939static PyObject *
1940half_richcompare(PyObject *v, PyObject *w, int op)
1941{
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001942 PyObject *method;
1943 PyObject *args;
1944 PyObject *res;
1945
1946 assert(PyInstance_Check(v));
1947
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001948 if (name_op == NULL) {
1949 if (init_name_op() < 0)
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001950 return NULL;
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001951 }
1952 /* If the instance doesn't define an __getattr__ method, use
1953 instance_getattr2 directly because it will not set an
1954 exception on failure. */
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001955 if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL)
Tim Peters34592512002-07-11 06:23:50 +00001956 method = instance_getattr2((PyInstanceObject *)v,
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001957 name_op[op]);
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001958 else
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001959 method = PyObject_GetAttr(v, name_op[op]);
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001960 if (method == NULL) {
1961 if (PyErr_Occurred()) {
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001962 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
1963 return NULL;
1964 PyErr_Clear();
Jeremy Hylton1b0feb42001-05-11 14:48:41 +00001965 }
Guido van Rossumf740bdf2002-10-29 18:36:40 +00001966 res = Py_NotImplemented;
1967 Py_INCREF(res);
1968 return res;
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001969 }
1970
Raymond Hettinger8ae46892003-10-12 19:09:37 +00001971 args = PyTuple_Pack(1, w);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001972 if (args == NULL) {
1973 Py_DECREF(method);
1974 return NULL;
1975 }
1976
1977 res = PyEval_CallObject(method, args);
1978 Py_DECREF(args);
1979 Py_DECREF(method);
1980
1981 return res;
1982}
1983
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001984static PyObject *
1985instance_richcompare(PyObject *v, PyObject *w, int op)
1986{
1987 PyObject *res;
1988
1989 if (PyInstance_Check(v)) {
1990 res = half_richcompare(v, w, op);
1991 if (res != Py_NotImplemented)
1992 return res;
1993 Py_DECREF(res);
1994 }
1995
1996 if (PyInstance_Check(w)) {
Tim Petersf4aca752004-09-23 02:39:37 +00001997 res = half_richcompare(w, v, _Py_SwappedOp[op]);
Guido van Rossum8998b4f2001-01-17 15:28:20 +00001998 if (res != Py_NotImplemented)
1999 return res;
2000 Py_DECREF(res);
2001 }
2002
2003 Py_INCREF(Py_NotImplemented);
2004 return Py_NotImplemented;
2005}
2006
Neil Schemenauer29bfc072001-01-04 01:43:46 +00002007
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002008/* Get the iterator */
2009static PyObject *
2010instance_getiter(PyInstanceObject *self)
2011{
2012 PyObject *func;
2013
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002014 if (iterstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002015 iterstr = PyString_InternFromString("__iter__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002016 if (iterstr == NULL)
2017 return NULL;
2018 }
2019 if (getitemstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002020 getitemstr = PyString_InternFromString("__getitem__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002021 if (getitemstr == NULL)
2022 return NULL;
2023 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002024
2025 if ((func = instance_getattr(self, iterstr)) != NULL) {
2026 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
2027 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002028 if (res != NULL && !PyIter_Check(res)) {
2029 PyErr_Format(PyExc_TypeError,
2030 "__iter__ returned non-iterator "
2031 "of type '%.100s'",
2032 res->ob_type->tp_name);
2033 Py_DECREF(res);
2034 res = NULL;
2035 }
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002036 return res;
2037 }
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002038 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2039 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002040 PyErr_Clear();
2041 if ((func = instance_getattr(self, getitemstr)) == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002042 PyErr_SetString(PyExc_TypeError,
2043 "iteration over non-sequence");
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002044 return NULL;
2045 }
2046 Py_DECREF(func);
Guido van Rossum213c7a62001-04-23 14:08:49 +00002047 return PySeqIter_New((PyObject *)self);
2048}
2049
2050
2051/* Call the iterator's next */
2052static PyObject *
2053instance_iternext(PyInstanceObject *self)
2054{
2055 PyObject *func;
2056
Neal Norwitzb09f4f52006-08-13 18:10:28 +00002057 if (nextstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002058 nextstr = PyString_InternFromString("next");
Neal Norwitzb09f4f52006-08-13 18:10:28 +00002059 if (nextstr == NULL)
2060 return NULL;
2061 }
Guido van Rossum213c7a62001-04-23 14:08:49 +00002062
2063 if ((func = instance_getattr(self, nextstr)) != NULL) {
2064 PyObject *res = PyEval_CallObject(func, (PyObject *)NULL);
2065 Py_DECREF(func);
2066 if (res != NULL) {
2067 return res;
2068 }
2069 if (PyErr_ExceptionMatches(PyExc_StopIteration)) {
2070 PyErr_Clear();
2071 return NULL;
2072 }
2073 return NULL;
2074 }
2075 PyErr_SetString(PyExc_TypeError, "instance has no next() method");
2076 return NULL;
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002077}
2078
Tim Peters6d6c1a32001-08-02 04:15:00 +00002079static PyObject *
2080instance_call(PyObject *func, PyObject *arg, PyObject *kw)
2081{
2082 PyObject *res, *call = PyObject_GetAttrString(func, "__call__");
2083 if (call == NULL) {
2084 PyInstanceObject *inst = (PyInstanceObject*) func;
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002085 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2086 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087 PyErr_Clear();
2088 PyErr_Format(PyExc_AttributeError,
2089 "%.200s instance has no __call__ method",
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002090 PyString_AsString(inst->in_class->cl_name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002091 return NULL;
2092 }
Guido van Rossum16b93b32002-06-13 21:32:51 +00002093 /* We must check and increment the recursion depth here. Scenario:
2094 class A:
2095 pass
2096 A.__call__ = A() # that's right
2097 a = A() # ok
2098 a() # infinite recursion
2099 This bounces between instance_call() and PyObject_Call() without
2100 ever hitting eval_frame() (which has the main recursion check). */
Armin Rigo2b3eb402003-10-28 12:05:48 +00002101 if (Py_EnterRecursiveCall(" in __call__")) {
Guido van Rossum16b93b32002-06-13 21:32:51 +00002102 res = NULL;
2103 }
Armin Rigo2b3eb402003-10-28 12:05:48 +00002104 else {
Guido van Rossum16b93b32002-06-13 21:32:51 +00002105 res = PyObject_Call(call, arg, kw);
Armin Rigo2b3eb402003-10-28 12:05:48 +00002106 Py_LeaveRecursiveCall();
2107 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002108 Py_DECREF(call);
2109 return res;
2110}
2111
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002112
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002113static PyNumberMethods instance_as_number = {
Georg Brandl347b3002006-03-30 11:57:00 +00002114 instance_add, /* nb_add */
2115 instance_sub, /* nb_subtract */
2116 instance_mul, /* nb_multiply */
2117 instance_div, /* nb_divide */
2118 instance_mod, /* nb_remainder */
2119 instance_divmod, /* nb_divmod */
2120 instance_pow, /* nb_power */
2121 (unaryfunc)instance_neg, /* nb_negative */
2122 (unaryfunc)instance_pos, /* nb_positive */
2123 (unaryfunc)instance_abs, /* nb_absolute */
2124 (inquiry)instance_nonzero, /* nb_nonzero */
2125 (unaryfunc)instance_invert, /* nb_invert */
2126 instance_lshift, /* nb_lshift */
2127 instance_rshift, /* nb_rshift */
2128 instance_and, /* nb_and */
2129 instance_xor, /* nb_xor */
2130 instance_or, /* nb_or */
2131 instance_coerce, /* nb_coerce */
2132 (unaryfunc)instance_int, /* nb_int */
2133 (unaryfunc)instance_long, /* nb_long */
2134 (unaryfunc)instance_float, /* nb_float */
2135 (unaryfunc)instance_oct, /* nb_oct */
2136 (unaryfunc)instance_hex, /* nb_hex */
2137 instance_iadd, /* nb_inplace_add */
2138 instance_isub, /* nb_inplace_subtract */
2139 instance_imul, /* nb_inplace_multiply */
2140 instance_idiv, /* nb_inplace_divide */
2141 instance_imod, /* nb_inplace_remainder */
2142 instance_ipow, /* nb_inplace_power */
2143 instance_ilshift, /* nb_inplace_lshift */
2144 instance_irshift, /* nb_inplace_rshift */
2145 instance_iand, /* nb_inplace_and */
2146 instance_ixor, /* nb_inplace_xor */
2147 instance_ior, /* nb_inplace_or */
2148 instance_floordiv, /* nb_floor_divide */
2149 instance_truediv, /* nb_true_divide */
2150 instance_ifloordiv, /* nb_inplace_floor_divide */
2151 instance_itruediv, /* nb_inplace_true_divide */
Neal Norwitz8a87f5d2006-08-12 17:03:09 +00002152 (unaryfunc)instance_index, /* nb_index */
Guido van Rossum04691fc1992-08-12 15:35:34 +00002153};
2154
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002155PyTypeObject PyInstance_Type = {
2156 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002157 0,
Guido van Rossum21ed88c1991-04-04 10:42:10 +00002158 "instance",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002159 sizeof(PyInstanceObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002160 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002161 (destructor)instance_dealloc, /* tp_dealloc */
2162 0, /* tp_print */
2163 0, /* tp_getattr */
2164 0, /* tp_setattr */
2165 instance_compare, /* tp_compare */
2166 (reprfunc)instance_repr, /* tp_repr */
2167 &instance_as_number, /* tp_as_number */
2168 &instance_as_sequence, /* tp_as_sequence */
2169 &instance_as_mapping, /* tp_as_mapping */
2170 (hashfunc)instance_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002171 instance_call, /* tp_call */
Guido van Rossum82c690f2001-04-30 14:39:18 +00002172 (reprfunc)instance_str, /* tp_str */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002173 (getattrofunc)instance_getattr, /* tp_getattro */
2174 (setattrofunc)instance_setattr, /* tp_setattro */
2175 0, /* tp_as_buffer */
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002176 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002177 instance_doc, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00002178 (traverseproc)instance_traverse, /* tp_traverse */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002179 0, /* tp_clear */
2180 instance_richcompare, /* tp_richcompare */
Guido van Rossum59d1d2b2001-04-20 19:13:02 +00002181 offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */
2182 (getiterfunc)instance_getiter, /* tp_iter */
Guido van Rossum213c7a62001-04-23 14:08:49 +00002183 (iternextfunc)instance_iternext, /* tp_iternext */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002184 0, /* tp_methods */
2185 0, /* tp_members */
2186 0, /* tp_getset */
2187 0, /* tp_base */
2188 0, /* tp_dict */
2189 0, /* tp_descr_get */
2190 0, /* tp_descr_set */
2191 0, /* tp_dictoffset */
2192 0, /* tp_init */
2193 0, /* tp_alloc */
2194 instance_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002195};
2196
2197
Guido van Rossum81daa321993-05-20 14:24:46 +00002198/* Instance method objects are used for two purposes:
2199 (a) as bound instance methods (returned by instancename.methodname)
2200 (b) as unbound methods (returned by ClassName.methodname)
2201 In case (b), im_self is NULL
2202*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002203
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002204PyObject *
Anthony Baxter377be112006-04-11 06:54:30 +00002205PyMethod_New(PyObject *func, PyObject *self, PyObject *klass)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002206{
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002207 register PyMethodObject *im;
Guido van Rossum7859f871998-07-08 14:58:16 +00002208 if (!PyCallable_Check(func)) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002209 PyErr_BadInternalCall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002210 return NULL;
2211 }
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002212 im = free_list;
2213 if (im != NULL) {
2214 free_list = (PyMethodObject *)(im->im_self);
Guido van Rossumb18618d2000-05-03 23:44:39 +00002215 PyObject_INIT(im, &PyMethod_Type);
Christian Heimes6075a822008-02-06 12:44:34 +00002216 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002217 }
2218 else {
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002219 im = PyObject_GC_New(PyMethodObject, &PyMethod_Type);
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002220 if (im == NULL)
2221 return NULL;
2222 }
Fred Drakedb81e8d2001-03-23 04:19:27 +00002223 im->im_weakreflist = NULL;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002224 Py_INCREF(func);
Guido van Rossume8122f11991-05-05 20:03:07 +00002225 im->im_func = func;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002226 Py_XINCREF(self);
Guido van Rossume8122f11991-05-05 20:03:07 +00002227 im->im_self = self;
Anthony Baxter377be112006-04-11 06:54:30 +00002228 Py_XINCREF(klass);
2229 im->im_class = klass;
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002230 _PyObject_GC_TRACK(im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002231 return (PyObject *)im;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002232}
2233
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002234/* Descriptors for PyMethod attributes */
2235
2236/* im_class, im_func and im_self are stored in the PyMethod object */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002237
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002238#define OFF(x) offsetof(PyMethodObject, x)
Guido van Rossum3f5da241990-12-20 15:06:42 +00002239
Guido van Rossum6f799372001-09-20 20:46:19 +00002240static PyMemberDef instancemethod_memberlist[] = {
2241 {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED,
2242 "the class associated with a method"},
2243 {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
2244 "the function (or other callable) implementing a method"},
Neal Norwitzb1d3d962008-03-18 04:46:00 +00002245 {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED,
2246 "the function (or other callable) implementing a method"},
Guido van Rossum6f799372001-09-20 20:46:19 +00002247 {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
2248 "the instance to which a method is bound; None for unbound methods"},
Neal Norwitzb1d3d962008-03-18 04:46:00 +00002249 {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED,
2250 "the instance to which a method is bound; None for unbound methods"},
Guido van Rossum3f5da241990-12-20 15:06:42 +00002251 {NULL} /* Sentinel */
2252};
2253
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002254/* Christian Tismer argued convincingly that method attributes should
2255 (nearly) always override function attributes.
2256 The one exception is __doc__; there's a default __doc__ which
2257 should only be used for the class, not for instances */
2258
2259static PyObject *
2260instancemethod_get_doc(PyMethodObject *im, void *context)
2261{
2262 static PyObject *docstr;
2263 if (docstr == NULL) {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002264 docstr= PyString_InternFromString("__doc__");
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002265 if (docstr == NULL)
2266 return NULL;
2267 }
2268 return PyObject_GetAttr(im->im_func, docstr);
2269}
2270
2271static PyGetSetDef instancemethod_getset[] = {
2272 {"__doc__", (getter)instancemethod_get_doc, NULL, NULL},
2273 {0}
2274};
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002275
2276static PyObject *
2277instancemethod_getattro(PyObject *obj, PyObject *name)
2278{
2279 PyMethodObject *im = (PyMethodObject *)obj;
2280 PyTypeObject *tp = obj->ob_type;
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002281 PyObject *descr = NULL;
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002282
Guido van Rossum915f0eb2001-10-17 20:26:38 +00002283 if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) {
2284 if (tp->tp_dict == NULL) {
2285 if (PyType_Ready(tp) < 0)
2286 return NULL;
2287 }
2288 descr = _PyType_Lookup(tp, name);
Barry Warsawd6a9e842001-01-15 20:40:19 +00002289 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002290
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002291 if (descr != NULL) {
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002292 descrgetfunc f = TP_DESCR_GET(descr->ob_type);
2293 if (f != NULL)
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002294 return f(descr, obj, (PyObject *)obj->ob_type);
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002295 else {
2296 Py_INCREF(descr);
2297 return descr;
2298 }
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002299 }
2300
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002301 return PyObject_GetAttr(im->im_func, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +00002302}
2303
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002304PyDoc_STRVAR(instancemethod_doc,
2305"instancemethod(function, instance, class)\n\
2306\n\
2307Create an instance method object.");
2308
2309static PyObject *
2310instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw)
2311{
2312 PyObject *func;
2313 PyObject *self;
Guido van Rossum2fb9fdc2003-04-09 19:35:08 +00002314 PyObject *classObj = NULL;
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002315
Georg Brandl5d59c092006-09-30 08:43:30 +00002316 if (!_PyArg_NoKeywords("instancemethod", kw))
2317 return NULL;
Guido van Rossum2fb9fdc2003-04-09 19:35:08 +00002318 if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3,
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002319 &func, &self, &classObj))
2320 return NULL;
2321 if (!PyCallable_Check(func)) {
2322 PyErr_SetString(PyExc_TypeError,
2323 "first argument must be callable");
2324 return NULL;
2325 }
2326 if (self == Py_None)
2327 self = NULL;
Michael W. Hudsone2749cb2005-03-30 16:32:10 +00002328 if (self == NULL && classObj == NULL) {
2329 PyErr_SetString(PyExc_TypeError,
2330 "unbound methods must have non-NULL im_class");
2331 return NULL;
2332 }
2333
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002334 return PyMethod_New(func, self, classObj);
2335}
2336
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002337static void
Fred Drake79912472000-07-09 04:06:11 +00002338instancemethod_dealloc(register PyMethodObject *im)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002339{
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002340 _PyObject_GC_UNTRACK(im);
Fred Drakec916f5a2001-10-26 17:56:51 +00002341 if (im->im_weakreflist != NULL)
2342 PyObject_ClearWeakRefs((PyObject *)im);
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002343 Py_DECREF(im->im_func);
2344 Py_XDECREF(im->im_self);
Guido van Rossumcdf0d752001-08-17 12:07:34 +00002345 Py_XDECREF(im->im_class);
Christian Heimes5b970ad2008-02-06 13:33:44 +00002346 if (numfree < PyMethod_MAXFREELIST) {
Christian Heimes6075a822008-02-06 12:44:34 +00002347 im->im_self = (PyObject *)free_list;
2348 free_list = im;
2349 numfree++;
2350 }
2351 else {
2352 PyObject_GC_Del(im);
2353 }
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002354}
2355
Guido van Rossumebc8c511992-09-03 20:39:51 +00002356static int
Fred Drake79912472000-07-09 04:06:11 +00002357instancemethod_compare(PyMethodObject *a, PyMethodObject *b)
Guido van Rossumebc8c511992-09-03 20:39:51 +00002358{
Armin Rigofd01d792006-06-08 10:56:24 +00002359 int cmp;
2360 cmp = PyObject_Compare(a->im_func, b->im_func);
2361 if (cmp)
2362 return cmp;
2363
2364 if (a->im_self == b->im_self)
2365 return 0;
2366 if (a->im_self == NULL || b->im_self == NULL)
Guido van Rossume9df7271995-04-06 14:46:51 +00002367 return (a->im_self < b->im_self) ? -1 : 1;
Armin Rigofd01d792006-06-08 10:56:24 +00002368 else
2369 return PyObject_Compare(a->im_self, b->im_self);
Guido van Rossumebc8c511992-09-03 20:39:51 +00002370}
2371
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002372static PyObject *
Fred Drake79912472000-07-09 04:06:11 +00002373instancemethod_repr(PyMethodObject *a)
Guido van Rossum25831651993-05-19 14:50:45 +00002374{
Tim Peters6d6c1a32001-08-02 04:15:00 +00002375 PyObject *self = a->im_self;
Guido van Rossum7859f871998-07-08 14:58:16 +00002376 PyObject *func = a->im_func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002377 PyObject *klass = a->im_class;
2378 PyObject *funcname = NULL, *klassname = NULL, *result = NULL;
2379 char *sfuncname = "?", *sklassname = "?";
2380
2381 funcname = PyObject_GetAttrString(func, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002382 if (funcname == NULL) {
2383 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2384 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002386 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002387 else if (!PyString_Check(funcname)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002388 Py_DECREF(funcname);
2389 funcname = NULL;
Guido van Rossum7859f871998-07-08 14:58:16 +00002390 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002391 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002392 sfuncname = PyString_AS_STRING(funcname);
Guido van Rossum40667692001-08-17 13:59:27 +00002393 if (klass == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394 klassname = NULL;
Guido van Rossum40667692001-08-17 13:59:27 +00002395 else {
2396 klassname = PyObject_GetAttrString(klass, "__name__");
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002397 if (klassname == NULL) {
2398 if (!PyErr_ExceptionMatches(PyExc_AttributeError))
2399 return NULL;
Guido van Rossum40667692001-08-17 13:59:27 +00002400 PyErr_Clear();
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002401 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002402 else if (!PyString_Check(klassname)) {
Guido van Rossum40667692001-08-17 13:59:27 +00002403 Py_DECREF(klassname);
2404 klassname = NULL;
2405 }
2406 else
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002407 sklassname = PyString_AS_STRING(klassname);
Guido van Rossum7859f871998-07-08 14:58:16 +00002408 }
Guido van Rossum81daa321993-05-20 14:24:46 +00002409 if (self == NULL)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002410 result = PyString_FromFormat("<unbound method %s.%s>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002411 sklassname, sfuncname);
Guido van Rossum81daa321993-05-20 14:24:46 +00002412 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002413 /* XXX Shouldn't use repr() here! */
2414 PyObject *selfrepr = PyObject_Repr(self);
2415 if (selfrepr == NULL)
2416 goto fail;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002417 if (!PyString_Check(selfrepr)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418 Py_DECREF(selfrepr);
2419 goto fail;
2420 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002421 result = PyString_FromFormat("<bound method %s.%s of %s>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00002422 sklassname, sfuncname,
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002423 PyString_AS_STRING(selfrepr));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002424 Py_DECREF(selfrepr);
Guido van Rossum81daa321993-05-20 14:24:46 +00002425 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002426 fail:
Guido van Rossum42636dc1999-10-11 14:03:12 +00002427 Py_XDECREF(funcname);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002428 Py_XDECREF(klassname);
2429 return result;
Guido van Rossum25831651993-05-19 14:50:45 +00002430}
2431
Guido van Rossum9bfef441993-03-29 10:43:31 +00002432static long
Fred Drake79912472000-07-09 04:06:11 +00002433instancemethod_hash(PyMethodObject *a)
Guido van Rossum9bfef441993-03-29 10:43:31 +00002434{
2435 long x, y;
Guido van Rossum81daa321993-05-20 14:24:46 +00002436 if (a->im_self == NULL)
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002437 x = PyObject_Hash(Py_None);
Guido van Rossum81daa321993-05-20 14:24:46 +00002438 else
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002439 x = PyObject_Hash(a->im_self);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002440 if (x == -1)
2441 return -1;
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002442 y = PyObject_Hash(a->im_func);
Guido van Rossum9bfef441993-03-29 10:43:31 +00002443 if (y == -1)
2444 return -1;
Armin Rigofd01d792006-06-08 10:56:24 +00002445 x = x ^ y;
2446 if (x == -1)
2447 x = -2;
2448 return x;
Guido van Rossum9bfef441993-03-29 10:43:31 +00002449}
2450
Jeremy Hylton8caad492000-06-23 14:18:11 +00002451static int
2452instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg)
2453{
Thomas Woutersc6e55062006-04-15 21:47:09 +00002454 Py_VISIT(im->im_func);
2455 Py_VISIT(im->im_self);
2456 Py_VISIT(im->im_class);
Jeremy Hyltond22162b2000-06-23 17:14:56 +00002457 return 0;
Jeremy Hylton8caad492000-06-23 14:18:11 +00002458}
2459
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002460static void
Anthony Baxter377be112006-04-11 06:54:30 +00002461getclassname(PyObject *klass, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +00002462{
2463 PyObject *name;
2464
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002465 assert(bufsize > 1);
2466 strcpy(buf, "?"); /* Default outcome */
Anthony Baxter377be112006-04-11 06:54:30 +00002467 if (klass == NULL)
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002468 return;
Anthony Baxter377be112006-04-11 06:54:30 +00002469 name = PyObject_GetAttrString(klass, "__name__");
Guido van Rossuma15dece2001-08-24 18:48:27 +00002470 if (name == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002471 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +00002472 PyErr_Clear();
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002473 return;
Guido van Rossuma15dece2001-08-24 18:48:27 +00002474 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00002475 if (PyString_Check(name)) {
2476 strncpy(buf, PyString_AS_STRING(name), bufsize);
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002477 buf[bufsize-1] = '\0';
Guido van Rossuma15dece2001-08-24 18:48:27 +00002478 }
Guido van Rossuma15dece2001-08-24 18:48:27 +00002479 Py_DECREF(name);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002480}
2481
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002482static void
2483getinstclassname(PyObject *inst, char *buf, int bufsize)
Guido van Rossuma15dece2001-08-24 18:48:27 +00002484{
Anthony Baxter377be112006-04-11 06:54:30 +00002485 PyObject *klass;
Guido van Rossuma15dece2001-08-24 18:48:27 +00002486
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002487 if (inst == NULL) {
Tim Peters75585d42002-08-20 14:31:35 +00002488 assert(bufsize > 0 && (size_t)bufsize > strlen("nothing"));
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002489 strcpy(buf, "nothing");
2490 return;
2491 }
Guido van Rossuma15dece2001-08-24 18:48:27 +00002492
Anthony Baxter377be112006-04-11 06:54:30 +00002493 klass = PyObject_GetAttrString(inst, "__class__");
2494 if (klass == NULL) {
Guido van Rossume7b8ecf2002-06-13 21:42:04 +00002495 /* This function cannot return an exception */
Guido van Rossuma15dece2001-08-24 18:48:27 +00002496 PyErr_Clear();
Anthony Baxter377be112006-04-11 06:54:30 +00002497 klass = (PyObject *)(inst->ob_type);
2498 Py_INCREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002499 }
Anthony Baxter377be112006-04-11 06:54:30 +00002500 getclassname(klass, buf, bufsize);
2501 Py_XDECREF(klass);
Guido van Rossuma15dece2001-08-24 18:48:27 +00002502}
2503
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504static PyObject *
2505instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw)
2506{
2507 PyObject *self = PyMethod_GET_SELF(func);
Anthony Baxter377be112006-04-11 06:54:30 +00002508 PyObject *klass = PyMethod_GET_CLASS(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002509 PyObject *result;
2510
2511 func = PyMethod_GET_FUNCTION(func);
2512 if (self == NULL) {
2513 /* Unbound methods must be called with an instance of
2514 the class (or a derived class) as first argument */
2515 int ok;
2516 if (PyTuple_Size(arg) >= 1)
2517 self = PyTuple_GET_ITEM(arg, 0);
2518 if (self == NULL)
2519 ok = 0;
2520 else {
Anthony Baxter377be112006-04-11 06:54:30 +00002521 ok = PyObject_IsInstance(self, klass);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002522 if (ok < 0)
2523 return NULL;
2524 }
2525 if (!ok) {
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002526 char clsbuf[256];
2527 char instbuf[256];
Anthony Baxter377be112006-04-11 06:54:30 +00002528 getclassname(klass, clsbuf, sizeof(clsbuf));
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002529 getinstclassname(self, instbuf, sizeof(instbuf));
Tim Peters6d6c1a32001-08-02 04:15:00 +00002530 PyErr_Format(PyExc_TypeError,
Guido van Rossuma15dece2001-08-24 18:48:27 +00002531 "unbound method %s%s must be called with "
2532 "%s instance as first argument "
2533 "(got %s%s instead)",
Tim Peters6d6c1a32001-08-02 04:15:00 +00002534 PyEval_GetFuncName(func),
Guido van Rossuma15dece2001-08-24 18:48:27 +00002535 PyEval_GetFuncDesc(func),
Guido van Rossum45ec02a2002-08-19 21:43:18 +00002536 clsbuf,
2537 instbuf,
Guido van Rossuma15dece2001-08-24 18:48:27 +00002538 self == NULL ? "" : " instance");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002539 return NULL;
2540 }
2541 Py_INCREF(arg);
2542 }
2543 else {
Martin v. Löwis18e16552006-02-15 17:27:45 +00002544 Py_ssize_t argcount = PyTuple_Size(arg);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002545 PyObject *newarg = PyTuple_New(argcount + 1);
2546 int i;
2547 if (newarg == NULL)
2548 return NULL;
2549 Py_INCREF(self);
2550 PyTuple_SET_ITEM(newarg, 0, self);
2551 for (i = 0; i < argcount; i++) {
2552 PyObject *v = PyTuple_GET_ITEM(arg, i);
2553 Py_XINCREF(v);
2554 PyTuple_SET_ITEM(newarg, i+1, v);
2555 }
2556 arg = newarg;
2557 }
2558 result = PyObject_Call((PyObject *)func, arg, kw);
2559 Py_DECREF(arg);
2560 return result;
2561}
2562
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002563static PyObject *
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002564instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls)
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002565{
Guido van Rossumf23c41d2001-08-17 13:43:27 +00002566 /* Don't rebind an already bound method, or an unbound method
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002567 of a class that's not a base class of cls. */
2568
2569 if (PyMethod_GET_SELF(meth) != NULL) {
2570 /* Already bound */
Guido van Rossum501c7c72001-08-16 20:41:56 +00002571 Py_INCREF(meth);
2572 return meth;
2573 }
Guido van Rossum6bae46d2003-02-11 18:43:00 +00002574 /* No, it is an unbound method */
2575 if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) {
2576 /* Do subclass test. If it fails, return meth unchanged. */
2577 int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth));
2578 if (ok < 0)
2579 return NULL;
2580 if (!ok) {
2581 Py_INCREF(meth);
2582 return meth;
2583 }
2584 }
2585 /* Bind it to obj */
2586 return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls);
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002587}
2588
Guido van Rossumc0b618a1997-05-02 03:12:38 +00002589PyTypeObject PyMethod_Type = {
2590 PyObject_HEAD_INIT(&PyType_Type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002591 0,
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002592 "instancemethod",
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002593 sizeof(PyMethodObject),
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002594 0,
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002595 (destructor)instancemethod_dealloc, /* tp_dealloc */
2596 0, /* tp_print */
2597 0, /* tp_getattr */
2598 0, /* tp_setattr */
2599 (cmpfunc)instancemethod_compare, /* tp_compare */
2600 (reprfunc)instancemethod_repr, /* tp_repr */
2601 0, /* tp_as_number */
2602 0, /* tp_as_sequence */
2603 0, /* tp_as_mapping */
2604 (hashfunc)instancemethod_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002605 instancemethod_call, /* tp_call */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002606 0, /* tp_str */
Georg Brandl347b3002006-03-30 11:57:00 +00002607 instancemethod_getattro, /* tp_getattro */
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002608 PyObject_GenericSetAttr, /* tp_setattro */
Guido van Rossum8998b4f2001-01-17 15:28:20 +00002609 0, /* tp_as_buffer */
Raymond Hettingerbff60ae2005-06-19 08:42:20 +00002610 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002611 instancemethod_doc, /* tp_doc */
Jeremy Hylton8caad492000-06-23 14:18:11 +00002612 (traverseproc)instancemethod_traverse, /* tp_traverse */
Fred Drakedb81e8d2001-03-23 04:19:27 +00002613 0, /* tp_clear */
2614 0, /* tp_richcompare */
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002615 offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */
2616 0, /* tp_iter */
2617 0, /* tp_iternext */
2618 0, /* tp_methods */
Guido van Rossumf0b35e12001-09-18 03:53:24 +00002619 instancemethod_memberlist, /* tp_members */
Guido van Rossumbaf0f8f2003-11-22 23:55:50 +00002620 instancemethod_getset, /* tp_getset */
Guido van Rossum23cc2b42001-08-15 17:52:31 +00002621 0, /* tp_base */
2622 0, /* tp_dict */
2623 instancemethod_descr_get, /* tp_descr_get */
2624 0, /* tp_descr_set */
2625 0, /* tp_dictoffset */
Guido van Rossumbea18cc2002-06-14 20:41:17 +00002626 0, /* tp_init */
2627 0, /* tp_alloc */
2628 instancemethod_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002629};
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002630
2631/* Clear out the free list */
2632
Christian Heimes3b718a72008-02-14 12:47:33 +00002633int
2634PyMethod_ClearFreeList(void)
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002635{
Christian Heimes3b718a72008-02-14 12:47:33 +00002636 int freelist_size = numfree;
2637
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002638 while (free_list) {
Guido van Rossumb18618d2000-05-03 23:44:39 +00002639 PyMethodObject *im = free_list;
2640 free_list = (PyMethodObject *)(im->im_self);
Neil Schemenauere83c00e2001-08-29 23:54:21 +00002641 PyObject_GC_Del(im);
Christian Heimes6075a822008-02-06 12:44:34 +00002642 numfree--;
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002643 }
Christian Heimes6075a822008-02-06 12:44:34 +00002644 assert(numfree == 0);
Christian Heimes3b718a72008-02-14 12:47:33 +00002645 return freelist_size;
2646}
2647
2648void
2649PyMethod_Fini(void)
2650{
2651 (void)PyMethod_ClearFreeList();
Guido van Rossuma0d349f1997-08-05 02:06:53 +00002652}