Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 4 | |
| 5 | All Rights Reserved |
| 6 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 7 | Permission to use, copy, modify, and distribute this software and its |
| 8 | documentation for any purpose and without fee is hereby granted, |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 9 | provided that the above copyright notice appear in all copies and that |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 10 | both that copyright notice and this permission notice appear in |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 11 | supporting documentation, and that the names of Stichting Mathematisch |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 12 | Centrum or CWI or Corporation for National Research Initiatives or |
| 13 | CNRI not be used in advertising or publicity pertaining to |
| 14 | distribution of the software without specific, written prior |
| 15 | permission. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 16 | |
Guido van Rossum | d266eb4 | 1996-10-25 14:44:06 +0000 | [diff] [blame] | 17 | While CWI is the initial source for this software, a modified version |
| 18 | is made available by the Corporation for National Research Initiatives |
| 19 | (CNRI) at the Internet address ftp://ftp.python.org. |
| 20 | |
| 21 | STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
| 22 | REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
| 23 | MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
| 24 | CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
| 25 | DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
| 26 | PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
| 27 | TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
| 28 | PERFORMANCE OF THIS SOFTWARE. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 29 | |
| 30 | ******************************************************************/ |
| 31 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 32 | /* Class object implementation */ |
| 33 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 34 | #include "Python.h" |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 35 | #include "structmember.h" |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 36 | |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 37 | /* Forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 38 | static PyObject *class_lookup |
| 39 | Py_PROTO((PyClassObject *, PyObject *, PyClassObject **)); |
| 40 | static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *)); |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 41 | static PyObject *instance_getattr2 Py_PROTO((PyInstanceObject *, PyObject *)); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 42 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 43 | static PyObject *getattrstr, *setattrstr, *delattrstr; |
| 44 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 45 | PyObject * |
| 46 | PyClass_New(bases, dict, name) |
| 47 | PyObject *bases; /* NULL or tuple of classobjects! */ |
| 48 | PyObject *dict; |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 49 | PyObject *name; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 50 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 51 | PyClassObject *op, *dummy; |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 52 | static PyObject *docstr, *modstr, *namestr; |
Guido van Rossum | 019f424 | 1996-08-21 14:54:28 +0000 | [diff] [blame] | 53 | if (docstr == NULL) { |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 54 | docstr= PyString_InternFromString("__doc__"); |
Guido van Rossum | 019f424 | 1996-08-21 14:54:28 +0000 | [diff] [blame] | 55 | if (docstr == NULL) |
| 56 | return NULL; |
| 57 | } |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 58 | if (modstr == NULL) { |
| 59 | modstr= PyString_InternFromString("__module__"); |
| 60 | if (modstr == NULL) |
| 61 | return NULL; |
| 62 | } |
| 63 | if (namestr == NULL) { |
| 64 | namestr= PyString_InternFromString("__name__"); |
| 65 | if (namestr == NULL) |
| 66 | return NULL; |
| 67 | } |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 68 | if (name == NULL || !PyString_Check(name)) { |
| 69 | PyErr_SetString(PyExc_SystemError, |
| 70 | "PyClass_New: name must be a string"); |
| 71 | return NULL; |
| 72 | } |
| 73 | if (dict == NULL || !PyDict_Check(dict)) { |
| 74 | PyErr_SetString(PyExc_SystemError, |
| 75 | "PyClass_New: dict must be a dictionary"); |
| 76 | return NULL; |
| 77 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 78 | if (PyDict_GetItem(dict, docstr) == NULL) { |
| 79 | if (PyDict_SetItem(dict, docstr, Py_None) < 0) |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 80 | return NULL; |
| 81 | } |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 82 | if (PyDict_GetItem(dict, modstr) == NULL) { |
| 83 | PyObject *globals = PyEval_GetGlobals(); |
| 84 | if (globals != NULL) { |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 85 | PyObject *modname = PyDict_GetItem(globals, namestr); |
| 86 | if (modname != NULL) { |
| 87 | if (PyDict_SetItem(dict, modstr, modname) < 0) |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 88 | return NULL; |
| 89 | } |
| 90 | } |
| 91 | } |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 92 | if (bases == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 93 | bases = PyTuple_New(0); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 94 | if (bases == NULL) |
Guido van Rossum | e537240 | 1993-03-16 12:15:04 +0000 | [diff] [blame] | 95 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 96 | } |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 97 | else { |
| 98 | int i; |
| 99 | if (!PyTuple_Check(bases)) { |
| 100 | PyErr_SetString(PyExc_SystemError, |
| 101 | "PyClass_New: bases must be a tuple"); |
| 102 | return NULL; |
| 103 | } |
| 104 | i = PyTuple_Size(bases); |
| 105 | while (--i >= 0) { |
| 106 | if (!PyClass_Check(PyTuple_GetItem(bases, i))) { |
| 107 | PyErr_SetString(PyExc_SystemError, |
| 108 | "PyClass_New: base must be a class"); |
| 109 | return NULL; |
| 110 | } |
| 111 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 112 | Py_INCREF(bases); |
Guido van Rossum | 04d73c4 | 1997-10-07 14:54:11 +0000 | [diff] [blame] | 113 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 114 | op = PyObject_NEW(PyClassObject, &PyClass_Type); |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 115 | if (op == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 116 | Py_DECREF(bases); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 117 | return NULL; |
Guido van Rossum | e2966a6 | 1991-12-10 13:53:23 +0000 | [diff] [blame] | 118 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 119 | op->cl_bases = bases; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 120 | Py_INCREF(dict); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 121 | op->cl_dict = dict; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 122 | Py_XINCREF(name); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 123 | op->cl_name = name; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 124 | if (getattrstr == NULL) { |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 125 | getattrstr = PyString_InternFromString("__getattr__"); |
| 126 | setattrstr = PyString_InternFromString("__setattr__"); |
| 127 | delattrstr = PyString_InternFromString("__delattr__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 128 | } |
| 129 | op->cl_getattr = class_lookup(op, getattrstr, &dummy); |
| 130 | op->cl_setattr = class_lookup(op, setattrstr, &dummy); |
| 131 | op->cl_delattr = class_lookup(op, delattrstr, &dummy); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 132 | Py_XINCREF(op->cl_getattr); |
| 133 | Py_XINCREF(op->cl_setattr); |
| 134 | Py_XINCREF(op->cl_delattr); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 135 | PyObject_GC_Init(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 136 | return (PyObject *) op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | /* Class methods */ |
| 140 | |
| 141 | static void |
| 142 | class_dealloc(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 143 | PyClassObject *op; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 144 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 145 | PyObject_GC_Fini(op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 146 | Py_DECREF(op->cl_bases); |
| 147 | Py_DECREF(op->cl_dict); |
| 148 | Py_XDECREF(op->cl_name); |
Guido van Rossum | 152d817 | 1998-08-04 14:59:16 +0000 | [diff] [blame] | 149 | Py_XDECREF(op->cl_getattr); |
| 150 | Py_XDECREF(op->cl_setattr); |
| 151 | Py_XDECREF(op->cl_delattr); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 152 | PyObject_DEL(op); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 153 | } |
| 154 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 155 | static PyObject * |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 156 | class_lookup(cp, name, pclass) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 157 | PyClassObject *cp; |
| 158 | PyObject *name; |
| 159 | PyClassObject **pclass; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 160 | { |
| 161 | int i, n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 162 | PyObject *value = PyDict_GetItem(cp->cl_dict, name); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 163 | if (value != NULL) { |
| 164 | *pclass = cp; |
| 165 | return value; |
| 166 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 167 | n = PyTuple_Size(cp->cl_bases); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 168 | for (i = 0; i < n; i++) { |
Guido van Rossum | 7cc56eb | 1997-09-12 20:04:46 +0000 | [diff] [blame] | 169 | /* XXX What if one of the bases is not a class? */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 170 | PyObject *v = class_lookup( |
| 171 | (PyClassObject *) |
| 172 | PyTuple_GetItem(cp->cl_bases, i), name, pclass); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 173 | if (v != NULL) |
| 174 | return v; |
| 175 | } |
| 176 | return NULL; |
| 177 | } |
| 178 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 179 | static PyObject * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 180 | class_getattr(op, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 181 | register PyClassObject *op; |
| 182 | PyObject *name; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 183 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 184 | register PyObject *v; |
| 185 | register char *sname = PyString_AsString(name); |
| 186 | PyClassObject *class; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 187 | if (sname[0] == '_' && sname[1] == '_') { |
| 188 | if (strcmp(sname, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 189 | if (PyEval_GetRestricted()) { |
| 190 | PyErr_SetString(PyExc_RuntimeError, |
| 191 | "class.__dict__ not accessible in restricted mode"); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 192 | return NULL; |
| 193 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 194 | Py_INCREF(op->cl_dict); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 195 | return op->cl_dict; |
| 196 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 197 | if (strcmp(sname, "__bases__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 198 | Py_INCREF(op->cl_bases); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 199 | return op->cl_bases; |
| 200 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 201 | if (strcmp(sname, "__name__") == 0) { |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 202 | if (op->cl_name == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 203 | v = Py_None; |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 204 | else |
| 205 | v = op->cl_name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 206 | Py_INCREF(v); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 207 | return v; |
| 208 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 209 | } |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 210 | v = class_lookup(op, name, &class); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 211 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 212 | PyErr_SetObject(PyExc_AttributeError, name); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 213 | return NULL; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 214 | } |
Guido van Rossum | 0add15f | 1997-05-09 01:07:15 +0000 | [diff] [blame] | 215 | Py_INCREF(v); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 216 | if (PyFunction_Check(v)) { |
| 217 | PyObject *w = PyMethod_New(v, (PyObject *)NULL, |
| 218 | (PyObject *)class); |
| 219 | Py_DECREF(v); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 220 | v = w; |
| 221 | } |
| 222 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 223 | } |
| 224 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 225 | static void |
| 226 | set_slot(slot, v) |
| 227 | PyObject **slot; |
| 228 | PyObject *v; |
| 229 | { |
| 230 | PyObject *temp = *slot; |
| 231 | Py_XINCREF(v); |
| 232 | *slot = v; |
| 233 | Py_XDECREF(temp); |
| 234 | } |
| 235 | |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 236 | static void |
| 237 | set_attr_slots(c) |
| 238 | PyClassObject *c; |
| 239 | { |
| 240 | PyClassObject *dummy; |
| 241 | |
| 242 | set_slot(&c->cl_getattr, class_lookup(c, getattrstr, &dummy)); |
| 243 | set_slot(&c->cl_setattr, class_lookup(c, setattrstr, &dummy)); |
| 244 | set_slot(&c->cl_delattr, class_lookup(c, delattrstr, &dummy)); |
| 245 | } |
| 246 | |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 247 | static char * |
| 248 | set_dict(c, v) |
| 249 | PyClassObject *c; |
| 250 | PyObject *v; |
| 251 | { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 252 | if (v == NULL || !PyDict_Check(v)) |
| 253 | return "__dict__ must be a dictionary object"; |
| 254 | set_slot(&c->cl_dict, v); |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 255 | set_attr_slots(c); |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 256 | return ""; |
| 257 | } |
| 258 | |
| 259 | static char * |
| 260 | set_bases(c, v) |
| 261 | PyClassObject *c; |
| 262 | PyObject *v; |
| 263 | { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 264 | int i, n; |
| 265 | |
| 266 | if (v == NULL || !PyTuple_Check(v)) |
| 267 | return "__bases__ must be a tuple object"; |
| 268 | n = PyTuple_Size(v); |
| 269 | for (i = 0; i < n; i++) { |
| 270 | PyObject *x = PyTuple_GET_ITEM(v, i); |
| 271 | if (!PyClass_Check(x)) |
| 272 | return "__bases__ items must be classes"; |
| 273 | if (PyClass_IsSubclass(x, (PyObject *)c)) |
| 274 | return "a __bases__ item causes an inheritance cycle"; |
| 275 | } |
| 276 | set_slot(&c->cl_bases, v); |
Guido van Rossum | 7ba3043 | 1998-07-08 13:34:48 +0000 | [diff] [blame] | 277 | set_attr_slots(c); |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 278 | return ""; |
| 279 | } |
| 280 | |
| 281 | static char * |
| 282 | set_name(c, v) |
| 283 | PyClassObject *c; |
| 284 | PyObject *v; |
| 285 | { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 286 | if (v == NULL || !PyString_Check(v)) |
| 287 | return "__name__ must be a string object"; |
Guido van Rossum | ad89bbc | 2000-06-28 21:57:18 +0000 | [diff] [blame] | 288 | 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] | 289 | return "__name__ must not contain null bytes"; |
| 290 | set_slot(&c->cl_name, v); |
| 291 | return ""; |
| 292 | } |
| 293 | |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 294 | static int |
| 295 | class_setattr(op, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 296 | PyClassObject *op; |
| 297 | PyObject *name; |
| 298 | PyObject *v; |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 299 | { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 300 | char *sname; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 301 | if (PyEval_GetRestricted()) { |
| 302 | PyErr_SetString(PyExc_RuntimeError, |
Guido van Rossum | ac21f6a | 1995-08-04 04:05:31 +0000 | [diff] [blame] | 303 | "classes are read-only in restricted mode"); |
| 304 | return -1; |
| 305 | } |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 306 | sname = PyString_AsString(name); |
| 307 | if (sname[0] == '_' && sname[1] == '_') { |
| 308 | int n = PyString_Size(name); |
| 309 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
Guido van Rossum | a63eff6 | 1998-05-29 21:37:21 +0000 | [diff] [blame] | 310 | char *err = NULL; |
| 311 | if (strcmp(sname, "__dict__") == 0) |
| 312 | err = set_dict(op, v); |
| 313 | else if (strcmp(sname, "__bases__") == 0) |
| 314 | err = set_bases(op, v); |
| 315 | else if (strcmp(sname, "__name__") == 0) |
| 316 | err = set_name(op, v); |
| 317 | else if (strcmp(sname, "__getattr__") == 0) |
| 318 | set_slot(&op->cl_getattr, v); |
| 319 | else if (strcmp(sname, "__setattr__") == 0) |
| 320 | set_slot(&op->cl_setattr, v); |
| 321 | else if (strcmp(sname, "__delattr__") == 0) |
| 322 | set_slot(&op->cl_delattr, v); |
| 323 | /* For the last three, we fall through to update the |
| 324 | dictionary as well. */ |
| 325 | if (err != NULL) { |
| 326 | if (*err == '\0') |
| 327 | return 0; |
| 328 | PyErr_SetString(PyExc_TypeError, err); |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 329 | return -1; |
| 330 | } |
| 331 | } |
| 332 | } |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 333 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 334 | int rv = PyDict_DelItem(op->cl_dict, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 335 | if (rv < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 336 | PyErr_SetString(PyExc_AttributeError, |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 337 | "delete non-existing class attribute"); |
| 338 | return rv; |
| 339 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 340 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 341 | return PyDict_SetItem(op->cl_dict, name, v); |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 342 | } |
| 343 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 344 | static PyObject * |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 345 | class_repr(op) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 346 | PyClassObject *op; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 347 | { |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 348 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 349 | char buf[140]; |
| 350 | char *name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 351 | if (op->cl_name == NULL || !PyString_Check(op->cl_name)) |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 352 | name = "?"; |
| 353 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 354 | name = PyString_AsString(op->cl_name); |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 355 | if (mod == NULL || !PyString_Check(mod)) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 356 | sprintf(buf, "<class ?.%.100s at %p>", name, op); |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 357 | else |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 358 | sprintf(buf, "<class %.50s.%.50s at %p>", |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 359 | PyString_AsString(mod), |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 360 | name, op); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 361 | return PyString_FromString(buf); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 362 | } |
| 363 | |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 364 | static PyObject * |
| 365 | class_str(op) |
| 366 | PyClassObject *op; |
| 367 | { |
| 368 | PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); |
| 369 | PyObject *name = op->cl_name; |
| 370 | PyObject *res; |
| 371 | int m, n; |
| 372 | |
| 373 | if (name == NULL || !PyString_Check(name)) |
| 374 | return class_repr(op); |
| 375 | if (mod == NULL || !PyString_Check(mod)) { |
| 376 | Py_INCREF(name); |
| 377 | return name; |
| 378 | } |
| 379 | m = PyString_Size(mod); |
| 380 | n = PyString_Size(name); |
| 381 | res = PyString_FromStringAndSize((char *)NULL, m+1+n); |
| 382 | if (res != NULL) { |
| 383 | char *s = PyString_AsString(res); |
| 384 | memcpy(s, PyString_AsString(mod), m); |
| 385 | s += m; |
| 386 | *s++ = '.'; |
| 387 | memcpy(s, PyString_AsString(name), n); |
| 388 | } |
| 389 | return res; |
| 390 | } |
| 391 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 392 | static int |
| 393 | class_traverse(PyClassObject *o, visitproc visit, void *arg) |
| 394 | { |
| 395 | int err; |
| 396 | if (o->cl_bases) { |
| 397 | err = visit(o->cl_bases, arg); |
| 398 | if (err) |
| 399 | return err; |
| 400 | } |
| 401 | if (o->cl_dict) { |
| 402 | err = visit(o->cl_dict, arg); |
| 403 | if (err) |
| 404 | return err; |
| 405 | } |
| 406 | if (o->cl_name) { |
| 407 | err = visit(o->cl_name, arg); |
| 408 | if (err) |
| 409 | return err; |
| 410 | } |
| 411 | if (o->cl_getattr) { |
| 412 | err = visit(o->cl_getattr, arg); |
| 413 | if (err) |
| 414 | return err; |
| 415 | } |
| 416 | if (o->cl_setattr) { |
| 417 | err = visit(o->cl_setattr, arg); |
| 418 | if (err) |
| 419 | return err; |
| 420 | } |
| 421 | if (o->cl_delattr) { |
| 422 | err = visit(o->cl_delattr, arg); |
| 423 | if (err) |
| 424 | return err; |
| 425 | } |
| 426 | return 0; |
| 427 | } |
| 428 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 429 | PyTypeObject PyClass_Type = { |
| 430 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 431 | 0, |
| 432 | "class", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 433 | sizeof(PyClassObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 434 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 435 | (destructor)class_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 436 | 0, /*tp_print*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 437 | 0, /*tp_getattr*/ |
| 438 | 0, /*tp_setattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 439 | 0, /*tp_compare*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 440 | (reprfunc)class_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 441 | 0, /*tp_as_number*/ |
| 442 | 0, /*tp_as_sequence*/ |
| 443 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 444 | 0, /*tp_hash*/ |
| 445 | 0, /*tp_call*/ |
Guido van Rossum | 4a2a621 | 1997-10-20 23:26:11 +0000 | [diff] [blame] | 446 | (reprfunc)class_str, /*tp_str*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 447 | (getattrofunc)class_getattr, /*tp_getattro*/ |
| 448 | (setattrofunc)class_setattr, /*tp_setattro*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 449 | 0, /* tp_as_buffer */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 450 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 451 | 0, /* tp_doc */ |
| 452 | (traverseproc)class_traverse, /* tp_traverse */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 453 | }; |
| 454 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 455 | int |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 456 | PyClass_IsSubclass(class, base) |
| 457 | PyObject *class; |
| 458 | PyObject *base; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 459 | { |
| 460 | int i, n; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 461 | PyClassObject *cp; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 462 | if (class == base) |
| 463 | return 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 464 | if (class == NULL || !PyClass_Check(class)) |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 465 | return 0; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 466 | cp = (PyClassObject *)class; |
| 467 | n = PyTuple_Size(cp->cl_bases); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 468 | for (i = 0; i < n; i++) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 469 | if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 470 | return 1; |
| 471 | } |
| 472 | return 0; |
| 473 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 474 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 475 | |
| 476 | /* Instance objects */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 477 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 478 | PyObject * |
| 479 | PyInstance_New(class, arg, kw) |
| 480 | PyObject *class; |
| 481 | PyObject *arg; |
| 482 | PyObject *kw; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 483 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 484 | register PyInstanceObject *inst; |
| 485 | PyObject *init; |
| 486 | static PyObject *initstr; |
| 487 | if (!PyClass_Check(class)) { |
| 488 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 489 | return NULL; |
| 490 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 491 | inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 492 | if (inst == NULL) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 493 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 494 | inst->in_dict = PyDict_New(); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 495 | PyObject_GC_Init(inst); |
Guido van Rossum | 0add15f | 1997-05-09 01:07:15 +0000 | [diff] [blame] | 496 | if (inst->in_dict == NULL) { |
Guido van Rossum | d7823f2 | 2000-06-28 23:46:07 +0000 | [diff] [blame] | 497 | PyObject_DEL(inst); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 498 | return NULL; |
| 499 | } |
Guido van Rossum | d7823f2 | 2000-06-28 23:46:07 +0000 | [diff] [blame] | 500 | Py_INCREF(class); |
| 501 | inst->in_class = (PyClassObject *)class; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 502 | if (initstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 503 | initstr = PyString_InternFromString("__init__"); |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 504 | init = instance_getattr2(inst, initstr); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 505 | if (init == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 506 | if ((arg != NULL && (!PyTuple_Check(arg) || |
| 507 | PyTuple_Size(arg) != 0)) |
| 508 | || (kw != NULL && (!PyDict_Check(kw) || |
| 509 | PyDict_Size(kw) != 0))) { |
| 510 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | a83f270 | 1995-07-26 18:07:32 +0000 | [diff] [blame] | 511 | "this constructor takes no arguments"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 512 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 513 | inst = NULL; |
| 514 | } |
| 515 | } |
| 516 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 517 | PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); |
| 518 | Py_DECREF(init); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 519 | if (res == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 520 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 521 | inst = NULL; |
| 522 | } |
| 523 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 524 | if (res != Py_None) { |
| 525 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 526 | "__init__() should return None"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 527 | Py_DECREF(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 528 | inst = NULL; |
| 529 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 530 | Py_DECREF(res); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 531 | } |
| 532 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 533 | return (PyObject *)inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 534 | } |
| 535 | |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 536 | /* Instance methods */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 537 | |
| 538 | static void |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 539 | instance_dealloc(inst) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 540 | register PyInstanceObject *inst; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 541 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 542 | PyObject *error_type, *error_value, *error_traceback; |
| 543 | PyObject *del; |
| 544 | static PyObject *delstr; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 545 | extern long _Py_RefTotal; |
| 546 | PyObject_GC_Fini(inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 547 | /* Call the __del__ method if it exists. First temporarily |
| 548 | revive the object and save the current exception, if any. */ |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 549 | #ifdef Py_TRACE_REFS |
| 550 | /* much too complicated if Py_TRACE_REFS defined */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 551 | inst->ob_type = &PyInstance_Type; |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 552 | _Py_NewReference((PyObject *)inst); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 553 | _Py_RefTotal--; /* compensate for increment in NEWREF */ |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 554 | #ifdef COUNT_ALLOCS |
| 555 | inst->ob_type->tp_alloc--; /* ditto */ |
| 556 | #endif |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 557 | #else /* !Py_TRACE_REFS */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 558 | Py_INCREF(inst); |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 559 | #endif /* !Py_TRACE_REFS */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 560 | PyErr_Fetch(&error_type, &error_value, &error_traceback); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 561 | if (delstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 562 | delstr = PyString_InternFromString("__del__"); |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 563 | if ((del = instance_getattr2(inst, delstr)) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 564 | PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 565 | if (res == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 566 | PyObject *f, *t, *v, *tb; |
| 567 | PyErr_Fetch(&t, &v, &tb); |
| 568 | f = PySys_GetObject("stderr"); |
Guido van Rossum | 22a85e5 | 1996-09-11 22:51:57 +0000 | [diff] [blame] | 569 | if (f != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 570 | PyFile_WriteString("Exception ", f); |
Guido van Rossum | cbd1e4e | 1996-12-05 21:52:32 +0000 | [diff] [blame] | 571 | if (t) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 572 | PyFile_WriteObject(t, f, Py_PRINT_RAW); |
| 573 | if (v && v != Py_None) { |
| 574 | PyFile_WriteString(": ", f); |
| 575 | PyFile_WriteObject(v, f, 0); |
Guido van Rossum | cbd1e4e | 1996-12-05 21:52:32 +0000 | [diff] [blame] | 576 | } |
| 577 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 578 | PyFile_WriteString(" in ", f); |
| 579 | PyFile_WriteObject(del, f, 0); |
| 580 | PyFile_WriteString(" ignored\n", f); |
| 581 | PyErr_Clear(); /* Just in case */ |
Guido van Rossum | 22a85e5 | 1996-09-11 22:51:57 +0000 | [diff] [blame] | 582 | } |
Guido van Rossum | cbd1e4e | 1996-12-05 21:52:32 +0000 | [diff] [blame] | 583 | Py_XDECREF(t); |
| 584 | Py_XDECREF(v); |
| 585 | Py_XDECREF(tb); |
Guido van Rossum | 0dfcf75 | 1996-08-12 22:00:53 +0000 | [diff] [blame] | 586 | } |
| 587 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 588 | Py_DECREF(res); |
| 589 | Py_DECREF(del); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 590 | } |
| 591 | /* Restore the saved exception and undo the temporary revival */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 592 | PyErr_Restore(error_type, error_value, error_traceback); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 593 | /* Can't use DECREF here, it would cause a recursive call */ |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 594 | if (--inst->ob_refcnt > 0) { |
| 595 | #ifdef COUNT_ALLOCS |
| 596 | inst->ob_type->tp_free--; |
| 597 | #endif |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 598 | PyObject_GC_Init((PyObject *)inst); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 599 | return; /* __del__ added a reference; don't delete now */ |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 600 | } |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 601 | #ifdef Py_TRACE_REFS |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 602 | #ifdef COUNT_ALLOCS |
| 603 | inst->ob_type->tp_free--; /* compensate for increment in UNREF */ |
| 604 | #endif |
Guido van Rossum | bffd683 | 2000-01-20 22:32:56 +0000 | [diff] [blame] | 605 | _Py_ForgetReference((PyObject *)inst); |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 606 | #ifndef WITH_CYCLE_GC |
Sjoerd Mullender | 740f357 | 1995-08-28 09:00:43 +0000 | [diff] [blame] | 607 | inst->ob_type = NULL; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 608 | #endif |
Guido van Rossum | 441e4ab | 1996-05-23 22:46:51 +0000 | [diff] [blame] | 609 | #endif /* Py_TRACE_REFS */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 610 | Py_DECREF(inst->in_class); |
| 611 | Py_XDECREF(inst->in_dict); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 612 | PyObject_DEL(inst); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 615 | static PyObject * |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 616 | instance_getattr1(inst, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 617 | register PyInstanceObject *inst; |
| 618 | PyObject *name; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 619 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 620 | register PyObject *v; |
| 621 | register char *sname = PyString_AsString(name); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 622 | if (sname[0] == '_' && sname[1] == '_') { |
| 623 | if (strcmp(sname, "__dict__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 624 | if (PyEval_GetRestricted()) { |
| 625 | PyErr_SetString(PyExc_RuntimeError, |
| 626 | "instance.__dict__ not accessible in restricted mode"); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 627 | return NULL; |
| 628 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 629 | Py_INCREF(inst->in_dict); |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 630 | return inst->in_dict; |
| 631 | } |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 632 | if (strcmp(sname, "__class__") == 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 633 | Py_INCREF(inst->in_class); |
| 634 | return (PyObject *)inst->in_class; |
Sjoerd Mullender | 3bb8a05 | 1993-10-22 12:04:32 +0000 | [diff] [blame] | 635 | } |
Guido van Rossum | 9430839 | 1991-10-20 20:11:48 +0000 | [diff] [blame] | 636 | } |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 637 | v = instance_getattr2(inst, name); |
| 638 | if (v == NULL) { |
| 639 | PyErr_Format(PyExc_AttributeError,"'%.50s' instance has no attribute '%.400s'", |
| 640 | PyString_AS_STRING(inst->in_class->cl_name), sname); |
| 641 | } |
| 642 | return v; |
| 643 | } |
| 644 | |
| 645 | static PyObject * |
| 646 | instance_getattr2(inst, name) |
| 647 | register PyInstanceObject *inst; |
| 648 | PyObject *name; |
| 649 | { |
| 650 | register PyObject *v; |
| 651 | PyClassObject *class; |
Guido van Rossum | eb6b33a | 1993-05-25 09:38:27 +0000 | [diff] [blame] | 652 | class = NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 653 | v = PyDict_GetItem(inst->in_dict, name); |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 654 | if (v == NULL) { |
| 655 | v = class_lookup(inst->in_class, name, &class); |
Jeremy Hylton | 9e392e2 | 2000-04-26 20:39:20 +0000 | [diff] [blame] | 656 | if (v == NULL) |
| 657 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 658 | } |
Guido van Rossum | 0add15f | 1997-05-09 01:07:15 +0000 | [diff] [blame] | 659 | Py_INCREF(v); |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 660 | if (class != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 661 | if (PyFunction_Check(v)) { |
| 662 | PyObject *w = PyMethod_New(v, (PyObject *)inst, |
| 663 | (PyObject *)class); |
| 664 | Py_DECREF(v); |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 665 | v = w; |
| 666 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 667 | else if (PyMethod_Check(v)) { |
| 668 | PyObject *im_class = PyMethod_Class(v); |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 669 | /* Only if classes are compatible */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 670 | if (PyClass_IsSubclass((PyObject *)class, im_class)) { |
| 671 | PyObject *im_func = PyMethod_Function(v); |
| 672 | PyObject *w = PyMethod_New(im_func, |
| 673 | (PyObject *)inst, im_class); |
| 674 | Py_DECREF(v); |
Guido van Rossum | 21d335e | 1993-10-15 13:01:11 +0000 | [diff] [blame] | 675 | v = w; |
| 676 | } |
| 677 | } |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 678 | } |
| 679 | return v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 680 | } |
| 681 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 682 | static PyObject * |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 683 | instance_getattr(inst, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 684 | register PyInstanceObject *inst; |
| 685 | PyObject *name; |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 686 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 687 | register PyObject *func, *res; |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 688 | res = instance_getattr1(inst, name); |
| 689 | if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 690 | PyObject *args; |
| 691 | PyErr_Clear(); |
| 692 | args = Py_BuildValue("(OO)", inst, name); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 693 | if (args == NULL) |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 694 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 695 | res = PyEval_CallObject(func, args); |
| 696 | Py_DECREF(args); |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 697 | } |
| 698 | return res; |
| 699 | } |
| 700 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 701 | static int |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 702 | instance_setattr1(inst, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 703 | PyInstanceObject *inst; |
| 704 | PyObject *name; |
| 705 | PyObject *v; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 706 | { |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 707 | if (v == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 708 | int rv = PyDict_DelItem(inst->in_dict, name); |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 709 | if (rv < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 710 | PyErr_SetString(PyExc_AttributeError, |
Guido van Rossum | 94472a0 | 1992-09-04 09:45:18 +0000 | [diff] [blame] | 711 | "delete non-existing instance attribute"); |
| 712 | return rv; |
| 713 | } |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 714 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 715 | return PyDict_SetItem(inst->in_dict, name, v); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 716 | } |
| 717 | |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 718 | static int |
| 719 | instance_setattr(inst, name, v) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 720 | PyInstanceObject *inst; |
| 721 | PyObject *name; |
| 722 | PyObject *v; |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 723 | { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 724 | PyObject *func, *args, *res, *tmp; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 725 | char *sname = PyString_AsString(name); |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 726 | if (sname[0] == '_' && sname[1] == '_') { |
| 727 | int n = PyString_Size(name); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 728 | if (sname[n-1] == '_' && sname[n-2] == '_') { |
Guido van Rossum | b2173c3 | 1997-08-25 21:23:56 +0000 | [diff] [blame] | 729 | if (strcmp(sname, "__dict__") == 0) { |
| 730 | if (PyEval_GetRestricted()) { |
| 731 | PyErr_SetString(PyExc_RuntimeError, |
| 732 | "__dict__ not accessible in restricted mode"); |
| 733 | return -1; |
| 734 | } |
| 735 | if (v == NULL || !PyDict_Check(v)) { |
| 736 | PyErr_SetString(PyExc_TypeError, |
| 737 | "__dict__ must be set to a dictionary"); |
| 738 | return -1; |
| 739 | } |
| 740 | tmp = inst->in_dict; |
| 741 | Py_INCREF(v); |
| 742 | inst->in_dict = v; |
| 743 | Py_DECREF(tmp); |
| 744 | return 0; |
| 745 | } |
| 746 | if (strcmp(sname, "__class__") == 0) { |
| 747 | if (PyEval_GetRestricted()) { |
| 748 | PyErr_SetString(PyExc_RuntimeError, |
| 749 | "__class__ not accessible in restricted mode"); |
| 750 | return -1; |
| 751 | } |
| 752 | if (v == NULL || !PyClass_Check(v)) { |
| 753 | PyErr_SetString(PyExc_TypeError, |
| 754 | "__class__ must be set to a class"); |
| 755 | return -1; |
| 756 | } |
| 757 | tmp = (PyObject *)(inst->in_class); |
| 758 | Py_INCREF(v); |
| 759 | inst->in_class = (PyClassObject *)v; |
| 760 | Py_DECREF(tmp); |
| 761 | return 0; |
| 762 | } |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 763 | } |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 764 | } |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 765 | if (v == NULL) |
| 766 | func = inst->in_class->cl_delattr; |
| 767 | else |
| 768 | func = inst->in_class->cl_setattr; |
| 769 | if (func == NULL) |
| 770 | return instance_setattr1(inst, name, v); |
| 771 | if (v == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 772 | args = Py_BuildValue("(OO)", inst, name); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 773 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 774 | args = Py_BuildValue("(OOO)", inst, name, v); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 775 | if (args == NULL) |
| 776 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 777 | res = PyEval_CallObject(func, args); |
| 778 | Py_DECREF(args); |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 779 | if (res == NULL) |
| 780 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 781 | Py_DECREF(res); |
Guido van Rossum | e773754 | 1994-09-05 07:31:41 +0000 | [diff] [blame] | 782 | return 0; |
| 783 | } |
| 784 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 785 | static PyObject * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 786 | instance_repr(inst) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 787 | PyInstanceObject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 788 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 789 | PyObject *func; |
| 790 | PyObject *res; |
| 791 | static PyObject *reprstr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 792 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 793 | if (reprstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 794 | reprstr = PyString_InternFromString("__repr__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 795 | func = instance_getattr(inst, reprstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 796 | if (func == NULL) { |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 797 | char buf[140]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 798 | PyObject *classname = inst->in_class->cl_name; |
Guido van Rossum | b7f1afe | 1997-12-03 00:06:02 +0000 | [diff] [blame] | 799 | PyObject *mod = PyDict_GetItemString( |
| 800 | inst->in_class->cl_dict, "__module__"); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 801 | char *cname; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 802 | if (classname != NULL && PyString_Check(classname)) |
| 803 | cname = PyString_AsString(classname); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 804 | else |
| 805 | cname = "?"; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 806 | PyErr_Clear(); |
Guido van Rossum | b7f1afe | 1997-12-03 00:06:02 +0000 | [diff] [blame] | 807 | if (mod == NULL || !PyString_Check(mod)) |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 808 | sprintf(buf, "<?.%.100s instance at %p>", |
| 809 | cname, inst); |
Guido van Rossum | b7f1afe | 1997-12-03 00:06:02 +0000 | [diff] [blame] | 810 | else |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 811 | sprintf(buf, "<%.50s.%.50s instance at %p>", |
Guido van Rossum | b7f1afe | 1997-12-03 00:06:02 +0000 | [diff] [blame] | 812 | PyString_AsString(mod), |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 813 | cname, inst); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 814 | return PyString_FromString(buf); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 815 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 816 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 817 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 818 | return res; |
| 819 | } |
| 820 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 821 | static PyObject * |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 822 | instance_compare1(inst, other) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 823 | PyObject *inst, *other; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 824 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 825 | return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__", |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 826 | instance_compare1); |
| 827 | } |
| 828 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 829 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 830 | instance_compare(inst, other) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 831 | PyObject *inst, *other; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 832 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 833 | PyObject *result; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 834 | long outcome; |
| 835 | result = instance_compare1(inst, other); |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 836 | if (result == NULL) |
| 837 | return -1; |
| 838 | if (!PyInt_Check(result)) { |
| 839 | Py_DECREF(result); |
| 840 | PyErr_SetString(PyExc_TypeError, |
| 841 | "comparison did not return an int"); |
| 842 | return -1; |
Sjoerd Mullender | b9a6d12 | 1994-10-19 15:11:52 +0000 | [diff] [blame] | 843 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 844 | outcome = PyInt_AsLong(result); |
| 845 | Py_DECREF(result); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 846 | if (outcome < 0) |
| 847 | return -1; |
| 848 | else if (outcome > 0) |
| 849 | return 1; |
| 850 | return 0; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 851 | } |
| 852 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 853 | static long |
| 854 | instance_hash(inst) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 855 | PyInstanceObject *inst; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 856 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 857 | PyObject *func; |
| 858 | PyObject *res; |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 859 | long outcome; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 860 | static PyObject *hashstr, *cmpstr; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 861 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 862 | if (hashstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 863 | hashstr = PyString_InternFromString("__hash__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 864 | func = instance_getattr(inst, hashstr); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 865 | if (func == NULL) { |
| 866 | /* If there is no __cmp__ method, we hash on the address. |
| 867 | If a __cmp__ method exists, there must be a __hash__. */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 868 | PyErr_Clear(); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 869 | if (cmpstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 870 | cmpstr = PyString_InternFromString("__cmp__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 871 | func = instance_getattr(inst, cmpstr); |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 872 | if (func == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 873 | PyErr_Clear(); |
Fred Drake | 13634cf | 2000-06-29 19:17:04 +0000 | [diff] [blame] | 874 | return _Py_HashPointer(inst); |
Guido van Rossum | 8a0c345 | 1993-04-08 12:56:19 +0000 | [diff] [blame] | 875 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 876 | PyErr_SetString(PyExc_TypeError, "unhashable instance"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 877 | return -1; |
| 878 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 879 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 880 | Py_DECREF(func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 881 | if (res == NULL) |
| 882 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 883 | if (PyInt_Check(res)) { |
| 884 | outcome = PyInt_AsLong(res); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 885 | if (outcome == -1) |
| 886 | outcome = -2; |
| 887 | } |
| 888 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 889 | PyErr_SetString(PyExc_TypeError, |
| 890 | "__hash__() should return an int"); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 891 | outcome = -1; |
| 892 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 893 | Py_DECREF(res); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 894 | return outcome; |
| 895 | } |
| 896 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 897 | static int |
| 898 | instance_traverse(PyInstanceObject *o, visitproc visit, void *arg) |
| 899 | { |
| 900 | int err; |
| 901 | if (o->in_class) { |
| 902 | err = visit((PyObject *)(o->in_class), arg); |
| 903 | if (err) |
| 904 | return err; |
| 905 | } |
| 906 | if (o->in_dict) { |
| 907 | err = visit(o->in_dict, arg); |
| 908 | if (err) |
| 909 | return err; |
| 910 | } |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 911 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 912 | } |
| 913 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 914 | static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 915 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 916 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 917 | instance_length(inst) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 918 | PyInstanceObject *inst; |
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 *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 922 | int outcome; |
| 923 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 924 | if (lenstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 925 | lenstr = PyString_InternFromString("__len__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 926 | func = instance_getattr(inst, lenstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 927 | if (func == NULL) |
| 928 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 929 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 930 | Py_DECREF(func); |
Guido van Rossum | d014ea6 | 1992-11-26 10:30:26 +0000 | [diff] [blame] | 931 | if (res == NULL) |
| 932 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 933 | if (PyInt_Check(res)) { |
| 934 | outcome = PyInt_AsLong(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 935 | if (outcome < 0) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 936 | PyErr_SetString(PyExc_ValueError, |
| 937 | "__len__() should return >= 0"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 938 | } |
| 939 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 940 | PyErr_SetString(PyExc_TypeError, |
| 941 | "__len__() should return an int"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 942 | outcome = -1; |
| 943 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 944 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 945 | return outcome; |
| 946 | } |
| 947 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 948 | static PyObject * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 949 | instance_subscript(inst, key) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 950 | PyInstanceObject *inst; |
| 951 | PyObject *key; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 952 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 953 | PyObject *func; |
| 954 | PyObject *arg; |
| 955 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 956 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 957 | if (getitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 958 | getitemstr = PyString_InternFromString("__getitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 959 | func = instance_getattr(inst, getitemstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 960 | if (func == NULL) |
| 961 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 962 | arg = Py_BuildValue("(O)", key); |
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 | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 965 | return NULL; |
| 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 | return res; |
| 971 | } |
| 972 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 973 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 974 | instance_ass_subscript(inst, key, value) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 975 | PyInstanceObject*inst; |
| 976 | PyObject *key; |
| 977 | PyObject *value; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 978 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 979 | PyObject *func; |
| 980 | PyObject *arg; |
| 981 | PyObject *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 982 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 983 | if (value == NULL) { |
| 984 | if (delitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 985 | delitemstr = PyString_InternFromString("__delitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 986 | func = instance_getattr(inst, delitemstr); |
| 987 | } |
| 988 | else { |
| 989 | if (setitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 990 | setitemstr = PyString_InternFromString("__setitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 991 | func = instance_getattr(inst, setitemstr); |
| 992 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 993 | if (func == NULL) |
| 994 | return -1; |
| 995 | if (value == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 996 | arg = Py_BuildValue("(O)", key); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 997 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 998 | arg = Py_BuildValue("(OO)", key, value); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 999 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1000 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1001 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1002 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1003 | res = PyEval_CallObject(func, arg); |
| 1004 | Py_DECREF(func); |
| 1005 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1006 | if (res == NULL) |
| 1007 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1008 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1009 | return 0; |
| 1010 | } |
| 1011 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1012 | static PyMappingMethods instance_as_mapping = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1013 | (inquiry)instance_length, /*mp_length*/ |
| 1014 | (binaryfunc)instance_subscript, /*mp_subscript*/ |
| 1015 | (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1016 | }; |
| 1017 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1018 | static PyObject * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1019 | instance_item(inst, i) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1020 | PyInstanceObject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1021 | int i; |
| 1022 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1023 | PyObject *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1024 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1025 | if (getitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1026 | getitemstr = PyString_InternFromString("__getitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1027 | func = instance_getattr(inst, getitemstr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1028 | if (func == NULL) |
| 1029 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1030 | arg = Py_BuildValue("(i)", i); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1031 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1032 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1033 | return NULL; |
| 1034 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1035 | res = PyEval_CallObject(func, arg); |
| 1036 | Py_DECREF(func); |
| 1037 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1038 | return res; |
| 1039 | } |
| 1040 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1041 | static PyObject * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1042 | instance_slice(inst, i, j) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1043 | PyInstanceObject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1044 | int i, j; |
| 1045 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1046 | PyObject *func, *arg, *res; |
| 1047 | static PyObject *getslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1048 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1049 | if (getslicestr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1050 | getslicestr = PyString_InternFromString("__getslice__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1051 | func = instance_getattr(inst, getslicestr); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1052 | if (func == NULL) |
| 1053 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1054 | arg = Py_BuildValue("(ii)", i, j); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1055 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1056 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1057 | return NULL; |
| 1058 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1059 | res = PyEval_CallObject(func, arg); |
| 1060 | Py_DECREF(func); |
| 1061 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1062 | return res; |
| 1063 | } |
| 1064 | |
| 1065 | static int |
| 1066 | instance_ass_item(inst, i, item) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1067 | PyInstanceObject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1068 | int i; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1069 | PyObject *item; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1070 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1071 | PyObject *func, *arg, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1072 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1073 | if (item == NULL) { |
| 1074 | if (delitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1075 | delitemstr = PyString_InternFromString("__delitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1076 | func = instance_getattr(inst, delitemstr); |
| 1077 | } |
| 1078 | else { |
| 1079 | if (setitemstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1080 | setitemstr = PyString_InternFromString("__setitem__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1081 | func = instance_getattr(inst, setitemstr); |
| 1082 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1083 | if (func == NULL) |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1084 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1085 | if (item == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1086 | arg = Py_BuildValue("i", i); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1087 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1088 | arg = Py_BuildValue("(iO)", i, item); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1089 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1090 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1091 | return -1; |
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 | res = PyEval_CallObject(func, arg); |
| 1094 | Py_DECREF(func); |
| 1095 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1096 | if (res == NULL) |
| 1097 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1098 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1099 | return 0; |
| 1100 | } |
| 1101 | |
| 1102 | static int |
| 1103 | instance_ass_slice(inst, i, j, value) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1104 | PyInstanceObject *inst; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1105 | int i, j; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1106 | PyObject *value; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1107 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1108 | PyObject *func, *arg, *res; |
| 1109 | static PyObject *setslicestr, *delslicestr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1110 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1111 | if (value == NULL) { |
| 1112 | if (delslicestr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1113 | delslicestr = |
| 1114 | PyString_InternFromString("__delslice__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1115 | func = instance_getattr(inst, delslicestr); |
| 1116 | } |
| 1117 | else { |
| 1118 | if (setslicestr == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1119 | setslicestr = |
| 1120 | PyString_InternFromString("__setslice__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1121 | func = instance_getattr(inst, setslicestr); |
| 1122 | } |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1123 | if (func == NULL) |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1124 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1125 | if (value == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1126 | arg = Py_BuildValue("(ii)", i, j); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1127 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1128 | arg = Py_BuildValue("(iiO)", i, j, value); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1129 | if (arg == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1130 | Py_DECREF(func); |
Guido van Rossum | 6d946f9 | 1992-08-14 13:49:30 +0000 | [diff] [blame] | 1131 | return -1; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1132 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1133 | res = PyEval_CallObject(func, arg); |
| 1134 | Py_DECREF(func); |
| 1135 | Py_DECREF(arg); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1136 | if (res == NULL) |
| 1137 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1138 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1139 | return 0; |
| 1140 | } |
| 1141 | |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1142 | static int instance_contains(PyInstanceObject *inst, PyObject *member) |
| 1143 | { |
| 1144 | static PyObject *__contains__; |
| 1145 | PyObject *func, *arg, *res; |
| 1146 | int ret; |
| 1147 | |
| 1148 | if(__contains__ == NULL) { |
| 1149 | __contains__ = PyString_InternFromString("__contains__"); |
| 1150 | if(__contains__ == NULL) |
| 1151 | return -1; |
| 1152 | } |
| 1153 | func = instance_getattr(inst, __contains__); |
| 1154 | if(func == NULL) { |
| 1155 | /* fall back to previous behaviour */ |
| 1156 | int i, cmp_res; |
| 1157 | |
| 1158 | if(!PyErr_ExceptionMatches(PyExc_AttributeError)) |
| 1159 | return -1; |
| 1160 | PyErr_Clear(); |
| 1161 | for(i=0;;i++) { |
| 1162 | PyObject *obj = instance_item(inst, i); |
| 1163 | int ret = 0; |
| 1164 | |
| 1165 | if(obj == NULL) { |
| 1166 | if(!PyErr_ExceptionMatches(PyExc_IndexError)) |
| 1167 | return -1; |
| 1168 | PyErr_Clear(); |
| 1169 | return 0; |
| 1170 | } |
| 1171 | if(PyObject_Cmp(obj, member, &cmp_res) == -1) |
| 1172 | ret = -1; |
| 1173 | if(cmp_res == 0) |
| 1174 | ret = 1; |
| 1175 | Py_DECREF(obj); |
| 1176 | if(ret) |
| 1177 | return ret; |
| 1178 | } |
| 1179 | } |
| 1180 | arg = Py_BuildValue("(O)", member); |
| 1181 | if(arg == NULL) { |
| 1182 | Py_DECREF(func); |
| 1183 | return -1; |
| 1184 | } |
| 1185 | res = PyEval_CallObject(func, arg); |
| 1186 | Py_DECREF(func); |
| 1187 | Py_DECREF(arg); |
| 1188 | if(res == NULL) |
| 1189 | return -1; |
| 1190 | ret = PyObject_IsTrue(res); |
| 1191 | Py_DECREF(res); |
| 1192 | return ret; |
| 1193 | } |
| 1194 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1195 | static PySequenceMethods instance_as_sequence = { |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1196 | (inquiry)instance_length, /*sq_length*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1197 | 0, /*sq_concat*/ |
| 1198 | 0, /*sq_repeat*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1199 | (intargfunc)instance_item, /*sq_item*/ |
| 1200 | (intintargfunc)instance_slice, /*sq_slice*/ |
| 1201 | (intobjargproc)instance_ass_item, /*sq_ass_item*/ |
| 1202 | (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/ |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1203 | (objobjproc)instance_contains, /* sq_contains */ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1204 | }; |
| 1205 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1206 | static PyObject * |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1207 | generic_unary_op(self, methodname) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1208 | PyInstanceObject *self; |
| 1209 | PyObject *methodname; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1210 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1211 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1212 | |
| 1213 | if ((func = instance_getattr(self, methodname)) == NULL) |
| 1214 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1215 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1216 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1217 | return res; |
| 1218 | } |
| 1219 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1220 | |
| 1221 | /* Forward */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1222 | static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **, |
| 1223 | PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int )); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1224 | |
| 1225 | |
| 1226 | /* Implement a binary operator involving at least one class instance. */ |
| 1227 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1228 | PyObject * |
| 1229 | PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) |
| 1230 | PyObject *v; |
| 1231 | PyObject *w; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1232 | char *opname; |
| 1233 | char *ropname; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1234 | PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1235 | { |
| 1236 | char buf[256]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1237 | PyObject *result = NULL; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1238 | if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1239 | return result; |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1240 | if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1241 | return result; |
Guido van Rossum | c8b6df9 | 1997-05-23 00:06:51 +0000 | [diff] [blame] | 1242 | /* Sigh -- special case for comnparisons */ |
| 1243 | if (strcmp(opname, "__cmp__") == 0) { |
| 1244 | long c = (v < w) ? -1 : (v > w) ? 1 : 0; |
| 1245 | return PyInt_FromLong(c); |
| 1246 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1247 | sprintf(buf, "%s nor %s defined for these operands", opname, ropname); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1248 | PyErr_SetString(PyExc_TypeError, buf); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1249 | return NULL; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1250 | } |
| 1251 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1252 | |
| 1253 | /* Try one half of a binary operator involving a class instance. |
| 1254 | Return value: |
| 1255 | -1 if an exception is to be reported right away |
| 1256 | 0 if we have a valid result |
| 1257 | 1 if we could try another operation |
| 1258 | */ |
| 1259 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1260 | static PyObject *coerce_obj; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1261 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1262 | static int |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1263 | halfbinop(v, w, opname, r_result, thisfunc, swapped) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1264 | PyObject *v; |
| 1265 | PyObject *w; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1266 | char *opname; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1267 | PyObject **r_result; |
| 1268 | PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1269 | int swapped; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1270 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1271 | PyObject *func; |
| 1272 | PyObject *args; |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1273 | PyObject *coercefunc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1274 | PyObject *coerced = NULL; |
| 1275 | PyObject *v1; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1276 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1277 | if (!PyInstance_Check(v)) |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1278 | return 1; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1279 | if (coerce_obj == NULL) { |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1280 | coerce_obj = PyString_InternFromString("__coerce__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1281 | if (coerce_obj == NULL) |
| 1282 | return -1; |
| 1283 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1284 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1285 | if (coercefunc == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1286 | PyErr_Clear(); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1287 | } |
| 1288 | else { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1289 | args = Py_BuildValue("(O)", w); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1290 | if (args == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1291 | return -1; |
| 1292 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1293 | coerced = PyEval_CallObject(coercefunc, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1294 | Py_DECREF(args); |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1295 | Py_DECREF(coercefunc); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1296 | if (coerced == NULL) { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1297 | return -1; |
| 1298 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1299 | if (coerced == Py_None) { |
| 1300 | Py_DECREF(coerced); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1301 | return 1; |
| 1302 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1303 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
| 1304 | Py_DECREF(coerced); |
| 1305 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1306 | "coercion should return None or 2-tuple"); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1307 | return -1; |
| 1308 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1309 | v1 = PyTuple_GetItem(coerced, 0); |
| 1310 | w = PyTuple_GetItem(coerced, 1); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1311 | if (v1 != v) { |
| 1312 | v = v1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1313 | if (!PyInstance_Check(v) && !PyInstance_Check(w)) { |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1314 | if (swapped) |
| 1315 | *r_result = (*thisfunc)(w, v); |
| 1316 | else |
| 1317 | *r_result = (*thisfunc)(v, w); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1318 | Py_DECREF(coerced); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1319 | return *r_result == NULL ? -1 : 0; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1320 | } |
| 1321 | } |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1322 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1323 | func = PyObject_GetAttrString(v, opname); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1324 | if (func == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1325 | Py_XDECREF(coerced); |
Guido van Rossum | 617c1b0 | 1998-05-28 19:50:02 +0000 | [diff] [blame] | 1326 | if (!PyErr_ExceptionMatches(PyExc_AttributeError)) |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1327 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1328 | PyErr_Clear(); |
Guido van Rossum | e7d444f | 1995-01-07 12:35:18 +0000 | [diff] [blame] | 1329 | return 1; |
| 1330 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1331 | args = Py_BuildValue("(O)", w); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1332 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1333 | Py_DECREF(func); |
| 1334 | Py_XDECREF(coerced); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1335 | return -1; |
| 1336 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1337 | *r_result = PyEval_CallObject(func, args); |
| 1338 | Py_DECREF(args); |
| 1339 | Py_DECREF(func); |
| 1340 | Py_XDECREF(coerced); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1341 | return *r_result == NULL ? -1 : 0; |
| 1342 | } |
| 1343 | |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1344 | static int |
| 1345 | instance_coerce(pv, pw) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1346 | PyObject **pv; |
| 1347 | PyObject **pw; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1348 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1349 | PyObject *v = *pv; |
| 1350 | PyObject *w = *pw; |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1351 | PyObject *coercefunc; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1352 | PyObject *args; |
| 1353 | PyObject *coerced; |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1354 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1355 | if (coerce_obj == NULL) { |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1356 | coerce_obj = PyString_InternFromString("__coerce__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1357 | if (coerce_obj == NULL) |
| 1358 | return -1; |
| 1359 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1360 | coercefunc = PyObject_GetAttr(v, coerce_obj); |
| 1361 | if (coercefunc == NULL) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1362 | /* No __coerce__ method: always OK */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1363 | PyErr_Clear(); |
| 1364 | Py_INCREF(v); |
| 1365 | Py_INCREF(w); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1366 | return 0; |
| 1367 | } |
| 1368 | /* Has __coerce__ method: call it */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1369 | args = Py_BuildValue("(O)", w); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1370 | if (args == NULL) { |
| 1371 | return -1; |
| 1372 | } |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1373 | coerced = PyEval_CallObject(coercefunc, args); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1374 | Py_DECREF(args); |
Guido van Rossum | 3931df9 | 1997-11-18 19:23:07 +0000 | [diff] [blame] | 1375 | Py_DECREF(coercefunc); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1376 | if (coerced == NULL) { |
| 1377 | /* __coerce__ call raised an exception */ |
| 1378 | return -1; |
| 1379 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1380 | if (coerced == Py_None) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1381 | /* __coerce__ says "I can't do it" */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1382 | Py_DECREF(coerced); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1383 | return 1; |
| 1384 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1385 | if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1386 | /* __coerce__ return value is malformed */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1387 | Py_DECREF(coerced); |
| 1388 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1389 | "coercion should return None or 2-tuple"); |
| 1390 | return -1; |
| 1391 | } |
| 1392 | /* __coerce__ returned two new values */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1393 | *pv = PyTuple_GetItem(coerced, 0); |
| 1394 | *pw = PyTuple_GetItem(coerced, 1); |
| 1395 | Py_INCREF(*pv); |
| 1396 | Py_INCREF(*pw); |
| 1397 | Py_DECREF(coerced); |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1398 | return 0; |
| 1399 | } |
| 1400 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1401 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1402 | #define UNARY(funcname, methodname) \ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1403 | static PyObject *funcname(self) PyInstanceObject *self; { \ |
| 1404 | static PyObject *o; \ |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1405 | if (o == NULL) o = PyString_InternFromString(methodname); \ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1406 | return generic_unary_op(self, o); \ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1407 | } |
| 1408 | |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1409 | UNARY(instance_neg, "__neg__") |
| 1410 | UNARY(instance_pos, "__pos__") |
| 1411 | UNARY(instance_abs, "__abs__") |
| 1412 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1413 | static int |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1414 | instance_nonzero(self) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1415 | PyInstanceObject *self; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1416 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1417 | PyObject *func, *res; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1418 | long outcome; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1419 | static PyObject *nonzerostr; |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1420 | |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1421 | if (nonzerostr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1422 | nonzerostr = PyString_InternFromString("__nonzero__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1423 | if ((func = instance_getattr(self, nonzerostr)) == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1424 | PyErr_Clear(); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1425 | if (lenstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1426 | lenstr = PyString_InternFromString("__len__"); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1427 | if ((func = instance_getattr(self, lenstr)) == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1428 | PyErr_Clear(); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1429 | /* Fall back to the default behavior: |
| 1430 | all instances are nonzero */ |
| 1431 | return 1; |
| 1432 | } |
| 1433 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1434 | res = PyEval_CallObject(func, (PyObject *)NULL); |
| 1435 | Py_DECREF(func); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1436 | if (res == NULL) |
| 1437 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1438 | if (!PyInt_Check(res)) { |
| 1439 | Py_DECREF(res); |
| 1440 | PyErr_SetString(PyExc_TypeError, |
| 1441 | "__nonzero__ should return an int"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1442 | return -1; |
| 1443 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1444 | outcome = PyInt_AsLong(res); |
| 1445 | Py_DECREF(res); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1446 | if (outcome < 0) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1447 | PyErr_SetString(PyExc_ValueError, |
| 1448 | "__nonzero__ should return >= 0"); |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1449 | return -1; |
| 1450 | } |
| 1451 | return outcome > 0; |
| 1452 | } |
| 1453 | |
| 1454 | UNARY(instance_invert, "__invert__") |
Guido van Rossum | 1899c2e | 1992-09-12 11:09:23 +0000 | [diff] [blame] | 1455 | UNARY(instance_int, "__int__") |
| 1456 | UNARY(instance_long, "__long__") |
| 1457 | UNARY(instance_float, "__float__") |
| 1458 | UNARY(instance_oct, "__oct__") |
| 1459 | UNARY(instance_hex, "__hex__") |
| 1460 | |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1461 | /* This version is for ternary calls only (z != None) */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1462 | static PyObject * |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1463 | instance_pow(v, w, z) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1464 | PyObject *v; |
| 1465 | PyObject *w; |
| 1466 | PyObject *z; |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1467 | { |
| 1468 | /* XXX Doesn't do coercions... */ |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1469 | PyObject *func; |
| 1470 | PyObject *args; |
| 1471 | PyObject *result; |
| 1472 | static PyObject *powstr; |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1473 | |
| 1474 | if (powstr == NULL) |
Guido van Rossum | a412d24 | 1997-01-18 07:59:12 +0000 | [diff] [blame] | 1475 | powstr = PyString_InternFromString("__pow__"); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1476 | func = PyObject_GetAttr(v, powstr); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1477 | if (func == NULL) |
| 1478 | return NULL; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1479 | args = Py_BuildValue("(OO)", w, z); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1480 | if (args == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1481 | Py_DECREF(func); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1482 | return NULL; |
| 1483 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1484 | result = PyEval_CallObject(func, args); |
| 1485 | Py_DECREF(func); |
| 1486 | Py_DECREF(args); |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1487 | return result; |
| 1488 | } |
| 1489 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1490 | static PyNumberMethods instance_as_number = { |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1491 | 0, /*nb_add*/ |
| 1492 | 0, /*nb_subtract*/ |
| 1493 | 0, /*nb_multiply*/ |
| 1494 | 0, /*nb_divide*/ |
| 1495 | 0, /*nb_remainder*/ |
| 1496 | 0, /*nb_divmod*/ |
Guido van Rossum | 52ca98a | 1994-09-05 07:32:29 +0000 | [diff] [blame] | 1497 | (ternaryfunc)instance_pow, /*nb_power*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1498 | (unaryfunc)instance_neg, /*nb_negative*/ |
| 1499 | (unaryfunc)instance_pos, /*nb_positive*/ |
| 1500 | (unaryfunc)instance_abs, /*nb_absolute*/ |
| 1501 | (inquiry)instance_nonzero, /*nb_nonzero*/ |
| 1502 | (unaryfunc)instance_invert, /*nb_invert*/ |
Guido van Rossum | 03093a2 | 1994-09-28 15:51:32 +0000 | [diff] [blame] | 1503 | 0, /*nb_lshift*/ |
| 1504 | 0, /*nb_rshift*/ |
| 1505 | 0, /*nb_and*/ |
| 1506 | 0, /*nb_xor*/ |
| 1507 | 0, /*nb_or*/ |
Guido van Rossum | 879c581 | 1995-01-10 15:24:06 +0000 | [diff] [blame] | 1508 | (coercion)instance_coerce, /*nb_coerce*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1509 | (unaryfunc)instance_int, /*nb_int*/ |
| 1510 | (unaryfunc)instance_long, /*nb_long*/ |
| 1511 | (unaryfunc)instance_float, /*nb_float*/ |
| 1512 | (unaryfunc)instance_oct, /*nb_oct*/ |
| 1513 | (unaryfunc)instance_hex, /*nb_hex*/ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1514 | }; |
| 1515 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1516 | PyTypeObject PyInstance_Type = { |
| 1517 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1518 | 0, |
Guido van Rossum | 21ed88c | 1991-04-04 10:42:10 +0000 | [diff] [blame] | 1519 | "instance", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1520 | sizeof(PyInstanceObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1521 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1522 | (destructor)instance_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 1523 | 0, /*tp_print*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1524 | 0, /*tp_getattr*/ |
| 1525 | 0, /*tp_setattr*/ |
| 1526 | instance_compare, /*tp_compare*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1527 | (reprfunc)instance_repr, /*tp_repr*/ |
Guido van Rossum | 04691fc | 1992-08-12 15:35:34 +0000 | [diff] [blame] | 1528 | &instance_as_number, /*tp_as_number*/ |
| 1529 | &instance_as_sequence, /*tp_as_sequence*/ |
| 1530 | &instance_as_mapping, /*tp_as_mapping*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1531 | (hashfunc)instance_hash, /*tp_hash*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1532 | 0, /*tp_call*/ |
| 1533 | 0, /*tp_str*/ |
| 1534 | (getattrofunc)instance_getattr, /*tp_getattro*/ |
| 1535 | (setattrofunc)instance_setattr, /*tp_setattro*/ |
Guido van Rossum | ee28c3a | 2000-02-28 15:03:15 +0000 | [diff] [blame] | 1536 | 0, /* tp_as_buffer */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1537 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1538 | 0, /* tp_doc */ |
| 1539 | (traverseproc)instance_traverse, /* tp_traverse */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1540 | }; |
| 1541 | |
| 1542 | |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1543 | /* Instance method objects are used for two purposes: |
| 1544 | (a) as bound instance methods (returned by instancename.methodname) |
| 1545 | (b) as unbound methods (returned by ClassName.methodname) |
| 1546 | In case (b), im_self is NULL |
| 1547 | */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1548 | |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1549 | static PyMethodObject *free_list; |
| 1550 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1551 | PyObject * |
| 1552 | PyMethod_New(func, self, class) |
| 1553 | PyObject *func; |
| 1554 | PyObject *self; |
| 1555 | PyObject *class; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1556 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1557 | register PyMethodObject *im; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 1558 | if (!PyCallable_Check(func)) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1559 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1560 | return NULL; |
| 1561 | } |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1562 | im = free_list; |
| 1563 | if (im != NULL) { |
| 1564 | free_list = (PyMethodObject *)(im->im_self); |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1565 | PyObject_INIT(im, &PyMethod_Type); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1566 | } |
| 1567 | else { |
| 1568 | im = PyObject_NEW(PyMethodObject, &PyMethod_Type); |
| 1569 | if (im == NULL) |
| 1570 | return NULL; |
| 1571 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1572 | Py_INCREF(func); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1573 | im->im_func = func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1574 | Py_XINCREF(self); |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1575 | im->im_self = self; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1576 | Py_INCREF(class); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1577 | im->im_class = class; |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1578 | PyObject_GC_Init(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1579 | return (PyObject *)im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1580 | } |
| 1581 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1582 | PyObject * |
| 1583 | PyMethod_Function(im) |
| 1584 | register PyObject *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1585 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1586 | if (!PyMethod_Check(im)) { |
| 1587 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1588 | return NULL; |
| 1589 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1590 | return ((PyMethodObject *)im)->im_func; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1593 | PyObject * |
| 1594 | PyMethod_Self(im) |
| 1595 | register PyObject *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1596 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1597 | if (!PyMethod_Check(im)) { |
| 1598 | PyErr_BadInternalCall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1599 | return NULL; |
| 1600 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1601 | return ((PyMethodObject *)im)->im_self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1602 | } |
| 1603 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1604 | PyObject * |
| 1605 | PyMethod_Class(im) |
| 1606 | register PyObject *im; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1607 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1608 | if (!PyMethod_Check(im)) { |
| 1609 | PyErr_BadInternalCall(); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1610 | return NULL; |
| 1611 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1612 | return ((PyMethodObject *)im)->im_class; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1613 | } |
| 1614 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1615 | /* Class method methods */ |
| 1616 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1617 | #define OFF(x) offsetof(PyMethodObject, x) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1618 | |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1619 | static struct memberlist instancemethod_memberlist[] = { |
| 1620 | {"im_func", T_OBJECT, OFF(im_func)}, |
| 1621 | {"im_self", T_OBJECT, OFF(im_self)}, |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1622 | {"im_class", T_OBJECT, OFF(im_class)}, |
Guido van Rossum | 6dabc98 | 1996-05-14 21:54:20 +0000 | [diff] [blame] | 1623 | /* Dummies that are not handled by getattr() except for __members__ */ |
| 1624 | {"__doc__", T_INT, 0}, |
| 1625 | {"__name__", T_INT, 0}, |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1626 | {NULL} /* Sentinel */ |
| 1627 | }; |
| 1628 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1629 | static PyObject * |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1630 | instancemethod_getattr(im, name) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1631 | register PyMethodObject *im; |
| 1632 | PyObject *name; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1633 | { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1634 | char *sname = PyString_AsString(name); |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1635 | if (sname[0] == '_') { |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 1636 | /* Inherit __name__ and __doc__ from the callable object |
| 1637 | implementing the method */ |
| 1638 | if (strcmp(sname, "__name__") == 0 || |
| 1639 | strcmp(sname, "__doc__") == 0) |
| 1640 | return PyObject_GetAttr(im->im_func, name); |
Guido van Rossum | 6dabc98 | 1996-05-14 21:54:20 +0000 | [diff] [blame] | 1641 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1642 | if (PyEval_GetRestricted()) { |
| 1643 | PyErr_SetString(PyExc_RuntimeError, |
| 1644 | "instance-method attributes not accessible in restricted mode"); |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 1645 | return NULL; |
| 1646 | } |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1647 | return PyMember_Get((char *)im, instancemethod_memberlist, sname); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 1648 | } |
| 1649 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1650 | static void |
Guido van Rossum | e8122f1 | 1991-05-05 20:03:07 +0000 | [diff] [blame] | 1651 | instancemethod_dealloc(im) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1652 | register PyMethodObject *im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1653 | { |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1654 | PyObject_GC_Fini(im); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1655 | Py_DECREF(im->im_func); |
| 1656 | Py_XDECREF(im->im_self); |
| 1657 | Py_DECREF(im->im_class); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1658 | im->im_self = (PyObject *)free_list; |
| 1659 | free_list = im; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1660 | } |
| 1661 | |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 1662 | static int |
| 1663 | instancemethod_compare(a, b) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1664 | PyMethodObject *a, *b; |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 1665 | { |
Guido van Rossum | e9df727 | 1995-04-06 14:46:51 +0000 | [diff] [blame] | 1666 | if (a->im_self != b->im_self) |
| 1667 | return (a->im_self < b->im_self) ? -1 : 1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1668 | return PyObject_Compare(a->im_func, b->im_func); |
Guido van Rossum | ebc8c51 | 1992-09-03 20:39:51 +0000 | [diff] [blame] | 1669 | } |
| 1670 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1671 | static PyObject * |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1672 | instancemethod_repr(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1673 | PyMethodObject *a; |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1674 | { |
| 1675 | char buf[240]; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1676 | PyInstanceObject *self = (PyInstanceObject *)(a->im_self); |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 1677 | PyObject *func = a->im_func; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1678 | PyClassObject *class = (PyClassObject *)(a->im_class); |
| 1679 | PyObject *fclassname, *iclassname, *funcname; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1680 | char *fcname, *icname, *fname; |
| 1681 | fclassname = class->cl_name; |
Guido van Rossum | 7859f87 | 1998-07-08 14:58:16 +0000 | [diff] [blame] | 1682 | if (PyFunction_Check(func)) { |
| 1683 | funcname = ((PyFunctionObject *)func)->func_name; |
| 1684 | Py_INCREF(funcname); |
| 1685 | } |
| 1686 | else { |
| 1687 | funcname = PyObject_GetAttrString(func,"__name__"); |
| 1688 | if (funcname == NULL) |
| 1689 | PyErr_Clear(); |
| 1690 | } |
| 1691 | if (funcname != NULL && PyString_Check(funcname)) |
| 1692 | fname = PyString_AS_STRING(funcname); |
| 1693 | else |
| 1694 | fname = "?"; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1695 | if (fclassname != NULL && PyString_Check(fclassname)) |
| 1696 | fcname = PyString_AsString(fclassname); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1697 | else |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1698 | fcname = "?"; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1699 | if (self == NULL) |
| 1700 | sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname); |
| 1701 | else { |
| 1702 | iclassname = self->in_class->cl_name; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1703 | if (iclassname != NULL && PyString_Check(iclassname)) |
| 1704 | icname = PyString_AsString(iclassname); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1705 | else |
| 1706 | icname = "?"; |
Fred Drake | a44d353 | 2000-06-30 15:01:00 +0000 | [diff] [blame] | 1707 | sprintf(buf, "<method %.60s.%.60s of %.60s instance at %p>", |
| 1708 | fcname, fname, icname, self); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1709 | } |
Guido van Rossum | 42636dc | 1999-10-11 14:03:12 +0000 | [diff] [blame] | 1710 | Py_XDECREF(funcname); |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1711 | return PyString_FromString(buf); |
Guido van Rossum | 2583165 | 1993-05-19 14:50:45 +0000 | [diff] [blame] | 1712 | } |
| 1713 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1714 | static long |
| 1715 | instancemethod_hash(a) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1716 | PyMethodObject *a; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1717 | { |
| 1718 | long x, y; |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1719 | if (a->im_self == NULL) |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1720 | x = PyObject_Hash(Py_None); |
Guido van Rossum | 81daa32 | 1993-05-20 14:24:46 +0000 | [diff] [blame] | 1721 | else |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1722 | x = PyObject_Hash(a->im_self); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1723 | if (x == -1) |
| 1724 | return -1; |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1725 | y = PyObject_Hash(a->im_func); |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 1726 | if (y == -1) |
| 1727 | return -1; |
| 1728 | return x ^ y; |
| 1729 | } |
| 1730 | |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1731 | static int |
| 1732 | instancemethod_traverse(PyMethodObject *im, visitproc visit, void *arg) |
| 1733 | { |
| 1734 | int err; |
| 1735 | if (im->im_func) { |
| 1736 | err = visit(im->im_func, arg); |
| 1737 | if (err) |
| 1738 | return err; |
| 1739 | } |
| 1740 | if (im->im_self) { |
| 1741 | err = visit(im->im_self, arg); |
| 1742 | if (err) |
| 1743 | return err; |
| 1744 | } |
| 1745 | if (im->im_class) { |
| 1746 | err = visit(im->im_class, arg); |
| 1747 | if (err) |
| 1748 | return err; |
| 1749 | } |
Jeremy Hylton | d22162b | 2000-06-23 17:14:56 +0000 | [diff] [blame] | 1750 | return 0; |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1751 | } |
| 1752 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1753 | PyTypeObject PyMethod_Type = { |
| 1754 | PyObject_HEAD_INIT(&PyType_Type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1755 | 0, |
Guido van Rossum | 569fce7 | 1991-04-16 08:38:43 +0000 | [diff] [blame] | 1756 | "instance method", |
Jeremy Hylton | c5007aa | 2000-06-30 05:02:53 +0000 | [diff] [blame] | 1757 | sizeof(PyMethodObject) + PyGC_HEAD_SIZE, |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1758 | 0, |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1759 | (destructor)instancemethod_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1760 | 0, /*tp_print*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1761 | 0, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1762 | 0, /*tp_setattr*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1763 | (cmpfunc)instancemethod_compare, /*tp_compare*/ |
| 1764 | (reprfunc)instancemethod_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1765 | 0, /*tp_as_number*/ |
| 1766 | 0, /*tp_as_sequence*/ |
| 1767 | 0, /*tp_as_mapping*/ |
Guido van Rossum | b6775db | 1994-08-01 11:34:53 +0000 | [diff] [blame] | 1768 | (hashfunc)instancemethod_hash, /*tp_hash*/ |
Guido van Rossum | 2878a69 | 1996-08-09 20:53:24 +0000 | [diff] [blame] | 1769 | 0, /*tp_call*/ |
| 1770 | 0, /*tp_str*/ |
| 1771 | (getattrofunc)instancemethod_getattr, /*tp_getattro*/ |
| 1772 | 0, /*tp_setattro*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1773 | 0, /* tp_as_buffer */ |
Jeremy Hylton | d08b4c4 | 2000-06-23 19:37:02 +0000 | [diff] [blame] | 1774 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_GC, /*tp_flags*/ |
Jeremy Hylton | 8caad49 | 2000-06-23 14:18:11 +0000 | [diff] [blame] | 1775 | 0, /* tp_doc */ |
| 1776 | (traverseproc)instancemethod_traverse, /* tp_traverse */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1777 | }; |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1778 | |
| 1779 | /* Clear out the free list */ |
| 1780 | |
| 1781 | void |
| 1782 | PyMethod_Fini() |
| 1783 | { |
| 1784 | while (free_list) { |
Guido van Rossum | b18618d | 2000-05-03 23:44:39 +0000 | [diff] [blame] | 1785 | PyMethodObject *im = free_list; |
| 1786 | free_list = (PyMethodObject *)(im->im_self); |
| 1787 | PyObject_DEL(im); |
Guido van Rossum | a0d349f | 1997-08-05 02:06:53 +0000 | [diff] [blame] | 1788 | } |
| 1789 | } |