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