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 | |
Raymond Hettinger | 9376c8b | 2014-08-01 23:51:51 -0700 | [diff] [blame] | 7 | /* Free list for method objects to save malloc/free overhead |
Christian Heimes | 6075a82 | 2008-02-06 12:44:34 +0000 | [diff] [blame] | 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 *, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 21 | PyClassObject **); |
Tim Peters | dbd9ba6 | 2000-07-09 03:09:57 +0000 | [diff] [blame] | 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 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 32 | PyClassObject *op, *dummy; |
| 33 | static PyObject *docstr, *modstr, *namestr; |
| 34 | if (docstr == NULL) { |
| 35 | docstr= PyString_InternFromString("__doc__"); |
| 36 | if (docstr == NULL) |
| 37 | return NULL; |
| 38 | } |
| 39 | if (modstr == NULL) { |
| 40 | modstr= PyString_InternFromString("__module__"); |
| 41 | if (modstr == NULL) |
| 42 | return NULL; |
| 43 | } |
| 44 | if (namestr == NULL) { |
| 45 | namestr= PyString_InternFromString("__name__"); |
| 46 | if (namestr == NULL) |
| 47 | return NULL; |
| 48 | } |
| 49 | if (name == NULL || !PyString_Check(name)) { |
| 50 | PyErr_SetString(PyExc_TypeError, |
| 51 | "PyClass_New: name must be a string"); |
| 52 | return NULL; |
| 53 | } |
| 54 | if (dict == NULL || !PyDict_Check(dict)) { |
| 55 | PyErr_SetString(PyExc_TypeError, |
| 56 | "PyClass_New: dict must be a dictionary"); |
| 57 | return NULL; |
| 58 | } |
| 59 | if (PyDict_GetItem(dict, docstr) == NULL) { |
| 60 | if (PyDict_SetItem(dict, docstr, Py_None) < 0) |
| 61 | return NULL; |
| 62 | } |
| 63 | if (PyDict_GetItem(dict, modstr) == NULL) { |
| 64 | PyObject *globals = PyEval_GetGlobals(); |
| 65 | if (globals != NULL) { |
| 66 | PyObject *modname = PyDict_GetItem(globals, namestr); |
| 67 | if (modname != NULL) { |
| 68 | if (PyDict_SetItem(dict, modstr, modname) < 0) |
| 69 | return NULL; |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | if (bases == NULL) { |
| 74 | bases = PyTuple_New(0); |
| 75 | if (bases == NULL) |
| 76 | return NULL; |
| 77 | } |
| 78 | else { |
| 79 | Py_ssize_t i, n; |
| 80 | PyObject *base; |
| 81 | if (!PyTuple_Check(bases)) { |
| 82 | PyErr_SetString(PyExc_TypeError, |
| 83 | "PyClass_New: bases must be a tuple"); |
| 84 | return NULL; |
| 85 | } |
| 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)) |
| 92 | return PyObject_CallFunctionObjArgs( |
| 93 | (PyObject *) base->ob_type, |
| 94 | name, bases, dict, NULL); |
| 95 | PyErr_SetString(PyExc_TypeError, |
| 96 | "PyClass_New: base must be a class"); |
| 97 | return NULL; |
| 98 | } |
| 99 | } |
| 100 | Py_INCREF(bases); |
| 101 | } |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 102 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 103 | if (getattrstr == NULL) { |
| 104 | getattrstr = PyString_InternFromString("__getattr__"); |
| 105 | if (getattrstr == NULL) |
| 106 | goto alloc_error; |
| 107 | setattrstr = PyString_InternFromString("__setattr__"); |
| 108 | if (setattrstr == NULL) |
| 109 | goto alloc_error; |
| 110 | delattrstr = PyString_InternFromString("__delattr__"); |
| 111 | if (delattrstr == NULL) |
| 112 | goto alloc_error; |
| 113 | } |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 114 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 115 | op = PyObject_GC_New(PyClassObject, &PyClass_Type); |
| 116 | if (op == NULL) { |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 117 | alloc_error: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 118 | Py_DECREF(bases); |
| 119 | return NULL; |
| 120 | } |
| 121 | op->cl_bases = bases; |
| 122 | Py_INCREF(dict); |
| 123 | op->cl_dict = dict; |
| 124 | Py_XINCREF(name); |
| 125 | op->cl_name = name; |
| 126 | op->cl_weakreflist = NULL; |
Neal Norwitz | 6cbb726 | 2006-08-19 04:22:33 +0000 | [diff] [blame] | 127 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 128 | op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
| 129 | op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
| 130 | op->cl_delattr = class_lookup(op, delattrstr, &dummy); |
| 131 | Py_XINCREF(op->cl_getattr); |
| 132 | Py_XINCREF(op->cl_setattr); |
| 133 | Py_XINCREF(op->cl_delattr); |
| 134 | _PyObject_GC_TRACK(op); |
| 135 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 138 | PyObject * |
| 139 | PyMethod_Function(PyObject *im) |
| 140 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 141 | if (!PyMethod_Check(im)) { |
| 142 | PyErr_BadInternalCall(); |
| 143 | return NULL; |
| 144 | } |
| 145 | return ((PyMethodObject *)im)->im_func; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 146 | } |
| 147 | |
| 148 | PyObject * |
| 149 | PyMethod_Self(PyObject *im) |
| 150 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 151 | if (!PyMethod_Check(im)) { |
| 152 | PyErr_BadInternalCall(); |
| 153 | return NULL; |
| 154 | } |
| 155 | return ((PyMethodObject *)im)->im_self; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | PyObject * |
| 159 | PyMethod_Class(PyObject *im) |
| 160 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 161 | if (!PyMethod_Check(im)) { |
| 162 | PyErr_BadInternalCall(); |
| 163 | return NULL; |
| 164 | } |
| 165 | return ((PyMethodObject *)im)->im_class; |
Guido van Rossum | b479dc5 | 2001-09-05 22:52:50 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 168 | PyDoc_STRVAR(class_doc, |
| 169 | "classobj(name, bases, dict)\n\ |
| 170 | \n\ |
| 171 | Create a class object. The name must be a string; the second argument\n\ |
| 172 | a tuple of classes, and the third a dictionary."); |
| 173 | |
| 174 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 175 | class_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 176 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 177 | PyObject *name, *bases, *dict; |
| 178 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 179 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 180 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SOO", kwlist, |
| 181 | &name, &bases, &dict)) |
| 182 | return NULL; |
| 183 | return PyClass_New(bases, dict, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 186 | /* Class methods */ |
| 187 | |
| 188 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 189 | class_dealloc(PyClassObject *op) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 190 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 191 | _PyObject_GC_UNTRACK(op); |
| 192 | if (op->cl_weakreflist != NULL) |
| 193 | PyObject_ClearWeakRefs((PyObject *) op); |
| 194 | Py_DECREF(op->cl_bases); |
| 195 | Py_DECREF(op->cl_dict); |
| 196 | Py_XDECREF(op->cl_name); |
| 197 | Py_XDECREF(op->cl_getattr); |
| 198 | Py_XDECREF(op->cl_setattr); |
| 199 | Py_XDECREF(op->cl_delattr); |
| 200 | PyObject_GC_Del(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 201 | } |
| 202 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 204 | class_lookup(PyClassObject *cp, PyObject *name, PyClassObject **pclass) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 205 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 206 | Py_ssize_t i, n; |
| 207 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); |
| 208 | if (value != NULL) { |
| 209 | *pclass = cp; |
| 210 | return value; |
| 211 | } |
| 212 | n = PyTuple_Size(cp->cl_bases); |
| 213 | for (i = 0; i < n; i++) { |
| 214 | /* XXX What if one of the bases is not a class? */ |
| 215 | PyObject *v = class_lookup( |
| 216 | (PyClassObject *) |
| 217 | PyTuple_GetItem(cp->cl_bases, i), name, pclass); |
| 218 | if (v != NULL) |
| 219 | return v; |
| 220 | } |
| 221 | return NULL; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 224 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 225 | class_getattr(register PyClassObject *op, PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 226 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 227 | register PyObject *v; |
Benjamin Peterson | dbc52f8 | 2012-03-16 10:58:46 -0500 | [diff] [blame] | 228 | register char *sname; |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 229 | PyClassObject *klass; |
| 230 | descrgetfunc f; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 231 | |
Benjamin Peterson | dbc52f8 | 2012-03-16 10:58:46 -0500 | [diff] [blame] | 232 | if (!PyString_Check(name)) { |
| 233 | PyErr_SetString(PyExc_TypeError, "attribute name must be a string"); |
| 234 | return NULL; |
| 235 | } |
| 236 | |
| 237 | sname = PyString_AsString(name); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 238 | if (sname[0] == '_' && sname[1] == '_') { |
| 239 | if (strcmp(sname, "__dict__") == 0) { |
| 240 | if (PyEval_GetRestricted()) { |
| 241 | PyErr_SetString(PyExc_RuntimeError, |
| 242 | "class.__dict__ not accessible in restricted mode"); |
| 243 | return NULL; |
| 244 | } |
| 245 | Py_INCREF(op->cl_dict); |
| 246 | return op->cl_dict; |
| 247 | } |
| 248 | if (strcmp(sname, "__bases__") == 0) { |
| 249 | Py_INCREF(op->cl_bases); |
| 250 | return op->cl_bases; |
| 251 | } |
| 252 | if (strcmp(sname, "__name__") == 0) { |
| 253 | if (op->cl_name == NULL) |
| 254 | v = Py_None; |
| 255 | else |
| 256 | v = op->cl_name; |
| 257 | Py_INCREF(v); |
| 258 | return v; |
| 259 | } |
| 260 | } |
| 261 | v = class_lookup(op, name, &klass); |
| 262 | if (v == NULL) { |
| 263 | PyErr_Format(PyExc_AttributeError, |
| 264 | "class %.50s has no attribute '%.400s'", |
| 265 | PyString_AS_STRING(op->cl_name), sname); |
| 266 | return NULL; |
| 267 | } |
| 268 | f = TP_DESCR_GET(v->ob_type); |
| 269 | if (f == NULL) |
| 270 | Py_INCREF(v); |
| 271 | else |
| 272 | v = f(v, (PyObject *)NULL, (PyObject *)op); |
| 273 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 276 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 277 | set_slot(PyObject **slot, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 278 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 279 | PyObject *temp = *slot; |
| 280 | Py_XINCREF(v); |
| 281 | *slot = v; |
| 282 | Py_XDECREF(temp); |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 285 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 286 | set_attr_slots(PyClassObject *c) |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 287 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 288 | PyClassObject *dummy; |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 289 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 290 | set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy)); |
| 291 | set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy)); |
| 292 | set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy)); |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 295 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 296 | set_dict(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 297 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 298 | if (v == NULL || !PyDict_Check(v)) |
| 299 | return "__dict__ must be a dictionary object"; |
| 300 | set_slot(&c->cl_dict, v); |
| 301 | set_attr_slots(c); |
| 302 | return ""; |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 303 | } |
| 304 | |
| 305 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 306 | set_bases(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 307 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 308 | Py_ssize_t i, n; |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 309 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 310 | if (v == NULL || !PyTuple_Check(v)) |
| 311 | return "__bases__ must be a tuple object"; |
| 312 | n = PyTuple_Size(v); |
| 313 | for (i = 0; i < n; i++) { |
| 314 | PyObject *x = PyTuple_GET_ITEM(v, i); |
| 315 | if (!PyClass_Check(x)) |
| 316 | return "__bases__ items must be classes"; |
| 317 | if (PyClass_IsSubclass(x, (PyObject *)c)) |
| 318 | return "a __bases__ item causes an inheritance cycle"; |
| 319 | } |
| 320 | set_slot(&c->cl_bases, v); |
| 321 | set_attr_slots(c); |
| 322 | return ""; |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | static char * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 326 | set_name(PyClassObject *c, PyObject *v) |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 327 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 328 | if (v == NULL || !PyString_Check(v)) |
| 329 | return "__name__ must be a string object"; |
| 330 | if (strlen(PyString_AS_STRING(v)) != (size_t)PyString_GET_SIZE(v)) |
| 331 | return "__name__ must not contain null bytes"; |
| 332 | set_slot(&c->cl_name, v); |
| 333 | return ""; |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 336 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 337 | class_setattr(PyClassObject *op, PyObject *name, PyObject *v) |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 338 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 339 | char *sname; |
| 340 | if (PyEval_GetRestricted()) { |
| 341 | PyErr_SetString(PyExc_RuntimeError, |
| 342 | "classes are read-only in restricted mode"); |
| 343 | return -1; |
| 344 | } |
Benjamin Peterson | dbc52f8 | 2012-03-16 10:58:46 -0500 | [diff] [blame] | 345 | if (!PyString_Check(name)) { |
| 346 | PyErr_SetString(PyExc_TypeError, "attribute name must be a string"); |
| 347 | return -1; |
| 348 | } |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 349 | sname = PyString_AsString(name); |
| 350 | if (sname[0] == '_' && sname[1] == '_') { |
| 351 | Py_ssize_t n = PyString_Size(name); |
| 352 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
| 353 | char *err = NULL; |
| 354 | if (strcmp(sname, "__dict__") == 0) |
| 355 | err = set_dict(op, v); |
| 356 | else if (strcmp(sname, "__bases__") == 0) |
| 357 | err = set_bases(op, v); |
| 358 | else if (strcmp(sname, "__name__") == 0) |
| 359 | err = set_name(op, v); |
| 360 | else if (strcmp(sname, "__getattr__") == 0) |
| 361 | set_slot(&op->cl_getattr, v); |
| 362 | else if (strcmp(sname, "__setattr__") == 0) |
| 363 | set_slot(&op->cl_setattr, v); |
| 364 | else if (strcmp(sname, "__delattr__") == 0) |
| 365 | set_slot(&op->cl_delattr, v); |
| 366 | /* For the last three, we fall through to update the |
| 367 | dictionary as well. */ |
| 368 | if (err != NULL) { |
| 369 | if (*err == '\0') |
| 370 | return 0; |
| 371 | PyErr_SetString(PyExc_TypeError, err); |
| 372 | return -1; |
| 373 | } |
| 374 | } |
| 375 | } |
| 376 | if (v == NULL) { |
| 377 | int rv = PyDict_DelItem(op->cl_dict, name); |
| 378 | if (rv < 0) |
| 379 | PyErr_Format(PyExc_AttributeError, |
| 380 | "class %.50s has no attribute '%.400s'", |
| 381 | PyString_AS_STRING(op->cl_name), sname); |
| 382 | return rv; |
| 383 | } |
| 384 | else |
| 385 | return PyDict_SetItem(op->cl_dict, name, v); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 388 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 389 | class_repr(PyClassObject *op) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 390 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 391 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
| 392 | char *name; |
| 393 | if (op->cl_name == NULL || !PyString_Check(op->cl_name)) |
| 394 | name = "?"; |
| 395 | else |
| 396 | name = PyString_AsString(op->cl_name); |
| 397 | if (mod == NULL || !PyString_Check(mod)) |
| 398 | return PyString_FromFormat("<class ?.%s at %p>", name, op); |
| 399 | else |
| 400 | return PyString_FromFormat("<class %s.%s at %p>", |
| 401 | PyString_AsString(mod), |
| 402 | name, op); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 403 | } |
| 404 | |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 405 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 406 | class_str(PyClassObject *op) |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 407 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 408 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
| 409 | PyObject *name = op->cl_name; |
| 410 | PyObject *res; |
| 411 | Py_ssize_t m, n; |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 412 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 413 | if (name == NULL || !PyString_Check(name)) |
| 414 | return class_repr(op); |
| 415 | if (mod == NULL || !PyString_Check(mod)) { |
| 416 | Py_INCREF(name); |
| 417 | return name; |
| 418 | } |
| 419 | m = PyString_GET_SIZE(mod); |
| 420 | n = PyString_GET_SIZE(name); |
| 421 | res = PyString_FromStringAndSize((char *)NULL, m+1+n); |
| 422 | if (res != NULL) { |
| 423 | char *s = PyString_AS_STRING(res); |
| 424 | memcpy(s, PyString_AS_STRING(mod), m); |
| 425 | s += m; |
| 426 | *s++ = '.'; |
| 427 | memcpy(s, PyString_AS_STRING(name), n); |
| 428 | } |
| 429 | return res; |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 432 | static int |
| 433 | class_traverse(PyClassObject *o, visitproc visit, void *arg) |
| 434 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 435 | Py_VISIT(o->cl_bases); |
| 436 | Py_VISIT(o->cl_dict); |
| 437 | Py_VISIT(o->cl_name); |
| 438 | Py_VISIT(o->cl_getattr); |
| 439 | Py_VISIT(o->cl_setattr); |
| 440 | Py_VISIT(o->cl_delattr); |
| 441 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 442 | } |
| 443 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 444 | PyTypeObject PyClass_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 445 | PyObject_HEAD_INIT(&PyType_Type) |
| 446 | 0, |
| 447 | "classobj", |
| 448 | sizeof(PyClassObject), |
| 449 | 0, |
| 450 | (destructor)class_dealloc, /* tp_dealloc */ |
| 451 | 0, /* tp_print */ |
| 452 | 0, /* tp_getattr */ |
| 453 | 0, /* tp_setattr */ |
| 454 | 0, /* tp_compare */ |
| 455 | (reprfunc)class_repr, /* tp_repr */ |
| 456 | 0, /* tp_as_number */ |
| 457 | 0, /* tp_as_sequence */ |
| 458 | 0, /* tp_as_mapping */ |
| 459 | 0, /* tp_hash */ |
| 460 | PyInstance_New, /* tp_call */ |
| 461 | (reprfunc)class_str, /* tp_str */ |
| 462 | (getattrofunc)class_getattr, /* tp_getattro */ |
| 463 | (setattrofunc)class_setattr, /* tp_setattro */ |
| 464 | 0, /* tp_as_buffer */ |
| 465 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
| 466 | class_doc, /* tp_doc */ |
| 467 | (traverseproc)class_traverse, /* tp_traverse */ |
| 468 | 0, /* tp_clear */ |
| 469 | 0, /* tp_richcompare */ |
| 470 | offsetof(PyClassObject, cl_weakreflist), /* tp_weaklistoffset */ |
| 471 | 0, /* tp_iter */ |
| 472 | 0, /* tp_iternext */ |
| 473 | 0, /* tp_methods */ |
| 474 | 0, /* tp_members */ |
| 475 | 0, /* tp_getset */ |
| 476 | 0, /* tp_base */ |
| 477 | 0, /* tp_dict */ |
| 478 | 0, /* tp_descr_get */ |
| 479 | 0, /* tp_descr_set */ |
| 480 | 0, /* tp_dictoffset */ |
| 481 | 0, /* tp_init */ |
| 482 | 0, /* tp_alloc */ |
| 483 | class_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 484 | }; |
| 485 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 486 | int |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 487 | PyClass_IsSubclass(PyObject *klass, PyObject *base) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 488 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 489 | Py_ssize_t i, n; |
| 490 | PyClassObject *cp; |
| 491 | if (klass == base) |
| 492 | return 1; |
| 493 | if (PyTuple_Check(base)) { |
| 494 | n = PyTuple_GET_SIZE(base); |
| 495 | for (i = 0; i < n; i++) { |
| 496 | if (PyClass_IsSubclass(klass, PyTuple_GET_ITEM(base, i))) |
| 497 | return 1; |
| 498 | } |
| 499 | return 0; |
| 500 | } |
| 501 | if (klass == NULL || !PyClass_Check(klass)) |
| 502 | return 0; |
| 503 | cp = (PyClassObject *)klass; |
| 504 | n = PyTuple_Size(cp->cl_bases); |
| 505 | for (i = 0; i < n; i++) { |
| 506 | if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) |
| 507 | return 1; |
| 508 | } |
| 509 | return 0; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 510 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 511 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 512 | |
| 513 | /* Instance objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 514 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 515 | PyObject * |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 516 | PyInstance_NewRaw(PyObject *klass, PyObject *dict) |
| 517 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 518 | PyInstanceObject *inst; |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 519 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 520 | if (!PyClass_Check(klass)) { |
| 521 | PyErr_BadInternalCall(); |
| 522 | return NULL; |
| 523 | } |
| 524 | if (dict == NULL) { |
| 525 | dict = PyDict_New(); |
| 526 | if (dict == NULL) |
| 527 | return NULL; |
| 528 | } |
| 529 | else { |
| 530 | if (!PyDict_Check(dict)) { |
| 531 | PyErr_BadInternalCall(); |
| 532 | return NULL; |
| 533 | } |
| 534 | Py_INCREF(dict); |
| 535 | } |
| 536 | inst = PyObject_GC_New(PyInstanceObject, &PyInstance_Type); |
| 537 | if (inst == NULL) { |
| 538 | Py_DECREF(dict); |
| 539 | return NULL; |
| 540 | } |
| 541 | inst->in_weakreflist = NULL; |
| 542 | Py_INCREF(klass); |
| 543 | inst->in_class = (PyClassObject *)klass; |
| 544 | inst->in_dict = dict; |
| 545 | _PyObject_GC_TRACK(inst); |
| 546 | return (PyObject *)inst; |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 547 | } |
| 548 | |
| 549 | PyObject * |
| 550 | PyInstance_New(PyObject *klass, PyObject *arg, PyObject *kw) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 551 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 552 | register PyInstanceObject *inst; |
| 553 | PyObject *init; |
| 554 | static PyObject *initstr; |
Fred Drake | 5cc2c8c | 2001-01-28 03:53:08 +0000 | [diff] [blame] | 555 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 556 | if (initstr == NULL) { |
| 557 | initstr = PyString_InternFromString("__init__"); |
| 558 | if (initstr == NULL) |
| 559 | return NULL; |
| 560 | } |
| 561 | inst = (PyInstanceObject *) PyInstance_NewRaw(klass, NULL); |
| 562 | if (inst == NULL) |
| 563 | return NULL; |
| 564 | init = instance_getattr2(inst, initstr); |
| 565 | if (init == NULL) { |
| 566 | if (PyErr_Occurred()) { |
| 567 | Py_DECREF(inst); |
| 568 | return NULL; |
| 569 | } |
| 570 | if ((arg != NULL && (!PyTuple_Check(arg) || |
| 571 | PyTuple_Size(arg) != 0)) |
| 572 | || (kw != NULL && (!PyDict_Check(kw) || |
| 573 | PyDict_Size(kw) != 0))) { |
| 574 | PyErr_SetString(PyExc_TypeError, |
| 575 | "this constructor takes no arguments"); |
| 576 | Py_DECREF(inst); |
| 577 | inst = NULL; |
| 578 | } |
| 579 | } |
| 580 | else { |
| 581 | PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
| 582 | Py_DECREF(init); |
| 583 | if (res == NULL) { |
| 584 | Py_DECREF(inst); |
| 585 | inst = NULL; |
| 586 | } |
| 587 | else { |
| 588 | if (res != Py_None) { |
| 589 | PyErr_SetString(PyExc_TypeError, |
| 590 | "__init__() should return None"); |
| 591 | Py_DECREF(inst); |
| 592 | inst = NULL; |
| 593 | } |
| 594 | Py_DECREF(res); |
| 595 | } |
| 596 | } |
| 597 | return (PyObject *)inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 598 | } |
| 599 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 600 | /* Instance methods */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 602 | PyDoc_STRVAR(instance_doc, |
| 603 | "instance(class[, dict])\n\ |
| 604 | \n\ |
| 605 | Create an instance without calling its __init__() method.\n\ |
| 606 | The class must be a classic class.\n\ |
| 607 | If present, dict must be a dictionary or None."); |
| 608 | |
| 609 | static PyObject * |
| 610 | instance_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 611 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 612 | PyObject *klass; |
| 613 | PyObject *dict = Py_None; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 614 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 615 | if (!PyArg_ParseTuple(args, "O!|O:instance", |
| 616 | &PyClass_Type, &klass, &dict)) |
| 617 | return NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 618 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 619 | if (dict == Py_None) |
| 620 | dict = NULL; |
| 621 | else if (!PyDict_Check(dict)) { |
| 622 | PyErr_SetString(PyExc_TypeError, |
| 623 | "instance() second arg must be dictionary or None"); |
| 624 | return NULL; |
| 625 | } |
| 626 | return PyInstance_NewRaw(klass, dict); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 627 | } |
| 628 | |
| 629 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 630 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 631 | instance_dealloc(register PyInstanceObject *inst) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 632 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 633 | PyObject *error_type, *error_value, *error_traceback; |
| 634 | PyObject *del; |
| 635 | static PyObject *delstr; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 636 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 637 | _PyObject_GC_UNTRACK(inst); |
| 638 | if (inst->in_weakreflist != NULL) |
| 639 | PyObject_ClearWeakRefs((PyObject *) inst); |
Fred Drake | 41deb1e | 2001-02-01 05:27:45 +0000 | [diff] [blame] | 640 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 641 | /* Temporarily resurrect the object. */ |
| 642 | assert(inst->ob_type == &PyInstance_Type); |
| 643 | assert(inst->ob_refcnt == 0); |
| 644 | inst->ob_refcnt = 1; |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 645 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 646 | /* Save the current exception, if any. */ |
| 647 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
| 648 | /* Execute __del__ method, if any. */ |
| 649 | if (delstr == NULL) { |
| 650 | delstr = PyString_InternFromString("__del__"); |
| 651 | if (delstr == NULL) |
| 652 | PyErr_WriteUnraisable((PyObject*)inst); |
| 653 | } |
| 654 | if (delstr && (del = instance_getattr2(inst, delstr)) != NULL) { |
| 655 | PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); |
| 656 | if (res == NULL) |
| 657 | PyErr_WriteUnraisable(del); |
| 658 | else |
| 659 | Py_DECREF(res); |
| 660 | Py_DECREF(del); |
| 661 | } |
| 662 | /* Restore the saved exception. */ |
| 663 | PyErr_Restore(error_type, error_value, error_traceback); |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 664 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 665 | /* Undo the temporary resurrection; can't use DECREF here, it would |
| 666 | * cause a recursive call. |
| 667 | */ |
| 668 | assert(inst->ob_refcnt > 0); |
| 669 | if (--inst->ob_refcnt == 0) { |
Guido van Rossum | 9ff1a44 | 2008-01-18 20:56:30 +0000 | [diff] [blame] | 670 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 671 | /* New weakrefs could be created during the finalizer call. |
| 672 | If this occurs, clear them out without calling their |
| 673 | finalizers since they might rely on part of the object |
| 674 | being finalized that has already been destroyed. */ |
| 675 | while (inst->in_weakreflist != NULL) { |
| 676 | _PyWeakref_ClearRef((PyWeakReference *) |
| 677 | (inst->in_weakreflist)); |
| 678 | } |
Guido van Rossum | 9ff1a44 | 2008-01-18 20:56:30 +0000 | [diff] [blame] | 679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 680 | Py_DECREF(inst->in_class); |
| 681 | Py_XDECREF(inst->in_dict); |
| 682 | PyObject_GC_Del(inst); |
| 683 | } |
| 684 | else { |
| 685 | Py_ssize_t refcnt = inst->ob_refcnt; |
| 686 | /* __del__ resurrected it! Make it look like the original |
| 687 | * Py_DECREF never happened. |
| 688 | */ |
| 689 | _Py_NewReference((PyObject *)inst); |
| 690 | inst->ob_refcnt = refcnt; |
| 691 | _PyObject_GC_TRACK(inst); |
| 692 | /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so |
| 693 | * we need to undo that. */ |
| 694 | _Py_DEC_REFTOTAL; |
| 695 | /* If Py_TRACE_REFS, _Py_NewReference re-added self to the |
| 696 | * object chain, so no more to do there. |
| 697 | * If COUNT_ALLOCS, the original decref bumped tp_frees, and |
| 698 | * _Py_NewReference bumped tp_allocs: both of those need to be |
| 699 | * undone. |
| 700 | */ |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 701 | #ifdef COUNT_ALLOCS |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 702 | --inst->ob_type->tp_frees; |
| 703 | --inst->ob_type->tp_allocs; |
Tim Peters | 6b18491 | 2000-09-17 14:40:17 +0000 | [diff] [blame] | 704 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 705 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 706 | } |
| 707 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 708 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 709 | instance_getattr1(register PyInstanceObject *inst, PyObject *name) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 710 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 711 | register PyObject *v; |
Benjamin Peterson | dbc52f8 | 2012-03-16 10:58:46 -0500 | [diff] [blame] | 712 | register char *sname; |
| 713 | |
| 714 | if (!PyString_Check(name)) { |
| 715 | PyErr_SetString(PyExc_TypeError, "attribute name must be a string"); |
| 716 | return NULL; |
| 717 | } |
| 718 | |
| 719 | sname = PyString_AsString(name); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 720 | if (sname[0] == '_' && sname[1] == '_') { |
| 721 | if (strcmp(sname, "__dict__") == 0) { |
| 722 | if (PyEval_GetRestricted()) { |
| 723 | PyErr_SetString(PyExc_RuntimeError, |
| 724 | "instance.__dict__ not accessible in restricted mode"); |
| 725 | return NULL; |
| 726 | } |
| 727 | Py_INCREF(inst->in_dict); |
| 728 | return inst->in_dict; |
| 729 | } |
| 730 | if (strcmp(sname, "__class__") == 0) { |
| 731 | Py_INCREF(inst->in_class); |
| 732 | return (PyObject *)inst->in_class; |
| 733 | } |
| 734 | } |
| 735 | v = instance_getattr2(inst, name); |
| 736 | if (v == NULL && !PyErr_Occurred()) { |
| 737 | PyErr_Format(PyExc_AttributeError, |
| 738 | "%.50s instance has no attribute '%.400s'", |
| 739 | PyString_AS_STRING(inst->in_class->cl_name), sname); |
| 740 | } |
| 741 | return v; |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 742 | } |
| 743 | |
| 744 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 745 | instance_getattr2(register PyInstanceObject *inst, PyObject *name) |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 746 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 747 | register PyObject *v; |
| 748 | PyClassObject *klass; |
| 749 | descrgetfunc f; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 750 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 751 | v = PyDict_GetItem(inst->in_dict, name); |
| 752 | if (v != NULL) { |
| 753 | Py_INCREF(v); |
| 754 | return v; |
| 755 | } |
| 756 | v = class_lookup(inst->in_class, name, &klass); |
| 757 | if (v != NULL) { |
| 758 | Py_INCREF(v); |
| 759 | f = TP_DESCR_GET(v->ob_type); |
| 760 | if (f != NULL) { |
| 761 | PyObject *w = f(v, (PyObject *)inst, |
| 762 | (PyObject *)(inst->in_class)); |
| 763 | Py_DECREF(v); |
| 764 | v = w; |
| 765 | } |
| 766 | } |
| 767 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 768 | } |
| 769 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 770 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 771 | instance_getattr(register PyInstanceObject *inst, PyObject *name) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 772 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 773 | register PyObject *func, *res; |
| 774 | res = instance_getattr1(inst, name); |
| 775 | if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { |
| 776 | PyObject *args; |
| 777 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 778 | return NULL; |
| 779 | PyErr_Clear(); |
| 780 | args = PyTuple_Pack(2, inst, name); |
| 781 | if (args == NULL) |
| 782 | return NULL; |
| 783 | res = PyEval_CallObject(func, args); |
| 784 | Py_DECREF(args); |
| 785 | } |
| 786 | return res; |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 787 | } |
| 788 | |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 789 | /* See classobject.h comments: this only does dict lookups, and is always |
| 790 | * safe to call. |
| 791 | */ |
| 792 | PyObject * |
| 793 | _PyInstance_Lookup(PyObject *pinst, PyObject *name) |
| 794 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 795 | PyObject *v; |
| 796 | PyClassObject *klass; |
| 797 | PyInstanceObject *inst; /* pinst cast to the right type */ |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 798 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 799 | assert(PyInstance_Check(pinst)); |
| 800 | inst = (PyInstanceObject *)pinst; |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 801 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 802 | assert(PyString_Check(name)); |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 803 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 804 | v = PyDict_GetItem(inst->in_dict, name); |
| 805 | if (v == NULL) |
| 806 | v = class_lookup(inst->in_class, name, &klass); |
| 807 | return v; |
Tim Peters | df875b9 | 2003-04-07 17:51:59 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 810 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 811 | instance_setattr1(PyInstanceObject *inst, PyObject *name, PyObject *v) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 812 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 813 | if (v == NULL) { |
| 814 | int rv = PyDict_DelItem(inst->in_dict, name); |
| 815 | if (rv < 0) |
| 816 | PyErr_Format(PyExc_AttributeError, |
| 817 | "%.50s instance has no attribute '%.400s'", |
| 818 | PyString_AS_STRING(inst->in_class->cl_name), |
| 819 | PyString_AS_STRING(name)); |
| 820 | return rv; |
| 821 | } |
| 822 | else |
| 823 | return PyDict_SetItem(inst->in_dict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 824 | } |
| 825 | |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 826 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 827 | instance_setattr(PyInstanceObject *inst, PyObject *name, PyObject *v) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 828 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 829 | PyObject *func, *args, *res, *tmp; |
Benjamin Peterson | dbc52f8 | 2012-03-16 10:58:46 -0500 | [diff] [blame] | 830 | char *sname; |
| 831 | |
| 832 | if (!PyString_Check(name)) { |
| 833 | PyErr_SetString(PyExc_TypeError, "attribute name must be a string"); |
| 834 | return -1; |
| 835 | } |
| 836 | |
| 837 | sname = PyString_AsString(name); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 838 | if (sname[0] == '_' && sname[1] == '_') { |
| 839 | Py_ssize_t n = PyString_Size(name); |
| 840 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
| 841 | if (strcmp(sname, "__dict__") == 0) { |
| 842 | if (PyEval_GetRestricted()) { |
| 843 | PyErr_SetString(PyExc_RuntimeError, |
| 844 | "__dict__ not accessible in restricted mode"); |
| 845 | return -1; |
| 846 | } |
| 847 | if (v == NULL || !PyDict_Check(v)) { |
| 848 | PyErr_SetString(PyExc_TypeError, |
| 849 | "__dict__ must be set to a dictionary"); |
| 850 | return -1; |
| 851 | } |
| 852 | tmp = inst->in_dict; |
| 853 | Py_INCREF(v); |
| 854 | inst->in_dict = v; |
| 855 | Py_DECREF(tmp); |
| 856 | return 0; |
| 857 | } |
| 858 | if (strcmp(sname, "__class__") == 0) { |
| 859 | if (PyEval_GetRestricted()) { |
| 860 | PyErr_SetString(PyExc_RuntimeError, |
| 861 | "__class__ not accessible in restricted mode"); |
| 862 | return -1; |
| 863 | } |
| 864 | if (v == NULL || !PyClass_Check(v)) { |
| 865 | PyErr_SetString(PyExc_TypeError, |
| 866 | "__class__ must be set to a class"); |
| 867 | return -1; |
| 868 | } |
| 869 | tmp = (PyObject *)(inst->in_class); |
| 870 | Py_INCREF(v); |
| 871 | inst->in_class = (PyClassObject *)v; |
| 872 | Py_DECREF(tmp); |
| 873 | return 0; |
| 874 | } |
| 875 | } |
| 876 | } |
| 877 | if (v == NULL) |
| 878 | func = inst->in_class->cl_delattr; |
| 879 | else |
| 880 | func = inst->in_class->cl_setattr; |
| 881 | if (func == NULL) |
| 882 | return instance_setattr1(inst, name, v); |
| 883 | if (v == NULL) |
| 884 | args = PyTuple_Pack(2, inst, name); |
| 885 | else |
| 886 | args = PyTuple_Pack(3, inst, name, v); |
| 887 | if (args == NULL) |
| 888 | return -1; |
| 889 | res = PyEval_CallObject(func, args); |
| 890 | Py_DECREF(args); |
| 891 | if (res == NULL) |
| 892 | return -1; |
| 893 | Py_DECREF(res); |
| 894 | return 0; |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 895 | } |
| 896 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 897 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 898 | instance_repr(PyInstanceObject *inst) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 899 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 900 | PyObject *func; |
| 901 | PyObject *res; |
| 902 | static PyObject *reprstr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 903 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 904 | if (reprstr == NULL) { |
| 905 | reprstr = PyString_InternFromString("__repr__"); |
| 906 | if (reprstr == NULL) |
| 907 | return NULL; |
| 908 | } |
| 909 | func = instance_getattr(inst, reprstr); |
| 910 | if (func == NULL) { |
| 911 | PyObject *classname, *mod; |
| 912 | char *cname; |
| 913 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 914 | return NULL; |
| 915 | PyErr_Clear(); |
| 916 | classname = inst->in_class->cl_name; |
| 917 | mod = PyDict_GetItemString(inst->in_class->cl_dict, |
| 918 | "__module__"); |
| 919 | if (classname != NULL && PyString_Check(classname)) |
| 920 | cname = PyString_AsString(classname); |
| 921 | else |
| 922 | cname = "?"; |
| 923 | if (mod == NULL || !PyString_Check(mod)) |
| 924 | return PyString_FromFormat("<?.%s instance at %p>", |
| 925 | cname, inst); |
| 926 | else |
| 927 | return PyString_FromFormat("<%s.%s instance at %p>", |
| 928 | PyString_AsString(mod), |
| 929 | cname, inst); |
| 930 | } |
| 931 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 932 | Py_DECREF(func); |
| 933 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 934 | } |
| 935 | |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 936 | static PyObject * |
| 937 | instance_str(PyInstanceObject *inst) |
| 938 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 939 | PyObject *func; |
| 940 | PyObject *res; |
| 941 | static PyObject *strstr; |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 942 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 943 | if (strstr == NULL) { |
| 944 | strstr = PyString_InternFromString("__str__"); |
| 945 | if (strstr == NULL) |
| 946 | return NULL; |
| 947 | } |
| 948 | func = instance_getattr(inst, strstr); |
| 949 | if (func == NULL) { |
| 950 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 951 | return NULL; |
| 952 | PyErr_Clear(); |
| 953 | return instance_repr(inst); |
| 954 | } |
| 955 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 956 | Py_DECREF(func); |
| 957 | return res; |
Guido van Rossum | 82c690f | 2001-04-30 14:39:18 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 960 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 961 | instance_hash(PyInstanceObject *inst) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 962 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 963 | PyObject *func; |
| 964 | PyObject *res; |
| 965 | long outcome; |
| 966 | static PyObject *hashstr, *eqstr, *cmpstr; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 967 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 968 | if (hashstr == NULL) { |
| 969 | hashstr = PyString_InternFromString("__hash__"); |
| 970 | if (hashstr == NULL) |
| 971 | return -1; |
| 972 | } |
| 973 | func = instance_getattr(inst, hashstr); |
| 974 | if (func == NULL) { |
| 975 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 976 | return -1; |
| 977 | PyErr_Clear(); |
| 978 | /* If there is no __eq__ and no __cmp__ method, we hash on the |
| 979 | address. If an __eq__ or __cmp__ method exists, there must |
| 980 | be a __hash__. */ |
| 981 | if (eqstr == NULL) { |
| 982 | eqstr = PyString_InternFromString("__eq__"); |
| 983 | if (eqstr == NULL) |
| 984 | return -1; |
| 985 | } |
| 986 | func = instance_getattr(inst, eqstr); |
| 987 | if (func == NULL) { |
| 988 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 989 | return -1; |
| 990 | PyErr_Clear(); |
| 991 | if (cmpstr == NULL) { |
| 992 | cmpstr = PyString_InternFromString("__cmp__"); |
| 993 | if (cmpstr == NULL) |
| 994 | return -1; |
| 995 | } |
| 996 | func = instance_getattr(inst, cmpstr); |
| 997 | if (func == NULL) { |
| 998 | if (!PyErr_ExceptionMatches( |
| 999 | PyExc_AttributeError)) |
| 1000 | return -1; |
| 1001 | PyErr_Clear(); |
| 1002 | return _Py_HashPointer(inst); |
| 1003 | } |
| 1004 | } |
| 1005 | Py_XDECREF(func); |
| 1006 | PyErr_SetString(PyExc_TypeError, "unhashable instance"); |
| 1007 | return -1; |
| 1008 | } |
| 1009 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1010 | Py_DECREF(func); |
| 1011 | if (res == NULL) |
| 1012 | return -1; |
| 1013 | if (PyInt_Check(res) || PyLong_Check(res)) |
| 1014 | /* This already converts a -1 result to -2. */ |
| 1015 | outcome = res->ob_type->tp_hash(res); |
| 1016 | else { |
| 1017 | PyErr_SetString(PyExc_TypeError, |
| 1018 | "__hash__() should return an int"); |
| 1019 | outcome = -1; |
| 1020 | } |
| 1021 | Py_DECREF(res); |
| 1022 | return outcome; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1025 | static int |
| 1026 | instance_traverse(PyInstanceObject *o, visitproc visit, void *arg) |
| 1027 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1028 | Py_VISIT(o->in_class); |
| 1029 | Py_VISIT(o->in_dict); |
| 1030 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1031 | } |
| 1032 | |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 1033 | static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; |
| 1034 | static PyObject *iterstr, *nextstr; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1035 | |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1036 | static Py_ssize_t |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1037 | instance_length(PyInstanceObject *inst) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1038 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1039 | PyObject *func; |
| 1040 | PyObject *res; |
| 1041 | Py_ssize_t outcome; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1042 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1043 | if (lenstr == NULL) { |
| 1044 | lenstr = PyString_InternFromString("__len__"); |
| 1045 | if (lenstr == NULL) |
| 1046 | return -1; |
| 1047 | } |
| 1048 | func = instance_getattr(inst, lenstr); |
| 1049 | if (func == NULL) |
| 1050 | return -1; |
| 1051 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1052 | Py_DECREF(func); |
| 1053 | if (res == NULL) |
| 1054 | return -1; |
| 1055 | if (PyInt_Check(res)) { |
| 1056 | outcome = PyInt_AsSsize_t(res); |
| 1057 | if (outcome == -1 && PyErr_Occurred()) { |
| 1058 | Py_DECREF(res); |
| 1059 | return -1; |
| 1060 | } |
Neal Norwitz | 1872b1c | 2006-08-12 18:44:06 +0000 | [diff] [blame] | 1061 | #if SIZEOF_SIZE_T < SIZEOF_INT |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1062 | /* Overflow check -- range of PyInt is more than C int */ |
| 1063 | if (outcome != (int)outcome) { |
| 1064 | PyErr_SetString(PyExc_OverflowError, |
| 1065 | "__len__() should return 0 <= outcome < 2**31"); |
| 1066 | outcome = -1; |
| 1067 | } |
| 1068 | else |
Guido van Rossum | ba3e6ec | 2005-09-19 22:42:41 +0000 | [diff] [blame] | 1069 | #endif |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1070 | if (outcome < 0) { |
| 1071 | PyErr_SetString(PyExc_ValueError, |
| 1072 | "__len__() should return >= 0"); |
| 1073 | outcome = -1; |
| 1074 | } |
| 1075 | } |
| 1076 | else { |
| 1077 | PyErr_SetString(PyExc_TypeError, |
| 1078 | "__len__() should return an int"); |
| 1079 | outcome = -1; |
| 1080 | } |
| 1081 | Py_DECREF(res); |
| 1082 | return outcome; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1083 | } |
| 1084 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1085 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1086 | instance_subscript(PyInstanceObject *inst, PyObject *key) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1087 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1088 | PyObject *func; |
| 1089 | PyObject *arg; |
| 1090 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1091 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1092 | if (getitemstr == NULL) { |
| 1093 | getitemstr = PyString_InternFromString("__getitem__"); |
| 1094 | if (getitemstr == NULL) |
| 1095 | return NULL; |
| 1096 | } |
| 1097 | func = instance_getattr(inst, getitemstr); |
| 1098 | if (func == NULL) |
| 1099 | return NULL; |
| 1100 | arg = PyTuple_Pack(1, key); |
| 1101 | if (arg == NULL) { |
| 1102 | Py_DECREF(func); |
| 1103 | return NULL; |
| 1104 | } |
| 1105 | res = PyEval_CallObject(func, arg); |
| 1106 | Py_DECREF(func); |
| 1107 | Py_DECREF(arg); |
| 1108 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1109 | } |
| 1110 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1111 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1112 | instance_ass_subscript(PyInstanceObject *inst, PyObject *key, PyObject *value) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1113 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1114 | PyObject *func; |
| 1115 | PyObject *arg; |
| 1116 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1117 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1118 | if (value == NULL) { |
| 1119 | if (delitemstr == NULL) { |
| 1120 | delitemstr = PyString_InternFromString("__delitem__"); |
| 1121 | if (delitemstr == NULL) |
| 1122 | return -1; |
| 1123 | } |
| 1124 | func = instance_getattr(inst, delitemstr); |
| 1125 | } |
| 1126 | else { |
| 1127 | if (setitemstr == NULL) { |
| 1128 | setitemstr = PyString_InternFromString("__setitem__"); |
| 1129 | if (setitemstr == NULL) |
| 1130 | return -1; |
| 1131 | } |
| 1132 | func = instance_getattr(inst, setitemstr); |
| 1133 | } |
| 1134 | if (func == NULL) |
| 1135 | return -1; |
| 1136 | if (value == NULL) |
| 1137 | arg = PyTuple_Pack(1, key); |
| 1138 | else |
| 1139 | arg = PyTuple_Pack(2, key, value); |
| 1140 | if (arg == NULL) { |
| 1141 | Py_DECREF(func); |
| 1142 | return -1; |
| 1143 | } |
| 1144 | res = PyEval_CallObject(func, arg); |
| 1145 | Py_DECREF(func); |
| 1146 | Py_DECREF(arg); |
| 1147 | if (res == NULL) |
| 1148 | return -1; |
| 1149 | Py_DECREF(res); |
| 1150 | return 0; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1151 | } |
| 1152 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1153 | static PyMappingMethods instance_as_mapping = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1154 | (lenfunc)instance_length, /* mp_length */ |
| 1155 | (binaryfunc)instance_subscript, /* mp_subscript */ |
| 1156 | (objobjargproc)instance_ass_subscript, /* mp_ass_subscript */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1157 | }; |
| 1158 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1159 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1160 | instance_item(PyInstanceObject *inst, Py_ssize_t i) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1161 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1162 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1163 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1164 | if (getitemstr == NULL) { |
| 1165 | getitemstr = PyString_InternFromString("__getitem__"); |
| 1166 | if (getitemstr == NULL) |
| 1167 | return NULL; |
| 1168 | } |
| 1169 | func = instance_getattr(inst, getitemstr); |
| 1170 | if (func == NULL) |
| 1171 | return NULL; |
| 1172 | res = PyObject_CallFunction(func, "n", i); |
| 1173 | Py_DECREF(func); |
| 1174 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1175 | } |
| 1176 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1177 | static PyObject * |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1178 | 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] | 1179 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1180 | PyObject *func, *arg, *res; |
| 1181 | static PyObject *getslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1182 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1183 | if (getslicestr == NULL) { |
| 1184 | getslicestr = PyString_InternFromString("__getslice__"); |
| 1185 | if (getslicestr == NULL) |
| 1186 | return NULL; |
| 1187 | } |
| 1188 | func = instance_getattr(inst, getslicestr); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1189 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1190 | if (func == NULL) { |
| 1191 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1192 | return NULL; |
| 1193 | PyErr_Clear(); |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1194 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1195 | if (getitemstr == NULL) { |
| 1196 | getitemstr = PyString_InternFromString("__getitem__"); |
| 1197 | if (getitemstr == NULL) |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | func = instance_getattr(inst, getitemstr); |
| 1201 | if (func == NULL) |
| 1202 | return NULL; |
| 1203 | arg = Py_BuildValue("(N)", _PySlice_FromIndices(i, j)); |
| 1204 | } |
| 1205 | else { |
| 1206 | if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " |
| 1207 | "use __getitem__", 1) < 0) { |
| 1208 | Py_DECREF(func); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | arg = Py_BuildValue("(nn)", i, j); |
| 1212 | } |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1213 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1214 | if (arg == NULL) { |
| 1215 | Py_DECREF(func); |
| 1216 | return NULL; |
| 1217 | } |
| 1218 | res = PyEval_CallObject(func, arg); |
| 1219 | Py_DECREF(func); |
| 1220 | Py_DECREF(arg); |
| 1221 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1222 | } |
| 1223 | |
| 1224 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1225 | instance_ass_item(PyInstanceObject *inst, Py_ssize_t i, PyObject *item) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1226 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1227 | PyObject *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1228 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1229 | if (item == NULL) { |
| 1230 | if (delitemstr == NULL) { |
| 1231 | delitemstr = PyString_InternFromString("__delitem__"); |
| 1232 | if (delitemstr == NULL) |
| 1233 | return -1; |
| 1234 | } |
| 1235 | func = instance_getattr(inst, delitemstr); |
| 1236 | } |
| 1237 | else { |
| 1238 | if (setitemstr == NULL) { |
| 1239 | setitemstr = PyString_InternFromString("__setitem__"); |
| 1240 | if (setitemstr == NULL) |
| 1241 | return -1; |
| 1242 | } |
| 1243 | func = instance_getattr(inst, setitemstr); |
| 1244 | } |
| 1245 | if (func == NULL) |
| 1246 | return -1; |
| 1247 | if (item == NULL) |
Benjamin Peterson | a7b0976 | 2011-10-15 13:43:21 -0400 | [diff] [blame] | 1248 | arg = Py_BuildValue("(n)", i); |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1249 | else |
| 1250 | arg = Py_BuildValue("(nO)", i, item); |
| 1251 | if (arg == NULL) { |
| 1252 | Py_DECREF(func); |
| 1253 | return -1; |
| 1254 | } |
| 1255 | res = PyEval_CallObject(func, arg); |
| 1256 | Py_DECREF(func); |
| 1257 | Py_DECREF(arg); |
| 1258 | if (res == NULL) |
| 1259 | return -1; |
| 1260 | Py_DECREF(res); |
| 1261 | return 0; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
| 1264 | static int |
Martin v. Löwis | 18e1655 | 2006-02-15 17:27:45 +0000 | [diff] [blame] | 1265 | 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] | 1266 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1267 | PyObject *func, *arg, *res; |
| 1268 | static PyObject *setslicestr, *delslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1269 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1270 | if (value == NULL) { |
| 1271 | if (delslicestr == NULL) { |
| 1272 | delslicestr = |
| 1273 | PyString_InternFromString("__delslice__"); |
| 1274 | if (delslicestr == NULL) |
| 1275 | return -1; |
| 1276 | } |
| 1277 | func = instance_getattr(inst, delslicestr); |
| 1278 | if (func == NULL) { |
| 1279 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1280 | return -1; |
| 1281 | PyErr_Clear(); |
| 1282 | if (delitemstr == NULL) { |
| 1283 | delitemstr = |
| 1284 | PyString_InternFromString("__delitem__"); |
| 1285 | if (delitemstr == NULL) |
| 1286 | return -1; |
| 1287 | } |
| 1288 | func = instance_getattr(inst, delitemstr); |
| 1289 | if (func == NULL) |
| 1290 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1291 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1292 | arg = Py_BuildValue("(N)", |
| 1293 | _PySlice_FromIndices(i, j)); |
| 1294 | } |
| 1295 | else { |
| 1296 | if (PyErr_WarnPy3k("in 3.x, __delslice__ has been " |
| 1297 | "removed; use __delitem__", 1) < 0) { |
| 1298 | Py_DECREF(func); |
| 1299 | return -1; |
| 1300 | } |
| 1301 | arg = Py_BuildValue("(nn)", i, j); |
| 1302 | } |
| 1303 | } |
| 1304 | else { |
| 1305 | if (setslicestr == NULL) { |
| 1306 | setslicestr = |
| 1307 | PyString_InternFromString("__setslice__"); |
| 1308 | if (setslicestr == NULL) |
| 1309 | return -1; |
| 1310 | } |
| 1311 | func = instance_getattr(inst, setslicestr); |
| 1312 | if (func == NULL) { |
| 1313 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1314 | return -1; |
| 1315 | PyErr_Clear(); |
| 1316 | if (setitemstr == NULL) { |
| 1317 | setitemstr = |
| 1318 | PyString_InternFromString("__setitem__"); |
| 1319 | if (setitemstr == NULL) |
| 1320 | return -1; |
| 1321 | } |
| 1322 | func = instance_getattr(inst, setitemstr); |
| 1323 | if (func == NULL) |
| 1324 | return -1; |
Thomas Wouters | 1d75a79 | 2000-08-17 22:37:32 +0000 | [diff] [blame] | 1325 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1326 | arg = Py_BuildValue("(NO)", |
| 1327 | _PySlice_FromIndices(i, j), value); |
| 1328 | } |
| 1329 | else { |
| 1330 | if (PyErr_WarnPy3k("in 3.x, __setslice__ has been " |
| 1331 | "removed; use __setitem__", 1) < 0) { |
| 1332 | Py_DECREF(func); |
| 1333 | return -1; |
| 1334 | } |
| 1335 | arg = Py_BuildValue("(nnO)", i, j, value); |
| 1336 | } |
| 1337 | } |
| 1338 | if (arg == NULL) { |
| 1339 | Py_DECREF(func); |
| 1340 | return -1; |
| 1341 | } |
| 1342 | res = PyEval_CallObject(func, arg); |
| 1343 | Py_DECREF(func); |
| 1344 | Py_DECREF(arg); |
| 1345 | if (res == NULL) |
| 1346 | return -1; |
| 1347 | Py_DECREF(res); |
| 1348 | return 0; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1349 | } |
| 1350 | |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1351 | static int |
| 1352 | instance_contains(PyInstanceObject *inst, PyObject *member) |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1353 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1354 | static PyObject *__contains__; |
| 1355 | PyObject *func; |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1356 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1357 | /* Try __contains__ first. |
| 1358 | * If that can't be done, try iterator-based searching. |
| 1359 | */ |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1360 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1361 | if(__contains__ == NULL) { |
| 1362 | __contains__ = PyString_InternFromString("__contains__"); |
| 1363 | if(__contains__ == NULL) |
| 1364 | return -1; |
| 1365 | } |
| 1366 | func = instance_getattr(inst, __contains__); |
| 1367 | if (func) { |
| 1368 | PyObject *res; |
| 1369 | int ret; |
| 1370 | PyObject *arg = PyTuple_Pack(1, member); |
| 1371 | if(arg == NULL) { |
| 1372 | Py_DECREF(func); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | res = PyEval_CallObject(func, arg); |
| 1376 | Py_DECREF(func); |
| 1377 | Py_DECREF(arg); |
| 1378 | if(res == NULL) |
| 1379 | return -1; |
| 1380 | ret = PyObject_IsTrue(res); |
| 1381 | Py_DECREF(res); |
| 1382 | return ret; |
| 1383 | } |
Tim Peters | cb8d368 | 2001-05-05 21:05:01 +0000 | [diff] [blame] | 1384 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1385 | /* Couldn't find __contains__. */ |
| 1386 | if (PyErr_ExceptionMatches(PyExc_AttributeError)) { |
| 1387 | Py_ssize_t rc; |
| 1388 | /* Assume the failure was simply due to that there is no |
| 1389 | * __contains__ attribute, and try iterating instead. |
| 1390 | */ |
| 1391 | PyErr_Clear(); |
| 1392 | rc = _PySequence_IterSearch((PyObject *)inst, member, |
| 1393 | PY_ITERSEARCH_CONTAINS); |
| 1394 | if (rc >= 0) |
| 1395 | return rc > 0; |
| 1396 | } |
| 1397 | return -1; |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1398 | } |
| 1399 | |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1400 | static PySequenceMethods |
| 1401 | instance_as_sequence = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1402 | (lenfunc)instance_length, /* sq_length */ |
| 1403 | 0, /* sq_concat */ |
| 1404 | 0, /* sq_repeat */ |
| 1405 | (ssizeargfunc)instance_item, /* sq_item */ |
| 1406 | (ssizessizeargfunc)instance_slice, /* sq_slice */ |
| 1407 | (ssizeobjargproc)instance_ass_item, /* sq_ass_item */ |
| 1408 | (ssizessizeobjargproc)instance_ass_slice,/* sq_ass_slice */ |
| 1409 | (objobjproc)instance_contains, /* sq_contains */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1410 | }; |
| 1411 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1412 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1413 | generic_unary_op(PyInstanceObject *self, PyObject *methodname) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1414 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1415 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1416 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1417 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 1418 | return NULL; |
| 1419 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1420 | Py_DECREF(func); |
| 1421 | return res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1424 | static PyObject * |
| 1425 | generic_binary_op(PyObject *v, PyObject *w, char *opname) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1426 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1427 | PyObject *result; |
| 1428 | PyObject *args; |
| 1429 | PyObject *func = PyObject_GetAttrString(v, opname); |
| 1430 | if (func == NULL) { |
| 1431 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1432 | return NULL; |
| 1433 | PyErr_Clear(); |
| 1434 | Py_INCREF(Py_NotImplemented); |
| 1435 | return Py_NotImplemented; |
| 1436 | } |
| 1437 | args = PyTuple_Pack(1, w); |
| 1438 | if (args == NULL) { |
| 1439 | Py_DECREF(func); |
| 1440 | return NULL; |
| 1441 | } |
| 1442 | result = PyEval_CallObject(func, args); |
| 1443 | Py_DECREF(args); |
| 1444 | Py_DECREF(func); |
| 1445 | return result; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | |
| 1449 | static PyObject *coerce_obj; |
| 1450 | |
| 1451 | /* Try one half of a binary operator involving a class instance. */ |
| 1452 | static PyObject * |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1453 | half_binop(PyObject *v, PyObject *w, char *opname, binaryfunc thisfunc, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1454 | int swapped) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1455 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1456 | PyObject *args; |
| 1457 | PyObject *coercefunc; |
| 1458 | PyObject *coerced = NULL; |
| 1459 | PyObject *v1; |
| 1460 | PyObject *result; |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1461 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1462 | if (!PyInstance_Check(v)) { |
| 1463 | Py_INCREF(Py_NotImplemented); |
| 1464 | return Py_NotImplemented; |
| 1465 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1466 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1467 | if (coerce_obj == NULL) { |
| 1468 | coerce_obj = PyString_InternFromString("__coerce__"); |
| 1469 | if (coerce_obj == NULL) |
| 1470 | return NULL; |
| 1471 | } |
| 1472 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1473 | if (coercefunc == NULL) { |
| 1474 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1475 | return NULL; |
| 1476 | PyErr_Clear(); |
| 1477 | return generic_binary_op(v, w, opname); |
| 1478 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1479 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1480 | args = PyTuple_Pack(1, w); |
| 1481 | if (args == NULL) { |
| 1482 | Py_DECREF(coercefunc); |
| 1483 | return NULL; |
| 1484 | } |
| 1485 | coerced = PyEval_CallObject(coercefunc, args); |
| 1486 | Py_DECREF(args); |
| 1487 | Py_DECREF(coercefunc); |
| 1488 | if (coerced == NULL) { |
| 1489 | return NULL; |
| 1490 | } |
| 1491 | if (coerced == Py_None || coerced == Py_NotImplemented) { |
| 1492 | Py_DECREF(coerced); |
| 1493 | return generic_binary_op(v, w, opname); |
| 1494 | } |
| 1495 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
| 1496 | Py_DECREF(coerced); |
| 1497 | PyErr_SetString(PyExc_TypeError, |
| 1498 | "coercion should return None or 2-tuple"); |
| 1499 | return NULL; |
| 1500 | } |
| 1501 | v1 = PyTuple_GetItem(coerced, 0); |
| 1502 | w = PyTuple_GetItem(coerced, 1); |
| 1503 | if (v1->ob_type == v->ob_type && PyInstance_Check(v)) { |
| 1504 | /* prevent recursion if __coerce__ returns self as the first |
| 1505 | * argument */ |
| 1506 | result = generic_binary_op(v1, w, opname); |
| 1507 | } else { |
| 1508 | if (Py_EnterRecursiveCall(" after coercion")) |
| 1509 | return NULL; |
| 1510 | if (swapped) |
| 1511 | result = (thisfunc)(w, v1); |
| 1512 | else |
| 1513 | result = (thisfunc)(v1, w); |
| 1514 | Py_LeaveRecursiveCall(); |
| 1515 | } |
| 1516 | Py_DECREF(coerced); |
| 1517 | return result; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1518 | } |
| 1519 | |
| 1520 | /* Implement a binary operator involving at least one class instance. */ |
| 1521 | static PyObject * |
| 1522 | do_binop(PyObject *v, PyObject *w, char *opname, char *ropname, |
| 1523 | binaryfunc thisfunc) |
| 1524 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1525 | PyObject *result = half_binop(v, w, opname, thisfunc, 0); |
| 1526 | if (result == Py_NotImplemented) { |
| 1527 | Py_DECREF(result); |
| 1528 | result = half_binop(w, v, ropname, thisfunc, 1); |
| 1529 | } |
| 1530 | return result; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1531 | } |
| 1532 | |
| 1533 | static PyObject * |
| 1534 | do_binop_inplace(PyObject *v, PyObject *w, char *iopname, char *opname, |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1535 | char *ropname, binaryfunc thisfunc) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1536 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1537 | PyObject *result = half_binop(v, w, iopname, thisfunc, 0); |
| 1538 | if (result == Py_NotImplemented) { |
| 1539 | Py_DECREF(result); |
| 1540 | result = do_binop(v, w, opname, ropname, thisfunc); |
| 1541 | } |
| 1542 | return result; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1543 | } |
| 1544 | |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1545 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1546 | instance_coerce(PyObject **pv, PyObject **pw) |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1547 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1548 | PyObject *v = *pv; |
| 1549 | PyObject *w = *pw; |
| 1550 | PyObject *coercefunc; |
| 1551 | PyObject *args; |
| 1552 | PyObject *coerced; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1553 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1554 | if (coerce_obj == NULL) { |
| 1555 | coerce_obj = PyString_InternFromString("__coerce__"); |
| 1556 | if (coerce_obj == NULL) |
| 1557 | return -1; |
| 1558 | } |
| 1559 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1560 | if (coercefunc == NULL) { |
| 1561 | /* No __coerce__ method */ |
| 1562 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1563 | return -1; |
| 1564 | PyErr_Clear(); |
| 1565 | return 1; |
| 1566 | } |
| 1567 | /* Has __coerce__ method: call it */ |
| 1568 | args = PyTuple_Pack(1, w); |
| 1569 | if (args == NULL) { |
| 1570 | return -1; |
| 1571 | } |
| 1572 | coerced = PyEval_CallObject(coercefunc, args); |
| 1573 | Py_DECREF(args); |
| 1574 | Py_DECREF(coercefunc); |
| 1575 | if (coerced == NULL) { |
| 1576 | /* __coerce__ call raised an exception */ |
| 1577 | return -1; |
| 1578 | } |
| 1579 | if (coerced == Py_None || coerced == Py_NotImplemented) { |
| 1580 | /* __coerce__ says "I can't do it" */ |
| 1581 | Py_DECREF(coerced); |
| 1582 | return 1; |
| 1583 | } |
| 1584 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
| 1585 | /* __coerce__ return value is malformed */ |
| 1586 | Py_DECREF(coerced); |
| 1587 | PyErr_SetString(PyExc_TypeError, |
| 1588 | "coercion should return None or 2-tuple"); |
| 1589 | return -1; |
| 1590 | } |
| 1591 | /* __coerce__ returned two new values */ |
| 1592 | *pv = PyTuple_GetItem(coerced, 0); |
| 1593 | *pw = PyTuple_GetItem(coerced, 1); |
| 1594 | Py_INCREF(*pv); |
| 1595 | Py_INCREF(*pw); |
| 1596 | Py_DECREF(coerced); |
| 1597 | return 0; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1598 | } |
| 1599 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1600 | #define UNARY(funcname, methodname) \ |
Thomas Wouters | c307352 | 2000-07-23 22:09:59 +0000 | [diff] [blame] | 1601 | static PyObject *funcname(PyInstanceObject *self) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1602 | static PyObject *o; \ |
| 1603 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
| 1604 | if (o == NULL) return NULL; } \ |
| 1605 | return generic_unary_op(self, o); \ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1608 | /* unary function with a fallback */ |
| 1609 | #define UNARY_FB(funcname, methodname, funcname_fb) \ |
| 1610 | static PyObject *funcname(PyInstanceObject *self) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1611 | static PyObject *o; \ |
| 1612 | if (o == NULL) { o = PyString_InternFromString(methodname); \ |
| 1613 | if (o == NULL) return NULL; } \ |
| 1614 | if (PyObject_HasAttr((PyObject*)self, o)) \ |
| 1615 | return generic_unary_op(self, o); \ |
| 1616 | else \ |
| 1617 | return funcname_fb(self); \ |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1618 | } |
| 1619 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1620 | #define BINARY(f, m, n) \ |
| 1621 | static PyObject *f(PyObject *v, PyObject *w) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1622 | return do_binop(v, w, "__" m "__", "__r" m "__", n); \ |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1623 | } |
| 1624 | |
| 1625 | #define BINARY_INPLACE(f, m, n) \ |
| 1626 | static PyObject *f(PyObject *v, PyObject *w) { \ |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1627 | return do_binop_inplace(v, w, "__i" m "__", "__" m "__", \ |
| 1628 | "__r" m "__", n); \ |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1629 | } |
| 1630 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1631 | UNARY(instance_neg, "__neg__") |
| 1632 | UNARY(instance_pos, "__pos__") |
| 1633 | UNARY(instance_abs, "__abs__") |
| 1634 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1635 | BINARY(instance_or, "or", PyNumber_Or) |
| 1636 | BINARY(instance_and, "and", PyNumber_And) |
| 1637 | BINARY(instance_xor, "xor", PyNumber_Xor) |
| 1638 | BINARY(instance_lshift, "lshift", PyNumber_Lshift) |
| 1639 | BINARY(instance_rshift, "rshift", PyNumber_Rshift) |
| 1640 | BINARY(instance_add, "add", PyNumber_Add) |
| 1641 | BINARY(instance_sub, "sub", PyNumber_Subtract) |
| 1642 | BINARY(instance_mul, "mul", PyNumber_Multiply) |
| 1643 | BINARY(instance_div, "div", PyNumber_Divide) |
| 1644 | BINARY(instance_mod, "mod", PyNumber_Remainder) |
| 1645 | BINARY(instance_divmod, "divmod", PyNumber_Divmod) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1646 | BINARY(instance_floordiv, "floordiv", PyNumber_FloorDivide) |
| 1647 | BINARY(instance_truediv, "truediv", PyNumber_TrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1648 | |
| 1649 | BINARY_INPLACE(instance_ior, "or", PyNumber_InPlaceOr) |
| 1650 | BINARY_INPLACE(instance_ixor, "xor", PyNumber_InPlaceXor) |
| 1651 | BINARY_INPLACE(instance_iand, "and", PyNumber_InPlaceAnd) |
| 1652 | BINARY_INPLACE(instance_ilshift, "lshift", PyNumber_InPlaceLshift) |
| 1653 | BINARY_INPLACE(instance_irshift, "rshift", PyNumber_InPlaceRshift) |
| 1654 | BINARY_INPLACE(instance_iadd, "add", PyNumber_InPlaceAdd) |
| 1655 | BINARY_INPLACE(instance_isub, "sub", PyNumber_InPlaceSubtract) |
| 1656 | BINARY_INPLACE(instance_imul, "mul", PyNumber_InPlaceMultiply) |
| 1657 | BINARY_INPLACE(instance_idiv, "div", PyNumber_InPlaceDivide) |
| 1658 | BINARY_INPLACE(instance_imod, "mod", PyNumber_InPlaceRemainder) |
Guido van Rossum | 4668b00 | 2001-08-08 05:00:18 +0000 | [diff] [blame] | 1659 | BINARY_INPLACE(instance_ifloordiv, "floordiv", PyNumber_InPlaceFloorDivide) |
| 1660 | BINARY_INPLACE(instance_itruediv, "truediv", PyNumber_InPlaceTrueDivide) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1661 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1662 | /* Try a 3-way comparison, returning an int; v is an instance. Return: |
| 1663 | -2 for an exception; |
| 1664 | -1 if v < w; |
| 1665 | 0 if v == w; |
| 1666 | 1 if v > w; |
| 1667 | 2 if this particular 3-way comparison is not implemented or undefined. |
| 1668 | */ |
| 1669 | static int |
| 1670 | half_cmp(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1671 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1672 | static PyObject *cmp_obj; |
| 1673 | PyObject *args; |
| 1674 | PyObject *cmp_func; |
| 1675 | PyObject *result; |
| 1676 | long l; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1677 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1678 | assert(PyInstance_Check(v)); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1679 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1680 | if (cmp_obj == NULL) { |
| 1681 | cmp_obj = PyString_InternFromString("__cmp__"); |
| 1682 | if (cmp_obj == NULL) |
| 1683 | return -2; |
| 1684 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1685 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1686 | cmp_func = PyObject_GetAttr(v, cmp_obj); |
| 1687 | if (cmp_func == NULL) { |
| 1688 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1689 | return -2; |
| 1690 | PyErr_Clear(); |
| 1691 | return 2; |
| 1692 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1693 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1694 | args = PyTuple_Pack(1, w); |
| 1695 | if (args == NULL) { |
| 1696 | Py_DECREF(cmp_func); |
| 1697 | return -2; |
| 1698 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1699 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1700 | result = PyEval_CallObject(cmp_func, args); |
| 1701 | Py_DECREF(args); |
| 1702 | Py_DECREF(cmp_func); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1703 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1704 | if (result == NULL) |
| 1705 | return -2; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1706 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1707 | if (result == Py_NotImplemented) { |
| 1708 | Py_DECREF(result); |
| 1709 | return 2; |
| 1710 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1711 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1712 | l = PyInt_AsLong(result); |
| 1713 | Py_DECREF(result); |
| 1714 | if (l == -1 && PyErr_Occurred()) { |
| 1715 | PyErr_SetString(PyExc_TypeError, |
| 1716 | "comparison did not return an int"); |
| 1717 | return -2; |
| 1718 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1719 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1720 | return l < 0 ? -1 : l > 0 ? 1 : 0; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1721 | } |
| 1722 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1723 | /* Try a 3-way comparison, returning an int; either v or w is an instance. |
| 1724 | We first try a coercion. Return: |
| 1725 | -2 for an exception; |
| 1726 | -1 if v < w; |
| 1727 | 0 if v == w; |
| 1728 | 1 if v > w; |
| 1729 | 2 if this particular 3-way comparison is not implemented or undefined. |
| 1730 | THIS IS ONLY CALLED FROM object.c! |
| 1731 | */ |
| 1732 | static int |
| 1733 | instance_compare(PyObject *v, PyObject *w) |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1734 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1735 | int c; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1736 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1737 | c = PyNumber_CoerceEx(&v, &w); |
| 1738 | if (c < 0) |
| 1739 | return -2; |
| 1740 | if (c == 0) { |
| 1741 | /* If neither is now an instance, use regular comparison */ |
| 1742 | if (!PyInstance_Check(v) && !PyInstance_Check(w)) { |
| 1743 | c = PyObject_Compare(v, w); |
| 1744 | Py_DECREF(v); |
| 1745 | Py_DECREF(w); |
| 1746 | if (PyErr_Occurred()) |
| 1747 | return -2; |
| 1748 | return c < 0 ? -1 : c > 0 ? 1 : 0; |
| 1749 | } |
| 1750 | } |
| 1751 | else { |
| 1752 | /* The coercion didn't do anything. |
| 1753 | Treat this the same as returning v and w unchanged. */ |
| 1754 | Py_INCREF(v); |
| 1755 | Py_INCREF(w); |
| 1756 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1757 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1758 | if (PyInstance_Check(v)) { |
| 1759 | c = half_cmp(v, w); |
| 1760 | if (c <= 1) { |
| 1761 | Py_DECREF(v); |
| 1762 | Py_DECREF(w); |
| 1763 | return c; |
| 1764 | } |
| 1765 | } |
| 1766 | if (PyInstance_Check(w)) { |
| 1767 | c = half_cmp(w, v); |
| 1768 | if (c <= 1) { |
| 1769 | Py_DECREF(v); |
| 1770 | Py_DECREF(w); |
| 1771 | if (c >= -1) |
| 1772 | c = -c; |
| 1773 | return c; |
| 1774 | } |
| 1775 | } |
| 1776 | Py_DECREF(v); |
| 1777 | Py_DECREF(w); |
| 1778 | return 2; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1779 | } |
| 1780 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1781 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1782 | instance_nonzero(PyInstanceObject *self) |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1783 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1784 | PyObject *func, *res; |
| 1785 | long outcome; |
| 1786 | static PyObject *nonzerostr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1787 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1788 | if (nonzerostr == NULL) { |
| 1789 | nonzerostr = PyString_InternFromString("__nonzero__"); |
| 1790 | if (nonzerostr == NULL) |
| 1791 | return -1; |
| 1792 | } |
| 1793 | if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
| 1794 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1795 | return -1; |
| 1796 | PyErr_Clear(); |
| 1797 | if (lenstr == NULL) { |
| 1798 | lenstr = PyString_InternFromString("__len__"); |
| 1799 | if (lenstr == NULL) |
| 1800 | return -1; |
| 1801 | } |
| 1802 | if ((func = instance_getattr(self, lenstr)) == NULL) { |
| 1803 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1804 | return -1; |
| 1805 | PyErr_Clear(); |
| 1806 | /* Fall back to the default behavior: |
| 1807 | all instances are nonzero */ |
| 1808 | return 1; |
| 1809 | } |
| 1810 | } |
| 1811 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1812 | Py_DECREF(func); |
| 1813 | if (res == NULL) |
| 1814 | return -1; |
| 1815 | if (!PyInt_Check(res)) { |
| 1816 | Py_DECREF(res); |
| 1817 | PyErr_SetString(PyExc_TypeError, |
| 1818 | "__nonzero__ should return an int"); |
| 1819 | return -1; |
| 1820 | } |
| 1821 | outcome = PyInt_AsLong(res); |
| 1822 | Py_DECREF(res); |
| 1823 | if (outcome < 0) { |
| 1824 | PyErr_SetString(PyExc_ValueError, |
| 1825 | "__nonzero__ should return >= 0"); |
| 1826 | return -1; |
| 1827 | } |
| 1828 | return outcome > 0; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1829 | } |
| 1830 | |
Neal Norwitz | 8a87f5d | 2006-08-12 17:03:09 +0000 | [diff] [blame] | 1831 | static PyObject * |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1832 | instance_index(PyInstanceObject *self) |
| 1833 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1834 | PyObject *func, *res; |
| 1835 | static PyObject *indexstr = NULL; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1836 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1837 | if (indexstr == NULL) { |
| 1838 | indexstr = PyString_InternFromString("__index__"); |
| 1839 | if (indexstr == NULL) |
| 1840 | return NULL; |
| 1841 | } |
| 1842 | if ((func = instance_getattr(self, indexstr)) == NULL) { |
| 1843 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1844 | return NULL; |
| 1845 | PyErr_Clear(); |
| 1846 | PyErr_SetString(PyExc_TypeError, |
| 1847 | "object cannot be interpreted as an index"); |
| 1848 | return NULL; |
| 1849 | } |
| 1850 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1851 | Py_DECREF(func); |
| 1852 | return res; |
Guido van Rossum | 38fff8c | 2006-03-07 18:50:55 +0000 | [diff] [blame] | 1853 | } |
| 1854 | |
| 1855 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1856 | UNARY(instance_invert, "__invert__") |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1857 | UNARY(_instance_trunc, "__trunc__") |
| 1858 | |
| 1859 | static PyObject * |
| 1860 | instance_int(PyInstanceObject *self) |
| 1861 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1862 | PyObject *truncated; |
| 1863 | static PyObject *int_name; |
| 1864 | if (int_name == NULL) { |
| 1865 | int_name = PyString_InternFromString("__int__"); |
| 1866 | if (int_name == NULL) |
| 1867 | return NULL; |
| 1868 | } |
| 1869 | if (PyObject_HasAttr((PyObject*)self, int_name)) |
| 1870 | return generic_unary_op(self, int_name); |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1871 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1872 | truncated = _instance_trunc(self); |
| 1873 | /* __trunc__ is specified to return an Integral type, but |
| 1874 | int() needs to return an int. */ |
| 1875 | return _PyNumber_ConvertIntegralToInt( |
| 1876 | truncated, |
| 1877 | "__trunc__ returned non-Integral (type %.200s)"); |
Jeffrey Yasskin | a26cf9b | 2008-02-04 01:04:35 +0000 | [diff] [blame] | 1878 | } |
| 1879 | |
Kristján Valur Jónsson | abe1d48 | 2007-05-07 16:46:54 +0000 | [diff] [blame] | 1880 | UNARY_FB(instance_long, "__long__", instance_int) |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1881 | UNARY(instance_float, "__float__") |
| 1882 | UNARY(instance_oct, "__oct__") |
| 1883 | UNARY(instance_hex, "__hex__") |
| 1884 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1885 | static PyObject * |
| 1886 | bin_power(PyObject *v, PyObject *w) |
| 1887 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1888 | return PyNumber_Power(v, w, Py_None); |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1889 | } |
| 1890 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1891 | /* This version is for ternary calls only (z != None) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1892 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 1893 | instance_pow(PyObject *v, PyObject *w, PyObject *z) |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1894 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1895 | if (z == Py_None) { |
| 1896 | return do_binop(v, w, "__pow__", "__rpow__", bin_power); |
| 1897 | } |
| 1898 | else { |
| 1899 | PyObject *func; |
| 1900 | PyObject *args; |
| 1901 | PyObject *result; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1902 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1903 | /* XXX Doesn't do coercions... */ |
| 1904 | func = PyObject_GetAttrString(v, "__pow__"); |
| 1905 | if (func == NULL) |
| 1906 | return NULL; |
| 1907 | args = PyTuple_Pack(2, w, z); |
| 1908 | if (args == NULL) { |
| 1909 | Py_DECREF(func); |
| 1910 | return NULL; |
| 1911 | } |
| 1912 | result = PyEval_CallObject(func, args); |
| 1913 | Py_DECREF(func); |
| 1914 | Py_DECREF(args); |
| 1915 | return result; |
| 1916 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1917 | } |
| 1918 | |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1919 | static PyObject * |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1920 | bin_inplace_power(PyObject *v, PyObject *w) |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1921 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1922 | return PyNumber_InPlacePower(v, w, Py_None); |
Thomas Wouters | e289e0b | 2000-08-24 20:08:19 +0000 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1926 | static PyObject * |
| 1927 | instance_ipow(PyObject *v, PyObject *w, PyObject *z) |
| 1928 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1929 | if (z == Py_None) { |
| 1930 | return do_binop_inplace(v, w, "__ipow__", "__pow__", |
| 1931 | "__rpow__", bin_inplace_power); |
| 1932 | } |
| 1933 | else { |
| 1934 | /* XXX Doesn't do coercions... */ |
| 1935 | PyObject *func; |
| 1936 | PyObject *args; |
| 1937 | PyObject *result; |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1938 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1939 | func = PyObject_GetAttrString(v, "__ipow__"); |
| 1940 | if (func == NULL) { |
| 1941 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1942 | return NULL; |
| 1943 | PyErr_Clear(); |
| 1944 | return instance_pow(v, w, z); |
| 1945 | } |
| 1946 | args = PyTuple_Pack(2, w, z); |
| 1947 | if (args == NULL) { |
| 1948 | Py_DECREF(func); |
| 1949 | return NULL; |
| 1950 | } |
| 1951 | result = PyEval_CallObject(func, args); |
| 1952 | Py_DECREF(func); |
| 1953 | Py_DECREF(args); |
| 1954 | return result; |
| 1955 | } |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 1956 | } |
| 1957 | |
| 1958 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1959 | /* Map rich comparison operators to their __xx__ namesakes */ |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1960 | #define NAME_OPS 6 |
| 1961 | static PyObject **name_op = NULL; |
| 1962 | |
Tim Peters | 3459251 | 2002-07-11 06:23:50 +0000 | [diff] [blame] | 1963 | static int |
Guido van Rossum | 0ba9e3a | 2001-05-22 02:33:08 +0000 | [diff] [blame] | 1964 | init_name_op(void) |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1965 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1966 | int i; |
| 1967 | char *_name_op[] = { |
| 1968 | "__lt__", |
| 1969 | "__le__", |
| 1970 | "__eq__", |
| 1971 | "__ne__", |
| 1972 | "__gt__", |
| 1973 | "__ge__", |
| 1974 | }; |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1975 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1976 | name_op = (PyObject **)malloc(sizeof(PyObject *) * NAME_OPS); |
| 1977 | if (name_op == NULL) |
| 1978 | return -1; |
| 1979 | for (i = 0; i < NAME_OPS; ++i) { |
| 1980 | name_op[i] = PyString_InternFromString(_name_op[i]); |
| 1981 | if (name_op[i] == NULL) |
| 1982 | return -1; |
| 1983 | } |
| 1984 | return 0; |
Jeremy Hylton | 1b0feb4 | 2001-05-11 14:48:41 +0000 | [diff] [blame] | 1985 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1986 | |
| 1987 | static PyObject * |
| 1988 | half_richcompare(PyObject *v, PyObject *w, int op) |
| 1989 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1990 | PyObject *method; |
| 1991 | PyObject *args; |
| 1992 | PyObject *res; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1993 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1994 | assert(PyInstance_Check(v)); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 1995 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 1996 | if (name_op == NULL) { |
| 1997 | if (init_name_op() < 0) |
| 1998 | return NULL; |
| 1999 | } |
| 2000 | /* If the instance doesn't define an __getattr__ method, use |
| 2001 | instance_getattr2 directly because it will not set an |
| 2002 | exception on failure. */ |
| 2003 | if (((PyInstanceObject *)v)->in_class->cl_getattr == NULL) |
| 2004 | method = instance_getattr2((PyInstanceObject *)v, |
| 2005 | name_op[op]); |
| 2006 | else |
| 2007 | method = PyObject_GetAttr(v, name_op[op]); |
| 2008 | if (method == NULL) { |
| 2009 | if (PyErr_Occurred()) { |
| 2010 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2011 | return NULL; |
| 2012 | PyErr_Clear(); |
| 2013 | } |
| 2014 | res = Py_NotImplemented; |
| 2015 | Py_INCREF(res); |
| 2016 | return res; |
| 2017 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2018 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2019 | args = PyTuple_Pack(1, w); |
| 2020 | if (args == NULL) { |
| 2021 | Py_DECREF(method); |
| 2022 | return NULL; |
| 2023 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2024 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2025 | res = PyEval_CallObject(method, args); |
| 2026 | Py_DECREF(args); |
| 2027 | Py_DECREF(method); |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2028 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2029 | return res; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2030 | } |
| 2031 | |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2032 | static PyObject * |
| 2033 | instance_richcompare(PyObject *v, PyObject *w, int op) |
| 2034 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2035 | PyObject *res; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2036 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2037 | if (PyInstance_Check(v)) { |
| 2038 | res = half_richcompare(v, w, op); |
| 2039 | if (res != Py_NotImplemented) |
| 2040 | return res; |
| 2041 | Py_DECREF(res); |
| 2042 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2043 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2044 | if (PyInstance_Check(w)) { |
| 2045 | res = half_richcompare(w, v, _Py_SwappedOp[op]); |
| 2046 | if (res != Py_NotImplemented) |
| 2047 | return res; |
| 2048 | Py_DECREF(res); |
| 2049 | } |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2050 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2051 | Py_INCREF(Py_NotImplemented); |
| 2052 | return Py_NotImplemented; |
Guido van Rossum | 8998b4f | 2001-01-17 15:28:20 +0000 | [diff] [blame] | 2053 | } |
| 2054 | |
Neil Schemenauer | 29bfc07 | 2001-01-04 01:43:46 +0000 | [diff] [blame] | 2055 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2056 | /* Get the iterator */ |
| 2057 | static PyObject * |
| 2058 | instance_getiter(PyInstanceObject *self) |
| 2059 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2060 | PyObject *func; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2061 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2062 | if (iterstr == NULL) { |
| 2063 | iterstr = PyString_InternFromString("__iter__"); |
| 2064 | if (iterstr == NULL) |
| 2065 | return NULL; |
| 2066 | } |
| 2067 | if (getitemstr == NULL) { |
| 2068 | getitemstr = PyString_InternFromString("__getitem__"); |
| 2069 | if (getitemstr == NULL) |
| 2070 | return NULL; |
| 2071 | } |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2072 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2073 | if ((func = instance_getattr(self, iterstr)) != NULL) { |
| 2074 | PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
| 2075 | Py_DECREF(func); |
| 2076 | if (res != NULL && !PyIter_Check(res)) { |
| 2077 | PyErr_Format(PyExc_TypeError, |
| 2078 | "__iter__ returned non-iterator " |
| 2079 | "of type '%.100s'", |
| 2080 | res->ob_type->tp_name); |
| 2081 | Py_DECREF(res); |
| 2082 | res = NULL; |
| 2083 | } |
| 2084 | return res; |
| 2085 | } |
| 2086 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2087 | return NULL; |
| 2088 | PyErr_Clear(); |
| 2089 | if ((func = instance_getattr(self, getitemstr)) == NULL) { |
| 2090 | PyErr_SetString(PyExc_TypeError, |
| 2091 | "iteration over non-sequence"); |
| 2092 | return NULL; |
| 2093 | } |
| 2094 | Py_DECREF(func); |
| 2095 | return PySeqIter_New((PyObject *)self); |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2096 | } |
| 2097 | |
| 2098 | |
| 2099 | /* Call the iterator's next */ |
| 2100 | static PyObject * |
| 2101 | instance_iternext(PyInstanceObject *self) |
| 2102 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2103 | PyObject *func; |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2104 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2105 | if (nextstr == NULL) { |
| 2106 | nextstr = PyString_InternFromString("next"); |
| 2107 | if (nextstr == NULL) |
| 2108 | return NULL; |
| 2109 | } |
Guido van Rossum | 213c7a6 | 2001-04-23 14:08:49 +0000 | [diff] [blame] | 2110 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2111 | if ((func = instance_getattr(self, nextstr)) != NULL) { |
| 2112 | PyObject *res = PyEval_CallObject(func, (PyObject *)NULL); |
| 2113 | Py_DECREF(func); |
| 2114 | if (res != NULL) { |
| 2115 | return res; |
| 2116 | } |
| 2117 | if (PyErr_ExceptionMatches(PyExc_StopIteration)) { |
| 2118 | PyErr_Clear(); |
| 2119 | return NULL; |
| 2120 | } |
| 2121 | return NULL; |
| 2122 | } |
| 2123 | PyErr_SetString(PyExc_TypeError, "instance has no next() method"); |
| 2124 | return NULL; |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2125 | } |
| 2126 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2127 | static PyObject * |
| 2128 | instance_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2129 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2130 | PyObject *res, *call = PyObject_GetAttrString(func, "__call__"); |
| 2131 | if (call == NULL) { |
| 2132 | PyInstanceObject *inst = (PyInstanceObject*) func; |
| 2133 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2134 | return NULL; |
| 2135 | PyErr_Clear(); |
| 2136 | PyErr_Format(PyExc_AttributeError, |
| 2137 | "%.200s instance has no __call__ method", |
| 2138 | PyString_AsString(inst->in_class->cl_name)); |
| 2139 | return NULL; |
| 2140 | } |
| 2141 | /* We must check and increment the recursion depth here. Scenario: |
| 2142 | class A: |
| 2143 | pass |
| 2144 | A.__call__ = A() # that's right |
| 2145 | a = A() # ok |
| 2146 | a() # infinite recursion |
| 2147 | This bounces between instance_call() and PyObject_Call() without |
| 2148 | ever hitting eval_frame() (which has the main recursion check). */ |
| 2149 | if (Py_EnterRecursiveCall(" in __call__")) { |
| 2150 | res = NULL; |
| 2151 | } |
| 2152 | else { |
| 2153 | res = PyObject_Call(call, arg, kw); |
| 2154 | Py_LeaveRecursiveCall(); |
| 2155 | } |
| 2156 | Py_DECREF(call); |
| 2157 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2158 | } |
| 2159 | |
Guido van Rossum | 59d1d2b | 2001-04-20 19:13:02 +0000 | [diff] [blame] | 2160 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2161 | static PyNumberMethods instance_as_number = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2162 | instance_add, /* nb_add */ |
| 2163 | instance_sub, /* nb_subtract */ |
| 2164 | instance_mul, /* nb_multiply */ |
| 2165 | instance_div, /* nb_divide */ |
| 2166 | instance_mod, /* nb_remainder */ |
| 2167 | instance_divmod, /* nb_divmod */ |
| 2168 | instance_pow, /* nb_power */ |
| 2169 | (unaryfunc)instance_neg, /* nb_negative */ |
| 2170 | (unaryfunc)instance_pos, /* nb_positive */ |
| 2171 | (unaryfunc)instance_abs, /* nb_absolute */ |
| 2172 | (inquiry)instance_nonzero, /* nb_nonzero */ |
| 2173 | (unaryfunc)instance_invert, /* nb_invert */ |
| 2174 | instance_lshift, /* nb_lshift */ |
| 2175 | instance_rshift, /* nb_rshift */ |
| 2176 | instance_and, /* nb_and */ |
| 2177 | instance_xor, /* nb_xor */ |
| 2178 | instance_or, /* nb_or */ |
| 2179 | instance_coerce, /* nb_coerce */ |
| 2180 | (unaryfunc)instance_int, /* nb_int */ |
| 2181 | (unaryfunc)instance_long, /* nb_long */ |
| 2182 | (unaryfunc)instance_float, /* nb_float */ |
| 2183 | (unaryfunc)instance_oct, /* nb_oct */ |
| 2184 | (unaryfunc)instance_hex, /* nb_hex */ |
| 2185 | instance_iadd, /* nb_inplace_add */ |
| 2186 | instance_isub, /* nb_inplace_subtract */ |
| 2187 | instance_imul, /* nb_inplace_multiply */ |
| 2188 | instance_idiv, /* nb_inplace_divide */ |
| 2189 | instance_imod, /* nb_inplace_remainder */ |
| 2190 | instance_ipow, /* nb_inplace_power */ |
| 2191 | instance_ilshift, /* nb_inplace_lshift */ |
| 2192 | instance_irshift, /* nb_inplace_rshift */ |
| 2193 | instance_iand, /* nb_inplace_and */ |
| 2194 | instance_ixor, /* nb_inplace_xor */ |
| 2195 | instance_ior, /* nb_inplace_or */ |
| 2196 | instance_floordiv, /* nb_floor_divide */ |
| 2197 | instance_truediv, /* nb_true_divide */ |
| 2198 | instance_ifloordiv, /* nb_inplace_floor_divide */ |
| 2199 | instance_itruediv, /* nb_inplace_true_divide */ |
| 2200 | (unaryfunc)instance_index, /* nb_index */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 2201 | }; |
| 2202 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2203 | PyTypeObject PyInstance_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2204 | PyObject_HEAD_INIT(&PyType_Type) |
| 2205 | 0, |
| 2206 | "instance", |
| 2207 | sizeof(PyInstanceObject), |
| 2208 | 0, |
| 2209 | (destructor)instance_dealloc, /* tp_dealloc */ |
| 2210 | 0, /* tp_print */ |
| 2211 | 0, /* tp_getattr */ |
| 2212 | 0, /* tp_setattr */ |
| 2213 | instance_compare, /* tp_compare */ |
| 2214 | (reprfunc)instance_repr, /* tp_repr */ |
| 2215 | &instance_as_number, /* tp_as_number */ |
| 2216 | &instance_as_sequence, /* tp_as_sequence */ |
| 2217 | &instance_as_mapping, /* tp_as_mapping */ |
| 2218 | (hashfunc)instance_hash, /* tp_hash */ |
| 2219 | instance_call, /* tp_call */ |
| 2220 | (reprfunc)instance_str, /* tp_str */ |
| 2221 | (getattrofunc)instance_getattr, /* tp_getattro */ |
| 2222 | (setattrofunc)instance_setattr, /* tp_setattro */ |
| 2223 | 0, /* tp_as_buffer */ |
| 2224 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_CHECKTYPES,/*tp_flags*/ |
| 2225 | instance_doc, /* tp_doc */ |
| 2226 | (traverseproc)instance_traverse, /* tp_traverse */ |
| 2227 | 0, /* tp_clear */ |
| 2228 | instance_richcompare, /* tp_richcompare */ |
| 2229 | offsetof(PyInstanceObject, in_weakreflist), /* tp_weaklistoffset */ |
| 2230 | (getiterfunc)instance_getiter, /* tp_iter */ |
| 2231 | (iternextfunc)instance_iternext, /* tp_iternext */ |
| 2232 | 0, /* tp_methods */ |
| 2233 | 0, /* tp_members */ |
| 2234 | 0, /* tp_getset */ |
| 2235 | 0, /* tp_base */ |
| 2236 | 0, /* tp_dict */ |
| 2237 | 0, /* tp_descr_get */ |
| 2238 | 0, /* tp_descr_set */ |
| 2239 | 0, /* tp_dictoffset */ |
| 2240 | 0, /* tp_init */ |
| 2241 | 0, /* tp_alloc */ |
| 2242 | instance_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2243 | }; |
| 2244 | |
| 2245 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 2246 | /* Instance method objects are used for two purposes: |
| 2247 | (a) as bound instance methods (returned by instancename.methodname) |
| 2248 | (b) as unbound methods (returned by ClassName.methodname) |
| 2249 | In case (b), im_self is NULL |
| 2250 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2251 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2252 | PyObject * |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2253 | PyMethod_New(PyObject *func, PyObject *self, PyObject *klass) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2254 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2255 | register PyMethodObject *im; |
| 2256 | im = free_list; |
| 2257 | if (im != NULL) { |
| 2258 | free_list = (PyMethodObject *)(im->im_self); |
| 2259 | PyObject_INIT(im, &PyMethod_Type); |
| 2260 | numfree--; |
| 2261 | } |
| 2262 | else { |
| 2263 | im = PyObject_GC_New(PyMethodObject, &PyMethod_Type); |
| 2264 | if (im == NULL) |
| 2265 | return NULL; |
| 2266 | } |
| 2267 | im->im_weakreflist = NULL; |
| 2268 | Py_INCREF(func); |
| 2269 | im->im_func = func; |
| 2270 | Py_XINCREF(self); |
| 2271 | im->im_self = self; |
| 2272 | Py_XINCREF(klass); |
| 2273 | im->im_class = klass; |
| 2274 | _PyObject_GC_TRACK(im); |
| 2275 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2276 | } |
| 2277 | |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2278 | /* Descriptors for PyMethod attributes */ |
| 2279 | |
| 2280 | /* 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] | 2281 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2282 | #define OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2283 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 2284 | static PyMemberDef instancemethod_memberlist[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2285 | {"im_class", T_OBJECT, OFF(im_class), READONLY|RESTRICTED, |
| 2286 | "the class associated with a method"}, |
| 2287 | {"im_func", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 2288 | "the function (or other callable) implementing a method"}, |
| 2289 | {"__func__", T_OBJECT, OFF(im_func), READONLY|RESTRICTED, |
| 2290 | "the function (or other callable) implementing a method"}, |
| 2291 | {"im_self", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2292 | "the instance to which a method is bound; None for unbound methods"}, |
| 2293 | {"__self__", T_OBJECT, OFF(im_self), READONLY|RESTRICTED, |
| 2294 | "the instance to which a method is bound; None for unbound methods"}, |
| 2295 | {NULL} /* Sentinel */ |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2296 | }; |
| 2297 | |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2298 | /* Christian Tismer argued convincingly that method attributes should |
| 2299 | (nearly) always override function attributes. |
| 2300 | The one exception is __doc__; there's a default __doc__ which |
| 2301 | should only be used for the class, not for instances */ |
| 2302 | |
| 2303 | static PyObject * |
| 2304 | instancemethod_get_doc(PyMethodObject *im, void *context) |
| 2305 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2306 | static PyObject *docstr; |
| 2307 | if (docstr == NULL) { |
| 2308 | docstr= PyString_InternFromString("__doc__"); |
| 2309 | if (docstr == NULL) |
| 2310 | return NULL; |
| 2311 | } |
| 2312 | return PyObject_GetAttr(im->im_func, docstr); |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2313 | } |
| 2314 | |
| 2315 | static PyGetSetDef instancemethod_getset[] = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2316 | {"__doc__", (getter)instancemethod_get_doc, NULL, NULL}, |
| 2317 | {0} |
Guido van Rossum | baf0f8f | 2003-11-22 23:55:50 +0000 | [diff] [blame] | 2318 | }; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2319 | |
| 2320 | static PyObject * |
| 2321 | instancemethod_getattro(PyObject *obj, PyObject *name) |
| 2322 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2323 | PyMethodObject *im = (PyMethodObject *)obj; |
| 2324 | PyTypeObject *tp = obj->ob_type; |
| 2325 | PyObject *descr = NULL; |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2326 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2327 | if (PyType_HasFeature(tp, Py_TPFLAGS_HAVE_CLASS)) { |
| 2328 | if (tp->tp_dict == NULL) { |
| 2329 | if (PyType_Ready(tp) < 0) |
| 2330 | return NULL; |
| 2331 | } |
| 2332 | descr = _PyType_Lookup(tp, name); |
| 2333 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2334 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2335 | if (descr != NULL) { |
| 2336 | descrgetfunc f = TP_DESCR_GET(descr->ob_type); |
| 2337 | if (f != NULL) |
| 2338 | return f(descr, obj, (PyObject *)obj->ob_type); |
| 2339 | else { |
| 2340 | Py_INCREF(descr); |
| 2341 | return descr; |
| 2342 | } |
| 2343 | } |
Guido van Rossum | f0b35e1 | 2001-09-18 03:53:24 +0000 | [diff] [blame] | 2344 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2345 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 2346 | } |
| 2347 | |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2348 | PyDoc_STRVAR(instancemethod_doc, |
| 2349 | "instancemethod(function, instance, class)\n\ |
| 2350 | \n\ |
| 2351 | Create an instance method object."); |
| 2352 | |
| 2353 | static PyObject * |
| 2354 | instancemethod_new(PyTypeObject* type, PyObject* args, PyObject *kw) |
| 2355 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2356 | PyObject *func; |
| 2357 | PyObject *self; |
| 2358 | PyObject *classObj = NULL; |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2359 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2360 | if (!_PyArg_NoKeywords("instancemethod", kw)) |
| 2361 | return NULL; |
| 2362 | if (!PyArg_UnpackTuple(args, "instancemethod", 2, 3, |
| 2363 | &func, &self, &classObj)) |
| 2364 | return NULL; |
| 2365 | if (!PyCallable_Check(func)) { |
| 2366 | PyErr_SetString(PyExc_TypeError, |
| 2367 | "first argument must be callable"); |
| 2368 | return NULL; |
| 2369 | } |
| 2370 | if (self == Py_None) |
| 2371 | self = NULL; |
| 2372 | if (self == NULL && classObj == NULL) { |
| 2373 | PyErr_SetString(PyExc_TypeError, |
| 2374 | "unbound methods must have non-NULL im_class"); |
| 2375 | return NULL; |
| 2376 | } |
Michael W. Hudson | e2749cb | 2005-03-30 16:32:10 +0000 | [diff] [blame] | 2377 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2378 | return PyMethod_New(func, self, classObj); |
Guido van Rossum | bea18cc | 2002-06-14 20:41:17 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2381 | static void |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2382 | instancemethod_dealloc(register PyMethodObject *im) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2383 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2384 | _PyObject_GC_UNTRACK(im); |
| 2385 | if (im->im_weakreflist != NULL) |
| 2386 | PyObject_ClearWeakRefs((PyObject *)im); |
| 2387 | Py_DECREF(im->im_func); |
| 2388 | Py_XDECREF(im->im_self); |
| 2389 | Py_XDECREF(im->im_class); |
| 2390 | if (numfree < PyMethod_MAXFREELIST) { |
| 2391 | im->im_self = (PyObject *)free_list; |
| 2392 | free_list = im; |
| 2393 | numfree++; |
| 2394 | } |
| 2395 | else { |
| 2396 | PyObject_GC_Del(im); |
| 2397 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2398 | } |
| 2399 | |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2400 | static int |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2401 | instancemethod_compare(PyMethodObject *a, PyMethodObject *b) |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2402 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2403 | int cmp; |
| 2404 | cmp = PyObject_Compare(a->im_func, b->im_func); |
| 2405 | if (cmp) |
| 2406 | return cmp; |
Armin Rigo | fd01d79 | 2006-06-08 10:56:24 +0000 | [diff] [blame] | 2407 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2408 | if (a->im_self == b->im_self) |
| 2409 | return 0; |
| 2410 | if (a->im_self == NULL || b->im_self == NULL) |
| 2411 | return (a->im_self < b->im_self) ? -1 : 1; |
| 2412 | else |
| 2413 | return PyObject_Compare(a->im_self, b->im_self); |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 2414 | } |
| 2415 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2416 | static PyObject * |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2417 | instancemethod_repr(PyMethodObject *a) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2418 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2419 | PyObject *self = a->im_self; |
| 2420 | PyObject *func = a->im_func; |
| 2421 | PyObject *klass = a->im_class; |
| 2422 | PyObject *funcname = NULL, *klassname = NULL, *result = NULL; |
| 2423 | char *sfuncname = "?", *sklassname = "?"; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2424 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2425 | funcname = PyObject_GetAttrString(func, "__name__"); |
| 2426 | if (funcname == NULL) { |
| 2427 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2428 | return NULL; |
| 2429 | PyErr_Clear(); |
| 2430 | } |
| 2431 | else if (!PyString_Check(funcname)) { |
| 2432 | Py_DECREF(funcname); |
| 2433 | funcname = NULL; |
| 2434 | } |
| 2435 | else |
| 2436 | sfuncname = PyString_AS_STRING(funcname); |
| 2437 | if (klass == NULL) |
| 2438 | klassname = NULL; |
| 2439 | else { |
| 2440 | klassname = PyObject_GetAttrString(klass, "__name__"); |
| 2441 | if (klassname == NULL) { |
| 2442 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 2443 | return NULL; |
| 2444 | PyErr_Clear(); |
| 2445 | } |
| 2446 | else if (!PyString_Check(klassname)) { |
| 2447 | Py_DECREF(klassname); |
| 2448 | klassname = NULL; |
| 2449 | } |
| 2450 | else |
| 2451 | sklassname = PyString_AS_STRING(klassname); |
| 2452 | } |
| 2453 | if (self == NULL) |
| 2454 | result = PyString_FromFormat("<unbound method %s.%s>", |
| 2455 | sklassname, sfuncname); |
| 2456 | else { |
| 2457 | /* XXX Shouldn't use repr() here! */ |
| 2458 | PyObject *selfrepr = PyObject_Repr(self); |
| 2459 | if (selfrepr == NULL) |
| 2460 | goto fail; |
| 2461 | if (!PyString_Check(selfrepr)) { |
| 2462 | Py_DECREF(selfrepr); |
| 2463 | goto fail; |
| 2464 | } |
| 2465 | result = PyString_FromFormat("<bound method %s.%s of %s>", |
| 2466 | sklassname, sfuncname, |
| 2467 | PyString_AS_STRING(selfrepr)); |
| 2468 | Py_DECREF(selfrepr); |
| 2469 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2470 | fail: |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2471 | Py_XDECREF(funcname); |
| 2472 | Py_XDECREF(klassname); |
| 2473 | return result; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 2474 | } |
| 2475 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2476 | static long |
Fred Drake | 7991247 | 2000-07-09 04:06:11 +0000 | [diff] [blame] | 2477 | instancemethod_hash(PyMethodObject *a) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2478 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2479 | long x, y; |
| 2480 | if (a->im_self == NULL) |
| 2481 | x = PyObject_Hash(Py_None); |
| 2482 | else |
| 2483 | x = PyObject_Hash(a->im_self); |
| 2484 | if (x == -1) |
| 2485 | return -1; |
| 2486 | y = PyObject_Hash(a->im_func); |
| 2487 | if (y == -1) |
| 2488 | return -1; |
| 2489 | x = x ^ y; |
| 2490 | if (x == -1) |
| 2491 | x = -2; |
| 2492 | return x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2495 | static int |
| 2496 | instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) |
| 2497 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2498 | Py_VISIT(im->im_func); |
| 2499 | Py_VISIT(im->im_self); |
| 2500 | Py_VISIT(im->im_class); |
| 2501 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 2502 | } |
| 2503 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2504 | static void |
Anthony Baxter | 377be11 | 2006-04-11 06:54:30 +0000 | [diff] [blame] | 2505 | getclassname(PyObject *klass, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2506 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2507 | PyObject *name; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2508 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2509 | assert(bufsize > 1); |
| 2510 | strcpy(buf, "?"); /* Default outcome */ |
| 2511 | if (klass == NULL) |
| 2512 | return; |
| 2513 | name = PyObject_GetAttrString(klass, "__name__"); |
| 2514 | if (name == NULL) { |
| 2515 | /* This function cannot return an exception */ |
| 2516 | PyErr_Clear(); |
| 2517 | return; |
| 2518 | } |
| 2519 | if (PyString_Check(name)) { |
| 2520 | strncpy(buf, PyString_AS_STRING(name), bufsize); |
| 2521 | buf[bufsize-1] = '\0'; |
| 2522 | } |
| 2523 | Py_DECREF(name); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2524 | } |
| 2525 | |
Guido van Rossum | 45ec02a | 2002-08-19 21:43:18 +0000 | [diff] [blame] | 2526 | static void |
| 2527 | getinstclassname(PyObject *inst, char *buf, int bufsize) |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2528 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2529 | PyObject *klass; |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2530 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2531 | if (inst == NULL) { |
| 2532 | assert(bufsize > 0 && (size_t)bufsize > strlen("nothing")); |
| 2533 | strcpy(buf, "nothing"); |
| 2534 | return; |
| 2535 | } |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2536 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2537 | klass = PyObject_GetAttrString(inst, "__class__"); |
| 2538 | if (klass == NULL) { |
| 2539 | /* This function cannot return an exception */ |
| 2540 | PyErr_Clear(); |
| 2541 | klass = (PyObject *)(inst->ob_type); |
| 2542 | Py_INCREF(klass); |
| 2543 | } |
| 2544 | getclassname(klass, buf, bufsize); |
| 2545 | Py_XDECREF(klass); |
Guido van Rossum | a15dece | 2001-08-24 18:48:27 +0000 | [diff] [blame] | 2546 | } |
| 2547 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2548 | static PyObject * |
| 2549 | instancemethod_call(PyObject *func, PyObject *arg, PyObject *kw) |
| 2550 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2551 | PyObject *self = PyMethod_GET_SELF(func); |
| 2552 | PyObject *klass = PyMethod_GET_CLASS(func); |
| 2553 | PyObject *result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2554 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2555 | func = PyMethod_GET_FUNCTION(func); |
| 2556 | if (self == NULL) { |
| 2557 | /* Unbound methods must be called with an instance of |
| 2558 | the class (or a derived class) as first argument */ |
| 2559 | int ok; |
| 2560 | if (PyTuple_Size(arg) >= 1) |
| 2561 | self = PyTuple_GET_ITEM(arg, 0); |
| 2562 | if (self == NULL) |
| 2563 | ok = 0; |
| 2564 | else { |
| 2565 | ok = PyObject_IsInstance(self, klass); |
| 2566 | if (ok < 0) |
| 2567 | return NULL; |
| 2568 | } |
| 2569 | if (!ok) { |
| 2570 | char clsbuf[256]; |
| 2571 | char instbuf[256]; |
| 2572 | getclassname(klass, clsbuf, sizeof(clsbuf)); |
| 2573 | getinstclassname(self, instbuf, sizeof(instbuf)); |
| 2574 | PyErr_Format(PyExc_TypeError, |
| 2575 | "unbound method %s%s must be called with " |
| 2576 | "%s instance as first argument " |
| 2577 | "(got %s%s instead)", |
| 2578 | PyEval_GetFuncName(func), |
| 2579 | PyEval_GetFuncDesc(func), |
| 2580 | clsbuf, |
| 2581 | instbuf, |
| 2582 | self == NULL ? "" : " instance"); |
| 2583 | return NULL; |
| 2584 | } |
| 2585 | Py_INCREF(arg); |
| 2586 | } |
| 2587 | else { |
| 2588 | Py_ssize_t argcount = PyTuple_Size(arg); |
| 2589 | PyObject *newarg = PyTuple_New(argcount + 1); |
| 2590 | int i; |
| 2591 | if (newarg == NULL) |
| 2592 | return NULL; |
| 2593 | Py_INCREF(self); |
| 2594 | PyTuple_SET_ITEM(newarg, 0, self); |
| 2595 | for (i = 0; i < argcount; i++) { |
| 2596 | PyObject *v = PyTuple_GET_ITEM(arg, i); |
| 2597 | Py_XINCREF(v); |
| 2598 | PyTuple_SET_ITEM(newarg, i+1, v); |
| 2599 | } |
| 2600 | arg = newarg; |
| 2601 | } |
| 2602 | result = PyObject_Call((PyObject *)func, arg, kw); |
| 2603 | Py_DECREF(arg); |
| 2604 | return result; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2605 | } |
| 2606 | |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2607 | static PyObject * |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2608 | instancemethod_descr_get(PyObject *meth, PyObject *obj, PyObject *cls) |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2609 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2610 | /* Don't rebind an already bound method, or an unbound method |
| 2611 | of a class that's not a base class of cls. */ |
Guido van Rossum | 6bae46d | 2003-02-11 18:43:00 +0000 | [diff] [blame] | 2612 | |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2613 | if (PyMethod_GET_SELF(meth) != NULL) { |
| 2614 | /* Already bound */ |
| 2615 | Py_INCREF(meth); |
| 2616 | return meth; |
| 2617 | } |
| 2618 | /* No, it is an unbound method */ |
| 2619 | if (PyMethod_GET_CLASS(meth) != NULL && cls != NULL) { |
| 2620 | /* Do subclass test. If it fails, return meth unchanged. */ |
| 2621 | int ok = PyObject_IsSubclass(cls, PyMethod_GET_CLASS(meth)); |
| 2622 | if (ok < 0) |
| 2623 | return NULL; |
| 2624 | if (!ok) { |
| 2625 | Py_INCREF(meth); |
| 2626 | return meth; |
| 2627 | } |
| 2628 | } |
| 2629 | /* Bind it to obj */ |
| 2630 | return PyMethod_New(PyMethod_GET_FUNCTION(meth), obj, cls); |
Guido van Rossum | 23cc2b4 | 2001-08-15 17:52:31 +0000 | [diff] [blame] | 2631 | } |
| 2632 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 2633 | PyTypeObject PyMethod_Type = { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2634 | PyObject_HEAD_INIT(&PyType_Type) |
| 2635 | 0, |
| 2636 | "instancemethod", |
| 2637 | sizeof(PyMethodObject), |
| 2638 | 0, |
| 2639 | (destructor)instancemethod_dealloc, /* tp_dealloc */ |
| 2640 | 0, /* tp_print */ |
| 2641 | 0, /* tp_getattr */ |
| 2642 | 0, /* tp_setattr */ |
| 2643 | (cmpfunc)instancemethod_compare, /* tp_compare */ |
| 2644 | (reprfunc)instancemethod_repr, /* tp_repr */ |
| 2645 | 0, /* tp_as_number */ |
| 2646 | 0, /* tp_as_sequence */ |
| 2647 | 0, /* tp_as_mapping */ |
| 2648 | (hashfunc)instancemethod_hash, /* tp_hash */ |
| 2649 | instancemethod_call, /* tp_call */ |
| 2650 | 0, /* tp_str */ |
| 2651 | instancemethod_getattro, /* tp_getattro */ |
| 2652 | PyObject_GenericSetAttr, /* tp_setattro */ |
| 2653 | 0, /* tp_as_buffer */ |
| 2654 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | Py_TPFLAGS_HAVE_WEAKREFS, /* tp_flags */ |
| 2655 | instancemethod_doc, /* tp_doc */ |
| 2656 | (traverseproc)instancemethod_traverse, /* tp_traverse */ |
| 2657 | 0, /* tp_clear */ |
| 2658 | 0, /* tp_richcompare */ |
| 2659 | offsetof(PyMethodObject, im_weakreflist), /* tp_weaklistoffset */ |
| 2660 | 0, /* tp_iter */ |
| 2661 | 0, /* tp_iternext */ |
| 2662 | 0, /* tp_methods */ |
| 2663 | instancemethod_memberlist, /* tp_members */ |
| 2664 | instancemethod_getset, /* tp_getset */ |
| 2665 | 0, /* tp_base */ |
| 2666 | 0, /* tp_dict */ |
| 2667 | instancemethod_descr_get, /* tp_descr_get */ |
| 2668 | 0, /* tp_descr_set */ |
| 2669 | 0, /* tp_dictoffset */ |
| 2670 | 0, /* tp_init */ |
| 2671 | 0, /* tp_alloc */ |
| 2672 | instancemethod_new, /* tp_new */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2673 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2674 | |
| 2675 | /* Clear out the free list */ |
| 2676 | |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2677 | int |
| 2678 | PyMethod_ClearFreeList(void) |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2679 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2680 | int freelist_size = numfree; |
| 2681 | |
| 2682 | while (free_list) { |
| 2683 | PyMethodObject *im = free_list; |
| 2684 | free_list = (PyMethodObject *)(im->im_self); |
| 2685 | PyObject_GC_Del(im); |
| 2686 | numfree--; |
| 2687 | } |
| 2688 | assert(numfree == 0); |
| 2689 | return freelist_size; |
Christian Heimes | 3b718a7 | 2008-02-14 12:47:33 +0000 | [diff] [blame] | 2690 | } |
| 2691 | |
| 2692 | void |
| 2693 | PyMethod_Fini(void) |
| 2694 | { |
Antoine Pitrou | c83ea13 | 2010-05-09 14:46:46 +0000 | [diff] [blame] | 2695 | (void)PyMethod_ClearFreeList(); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 2696 | } |