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