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