blob: ba2834a08bfbca82b6e1ea6a8ae3b2d52068f09a [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
Guido van Rossum6661be32001-10-26 04:26:12 +0000677static int
678subtype_setdict(PyObject *obj, PyObject *value, void *context)
679{
680 PyObject **dictptr = _PyObject_GetDictPtr(obj);
681 PyObject *dict;
682
683 if (dictptr == NULL) {
684 PyErr_SetString(PyExc_AttributeError,
685 "This object has no __dict__");
686 return -1;
687 }
688 if (value == NULL || !PyDict_Check(value)) {
689 PyErr_SetString(PyExc_TypeError,
690 "__dict__ must be set to a dictionary");
691 return -1;
692 }
693 dict = *dictptr;
694 Py_INCREF(value);
695 *dictptr = value;
696 Py_XDECREF(dict);
697 return 0;
698}
699
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000700static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000701 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000702 {0},
703};
704
705static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000706type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
707{
708 PyObject *name, *bases, *dict;
709 static char *kwlist[] = {"name", "bases", "dict", 0};
710 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000711 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000713 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000714 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000716 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717 if (metatype == &PyType_Type &&
718 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
719 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720 PyObject *x = PyTuple_GET_ITEM(args, 0);
721 Py_INCREF(x->ob_type);
722 return (PyObject *) x->ob_type;
723 }
724
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000725 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
727 &name,
728 &PyTuple_Type, &bases,
729 &PyDict_Type, &dict))
730 return NULL;
731
732 /* Determine the proper metatype to deal with this,
733 and check for metatype conflicts while we're at it.
734 Note that if some other metatype wins to contract,
735 it's possible that its instances are not types. */
736 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000737 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738 for (i = 0; i < nbases; i++) {
739 tmp = PyTuple_GET_ITEM(bases, i);
740 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000741 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000742 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000743 if (PyType_IsSubtype(tmptype, winner)) {
744 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000745 continue;
746 }
747 PyErr_SetString(PyExc_TypeError,
748 "metatype conflict among bases");
749 return NULL;
750 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000751 if (winner != metatype) {
752 if (winner->tp_new != type_new) /* Pass it to the winner */
753 return winner->tp_new(winner, args, kwds);
754 metatype = winner;
755 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756
757 /* Adjust for empty tuple bases */
758 if (nbases == 0) {
759 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
760 if (bases == NULL)
761 return NULL;
762 nbases = 1;
763 }
764 else
765 Py_INCREF(bases);
766
767 /* XXX From here until type is allocated, "return NULL" leaks bases! */
768
769 /* Calculate best base, and check that all bases are type objects */
770 base = best_base(bases);
771 if (base == NULL)
772 return NULL;
773 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
774 PyErr_Format(PyExc_TypeError,
775 "type '%.100s' is not an acceptable base type",
776 base->tp_name);
777 return NULL;
778 }
779
Tim Peters6d6c1a32001-08-02 04:15:00 +0000780 /* Check for a __slots__ sequence variable in dict, and count it */
781 slots = PyDict_GetItemString(dict, "__slots__");
782 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000783 add_dict = 0;
784 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785 if (slots != NULL) {
786 /* Make it into a tuple */
787 if (PyString_Check(slots))
788 slots = Py_BuildValue("(O)", slots);
789 else
790 slots = PySequence_Tuple(slots);
791 if (slots == NULL)
792 return NULL;
793 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000794 if (nslots > 0 && base->tp_itemsize != 0) {
795 PyErr_Format(PyExc_TypeError,
796 "nonempty __slots__ "
797 "not supported for subtype of '%s'",
798 base->tp_name);
799 return NULL;
800 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801 for (i = 0; i < nslots; i++) {
802 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
803 PyErr_SetString(PyExc_TypeError,
804 "__slots__ must be a sequence of strings");
805 Py_DECREF(slots);
806 return NULL;
807 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000808 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809 }
810 }
811 if (slots == NULL && base->tp_dictoffset == 0 &&
812 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000813 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000814 add_dict++;
815 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000816 if (slots == NULL && base->tp_weaklistoffset == 0 &&
817 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000818 nslots++;
819 add_weak++;
820 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000821
822 /* XXX From here until type is safely allocated,
823 "return NULL" may leak slots! */
824
825 /* Allocate the type object */
826 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
827 if (type == NULL)
828 return NULL;
829
830 /* Keep name and slots alive in the extended type object */
831 et = (etype *)type;
832 Py_INCREF(name);
833 et->name = name;
834 et->slots = slots;
835
Guido van Rossumdc91b992001-08-08 22:26:22 +0000836 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000837 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
838 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +0000839 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
840 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000841
842 /* It's a new-style number unless it specifically inherits any
843 old-style numeric behavior */
844 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
845 (base->tp_as_number == NULL))
846 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
847
848 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000849 type->tp_as_number = &et->as_number;
850 type->tp_as_sequence = &et->as_sequence;
851 type->tp_as_mapping = &et->as_mapping;
852 type->tp_as_buffer = &et->as_buffer;
853 type->tp_name = PyString_AS_STRING(name);
854
855 /* Set tp_base and tp_bases */
856 type->tp_bases = bases;
857 Py_INCREF(base);
858 type->tp_base = base;
859
Guido van Rossum687ae002001-10-15 22:03:32 +0000860 /* Initialize tp_dict from passed-in dict */
861 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000862 if (dict == NULL) {
863 Py_DECREF(type);
864 return NULL;
865 }
866
Guido van Rossumc3542212001-08-16 09:18:56 +0000867 /* Set __module__ in the dict */
868 if (PyDict_GetItemString(dict, "__module__") == NULL) {
869 tmp = PyEval_GetGlobals();
870 if (tmp != NULL) {
871 tmp = PyDict_GetItemString(tmp, "__name__");
872 if (tmp != NULL) {
873 if (PyDict_SetItemString(dict, "__module__",
874 tmp) < 0)
875 return NULL;
876 }
877 }
878 }
879
Tim Peters2f93e282001-10-04 05:27:00 +0000880 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
881 and is a string (tp_doc is a char* -- can't copy a general object
882 into it).
883 XXX What if it's a Unicode string? Don't know -- this ignores it.
884 */
885 {
886 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
887 if (doc != NULL && PyString_Check(doc)) {
888 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +0000889 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +0000890 if (type->tp_doc == NULL) {
891 Py_DECREF(type);
892 return NULL;
893 }
894 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
895 }
896 }
897
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898 /* Special-case __new__: if it's a plain function,
899 make it a static function */
900 tmp = PyDict_GetItemString(dict, "__new__");
901 if (tmp != NULL && PyFunction_Check(tmp)) {
902 tmp = PyStaticMethod_New(tmp);
903 if (tmp == NULL) {
904 Py_DECREF(type);
905 return NULL;
906 }
907 PyDict_SetItemString(dict, "__new__", tmp);
908 Py_DECREF(tmp);
909 }
910
911 /* Add descriptors for custom slots from __slots__, or for __dict__ */
912 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000913 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000914 if (slots != NULL) {
915 for (i = 0; i < nslots; i++, mp++) {
916 mp->name = PyString_AS_STRING(
917 PyTuple_GET_ITEM(slots, i));
918 mp->type = T_OBJECT;
919 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000920 if (base->tp_weaklistoffset == 0 &&
921 strcmp(mp->name, "__weakref__") == 0)
922 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000923 slotoffset += sizeof(PyObject *);
924 }
925 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000926 else {
927 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000928 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +0000929 type->tp_dictoffset =
930 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000931 else
932 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000933 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000934 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000935 }
936 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000937 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000938 type->tp_weaklistoffset = slotoffset;
939 mp->name = "__weakref__";
940 mp->type = T_OBJECT;
941 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +0000942 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +0000943 mp++;
944 slotoffset += sizeof(PyObject *);
945 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946 }
947 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000948 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000949 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950
951 /* Special case some slots */
952 if (type->tp_dictoffset != 0 || nslots > 0) {
953 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
954 type->tp_getattro = PyObject_GenericGetAttr;
955 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
956 type->tp_setattro = PyObject_GenericSetAttr;
957 }
958 type->tp_dealloc = subtype_dealloc;
959
Guido van Rossum9475a232001-10-05 20:51:39 +0000960 /* Enable GC unless there are really no instance variables possible */
961 if (!(type->tp_basicsize == sizeof(PyObject) &&
962 type->tp_itemsize == 0))
963 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
964
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965 /* Always override allocation strategy to use regular heap */
966 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +0000967 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
968 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +0000969 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +0000970 type->tp_clear = base->tp_clear;
971 }
972 else
973 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000974
975 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000976 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000977 Py_DECREF(type);
978 return NULL;
979 }
980
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000981 /* Put the proper slots in place */
982 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000983
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984 return (PyObject *)type;
985}
986
987/* Internal API to look for a name through the MRO.
988 This returns a borrowed reference, and doesn't set an exception! */
989PyObject *
990_PyType_Lookup(PyTypeObject *type, PyObject *name)
991{
992 int i, n;
993 PyObject *mro, *res, *dict;
994
Guido van Rossum687ae002001-10-15 22:03:32 +0000995 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000996 mro = type->tp_mro;
997 assert(PyTuple_Check(mro));
998 n = PyTuple_GET_SIZE(mro);
999 for (i = 0; i < n; i++) {
1000 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
1001 assert(PyType_Check(type));
Guido van Rossum687ae002001-10-15 22:03:32 +00001002 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 assert(dict && PyDict_Check(dict));
1004 res = PyDict_GetItem(dict, name);
1005 if (res != NULL)
1006 return res;
1007 }
1008 return NULL;
1009}
1010
1011/* This is similar to PyObject_GenericGetAttr(),
1012 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1013static PyObject *
1014type_getattro(PyTypeObject *type, PyObject *name)
1015{
1016 PyTypeObject *metatype = type->ob_type;
1017 PyObject *descr, *res;
1018 descrgetfunc f;
1019
1020 /* Initialize this type (we'll assume the metatype is initialized) */
1021 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001022 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001023 return NULL;
1024 }
1025
1026 /* Get a descriptor from the metatype */
1027 descr = _PyType_Lookup(metatype, name);
1028 f = NULL;
1029 if (descr != NULL) {
1030 f = descr->ob_type->tp_descr_get;
1031 if (f != NULL && PyDescr_IsData(descr))
1032 return f(descr,
1033 (PyObject *)type, (PyObject *)metatype);
1034 }
1035
Guido van Rossum687ae002001-10-15 22:03:32 +00001036 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 res = _PyType_Lookup(type, name);
1038 if (res != NULL) {
1039 f = res->ob_type->tp_descr_get;
1040 if (f != NULL)
1041 return f(res, (PyObject *)NULL, (PyObject *)type);
1042 Py_INCREF(res);
1043 return res;
1044 }
1045
1046 /* Use the descriptor from the metatype */
1047 if (f != NULL) {
1048 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1049 return res;
1050 }
1051 if (descr != NULL) {
1052 Py_INCREF(descr);
1053 return descr;
1054 }
1055
1056 /* Give up */
1057 PyErr_Format(PyExc_AttributeError,
1058 "type object '%.50s' has no attribute '%.400s'",
1059 type->tp_name, PyString_AS_STRING(name));
1060 return NULL;
1061}
1062
1063static int
1064type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1065{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001066 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1067 PyErr_Format(
1068 PyExc_TypeError,
1069 "can't set attributes of built-in/extension type '%s'",
1070 type->tp_name);
1071 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001072 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001073 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1074 return -1;
1075 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076}
1077
1078static void
1079type_dealloc(PyTypeObject *type)
1080{
1081 etype *et;
1082
1083 /* Assert this is a heap-allocated type object */
1084 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001085 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001086 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001087 et = (etype *)type;
1088 Py_XDECREF(type->tp_base);
1089 Py_XDECREF(type->tp_dict);
1090 Py_XDECREF(type->tp_bases);
1091 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001092 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001093 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 Py_XDECREF(et->name);
1095 Py_XDECREF(et->slots);
1096 type->ob_type->tp_free((PyObject *)type);
1097}
1098
Guido van Rossum1c450732001-10-08 15:18:27 +00001099static PyObject *
1100type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1101{
1102 PyObject *list, *raw, *ref;
1103 int i, n;
1104
1105 list = PyList_New(0);
1106 if (list == NULL)
1107 return NULL;
1108 raw = type->tp_subclasses;
1109 if (raw == NULL)
1110 return list;
1111 assert(PyList_Check(raw));
1112 n = PyList_GET_SIZE(raw);
1113 for (i = 0; i < n; i++) {
1114 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001115 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001116 ref = PyWeakref_GET_OBJECT(ref);
1117 if (ref != Py_None) {
1118 if (PyList_Append(list, ref) < 0) {
1119 Py_DECREF(list);
1120 return NULL;
1121 }
1122 }
1123 }
1124 return list;
1125}
1126
Tim Peters6d6c1a32001-08-02 04:15:00 +00001127static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001128 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001129 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001130 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1131 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132 {0}
1133};
1134
1135static char type_doc[] =
1136"type(object) -> the object's type\n"
1137"type(name, bases, dict) -> a new type";
1138
Guido van Rossum048eb752001-10-02 21:24:57 +00001139static int
1140type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1141{
1142 etype *et;
1143 int err;
1144
1145 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1146 return 0;
1147
1148 et = (etype *)type;
1149
1150#define VISIT(SLOT) \
1151 if (SLOT) { \
1152 err = visit((PyObject *)(SLOT), arg); \
1153 if (err) \
1154 return err; \
1155 }
1156
1157 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001158 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001159 VISIT(type->tp_mro);
1160 VISIT(type->tp_bases);
1161 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001162 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001163 VISIT(et->slots);
1164
1165#undef VISIT
1166
1167 return 0;
1168}
1169
1170static int
1171type_clear(PyTypeObject *type)
1172{
1173 etype *et;
1174 PyObject *tmp;
1175
1176 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1177 return 0;
1178
1179 et = (etype *)type;
1180
1181#define CLEAR(SLOT) \
1182 if (SLOT) { \
1183 tmp = (PyObject *)(SLOT); \
1184 SLOT = NULL; \
1185 Py_DECREF(tmp); \
1186 }
1187
1188 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001189 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001190 CLEAR(type->tp_mro);
1191 CLEAR(type->tp_bases);
1192 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001193 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001194 CLEAR(et->slots);
1195
Tim Peters2f93e282001-10-04 05:27:00 +00001196 if (type->tp_doc != NULL) {
1197 PyObject_FREE(type->tp_doc);
1198 type->tp_doc = NULL;
1199 }
1200
Guido van Rossum048eb752001-10-02 21:24:57 +00001201#undef CLEAR
1202
1203 return 0;
1204}
1205
1206static int
1207type_is_gc(PyTypeObject *type)
1208{
1209 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1210}
1211
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001212PyTypeObject PyType_Type = {
1213 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214 0, /* ob_size */
1215 "type", /* tp_name */
1216 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001217 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001218 (destructor)type_dealloc, /* tp_dealloc */
1219 0, /* tp_print */
1220 0, /* tp_getattr */
1221 0, /* tp_setattr */
1222 type_compare, /* tp_compare */
1223 (reprfunc)type_repr, /* tp_repr */
1224 0, /* tp_as_number */
1225 0, /* tp_as_sequence */
1226 0, /* tp_as_mapping */
1227 (hashfunc)_Py_HashPointer, /* tp_hash */
1228 (ternaryfunc)type_call, /* tp_call */
1229 0, /* tp_str */
1230 (getattrofunc)type_getattro, /* tp_getattro */
1231 (setattrofunc)type_setattro, /* tp_setattro */
1232 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001233 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1234 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001235 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001236 (traverseproc)type_traverse, /* tp_traverse */
1237 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001239 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001240 0, /* tp_iter */
1241 0, /* tp_iternext */
1242 type_methods, /* tp_methods */
1243 type_members, /* tp_members */
1244 type_getsets, /* tp_getset */
1245 0, /* tp_base */
1246 0, /* tp_dict */
1247 0, /* tp_descr_get */
1248 0, /* tp_descr_set */
1249 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1250 0, /* tp_init */
1251 0, /* tp_alloc */
1252 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001253 _PyObject_GC_Del, /* tp_free */
1254 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001255};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001256
1257
1258/* The base type of all types (eventually)... except itself. */
1259
1260static int
1261object_init(PyObject *self, PyObject *args, PyObject *kwds)
1262{
1263 return 0;
1264}
1265
1266static void
1267object_dealloc(PyObject *self)
1268{
1269 self->ob_type->tp_free(self);
1270}
1271
Guido van Rossum8e248182001-08-12 05:17:56 +00001272static PyObject *
1273object_repr(PyObject *self)
1274{
Guido van Rossum76e69632001-08-16 18:52:43 +00001275 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001276 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001277
Guido van Rossum76e69632001-08-16 18:52:43 +00001278 type = self->ob_type;
1279 mod = type_module(type, NULL);
1280 if (mod == NULL)
1281 PyErr_Clear();
1282 else if (!PyString_Check(mod)) {
1283 Py_DECREF(mod);
1284 mod = NULL;
1285 }
1286 name = type_name(type, NULL);
1287 if (name == NULL)
1288 return NULL;
1289 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001290 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001291 PyString_AS_STRING(mod),
1292 PyString_AS_STRING(name),
1293 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001294 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001295 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001296 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001297 Py_XDECREF(mod);
1298 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001299 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001300}
1301
Guido van Rossumb8f63662001-08-15 23:57:02 +00001302static PyObject *
1303object_str(PyObject *self)
1304{
1305 unaryfunc f;
1306
1307 f = self->ob_type->tp_repr;
1308 if (f == NULL)
1309 f = object_repr;
1310 return f(self);
1311}
1312
Guido van Rossum8e248182001-08-12 05:17:56 +00001313static long
1314object_hash(PyObject *self)
1315{
1316 return _Py_HashPointer(self);
1317}
Guido van Rossum8e248182001-08-12 05:17:56 +00001318
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001319static PyObject *
1320object_get_class(PyObject *self, void *closure)
1321{
1322 Py_INCREF(self->ob_type);
1323 return (PyObject *)(self->ob_type);
1324}
1325
1326static int
1327equiv_structs(PyTypeObject *a, PyTypeObject *b)
1328{
1329 return a == b ||
1330 (a != NULL &&
1331 b != NULL &&
1332 a->tp_basicsize == b->tp_basicsize &&
1333 a->tp_itemsize == b->tp_itemsize &&
1334 a->tp_dictoffset == b->tp_dictoffset &&
1335 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1336 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1337 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1338}
1339
1340static int
1341same_slots_added(PyTypeObject *a, PyTypeObject *b)
1342{
1343 PyTypeObject *base = a->tp_base;
1344 int size;
1345
1346 if (base != b->tp_base)
1347 return 0;
1348 if (equiv_structs(a, base) && equiv_structs(b, base))
1349 return 1;
1350 size = base->tp_basicsize;
1351 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1352 size += sizeof(PyObject *);
1353 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1354 size += sizeof(PyObject *);
1355 return size == a->tp_basicsize && size == b->tp_basicsize;
1356}
1357
1358static int
1359object_set_class(PyObject *self, PyObject *value, void *closure)
1360{
1361 PyTypeObject *old = self->ob_type;
1362 PyTypeObject *new, *newbase, *oldbase;
1363
1364 if (!PyType_Check(value)) {
1365 PyErr_Format(PyExc_TypeError,
1366 "__class__ must be set to new-style class, not '%s' object",
1367 value->ob_type->tp_name);
1368 return -1;
1369 }
1370 new = (PyTypeObject *)value;
1371 newbase = new;
1372 oldbase = old;
1373 while (equiv_structs(newbase, newbase->tp_base))
1374 newbase = newbase->tp_base;
1375 while (equiv_structs(oldbase, oldbase->tp_base))
1376 oldbase = oldbase->tp_base;
1377 if (newbase != oldbase &&
1378 (newbase->tp_base != oldbase->tp_base ||
1379 !same_slots_added(newbase, oldbase))) {
1380 PyErr_Format(PyExc_TypeError,
1381 "__class__ assignment: "
1382 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001383 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001384 old->tp_name);
1385 return -1;
1386 }
1387 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1388 Py_INCREF(new);
1389 }
1390 self->ob_type = new;
1391 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1392 Py_DECREF(old);
1393 }
1394 return 0;
1395}
1396
1397static PyGetSetDef object_getsets[] = {
1398 {"__class__", object_get_class, object_set_class,
1399 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400 {0}
1401};
1402
Guido van Rossum3926a632001-09-25 16:25:58 +00001403static PyObject *
1404object_reduce(PyObject *self, PyObject *args)
1405{
1406 /* Call copy_reg._reduce(self) */
1407 static PyObject *copy_reg_str;
1408 PyObject *copy_reg, *res;
1409
1410 if (!copy_reg_str) {
1411 copy_reg_str = PyString_InternFromString("copy_reg");
1412 if (copy_reg_str == NULL)
1413 return NULL;
1414 }
1415 copy_reg = PyImport_Import(copy_reg_str);
1416 if (!copy_reg)
1417 return NULL;
1418 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1419 Py_DECREF(copy_reg);
1420 return res;
1421}
1422
1423static PyMethodDef object_methods[] = {
1424 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1425 {0}
1426};
1427
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428PyTypeObject PyBaseObject_Type = {
1429 PyObject_HEAD_INIT(&PyType_Type)
1430 0, /* ob_size */
1431 "object", /* tp_name */
1432 sizeof(PyObject), /* tp_basicsize */
1433 0, /* tp_itemsize */
1434 (destructor)object_dealloc, /* tp_dealloc */
1435 0, /* tp_print */
1436 0, /* tp_getattr */
1437 0, /* tp_setattr */
1438 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001439 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 0, /* tp_as_number */
1441 0, /* tp_as_sequence */
1442 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001443 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001444 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001445 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001446 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001447 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001448 0, /* tp_as_buffer */
1449 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1450 "The most base type", /* tp_doc */
1451 0, /* tp_traverse */
1452 0, /* tp_clear */
1453 0, /* tp_richcompare */
1454 0, /* tp_weaklistoffset */
1455 0, /* tp_iter */
1456 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001457 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001458 0, /* tp_members */
1459 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001460 0, /* tp_base */
1461 0, /* tp_dict */
1462 0, /* tp_descr_get */
1463 0, /* tp_descr_set */
1464 0, /* tp_dictoffset */
1465 object_init, /* tp_init */
1466 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001467 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001468 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001469};
1470
1471
1472/* Initialize the __dict__ in a type object */
1473
1474static int
1475add_methods(PyTypeObject *type, PyMethodDef *meth)
1476{
Guido van Rossum687ae002001-10-15 22:03:32 +00001477 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001478
1479 for (; meth->ml_name != NULL; meth++) {
1480 PyObject *descr;
1481 if (PyDict_GetItemString(dict, meth->ml_name))
1482 continue;
1483 descr = PyDescr_NewMethod(type, meth);
1484 if (descr == NULL)
1485 return -1;
1486 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1487 return -1;
1488 Py_DECREF(descr);
1489 }
1490 return 0;
1491}
1492
1493static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001494add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001495{
Guido van Rossum687ae002001-10-15 22:03:32 +00001496 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001497
1498 for (; memb->name != NULL; memb++) {
1499 PyObject *descr;
1500 if (PyDict_GetItemString(dict, memb->name))
1501 continue;
1502 descr = PyDescr_NewMember(type, memb);
1503 if (descr == NULL)
1504 return -1;
1505 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1506 return -1;
1507 Py_DECREF(descr);
1508 }
1509 return 0;
1510}
1511
1512static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001513add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001514{
Guido van Rossum687ae002001-10-15 22:03:32 +00001515 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001516
1517 for (; gsp->name != NULL; gsp++) {
1518 PyObject *descr;
1519 if (PyDict_GetItemString(dict, gsp->name))
1520 continue;
1521 descr = PyDescr_NewGetSet(type, gsp);
1522
1523 if (descr == NULL)
1524 return -1;
1525 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1526 return -1;
1527 Py_DECREF(descr);
1528 }
1529 return 0;
1530}
1531
Guido van Rossum13d52f02001-08-10 21:24:08 +00001532static void
1533inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001534{
1535 int oldsize, newsize;
1536
Guido van Rossum13d52f02001-08-10 21:24:08 +00001537 /* Special flag magic */
1538 if (!type->tp_as_buffer && base->tp_as_buffer) {
1539 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1540 type->tp_flags |=
1541 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1542 }
1543 if (!type->tp_as_sequence && base->tp_as_sequence) {
1544 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1545 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1546 }
1547 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1548 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1549 if ((!type->tp_as_number && base->tp_as_number) ||
1550 (!type->tp_as_sequence && base->tp_as_sequence)) {
1551 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1552 if (!type->tp_as_number && !type->tp_as_sequence) {
1553 type->tp_flags |= base->tp_flags &
1554 Py_TPFLAGS_HAVE_INPLACEOPS;
1555 }
1556 }
1557 /* Wow */
1558 }
1559 if (!type->tp_as_number && base->tp_as_number) {
1560 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1561 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1562 }
1563
1564 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001565 oldsize = base->tp_basicsize;
1566 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1567 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1568 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001569 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1570 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001571 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001572 if (type->tp_traverse == NULL)
1573 type->tp_traverse = base->tp_traverse;
1574 if (type->tp_clear == NULL)
1575 type->tp_clear = base->tp_clear;
1576 }
1577 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1578 if (base != &PyBaseObject_Type ||
1579 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1580 if (type->tp_new == NULL)
1581 type->tp_new = base->tp_new;
1582 }
1583 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001584 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001585
1586 /* Copy other non-function slots */
1587
1588#undef COPYVAL
1589#define COPYVAL(SLOT) \
1590 if (type->SLOT == 0) type->SLOT = base->SLOT
1591
1592 COPYVAL(tp_itemsize);
1593 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1594 COPYVAL(tp_weaklistoffset);
1595 }
1596 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1597 COPYVAL(tp_dictoffset);
1598 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001599}
1600
1601static void
1602inherit_slots(PyTypeObject *type, PyTypeObject *base)
1603{
1604 PyTypeObject *basebase;
1605
1606#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001607#undef COPYSLOT
1608#undef COPYNUM
1609#undef COPYSEQ
1610#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001611#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001612
1613#define SLOTDEFINED(SLOT) \
1614 (base->SLOT != 0 && \
1615 (basebase == NULL || base->SLOT != basebase->SLOT))
1616
Tim Peters6d6c1a32001-08-02 04:15:00 +00001617#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001618 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001619
1620#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1621#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1622#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001623#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001624
Guido van Rossum13d52f02001-08-10 21:24:08 +00001625 /* This won't inherit indirect slots (from tp_as_number etc.)
1626 if type doesn't provide the space. */
1627
1628 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1629 basebase = base->tp_base;
1630 if (basebase->tp_as_number == NULL)
1631 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001632 COPYNUM(nb_add);
1633 COPYNUM(nb_subtract);
1634 COPYNUM(nb_multiply);
1635 COPYNUM(nb_divide);
1636 COPYNUM(nb_remainder);
1637 COPYNUM(nb_divmod);
1638 COPYNUM(nb_power);
1639 COPYNUM(nb_negative);
1640 COPYNUM(nb_positive);
1641 COPYNUM(nb_absolute);
1642 COPYNUM(nb_nonzero);
1643 COPYNUM(nb_invert);
1644 COPYNUM(nb_lshift);
1645 COPYNUM(nb_rshift);
1646 COPYNUM(nb_and);
1647 COPYNUM(nb_xor);
1648 COPYNUM(nb_or);
1649 COPYNUM(nb_coerce);
1650 COPYNUM(nb_int);
1651 COPYNUM(nb_long);
1652 COPYNUM(nb_float);
1653 COPYNUM(nb_oct);
1654 COPYNUM(nb_hex);
1655 COPYNUM(nb_inplace_add);
1656 COPYNUM(nb_inplace_subtract);
1657 COPYNUM(nb_inplace_multiply);
1658 COPYNUM(nb_inplace_divide);
1659 COPYNUM(nb_inplace_remainder);
1660 COPYNUM(nb_inplace_power);
1661 COPYNUM(nb_inplace_lshift);
1662 COPYNUM(nb_inplace_rshift);
1663 COPYNUM(nb_inplace_and);
1664 COPYNUM(nb_inplace_xor);
1665 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001666 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1667 COPYNUM(nb_true_divide);
1668 COPYNUM(nb_floor_divide);
1669 COPYNUM(nb_inplace_true_divide);
1670 COPYNUM(nb_inplace_floor_divide);
1671 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001672 }
1673
Guido van Rossum13d52f02001-08-10 21:24:08 +00001674 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1675 basebase = base->tp_base;
1676 if (basebase->tp_as_sequence == NULL)
1677 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001678 COPYSEQ(sq_length);
1679 COPYSEQ(sq_concat);
1680 COPYSEQ(sq_repeat);
1681 COPYSEQ(sq_item);
1682 COPYSEQ(sq_slice);
1683 COPYSEQ(sq_ass_item);
1684 COPYSEQ(sq_ass_slice);
1685 COPYSEQ(sq_contains);
1686 COPYSEQ(sq_inplace_concat);
1687 COPYSEQ(sq_inplace_repeat);
1688 }
1689
Guido van Rossum13d52f02001-08-10 21:24:08 +00001690 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1691 basebase = base->tp_base;
1692 if (basebase->tp_as_mapping == NULL)
1693 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001694 COPYMAP(mp_length);
1695 COPYMAP(mp_subscript);
1696 COPYMAP(mp_ass_subscript);
1697 }
1698
Tim Petersfc57ccb2001-10-12 02:38:24 +00001699 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1700 basebase = base->tp_base;
1701 if (basebase->tp_as_buffer == NULL)
1702 basebase = NULL;
1703 COPYBUF(bf_getreadbuffer);
1704 COPYBUF(bf_getwritebuffer);
1705 COPYBUF(bf_getsegcount);
1706 COPYBUF(bf_getcharbuffer);
1707 }
1708
Guido van Rossum13d52f02001-08-10 21:24:08 +00001709 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001710
Tim Peters6d6c1a32001-08-02 04:15:00 +00001711 COPYSLOT(tp_dealloc);
1712 COPYSLOT(tp_print);
1713 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1714 type->tp_getattr = base->tp_getattr;
1715 type->tp_getattro = base->tp_getattro;
1716 }
1717 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1718 type->tp_setattr = base->tp_setattr;
1719 type->tp_setattro = base->tp_setattro;
1720 }
1721 /* tp_compare see tp_richcompare */
1722 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001723 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001724 COPYSLOT(tp_call);
1725 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001727 if (type->tp_compare == NULL &&
1728 type->tp_richcompare == NULL &&
1729 type->tp_hash == NULL)
1730 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001731 type->tp_compare = base->tp_compare;
1732 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001733 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001734 }
1735 }
1736 else {
1737 COPYSLOT(tp_compare);
1738 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001739 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1740 COPYSLOT(tp_iter);
1741 COPYSLOT(tp_iternext);
1742 }
1743 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1744 COPYSLOT(tp_descr_get);
1745 COPYSLOT(tp_descr_set);
1746 COPYSLOT(tp_dictoffset);
1747 COPYSLOT(tp_init);
1748 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001749 COPYSLOT(tp_free);
1750 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001751}
1752
Guido van Rossum13d52f02001-08-10 21:24:08 +00001753staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00001754staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001755
Tim Peters6d6c1a32001-08-02 04:15:00 +00001756int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001757PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001758{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001759 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001760 PyTypeObject *base;
1761 int i, n;
1762
Guido van Rossumd614f972001-08-10 17:39:49 +00001763 if (type->tp_flags & Py_TPFLAGS_READY) {
1764 assert(type->tp_dict != NULL);
1765 return 0;
1766 }
1767 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00001768
1769 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001770
1771 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1772 base = type->tp_base;
1773 if (base == NULL && type != &PyBaseObject_Type)
1774 base = type->tp_base = &PyBaseObject_Type;
1775
1776 /* Initialize tp_bases */
1777 bases = type->tp_bases;
1778 if (bases == NULL) {
1779 if (base == NULL)
1780 bases = PyTuple_New(0);
1781 else
1782 bases = Py_BuildValue("(O)", base);
1783 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001784 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001785 type->tp_bases = bases;
1786 }
1787
1788 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001789 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001790 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001791 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001792 }
1793
Guido van Rossum687ae002001-10-15 22:03:32 +00001794 /* Initialize tp_dict */
1795 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001796 if (dict == NULL) {
1797 dict = PyDict_New();
1798 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001799 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00001800 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001801 }
1802
Guido van Rossum687ae002001-10-15 22:03:32 +00001803 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001804 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001805 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001806 if (type->tp_methods != NULL) {
1807 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001808 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001809 }
1810 if (type->tp_members != NULL) {
1811 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001812 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001813 }
1814 if (type->tp_getset != NULL) {
1815 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001816 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001817 }
1818
Tim Peters6d6c1a32001-08-02 04:15:00 +00001819 /* Calculate method resolution order */
1820 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001821 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001822 }
1823
Guido van Rossum13d52f02001-08-10 21:24:08 +00001824 /* Inherit special flags from dominant base */
1825 if (type->tp_base != NULL)
1826 inherit_special(type, type->tp_base);
1827
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001829 bases = type->tp_mro;
1830 assert(bases != NULL);
1831 assert(PyTuple_Check(bases));
1832 n = PyTuple_GET_SIZE(bases);
1833 for (i = 1; i < n; i++) {
1834 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1835 assert(PyType_Check(base));
1836 inherit_slots(type, base);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001837 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001838
Guido van Rossum13d52f02001-08-10 21:24:08 +00001839 /* Some more special stuff */
1840 base = type->tp_base;
1841 if (base != NULL) {
1842 if (type->tp_as_number == NULL)
1843 type->tp_as_number = base->tp_as_number;
1844 if (type->tp_as_sequence == NULL)
1845 type->tp_as_sequence = base->tp_as_sequence;
1846 if (type->tp_as_mapping == NULL)
1847 type->tp_as_mapping = base->tp_as_mapping;
1848 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001849
Guido van Rossum1c450732001-10-08 15:18:27 +00001850 /* Link into each base class's list of subclasses */
1851 bases = type->tp_bases;
1852 n = PyTuple_GET_SIZE(bases);
1853 for (i = 0; i < n; i++) {
1854 base = (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
1855 if (add_subclass((PyTypeObject *)base, type) < 0)
1856 goto error;
1857 }
1858
Guido van Rossum13d52f02001-08-10 21:24:08 +00001859 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001860 assert(type->tp_dict != NULL);
1861 type->tp_flags =
1862 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001863 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001864
1865 error:
1866 type->tp_flags &= ~Py_TPFLAGS_READYING;
1867 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001868}
1869
Guido van Rossum1c450732001-10-08 15:18:27 +00001870static int
1871add_subclass(PyTypeObject *base, PyTypeObject *type)
1872{
1873 int i;
1874 PyObject *list, *ref, *new;
1875
1876 list = base->tp_subclasses;
1877 if (list == NULL) {
1878 base->tp_subclasses = list = PyList_New(0);
1879 if (list == NULL)
1880 return -1;
1881 }
1882 assert(PyList_Check(list));
1883 new = PyWeakref_NewRef((PyObject *)type, NULL);
1884 i = PyList_GET_SIZE(list);
1885 while (--i >= 0) {
1886 ref = PyList_GET_ITEM(list, i);
1887 assert(PyWeakref_CheckRef(ref));
1888 if (PyWeakref_GET_OBJECT(ref) == Py_None)
1889 return PyList_SetItem(list, i, new);
1890 }
1891 i = PyList_Append(list, new);
1892 Py_DECREF(new);
1893 return i;
1894}
1895
Tim Peters6d6c1a32001-08-02 04:15:00 +00001896
1897/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1898
1899/* There's a wrapper *function* for each distinct function typedef used
1900 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1901 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1902 Most tables have only one entry; the tables for binary operators have two
1903 entries, one regular and one with reversed arguments. */
1904
1905static PyObject *
1906wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1907{
1908 inquiry func = (inquiry)wrapped;
1909 int res;
1910
1911 if (!PyArg_ParseTuple(args, ""))
1912 return NULL;
1913 res = (*func)(self);
1914 if (res == -1 && PyErr_Occurred())
1915 return NULL;
1916 return PyInt_FromLong((long)res);
1917}
1918
Tim Peters6d6c1a32001-08-02 04:15:00 +00001919static PyObject *
1920wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1921{
1922 binaryfunc func = (binaryfunc)wrapped;
1923 PyObject *other;
1924
1925 if (!PyArg_ParseTuple(args, "O", &other))
1926 return NULL;
1927 return (*func)(self, other);
1928}
1929
1930static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001931wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
1932{
1933 binaryfunc func = (binaryfunc)wrapped;
1934 PyObject *other;
1935
1936 if (!PyArg_ParseTuple(args, "O", &other))
1937 return NULL;
1938 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001939 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001940 Py_INCREF(Py_NotImplemented);
1941 return Py_NotImplemented;
1942 }
1943 return (*func)(self, other);
1944}
1945
1946static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001947wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1948{
1949 binaryfunc func = (binaryfunc)wrapped;
1950 PyObject *other;
1951
1952 if (!PyArg_ParseTuple(args, "O", &other))
1953 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001954 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001955 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001956 Py_INCREF(Py_NotImplemented);
1957 return Py_NotImplemented;
1958 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001959 return (*func)(other, self);
1960}
1961
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00001962static PyObject *
1963wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
1964{
1965 coercion func = (coercion)wrapped;
1966 PyObject *other, *res;
1967 int ok;
1968
1969 if (!PyArg_ParseTuple(args, "O", &other))
1970 return NULL;
1971 ok = func(&self, &other);
1972 if (ok < 0)
1973 return NULL;
1974 if (ok > 0) {
1975 Py_INCREF(Py_NotImplemented);
1976 return Py_NotImplemented;
1977 }
1978 res = PyTuple_New(2);
1979 if (res == NULL) {
1980 Py_DECREF(self);
1981 Py_DECREF(other);
1982 return NULL;
1983 }
1984 PyTuple_SET_ITEM(res, 0, self);
1985 PyTuple_SET_ITEM(res, 1, other);
1986 return res;
1987}
1988
Tim Peters6d6c1a32001-08-02 04:15:00 +00001989static PyObject *
1990wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1991{
1992 ternaryfunc func = (ternaryfunc)wrapped;
1993 PyObject *other;
1994 PyObject *third = Py_None;
1995
1996 /* Note: This wrapper only works for __pow__() */
1997
1998 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1999 return NULL;
2000 return (*func)(self, other, third);
2001}
2002
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002003static PyObject *
2004wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2005{
2006 ternaryfunc func = (ternaryfunc)wrapped;
2007 PyObject *other;
2008 PyObject *third = Py_None;
2009
2010 /* Note: This wrapper only works for __pow__() */
2011
2012 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2013 return NULL;
2014 return (*func)(other, self, third);
2015}
2016
Tim Peters6d6c1a32001-08-02 04:15:00 +00002017static PyObject *
2018wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2019{
2020 unaryfunc func = (unaryfunc)wrapped;
2021
2022 if (!PyArg_ParseTuple(args, ""))
2023 return NULL;
2024 return (*func)(self);
2025}
2026
Tim Peters6d6c1a32001-08-02 04:15:00 +00002027static PyObject *
2028wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2029{
2030 intargfunc func = (intargfunc)wrapped;
2031 int i;
2032
2033 if (!PyArg_ParseTuple(args, "i", &i))
2034 return NULL;
2035 return (*func)(self, i);
2036}
2037
Guido van Rossum5d815f32001-08-17 21:57:47 +00002038static int
2039getindex(PyObject *self, PyObject *arg)
2040{
2041 int i;
2042
2043 i = PyInt_AsLong(arg);
2044 if (i == -1 && PyErr_Occurred())
2045 return -1;
2046 if (i < 0) {
2047 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2048 if (sq && sq->sq_length) {
2049 int n = (*sq->sq_length)(self);
2050 if (n < 0)
2051 return -1;
2052 i += n;
2053 }
2054 }
2055 return i;
2056}
2057
2058static PyObject *
2059wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2060{
2061 intargfunc func = (intargfunc)wrapped;
2062 PyObject *arg;
2063 int i;
2064
Guido van Rossumf4593e02001-10-03 12:09:30 +00002065 if (PyTuple_GET_SIZE(args) == 1) {
2066 arg = PyTuple_GET_ITEM(args, 0);
2067 i = getindex(self, arg);
2068 if (i == -1 && PyErr_Occurred())
2069 return NULL;
2070 return (*func)(self, i);
2071 }
2072 PyArg_ParseTuple(args, "O", &arg);
2073 assert(PyErr_Occurred());
2074 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002075}
2076
Tim Peters6d6c1a32001-08-02 04:15:00 +00002077static PyObject *
2078wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2079{
2080 intintargfunc func = (intintargfunc)wrapped;
2081 int i, j;
2082
2083 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2084 return NULL;
2085 return (*func)(self, i, j);
2086}
2087
Tim Peters6d6c1a32001-08-02 04:15:00 +00002088static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002089wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002090{
2091 intobjargproc func = (intobjargproc)wrapped;
2092 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002093 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002094
Guido van Rossum5d815f32001-08-17 21:57:47 +00002095 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2096 return NULL;
2097 i = getindex(self, arg);
2098 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002099 return NULL;
2100 res = (*func)(self, i, value);
2101 if (res == -1 && PyErr_Occurred())
2102 return NULL;
2103 Py_INCREF(Py_None);
2104 return Py_None;
2105}
2106
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002107static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002108wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002109{
2110 intobjargproc func = (intobjargproc)wrapped;
2111 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002112 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002113
Guido van Rossum5d815f32001-08-17 21:57:47 +00002114 if (!PyArg_ParseTuple(args, "O", &arg))
2115 return NULL;
2116 i = getindex(self, arg);
2117 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002118 return NULL;
2119 res = (*func)(self, i, NULL);
2120 if (res == -1 && PyErr_Occurred())
2121 return NULL;
2122 Py_INCREF(Py_None);
2123 return Py_None;
2124}
2125
Tim Peters6d6c1a32001-08-02 04:15:00 +00002126static PyObject *
2127wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2128{
2129 intintobjargproc func = (intintobjargproc)wrapped;
2130 int i, j, res;
2131 PyObject *value;
2132
2133 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2134 return NULL;
2135 res = (*func)(self, i, j, value);
2136 if (res == -1 && PyErr_Occurred())
2137 return NULL;
2138 Py_INCREF(Py_None);
2139 return Py_None;
2140}
2141
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002142static PyObject *
2143wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2144{
2145 intintobjargproc func = (intintobjargproc)wrapped;
2146 int i, j, res;
2147
2148 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2149 return NULL;
2150 res = (*func)(self, i, j, NULL);
2151 if (res == -1 && PyErr_Occurred())
2152 return NULL;
2153 Py_INCREF(Py_None);
2154 return Py_None;
2155}
2156
Tim Peters6d6c1a32001-08-02 04:15:00 +00002157/* XXX objobjproc is a misnomer; should be objargpred */
2158static PyObject *
2159wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2160{
2161 objobjproc func = (objobjproc)wrapped;
2162 int res;
2163 PyObject *value;
2164
2165 if (!PyArg_ParseTuple(args, "O", &value))
2166 return NULL;
2167 res = (*func)(self, value);
2168 if (res == -1 && PyErr_Occurred())
2169 return NULL;
2170 return PyInt_FromLong((long)res);
2171}
2172
Tim Peters6d6c1a32001-08-02 04:15:00 +00002173static PyObject *
2174wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2175{
2176 objobjargproc func = (objobjargproc)wrapped;
2177 int res;
2178 PyObject *key, *value;
2179
2180 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2181 return NULL;
2182 res = (*func)(self, key, value);
2183 if (res == -1 && PyErr_Occurred())
2184 return NULL;
2185 Py_INCREF(Py_None);
2186 return Py_None;
2187}
2188
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002189static PyObject *
2190wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2191{
2192 objobjargproc func = (objobjargproc)wrapped;
2193 int res;
2194 PyObject *key;
2195
2196 if (!PyArg_ParseTuple(args, "O", &key))
2197 return NULL;
2198 res = (*func)(self, key, NULL);
2199 if (res == -1 && PyErr_Occurred())
2200 return NULL;
2201 Py_INCREF(Py_None);
2202 return Py_None;
2203}
2204
Tim Peters6d6c1a32001-08-02 04:15:00 +00002205static PyObject *
2206wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2207{
2208 cmpfunc func = (cmpfunc)wrapped;
2209 int res;
2210 PyObject *other;
2211
2212 if (!PyArg_ParseTuple(args, "O", &other))
2213 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002214 if (other->ob_type->tp_compare != func &&
2215 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002216 PyErr_Format(
2217 PyExc_TypeError,
2218 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2219 self->ob_type->tp_name,
2220 self->ob_type->tp_name,
2221 other->ob_type->tp_name);
2222 return NULL;
2223 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224 res = (*func)(self, other);
2225 if (PyErr_Occurred())
2226 return NULL;
2227 return PyInt_FromLong((long)res);
2228}
2229
Tim Peters6d6c1a32001-08-02 04:15:00 +00002230static PyObject *
2231wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2232{
2233 setattrofunc func = (setattrofunc)wrapped;
2234 int res;
2235 PyObject *name, *value;
2236
2237 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2238 return NULL;
2239 res = (*func)(self, name, value);
2240 if (res < 0)
2241 return NULL;
2242 Py_INCREF(Py_None);
2243 return Py_None;
2244}
2245
2246static PyObject *
2247wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2248{
2249 setattrofunc func = (setattrofunc)wrapped;
2250 int res;
2251 PyObject *name;
2252
2253 if (!PyArg_ParseTuple(args, "O", &name))
2254 return NULL;
2255 res = (*func)(self, name, NULL);
2256 if (res < 0)
2257 return NULL;
2258 Py_INCREF(Py_None);
2259 return Py_None;
2260}
2261
Tim Peters6d6c1a32001-08-02 04:15:00 +00002262static PyObject *
2263wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2264{
2265 hashfunc func = (hashfunc)wrapped;
2266 long res;
2267
2268 if (!PyArg_ParseTuple(args, ""))
2269 return NULL;
2270 res = (*func)(self);
2271 if (res == -1 && PyErr_Occurred())
2272 return NULL;
2273 return PyInt_FromLong(res);
2274}
2275
Tim Peters6d6c1a32001-08-02 04:15:00 +00002276static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002277wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002278{
2279 ternaryfunc func = (ternaryfunc)wrapped;
2280
Guido van Rossumc8e56452001-10-22 00:43:43 +00002281 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002282}
2283
Tim Peters6d6c1a32001-08-02 04:15:00 +00002284static PyObject *
2285wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2286{
2287 richcmpfunc func = (richcmpfunc)wrapped;
2288 PyObject *other;
2289
2290 if (!PyArg_ParseTuple(args, "O", &other))
2291 return NULL;
2292 return (*func)(self, other, op);
2293}
2294
2295#undef RICHCMP_WRAPPER
2296#define RICHCMP_WRAPPER(NAME, OP) \
2297static PyObject * \
2298richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2299{ \
2300 return wrap_richcmpfunc(self, args, wrapped, OP); \
2301}
2302
Jack Jansen8e938b42001-08-08 15:29:49 +00002303RICHCMP_WRAPPER(lt, Py_LT)
2304RICHCMP_WRAPPER(le, Py_LE)
2305RICHCMP_WRAPPER(eq, Py_EQ)
2306RICHCMP_WRAPPER(ne, Py_NE)
2307RICHCMP_WRAPPER(gt, Py_GT)
2308RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002309
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310static PyObject *
2311wrap_next(PyObject *self, PyObject *args, void *wrapped)
2312{
2313 unaryfunc func = (unaryfunc)wrapped;
2314 PyObject *res;
2315
2316 if (!PyArg_ParseTuple(args, ""))
2317 return NULL;
2318 res = (*func)(self);
2319 if (res == NULL && !PyErr_Occurred())
2320 PyErr_SetNone(PyExc_StopIteration);
2321 return res;
2322}
2323
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324static PyObject *
2325wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2326{
2327 descrgetfunc func = (descrgetfunc)wrapped;
2328 PyObject *obj;
2329 PyObject *type = NULL;
2330
2331 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2332 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002333 return (*func)(self, obj, type);
2334}
2335
Tim Peters6d6c1a32001-08-02 04:15:00 +00002336static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002337wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002338{
2339 descrsetfunc func = (descrsetfunc)wrapped;
2340 PyObject *obj, *value;
2341 int ret;
2342
2343 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2344 return NULL;
2345 ret = (*func)(self, obj, value);
2346 if (ret < 0)
2347 return NULL;
2348 Py_INCREF(Py_None);
2349 return Py_None;
2350}
2351
Tim Peters6d6c1a32001-08-02 04:15:00 +00002352static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002353wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002354{
2355 initproc func = (initproc)wrapped;
2356
Guido van Rossumc8e56452001-10-22 00:43:43 +00002357 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002358 return NULL;
2359 Py_INCREF(Py_None);
2360 return Py_None;
2361}
2362
Tim Peters6d6c1a32001-08-02 04:15:00 +00002363static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002364tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002365{
Barry Warsaw60f01882001-08-22 19:24:42 +00002366 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002367 PyObject *arg0, *res;
2368
2369 if (self == NULL || !PyType_Check(self))
2370 Py_FatalError("__new__() called with non-type 'self'");
2371 type = (PyTypeObject *)self;
2372 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002373 PyErr_Format(PyExc_TypeError,
2374 "%s.__new__(): not enough arguments",
2375 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002376 return NULL;
2377 }
2378 arg0 = PyTuple_GET_ITEM(args, 0);
2379 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002380 PyErr_Format(PyExc_TypeError,
2381 "%s.__new__(X): X is not a type object (%s)",
2382 type->tp_name,
2383 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002384 return NULL;
2385 }
2386 subtype = (PyTypeObject *)arg0;
2387 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002388 PyErr_Format(PyExc_TypeError,
2389 "%s.__new__(%s): %s is not a subtype of %s",
2390 type->tp_name,
2391 subtype->tp_name,
2392 subtype->tp_name,
2393 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002394 return NULL;
2395 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002396
2397 /* Check that the use doesn't do something silly and unsafe like
2398 object.__new__(dictionary). To do this, we check that the
2399 most derived base that's not a heap type is this type. */
2400 staticbase = subtype;
2401 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2402 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002403 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002404 PyErr_Format(PyExc_TypeError,
2405 "%s.__new__(%s) is not safe, use %s.__new__()",
2406 type->tp_name,
2407 subtype->tp_name,
2408 staticbase == NULL ? "?" : staticbase->tp_name);
2409 return NULL;
2410 }
2411
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002412 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2413 if (args == NULL)
2414 return NULL;
2415 res = type->tp_new(subtype, args, kwds);
2416 Py_DECREF(args);
2417 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418}
2419
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002420static struct PyMethodDef tp_new_methoddef[] = {
2421 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2422 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002423 {0}
2424};
2425
2426static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002427add_tp_new_wrapper(PyTypeObject *type)
2428{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002429 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002430
Guido van Rossum687ae002001-10-15 22:03:32 +00002431 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002432 return 0;
2433 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002434 if (func == NULL)
2435 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002436 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002437}
2438
Guido van Rossumf040ede2001-08-07 16:40:56 +00002439/* Slot wrappers that call the corresponding __foo__ slot. See comments
2440 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002441
Guido van Rossumdc91b992001-08-08 22:26:22 +00002442#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002443static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002444FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002445{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002446 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002447 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002448}
2449
Guido van Rossumdc91b992001-08-08 22:26:22 +00002450#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002451static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002452FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002454 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002455 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002456}
2457
Guido van Rossumdc91b992001-08-08 22:26:22 +00002458
2459#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002460static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002461FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002462{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002463 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002464 int do_other = self->ob_type != other->ob_type && \
2465 other->ob_type->tp_as_number != NULL && \
2466 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002467 if (self->ob_type->tp_as_number != NULL && \
2468 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2469 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002470 if (do_other && \
2471 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2472 r = call_maybe( \
2473 other, ROPSTR, &rcache_str, "(O)", self); \
2474 if (r != Py_NotImplemented) \
2475 return r; \
2476 Py_DECREF(r); \
2477 do_other = 0; \
2478 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002479 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002480 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002481 if (r != Py_NotImplemented || \
2482 other->ob_type == self->ob_type) \
2483 return r; \
2484 Py_DECREF(r); \
2485 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002486 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002487 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002488 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002489 } \
2490 Py_INCREF(Py_NotImplemented); \
2491 return Py_NotImplemented; \
2492}
2493
2494#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2495 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2496
2497#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2498static PyObject * \
2499FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2500{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002501 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002502 return call_method(self, OPSTR, &cache_str, \
2503 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504}
2505
2506static int
2507slot_sq_length(PyObject *self)
2508{
Guido van Rossum2730b132001-08-28 18:22:14 +00002509 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002510 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002511 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002512
2513 if (res == NULL)
2514 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002515 len = (int)PyInt_AsLong(res);
2516 Py_DECREF(res);
2517 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002518}
2519
Guido van Rossumdc91b992001-08-08 22:26:22 +00002520SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2521SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002522
2523/* Super-optimized version of slot_sq_item.
2524 Other slots could do the same... */
2525static PyObject *
2526slot_sq_item(PyObject *self, int i)
2527{
2528 static PyObject *getitem_str;
2529 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2530 descrgetfunc f;
2531
2532 if (getitem_str == NULL) {
2533 getitem_str = PyString_InternFromString("__getitem__");
2534 if (getitem_str == NULL)
2535 return NULL;
2536 }
2537 func = _PyType_Lookup(self->ob_type, getitem_str);
2538 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002539 if ((f = func->ob_type->tp_descr_get) == NULL)
2540 Py_INCREF(func);
2541 else
2542 func = f(func, self, (PyObject *)(self->ob_type));
2543 ival = PyInt_FromLong(i);
2544 if (ival != NULL) {
2545 args = PyTuple_New(1);
2546 if (args != NULL) {
2547 PyTuple_SET_ITEM(args, 0, ival);
2548 retval = PyObject_Call(func, args, NULL);
2549 Py_XDECREF(args);
2550 Py_XDECREF(func);
2551 return retval;
2552 }
2553 }
2554 }
2555 else {
2556 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2557 }
2558 Py_XDECREF(args);
2559 Py_XDECREF(ival);
2560 Py_XDECREF(func);
2561 return NULL;
2562}
2563
Guido van Rossumdc91b992001-08-08 22:26:22 +00002564SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002565
2566static int
2567slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2568{
2569 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002570 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002571
2572 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002573 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002574 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002575 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002576 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002577 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002578 if (res == NULL)
2579 return -1;
2580 Py_DECREF(res);
2581 return 0;
2582}
2583
2584static int
2585slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2586{
2587 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002588 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002589
2590 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002591 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002592 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002594 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002595 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002596 if (res == NULL)
2597 return -1;
2598 Py_DECREF(res);
2599 return 0;
2600}
2601
2602static int
2603slot_sq_contains(PyObject *self, PyObject *value)
2604{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002605 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002606 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002607
Guido van Rossum55f20992001-10-01 17:18:22 +00002608 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002609
2610 if (func != NULL) {
2611 args = Py_BuildValue("(O)", value);
2612 if (args == NULL)
2613 res = NULL;
2614 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002615 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002616 Py_DECREF(args);
2617 }
2618 Py_DECREF(func);
2619 if (res == NULL)
2620 return -1;
2621 return PyObject_IsTrue(res);
2622 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002623 else if (PyErr_Occurred())
2624 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002625 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002626 return _PySequence_IterSearch(self, value,
2627 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002628 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002629}
2630
Guido van Rossumdc91b992001-08-08 22:26:22 +00002631SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2632SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002633
2634#define slot_mp_length slot_sq_length
2635
Guido van Rossumdc91b992001-08-08 22:26:22 +00002636SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002637
2638static int
2639slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2640{
2641 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002642 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002643
2644 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002645 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002646 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002647 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002648 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002649 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002650 if (res == NULL)
2651 return -1;
2652 Py_DECREF(res);
2653 return 0;
2654}
2655
Guido van Rossumdc91b992001-08-08 22:26:22 +00002656SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2657SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2658SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2659SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2660SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2661SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2662
2663staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2664
2665SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2666 nb_power, "__pow__", "__rpow__")
2667
2668static PyObject *
2669slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2670{
Guido van Rossum2730b132001-08-28 18:22:14 +00002671 static PyObject *pow_str;
2672
Guido van Rossumdc91b992001-08-08 22:26:22 +00002673 if (modulus == Py_None)
2674 return slot_nb_power_binary(self, other);
2675 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002676 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002677 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002678}
2679
2680SLOT0(slot_nb_negative, "__neg__")
2681SLOT0(slot_nb_positive, "__pos__")
2682SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002683
2684static int
2685slot_nb_nonzero(PyObject *self)
2686{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002687 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002688 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002689
Guido van Rossum55f20992001-10-01 17:18:22 +00002690 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002691 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002692 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002693 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002694 func = lookup_maybe(self, "__len__", &len_str);
2695 if (func == NULL) {
2696 if (PyErr_Occurred())
2697 return -1;
2698 else
2699 return 1;
2700 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002701 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002702 res = PyObject_CallObject(func, NULL);
2703 Py_DECREF(func);
2704 if (res == NULL)
2705 return -1;
2706 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002707}
2708
Guido van Rossumdc91b992001-08-08 22:26:22 +00002709SLOT0(slot_nb_invert, "__invert__")
2710SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2711SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2712SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2713SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2714SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002715
2716static int
2717slot_nb_coerce(PyObject **a, PyObject **b)
2718{
2719 static PyObject *coerce_str;
2720 PyObject *self = *a, *other = *b;
2721
2722 if (self->ob_type->tp_as_number != NULL &&
2723 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2724 PyObject *r;
2725 r = call_maybe(
2726 self, "__coerce__", &coerce_str, "(O)", other);
2727 if (r == NULL)
2728 return -1;
2729 if (r == Py_NotImplemented) {
2730 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002731 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002732 else {
2733 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2734 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002735 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002736 Py_DECREF(r);
2737 return -1;
2738 }
2739 *a = PyTuple_GET_ITEM(r, 0);
2740 Py_INCREF(*a);
2741 *b = PyTuple_GET_ITEM(r, 1);
2742 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002743 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002744 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002745 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002746 }
2747 if (other->ob_type->tp_as_number != NULL &&
2748 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2749 PyObject *r;
2750 r = call_maybe(
2751 other, "__coerce__", &coerce_str, "(O)", self);
2752 if (r == NULL)
2753 return -1;
2754 if (r == Py_NotImplemented) {
2755 Py_DECREF(r);
2756 return 1;
2757 }
2758 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2759 PyErr_SetString(PyExc_TypeError,
2760 "__coerce__ didn't return a 2-tuple");
2761 Py_DECREF(r);
2762 return -1;
2763 }
2764 *a = PyTuple_GET_ITEM(r, 1);
2765 Py_INCREF(*a);
2766 *b = PyTuple_GET_ITEM(r, 0);
2767 Py_INCREF(*b);
2768 Py_DECREF(r);
2769 return 0;
2770 }
2771 return 1;
2772}
2773
Guido van Rossumdc91b992001-08-08 22:26:22 +00002774SLOT0(slot_nb_int, "__int__")
2775SLOT0(slot_nb_long, "__long__")
2776SLOT0(slot_nb_float, "__float__")
2777SLOT0(slot_nb_oct, "__oct__")
2778SLOT0(slot_nb_hex, "__hex__")
2779SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2780SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2781SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2782SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2783SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2784SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2785SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2786SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2787SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2788SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2789SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2790SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2791 "__floordiv__", "__rfloordiv__")
2792SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2793SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2794SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002795
2796static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002797half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002798{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002799 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002800 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002801 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002802
Guido van Rossum60718732001-08-28 17:47:51 +00002803 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002804 if (func == NULL) {
2805 PyErr_Clear();
2806 }
2807 else {
2808 args = Py_BuildValue("(O)", other);
2809 if (args == NULL)
2810 res = NULL;
2811 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002812 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002813 Py_DECREF(args);
2814 }
2815 if (res != Py_NotImplemented) {
2816 if (res == NULL)
2817 return -2;
2818 c = PyInt_AsLong(res);
2819 Py_DECREF(res);
2820 if (c == -1 && PyErr_Occurred())
2821 return -2;
2822 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2823 }
2824 Py_DECREF(res);
2825 }
2826 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002827}
2828
Guido van Rossumab3b0342001-09-18 20:38:53 +00002829/* This slot is published for the benefit of try_3way_compare in object.c */
2830int
2831_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00002832{
2833 int c;
2834
Guido van Rossumab3b0342001-09-18 20:38:53 +00002835 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002836 c = half_compare(self, other);
2837 if (c <= 1)
2838 return c;
2839 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00002840 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002841 c = half_compare(other, self);
2842 if (c < -1)
2843 return -2;
2844 if (c <= 1)
2845 return -c;
2846 }
2847 return (void *)self < (void *)other ? -1 :
2848 (void *)self > (void *)other ? 1 : 0;
2849}
2850
2851static PyObject *
2852slot_tp_repr(PyObject *self)
2853{
2854 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002855 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002856
Guido van Rossum60718732001-08-28 17:47:51 +00002857 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002858 if (func != NULL) {
2859 res = PyEval_CallObject(func, NULL);
2860 Py_DECREF(func);
2861 return res;
2862 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002863 PyErr_Clear();
2864 return PyString_FromFormat("<%s object at %p>",
2865 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002866}
2867
2868static PyObject *
2869slot_tp_str(PyObject *self)
2870{
2871 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002872 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002873
Guido van Rossum60718732001-08-28 17:47:51 +00002874 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002875 if (func != NULL) {
2876 res = PyEval_CallObject(func, NULL);
2877 Py_DECREF(func);
2878 return res;
2879 }
2880 else {
2881 PyErr_Clear();
2882 return slot_tp_repr(self);
2883 }
2884}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002885
2886static long
2887slot_tp_hash(PyObject *self)
2888{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002889 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002890 static PyObject *hash_str, *eq_str, *cmp_str;
2891
Tim Peters6d6c1a32001-08-02 04:15:00 +00002892 long h;
2893
Guido van Rossum60718732001-08-28 17:47:51 +00002894 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002895
2896 if (func != NULL) {
2897 res = PyEval_CallObject(func, NULL);
2898 Py_DECREF(func);
2899 if (res == NULL)
2900 return -1;
2901 h = PyInt_AsLong(res);
2902 }
2903 else {
2904 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002905 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002906 if (func == NULL) {
2907 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002908 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002909 }
2910 if (func != NULL) {
2911 Py_DECREF(func);
2912 PyErr_SetString(PyExc_TypeError, "unhashable type");
2913 return -1;
2914 }
2915 PyErr_Clear();
2916 h = _Py_HashPointer((void *)self);
2917 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002918 if (h == -1 && !PyErr_Occurred())
2919 h = -2;
2920 return h;
2921}
2922
2923static PyObject *
2924slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2925{
Guido van Rossum60718732001-08-28 17:47:51 +00002926 static PyObject *call_str;
2927 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002928 PyObject *res;
2929
2930 if (meth == NULL)
2931 return NULL;
2932 res = PyObject_Call(meth, args, kwds);
2933 Py_DECREF(meth);
2934 return res;
2935}
2936
Guido van Rossum14a6f832001-10-17 13:59:09 +00002937/* There are two slot dispatch functions for tp_getattro.
2938
2939 - slot_tp_getattro() is used when __getattribute__ is overridden
2940 but no __getattr__ hook is present;
2941
2942 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
2943
2944 The code in update_slot() and fixup_slot_dispatchers() always installs
2945 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
2946 installs the simpler slot if necessary. */
2947
Tim Peters6d6c1a32001-08-02 04:15:00 +00002948static PyObject *
2949slot_tp_getattro(PyObject *self, PyObject *name)
2950{
Guido van Rossum14a6f832001-10-17 13:59:09 +00002951 static PyObject *getattribute_str = NULL;
2952 return call_method(self, "__getattribute__", &getattribute_str,
2953 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002954}
2955
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002956static PyObject *
2957slot_tp_getattr_hook(PyObject *self, PyObject *name)
2958{
2959 PyTypeObject *tp = self->ob_type;
2960 PyObject *getattr, *getattribute, *res;
2961 static PyObject *getattribute_str = NULL;
2962 static PyObject *getattr_str = NULL;
2963
2964 if (getattr_str == NULL) {
2965 getattr_str = PyString_InternFromString("__getattr__");
2966 if (getattr_str == NULL)
2967 return NULL;
2968 }
2969 if (getattribute_str == NULL) {
2970 getattribute_str =
2971 PyString_InternFromString("__getattribute__");
2972 if (getattribute_str == NULL)
2973 return NULL;
2974 }
2975 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002976 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00002977 /* No __getattr__ hook: use a simpler dispatcher */
2978 tp->tp_getattro = slot_tp_getattro;
2979 return slot_tp_getattro(self, name);
2980 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002981 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002982 if (getattribute == NULL ||
2983 (getattribute->ob_type == &PyWrapperDescr_Type &&
2984 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
2985 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002986 res = PyObject_GenericGetAttr(self, name);
2987 else
2988 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002989 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002990 PyErr_Clear();
2991 res = PyObject_CallFunction(getattr, "OO", self, name);
2992 }
2993 return res;
2994}
2995
Tim Peters6d6c1a32001-08-02 04:15:00 +00002996static int
2997slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2998{
2999 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003000 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003001
3002 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003003 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003004 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003005 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003006 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003007 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003008 if (res == NULL)
3009 return -1;
3010 Py_DECREF(res);
3011 return 0;
3012}
3013
3014/* Map rich comparison operators to their __xx__ namesakes */
3015static char *name_op[] = {
3016 "__lt__",
3017 "__le__",
3018 "__eq__",
3019 "__ne__",
3020 "__gt__",
3021 "__ge__",
3022};
3023
3024static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003025half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003026{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003027 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003028 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003029
Guido van Rossum60718732001-08-28 17:47:51 +00003030 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003031 if (func == NULL) {
3032 PyErr_Clear();
3033 Py_INCREF(Py_NotImplemented);
3034 return Py_NotImplemented;
3035 }
3036 args = Py_BuildValue("(O)", other);
3037 if (args == NULL)
3038 res = NULL;
3039 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003040 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003041 Py_DECREF(args);
3042 }
3043 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003044 return res;
3045}
3046
Guido van Rossumb8f63662001-08-15 23:57:02 +00003047/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3048static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3049
3050static PyObject *
3051slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3052{
3053 PyObject *res;
3054
3055 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3056 res = half_richcompare(self, other, op);
3057 if (res != Py_NotImplemented)
3058 return res;
3059 Py_DECREF(res);
3060 }
3061 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3062 res = half_richcompare(other, self, swapped_op[op]);
3063 if (res != Py_NotImplemented) {
3064 return res;
3065 }
3066 Py_DECREF(res);
3067 }
3068 Py_INCREF(Py_NotImplemented);
3069 return Py_NotImplemented;
3070}
3071
3072static PyObject *
3073slot_tp_iter(PyObject *self)
3074{
3075 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003076 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003077
Guido van Rossum60718732001-08-28 17:47:51 +00003078 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003079 if (func != NULL) {
3080 res = PyObject_CallObject(func, NULL);
3081 Py_DECREF(func);
3082 return res;
3083 }
3084 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003085 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003086 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003087 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003088 return NULL;
3089 }
3090 Py_DECREF(func);
3091 return PySeqIter_New(self);
3092}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003093
3094static PyObject *
3095slot_tp_iternext(PyObject *self)
3096{
Guido van Rossum2730b132001-08-28 18:22:14 +00003097 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003098 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003099}
3100
Guido van Rossum1a493502001-08-17 16:47:50 +00003101static PyObject *
3102slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3103{
3104 PyTypeObject *tp = self->ob_type;
3105 PyObject *get;
3106 static PyObject *get_str = NULL;
3107
3108 if (get_str == NULL) {
3109 get_str = PyString_InternFromString("__get__");
3110 if (get_str == NULL)
3111 return NULL;
3112 }
3113 get = _PyType_Lookup(tp, get_str);
3114 if (get == NULL) {
3115 /* Avoid further slowdowns */
3116 if (tp->tp_descr_get == slot_tp_descr_get)
3117 tp->tp_descr_get = NULL;
3118 Py_INCREF(self);
3119 return self;
3120 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003121 if (obj == NULL)
3122 obj = Py_None;
3123 if (type == NULL)
3124 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003125 return PyObject_CallFunction(get, "OOO", self, obj, type);
3126}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003127
3128static int
3129slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3130{
Guido van Rossum2c252392001-08-24 10:13:31 +00003131 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003132 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003133
3134 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003135 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003136 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003137 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003138 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003139 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003140 if (res == NULL)
3141 return -1;
3142 Py_DECREF(res);
3143 return 0;
3144}
3145
3146static int
3147slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3148{
Guido van Rossum60718732001-08-28 17:47:51 +00003149 static PyObject *init_str;
3150 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003151 PyObject *res;
3152
3153 if (meth == NULL)
3154 return -1;
3155 res = PyObject_Call(meth, args, kwds);
3156 Py_DECREF(meth);
3157 if (res == NULL)
3158 return -1;
3159 Py_DECREF(res);
3160 return 0;
3161}
3162
3163static PyObject *
3164slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3165{
3166 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3167 PyObject *newargs, *x;
3168 int i, n;
3169
3170 if (func == NULL)
3171 return NULL;
3172 assert(PyTuple_Check(args));
3173 n = PyTuple_GET_SIZE(args);
3174 newargs = PyTuple_New(n+1);
3175 if (newargs == NULL)
3176 return NULL;
3177 Py_INCREF(type);
3178 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3179 for (i = 0; i < n; i++) {
3180 x = PyTuple_GET_ITEM(args, i);
3181 Py_INCREF(x);
3182 PyTuple_SET_ITEM(newargs, i+1, x);
3183 }
3184 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003185 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003186 Py_DECREF(func);
3187 return x;
3188}
3189
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003190
3191/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3192 functions. The offsets here are relative to the 'etype' structure, which
3193 incorporates the additional structures used for numbers, sequences and
3194 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3195 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3196 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3197
Guido van Rossum6d204072001-10-21 00:44:31 +00003198typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003199
3200#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003201#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003202#undef ETSLOT
3203#undef SQSLOT
3204#undef MPSLOT
3205#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003206#undef UNSLOT
3207#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003208#undef BINSLOT
3209#undef RBINSLOT
3210
Guido van Rossum6d204072001-10-21 00:44:31 +00003211#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3212 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003213#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3214 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3215 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003216#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3217 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3218#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3219 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3220#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3221 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3222#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3223 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3224#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3225 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3226 "x." NAME "() <==> " DOC)
3227#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3228 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3229 "x." NAME "(y) <==> x" DOC "y")
3230#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3231 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3232 "x." NAME "(y) <==> x" DOC "y")
3233#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3234 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3235 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003236
3237static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003238 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3239 "x.__len__() <==> len(x)"),
3240 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3241 "x.__add__(y) <==> x+y"),
3242 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3243 "x.__mul__(n) <==> x*n"),
3244 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3245 "x.__rmul__(n) <==> n*x"),
3246 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3247 "x.__getitem__(y) <==> x[y]"),
3248 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3249 "x.__getslice__(i, j) <==> x[i:j]"),
3250 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3251 "x.__setitem__(i, y) <==> x[i]=y"),
3252 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3253 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003254 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003255 wrap_intintobjargproc,
3256 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3257 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3258 "x.__delslice__(i, j) <==> del x[i:j]"),
3259 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3260 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003261 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003262 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003263 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003264 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003265
Guido van Rossum6d204072001-10-21 00:44:31 +00003266 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3267 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003268 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003269 wrap_binaryfunc,
3270 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003271 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003272 wrap_objobjargproc,
3273 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003274 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003275 wrap_delitem,
3276 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003277
Guido van Rossum6d204072001-10-21 00:44:31 +00003278 BINSLOT("__add__", nb_add, slot_nb_add,
3279 "+"),
3280 RBINSLOT("__radd__", nb_add, slot_nb_add,
3281 "+"),
3282 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3283 "-"),
3284 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3285 "-"),
3286 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3287 "*"),
3288 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3289 "*"),
3290 BINSLOT("__div__", nb_divide, slot_nb_divide,
3291 "/"),
3292 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3293 "/"),
3294 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3295 "%"),
3296 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3297 "%"),
3298 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3299 "divmod(x, y)"),
3300 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3301 "divmod(y, x)"),
3302 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3303 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3304 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3305 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3306 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3307 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3308 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3309 "abs(x)"),
3310 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3311 "x != 0"),
3312 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3313 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3314 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3315 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3316 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3317 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3318 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3319 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3320 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3321 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3322 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3323 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3324 "x.__coerce__(y) <==> coerce(x, y)"),
3325 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3326 "int(x)"),
3327 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3328 "long(x)"),
3329 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3330 "float(x)"),
3331 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3332 "oct(x)"),
3333 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3334 "hex(x)"),
3335 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3336 wrap_binaryfunc, "+"),
3337 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3338 wrap_binaryfunc, "-"),
3339 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3340 wrap_binaryfunc, "*"),
3341 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3342 wrap_binaryfunc, "/"),
3343 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3344 wrap_binaryfunc, "%"),
3345 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3346 wrap_ternaryfunc, "**"),
3347 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3348 wrap_binaryfunc, "<<"),
3349 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3350 wrap_binaryfunc, ">>"),
3351 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3352 wrap_binaryfunc, "&"),
3353 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3354 wrap_binaryfunc, "^"),
3355 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3356 wrap_binaryfunc, "|"),
3357 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3358 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3359 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3360 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3361 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3362 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3363 IBSLOT("__itruediv__", nb_inplace_true_divide,
3364 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003365
Guido van Rossum6d204072001-10-21 00:44:31 +00003366 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3367 "x.__str__() <==> str(x)"),
3368 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3369 "x.__repr__() <==> repr(x)"),
3370 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3371 "x.__cmp__(y) <==> cmp(x,y)"),
3372 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3373 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003374 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3375 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003376 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003377 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3378 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3379 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3380 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3381 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3382 "x.__setattr__('name', value) <==> x.name = value"),
3383 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3384 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3385 "x.__delattr__('name') <==> del x.name"),
3386 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3387 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3388 "x.__lt__(y) <==> x<y"),
3389 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3390 "x.__le__(y) <==> x<=y"),
3391 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3392 "x.__eq__(y) <==> x==y"),
3393 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3394 "x.__ne__(y) <==> x!=y"),
3395 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3396 "x.__gt__(y) <==> x>y"),
3397 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3398 "x.__ge__(y) <==> x>=y"),
3399 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3400 "x.__iter__() <==> iter(x)"),
3401 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3402 "x.next() -> the next value, or raise StopIteration"),
3403 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3404 "descr.__get__(obj[, type]) -> value"),
3405 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3406 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003407 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003408 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003409 "see x.__class__.__doc__ for signature",
3410 PyWrapperFlag_KEYWORDS),
3411 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003412 {NULL}
3413};
3414
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003415static void **
3416slotptr(PyTypeObject *type, int offset)
3417{
3418 char *ptr;
3419
3420 assert(offset >= 0);
3421 assert(offset < offsetof(etype, as_buffer));
3422 if (offset >= offsetof(etype, as_mapping)) {
3423 ptr = (void *)type->tp_as_mapping;
3424 offset -= offsetof(etype, as_mapping);
3425 }
3426 else if (offset >= offsetof(etype, as_sequence)) {
3427 ptr = (void *)type->tp_as_sequence;
3428 offset -= offsetof(etype, as_sequence);
3429 }
3430 else if (offset >= offsetof(etype, as_number)) {
3431 ptr = (void *)type->tp_as_number;
3432 offset -= offsetof(etype, as_number);
3433 }
3434 else {
3435 ptr = (void *)type;
3436 }
3437 if (ptr != NULL)
3438 ptr += offset;
3439 return (void **)ptr;
3440}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003441
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003442staticforward int recurse_down_subclasses(PyTypeObject *type,
3443 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003444
3445static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003446update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003447{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003448 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003449
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003450 for (pp = pp0; *pp; pp++) {
3451 slotdef *p = *pp;
3452 PyObject *descr;
3453 PyWrapperDescrObject *d;
3454 void *generic = NULL, *specific = NULL;
3455 int use_generic = 0;
3456 int offset = p->offset;
3457 void **ptr = slotptr(type, offset);
3458 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003459 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003460 do {
3461 descr = _PyType_Lookup(type, p->name_strobj);
3462 if (descr == NULL)
3463 continue;
3464 generic = p->function;
3465 if (descr->ob_type == &PyWrapperDescr_Type) {
3466 d = (PyWrapperDescrObject *)descr;
3467 if (d->d_base->wrapper == p->wrapper &&
3468 PyType_IsSubtype(type, d->d_type)) {
3469 if (specific == NULL ||
3470 specific == d->d_wrapped)
3471 specific = d->d_wrapped;
3472 else
3473 use_generic = 1;
3474 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003475 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003476 else
3477 use_generic = 1;
3478 } while ((++p)->offset == offset);
3479 if (specific && !use_generic)
3480 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003481 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003482 *ptr = generic;
3483 }
3484 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003485}
3486
3487static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003488recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003489{
3490 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003491 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003492 int i, n;
3493
3494 subclasses = type->tp_subclasses;
3495 if (subclasses == NULL)
3496 return 0;
3497 assert(PyList_Check(subclasses));
3498 n = PyList_GET_SIZE(subclasses);
3499 for (i = 0; i < n; i++) {
3500 ref = PyList_GET_ITEM(subclasses, i);
3501 assert(PyWeakref_CheckRef(ref));
3502 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3503 if (subclass == NULL)
3504 continue;
3505 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003506 /* Avoid recursing down into unaffected classes */
3507 dict = subclass->tp_dict;
3508 if (dict != NULL && PyDict_Check(dict) &&
3509 PyDict_GetItem(dict, name) != NULL)
3510 continue;
3511 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003512 return -1;
3513 }
3514 return 0;
3515}
3516
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003517static int
3518slotdef_cmp(const void *aa, const void *bb)
3519{
3520 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3521 int c = a->offset - b->offset;
3522 if (c != 0)
3523 return c;
3524 else
3525 return a - b;
3526}
3527
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003528static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003529init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003530{
3531 slotdef *p;
3532 static int initialized = 0;
3533
3534 if (initialized)
3535 return;
3536 for (p = slotdefs; p->name; p++) {
3537 p->name_strobj = PyString_InternFromString(p->name);
3538 if (!p->name_strobj)
3539 Py_FatalError("XXX ouch");
3540 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003541 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3542 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003543 initialized = 1;
3544}
3545
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003546static int
3547update_slot(PyTypeObject *type, PyObject *name)
3548{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003549 slotdef *ptrs[10];
3550 slotdef *p;
3551 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003552 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003553
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003554 init_slotdefs();
3555 pp = ptrs;
3556 for (p = slotdefs; p->name; p++) {
3557 /* XXX assume name is interned! */
3558 if (p->name_strobj == name)
3559 *pp++ = p;
3560 }
3561 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003562 for (pp = ptrs; *pp; pp++) {
3563 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003564 offset = p->offset;
3565 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003566 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003567 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003568 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003569 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003570}
3571
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003573fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003574{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003575 slotdef *p;
3576 PyObject *mro, *descr;
3577 PyTypeObject *base;
3578 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003579 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003580 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003581 void *generic, *specific;
3582 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003583
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003584 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003585 mro = type->tp_mro;
3586 assert(PyTuple_Check(mro));
3587 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003588 for (p = slotdefs; p->name; ) {
3589 offset = p->offset;
3590 ptr = slotptr(type, offset);
3591 if (!ptr) {
3592 do {
3593 ++p;
3594 } while (p->offset == offset);
3595 continue;
3596 }
3597 generic = specific = NULL;
3598 use_generic = 0;
3599 do {
3600 descr = NULL;
3601 for (i = 0; i < n; i++) {
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003602 base = (PyTypeObject *)
3603 PyTuple_GET_ITEM(mro, i);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003604 assert(PyType_Check(base));
3605 descr = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003606 base->tp_dict, p->name_strobj);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003607 if (descr != NULL)
3608 break;
3609 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003610 if (descr == NULL)
3611 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003612 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003613 if (descr->ob_type == &PyWrapperDescr_Type) {
3614 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003615 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003616 PyType_IsSubtype(type, d->d_type))
3617 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003618 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003619 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003620 specific = d->d_wrapped;
3621 else
3622 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003623 }
3624 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003625 else
3626 use_generic = 1;
3627 } while ((++p)->offset == offset);
3628 if (specific && !use_generic)
3629 *ptr = specific;
3630 else
3631 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003632 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003633}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003634
Guido van Rossum6d204072001-10-21 00:44:31 +00003635/* This function is called by PyType_Ready() to populate the type's
3636 dictionary with method descriptors for function slots. For each
3637 function slot (like tp_repr) that's defined in the type, one or
3638 more corresponding descriptors are added in the type's tp_dict
3639 dictionary under the appropriate name (like __repr__). Some
3640 function slots cause more than one descriptor to be added (for
3641 example, the nb_add slot adds both __add__ and __radd__
3642 descriptors) and some function slots compete for the same
3643 descriptor (for example both sq_item and mp_subscript generate a
3644 __getitem__ descriptor). This only adds new descriptors and
3645 doesn't overwrite entries in tp_dict that were previously
3646 defined. The descriptors contain a reference to the C function
3647 they must call, so that it's safe if they are copied into a
3648 subtype's __dict__ and the subtype has a different C function in
3649 its slot -- calling the method defined by the descriptor will call
3650 the C function that was used to create it, rather than the C
3651 function present in the slot when it is called. (This is important
3652 because a subtype may have a C function in the slot that calls the
3653 method from the dictionary, and we want to avoid infinite recursion
3654 here.) */
3655
3656static int
3657add_operators(PyTypeObject *type)
3658{
3659 PyObject *dict = type->tp_dict;
3660 slotdef *p;
3661 PyObject *descr;
3662 void **ptr;
3663
3664 init_slotdefs();
3665 for (p = slotdefs; p->name; p++) {
3666 if (p->wrapper == NULL)
3667 continue;
3668 ptr = slotptr(type, p->offset);
3669 if (!ptr || !*ptr)
3670 continue;
3671 if (PyDict_GetItem(dict, p->name_strobj))
3672 continue;
3673 descr = PyDescr_NewWrapper(type, p, *ptr);
3674 if (descr == NULL)
3675 return -1;
3676 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3677 return -1;
3678 Py_DECREF(descr);
3679 }
3680 if (type->tp_new != NULL) {
3681 if (add_tp_new_wrapper(type) < 0)
3682 return -1;
3683 }
3684 return 0;
3685}
3686
Guido van Rossum705f0f52001-08-24 16:47:00 +00003687
3688/* Cooperative 'super' */
3689
3690typedef struct {
3691 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003692 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003693 PyObject *obj;
3694} superobject;
3695
Guido van Rossum6f799372001-09-20 20:46:19 +00003696static PyMemberDef super_members[] = {
3697 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3698 "the class invoking super()"},
3699 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3700 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003701 {0}
3702};
3703
Guido van Rossum705f0f52001-08-24 16:47:00 +00003704static void
3705super_dealloc(PyObject *self)
3706{
3707 superobject *su = (superobject *)self;
3708
Guido van Rossum048eb752001-10-02 21:24:57 +00003709 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003710 Py_XDECREF(su->obj);
3711 Py_XDECREF(su->type);
3712 self->ob_type->tp_free(self);
3713}
3714
3715static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003716super_repr(PyObject *self)
3717{
3718 superobject *su = (superobject *)self;
3719
3720 if (su->obj)
3721 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003722 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003723 su->type ? su->type->tp_name : "NULL",
3724 su->obj->ob_type->tp_name);
3725 else
3726 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003727 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003728 su->type ? su->type->tp_name : "NULL");
3729}
3730
3731static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003732super_getattro(PyObject *self, PyObject *name)
3733{
3734 superobject *su = (superobject *)self;
3735
3736 if (su->obj != NULL) {
3737 PyObject *mro, *res, *tmp;
3738 descrgetfunc f;
3739 int i, n;
3740
Guido van Rossume705ef12001-08-29 15:47:06 +00003741 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003742 if (mro == NULL)
3743 n = 0;
3744 else {
3745 assert(PyTuple_Check(mro));
3746 n = PyTuple_GET_SIZE(mro);
3747 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003748 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003749 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003750 break;
3751 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003752 if (i >= n && PyType_Check(su->obj)) {
3753 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003754 if (mro == NULL)
3755 n = 0;
3756 else {
3757 assert(PyTuple_Check(mro));
3758 n = PyTuple_GET_SIZE(mro);
3759 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003760 for (i = 0; i < n; i++) {
3761 if ((PyObject *)(su->type) ==
3762 PyTuple_GET_ITEM(mro, i))
3763 break;
3764 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003765 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003766 i++;
3767 res = NULL;
3768 for (; i < n; i++) {
3769 tmp = PyTuple_GET_ITEM(mro, i);
3770 assert(PyType_Check(tmp));
3771 res = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003772 ((PyTypeObject *)tmp)->tp_dict, name);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003773 if (res != NULL) {
3774 Py_INCREF(res);
3775 f = res->ob_type->tp_descr_get;
3776 if (f != NULL) {
3777 tmp = f(res, su->obj, res);
3778 Py_DECREF(res);
3779 res = tmp;
3780 }
3781 return res;
3782 }
3783 }
3784 }
3785 return PyObject_GenericGetAttr(self, name);
3786}
3787
3788static PyObject *
3789super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3790{
3791 superobject *su = (superobject *)self;
3792 superobject *new;
3793
3794 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3795 /* Not binding to an object, or already bound */
3796 Py_INCREF(self);
3797 return self;
3798 }
3799 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3800 if (new == NULL)
3801 return NULL;
3802 Py_INCREF(su->type);
3803 Py_INCREF(obj);
3804 new->type = su->type;
3805 new->obj = obj;
3806 return (PyObject *)new;
3807}
3808
3809static int
3810super_init(PyObject *self, PyObject *args, PyObject *kwds)
3811{
3812 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003813 PyTypeObject *type;
3814 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003815
3816 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3817 return -1;
3818 if (obj == Py_None)
3819 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003820 if (obj != NULL &&
3821 !PyType_IsSubtype(obj->ob_type, type) &&
3822 !(PyType_Check(obj) &&
3823 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003824 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003825 "super(type, obj): "
3826 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003827 return -1;
3828 }
3829 Py_INCREF(type);
3830 Py_XINCREF(obj);
3831 su->type = type;
3832 su->obj = obj;
3833 return 0;
3834}
3835
3836static char super_doc[] =
3837"super(type) -> unbound super object\n"
3838"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003839"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003840"Typical use to call a cooperative superclass method:\n"
3841"class C(B):\n"
3842" def meth(self, arg):\n"
3843" super(C, self).meth(arg)";
3844
Guido van Rossum048eb752001-10-02 21:24:57 +00003845static int
3846super_traverse(PyObject *self, visitproc visit, void *arg)
3847{
3848 superobject *su = (superobject *)self;
3849 int err;
3850
3851#define VISIT(SLOT) \
3852 if (SLOT) { \
3853 err = visit((PyObject *)(SLOT), arg); \
3854 if (err) \
3855 return err; \
3856 }
3857
3858 VISIT(su->obj);
3859 VISIT(su->type);
3860
3861#undef VISIT
3862
3863 return 0;
3864}
3865
Guido van Rossum705f0f52001-08-24 16:47:00 +00003866PyTypeObject PySuper_Type = {
3867 PyObject_HEAD_INIT(&PyType_Type)
3868 0, /* ob_size */
3869 "super", /* tp_name */
3870 sizeof(superobject), /* tp_basicsize */
3871 0, /* tp_itemsize */
3872 /* methods */
3873 super_dealloc, /* tp_dealloc */
3874 0, /* tp_print */
3875 0, /* tp_getattr */
3876 0, /* tp_setattr */
3877 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003878 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003879 0, /* tp_as_number */
3880 0, /* tp_as_sequence */
3881 0, /* tp_as_mapping */
3882 0, /* tp_hash */
3883 0, /* tp_call */
3884 0, /* tp_str */
3885 super_getattro, /* tp_getattro */
3886 0, /* tp_setattro */
3887 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003888 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3889 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003890 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003891 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003892 0, /* tp_clear */
3893 0, /* tp_richcompare */
3894 0, /* tp_weaklistoffset */
3895 0, /* tp_iter */
3896 0, /* tp_iternext */
3897 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003898 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003899 0, /* tp_getset */
3900 0, /* tp_base */
3901 0, /* tp_dict */
3902 super_descr_get, /* tp_descr_get */
3903 0, /* tp_descr_set */
3904 0, /* tp_dictoffset */
3905 super_init, /* tp_init */
3906 PyType_GenericAlloc, /* tp_alloc */
3907 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003908 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003909};