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)); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1177 | } |
| 1178 | else { |
| 1179 | if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " |
| 1180 | "use __getitem__", 1) < 0) { |
| 1181 | Py_DECREF(func); |
| 1182 | return NULL; |
| 1183 | } |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1184 | arg = Py_BuildValue("(nn)", i, j); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1185 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1186 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1187 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1188 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1189 | return NULL; |
| 1190 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1191 | res = PyEval_CallObject(func, arg); |
| 1192 | Py_DECREF(func); |
| 1193 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1194 | return res; |
| 1195 | } |
| 1196 | |
| 1197 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1198 | instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1199 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1200 | PyObject *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1201 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1202 | if (item == NULL) { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1203 | if (delitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1204 | delitemstr = PyString_InternFromString("__delitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1205 | if (delitemstr == NULL) |
| 1206 | return -1; |
| 1207 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1208 | func = instance_getattr(inst, delitemstr); |
| 1209 | } |
| 1210 | else { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1211 | if (setitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1212 | setitemstr = PyString_InternFromString("__setitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1213 | if (setitemstr == NULL) |
| 1214 | return -1; |
| 1215 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1216 | func = instance_getattr(inst, setitemstr); |
| 1217 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1218 | if (func == NULL) |
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 | if (item == NULL) |
Georg Brandl | 2cfaa34 | 2006-05-29 19:39:45 +0000 | [diff] [blame] | 1221 | arg = PyInt_FromSsize_t(i); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1222 | else |
Georg Brandl | 2cfaa34 | 2006-05-29 19:39:45 +0000 | [diff] [blame] | 1223 | arg = Py_BuildValue("(nO)", i, item); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1224 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1225 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1226 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1227 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1228 | res = PyEval_CallObject(func, arg); |
| 1229 | Py_DECREF(func); |
| 1230 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1231 | if (res == NULL) |
| 1232 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1233 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1234 | return 0; |
| 1235 | } |
| 1236 | |
| 1237 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1238 | 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] | 1239 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1240 | PyObject *func, *arg, *res; |
| 1241 | static PyObject *setslicestr, *delslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1242 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1243 | if (value == NULL) { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1244 | if (delslicestr == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1245 | delslicestr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1246 | PyString_InternFromString("__delslice__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1247 | if (delslicestr == NULL) |
| 1248 | return -1; |
| 1249 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1250 | func = instance_getattr(inst, delslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1251 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1252 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1253 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1254 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1255 | if (delitemstr == NULL) { |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1256 | delitemstr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1257 | PyString_InternFromString("__delitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1258 | if (delitemstr == NULL) |
| 1259 | return -1; |
| 1260 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1261 | func = instance_getattr(inst, delitemstr); |
| 1262 | if (func == NULL) |
| 1263 | return -1; |
| 1264 | |
| 1265 | arg = Py_BuildValue("(N)", |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 1266 | _PySlice_FromIndices(i, j)); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1267 | } |
| 1268 | else { |
| 1269 | if (PyErr_WarnPy3k("in 3.x, __delslice__ has been " |
| 1270 | "removed; use __delitem__", 1) < 0) { |
| 1271 | Py_DECREF(func); |
| 1272 | return -1; |
| 1273 | } |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1274 | arg = Py_BuildValue("(nn)", i, j); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1275 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1276 | } |
| 1277 | else { |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1278 | if (setslicestr == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1279 | setslicestr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1280 | PyString_InternFromString("__setslice__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1281 | if (setslicestr == NULL) |
| 1282 | return -1; |
| 1283 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1284 | func = instance_getattr(inst, setslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1285 | if (func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1286 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1287 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1288 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1289 | if (setitemstr == NULL) { |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1290 | setitemstr = |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1291 | PyString_InternFromString("__setitem__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1292 | if (setitemstr == NULL) |
| 1293 | return -1; |
| 1294 | } |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1295 | func = instance_getattr(inst, setitemstr); |
| 1296 | if (func == NULL) |
| 1297 | return -1; |
| 1298 | |
| 1299 | arg = Py_BuildValue("(NO)", |
Neal Norwitz | badc086 | 2006-03-23 06:03:08 +0000 | [diff] [blame] | 1300 | _PySlice_FromIndices(i, j), value); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1301 | } |
| 1302 | else { |
| 1303 | if (PyErr_WarnPy3k("in 3.x, __setslice__ has been " |
| 1304 | "removed; use __setitem__", 1) < 0) { |
| 1305 | Py_DECREF(func); |
| 1306 | return -1; |
| 1307 | } |
Martin v. Löwis | dde99d2 | 2006-02-17 15:57:41 +0000 | [diff] [blame] | 1308 | arg = Py_BuildValue("(nnO)", i, j, value); |
Benjamin Peterson | 712ee92 | 2008-08-24 18:10:20 +0000 | [diff] [blame] | 1309 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1310 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1311 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1312 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1313 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1314 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1315 | res = PyEval_CallObject(func, arg); |
| 1316 | Py_DECREF(func); |
| 1317 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1318 | if (res == NULL) |
| 1319 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1320 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1321 | return 0; |
| 1322 | } |
| 1323 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1324 | static int |
| 1325 | instance_contains(PyInstanceObject *inst, PyObject *member) |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1326 | { |
| 1327 | static PyObject *__contains__; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1328 | PyObject *func; |
| 1329 | |
| 1330 | /* Try __contains__ first. |
| 1331 | * If that can't be done, try iterator-based searching. |
| 1332 | */ |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1333 | |
| 1334 | if(__contains__ == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1335 | __contains__ = PyString_InternFromString("__contains__"); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1336 | if(__contains__ == NULL) |
| 1337 | return -1; |
| 1338 | } |
| 1339 | func = instance_getattr(inst, __contains__); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1340 | if (func) { |
| 1341 | PyObject *res; |
| 1342 | int ret; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1343 | PyObject *arg = PyTuple_Pack(1, member); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1344 | if(arg == NULL) { |
| 1345 | Py_DECREF(func); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1346 | return -1; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1347 | } |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1348 | res = PyEval_CallObject(func, arg); |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1349 | Py_DECREF(func); |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1350 | Py_DECREF(arg); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1351 | if(res == NULL) |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1352 | return -1; |
| 1353 | ret = PyObject_IsTrue(res); |
| 1354 | Py_DECREF(res); |
| 1355 | return ret; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1356 | } |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1357 | |
| 1358 | /* Couldn't find __contains__. */ |
| 1359 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Martin v. Löwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1360 | Py_ssize_t rc; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1361 | /* Assume the failure was simply due to that there is no |
| 1362 | * __contains__ attribute, and try iterating instead. |
| 1363 | */ |
| 1364 | PyErr_Clear(); |
Martin v. Löwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1365 | rc = _PySequence_IterSearch((PyObject *)inst, member, |
| 1366 | PY_ITERSEARCH_CONTAINS); |
| 1367 | if (rc >= 0) |
| 1368 | return rc > 0; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1369 | } |
Martin v. Löwis | 3a62404 | 2006-11-08 06:46:37 +0000 | [diff] [blame] | 1370 | return -1; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1371 | } |
| 1372 | |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1373 | static PySequenceMethods |
| 1374 | instance_as_sequence = { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1375 | (lenfunc)instance_length, /* sq_length */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1376 | 0, /* sq_concat */ |
| 1377 | 0, /* sq_repeat */ |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1378 | (ssizeargfunc)instance_item, /* sq_item */ |
| 1379 | (ssizessizeargfunc)instance_slice, /* sq_slice */ |
| 1380 | (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ |
| 1381 | (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1382 | (objobjproc)instance_contains, /* sq_contains */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1383 | }; |
| 1384 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1385 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1386 | generic_unary_op(PyInstanceObject *self, PyObject *methodname) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1387 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1388 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1389 | |
| 1390 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 1391 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1392 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1393 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1394 | return res; |
| 1395 | } |
| 1396 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1397 | static PyObject * |
| 1398 | generic_binary_op(PyObject *v, PyObject *w, char *opname) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1399 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1400 | PyObject *result; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1401 | PyObject *args; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1402 | PyObject *func = PyObject_GetAttrString(v, opname); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1403 | if (func == NULL) { |
Guido van Rossum | 617c1b0 | 1998-05-28 19:50:02 +0000 | [diff] [blame] | 1404 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1405 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1406 | PyErr_Clear(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1407 | Py_INCREF(Py_NotImplemented); |
| 1408 | return Py_NotImplemented; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1409 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1410 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1411 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1412 | Py_DECREF(func); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1413 | return NULL; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1414 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1415 | result = PyEval_CallObject(func, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1416 | Py_DECREF(args); |
| 1417 | Py_DECREF(func); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1418 | return result; |
| 1419 | } |
| 1420 | |
| 1421 | |
| 1422 | static PyObject *coerce_obj; |
| 1423 | |
| 1424 | /* Try one half of a binary operator involving a class instance. */ |
| 1425 | static PyObject * |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1426 | half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1427 | int swapped) |
| 1428 | { |
| 1429 | PyObject *args; |
| 1430 | PyObject *coercefunc; |
| 1431 | PyObject *coerced = NULL; |
| 1432 | PyObject *v1; |
| 1433 | PyObject *result; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1434 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1435 | if (!PyInstance_Check(v)) { |
| 1436 | Py_INCREF(Py_NotImplemented); |
| 1437 | return Py_NotImplemented; |
| 1438 | } |
| 1439 | |
| 1440 | if (coerce_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1441 | coerce_obj = PyString_InternFromString("__coerce__"); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1442 | if (coerce_obj == NULL) |
| 1443 | return NULL; |
| 1444 | } |
| 1445 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1446 | if (coercefunc == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1447 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1448 | return NULL; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1449 | PyErr_Clear(); |
| 1450 | return generic_binary_op(v, w, opname); |
| 1451 | } |
| 1452 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1453 | args = PyTuple_Pack(1, w); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1454 | if (args == NULL) { |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1455 | Py_DECREF(coercefunc); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1456 | return NULL; |
| 1457 | } |
| 1458 | coerced = PyEval_CallObject(coercefunc, args); |
| 1459 | Py_DECREF(args); |
| 1460 | Py_DECREF(coercefunc); |
| 1461 | if (coerced == NULL) { |
| 1462 | return NULL; |
| 1463 | } |
| 1464 | if (coerced == Py_None || coerced == Py_NotImplemented) { |
| 1465 | Py_DECREF(coerced); |
| 1466 | return generic_binary_op(v, w, opname); |
| 1467 | } |
| 1468 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
| 1469 | Py_DECREF(coerced); |
| 1470 | PyErr_SetString(PyExc_TypeError, |
| 1471 | "coercion should return None or 2-tuple"); |
| 1472 | return NULL; |
| 1473 | } |
| 1474 | v1 = PyTuple_GetItem(coerced, 0); |
| 1475 | w = PyTuple_GetItem(coerced, 1); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1476 | if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1477 | /* prevent recursion if __coerce__ returns self as the first |
| 1478 | * argument */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1479 | result = generic_binary_op(v1, w, opname); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1480 | } else { |
Brett Cannon | ea3912b | 2006-06-13 21:46:41 +0000 | [diff] [blame] | 1481 | if (Py_EnterRecursiveCall(" after coercion")) |
| 1482 | return NULL; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1483 | if (swapped) |
| 1484 | result = (thisfunc)(w, v1); |
| 1485 | else |
| 1486 | result = (thisfunc)(v1, w); |
Brett Cannon | ea3912b | 2006-06-13 21:46:41 +0000 | [diff] [blame] | 1487 | Py_LeaveRecursiveCall(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1488 | } |
| 1489 | Py_DECREF(coerced); |
| 1490 | return result; |
| 1491 | } |
| 1492 | |
| 1493 | /* Implement a binary operator involving at least one class instance. */ |
| 1494 | static PyObject * |
| 1495 | do_binop(PyObject *v, PyObject *w, char *opname, char *ropname, |
| 1496 | binaryfunc thisfunc) |
| 1497 | { |
| 1498 | PyObject *result = half_binop(v, w, opname, thisfunc, 0); |
| 1499 | if (result == Py_NotImplemented) { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1500 | Py_DECREF(result); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1501 | result = half_binop(w, v, ropname, thisfunc, 1); |
| 1502 | } |
| 1503 | return result; |
| 1504 | } |
| 1505 | |
| 1506 | static PyObject * |
| 1507 | do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname, |
| 1508 | char *ropname, binaryfunc thisfunc) |
| 1509 | { |
| 1510 | PyObject *result = half_binop(v, w, iopname, thisfunc, 0); |
| 1511 | if (result == Py_NotImplemented) { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1512 | Py_DECREF(result); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1513 | result = do_binop(v, w, opname, ropname, thisfunc); |
| 1514 | } |
| 1515 | return result; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1516 | } |
| 1517 | |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1518 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1519 | instance_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1520 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1521 | PyObject *v = *pv; |
| 1522 | PyObject *w = *pw; |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1523 | PyObject *coercefunc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1524 | PyObject *args; |
| 1525 | PyObject *coerced; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1526 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1527 | if (coerce_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1528 | coerce_obj = PyString_InternFromString("__coerce__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1529 | if (coerce_obj == NULL) |
| 1530 | return -1; |
| 1531 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1532 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1533 | if (coercefunc == NULL) { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1534 | /* No __coerce__ method */ |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1535 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1536 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1537 | PyErr_Clear(); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1538 | return 1; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1539 | } |
| 1540 | /* Has __coerce__ method: call it */ |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1541 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1542 | if (args == NULL) { |
| 1543 | return -1; |
| 1544 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1545 | coerced = PyEval_CallObject(coercefunc, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1546 | Py_DECREF(args); |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1547 | Py_DECREF(coercefunc); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1548 | if (coerced == NULL) { |
| 1549 | /* __coerce__ call raised an exception */ |
| 1550 | return -1; |
| 1551 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1552 | if (coerced == Py_None || coerced == Py_NotImplemented) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1553 | /* __coerce__ says "I can't do it" */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1554 | Py_DECREF(coerced); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1555 | return 1; |
| 1556 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1557 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1558 | /* __coerce__ return value is malformed */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1559 | Py_DECREF(coerced); |
| 1560 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1561 | "coercion should return None or 2-tuple"); |
| 1562 | return -1; |
| 1563 | } |
| 1564 | /* __coerce__ returned two new values */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1565 | *pv = PyTuple_GetItem(coerced, 0); |
| 1566 | *pw = PyTuple_GetItem(coerced, 1); |
| 1567 | Py_INCREF(*pv); |
| 1568 | Py_INCREF(*pw); |
| 1569 | Py_DECREF(coerced); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1570 | return 0; |
| 1571 | } |
| 1572 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1573 | #define UNARY(funcname, methodname) \ |
Thomas Wouters | c307352 | 2000-07-23 22:09:59 +0000 | [diff] [blame] | 1574 | static PyObject *funcname(PyInstanceObject *self) { \ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1575 | static PyObject *o; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1576 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1577 | if (o == NULL) return NULL; } \ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1578 | return generic_unary_op(self, o); \ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1581 | /* unary function with a fallback */ |
| 1582 | #define UNARY_FB(funcname, methodname, funcname_fb) \ |
| 1583 | static PyObject *funcname(PyInstanceObject *self) { \ |
| 1584 | static PyObject *o; \ |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1585 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1586 | if (o == NULL) return NULL; } \ |
| 1587 | if (PyObject_HasAttr((PyObject*)self, o)) \ |
| 1588 | return generic_unary_op(self, o); \ |
| 1589 | else \ |
| 1590 | return funcname_fb(self); \ |
| 1591 | } |
| 1592 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1593 | #define BINARY(f, m, n) \ |
| 1594 | static PyObject *f(PyObject *v, PyObject *w) { \ |
| 1595 | return do_binop(v, w, "__" m "__", "__r" m "__", n); \ |
| 1596 | } |
| 1597 | |
| 1598 | #define BINARY_INPLACE(f, m, n) \ |
| 1599 | static PyObject *f(PyObject *v, PyObject *w) { \ |
| 1600 | return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \ |
| 1601 | "__r" m "__", n); \ |
| 1602 | } |
| 1603 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1604 | UNARY(instance_neg, "__neg__") |
| 1605 | UNARY(instance_pos, "__pos__") |
| 1606 | UNARY(instance_abs, "__abs__") |
| 1607 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1608 | BINARY(instance_or, "or", PyNumber_Or) |
| 1609 | BINARY(instance_and, "and", PyNumber_And) |
| 1610 | BINARY(instance_xor, "xor", PyNumber_Xor) |
| 1611 | BINARY(instance_lshift, "lshift", PyNumber_Lshift) |
| 1612 | BINARY(instance_rshift, "rshift", PyNumber_Rshift) |
| 1613 | BINARY(instance_add, "add", PyNumber_Add) |
| 1614 | BINARY(instance_sub, "sub", PyNumber_Subtract) |
| 1615 | BINARY(instance_mul, "mul", PyNumber_Multiply) |
| 1616 | BINARY(instance_div, "div", PyNumber_Divide) |
| 1617 | BINARY(instance_mod, "mod", PyNumber_Remainder) |
| 1618 | BINARY(instance_divmod, "divmod", PyNumber_Divmod) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1619 | BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide) |
| 1620 | BINARY(instance_truediv, "truediv", PyNumber_TrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1621 | |
| 1622 | BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr) |
| 1623 | BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor) |
| 1624 | BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd) |
| 1625 | BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift) |
| 1626 | BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift) |
| 1627 | BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd) |
| 1628 | BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract) |
| 1629 | BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply) |
| 1630 | BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide) |
| 1631 | BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1632 | BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide) |
| 1633 | BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1634 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1635 | /* Try a 3-way comparison, returning an int; v is an instance. Return: |
| 1636 | -2 for an exception; |
| 1637 | -1 if v < w; |
| 1638 | 0 if v == w; |
| 1639 | 1 if v > w; |
| 1640 | 2 if this particular 3-way comparison is not implemented or undefined. |
| 1641 | */ |
| 1642 | static int |
| 1643 | half_cmp(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1644 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1645 | static PyObject *cmp_obj; |
| 1646 | PyObject *args; |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1647 | PyObject *cmp_func; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1648 | PyObject *result; |
| 1649 | long l; |
| 1650 | |
| 1651 | assert(PyInstance_Check(v)); |
| 1652 | |
| 1653 | if (cmp_obj == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1654 | cmp_obj = PyString_InternFromString("__cmp__"); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1655 | if (cmp_obj == NULL) |
| 1656 | return -2; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1657 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1658 | |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1659 | cmp_func = PyObject_GetAttr(v, cmp_obj); |
| 1660 | if (cmp_func == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1661 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1662 | return -2; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1663 | PyErr_Clear(); |
| 1664 | return 2; |
| 1665 | } |
| 1666 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1667 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1668 | if (args == NULL) { |
| 1669 | Py_DECREF(cmp_func); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1670 | return -2; |
Guido van Rossum | 617080b | 2002-10-18 14:15:33 +0000 | [diff] [blame] | 1671 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1672 | |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1673 | result = PyEval_CallObject(cmp_func, args); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1674 | Py_DECREF(args); |
Guido van Rossum | 3202c6f | 2001-01-29 23:50:25 +0000 | [diff] [blame] | 1675 | Py_DECREF(cmp_func); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1676 | |
| 1677 | if (result == NULL) |
| 1678 | return -2; |
| 1679 | |
| 1680 | if (result == Py_NotImplemented) { |
| 1681 | Py_DECREF(result); |
| 1682 | return 2; |
| 1683 | } |
| 1684 | |
| 1685 | l = PyInt_AsLong(result); |
| 1686 | Py_DECREF(result); |
| 1687 | if (l == -1 && PyErr_Occurred()) { |
| 1688 | PyErr_SetString(PyExc_TypeError, |
| 1689 | "comparison did not return an int"); |
| 1690 | return -2; |
| 1691 | } |
| 1692 | |
| 1693 | return l < 0 ? -1 : l > 0 ? 1 : 0; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1694 | } |
| 1695 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1696 | /* Try a 3-way comparison, returning an int; either v or w is an instance. |
| 1697 | We first try a coercion. Return: |
| 1698 | -2 for an exception; |
| 1699 | -1 if v < w; |
| 1700 | 0 if v == w; |
| 1701 | 1 if v > w; |
| 1702 | 2 if this particular 3-way comparison is not implemented or undefined. |
| 1703 | THIS IS ONLY CALLED FROM object.c! |
| 1704 | */ |
| 1705 | static int |
| 1706 | instance_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1707 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1708 | int c; |
| 1709 | |
| 1710 | c = PyNumber_CoerceEx(&v, &w); |
| 1711 | if (c < 0) |
| 1712 | return -2; |
| 1713 | if (c == 0) { |
| 1714 | /* If neither is now an instance, use regular comparison */ |
| 1715 | if (!PyInstance_Check(v) && !PyInstance_Check(w)) { |
| 1716 | c = PyObject_Compare(v, w); |
| 1717 | Py_DECREF(v); |
| 1718 | Py_DECREF(w); |
| 1719 | if (PyErr_Occurred()) |
| 1720 | return -2; |
| 1721 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1722 | } |
| 1723 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1724 | else { |
| 1725 | /* The coercion didn't do anything. |
| 1726 | Treat this the same as returning v and w unchanged. */ |
| 1727 | Py_INCREF(v); |
| 1728 | Py_INCREF(w); |
| 1729 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1730 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1731 | if (PyInstance_Check(v)) { |
| 1732 | c = half_cmp(v, w); |
| 1733 | if (c <= 1) { |
| 1734 | Py_DECREF(v); |
| 1735 | Py_DECREF(w); |
| 1736 | return c; |
| 1737 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1738 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1739 | if (PyInstance_Check(w)) { |
| 1740 | c = half_cmp(w, v); |
| 1741 | if (c <= 1) { |
| 1742 | Py_DECREF(v); |
| 1743 | Py_DECREF(w); |
| 1744 | if (c >= -1) |
| 1745 | c = -c; |
| 1746 | return c; |
| 1747 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1748 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1749 | Py_DECREF(v); |
| 1750 | Py_DECREF(w); |
| 1751 | return 2; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1752 | } |
| 1753 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1754 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1755 | instance_nonzero(PyInstanceObject *self) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1756 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1757 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1758 | long outcome; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1759 | static PyObject *nonzerostr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1760 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1761 | if (nonzerostr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1762 | nonzerostr = PyString_InternFromString("__nonzero__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1763 | if (nonzerostr == NULL) |
| 1764 | return -1; |
| 1765 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1766 | if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1767 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1768 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1769 | PyErr_Clear(); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1770 | if (lenstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1771 | lenstr = PyString_InternFromString("__len__"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 1772 | if (lenstr == NULL) |
| 1773 | return -1; |
| 1774 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1775 | if ((func = instance_getattr(self, lenstr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 1776 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1777 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1778 | PyErr_Clear(); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1779 | /* Fall back to the default behavior: |
| 1780 | all instances are nonzero */ |
| 1781 | return 1; |
| 1782 | } |
| 1783 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1784 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1785 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1786 | if (res == NULL) |
| 1787 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1788 | if (!PyInt_Check(res)) { |
| 1789 | Py_DECREF(res); |
| 1790 | PyErr_SetString(PyExc_TypeError, |
| 1791 | "__nonzero__ should return an int"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1792 | return -1; |
| 1793 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1794 | outcome = PyInt_AsLong(res); |
| 1795 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1796 | if (outcome < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1797 | PyErr_SetString(PyExc_ValueError, |
| 1798 | "__nonzero__ should return >= 0"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1799 | return -1; |
| 1800 | } |
| 1801 | return outcome > 0; |
| 1802 | } |
| 1803 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1804 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1805 | instance_index(PyInstanceObject *self) |
| 1806 | { |
| 1807 | PyObject *func, *res; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1808 | static PyObject *indexstr = NULL; |
| 1809 | |
| 1810 | if (indexstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1811 | indexstr = PyString_InternFromString("__index__"); |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1812 | if (indexstr == NULL) |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1813 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1814 | } |
| 1815 | if ((func = instance_getattr(self, indexstr)) == NULL) { |
| 1816 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1817 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1818 | PyErr_Clear(); |
| 1819 | PyErr_SetString(PyExc_TypeError, |
| 1820 | "object cannot be interpreted as an index"); |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1821 | return NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1822 | } |
| 1823 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1824 | Py_DECREF(func); |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1825 | return res; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1826 | } |
| 1827 | |
| 1828 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1829 | UNARY(instance_invert, "__invert__") |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1830 | UNARY(_instance_trunc, "__trunc__") |
| 1831 | |
| 1832 | static PyObject * |
| 1833 | instance_int(PyInstanceObject *self) |
| 1834 | { |
| 1835 | PyObject *truncated; |
| 1836 | static PyObject *int_name; |
| 1837 | if (int_name == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1838 | int_name = PyString_InternFromString("__int__"); |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1839 | if (int_name == NULL) |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | if (PyObject_HasAttr((PyObject*)self, int_name)) |
| 1843 | return generic_unary_op(self, int_name); |
| 1844 | |
| 1845 | truncated = _instance_trunc(self); |
| 1846 | /* __trunc__ is specified to return an Integral type, but |
| 1847 | int() needs to return an int. */ |
| 1848 | return _PyNumber_ConvertIntegralToInt( |
| 1849 | truncated, |
| 1850 | "__trunc__ returned non-Integral (type %.200s)"); |
| 1851 | } |
| 1852 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1853 | UNARY_FB(instance_long, "__long__", instance_int) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1854 | UNARY(instance_float, "__float__") |
| 1855 | UNARY(instance_oct, "__oct__") |
| 1856 | UNARY(instance_hex, "__hex__") |
| 1857 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1858 | static PyObject * |
| 1859 | bin_power(PyObject *v, PyObject *w) |
| 1860 | { |
| 1861 | return PyNumber_Power(v, w, Py_None); |
| 1862 | } |
| 1863 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1864 | /* This version is for ternary calls only (z != None) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1865 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1866 | instance_pow(PyObject *v, PyObject *w, PyObject *z) |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1867 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1868 | if (z == Py_None) { |
| 1869 | return do_binop(v, w, "__pow__", "__rpow__", bin_power); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1870 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1871 | else { |
| 1872 | PyObject *func; |
| 1873 | PyObject *args; |
| 1874 | PyObject *result; |
| 1875 | |
| 1876 | /* XXX Doesn't do coercions... */ |
| 1877 | func = PyObject_GetAttrString(v, "__pow__"); |
| 1878 | if (func == NULL) |
| 1879 | return NULL; |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1880 | args = PyTuple_Pack(2, w, z); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1881 | if (args == NULL) { |
| 1882 | Py_DECREF(func); |
| 1883 | return NULL; |
| 1884 | } |
| 1885 | result = PyEval_CallObject(func, args); |
| 1886 | Py_DECREF(func); |
| 1887 | Py_DECREF(args); |
| 1888 | return result; |
| 1889 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1890 | } |
| 1891 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1892 | static PyObject * |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1893 | bin_inplace_power(PyObject *v, PyObject *w) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1894 | { |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1895 | return PyNumber_InPlacePower(v, w, Py_None); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1896 | } |
| 1897 | |
| 1898 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1899 | static PyObject * |
| 1900 | instance_ipow(PyObject *v, PyObject *w, PyObject *z) |
| 1901 | { |
| 1902 | if (z == Py_None) { |
| 1903 | return do_binop_inplace(v, w, "__ipow__", "__pow__", |
| 1904 | "__rpow__", bin_inplace_power); |
| 1905 | } |
| 1906 | else { |
| 1907 | /* XXX Doesn't do coercions... */ |
| 1908 | PyObject *func; |
| 1909 | PyObject *args; |
| 1910 | PyObject *result; |
| 1911 | |
| 1912 | func = PyObject_GetAttrString(v, "__ipow__"); |
| 1913 | if (func == NULL) { |
| 1914 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1915 | return NULL; |
| 1916 | PyErr_Clear(); |
| 1917 | return instance_pow(v, w, z); |
| 1918 | } |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1919 | args = PyTuple_Pack(2, w, z); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1920 | if (args == NULL) { |
| 1921 | Py_DECREF(func); |
| 1922 | return NULL; |
| 1923 | } |
| 1924 | result = PyEval_CallObject(func, args); |
| 1925 | Py_DECREF(func); |
| 1926 | Py_DECREF(args); |
| 1927 | return result; |
| 1928 | } |
| 1929 | } |
| 1930 | |
| 1931 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1932 | /* Map rich comparison operators to their __xx__ namesakes */ |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1933 | #define NAME_OPS 6 |
| 1934 | static PyObject **name_op = NULL; |
| 1935 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1936 | static int |
Guido van Rossum | 0ba9e3a | 2001-05-22 02:33:08 +0000 | [diff] [blame] | 1937 | init_name_op(void) |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1938 | { |
| 1939 | int i; |
| 1940 | char *_name_op[] = { |
| 1941 | "__lt__", |
| 1942 | "__le__", |
| 1943 | "__eq__", |
| 1944 | "__ne__", |
| 1945 | "__gt__", |
| 1946 | "__ge__", |
| 1947 | }; |
| 1948 | |
| 1949 | name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS); |
| 1950 | if (name_op == NULL) |
| 1951 | return -1; |
| 1952 | for (i = 0; i < NAME_OPS; ++i) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 1953 | name_op[i] = PyString_InternFromString(_name_op[i]); |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1954 | if (name_op[i] == NULL) |
| 1955 | return -1; |
| 1956 | } |
| 1957 | return 0; |
| 1958 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1959 | |
| 1960 | static PyObject * |
| 1961 | half_richcompare(PyObject *v, PyObject *w, int op) |
| 1962 | { |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1963 | PyObject *method; |
| 1964 | PyObject *args; |
| 1965 | PyObject *res; |
| 1966 | |
| 1967 | assert(PyInstance_Check(v)); |
| 1968 | |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1969 | if (name_op == NULL) { |
| 1970 | if (init_name_op() < 0) |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1971 | return NULL; |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1972 | } |
| 1973 | /* If the instance doesn't define an __getattr__ method, use |
| 1974 | instance_getattr2 directly because it will not set an |
| 1975 | exception on failure. */ |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1976 | if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1977 | method = instance_getattr2((PyInstanceObject *)v, |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1978 | name_op[op]); |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1979 | else |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1980 | method = PyObject_GetAttr(v, name_op[op]); |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1981 | if (method == NULL) { |
| 1982 | if (PyErr_Occurred()) { |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1983 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1984 | return NULL; |
| 1985 | PyErr_Clear(); |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1986 | } |
Guido van Rossum | f740bdf | 2002-10-29 18:36:40 +0000 | [diff] [blame] | 1987 | res = Py_NotImplemented; |
| 1988 | Py_INCREF(res); |
| 1989 | return res; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Raymond Hettinger | 8ae4689 | 2003-10-12 19:09:37 +0000 | [diff] [blame] | 1992 | args = PyTuple_Pack(1, w); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1993 | if (args == NULL) { |
| 1994 | Py_DECREF(method); |
| 1995 | return NULL; |
| 1996 | } |
| 1997 | |
| 1998 | res = PyEval_CallObject(method, args); |
| 1999 | Py_DECREF(args); |
| 2000 | Py_DECREF(method); |
| 2001 | |
| 2002 | return res; |
| 2003 | } |
| 2004 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2005 | static PyObject * |
| 2006 | instance_richcompare(PyObject *v, PyObject *w, int op) |
| 2007 | { |
| 2008 | PyObject *res; |
| 2009 | |
| 2010 | if (PyInstance_Check(v)) { |
| 2011 | res = half_richcompare(v, w, op); |
| 2012 | if (res != Py_NotImplemented) |
| 2013 | return res; |
| 2014 | Py_DECREF(res); |
| 2015 | } |
| 2016 | |
| 2017 | if (PyInstance_Check(w)) { |
Tim Peters | f4aca75 | 2004-09-23 02:39:37 +0000 | [diff] [blame] | 2018 | res = half_richcompare(w, v, _Py_SwappedOp[op]); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2019 | if (res != Py_NotImplemented) |
| 2020 | return res; |
| 2021 | Py_DECREF(res); |
| 2022 | } |
| 2023 | |
| 2024 | Py_INCREF(Py_NotImplemented); |
| 2025 | return Py_NotImplemented; |
| 2026 | } |
| 2027 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 2028 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2029 | /* Get the iterator */ |
| 2030 | static PyObject * |
| 2031 | instance_getiter(PyInstanceObject *self) |
| 2032 | { |
| 2033 | PyObject *func; |
| 2034 | |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2035 | if (iterstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2036 | iterstr = PyString_InternFromString("__iter__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2037 | if (iterstr == NULL) |
| 2038 | return NULL; |
| 2039 | } |
| 2040 | if (getitemstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2041 | getitemstr = PyString_InternFromString("__getitem__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2042 | if (getitemstr == NULL) |
| 2043 | return NULL; |
| 2044 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2045 | |
| 2046 | if ((func = instance_getattr(self, iterstr)) != NULL) { |
| 2047 | PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
| 2048 | Py_DECREF(func); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2049 | if (res != NULL && !PyIter_Check(res)) { |
| 2050 | PyErr_Format(PyExc_TypeError, |
| 2051 | "__iter__ returned non-iterator " |
| 2052 | "of type '%.100s'", |
| 2053 | res->ob_type->tp_name); |
| 2054 | Py_DECREF(res); |
| 2055 | res = NULL; |
| 2056 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2057 | return res; |
| 2058 | } |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2059 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2060 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2061 | PyErr_Clear(); |
| 2062 | if ((func = instance_getattr(self, getitemstr)) == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2063 | PyErr_SetString(PyExc_TypeError, |
| 2064 | "iteration over non-sequence"); |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2065 | return NULL; |
| 2066 | } |
| 2067 | Py_DECREF(func); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2068 | return PySeqIter_New((PyObject *)self); |
| 2069 | } |
| 2070 | |
| 2071 | |
| 2072 | /* Call the iterator's next */ |
| 2073 | static PyObject * |
| 2074 | instance_iternext(PyInstanceObject *self) |
| 2075 | { |
| 2076 | PyObject *func; |
| 2077 | |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 2078 | if (nextstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2079 | nextstr = PyString_InternFromString("next"); |
Neal Norwitz | b09f4f5 | 2006-08-13 18:10:28 +0000 | [diff] [blame] | 2080 | if (nextstr == NULL) |
| 2081 | return NULL; |
| 2082 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2083 | |
| 2084 | if ((func = instance_getattr(self, nextstr)) != NULL) { |
| 2085 | PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
| 2086 | Py_DECREF(func); |
| 2087 | if (res != NULL) { |
| 2088 | return res; |
| 2089 | } |
| 2090 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 2091 | PyErr_Clear(); |
| 2092 | return NULL; |
| 2093 | } |
| 2094 | return NULL; |
| 2095 | } |
| 2096 | PyErr_SetString(PyExc_TypeError, "instance has no next() method"); |
| 2097 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2098 | } |
| 2099 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2100 | static PyObject * |
| 2101 | instance_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2102 | { |
| 2103 | PyObject *res, *call = PyObject_GetAttrString(func, "__call__"); |
| 2104 | if (call == NULL) { |
| 2105 | PyInstanceObject *inst = (PyInstanceObject*) func; |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2106 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2107 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2108 | PyErr_Clear(); |
| 2109 | PyErr_Format(PyExc_AttributeError, |
| 2110 | "%.200s instance has no __call__ method", |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2111 | PyString_AsString(inst->in_class->cl_name)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2112 | return NULL; |
| 2113 | } |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2114 | /* We must check and increment the recursion depth here. Scenario: |
| 2115 | class A: |
| 2116 | pass |
| 2117 | A.__call__ = A() # that's right |
| 2118 | a = A() # ok |
| 2119 | a() # infinite recursion |
| 2120 | This bounces between instance_call() and PyObject_Call() without |
| 2121 | ever hitting eval_frame() (which has the main recursion check). */ |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2122 | if (Py_EnterRecursiveCall(" in __call__")) { |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2123 | res = NULL; |
| 2124 | } |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2125 | else { |
Guido van Rossum | 16b93b3 | 2002-06-13 21:32:51 +0000 | [diff] [blame] | 2126 | res = PyObject_Call(call, arg, kw); |
Armin Rigo | 2b3eb40 | 2003-10-28 12:05:48 +0000 | [diff] [blame] | 2127 | Py_LeaveRecursiveCall(); |
| 2128 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2129 | Py_DECREF(call); |
| 2130 | return res; |
| 2131 | } |
| 2132 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2133 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2134 | static PyNumberMethods instance_as_number = { |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2135 | instance_add, /* nb_add */ |
| 2136 | instance_sub, /* nb_subtract */ |
| 2137 | instance_mul, /* nb_multiply */ |
| 2138 | instance_div, /* nb_divide */ |
| 2139 | instance_mod, /* nb_remainder */ |
| 2140 | instance_divmod, /* nb_divmod */ |
| 2141 | instance_pow, /* nb_power */ |
| 2142 | (unaryfunc)instance_neg, /* nb_negative */ |
| 2143 | (unaryfunc)instance_pos, /* nb_positive */ |
| 2144 | (unaryfunc)instance_abs, /* nb_absolute */ |
| 2145 | (inquiry)instance_nonzero, /* nb_nonzero */ |
| 2146 | (unaryfunc)instance_invert, /* nb_invert */ |
| 2147 | instance_lshift, /* nb_lshift */ |
| 2148 | instance_rshift, /* nb_rshift */ |
| 2149 | instance_and, /* nb_and */ |
| 2150 | instance_xor, /* nb_xor */ |
| 2151 | instance_or, /* nb_or */ |
| 2152 | instance_coerce, /* nb_coerce */ |
| 2153 | (unaryfunc)instance_int, /* nb_int */ |
| 2154 | (unaryfunc)instance_long, /* nb_long */ |
| 2155 | (unaryfunc)instance_float, /* nb_float */ |
| 2156 | (unaryfunc)instance_oct, /* nb_oct */ |
| 2157 | (unaryfunc)instance_hex, /* nb_hex */ |
| 2158 | instance_iadd, /* nb_inplace_add */ |
| 2159 | instance_isub, /* nb_inplace_subtract */ |
| 2160 | instance_imul, /* nb_inplace_multiply */ |
| 2161 | instance_idiv, /* nb_inplace_divide */ |
| 2162 | instance_imod, /* nb_inplace_remainder */ |
| 2163 | instance_ipow, /* nb_inplace_power */ |
| 2164 | instance_ilshift, /* nb_inplace_lshift */ |
| 2165 | instance_irshift, /* nb_inplace_rshift */ |
| 2166 | instance_iand, /* nb_inplace_and */ |
| 2167 | instance_ixor, /* nb_inplace_xor */ |
| 2168 | instance_ior, /* nb_inplace_or */ |
| 2169 | instance_floordiv, /* nb_floor_divide */ |
| 2170 | instance_truediv, /* nb_true_divide */ |
| 2171 | instance_ifloordiv, /* nb_inplace_floor_divide */ |
| 2172 | instance_itruediv, /* nb_inplace_true_divide */ |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 2173 | (unaryfunc)instance_index, /* nb_index */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2174 | }; |
| 2175 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2176 | PyTypeObject PyInstance_Type = { |
| 2177 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2178 | 0, |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 2179 | "instance", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2180 | sizeof(PyInstanceObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2181 | 0, |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2182 | (destructor)instance_dealloc, /* tp_dealloc */ |
| 2183 | 0, /* tp_print */ |
| 2184 | 0, /* tp_getattr */ |
| 2185 | 0, /* tp_setattr */ |
| 2186 | instance_compare, /* tp_compare */ |
| 2187 | (reprfunc)instance_repr, /* tp_repr */ |
| 2188 | &instance_as_number, /* tp_as_number */ |
| 2189 | &instance_as_sequence, /* tp_as_sequence */ |
| 2190 | &instance_as_mapping, /* tp_as_mapping */ |
| 2191 | (hashfunc)instance_hash, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2192 | instance_call, /* tp_call */ |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 2193 | (reprfunc)instance_str, /* tp_str */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2194 | (getattrofunc)instance_getattr, /* tp_getattro */ |
| 2195 | (setattrofunc)instance_setattr, /* tp_setattro */ |
| 2196 | 0, /* tp_as_buffer */ |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2197 | 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] | 2198 | instance_doc, /* tp_doc */ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2199 | (traverseproc)instance_traverse, /* tp_traverse */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2200 | 0, /* tp_clear */ |
| 2201 | instance_richcompare, /* tp_richcompare */ |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2202 | offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ |
| 2203 | (getiterfunc)instance_getiter, /* tp_iter */ |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2204 | (iternextfunc)instance_iternext, /* tp_iternext */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2205 | 0, /* tp_methods */ |
| 2206 | 0, /* tp_members */ |
| 2207 | 0, /* tp_getset */ |
| 2208 | 0, /* tp_base */ |
| 2209 | 0, /* tp_dict */ |
| 2210 | 0, /* tp_descr_get */ |
| 2211 | 0, /* tp_descr_set */ |
| 2212 | 0, /* tp_dictoffset */ |
| 2213 | 0, /* tp_init */ |
| 2214 | 0, /* tp_alloc */ |
| 2215 | instance_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2216 | }; |
| 2217 | |
| 2218 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2219 | /* Instance method objects are used for two purposes: |
| 2220 | (a) as bound instance methods (returned by instancename.methodname) |
| 2221 | (b) as unbound methods (returned by ClassName.methodname) |
| 2222 | In case (b), im_self is NULL |
| 2223 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2224 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2225 | PyObject * |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2226 | PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2227 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2228 | register PyMethodObject *im; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2229 | im = free_list; |
| 2230 | if (im != NULL) { |
| 2231 | free_list = (PyMethodObject *)(im->im_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2232 | PyObject_INIT(im, &PyMethod_Type); |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2233 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2234 | } |
| 2235 | else { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2236 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2237 | if (im == NULL) |
| 2238 | return NULL; |
| 2239 | } |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 2240 | im->im_weakreflist = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2241 | Py_INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 2242 | im->im_func = func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2243 | Py_XINCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 2244 | im->im_self = self; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2245 | Py_XINCREF(klass); |
| 2246 | im->im_class = klass; |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2247 | _PyObject_GC_TRACK(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2248 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2249 | } |
| 2250 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2251 | /* Descriptors for PyMethod attributes */ |
| 2252 | |
| 2253 | /* 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] | 2254 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2255 | #define OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2256 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2257 | static PyMemberDef instancemethod_memberlist[] = { |
| 2258 | {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, |
| 2259 | "the class associated with a method"}, |
| 2260 | {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 2261 | "the function (or other callable) implementing a method"}, |
Neal Norwitz | b1d3d96 | 2008-03-18 04:46:00 +0000 | [diff] [blame] | 2262 | {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 2263 | "the function (or other callable) implementing a method"}, |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2264 | {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2265 | "the instance to which a method is bound; None for unbound methods"}, |
Neal Norwitz | b1d3d96 | 2008-03-18 04:46:00 +0000 | [diff] [blame] | 2266 | {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2267 | "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] | 2268 | {NULL} /* Sentinel */ |
| 2269 | }; |
| 2270 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2271 | /* Christian Tismer argued convincingly that method attributes should |
| 2272 | (nearly) always override function attributes. |
| 2273 | The one exception is __doc__; there's a default __doc__ which |
| 2274 | should only be used for the class, not for instances */ |
| 2275 | |
| 2276 | static PyObject * |
| 2277 | instancemethod_get_doc(PyMethodObject *im, void *context) |
| 2278 | { |
| 2279 | static PyObject *docstr; |
| 2280 | if (docstr == NULL) { |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2281 | docstr= PyString_InternFromString("__doc__"); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2282 | if (docstr == NULL) |
| 2283 | return NULL; |
| 2284 | } |
| 2285 | return PyObject_GetAttr(im->im_func, docstr); |
| 2286 | } |
| 2287 | |
| 2288 | static PyGetSetDef instancemethod_getset[] = { |
| 2289 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 2290 | {0} |
| 2291 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2292 | |
| 2293 | static PyObject * |
| 2294 | instancemethod_getattro(PyObject *obj, PyObject *name) |
| 2295 | { |
| 2296 | PyMethodObject *im = (PyMethodObject *)obj; |
| 2297 | PyTypeObject *tp = obj->ob_type; |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2298 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2299 | |
Guido van Rossum | 915f0eb | 2001-10-17 20:26:38 +0000 | [diff] [blame] | 2300 | if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) { |
| 2301 | if (tp->tp_dict == NULL) { |
| 2302 | if (PyType_Ready(tp) < 0) |
| 2303 | return NULL; |
| 2304 | } |
| 2305 | descr = _PyType_Lookup(tp, name); |
Barry Warsaw | d6a9e84 | 2001-01-15 20:40:19 +0000 | [diff] [blame] | 2306 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2307 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2308 | if (descr != NULL) { |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2309 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 2310 | if (f != NULL) |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2311 | return f(descr, obj, (PyObject *)obj->ob_type); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2312 | else { |
| 2313 | Py_INCREF(descr); |
| 2314 | return descr; |
| 2315 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2316 | } |
| 2317 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2318 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2319 | } |
| 2320 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2321 | PyDoc_STRVAR(instancemethod_doc, |
| 2322 | "instancemethod(function, instance, class)\n\ |
| 2323 | \n\ |
| 2324 | Create an instance method object."); |
| 2325 | |
| 2326 | static PyObject * |
| 2327 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 2328 | { |
| 2329 | PyObject *func; |
| 2330 | PyObject *self; |
Guido van Rossum | 2fb9fdc | 2003-04-09 19:35:08 +0000 | [diff] [blame] | 2331 | PyObject *classObj = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2332 | |
Georg Brandl | 5d59c09 | 2006-09-30 08:43:30 +0000 | [diff] [blame] | 2333 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 2334 | return NULL; |
Guido van Rossum | 2fb9fdc | 2003-04-09 19:35:08 +0000 | [diff] [blame] | 2335 | if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2336 | &func, &self, &classObj)) |
| 2337 | return NULL; |
| 2338 | if (!PyCallable_Check(func)) { |
| 2339 | PyErr_SetString(PyExc_TypeError, |
| 2340 | "first argument must be callable"); |
| 2341 | return NULL; |
| 2342 | } |
| 2343 | if (self == Py_None) |
| 2344 | self = NULL; |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 2345 | if (self == NULL && classObj == NULL) { |
| 2346 | PyErr_SetString(PyExc_TypeError, |
| 2347 | "unbound methods must have non-NULL im_class"); |
| 2348 | return NULL; |
| 2349 | } |
| 2350 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2351 | return PyMethod_New(func, self, classObj); |
| 2352 | } |
| 2353 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2354 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2355 | instancemethod_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2356 | { |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2357 | _PyObject_GC_UNTRACK(im); |
Fred Drake | c916f5a | 2001-10-26 17:56:51 +0000 | [diff] [blame] | 2358 | if (im->im_weakreflist != NULL) |
| 2359 | PyObject_ClearWeakRefs((PyObject *)im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2360 | Py_DECREF(im->im_func); |
| 2361 | Py_XDECREF(im->im_self); |
Guido van Rossum | cdf0d75 | 2001-08-17 12:07:34 +0000 | [diff] [blame] | 2362 | Py_XDECREF(im->im_class); |
Christian Heimes | 5b970ad | 2008-02-06 13:33:44 +0000 | [diff] [blame] | 2363 | if (numfree < PyMethod_MAXFREELIST) { |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2364 | im->im_self = (PyObject *)free_list; |
| 2365 | free_list = im; |
| 2366 | numfree++; |
| 2367 | } |
| 2368 | else { |
| 2369 | PyObject_GC_Del(im); |
| 2370 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2371 | } |
| 2372 | |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2373 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2374 | instancemethod_compare(PyMethodObject *a, PyMethodObject *b) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2375 | { |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2376 | int cmp; |
| 2377 | cmp = PyObject_Compare(a->im_func, b->im_func); |
| 2378 | if (cmp) |
| 2379 | return cmp; |
| 2380 | |
| 2381 | if (a->im_self == b->im_self) |
| 2382 | return 0; |
| 2383 | if (a->im_self == NULL || b->im_self == NULL) |
Guido van Rossum | e9df727 | 1995-04-06 14:46:51 +0000 | [diff] [blame] | 2384 | return (a->im_self < b->im_self) ? -1 : 1; |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2385 | else |
| 2386 | return PyObject_Compare(a->im_self, b->im_self); |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2389 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2390 | instancemethod_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2391 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2392 | PyObject *self = a->im_self; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2393 | PyObject *func = a->im_func; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2394 | PyObject *klass = a->im_class; |
| 2395 | PyObject *funcname = NULL, *klassname = NULL, *result = NULL; |
| 2396 | char *sfuncname = "?", *sklassname = "?"; |
| 2397 | |
| 2398 | funcname = PyObject_GetAttrString(func, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2399 | if (funcname == NULL) { |
| 2400 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2401 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2402 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2403 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2404 | else if (!PyString_Check(funcname)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2405 | Py_DECREF(funcname); |
| 2406 | funcname = NULL; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2407 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2408 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2409 | sfuncname = PyString_AS_STRING(funcname); |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2410 | if (klass == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2411 | klassname = NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2412 | else { |
| 2413 | klassname = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2414 | if (klassname == NULL) { |
| 2415 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2416 | return NULL; |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2417 | PyErr_Clear(); |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2418 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2419 | else if (!PyString_Check(klassname)) { |
Guido van Rossum | 4066769 | 2001-08-17 13:59:27 +0000 | [diff] [blame] | 2420 | Py_DECREF(klassname); |
| 2421 | klassname = NULL; |
| 2422 | } |
| 2423 | else |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2424 | sklassname = PyString_AS_STRING(klassname); |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 2425 | } |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2426 | if (self == NULL) |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2427 | result = PyString_FromFormat("<unbound method %s.%s>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2428 | sklassname, sfuncname); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2429 | else { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2430 | /* XXX Shouldn't use repr() here! */ |
| 2431 | PyObject *selfrepr = PyObject_Repr(self); |
| 2432 | if (selfrepr == NULL) |
| 2433 | goto fail; |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2434 | if (!PyString_Check(selfrepr)) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2435 | Py_DECREF(selfrepr); |
| 2436 | goto fail; |
| 2437 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2438 | result = PyString_FromFormat("<bound method %s.%s of %s>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2439 | sklassname, sfuncname, |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2440 | PyString_AS_STRING(selfrepr)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2441 | Py_DECREF(selfrepr); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2442 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2443 | fail: |
Guido van Rossum | 42636dc | 1999-10-11 14:03:12 +0000 | [diff] [blame] | 2444 | Py_XDECREF(funcname); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2445 | Py_XDECREF(klassname); |
| 2446 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2447 | } |
| 2448 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2449 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2450 | instancemethod_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2451 | { |
| 2452 | long x, y; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2453 | if (a->im_self == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2454 | x = PyObject_Hash(Py_None); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2455 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2456 | x = PyObject_Hash(a->im_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2457 | if (x == -1) |
| 2458 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2459 | y = PyObject_Hash(a->im_func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2460 | if (y == -1) |
| 2461 | return -1; |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2462 | x = x ^ y; |
| 2463 | if (x == -1) |
| 2464 | x = -2; |
| 2465 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2466 | } |
| 2467 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2468 | static int |
| 2469 | instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) |
| 2470 | { |
Thomas Wouters | c6e5506 | 2006-04-15 21:47:09 +0000 | [diff] [blame] | 2471 | Py_VISIT(im->im_func); |
| 2472 | Py_VISIT(im->im_self); |
| 2473 | Py_VISIT(im->im_class); |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 2474 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2477 | static void |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2478 | getclassname(PyObject *klass, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2479 | { |
| 2480 | PyObject *name; |
| 2481 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2482 | assert(bufsize > 1); |
| 2483 | strcpy(buf, "?"); /* Default outcome */ |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2484 | if (klass == NULL) |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2485 | return; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2486 | name = PyObject_GetAttrString(klass, "__name__"); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2487 | if (name == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2488 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2489 | PyErr_Clear(); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2490 | return; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2491 | } |
Gregory P. Smith | dd96db6 | 2008-06-09 04:58:54 +0000 | [diff] [blame] | 2492 | if (PyString_Check(name)) { |
| 2493 | strncpy(buf, PyString_AS_STRING(name), bufsize); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2494 | buf[bufsize-1] = '\0'; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2495 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2496 | Py_DECREF(name); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2499 | static void |
| 2500 | getinstclassname(PyObject *inst, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2501 | { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2502 | PyObject *klass; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2503 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2504 | if (inst == NULL) { |
Tim Peters | 75585d4 | 2002-08-20 14:31:35 +0000 | [diff] [blame] | 2505 | assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2506 | strcpy(buf, "nothing"); |
| 2507 | return; |
| 2508 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2509 | |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2510 | klass = PyObject_GetAttrString(inst, "__class__"); |
| 2511 | if (klass == NULL) { |
Guido van Rossum | e7b8ecf | 2002-06-13 21:42:04 +0000 | [diff] [blame] | 2512 | /* This function cannot return an exception */ |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2513 | PyErr_Clear(); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2514 | klass = (PyObject *)(inst->ob_type); |
| 2515 | Py_INCREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2516 | } |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2517 | getclassname(klass, buf, bufsize); |
| 2518 | Py_XDECREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2521 | static PyObject * |
| 2522 | instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2523 | { |
| 2524 | PyObject *self = PyMethod_GET_SELF(func); |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2525 | PyObject *klass = PyMethod_GET_CLASS(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2526 | PyObject *result; |
| 2527 | |
| 2528 | func = PyMethod_GET_FUNCTION(func); |
| 2529 | if (self == NULL) { |
| 2530 | /* Unbound methods must be called with an instance of |
| 2531 | the class (or a derived class) as first argument */ |
| 2532 | int ok; |
| 2533 | if (PyTuple_Size(arg) >= 1) |
| 2534 | self = PyTuple_GET_ITEM(arg, 0); |
| 2535 | if (self == NULL) |
| 2536 | ok = 0; |
| 2537 | else { |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2538 | ok = PyObject_IsInstance(self, klass); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2539 | if (ok < 0) |
| 2540 | return NULL; |
| 2541 | } |
| 2542 | if (!ok) { |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2543 | char clsbuf[256]; |
| 2544 | char instbuf[256]; |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2545 | getclassname(klass, clsbuf, sizeof(clsbuf)); |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2546 | getinstclassname(self, instbuf, sizeof(instbuf)); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2547 | PyErr_Format(PyExc_TypeError, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2548 | "unbound method %s%s must be called with " |
| 2549 | "%s instance as first argument " |
| 2550 | "(got %s%s instead)", |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2551 | PyEval_GetFuncName(func), |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2552 | PyEval_GetFuncDesc(func), |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2553 | clsbuf, |
| 2554 | instbuf, |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2555 | self == NULL ? "" : " instance"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2556 | return NULL; |
| 2557 | } |
| 2558 | Py_INCREF(arg); |
| 2559 | } |
| 2560 | else { |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 2561 | Py_ssize_t argcount = PyTuple_Size(arg); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2562 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 2563 | int i; |
| 2564 | if (newarg == NULL) |
| 2565 | return NULL; |
| 2566 | Py_INCREF(self); |
| 2567 | PyTuple_SET_ITEM(newarg, 0, self); |
| 2568 | for (i = 0; i < argcount; i++) { |
| 2569 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 2570 | Py_XINCREF(v); |
| 2571 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 2572 | } |
| 2573 | arg = newarg; |
| 2574 | } |
| 2575 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 2576 | Py_DECREF(arg); |
| 2577 | return result; |
| 2578 | } |
| 2579 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2580 | static PyObject * |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2581 | instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2582 | { |
Guido van Rossum | f23c41d | 2001-08-17 13:43:27 +0000 | [diff] [blame] | 2583 | /* Don't rebind an already bound method, or an unbound method |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2584 | of a class that's not a base class of cls. */ |
| 2585 | |
| 2586 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 2587 | /* Already bound */ |
Guido van Rossum | 501c7c7 | 2001-08-16 20:41:56 +0000 | [diff] [blame] | 2588 | Py_INCREF(meth); |
| 2589 | return meth; |
| 2590 | } |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2591 | /* No, it is an unbound method */ |
| 2592 | if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) { |
| 2593 | /* Do subclass test. If it fails, return meth unchanged. */ |
| 2594 | int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth)); |
| 2595 | if (ok < 0) |
| 2596 | return NULL; |
| 2597 | if (!ok) { |
| 2598 | Py_INCREF(meth); |
| 2599 | return meth; |
| 2600 | } |
| 2601 | } |
| 2602 | /* Bind it to obj */ |
| 2603 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2604 | } |
| 2605 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2606 | PyTypeObject PyMethod_Type = { |
| 2607 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2608 | 0, |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2609 | "instancemethod", |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2610 | sizeof(PyMethodObject), |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2611 | 0, |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2612 | (destructor)instancemethod_dealloc, /* tp_dealloc */ |
| 2613 | 0, /* tp_print */ |
| 2614 | 0, /* tp_getattr */ |
| 2615 | 0, /* tp_setattr */ |
| 2616 | (cmpfunc)instancemethod_compare, /* tp_compare */ |
| 2617 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 2618 | 0, /* tp_as_number */ |
| 2619 | 0, /* tp_as_sequence */ |
| 2620 | 0, /* tp_as_mapping */ |
| 2621 | (hashfunc)instancemethod_hash, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2622 | instancemethod_call, /* tp_call */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2623 | 0, /* tp_str */ |
Georg Brandl | 347b300 | 2006-03-30 11:57:00 +0000 | [diff] [blame] | 2624 | instancemethod_getattro, /* tp_getattro */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2625 | PyObject_GenericSetAttr, /* tp_setattro */ |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2626 | 0, /* tp_as_buffer */ |
Raymond Hettinger | bff60ae | 2005-06-19 08:42:20 +0000 | [diff] [blame] | 2627 | 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] | 2628 | instancemethod_doc, /* tp_doc */ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2629 | (traverseproc)instancemethod_traverse, /* tp_traverse */ |
Fred Drake | db81e8d | 2001-03-23 04:19:27 +0000 | [diff] [blame] | 2630 | 0, /* tp_clear */ |
| 2631 | 0, /* tp_richcompare */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2632 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 2633 | 0, /* tp_iter */ |
| 2634 | 0, /* tp_iternext */ |
| 2635 | 0, /* tp_methods */ |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2636 | instancemethod_memberlist, /* tp_members */ |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2637 | instancemethod_getset, /* tp_getset */ |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2638 | 0, /* tp_base */ |
| 2639 | 0, /* tp_dict */ |
| 2640 | instancemethod_descr_get, /* tp_descr_get */ |
| 2641 | 0, /* tp_descr_set */ |
| 2642 | 0, /* tp_dictoffset */ |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2643 | 0, /* tp_init */ |
| 2644 | 0, /* tp_alloc */ |
| 2645 | instancemethod_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2646 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2647 | |
| 2648 | /* Clear out the free list */ |
| 2649 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2650 | int |
| 2651 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2652 | { |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2653 | int freelist_size = numfree; |
| 2654 | |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2655 | while (free_list) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 2656 | PyMethodObject *im = free_list; |
| 2657 | free_list = (PyMethodObject *)(im->im_self); |
Neil Schemenauer | e83c00e | 2001-08-29 23:54:21 +0000 | [diff] [blame] | 2658 | PyObject_GC_Del(im); |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2659 | numfree--; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2660 | } |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 2661 | assert(numfree == 0); |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2662 | return freelist_size; |
| 2663 | } |
| 2664 | |
| 2665 | void |
| 2666 | PyMethod_Fini(void) |
| 2667 | { |
| 2668 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2669 | } |