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