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