blob: 5952b4efcff2b4d79d75dfb7a20fdfc1b5d44e0f [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 *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002254wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002255{
2256 ternaryfunc func = (ternaryfunc)wrapped;
2257
Guido van Rossumc8e56452001-10-22 00:43:43 +00002258 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002259}
2260
Tim Peters6d6c1a32001-08-02 04:15:00 +00002261static PyObject *
2262wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2263{
2264 richcmpfunc func = (richcmpfunc)wrapped;
2265 PyObject *other;
2266
2267 if (!PyArg_ParseTuple(args, "O", &other))
2268 return NULL;
2269 return (*func)(self, other, op);
2270}
2271
2272#undef RICHCMP_WRAPPER
2273#define RICHCMP_WRAPPER(NAME, OP) \
2274static PyObject * \
2275richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2276{ \
2277 return wrap_richcmpfunc(self, args, wrapped, OP); \
2278}
2279
Jack Jansen8e938b42001-08-08 15:29:49 +00002280RICHCMP_WRAPPER(lt, Py_LT)
2281RICHCMP_WRAPPER(le, Py_LE)
2282RICHCMP_WRAPPER(eq, Py_EQ)
2283RICHCMP_WRAPPER(ne, Py_NE)
2284RICHCMP_WRAPPER(gt, Py_GT)
2285RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286
Tim Peters6d6c1a32001-08-02 04:15:00 +00002287static PyObject *
2288wrap_next(PyObject *self, PyObject *args, void *wrapped)
2289{
2290 unaryfunc func = (unaryfunc)wrapped;
2291 PyObject *res;
2292
2293 if (!PyArg_ParseTuple(args, ""))
2294 return NULL;
2295 res = (*func)(self);
2296 if (res == NULL && !PyErr_Occurred())
2297 PyErr_SetNone(PyExc_StopIteration);
2298 return res;
2299}
2300
Tim Peters6d6c1a32001-08-02 04:15:00 +00002301static PyObject *
2302wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2303{
2304 descrgetfunc func = (descrgetfunc)wrapped;
2305 PyObject *obj;
2306 PyObject *type = NULL;
2307
2308 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2309 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310 return (*func)(self, obj, type);
2311}
2312
Tim Peters6d6c1a32001-08-02 04:15:00 +00002313static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002314wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002315{
2316 descrsetfunc func = (descrsetfunc)wrapped;
2317 PyObject *obj, *value;
2318 int ret;
2319
2320 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2321 return NULL;
2322 ret = (*func)(self, obj, value);
2323 if (ret < 0)
2324 return NULL;
2325 Py_INCREF(Py_None);
2326 return Py_None;
2327}
2328
Tim Peters6d6c1a32001-08-02 04:15:00 +00002329static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002330wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002331{
2332 initproc func = (initproc)wrapped;
2333
Guido van Rossumc8e56452001-10-22 00:43:43 +00002334 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002335 return NULL;
2336 Py_INCREF(Py_None);
2337 return Py_None;
2338}
2339
Tim Peters6d6c1a32001-08-02 04:15:00 +00002340static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002341tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002342{
Barry Warsaw60f01882001-08-22 19:24:42 +00002343 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002344 PyObject *arg0, *res;
2345
2346 if (self == NULL || !PyType_Check(self))
2347 Py_FatalError("__new__() called with non-type 'self'");
2348 type = (PyTypeObject *)self;
2349 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002350 PyErr_Format(PyExc_TypeError,
2351 "%s.__new__(): not enough arguments",
2352 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002353 return NULL;
2354 }
2355 arg0 = PyTuple_GET_ITEM(args, 0);
2356 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002357 PyErr_Format(PyExc_TypeError,
2358 "%s.__new__(X): X is not a type object (%s)",
2359 type->tp_name,
2360 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002361 return NULL;
2362 }
2363 subtype = (PyTypeObject *)arg0;
2364 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002365 PyErr_Format(PyExc_TypeError,
2366 "%s.__new__(%s): %s is not a subtype of %s",
2367 type->tp_name,
2368 subtype->tp_name,
2369 subtype->tp_name,
2370 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002371 return NULL;
2372 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002373
2374 /* Check that the use doesn't do something silly and unsafe like
2375 object.__new__(dictionary). To do this, we check that the
2376 most derived base that's not a heap type is this type. */
2377 staticbase = subtype;
2378 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2379 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002380 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002381 PyErr_Format(PyExc_TypeError,
2382 "%s.__new__(%s) is not safe, use %s.__new__()",
2383 type->tp_name,
2384 subtype->tp_name,
2385 staticbase == NULL ? "?" : staticbase->tp_name);
2386 return NULL;
2387 }
2388
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002389 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2390 if (args == NULL)
2391 return NULL;
2392 res = type->tp_new(subtype, args, kwds);
2393 Py_DECREF(args);
2394 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002395}
2396
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002397static struct PyMethodDef tp_new_methoddef[] = {
2398 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2399 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002400 {0}
2401};
2402
2403static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002404add_tp_new_wrapper(PyTypeObject *type)
2405{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002406 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002407
Guido van Rossum687ae002001-10-15 22:03:32 +00002408 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002409 return 0;
2410 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002411 if (func == NULL)
2412 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002413 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002414}
2415
Guido van Rossumf040ede2001-08-07 16:40:56 +00002416/* Slot wrappers that call the corresponding __foo__ slot. See comments
2417 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418
Guido van Rossumdc91b992001-08-08 22:26:22 +00002419#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002420static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002421FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002422{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002423 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002424 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002425}
2426
Guido van Rossumdc91b992001-08-08 22:26:22 +00002427#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002428static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002429FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002430{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002431 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002432 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433}
2434
Guido van Rossumdc91b992001-08-08 22:26:22 +00002435
2436#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002437static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002438FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002439{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002440 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002441 int do_other = self->ob_type != other->ob_type && \
2442 other->ob_type->tp_as_number != NULL && \
2443 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002444 if (self->ob_type->tp_as_number != NULL && \
2445 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2446 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002447 if (do_other && \
2448 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2449 r = call_maybe( \
2450 other, ROPSTR, &rcache_str, "(O)", self); \
2451 if (r != Py_NotImplemented) \
2452 return r; \
2453 Py_DECREF(r); \
2454 do_other = 0; \
2455 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002456 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002457 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002458 if (r != Py_NotImplemented || \
2459 other->ob_type == self->ob_type) \
2460 return r; \
2461 Py_DECREF(r); \
2462 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002463 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002464 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002465 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002466 } \
2467 Py_INCREF(Py_NotImplemented); \
2468 return Py_NotImplemented; \
2469}
2470
2471#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2472 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2473
2474#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2475static PyObject * \
2476FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2477{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002478 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002479 return call_method(self, OPSTR, &cache_str, \
2480 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002481}
2482
2483static int
2484slot_sq_length(PyObject *self)
2485{
Guido van Rossum2730b132001-08-28 18:22:14 +00002486 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002487 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002488 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002489
2490 if (res == NULL)
2491 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002492 len = (int)PyInt_AsLong(res);
2493 Py_DECREF(res);
2494 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002495}
2496
Guido van Rossumdc91b992001-08-08 22:26:22 +00002497SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2498SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002499
2500/* Super-optimized version of slot_sq_item.
2501 Other slots could do the same... */
2502static PyObject *
2503slot_sq_item(PyObject *self, int i)
2504{
2505 static PyObject *getitem_str;
2506 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2507 descrgetfunc f;
2508
2509 if (getitem_str == NULL) {
2510 getitem_str = PyString_InternFromString("__getitem__");
2511 if (getitem_str == NULL)
2512 return NULL;
2513 }
2514 func = _PyType_Lookup(self->ob_type, getitem_str);
2515 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002516 if ((f = func->ob_type->tp_descr_get) == NULL)
2517 Py_INCREF(func);
2518 else
2519 func = f(func, self, (PyObject *)(self->ob_type));
2520 ival = PyInt_FromLong(i);
2521 if (ival != NULL) {
2522 args = PyTuple_New(1);
2523 if (args != NULL) {
2524 PyTuple_SET_ITEM(args, 0, ival);
2525 retval = PyObject_Call(func, args, NULL);
2526 Py_XDECREF(args);
2527 Py_XDECREF(func);
2528 return retval;
2529 }
2530 }
2531 }
2532 else {
2533 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2534 }
2535 Py_XDECREF(args);
2536 Py_XDECREF(ival);
2537 Py_XDECREF(func);
2538 return NULL;
2539}
2540
Guido van Rossumdc91b992001-08-08 22:26:22 +00002541SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002542
2543static int
2544slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2545{
2546 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002547 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002548
2549 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002550 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002551 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002552 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002553 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002554 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002555 if (res == NULL)
2556 return -1;
2557 Py_DECREF(res);
2558 return 0;
2559}
2560
2561static int
2562slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2563{
2564 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002565 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002566
2567 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002568 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002569 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002570 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002571 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002572 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002573 if (res == NULL)
2574 return -1;
2575 Py_DECREF(res);
2576 return 0;
2577}
2578
2579static int
2580slot_sq_contains(PyObject *self, PyObject *value)
2581{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002582 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002583 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002584
Guido van Rossum55f20992001-10-01 17:18:22 +00002585 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002586
2587 if (func != NULL) {
2588 args = Py_BuildValue("(O)", value);
2589 if (args == NULL)
2590 res = NULL;
2591 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002592 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002593 Py_DECREF(args);
2594 }
2595 Py_DECREF(func);
2596 if (res == NULL)
2597 return -1;
2598 return PyObject_IsTrue(res);
2599 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002600 else if (PyErr_Occurred())
2601 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002602 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002603 return _PySequence_IterSearch(self, value,
2604 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002605 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002606}
2607
Guido van Rossumdc91b992001-08-08 22:26:22 +00002608SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2609SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002610
2611#define slot_mp_length slot_sq_length
2612
Guido van Rossumdc91b992001-08-08 22:26:22 +00002613SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002614
2615static int
2616slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2617{
2618 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002619 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002620
2621 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002622 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002623 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002624 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002625 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002626 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002627 if (res == NULL)
2628 return -1;
2629 Py_DECREF(res);
2630 return 0;
2631}
2632
Guido van Rossumdc91b992001-08-08 22:26:22 +00002633SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2634SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2635SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2636SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2637SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2638SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2639
2640staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2641
2642SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2643 nb_power, "__pow__", "__rpow__")
2644
2645static PyObject *
2646slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2647{
Guido van Rossum2730b132001-08-28 18:22:14 +00002648 static PyObject *pow_str;
2649
Guido van Rossumdc91b992001-08-08 22:26:22 +00002650 if (modulus == Py_None)
2651 return slot_nb_power_binary(self, other);
2652 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002653 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002654 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002655}
2656
2657SLOT0(slot_nb_negative, "__neg__")
2658SLOT0(slot_nb_positive, "__pos__")
2659SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002660
2661static int
2662slot_nb_nonzero(PyObject *self)
2663{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002664 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002665 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002666
Guido van Rossum55f20992001-10-01 17:18:22 +00002667 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002668 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002669 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002670 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002671 func = lookup_maybe(self, "__len__", &len_str);
2672 if (func == NULL) {
2673 if (PyErr_Occurred())
2674 return -1;
2675 else
2676 return 1;
2677 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002678 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002679 res = PyObject_CallObject(func, NULL);
2680 Py_DECREF(func);
2681 if (res == NULL)
2682 return -1;
2683 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002684}
2685
Guido van Rossumdc91b992001-08-08 22:26:22 +00002686SLOT0(slot_nb_invert, "__invert__")
2687SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2688SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2689SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2690SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2691SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002692
2693static int
2694slot_nb_coerce(PyObject **a, PyObject **b)
2695{
2696 static PyObject *coerce_str;
2697 PyObject *self = *a, *other = *b;
2698
2699 if (self->ob_type->tp_as_number != NULL &&
2700 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2701 PyObject *r;
2702 r = call_maybe(
2703 self, "__coerce__", &coerce_str, "(O)", other);
2704 if (r == NULL)
2705 return -1;
2706 if (r == Py_NotImplemented) {
2707 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002708 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002709 else {
2710 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2711 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002712 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002713 Py_DECREF(r);
2714 return -1;
2715 }
2716 *a = PyTuple_GET_ITEM(r, 0);
2717 Py_INCREF(*a);
2718 *b = PyTuple_GET_ITEM(r, 1);
2719 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002720 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002721 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002722 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002723 }
2724 if (other->ob_type->tp_as_number != NULL &&
2725 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2726 PyObject *r;
2727 r = call_maybe(
2728 other, "__coerce__", &coerce_str, "(O)", self);
2729 if (r == NULL)
2730 return -1;
2731 if (r == Py_NotImplemented) {
2732 Py_DECREF(r);
2733 return 1;
2734 }
2735 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2736 PyErr_SetString(PyExc_TypeError,
2737 "__coerce__ didn't return a 2-tuple");
2738 Py_DECREF(r);
2739 return -1;
2740 }
2741 *a = PyTuple_GET_ITEM(r, 1);
2742 Py_INCREF(*a);
2743 *b = PyTuple_GET_ITEM(r, 0);
2744 Py_INCREF(*b);
2745 Py_DECREF(r);
2746 return 0;
2747 }
2748 return 1;
2749}
2750
Guido van Rossumdc91b992001-08-08 22:26:22 +00002751SLOT0(slot_nb_int, "__int__")
2752SLOT0(slot_nb_long, "__long__")
2753SLOT0(slot_nb_float, "__float__")
2754SLOT0(slot_nb_oct, "__oct__")
2755SLOT0(slot_nb_hex, "__hex__")
2756SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2757SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2758SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2759SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2760SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2761SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2762SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2763SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2764SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2765SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2766SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2767SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2768 "__floordiv__", "__rfloordiv__")
2769SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2770SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2771SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772
2773static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002774half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002775{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002776 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002777 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002778 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002779
Guido van Rossum60718732001-08-28 17:47:51 +00002780 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002781 if (func == NULL) {
2782 PyErr_Clear();
2783 }
2784 else {
2785 args = Py_BuildValue("(O)", other);
2786 if (args == NULL)
2787 res = NULL;
2788 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002789 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002790 Py_DECREF(args);
2791 }
2792 if (res != Py_NotImplemented) {
2793 if (res == NULL)
2794 return -2;
2795 c = PyInt_AsLong(res);
2796 Py_DECREF(res);
2797 if (c == -1 && PyErr_Occurred())
2798 return -2;
2799 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2800 }
2801 Py_DECREF(res);
2802 }
2803 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002804}
2805
Guido van Rossumab3b0342001-09-18 20:38:53 +00002806/* This slot is published for the benefit of try_3way_compare in object.c */
2807int
2808_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00002809{
2810 int c;
2811
Guido van Rossumab3b0342001-09-18 20:38:53 +00002812 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002813 c = half_compare(self, other);
2814 if (c <= 1)
2815 return c;
2816 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00002817 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002818 c = half_compare(other, self);
2819 if (c < -1)
2820 return -2;
2821 if (c <= 1)
2822 return -c;
2823 }
2824 return (void *)self < (void *)other ? -1 :
2825 (void *)self > (void *)other ? 1 : 0;
2826}
2827
2828static PyObject *
2829slot_tp_repr(PyObject *self)
2830{
2831 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002832 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002833
Guido van Rossum60718732001-08-28 17:47:51 +00002834 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002835 if (func != NULL) {
2836 res = PyEval_CallObject(func, NULL);
2837 Py_DECREF(func);
2838 return res;
2839 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002840 PyErr_Clear();
2841 return PyString_FromFormat("<%s object at %p>",
2842 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002843}
2844
2845static PyObject *
2846slot_tp_str(PyObject *self)
2847{
2848 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002849 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002850
Guido van Rossum60718732001-08-28 17:47:51 +00002851 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002852 if (func != NULL) {
2853 res = PyEval_CallObject(func, NULL);
2854 Py_DECREF(func);
2855 return res;
2856 }
2857 else {
2858 PyErr_Clear();
2859 return slot_tp_repr(self);
2860 }
2861}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002862
2863static long
2864slot_tp_hash(PyObject *self)
2865{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002866 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002867 static PyObject *hash_str, *eq_str, *cmp_str;
2868
Tim Peters6d6c1a32001-08-02 04:15:00 +00002869 long h;
2870
Guido van Rossum60718732001-08-28 17:47:51 +00002871 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002872
2873 if (func != NULL) {
2874 res = PyEval_CallObject(func, NULL);
2875 Py_DECREF(func);
2876 if (res == NULL)
2877 return -1;
2878 h = PyInt_AsLong(res);
2879 }
2880 else {
2881 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002882 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002883 if (func == NULL) {
2884 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002885 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002886 }
2887 if (func != NULL) {
2888 Py_DECREF(func);
2889 PyErr_SetString(PyExc_TypeError, "unhashable type");
2890 return -1;
2891 }
2892 PyErr_Clear();
2893 h = _Py_HashPointer((void *)self);
2894 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002895 if (h == -1 && !PyErr_Occurred())
2896 h = -2;
2897 return h;
2898}
2899
2900static PyObject *
2901slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2902{
Guido van Rossum60718732001-08-28 17:47:51 +00002903 static PyObject *call_str;
2904 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002905 PyObject *res;
2906
2907 if (meth == NULL)
2908 return NULL;
2909 res = PyObject_Call(meth, args, kwds);
2910 Py_DECREF(meth);
2911 return res;
2912}
2913
Guido van Rossum14a6f832001-10-17 13:59:09 +00002914/* There are two slot dispatch functions for tp_getattro.
2915
2916 - slot_tp_getattro() is used when __getattribute__ is overridden
2917 but no __getattr__ hook is present;
2918
2919 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
2920
2921 The code in update_slot() and fixup_slot_dispatchers() always installs
2922 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
2923 installs the simpler slot if necessary. */
2924
Tim Peters6d6c1a32001-08-02 04:15:00 +00002925static PyObject *
2926slot_tp_getattro(PyObject *self, PyObject *name)
2927{
Guido van Rossum14a6f832001-10-17 13:59:09 +00002928 static PyObject *getattribute_str = NULL;
2929 return call_method(self, "__getattribute__", &getattribute_str,
2930 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002931}
2932
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002933static PyObject *
2934slot_tp_getattr_hook(PyObject *self, PyObject *name)
2935{
2936 PyTypeObject *tp = self->ob_type;
2937 PyObject *getattr, *getattribute, *res;
2938 static PyObject *getattribute_str = NULL;
2939 static PyObject *getattr_str = NULL;
2940
2941 if (getattr_str == NULL) {
2942 getattr_str = PyString_InternFromString("__getattr__");
2943 if (getattr_str == NULL)
2944 return NULL;
2945 }
2946 if (getattribute_str == NULL) {
2947 getattribute_str =
2948 PyString_InternFromString("__getattribute__");
2949 if (getattribute_str == NULL)
2950 return NULL;
2951 }
2952 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002953 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00002954 /* No __getattr__ hook: use a simpler dispatcher */
2955 tp->tp_getattro = slot_tp_getattro;
2956 return slot_tp_getattro(self, name);
2957 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002958 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002959 if (getattribute == NULL ||
2960 (getattribute->ob_type == &PyWrapperDescr_Type &&
2961 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
2962 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002963 res = PyObject_GenericGetAttr(self, name);
2964 else
2965 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002966 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002967 PyErr_Clear();
2968 res = PyObject_CallFunction(getattr, "OO", self, name);
2969 }
2970 return res;
2971}
2972
Tim Peters6d6c1a32001-08-02 04:15:00 +00002973static int
2974slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2975{
2976 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002977 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002978
2979 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002980 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002981 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002982 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002983 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002984 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002985 if (res == NULL)
2986 return -1;
2987 Py_DECREF(res);
2988 return 0;
2989}
2990
2991/* Map rich comparison operators to their __xx__ namesakes */
2992static char *name_op[] = {
2993 "__lt__",
2994 "__le__",
2995 "__eq__",
2996 "__ne__",
2997 "__gt__",
2998 "__ge__",
2999};
3000
3001static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003002half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003004 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003005 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003006
Guido van Rossum60718732001-08-28 17:47:51 +00003007 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003008 if (func == NULL) {
3009 PyErr_Clear();
3010 Py_INCREF(Py_NotImplemented);
3011 return Py_NotImplemented;
3012 }
3013 args = Py_BuildValue("(O)", other);
3014 if (args == NULL)
3015 res = NULL;
3016 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003017 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003018 Py_DECREF(args);
3019 }
3020 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003021 return res;
3022}
3023
Guido van Rossumb8f63662001-08-15 23:57:02 +00003024/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3025static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3026
3027static PyObject *
3028slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3029{
3030 PyObject *res;
3031
3032 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3033 res = half_richcompare(self, other, op);
3034 if (res != Py_NotImplemented)
3035 return res;
3036 Py_DECREF(res);
3037 }
3038 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3039 res = half_richcompare(other, self, swapped_op[op]);
3040 if (res != Py_NotImplemented) {
3041 return res;
3042 }
3043 Py_DECREF(res);
3044 }
3045 Py_INCREF(Py_NotImplemented);
3046 return Py_NotImplemented;
3047}
3048
3049static PyObject *
3050slot_tp_iter(PyObject *self)
3051{
3052 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003053 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003054
Guido van Rossum60718732001-08-28 17:47:51 +00003055 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003056 if (func != NULL) {
3057 res = PyObject_CallObject(func, NULL);
3058 Py_DECREF(func);
3059 return res;
3060 }
3061 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003062 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003063 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003064 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003065 return NULL;
3066 }
3067 Py_DECREF(func);
3068 return PySeqIter_New(self);
3069}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003070
3071static PyObject *
3072slot_tp_iternext(PyObject *self)
3073{
Guido van Rossum2730b132001-08-28 18:22:14 +00003074 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003075 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003076}
3077
Guido van Rossum1a493502001-08-17 16:47:50 +00003078static PyObject *
3079slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3080{
3081 PyTypeObject *tp = self->ob_type;
3082 PyObject *get;
3083 static PyObject *get_str = NULL;
3084
3085 if (get_str == NULL) {
3086 get_str = PyString_InternFromString("__get__");
3087 if (get_str == NULL)
3088 return NULL;
3089 }
3090 get = _PyType_Lookup(tp, get_str);
3091 if (get == NULL) {
3092 /* Avoid further slowdowns */
3093 if (tp->tp_descr_get == slot_tp_descr_get)
3094 tp->tp_descr_get = NULL;
3095 Py_INCREF(self);
3096 return self;
3097 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003098 if (obj == NULL)
3099 obj = Py_None;
3100 if (type == NULL)
3101 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003102 return PyObject_CallFunction(get, "OOO", self, obj, type);
3103}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003104
3105static int
3106slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3107{
Guido van Rossum2c252392001-08-24 10:13:31 +00003108 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003109 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003110
3111 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003112 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003113 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003114 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003115 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003116 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003117 if (res == NULL)
3118 return -1;
3119 Py_DECREF(res);
3120 return 0;
3121}
3122
3123static int
3124slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3125{
Guido van Rossum60718732001-08-28 17:47:51 +00003126 static PyObject *init_str;
3127 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003128 PyObject *res;
3129
3130 if (meth == NULL)
3131 return -1;
3132 res = PyObject_Call(meth, args, kwds);
3133 Py_DECREF(meth);
3134 if (res == NULL)
3135 return -1;
3136 Py_DECREF(res);
3137 return 0;
3138}
3139
3140static PyObject *
3141slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3142{
3143 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3144 PyObject *newargs, *x;
3145 int i, n;
3146
3147 if (func == NULL)
3148 return NULL;
3149 assert(PyTuple_Check(args));
3150 n = PyTuple_GET_SIZE(args);
3151 newargs = PyTuple_New(n+1);
3152 if (newargs == NULL)
3153 return NULL;
3154 Py_INCREF(type);
3155 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3156 for (i = 0; i < n; i++) {
3157 x = PyTuple_GET_ITEM(args, i);
3158 Py_INCREF(x);
3159 PyTuple_SET_ITEM(newargs, i+1, x);
3160 }
3161 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003162 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003163 Py_DECREF(func);
3164 return x;
3165}
3166
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003167
3168/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3169 functions. The offsets here are relative to the 'etype' structure, which
3170 incorporates the additional structures used for numbers, sequences and
3171 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3172 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3173 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3174
Guido van Rossum6d204072001-10-21 00:44:31 +00003175typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003176
3177#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003178#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003179#undef ETSLOT
3180#undef SQSLOT
3181#undef MPSLOT
3182#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003183#undef UNSLOT
3184#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003185#undef BINSLOT
3186#undef RBINSLOT
3187
Guido van Rossum6d204072001-10-21 00:44:31 +00003188#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3189 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003190#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3191 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3192 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003193#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3194 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3195#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3196 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3197#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3198 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3199#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3200 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3201#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3202 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3203 "x." NAME "() <==> " DOC)
3204#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3205 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3206 "x." NAME "(y) <==> x" DOC "y")
3207#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3208 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3209 "x." NAME "(y) <==> x" DOC "y")
3210#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3211 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3212 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003213
3214static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003215 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3216 "x.__len__() <==> len(x)"),
3217 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3218 "x.__add__(y) <==> x+y"),
3219 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3220 "x.__mul__(n) <==> x*n"),
3221 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3222 "x.__rmul__(n) <==> n*x"),
3223 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3224 "x.__getitem__(y) <==> x[y]"),
3225 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3226 "x.__getslice__(i, j) <==> x[i:j]"),
3227 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3228 "x.__setitem__(i, y) <==> x[i]=y"),
3229 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3230 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003231 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003232 wrap_intintobjargproc,
3233 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3234 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3235 "x.__delslice__(i, j) <==> del x[i:j]"),
3236 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3237 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003238 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003239 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003240 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003241 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003242
Guido van Rossum6d204072001-10-21 00:44:31 +00003243 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3244 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003245 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003246 wrap_binaryfunc,
3247 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003248 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003249 wrap_objobjargproc,
3250 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003251 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003252 wrap_delitem,
3253 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003254
Guido van Rossum6d204072001-10-21 00:44:31 +00003255 BINSLOT("__add__", nb_add, slot_nb_add,
3256 "+"),
3257 RBINSLOT("__radd__", nb_add, slot_nb_add,
3258 "+"),
3259 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3260 "-"),
3261 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3262 "-"),
3263 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3264 "*"),
3265 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3266 "*"),
3267 BINSLOT("__div__", nb_divide, slot_nb_divide,
3268 "/"),
3269 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3270 "/"),
3271 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3272 "%"),
3273 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3274 "%"),
3275 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3276 "divmod(x, y)"),
3277 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3278 "divmod(y, x)"),
3279 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3280 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3281 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3282 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3283 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3284 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3285 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3286 "abs(x)"),
3287 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3288 "x != 0"),
3289 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3290 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3291 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3292 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3293 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3294 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3295 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3296 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3297 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3298 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3299 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3300 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3301 "x.__coerce__(y) <==> coerce(x, y)"),
3302 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3303 "int(x)"),
3304 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3305 "long(x)"),
3306 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3307 "float(x)"),
3308 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3309 "oct(x)"),
3310 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3311 "hex(x)"),
3312 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3313 wrap_binaryfunc, "+"),
3314 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3315 wrap_binaryfunc, "-"),
3316 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3317 wrap_binaryfunc, "*"),
3318 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3319 wrap_binaryfunc, "/"),
3320 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3321 wrap_binaryfunc, "%"),
3322 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3323 wrap_ternaryfunc, "**"),
3324 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3325 wrap_binaryfunc, "<<"),
3326 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3327 wrap_binaryfunc, ">>"),
3328 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3329 wrap_binaryfunc, "&"),
3330 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3331 wrap_binaryfunc, "^"),
3332 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3333 wrap_binaryfunc, "|"),
3334 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3335 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3336 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3337 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3338 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3339 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3340 IBSLOT("__itruediv__", nb_inplace_true_divide,
3341 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003342
Guido van Rossum6d204072001-10-21 00:44:31 +00003343 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3344 "x.__str__() <==> str(x)"),
3345 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3346 "x.__repr__() <==> repr(x)"),
3347 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3348 "x.__cmp__(y) <==> cmp(x,y)"),
3349 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3350 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003351 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3352 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003353 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003354 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3355 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3356 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3357 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3358 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3359 "x.__setattr__('name', value) <==> x.name = value"),
3360 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3361 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3362 "x.__delattr__('name') <==> del x.name"),
3363 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3364 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3365 "x.__lt__(y) <==> x<y"),
3366 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3367 "x.__le__(y) <==> x<=y"),
3368 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3369 "x.__eq__(y) <==> x==y"),
3370 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3371 "x.__ne__(y) <==> x!=y"),
3372 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3373 "x.__gt__(y) <==> x>y"),
3374 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3375 "x.__ge__(y) <==> x>=y"),
3376 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3377 "x.__iter__() <==> iter(x)"),
3378 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3379 "x.next() -> the next value, or raise StopIteration"),
3380 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3381 "descr.__get__(obj[, type]) -> value"),
3382 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3383 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003384 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003385 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003386 "see x.__class__.__doc__ for signature",
3387 PyWrapperFlag_KEYWORDS),
3388 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003389 {NULL}
3390};
3391
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003392static void **
3393slotptr(PyTypeObject *type, int offset)
3394{
3395 char *ptr;
3396
3397 assert(offset >= 0);
3398 assert(offset < offsetof(etype, as_buffer));
3399 if (offset >= offsetof(etype, as_mapping)) {
3400 ptr = (void *)type->tp_as_mapping;
3401 offset -= offsetof(etype, as_mapping);
3402 }
3403 else if (offset >= offsetof(etype, as_sequence)) {
3404 ptr = (void *)type->tp_as_sequence;
3405 offset -= offsetof(etype, as_sequence);
3406 }
3407 else if (offset >= offsetof(etype, as_number)) {
3408 ptr = (void *)type->tp_as_number;
3409 offset -= offsetof(etype, as_number);
3410 }
3411 else {
3412 ptr = (void *)type;
3413 }
3414 if (ptr != NULL)
3415 ptr += offset;
3416 return (void **)ptr;
3417}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003418
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003419staticforward int recurse_down_subclasses(PyTypeObject *type,
3420 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003421
3422static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003423update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003424{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003425 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003426
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003427 for (pp = pp0; *pp; pp++) {
3428 slotdef *p = *pp;
3429 PyObject *descr;
3430 PyWrapperDescrObject *d;
3431 void *generic = NULL, *specific = NULL;
3432 int use_generic = 0;
3433 int offset = p->offset;
3434 void **ptr = slotptr(type, offset);
3435 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003436 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003437 do {
3438 descr = _PyType_Lookup(type, p->name_strobj);
3439 if (descr == NULL)
3440 continue;
3441 generic = p->function;
3442 if (descr->ob_type == &PyWrapperDescr_Type) {
3443 d = (PyWrapperDescrObject *)descr;
3444 if (d->d_base->wrapper == p->wrapper &&
3445 PyType_IsSubtype(type, d->d_type)) {
3446 if (specific == NULL ||
3447 specific == d->d_wrapped)
3448 specific = d->d_wrapped;
3449 else
3450 use_generic = 1;
3451 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003452 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003453 else
3454 use_generic = 1;
3455 } while ((++p)->offset == offset);
3456 if (specific && !use_generic)
3457 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003458 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003459 *ptr = generic;
3460 }
3461 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003462}
3463
3464static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003465recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003466{
3467 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003468 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003469 int i, n;
3470
3471 subclasses = type->tp_subclasses;
3472 if (subclasses == NULL)
3473 return 0;
3474 assert(PyList_Check(subclasses));
3475 n = PyList_GET_SIZE(subclasses);
3476 for (i = 0; i < n; i++) {
3477 ref = PyList_GET_ITEM(subclasses, i);
3478 assert(PyWeakref_CheckRef(ref));
3479 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3480 if (subclass == NULL)
3481 continue;
3482 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003483 /* Avoid recursing down into unaffected classes */
3484 dict = subclass->tp_dict;
3485 if (dict != NULL && PyDict_Check(dict) &&
3486 PyDict_GetItem(dict, name) != NULL)
3487 continue;
3488 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003489 return -1;
3490 }
3491 return 0;
3492}
3493
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003494static int
3495slotdef_cmp(const void *aa, const void *bb)
3496{
3497 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3498 int c = a->offset - b->offset;
3499 if (c != 0)
3500 return c;
3501 else
3502 return a - b;
3503}
3504
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003505static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003506init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003507{
3508 slotdef *p;
3509 static int initialized = 0;
3510
3511 if (initialized)
3512 return;
3513 for (p = slotdefs; p->name; p++) {
3514 p->name_strobj = PyString_InternFromString(p->name);
3515 if (!p->name_strobj)
3516 Py_FatalError("XXX ouch");
3517 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003518 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3519 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003520 initialized = 1;
3521}
3522
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003523static int
3524update_slot(PyTypeObject *type, PyObject *name)
3525{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003526 slotdef *ptrs[10];
3527 slotdef *p;
3528 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003529 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003530
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003531 init_slotdefs();
3532 pp = ptrs;
3533 for (p = slotdefs; p->name; p++) {
3534 /* XXX assume name is interned! */
3535 if (p->name_strobj == name)
3536 *pp++ = p;
3537 }
3538 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003539 for (pp = ptrs; *pp; pp++) {
3540 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003541 offset = p->offset;
3542 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003543 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003544 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003545 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003546 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003547}
3548
Tim Peters6d6c1a32001-08-02 04:15:00 +00003549static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003550fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003551{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003552 slotdef *p;
3553 PyObject *mro, *descr;
3554 PyTypeObject *base;
3555 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003556 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003557 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003558 void *generic, *specific;
3559 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003560
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003561 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003562 mro = type->tp_mro;
3563 assert(PyTuple_Check(mro));
3564 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003565 for (p = slotdefs; p->name; ) {
3566 offset = p->offset;
3567 ptr = slotptr(type, offset);
3568 if (!ptr) {
3569 do {
3570 ++p;
3571 } while (p->offset == offset);
3572 continue;
3573 }
3574 generic = specific = NULL;
3575 use_generic = 0;
3576 do {
3577 descr = NULL;
3578 for (i = 0; i < n; i++) {
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003579 base = (PyTypeObject *)
3580 PyTuple_GET_ITEM(mro, i);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003581 assert(PyType_Check(base));
3582 descr = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003583 base->tp_dict, p->name_strobj);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003584 if (descr != NULL)
3585 break;
3586 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003587 if (descr == NULL)
3588 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003589 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003590 if (descr->ob_type == &PyWrapperDescr_Type) {
3591 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003592 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003593 PyType_IsSubtype(type, d->d_type))
3594 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003595 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003596 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003597 specific = d->d_wrapped;
3598 else
3599 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003600 }
3601 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003602 else
3603 use_generic = 1;
3604 } while ((++p)->offset == offset);
3605 if (specific && !use_generic)
3606 *ptr = specific;
3607 else
3608 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003609 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003610}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003611
Guido van Rossum6d204072001-10-21 00:44:31 +00003612/* This function is called by PyType_Ready() to populate the type's
3613 dictionary with method descriptors for function slots. For each
3614 function slot (like tp_repr) that's defined in the type, one or
3615 more corresponding descriptors are added in the type's tp_dict
3616 dictionary under the appropriate name (like __repr__). Some
3617 function slots cause more than one descriptor to be added (for
3618 example, the nb_add slot adds both __add__ and __radd__
3619 descriptors) and some function slots compete for the same
3620 descriptor (for example both sq_item and mp_subscript generate a
3621 __getitem__ descriptor). This only adds new descriptors and
3622 doesn't overwrite entries in tp_dict that were previously
3623 defined. The descriptors contain a reference to the C function
3624 they must call, so that it's safe if they are copied into a
3625 subtype's __dict__ and the subtype has a different C function in
3626 its slot -- calling the method defined by the descriptor will call
3627 the C function that was used to create it, rather than the C
3628 function present in the slot when it is called. (This is important
3629 because a subtype may have a C function in the slot that calls the
3630 method from the dictionary, and we want to avoid infinite recursion
3631 here.) */
3632
3633static int
3634add_operators(PyTypeObject *type)
3635{
3636 PyObject *dict = type->tp_dict;
3637 slotdef *p;
3638 PyObject *descr;
3639 void **ptr;
3640
3641 init_slotdefs();
3642 for (p = slotdefs; p->name; p++) {
3643 if (p->wrapper == NULL)
3644 continue;
3645 ptr = slotptr(type, p->offset);
3646 if (!ptr || !*ptr)
3647 continue;
3648 if (PyDict_GetItem(dict, p->name_strobj))
3649 continue;
3650 descr = PyDescr_NewWrapper(type, p, *ptr);
3651 if (descr == NULL)
3652 return -1;
3653 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3654 return -1;
3655 Py_DECREF(descr);
3656 }
3657 if (type->tp_new != NULL) {
3658 if (add_tp_new_wrapper(type) < 0)
3659 return -1;
3660 }
3661 return 0;
3662}
3663
Guido van Rossum705f0f52001-08-24 16:47:00 +00003664
3665/* Cooperative 'super' */
3666
3667typedef struct {
3668 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003669 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003670 PyObject *obj;
3671} superobject;
3672
Guido van Rossum6f799372001-09-20 20:46:19 +00003673static PyMemberDef super_members[] = {
3674 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3675 "the class invoking super()"},
3676 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3677 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003678 {0}
3679};
3680
Guido van Rossum705f0f52001-08-24 16:47:00 +00003681static void
3682super_dealloc(PyObject *self)
3683{
3684 superobject *su = (superobject *)self;
3685
Guido van Rossum048eb752001-10-02 21:24:57 +00003686 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003687 Py_XDECREF(su->obj);
3688 Py_XDECREF(su->type);
3689 self->ob_type->tp_free(self);
3690}
3691
3692static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003693super_repr(PyObject *self)
3694{
3695 superobject *su = (superobject *)self;
3696
3697 if (su->obj)
3698 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003699 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003700 su->type ? su->type->tp_name : "NULL",
3701 su->obj->ob_type->tp_name);
3702 else
3703 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003704 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003705 su->type ? su->type->tp_name : "NULL");
3706}
3707
3708static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003709super_getattro(PyObject *self, PyObject *name)
3710{
3711 superobject *su = (superobject *)self;
3712
3713 if (su->obj != NULL) {
3714 PyObject *mro, *res, *tmp;
3715 descrgetfunc f;
3716 int i, n;
3717
Guido van Rossume705ef12001-08-29 15:47:06 +00003718 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003719 if (mro == NULL)
3720 n = 0;
3721 else {
3722 assert(PyTuple_Check(mro));
3723 n = PyTuple_GET_SIZE(mro);
3724 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003725 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003726 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003727 break;
3728 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003729 if (i >= n && PyType_Check(su->obj)) {
3730 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003731 if (mro == NULL)
3732 n = 0;
3733 else {
3734 assert(PyTuple_Check(mro));
3735 n = PyTuple_GET_SIZE(mro);
3736 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003737 for (i = 0; i < n; i++) {
3738 if ((PyObject *)(su->type) ==
3739 PyTuple_GET_ITEM(mro, i))
3740 break;
3741 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003742 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003743 i++;
3744 res = NULL;
3745 for (; i < n; i++) {
3746 tmp = PyTuple_GET_ITEM(mro, i);
3747 assert(PyType_Check(tmp));
3748 res = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003749 ((PyTypeObject *)tmp)->tp_dict, name);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003750 if (res != NULL) {
3751 Py_INCREF(res);
3752 f = res->ob_type->tp_descr_get;
3753 if (f != NULL) {
3754 tmp = f(res, su->obj, res);
3755 Py_DECREF(res);
3756 res = tmp;
3757 }
3758 return res;
3759 }
3760 }
3761 }
3762 return PyObject_GenericGetAttr(self, name);
3763}
3764
3765static PyObject *
3766super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3767{
3768 superobject *su = (superobject *)self;
3769 superobject *new;
3770
3771 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3772 /* Not binding to an object, or already bound */
3773 Py_INCREF(self);
3774 return self;
3775 }
3776 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3777 if (new == NULL)
3778 return NULL;
3779 Py_INCREF(su->type);
3780 Py_INCREF(obj);
3781 new->type = su->type;
3782 new->obj = obj;
3783 return (PyObject *)new;
3784}
3785
3786static int
3787super_init(PyObject *self, PyObject *args, PyObject *kwds)
3788{
3789 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003790 PyTypeObject *type;
3791 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003792
3793 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3794 return -1;
3795 if (obj == Py_None)
3796 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003797 if (obj != NULL &&
3798 !PyType_IsSubtype(obj->ob_type, type) &&
3799 !(PyType_Check(obj) &&
3800 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003801 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003802 "super(type, obj): "
3803 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003804 return -1;
3805 }
3806 Py_INCREF(type);
3807 Py_XINCREF(obj);
3808 su->type = type;
3809 su->obj = obj;
3810 return 0;
3811}
3812
3813static char super_doc[] =
3814"super(type) -> unbound super object\n"
3815"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003816"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003817"Typical use to call a cooperative superclass method:\n"
3818"class C(B):\n"
3819" def meth(self, arg):\n"
3820" super(C, self).meth(arg)";
3821
Guido van Rossum048eb752001-10-02 21:24:57 +00003822static int
3823super_traverse(PyObject *self, visitproc visit, void *arg)
3824{
3825 superobject *su = (superobject *)self;
3826 int err;
3827
3828#define VISIT(SLOT) \
3829 if (SLOT) { \
3830 err = visit((PyObject *)(SLOT), arg); \
3831 if (err) \
3832 return err; \
3833 }
3834
3835 VISIT(su->obj);
3836 VISIT(su->type);
3837
3838#undef VISIT
3839
3840 return 0;
3841}
3842
Guido van Rossum705f0f52001-08-24 16:47:00 +00003843PyTypeObject PySuper_Type = {
3844 PyObject_HEAD_INIT(&PyType_Type)
3845 0, /* ob_size */
3846 "super", /* tp_name */
3847 sizeof(superobject), /* tp_basicsize */
3848 0, /* tp_itemsize */
3849 /* methods */
3850 super_dealloc, /* tp_dealloc */
3851 0, /* tp_print */
3852 0, /* tp_getattr */
3853 0, /* tp_setattr */
3854 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003855 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003856 0, /* tp_as_number */
3857 0, /* tp_as_sequence */
3858 0, /* tp_as_mapping */
3859 0, /* tp_hash */
3860 0, /* tp_call */
3861 0, /* tp_str */
3862 super_getattro, /* tp_getattro */
3863 0, /* tp_setattro */
3864 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003865 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3866 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003867 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003868 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003869 0, /* tp_clear */
3870 0, /* tp_richcompare */
3871 0, /* tp_weaklistoffset */
3872 0, /* tp_iter */
3873 0, /* tp_iternext */
3874 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003875 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003876 0, /* tp_getset */
3877 0, /* tp_base */
3878 0, /* tp_dict */
3879 super_descr_get, /* tp_descr_get */
3880 0, /* tp_descr_set */
3881 0, /* tp_dictoffset */
3882 super_init, /* tp_init */
3883 PyType_GenericAlloc, /* tp_alloc */
3884 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003885 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003886};