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