Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 2 | /* Type object implementation */ |
| 3 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 4 | #include "Python.h" |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 5 | #include "structmember.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 6 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 7 | static PyMemberDef type_members[] = { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 8 | {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY}, |
| 9 | {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY}, |
| 10 | {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY}, |
| 11 | {"__doc__", T_STRING, offsetof(PyTypeObject, tp_doc), READONLY}, |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 12 | {"__weakrefoffset__", T_LONG, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 13 | offsetof(PyTypeObject, tp_weaklistoffset), READONLY}, |
| 14 | {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY}, |
| 15 | {"__dictoffset__", T_LONG, |
| 16 | offsetof(PyTypeObject, tp_dictoffset), READONLY}, |
| 17 | {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY}, |
| 18 | {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY}, |
| 19 | {0} |
| 20 | }; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 21 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 22 | static PyObject * |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 23 | type_name(PyTypeObject *type, void *context) |
| 24 | { |
| 25 | char *s; |
| 26 | |
| 27 | s = strrchr(type->tp_name, '.'); |
| 28 | if (s == NULL) |
| 29 | s = type->tp_name; |
| 30 | else |
| 31 | s++; |
| 32 | return PyString_FromString(s); |
| 33 | } |
| 34 | |
| 35 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 36 | type_module(PyTypeObject *type, void *context) |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 37 | { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 38 | PyObject *mod; |
| 39 | char *s; |
| 40 | |
| 41 | s = strrchr(type->tp_name, '.'); |
| 42 | if (s != NULL) |
| 43 | return PyString_FromStringAndSize(type->tp_name, |
| 44 | (int)(s - type->tp_name)); |
| 45 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 46 | return PyString_FromString("__builtin__"); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 47 | mod = PyDict_GetItemString(type->tp_dict, "__module__"); |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 48 | if (mod != NULL && PyString_Check(mod)) { |
| 49 | Py_INCREF(mod); |
| 50 | return mod; |
| 51 | } |
| 52 | PyErr_SetString(PyExc_AttributeError, "__module__"); |
| 53 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 56 | static int |
| 57 | type_set_module(PyTypeObject *type, PyObject *value, void *context) |
| 58 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 59 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) || |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 60 | strrchr(type->tp_name, '.')) { |
| 61 | PyErr_Format(PyExc_TypeError, |
| 62 | "can't set %s.__module__", type->tp_name); |
| 63 | return -1; |
| 64 | } |
| 65 | if (!value) { |
| 66 | PyErr_Format(PyExc_TypeError, |
| 67 | "can't delete %s.__module__", type->tp_name); |
| 68 | return -1; |
| 69 | } |
| 70 | return PyDict_SetItemString(type->tp_dict, "__module__", value); |
| 71 | } |
| 72 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 73 | static PyObject * |
| 74 | type_dict(PyTypeObject *type, void *context) |
| 75 | { |
| 76 | if (type->tp_dict == NULL) { |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 77 | Py_INCREF(Py_None); |
| 78 | return Py_None; |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 79 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 80 | return PyDictProxy_New(type->tp_dict); |
Guido van Rossum | 29ca26e | 1995-01-07 11:58:15 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 83 | static PyGetSetDef type_getsets[] = { |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 84 | {"__name__", (getter)type_name, NULL, NULL}, |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 85 | {"__module__", (getter)type_module, (setter)type_set_module, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 86 | {"__dict__", (getter)type_dict, NULL, NULL}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 87 | {0} |
| 88 | }; |
| 89 | |
Martin v. Löwis | 0163d6d | 2001-06-09 07:34:05 +0000 | [diff] [blame] | 90 | static int |
| 91 | type_compare(PyObject *v, PyObject *w) |
| 92 | { |
| 93 | /* This is called with type objects only. So we |
| 94 | can just compare the addresses. */ |
| 95 | Py_uintptr_t vv = (Py_uintptr_t)v; |
| 96 | Py_uintptr_t ww = (Py_uintptr_t)w; |
| 97 | return (vv < ww) ? -1 : (vv > ww) ? 1 : 0; |
| 98 | } |
| 99 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 100 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 101 | type_repr(PyTypeObject *type) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 102 | { |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 103 | PyObject *mod, *name, *rtn; |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 104 | char *kind; |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 105 | |
| 106 | mod = type_module(type, NULL); |
| 107 | if (mod == NULL) |
| 108 | PyErr_Clear(); |
| 109 | else if (!PyString_Check(mod)) { |
| 110 | Py_DECREF(mod); |
| 111 | mod = NULL; |
| 112 | } |
| 113 | name = type_name(type, NULL); |
| 114 | if (name == NULL) |
| 115 | return NULL; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 116 | |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 117 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 118 | kind = "class"; |
| 119 | else |
| 120 | kind = "type"; |
| 121 | |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 122 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) { |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 123 | rtn = PyString_FromFormat("<%s '%s.%s'>", |
| 124 | kind, |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 125 | PyString_AS_STRING(mod), |
| 126 | PyString_AS_STRING(name)); |
| 127 | } |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 128 | else |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 129 | rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 130 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 131 | Py_XDECREF(mod); |
| 132 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 133 | return rtn; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 134 | } |
| 135 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 136 | static PyObject * |
| 137 | type_call(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 138 | { |
| 139 | PyObject *obj; |
| 140 | |
| 141 | if (type->tp_new == NULL) { |
| 142 | PyErr_Format(PyExc_TypeError, |
| 143 | "cannot create '%.100s' instances", |
| 144 | type->tp_name); |
| 145 | return NULL; |
| 146 | } |
| 147 | |
Tim Peters | 3f996e7 | 2001-09-13 19:18:27 +0000 | [diff] [blame] | 148 | obj = type->tp_new(type, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 149 | if (obj != NULL) { |
Guido van Rossum | f76de62 | 2001-10-18 15:49:21 +0000 | [diff] [blame] | 150 | /* Ugly exception: when the call was type(something), |
| 151 | don't call tp_init on the result. */ |
| 152 | if (type == &PyType_Type && |
| 153 | PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 && |
| 154 | (kwds == NULL || |
| 155 | (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) |
| 156 | return obj; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 157 | type = obj->ob_type; |
| 158 | if (type->tp_init != NULL && |
| 159 | type->tp_init(obj, args, kwds) < 0) { |
| 160 | Py_DECREF(obj); |
| 161 | obj = NULL; |
| 162 | } |
| 163 | } |
| 164 | return obj; |
| 165 | } |
| 166 | |
| 167 | PyObject * |
| 168 | PyType_GenericAlloc(PyTypeObject *type, int nitems) |
| 169 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 170 | PyObject *obj; |
Tim Peters | f2a67da | 2001-10-07 03:54:51 +0000 | [diff] [blame] | 171 | const size_t size = _PyObject_VAR_SIZE(type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 172 | |
| 173 | if (PyType_IS_GC(type)) |
Tim Peters | 6d483d3 | 2001-10-06 21:27:34 +0000 | [diff] [blame] | 174 | obj = _PyObject_GC_Malloc(type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 175 | else |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 176 | obj = PyObject_MALLOC(size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 177 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 178 | if (obj == NULL) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 179 | return PyErr_NoMemory(); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 180 | |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 181 | memset(obj, '\0', size); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 182 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 183 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) |
| 184 | Py_INCREF(type); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 185 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 186 | if (type->tp_itemsize == 0) |
| 187 | PyObject_INIT(obj, type); |
| 188 | else |
| 189 | (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems); |
Tim Peters | 406fe3b | 2001-10-06 19:04:01 +0000 | [diff] [blame] | 190 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 191 | if (PyType_IS_GC(type)) |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 192 | _PyObject_GC_TRACK(obj); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 193 | return obj; |
| 194 | } |
| 195 | |
| 196 | PyObject * |
| 197 | PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 198 | { |
| 199 | return type->tp_alloc(type, 0); |
| 200 | } |
| 201 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 202 | /* Helpers for subtyping */ |
| 203 | |
| 204 | static int |
| 205 | subtype_traverse(PyObject *self, visitproc visit, void *arg) |
| 206 | { |
| 207 | PyTypeObject *type, *base; |
| 208 | traverseproc f; |
| 209 | int err; |
| 210 | |
| 211 | /* Find the nearest base with a different tp_traverse */ |
| 212 | type = self->ob_type; |
| 213 | base = type->tp_base; |
| 214 | while ((f = base->tp_traverse) == subtype_traverse) { |
| 215 | base = base->tp_base; |
| 216 | assert(base); |
| 217 | } |
| 218 | |
| 219 | if (type->tp_dictoffset != base->tp_dictoffset) { |
| 220 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 221 | if (dictptr && *dictptr) { |
| 222 | err = visit(*dictptr, arg); |
| 223 | if (err) |
| 224 | return err; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | if (f) |
| 229 | return f(self, visit, arg); |
| 230 | return 0; |
| 231 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 232 | |
| 233 | static void |
| 234 | subtype_dealloc(PyObject *self) |
| 235 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 236 | PyTypeObject *type, *base; |
| 237 | destructor f; |
| 238 | |
| 239 | /* This exists so we can DECREF self->ob_type */ |
| 240 | |
| 241 | /* Find the nearest base with a different tp_dealloc */ |
| 242 | type = self->ob_type; |
| 243 | base = type->tp_base; |
| 244 | while ((f = base->tp_dealloc) == subtype_dealloc) { |
| 245 | base = base->tp_base; |
| 246 | assert(base); |
| 247 | } |
| 248 | |
| 249 | /* If we added a dict, DECREF it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 250 | if (type->tp_dictoffset && !base->tp_dictoffset) { |
| 251 | PyObject **dictptr = _PyObject_GetDictPtr(self); |
| 252 | if (dictptr != NULL) { |
| 253 | PyObject *dict = *dictptr; |
| 254 | if (dict != NULL) { |
| 255 | Py_DECREF(dict); |
| 256 | *dictptr = NULL; |
| 257 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 261 | /* If we added weaklist, we clear it */ |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 262 | if (type->tp_weaklistoffset && !base->tp_weaklistoffset) |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 263 | PyObject_ClearWeakRefs(self); |
| 264 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 265 | /* Finalize GC if the base doesn't do GC and we do */ |
| 266 | if (PyType_IS_GC(type) && !PyType_IS_GC(base)) |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 267 | _PyObject_GC_UNTRACK(self); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 268 | |
| 269 | /* Call the base tp_dealloc() */ |
| 270 | assert(f); |
| 271 | f(self); |
| 272 | |
| 273 | /* Can't reference self beyond this point */ |
| 274 | if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 275 | Py_DECREF(type); |
| 276 | } |
| 277 | } |
| 278 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 279 | staticforward PyTypeObject *solid_base(PyTypeObject *type); |
| 280 | |
| 281 | typedef struct { |
| 282 | PyTypeObject type; |
| 283 | PyNumberMethods as_number; |
| 284 | PySequenceMethods as_sequence; |
| 285 | PyMappingMethods as_mapping; |
| 286 | PyBufferProcs as_buffer; |
| 287 | PyObject *name, *slots; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 288 | PyMemberDef members[1]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 289 | } etype; |
| 290 | |
| 291 | /* type test with subclassing support */ |
| 292 | |
| 293 | int |
| 294 | PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b) |
| 295 | { |
| 296 | PyObject *mro; |
| 297 | |
Guido van Rossum | 9478d07 | 2001-09-07 18:52:13 +0000 | [diff] [blame] | 298 | if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS)) |
| 299 | return b == a || b == &PyBaseObject_Type; |
| 300 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 301 | mro = a->tp_mro; |
| 302 | if (mro != NULL) { |
| 303 | /* Deal with multiple inheritance without recursion |
| 304 | by walking the MRO tuple */ |
| 305 | int i, n; |
| 306 | assert(PyTuple_Check(mro)); |
| 307 | n = PyTuple_GET_SIZE(mro); |
| 308 | for (i = 0; i < n; i++) { |
| 309 | if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b) |
| 310 | return 1; |
| 311 | } |
| 312 | return 0; |
| 313 | } |
| 314 | else { |
| 315 | /* a is not completely initilized yet; follow tp_base */ |
| 316 | do { |
| 317 | if (a == b) |
| 318 | return 1; |
| 319 | a = a->tp_base; |
| 320 | } while (a != NULL); |
| 321 | return b == &PyBaseObject_Type; |
| 322 | } |
| 323 | } |
| 324 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 325 | /* Internal routines to do a method lookup in the type |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 326 | without looking in the instance dictionary |
| 327 | (so we can't use PyObject_GetAttr) but still binding |
| 328 | it to the instance. The arguments are the object, |
| 329 | the method name as a C string, and the address of a |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 330 | static variable used to cache the interned Python string. |
| 331 | |
| 332 | Two variants: |
| 333 | |
| 334 | - lookup_maybe() returns NULL without raising an exception |
| 335 | when the _PyType_Lookup() call fails; |
| 336 | |
| 337 | - lookup_method() always raises an exception upon errors. |
| 338 | */ |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 339 | |
| 340 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 341 | lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj) |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 342 | { |
| 343 | PyObject *res; |
| 344 | |
| 345 | if (*attrobj == NULL) { |
| 346 | *attrobj = PyString_InternFromString(attrstr); |
| 347 | if (*attrobj == NULL) |
| 348 | return NULL; |
| 349 | } |
| 350 | res = _PyType_Lookup(self->ob_type, *attrobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 351 | if (res != NULL) { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 352 | descrgetfunc f; |
| 353 | if ((f = res->ob_type->tp_descr_get) == NULL) |
| 354 | Py_INCREF(res); |
| 355 | else |
| 356 | res = f(res, self, (PyObject *)(self->ob_type)); |
| 357 | } |
| 358 | return res; |
| 359 | } |
| 360 | |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 361 | static PyObject * |
| 362 | lookup_method(PyObject *self, char *attrstr, PyObject **attrobj) |
| 363 | { |
| 364 | PyObject *res = lookup_maybe(self, attrstr, attrobj); |
| 365 | if (res == NULL && !PyErr_Occurred()) |
| 366 | PyErr_SetObject(PyExc_AttributeError, *attrobj); |
| 367 | return res; |
| 368 | } |
| 369 | |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 370 | /* A variation of PyObject_CallMethod that uses lookup_method() |
| 371 | instead of PyObject_GetAttrString(). This uses the same convention |
| 372 | as lookup_method to cache the interned name string object. */ |
| 373 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 374 | static PyObject * |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 375 | call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 376 | { |
| 377 | va_list va; |
| 378 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 379 | va_start(va, format); |
| 380 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 381 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 382 | if (func == NULL) { |
| 383 | va_end(va); |
| 384 | if (!PyErr_Occurred()) |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 385 | PyErr_SetObject(PyExc_AttributeError, *nameobj); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 386 | return NULL; |
| 387 | } |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 388 | |
| 389 | if (format && *format) |
| 390 | args = Py_VaBuildValue(format, va); |
| 391 | else |
| 392 | args = PyTuple_New(0); |
| 393 | |
| 394 | va_end(va); |
| 395 | |
| 396 | if (args == NULL) |
| 397 | return NULL; |
| 398 | |
| 399 | assert(PyTuple_Check(args)); |
| 400 | retval = PyObject_Call(func, args, NULL); |
| 401 | |
| 402 | Py_DECREF(args); |
| 403 | Py_DECREF(func); |
| 404 | |
| 405 | return retval; |
| 406 | } |
| 407 | |
| 408 | /* Clone of call_method() that returns NotImplemented when the lookup fails. */ |
| 409 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 410 | static PyObject * |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 411 | call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...) |
| 412 | { |
| 413 | va_list va; |
| 414 | PyObject *args, *func = 0, *retval; |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 415 | va_start(va, format); |
| 416 | |
Guido van Rossum | da21c01 | 2001-10-03 00:50:18 +0000 | [diff] [blame] | 417 | func = lookup_maybe(o, name, nameobj); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 418 | if (func == NULL) { |
| 419 | va_end(va); |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 420 | if (!PyErr_Occurred()) { |
| 421 | Py_INCREF(Py_NotImplemented); |
| 422 | return Py_NotImplemented; |
| 423 | } |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 424 | return NULL; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | if (format && *format) |
| 428 | args = Py_VaBuildValue(format, va); |
| 429 | else |
| 430 | args = PyTuple_New(0); |
| 431 | |
| 432 | va_end(va); |
| 433 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 434 | if (args == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 435 | return NULL; |
| 436 | |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 437 | assert(PyTuple_Check(args)); |
| 438 | retval = PyObject_Call(func, args, NULL); |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 439 | |
| 440 | Py_DECREF(args); |
| 441 | Py_DECREF(func); |
| 442 | |
| 443 | return retval; |
| 444 | } |
| 445 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 446 | /* Method resolution order algorithm from "Putting Metaclasses to Work" |
| 447 | by Forman and Danforth (Addison-Wesley 1999). */ |
| 448 | |
| 449 | static int |
| 450 | conservative_merge(PyObject *left, PyObject *right) |
| 451 | { |
| 452 | int left_size; |
| 453 | int right_size; |
| 454 | int i, j, r, ok; |
| 455 | PyObject *temp, *rr; |
| 456 | |
| 457 | assert(PyList_Check(left)); |
| 458 | assert(PyList_Check(right)); |
| 459 | |
| 460 | again: |
| 461 | left_size = PyList_GET_SIZE(left); |
| 462 | right_size = PyList_GET_SIZE(right); |
| 463 | for (i = 0; i < left_size; i++) { |
| 464 | for (j = 0; j < right_size; j++) { |
| 465 | if (PyList_GET_ITEM(left, i) == |
| 466 | PyList_GET_ITEM(right, j)) { |
| 467 | /* found a merge point */ |
| 468 | temp = PyList_New(0); |
| 469 | if (temp == NULL) |
| 470 | return -1; |
| 471 | for (r = 0; r < j; r++) { |
| 472 | rr = PyList_GET_ITEM(right, r); |
| 473 | ok = PySequence_Contains(left, rr); |
| 474 | if (ok < 0) { |
| 475 | Py_DECREF(temp); |
| 476 | return -1; |
| 477 | } |
| 478 | if (!ok) { |
| 479 | ok = PyList_Append(temp, rr); |
| 480 | if (ok < 0) { |
| 481 | Py_DECREF(temp); |
| 482 | return -1; |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | ok = PyList_SetSlice(left, i, i, temp); |
| 487 | Py_DECREF(temp); |
| 488 | if (ok < 0) |
| 489 | return -1; |
| 490 | ok = PyList_SetSlice(right, 0, j+1, NULL); |
| 491 | if (ok < 0) |
| 492 | return -1; |
| 493 | goto again; |
| 494 | } |
| 495 | } |
| 496 | } |
| 497 | return PyList_SetSlice(left, left_size, left_size, right); |
| 498 | } |
| 499 | |
| 500 | static int |
| 501 | serious_order_disagreements(PyObject *left, PyObject *right) |
| 502 | { |
| 503 | return 0; /* XXX later -- for now, we cheat: "don't do that" */ |
| 504 | } |
| 505 | |
| 506 | static PyObject * |
| 507 | mro_implementation(PyTypeObject *type) |
| 508 | { |
| 509 | int i, n, ok; |
| 510 | PyObject *bases, *result; |
| 511 | |
| 512 | bases = type->tp_bases; |
| 513 | n = PyTuple_GET_SIZE(bases); |
| 514 | result = Py_BuildValue("[O]", (PyObject *)type); |
| 515 | if (result == NULL) |
| 516 | return NULL; |
| 517 | for (i = 0; i < n; i++) { |
| 518 | PyTypeObject *base = |
| 519 | (PyTypeObject *) PyTuple_GET_ITEM(bases, i); |
| 520 | PyObject *parentMRO = PySequence_List(base->tp_mro); |
| 521 | if (parentMRO == NULL) { |
| 522 | Py_DECREF(result); |
| 523 | return NULL; |
| 524 | } |
| 525 | if (serious_order_disagreements(result, parentMRO)) { |
| 526 | Py_DECREF(result); |
| 527 | return NULL; |
| 528 | } |
| 529 | ok = conservative_merge(result, parentMRO); |
| 530 | Py_DECREF(parentMRO); |
| 531 | if (ok < 0) { |
| 532 | Py_DECREF(result); |
| 533 | return NULL; |
| 534 | } |
| 535 | } |
| 536 | return result; |
| 537 | } |
| 538 | |
| 539 | static PyObject * |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 540 | mro_external(PyObject *self) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 541 | { |
| 542 | PyTypeObject *type = (PyTypeObject *)self; |
| 543 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 544 | return mro_implementation(type); |
| 545 | } |
| 546 | |
| 547 | static int |
| 548 | mro_internal(PyTypeObject *type) |
| 549 | { |
| 550 | PyObject *mro, *result, *tuple; |
| 551 | |
| 552 | if (type->ob_type == &PyType_Type) { |
| 553 | result = mro_implementation(type); |
| 554 | } |
| 555 | else { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 556 | static PyObject *mro_str; |
| 557 | mro = lookup_method((PyObject *)type, "mro", &mro_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 558 | if (mro == NULL) |
| 559 | return -1; |
| 560 | result = PyObject_CallObject(mro, NULL); |
| 561 | Py_DECREF(mro); |
| 562 | } |
| 563 | if (result == NULL) |
| 564 | return -1; |
| 565 | tuple = PySequence_Tuple(result); |
| 566 | Py_DECREF(result); |
| 567 | type->tp_mro = tuple; |
| 568 | return 0; |
| 569 | } |
| 570 | |
| 571 | |
| 572 | /* Calculate the best base amongst multiple base classes. |
| 573 | This is the first one that's on the path to the "solid base". */ |
| 574 | |
| 575 | static PyTypeObject * |
| 576 | best_base(PyObject *bases) |
| 577 | { |
| 578 | int i, n; |
| 579 | PyTypeObject *base, *winner, *candidate, *base_i; |
| 580 | |
| 581 | assert(PyTuple_Check(bases)); |
| 582 | n = PyTuple_GET_SIZE(bases); |
| 583 | assert(n > 0); |
| 584 | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0); |
| 585 | winner = &PyBaseObject_Type; |
| 586 | for (i = 0; i < n; i++) { |
| 587 | base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 588 | if (!PyType_Check((PyObject *)base_i)) { |
| 589 | PyErr_SetString( |
| 590 | PyExc_TypeError, |
| 591 | "bases must be types"); |
| 592 | return NULL; |
| 593 | } |
| 594 | if (base_i->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 595 | if (PyType_Ready(base_i) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 596 | return NULL; |
| 597 | } |
| 598 | candidate = solid_base(base_i); |
| 599 | if (PyType_IsSubtype(winner, candidate)) |
| 600 | ; |
| 601 | else if (PyType_IsSubtype(candidate, winner)) { |
| 602 | winner = candidate; |
| 603 | base = base_i; |
| 604 | } |
| 605 | else { |
| 606 | PyErr_SetString( |
| 607 | PyExc_TypeError, |
| 608 | "multiple bases have " |
| 609 | "instance lay-out conflict"); |
| 610 | return NULL; |
| 611 | } |
| 612 | } |
| 613 | assert(base != NULL); |
| 614 | return base; |
| 615 | } |
| 616 | |
| 617 | static int |
| 618 | extra_ivars(PyTypeObject *type, PyTypeObject *base) |
| 619 | { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 620 | size_t t_size = type->tp_basicsize; |
| 621 | size_t b_size = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 622 | |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 623 | assert(t_size >= b_size); /* Else type smaller than base! */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 624 | if (type->tp_itemsize || base->tp_itemsize) { |
| 625 | /* If itemsize is involved, stricter rules */ |
| 626 | return t_size != b_size || |
| 627 | type->tp_itemsize != base->tp_itemsize; |
| 628 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 629 | if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 && |
| 630 | type->tp_weaklistoffset + sizeof(PyObject *) == t_size) |
| 631 | t_size -= sizeof(PyObject *); |
| 632 | if (type->tp_dictoffset && base->tp_dictoffset == 0 && |
| 633 | type->tp_dictoffset + sizeof(PyObject *) == t_size) |
| 634 | t_size -= sizeof(PyObject *); |
| 635 | |
| 636 | return t_size != b_size; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 637 | } |
| 638 | |
| 639 | static PyTypeObject * |
| 640 | solid_base(PyTypeObject *type) |
| 641 | { |
| 642 | PyTypeObject *base; |
| 643 | |
| 644 | if (type->tp_base) |
| 645 | base = solid_base(type->tp_base); |
| 646 | else |
| 647 | base = &PyBaseObject_Type; |
| 648 | if (extra_ivars(type, base)) |
| 649 | return type; |
| 650 | else |
| 651 | return base; |
| 652 | } |
| 653 | |
| 654 | staticforward void object_dealloc(PyObject *); |
| 655 | staticforward int object_init(PyObject *, PyObject *, PyObject *); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 656 | staticforward int update_slot(PyTypeObject *, PyObject *); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 657 | staticforward void fixup_slot_dispatchers(PyTypeObject *); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 658 | |
| 659 | static PyObject * |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 660 | subtype_dict(PyObject *obj, void *context) |
| 661 | { |
| 662 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 663 | PyObject *dict; |
| 664 | |
| 665 | if (dictptr == NULL) { |
| 666 | PyErr_SetString(PyExc_AttributeError, |
| 667 | "This object has no __dict__"); |
| 668 | return NULL; |
| 669 | } |
| 670 | dict = *dictptr; |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 671 | if (dict == NULL) |
| 672 | *dictptr = dict = PyDict_New(); |
| 673 | Py_XINCREF(dict); |
| 674 | return dict; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 675 | } |
| 676 | |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 677 | static int |
| 678 | subtype_setdict(PyObject *obj, PyObject *value, void *context) |
| 679 | { |
| 680 | PyObject **dictptr = _PyObject_GetDictPtr(obj); |
| 681 | PyObject *dict; |
| 682 | |
| 683 | if (dictptr == NULL) { |
| 684 | PyErr_SetString(PyExc_AttributeError, |
| 685 | "This object has no __dict__"); |
| 686 | return -1; |
| 687 | } |
| 688 | if (value == NULL || !PyDict_Check(value)) { |
| 689 | PyErr_SetString(PyExc_TypeError, |
| 690 | "__dict__ must be set to a dictionary"); |
| 691 | return -1; |
| 692 | } |
| 693 | dict = *dictptr; |
| 694 | Py_INCREF(value); |
| 695 | *dictptr = value; |
| 696 | Py_XDECREF(dict); |
| 697 | return 0; |
| 698 | } |
| 699 | |
Neil Schemenauer | f23473f | 2001-10-21 22:28:58 +0000 | [diff] [blame] | 700 | static PyGetSetDef subtype_getsets[] = { |
Guido van Rossum | 6661be3 | 2001-10-26 04:26:12 +0000 | [diff] [blame] | 701 | {"__dict__", subtype_dict, subtype_setdict, NULL}, |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 702 | {0}, |
| 703 | }; |
| 704 | |
| 705 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 706 | type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds) |
| 707 | { |
| 708 | PyObject *name, *bases, *dict; |
| 709 | static char *kwlist[] = {"name", "bases", "dict", 0}; |
| 710 | PyObject *slots, *tmp; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 711 | PyTypeObject *type, *base, *tmptype, *winner; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 712 | etype *et; |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 713 | PyMemberDef *mp; |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 714 | int i, nbases, nslots, slotoffset, add_dict, add_weak; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 715 | |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 716 | assert(args != NULL && PyTuple_Check(args)); |
| 717 | assert(kwds == NULL || PyDict_Check(kwds)); |
| 718 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 719 | /* Special case: type(x) should return x->ob_type */ |
Tim Peters | 3abca12 | 2001-10-27 19:37:48 +0000 | [diff] [blame] | 720 | { |
| 721 | const int nargs = PyTuple_GET_SIZE(args); |
| 722 | const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds); |
| 723 | |
| 724 | if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) { |
| 725 | PyObject *x = PyTuple_GET_ITEM(args, 0); |
| 726 | Py_INCREF(x->ob_type); |
| 727 | return (PyObject *) x->ob_type; |
| 728 | } |
| 729 | |
| 730 | /* SF bug 475327 -- if that didn't trigger, we need 3 |
| 731 | arguments. but PyArg_ParseTupleAndKeywords below may give |
| 732 | a msg saying type() needs exactly 3. */ |
| 733 | if (nargs + nkwds != 3) { |
| 734 | PyErr_SetString(PyExc_TypeError, |
| 735 | "type() takes 1 or 3 arguments"); |
| 736 | return NULL; |
| 737 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 738 | } |
| 739 | |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 740 | /* Check arguments: (name, bases, dict) */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 741 | if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist, |
| 742 | &name, |
| 743 | &PyTuple_Type, &bases, |
| 744 | &PyDict_Type, &dict)) |
| 745 | return NULL; |
| 746 | |
| 747 | /* Determine the proper metatype to deal with this, |
| 748 | and check for metatype conflicts while we're at it. |
| 749 | Note that if some other metatype wins to contract, |
| 750 | it's possible that its instances are not types. */ |
| 751 | nbases = PyTuple_GET_SIZE(bases); |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 752 | winner = metatype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 753 | for (i = 0; i < nbases; i++) { |
| 754 | tmp = PyTuple_GET_ITEM(bases, i); |
| 755 | tmptype = tmp->ob_type; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 756 | if (PyType_IsSubtype(winner, tmptype)) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 757 | continue; |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 758 | if (PyType_IsSubtype(tmptype, winner)) { |
| 759 | winner = tmptype; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 760 | continue; |
| 761 | } |
| 762 | PyErr_SetString(PyExc_TypeError, |
| 763 | "metatype conflict among bases"); |
| 764 | return NULL; |
| 765 | } |
Guido van Rossum | 8d32c8b | 2001-08-17 11:18:38 +0000 | [diff] [blame] | 766 | if (winner != metatype) { |
| 767 | if (winner->tp_new != type_new) /* Pass it to the winner */ |
| 768 | return winner->tp_new(winner, args, kwds); |
| 769 | metatype = winner; |
| 770 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 771 | |
| 772 | /* Adjust for empty tuple bases */ |
| 773 | if (nbases == 0) { |
| 774 | bases = Py_BuildValue("(O)", &PyBaseObject_Type); |
| 775 | if (bases == NULL) |
| 776 | return NULL; |
| 777 | nbases = 1; |
| 778 | } |
| 779 | else |
| 780 | Py_INCREF(bases); |
| 781 | |
| 782 | /* XXX From here until type is allocated, "return NULL" leaks bases! */ |
| 783 | |
| 784 | /* Calculate best base, and check that all bases are type objects */ |
| 785 | base = best_base(bases); |
| 786 | if (base == NULL) |
| 787 | return NULL; |
| 788 | if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) { |
| 789 | PyErr_Format(PyExc_TypeError, |
| 790 | "type '%.100s' is not an acceptable base type", |
| 791 | base->tp_name); |
| 792 | return NULL; |
| 793 | } |
| 794 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 795 | /* Check for a __slots__ sequence variable in dict, and count it */ |
| 796 | slots = PyDict_GetItemString(dict, "__slots__"); |
| 797 | nslots = 0; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 798 | add_dict = 0; |
| 799 | add_weak = 0; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 800 | if (slots != NULL) { |
| 801 | /* Make it into a tuple */ |
| 802 | if (PyString_Check(slots)) |
| 803 | slots = Py_BuildValue("(O)", slots); |
| 804 | else |
| 805 | slots = PySequence_Tuple(slots); |
| 806 | if (slots == NULL) |
| 807 | return NULL; |
| 808 | nslots = PyTuple_GET_SIZE(slots); |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 809 | if (nslots > 0 && base->tp_itemsize != 0) { |
| 810 | PyErr_Format(PyExc_TypeError, |
| 811 | "nonempty __slots__ " |
| 812 | "not supported for subtype of '%s'", |
| 813 | base->tp_name); |
| 814 | return NULL; |
| 815 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 816 | for (i = 0; i < nslots; i++) { |
| 817 | if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) { |
| 818 | PyErr_SetString(PyExc_TypeError, |
| 819 | "__slots__ must be a sequence of strings"); |
| 820 | Py_DECREF(slots); |
| 821 | return NULL; |
| 822 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 823 | /* XXX Check against null bytes in name */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 824 | } |
| 825 | } |
| 826 | if (slots == NULL && base->tp_dictoffset == 0 && |
| 827 | (base->tp_setattro == PyObject_GenericSetAttr || |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 828 | base->tp_setattro == NULL)) { |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 829 | add_dict++; |
| 830 | } |
Guido van Rossum | c414187 | 2001-08-30 04:43:35 +0000 | [diff] [blame] | 831 | if (slots == NULL && base->tp_weaklistoffset == 0 && |
| 832 | base->tp_itemsize == 0) { |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 833 | nslots++; |
| 834 | add_weak++; |
| 835 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 836 | |
| 837 | /* XXX From here until type is safely allocated, |
| 838 | "return NULL" may leak slots! */ |
| 839 | |
| 840 | /* Allocate the type object */ |
| 841 | type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots); |
| 842 | if (type == NULL) |
| 843 | return NULL; |
| 844 | |
| 845 | /* Keep name and slots alive in the extended type object */ |
| 846 | et = (etype *)type; |
| 847 | Py_INCREF(name); |
| 848 | et->name = name; |
| 849 | et->slots = slots; |
| 850 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 851 | /* Initialize tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 852 | type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE | |
| 853 | Py_TPFLAGS_BASETYPE; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 854 | if (base->tp_flags & Py_TPFLAGS_HAVE_GC) |
| 855 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 856 | |
| 857 | /* It's a new-style number unless it specifically inherits any |
| 858 | old-style numeric behavior */ |
| 859 | if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) || |
| 860 | (base->tp_as_number == NULL)) |
| 861 | type->tp_flags |= Py_TPFLAGS_CHECKTYPES; |
| 862 | |
| 863 | /* Initialize essential fields */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 864 | type->tp_as_number = &et->as_number; |
| 865 | type->tp_as_sequence = &et->as_sequence; |
| 866 | type->tp_as_mapping = &et->as_mapping; |
| 867 | type->tp_as_buffer = &et->as_buffer; |
| 868 | type->tp_name = PyString_AS_STRING(name); |
| 869 | |
| 870 | /* Set tp_base and tp_bases */ |
| 871 | type->tp_bases = bases; |
| 872 | Py_INCREF(base); |
| 873 | type->tp_base = base; |
| 874 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 875 | /* Initialize tp_dict from passed-in dict */ |
| 876 | type->tp_dict = dict = PyDict_Copy(dict); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 877 | if (dict == NULL) { |
| 878 | Py_DECREF(type); |
| 879 | return NULL; |
| 880 | } |
| 881 | |
Guido van Rossum | c354221 | 2001-08-16 09:18:56 +0000 | [diff] [blame] | 882 | /* Set __module__ in the dict */ |
| 883 | if (PyDict_GetItemString(dict, "__module__") == NULL) { |
| 884 | tmp = PyEval_GetGlobals(); |
| 885 | if (tmp != NULL) { |
| 886 | tmp = PyDict_GetItemString(tmp, "__name__"); |
| 887 | if (tmp != NULL) { |
| 888 | if (PyDict_SetItemString(dict, "__module__", |
| 889 | tmp) < 0) |
| 890 | return NULL; |
| 891 | } |
| 892 | } |
| 893 | } |
| 894 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 895 | /* Set tp_doc to a copy of dict['__doc__'], if the latter is there |
| 896 | and is a string (tp_doc is a char* -- can't copy a general object |
| 897 | into it). |
| 898 | XXX What if it's a Unicode string? Don't know -- this ignores it. |
| 899 | */ |
| 900 | { |
| 901 | PyObject *doc = PyDict_GetItemString(dict, "__doc__"); |
| 902 | if (doc != NULL && PyString_Check(doc)) { |
| 903 | const size_t n = (size_t)PyString_GET_SIZE(doc); |
Tim Peters | 59f809d | 2001-10-04 05:43:02 +0000 | [diff] [blame] | 904 | type->tp_doc = (char *)PyObject_MALLOC(n+1); |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 905 | if (type->tp_doc == NULL) { |
| 906 | Py_DECREF(type); |
| 907 | return NULL; |
| 908 | } |
| 909 | memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1); |
| 910 | } |
| 911 | } |
| 912 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 913 | /* Special-case __new__: if it's a plain function, |
| 914 | make it a static function */ |
| 915 | tmp = PyDict_GetItemString(dict, "__new__"); |
| 916 | if (tmp != NULL && PyFunction_Check(tmp)) { |
| 917 | tmp = PyStaticMethod_New(tmp); |
| 918 | if (tmp == NULL) { |
| 919 | Py_DECREF(type); |
| 920 | return NULL; |
| 921 | } |
| 922 | PyDict_SetItemString(dict, "__new__", tmp); |
| 923 | Py_DECREF(tmp); |
| 924 | } |
| 925 | |
| 926 | /* Add descriptors for custom slots from __slots__, or for __dict__ */ |
| 927 | mp = et->members; |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 928 | slotoffset = base->tp_basicsize; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 929 | if (slots != NULL) { |
| 930 | for (i = 0; i < nslots; i++, mp++) { |
| 931 | mp->name = PyString_AS_STRING( |
| 932 | PyTuple_GET_ITEM(slots, i)); |
| 933 | mp->type = T_OBJECT; |
| 934 | mp->offset = slotoffset; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 935 | if (base->tp_weaklistoffset == 0 && |
| 936 | strcmp(mp->name, "__weakref__") == 0) |
| 937 | type->tp_weaklistoffset = slotoffset; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 938 | slotoffset += sizeof(PyObject *); |
| 939 | } |
| 940 | } |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 941 | else { |
| 942 | if (add_dict) { |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 943 | if (base->tp_itemsize) |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 944 | type->tp_dictoffset = |
| 945 | -(long)sizeof(PyObject *); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 946 | else |
| 947 | type->tp_dictoffset = slotoffset; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 948 | slotoffset += sizeof(PyObject *); |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 949 | type->tp_getset = subtype_getsets; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 950 | } |
| 951 | if (add_weak) { |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 952 | assert(!base->tp_itemsize); |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 953 | type->tp_weaklistoffset = slotoffset; |
| 954 | mp->name = "__weakref__"; |
| 955 | mp->type = T_OBJECT; |
| 956 | mp->offset = slotoffset; |
Tim Peters | 26f68f5 | 2001-09-18 00:23:33 +0000 | [diff] [blame] | 957 | mp->flags = READONLY; |
Guido van Rossum | 9676b22 | 2001-08-17 20:32:36 +0000 | [diff] [blame] | 958 | mp++; |
| 959 | slotoffset += sizeof(PyObject *); |
| 960 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 961 | } |
| 962 | type->tp_basicsize = slotoffset; |
Guido van Rossum | 6fb3fde | 2001-08-30 20:00:07 +0000 | [diff] [blame] | 963 | type->tp_itemsize = base->tp_itemsize; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 964 | type->tp_members = et->members; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 965 | |
| 966 | /* Special case some slots */ |
| 967 | if (type->tp_dictoffset != 0 || nslots > 0) { |
| 968 | if (base->tp_getattr == NULL && base->tp_getattro == NULL) |
| 969 | type->tp_getattro = PyObject_GenericGetAttr; |
| 970 | if (base->tp_setattr == NULL && base->tp_setattro == NULL) |
| 971 | type->tp_setattro = PyObject_GenericSetAttr; |
| 972 | } |
| 973 | type->tp_dealloc = subtype_dealloc; |
| 974 | |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 975 | /* Enable GC unless there are really no instance variables possible */ |
| 976 | if (!(type->tp_basicsize == sizeof(PyObject) && |
| 977 | type->tp_itemsize == 0)) |
| 978 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
| 979 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 980 | /* Always override allocation strategy to use regular heap */ |
| 981 | type->tp_alloc = PyType_GenericAlloc; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 982 | if (type->tp_flags & Py_TPFLAGS_HAVE_GC) { |
| 983 | type->tp_free = _PyObject_GC_Del; |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 984 | type->tp_traverse = subtype_traverse; |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 985 | type->tp_clear = base->tp_clear; |
| 986 | } |
| 987 | else |
| 988 | type->tp_free = _PyObject_Del; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 989 | |
| 990 | /* Initialize the rest */ |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 991 | if (PyType_Ready(type) < 0) { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 992 | Py_DECREF(type); |
| 993 | return NULL; |
| 994 | } |
| 995 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 996 | /* Put the proper slots in place */ |
| 997 | fixup_slot_dispatchers(type); |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 998 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 999 | return (PyObject *)type; |
| 1000 | } |
| 1001 | |
| 1002 | /* Internal API to look for a name through the MRO. |
| 1003 | This returns a borrowed reference, and doesn't set an exception! */ |
| 1004 | PyObject * |
| 1005 | _PyType_Lookup(PyTypeObject *type, PyObject *name) |
| 1006 | { |
| 1007 | int i, n; |
| 1008 | PyObject *mro, *res, *dict; |
| 1009 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1010 | /* Look in tp_dict of types in MRO */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1011 | mro = type->tp_mro; |
| 1012 | assert(PyTuple_Check(mro)); |
| 1013 | n = PyTuple_GET_SIZE(mro); |
| 1014 | for (i = 0; i < n; i++) { |
| 1015 | type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i); |
| 1016 | assert(PyType_Check(type)); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1017 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1018 | assert(dict && PyDict_Check(dict)); |
| 1019 | res = PyDict_GetItem(dict, name); |
| 1020 | if (res != NULL) |
| 1021 | return res; |
| 1022 | } |
| 1023 | return NULL; |
| 1024 | } |
| 1025 | |
| 1026 | /* This is similar to PyObject_GenericGetAttr(), |
| 1027 | but uses _PyType_Lookup() instead of just looking in type->tp_dict. */ |
| 1028 | static PyObject * |
| 1029 | type_getattro(PyTypeObject *type, PyObject *name) |
| 1030 | { |
| 1031 | PyTypeObject *metatype = type->ob_type; |
| 1032 | PyObject *descr, *res; |
| 1033 | descrgetfunc f; |
| 1034 | |
| 1035 | /* Initialize this type (we'll assume the metatype is initialized) */ |
| 1036 | if (type->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1037 | if (PyType_Ready(type) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1038 | return NULL; |
| 1039 | } |
| 1040 | |
| 1041 | /* Get a descriptor from the metatype */ |
| 1042 | descr = _PyType_Lookup(metatype, name); |
| 1043 | f = NULL; |
| 1044 | if (descr != NULL) { |
| 1045 | f = descr->ob_type->tp_descr_get; |
| 1046 | if (f != NULL && PyDescr_IsData(descr)) |
| 1047 | return f(descr, |
| 1048 | (PyObject *)type, (PyObject *)metatype); |
| 1049 | } |
| 1050 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1051 | /* Look in tp_dict of this type and its bases */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1052 | res = _PyType_Lookup(type, name); |
| 1053 | if (res != NULL) { |
| 1054 | f = res->ob_type->tp_descr_get; |
| 1055 | if (f != NULL) |
| 1056 | return f(res, (PyObject *)NULL, (PyObject *)type); |
| 1057 | Py_INCREF(res); |
| 1058 | return res; |
| 1059 | } |
| 1060 | |
| 1061 | /* Use the descriptor from the metatype */ |
| 1062 | if (f != NULL) { |
| 1063 | res = f(descr, (PyObject *)type, (PyObject *)metatype); |
| 1064 | return res; |
| 1065 | } |
| 1066 | if (descr != NULL) { |
| 1067 | Py_INCREF(descr); |
| 1068 | return descr; |
| 1069 | } |
| 1070 | |
| 1071 | /* Give up */ |
| 1072 | PyErr_Format(PyExc_AttributeError, |
| 1073 | "type object '%.50s' has no attribute '%.400s'", |
| 1074 | type->tp_name, PyString_AS_STRING(name)); |
| 1075 | return NULL; |
| 1076 | } |
| 1077 | |
| 1078 | static int |
| 1079 | type_setattro(PyTypeObject *type, PyObject *name, PyObject *value) |
| 1080 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1081 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 1082 | PyErr_Format( |
| 1083 | PyExc_TypeError, |
| 1084 | "can't set attributes of built-in/extension type '%s'", |
| 1085 | type->tp_name); |
| 1086 | return -1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1087 | } |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1088 | if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0) |
| 1089 | return -1; |
| 1090 | return update_slot(type, name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | static void |
| 1094 | type_dealloc(PyTypeObject *type) |
| 1095 | { |
| 1096 | etype *et; |
| 1097 | |
| 1098 | /* Assert this is a heap-allocated type object */ |
| 1099 | assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1100 | _PyObject_GC_UNTRACK(type); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1101 | PyObject_ClearWeakRefs((PyObject *)type); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1102 | et = (etype *)type; |
| 1103 | Py_XDECREF(type->tp_base); |
| 1104 | Py_XDECREF(type->tp_dict); |
| 1105 | Py_XDECREF(type->tp_bases); |
| 1106 | Py_XDECREF(type->tp_mro); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1107 | Py_XDECREF(type->tp_cache); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1108 | Py_XDECREF(type->tp_subclasses); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1109 | Py_XDECREF(et->name); |
| 1110 | Py_XDECREF(et->slots); |
| 1111 | type->ob_type->tp_free((PyObject *)type); |
| 1112 | } |
| 1113 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1114 | static PyObject * |
| 1115 | type_subclasses(PyTypeObject *type, PyObject *args_ignored) |
| 1116 | { |
| 1117 | PyObject *list, *raw, *ref; |
| 1118 | int i, n; |
| 1119 | |
| 1120 | list = PyList_New(0); |
| 1121 | if (list == NULL) |
| 1122 | return NULL; |
| 1123 | raw = type->tp_subclasses; |
| 1124 | if (raw == NULL) |
| 1125 | return list; |
| 1126 | assert(PyList_Check(raw)); |
| 1127 | n = PyList_GET_SIZE(raw); |
| 1128 | for (i = 0; i < n; i++) { |
| 1129 | ref = PyList_GET_ITEM(raw, i); |
Tim Peters | 4438338 | 2001-10-08 16:49:26 +0000 | [diff] [blame] | 1130 | assert(PyWeakref_CheckRef(ref)); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1131 | ref = PyWeakref_GET_OBJECT(ref); |
| 1132 | if (ref != Py_None) { |
| 1133 | if (PyList_Append(list, ref) < 0) { |
| 1134 | Py_DECREF(list); |
| 1135 | return NULL; |
| 1136 | } |
| 1137 | } |
| 1138 | } |
| 1139 | return list; |
| 1140 | } |
| 1141 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1142 | static PyMethodDef type_methods[] = { |
Martin v. Löwis | e3eb1f2 | 2001-08-16 13:15:00 +0000 | [diff] [blame] | 1143 | {"mro", (PyCFunction)mro_external, METH_NOARGS, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1144 | "mro() -> list\nreturn a type's method resolution order"}, |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1145 | {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS, |
| 1146 | "__subclasses__() -> list of immediate subclasses"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1147 | {0} |
| 1148 | }; |
| 1149 | |
| 1150 | static char type_doc[] = |
| 1151 | "type(object) -> the object's type\n" |
| 1152 | "type(name, bases, dict) -> a new type"; |
| 1153 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1154 | static int |
| 1155 | type_traverse(PyTypeObject *type, visitproc visit, void *arg) |
| 1156 | { |
| 1157 | etype *et; |
| 1158 | int err; |
| 1159 | |
| 1160 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1161 | return 0; |
| 1162 | |
| 1163 | et = (etype *)type; |
| 1164 | |
| 1165 | #define VISIT(SLOT) \ |
| 1166 | if (SLOT) { \ |
| 1167 | err = visit((PyObject *)(SLOT), arg); \ |
| 1168 | if (err) \ |
| 1169 | return err; \ |
| 1170 | } |
| 1171 | |
| 1172 | VISIT(type->tp_dict); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1173 | VISIT(type->tp_cache); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1174 | VISIT(type->tp_mro); |
| 1175 | VISIT(type->tp_bases); |
| 1176 | VISIT(type->tp_base); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1177 | VISIT(type->tp_subclasses); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1178 | VISIT(et->slots); |
| 1179 | |
| 1180 | #undef VISIT |
| 1181 | |
| 1182 | return 0; |
| 1183 | } |
| 1184 | |
| 1185 | static int |
| 1186 | type_clear(PyTypeObject *type) |
| 1187 | { |
| 1188 | etype *et; |
| 1189 | PyObject *tmp; |
| 1190 | |
| 1191 | if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 1192 | return 0; |
| 1193 | |
| 1194 | et = (etype *)type; |
| 1195 | |
| 1196 | #define CLEAR(SLOT) \ |
| 1197 | if (SLOT) { \ |
| 1198 | tmp = (PyObject *)(SLOT); \ |
| 1199 | SLOT = NULL; \ |
| 1200 | Py_DECREF(tmp); \ |
| 1201 | } |
| 1202 | |
| 1203 | CLEAR(type->tp_dict); |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1204 | CLEAR(type->tp_cache); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1205 | CLEAR(type->tp_mro); |
| 1206 | CLEAR(type->tp_bases); |
| 1207 | CLEAR(type->tp_base); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1208 | CLEAR(type->tp_subclasses); |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1209 | CLEAR(et->slots); |
| 1210 | |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1211 | if (type->tp_doc != NULL) { |
| 1212 | PyObject_FREE(type->tp_doc); |
| 1213 | type->tp_doc = NULL; |
| 1214 | } |
| 1215 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1216 | #undef CLEAR |
| 1217 | |
| 1218 | return 0; |
| 1219 | } |
| 1220 | |
| 1221 | static int |
| 1222 | type_is_gc(PyTypeObject *type) |
| 1223 | { |
| 1224 | return type->tp_flags & Py_TPFLAGS_HEAPTYPE; |
| 1225 | } |
| 1226 | |
Guido van Rossum | c0b618a | 1997-05-02 03:12:38 +0000 | [diff] [blame] | 1227 | PyTypeObject PyType_Type = { |
| 1228 | PyObject_HEAD_INIT(&PyType_Type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1229 | 0, /* ob_size */ |
| 1230 | "type", /* tp_name */ |
| 1231 | sizeof(etype), /* tp_basicsize */ |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1232 | sizeof(PyMemberDef), /* tp_itemsize */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1233 | (destructor)type_dealloc, /* tp_dealloc */ |
| 1234 | 0, /* tp_print */ |
| 1235 | 0, /* tp_getattr */ |
| 1236 | 0, /* tp_setattr */ |
| 1237 | type_compare, /* tp_compare */ |
| 1238 | (reprfunc)type_repr, /* tp_repr */ |
| 1239 | 0, /* tp_as_number */ |
| 1240 | 0, /* tp_as_sequence */ |
| 1241 | 0, /* tp_as_mapping */ |
| 1242 | (hashfunc)_Py_HashPointer, /* tp_hash */ |
| 1243 | (ternaryfunc)type_call, /* tp_call */ |
| 1244 | 0, /* tp_str */ |
| 1245 | (getattrofunc)type_getattro, /* tp_getattro */ |
| 1246 | (setattrofunc)type_setattro, /* tp_setattro */ |
| 1247 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1248 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 1249 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1250 | type_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1251 | (traverseproc)type_traverse, /* tp_traverse */ |
| 1252 | (inquiry)type_clear, /* tp_clear */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1253 | 0, /* tp_richcompare */ |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1254 | offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1255 | 0, /* tp_iter */ |
| 1256 | 0, /* tp_iternext */ |
| 1257 | type_methods, /* tp_methods */ |
| 1258 | type_members, /* tp_members */ |
| 1259 | type_getsets, /* tp_getset */ |
| 1260 | 0, /* tp_base */ |
| 1261 | 0, /* tp_dict */ |
| 1262 | 0, /* tp_descr_get */ |
| 1263 | 0, /* tp_descr_set */ |
| 1264 | offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */ |
| 1265 | 0, /* tp_init */ |
| 1266 | 0, /* tp_alloc */ |
| 1267 | type_new, /* tp_new */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 1268 | _PyObject_GC_Del, /* tp_free */ |
| 1269 | (inquiry)type_is_gc, /* tp_is_gc */ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1270 | }; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1271 | |
| 1272 | |
| 1273 | /* The base type of all types (eventually)... except itself. */ |
| 1274 | |
| 1275 | static int |
| 1276 | object_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 1277 | { |
| 1278 | return 0; |
| 1279 | } |
| 1280 | |
| 1281 | static void |
| 1282 | object_dealloc(PyObject *self) |
| 1283 | { |
| 1284 | self->ob_type->tp_free(self); |
| 1285 | } |
| 1286 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1287 | static PyObject * |
| 1288 | object_repr(PyObject *self) |
| 1289 | { |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1290 | PyTypeObject *type; |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1291 | PyObject *mod, *name, *rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1292 | |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1293 | type = self->ob_type; |
| 1294 | mod = type_module(type, NULL); |
| 1295 | if (mod == NULL) |
| 1296 | PyErr_Clear(); |
| 1297 | else if (!PyString_Check(mod)) { |
| 1298 | Py_DECREF(mod); |
| 1299 | mod = NULL; |
| 1300 | } |
| 1301 | name = type_name(type, NULL); |
| 1302 | if (name == NULL) |
| 1303 | return NULL; |
| 1304 | if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1305 | rtn = PyString_FromFormat("<%s.%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1306 | PyString_AS_STRING(mod), |
| 1307 | PyString_AS_STRING(name), |
| 1308 | self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1309 | else |
Guido van Rossum | ff0e6d6 | 2001-09-24 16:03:59 +0000 | [diff] [blame] | 1310 | rtn = PyString_FromFormat("<%s object at %p>", |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1311 | type->tp_name, self); |
Guido van Rossum | 76e6963 | 2001-08-16 18:52:43 +0000 | [diff] [blame] | 1312 | Py_XDECREF(mod); |
| 1313 | Py_DECREF(name); |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 1314 | return rtn; |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1315 | } |
| 1316 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1317 | static PyObject * |
| 1318 | object_str(PyObject *self) |
| 1319 | { |
| 1320 | unaryfunc f; |
| 1321 | |
| 1322 | f = self->ob_type->tp_repr; |
| 1323 | if (f == NULL) |
| 1324 | f = object_repr; |
| 1325 | return f(self); |
| 1326 | } |
| 1327 | |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1328 | static long |
| 1329 | object_hash(PyObject *self) |
| 1330 | { |
| 1331 | return _Py_HashPointer(self); |
| 1332 | } |
Guido van Rossum | 8e24818 | 2001-08-12 05:17:56 +0000 | [diff] [blame] | 1333 | |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1334 | static PyObject * |
| 1335 | object_get_class(PyObject *self, void *closure) |
| 1336 | { |
| 1337 | Py_INCREF(self->ob_type); |
| 1338 | return (PyObject *)(self->ob_type); |
| 1339 | } |
| 1340 | |
| 1341 | static int |
| 1342 | equiv_structs(PyTypeObject *a, PyTypeObject *b) |
| 1343 | { |
| 1344 | return a == b || |
| 1345 | (a != NULL && |
| 1346 | b != NULL && |
| 1347 | a->tp_basicsize == b->tp_basicsize && |
| 1348 | a->tp_itemsize == b->tp_itemsize && |
| 1349 | a->tp_dictoffset == b->tp_dictoffset && |
| 1350 | a->tp_weaklistoffset == b->tp_weaklistoffset && |
| 1351 | ((a->tp_flags & Py_TPFLAGS_HAVE_GC) == |
| 1352 | (b->tp_flags & Py_TPFLAGS_HAVE_GC))); |
| 1353 | } |
| 1354 | |
| 1355 | static int |
| 1356 | same_slots_added(PyTypeObject *a, PyTypeObject *b) |
| 1357 | { |
| 1358 | PyTypeObject *base = a->tp_base; |
| 1359 | int size; |
| 1360 | |
| 1361 | if (base != b->tp_base) |
| 1362 | return 0; |
| 1363 | if (equiv_structs(a, base) && equiv_structs(b, base)) |
| 1364 | return 1; |
| 1365 | size = base->tp_basicsize; |
| 1366 | if (a->tp_dictoffset == size && b->tp_dictoffset == size) |
| 1367 | size += sizeof(PyObject *); |
| 1368 | if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size) |
| 1369 | size += sizeof(PyObject *); |
| 1370 | return size == a->tp_basicsize && size == b->tp_basicsize; |
| 1371 | } |
| 1372 | |
| 1373 | static int |
| 1374 | object_set_class(PyObject *self, PyObject *value, void *closure) |
| 1375 | { |
| 1376 | PyTypeObject *old = self->ob_type; |
| 1377 | PyTypeObject *new, *newbase, *oldbase; |
| 1378 | |
| 1379 | if (!PyType_Check(value)) { |
| 1380 | PyErr_Format(PyExc_TypeError, |
| 1381 | "__class__ must be set to new-style class, not '%s' object", |
| 1382 | value->ob_type->tp_name); |
| 1383 | return -1; |
| 1384 | } |
| 1385 | new = (PyTypeObject *)value; |
| 1386 | newbase = new; |
| 1387 | oldbase = old; |
| 1388 | while (equiv_structs(newbase, newbase->tp_base)) |
| 1389 | newbase = newbase->tp_base; |
| 1390 | while (equiv_structs(oldbase, oldbase->tp_base)) |
| 1391 | oldbase = oldbase->tp_base; |
| 1392 | if (newbase != oldbase && |
| 1393 | (newbase->tp_base != oldbase->tp_base || |
| 1394 | !same_slots_added(newbase, oldbase))) { |
| 1395 | PyErr_Format(PyExc_TypeError, |
| 1396 | "__class__ assignment: " |
| 1397 | "'%s' object layout differs from '%s'", |
Tim Peters | 2f93e28 | 2001-10-04 05:27:00 +0000 | [diff] [blame] | 1398 | new->tp_name, |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1399 | old->tp_name); |
| 1400 | return -1; |
| 1401 | } |
| 1402 | if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 1403 | Py_INCREF(new); |
| 1404 | } |
| 1405 | self->ob_type = new; |
| 1406 | if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) { |
| 1407 | Py_DECREF(old); |
| 1408 | } |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static PyGetSetDef object_getsets[] = { |
| 1413 | {"__class__", object_get_class, object_set_class, |
| 1414 | "the object's class"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1415 | {0} |
| 1416 | }; |
| 1417 | |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1418 | static PyObject * |
| 1419 | object_reduce(PyObject *self, PyObject *args) |
| 1420 | { |
| 1421 | /* Call copy_reg._reduce(self) */ |
| 1422 | static PyObject *copy_reg_str; |
| 1423 | PyObject *copy_reg, *res; |
| 1424 | |
| 1425 | if (!copy_reg_str) { |
| 1426 | copy_reg_str = PyString_InternFromString("copy_reg"); |
| 1427 | if (copy_reg_str == NULL) |
| 1428 | return NULL; |
| 1429 | } |
| 1430 | copy_reg = PyImport_Import(copy_reg_str); |
| 1431 | if (!copy_reg) |
| 1432 | return NULL; |
| 1433 | res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self); |
| 1434 | Py_DECREF(copy_reg); |
| 1435 | return res; |
| 1436 | } |
| 1437 | |
| 1438 | static PyMethodDef object_methods[] = { |
| 1439 | {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"}, |
| 1440 | {0} |
| 1441 | }; |
| 1442 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1443 | PyTypeObject PyBaseObject_Type = { |
| 1444 | PyObject_HEAD_INIT(&PyType_Type) |
| 1445 | 0, /* ob_size */ |
| 1446 | "object", /* tp_name */ |
| 1447 | sizeof(PyObject), /* tp_basicsize */ |
| 1448 | 0, /* tp_itemsize */ |
| 1449 | (destructor)object_dealloc, /* tp_dealloc */ |
| 1450 | 0, /* tp_print */ |
| 1451 | 0, /* tp_getattr */ |
| 1452 | 0, /* tp_setattr */ |
| 1453 | 0, /* tp_compare */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1454 | object_repr, /* tp_repr */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1455 | 0, /* tp_as_number */ |
| 1456 | 0, /* tp_as_sequence */ |
| 1457 | 0, /* tp_as_mapping */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1458 | object_hash, /* tp_hash */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1459 | 0, /* tp_call */ |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1460 | object_str, /* tp_str */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1461 | PyObject_GenericGetAttr, /* tp_getattro */ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1462 | PyObject_GenericSetAttr, /* tp_setattro */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1463 | 0, /* tp_as_buffer */ |
| 1464 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
| 1465 | "The most base type", /* tp_doc */ |
| 1466 | 0, /* tp_traverse */ |
| 1467 | 0, /* tp_clear */ |
| 1468 | 0, /* tp_richcompare */ |
| 1469 | 0, /* tp_weaklistoffset */ |
| 1470 | 0, /* tp_iter */ |
| 1471 | 0, /* tp_iternext */ |
Guido van Rossum | 3926a63 | 2001-09-25 16:25:58 +0000 | [diff] [blame] | 1472 | object_methods, /* tp_methods */ |
Guido van Rossum | 5c294fb | 2001-09-25 03:43:42 +0000 | [diff] [blame] | 1473 | 0, /* tp_members */ |
| 1474 | object_getsets, /* tp_getset */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1475 | 0, /* tp_base */ |
| 1476 | 0, /* tp_dict */ |
| 1477 | 0, /* tp_descr_get */ |
| 1478 | 0, /* tp_descr_set */ |
| 1479 | 0, /* tp_dictoffset */ |
| 1480 | object_init, /* tp_init */ |
| 1481 | PyType_GenericAlloc, /* tp_alloc */ |
Guido van Rossum | c11e192 | 2001-08-09 19:38:15 +0000 | [diff] [blame] | 1482 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 9475a23 | 2001-10-05 20:51:39 +0000 | [diff] [blame] | 1483 | _PyObject_Del, /* tp_free */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1484 | }; |
| 1485 | |
| 1486 | |
| 1487 | /* Initialize the __dict__ in a type object */ |
| 1488 | |
| 1489 | static int |
| 1490 | add_methods(PyTypeObject *type, PyMethodDef *meth) |
| 1491 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1492 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1493 | |
| 1494 | for (; meth->ml_name != NULL; meth++) { |
| 1495 | PyObject *descr; |
| 1496 | if (PyDict_GetItemString(dict, meth->ml_name)) |
| 1497 | continue; |
| 1498 | descr = PyDescr_NewMethod(type, meth); |
| 1499 | if (descr == NULL) |
| 1500 | return -1; |
| 1501 | if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0) |
| 1502 | return -1; |
| 1503 | Py_DECREF(descr); |
| 1504 | } |
| 1505 | return 0; |
| 1506 | } |
| 1507 | |
| 1508 | static int |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 1509 | add_members(PyTypeObject *type, PyMemberDef *memb) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1510 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1511 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1512 | |
| 1513 | for (; memb->name != NULL; memb++) { |
| 1514 | PyObject *descr; |
| 1515 | if (PyDict_GetItemString(dict, memb->name)) |
| 1516 | continue; |
| 1517 | descr = PyDescr_NewMember(type, memb); |
| 1518 | if (descr == NULL) |
| 1519 | return -1; |
| 1520 | if (PyDict_SetItemString(dict, memb->name, descr) < 0) |
| 1521 | return -1; |
| 1522 | Py_DECREF(descr); |
| 1523 | } |
| 1524 | return 0; |
| 1525 | } |
| 1526 | |
| 1527 | static int |
Guido van Rossum | 32d34c8 | 2001-09-20 21:45:26 +0000 | [diff] [blame] | 1528 | add_getset(PyTypeObject *type, PyGetSetDef *gsp) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1529 | { |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1530 | PyObject *dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1531 | |
| 1532 | for (; gsp->name != NULL; gsp++) { |
| 1533 | PyObject *descr; |
| 1534 | if (PyDict_GetItemString(dict, gsp->name)) |
| 1535 | continue; |
| 1536 | descr = PyDescr_NewGetSet(type, gsp); |
| 1537 | |
| 1538 | if (descr == NULL) |
| 1539 | return -1; |
| 1540 | if (PyDict_SetItemString(dict, gsp->name, descr) < 0) |
| 1541 | return -1; |
| 1542 | Py_DECREF(descr); |
| 1543 | } |
| 1544 | return 0; |
| 1545 | } |
| 1546 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1547 | static void |
| 1548 | inherit_special(PyTypeObject *type, PyTypeObject *base) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1549 | { |
| 1550 | int oldsize, newsize; |
| 1551 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1552 | /* Special flag magic */ |
| 1553 | if (!type->tp_as_buffer && base->tp_as_buffer) { |
| 1554 | type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1555 | type->tp_flags |= |
| 1556 | base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER; |
| 1557 | } |
| 1558 | if (!type->tp_as_sequence && base->tp_as_sequence) { |
| 1559 | type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1560 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN; |
| 1561 | } |
| 1562 | if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) != |
| 1563 | (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) { |
| 1564 | if ((!type->tp_as_number && base->tp_as_number) || |
| 1565 | (!type->tp_as_sequence && base->tp_as_sequence)) { |
| 1566 | type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1567 | if (!type->tp_as_number && !type->tp_as_sequence) { |
| 1568 | type->tp_flags |= base->tp_flags & |
| 1569 | Py_TPFLAGS_HAVE_INPLACEOPS; |
| 1570 | } |
| 1571 | } |
| 1572 | /* Wow */ |
| 1573 | } |
| 1574 | if (!type->tp_as_number && base->tp_as_number) { |
| 1575 | type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES; |
| 1576 | type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES; |
| 1577 | } |
| 1578 | |
| 1579 | /* Copying basicsize is connected to the GC flags */ |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1580 | oldsize = base->tp_basicsize; |
| 1581 | newsize = type->tp_basicsize ? type->tp_basicsize : oldsize; |
| 1582 | if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) && |
| 1583 | (base->tp_flags & Py_TPFLAGS_HAVE_GC) && |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1584 | (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) && |
| 1585 | (!type->tp_traverse && !type->tp_clear)) { |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1586 | type->tp_flags |= Py_TPFLAGS_HAVE_GC; |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1587 | if (type->tp_traverse == NULL) |
| 1588 | type->tp_traverse = base->tp_traverse; |
| 1589 | if (type->tp_clear == NULL) |
| 1590 | type->tp_clear = base->tp_clear; |
| 1591 | } |
| 1592 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 1593 | if (base != &PyBaseObject_Type || |
| 1594 | (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) { |
| 1595 | if (type->tp_new == NULL) |
| 1596 | type->tp_new = base->tp_new; |
| 1597 | } |
| 1598 | } |
Neil Schemenauer | c806c88 | 2001-08-29 23:54:54 +0000 | [diff] [blame] | 1599 | type->tp_basicsize = newsize; |
Guido van Rossum | 4dd64ab | 2001-08-14 20:04:48 +0000 | [diff] [blame] | 1600 | |
| 1601 | /* Copy other non-function slots */ |
| 1602 | |
| 1603 | #undef COPYVAL |
| 1604 | #define COPYVAL(SLOT) \ |
| 1605 | if (type->SLOT == 0) type->SLOT = base->SLOT |
| 1606 | |
| 1607 | COPYVAL(tp_itemsize); |
| 1608 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) { |
| 1609 | COPYVAL(tp_weaklistoffset); |
| 1610 | } |
| 1611 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 1612 | COPYVAL(tp_dictoffset); |
| 1613 | } |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1614 | } |
| 1615 | |
| 1616 | static void |
| 1617 | inherit_slots(PyTypeObject *type, PyTypeObject *base) |
| 1618 | { |
| 1619 | PyTypeObject *basebase; |
| 1620 | |
| 1621 | #undef SLOTDEFINED |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1622 | #undef COPYSLOT |
| 1623 | #undef COPYNUM |
| 1624 | #undef COPYSEQ |
| 1625 | #undef COPYMAP |
Guido van Rossum | 5af588b | 2001-10-12 14:13:21 +0000 | [diff] [blame] | 1626 | #undef COPYBUF |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1627 | |
| 1628 | #define SLOTDEFINED(SLOT) \ |
| 1629 | (base->SLOT != 0 && \ |
| 1630 | (basebase == NULL || base->SLOT != basebase->SLOT)) |
| 1631 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1632 | #define COPYSLOT(SLOT) \ |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1633 | if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1634 | |
| 1635 | #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT) |
| 1636 | #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT) |
| 1637 | #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT) |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 1638 | #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1639 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1640 | /* This won't inherit indirect slots (from tp_as_number etc.) |
| 1641 | if type doesn't provide the space. */ |
| 1642 | |
| 1643 | if (type->tp_as_number != NULL && base->tp_as_number != NULL) { |
| 1644 | basebase = base->tp_base; |
| 1645 | if (basebase->tp_as_number == NULL) |
| 1646 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1647 | COPYNUM(nb_add); |
| 1648 | COPYNUM(nb_subtract); |
| 1649 | COPYNUM(nb_multiply); |
| 1650 | COPYNUM(nb_divide); |
| 1651 | COPYNUM(nb_remainder); |
| 1652 | COPYNUM(nb_divmod); |
| 1653 | COPYNUM(nb_power); |
| 1654 | COPYNUM(nb_negative); |
| 1655 | COPYNUM(nb_positive); |
| 1656 | COPYNUM(nb_absolute); |
| 1657 | COPYNUM(nb_nonzero); |
| 1658 | COPYNUM(nb_invert); |
| 1659 | COPYNUM(nb_lshift); |
| 1660 | COPYNUM(nb_rshift); |
| 1661 | COPYNUM(nb_and); |
| 1662 | COPYNUM(nb_xor); |
| 1663 | COPYNUM(nb_or); |
| 1664 | COPYNUM(nb_coerce); |
| 1665 | COPYNUM(nb_int); |
| 1666 | COPYNUM(nb_long); |
| 1667 | COPYNUM(nb_float); |
| 1668 | COPYNUM(nb_oct); |
| 1669 | COPYNUM(nb_hex); |
| 1670 | COPYNUM(nb_inplace_add); |
| 1671 | COPYNUM(nb_inplace_subtract); |
| 1672 | COPYNUM(nb_inplace_multiply); |
| 1673 | COPYNUM(nb_inplace_divide); |
| 1674 | COPYNUM(nb_inplace_remainder); |
| 1675 | COPYNUM(nb_inplace_power); |
| 1676 | COPYNUM(nb_inplace_lshift); |
| 1677 | COPYNUM(nb_inplace_rshift); |
| 1678 | COPYNUM(nb_inplace_and); |
| 1679 | COPYNUM(nb_inplace_xor); |
| 1680 | COPYNUM(nb_inplace_or); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 1681 | if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) { |
| 1682 | COPYNUM(nb_true_divide); |
| 1683 | COPYNUM(nb_floor_divide); |
| 1684 | COPYNUM(nb_inplace_true_divide); |
| 1685 | COPYNUM(nb_inplace_floor_divide); |
| 1686 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1687 | } |
| 1688 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1689 | if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) { |
| 1690 | basebase = base->tp_base; |
| 1691 | if (basebase->tp_as_sequence == NULL) |
| 1692 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1693 | COPYSEQ(sq_length); |
| 1694 | COPYSEQ(sq_concat); |
| 1695 | COPYSEQ(sq_repeat); |
| 1696 | COPYSEQ(sq_item); |
| 1697 | COPYSEQ(sq_slice); |
| 1698 | COPYSEQ(sq_ass_item); |
| 1699 | COPYSEQ(sq_ass_slice); |
| 1700 | COPYSEQ(sq_contains); |
| 1701 | COPYSEQ(sq_inplace_concat); |
| 1702 | COPYSEQ(sq_inplace_repeat); |
| 1703 | } |
| 1704 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1705 | if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) { |
| 1706 | basebase = base->tp_base; |
| 1707 | if (basebase->tp_as_mapping == NULL) |
| 1708 | basebase = NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1709 | COPYMAP(mp_length); |
| 1710 | COPYMAP(mp_subscript); |
| 1711 | COPYMAP(mp_ass_subscript); |
| 1712 | } |
| 1713 | |
Tim Peters | fc57ccb | 2001-10-12 02:38:24 +0000 | [diff] [blame] | 1714 | if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) { |
| 1715 | basebase = base->tp_base; |
| 1716 | if (basebase->tp_as_buffer == NULL) |
| 1717 | basebase = NULL; |
| 1718 | COPYBUF(bf_getreadbuffer); |
| 1719 | COPYBUF(bf_getwritebuffer); |
| 1720 | COPYBUF(bf_getsegcount); |
| 1721 | COPYBUF(bf_getcharbuffer); |
| 1722 | } |
| 1723 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1724 | basebase = base->tp_base; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1725 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1726 | COPYSLOT(tp_dealloc); |
| 1727 | COPYSLOT(tp_print); |
| 1728 | if (type->tp_getattr == NULL && type->tp_getattro == NULL) { |
| 1729 | type->tp_getattr = base->tp_getattr; |
| 1730 | type->tp_getattro = base->tp_getattro; |
| 1731 | } |
| 1732 | if (type->tp_setattr == NULL && type->tp_setattro == NULL) { |
| 1733 | type->tp_setattr = base->tp_setattr; |
| 1734 | type->tp_setattro = base->tp_setattro; |
| 1735 | } |
| 1736 | /* tp_compare see tp_richcompare */ |
| 1737 | COPYSLOT(tp_repr); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1738 | /* tp_hash see tp_richcompare */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1739 | COPYSLOT(tp_call); |
| 1740 | COPYSLOT(tp_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1741 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1742 | if (type->tp_compare == NULL && |
| 1743 | type->tp_richcompare == NULL && |
| 1744 | type->tp_hash == NULL) |
| 1745 | { |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1746 | type->tp_compare = base->tp_compare; |
| 1747 | type->tp_richcompare = base->tp_richcompare; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 1748 | type->tp_hash = base->tp_hash; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1749 | } |
| 1750 | } |
| 1751 | else { |
| 1752 | COPYSLOT(tp_compare); |
| 1753 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1754 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) { |
| 1755 | COPYSLOT(tp_iter); |
| 1756 | COPYSLOT(tp_iternext); |
| 1757 | } |
| 1758 | if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) { |
| 1759 | COPYSLOT(tp_descr_get); |
| 1760 | COPYSLOT(tp_descr_set); |
| 1761 | COPYSLOT(tp_dictoffset); |
| 1762 | COPYSLOT(tp_init); |
| 1763 | COPYSLOT(tp_alloc); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1764 | COPYSLOT(tp_free); |
| 1765 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1766 | } |
| 1767 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1768 | staticforward int add_operators(PyTypeObject *); |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1769 | staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type); |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1770 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1771 | int |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1772 | PyType_Ready(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1773 | { |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1774 | PyObject *dict, *bases; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1775 | PyTypeObject *base; |
| 1776 | int i, n; |
| 1777 | |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1778 | if (type->tp_flags & Py_TPFLAGS_READY) { |
| 1779 | assert(type->tp_dict != NULL); |
| 1780 | return 0; |
| 1781 | } |
| 1782 | assert((type->tp_flags & Py_TPFLAGS_READYING) == 0); |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1783 | |
| 1784 | type->tp_flags |= Py_TPFLAGS_READYING; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1785 | |
| 1786 | /* Initialize tp_base (defaults to BaseObject unless that's us) */ |
| 1787 | base = type->tp_base; |
| 1788 | if (base == NULL && type != &PyBaseObject_Type) |
| 1789 | base = type->tp_base = &PyBaseObject_Type; |
| 1790 | |
| 1791 | /* Initialize tp_bases */ |
| 1792 | bases = type->tp_bases; |
| 1793 | if (bases == NULL) { |
| 1794 | if (base == NULL) |
| 1795 | bases = PyTuple_New(0); |
| 1796 | else |
| 1797 | bases = Py_BuildValue("(O)", base); |
| 1798 | if (bases == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1799 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1800 | type->tp_bases = bases; |
| 1801 | } |
| 1802 | |
| 1803 | /* Initialize the base class */ |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 1804 | if (base && base->tp_dict == NULL) { |
Guido van Rossum | 528b7eb | 2001-08-07 17:24:28 +0000 | [diff] [blame] | 1805 | if (PyType_Ready(base) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1806 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1807 | } |
| 1808 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1809 | /* Initialize tp_dict */ |
| 1810 | dict = type->tp_dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1811 | if (dict == NULL) { |
| 1812 | dict = PyDict_New(); |
| 1813 | if (dict == NULL) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1814 | goto error; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1815 | type->tp_dict = dict; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 1818 | /* Add type-specific descriptors to tp_dict */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1819 | if (add_operators(type) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1820 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1821 | if (type->tp_methods != NULL) { |
| 1822 | if (add_methods(type, type->tp_methods) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1823 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1824 | } |
| 1825 | if (type->tp_members != NULL) { |
| 1826 | if (add_members(type, type->tp_members) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1827 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1828 | } |
| 1829 | if (type->tp_getset != NULL) { |
| 1830 | if (add_getset(type, type->tp_getset) < 0) |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1831 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1834 | /* Calculate method resolution order */ |
| 1835 | if (mro_internal(type) < 0) { |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1836 | goto error; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1837 | } |
| 1838 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1839 | /* Inherit special flags from dominant base */ |
| 1840 | if (type->tp_base != NULL) |
| 1841 | inherit_special(type, type->tp_base); |
| 1842 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1843 | /* Initialize tp_dict properly */ |
Guido van Rossum | 2f3ca6e | 2001-10-15 21:05:10 +0000 | [diff] [blame] | 1844 | bases = type->tp_mro; |
| 1845 | assert(bases != NULL); |
| 1846 | assert(PyTuple_Check(bases)); |
| 1847 | n = PyTuple_GET_SIZE(bases); |
| 1848 | for (i = 1; i < n; i++) { |
| 1849 | base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i); |
| 1850 | assert(PyType_Check(base)); |
| 1851 | inherit_slots(type, base); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1852 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1853 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1854 | /* Some more special stuff */ |
| 1855 | base = type->tp_base; |
| 1856 | if (base != NULL) { |
| 1857 | if (type->tp_as_number == NULL) |
| 1858 | type->tp_as_number = base->tp_as_number; |
| 1859 | if (type->tp_as_sequence == NULL) |
| 1860 | type->tp_as_sequence = base->tp_as_sequence; |
| 1861 | if (type->tp_as_mapping == NULL) |
| 1862 | type->tp_as_mapping = base->tp_as_mapping; |
| 1863 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1864 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1865 | /* Link into each base class's list of subclasses */ |
| 1866 | bases = type->tp_bases; |
| 1867 | n = PyTuple_GET_SIZE(bases); |
| 1868 | for (i = 0; i < n; i++) { |
| 1869 | base = (PyTypeObject *) PyTuple_GET_ITEM(bases, i); |
| 1870 | if (add_subclass((PyTypeObject *)base, type) < 0) |
| 1871 | goto error; |
| 1872 | } |
| 1873 | |
Guido van Rossum | 13d52f0 | 2001-08-10 21:24:08 +0000 | [diff] [blame] | 1874 | /* All done -- set the ready flag */ |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1875 | assert(type->tp_dict != NULL); |
| 1876 | type->tp_flags = |
| 1877 | (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1878 | return 0; |
Guido van Rossum | d614f97 | 2001-08-10 17:39:49 +0000 | [diff] [blame] | 1879 | |
| 1880 | error: |
| 1881 | type->tp_flags &= ~Py_TPFLAGS_READYING; |
| 1882 | return -1; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1883 | } |
| 1884 | |
Guido van Rossum | 1c45073 | 2001-10-08 15:18:27 +0000 | [diff] [blame] | 1885 | static int |
| 1886 | add_subclass(PyTypeObject *base, PyTypeObject *type) |
| 1887 | { |
| 1888 | int i; |
| 1889 | PyObject *list, *ref, *new; |
| 1890 | |
| 1891 | list = base->tp_subclasses; |
| 1892 | if (list == NULL) { |
| 1893 | base->tp_subclasses = list = PyList_New(0); |
| 1894 | if (list == NULL) |
| 1895 | return -1; |
| 1896 | } |
| 1897 | assert(PyList_Check(list)); |
| 1898 | new = PyWeakref_NewRef((PyObject *)type, NULL); |
| 1899 | i = PyList_GET_SIZE(list); |
| 1900 | while (--i >= 0) { |
| 1901 | ref = PyList_GET_ITEM(list, i); |
| 1902 | assert(PyWeakref_CheckRef(ref)); |
| 1903 | if (PyWeakref_GET_OBJECT(ref) == Py_None) |
| 1904 | return PyList_SetItem(list, i, new); |
| 1905 | } |
| 1906 | i = PyList_Append(list, new); |
| 1907 | Py_DECREF(new); |
| 1908 | return i; |
| 1909 | } |
| 1910 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1911 | |
| 1912 | /* Generic wrappers for overloadable 'operators' such as __getitem__ */ |
| 1913 | |
| 1914 | /* There's a wrapper *function* for each distinct function typedef used |
| 1915 | for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a |
| 1916 | wrapper *table* for each distinct operation (e.g. __len__, __add__). |
| 1917 | Most tables have only one entry; the tables for binary operators have two |
| 1918 | entries, one regular and one with reversed arguments. */ |
| 1919 | |
| 1920 | static PyObject * |
| 1921 | wrap_inquiry(PyObject *self, PyObject *args, void *wrapped) |
| 1922 | { |
| 1923 | inquiry func = (inquiry)wrapped; |
| 1924 | int res; |
| 1925 | |
| 1926 | if (!PyArg_ParseTuple(args, "")) |
| 1927 | return NULL; |
| 1928 | res = (*func)(self); |
| 1929 | if (res == -1 && PyErr_Occurred()) |
| 1930 | return NULL; |
| 1931 | return PyInt_FromLong((long)res); |
| 1932 | } |
| 1933 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1934 | static PyObject * |
| 1935 | wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 1936 | { |
| 1937 | binaryfunc func = (binaryfunc)wrapped; |
| 1938 | PyObject *other; |
| 1939 | |
| 1940 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1941 | return NULL; |
| 1942 | return (*func)(self, other); |
| 1943 | } |
| 1944 | |
| 1945 | static PyObject * |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 1946 | wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped) |
| 1947 | { |
| 1948 | binaryfunc func = (binaryfunc)wrapped; |
| 1949 | PyObject *other; |
| 1950 | |
| 1951 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1952 | return NULL; |
| 1953 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1954 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 1955 | Py_INCREF(Py_NotImplemented); |
| 1956 | return Py_NotImplemented; |
| 1957 | } |
| 1958 | return (*func)(self, other); |
| 1959 | } |
| 1960 | |
| 1961 | static PyObject * |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1962 | wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 1963 | { |
| 1964 | binaryfunc func = (binaryfunc)wrapped; |
| 1965 | PyObject *other; |
| 1966 | |
| 1967 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1968 | return NULL; |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 1969 | if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) && |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 1970 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | 0eb2a6e | 2001-10-09 11:07:24 +0000 | [diff] [blame] | 1971 | Py_INCREF(Py_NotImplemented); |
| 1972 | return Py_NotImplemented; |
| 1973 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 1974 | return (*func)(other, self); |
| 1975 | } |
| 1976 | |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 1977 | static PyObject * |
| 1978 | wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped) |
| 1979 | { |
| 1980 | coercion func = (coercion)wrapped; |
| 1981 | PyObject *other, *res; |
| 1982 | int ok; |
| 1983 | |
| 1984 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 1985 | return NULL; |
| 1986 | ok = func(&self, &other); |
| 1987 | if (ok < 0) |
| 1988 | return NULL; |
| 1989 | if (ok > 0) { |
| 1990 | Py_INCREF(Py_NotImplemented); |
| 1991 | return Py_NotImplemented; |
| 1992 | } |
| 1993 | res = PyTuple_New(2); |
| 1994 | if (res == NULL) { |
| 1995 | Py_DECREF(self); |
| 1996 | Py_DECREF(other); |
| 1997 | return NULL; |
| 1998 | } |
| 1999 | PyTuple_SET_ITEM(res, 0, self); |
| 2000 | PyTuple_SET_ITEM(res, 1, other); |
| 2001 | return res; |
| 2002 | } |
| 2003 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2004 | static PyObject * |
| 2005 | wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2006 | { |
| 2007 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2008 | PyObject *other; |
| 2009 | PyObject *third = Py_None; |
| 2010 | |
| 2011 | /* Note: This wrapper only works for __pow__() */ |
| 2012 | |
| 2013 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2014 | return NULL; |
| 2015 | return (*func)(self, other, third); |
| 2016 | } |
| 2017 | |
Guido van Rossum | 9bea3ab | 2001-09-28 22:58:52 +0000 | [diff] [blame] | 2018 | static PyObject * |
| 2019 | wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped) |
| 2020 | { |
| 2021 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2022 | PyObject *other; |
| 2023 | PyObject *third = Py_None; |
| 2024 | |
| 2025 | /* Note: This wrapper only works for __pow__() */ |
| 2026 | |
| 2027 | if (!PyArg_ParseTuple(args, "O|O", &other, &third)) |
| 2028 | return NULL; |
| 2029 | return (*func)(other, self, third); |
| 2030 | } |
| 2031 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2032 | static PyObject * |
| 2033 | wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2034 | { |
| 2035 | unaryfunc func = (unaryfunc)wrapped; |
| 2036 | |
| 2037 | if (!PyArg_ParseTuple(args, "")) |
| 2038 | return NULL; |
| 2039 | return (*func)(self); |
| 2040 | } |
| 2041 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2042 | static PyObject * |
| 2043 | wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2044 | { |
| 2045 | intargfunc func = (intargfunc)wrapped; |
| 2046 | int i; |
| 2047 | |
| 2048 | if (!PyArg_ParseTuple(args, "i", &i)) |
| 2049 | return NULL; |
| 2050 | return (*func)(self, i); |
| 2051 | } |
| 2052 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2053 | static int |
| 2054 | getindex(PyObject *self, PyObject *arg) |
| 2055 | { |
| 2056 | int i; |
| 2057 | |
| 2058 | i = PyInt_AsLong(arg); |
| 2059 | if (i == -1 && PyErr_Occurred()) |
| 2060 | return -1; |
| 2061 | if (i < 0) { |
| 2062 | PySequenceMethods *sq = self->ob_type->tp_as_sequence; |
| 2063 | if (sq && sq->sq_length) { |
| 2064 | int n = (*sq->sq_length)(self); |
| 2065 | if (n < 0) |
| 2066 | return -1; |
| 2067 | i += n; |
| 2068 | } |
| 2069 | } |
| 2070 | return i; |
| 2071 | } |
| 2072 | |
| 2073 | static PyObject * |
| 2074 | wrap_sq_item(PyObject *self, PyObject *args, void *wrapped) |
| 2075 | { |
| 2076 | intargfunc func = (intargfunc)wrapped; |
| 2077 | PyObject *arg; |
| 2078 | int i; |
| 2079 | |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 2080 | if (PyTuple_GET_SIZE(args) == 1) { |
| 2081 | arg = PyTuple_GET_ITEM(args, 0); |
| 2082 | i = getindex(self, arg); |
| 2083 | if (i == -1 && PyErr_Occurred()) |
| 2084 | return NULL; |
| 2085 | return (*func)(self, i); |
| 2086 | } |
| 2087 | PyArg_ParseTuple(args, "O", &arg); |
| 2088 | assert(PyErr_Occurred()); |
| 2089 | return NULL; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2090 | } |
| 2091 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2092 | static PyObject * |
| 2093 | wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2094 | { |
| 2095 | intintargfunc func = (intintargfunc)wrapped; |
| 2096 | int i, j; |
| 2097 | |
| 2098 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 2099 | return NULL; |
| 2100 | return (*func)(self, i, j); |
| 2101 | } |
| 2102 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2103 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2104 | wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2105 | { |
| 2106 | intobjargproc func = (intobjargproc)wrapped; |
| 2107 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2108 | PyObject *arg, *value; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2109 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2110 | if (!PyArg_ParseTuple(args, "OO", &arg, &value)) |
| 2111 | return NULL; |
| 2112 | i = getindex(self, arg); |
| 2113 | if (i == -1 && PyErr_Occurred()) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2114 | return NULL; |
| 2115 | res = (*func)(self, i, value); |
| 2116 | if (res == -1 && PyErr_Occurred()) |
| 2117 | return NULL; |
| 2118 | Py_INCREF(Py_None); |
| 2119 | return Py_None; |
| 2120 | } |
| 2121 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2122 | static PyObject * |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2123 | wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2124 | { |
| 2125 | intobjargproc func = (intobjargproc)wrapped; |
| 2126 | int i, res; |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2127 | PyObject *arg; |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2128 | |
Guido van Rossum | 5d815f3 | 2001-08-17 21:57:47 +0000 | [diff] [blame] | 2129 | if (!PyArg_ParseTuple(args, "O", &arg)) |
| 2130 | return NULL; |
| 2131 | i = getindex(self, arg); |
| 2132 | if (i == -1 && PyErr_Occurred()) |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2133 | return NULL; |
| 2134 | res = (*func)(self, i, NULL); |
| 2135 | if (res == -1 && PyErr_Occurred()) |
| 2136 | return NULL; |
| 2137 | Py_INCREF(Py_None); |
| 2138 | return Py_None; |
| 2139 | } |
| 2140 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2141 | static PyObject * |
| 2142 | wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 2143 | { |
| 2144 | intintobjargproc func = (intintobjargproc)wrapped; |
| 2145 | int i, j, res; |
| 2146 | PyObject *value; |
| 2147 | |
| 2148 | if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value)) |
| 2149 | return NULL; |
| 2150 | res = (*func)(self, i, j, value); |
| 2151 | if (res == -1 && PyErr_Occurred()) |
| 2152 | return NULL; |
| 2153 | Py_INCREF(Py_None); |
| 2154 | return Py_None; |
| 2155 | } |
| 2156 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2157 | static PyObject * |
| 2158 | wrap_delslice(PyObject *self, PyObject *args, void *wrapped) |
| 2159 | { |
| 2160 | intintobjargproc func = (intintobjargproc)wrapped; |
| 2161 | int i, j, res; |
| 2162 | |
| 2163 | if (!PyArg_ParseTuple(args, "ii", &i, &j)) |
| 2164 | return NULL; |
| 2165 | res = (*func)(self, i, j, NULL); |
| 2166 | if (res == -1 && PyErr_Occurred()) |
| 2167 | return NULL; |
| 2168 | Py_INCREF(Py_None); |
| 2169 | return Py_None; |
| 2170 | } |
| 2171 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2172 | /* XXX objobjproc is a misnomer; should be objargpred */ |
| 2173 | static PyObject * |
| 2174 | wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped) |
| 2175 | { |
| 2176 | objobjproc func = (objobjproc)wrapped; |
| 2177 | int res; |
| 2178 | PyObject *value; |
| 2179 | |
| 2180 | if (!PyArg_ParseTuple(args, "O", &value)) |
| 2181 | return NULL; |
| 2182 | res = (*func)(self, value); |
| 2183 | if (res == -1 && PyErr_Occurred()) |
| 2184 | return NULL; |
| 2185 | return PyInt_FromLong((long)res); |
| 2186 | } |
| 2187 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2188 | static PyObject * |
| 2189 | wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped) |
| 2190 | { |
| 2191 | objobjargproc func = (objobjargproc)wrapped; |
| 2192 | int res; |
| 2193 | PyObject *key, *value; |
| 2194 | |
| 2195 | if (!PyArg_ParseTuple(args, "OO", &key, &value)) |
| 2196 | return NULL; |
| 2197 | res = (*func)(self, key, value); |
| 2198 | if (res == -1 && PyErr_Occurred()) |
| 2199 | return NULL; |
| 2200 | Py_INCREF(Py_None); |
| 2201 | return Py_None; |
| 2202 | } |
| 2203 | |
Guido van Rossum | 2b8d7bd | 2001-08-02 15:31:58 +0000 | [diff] [blame] | 2204 | static PyObject * |
| 2205 | wrap_delitem(PyObject *self, PyObject *args, void *wrapped) |
| 2206 | { |
| 2207 | objobjargproc func = (objobjargproc)wrapped; |
| 2208 | int res; |
| 2209 | PyObject *key; |
| 2210 | |
| 2211 | if (!PyArg_ParseTuple(args, "O", &key)) |
| 2212 | return NULL; |
| 2213 | res = (*func)(self, key, NULL); |
| 2214 | if (res == -1 && PyErr_Occurred()) |
| 2215 | return NULL; |
| 2216 | Py_INCREF(Py_None); |
| 2217 | return Py_None; |
| 2218 | } |
| 2219 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2220 | static PyObject * |
| 2221 | wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2222 | { |
| 2223 | cmpfunc func = (cmpfunc)wrapped; |
| 2224 | int res; |
| 2225 | PyObject *other; |
| 2226 | |
| 2227 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2228 | return NULL; |
Guido van Rossum | 3d45d8f | 2001-09-24 18:47:40 +0000 | [diff] [blame] | 2229 | if (other->ob_type->tp_compare != func && |
| 2230 | !PyType_IsSubtype(other->ob_type, self->ob_type)) { |
Guido van Rossum | ceccae5 | 2001-09-18 20:03:57 +0000 | [diff] [blame] | 2231 | PyErr_Format( |
| 2232 | PyExc_TypeError, |
| 2233 | "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'", |
| 2234 | self->ob_type->tp_name, |
| 2235 | self->ob_type->tp_name, |
| 2236 | other->ob_type->tp_name); |
| 2237 | return NULL; |
| 2238 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2239 | res = (*func)(self, other); |
| 2240 | if (PyErr_Occurred()) |
| 2241 | return NULL; |
| 2242 | return PyInt_FromLong((long)res); |
| 2243 | } |
| 2244 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2245 | static PyObject * |
| 2246 | wrap_setattr(PyObject *self, PyObject *args, void *wrapped) |
| 2247 | { |
| 2248 | setattrofunc func = (setattrofunc)wrapped; |
| 2249 | int res; |
| 2250 | PyObject *name, *value; |
| 2251 | |
| 2252 | if (!PyArg_ParseTuple(args, "OO", &name, &value)) |
| 2253 | return NULL; |
| 2254 | res = (*func)(self, name, value); |
| 2255 | if (res < 0) |
| 2256 | return NULL; |
| 2257 | Py_INCREF(Py_None); |
| 2258 | return Py_None; |
| 2259 | } |
| 2260 | |
| 2261 | static PyObject * |
| 2262 | wrap_delattr(PyObject *self, PyObject *args, void *wrapped) |
| 2263 | { |
| 2264 | setattrofunc func = (setattrofunc)wrapped; |
| 2265 | int res; |
| 2266 | PyObject *name; |
| 2267 | |
| 2268 | if (!PyArg_ParseTuple(args, "O", &name)) |
| 2269 | return NULL; |
| 2270 | res = (*func)(self, name, NULL); |
| 2271 | if (res < 0) |
| 2272 | return NULL; |
| 2273 | Py_INCREF(Py_None); |
| 2274 | return Py_None; |
| 2275 | } |
| 2276 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2277 | static PyObject * |
| 2278 | wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped) |
| 2279 | { |
| 2280 | hashfunc func = (hashfunc)wrapped; |
| 2281 | long res; |
| 2282 | |
| 2283 | if (!PyArg_ParseTuple(args, "")) |
| 2284 | return NULL; |
| 2285 | res = (*func)(self); |
| 2286 | if (res == -1 && PyErr_Occurred()) |
| 2287 | return NULL; |
| 2288 | return PyInt_FromLong(res); |
| 2289 | } |
| 2290 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2291 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2292 | wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2293 | { |
| 2294 | ternaryfunc func = (ternaryfunc)wrapped; |
| 2295 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2296 | return (*func)(self, args, kwds); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2297 | } |
| 2298 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2299 | static PyObject * |
| 2300 | wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op) |
| 2301 | { |
| 2302 | richcmpfunc func = (richcmpfunc)wrapped; |
| 2303 | PyObject *other; |
| 2304 | |
| 2305 | if (!PyArg_ParseTuple(args, "O", &other)) |
| 2306 | return NULL; |
| 2307 | return (*func)(self, other, op); |
| 2308 | } |
| 2309 | |
| 2310 | #undef RICHCMP_WRAPPER |
| 2311 | #define RICHCMP_WRAPPER(NAME, OP) \ |
| 2312 | static PyObject * \ |
| 2313 | richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \ |
| 2314 | { \ |
| 2315 | return wrap_richcmpfunc(self, args, wrapped, OP); \ |
| 2316 | } |
| 2317 | |
Jack Jansen | 8e938b4 | 2001-08-08 15:29:49 +0000 | [diff] [blame] | 2318 | RICHCMP_WRAPPER(lt, Py_LT) |
| 2319 | RICHCMP_WRAPPER(le, Py_LE) |
| 2320 | RICHCMP_WRAPPER(eq, Py_EQ) |
| 2321 | RICHCMP_WRAPPER(ne, Py_NE) |
| 2322 | RICHCMP_WRAPPER(gt, Py_GT) |
| 2323 | RICHCMP_WRAPPER(ge, Py_GE) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2324 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2325 | static PyObject * |
| 2326 | wrap_next(PyObject *self, PyObject *args, void *wrapped) |
| 2327 | { |
| 2328 | unaryfunc func = (unaryfunc)wrapped; |
| 2329 | PyObject *res; |
| 2330 | |
| 2331 | if (!PyArg_ParseTuple(args, "")) |
| 2332 | return NULL; |
| 2333 | res = (*func)(self); |
| 2334 | if (res == NULL && !PyErr_Occurred()) |
| 2335 | PyErr_SetNone(PyExc_StopIteration); |
| 2336 | return res; |
| 2337 | } |
| 2338 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2339 | static PyObject * |
| 2340 | wrap_descr_get(PyObject *self, PyObject *args, void *wrapped) |
| 2341 | { |
| 2342 | descrgetfunc func = (descrgetfunc)wrapped; |
| 2343 | PyObject *obj; |
| 2344 | PyObject *type = NULL; |
| 2345 | |
| 2346 | if (!PyArg_ParseTuple(args, "O|O", &obj, &type)) |
| 2347 | return NULL; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2348 | return (*func)(self, obj, type); |
| 2349 | } |
| 2350 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2351 | static PyObject * |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 2352 | wrap_descr_set(PyObject *self, PyObject *args, void *wrapped) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2353 | { |
| 2354 | descrsetfunc func = (descrsetfunc)wrapped; |
| 2355 | PyObject *obj, *value; |
| 2356 | int ret; |
| 2357 | |
| 2358 | if (!PyArg_ParseTuple(args, "OO", &obj, &value)) |
| 2359 | return NULL; |
| 2360 | ret = (*func)(self, obj, value); |
| 2361 | if (ret < 0) |
| 2362 | return NULL; |
| 2363 | Py_INCREF(Py_None); |
| 2364 | return Py_None; |
| 2365 | } |
| 2366 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2367 | static PyObject * |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2368 | wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2369 | { |
| 2370 | initproc func = (initproc)wrapped; |
| 2371 | |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 2372 | if (func(self, args, kwds) < 0) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2373 | return NULL; |
| 2374 | Py_INCREF(Py_None); |
| 2375 | return Py_None; |
| 2376 | } |
| 2377 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2378 | static PyObject * |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2379 | tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2380 | { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2381 | PyTypeObject *type, *subtype, *staticbase; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2382 | PyObject *arg0, *res; |
| 2383 | |
| 2384 | if (self == NULL || !PyType_Check(self)) |
| 2385 | Py_FatalError("__new__() called with non-type 'self'"); |
| 2386 | type = (PyTypeObject *)self; |
| 2387 | if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2388 | PyErr_Format(PyExc_TypeError, |
| 2389 | "%s.__new__(): not enough arguments", |
| 2390 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2391 | return NULL; |
| 2392 | } |
| 2393 | arg0 = PyTuple_GET_ITEM(args, 0); |
| 2394 | if (!PyType_Check(arg0)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2395 | PyErr_Format(PyExc_TypeError, |
| 2396 | "%s.__new__(X): X is not a type object (%s)", |
| 2397 | type->tp_name, |
| 2398 | arg0->ob_type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2399 | return NULL; |
| 2400 | } |
| 2401 | subtype = (PyTypeObject *)arg0; |
| 2402 | if (!PyType_IsSubtype(subtype, type)) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2403 | PyErr_Format(PyExc_TypeError, |
| 2404 | "%s.__new__(%s): %s is not a subtype of %s", |
| 2405 | type->tp_name, |
| 2406 | subtype->tp_name, |
| 2407 | subtype->tp_name, |
| 2408 | type->tp_name); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2409 | return NULL; |
| 2410 | } |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2411 | |
| 2412 | /* Check that the use doesn't do something silly and unsafe like |
| 2413 | object.__new__(dictionary). To do this, we check that the |
| 2414 | most derived base that's not a heap type is this type. */ |
| 2415 | staticbase = subtype; |
| 2416 | while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) |
| 2417 | staticbase = staticbase->tp_base; |
Guido van Rossum | a8c60f4 | 2001-09-14 19:43:36 +0000 | [diff] [blame] | 2418 | if (staticbase->tp_new != type->tp_new) { |
Barry Warsaw | 60f0188 | 2001-08-22 19:24:42 +0000 | [diff] [blame] | 2419 | PyErr_Format(PyExc_TypeError, |
| 2420 | "%s.__new__(%s) is not safe, use %s.__new__()", |
| 2421 | type->tp_name, |
| 2422 | subtype->tp_name, |
| 2423 | staticbase == NULL ? "?" : staticbase->tp_name); |
| 2424 | return NULL; |
| 2425 | } |
| 2426 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2427 | args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
| 2428 | if (args == NULL) |
| 2429 | return NULL; |
| 2430 | res = type->tp_new(subtype, args, kwds); |
| 2431 | Py_DECREF(args); |
| 2432 | return res; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2435 | static struct PyMethodDef tp_new_methoddef[] = { |
| 2436 | {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS, |
| 2437 | "T.__new__(S, ...) -> a new object with type S, a subtype of T"}, |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2438 | {0} |
| 2439 | }; |
| 2440 | |
| 2441 | static int |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2442 | add_tp_new_wrapper(PyTypeObject *type) |
| 2443 | { |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2444 | PyObject *func; |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2445 | |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2446 | if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2447 | return 0; |
| 2448 | func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2449 | if (func == NULL) |
| 2450 | return -1; |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 2451 | return PyDict_SetItemString(type->tp_dict, "__new__", func); |
Guido van Rossum | 0d231ed | 2001-08-06 16:50:37 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 2454 | /* Slot wrappers that call the corresponding __foo__ slot. See comments |
| 2455 | below at override_slots() for more explanation. */ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2456 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2457 | #define SLOT0(FUNCNAME, OPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2458 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2459 | FUNCNAME(PyObject *self) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2460 | { \ |
Guido van Rossum | 5592e4d | 2001-08-28 18:28:21 +0000 | [diff] [blame] | 2461 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2462 | return call_method(self, OPSTR, &cache_str, "()"); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2463 | } |
| 2464 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2465 | #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2466 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2467 | FUNCNAME(PyObject *self, ARG1TYPE arg1) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2468 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2469 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2470 | return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2471 | } |
| 2472 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2473 | |
| 2474 | #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2475 | static PyObject * \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2476 | FUNCNAME(PyObject *self, PyObject *other) \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2477 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2478 | static PyObject *cache_str, *rcache_str; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2479 | int do_other = self->ob_type != other->ob_type && \ |
| 2480 | other->ob_type->tp_as_number != NULL && \ |
| 2481 | other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2482 | if (self->ob_type->tp_as_number != NULL && \ |
| 2483 | self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \ |
| 2484 | PyObject *r; \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2485 | if (do_other && \ |
| 2486 | PyType_IsSubtype(other->ob_type, self->ob_type)) { \ |
| 2487 | r = call_maybe( \ |
| 2488 | other, ROPSTR, &rcache_str, "(O)", self); \ |
| 2489 | if (r != Py_NotImplemented) \ |
| 2490 | return r; \ |
| 2491 | Py_DECREF(r); \ |
| 2492 | do_other = 0; \ |
| 2493 | } \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 2494 | r = call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2495 | self, OPSTR, &cache_str, "(O)", other); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2496 | if (r != Py_NotImplemented || \ |
| 2497 | other->ob_type == self->ob_type) \ |
| 2498 | return r; \ |
| 2499 | Py_DECREF(r); \ |
| 2500 | } \ |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2501 | if (do_other) { \ |
Guido van Rossum | f21c6be | 2001-09-14 17:51:50 +0000 | [diff] [blame] | 2502 | return call_maybe( \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2503 | other, ROPSTR, &rcache_str, "(O)", self); \ |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2504 | } \ |
| 2505 | Py_INCREF(Py_NotImplemented); \ |
| 2506 | return Py_NotImplemented; \ |
| 2507 | } |
| 2508 | |
| 2509 | #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ |
| 2510 | SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) |
| 2511 | |
| 2512 | #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ |
| 2513 | static PyObject * \ |
| 2514 | FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ |
| 2515 | { \ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2516 | static PyObject *cache_str; \ |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2517 | return call_method(self, OPSTR, &cache_str, \ |
| 2518 | "(" ARGCODES ")", arg1, arg2); \ |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2519 | } |
| 2520 | |
| 2521 | static int |
| 2522 | slot_sq_length(PyObject *self) |
| 2523 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2524 | static PyObject *len_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2525 | PyObject *res = call_method(self, "__len__", &len_str, "()"); |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 2526 | int len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2527 | |
| 2528 | if (res == NULL) |
| 2529 | return -1; |
Guido van Rossum | 2611162 | 2001-10-01 16:42:49 +0000 | [diff] [blame] | 2530 | len = (int)PyInt_AsLong(res); |
| 2531 | Py_DECREF(res); |
| 2532 | return len; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2535 | SLOT1(slot_sq_concat, "__add__", PyObject *, "O") |
| 2536 | SLOT1(slot_sq_repeat, "__mul__", int, "i") |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 2537 | |
| 2538 | /* Super-optimized version of slot_sq_item. |
| 2539 | Other slots could do the same... */ |
| 2540 | static PyObject * |
| 2541 | slot_sq_item(PyObject *self, int i) |
| 2542 | { |
| 2543 | static PyObject *getitem_str; |
| 2544 | PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; |
| 2545 | descrgetfunc f; |
| 2546 | |
| 2547 | if (getitem_str == NULL) { |
| 2548 | getitem_str = PyString_InternFromString("__getitem__"); |
| 2549 | if (getitem_str == NULL) |
| 2550 | return NULL; |
| 2551 | } |
| 2552 | func = _PyType_Lookup(self->ob_type, getitem_str); |
| 2553 | if (func != NULL) { |
Guido van Rossum | f4593e0 | 2001-10-03 12:09:30 +0000 | [diff] [blame] | 2554 | if ((f = func->ob_type->tp_descr_get) == NULL) |
| 2555 | Py_INCREF(func); |
| 2556 | else |
| 2557 | func = f(func, self, (PyObject *)(self->ob_type)); |
| 2558 | ival = PyInt_FromLong(i); |
| 2559 | if (ival != NULL) { |
| 2560 | args = PyTuple_New(1); |
| 2561 | if (args != NULL) { |
| 2562 | PyTuple_SET_ITEM(args, 0, ival); |
| 2563 | retval = PyObject_Call(func, args, NULL); |
| 2564 | Py_XDECREF(args); |
| 2565 | Py_XDECREF(func); |
| 2566 | return retval; |
| 2567 | } |
| 2568 | } |
| 2569 | } |
| 2570 | else { |
| 2571 | PyErr_SetObject(PyExc_AttributeError, getitem_str); |
| 2572 | } |
| 2573 | Py_XDECREF(args); |
| 2574 | Py_XDECREF(ival); |
| 2575 | Py_XDECREF(func); |
| 2576 | return NULL; |
| 2577 | } |
| 2578 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2579 | SLOT2(slot_sq_slice, "__getslice__", int, int, "ii") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2580 | |
| 2581 | static int |
| 2582 | slot_sq_ass_item(PyObject *self, int index, PyObject *value) |
| 2583 | { |
| 2584 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2585 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2586 | |
| 2587 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2588 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2589 | "(i)", index); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2590 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2591 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2592 | "(iO)", index, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2593 | if (res == NULL) |
| 2594 | return -1; |
| 2595 | Py_DECREF(res); |
| 2596 | return 0; |
| 2597 | } |
| 2598 | |
| 2599 | static int |
| 2600 | slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value) |
| 2601 | { |
| 2602 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2603 | static PyObject *delslice_str, *setslice_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2604 | |
| 2605 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2606 | res = call_method(self, "__delslice__", &delslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2607 | "(ii)", i, j); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2608 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2609 | res = call_method(self, "__setslice__", &setslice_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2610 | "(iiO)", i, j, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2611 | if (res == NULL) |
| 2612 | return -1; |
| 2613 | Py_DECREF(res); |
| 2614 | return 0; |
| 2615 | } |
| 2616 | |
| 2617 | static int |
| 2618 | slot_sq_contains(PyObject *self, PyObject *value) |
| 2619 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2620 | PyObject *func, *res, *args; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2621 | static PyObject *contains_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2622 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2623 | func = lookup_maybe(self, "__contains__", &contains_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2624 | |
| 2625 | if (func != NULL) { |
| 2626 | args = Py_BuildValue("(O)", value); |
| 2627 | if (args == NULL) |
| 2628 | res = NULL; |
| 2629 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2630 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2631 | Py_DECREF(args); |
| 2632 | } |
| 2633 | Py_DECREF(func); |
| 2634 | if (res == NULL) |
| 2635 | return -1; |
| 2636 | return PyObject_IsTrue(res); |
| 2637 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2638 | else if (PyErr_Occurred()) |
| 2639 | return -1; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2640 | else { |
Tim Peters | 16a77ad | 2001-09-08 04:00:12 +0000 | [diff] [blame] | 2641 | return _PySequence_IterSearch(self, value, |
| 2642 | PY_ITERSEARCH_CONTAINS); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2643 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2644 | } |
| 2645 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2646 | SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O") |
| 2647 | SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2648 | |
| 2649 | #define slot_mp_length slot_sq_length |
| 2650 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2651 | SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2652 | |
| 2653 | static int |
| 2654 | slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) |
| 2655 | { |
| 2656 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2657 | static PyObject *delitem_str, *setitem_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2658 | |
| 2659 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2660 | res = call_method(self, "__delitem__", &delitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2661 | "(O)", key); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2662 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2663 | res = call_method(self, "__setitem__", &setitem_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2664 | "(OO)", key, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2665 | if (res == NULL) |
| 2666 | return -1; |
| 2667 | Py_DECREF(res); |
| 2668 | return 0; |
| 2669 | } |
| 2670 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2671 | SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") |
| 2672 | SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") |
| 2673 | SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") |
| 2674 | SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") |
| 2675 | SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") |
| 2676 | SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") |
| 2677 | |
| 2678 | staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); |
| 2679 | |
| 2680 | SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, |
| 2681 | nb_power, "__pow__", "__rpow__") |
| 2682 | |
| 2683 | static PyObject * |
| 2684 | slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) |
| 2685 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2686 | static PyObject *pow_str; |
| 2687 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2688 | if (modulus == Py_None) |
| 2689 | return slot_nb_power_binary(self, other); |
| 2690 | /* Three-arg power doesn't use __rpow__ */ |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 2691 | return call_method(self, "__pow__", &pow_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2692 | "(OO)", other, modulus); |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2693 | } |
| 2694 | |
| 2695 | SLOT0(slot_nb_negative, "__neg__") |
| 2696 | SLOT0(slot_nb_positive, "__pos__") |
| 2697 | SLOT0(slot_nb_absolute, "__abs__") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2698 | |
| 2699 | static int |
| 2700 | slot_nb_nonzero(PyObject *self) |
| 2701 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2702 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2703 | static PyObject *nonzero_str, *len_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2704 | |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2705 | func = lookup_maybe(self, "__nonzero__", &nonzero_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2706 | if (func == NULL) { |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2707 | if (PyErr_Occurred()) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2708 | return -1; |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2709 | func = lookup_maybe(self, "__len__", &len_str); |
| 2710 | if (func == NULL) { |
| 2711 | if (PyErr_Occurred()) |
| 2712 | return -1; |
| 2713 | else |
| 2714 | return 1; |
| 2715 | } |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2716 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2717 | res = PyObject_CallObject(func, NULL); |
| 2718 | Py_DECREF(func); |
| 2719 | if (res == NULL) |
| 2720 | return -1; |
| 2721 | return PyObject_IsTrue(res); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2722 | } |
| 2723 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2724 | SLOT0(slot_nb_invert, "__invert__") |
| 2725 | SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") |
| 2726 | SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") |
| 2727 | SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") |
| 2728 | SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") |
| 2729 | SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2730 | |
| 2731 | static int |
| 2732 | slot_nb_coerce(PyObject **a, PyObject **b) |
| 2733 | { |
| 2734 | static PyObject *coerce_str; |
| 2735 | PyObject *self = *a, *other = *b; |
| 2736 | |
| 2737 | if (self->ob_type->tp_as_number != NULL && |
| 2738 | self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 2739 | PyObject *r; |
| 2740 | r = call_maybe( |
| 2741 | self, "__coerce__", &coerce_str, "(O)", other); |
| 2742 | if (r == NULL) |
| 2743 | return -1; |
| 2744 | if (r == Py_NotImplemented) { |
| 2745 | Py_DECREF(r); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2746 | } |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2747 | else { |
| 2748 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 2749 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2750 | "__coerce__ didn't return a 2-tuple"); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2751 | Py_DECREF(r); |
| 2752 | return -1; |
| 2753 | } |
| 2754 | *a = PyTuple_GET_ITEM(r, 0); |
| 2755 | Py_INCREF(*a); |
| 2756 | *b = PyTuple_GET_ITEM(r, 1); |
| 2757 | Py_INCREF(*b); |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2758 | Py_DECREF(r); |
Guido van Rossum | 55f2099 | 2001-10-01 17:18:22 +0000 | [diff] [blame] | 2759 | return 0; |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2760 | } |
Guido van Rossum | 2ed6bf8 | 2001-09-27 20:30:07 +0000 | [diff] [blame] | 2761 | } |
| 2762 | if (other->ob_type->tp_as_number != NULL && |
| 2763 | other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { |
| 2764 | PyObject *r; |
| 2765 | r = call_maybe( |
| 2766 | other, "__coerce__", &coerce_str, "(O)", self); |
| 2767 | if (r == NULL) |
| 2768 | return -1; |
| 2769 | if (r == Py_NotImplemented) { |
| 2770 | Py_DECREF(r); |
| 2771 | return 1; |
| 2772 | } |
| 2773 | if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { |
| 2774 | PyErr_SetString(PyExc_TypeError, |
| 2775 | "__coerce__ didn't return a 2-tuple"); |
| 2776 | Py_DECREF(r); |
| 2777 | return -1; |
| 2778 | } |
| 2779 | *a = PyTuple_GET_ITEM(r, 1); |
| 2780 | Py_INCREF(*a); |
| 2781 | *b = PyTuple_GET_ITEM(r, 0); |
| 2782 | Py_INCREF(*b); |
| 2783 | Py_DECREF(r); |
| 2784 | return 0; |
| 2785 | } |
| 2786 | return 1; |
| 2787 | } |
| 2788 | |
Guido van Rossum | dc91b99 | 2001-08-08 22:26:22 +0000 | [diff] [blame] | 2789 | SLOT0(slot_nb_int, "__int__") |
| 2790 | SLOT0(slot_nb_long, "__long__") |
| 2791 | SLOT0(slot_nb_float, "__float__") |
| 2792 | SLOT0(slot_nb_oct, "__oct__") |
| 2793 | SLOT0(slot_nb_hex, "__hex__") |
| 2794 | SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") |
| 2795 | SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") |
| 2796 | SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") |
| 2797 | SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") |
| 2798 | SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") |
| 2799 | SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO") |
| 2800 | SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") |
| 2801 | SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") |
| 2802 | SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") |
| 2803 | SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") |
| 2804 | SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") |
| 2805 | SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, |
| 2806 | "__floordiv__", "__rfloordiv__") |
| 2807 | SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") |
| 2808 | SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") |
| 2809 | SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2810 | |
| 2811 | static int |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2812 | half_compare(PyObject *self, PyObject *other) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2813 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2814 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2815 | static PyObject *cmp_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2816 | int c; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2817 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2818 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2819 | if (func == NULL) { |
| 2820 | PyErr_Clear(); |
| 2821 | } |
| 2822 | else { |
| 2823 | args = Py_BuildValue("(O)", other); |
| 2824 | if (args == NULL) |
| 2825 | res = NULL; |
| 2826 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 2827 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2828 | Py_DECREF(args); |
| 2829 | } |
| 2830 | if (res != Py_NotImplemented) { |
| 2831 | if (res == NULL) |
| 2832 | return -2; |
| 2833 | c = PyInt_AsLong(res); |
| 2834 | Py_DECREF(res); |
| 2835 | if (c == -1 && PyErr_Occurred()) |
| 2836 | return -2; |
| 2837 | return (c < 0) ? -1 : (c > 0) ? 1 : 0; |
| 2838 | } |
| 2839 | Py_DECREF(res); |
| 2840 | } |
| 2841 | return 2; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2842 | } |
| 2843 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2844 | /* This slot is published for the benefit of try_3way_compare in object.c */ |
| 2845 | int |
| 2846 | _PyObject_SlotCompare(PyObject *self, PyObject *other) |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2847 | { |
| 2848 | int c; |
| 2849 | |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2850 | if (self->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2851 | c = half_compare(self, other); |
| 2852 | if (c <= 1) |
| 2853 | return c; |
| 2854 | } |
Guido van Rossum | ab3b034 | 2001-09-18 20:38:53 +0000 | [diff] [blame] | 2855 | if (other->ob_type->tp_compare == _PyObject_SlotCompare) { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2856 | c = half_compare(other, self); |
| 2857 | if (c < -1) |
| 2858 | return -2; |
| 2859 | if (c <= 1) |
| 2860 | return -c; |
| 2861 | } |
| 2862 | return (void *)self < (void *)other ? -1 : |
| 2863 | (void *)self > (void *)other ? 1 : 0; |
| 2864 | } |
| 2865 | |
| 2866 | static PyObject * |
| 2867 | slot_tp_repr(PyObject *self) |
| 2868 | { |
| 2869 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2870 | static PyObject *repr_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2871 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2872 | func = lookup_method(self, "__repr__", &repr_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2873 | if (func != NULL) { |
| 2874 | res = PyEval_CallObject(func, NULL); |
| 2875 | Py_DECREF(func); |
| 2876 | return res; |
| 2877 | } |
Barry Warsaw | 7ce3694 | 2001-08-24 18:34:26 +0000 | [diff] [blame] | 2878 | PyErr_Clear(); |
| 2879 | return PyString_FromFormat("<%s object at %p>", |
| 2880 | self->ob_type->tp_name, self); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2881 | } |
| 2882 | |
| 2883 | static PyObject * |
| 2884 | slot_tp_str(PyObject *self) |
| 2885 | { |
| 2886 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2887 | static PyObject *str_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2888 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2889 | func = lookup_method(self, "__str__", &str_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2890 | if (func != NULL) { |
| 2891 | res = PyEval_CallObject(func, NULL); |
| 2892 | Py_DECREF(func); |
| 2893 | return res; |
| 2894 | } |
| 2895 | else { |
| 2896 | PyErr_Clear(); |
| 2897 | return slot_tp_repr(self); |
| 2898 | } |
| 2899 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2900 | |
| 2901 | static long |
| 2902 | slot_tp_hash(PyObject *self) |
| 2903 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2904 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2905 | static PyObject *hash_str, *eq_str, *cmp_str; |
| 2906 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2907 | long h; |
| 2908 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2909 | func = lookup_method(self, "__hash__", &hash_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2910 | |
| 2911 | if (func != NULL) { |
| 2912 | res = PyEval_CallObject(func, NULL); |
| 2913 | Py_DECREF(func); |
| 2914 | if (res == NULL) |
| 2915 | return -1; |
| 2916 | h = PyInt_AsLong(res); |
| 2917 | } |
| 2918 | else { |
| 2919 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2920 | func = lookup_method(self, "__eq__", &eq_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2921 | if (func == NULL) { |
| 2922 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2923 | func = lookup_method(self, "__cmp__", &cmp_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 2924 | } |
| 2925 | if (func != NULL) { |
| 2926 | Py_DECREF(func); |
| 2927 | PyErr_SetString(PyExc_TypeError, "unhashable type"); |
| 2928 | return -1; |
| 2929 | } |
| 2930 | PyErr_Clear(); |
| 2931 | h = _Py_HashPointer((void *)self); |
| 2932 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2933 | if (h == -1 && !PyErr_Occurred()) |
| 2934 | h = -2; |
| 2935 | return h; |
| 2936 | } |
| 2937 | |
| 2938 | static PyObject * |
| 2939 | slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) |
| 2940 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 2941 | static PyObject *call_str; |
| 2942 | PyObject *meth = lookup_method(self, "__call__", &call_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2943 | PyObject *res; |
| 2944 | |
| 2945 | if (meth == NULL) |
| 2946 | return NULL; |
| 2947 | res = PyObject_Call(meth, args, kwds); |
| 2948 | Py_DECREF(meth); |
| 2949 | return res; |
| 2950 | } |
| 2951 | |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 2952 | /* There are two slot dispatch functions for tp_getattro. |
| 2953 | |
| 2954 | - slot_tp_getattro() is used when __getattribute__ is overridden |
| 2955 | but no __getattr__ hook is present; |
| 2956 | |
| 2957 | - slot_tp_getattr_hook() is used when a __getattr__ hook is present. |
| 2958 | |
| 2959 | The code in update_slot() and fixup_slot_dispatchers() always installs |
| 2960 | slot_tp_getattr_hook(); this detects the absence of __getattr__ and then |
| 2961 | installs the simpler slot if necessary. */ |
| 2962 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2963 | static PyObject * |
| 2964 | slot_tp_getattro(PyObject *self, PyObject *name) |
| 2965 | { |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 2966 | static PyObject *getattribute_str = NULL; |
| 2967 | return call_method(self, "__getattribute__", &getattribute_str, |
| 2968 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 2971 | static PyObject * |
| 2972 | slot_tp_getattr_hook(PyObject *self, PyObject *name) |
| 2973 | { |
| 2974 | PyTypeObject *tp = self->ob_type; |
| 2975 | PyObject *getattr, *getattribute, *res; |
| 2976 | static PyObject *getattribute_str = NULL; |
| 2977 | static PyObject *getattr_str = NULL; |
| 2978 | |
| 2979 | if (getattr_str == NULL) { |
| 2980 | getattr_str = PyString_InternFromString("__getattr__"); |
| 2981 | if (getattr_str == NULL) |
| 2982 | return NULL; |
| 2983 | } |
| 2984 | if (getattribute_str == NULL) { |
| 2985 | getattribute_str = |
| 2986 | PyString_InternFromString("__getattribute__"); |
| 2987 | if (getattribute_str == NULL) |
| 2988 | return NULL; |
| 2989 | } |
| 2990 | getattr = _PyType_Lookup(tp, getattr_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 2991 | if (getattr == NULL) { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 2992 | /* No __getattr__ hook: use a simpler dispatcher */ |
| 2993 | tp->tp_getattro = slot_tp_getattro; |
| 2994 | return slot_tp_getattro(self, name); |
| 2995 | } |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 2996 | getattribute = _PyType_Lookup(tp, getattribute_str); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 2997 | if (getattribute == NULL || |
| 2998 | (getattribute->ob_type == &PyWrapperDescr_Type && |
| 2999 | ((PyWrapperDescrObject *)getattribute)->d_wrapped == |
| 3000 | (void *)PyObject_GenericGetAttr)) |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3001 | res = PyObject_GenericGetAttr(self, name); |
| 3002 | else |
| 3003 | res = PyObject_CallFunction(getattribute, "OO", self, name); |
Guido van Rossum | 14a6f83 | 2001-10-17 13:59:09 +0000 | [diff] [blame] | 3004 | if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { |
Guido van Rossum | 19c1cd5 | 2001-09-21 21:24:49 +0000 | [diff] [blame] | 3005 | PyErr_Clear(); |
| 3006 | res = PyObject_CallFunction(getattr, "OO", self, name); |
| 3007 | } |
| 3008 | return res; |
| 3009 | } |
| 3010 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3011 | static int |
| 3012 | slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) |
| 3013 | { |
| 3014 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3015 | static PyObject *delattr_str, *setattr_str; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3016 | |
| 3017 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3018 | res = call_method(self, "__delattr__", &delattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3019 | "(O)", name); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3020 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3021 | res = call_method(self, "__setattr__", &setattr_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3022 | "(OO)", name, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3023 | if (res == NULL) |
| 3024 | return -1; |
| 3025 | Py_DECREF(res); |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | /* Map rich comparison operators to their __xx__ namesakes */ |
| 3030 | static char *name_op[] = { |
| 3031 | "__lt__", |
| 3032 | "__le__", |
| 3033 | "__eq__", |
| 3034 | "__ne__", |
| 3035 | "__gt__", |
| 3036 | "__ge__", |
| 3037 | }; |
| 3038 | |
| 3039 | static PyObject * |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3040 | half_richcompare(PyObject *self, PyObject *other, int op) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3041 | { |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3042 | PyObject *func, *args, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3043 | static PyObject *op_str[6]; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3044 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3045 | func = lookup_method(self, name_op[op], &op_str[op]); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3046 | if (func == NULL) { |
| 3047 | PyErr_Clear(); |
| 3048 | Py_INCREF(Py_NotImplemented); |
| 3049 | return Py_NotImplemented; |
| 3050 | } |
| 3051 | args = Py_BuildValue("(O)", other); |
| 3052 | if (args == NULL) |
| 3053 | res = NULL; |
| 3054 | else { |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3055 | res = PyObject_Call(func, args, NULL); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3056 | Py_DECREF(args); |
| 3057 | } |
| 3058 | Py_DECREF(func); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3059 | return res; |
| 3060 | } |
| 3061 | |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3062 | /* Map rich comparison operators to their swapped version, e.g. LT --> GT */ |
| 3063 | static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; |
| 3064 | |
| 3065 | static PyObject * |
| 3066 | slot_tp_richcompare(PyObject *self, PyObject *other, int op) |
| 3067 | { |
| 3068 | PyObject *res; |
| 3069 | |
| 3070 | if (self->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 3071 | res = half_richcompare(self, other, op); |
| 3072 | if (res != Py_NotImplemented) |
| 3073 | return res; |
| 3074 | Py_DECREF(res); |
| 3075 | } |
| 3076 | if (other->ob_type->tp_richcompare == slot_tp_richcompare) { |
| 3077 | res = half_richcompare(other, self, swapped_op[op]); |
| 3078 | if (res != Py_NotImplemented) { |
| 3079 | return res; |
| 3080 | } |
| 3081 | Py_DECREF(res); |
| 3082 | } |
| 3083 | Py_INCREF(Py_NotImplemented); |
| 3084 | return Py_NotImplemented; |
| 3085 | } |
| 3086 | |
| 3087 | static PyObject * |
| 3088 | slot_tp_iter(PyObject *self) |
| 3089 | { |
| 3090 | PyObject *func, *res; |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3091 | static PyObject *iter_str, *getitem_str; |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3092 | |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3093 | func = lookup_method(self, "__iter__", &iter_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3094 | if (func != NULL) { |
| 3095 | res = PyObject_CallObject(func, NULL); |
| 3096 | Py_DECREF(func); |
| 3097 | return res; |
| 3098 | } |
| 3099 | PyErr_Clear(); |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3100 | func = lookup_method(self, "__getitem__", &getitem_str); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3101 | if (func == NULL) { |
Tim Peters | 8b13b3e | 2001-09-30 05:58:42 +0000 | [diff] [blame] | 3102 | PyErr_SetString(PyExc_TypeError, "iteration over non-sequence"); |
Guido van Rossum | b8f6366 | 2001-08-15 23:57:02 +0000 | [diff] [blame] | 3103 | return NULL; |
| 3104 | } |
| 3105 | Py_DECREF(func); |
| 3106 | return PySeqIter_New(self); |
| 3107 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3108 | |
| 3109 | static PyObject * |
| 3110 | slot_tp_iternext(PyObject *self) |
| 3111 | { |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3112 | static PyObject *next_str; |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3113 | return call_method(self, "next", &next_str, "()"); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3114 | } |
| 3115 | |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 3116 | static PyObject * |
| 3117 | slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 3118 | { |
| 3119 | PyTypeObject *tp = self->ob_type; |
| 3120 | PyObject *get; |
| 3121 | static PyObject *get_str = NULL; |
| 3122 | |
| 3123 | if (get_str == NULL) { |
| 3124 | get_str = PyString_InternFromString("__get__"); |
| 3125 | if (get_str == NULL) |
| 3126 | return NULL; |
| 3127 | } |
| 3128 | get = _PyType_Lookup(tp, get_str); |
| 3129 | if (get == NULL) { |
| 3130 | /* Avoid further slowdowns */ |
| 3131 | if (tp->tp_descr_get == slot_tp_descr_get) |
| 3132 | tp->tp_descr_get = NULL; |
| 3133 | Py_INCREF(self); |
| 3134 | return self; |
| 3135 | } |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3136 | if (obj == NULL) |
| 3137 | obj = Py_None; |
| 3138 | if (type == NULL) |
| 3139 | type = Py_None; |
Guido van Rossum | 1a49350 | 2001-08-17 16:47:50 +0000 | [diff] [blame] | 3140 | return PyObject_CallFunction(get, "OOO", self, obj, type); |
| 3141 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3142 | |
| 3143 | static int |
| 3144 | slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) |
| 3145 | { |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3146 | PyObject *res; |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3147 | static PyObject *del_str, *set_str; |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3148 | |
| 3149 | if (value == NULL) |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3150 | res = call_method(self, "__del__", &del_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3151 | "(O)", target); |
Guido van Rossum | 2c25239 | 2001-08-24 10:13:31 +0000 | [diff] [blame] | 3152 | else |
Guido van Rossum | 2730b13 | 2001-08-28 18:22:14 +0000 | [diff] [blame] | 3153 | res = call_method(self, "__set__", &set_str, |
Guido van Rossum | 717ce00 | 2001-09-14 16:58:08 +0000 | [diff] [blame] | 3154 | "(OO)", target, value); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3155 | if (res == NULL) |
| 3156 | return -1; |
| 3157 | Py_DECREF(res); |
| 3158 | return 0; |
| 3159 | } |
| 3160 | |
| 3161 | static int |
| 3162 | slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3163 | { |
Guido van Rossum | 6071873 | 2001-08-28 17:47:51 +0000 | [diff] [blame] | 3164 | static PyObject *init_str; |
| 3165 | PyObject *meth = lookup_method(self, "__init__", &init_str); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3166 | PyObject *res; |
| 3167 | |
| 3168 | if (meth == NULL) |
| 3169 | return -1; |
| 3170 | res = PyObject_Call(meth, args, kwds); |
| 3171 | Py_DECREF(meth); |
| 3172 | if (res == NULL) |
| 3173 | return -1; |
| 3174 | Py_DECREF(res); |
| 3175 | return 0; |
| 3176 | } |
| 3177 | |
| 3178 | static PyObject * |
| 3179 | slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
| 3180 | { |
| 3181 | PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__"); |
| 3182 | PyObject *newargs, *x; |
| 3183 | int i, n; |
| 3184 | |
| 3185 | if (func == NULL) |
| 3186 | return NULL; |
| 3187 | assert(PyTuple_Check(args)); |
| 3188 | n = PyTuple_GET_SIZE(args); |
| 3189 | newargs = PyTuple_New(n+1); |
| 3190 | if (newargs == NULL) |
| 3191 | return NULL; |
| 3192 | Py_INCREF(type); |
| 3193 | PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); |
| 3194 | for (i = 0; i < n; i++) { |
| 3195 | x = PyTuple_GET_ITEM(args, i); |
| 3196 | Py_INCREF(x); |
| 3197 | PyTuple_SET_ITEM(newargs, i+1, x); |
| 3198 | } |
| 3199 | x = PyObject_Call(func, newargs, kwds); |
Guido van Rossum | 25d1807 | 2001-10-01 15:55:28 +0000 | [diff] [blame] | 3200 | Py_DECREF(newargs); |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3201 | Py_DECREF(func); |
| 3202 | return x; |
| 3203 | } |
| 3204 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3205 | |
| 3206 | /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper |
| 3207 | functions. The offsets here are relative to the 'etype' structure, which |
| 3208 | incorporates the additional structures used for numbers, sequences and |
| 3209 | mappings. Note that multiple names may map to the same slot (e.g. __eq__, |
| 3210 | __ne__ etc. all map to tp_richcompare) and one name may map to multiple |
| 3211 | slots (e.g. __str__ affects tp_str as well as tp_repr). */ |
| 3212 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3213 | typedef struct wrapperbase slotdef; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3214 | |
| 3215 | #undef TPSLOT |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3216 | #undef FLSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3217 | #undef ETSLOT |
| 3218 | #undef SQSLOT |
| 3219 | #undef MPSLOT |
| 3220 | #undef NBSLOT |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3221 | #undef UNSLOT |
| 3222 | #undef IBSLOT |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3223 | #undef BINSLOT |
| 3224 | #undef RBINSLOT |
| 3225 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3226 | #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3227 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC} |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3228 | #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ |
| 3229 | {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ |
| 3230 | DOC, FLAGS} |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3231 | #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3232 | {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC} |
| 3233 | #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3234 | ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) |
| 3235 | #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3236 | ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) |
| 3237 | #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3238 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) |
| 3239 | #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3240 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 3241 | "x." NAME "() <==> " DOC) |
| 3242 | #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ |
| 3243 | ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ |
| 3244 | "x." NAME "(y) <==> x" DOC "y") |
| 3245 | #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 3246 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ |
| 3247 | "x." NAME "(y) <==> x" DOC "y") |
| 3248 | #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ |
| 3249 | ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ |
| 3250 | "x." NAME "(y) <==> y" DOC "x") |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3251 | |
| 3252 | static slotdef slotdefs[] = { |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3253 | SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry, |
| 3254 | "x.__len__() <==> len(x)"), |
| 3255 | SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc, |
| 3256 | "x.__add__(y) <==> x+y"), |
| 3257 | SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 3258 | "x.__mul__(n) <==> x*n"), |
| 3259 | SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc, |
| 3260 | "x.__rmul__(n) <==> n*x"), |
| 3261 | SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, |
| 3262 | "x.__getitem__(y) <==> x[y]"), |
| 3263 | SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc, |
| 3264 | "x.__getslice__(i, j) <==> x[i:j]"), |
| 3265 | SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, |
| 3266 | "x.__setitem__(i, y) <==> x[i]=y"), |
| 3267 | SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, |
| 3268 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3269 | SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3270 | wrap_intintobjargproc, |
| 3271 | "x.__setslice__(i, j, y) <==> x[i:j]=y"), |
| 3272 | SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, |
| 3273 | "x.__delslice__(i, j) <==> del x[i:j]"), |
| 3274 | SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, |
| 3275 | "x.__contains__(y) <==> y in x"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3276 | SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3277 | wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3278 | SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3279 | wrap_intargfunc, "x.__imul__(y) <==> x*=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3280 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3281 | MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry, |
| 3282 | "x.__len__() <==> len(x)"), |
Guido van Rossum | fd38f8e | 2001-10-09 20:17:57 +0000 | [diff] [blame] | 3283 | MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3284 | wrap_binaryfunc, |
| 3285 | "x.__getitem__(y) <==> x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3286 | MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3287 | wrap_objobjargproc, |
| 3288 | "x.__setitem__(i, y) <==> x[i]=y"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3289 | MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3290 | wrap_delitem, |
| 3291 | "x.__delitem__(y) <==> del x[y]"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3292 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3293 | BINSLOT("__add__", nb_add, slot_nb_add, |
| 3294 | "+"), |
| 3295 | RBINSLOT("__radd__", nb_add, slot_nb_add, |
| 3296 | "+"), |
| 3297 | BINSLOT("__sub__", nb_subtract, slot_nb_subtract, |
| 3298 | "-"), |
| 3299 | RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, |
| 3300 | "-"), |
| 3301 | BINSLOT("__mul__", nb_multiply, slot_nb_multiply, |
| 3302 | "*"), |
| 3303 | RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, |
| 3304 | "*"), |
| 3305 | BINSLOT("__div__", nb_divide, slot_nb_divide, |
| 3306 | "/"), |
| 3307 | RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, |
| 3308 | "/"), |
| 3309 | BINSLOT("__mod__", nb_remainder, slot_nb_remainder, |
| 3310 | "%"), |
| 3311 | RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, |
| 3312 | "%"), |
| 3313 | BINSLOT("__divmod__", nb_divmod, slot_nb_divmod, |
| 3314 | "divmod(x, y)"), |
| 3315 | RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod, |
| 3316 | "divmod(y, x)"), |
| 3317 | NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, |
| 3318 | "x.__pow__(y[, z]) <==> pow(x, y[, z])"), |
| 3319 | NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, |
| 3320 | "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), |
| 3321 | UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), |
| 3322 | UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), |
| 3323 | UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, |
| 3324 | "abs(x)"), |
| 3325 | UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc, |
| 3326 | "x != 0"), |
| 3327 | UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), |
| 3328 | BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 3329 | RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), |
| 3330 | BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 3331 | RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), |
| 3332 | BINSLOT("__and__", nb_and, slot_nb_and, "&"), |
| 3333 | RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), |
| 3334 | BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), |
| 3335 | RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), |
| 3336 | BINSLOT("__or__", nb_or, slot_nb_or, "|"), |
| 3337 | RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), |
| 3338 | NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, |
| 3339 | "x.__coerce__(y) <==> coerce(x, y)"), |
| 3340 | UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, |
| 3341 | "int(x)"), |
| 3342 | UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, |
| 3343 | "long(x)"), |
| 3344 | UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, |
| 3345 | "float(x)"), |
| 3346 | UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, |
| 3347 | "oct(x)"), |
| 3348 | UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, |
| 3349 | "hex(x)"), |
| 3350 | IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, |
| 3351 | wrap_binaryfunc, "+"), |
| 3352 | IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, |
| 3353 | wrap_binaryfunc, "-"), |
| 3354 | IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, |
| 3355 | wrap_binaryfunc, "*"), |
| 3356 | IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, |
| 3357 | wrap_binaryfunc, "/"), |
| 3358 | IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, |
| 3359 | wrap_binaryfunc, "%"), |
| 3360 | IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, |
| 3361 | wrap_ternaryfunc, "**"), |
| 3362 | IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, |
| 3363 | wrap_binaryfunc, "<<"), |
| 3364 | IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, |
| 3365 | wrap_binaryfunc, ">>"), |
| 3366 | IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, |
| 3367 | wrap_binaryfunc, "&"), |
| 3368 | IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, |
| 3369 | wrap_binaryfunc, "^"), |
| 3370 | IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, |
| 3371 | wrap_binaryfunc, "|"), |
| 3372 | BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 3373 | RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), |
| 3374 | BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 3375 | RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), |
| 3376 | IBSLOT("__ifloordiv__", nb_inplace_floor_divide, |
| 3377 | slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), |
| 3378 | IBSLOT("__itruediv__", nb_inplace_true_divide, |
| 3379 | slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3380 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3381 | TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, |
| 3382 | "x.__str__() <==> str(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame^] | 3383 | TPSLOT("__str__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3384 | TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, |
| 3385 | "x.__repr__() <==> repr(x)"), |
Guido van Rossum | afe7a94 | 2001-10-29 14:33:44 +0000 | [diff] [blame^] | 3386 | TPSLOT("__repr__", tp_print, NULL, NULL, ""), |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3387 | TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, |
| 3388 | "x.__cmp__(y) <==> cmp(x,y)"), |
| 3389 | TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, |
| 3390 | "x.__hash__() <==> hash(x)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3391 | FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, |
| 3392 | "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3393 | TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3394 | wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), |
| 3395 | TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), |
| 3396 | TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), |
| 3397 | TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), |
| 3398 | TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, |
| 3399 | "x.__setattr__('name', value) <==> x.name = value"), |
| 3400 | TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), |
| 3401 | TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, |
| 3402 | "x.__delattr__('name') <==> del x.name"), |
| 3403 | TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), |
| 3404 | TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, |
| 3405 | "x.__lt__(y) <==> x<y"), |
| 3406 | TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, |
| 3407 | "x.__le__(y) <==> x<=y"), |
| 3408 | TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, |
| 3409 | "x.__eq__(y) <==> x==y"), |
| 3410 | TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, |
| 3411 | "x.__ne__(y) <==> x!=y"), |
| 3412 | TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, |
| 3413 | "x.__gt__(y) <==> x>y"), |
| 3414 | TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, |
| 3415 | "x.__ge__(y) <==> x>=y"), |
| 3416 | TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, |
| 3417 | "x.__iter__() <==> iter(x)"), |
| 3418 | TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, |
| 3419 | "x.next() -> the next value, or raise StopIteration"), |
| 3420 | TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, |
| 3421 | "descr.__get__(obj[, type]) -> value"), |
| 3422 | TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, |
| 3423 | "descr.__set__(obj, value)"), |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3424 | FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3425 | "x.__init__(...) initializes x; " |
Guido van Rossum | c8e5645 | 2001-10-22 00:43:43 +0000 | [diff] [blame] | 3426 | "see x.__class__.__doc__ for signature", |
| 3427 | PyWrapperFlag_KEYWORDS), |
| 3428 | TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3429 | {NULL} |
| 3430 | }; |
| 3431 | |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3432 | static void ** |
| 3433 | slotptr(PyTypeObject *type, int offset) |
| 3434 | { |
| 3435 | char *ptr; |
| 3436 | |
| 3437 | assert(offset >= 0); |
| 3438 | assert(offset < offsetof(etype, as_buffer)); |
| 3439 | if (offset >= offsetof(etype, as_mapping)) { |
| 3440 | ptr = (void *)type->tp_as_mapping; |
| 3441 | offset -= offsetof(etype, as_mapping); |
| 3442 | } |
| 3443 | else if (offset >= offsetof(etype, as_sequence)) { |
| 3444 | ptr = (void *)type->tp_as_sequence; |
| 3445 | offset -= offsetof(etype, as_sequence); |
| 3446 | } |
| 3447 | else if (offset >= offsetof(etype, as_number)) { |
| 3448 | ptr = (void *)type->tp_as_number; |
| 3449 | offset -= offsetof(etype, as_number); |
| 3450 | } |
| 3451 | else { |
| 3452 | ptr = (void *)type; |
| 3453 | } |
| 3454 | if (ptr != NULL) |
| 3455 | ptr += offset; |
| 3456 | return (void **)ptr; |
| 3457 | } |
Guido van Rossum | f040ede | 2001-08-07 16:40:56 +0000 | [diff] [blame] | 3458 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3459 | staticforward int recurse_down_subclasses(PyTypeObject *type, |
| 3460 | slotdef **pp, PyObject *name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3461 | |
| 3462 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3463 | update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3464 | { |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3465 | slotdef **pp; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3466 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3467 | for (pp = pp0; *pp; pp++) { |
| 3468 | slotdef *p = *pp; |
| 3469 | PyObject *descr; |
| 3470 | PyWrapperDescrObject *d; |
| 3471 | void *generic = NULL, *specific = NULL; |
| 3472 | int use_generic = 0; |
| 3473 | int offset = p->offset; |
| 3474 | void **ptr = slotptr(type, offset); |
| 3475 | if (ptr == NULL) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3476 | continue; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3477 | do { |
| 3478 | descr = _PyType_Lookup(type, p->name_strobj); |
| 3479 | if (descr == NULL) |
| 3480 | continue; |
| 3481 | generic = p->function; |
| 3482 | if (descr->ob_type == &PyWrapperDescr_Type) { |
| 3483 | d = (PyWrapperDescrObject *)descr; |
| 3484 | if (d->d_base->wrapper == p->wrapper && |
| 3485 | PyType_IsSubtype(type, d->d_type)) { |
| 3486 | if (specific == NULL || |
| 3487 | specific == d->d_wrapped) |
| 3488 | specific = d->d_wrapped; |
| 3489 | else |
| 3490 | use_generic = 1; |
| 3491 | } |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3492 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3493 | else |
| 3494 | use_generic = 1; |
| 3495 | } while ((++p)->offset == offset); |
| 3496 | if (specific && !use_generic) |
| 3497 | *ptr = specific; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3498 | else |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3499 | *ptr = generic; |
| 3500 | } |
| 3501 | return recurse_down_subclasses(type, pp0, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3502 | } |
| 3503 | |
| 3504 | static int |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3505 | recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3506 | { |
| 3507 | PyTypeObject *subclass; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3508 | PyObject *ref, *subclasses, *dict; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3509 | int i, n; |
| 3510 | |
| 3511 | subclasses = type->tp_subclasses; |
| 3512 | if (subclasses == NULL) |
| 3513 | return 0; |
| 3514 | assert(PyList_Check(subclasses)); |
| 3515 | n = PyList_GET_SIZE(subclasses); |
| 3516 | for (i = 0; i < n; i++) { |
| 3517 | ref = PyList_GET_ITEM(subclasses, i); |
| 3518 | assert(PyWeakref_CheckRef(ref)); |
| 3519 | subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); |
| 3520 | if (subclass == NULL) |
| 3521 | continue; |
| 3522 | assert(PyType_Check(subclass)); |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3523 | /* Avoid recursing down into unaffected classes */ |
| 3524 | dict = subclass->tp_dict; |
| 3525 | if (dict != NULL && PyDict_Check(dict) && |
| 3526 | PyDict_GetItem(dict, name) != NULL) |
| 3527 | continue; |
| 3528 | if (update_these_slots(subclass, pp, name) < 0) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3529 | return -1; |
| 3530 | } |
| 3531 | return 0; |
| 3532 | } |
| 3533 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3534 | static int |
| 3535 | slotdef_cmp(const void *aa, const void *bb) |
| 3536 | { |
| 3537 | const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; |
| 3538 | int c = a->offset - b->offset; |
| 3539 | if (c != 0) |
| 3540 | return c; |
| 3541 | else |
| 3542 | return a - b; |
| 3543 | } |
| 3544 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3545 | static void |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3546 | init_slotdefs(void) |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3547 | { |
| 3548 | slotdef *p; |
| 3549 | static int initialized = 0; |
| 3550 | |
| 3551 | if (initialized) |
| 3552 | return; |
| 3553 | for (p = slotdefs; p->name; p++) { |
| 3554 | p->name_strobj = PyString_InternFromString(p->name); |
| 3555 | if (!p->name_strobj) |
| 3556 | Py_FatalError("XXX ouch"); |
| 3557 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3558 | qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), |
| 3559 | slotdef_cmp); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3560 | initialized = 1; |
| 3561 | } |
| 3562 | |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3563 | static int |
| 3564 | update_slot(PyTypeObject *type, PyObject *name) |
| 3565 | { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3566 | slotdef *ptrs[10]; |
| 3567 | slotdef *p; |
| 3568 | slotdef **pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3569 | int offset; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3570 | |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3571 | init_slotdefs(); |
| 3572 | pp = ptrs; |
| 3573 | for (p = slotdefs; p->name; p++) { |
| 3574 | /* XXX assume name is interned! */ |
| 3575 | if (p->name_strobj == name) |
| 3576 | *pp++ = p; |
| 3577 | } |
| 3578 | *pp = NULL; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3579 | for (pp = ptrs; *pp; pp++) { |
| 3580 | p = *pp; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3581 | offset = p->offset; |
| 3582 | while (p > slotdefs && (p-1)->offset == offset) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3583 | --p; |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3584 | *pp = p; |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3585 | } |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3586 | return update_these_slots(type, ptrs, name); |
Guido van Rossum | 875eeaa | 2001-10-11 18:33:53 +0000 | [diff] [blame] | 3587 | } |
| 3588 | |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3589 | static void |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3590 | fixup_slot_dispatchers(PyTypeObject *type) |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3591 | { |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3592 | slotdef *p; |
| 3593 | PyObject *mro, *descr; |
| 3594 | PyTypeObject *base; |
| 3595 | PyWrapperDescrObject *d; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3596 | int i, n, offset; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3597 | void **ptr; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3598 | void *generic, *specific; |
| 3599 | int use_generic; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3600 | |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3601 | init_slotdefs(); |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3602 | mro = type->tp_mro; |
| 3603 | assert(PyTuple_Check(mro)); |
| 3604 | n = PyTuple_GET_SIZE(mro); |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3605 | for (p = slotdefs; p->name; ) { |
| 3606 | offset = p->offset; |
| 3607 | ptr = slotptr(type, offset); |
| 3608 | if (!ptr) { |
| 3609 | do { |
| 3610 | ++p; |
| 3611 | } while (p->offset == offset); |
| 3612 | continue; |
| 3613 | } |
| 3614 | generic = specific = NULL; |
| 3615 | use_generic = 0; |
| 3616 | do { |
| 3617 | descr = NULL; |
| 3618 | for (i = 0; i < n; i++) { |
Guido van Rossum | b85a8b7 | 2001-10-16 17:00:48 +0000 | [diff] [blame] | 3619 | base = (PyTypeObject *) |
| 3620 | PyTuple_GET_ITEM(mro, i); |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3621 | assert(PyType_Check(base)); |
| 3622 | descr = PyDict_GetItem( |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3623 | base->tp_dict, p->name_strobj); |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3624 | if (descr != NULL) |
| 3625 | break; |
| 3626 | } |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3627 | if (descr == NULL) |
| 3628 | continue; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3629 | generic = p->function; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3630 | if (descr->ob_type == &PyWrapperDescr_Type) { |
| 3631 | d = (PyWrapperDescrObject *)descr; |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3632 | if (d->d_base->wrapper == p->wrapper && |
Guido van Rossum | caf5904 | 2001-10-17 07:15:43 +0000 | [diff] [blame] | 3633 | PyType_IsSubtype(type, d->d_type)) |
| 3634 | { |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3635 | if (specific == NULL || |
Guido van Rossum | caf5904 | 2001-10-17 07:15:43 +0000 | [diff] [blame] | 3636 | specific == d->d_wrapped) |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3637 | specific = d->d_wrapped; |
| 3638 | else |
| 3639 | use_generic = 1; |
Guido van Rossum | 7b9144b | 2001-10-09 19:39:46 +0000 | [diff] [blame] | 3640 | } |
| 3641 | } |
Guido van Rossum | d396b9c | 2001-10-13 20:02:41 +0000 | [diff] [blame] | 3642 | else |
| 3643 | use_generic = 1; |
| 3644 | } while ((++p)->offset == offset); |
| 3645 | if (specific && !use_generic) |
| 3646 | *ptr = specific; |
| 3647 | else |
| 3648 | *ptr = generic; |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3649 | } |
Tim Peters | 6d6c1a3 | 2001-08-02 04:15:00 +0000 | [diff] [blame] | 3650 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3651 | |
Guido van Rossum | 6d20407 | 2001-10-21 00:44:31 +0000 | [diff] [blame] | 3652 | /* This function is called by PyType_Ready() to populate the type's |
| 3653 | dictionary with method descriptors for function slots. For each |
| 3654 | function slot (like tp_repr) that's defined in the type, one or |
| 3655 | more corresponding descriptors are added in the type's tp_dict |
| 3656 | dictionary under the appropriate name (like __repr__). Some |
| 3657 | function slots cause more than one descriptor to be added (for |
| 3658 | example, the nb_add slot adds both __add__ and __radd__ |
| 3659 | descriptors) and some function slots compete for the same |
| 3660 | descriptor (for example both sq_item and mp_subscript generate a |
| 3661 | __getitem__ descriptor). This only adds new descriptors and |
| 3662 | doesn't overwrite entries in tp_dict that were previously |
| 3663 | defined. The descriptors contain a reference to the C function |
| 3664 | they must call, so that it's safe if they are copied into a |
| 3665 | subtype's __dict__ and the subtype has a different C function in |
| 3666 | its slot -- calling the method defined by the descriptor will call |
| 3667 | the C function that was used to create it, rather than the C |
| 3668 | function present in the slot when it is called. (This is important |
| 3669 | because a subtype may have a C function in the slot that calls the |
| 3670 | method from the dictionary, and we want to avoid infinite recursion |
| 3671 | here.) */ |
| 3672 | |
| 3673 | static int |
| 3674 | add_operators(PyTypeObject *type) |
| 3675 | { |
| 3676 | PyObject *dict = type->tp_dict; |
| 3677 | slotdef *p; |
| 3678 | PyObject *descr; |
| 3679 | void **ptr; |
| 3680 | |
| 3681 | init_slotdefs(); |
| 3682 | for (p = slotdefs; p->name; p++) { |
| 3683 | if (p->wrapper == NULL) |
| 3684 | continue; |
| 3685 | ptr = slotptr(type, p->offset); |
| 3686 | if (!ptr || !*ptr) |
| 3687 | continue; |
| 3688 | if (PyDict_GetItem(dict, p->name_strobj)) |
| 3689 | continue; |
| 3690 | descr = PyDescr_NewWrapper(type, p, *ptr); |
| 3691 | if (descr == NULL) |
| 3692 | return -1; |
| 3693 | if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) |
| 3694 | return -1; |
| 3695 | Py_DECREF(descr); |
| 3696 | } |
| 3697 | if (type->tp_new != NULL) { |
| 3698 | if (add_tp_new_wrapper(type) < 0) |
| 3699 | return -1; |
| 3700 | } |
| 3701 | return 0; |
| 3702 | } |
| 3703 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3704 | |
| 3705 | /* Cooperative 'super' */ |
| 3706 | |
| 3707 | typedef struct { |
| 3708 | PyObject_HEAD |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3709 | PyTypeObject *type; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3710 | PyObject *obj; |
| 3711 | } superobject; |
| 3712 | |
Guido van Rossum | 6f79937 | 2001-09-20 20:46:19 +0000 | [diff] [blame] | 3713 | static PyMemberDef super_members[] = { |
| 3714 | {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, |
| 3715 | "the class invoking super()"}, |
| 3716 | {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, |
| 3717 | "the instance invoking super(); may be None"}, |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3718 | {0} |
| 3719 | }; |
| 3720 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3721 | static void |
| 3722 | super_dealloc(PyObject *self) |
| 3723 | { |
| 3724 | superobject *su = (superobject *)self; |
| 3725 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3726 | _PyObject_GC_UNTRACK(self); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3727 | Py_XDECREF(su->obj); |
| 3728 | Py_XDECREF(su->type); |
| 3729 | self->ob_type->tp_free(self); |
| 3730 | } |
| 3731 | |
| 3732 | static PyObject * |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3733 | super_repr(PyObject *self) |
| 3734 | { |
| 3735 | superobject *su = (superobject *)self; |
| 3736 | |
| 3737 | if (su->obj) |
| 3738 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 3739 | "<super: <class '%s'>, <%s object>>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3740 | su->type ? su->type->tp_name : "NULL", |
| 3741 | su->obj->ob_type->tp_name); |
| 3742 | else |
| 3743 | return PyString_FromFormat( |
Guido van Rossum | a4cb788 | 2001-09-25 03:56:29 +0000 | [diff] [blame] | 3744 | "<super: <class '%s'>, NULL>", |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3745 | su->type ? su->type->tp_name : "NULL"); |
| 3746 | } |
| 3747 | |
| 3748 | static PyObject * |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3749 | super_getattro(PyObject *self, PyObject *name) |
| 3750 | { |
| 3751 | superobject *su = (superobject *)self; |
| 3752 | |
| 3753 | if (su->obj != NULL) { |
| 3754 | PyObject *mro, *res, *tmp; |
| 3755 | descrgetfunc f; |
| 3756 | int i, n; |
| 3757 | |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3758 | mro = su->obj->ob_type->tp_mro; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3759 | if (mro == NULL) |
| 3760 | n = 0; |
| 3761 | else { |
| 3762 | assert(PyTuple_Check(mro)); |
| 3763 | n = PyTuple_GET_SIZE(mro); |
| 3764 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3765 | for (i = 0; i < n; i++) { |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3766 | if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3767 | break; |
| 3768 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3769 | if (i >= n && PyType_Check(su->obj)) { |
| 3770 | mro = ((PyTypeObject *)(su->obj))->tp_mro; |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3771 | if (mro == NULL) |
| 3772 | n = 0; |
| 3773 | else { |
| 3774 | assert(PyTuple_Check(mro)); |
| 3775 | n = PyTuple_GET_SIZE(mro); |
| 3776 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3777 | for (i = 0; i < n; i++) { |
| 3778 | if ((PyObject *)(su->type) == |
| 3779 | PyTuple_GET_ITEM(mro, i)) |
| 3780 | break; |
| 3781 | } |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3782 | } |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3783 | i++; |
| 3784 | res = NULL; |
| 3785 | for (; i < n; i++) { |
| 3786 | tmp = PyTuple_GET_ITEM(mro, i); |
| 3787 | assert(PyType_Check(tmp)); |
| 3788 | res = PyDict_GetItem( |
Guido van Rossum | 687ae00 | 2001-10-15 22:03:32 +0000 | [diff] [blame] | 3789 | ((PyTypeObject *)tmp)->tp_dict, name); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3790 | if (res != NULL) { |
| 3791 | Py_INCREF(res); |
| 3792 | f = res->ob_type->tp_descr_get; |
| 3793 | if (f != NULL) { |
| 3794 | tmp = f(res, su->obj, res); |
| 3795 | Py_DECREF(res); |
| 3796 | res = tmp; |
| 3797 | } |
| 3798 | return res; |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | return PyObject_GenericGetAttr(self, name); |
| 3803 | } |
| 3804 | |
| 3805 | static PyObject * |
| 3806 | super_descr_get(PyObject *self, PyObject *obj, PyObject *type) |
| 3807 | { |
| 3808 | superobject *su = (superobject *)self; |
| 3809 | superobject *new; |
| 3810 | |
| 3811 | if (obj == NULL || obj == Py_None || su->obj != NULL) { |
| 3812 | /* Not binding to an object, or already bound */ |
| 3813 | Py_INCREF(self); |
| 3814 | return self; |
| 3815 | } |
| 3816 | new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL); |
| 3817 | if (new == NULL) |
| 3818 | return NULL; |
| 3819 | Py_INCREF(su->type); |
| 3820 | Py_INCREF(obj); |
| 3821 | new->type = su->type; |
| 3822 | new->obj = obj; |
| 3823 | return (PyObject *)new; |
| 3824 | } |
| 3825 | |
| 3826 | static int |
| 3827 | super_init(PyObject *self, PyObject *args, PyObject *kwds) |
| 3828 | { |
| 3829 | superobject *su = (superobject *)self; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3830 | PyTypeObject *type; |
| 3831 | PyObject *obj = NULL; |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3832 | |
| 3833 | if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) |
| 3834 | return -1; |
| 3835 | if (obj == Py_None) |
| 3836 | obj = NULL; |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3837 | if (obj != NULL && |
| 3838 | !PyType_IsSubtype(obj->ob_type, type) && |
| 3839 | !(PyType_Check(obj) && |
| 3840 | PyType_IsSubtype((PyTypeObject *)obj, type))) { |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3841 | PyErr_SetString(PyExc_TypeError, |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3842 | "super(type, obj): " |
| 3843 | "obj must be an instance or subtype of type"); |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3844 | return -1; |
| 3845 | } |
| 3846 | Py_INCREF(type); |
| 3847 | Py_XINCREF(obj); |
| 3848 | su->type = type; |
| 3849 | su->obj = obj; |
| 3850 | return 0; |
| 3851 | } |
| 3852 | |
| 3853 | static char super_doc[] = |
| 3854 | "super(type) -> unbound super object\n" |
| 3855 | "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" |
Guido van Rossum | e705ef1 | 2001-08-29 15:47:06 +0000 | [diff] [blame] | 3856 | "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3857 | "Typical use to call a cooperative superclass method:\n" |
| 3858 | "class C(B):\n" |
| 3859 | " def meth(self, arg):\n" |
| 3860 | " super(C, self).meth(arg)"; |
| 3861 | |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3862 | static int |
| 3863 | super_traverse(PyObject *self, visitproc visit, void *arg) |
| 3864 | { |
| 3865 | superobject *su = (superobject *)self; |
| 3866 | int err; |
| 3867 | |
| 3868 | #define VISIT(SLOT) \ |
| 3869 | if (SLOT) { \ |
| 3870 | err = visit((PyObject *)(SLOT), arg); \ |
| 3871 | if (err) \ |
| 3872 | return err; \ |
| 3873 | } |
| 3874 | |
| 3875 | VISIT(su->obj); |
| 3876 | VISIT(su->type); |
| 3877 | |
| 3878 | #undef VISIT |
| 3879 | |
| 3880 | return 0; |
| 3881 | } |
| 3882 | |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3883 | PyTypeObject PySuper_Type = { |
| 3884 | PyObject_HEAD_INIT(&PyType_Type) |
| 3885 | 0, /* ob_size */ |
| 3886 | "super", /* tp_name */ |
| 3887 | sizeof(superobject), /* tp_basicsize */ |
| 3888 | 0, /* tp_itemsize */ |
| 3889 | /* methods */ |
| 3890 | super_dealloc, /* tp_dealloc */ |
| 3891 | 0, /* tp_print */ |
| 3892 | 0, /* tp_getattr */ |
| 3893 | 0, /* tp_setattr */ |
| 3894 | 0, /* tp_compare */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3895 | super_repr, /* tp_repr */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3896 | 0, /* tp_as_number */ |
| 3897 | 0, /* tp_as_sequence */ |
| 3898 | 0, /* tp_as_mapping */ |
| 3899 | 0, /* tp_hash */ |
| 3900 | 0, /* tp_call */ |
| 3901 | 0, /* tp_str */ |
| 3902 | super_getattro, /* tp_getattro */ |
| 3903 | 0, /* tp_setattro */ |
| 3904 | 0, /* tp_as_buffer */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3905 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
| 3906 | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3907 | super_doc, /* tp_doc */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3908 | super_traverse, /* tp_traverse */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3909 | 0, /* tp_clear */ |
| 3910 | 0, /* tp_richcompare */ |
| 3911 | 0, /* tp_weaklistoffset */ |
| 3912 | 0, /* tp_iter */ |
| 3913 | 0, /* tp_iternext */ |
| 3914 | 0, /* tp_methods */ |
Guido van Rossum | 41eb14d | 2001-08-30 23:13:11 +0000 | [diff] [blame] | 3915 | super_members, /* tp_members */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3916 | 0, /* tp_getset */ |
| 3917 | 0, /* tp_base */ |
| 3918 | 0, /* tp_dict */ |
| 3919 | super_descr_get, /* tp_descr_get */ |
| 3920 | 0, /* tp_descr_set */ |
| 3921 | 0, /* tp_dictoffset */ |
| 3922 | super_init, /* tp_init */ |
| 3923 | PyType_GenericAlloc, /* tp_alloc */ |
| 3924 | PyType_GenericNew, /* tp_new */ |
Guido van Rossum | 048eb75 | 2001-10-02 21:24:57 +0000 | [diff] [blame] | 3925 | _PyObject_GC_Del, /* tp_free */ |
Guido van Rossum | 705f0f5 | 2001-08-24 16:47:00 +0000 | [diff] [blame] | 3926 | }; |