Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Class object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 6 | |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 7 | /* Free list for method objects to safe malloc/free overhead |
| 8 | * The im_self element is used to chain the elements. |
| 9 | */ |
| 10 | static PyMethodObject *free_list; |
| 11 | static int numfree = 0; |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 12 | #ifndef PyMethod_MAXFREELIST |
| 13 | #define PyMethod_MAXFREELIST 256 |
| 14 | #endif |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 16 | #define TP_DESCR_GET(t) \ |
| 17 | (PyType_HasFeature(t, Py_TPFLAGS_HAVE_CLASS) ? (t)->tp_descr_get : NULL) |
| 18 | |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 19 | /* Forward */ |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 20 | static PyObject *class_lookup(PyClassObject *, PyObject *, |
| 21 | PyClassObject **); |
| 22 | static PyObject *instance_getattr1(PyInstanceObject *, PyObject *); |
| 23 | static PyObject *instance_getattr2(PyInstanceObject *, PyObject *); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 24 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 25 | static PyObject *getattrstr, *setattrstr, *delattrstr; |
| 26 | |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 27 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 28 | PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 29 | PyClass_New(PyObject *bases, PyObject *dict, PyObject *name) |
| 30 | /* bases is NULL or tuple of classobjects! */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 31 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 32 | PyClassObject *op, *dummy; |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 33 | static PyObject *docstr, *modstr, *namestr; |
Guido van Rossum | 019f424 | 1996-08-21 14:54:28 +0000 | [diff] [blame] | 34 | if (docstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 35 | docstr= PyString_InternFromString("__doc__"); |
Guido van Rossum | 019f424 | 1996-08-21 14:54:28 +0000 | [diff] [blame] | 36 | if (docstr == NULL) |
| 37 | return NULL; |
| 38 | } |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 39 | if (modstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 40 | modstr= PyString_InternFromString("__module__"); |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 41 | if (modstr == NULL) |
| 42 | return NULL; |
| 43 | } |
| 44 | if (namestr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 45 | namestr= PyString_InternFromString("__name__"); |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 46 | if (namestr == NULL) |
| 47 | return NULL; |
| 48 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 49 | if (name == NULL || !PyString_Check(name)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 50 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 51 | "PyClass_New: name must be a string"); |
| 52 | return NULL; |
| 53 | } |
| 54 | if (dict == NULL || !PyDict_Check(dict)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 55 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 56 | "PyClass_New: dict must be a dictionary"); |
| 57 | return NULL; |
| 58 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 59 | if (PyDict_GetItem(dict, docstr) == NULL) { |
| 60 | if (PyDict_SetItem(dict, docstr, Py_None) < 0) |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 63 | if (PyDict_GetItem(dict, modstr) == NULL) { |
| 64 | PyObject *globals = PyEval_GetGlobals(); |
| 65 | if (globals != NULL) { |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 66 | PyObject *modname = PyDict_GetItem(globals, namestr); |
| 67 | if (modname != NULL) { |
| 68 | if (PyDict_SetItem(dict, modstr, modname) < 0) |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 69 | return NULL; |
| 70 | } |
| 71 | } |
| 72 | } |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 73 | if (bases == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 74 | bases = PyTuple_New(0); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 75 | if (bases == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 76 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 77 | } |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 78 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 79 | Py_ssize_t i, n; |
Guido van Rossum | 28d80b1 | 2001-09-07 21:08:32 +0000 | [diff] [blame] | 80 | PyObject *base; |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 81 | if (!PyTuple_Check(bases)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 82 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 83 | "PyClass_New: bases must be a tuple"); |
| 84 | return NULL; |
| 85 | } |
Guido van Rossum | 28d80b1 | 2001-09-07 21:08:32 +0000 | [diff] [blame] | 86 | 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 Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 92 | return PyObject_CallFunctionObjArgs( |
Guido van Rossum | 28d80b1 | 2001-09-07 21:08:32 +0000 | [diff] [blame] | 93 | (PyObject *) base->ob_type, |
Georg Brandl | 684fd0c | 2006-05-25 19:15:31 +0000 | [diff] [blame] | 94 | name, bases, dict, NULL); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 95 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 96 | "PyClass_New: base must be a class"); |
| 97 | return NULL; |
| 98 | } |
| 99 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | Py_INCREF(bases); |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 101 | } |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 102 | |
| 103 | if (getattrstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 104 | getattrstr = PyString_InternFromString("__getattr__"); |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 105 | if (getattrstr == NULL) |
| 106 | goto alloc_error; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 107 | setattrstr = PyString_InternFromString("__setattr__"); |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 108 | if (setattrstr == NULL) |
| 109 | goto alloc_error; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 110 | delattrstr = PyString_InternFromString("__delattr__"); |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 111 | if (delattrstr == NULL) |
| 112 | goto alloc_error; |
| 113 | } |
| 114 | |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 115 | op = PyObject_GC_New(PyClassObject, &PyClass_Type); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 116 | if (op == NULL) { |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 117 | alloc_error: |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 118 | Py_DECREF(bases); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 120 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 121 | op->cl_bases = bases; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | Py_INCREF(dict); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 123 | op->cl_dict = dict; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 124 | Py_XINCREF(name); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 125 | op->cl_name = name; |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 126 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 127 | op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
| 128 | op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
| 129 | op->cl_delattr = class_lookup(op, delattrstr, &dummy); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 130 | Py_XINCREF(op->cl_getattr); |
| 131 | Py_XINCREF(op->cl_setattr); |
| 132 | Py_XINCREF(op->cl_delattr); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 133 | _PyObject_GC_TRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 134 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 137 | PyObject * |
| 138 | PyMethod_Function(PyObject *im) |
| 139 | { |
| 140 | if (!PyMethod_Check(im)) { |
| 141 | PyErr_BadInternalCall(); |
| 142 | return NULL; |
| 143 | } |
| 144 | return ((PyMethodObject *)im)->im_func; |
| 145 | } |
| 146 | |
| 147 | PyObject * |
| 148 | PyMethod_Self(PyObject *im) |
| 149 | { |
| 150 | if (!PyMethod_Check(im)) { |
| 151 | PyErr_BadInternalCall(); |
| 152 | return NULL; |
| 153 | } |
| 154 | return ((PyMethodObject *)im)->im_self; |
| 155 | } |
| 156 | |
| 157 | PyObject * |
| 158 | PyMethod_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 Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 167 | PyDoc_STRVAR(class_doc, |
| 168 | "classobj(name, bases, dict)\n\ |
| 169 | \n\ |
| 170 | Create a class object. The name must be a string; the second argument\n\ |
| 171 | a tuple of classes, and the third a dictionary."); |
| 172 | |
| 173 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 174 | class_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 175 | { |
| 176 | PyObject *name, *bases, *dict; |
Martin v. Löwis | 15e6274 | 2006-02-27 16:46:16 +0000 | [diff] [blame] | 177 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 178 | |
| 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | /* Class methods */ |
| 186 | |
| 187 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 188 | class_dealloc(PyClassObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 190 | _PyObject_GC_UNTRACK(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 191 | Py_DECREF(op->cl_bases); |
| 192 | Py_DECREF(op->cl_dict); |
| 193 | Py_XDECREF(op->cl_name); |
Guido van Rossum | 152d817 | 1998-08-04 14:59:16 +0000 | [diff] [blame] | 194 | Py_XDECREF(op->cl_getattr); |
| 195 | Py_XDECREF(op->cl_setattr); |
| 196 | Py_XDECREF(op->cl_delattr); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 197 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 198 | } |
| 199 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 200 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 201 | class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 202 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 203 | Py_ssize_t i, n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 204 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 205 | if (value != NULL) { |
| 206 | *pclass = cp; |
| 207 | return value; |
| 208 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 209 | n = PyTuple_Size(cp->cl_bases); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 210 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 211 | /* XXX What if one of the bases is not a class? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | PyObject *v = class_lookup( |
| 213 | (PyClassObject *) |
| 214 | PyTuple_GetItem(cp->cl_bases, i), name, pclass); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 215 | if (v != NULL) |
| 216 | return v; |
| 217 | } |
| 218 | return NULL; |
| 219 | } |
| 220 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 221 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 222 | class_getattr(register PyClassObject *op, PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | register PyObject *v; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 225 | register char *sname = PyString_AsString(name); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 226 | PyClassObject *klass; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 227 | descrgetfunc f; |
| 228 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 229 | if (sname[0] == '_' && sname[1] == '_') { |
| 230 | if (strcmp(sname, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 231 | if (PyEval_GetRestricted()) { |
| 232 | PyErr_SetString(PyExc_RuntimeError, |
| 233 | "class.__dict__ not accessible in restricted mode"); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 234 | return NULL; |
| 235 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 236 | Py_INCREF(op->cl_dict); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 237 | return op->cl_dict; |
| 238 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 239 | if (strcmp(sname, "__bases__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 240 | Py_INCREF(op->cl_bases); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 241 | return op->cl_bases; |
| 242 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 243 | if (strcmp(sname, "__name__") == 0) { |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 244 | if (op->cl_name == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 245 | v = Py_None; |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 246 | else |
| 247 | v = op->cl_name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 248 | Py_INCREF(v); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 249 | return v; |
| 250 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 251 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 252 | v = class_lookup(op, name, &klass); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 253 | if (v == NULL) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 254 | PyErr_Format(PyExc_AttributeError, |
| 255 | "class %.50s has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 256 | PyString_AS_STRING(op->cl_name), sname); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 257 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 258 | } |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 259 | f = TP_DESCR_GET(v->ob_type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 260 | if (f == NULL) |
| 261 | Py_INCREF(v); |
| 262 | else |
| 263 | v = f(v, (PyObject *)NULL, (PyObject *)op); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 264 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 265 | } |
| 266 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 267 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 268 | set_slot(PyObject **slot, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 269 | { |
| 270 | PyObject *temp = *slot; |
| 271 | Py_XINCREF(v); |
| 272 | *slot = v; |
| 273 | Py_XDECREF(temp); |
| 274 | } |
| 275 | |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 276 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 277 | set_attr_slots(PyClassObject *c) |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 278 | { |
| 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 Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 286 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 287 | set_dict(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 288 | { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 289 | if (v == NULL || !PyDict_Check(v)) |
| 290 | return "__dict__ must be a dictionary object"; |
| 291 | set_slot(&c->cl_dict, v); |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 292 | set_attr_slots(c); |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 293 | return ""; |
| 294 | } |
| 295 | |
| 296 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 297 | set_bases(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 298 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 299 | Py_ssize_t i, n; |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 300 | |
| 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 Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 312 | set_attr_slots(c); |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 313 | return ""; |
| 314 | } |
| 315 | |
| 316 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 317 | set_name(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 318 | { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 319 | if (v == NULL || !PyString_Check(v)) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 320 | return "__name__ must be a string object"; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 321 | if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v)) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 322 | return "__name__ must not contain null bytes"; |
| 323 | set_slot(&c->cl_name, v); |
| 324 | return ""; |
| 325 | } |
| 326 | |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 327 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 328 | class_setattr(PyClassObject *op, PyObject *name, PyObject *v) |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 329 | { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 330 | char *sname; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 331 | if (PyEval_GetRestricted()) { |
| 332 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | ac21f6a | 1995-08-04 04:05:31 +0000 | [diff] [blame] | 333 | "classes are read-only in restricted mode"); |
| 334 | return -1; |
| 335 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 336 | sname = PyString_AsString(name); |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 337 | if (sname[0] == '_' && sname[1] == '_') { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 338 | Py_ssize_t n = PyString_Size(name); |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 339 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 340 | 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 Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 359 | return -1; |
| 360 | } |
| 361 | } |
| 362 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 363 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 364 | int rv = PyDict_DelItem(op->cl_dict, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 365 | if (rv < 0) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 366 | PyErr_Format(PyExc_AttributeError, |
| 367 | "class %.50s has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 368 | PyString_AS_STRING(op->cl_name), sname); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 369 | return rv; |
| 370 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 371 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 372 | return PyDict_SetItem(op->cl_dict, name, v); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 373 | } |
| 374 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 375 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 376 | class_repr(PyClassObject *op) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 377 | { |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 378 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 379 | char *name; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 380 | if (op->cl_name == NULL || !PyString_Check(op->cl_name)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 381 | name = "?"; |
| 382 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 383 | 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 Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 386 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 387 | return PyString_FromFormat("<class %s.%s at %p>", |
| 388 | PyString_AsString(mod), |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 389 | name, op); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 390 | } |
| 391 | |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 392 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 393 | class_str(PyClassObject *op) |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 394 | { |
| 395 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
| 396 | PyObject *name = op->cl_name; |
| 397 | PyObject *res; |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 398 | Py_ssize_t m, n; |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 399 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 400 | if (name == NULL || !PyString_Check(name)) |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 401 | return class_repr(op); |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 402 | if (mod == NULL || !PyString_Check(mod)) { |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 403 | Py_INCREF(name); |
| 404 | return name; |
| 405 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 406 | m = PyString_GET_SIZE(mod); |
| 407 | n = PyString_GET_SIZE(name); |
| 408 | res = PyString_FromStringAndSize((char *)NULL, m+1+n); |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 409 | if (res != NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 410 | char *s = PyString_AS_STRING(res); |
| 411 | memcpy(s, PyString_AS_STRING(mod), m); |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 412 | s += m; |
| 413 | *s++ = '.'; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 414 | memcpy(s, PyString_AS_STRING(name), n); |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 415 | } |
| 416 | return res; |
| 417 | } |
| 418 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 419 | static int |
| 420 | class_traverse(PyClassObject *o, visitproc visit, void *arg) |
| 421 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 422 | 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 Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 428 | return 0; |
| 429 | } |
| 430 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 431 | PyTypeObject PyClass_Type = { |
| 432 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 433 | 0, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 434 | "classobj", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 435 | sizeof(PyClassObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | 0, |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 437 | (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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 447 | PyInstance_New, /* tp_call */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 448 | (reprfunc)class_str, /* tp_str */ |
| 449 | (getattrofunc)class_getattr, /* tp_getattro */ |
| 450 | (setattrofunc)class_setattr, /* tp_setattro */ |
| 451 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 452 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 453 | class_doc, /* tp_doc */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 454 | (traverseproc)class_traverse, /* tp_traverse */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 455 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 471 | }; |
| 472 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 473 | int |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 474 | PyClass_IsSubclass(PyObject *klass, PyObject *base) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 475 | { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 476 | Py_ssize_t i, n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 477 | PyClassObject *cp; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 478 | if (klass == base) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 479 | return 1; |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 480 | if (PyTuple_Check(base)) { |
| 481 | n = PyTuple_GET_SIZE(base); |
| 482 | for (i = 0; i < n; i++) { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 483 | if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i))) |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 484 | return 1; |
| 485 | } |
Walter Dörwald | 7e5c6a0 | 2002-12-12 19:14:08 +0000 | [diff] [blame] | 486 | return 0; |
Walter Dörwald | d9a6ad3 | 2002-12-12 16:41:44 +0000 | [diff] [blame] | 487 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 488 | if (klass == NULL || !PyClass_Check(klass)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 489 | return 0; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 490 | cp = (PyClassObject *)klass; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 491 | n = PyTuple_Size(cp->cl_bases); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 492 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 493 | if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 494 | return 1; |
| 495 | } |
| 496 | return 0; |
| 497 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 499 | |
| 500 | /* Instance objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 501 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 502 | PyObject * |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 503 | PyInstance_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 Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 523 | inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type); |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 524 | if (inst == NULL) { |
| 525 | Py_DECREF(dict); |
| 526 | return NULL; |
| 527 | } |
Fred Drake | 4e262a9 | 2001-03-22 18:26:47 +0000 | [diff] [blame] | 528 | inst->in_weakreflist = NULL; |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 529 | Py_INCREF(klass); |
| 530 | inst->in_class = (PyClassObject *)klass; |
| 531 | inst->in_dict = dict; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 532 | _PyObject_GC_TRACK(inst); |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 533 | return (PyObject *)inst; |
| 534 | } |
| 535 | |
| 536 | PyObject * |
| 537 | PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 538 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 539 | register PyInstanceObject *inst; |
| 540 | PyObject *init; |
| 541 | static PyObject *initstr; |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 542 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 543 | if (initstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 544 | initstr = PyString_InternFromString("__init__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 545 | if (initstr == NULL) |
| 546 | return NULL; |
| 547 | } |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 548 | inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 549 | if (inst == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 550 | return NULL; |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 551 | init = instance_getattr2(inst, initstr); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 552 | if (init == NULL) { |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 553 | if (PyErr_Occurred()) { |
| 554 | Py_DECREF(inst); |
| 555 | return NULL; |
| 556 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 557 | 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 Rossum | a83f270 | 1995-07-26 18:07:32 +0000 | [diff] [blame] | 562 | "this constructor takes no arguments"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 563 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 564 | inst = NULL; |
| 565 | } |
| 566 | } |
| 567 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 568 | PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
| 569 | Py_DECREF(init); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 570 | if (res == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 571 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 572 | inst = NULL; |
| 573 | } |
| 574 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 575 | if (res != Py_None) { |
| 576 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 577 | "__init__() should return None"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 578 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 579 | inst = NULL; |
| 580 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 581 | Py_DECREF(res); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 582 | } |
| 583 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 584 | return (PyObject *)inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 585 | } |
| 586 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 587 | /* Instance methods */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 588 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 589 | PyDoc_STRVAR(instance_doc, |
| 590 | "instance(class[, dict])\n\ |
| 591 | \n\ |
| 592 | Create an instance without calling its __init__() method.\n\ |
| 593 | The class must be a classic class.\n\ |
| 594 | If present, dict must be a dictionary or None."); |
| 595 | |
| 596 | static PyObject * |
| 597 | instance_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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 617 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 618 | instance_dealloc(register PyInstanceObject *inst) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | PyObject *error_type, *error_value, *error_traceback; |
| 621 | PyObject *del; |
| 622 | static PyObject *delstr; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 623 | |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 624 | _PyObject_GC_UNTRACK(inst); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 625 | if (inst->in_weakreflist != NULL) |
| 626 | PyObject_ClearWeakRefs((PyObject *) inst); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 627 | |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 628 | /* Temporarily resurrect the object. */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 629 | assert(inst->ob_type == &PyInstance_Type); |
| 630 | assert(inst->ob_refcnt == 0); |
| 631 | inst->ob_refcnt = 1; |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 632 | |
| 633 | /* Save the current exception, if any. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 634 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 635 | /* Execute __del__ method, if any. */ |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 636 | if (delstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 637 | delstr = PyString_InternFromString("__del__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 638 | if (delstr == NULL) |
Neal Norwitz | af33f2d | 2006-08-14 00:59:03 +0000 | [diff] [blame] | 639 | PyErr_WriteUnraisable((PyObject*)inst); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 640 | } |
Neal Norwitz | af33f2d | 2006-08-14 00:59:03 +0000 | [diff] [blame] | 641 | if (delstr && (del = instance_getattr2(inst, delstr)) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 642 | PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 643 | if (res == NULL) |
Jeremy Hylton | b709df3 | 2000-09-01 02:47:25 +0000 | [diff] [blame] | 644 | PyErr_WriteUnraisable(del); |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 645 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 646 | Py_DECREF(res); |
| 647 | Py_DECREF(del); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 648 | } |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 649 | /* Restore the saved exception. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 650 | PyErr_Restore(error_type, error_value, error_traceback); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 651 | |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 652 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 653 | * cause a recursive call. |
| 654 | */ |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 655 | assert(inst->ob_refcnt > 0); |
| 656 | if (--inst->ob_refcnt == 0) { |
Guido van Rossum | 9ff1a44 | 2008-01-18 20:56:30 +0000 | [diff] [blame] | 657 | |
| 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 Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 667 | Py_DECREF(inst->in_class); |
| 668 | Py_XDECREF(inst->in_dict); |
| 669 | PyObject_GC_Del(inst); |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 670 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 671 | else { |
Martin v. Löwis | 6685128 | 2006-04-22 11:40:03 +0000 | [diff] [blame] | 672 | Py_ssize_t refcnt = inst->ob_refcnt; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 673 | /* __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. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 679 | /* 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 Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 684 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
Michael W. Hudson | 3f3b668 | 2004-08-03 10:21:03 +0000 | [diff] [blame] | 685 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 686 | * undone. |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 687 | */ |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 688 | #ifdef COUNT_ALLOCS |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 689 | --inst->ob_type->tp_frees; |
| 690 | --inst->ob_type->tp_allocs; |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 691 | #endif |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 692 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 693 | } |
| 694 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 696 | instance_getattr1(register PyInstanceObject *inst, PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 697 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 698 | register PyObject *v; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 699 | register char *sname = PyString_AsString(name); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 700 | if (sname[0] == '_' && sname[1] == '_') { |
| 701 | if (strcmp(sname, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 702 | if (PyEval_GetRestricted()) { |
| 703 | PyErr_SetString(PyExc_RuntimeError, |
| 704 | "instance.__dict__ not accessible in restricted mode"); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 705 | return NULL; |
| 706 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 707 | Py_INCREF(inst->in_dict); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 708 | return inst->in_dict; |
| 709 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 710 | if (strcmp(sname, "__class__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 711 | Py_INCREF(inst->in_class); |
| 712 | return (PyObject *)inst->in_class; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 713 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 714 | } |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 715 | v = instance_getattr2(inst, name); |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 716 | if (v == NULL && !PyErr_Occurred()) { |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 717 | PyErr_Format(PyExc_AttributeError, |
| 718 | "%.50s instance has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 719 | PyString_AS_STRING(inst->in_class->cl_name), sname); |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 720 | } |
| 721 | return v; |
| 722 | } |
| 723 | |
| 724 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 725 | instance_getattr2(register PyInstanceObject *inst, PyObject *name) |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 726 | { |
| 727 | register PyObject *v; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 728 | PyClassObject *klass; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 729 | descrgetfunc f; |
| 730 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 731 | v = PyDict_GetItem(inst->in_dict, name); |
Guido van Rossum | f23c41d | 2001-08-17 13:43:27 +0000 | [diff] [blame] | 732 | if (v != NULL) { |
| 733 | Py_INCREF(v); |
| 734 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 735 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 736 | v = class_lookup(inst->in_class, name, &klass); |
Guido van Rossum | f23c41d | 2001-08-17 13:43:27 +0000 | [diff] [blame] | 737 | if (v != NULL) { |
| 738 | Py_INCREF(v); |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 739 | f = TP_DESCR_GET(v->ob_type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 740 | if (f != NULL) { |
| 741 | PyObject *w = f(v, (PyObject *)inst, |
| 742 | (PyObject *)(inst->in_class)); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 743 | Py_DECREF(v); |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 744 | v = w; |
| 745 | } |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 746 | } |
| 747 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 750 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 751 | instance_getattr(register PyInstanceObject *inst, PyObject *name) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 752 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 753 | register PyObject *func, *res; |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 754 | res = instance_getattr1(inst, name); |
| 755 | if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 756 | PyObject *args; |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 757 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 758 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 759 | PyErr_Clear(); |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 760 | args = PyTuple_Pack(2, inst, name); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 761 | if (args == NULL) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 762 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 763 | res = PyEval_CallObject(func, args); |
| 764 | Py_DECREF(args); |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 765 | } |
| 766 | return res; |
| 767 | } |
| 768 | |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 769 | /* See classobject.h comments: this only does dict lookups, and is always |
| 770 | * safe to call. |
| 771 | */ |
| 772 | PyObject * |
| 773 | _PyInstance_Lookup(PyObject *pinst, PyObject *name) |
| 774 | { |
| 775 | PyObject *v; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 776 | PyClassObject *klass; |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 777 | PyInstanceObject *inst; /* pinst cast to the right type */ |
| 778 | |
| 779 | assert(PyInstance_Check(pinst)); |
| 780 | inst = (PyInstanceObject *)pinst; |
| 781 | |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 782 | assert(PyString_Check(name)); |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 783 | |
| 784 | v = PyDict_GetItem(inst->in_dict, name); |
| 785 | if (v == NULL) |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 786 | v = class_lookup(inst->in_class, name, &klass); |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 787 | return v; |
| 788 | } |
| 789 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 790 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 791 | instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 792 | { |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 793 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 794 | int rv = PyDict_DelItem(inst->in_dict, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 795 | if (rv < 0) |
Fred Drake | 661ea26 | 2000-10-24 19:57:45 +0000 | [diff] [blame] | 796 | PyErr_Format(PyExc_AttributeError, |
| 797 | "%.50s instance has no attribute '%.400s'", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 798 | PyString_AS_STRING(inst->in_class->cl_name), |
| 799 | PyString_AS_STRING(name)); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 800 | return rv; |
| 801 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 802 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 803 | return PyDict_SetItem(inst->in_dict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 804 | } |
| 805 | |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 806 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 807 | instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 808 | { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 809 | PyObject *func, *args, *res, *tmp; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 810 | char *sname = PyString_AsString(name); |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 811 | if (sname[0] == '_' && sname[1] == '_') { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 812 | Py_ssize_t n = PyString_Size(name); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 813 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 814 | 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 Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 848 | } |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 849 | } |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 850 | 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 Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 857 | args = PyTuple_Pack(2, inst, name); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 858 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 859 | args = PyTuple_Pack(3, inst, name, v); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 860 | if (args == NULL) |
| 861 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 862 | res = PyEval_CallObject(func, args); |
| 863 | Py_DECREF(args); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 864 | if (res == NULL) |
| 865 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 866 | Py_DECREF(res); |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 867 | return 0; |
| 868 | } |
| 869 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 870 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 871 | instance_repr(PyInstanceObject *inst) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 872 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 873 | PyObject *func; |
| 874 | PyObject *res; |
| 875 | static PyObject *reprstr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 876 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 877 | if (reprstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 878 | reprstr = PyString_InternFromString("__repr__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 879 | if (reprstr == NULL) |
| 880 | return NULL; |
| 881 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 882 | func = instance_getattr(inst, reprstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 883 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 884 | PyObject *classname, *mod; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 885 | char *cname; |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 886 | 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. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 892 | if (classname != NULL && PyString_Check(classname)) |
| 893 | cname = PyString_AsString(classname); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 894 | else |
| 895 | cname = "?"; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 896 | if (mod == NULL || !PyString_Check(mod)) |
| 897 | return PyString_FromFormat("<?.%s instance at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 898 | cname, inst); |
Guido van Rossum | b7f1afe | 1997-12-03 00:06:02 +0000 | [diff] [blame] | 899 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 900 | return PyString_FromFormat("<%s.%s instance at %p>", |
| 901 | PyString_AsString(mod), |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 902 | cname, inst); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 903 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 904 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 905 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 906 | return res; |
| 907 | } |
| 908 | |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 909 | static PyObject * |
| 910 | instance_str(PyInstanceObject *inst) |
| 911 | { |
| 912 | PyObject *func; |
| 913 | PyObject *res; |
| 914 | static PyObject *strstr; |
| 915 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 916 | if (strstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 917 | strstr = PyString_InternFromString("__str__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 918 | if (strstr == NULL) |
| 919 | return NULL; |
| 920 | } |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 921 | func = instance_getattr(inst, strstr); |
| 922 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 923 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 924 | return NULL; |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 925 | 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 933 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 934 | instance_hash(PyInstanceObject *inst) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 935 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 936 | PyObject *func; |
| 937 | PyObject *res; |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 938 | long outcome; |
Guido van Rossum | 65e8bd7 | 2001-01-18 23:46:31 +0000 | [diff] [blame] | 939 | static PyObject *hashstr, *eqstr, *cmpstr; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 940 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 941 | if (hashstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 942 | hashstr = PyString_InternFromString("__hash__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 943 | if (hashstr == NULL) |
| 944 | return -1; |
| 945 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 946 | func = instance_getattr(inst, hashstr); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 947 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 948 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 949 | return -1; |
| 950 | PyErr_Clear(); |
Guido van Rossum | 65e8bd7 | 2001-01-18 23:46:31 +0000 | [diff] [blame] | 951 | /* 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 Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 954 | if (eqstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 955 | eqstr = PyString_InternFromString("__eq__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 956 | if (eqstr == NULL) |
| 957 | return -1; |
| 958 | } |
Guido van Rossum | 65e8bd7 | 2001-01-18 23:46:31 +0000 | [diff] [blame] | 959 | func = instance_getattr(inst, eqstr); |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 960 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 961 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 962 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 963 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 964 | if (cmpstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 965 | cmpstr = PyString_InternFromString("__cmp__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 966 | if (cmpstr == NULL) |
| 967 | return -1; |
| 968 | } |
Guido van Rossum | 65e8bd7 | 2001-01-18 23:46:31 +0000 | [diff] [blame] | 969 | func = instance_getattr(inst, cmpstr); |
| 970 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 971 | if (!PyErr_ExceptionMatches( |
| 972 | PyExc_AttributeError)) |
| 973 | return -1; |
Guido van Rossum | 65e8bd7 | 2001-01-18 23:46:31 +0000 | [diff] [blame] | 974 | PyErr_Clear(); |
| 975 | return _Py_HashPointer(inst); |
| 976 | } |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 977 | } |
Raymond Hettinger | a9e14b7 | 2003-09-16 07:11:46 +0000 | [diff] [blame] | 978 | Py_XDECREF(func); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 979 | PyErr_SetString(PyExc_TypeError, "unhashable instance"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 980 | return -1; |
| 981 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 982 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 983 | Py_DECREF(func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 984 | if (res == NULL) |
| 985 | return -1; |
Martin v. Löwis | ab2f8f7 | 2006-08-09 07:57:39 +0000 | [diff] [blame] | 986 | 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 Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 989 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 990 | PyErr_SetString(PyExc_TypeError, |
| 991 | "__hash__() should return an int"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 992 | outcome = -1; |
| 993 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 994 | Py_DECREF(res); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 995 | return outcome; |
| 996 | } |
| 997 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 998 | static int |
| 999 | instance_traverse(PyInstanceObject *o, visitproc visit, void *arg) |
| 1000 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 1001 | Py_VISIT(o->in_class); |
| 1002 | Py_VISIT(o->in_dict); |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 1003 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1006 | static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; |
| 1007 | static PyObject *iterstr, *nextstr; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1008 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1009 | static Py_ssize_t |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1010 | instance_length(PyInstanceObject *inst) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1011 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1012 | PyObject *func; |
| 1013 | PyObject *res; |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1014 | Py_ssize_t outcome; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1015 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1016 | if (lenstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1017 | lenstr = PyString_InternFromString("__len__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1018 | if (lenstr == NULL) |
| 1019 | return -1; |
| 1020 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1021 | func = instance_getattr(inst, lenstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1022 | if (func == NULL) |
| 1023 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1024 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1025 | Py_DECREF(func); |
Guido van Rossum | d014ea6 | 1992-11-26 10:30:26 +0000 | [diff] [blame] | 1026 | if (res == NULL) |
| 1027 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1028 | if (PyInt_Check(res)) { |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1029 | outcome = PyInt_AsSsize_t(res); |
| 1030 | if (outcome == -1 && PyErr_Occurred()) { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1031 | Py_DECREF(res); |
| 1032 | return -1; |
| 1033 | } |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1034 | #if SIZEOF_SIZE_T < SIZEOF_INT |
Guido van Rossum | ba3e6ec | 2005-09-19 22:42:41 +0000 | [diff] [blame] | 1035 | /* Overflow check -- range of PyInt is more than C int */ |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1036 | if (outcome != (int)outcome) { |
Guido van Rossum | ba3e6ec | 2005-09-19 22:42:41 +0000 | [diff] [blame] | 1037 | PyErr_SetString(PyExc_OverflowError, |
Guido van Rossum | 630db60 | 2005-09-20 18:49:54 +0000 | [diff] [blame] | 1038 | "__len__() should return 0 <= outcome < 2**31"); |
Guido van Rossum | ba3e6ec | 2005-09-19 22:42:41 +0000 | [diff] [blame] | 1039 | outcome = -1; |
| 1040 | } |
| 1041 | else |
| 1042 | #endif |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1043 | if (outcome < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1044 | PyErr_SetString(PyExc_ValueError, |
| 1045 | "__len__() should return >= 0"); |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1046 | outcome = -1; |
| 1047 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1048 | } |
| 1049 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1050 | PyErr_SetString(PyExc_TypeError, |
| 1051 | "__len__() should return an int"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1052 | outcome = -1; |
| 1053 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1054 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1055 | return outcome; |
| 1056 | } |
| 1057 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1058 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1059 | instance_subscript(PyInstanceObject *inst, PyObject *key) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1060 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1061 | PyObject *func; |
| 1062 | PyObject *arg; |
| 1063 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1064 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1065 | if (getitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1066 | getitemstr = PyString_InternFromString("__getitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1067 | if (getitemstr == NULL) |
| 1068 | return NULL; |
| 1069 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1070 | func = instance_getattr(inst, getitemstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1071 | if (func == NULL) |
| 1072 | return NULL; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1073 | arg = PyTuple_Pack(1, key); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1074 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1075 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1076 | return NULL; |
| 1077 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1078 | res = PyEval_CallObject(func, arg); |
| 1079 | Py_DECREF(func); |
| 1080 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1081 | return res; |
| 1082 | } |
| 1083 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1084 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1085 | instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1086 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1087 | PyObject *func; |
| 1088 | PyObject *arg; |
| 1089 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1090 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1091 | if (value == NULL) { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1092 | if (delitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1093 | delitemstr = PyString_InternFromString("__delitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1094 | if (delitemstr == NULL) |
| 1095 | return -1; |
| 1096 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1097 | func = instance_getattr(inst, delitemstr); |
| 1098 | } |
| 1099 | else { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1100 | if (setitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1101 | setitemstr = PyString_InternFromString("__setitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1102 | if (setitemstr == NULL) |
| 1103 | return -1; |
| 1104 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1105 | func = instance_getattr(inst, setitemstr); |
| 1106 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1107 | if (func == NULL) |
| 1108 | return -1; |
| 1109 | if (value == NULL) |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1110 | arg = PyTuple_Pack(1, key); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1111 | else |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1112 | arg = PyTuple_Pack(2, key, value); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1113 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1114 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1115 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1116 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1117 | res = PyEval_CallObject(func, arg); |
| 1118 | Py_DECREF(func); |
| 1119 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1120 | if (res == NULL) |
| 1121 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1122 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1123 | return 0; |
| 1124 | } |
| 1125 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1126 | static PyMappingMethods instance_as_mapping = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1127 | (lenfunc)instance_length, /* mp_length */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1128 | (binaryfunc)instance_subscript, /* mp_subscript */ |
| 1129 | (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1130 | }; |
| 1131 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1132 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1133 | instance_item(PyInstanceObject *inst, Py_ssize_t i) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1134 | { |
Georg Brandl | e4e023c | 2006-05-26 20:22:50 +0000 | [diff] [blame] | 1135 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1136 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1137 | if (getitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1138 | getitemstr = PyString_InternFromString("__getitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1139 | if (getitemstr == NULL) |
| 1140 | return NULL; |
| 1141 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1142 | func = instance_getattr(inst, getitemstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1143 | if (func == NULL) |
| 1144 | return NULL; |
Georg Brandl | e4e023c | 2006-05-26 20:22:50 +0000 | [diff] [blame] | 1145 | res = PyObject_CallFunction(func, "n", i); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1146 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1147 | return res; |
| 1148 | } |
| 1149 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1150 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1151 | instance_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1152 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1153 | PyObject *func, *arg, *res; |
| 1154 | static PyObject *getslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1155 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1156 | if (getslicestr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1157 | getslicestr = PyString_InternFromString("__getslice__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1158 | if (getslicestr == NULL) |
| 1159 | return NULL; |
| 1160 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1161 | func = instance_getattr(inst, getslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1162 | |
| 1163 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1164 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1165 | return NULL; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1166 | PyErr_Clear(); |
| 1167 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1168 | if (getitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1169 | getitemstr = PyString_InternFromString("__getitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1170 | if (getitemstr == NULL) |
| 1171 | return NULL; |
| 1172 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1173 | func = instance_getattr(inst, getitemstr); |
| 1174 | if (func == NULL) |
| 1175 | return NULL; |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 1176 | arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1177 | } else |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1178 | arg = Py_BuildValue("(nn)", i, j); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1179 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1180 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1181 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1182 | return NULL; |
| 1183 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1184 | res = PyEval_CallObject(func, arg); |
| 1185 | Py_DECREF(func); |
| 1186 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1187 | return res; |
| 1188 | } |
| 1189 | |
| 1190 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1191 | instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1192 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1193 | PyObject *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1194 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1195 | if (item == NULL) { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1196 | if (delitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1197 | delitemstr = PyString_InternFromString("__delitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1198 | if (delitemstr == NULL) |
| 1199 | return -1; |
| 1200 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1201 | func = instance_getattr(inst, delitemstr); |
| 1202 | } |
| 1203 | else { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1204 | if (setitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1205 | setitemstr = PyString_InternFromString("__setitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1206 | if (setitemstr == NULL) |
| 1207 | return -1; |
| 1208 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1209 | func = instance_getattr(inst, setitemstr); |
| 1210 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1211 | if (func == NULL) |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1212 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1213 | if (item == NULL) |
Georg Brandl | 2cfaa34 | 2006-05-29 19:39:45 +0000 | [diff] [blame] | 1214 | arg = PyInt_FromSsize_t(i); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1215 | else |
Georg Brandl | 2cfaa34 | 2006-05-29 19:39:45 +0000 | [diff] [blame] | 1216 | arg = Py_BuildValue("(nO)", i, item); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1217 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1218 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1219 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1220 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1221 | res = PyEval_CallObject(func, arg); |
| 1222 | Py_DECREF(func); |
| 1223 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1224 | if (res == NULL) |
| 1225 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1226 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1227 | return 0; |
| 1228 | } |
| 1229 | |
| 1230 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1231 | instance_ass_slice(PyInstanceObject *inst, Py_ssize_t i, Py_ssize_t j, PyObject *value) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1232 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1233 | PyObject *func, *arg, *res; |
| 1234 | static PyObject *setslicestr, *delslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1235 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1236 | if (value == NULL) { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1237 | if (delslicestr == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1238 | delslicestr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1239 | PyString_InternFromString("__delslice__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1240 | if (delslicestr == NULL) |
| 1241 | return -1; |
| 1242 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1243 | func = instance_getattr(inst, delslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1244 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1245 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1246 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1247 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1248 | if (delitemstr == NULL) { |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1249 | delitemstr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1250 | PyString_InternFromString("__delitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1251 | if (delitemstr == NULL) |
| 1252 | return -1; |
| 1253 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1254 | func = instance_getattr(inst, delitemstr); |
| 1255 | if (func == NULL) |
| 1256 | return -1; |
| 1257 | |
| 1258 | arg = Py_BuildValue("(N)", |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 1259 | _PySlice_FromIndices(i, j)); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1260 | } else |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1261 | arg = Py_BuildValue("(nn)", i, j); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1262 | } |
| 1263 | else { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1264 | if (setslicestr == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1265 | setslicestr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1266 | PyString_InternFromString("__setslice__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1267 | if (setslicestr == NULL) |
| 1268 | return -1; |
| 1269 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1270 | func = instance_getattr(inst, setslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1271 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1272 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1273 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1274 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1275 | if (setitemstr == NULL) { |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1276 | setitemstr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1277 | PyString_InternFromString("__setitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1278 | if (setitemstr == NULL) |
| 1279 | return -1; |
| 1280 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1281 | func = instance_getattr(inst, setitemstr); |
| 1282 | if (func == NULL) |
| 1283 | return -1; |
| 1284 | |
| 1285 | arg = Py_BuildValue("(NO)", |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 1286 | _PySlice_FromIndices(i, j), value); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1287 | } else |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1288 | arg = Py_BuildValue("(nnO)", i, j, value); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1289 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1290 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1291 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1292 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1293 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1294 | res = PyEval_CallObject(func, arg); |
| 1295 | Py_DECREF(func); |
| 1296 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1297 | if (res == NULL) |
| 1298 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1299 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1300 | return 0; |
| 1301 | } |
| 1302 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1303 | static int |
| 1304 | instance_contains(PyInstanceObject *inst, PyObject *member) |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1305 | { |
| 1306 | static PyObject *__contains__; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1307 | PyObject *func; |
| 1308 | |
| 1309 | /* Try __contains__ first. |
| 1310 | * If that can't be done, try iterator-based searching. |
| 1311 | */ |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1312 | |
| 1313 | if(__contains__ == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1314 | __contains__ = PyString_InternFromString("__contains__"); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1315 | if(__contains__ == NULL) |
| 1316 | return -1; |
| 1317 | } |
| 1318 | func = instance_getattr(inst, __contains__); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1319 | if (func) { |
| 1320 | PyObject *res; |
| 1321 | int ret; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1322 | PyObject *arg = PyTuple_Pack(1, member); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1323 | if(arg == NULL) { |
| 1324 | Py_DECREF(func); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1325 | return -1; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1326 | } |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1327 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1328 | Py_DECREF(func); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1329 | Py_DECREF(arg); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1330 | if(res == NULL) |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1331 | return -1; |
| 1332 | ret = PyObject_IsTrue(res); |
| 1333 | Py_DECREF(res); |
| 1334 | return ret; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1335 | } |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1336 | |
| 1337 | /* Couldn't find __contains__. */ |
| 1338 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Martin v. Löwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1339 | Py_ssize_t rc; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1340 | /* 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öwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1344 | rc = _PySequence_IterSearch((PyObject *)inst, member, |
| 1345 | PY_ITERSEARCH_CONTAINS); |
| 1346 | if (rc >= 0) |
| 1347 | return rc > 0; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1348 | } |
Martin v. Löwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1349 | return -1; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1350 | } |
| 1351 | |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1352 | static PySequenceMethods |
| 1353 | instance_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1354 | (lenfunc)instance_length, /* sq_length */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1355 | 0, /* sq_concat */ |
| 1356 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1357 | (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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1361 | (objobjproc)instance_contains, /* sq_contains */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1362 | }; |
| 1363 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1364 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1365 | generic_unary_op(PyInstanceObject *self, PyObject *methodname) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1366 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1367 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1368 | |
| 1369 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 1370 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1371 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1372 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1373 | return res; |
| 1374 | } |
| 1375 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1376 | static PyObject * |
| 1377 | generic_binary_op(PyObject *v, PyObject *w, char *opname) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1378 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1379 | PyObject *result; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1380 | PyObject *args; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1381 | PyObject *func = PyObject_GetAttrString(v, opname); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1382 | if (func == NULL) { |
Guido van Rossum | 617c1b0 | 1998-05-28 19:50:02 +0000 | [diff] [blame] | 1383 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1384 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1385 | PyErr_Clear(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1386 | Py_INCREF(Py_NotImplemented); |
| 1387 | return Py_NotImplemented; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1388 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1389 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1390 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1391 | Py_DECREF(func); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1392 | return NULL; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1393 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1394 | result = PyEval_CallObject(func, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1395 | Py_DECREF(args); |
| 1396 | Py_DECREF(func); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1397 | return result; |
| 1398 | } |
| 1399 | |
| 1400 | |
| 1401 | static PyObject *coerce_obj; |
| 1402 | |
| 1403 | /* Try one half of a binary operator involving a class instance. */ |
| 1404 | static PyObject * |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1405 | half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1406 | int swapped) |
| 1407 | { |
| 1408 | PyObject *args; |
| 1409 | PyObject *coercefunc; |
| 1410 | PyObject *coerced = NULL; |
| 1411 | PyObject *v1; |
| 1412 | PyObject *result; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1413 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1414 | if (!PyInstance_Check(v)) { |
| 1415 | Py_INCREF(Py_NotImplemented); |
| 1416 | return Py_NotImplemented; |
| 1417 | } |
| 1418 | |
| 1419 | if (coerce_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1420 | coerce_obj = PyString_InternFromString("__coerce__"); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1421 | if (coerce_obj == NULL) |
| 1422 | return NULL; |
| 1423 | } |
| 1424 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1425 | if (coercefunc == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1426 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1427 | return NULL; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1428 | PyErr_Clear(); |
| 1429 | return generic_binary_op(v, w, opname); |
| 1430 | } |
| 1431 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1432 | args = PyTuple_Pack(1, w); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1433 | if (args == NULL) { |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1434 | Py_DECREF(coercefunc); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1435 | 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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1455 | if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1456 | /* prevent recursion if __coerce__ returns self as the first |
| 1457 | * argument */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1458 | result = generic_binary_op(v1, w, opname); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1459 | } else { |
Brett Cannon | ea3912b | 2006-06-13 21:46:41 +0000 | [diff] [blame] | 1460 | if (Py_EnterRecursiveCall(" after coercion")) |
| 1461 | return NULL; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1462 | if (swapped) |
| 1463 | result = (thisfunc)(w, v1); |
| 1464 | else |
| 1465 | result = (thisfunc)(v1, w); |
Brett Cannon | ea3912b | 2006-06-13 21:46:41 +0000 | [diff] [blame] | 1466 | Py_LeaveRecursiveCall(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1467 | } |
| 1468 | Py_DECREF(coerced); |
| 1469 | return result; |
| 1470 | } |
| 1471 | |
| 1472 | /* Implement a binary operator involving at least one class instance. */ |
| 1473 | static PyObject * |
| 1474 | do_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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1479 | Py_DECREF(result); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1480 | result = half_binop(w, v, ropname, thisfunc, 1); |
| 1481 | } |
| 1482 | return result; |
| 1483 | } |
| 1484 | |
| 1485 | static PyObject * |
| 1486 | do_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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1491 | Py_DECREF(result); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1492 | result = do_binop(v, w, opname, ropname, thisfunc); |
| 1493 | } |
| 1494 | return result; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1495 | } |
| 1496 | |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1497 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1498 | instance_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1499 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1500 | PyObject *v = *pv; |
| 1501 | PyObject *w = *pw; |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1502 | PyObject *coercefunc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1503 | PyObject *args; |
| 1504 | PyObject *coerced; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1505 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1506 | if (coerce_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1507 | coerce_obj = PyString_InternFromString("__coerce__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1508 | if (coerce_obj == NULL) |
| 1509 | return -1; |
| 1510 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1511 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1512 | if (coercefunc == NULL) { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1513 | /* No __coerce__ method */ |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1514 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1515 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1516 | PyErr_Clear(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1517 | return 1; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1518 | } |
| 1519 | /* Has __coerce__ method: call it */ |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1520 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1521 | if (args == NULL) { |
| 1522 | return -1; |
| 1523 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1524 | coerced = PyEval_CallObject(coercefunc, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1525 | Py_DECREF(args); |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1526 | Py_DECREF(coercefunc); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1527 | if (coerced == NULL) { |
| 1528 | /* __coerce__ call raised an exception */ |
| 1529 | return -1; |
| 1530 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1531 | if (coerced == Py_None || coerced == Py_NotImplemented) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1532 | /* __coerce__ says "I can't do it" */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1533 | Py_DECREF(coerced); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1534 | return 1; |
| 1535 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1536 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1537 | /* __coerce__ return value is malformed */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1538 | Py_DECREF(coerced); |
| 1539 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1540 | "coercion should return None or 2-tuple"); |
| 1541 | return -1; |
| 1542 | } |
| 1543 | /* __coerce__ returned two new values */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1544 | *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 Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1549 | return 0; |
| 1550 | } |
| 1551 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1552 | #define UNARY(funcname, methodname) \ |
Thomas Wouters | c307352 | 2000-07-23 22:09:59 +0000 | [diff] [blame] | 1553 | static PyObject *funcname(PyInstanceObject *self) { \ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1554 | static PyObject *o; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1555 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1556 | if (o == NULL) return NULL; } \ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1557 | return generic_unary_op(self, o); \ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1560 | /* unary function with a fallback */ |
| 1561 | #define UNARY_FB(funcname, methodname, funcname_fb) \ |
| 1562 | static PyObject *funcname(PyInstanceObject *self) { \ |
| 1563 | static PyObject *o; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1564 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1565 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1572 | #define BINARY(f, m, n) \ |
| 1573 | static 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) \ |
| 1578 | static PyObject *f(PyObject *v, PyObject *w) { \ |
| 1579 | return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \ |
| 1580 | "__r" m "__", n); \ |
| 1581 | } |
| 1582 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1583 | UNARY(instance_neg, "__neg__") |
| 1584 | UNARY(instance_pos, "__pos__") |
| 1585 | UNARY(instance_abs, "__abs__") |
| 1586 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1587 | BINARY(instance_or, "or", PyNumber_Or) |
| 1588 | BINARY(instance_and, "and", PyNumber_And) |
| 1589 | BINARY(instance_xor, "xor", PyNumber_Xor) |
| 1590 | BINARY(instance_lshift, "lshift", PyNumber_Lshift) |
| 1591 | BINARY(instance_rshift, "rshift", PyNumber_Rshift) |
| 1592 | BINARY(instance_add, "add", PyNumber_Add) |
| 1593 | BINARY(instance_sub, "sub", PyNumber_Subtract) |
| 1594 | BINARY(instance_mul, "mul", PyNumber_Multiply) |
| 1595 | BINARY(instance_div, "div", PyNumber_Divide) |
| 1596 | BINARY(instance_mod, "mod", PyNumber_Remainder) |
| 1597 | BINARY(instance_divmod, "divmod", PyNumber_Divmod) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1598 | BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide) |
| 1599 | BINARY(instance_truediv, "truediv", PyNumber_TrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1600 | |
| 1601 | BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr) |
| 1602 | BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor) |
| 1603 | BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd) |
| 1604 | BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift) |
| 1605 | BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift) |
| 1606 | BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd) |
| 1607 | BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract) |
| 1608 | BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply) |
| 1609 | BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide) |
| 1610 | BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1611 | BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide) |
| 1612 | BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1613 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1614 | /* 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 | */ |
| 1621 | static int |
| 1622 | half_cmp(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1623 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1624 | static PyObject *cmp_obj; |
| 1625 | PyObject *args; |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1626 | PyObject *cmp_func; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1627 | PyObject *result; |
| 1628 | long l; |
| 1629 | |
| 1630 | assert(PyInstance_Check(v)); |
| 1631 | |
| 1632 | if (cmp_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1633 | cmp_obj = PyString_InternFromString("__cmp__"); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1634 | if (cmp_obj == NULL) |
| 1635 | return -2; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1636 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1637 | |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1638 | cmp_func = PyObject_GetAttr(v, cmp_obj); |
| 1639 | if (cmp_func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1640 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1641 | return -2; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1642 | PyErr_Clear(); |
| 1643 | return 2; |
| 1644 | } |
| 1645 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1646 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1647 | if (args == NULL) { |
| 1648 | Py_DECREF(cmp_func); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1649 | return -2; |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1650 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1651 | |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1652 | result = PyEval_CallObject(cmp_func, args); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1653 | Py_DECREF(args); |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1654 | Py_DECREF(cmp_func); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1655 | |
| 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1673 | } |
| 1674 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1675 | /* 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 | */ |
| 1684 | static int |
| 1685 | instance_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1686 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1687 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1701 | } |
| 1702 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1703 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1709 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1710 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1717 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1718 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1727 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1728 | Py_DECREF(v); |
| 1729 | Py_DECREF(w); |
| 1730 | return 2; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1731 | } |
| 1732 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1733 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1734 | instance_nonzero(PyInstanceObject *self) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1735 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1736 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1737 | long outcome; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1738 | static PyObject *nonzerostr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1739 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1740 | if (nonzerostr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1741 | nonzerostr = PyString_InternFromString("__nonzero__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1742 | if (nonzerostr == NULL) |
| 1743 | return -1; |
| 1744 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1745 | if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1746 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1747 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1748 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1749 | if (lenstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1750 | lenstr = PyString_InternFromString("__len__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1751 | if (lenstr == NULL) |
| 1752 | return -1; |
| 1753 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1754 | if ((func = instance_getattr(self, lenstr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1755 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1756 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1757 | PyErr_Clear(); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1758 | /* Fall back to the default behavior: |
| 1759 | all instances are nonzero */ |
| 1760 | return 1; |
| 1761 | } |
| 1762 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1763 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1764 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1765 | if (res == NULL) |
| 1766 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1767 | if (!PyInt_Check(res)) { |
| 1768 | Py_DECREF(res); |
| 1769 | PyErr_SetString(PyExc_TypeError, |
| 1770 | "__nonzero__ should return an int"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1771 | return -1; |
| 1772 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1773 | outcome = PyInt_AsLong(res); |
| 1774 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1775 | if (outcome < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1776 | PyErr_SetString(PyExc_ValueError, |
| 1777 | "__nonzero__ should return >= 0"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1778 | return -1; |
| 1779 | } |
| 1780 | return outcome > 0; |
| 1781 | } |
| 1782 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1783 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1784 | instance_index(PyInstanceObject *self) |
| 1785 | { |
| 1786 | PyObject *func, *res; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1787 | static PyObject *indexstr = NULL; |
| 1788 | |
| 1789 | if (indexstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1790 | indexstr = PyString_InternFromString("__index__"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1791 | if (indexstr == NULL) |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1792 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1793 | } |
| 1794 | if ((func = instance_getattr(self, indexstr)) == NULL) { |
| 1795 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1796 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1797 | PyErr_Clear(); |
| 1798 | PyErr_SetString(PyExc_TypeError, |
| 1799 | "object cannot be interpreted as an index"); |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1800 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1801 | } |
| 1802 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1803 | Py_DECREF(func); |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1804 | return res; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
| 1807 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1808 | UNARY(instance_invert, "__invert__") |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1809 | UNARY(_instance_trunc, "__trunc__") |
| 1810 | |
| 1811 | static PyObject * |
| 1812 | instance_int(PyInstanceObject *self) |
| 1813 | { |
| 1814 | PyObject *truncated; |
| 1815 | static PyObject *int_name; |
| 1816 | if (int_name == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1817 | int_name = PyString_InternFromString("__int__"); |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1818 | 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ónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1832 | UNARY_FB(instance_long, "__long__", instance_int) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1833 | UNARY(instance_float, "__float__") |
| 1834 | UNARY(instance_oct, "__oct__") |
| 1835 | UNARY(instance_hex, "__hex__") |
| 1836 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1837 | static PyObject * |
| 1838 | bin_power(PyObject *v, PyObject *w) |
| 1839 | { |
| 1840 | return PyNumber_Power(v, w, Py_None); |
| 1841 | } |
| 1842 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1843 | /* This version is for ternary calls only (z != None) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1844 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1845 | instance_pow(PyObject *v, PyObject *w, PyObject *z) |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1846 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1847 | if (z == Py_None) { |
| 1848 | return do_binop(v, w, "__pow__", "__rpow__", bin_power); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1849 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1850 | 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 Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1859 | args = PyTuple_Pack(2, w, z); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1860 | 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 Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1869 | } |
| 1870 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1871 | static PyObject * |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1872 | bin_inplace_power(PyObject *v, PyObject *w) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1873 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1874 | return PyNumber_InPlacePower(v, w, Py_None); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1878 | static PyObject * |
| 1879 | instance_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 Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1898 | args = PyTuple_Pack(2, w, z); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1899 | 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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1911 | /* Map rich comparison operators to their __xx__ namesakes */ |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1912 | #define NAME_OPS 6 |
| 1913 | static PyObject **name_op = NULL; |
| 1914 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1915 | static int |
Guido van Rossum | 0ba9e3a | 2001-05-22 02:33:08 +0000 | [diff] [blame] | 1916 | init_name_op(void) |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1917 | { |
| 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. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1932 | name_op[i] = PyString_InternFromString(_name_op[i]); |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1933 | if (name_op[i] == NULL) |
| 1934 | return -1; |
| 1935 | } |
| 1936 | return 0; |
| 1937 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1938 | |
| 1939 | static PyObject * |
| 1940 | half_richcompare(PyObject *v, PyObject *w, int op) |
| 1941 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1942 | PyObject *method; |
| 1943 | PyObject *args; |
| 1944 | PyObject *res; |
| 1945 | |
| 1946 | assert(PyInstance_Check(v)); |
| 1947 | |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1948 | if (name_op == NULL) { |
| 1949 | if (init_name_op() < 0) |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1950 | return NULL; |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1951 | } |
| 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 Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1955 | if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1956 | method = instance_getattr2((PyInstanceObject *)v, |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1957 | name_op[op]); |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1958 | else |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1959 | method = PyObject_GetAttr(v, name_op[op]); |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1960 | if (method == NULL) { |
| 1961 | if (PyErr_Occurred()) { |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1962 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1963 | return NULL; |
| 1964 | PyErr_Clear(); |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1965 | } |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1966 | res = Py_NotImplemented; |
| 1967 | Py_INCREF(res); |
| 1968 | return res; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1969 | } |
| 1970 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1971 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1972 | 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 Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1984 | static PyObject * |
| 1985 | instance_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 Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 1997 | res = half_richcompare(w, v, _Py_SwappedOp[op]); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1998 | 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 Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 2007 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2008 | /* Get the iterator */ |
| 2009 | static PyObject * |
| 2010 | instance_getiter(PyInstanceObject *self) |
| 2011 | { |
| 2012 | PyObject *func; |
| 2013 | |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2014 | if (iterstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2015 | iterstr = PyString_InternFromString("__iter__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2016 | if (iterstr == NULL) |
| 2017 | return NULL; |
| 2018 | } |
| 2019 | if (getitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2020 | getitemstr = PyString_InternFromString("__getitem__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2021 | if (getitemstr == NULL) |
| 2022 | return NULL; |
| 2023 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2024 | |
| 2025 | if ((func = instance_getattr(self, iterstr)) != NULL) { |
| 2026 | PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
| 2027 | Py_DECREF(func); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2028 | 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 Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2036 | return res; |
| 2037 | } |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2038 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2039 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2040 | PyErr_Clear(); |
| 2041 | if ((func = instance_getattr(self, getitemstr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2042 | PyErr_SetString(PyExc_TypeError, |
| 2043 | "iteration over non-sequence"); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2044 | return NULL; |
| 2045 | } |
| 2046 | Py_DECREF(func); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2047 | return PySeqIter_New((PyObject *)self); |
| 2048 | } |
| 2049 | |
| 2050 | |
| 2051 | /* Call the iterator's next */ |
| 2052 | static PyObject * |
| 2053 | instance_iternext(PyInstanceObject *self) |
| 2054 | { |
| 2055 | PyObject *func; |
| 2056 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 2057 | if (nextstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2058 | nextstr = PyString_InternFromString("next"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 2059 | if (nextstr == NULL) |
| 2060 | return NULL; |
| 2061 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2062 | |
| 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 Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2077 | } |
| 2078 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2079 | static PyObject * |
| 2080 | instance_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 Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2085 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2086 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2087 | PyErr_Clear(); |
| 2088 | PyErr_Format(PyExc_AttributeError, |
| 2089 | "%.200s instance has no __call__ method", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2090 | PyString_AsString(inst->in_class->cl_name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2091 | return NULL; |
| 2092 | } |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2093 | /* 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 Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2101 | if (Py_EnterRecursiveCall(" in __call__")) { |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2102 | res = NULL; |
| 2103 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2104 | else { |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2105 | res = PyObject_Call(call, arg, kw); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2106 | Py_LeaveRecursiveCall(); |
| 2107 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2108 | Py_DECREF(call); |
| 2109 | return res; |
| 2110 | } |
| 2111 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2112 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2113 | static PyNumberMethods instance_as_number = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2114 | 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 Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 2152 | (unaryfunc)instance_index, /* nb_index */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2153 | }; |
| 2154 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2155 | PyTypeObject PyInstance_Type = { |
| 2156 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2157 | 0, |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 2158 | "instance", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2159 | sizeof(PyInstanceObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2160 | 0, |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2161 | (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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2171 | instance_call, /* tp_call */ |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 2172 | (reprfunc)instance_str, /* tp_str */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2173 | (getattrofunc)instance_getattr, /* tp_getattro */ |
| 2174 | (setattrofunc)instance_setattr, /* tp_setattro */ |
| 2175 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2176 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2177 | instance_doc, /* tp_doc */ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2178 | (traverseproc)instance_traverse, /* tp_traverse */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2179 | 0, /* tp_clear */ |
| 2180 | instance_richcompare, /* tp_richcompare */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2181 | offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ |
| 2182 | (getiterfunc)instance_getiter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2183 | (iternextfunc)instance_iternext, /* tp_iternext */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2184 | 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2195 | }; |
| 2196 | |
| 2197 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2198 | /* 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 Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2203 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2204 | PyObject * |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2205 | PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2206 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2207 | register PyMethodObject *im; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2208 | if (!PyCallable_Check(func)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2209 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2210 | return NULL; |
| 2211 | } |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2212 | im = free_list; |
| 2213 | if (im != NULL) { |
| 2214 | free_list = (PyMethodObject *)(im->im_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2215 | PyObject_INIT(im, &PyMethod_Type); |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2216 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2217 | } |
| 2218 | else { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2219 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2220 | if (im == NULL) |
| 2221 | return NULL; |
| 2222 | } |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 2223 | im->im_weakreflist = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2224 | Py_INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 2225 | im->im_func = func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2226 | Py_XINCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 2227 | im->im_self = self; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2228 | Py_XINCREF(klass); |
| 2229 | im->im_class = klass; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2230 | _PyObject_GC_TRACK(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2231 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2232 | } |
| 2233 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2234 | /* Descriptors for PyMethod attributes */ |
| 2235 | |
| 2236 | /* im_class, im_func and im_self are stored in the PyMethod object */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2237 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2238 | #define OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2239 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2240 | static 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 Norwitz | b1d3d96 | 2008-03-18 04:46:00 +0000 | [diff] [blame] | 2245 | {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 2246 | "the function (or other callable) implementing a method"}, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2247 | {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2248 | "the instance to which a method is bound; None for unbound methods"}, |
Neal Norwitz | b1d3d96 | 2008-03-18 04:46:00 +0000 | [diff] [blame] | 2249 | {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2250 | "the instance to which a method is bound; None for unbound methods"}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2251 | {NULL} /* Sentinel */ |
| 2252 | }; |
| 2253 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2254 | /* 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 | |
| 2259 | static PyObject * |
| 2260 | instancemethod_get_doc(PyMethodObject *im, void *context) |
| 2261 | { |
| 2262 | static PyObject *docstr; |
| 2263 | if (docstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2264 | docstr= PyString_InternFromString("__doc__"); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2265 | if (docstr == NULL) |
| 2266 | return NULL; |
| 2267 | } |
| 2268 | return PyObject_GetAttr(im->im_func, docstr); |
| 2269 | } |
| 2270 | |
| 2271 | static PyGetSetDef instancemethod_getset[] = { |
| 2272 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 2273 | {0} |
| 2274 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2275 | |
| 2276 | static PyObject * |
| 2277 | instancemethod_getattro(PyObject *obj, PyObject *name) |
| 2278 | { |
| 2279 | PyMethodObject *im = (PyMethodObject *)obj; |
| 2280 | PyTypeObject *tp = obj->ob_type; |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2281 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2282 | |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 2283 | 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 Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 2289 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2290 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2291 | if (descr != NULL) { |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2292 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 2293 | if (f != NULL) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2294 | return f(descr, obj, (PyObject *)obj->ob_type); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2295 | else { |
| 2296 | Py_INCREF(descr); |
| 2297 | return descr; |
| 2298 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2299 | } |
| 2300 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2301 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2302 | } |
| 2303 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2304 | PyDoc_STRVAR(instancemethod_doc, |
| 2305 | "instancemethod(function, instance, class)\n\ |
| 2306 | \n\ |
| 2307 | Create an instance method object."); |
| 2308 | |
| 2309 | static PyObject * |
| 2310 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 2311 | { |
| 2312 | PyObject *func; |
| 2313 | PyObject *self; |
Guido van Rossum | 2fb9fdc | 2003-04-09 19:35:08 +0000 | [diff] [blame] | 2314 | PyObject *classObj = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2315 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 2316 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 2317 | return NULL; |
Guido van Rossum | 2fb9fdc | 2003-04-09 19:35:08 +0000 | [diff] [blame] | 2318 | if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2319 | &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. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 2328 | 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 Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2334 | return PyMethod_New(func, self, classObj); |
| 2335 | } |
| 2336 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2337 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2338 | instancemethod_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2339 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2340 | _PyObject_GC_UNTRACK(im); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 2341 | if (im->im_weakreflist != NULL) |
| 2342 | PyObject_ClearWeakRefs((PyObject *)im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2343 | Py_DECREF(im->im_func); |
| 2344 | Py_XDECREF(im->im_self); |
Guido van Rossum | cdf0d75 | 2001-08-17 12:07:34 +0000 | [diff] [blame] | 2345 | Py_XDECREF(im->im_class); |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 2346 | if (numfree < PyMethod_MAXFREELIST) { |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2347 | im->im_self = (PyObject *)free_list; |
| 2348 | free_list = im; |
| 2349 | numfree++; |
| 2350 | } |
| 2351 | else { |
| 2352 | PyObject_GC_Del(im); |
| 2353 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2354 | } |
| 2355 | |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2356 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2357 | instancemethod_compare(PyMethodObject *a, PyMethodObject *b) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2358 | { |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2359 | 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 Rossum | e9df727 | 1995-04-06 14:46:51 +0000 | [diff] [blame] | 2367 | return (a->im_self < b->im_self) ? -1 : 1; |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2368 | else |
| 2369 | return PyObject_Compare(a->im_self, b->im_self); |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2370 | } |
| 2371 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2372 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2373 | instancemethod_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2374 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2375 | PyObject *self = a->im_self; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2376 | PyObject *func = a->im_func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2377 | 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 Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2382 | if (funcname == NULL) { |
| 2383 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2384 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2385 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2386 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2387 | else if (!PyString_Check(funcname)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2388 | Py_DECREF(funcname); |
| 2389 | funcname = NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2390 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2391 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2392 | sfuncname = PyString_AS_STRING(funcname); |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2393 | if (klass == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2394 | klassname = NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2395 | else { |
| 2396 | klassname = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2397 | if (klassname == NULL) { |
| 2398 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2399 | return NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2400 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2401 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2402 | else if (!PyString_Check(klassname)) { |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2403 | Py_DECREF(klassname); |
| 2404 | klassname = NULL; |
| 2405 | } |
| 2406 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2407 | sklassname = PyString_AS_STRING(klassname); |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2408 | } |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2409 | if (self == NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2410 | result = PyString_FromFormat("<unbound method %s.%s>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2411 | sklassname, sfuncname); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2412 | else { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2413 | /* XXX Shouldn't use repr() here! */ |
| 2414 | PyObject *selfrepr = PyObject_Repr(self); |
| 2415 | if (selfrepr == NULL) |
| 2416 | goto fail; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2417 | if (!PyString_Check(selfrepr)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2418 | Py_DECREF(selfrepr); |
| 2419 | goto fail; |
| 2420 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2421 | result = PyString_FromFormat("<bound method %s.%s of %s>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2422 | sklassname, sfuncname, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2423 | PyString_AS_STRING(selfrepr)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2424 | Py_DECREF(selfrepr); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2425 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2426 | fail: |
Guido van Rossum | 42636dc | 1999-10-11 14:03:12 +0000 | [diff] [blame] | 2427 | Py_XDECREF(funcname); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2428 | Py_XDECREF(klassname); |
| 2429 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2432 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2433 | instancemethod_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2434 | { |
| 2435 | long x, y; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2436 | if (a->im_self == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2437 | x = PyObject_Hash(Py_None); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2438 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2439 | x = PyObject_Hash(a->im_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2440 | if (x == -1) |
| 2441 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2442 | y = PyObject_Hash(a->im_func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2443 | if (y == -1) |
| 2444 | return -1; |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2445 | x = x ^ y; |
| 2446 | if (x == -1) |
| 2447 | x = -2; |
| 2448 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2449 | } |
| 2450 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2451 | static int |
| 2452 | instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) |
| 2453 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2454 | Py_VISIT(im->im_func); |
| 2455 | Py_VISIT(im->im_self); |
| 2456 | Py_VISIT(im->im_class); |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 2457 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2458 | } |
| 2459 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2460 | static void |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2461 | getclassname(PyObject *klass, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2462 | { |
| 2463 | PyObject *name; |
| 2464 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2465 | assert(bufsize > 1); |
| 2466 | strcpy(buf, "?"); /* Default outcome */ |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2467 | if (klass == NULL) |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2468 | return; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2469 | name = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2470 | if (name == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2471 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2472 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2473 | return; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2474 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2475 | if (PyString_Check(name)) { |
| 2476 | strncpy(buf, PyString_AS_STRING(name), bufsize); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2477 | buf[bufsize-1] = '\0'; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2478 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2479 | Py_DECREF(name); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2480 | } |
| 2481 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2482 | static void |
| 2483 | getinstclassname(PyObject *inst, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2484 | { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2485 | PyObject *klass; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2486 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2487 | if (inst == NULL) { |
Tim Peters | 75585d4 | 2002-08-20 14:31:35 +0000 | [diff] [blame] | 2488 | assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2489 | strcpy(buf, "nothing"); |
| 2490 | return; |
| 2491 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2492 | |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2493 | klass = PyObject_GetAttrString(inst, "__class__"); |
| 2494 | if (klass == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2495 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2496 | PyErr_Clear(); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2497 | klass = (PyObject *)(inst->ob_type); |
| 2498 | Py_INCREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2499 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2500 | getclassname(klass, buf, bufsize); |
| 2501 | Py_XDECREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2504 | static PyObject * |
| 2505 | instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2506 | { |
| 2507 | PyObject *self = PyMethod_GET_SELF(func); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2508 | PyObject *klass = PyMethod_GET_CLASS(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2509 | 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 Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2521 | ok = PyObject_IsInstance(self, klass); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2522 | if (ok < 0) |
| 2523 | return NULL; |
| 2524 | } |
| 2525 | if (!ok) { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2526 | char clsbuf[256]; |
| 2527 | char instbuf[256]; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2528 | getclassname(klass, clsbuf, sizeof(clsbuf)); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2529 | getinstclassname(self, instbuf, sizeof(instbuf)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2530 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2531 | "unbound method %s%s must be called with " |
| 2532 | "%s instance as first argument " |
| 2533 | "(got %s%s instead)", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2534 | PyEval_GetFuncName(func), |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2535 | PyEval_GetFuncDesc(func), |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2536 | clsbuf, |
| 2537 | instbuf, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2538 | self == NULL ? "" : " instance"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2539 | return NULL; |
| 2540 | } |
| 2541 | Py_INCREF(arg); |
| 2542 | } |
| 2543 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2544 | Py_ssize_t argcount = PyTuple_Size(arg); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2545 | 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 Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2563 | static PyObject * |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2564 | instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2565 | { |
Guido van Rossum | f23c41d | 2001-08-17 13:43:27 +0000 | [diff] [blame] | 2566 | /* Don't rebind an already bound method, or an unbound method |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2567 | of a class that's not a base class of cls. */ |
| 2568 | |
| 2569 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 2570 | /* Already bound */ |
Guido van Rossum | 501c7c7 | 2001-08-16 20:41:56 +0000 | [diff] [blame] | 2571 | Py_INCREF(meth); |
| 2572 | return meth; |
| 2573 | } |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2574 | /* 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 Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2587 | } |
| 2588 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2589 | PyTypeObject PyMethod_Type = { |
| 2590 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2591 | 0, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2592 | "instancemethod", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2593 | sizeof(PyMethodObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2594 | 0, |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2595 | (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 Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2605 | instancemethod_call, /* tp_call */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2606 | 0, /* tp_str */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2607 | instancemethod_getattro, /* tp_getattro */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2608 | PyObject_GenericSetAttr, /* tp_setattro */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2609 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bff60ae | 2005-06-19 08:42:20 +0000 | [diff] [blame] | 2610 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2611 | instancemethod_doc, /* tp_doc */ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2612 | (traverseproc)instancemethod_traverse, /* tp_traverse */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 2613 | 0, /* tp_clear */ |
| 2614 | 0, /* tp_richcompare */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2615 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 2616 | 0, /* tp_iter */ |
| 2617 | 0, /* tp_iternext */ |
| 2618 | 0, /* tp_methods */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2619 | instancemethod_memberlist, /* tp_members */ |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2620 | instancemethod_getset, /* tp_getset */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2621 | 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 Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2626 | 0, /* tp_init */ |
| 2627 | 0, /* tp_alloc */ |
| 2628 | instancemethod_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2629 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2630 | |
| 2631 | /* Clear out the free list */ |
| 2632 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2633 | int |
| 2634 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2635 | { |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2636 | int freelist_size = numfree; |
| 2637 | |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2638 | while (free_list) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2639 | PyMethodObject *im = free_list; |
| 2640 | free_list = (PyMethodObject *)(im->im_self); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2641 | PyObject_GC_Del(im); |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2642 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2643 | } |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2644 | assert(numfree == 0); |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2645 | return freelist_size; |
| 2646 | } |
| 2647 | |
| 2648 | void |
| 2649 | PyMethod_Fini(void) |
| 2650 | { |
| 2651 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2652 | } |