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