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