blob: 4e8f9676d391b58756eba1f51cd47d4d62a0e3bf [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Type object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum6f799372001-09-20 20:46:19 +00007static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +00008 {"__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 Rossum9676b222001-08-17 20:32:36 +000012 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 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 Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000023type_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
35static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000036type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000037{
Guido van Rossumc3542212001-08-16 09:18:56 +000038 PyObject *mod;
39 char *s;
40
41 s = strrchr(type->tp_name, '.');
42 if (s != NULL)
43 return PyString_FromStringAndSize(type->tp_name,
44 (int)(s - type->tp_name));
45 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
46 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000047 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000048 if (mod != NULL && PyString_Check(mod)) {
49 Py_INCREF(mod);
50 return mod;
51 }
52 PyErr_SetString(PyExc_AttributeError, "__module__");
53 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000054}
55
Guido van Rossum3926a632001-09-25 16:25:58 +000056static int
57type_set_module(PyTypeObject *type, PyObject *value, void *context)
58{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000059 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000060 strrchr(type->tp_name, '.')) {
61 PyErr_Format(PyExc_TypeError,
62 "can't set %s.__module__", type->tp_name);
63 return -1;
64 }
65 if (!value) {
66 PyErr_Format(PyExc_TypeError,
67 "can't delete %s.__module__", type->tp_name);
68 return -1;
69 }
70 return PyDict_SetItemString(type->tp_dict, "__module__", value);
71}
72
Tim Peters6d6c1a32001-08-02 04:15:00 +000073static PyObject *
74type_dict(PyTypeObject *type, void *context)
75{
76 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 Py_INCREF(Py_None);
78 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000079 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000080 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000081}
82
Neil Schemenauerf23473f2001-10-21 22:28:58 +000083static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +000084 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +000085 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000086 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000087 {0}
88};
89
Martin v. Löwis0163d6d2001-06-09 07:34:05 +000090static int
91type_compare(PyObject *v, PyObject *w)
92{
93 /* This is called with type objects only. So we
94 can just compare the addresses. */
95 Py_uintptr_t vv = (Py_uintptr_t)v;
96 Py_uintptr_t ww = (Py_uintptr_t)w;
97 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
98}
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000101type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000103 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000104 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000105
106 mod = type_module(type, NULL);
107 if (mod == NULL)
108 PyErr_Clear();
109 else if (!PyString_Check(mod)) {
110 Py_DECREF(mod);
111 mod = NULL;
112 }
113 name = type_name(type, NULL);
114 if (name == NULL)
115 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000116
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000117 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
118 kind = "class";
119 else
120 kind = "type";
121
Barry Warsaw7ce36942001-08-24 18:34:26 +0000122 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000123 rtn = PyString_FromFormat("<%s '%s.%s'>",
124 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000125 PyString_AS_STRING(mod),
126 PyString_AS_STRING(name));
127 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000128 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000129 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000130
Guido van Rossumc3542212001-08-16 09:18:56 +0000131 Py_XDECREF(mod);
132 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000133 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136static PyObject *
137type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
138{
139 PyObject *obj;
140
141 if (type->tp_new == NULL) {
142 PyErr_Format(PyExc_TypeError,
143 "cannot create '%.100s' instances",
144 type->tp_name);
145 return NULL;
146 }
147
Tim Peters3f996e72001-09-13 19:18:27 +0000148 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000150 /* Ugly exception: when the call was type(something),
151 don't call tp_init on the result. */
152 if (type == &PyType_Type &&
153 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
154 (kwds == NULL ||
155 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
156 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000157 type = obj->ob_type;
158 if (type->tp_init != NULL &&
159 type->tp_init(obj, args, kwds) < 0) {
160 Py_DECREF(obj);
161 obj = NULL;
162 }
163 }
164 return obj;
165}
166
167PyObject *
168PyType_GenericAlloc(PyTypeObject *type, int nitems)
169{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000171 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000172
173 if (PyType_IS_GC(type))
Tim Peters6d483d32001-10-06 21:27:34 +0000174 obj = _PyObject_GC_Malloc(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000175 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000176 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000177
Neil Schemenauerc806c882001-08-29 23:54:54 +0000178 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000179 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000180
Neil Schemenauerc806c882001-08-29 23:54:54 +0000181 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000182
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
184 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000185
Tim Peters6d6c1a32001-08-02 04:15:00 +0000186 if (type->tp_itemsize == 0)
187 PyObject_INIT(obj, type);
188 else
189 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000190
Tim Peters6d6c1a32001-08-02 04:15:00 +0000191 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000192 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193 return obj;
194}
195
196PyObject *
197PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
198{
199 return type->tp_alloc(type, 0);
200}
201
Guido van Rossum9475a232001-10-05 20:51:39 +0000202/* Helpers for subtyping */
203
204static int
205subtype_traverse(PyObject *self, visitproc visit, void *arg)
206{
207 PyTypeObject *type, *base;
208 traverseproc f;
209 int err;
210
211 /* Find the nearest base with a different tp_traverse */
212 type = self->ob_type;
213 base = type->tp_base;
214 while ((f = base->tp_traverse) == subtype_traverse) {
215 base = base->tp_base;
216 assert(base);
217 }
218
219 if (type->tp_dictoffset != base->tp_dictoffset) {
220 PyObject **dictptr = _PyObject_GetDictPtr(self);
221 if (dictptr && *dictptr) {
222 err = visit(*dictptr, arg);
223 if (err)
224 return err;
225 }
226 }
227
228 if (f)
229 return f(self, visit, arg);
230 return 0;
231}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000232
233static void
234subtype_dealloc(PyObject *self)
235{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000236 PyTypeObject *type, *base;
237 destructor f;
238
239 /* This exists so we can DECREF self->ob_type */
240
241 /* Find the nearest base with a different tp_dealloc */
242 type = self->ob_type;
243 base = type->tp_base;
244 while ((f = base->tp_dealloc) == subtype_dealloc) {
245 base = base->tp_base;
246 assert(base);
247 }
248
249 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000250 if (type->tp_dictoffset && !base->tp_dictoffset) {
251 PyObject **dictptr = _PyObject_GetDictPtr(self);
252 if (dictptr != NULL) {
253 PyObject *dict = *dictptr;
254 if (dict != NULL) {
255 Py_DECREF(dict);
256 *dictptr = NULL;
257 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000258 }
259 }
260
Guido van Rossum9676b222001-08-17 20:32:36 +0000261 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000262 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000263 PyObject_ClearWeakRefs(self);
264
Tim Peters6d6c1a32001-08-02 04:15:00 +0000265 /* Finalize GC if the base doesn't do GC and we do */
266 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000267 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000268
269 /* Call the base tp_dealloc() */
270 assert(f);
271 f(self);
272
273 /* Can't reference self beyond this point */
274 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
275 Py_DECREF(type);
276 }
277}
278
Tim Peters6d6c1a32001-08-02 04:15:00 +0000279staticforward PyTypeObject *solid_base(PyTypeObject *type);
280
281typedef struct {
282 PyTypeObject type;
283 PyNumberMethods as_number;
284 PySequenceMethods as_sequence;
285 PyMappingMethods as_mapping;
286 PyBufferProcs as_buffer;
287 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000288 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000289} etype;
290
291/* type test with subclassing support */
292
293int
294PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
295{
296 PyObject *mro;
297
Guido van Rossum9478d072001-09-07 18:52:13 +0000298 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
299 return b == a || b == &PyBaseObject_Type;
300
Tim Peters6d6c1a32001-08-02 04:15:00 +0000301 mro = a->tp_mro;
302 if (mro != NULL) {
303 /* Deal with multiple inheritance without recursion
304 by walking the MRO tuple */
305 int i, n;
306 assert(PyTuple_Check(mro));
307 n = PyTuple_GET_SIZE(mro);
308 for (i = 0; i < n; i++) {
309 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
310 return 1;
311 }
312 return 0;
313 }
314 else {
315 /* a is not completely initilized yet; follow tp_base */
316 do {
317 if (a == b)
318 return 1;
319 a = a->tp_base;
320 } while (a != NULL);
321 return b == &PyBaseObject_Type;
322 }
323}
324
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000325/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000326 without looking in the instance dictionary
327 (so we can't use PyObject_GetAttr) but still binding
328 it to the instance. The arguments are the object,
329 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000330 static variable used to cache the interned Python string.
331
332 Two variants:
333
334 - lookup_maybe() returns NULL without raising an exception
335 when the _PyType_Lookup() call fails;
336
337 - lookup_method() always raises an exception upon errors.
338*/
Guido van Rossum60718732001-08-28 17:47:51 +0000339
340static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000341lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000342{
343 PyObject *res;
344
345 if (*attrobj == NULL) {
346 *attrobj = PyString_InternFromString(attrstr);
347 if (*attrobj == NULL)
348 return NULL;
349 }
350 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000351 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000352 descrgetfunc f;
353 if ((f = res->ob_type->tp_descr_get) == NULL)
354 Py_INCREF(res);
355 else
356 res = f(res, self, (PyObject *)(self->ob_type));
357 }
358 return res;
359}
360
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000361static PyObject *
362lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
363{
364 PyObject *res = lookup_maybe(self, attrstr, attrobj);
365 if (res == NULL && !PyErr_Occurred())
366 PyErr_SetObject(PyExc_AttributeError, *attrobj);
367 return res;
368}
369
Guido van Rossum2730b132001-08-28 18:22:14 +0000370/* A variation of PyObject_CallMethod that uses lookup_method()
371 instead of PyObject_GetAttrString(). This uses the same convention
372 as lookup_method to cache the interned name string object. */
373
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000374static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000375call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
376{
377 va_list va;
378 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000379 va_start(va, format);
380
Guido van Rossumda21c012001-10-03 00:50:18 +0000381 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000382 if (func == NULL) {
383 va_end(va);
384 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000385 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000386 return NULL;
387 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000388
389 if (format && *format)
390 args = Py_VaBuildValue(format, va);
391 else
392 args = PyTuple_New(0);
393
394 va_end(va);
395
396 if (args == NULL)
397 return NULL;
398
399 assert(PyTuple_Check(args));
400 retval = PyObject_Call(func, args, NULL);
401
402 Py_DECREF(args);
403 Py_DECREF(func);
404
405 return retval;
406}
407
408/* Clone of call_method() that returns NotImplemented when the lookup fails. */
409
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000410static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000411call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
412{
413 va_list va;
414 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000415 va_start(va, format);
416
Guido van Rossumda21c012001-10-03 00:50:18 +0000417 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000418 if (func == NULL) {
419 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000420 if (!PyErr_Occurred()) {
421 Py_INCREF(Py_NotImplemented);
422 return Py_NotImplemented;
423 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000424 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000425 }
426
427 if (format && *format)
428 args = Py_VaBuildValue(format, va);
429 else
430 args = PyTuple_New(0);
431
432 va_end(va);
433
Guido van Rossum717ce002001-09-14 16:58:08 +0000434 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000435 return NULL;
436
Guido van Rossum717ce002001-09-14 16:58:08 +0000437 assert(PyTuple_Check(args));
438 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000439
440 Py_DECREF(args);
441 Py_DECREF(func);
442
443 return retval;
444}
445
Tim Peters6d6c1a32001-08-02 04:15:00 +0000446/* Method resolution order algorithm from "Putting Metaclasses to Work"
447 by Forman and Danforth (Addison-Wesley 1999). */
448
449static int
450conservative_merge(PyObject *left, PyObject *right)
451{
452 int left_size;
453 int right_size;
454 int i, j, r, ok;
455 PyObject *temp, *rr;
456
457 assert(PyList_Check(left));
458 assert(PyList_Check(right));
459
460 again:
461 left_size = PyList_GET_SIZE(left);
462 right_size = PyList_GET_SIZE(right);
463 for (i = 0; i < left_size; i++) {
464 for (j = 0; j < right_size; j++) {
465 if (PyList_GET_ITEM(left, i) ==
466 PyList_GET_ITEM(right, j)) {
467 /* found a merge point */
468 temp = PyList_New(0);
469 if (temp == NULL)
470 return -1;
471 for (r = 0; r < j; r++) {
472 rr = PyList_GET_ITEM(right, r);
473 ok = PySequence_Contains(left, rr);
474 if (ok < 0) {
475 Py_DECREF(temp);
476 return -1;
477 }
478 if (!ok) {
479 ok = PyList_Append(temp, rr);
480 if (ok < 0) {
481 Py_DECREF(temp);
482 return -1;
483 }
484 }
485 }
486 ok = PyList_SetSlice(left, i, i, temp);
487 Py_DECREF(temp);
488 if (ok < 0)
489 return -1;
490 ok = PyList_SetSlice(right, 0, j+1, NULL);
491 if (ok < 0)
492 return -1;
493 goto again;
494 }
495 }
496 }
497 return PyList_SetSlice(left, left_size, left_size, right);
498}
499
500static int
501serious_order_disagreements(PyObject *left, PyObject *right)
502{
503 return 0; /* XXX later -- for now, we cheat: "don't do that" */
504}
505
506static PyObject *
507mro_implementation(PyTypeObject *type)
508{
509 int i, n, ok;
510 PyObject *bases, *result;
511
512 bases = type->tp_bases;
513 n = PyTuple_GET_SIZE(bases);
514 result = Py_BuildValue("[O]", (PyObject *)type);
515 if (result == NULL)
516 return NULL;
517 for (i = 0; i < n; i++) {
518 PyTypeObject *base =
519 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
520 PyObject *parentMRO = PySequence_List(base->tp_mro);
521 if (parentMRO == NULL) {
522 Py_DECREF(result);
523 return NULL;
524 }
525 if (serious_order_disagreements(result, parentMRO)) {
526 Py_DECREF(result);
527 return NULL;
528 }
529 ok = conservative_merge(result, parentMRO);
530 Py_DECREF(parentMRO);
531 if (ok < 0) {
532 Py_DECREF(result);
533 return NULL;
534 }
535 }
536 return result;
537}
538
539static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000540mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000541{
542 PyTypeObject *type = (PyTypeObject *)self;
543
Tim Peters6d6c1a32001-08-02 04:15:00 +0000544 return mro_implementation(type);
545}
546
547static int
548mro_internal(PyTypeObject *type)
549{
550 PyObject *mro, *result, *tuple;
551
552 if (type->ob_type == &PyType_Type) {
553 result = mro_implementation(type);
554 }
555 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000556 static PyObject *mro_str;
557 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000558 if (mro == NULL)
559 return -1;
560 result = PyObject_CallObject(mro, NULL);
561 Py_DECREF(mro);
562 }
563 if (result == NULL)
564 return -1;
565 tuple = PySequence_Tuple(result);
566 Py_DECREF(result);
567 type->tp_mro = tuple;
568 return 0;
569}
570
571
572/* Calculate the best base amongst multiple base classes.
573 This is the first one that's on the path to the "solid base". */
574
575static PyTypeObject *
576best_base(PyObject *bases)
577{
578 int i, n;
579 PyTypeObject *base, *winner, *candidate, *base_i;
580
581 assert(PyTuple_Check(bases));
582 n = PyTuple_GET_SIZE(bases);
583 assert(n > 0);
584 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
585 winner = &PyBaseObject_Type;
586 for (i = 0; i < n; i++) {
587 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
588 if (!PyType_Check((PyObject *)base_i)) {
589 PyErr_SetString(
590 PyExc_TypeError,
591 "bases must be types");
592 return NULL;
593 }
594 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000595 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000596 return NULL;
597 }
598 candidate = solid_base(base_i);
599 if (PyType_IsSubtype(winner, candidate))
600 ;
601 else if (PyType_IsSubtype(candidate, winner)) {
602 winner = candidate;
603 base = base_i;
604 }
605 else {
606 PyErr_SetString(
607 PyExc_TypeError,
608 "multiple bases have "
609 "instance lay-out conflict");
610 return NULL;
611 }
612 }
613 assert(base != NULL);
614 return base;
615}
616
617static int
618extra_ivars(PyTypeObject *type, PyTypeObject *base)
619{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000620 size_t t_size = type->tp_basicsize;
621 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622
Guido van Rossum9676b222001-08-17 20:32:36 +0000623 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624 if (type->tp_itemsize || base->tp_itemsize) {
625 /* If itemsize is involved, stricter rules */
626 return t_size != b_size ||
627 type->tp_itemsize != base->tp_itemsize;
628 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000629 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
630 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
631 t_size -= sizeof(PyObject *);
632 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
633 type->tp_dictoffset + sizeof(PyObject *) == t_size)
634 t_size -= sizeof(PyObject *);
635
636 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000637}
638
639static PyTypeObject *
640solid_base(PyTypeObject *type)
641{
642 PyTypeObject *base;
643
644 if (type->tp_base)
645 base = solid_base(type->tp_base);
646 else
647 base = &PyBaseObject_Type;
648 if (extra_ivars(type, base))
649 return type;
650 else
651 return base;
652}
653
654staticforward void object_dealloc(PyObject *);
655staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000656staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000657staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000658
659static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000660subtype_dict(PyObject *obj, void *context)
661{
662 PyObject **dictptr = _PyObject_GetDictPtr(obj);
663 PyObject *dict;
664
665 if (dictptr == NULL) {
666 PyErr_SetString(PyExc_AttributeError,
667 "This object has no __dict__");
668 return NULL;
669 }
670 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000671 if (dict == NULL)
672 *dictptr = dict = PyDict_New();
673 Py_XINCREF(dict);
674 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000675}
676
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000677static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000678 {"__dict__", subtype_dict, NULL, NULL},
679 {0},
680};
681
682static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
684{
685 PyObject *name, *bases, *dict;
686 static char *kwlist[] = {"name", "bases", "dict", 0};
687 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000688 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000690 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000691 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000692
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000693 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694 if (metatype == &PyType_Type &&
695 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
696 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697 PyObject *x = PyTuple_GET_ITEM(args, 0);
698 Py_INCREF(x->ob_type);
699 return (PyObject *) x->ob_type;
700 }
701
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000702 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
704 &name,
705 &PyTuple_Type, &bases,
706 &PyDict_Type, &dict))
707 return NULL;
708
709 /* Determine the proper metatype to deal with this,
710 and check for metatype conflicts while we're at it.
711 Note that if some other metatype wins to contract,
712 it's possible that its instances are not types. */
713 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000714 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715 for (i = 0; i < nbases; i++) {
716 tmp = PyTuple_GET_ITEM(bases, i);
717 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000718 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000720 if (PyType_IsSubtype(tmptype, winner)) {
721 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722 continue;
723 }
724 PyErr_SetString(PyExc_TypeError,
725 "metatype conflict among bases");
726 return NULL;
727 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000728 if (winner != metatype) {
729 if (winner->tp_new != type_new) /* Pass it to the winner */
730 return winner->tp_new(winner, args, kwds);
731 metatype = winner;
732 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733
734 /* Adjust for empty tuple bases */
735 if (nbases == 0) {
736 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
737 if (bases == NULL)
738 return NULL;
739 nbases = 1;
740 }
741 else
742 Py_INCREF(bases);
743
744 /* XXX From here until type is allocated, "return NULL" leaks bases! */
745
746 /* Calculate best base, and check that all bases are type objects */
747 base = best_base(bases);
748 if (base == NULL)
749 return NULL;
750 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
751 PyErr_Format(PyExc_TypeError,
752 "type '%.100s' is not an acceptable base type",
753 base->tp_name);
754 return NULL;
755 }
756
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757 /* Check for a __slots__ sequence variable in dict, and count it */
758 slots = PyDict_GetItemString(dict, "__slots__");
759 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000760 add_dict = 0;
761 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762 if (slots != NULL) {
763 /* Make it into a tuple */
764 if (PyString_Check(slots))
765 slots = Py_BuildValue("(O)", slots);
766 else
767 slots = PySequence_Tuple(slots);
768 if (slots == NULL)
769 return NULL;
770 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000771 if (nslots > 0 && base->tp_itemsize != 0) {
772 PyErr_Format(PyExc_TypeError,
773 "nonempty __slots__ "
774 "not supported for subtype of '%s'",
775 base->tp_name);
776 return NULL;
777 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000778 for (i = 0; i < nslots; i++) {
779 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
780 PyErr_SetString(PyExc_TypeError,
781 "__slots__ must be a sequence of strings");
782 Py_DECREF(slots);
783 return NULL;
784 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000785 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 }
787 }
788 if (slots == NULL && base->tp_dictoffset == 0 &&
789 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000790 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000791 add_dict++;
792 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000793 if (slots == NULL && base->tp_weaklistoffset == 0 &&
794 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000795 nslots++;
796 add_weak++;
797 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798
799 /* XXX From here until type is safely allocated,
800 "return NULL" may leak slots! */
801
802 /* Allocate the type object */
803 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
804 if (type == NULL)
805 return NULL;
806
807 /* Keep name and slots alive in the extended type object */
808 et = (etype *)type;
809 Py_INCREF(name);
810 et->name = name;
811 et->slots = slots;
812
Guido van Rossumdc91b992001-08-08 22:26:22 +0000813 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000814 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
815 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +0000816 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
817 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000818
819 /* It's a new-style number unless it specifically inherits any
820 old-style numeric behavior */
821 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
822 (base->tp_as_number == NULL))
823 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
824
825 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826 type->tp_as_number = &et->as_number;
827 type->tp_as_sequence = &et->as_sequence;
828 type->tp_as_mapping = &et->as_mapping;
829 type->tp_as_buffer = &et->as_buffer;
830 type->tp_name = PyString_AS_STRING(name);
831
832 /* Set tp_base and tp_bases */
833 type->tp_bases = bases;
834 Py_INCREF(base);
835 type->tp_base = base;
836
Guido van Rossum687ae002001-10-15 22:03:32 +0000837 /* Initialize tp_dict from passed-in dict */
838 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000839 if (dict == NULL) {
840 Py_DECREF(type);
841 return NULL;
842 }
843
Guido van Rossumc3542212001-08-16 09:18:56 +0000844 /* Set __module__ in the dict */
845 if (PyDict_GetItemString(dict, "__module__") == NULL) {
846 tmp = PyEval_GetGlobals();
847 if (tmp != NULL) {
848 tmp = PyDict_GetItemString(tmp, "__name__");
849 if (tmp != NULL) {
850 if (PyDict_SetItemString(dict, "__module__",
851 tmp) < 0)
852 return NULL;
853 }
854 }
855 }
856
Tim Peters2f93e282001-10-04 05:27:00 +0000857 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
858 and is a string (tp_doc is a char* -- can't copy a general object
859 into it).
860 XXX What if it's a Unicode string? Don't know -- this ignores it.
861 */
862 {
863 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
864 if (doc != NULL && PyString_Check(doc)) {
865 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +0000866 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +0000867 if (type->tp_doc == NULL) {
868 Py_DECREF(type);
869 return NULL;
870 }
871 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
872 }
873 }
874
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875 /* Special-case __new__: if it's a plain function,
876 make it a static function */
877 tmp = PyDict_GetItemString(dict, "__new__");
878 if (tmp != NULL && PyFunction_Check(tmp)) {
879 tmp = PyStaticMethod_New(tmp);
880 if (tmp == NULL) {
881 Py_DECREF(type);
882 return NULL;
883 }
884 PyDict_SetItemString(dict, "__new__", tmp);
885 Py_DECREF(tmp);
886 }
887
888 /* Add descriptors for custom slots from __slots__, or for __dict__ */
889 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000890 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891 if (slots != NULL) {
892 for (i = 0; i < nslots; i++, mp++) {
893 mp->name = PyString_AS_STRING(
894 PyTuple_GET_ITEM(slots, i));
895 mp->type = T_OBJECT;
896 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000897 if (base->tp_weaklistoffset == 0 &&
898 strcmp(mp->name, "__weakref__") == 0)
899 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900 slotoffset += sizeof(PyObject *);
901 }
902 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000903 else {
904 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000905 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +0000906 type->tp_dictoffset =
907 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000908 else
909 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000910 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000911 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000912 }
913 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000914 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000915 type->tp_weaklistoffset = slotoffset;
916 mp->name = "__weakref__";
917 mp->type = T_OBJECT;
918 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +0000919 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +0000920 mp++;
921 slotoffset += sizeof(PyObject *);
922 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923 }
924 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000925 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000926 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000927
928 /* Special case some slots */
929 if (type->tp_dictoffset != 0 || nslots > 0) {
930 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
931 type->tp_getattro = PyObject_GenericGetAttr;
932 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
933 type->tp_setattro = PyObject_GenericSetAttr;
934 }
935 type->tp_dealloc = subtype_dealloc;
936
Guido van Rossum9475a232001-10-05 20:51:39 +0000937 /* Enable GC unless there are really no instance variables possible */
938 if (!(type->tp_basicsize == sizeof(PyObject) &&
939 type->tp_itemsize == 0))
940 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
941
Tim Peters6d6c1a32001-08-02 04:15:00 +0000942 /* Always override allocation strategy to use regular heap */
943 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +0000944 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
945 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +0000946 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +0000947 type->tp_clear = base->tp_clear;
948 }
949 else
950 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951
952 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000953 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000954 Py_DECREF(type);
955 return NULL;
956 }
957
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000958 /* Put the proper slots in place */
959 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000960
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 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! */
966PyObject *
967_PyType_Lookup(PyTypeObject *type, PyObject *name)
968{
969 int i, n;
970 PyObject *mro, *res, *dict;
971
Guido van Rossum687ae002001-10-15 22:03:32 +0000972 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973 mro = type->tp_mro;
974 assert(PyTuple_Check(mro));
975 n = PyTuple_GET_SIZE(mro);
976 for (i = 0; i < n; i++) {
977 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
978 assert(PyType_Check(type));
Guido van Rossum687ae002001-10-15 22:03:32 +0000979 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980 assert(dict && PyDict_Check(dict));
981 res = PyDict_GetItem(dict, name);
982 if (res != NULL)
983 return res;
984 }
985 return NULL;
986}
987
988/* This is similar to PyObject_GenericGetAttr(),
989 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
990static PyObject *
991type_getattro(PyTypeObject *type, PyObject *name)
992{
993 PyTypeObject *metatype = type->ob_type;
994 PyObject *descr, *res;
995 descrgetfunc f;
996
997 /* Initialize this type (we'll assume the metatype is initialized) */
998 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000999 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001000 return NULL;
1001 }
1002
1003 /* Get a descriptor from the metatype */
1004 descr = _PyType_Lookup(metatype, name);
1005 f = NULL;
1006 if (descr != NULL) {
1007 f = descr->ob_type->tp_descr_get;
1008 if (f != NULL && PyDescr_IsData(descr))
1009 return f(descr,
1010 (PyObject *)type, (PyObject *)metatype);
1011 }
1012
Guido van Rossum687ae002001-10-15 22:03:32 +00001013 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 res = _PyType_Lookup(type, name);
1015 if (res != NULL) {
1016 f = res->ob_type->tp_descr_get;
1017 if (f != NULL)
1018 return f(res, (PyObject *)NULL, (PyObject *)type);
1019 Py_INCREF(res);
1020 return res;
1021 }
1022
1023 /* Use the descriptor from the metatype */
1024 if (f != NULL) {
1025 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1026 return res;
1027 }
1028 if (descr != NULL) {
1029 Py_INCREF(descr);
1030 return descr;
1031 }
1032
1033 /* Give up */
1034 PyErr_Format(PyExc_AttributeError,
1035 "type object '%.50s' has no attribute '%.400s'",
1036 type->tp_name, PyString_AS_STRING(name));
1037 return NULL;
1038}
1039
1040static int
1041type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1042{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001043 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1044 PyErr_Format(
1045 PyExc_TypeError,
1046 "can't set attributes of built-in/extension type '%s'",
1047 type->tp_name);
1048 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001049 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001050 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1051 return -1;
1052 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001053}
1054
1055static void
1056type_dealloc(PyTypeObject *type)
1057{
1058 etype *et;
1059
1060 /* Assert this is a heap-allocated type object */
1061 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001062 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001063 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001064 et = (etype *)type;
1065 Py_XDECREF(type->tp_base);
1066 Py_XDECREF(type->tp_dict);
1067 Py_XDECREF(type->tp_bases);
1068 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001069 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001070 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001071 Py_XDECREF(et->name);
1072 Py_XDECREF(et->slots);
1073 type->ob_type->tp_free((PyObject *)type);
1074}
1075
Guido van Rossum1c450732001-10-08 15:18:27 +00001076static PyObject *
1077type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1078{
1079 PyObject *list, *raw, *ref;
1080 int i, n;
1081
1082 list = PyList_New(0);
1083 if (list == NULL)
1084 return NULL;
1085 raw = type->tp_subclasses;
1086 if (raw == NULL)
1087 return list;
1088 assert(PyList_Check(raw));
1089 n = PyList_GET_SIZE(raw);
1090 for (i = 0; i < n; i++) {
1091 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001092 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001093 ref = PyWeakref_GET_OBJECT(ref);
1094 if (ref != Py_None) {
1095 if (PyList_Append(list, ref) < 0) {
1096 Py_DECREF(list);
1097 return NULL;
1098 }
1099 }
1100 }
1101 return list;
1102}
1103
Tim Peters6d6c1a32001-08-02 04:15:00 +00001104static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001105 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001107 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1108 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109 {0}
1110};
1111
1112static char type_doc[] =
1113"type(object) -> the object's type\n"
1114"type(name, bases, dict) -> a new type";
1115
Guido van Rossum048eb752001-10-02 21:24:57 +00001116static int
1117type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1118{
1119 etype *et;
1120 int err;
1121
1122 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1123 return 0;
1124
1125 et = (etype *)type;
1126
1127#define VISIT(SLOT) \
1128 if (SLOT) { \
1129 err = visit((PyObject *)(SLOT), arg); \
1130 if (err) \
1131 return err; \
1132 }
1133
1134 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001135 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001136 VISIT(type->tp_mro);
1137 VISIT(type->tp_bases);
1138 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001139 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001140 VISIT(et->slots);
1141
1142#undef VISIT
1143
1144 return 0;
1145}
1146
1147static int
1148type_clear(PyTypeObject *type)
1149{
1150 etype *et;
1151 PyObject *tmp;
1152
1153 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1154 return 0;
1155
1156 et = (etype *)type;
1157
1158#define CLEAR(SLOT) \
1159 if (SLOT) { \
1160 tmp = (PyObject *)(SLOT); \
1161 SLOT = NULL; \
1162 Py_DECREF(tmp); \
1163 }
1164
1165 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001166 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001167 CLEAR(type->tp_mro);
1168 CLEAR(type->tp_bases);
1169 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001170 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001171 CLEAR(et->slots);
1172
Tim Peters2f93e282001-10-04 05:27:00 +00001173 if (type->tp_doc != NULL) {
1174 PyObject_FREE(type->tp_doc);
1175 type->tp_doc = NULL;
1176 }
1177
Guido van Rossum048eb752001-10-02 21:24:57 +00001178#undef CLEAR
1179
1180 return 0;
1181}
1182
1183static int
1184type_is_gc(PyTypeObject *type)
1185{
1186 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1187}
1188
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001189PyTypeObject PyType_Type = {
1190 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191 0, /* ob_size */
1192 "type", /* tp_name */
1193 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001194 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001195 (destructor)type_dealloc, /* tp_dealloc */
1196 0, /* tp_print */
1197 0, /* tp_getattr */
1198 0, /* tp_setattr */
1199 type_compare, /* tp_compare */
1200 (reprfunc)type_repr, /* tp_repr */
1201 0, /* tp_as_number */
1202 0, /* tp_as_sequence */
1203 0, /* tp_as_mapping */
1204 (hashfunc)_Py_HashPointer, /* tp_hash */
1205 (ternaryfunc)type_call, /* tp_call */
1206 0, /* tp_str */
1207 (getattrofunc)type_getattro, /* tp_getattro */
1208 (setattrofunc)type_setattro, /* tp_setattro */
1209 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001210 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1211 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001212 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001213 (traverseproc)type_traverse, /* tp_traverse */
1214 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001215 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001216 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001217 0, /* tp_iter */
1218 0, /* tp_iternext */
1219 type_methods, /* tp_methods */
1220 type_members, /* tp_members */
1221 type_getsets, /* tp_getset */
1222 0, /* tp_base */
1223 0, /* tp_dict */
1224 0, /* tp_descr_get */
1225 0, /* tp_descr_set */
1226 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1227 0, /* tp_init */
1228 0, /* tp_alloc */
1229 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001230 _PyObject_GC_Del, /* tp_free */
1231 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001232};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001233
1234
1235/* The base type of all types (eventually)... except itself. */
1236
1237static int
1238object_init(PyObject *self, PyObject *args, PyObject *kwds)
1239{
1240 return 0;
1241}
1242
1243static void
1244object_dealloc(PyObject *self)
1245{
1246 self->ob_type->tp_free(self);
1247}
1248
Guido van Rossum8e248182001-08-12 05:17:56 +00001249static PyObject *
1250object_repr(PyObject *self)
1251{
Guido van Rossum76e69632001-08-16 18:52:43 +00001252 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001253 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001254
Guido van Rossum76e69632001-08-16 18:52:43 +00001255 type = self->ob_type;
1256 mod = type_module(type, NULL);
1257 if (mod == NULL)
1258 PyErr_Clear();
1259 else if (!PyString_Check(mod)) {
1260 Py_DECREF(mod);
1261 mod = NULL;
1262 }
1263 name = type_name(type, NULL);
1264 if (name == NULL)
1265 return NULL;
1266 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001267 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001268 PyString_AS_STRING(mod),
1269 PyString_AS_STRING(name),
1270 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001271 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001272 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001273 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001274 Py_XDECREF(mod);
1275 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001276 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001277}
1278
Guido van Rossumb8f63662001-08-15 23:57:02 +00001279static PyObject *
1280object_str(PyObject *self)
1281{
1282 unaryfunc f;
1283
1284 f = self->ob_type->tp_repr;
1285 if (f == NULL)
1286 f = object_repr;
1287 return f(self);
1288}
1289
Guido van Rossum8e248182001-08-12 05:17:56 +00001290static long
1291object_hash(PyObject *self)
1292{
1293 return _Py_HashPointer(self);
1294}
Guido van Rossum8e248182001-08-12 05:17:56 +00001295
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001296static PyObject *
1297object_get_class(PyObject *self, void *closure)
1298{
1299 Py_INCREF(self->ob_type);
1300 return (PyObject *)(self->ob_type);
1301}
1302
1303static int
1304equiv_structs(PyTypeObject *a, PyTypeObject *b)
1305{
1306 return a == b ||
1307 (a != NULL &&
1308 b != NULL &&
1309 a->tp_basicsize == b->tp_basicsize &&
1310 a->tp_itemsize == b->tp_itemsize &&
1311 a->tp_dictoffset == b->tp_dictoffset &&
1312 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1313 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1314 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1315}
1316
1317static int
1318same_slots_added(PyTypeObject *a, PyTypeObject *b)
1319{
1320 PyTypeObject *base = a->tp_base;
1321 int size;
1322
1323 if (base != b->tp_base)
1324 return 0;
1325 if (equiv_structs(a, base) && equiv_structs(b, base))
1326 return 1;
1327 size = base->tp_basicsize;
1328 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1329 size += sizeof(PyObject *);
1330 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1331 size += sizeof(PyObject *);
1332 return size == a->tp_basicsize && size == b->tp_basicsize;
1333}
1334
1335static int
1336object_set_class(PyObject *self, PyObject *value, void *closure)
1337{
1338 PyTypeObject *old = self->ob_type;
1339 PyTypeObject *new, *newbase, *oldbase;
1340
1341 if (!PyType_Check(value)) {
1342 PyErr_Format(PyExc_TypeError,
1343 "__class__ must be set to new-style class, not '%s' object",
1344 value->ob_type->tp_name);
1345 return -1;
1346 }
1347 new = (PyTypeObject *)value;
1348 newbase = new;
1349 oldbase = old;
1350 while (equiv_structs(newbase, newbase->tp_base))
1351 newbase = newbase->tp_base;
1352 while (equiv_structs(oldbase, oldbase->tp_base))
1353 oldbase = oldbase->tp_base;
1354 if (newbase != oldbase &&
1355 (newbase->tp_base != oldbase->tp_base ||
1356 !same_slots_added(newbase, oldbase))) {
1357 PyErr_Format(PyExc_TypeError,
1358 "__class__ assignment: "
1359 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001360 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001361 old->tp_name);
1362 return -1;
1363 }
1364 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1365 Py_INCREF(new);
1366 }
1367 self->ob_type = new;
1368 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1369 Py_DECREF(old);
1370 }
1371 return 0;
1372}
1373
1374static PyGetSetDef object_getsets[] = {
1375 {"__class__", object_get_class, object_set_class,
1376 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001377 {0}
1378};
1379
Guido van Rossum3926a632001-09-25 16:25:58 +00001380static PyObject *
1381object_reduce(PyObject *self, PyObject *args)
1382{
1383 /* Call copy_reg._reduce(self) */
1384 static PyObject *copy_reg_str;
1385 PyObject *copy_reg, *res;
1386
1387 if (!copy_reg_str) {
1388 copy_reg_str = PyString_InternFromString("copy_reg");
1389 if (copy_reg_str == NULL)
1390 return NULL;
1391 }
1392 copy_reg = PyImport_Import(copy_reg_str);
1393 if (!copy_reg)
1394 return NULL;
1395 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1396 Py_DECREF(copy_reg);
1397 return res;
1398}
1399
1400static PyMethodDef object_methods[] = {
1401 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1402 {0}
1403};
1404
Tim Peters6d6c1a32001-08-02 04:15:00 +00001405PyTypeObject PyBaseObject_Type = {
1406 PyObject_HEAD_INIT(&PyType_Type)
1407 0, /* ob_size */
1408 "object", /* tp_name */
1409 sizeof(PyObject), /* tp_basicsize */
1410 0, /* tp_itemsize */
1411 (destructor)object_dealloc, /* tp_dealloc */
1412 0, /* tp_print */
1413 0, /* tp_getattr */
1414 0, /* tp_setattr */
1415 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001416 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001417 0, /* tp_as_number */
1418 0, /* tp_as_sequence */
1419 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001420 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001422 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001423 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001424 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425 0, /* tp_as_buffer */
1426 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1427 "The most base type", /* tp_doc */
1428 0, /* tp_traverse */
1429 0, /* tp_clear */
1430 0, /* tp_richcompare */
1431 0, /* tp_weaklistoffset */
1432 0, /* tp_iter */
1433 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001434 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001435 0, /* tp_members */
1436 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001437 0, /* tp_base */
1438 0, /* tp_dict */
1439 0, /* tp_descr_get */
1440 0, /* tp_descr_set */
1441 0, /* tp_dictoffset */
1442 object_init, /* tp_init */
1443 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001444 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001445 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001446};
1447
1448
1449/* Initialize the __dict__ in a type object */
1450
1451static int
1452add_methods(PyTypeObject *type, PyMethodDef *meth)
1453{
Guido van Rossum687ae002001-10-15 22:03:32 +00001454 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001455
1456 for (; meth->ml_name != NULL; meth++) {
1457 PyObject *descr;
1458 if (PyDict_GetItemString(dict, meth->ml_name))
1459 continue;
1460 descr = PyDescr_NewMethod(type, meth);
1461 if (descr == NULL)
1462 return -1;
1463 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1464 return -1;
1465 Py_DECREF(descr);
1466 }
1467 return 0;
1468}
1469
1470static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001471add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001472{
Guido van Rossum687ae002001-10-15 22:03:32 +00001473 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001474
1475 for (; memb->name != NULL; memb++) {
1476 PyObject *descr;
1477 if (PyDict_GetItemString(dict, memb->name))
1478 continue;
1479 descr = PyDescr_NewMember(type, memb);
1480 if (descr == NULL)
1481 return -1;
1482 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1483 return -1;
1484 Py_DECREF(descr);
1485 }
1486 return 0;
1487}
1488
1489static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001490add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001491{
Guido van Rossum687ae002001-10-15 22:03:32 +00001492 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001493
1494 for (; gsp->name != NULL; gsp++) {
1495 PyObject *descr;
1496 if (PyDict_GetItemString(dict, gsp->name))
1497 continue;
1498 descr = PyDescr_NewGetSet(type, gsp);
1499
1500 if (descr == NULL)
1501 return -1;
1502 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1503 return -1;
1504 Py_DECREF(descr);
1505 }
1506 return 0;
1507}
1508
Guido van Rossum13d52f02001-08-10 21:24:08 +00001509static void
1510inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001511{
1512 int oldsize, newsize;
1513
Guido van Rossum13d52f02001-08-10 21:24:08 +00001514 /* Special flag magic */
1515 if (!type->tp_as_buffer && base->tp_as_buffer) {
1516 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1517 type->tp_flags |=
1518 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1519 }
1520 if (!type->tp_as_sequence && base->tp_as_sequence) {
1521 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1522 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1523 }
1524 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1525 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1526 if ((!type->tp_as_number && base->tp_as_number) ||
1527 (!type->tp_as_sequence && base->tp_as_sequence)) {
1528 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1529 if (!type->tp_as_number && !type->tp_as_sequence) {
1530 type->tp_flags |= base->tp_flags &
1531 Py_TPFLAGS_HAVE_INPLACEOPS;
1532 }
1533 }
1534 /* Wow */
1535 }
1536 if (!type->tp_as_number && base->tp_as_number) {
1537 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1538 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1539 }
1540
1541 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001542 oldsize = base->tp_basicsize;
1543 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1544 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1545 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001546 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1547 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001548 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001549 if (type->tp_traverse == NULL)
1550 type->tp_traverse = base->tp_traverse;
1551 if (type->tp_clear == NULL)
1552 type->tp_clear = base->tp_clear;
1553 }
1554 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1555 if (base != &PyBaseObject_Type ||
1556 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1557 if (type->tp_new == NULL)
1558 type->tp_new = base->tp_new;
1559 }
1560 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001561 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001562
1563 /* Copy other non-function slots */
1564
1565#undef COPYVAL
1566#define COPYVAL(SLOT) \
1567 if (type->SLOT == 0) type->SLOT = base->SLOT
1568
1569 COPYVAL(tp_itemsize);
1570 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1571 COPYVAL(tp_weaklistoffset);
1572 }
1573 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1574 COPYVAL(tp_dictoffset);
1575 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001576}
1577
1578static void
1579inherit_slots(PyTypeObject *type, PyTypeObject *base)
1580{
1581 PyTypeObject *basebase;
1582
1583#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001584#undef COPYSLOT
1585#undef COPYNUM
1586#undef COPYSEQ
1587#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001588#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001589
1590#define SLOTDEFINED(SLOT) \
1591 (base->SLOT != 0 && \
1592 (basebase == NULL || base->SLOT != basebase->SLOT))
1593
Tim Peters6d6c1a32001-08-02 04:15:00 +00001594#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001595 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001596
1597#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1598#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1599#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001600#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001601
Guido van Rossum13d52f02001-08-10 21:24:08 +00001602 /* This won't inherit indirect slots (from tp_as_number etc.)
1603 if type doesn't provide the space. */
1604
1605 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1606 basebase = base->tp_base;
1607 if (basebase->tp_as_number == NULL)
1608 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001609 COPYNUM(nb_add);
1610 COPYNUM(nb_subtract);
1611 COPYNUM(nb_multiply);
1612 COPYNUM(nb_divide);
1613 COPYNUM(nb_remainder);
1614 COPYNUM(nb_divmod);
1615 COPYNUM(nb_power);
1616 COPYNUM(nb_negative);
1617 COPYNUM(nb_positive);
1618 COPYNUM(nb_absolute);
1619 COPYNUM(nb_nonzero);
1620 COPYNUM(nb_invert);
1621 COPYNUM(nb_lshift);
1622 COPYNUM(nb_rshift);
1623 COPYNUM(nb_and);
1624 COPYNUM(nb_xor);
1625 COPYNUM(nb_or);
1626 COPYNUM(nb_coerce);
1627 COPYNUM(nb_int);
1628 COPYNUM(nb_long);
1629 COPYNUM(nb_float);
1630 COPYNUM(nb_oct);
1631 COPYNUM(nb_hex);
1632 COPYNUM(nb_inplace_add);
1633 COPYNUM(nb_inplace_subtract);
1634 COPYNUM(nb_inplace_multiply);
1635 COPYNUM(nb_inplace_divide);
1636 COPYNUM(nb_inplace_remainder);
1637 COPYNUM(nb_inplace_power);
1638 COPYNUM(nb_inplace_lshift);
1639 COPYNUM(nb_inplace_rshift);
1640 COPYNUM(nb_inplace_and);
1641 COPYNUM(nb_inplace_xor);
1642 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001643 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1644 COPYNUM(nb_true_divide);
1645 COPYNUM(nb_floor_divide);
1646 COPYNUM(nb_inplace_true_divide);
1647 COPYNUM(nb_inplace_floor_divide);
1648 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001649 }
1650
Guido van Rossum13d52f02001-08-10 21:24:08 +00001651 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1652 basebase = base->tp_base;
1653 if (basebase->tp_as_sequence == NULL)
1654 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001655 COPYSEQ(sq_length);
1656 COPYSEQ(sq_concat);
1657 COPYSEQ(sq_repeat);
1658 COPYSEQ(sq_item);
1659 COPYSEQ(sq_slice);
1660 COPYSEQ(sq_ass_item);
1661 COPYSEQ(sq_ass_slice);
1662 COPYSEQ(sq_contains);
1663 COPYSEQ(sq_inplace_concat);
1664 COPYSEQ(sq_inplace_repeat);
1665 }
1666
Guido van Rossum13d52f02001-08-10 21:24:08 +00001667 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1668 basebase = base->tp_base;
1669 if (basebase->tp_as_mapping == NULL)
1670 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001671 COPYMAP(mp_length);
1672 COPYMAP(mp_subscript);
1673 COPYMAP(mp_ass_subscript);
1674 }
1675
Tim Petersfc57ccb2001-10-12 02:38:24 +00001676 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1677 basebase = base->tp_base;
1678 if (basebase->tp_as_buffer == NULL)
1679 basebase = NULL;
1680 COPYBUF(bf_getreadbuffer);
1681 COPYBUF(bf_getwritebuffer);
1682 COPYBUF(bf_getsegcount);
1683 COPYBUF(bf_getcharbuffer);
1684 }
1685
Guido van Rossum13d52f02001-08-10 21:24:08 +00001686 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001687
Tim Peters6d6c1a32001-08-02 04:15:00 +00001688 COPYSLOT(tp_dealloc);
1689 COPYSLOT(tp_print);
1690 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1691 type->tp_getattr = base->tp_getattr;
1692 type->tp_getattro = base->tp_getattro;
1693 }
1694 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1695 type->tp_setattr = base->tp_setattr;
1696 type->tp_setattro = base->tp_setattro;
1697 }
1698 /* tp_compare see tp_richcompare */
1699 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001700 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001701 COPYSLOT(tp_call);
1702 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001703 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001704 if (type->tp_compare == NULL &&
1705 type->tp_richcompare == NULL &&
1706 type->tp_hash == NULL)
1707 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001708 type->tp_compare = base->tp_compare;
1709 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001710 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001711 }
1712 }
1713 else {
1714 COPYSLOT(tp_compare);
1715 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001716 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1717 COPYSLOT(tp_iter);
1718 COPYSLOT(tp_iternext);
1719 }
1720 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1721 COPYSLOT(tp_descr_get);
1722 COPYSLOT(tp_descr_set);
1723 COPYSLOT(tp_dictoffset);
1724 COPYSLOT(tp_init);
1725 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726 COPYSLOT(tp_free);
1727 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001728}
1729
Guido van Rossum13d52f02001-08-10 21:24:08 +00001730staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00001731staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001732
Tim Peters6d6c1a32001-08-02 04:15:00 +00001733int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001734PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001735{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001736 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001737 PyTypeObject *base;
1738 int i, n;
1739
Guido van Rossumd614f972001-08-10 17:39:49 +00001740 if (type->tp_flags & Py_TPFLAGS_READY) {
1741 assert(type->tp_dict != NULL);
1742 return 0;
1743 }
1744 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00001745
1746 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001747
1748 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1749 base = type->tp_base;
1750 if (base == NULL && type != &PyBaseObject_Type)
1751 base = type->tp_base = &PyBaseObject_Type;
1752
1753 /* Initialize tp_bases */
1754 bases = type->tp_bases;
1755 if (bases == NULL) {
1756 if (base == NULL)
1757 bases = PyTuple_New(0);
1758 else
1759 bases = Py_BuildValue("(O)", base);
1760 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001761 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001762 type->tp_bases = bases;
1763 }
1764
1765 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001766 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001767 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001768 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001769 }
1770
Guido van Rossum687ae002001-10-15 22:03:32 +00001771 /* Initialize tp_dict */
1772 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001773 if (dict == NULL) {
1774 dict = PyDict_New();
1775 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001776 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00001777 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001778 }
1779
Guido van Rossum687ae002001-10-15 22:03:32 +00001780 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001781 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001782 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001783 if (type->tp_methods != NULL) {
1784 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001785 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001786 }
1787 if (type->tp_members != NULL) {
1788 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001789 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001790 }
1791 if (type->tp_getset != NULL) {
1792 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001793 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001794 }
1795
Tim Peters6d6c1a32001-08-02 04:15:00 +00001796 /* Calculate method resolution order */
1797 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001798 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001799 }
1800
Guido van Rossum13d52f02001-08-10 21:24:08 +00001801 /* Inherit special flags from dominant base */
1802 if (type->tp_base != NULL)
1803 inherit_special(type, type->tp_base);
1804
Tim Peters6d6c1a32001-08-02 04:15:00 +00001805 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001806 bases = type->tp_mro;
1807 assert(bases != NULL);
1808 assert(PyTuple_Check(bases));
1809 n = PyTuple_GET_SIZE(bases);
1810 for (i = 1; i < n; i++) {
1811 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1812 assert(PyType_Check(base));
1813 inherit_slots(type, base);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001814 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001815
Guido van Rossum13d52f02001-08-10 21:24:08 +00001816 /* Some more special stuff */
1817 base = type->tp_base;
1818 if (base != NULL) {
1819 if (type->tp_as_number == NULL)
1820 type->tp_as_number = base->tp_as_number;
1821 if (type->tp_as_sequence == NULL)
1822 type->tp_as_sequence = base->tp_as_sequence;
1823 if (type->tp_as_mapping == NULL)
1824 type->tp_as_mapping = base->tp_as_mapping;
1825 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001826
Guido van Rossum1c450732001-10-08 15:18:27 +00001827 /* Link into each base class's list of subclasses */
1828 bases = type->tp_bases;
1829 n = PyTuple_GET_SIZE(bases);
1830 for (i = 0; i < n; i++) {
1831 base = (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
1832 if (add_subclass((PyTypeObject *)base, type) < 0)
1833 goto error;
1834 }
1835
Guido van Rossum13d52f02001-08-10 21:24:08 +00001836 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001837 assert(type->tp_dict != NULL);
1838 type->tp_flags =
1839 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001840 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001841
1842 error:
1843 type->tp_flags &= ~Py_TPFLAGS_READYING;
1844 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001845}
1846
Guido van Rossum1c450732001-10-08 15:18:27 +00001847static int
1848add_subclass(PyTypeObject *base, PyTypeObject *type)
1849{
1850 int i;
1851 PyObject *list, *ref, *new;
1852
1853 list = base->tp_subclasses;
1854 if (list == NULL) {
1855 base->tp_subclasses = list = PyList_New(0);
1856 if (list == NULL)
1857 return -1;
1858 }
1859 assert(PyList_Check(list));
1860 new = PyWeakref_NewRef((PyObject *)type, NULL);
1861 i = PyList_GET_SIZE(list);
1862 while (--i >= 0) {
1863 ref = PyList_GET_ITEM(list, i);
1864 assert(PyWeakref_CheckRef(ref));
1865 if (PyWeakref_GET_OBJECT(ref) == Py_None)
1866 return PyList_SetItem(list, i, new);
1867 }
1868 i = PyList_Append(list, new);
1869 Py_DECREF(new);
1870 return i;
1871}
1872
Tim Peters6d6c1a32001-08-02 04:15:00 +00001873
1874/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1875
1876/* There's a wrapper *function* for each distinct function typedef used
1877 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1878 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1879 Most tables have only one entry; the tables for binary operators have two
1880 entries, one regular and one with reversed arguments. */
1881
1882static PyObject *
1883wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1884{
1885 inquiry func = (inquiry)wrapped;
1886 int res;
1887
1888 if (!PyArg_ParseTuple(args, ""))
1889 return NULL;
1890 res = (*func)(self);
1891 if (res == -1 && PyErr_Occurred())
1892 return NULL;
1893 return PyInt_FromLong((long)res);
1894}
1895
Tim Peters6d6c1a32001-08-02 04:15:00 +00001896static PyObject *
1897wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1898{
1899 binaryfunc func = (binaryfunc)wrapped;
1900 PyObject *other;
1901
1902 if (!PyArg_ParseTuple(args, "O", &other))
1903 return NULL;
1904 return (*func)(self, other);
1905}
1906
1907static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001908wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
1909{
1910 binaryfunc func = (binaryfunc)wrapped;
1911 PyObject *other;
1912
1913 if (!PyArg_ParseTuple(args, "O", &other))
1914 return NULL;
1915 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001916 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001917 Py_INCREF(Py_NotImplemented);
1918 return Py_NotImplemented;
1919 }
1920 return (*func)(self, other);
1921}
1922
1923static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1925{
1926 binaryfunc func = (binaryfunc)wrapped;
1927 PyObject *other;
1928
1929 if (!PyArg_ParseTuple(args, "O", &other))
1930 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001931 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001932 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001933 Py_INCREF(Py_NotImplemented);
1934 return Py_NotImplemented;
1935 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936 return (*func)(other, self);
1937}
1938
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00001939static PyObject *
1940wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
1941{
1942 coercion func = (coercion)wrapped;
1943 PyObject *other, *res;
1944 int ok;
1945
1946 if (!PyArg_ParseTuple(args, "O", &other))
1947 return NULL;
1948 ok = func(&self, &other);
1949 if (ok < 0)
1950 return NULL;
1951 if (ok > 0) {
1952 Py_INCREF(Py_NotImplemented);
1953 return Py_NotImplemented;
1954 }
1955 res = PyTuple_New(2);
1956 if (res == NULL) {
1957 Py_DECREF(self);
1958 Py_DECREF(other);
1959 return NULL;
1960 }
1961 PyTuple_SET_ITEM(res, 0, self);
1962 PyTuple_SET_ITEM(res, 1, other);
1963 return res;
1964}
1965
Tim Peters6d6c1a32001-08-02 04:15:00 +00001966static PyObject *
1967wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1968{
1969 ternaryfunc func = (ternaryfunc)wrapped;
1970 PyObject *other;
1971 PyObject *third = Py_None;
1972
1973 /* Note: This wrapper only works for __pow__() */
1974
1975 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1976 return NULL;
1977 return (*func)(self, other, third);
1978}
1979
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00001980static PyObject *
1981wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1982{
1983 ternaryfunc func = (ternaryfunc)wrapped;
1984 PyObject *other;
1985 PyObject *third = Py_None;
1986
1987 /* Note: This wrapper only works for __pow__() */
1988
1989 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1990 return NULL;
1991 return (*func)(other, self, third);
1992}
1993
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994static PyObject *
1995wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1996{
1997 unaryfunc func = (unaryfunc)wrapped;
1998
1999 if (!PyArg_ParseTuple(args, ""))
2000 return NULL;
2001 return (*func)(self);
2002}
2003
Tim Peters6d6c1a32001-08-02 04:15:00 +00002004static PyObject *
2005wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2006{
2007 intargfunc func = (intargfunc)wrapped;
2008 int i;
2009
2010 if (!PyArg_ParseTuple(args, "i", &i))
2011 return NULL;
2012 return (*func)(self, i);
2013}
2014
Guido van Rossum5d815f32001-08-17 21:57:47 +00002015static int
2016getindex(PyObject *self, PyObject *arg)
2017{
2018 int i;
2019
2020 i = PyInt_AsLong(arg);
2021 if (i == -1 && PyErr_Occurred())
2022 return -1;
2023 if (i < 0) {
2024 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2025 if (sq && sq->sq_length) {
2026 int n = (*sq->sq_length)(self);
2027 if (n < 0)
2028 return -1;
2029 i += n;
2030 }
2031 }
2032 return i;
2033}
2034
2035static PyObject *
2036wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2037{
2038 intargfunc func = (intargfunc)wrapped;
2039 PyObject *arg;
2040 int i;
2041
Guido van Rossumf4593e02001-10-03 12:09:30 +00002042 if (PyTuple_GET_SIZE(args) == 1) {
2043 arg = PyTuple_GET_ITEM(args, 0);
2044 i = getindex(self, arg);
2045 if (i == -1 && PyErr_Occurred())
2046 return NULL;
2047 return (*func)(self, i);
2048 }
2049 PyArg_ParseTuple(args, "O", &arg);
2050 assert(PyErr_Occurred());
2051 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002052}
2053
Tim Peters6d6c1a32001-08-02 04:15:00 +00002054static PyObject *
2055wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2056{
2057 intintargfunc func = (intintargfunc)wrapped;
2058 int i, j;
2059
2060 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2061 return NULL;
2062 return (*func)(self, i, j);
2063}
2064
Tim Peters6d6c1a32001-08-02 04:15:00 +00002065static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002066wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002067{
2068 intobjargproc func = (intobjargproc)wrapped;
2069 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002070 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002071
Guido van Rossum5d815f32001-08-17 21:57:47 +00002072 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2073 return NULL;
2074 i = getindex(self, arg);
2075 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002076 return NULL;
2077 res = (*func)(self, i, value);
2078 if (res == -1 && PyErr_Occurred())
2079 return NULL;
2080 Py_INCREF(Py_None);
2081 return Py_None;
2082}
2083
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002084static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002085wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002086{
2087 intobjargproc func = (intobjargproc)wrapped;
2088 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002089 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002090
Guido van Rossum5d815f32001-08-17 21:57:47 +00002091 if (!PyArg_ParseTuple(args, "O", &arg))
2092 return NULL;
2093 i = getindex(self, arg);
2094 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002095 return NULL;
2096 res = (*func)(self, i, NULL);
2097 if (res == -1 && PyErr_Occurred())
2098 return NULL;
2099 Py_INCREF(Py_None);
2100 return Py_None;
2101}
2102
Tim Peters6d6c1a32001-08-02 04:15:00 +00002103static PyObject *
2104wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2105{
2106 intintobjargproc func = (intintobjargproc)wrapped;
2107 int i, j, res;
2108 PyObject *value;
2109
2110 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2111 return NULL;
2112 res = (*func)(self, i, j, value);
2113 if (res == -1 && PyErr_Occurred())
2114 return NULL;
2115 Py_INCREF(Py_None);
2116 return Py_None;
2117}
2118
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002119static PyObject *
2120wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2121{
2122 intintobjargproc func = (intintobjargproc)wrapped;
2123 int i, j, res;
2124
2125 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2126 return NULL;
2127 res = (*func)(self, i, j, NULL);
2128 if (res == -1 && PyErr_Occurred())
2129 return NULL;
2130 Py_INCREF(Py_None);
2131 return Py_None;
2132}
2133
Tim Peters6d6c1a32001-08-02 04:15:00 +00002134/* XXX objobjproc is a misnomer; should be objargpred */
2135static PyObject *
2136wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2137{
2138 objobjproc func = (objobjproc)wrapped;
2139 int res;
2140 PyObject *value;
2141
2142 if (!PyArg_ParseTuple(args, "O", &value))
2143 return NULL;
2144 res = (*func)(self, value);
2145 if (res == -1 && PyErr_Occurred())
2146 return NULL;
2147 return PyInt_FromLong((long)res);
2148}
2149
Tim Peters6d6c1a32001-08-02 04:15:00 +00002150static PyObject *
2151wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2152{
2153 objobjargproc func = (objobjargproc)wrapped;
2154 int res;
2155 PyObject *key, *value;
2156
2157 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2158 return NULL;
2159 res = (*func)(self, key, value);
2160 if (res == -1 && PyErr_Occurred())
2161 return NULL;
2162 Py_INCREF(Py_None);
2163 return Py_None;
2164}
2165
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002166static PyObject *
2167wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2168{
2169 objobjargproc func = (objobjargproc)wrapped;
2170 int res;
2171 PyObject *key;
2172
2173 if (!PyArg_ParseTuple(args, "O", &key))
2174 return NULL;
2175 res = (*func)(self, key, NULL);
2176 if (res == -1 && PyErr_Occurred())
2177 return NULL;
2178 Py_INCREF(Py_None);
2179 return Py_None;
2180}
2181
Tim Peters6d6c1a32001-08-02 04:15:00 +00002182static PyObject *
2183wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2184{
2185 cmpfunc func = (cmpfunc)wrapped;
2186 int res;
2187 PyObject *other;
2188
2189 if (!PyArg_ParseTuple(args, "O", &other))
2190 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002191 if (other->ob_type->tp_compare != func &&
2192 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002193 PyErr_Format(
2194 PyExc_TypeError,
2195 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2196 self->ob_type->tp_name,
2197 self->ob_type->tp_name,
2198 other->ob_type->tp_name);
2199 return NULL;
2200 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002201 res = (*func)(self, other);
2202 if (PyErr_Occurred())
2203 return NULL;
2204 return PyInt_FromLong((long)res);
2205}
2206
Tim Peters6d6c1a32001-08-02 04:15:00 +00002207static PyObject *
2208wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2209{
2210 setattrofunc func = (setattrofunc)wrapped;
2211 int res;
2212 PyObject *name, *value;
2213
2214 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2215 return NULL;
2216 res = (*func)(self, name, value);
2217 if (res < 0)
2218 return NULL;
2219 Py_INCREF(Py_None);
2220 return Py_None;
2221}
2222
2223static PyObject *
2224wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2225{
2226 setattrofunc func = (setattrofunc)wrapped;
2227 int res;
2228 PyObject *name;
2229
2230 if (!PyArg_ParseTuple(args, "O", &name))
2231 return NULL;
2232 res = (*func)(self, name, NULL);
2233 if (res < 0)
2234 return NULL;
2235 Py_INCREF(Py_None);
2236 return Py_None;
2237}
2238
Tim Peters6d6c1a32001-08-02 04:15:00 +00002239static PyObject *
2240wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2241{
2242 hashfunc func = (hashfunc)wrapped;
2243 long res;
2244
2245 if (!PyArg_ParseTuple(args, ""))
2246 return NULL;
2247 res = (*func)(self);
2248 if (res == -1 && PyErr_Occurred())
2249 return NULL;
2250 return PyInt_FromLong(res);
2251}
2252
Tim Peters6d6c1a32001-08-02 04:15:00 +00002253static PyObject *
2254wrap_call(PyObject *self, PyObject *args, void *wrapped)
2255{
2256 ternaryfunc func = (ternaryfunc)wrapped;
2257
2258 /* XXX What about keyword arguments? */
2259 return (*func)(self, args, NULL);
2260}
2261
Tim Peters6d6c1a32001-08-02 04:15:00 +00002262static PyObject *
2263wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2264{
2265 richcmpfunc func = (richcmpfunc)wrapped;
2266 PyObject *other;
2267
2268 if (!PyArg_ParseTuple(args, "O", &other))
2269 return NULL;
2270 return (*func)(self, other, op);
2271}
2272
2273#undef RICHCMP_WRAPPER
2274#define RICHCMP_WRAPPER(NAME, OP) \
2275static PyObject * \
2276richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2277{ \
2278 return wrap_richcmpfunc(self, args, wrapped, OP); \
2279}
2280
Jack Jansen8e938b42001-08-08 15:29:49 +00002281RICHCMP_WRAPPER(lt, Py_LT)
2282RICHCMP_WRAPPER(le, Py_LE)
2283RICHCMP_WRAPPER(eq, Py_EQ)
2284RICHCMP_WRAPPER(ne, Py_NE)
2285RICHCMP_WRAPPER(gt, Py_GT)
2286RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002287
Tim Peters6d6c1a32001-08-02 04:15:00 +00002288static PyObject *
2289wrap_next(PyObject *self, PyObject *args, void *wrapped)
2290{
2291 unaryfunc func = (unaryfunc)wrapped;
2292 PyObject *res;
2293
2294 if (!PyArg_ParseTuple(args, ""))
2295 return NULL;
2296 res = (*func)(self);
2297 if (res == NULL && !PyErr_Occurred())
2298 PyErr_SetNone(PyExc_StopIteration);
2299 return res;
2300}
2301
Tim Peters6d6c1a32001-08-02 04:15:00 +00002302static PyObject *
2303wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2304{
2305 descrgetfunc func = (descrgetfunc)wrapped;
2306 PyObject *obj;
2307 PyObject *type = NULL;
2308
2309 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2310 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311 return (*func)(self, obj, type);
2312}
2313
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002315wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002316{
2317 descrsetfunc func = (descrsetfunc)wrapped;
2318 PyObject *obj, *value;
2319 int ret;
2320
2321 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2322 return NULL;
2323 ret = (*func)(self, obj, value);
2324 if (ret < 0)
2325 return NULL;
2326 Py_INCREF(Py_None);
2327 return Py_None;
2328}
2329
Tim Peters6d6c1a32001-08-02 04:15:00 +00002330static PyObject *
2331wrap_init(PyObject *self, PyObject *args, void *wrapped)
2332{
2333 initproc func = (initproc)wrapped;
2334
2335 /* XXX What about keyword arguments? */
2336 if (func(self, args, NULL) < 0)
2337 return NULL;
2338 Py_INCREF(Py_None);
2339 return Py_None;
2340}
2341
Tim Peters6d6c1a32001-08-02 04:15:00 +00002342static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002343tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002344{
Barry Warsaw60f01882001-08-22 19:24:42 +00002345 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002346 PyObject *arg0, *res;
2347
2348 if (self == NULL || !PyType_Check(self))
2349 Py_FatalError("__new__() called with non-type 'self'");
2350 type = (PyTypeObject *)self;
2351 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002352 PyErr_Format(PyExc_TypeError,
2353 "%s.__new__(): not enough arguments",
2354 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002355 return NULL;
2356 }
2357 arg0 = PyTuple_GET_ITEM(args, 0);
2358 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002359 PyErr_Format(PyExc_TypeError,
2360 "%s.__new__(X): X is not a type object (%s)",
2361 type->tp_name,
2362 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002363 return NULL;
2364 }
2365 subtype = (PyTypeObject *)arg0;
2366 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002367 PyErr_Format(PyExc_TypeError,
2368 "%s.__new__(%s): %s is not a subtype of %s",
2369 type->tp_name,
2370 subtype->tp_name,
2371 subtype->tp_name,
2372 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002373 return NULL;
2374 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002375
2376 /* Check that the use doesn't do something silly and unsafe like
2377 object.__new__(dictionary). To do this, we check that the
2378 most derived base that's not a heap type is this type. */
2379 staticbase = subtype;
2380 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2381 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002382 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002383 PyErr_Format(PyExc_TypeError,
2384 "%s.__new__(%s) is not safe, use %s.__new__()",
2385 type->tp_name,
2386 subtype->tp_name,
2387 staticbase == NULL ? "?" : staticbase->tp_name);
2388 return NULL;
2389 }
2390
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002391 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2392 if (args == NULL)
2393 return NULL;
2394 res = type->tp_new(subtype, args, kwds);
2395 Py_DECREF(args);
2396 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002397}
2398
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002399static struct PyMethodDef tp_new_methoddef[] = {
2400 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2401 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002402 {0}
2403};
2404
2405static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002406add_tp_new_wrapper(PyTypeObject *type)
2407{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002408 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002409
Guido van Rossum687ae002001-10-15 22:03:32 +00002410 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002411 return 0;
2412 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002413 if (func == NULL)
2414 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002415 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002416}
2417
Guido van Rossumf040ede2001-08-07 16:40:56 +00002418/* Slot wrappers that call the corresponding __foo__ slot. See comments
2419 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002420
Guido van Rossumdc91b992001-08-08 22:26:22 +00002421#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002422static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002423FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002424{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002425 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002426 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002427}
2428
Guido van Rossumdc91b992001-08-08 22:26:22 +00002429#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002430static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002431FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002432{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002433 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002434 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002435}
2436
Guido van Rossumdc91b992001-08-08 22:26:22 +00002437
2438#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002439static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002440FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002441{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002442 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002443 int do_other = self->ob_type != other->ob_type && \
2444 other->ob_type->tp_as_number != NULL && \
2445 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002446 if (self->ob_type->tp_as_number != NULL && \
2447 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2448 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002449 if (do_other && \
2450 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2451 r = call_maybe( \
2452 other, ROPSTR, &rcache_str, "(O)", self); \
2453 if (r != Py_NotImplemented) \
2454 return r; \
2455 Py_DECREF(r); \
2456 do_other = 0; \
2457 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002458 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002459 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002460 if (r != Py_NotImplemented || \
2461 other->ob_type == self->ob_type) \
2462 return r; \
2463 Py_DECREF(r); \
2464 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002465 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002466 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002467 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002468 } \
2469 Py_INCREF(Py_NotImplemented); \
2470 return Py_NotImplemented; \
2471}
2472
2473#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2474 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2475
2476#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2477static PyObject * \
2478FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2479{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002480 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002481 return call_method(self, OPSTR, &cache_str, \
2482 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002483}
2484
2485static int
2486slot_sq_length(PyObject *self)
2487{
Guido van Rossum2730b132001-08-28 18:22:14 +00002488 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002489 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002490 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002491
2492 if (res == NULL)
2493 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002494 len = (int)PyInt_AsLong(res);
2495 Py_DECREF(res);
2496 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002497}
2498
Guido van Rossumdc91b992001-08-08 22:26:22 +00002499SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2500SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002501
2502/* Super-optimized version of slot_sq_item.
2503 Other slots could do the same... */
2504static PyObject *
2505slot_sq_item(PyObject *self, int i)
2506{
2507 static PyObject *getitem_str;
2508 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2509 descrgetfunc f;
2510
2511 if (getitem_str == NULL) {
2512 getitem_str = PyString_InternFromString("__getitem__");
2513 if (getitem_str == NULL)
2514 return NULL;
2515 }
2516 func = _PyType_Lookup(self->ob_type, getitem_str);
2517 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002518 if ((f = func->ob_type->tp_descr_get) == NULL)
2519 Py_INCREF(func);
2520 else
2521 func = f(func, self, (PyObject *)(self->ob_type));
2522 ival = PyInt_FromLong(i);
2523 if (ival != NULL) {
2524 args = PyTuple_New(1);
2525 if (args != NULL) {
2526 PyTuple_SET_ITEM(args, 0, ival);
2527 retval = PyObject_Call(func, args, NULL);
2528 Py_XDECREF(args);
2529 Py_XDECREF(func);
2530 return retval;
2531 }
2532 }
2533 }
2534 else {
2535 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2536 }
2537 Py_XDECREF(args);
2538 Py_XDECREF(ival);
2539 Py_XDECREF(func);
2540 return NULL;
2541}
2542
Guido van Rossumdc91b992001-08-08 22:26:22 +00002543SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002544
2545static int
2546slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2547{
2548 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002549 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002550
2551 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002552 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002553 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002554 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002555 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002556 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002557 if (res == NULL)
2558 return -1;
2559 Py_DECREF(res);
2560 return 0;
2561}
2562
2563static int
2564slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2565{
2566 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002567 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002568
2569 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002570 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002571 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002573 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002574 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002575 if (res == NULL)
2576 return -1;
2577 Py_DECREF(res);
2578 return 0;
2579}
2580
2581static int
2582slot_sq_contains(PyObject *self, PyObject *value)
2583{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002584 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002585 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002586
Guido van Rossum55f20992001-10-01 17:18:22 +00002587 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002588
2589 if (func != NULL) {
2590 args = Py_BuildValue("(O)", value);
2591 if (args == NULL)
2592 res = NULL;
2593 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002594 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002595 Py_DECREF(args);
2596 }
2597 Py_DECREF(func);
2598 if (res == NULL)
2599 return -1;
2600 return PyObject_IsTrue(res);
2601 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002602 else if (PyErr_Occurred())
2603 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002604 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002605 return _PySequence_IterSearch(self, value,
2606 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002607 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002608}
2609
Guido van Rossumdc91b992001-08-08 22:26:22 +00002610SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2611SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002612
2613#define slot_mp_length slot_sq_length
2614
Guido van Rossumdc91b992001-08-08 22:26:22 +00002615SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002616
2617static int
2618slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2619{
2620 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002621 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002622
2623 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002624 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002625 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002626 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002627 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002628 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002629 if (res == NULL)
2630 return -1;
2631 Py_DECREF(res);
2632 return 0;
2633}
2634
Guido van Rossumdc91b992001-08-08 22:26:22 +00002635SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2636SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2637SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2638SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2639SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2640SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2641
2642staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2643
2644SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2645 nb_power, "__pow__", "__rpow__")
2646
2647static PyObject *
2648slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2649{
Guido van Rossum2730b132001-08-28 18:22:14 +00002650 static PyObject *pow_str;
2651
Guido van Rossumdc91b992001-08-08 22:26:22 +00002652 if (modulus == Py_None)
2653 return slot_nb_power_binary(self, other);
2654 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002655 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002656 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002657}
2658
2659SLOT0(slot_nb_negative, "__neg__")
2660SLOT0(slot_nb_positive, "__pos__")
2661SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002662
2663static int
2664slot_nb_nonzero(PyObject *self)
2665{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002666 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002667 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002668
Guido van Rossum55f20992001-10-01 17:18:22 +00002669 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002670 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002671 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002672 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002673 func = lookup_maybe(self, "__len__", &len_str);
2674 if (func == NULL) {
2675 if (PyErr_Occurred())
2676 return -1;
2677 else
2678 return 1;
2679 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002680 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002681 res = PyObject_CallObject(func, NULL);
2682 Py_DECREF(func);
2683 if (res == NULL)
2684 return -1;
2685 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002686}
2687
Guido van Rossumdc91b992001-08-08 22:26:22 +00002688SLOT0(slot_nb_invert, "__invert__")
2689SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2690SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2691SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2692SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2693SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002694
2695static int
2696slot_nb_coerce(PyObject **a, PyObject **b)
2697{
2698 static PyObject *coerce_str;
2699 PyObject *self = *a, *other = *b;
2700
2701 if (self->ob_type->tp_as_number != NULL &&
2702 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2703 PyObject *r;
2704 r = call_maybe(
2705 self, "__coerce__", &coerce_str, "(O)", other);
2706 if (r == NULL)
2707 return -1;
2708 if (r == Py_NotImplemented) {
2709 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002710 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002711 else {
2712 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2713 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002714 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002715 Py_DECREF(r);
2716 return -1;
2717 }
2718 *a = PyTuple_GET_ITEM(r, 0);
2719 Py_INCREF(*a);
2720 *b = PyTuple_GET_ITEM(r, 1);
2721 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002722 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002723 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002724 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002725 }
2726 if (other->ob_type->tp_as_number != NULL &&
2727 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2728 PyObject *r;
2729 r = call_maybe(
2730 other, "__coerce__", &coerce_str, "(O)", self);
2731 if (r == NULL)
2732 return -1;
2733 if (r == Py_NotImplemented) {
2734 Py_DECREF(r);
2735 return 1;
2736 }
2737 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2738 PyErr_SetString(PyExc_TypeError,
2739 "__coerce__ didn't return a 2-tuple");
2740 Py_DECREF(r);
2741 return -1;
2742 }
2743 *a = PyTuple_GET_ITEM(r, 1);
2744 Py_INCREF(*a);
2745 *b = PyTuple_GET_ITEM(r, 0);
2746 Py_INCREF(*b);
2747 Py_DECREF(r);
2748 return 0;
2749 }
2750 return 1;
2751}
2752
Guido van Rossumdc91b992001-08-08 22:26:22 +00002753SLOT0(slot_nb_int, "__int__")
2754SLOT0(slot_nb_long, "__long__")
2755SLOT0(slot_nb_float, "__float__")
2756SLOT0(slot_nb_oct, "__oct__")
2757SLOT0(slot_nb_hex, "__hex__")
2758SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2759SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2760SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2761SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2762SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2763SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2764SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2765SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2766SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2767SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2768SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2769SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2770 "__floordiv__", "__rfloordiv__")
2771SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2772SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2773SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002774
2775static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002776half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002777{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002778 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002779 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002780 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781
Guido van Rossum60718732001-08-28 17:47:51 +00002782 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002783 if (func == NULL) {
2784 PyErr_Clear();
2785 }
2786 else {
2787 args = Py_BuildValue("(O)", other);
2788 if (args == NULL)
2789 res = NULL;
2790 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002791 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002792 Py_DECREF(args);
2793 }
2794 if (res != Py_NotImplemented) {
2795 if (res == NULL)
2796 return -2;
2797 c = PyInt_AsLong(res);
2798 Py_DECREF(res);
2799 if (c == -1 && PyErr_Occurred())
2800 return -2;
2801 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2802 }
2803 Py_DECREF(res);
2804 }
2805 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002806}
2807
Guido van Rossumab3b0342001-09-18 20:38:53 +00002808/* This slot is published for the benefit of try_3way_compare in object.c */
2809int
2810_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00002811{
2812 int c;
2813
Guido van Rossumab3b0342001-09-18 20:38:53 +00002814 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002815 c = half_compare(self, other);
2816 if (c <= 1)
2817 return c;
2818 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00002819 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002820 c = half_compare(other, self);
2821 if (c < -1)
2822 return -2;
2823 if (c <= 1)
2824 return -c;
2825 }
2826 return (void *)self < (void *)other ? -1 :
2827 (void *)self > (void *)other ? 1 : 0;
2828}
2829
2830static PyObject *
2831slot_tp_repr(PyObject *self)
2832{
2833 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002834 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002835
Guido van Rossum60718732001-08-28 17:47:51 +00002836 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002837 if (func != NULL) {
2838 res = PyEval_CallObject(func, NULL);
2839 Py_DECREF(func);
2840 return res;
2841 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002842 PyErr_Clear();
2843 return PyString_FromFormat("<%s object at %p>",
2844 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002845}
2846
2847static PyObject *
2848slot_tp_str(PyObject *self)
2849{
2850 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002851 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002852
Guido van Rossum60718732001-08-28 17:47:51 +00002853 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002854 if (func != NULL) {
2855 res = PyEval_CallObject(func, NULL);
2856 Py_DECREF(func);
2857 return res;
2858 }
2859 else {
2860 PyErr_Clear();
2861 return slot_tp_repr(self);
2862 }
2863}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002864
2865static long
2866slot_tp_hash(PyObject *self)
2867{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002868 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002869 static PyObject *hash_str, *eq_str, *cmp_str;
2870
Tim Peters6d6c1a32001-08-02 04:15:00 +00002871 long h;
2872
Guido van Rossum60718732001-08-28 17:47:51 +00002873 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002874
2875 if (func != NULL) {
2876 res = PyEval_CallObject(func, NULL);
2877 Py_DECREF(func);
2878 if (res == NULL)
2879 return -1;
2880 h = PyInt_AsLong(res);
2881 }
2882 else {
2883 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002884 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002885 if (func == NULL) {
2886 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002887 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002888 }
2889 if (func != NULL) {
2890 Py_DECREF(func);
2891 PyErr_SetString(PyExc_TypeError, "unhashable type");
2892 return -1;
2893 }
2894 PyErr_Clear();
2895 h = _Py_HashPointer((void *)self);
2896 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002897 if (h == -1 && !PyErr_Occurred())
2898 h = -2;
2899 return h;
2900}
2901
2902static PyObject *
2903slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2904{
Guido van Rossum60718732001-08-28 17:47:51 +00002905 static PyObject *call_str;
2906 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002907 PyObject *res;
2908
2909 if (meth == NULL)
2910 return NULL;
2911 res = PyObject_Call(meth, args, kwds);
2912 Py_DECREF(meth);
2913 return res;
2914}
2915
Guido van Rossum14a6f832001-10-17 13:59:09 +00002916/* There are two slot dispatch functions for tp_getattro.
2917
2918 - slot_tp_getattro() is used when __getattribute__ is overridden
2919 but no __getattr__ hook is present;
2920
2921 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
2922
2923 The code in update_slot() and fixup_slot_dispatchers() always installs
2924 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
2925 installs the simpler slot if necessary. */
2926
Tim Peters6d6c1a32001-08-02 04:15:00 +00002927static PyObject *
2928slot_tp_getattro(PyObject *self, PyObject *name)
2929{
Guido van Rossum14a6f832001-10-17 13:59:09 +00002930 static PyObject *getattribute_str = NULL;
2931 return call_method(self, "__getattribute__", &getattribute_str,
2932 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002933}
2934
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002935static PyObject *
2936slot_tp_getattr_hook(PyObject *self, PyObject *name)
2937{
2938 PyTypeObject *tp = self->ob_type;
2939 PyObject *getattr, *getattribute, *res;
2940 static PyObject *getattribute_str = NULL;
2941 static PyObject *getattr_str = NULL;
2942
2943 if (getattr_str == NULL) {
2944 getattr_str = PyString_InternFromString("__getattr__");
2945 if (getattr_str == NULL)
2946 return NULL;
2947 }
2948 if (getattribute_str == NULL) {
2949 getattribute_str =
2950 PyString_InternFromString("__getattribute__");
2951 if (getattribute_str == NULL)
2952 return NULL;
2953 }
2954 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002955 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00002956 /* No __getattr__ hook: use a simpler dispatcher */
2957 tp->tp_getattro = slot_tp_getattro;
2958 return slot_tp_getattro(self, name);
2959 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002960 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002961 if (getattribute == NULL ||
2962 (getattribute->ob_type == &PyWrapperDescr_Type &&
2963 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
2964 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002965 res = PyObject_GenericGetAttr(self, name);
2966 else
2967 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002968 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002969 PyErr_Clear();
2970 res = PyObject_CallFunction(getattr, "OO", self, name);
2971 }
2972 return res;
2973}
2974
Tim Peters6d6c1a32001-08-02 04:15:00 +00002975static int
2976slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2977{
2978 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002979 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002980
2981 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002982 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002983 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002984 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002985 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002986 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002987 if (res == NULL)
2988 return -1;
2989 Py_DECREF(res);
2990 return 0;
2991}
2992
2993/* Map rich comparison operators to their __xx__ namesakes */
2994static char *name_op[] = {
2995 "__lt__",
2996 "__le__",
2997 "__eq__",
2998 "__ne__",
2999 "__gt__",
3000 "__ge__",
3001};
3002
3003static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003004half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003005{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003006 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003007 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003008
Guido van Rossum60718732001-08-28 17:47:51 +00003009 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003010 if (func == NULL) {
3011 PyErr_Clear();
3012 Py_INCREF(Py_NotImplemented);
3013 return Py_NotImplemented;
3014 }
3015 args = Py_BuildValue("(O)", other);
3016 if (args == NULL)
3017 res = NULL;
3018 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003019 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003020 Py_DECREF(args);
3021 }
3022 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 return res;
3024}
3025
Guido van Rossumb8f63662001-08-15 23:57:02 +00003026/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3027static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3028
3029static PyObject *
3030slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3031{
3032 PyObject *res;
3033
3034 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3035 res = half_richcompare(self, other, op);
3036 if (res != Py_NotImplemented)
3037 return res;
3038 Py_DECREF(res);
3039 }
3040 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3041 res = half_richcompare(other, self, swapped_op[op]);
3042 if (res != Py_NotImplemented) {
3043 return res;
3044 }
3045 Py_DECREF(res);
3046 }
3047 Py_INCREF(Py_NotImplemented);
3048 return Py_NotImplemented;
3049}
3050
3051static PyObject *
3052slot_tp_iter(PyObject *self)
3053{
3054 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003055 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003056
Guido van Rossum60718732001-08-28 17:47:51 +00003057 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003058 if (func != NULL) {
3059 res = PyObject_CallObject(func, NULL);
3060 Py_DECREF(func);
3061 return res;
3062 }
3063 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003064 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003065 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003066 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003067 return NULL;
3068 }
3069 Py_DECREF(func);
3070 return PySeqIter_New(self);
3071}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003072
3073static PyObject *
3074slot_tp_iternext(PyObject *self)
3075{
Guido van Rossum2730b132001-08-28 18:22:14 +00003076 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003077 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003078}
3079
Guido van Rossum1a493502001-08-17 16:47:50 +00003080static PyObject *
3081slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3082{
3083 PyTypeObject *tp = self->ob_type;
3084 PyObject *get;
3085 static PyObject *get_str = NULL;
3086
3087 if (get_str == NULL) {
3088 get_str = PyString_InternFromString("__get__");
3089 if (get_str == NULL)
3090 return NULL;
3091 }
3092 get = _PyType_Lookup(tp, get_str);
3093 if (get == NULL) {
3094 /* Avoid further slowdowns */
3095 if (tp->tp_descr_get == slot_tp_descr_get)
3096 tp->tp_descr_get = NULL;
3097 Py_INCREF(self);
3098 return self;
3099 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003100 if (obj == NULL)
3101 obj = Py_None;
3102 if (type == NULL)
3103 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003104 return PyObject_CallFunction(get, "OOO", self, obj, type);
3105}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003106
3107static int
3108slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3109{
Guido van Rossum2c252392001-08-24 10:13:31 +00003110 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003111 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003112
3113 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003114 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003115 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003116 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003117 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003118 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003119 if (res == NULL)
3120 return -1;
3121 Py_DECREF(res);
3122 return 0;
3123}
3124
3125static int
3126slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3127{
Guido van Rossum60718732001-08-28 17:47:51 +00003128 static PyObject *init_str;
3129 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003130 PyObject *res;
3131
3132 if (meth == NULL)
3133 return -1;
3134 res = PyObject_Call(meth, args, kwds);
3135 Py_DECREF(meth);
3136 if (res == NULL)
3137 return -1;
3138 Py_DECREF(res);
3139 return 0;
3140}
3141
3142static PyObject *
3143slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3144{
3145 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3146 PyObject *newargs, *x;
3147 int i, n;
3148
3149 if (func == NULL)
3150 return NULL;
3151 assert(PyTuple_Check(args));
3152 n = PyTuple_GET_SIZE(args);
3153 newargs = PyTuple_New(n+1);
3154 if (newargs == NULL)
3155 return NULL;
3156 Py_INCREF(type);
3157 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3158 for (i = 0; i < n; i++) {
3159 x = PyTuple_GET_ITEM(args, i);
3160 Py_INCREF(x);
3161 PyTuple_SET_ITEM(newargs, i+1, x);
3162 }
3163 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003164 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003165 Py_DECREF(func);
3166 return x;
3167}
3168
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003169
3170/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3171 functions. The offsets here are relative to the 'etype' structure, which
3172 incorporates the additional structures used for numbers, sequences and
3173 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3174 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3175 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3176
Guido van Rossum6d204072001-10-21 00:44:31 +00003177typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003178
3179#undef TPSLOT
3180#undef ETSLOT
3181#undef SQSLOT
3182#undef MPSLOT
3183#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003184#undef UNSLOT
3185#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003186#undef BINSLOT
3187#undef RBINSLOT
3188
Guido van Rossum6d204072001-10-21 00:44:31 +00003189#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3190 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3191#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3192 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3193#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3194 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3195#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3196 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3197#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3198 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3199#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3200 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3201 "x." NAME "() <==> " DOC)
3202#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3203 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3204 "x." NAME "(y) <==> x" DOC "y")
3205#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3206 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3207 "x." NAME "(y) <==> x" DOC "y")
3208#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3209 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3210 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003211
3212static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003213 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3214 "x.__len__() <==> len(x)"),
3215 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3216 "x.__add__(y) <==> x+y"),
3217 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3218 "x.__mul__(n) <==> x*n"),
3219 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3220 "x.__rmul__(n) <==> n*x"),
3221 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3222 "x.__getitem__(y) <==> x[y]"),
3223 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3224 "x.__getslice__(i, j) <==> x[i:j]"),
3225 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3226 "x.__setitem__(i, y) <==> x[i]=y"),
3227 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3228 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003229 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003230 wrap_intintobjargproc,
3231 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3232 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3233 "x.__delslice__(i, j) <==> del x[i:j]"),
3234 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3235 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003236 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003237 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003238 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003239 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003240
Guido van Rossum6d204072001-10-21 00:44:31 +00003241 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3242 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003243 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003244 wrap_binaryfunc,
3245 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003246 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003247 wrap_objobjargproc,
3248 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003249 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003250 wrap_delitem,
3251 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003252
Guido van Rossum6d204072001-10-21 00:44:31 +00003253 BINSLOT("__add__", nb_add, slot_nb_add,
3254 "+"),
3255 RBINSLOT("__radd__", nb_add, slot_nb_add,
3256 "+"),
3257 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3258 "-"),
3259 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3260 "-"),
3261 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3262 "*"),
3263 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3264 "*"),
3265 BINSLOT("__div__", nb_divide, slot_nb_divide,
3266 "/"),
3267 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3268 "/"),
3269 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3270 "%"),
3271 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3272 "%"),
3273 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3274 "divmod(x, y)"),
3275 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3276 "divmod(y, x)"),
3277 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3278 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3279 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3280 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3281 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3282 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3283 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3284 "abs(x)"),
3285 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3286 "x != 0"),
3287 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3288 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3289 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3290 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3291 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3292 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3293 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3294 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3295 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3296 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3297 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3298 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3299 "x.__coerce__(y) <==> coerce(x, y)"),
3300 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3301 "int(x)"),
3302 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3303 "long(x)"),
3304 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3305 "float(x)"),
3306 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3307 "oct(x)"),
3308 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3309 "hex(x)"),
3310 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3311 wrap_binaryfunc, "+"),
3312 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3313 wrap_binaryfunc, "-"),
3314 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3315 wrap_binaryfunc, "*"),
3316 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3317 wrap_binaryfunc, "/"),
3318 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3319 wrap_binaryfunc, "%"),
3320 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3321 wrap_ternaryfunc, "**"),
3322 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3323 wrap_binaryfunc, "<<"),
3324 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3325 wrap_binaryfunc, ">>"),
3326 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3327 wrap_binaryfunc, "&"),
3328 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3329 wrap_binaryfunc, "^"),
3330 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3331 wrap_binaryfunc, "|"),
3332 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3333 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3334 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3335 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3336 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3337 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3338 IBSLOT("__itruediv__", nb_inplace_true_divide,
3339 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003340
Guido van Rossum6d204072001-10-21 00:44:31 +00003341 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3342 "x.__str__() <==> str(x)"),
3343 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3344 "x.__repr__() <==> repr(x)"),
3345 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3346 "x.__cmp__(y) <==> cmp(x,y)"),
3347 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3348 "x.__hash__() <==> hash(x)"),
3349 TPSLOT("__call__", tp_call, slot_tp_call, wrap_call,
3350 "x.__call__(...) <==> x(...)"),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003351 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003352 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3353 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3354 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3355 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3356 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3357 "x.__setattr__('name', value) <==> x.name = value"),
3358 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3359 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3360 "x.__delattr__('name') <==> del x.name"),
3361 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3362 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3363 "x.__lt__(y) <==> x<y"),
3364 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3365 "x.__le__(y) <==> x<=y"),
3366 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3367 "x.__eq__(y) <==> x==y"),
3368 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3369 "x.__ne__(y) <==> x!=y"),
3370 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3371 "x.__gt__(y) <==> x>y"),
3372 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3373 "x.__ge__(y) <==> x>=y"),
3374 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3375 "x.__iter__() <==> iter(x)"),
3376 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3377 "x.next() -> the next value, or raise StopIteration"),
3378 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3379 "descr.__get__(obj[, type]) -> value"),
3380 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3381 "descr.__set__(obj, value)"),
3382 TPSLOT("__init__", tp_init, slot_tp_init, wrap_init,
3383 "x.__init__(...) initializes x; "
3384 "see x.__class__.__doc__ for signature"),
3385 TPSLOT("__new__", tp_new, slot_tp_new, NULL,
3386 ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003387 {NULL}
3388};
3389
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003390static void **
3391slotptr(PyTypeObject *type, int offset)
3392{
3393 char *ptr;
3394
3395 assert(offset >= 0);
3396 assert(offset < offsetof(etype, as_buffer));
3397 if (offset >= offsetof(etype, as_mapping)) {
3398 ptr = (void *)type->tp_as_mapping;
3399 offset -= offsetof(etype, as_mapping);
3400 }
3401 else if (offset >= offsetof(etype, as_sequence)) {
3402 ptr = (void *)type->tp_as_sequence;
3403 offset -= offsetof(etype, as_sequence);
3404 }
3405 else if (offset >= offsetof(etype, as_number)) {
3406 ptr = (void *)type->tp_as_number;
3407 offset -= offsetof(etype, as_number);
3408 }
3409 else {
3410 ptr = (void *)type;
3411 }
3412 if (ptr != NULL)
3413 ptr += offset;
3414 return (void **)ptr;
3415}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003416
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003417staticforward int recurse_down_subclasses(PyTypeObject *type,
3418 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003419
3420static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003421update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003422{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003423 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003424
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003425 for (pp = pp0; *pp; pp++) {
3426 slotdef *p = *pp;
3427 PyObject *descr;
3428 PyWrapperDescrObject *d;
3429 void *generic = NULL, *specific = NULL;
3430 int use_generic = 0;
3431 int offset = p->offset;
3432 void **ptr = slotptr(type, offset);
3433 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003434 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003435 do {
3436 descr = _PyType_Lookup(type, p->name_strobj);
3437 if (descr == NULL)
3438 continue;
3439 generic = p->function;
3440 if (descr->ob_type == &PyWrapperDescr_Type) {
3441 d = (PyWrapperDescrObject *)descr;
3442 if (d->d_base->wrapper == p->wrapper &&
3443 PyType_IsSubtype(type, d->d_type)) {
3444 if (specific == NULL ||
3445 specific == d->d_wrapped)
3446 specific = d->d_wrapped;
3447 else
3448 use_generic = 1;
3449 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003450 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003451 else
3452 use_generic = 1;
3453 } while ((++p)->offset == offset);
3454 if (specific && !use_generic)
3455 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003456 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003457 *ptr = generic;
3458 }
3459 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003460}
3461
3462static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003463recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003464{
3465 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003466 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003467 int i, n;
3468
3469 subclasses = type->tp_subclasses;
3470 if (subclasses == NULL)
3471 return 0;
3472 assert(PyList_Check(subclasses));
3473 n = PyList_GET_SIZE(subclasses);
3474 for (i = 0; i < n; i++) {
3475 ref = PyList_GET_ITEM(subclasses, i);
3476 assert(PyWeakref_CheckRef(ref));
3477 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3478 if (subclass == NULL)
3479 continue;
3480 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003481 /* Avoid recursing down into unaffected classes */
3482 dict = subclass->tp_dict;
3483 if (dict != NULL && PyDict_Check(dict) &&
3484 PyDict_GetItem(dict, name) != NULL)
3485 continue;
3486 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003487 return -1;
3488 }
3489 return 0;
3490}
3491
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003492static int
3493slotdef_cmp(const void *aa, const void *bb)
3494{
3495 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3496 int c = a->offset - b->offset;
3497 if (c != 0)
3498 return c;
3499 else
3500 return a - b;
3501}
3502
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003503static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003504init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003505{
3506 slotdef *p;
3507 static int initialized = 0;
3508
3509 if (initialized)
3510 return;
3511 for (p = slotdefs; p->name; p++) {
3512 p->name_strobj = PyString_InternFromString(p->name);
3513 if (!p->name_strobj)
3514 Py_FatalError("XXX ouch");
3515 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003516 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3517 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003518 initialized = 1;
3519}
3520
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003521static int
3522update_slot(PyTypeObject *type, PyObject *name)
3523{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003524 slotdef *ptrs[10];
3525 slotdef *p;
3526 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003527 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003528
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003529 init_slotdefs();
3530 pp = ptrs;
3531 for (p = slotdefs; p->name; p++) {
3532 /* XXX assume name is interned! */
3533 if (p->name_strobj == name)
3534 *pp++ = p;
3535 }
3536 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003537 for (pp = ptrs; *pp; pp++) {
3538 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003539 offset = p->offset;
3540 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003541 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003542 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003543 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003544 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003545}
3546
Tim Peters6d6c1a32001-08-02 04:15:00 +00003547static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003548fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003549{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003550 slotdef *p;
3551 PyObject *mro, *descr;
3552 PyTypeObject *base;
3553 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003554 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003555 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003556 void *generic, *specific;
3557 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003558
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003559 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003560 mro = type->tp_mro;
3561 assert(PyTuple_Check(mro));
3562 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003563 for (p = slotdefs; p->name; ) {
3564 offset = p->offset;
3565 ptr = slotptr(type, offset);
3566 if (!ptr) {
3567 do {
3568 ++p;
3569 } while (p->offset == offset);
3570 continue;
3571 }
3572 generic = specific = NULL;
3573 use_generic = 0;
3574 do {
3575 descr = NULL;
3576 for (i = 0; i < n; i++) {
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003577 base = (PyTypeObject *)
3578 PyTuple_GET_ITEM(mro, i);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003579 assert(PyType_Check(base));
3580 descr = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003581 base->tp_dict, p->name_strobj);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003582 if (descr != NULL)
3583 break;
3584 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003585 if (descr == NULL)
3586 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003587 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003588 if (descr->ob_type == &PyWrapperDescr_Type) {
3589 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003590 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003591 PyType_IsSubtype(type, d->d_type))
3592 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003593 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003594 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003595 specific = d->d_wrapped;
3596 else
3597 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003598 }
3599 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003600 else
3601 use_generic = 1;
3602 } while ((++p)->offset == offset);
3603 if (specific && !use_generic)
3604 *ptr = specific;
3605 else
3606 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003607 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003608}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003609
Guido van Rossum6d204072001-10-21 00:44:31 +00003610/* This function is called by PyType_Ready() to populate the type's
3611 dictionary with method descriptors for function slots. For each
3612 function slot (like tp_repr) that's defined in the type, one or
3613 more corresponding descriptors are added in the type's tp_dict
3614 dictionary under the appropriate name (like __repr__). Some
3615 function slots cause more than one descriptor to be added (for
3616 example, the nb_add slot adds both __add__ and __radd__
3617 descriptors) and some function slots compete for the same
3618 descriptor (for example both sq_item and mp_subscript generate a
3619 __getitem__ descriptor). This only adds new descriptors and
3620 doesn't overwrite entries in tp_dict that were previously
3621 defined. The descriptors contain a reference to the C function
3622 they must call, so that it's safe if they are copied into a
3623 subtype's __dict__ and the subtype has a different C function in
3624 its slot -- calling the method defined by the descriptor will call
3625 the C function that was used to create it, rather than the C
3626 function present in the slot when it is called. (This is important
3627 because a subtype may have a C function in the slot that calls the
3628 method from the dictionary, and we want to avoid infinite recursion
3629 here.) */
3630
3631static int
3632add_operators(PyTypeObject *type)
3633{
3634 PyObject *dict = type->tp_dict;
3635 slotdef *p;
3636 PyObject *descr;
3637 void **ptr;
3638
3639 init_slotdefs();
3640 for (p = slotdefs; p->name; p++) {
3641 if (p->wrapper == NULL)
3642 continue;
3643 ptr = slotptr(type, p->offset);
3644 if (!ptr || !*ptr)
3645 continue;
3646 if (PyDict_GetItem(dict, p->name_strobj))
3647 continue;
3648 descr = PyDescr_NewWrapper(type, p, *ptr);
3649 if (descr == NULL)
3650 return -1;
3651 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3652 return -1;
3653 Py_DECREF(descr);
3654 }
3655 if (type->tp_new != NULL) {
3656 if (add_tp_new_wrapper(type) < 0)
3657 return -1;
3658 }
3659 return 0;
3660}
3661
Guido van Rossum705f0f52001-08-24 16:47:00 +00003662
3663/* Cooperative 'super' */
3664
3665typedef struct {
3666 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003667 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003668 PyObject *obj;
3669} superobject;
3670
Guido van Rossum6f799372001-09-20 20:46:19 +00003671static PyMemberDef super_members[] = {
3672 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3673 "the class invoking super()"},
3674 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3675 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003676 {0}
3677};
3678
Guido van Rossum705f0f52001-08-24 16:47:00 +00003679static void
3680super_dealloc(PyObject *self)
3681{
3682 superobject *su = (superobject *)self;
3683
Guido van Rossum048eb752001-10-02 21:24:57 +00003684 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003685 Py_XDECREF(su->obj);
3686 Py_XDECREF(su->type);
3687 self->ob_type->tp_free(self);
3688}
3689
3690static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003691super_repr(PyObject *self)
3692{
3693 superobject *su = (superobject *)self;
3694
3695 if (su->obj)
3696 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003697 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003698 su->type ? su->type->tp_name : "NULL",
3699 su->obj->ob_type->tp_name);
3700 else
3701 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003702 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003703 su->type ? su->type->tp_name : "NULL");
3704}
3705
3706static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003707super_getattro(PyObject *self, PyObject *name)
3708{
3709 superobject *su = (superobject *)self;
3710
3711 if (su->obj != NULL) {
3712 PyObject *mro, *res, *tmp;
3713 descrgetfunc f;
3714 int i, n;
3715
Guido van Rossume705ef12001-08-29 15:47:06 +00003716 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003717 if (mro == NULL)
3718 n = 0;
3719 else {
3720 assert(PyTuple_Check(mro));
3721 n = PyTuple_GET_SIZE(mro);
3722 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003723 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003724 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003725 break;
3726 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003727 if (i >= n && PyType_Check(su->obj)) {
3728 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003729 if (mro == NULL)
3730 n = 0;
3731 else {
3732 assert(PyTuple_Check(mro));
3733 n = PyTuple_GET_SIZE(mro);
3734 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003735 for (i = 0; i < n; i++) {
3736 if ((PyObject *)(su->type) ==
3737 PyTuple_GET_ITEM(mro, i))
3738 break;
3739 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003740 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003741 i++;
3742 res = NULL;
3743 for (; i < n; i++) {
3744 tmp = PyTuple_GET_ITEM(mro, i);
3745 assert(PyType_Check(tmp));
3746 res = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003747 ((PyTypeObject *)tmp)->tp_dict, name);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003748 if (res != NULL) {
3749 Py_INCREF(res);
3750 f = res->ob_type->tp_descr_get;
3751 if (f != NULL) {
3752 tmp = f(res, su->obj, res);
3753 Py_DECREF(res);
3754 res = tmp;
3755 }
3756 return res;
3757 }
3758 }
3759 }
3760 return PyObject_GenericGetAttr(self, name);
3761}
3762
3763static PyObject *
3764super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3765{
3766 superobject *su = (superobject *)self;
3767 superobject *new;
3768
3769 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3770 /* Not binding to an object, or already bound */
3771 Py_INCREF(self);
3772 return self;
3773 }
3774 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3775 if (new == NULL)
3776 return NULL;
3777 Py_INCREF(su->type);
3778 Py_INCREF(obj);
3779 new->type = su->type;
3780 new->obj = obj;
3781 return (PyObject *)new;
3782}
3783
3784static int
3785super_init(PyObject *self, PyObject *args, PyObject *kwds)
3786{
3787 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003788 PyTypeObject *type;
3789 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003790
3791 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3792 return -1;
3793 if (obj == Py_None)
3794 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003795 if (obj != NULL &&
3796 !PyType_IsSubtype(obj->ob_type, type) &&
3797 !(PyType_Check(obj) &&
3798 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003799 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003800 "super(type, obj): "
3801 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003802 return -1;
3803 }
3804 Py_INCREF(type);
3805 Py_XINCREF(obj);
3806 su->type = type;
3807 su->obj = obj;
3808 return 0;
3809}
3810
3811static char super_doc[] =
3812"super(type) -> unbound super object\n"
3813"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003814"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003815"Typical use to call a cooperative superclass method:\n"
3816"class C(B):\n"
3817" def meth(self, arg):\n"
3818" super(C, self).meth(arg)";
3819
Guido van Rossum048eb752001-10-02 21:24:57 +00003820static int
3821super_traverse(PyObject *self, visitproc visit, void *arg)
3822{
3823 superobject *su = (superobject *)self;
3824 int err;
3825
3826#define VISIT(SLOT) \
3827 if (SLOT) { \
3828 err = visit((PyObject *)(SLOT), arg); \
3829 if (err) \
3830 return err; \
3831 }
3832
3833 VISIT(su->obj);
3834 VISIT(su->type);
3835
3836#undef VISIT
3837
3838 return 0;
3839}
3840
Guido van Rossum705f0f52001-08-24 16:47:00 +00003841PyTypeObject PySuper_Type = {
3842 PyObject_HEAD_INIT(&PyType_Type)
3843 0, /* ob_size */
3844 "super", /* tp_name */
3845 sizeof(superobject), /* tp_basicsize */
3846 0, /* tp_itemsize */
3847 /* methods */
3848 super_dealloc, /* tp_dealloc */
3849 0, /* tp_print */
3850 0, /* tp_getattr */
3851 0, /* tp_setattr */
3852 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003853 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003854 0, /* tp_as_number */
3855 0, /* tp_as_sequence */
3856 0, /* tp_as_mapping */
3857 0, /* tp_hash */
3858 0, /* tp_call */
3859 0, /* tp_str */
3860 super_getattro, /* tp_getattro */
3861 0, /* tp_setattro */
3862 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003863 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3864 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003865 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003866 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003867 0, /* tp_clear */
3868 0, /* tp_richcompare */
3869 0, /* tp_weaklistoffset */
3870 0, /* tp_iter */
3871 0, /* tp_iternext */
3872 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003873 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003874 0, /* tp_getset */
3875 0, /* tp_base */
3876 0, /* tp_dict */
3877 super_descr_get, /* tp_descr_get */
3878 0, /* tp_descr_set */
3879 0, /* tp_dictoffset */
3880 super_init, /* tp_init */
3881 PyType_GenericAlloc, /* tp_alloc */
3882 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003883 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003884};