blob: d5f6a8f0fe6e9e4d39b9a54b4c8341d5739000e0 [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
Tim Peters3abca122001-10-27 19:37:48 +0000716 assert(args != NULL && PyTuple_Check(args));
717 assert(kwds == NULL || PyDict_Check(kwds));
718
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000719 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000720 {
721 const int nargs = PyTuple_GET_SIZE(args);
722 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
723
724 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
725 PyObject *x = PyTuple_GET_ITEM(args, 0);
726 Py_INCREF(x->ob_type);
727 return (PyObject *) x->ob_type;
728 }
729
730 /* SF bug 475327 -- if that didn't trigger, we need 3
731 arguments. but PyArg_ParseTupleAndKeywords below may give
732 a msg saying type() needs exactly 3. */
733 if (nargs + nkwds != 3) {
734 PyErr_SetString(PyExc_TypeError,
735 "type() takes 1 or 3 arguments");
736 return NULL;
737 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738 }
739
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000740 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
742 &name,
743 &PyTuple_Type, &bases,
744 &PyDict_Type, &dict))
745 return NULL;
746
747 /* Determine the proper metatype to deal with this,
748 and check for metatype conflicts while we're at it.
749 Note that if some other metatype wins to contract,
750 it's possible that its instances are not types. */
751 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000752 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000753 for (i = 0; i < nbases; i++) {
754 tmp = PyTuple_GET_ITEM(bases, i);
755 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000756 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000757 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000758 if (PyType_IsSubtype(tmptype, winner)) {
759 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000760 continue;
761 }
762 PyErr_SetString(PyExc_TypeError,
763 "metatype conflict among bases");
764 return NULL;
765 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000766 if (winner != metatype) {
767 if (winner->tp_new != type_new) /* Pass it to the winner */
768 return winner->tp_new(winner, args, kwds);
769 metatype = winner;
770 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771
772 /* Adjust for empty tuple bases */
773 if (nbases == 0) {
774 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
775 if (bases == NULL)
776 return NULL;
777 nbases = 1;
778 }
779 else
780 Py_INCREF(bases);
781
782 /* XXX From here until type is allocated, "return NULL" leaks bases! */
783
784 /* Calculate best base, and check that all bases are type objects */
785 base = best_base(bases);
786 if (base == NULL)
787 return NULL;
788 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
789 PyErr_Format(PyExc_TypeError,
790 "type '%.100s' is not an acceptable base type",
791 base->tp_name);
792 return NULL;
793 }
794
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795 /* Check for a __slots__ sequence variable in dict, and count it */
796 slots = PyDict_GetItemString(dict, "__slots__");
797 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000798 add_dict = 0;
799 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000800 if (slots != NULL) {
801 /* Make it into a tuple */
802 if (PyString_Check(slots))
803 slots = Py_BuildValue("(O)", slots);
804 else
805 slots = PySequence_Tuple(slots);
806 if (slots == NULL)
807 return NULL;
808 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000809 if (nslots > 0 && base->tp_itemsize != 0) {
810 PyErr_Format(PyExc_TypeError,
811 "nonempty __slots__ "
812 "not supported for subtype of '%s'",
813 base->tp_name);
814 return NULL;
815 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000816 for (i = 0; i < nslots; i++) {
817 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
818 PyErr_SetString(PyExc_TypeError,
819 "__slots__ must be a sequence of strings");
820 Py_DECREF(slots);
821 return NULL;
822 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000823 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824 }
825 }
826 if (slots == NULL && base->tp_dictoffset == 0 &&
827 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000828 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000829 add_dict++;
830 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000831 if (slots == NULL && base->tp_weaklistoffset == 0 &&
832 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000833 nslots++;
834 add_weak++;
835 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000836
837 /* XXX From here until type is safely allocated,
838 "return NULL" may leak slots! */
839
840 /* Allocate the type object */
841 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
842 if (type == NULL)
843 return NULL;
844
845 /* Keep name and slots alive in the extended type object */
846 et = (etype *)type;
847 Py_INCREF(name);
848 et->name = name;
849 et->slots = slots;
850
Guido van Rossumdc91b992001-08-08 22:26:22 +0000851 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000852 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
853 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +0000854 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
855 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000856
857 /* It's a new-style number unless it specifically inherits any
858 old-style numeric behavior */
859 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
860 (base->tp_as_number == NULL))
861 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
862
863 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864 type->tp_as_number = &et->as_number;
865 type->tp_as_sequence = &et->as_sequence;
866 type->tp_as_mapping = &et->as_mapping;
867 type->tp_as_buffer = &et->as_buffer;
868 type->tp_name = PyString_AS_STRING(name);
869
870 /* Set tp_base and tp_bases */
871 type->tp_bases = bases;
872 Py_INCREF(base);
873 type->tp_base = base;
874
Guido van Rossum687ae002001-10-15 22:03:32 +0000875 /* Initialize tp_dict from passed-in dict */
876 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000877 if (dict == NULL) {
878 Py_DECREF(type);
879 return NULL;
880 }
881
Guido van Rossumc3542212001-08-16 09:18:56 +0000882 /* Set __module__ in the dict */
883 if (PyDict_GetItemString(dict, "__module__") == NULL) {
884 tmp = PyEval_GetGlobals();
885 if (tmp != NULL) {
886 tmp = PyDict_GetItemString(tmp, "__name__");
887 if (tmp != NULL) {
888 if (PyDict_SetItemString(dict, "__module__",
889 tmp) < 0)
890 return NULL;
891 }
892 }
893 }
894
Tim Peters2f93e282001-10-04 05:27:00 +0000895 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
896 and is a string (tp_doc is a char* -- can't copy a general object
897 into it).
898 XXX What if it's a Unicode string? Don't know -- this ignores it.
899 */
900 {
901 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
902 if (doc != NULL && PyString_Check(doc)) {
903 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +0000904 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +0000905 if (type->tp_doc == NULL) {
906 Py_DECREF(type);
907 return NULL;
908 }
909 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
910 }
911 }
912
Tim Peters6d6c1a32001-08-02 04:15:00 +0000913 /* Special-case __new__: if it's a plain function,
914 make it a static function */
915 tmp = PyDict_GetItemString(dict, "__new__");
916 if (tmp != NULL && PyFunction_Check(tmp)) {
917 tmp = PyStaticMethod_New(tmp);
918 if (tmp == NULL) {
919 Py_DECREF(type);
920 return NULL;
921 }
922 PyDict_SetItemString(dict, "__new__", tmp);
923 Py_DECREF(tmp);
924 }
925
926 /* Add descriptors for custom slots from __slots__, or for __dict__ */
927 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000928 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000929 if (slots != NULL) {
930 for (i = 0; i < nslots; i++, mp++) {
931 mp->name = PyString_AS_STRING(
932 PyTuple_GET_ITEM(slots, i));
933 mp->type = T_OBJECT;
934 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000935 if (base->tp_weaklistoffset == 0 &&
936 strcmp(mp->name, "__weakref__") == 0)
937 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938 slotoffset += sizeof(PyObject *);
939 }
940 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000941 else {
942 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000943 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +0000944 type->tp_dictoffset =
945 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000946 else
947 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000948 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000949 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000950 }
951 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000952 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000953 type->tp_weaklistoffset = slotoffset;
954 mp->name = "__weakref__";
955 mp->type = T_OBJECT;
956 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +0000957 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +0000958 mp++;
959 slotoffset += sizeof(PyObject *);
960 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 }
962 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000963 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000964 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965
966 /* Special case some slots */
967 if (type->tp_dictoffset != 0 || nslots > 0) {
968 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
969 type->tp_getattro = PyObject_GenericGetAttr;
970 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
971 type->tp_setattro = PyObject_GenericSetAttr;
972 }
973 type->tp_dealloc = subtype_dealloc;
974
Guido van Rossum9475a232001-10-05 20:51:39 +0000975 /* Enable GC unless there are really no instance variables possible */
976 if (!(type->tp_basicsize == sizeof(PyObject) &&
977 type->tp_itemsize == 0))
978 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
979
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980 /* Always override allocation strategy to use regular heap */
981 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +0000982 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
983 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +0000984 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +0000985 type->tp_clear = base->tp_clear;
986 }
987 else
988 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989
990 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000991 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992 Py_DECREF(type);
993 return NULL;
994 }
995
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000996 /* Put the proper slots in place */
997 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000998
Tim Peters6d6c1a32001-08-02 04:15:00 +0000999 return (PyObject *)type;
1000}
1001
1002/* Internal API to look for a name through the MRO.
1003 This returns a borrowed reference, and doesn't set an exception! */
1004PyObject *
1005_PyType_Lookup(PyTypeObject *type, PyObject *name)
1006{
1007 int i, n;
1008 PyObject *mro, *res, *dict;
1009
Guido van Rossum687ae002001-10-15 22:03:32 +00001010 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011 mro = type->tp_mro;
1012 assert(PyTuple_Check(mro));
1013 n = PyTuple_GET_SIZE(mro);
1014 for (i = 0; i < n; i++) {
1015 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
1016 assert(PyType_Check(type));
Guido van Rossum687ae002001-10-15 22:03:32 +00001017 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001018 assert(dict && PyDict_Check(dict));
1019 res = PyDict_GetItem(dict, name);
1020 if (res != NULL)
1021 return res;
1022 }
1023 return NULL;
1024}
1025
1026/* This is similar to PyObject_GenericGetAttr(),
1027 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1028static PyObject *
1029type_getattro(PyTypeObject *type, PyObject *name)
1030{
1031 PyTypeObject *metatype = type->ob_type;
1032 PyObject *descr, *res;
1033 descrgetfunc f;
1034
1035 /* Initialize this type (we'll assume the metatype is initialized) */
1036 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001037 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001038 return NULL;
1039 }
1040
1041 /* Get a descriptor from the metatype */
1042 descr = _PyType_Lookup(metatype, name);
1043 f = NULL;
1044 if (descr != NULL) {
1045 f = descr->ob_type->tp_descr_get;
1046 if (f != NULL && PyDescr_IsData(descr))
1047 return f(descr,
1048 (PyObject *)type, (PyObject *)metatype);
1049 }
1050
Guido van Rossum687ae002001-10-15 22:03:32 +00001051 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001052 res = _PyType_Lookup(type, name);
1053 if (res != NULL) {
1054 f = res->ob_type->tp_descr_get;
1055 if (f != NULL)
1056 return f(res, (PyObject *)NULL, (PyObject *)type);
1057 Py_INCREF(res);
1058 return res;
1059 }
1060
1061 /* Use the descriptor from the metatype */
1062 if (f != NULL) {
1063 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1064 return res;
1065 }
1066 if (descr != NULL) {
1067 Py_INCREF(descr);
1068 return descr;
1069 }
1070
1071 /* Give up */
1072 PyErr_Format(PyExc_AttributeError,
1073 "type object '%.50s' has no attribute '%.400s'",
1074 type->tp_name, PyString_AS_STRING(name));
1075 return NULL;
1076}
1077
1078static int
1079type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1080{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001081 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1082 PyErr_Format(
1083 PyExc_TypeError,
1084 "can't set attributes of built-in/extension type '%s'",
1085 type->tp_name);
1086 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001087 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001088 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1089 return -1;
1090 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091}
1092
1093static void
1094type_dealloc(PyTypeObject *type)
1095{
1096 etype *et;
1097
1098 /* Assert this is a heap-allocated type object */
1099 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001100 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001101 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001102 et = (etype *)type;
1103 Py_XDECREF(type->tp_base);
1104 Py_XDECREF(type->tp_dict);
1105 Py_XDECREF(type->tp_bases);
1106 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001107 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001108 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001109 Py_XDECREF(et->name);
1110 Py_XDECREF(et->slots);
1111 type->ob_type->tp_free((PyObject *)type);
1112}
1113
Guido van Rossum1c450732001-10-08 15:18:27 +00001114static PyObject *
1115type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1116{
1117 PyObject *list, *raw, *ref;
1118 int i, n;
1119
1120 list = PyList_New(0);
1121 if (list == NULL)
1122 return NULL;
1123 raw = type->tp_subclasses;
1124 if (raw == NULL)
1125 return list;
1126 assert(PyList_Check(raw));
1127 n = PyList_GET_SIZE(raw);
1128 for (i = 0; i < n; i++) {
1129 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001130 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001131 ref = PyWeakref_GET_OBJECT(ref);
1132 if (ref != Py_None) {
1133 if (PyList_Append(list, ref) < 0) {
1134 Py_DECREF(list);
1135 return NULL;
1136 }
1137 }
1138 }
1139 return list;
1140}
1141
Tim Peters6d6c1a32001-08-02 04:15:00 +00001142static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001143 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001144 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001145 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1146 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001147 {0}
1148};
1149
1150static char type_doc[] =
1151"type(object) -> the object's type\n"
1152"type(name, bases, dict) -> a new type";
1153
Guido van Rossum048eb752001-10-02 21:24:57 +00001154static int
1155type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1156{
1157 etype *et;
1158 int err;
1159
1160 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1161 return 0;
1162
1163 et = (etype *)type;
1164
1165#define VISIT(SLOT) \
1166 if (SLOT) { \
1167 err = visit((PyObject *)(SLOT), arg); \
1168 if (err) \
1169 return err; \
1170 }
1171
1172 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001173 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001174 VISIT(type->tp_mro);
1175 VISIT(type->tp_bases);
1176 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001177 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001178 VISIT(et->slots);
1179
1180#undef VISIT
1181
1182 return 0;
1183}
1184
1185static int
1186type_clear(PyTypeObject *type)
1187{
1188 etype *et;
1189 PyObject *tmp;
1190
1191 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1192 return 0;
1193
1194 et = (etype *)type;
1195
1196#define CLEAR(SLOT) \
1197 if (SLOT) { \
1198 tmp = (PyObject *)(SLOT); \
1199 SLOT = NULL; \
1200 Py_DECREF(tmp); \
1201 }
1202
1203 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001204 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001205 CLEAR(type->tp_mro);
1206 CLEAR(type->tp_bases);
1207 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001208 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001209 CLEAR(et->slots);
1210
Tim Peters2f93e282001-10-04 05:27:00 +00001211 if (type->tp_doc != NULL) {
1212 PyObject_FREE(type->tp_doc);
1213 type->tp_doc = NULL;
1214 }
1215
Guido van Rossum048eb752001-10-02 21:24:57 +00001216#undef CLEAR
1217
1218 return 0;
1219}
1220
1221static int
1222type_is_gc(PyTypeObject *type)
1223{
1224 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1225}
1226
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001227PyTypeObject PyType_Type = {
1228 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001229 0, /* ob_size */
1230 "type", /* tp_name */
1231 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001232 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001233 (destructor)type_dealloc, /* tp_dealloc */
1234 0, /* tp_print */
1235 0, /* tp_getattr */
1236 0, /* tp_setattr */
1237 type_compare, /* tp_compare */
1238 (reprfunc)type_repr, /* tp_repr */
1239 0, /* tp_as_number */
1240 0, /* tp_as_sequence */
1241 0, /* tp_as_mapping */
1242 (hashfunc)_Py_HashPointer, /* tp_hash */
1243 (ternaryfunc)type_call, /* tp_call */
1244 0, /* tp_str */
1245 (getattrofunc)type_getattro, /* tp_getattro */
1246 (setattrofunc)type_setattro, /* tp_setattro */
1247 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001248 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1249 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001251 (traverseproc)type_traverse, /* tp_traverse */
1252 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001253 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001254 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001255 0, /* tp_iter */
1256 0, /* tp_iternext */
1257 type_methods, /* tp_methods */
1258 type_members, /* tp_members */
1259 type_getsets, /* tp_getset */
1260 0, /* tp_base */
1261 0, /* tp_dict */
1262 0, /* tp_descr_get */
1263 0, /* tp_descr_set */
1264 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1265 0, /* tp_init */
1266 0, /* tp_alloc */
1267 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001268 _PyObject_GC_Del, /* tp_free */
1269 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001270};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001271
1272
1273/* The base type of all types (eventually)... except itself. */
1274
1275static int
1276object_init(PyObject *self, PyObject *args, PyObject *kwds)
1277{
1278 return 0;
1279}
1280
1281static void
1282object_dealloc(PyObject *self)
1283{
1284 self->ob_type->tp_free(self);
1285}
1286
Guido van Rossum8e248182001-08-12 05:17:56 +00001287static PyObject *
1288object_repr(PyObject *self)
1289{
Guido van Rossum76e69632001-08-16 18:52:43 +00001290 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001291 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001292
Guido van Rossum76e69632001-08-16 18:52:43 +00001293 type = self->ob_type;
1294 mod = type_module(type, NULL);
1295 if (mod == NULL)
1296 PyErr_Clear();
1297 else if (!PyString_Check(mod)) {
1298 Py_DECREF(mod);
1299 mod = NULL;
1300 }
1301 name = type_name(type, NULL);
1302 if (name == NULL)
1303 return NULL;
1304 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001305 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001306 PyString_AS_STRING(mod),
1307 PyString_AS_STRING(name),
1308 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001309 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001310 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001311 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001312 Py_XDECREF(mod);
1313 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001314 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001315}
1316
Guido van Rossumb8f63662001-08-15 23:57:02 +00001317static PyObject *
1318object_str(PyObject *self)
1319{
1320 unaryfunc f;
1321
1322 f = self->ob_type->tp_repr;
1323 if (f == NULL)
1324 f = object_repr;
1325 return f(self);
1326}
1327
Guido van Rossum8e248182001-08-12 05:17:56 +00001328static long
1329object_hash(PyObject *self)
1330{
1331 return _Py_HashPointer(self);
1332}
Guido van Rossum8e248182001-08-12 05:17:56 +00001333
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001334static PyObject *
1335object_get_class(PyObject *self, void *closure)
1336{
1337 Py_INCREF(self->ob_type);
1338 return (PyObject *)(self->ob_type);
1339}
1340
1341static int
1342equiv_structs(PyTypeObject *a, PyTypeObject *b)
1343{
1344 return a == b ||
1345 (a != NULL &&
1346 b != NULL &&
1347 a->tp_basicsize == b->tp_basicsize &&
1348 a->tp_itemsize == b->tp_itemsize &&
1349 a->tp_dictoffset == b->tp_dictoffset &&
1350 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1351 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1352 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1353}
1354
1355static int
1356same_slots_added(PyTypeObject *a, PyTypeObject *b)
1357{
1358 PyTypeObject *base = a->tp_base;
1359 int size;
1360
1361 if (base != b->tp_base)
1362 return 0;
1363 if (equiv_structs(a, base) && equiv_structs(b, base))
1364 return 1;
1365 size = base->tp_basicsize;
1366 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1367 size += sizeof(PyObject *);
1368 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1369 size += sizeof(PyObject *);
1370 return size == a->tp_basicsize && size == b->tp_basicsize;
1371}
1372
1373static int
1374object_set_class(PyObject *self, PyObject *value, void *closure)
1375{
1376 PyTypeObject *old = self->ob_type;
1377 PyTypeObject *new, *newbase, *oldbase;
1378
1379 if (!PyType_Check(value)) {
1380 PyErr_Format(PyExc_TypeError,
1381 "__class__ must be set to new-style class, not '%s' object",
1382 value->ob_type->tp_name);
1383 return -1;
1384 }
1385 new = (PyTypeObject *)value;
1386 newbase = new;
1387 oldbase = old;
1388 while (equiv_structs(newbase, newbase->tp_base))
1389 newbase = newbase->tp_base;
1390 while (equiv_structs(oldbase, oldbase->tp_base))
1391 oldbase = oldbase->tp_base;
1392 if (newbase != oldbase &&
1393 (newbase->tp_base != oldbase->tp_base ||
1394 !same_slots_added(newbase, oldbase))) {
1395 PyErr_Format(PyExc_TypeError,
1396 "__class__ assignment: "
1397 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001398 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001399 old->tp_name);
1400 return -1;
1401 }
1402 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1403 Py_INCREF(new);
1404 }
1405 self->ob_type = new;
1406 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1407 Py_DECREF(old);
1408 }
1409 return 0;
1410}
1411
1412static PyGetSetDef object_getsets[] = {
1413 {"__class__", object_get_class, object_set_class,
1414 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001415 {0}
1416};
1417
Guido van Rossum3926a632001-09-25 16:25:58 +00001418static PyObject *
1419object_reduce(PyObject *self, PyObject *args)
1420{
1421 /* Call copy_reg._reduce(self) */
1422 static PyObject *copy_reg_str;
1423 PyObject *copy_reg, *res;
1424
1425 if (!copy_reg_str) {
1426 copy_reg_str = PyString_InternFromString("copy_reg");
1427 if (copy_reg_str == NULL)
1428 return NULL;
1429 }
1430 copy_reg = PyImport_Import(copy_reg_str);
1431 if (!copy_reg)
1432 return NULL;
1433 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1434 Py_DECREF(copy_reg);
1435 return res;
1436}
1437
1438static PyMethodDef object_methods[] = {
1439 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1440 {0}
1441};
1442
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443PyTypeObject PyBaseObject_Type = {
1444 PyObject_HEAD_INIT(&PyType_Type)
1445 0, /* ob_size */
1446 "object", /* tp_name */
1447 sizeof(PyObject), /* tp_basicsize */
1448 0, /* tp_itemsize */
1449 (destructor)object_dealloc, /* tp_dealloc */
1450 0, /* tp_print */
1451 0, /* tp_getattr */
1452 0, /* tp_setattr */
1453 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001454 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001455 0, /* tp_as_number */
1456 0, /* tp_as_sequence */
1457 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001458 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001460 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001462 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001463 0, /* tp_as_buffer */
1464 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1465 "The most base type", /* tp_doc */
1466 0, /* tp_traverse */
1467 0, /* tp_clear */
1468 0, /* tp_richcompare */
1469 0, /* tp_weaklistoffset */
1470 0, /* tp_iter */
1471 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001472 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001473 0, /* tp_members */
1474 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001475 0, /* tp_base */
1476 0, /* tp_dict */
1477 0, /* tp_descr_get */
1478 0, /* tp_descr_set */
1479 0, /* tp_dictoffset */
1480 object_init, /* tp_init */
1481 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001482 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001483 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001484};
1485
1486
1487/* Initialize the __dict__ in a type object */
1488
1489static int
1490add_methods(PyTypeObject *type, PyMethodDef *meth)
1491{
Guido van Rossum687ae002001-10-15 22:03:32 +00001492 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001493
1494 for (; meth->ml_name != NULL; meth++) {
1495 PyObject *descr;
1496 if (PyDict_GetItemString(dict, meth->ml_name))
1497 continue;
1498 descr = PyDescr_NewMethod(type, meth);
1499 if (descr == NULL)
1500 return -1;
1501 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1502 return -1;
1503 Py_DECREF(descr);
1504 }
1505 return 0;
1506}
1507
1508static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001509add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001510{
Guido van Rossum687ae002001-10-15 22:03:32 +00001511 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001512
1513 for (; memb->name != NULL; memb++) {
1514 PyObject *descr;
1515 if (PyDict_GetItemString(dict, memb->name))
1516 continue;
1517 descr = PyDescr_NewMember(type, memb);
1518 if (descr == NULL)
1519 return -1;
1520 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1521 return -1;
1522 Py_DECREF(descr);
1523 }
1524 return 0;
1525}
1526
1527static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001528add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001529{
Guido van Rossum687ae002001-10-15 22:03:32 +00001530 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001531
1532 for (; gsp->name != NULL; gsp++) {
1533 PyObject *descr;
1534 if (PyDict_GetItemString(dict, gsp->name))
1535 continue;
1536 descr = PyDescr_NewGetSet(type, gsp);
1537
1538 if (descr == NULL)
1539 return -1;
1540 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1541 return -1;
1542 Py_DECREF(descr);
1543 }
1544 return 0;
1545}
1546
Guido van Rossum13d52f02001-08-10 21:24:08 +00001547static void
1548inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001549{
1550 int oldsize, newsize;
1551
Guido van Rossum13d52f02001-08-10 21:24:08 +00001552 /* Special flag magic */
1553 if (!type->tp_as_buffer && base->tp_as_buffer) {
1554 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1555 type->tp_flags |=
1556 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1557 }
1558 if (!type->tp_as_sequence && base->tp_as_sequence) {
1559 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1560 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1561 }
1562 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1563 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1564 if ((!type->tp_as_number && base->tp_as_number) ||
1565 (!type->tp_as_sequence && base->tp_as_sequence)) {
1566 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1567 if (!type->tp_as_number && !type->tp_as_sequence) {
1568 type->tp_flags |= base->tp_flags &
1569 Py_TPFLAGS_HAVE_INPLACEOPS;
1570 }
1571 }
1572 /* Wow */
1573 }
1574 if (!type->tp_as_number && base->tp_as_number) {
1575 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1576 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1577 }
1578
1579 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001580 oldsize = base->tp_basicsize;
1581 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1582 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1583 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001584 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1585 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001586 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001587 if (type->tp_traverse == NULL)
1588 type->tp_traverse = base->tp_traverse;
1589 if (type->tp_clear == NULL)
1590 type->tp_clear = base->tp_clear;
1591 }
1592 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1593 if (base != &PyBaseObject_Type ||
1594 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1595 if (type->tp_new == NULL)
1596 type->tp_new = base->tp_new;
1597 }
1598 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001599 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001600
1601 /* Copy other non-function slots */
1602
1603#undef COPYVAL
1604#define COPYVAL(SLOT) \
1605 if (type->SLOT == 0) type->SLOT = base->SLOT
1606
1607 COPYVAL(tp_itemsize);
1608 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1609 COPYVAL(tp_weaklistoffset);
1610 }
1611 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1612 COPYVAL(tp_dictoffset);
1613 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001614}
1615
1616static void
1617inherit_slots(PyTypeObject *type, PyTypeObject *base)
1618{
1619 PyTypeObject *basebase;
1620
1621#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001622#undef COPYSLOT
1623#undef COPYNUM
1624#undef COPYSEQ
1625#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001626#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001627
1628#define SLOTDEFINED(SLOT) \
1629 (base->SLOT != 0 && \
1630 (basebase == NULL || base->SLOT != basebase->SLOT))
1631
Tim Peters6d6c1a32001-08-02 04:15:00 +00001632#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001633 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001634
1635#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1636#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1637#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001638#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001639
Guido van Rossum13d52f02001-08-10 21:24:08 +00001640 /* This won't inherit indirect slots (from tp_as_number etc.)
1641 if type doesn't provide the space. */
1642
1643 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1644 basebase = base->tp_base;
1645 if (basebase->tp_as_number == NULL)
1646 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001647 COPYNUM(nb_add);
1648 COPYNUM(nb_subtract);
1649 COPYNUM(nb_multiply);
1650 COPYNUM(nb_divide);
1651 COPYNUM(nb_remainder);
1652 COPYNUM(nb_divmod);
1653 COPYNUM(nb_power);
1654 COPYNUM(nb_negative);
1655 COPYNUM(nb_positive);
1656 COPYNUM(nb_absolute);
1657 COPYNUM(nb_nonzero);
1658 COPYNUM(nb_invert);
1659 COPYNUM(nb_lshift);
1660 COPYNUM(nb_rshift);
1661 COPYNUM(nb_and);
1662 COPYNUM(nb_xor);
1663 COPYNUM(nb_or);
1664 COPYNUM(nb_coerce);
1665 COPYNUM(nb_int);
1666 COPYNUM(nb_long);
1667 COPYNUM(nb_float);
1668 COPYNUM(nb_oct);
1669 COPYNUM(nb_hex);
1670 COPYNUM(nb_inplace_add);
1671 COPYNUM(nb_inplace_subtract);
1672 COPYNUM(nb_inplace_multiply);
1673 COPYNUM(nb_inplace_divide);
1674 COPYNUM(nb_inplace_remainder);
1675 COPYNUM(nb_inplace_power);
1676 COPYNUM(nb_inplace_lshift);
1677 COPYNUM(nb_inplace_rshift);
1678 COPYNUM(nb_inplace_and);
1679 COPYNUM(nb_inplace_xor);
1680 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001681 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1682 COPYNUM(nb_true_divide);
1683 COPYNUM(nb_floor_divide);
1684 COPYNUM(nb_inplace_true_divide);
1685 COPYNUM(nb_inplace_floor_divide);
1686 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001687 }
1688
Guido van Rossum13d52f02001-08-10 21:24:08 +00001689 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1690 basebase = base->tp_base;
1691 if (basebase->tp_as_sequence == NULL)
1692 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001693 COPYSEQ(sq_length);
1694 COPYSEQ(sq_concat);
1695 COPYSEQ(sq_repeat);
1696 COPYSEQ(sq_item);
1697 COPYSEQ(sq_slice);
1698 COPYSEQ(sq_ass_item);
1699 COPYSEQ(sq_ass_slice);
1700 COPYSEQ(sq_contains);
1701 COPYSEQ(sq_inplace_concat);
1702 COPYSEQ(sq_inplace_repeat);
1703 }
1704
Guido van Rossum13d52f02001-08-10 21:24:08 +00001705 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1706 basebase = base->tp_base;
1707 if (basebase->tp_as_mapping == NULL)
1708 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001709 COPYMAP(mp_length);
1710 COPYMAP(mp_subscript);
1711 COPYMAP(mp_ass_subscript);
1712 }
1713
Tim Petersfc57ccb2001-10-12 02:38:24 +00001714 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1715 basebase = base->tp_base;
1716 if (basebase->tp_as_buffer == NULL)
1717 basebase = NULL;
1718 COPYBUF(bf_getreadbuffer);
1719 COPYBUF(bf_getwritebuffer);
1720 COPYBUF(bf_getsegcount);
1721 COPYBUF(bf_getcharbuffer);
1722 }
1723
Guido van Rossum13d52f02001-08-10 21:24:08 +00001724 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001725
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726 COPYSLOT(tp_dealloc);
1727 COPYSLOT(tp_print);
1728 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1729 type->tp_getattr = base->tp_getattr;
1730 type->tp_getattro = base->tp_getattro;
1731 }
1732 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1733 type->tp_setattr = base->tp_setattr;
1734 type->tp_setattro = base->tp_setattro;
1735 }
1736 /* tp_compare see tp_richcompare */
1737 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001738 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001739 COPYSLOT(tp_call);
1740 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001741 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001742 if (type->tp_compare == NULL &&
1743 type->tp_richcompare == NULL &&
1744 type->tp_hash == NULL)
1745 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001746 type->tp_compare = base->tp_compare;
1747 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001748 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001749 }
1750 }
1751 else {
1752 COPYSLOT(tp_compare);
1753 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001754 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1755 COPYSLOT(tp_iter);
1756 COPYSLOT(tp_iternext);
1757 }
1758 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1759 COPYSLOT(tp_descr_get);
1760 COPYSLOT(tp_descr_set);
1761 COPYSLOT(tp_dictoffset);
1762 COPYSLOT(tp_init);
1763 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001764 COPYSLOT(tp_free);
1765 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001766}
1767
Guido van Rossum13d52f02001-08-10 21:24:08 +00001768staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00001769staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001770
Tim Peters6d6c1a32001-08-02 04:15:00 +00001771int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001772PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001773{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001774 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001775 PyTypeObject *base;
1776 int i, n;
1777
Guido van Rossumd614f972001-08-10 17:39:49 +00001778 if (type->tp_flags & Py_TPFLAGS_READY) {
1779 assert(type->tp_dict != NULL);
1780 return 0;
1781 }
1782 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00001783
1784 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001785
1786 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1787 base = type->tp_base;
1788 if (base == NULL && type != &PyBaseObject_Type)
1789 base = type->tp_base = &PyBaseObject_Type;
1790
1791 /* Initialize tp_bases */
1792 bases = type->tp_bases;
1793 if (bases == NULL) {
1794 if (base == NULL)
1795 bases = PyTuple_New(0);
1796 else
1797 bases = Py_BuildValue("(O)", base);
1798 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001799 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001800 type->tp_bases = bases;
1801 }
1802
1803 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001804 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001805 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001806 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001807 }
1808
Guido van Rossum687ae002001-10-15 22:03:32 +00001809 /* Initialize tp_dict */
1810 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001811 if (dict == NULL) {
1812 dict = PyDict_New();
1813 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001814 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00001815 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001816 }
1817
Guido van Rossum687ae002001-10-15 22:03:32 +00001818 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001819 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001820 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001821 if (type->tp_methods != NULL) {
1822 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001823 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001824 }
1825 if (type->tp_members != NULL) {
1826 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001827 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828 }
1829 if (type->tp_getset != NULL) {
1830 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001831 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001832 }
1833
Tim Peters6d6c1a32001-08-02 04:15:00 +00001834 /* Calculate method resolution order */
1835 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001836 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001837 }
1838
Guido van Rossum13d52f02001-08-10 21:24:08 +00001839 /* Inherit special flags from dominant base */
1840 if (type->tp_base != NULL)
1841 inherit_special(type, type->tp_base);
1842
Tim Peters6d6c1a32001-08-02 04:15:00 +00001843 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001844 bases = type->tp_mro;
1845 assert(bases != NULL);
1846 assert(PyTuple_Check(bases));
1847 n = PyTuple_GET_SIZE(bases);
1848 for (i = 1; i < n; i++) {
1849 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1850 assert(PyType_Check(base));
1851 inherit_slots(type, base);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001852 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853
Guido van Rossum13d52f02001-08-10 21:24:08 +00001854 /* Some more special stuff */
1855 base = type->tp_base;
1856 if (base != NULL) {
1857 if (type->tp_as_number == NULL)
1858 type->tp_as_number = base->tp_as_number;
1859 if (type->tp_as_sequence == NULL)
1860 type->tp_as_sequence = base->tp_as_sequence;
1861 if (type->tp_as_mapping == NULL)
1862 type->tp_as_mapping = base->tp_as_mapping;
1863 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001864
Guido van Rossum1c450732001-10-08 15:18:27 +00001865 /* Link into each base class's list of subclasses */
1866 bases = type->tp_bases;
1867 n = PyTuple_GET_SIZE(bases);
1868 for (i = 0; i < n; i++) {
1869 base = (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
1870 if (add_subclass((PyTypeObject *)base, type) < 0)
1871 goto error;
1872 }
1873
Guido van Rossum13d52f02001-08-10 21:24:08 +00001874 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001875 assert(type->tp_dict != NULL);
1876 type->tp_flags =
1877 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001878 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001879
1880 error:
1881 type->tp_flags &= ~Py_TPFLAGS_READYING;
1882 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883}
1884
Guido van Rossum1c450732001-10-08 15:18:27 +00001885static int
1886add_subclass(PyTypeObject *base, PyTypeObject *type)
1887{
1888 int i;
1889 PyObject *list, *ref, *new;
1890
1891 list = base->tp_subclasses;
1892 if (list == NULL) {
1893 base->tp_subclasses = list = PyList_New(0);
1894 if (list == NULL)
1895 return -1;
1896 }
1897 assert(PyList_Check(list));
1898 new = PyWeakref_NewRef((PyObject *)type, NULL);
1899 i = PyList_GET_SIZE(list);
1900 while (--i >= 0) {
1901 ref = PyList_GET_ITEM(list, i);
1902 assert(PyWeakref_CheckRef(ref));
1903 if (PyWeakref_GET_OBJECT(ref) == Py_None)
1904 return PyList_SetItem(list, i, new);
1905 }
1906 i = PyList_Append(list, new);
1907 Py_DECREF(new);
1908 return i;
1909}
1910
Tim Peters6d6c1a32001-08-02 04:15:00 +00001911
1912/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1913
1914/* There's a wrapper *function* for each distinct function typedef used
1915 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1916 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1917 Most tables have only one entry; the tables for binary operators have two
1918 entries, one regular and one with reversed arguments. */
1919
1920static PyObject *
1921wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1922{
1923 inquiry func = (inquiry)wrapped;
1924 int res;
1925
1926 if (!PyArg_ParseTuple(args, ""))
1927 return NULL;
1928 res = (*func)(self);
1929 if (res == -1 && PyErr_Occurred())
1930 return NULL;
1931 return PyInt_FromLong((long)res);
1932}
1933
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934static PyObject *
1935wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1936{
1937 binaryfunc func = (binaryfunc)wrapped;
1938 PyObject *other;
1939
1940 if (!PyArg_ParseTuple(args, "O", &other))
1941 return NULL;
1942 return (*func)(self, other);
1943}
1944
1945static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001946wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
1947{
1948 binaryfunc func = (binaryfunc)wrapped;
1949 PyObject *other;
1950
1951 if (!PyArg_ParseTuple(args, "O", &other))
1952 return NULL;
1953 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001954 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001955 Py_INCREF(Py_NotImplemented);
1956 return Py_NotImplemented;
1957 }
1958 return (*func)(self, other);
1959}
1960
1961static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001962wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1963{
1964 binaryfunc func = (binaryfunc)wrapped;
1965 PyObject *other;
1966
1967 if (!PyArg_ParseTuple(args, "O", &other))
1968 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001969 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001970 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00001971 Py_INCREF(Py_NotImplemented);
1972 return Py_NotImplemented;
1973 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001974 return (*func)(other, self);
1975}
1976
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00001977static PyObject *
1978wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
1979{
1980 coercion func = (coercion)wrapped;
1981 PyObject *other, *res;
1982 int ok;
1983
1984 if (!PyArg_ParseTuple(args, "O", &other))
1985 return NULL;
1986 ok = func(&self, &other);
1987 if (ok < 0)
1988 return NULL;
1989 if (ok > 0) {
1990 Py_INCREF(Py_NotImplemented);
1991 return Py_NotImplemented;
1992 }
1993 res = PyTuple_New(2);
1994 if (res == NULL) {
1995 Py_DECREF(self);
1996 Py_DECREF(other);
1997 return NULL;
1998 }
1999 PyTuple_SET_ITEM(res, 0, self);
2000 PyTuple_SET_ITEM(res, 1, other);
2001 return res;
2002}
2003
Tim Peters6d6c1a32001-08-02 04:15:00 +00002004static PyObject *
2005wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2006{
2007 ternaryfunc func = (ternaryfunc)wrapped;
2008 PyObject *other;
2009 PyObject *third = Py_None;
2010
2011 /* Note: This wrapper only works for __pow__() */
2012
2013 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2014 return NULL;
2015 return (*func)(self, other, third);
2016}
2017
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002018static PyObject *
2019wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2020{
2021 ternaryfunc func = (ternaryfunc)wrapped;
2022 PyObject *other;
2023 PyObject *third = Py_None;
2024
2025 /* Note: This wrapper only works for __pow__() */
2026
2027 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2028 return NULL;
2029 return (*func)(other, self, third);
2030}
2031
Tim Peters6d6c1a32001-08-02 04:15:00 +00002032static PyObject *
2033wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2034{
2035 unaryfunc func = (unaryfunc)wrapped;
2036
2037 if (!PyArg_ParseTuple(args, ""))
2038 return NULL;
2039 return (*func)(self);
2040}
2041
Tim Peters6d6c1a32001-08-02 04:15:00 +00002042static PyObject *
2043wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2044{
2045 intargfunc func = (intargfunc)wrapped;
2046 int i;
2047
2048 if (!PyArg_ParseTuple(args, "i", &i))
2049 return NULL;
2050 return (*func)(self, i);
2051}
2052
Guido van Rossum5d815f32001-08-17 21:57:47 +00002053static int
2054getindex(PyObject *self, PyObject *arg)
2055{
2056 int i;
2057
2058 i = PyInt_AsLong(arg);
2059 if (i == -1 && PyErr_Occurred())
2060 return -1;
2061 if (i < 0) {
2062 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2063 if (sq && sq->sq_length) {
2064 int n = (*sq->sq_length)(self);
2065 if (n < 0)
2066 return -1;
2067 i += n;
2068 }
2069 }
2070 return i;
2071}
2072
2073static PyObject *
2074wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2075{
2076 intargfunc func = (intargfunc)wrapped;
2077 PyObject *arg;
2078 int i;
2079
Guido van Rossumf4593e02001-10-03 12:09:30 +00002080 if (PyTuple_GET_SIZE(args) == 1) {
2081 arg = PyTuple_GET_ITEM(args, 0);
2082 i = getindex(self, arg);
2083 if (i == -1 && PyErr_Occurred())
2084 return NULL;
2085 return (*func)(self, i);
2086 }
2087 PyArg_ParseTuple(args, "O", &arg);
2088 assert(PyErr_Occurred());
2089 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002090}
2091
Tim Peters6d6c1a32001-08-02 04:15:00 +00002092static PyObject *
2093wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2094{
2095 intintargfunc func = (intintargfunc)wrapped;
2096 int i, j;
2097
2098 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2099 return NULL;
2100 return (*func)(self, i, j);
2101}
2102
Tim Peters6d6c1a32001-08-02 04:15:00 +00002103static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002104wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002105{
2106 intobjargproc func = (intobjargproc)wrapped;
2107 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002108 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109
Guido van Rossum5d815f32001-08-17 21:57:47 +00002110 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2111 return NULL;
2112 i = getindex(self, arg);
2113 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002114 return NULL;
2115 res = (*func)(self, i, value);
2116 if (res == -1 && PyErr_Occurred())
2117 return NULL;
2118 Py_INCREF(Py_None);
2119 return Py_None;
2120}
2121
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002122static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002123wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002124{
2125 intobjargproc func = (intobjargproc)wrapped;
2126 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002127 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002128
Guido van Rossum5d815f32001-08-17 21:57:47 +00002129 if (!PyArg_ParseTuple(args, "O", &arg))
2130 return NULL;
2131 i = getindex(self, arg);
2132 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002133 return NULL;
2134 res = (*func)(self, i, NULL);
2135 if (res == -1 && PyErr_Occurred())
2136 return NULL;
2137 Py_INCREF(Py_None);
2138 return Py_None;
2139}
2140
Tim Peters6d6c1a32001-08-02 04:15:00 +00002141static PyObject *
2142wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2143{
2144 intintobjargproc func = (intintobjargproc)wrapped;
2145 int i, j, res;
2146 PyObject *value;
2147
2148 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2149 return NULL;
2150 res = (*func)(self, i, j, value);
2151 if (res == -1 && PyErr_Occurred())
2152 return NULL;
2153 Py_INCREF(Py_None);
2154 return Py_None;
2155}
2156
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002157static PyObject *
2158wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2159{
2160 intintobjargproc func = (intintobjargproc)wrapped;
2161 int i, j, res;
2162
2163 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2164 return NULL;
2165 res = (*func)(self, i, j, NULL);
2166 if (res == -1 && PyErr_Occurred())
2167 return NULL;
2168 Py_INCREF(Py_None);
2169 return Py_None;
2170}
2171
Tim Peters6d6c1a32001-08-02 04:15:00 +00002172/* XXX objobjproc is a misnomer; should be objargpred */
2173static PyObject *
2174wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2175{
2176 objobjproc func = (objobjproc)wrapped;
2177 int res;
2178 PyObject *value;
2179
2180 if (!PyArg_ParseTuple(args, "O", &value))
2181 return NULL;
2182 res = (*func)(self, value);
2183 if (res == -1 && PyErr_Occurred())
2184 return NULL;
2185 return PyInt_FromLong((long)res);
2186}
2187
Tim Peters6d6c1a32001-08-02 04:15:00 +00002188static PyObject *
2189wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2190{
2191 objobjargproc func = (objobjargproc)wrapped;
2192 int res;
2193 PyObject *key, *value;
2194
2195 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2196 return NULL;
2197 res = (*func)(self, key, value);
2198 if (res == -1 && PyErr_Occurred())
2199 return NULL;
2200 Py_INCREF(Py_None);
2201 return Py_None;
2202}
2203
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002204static PyObject *
2205wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2206{
2207 objobjargproc func = (objobjargproc)wrapped;
2208 int res;
2209 PyObject *key;
2210
2211 if (!PyArg_ParseTuple(args, "O", &key))
2212 return NULL;
2213 res = (*func)(self, key, NULL);
2214 if (res == -1 && PyErr_Occurred())
2215 return NULL;
2216 Py_INCREF(Py_None);
2217 return Py_None;
2218}
2219
Tim Peters6d6c1a32001-08-02 04:15:00 +00002220static PyObject *
2221wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2222{
2223 cmpfunc func = (cmpfunc)wrapped;
2224 int res;
2225 PyObject *other;
2226
2227 if (!PyArg_ParseTuple(args, "O", &other))
2228 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002229 if (other->ob_type->tp_compare != func &&
2230 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002231 PyErr_Format(
2232 PyExc_TypeError,
2233 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2234 self->ob_type->tp_name,
2235 self->ob_type->tp_name,
2236 other->ob_type->tp_name);
2237 return NULL;
2238 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002239 res = (*func)(self, other);
2240 if (PyErr_Occurred())
2241 return NULL;
2242 return PyInt_FromLong((long)res);
2243}
2244
Tim Peters6d6c1a32001-08-02 04:15:00 +00002245static PyObject *
2246wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2247{
2248 setattrofunc func = (setattrofunc)wrapped;
2249 int res;
2250 PyObject *name, *value;
2251
2252 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2253 return NULL;
2254 res = (*func)(self, name, value);
2255 if (res < 0)
2256 return NULL;
2257 Py_INCREF(Py_None);
2258 return Py_None;
2259}
2260
2261static PyObject *
2262wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2263{
2264 setattrofunc func = (setattrofunc)wrapped;
2265 int res;
2266 PyObject *name;
2267
2268 if (!PyArg_ParseTuple(args, "O", &name))
2269 return NULL;
2270 res = (*func)(self, name, NULL);
2271 if (res < 0)
2272 return NULL;
2273 Py_INCREF(Py_None);
2274 return Py_None;
2275}
2276
Tim Peters6d6c1a32001-08-02 04:15:00 +00002277static PyObject *
2278wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2279{
2280 hashfunc func = (hashfunc)wrapped;
2281 long res;
2282
2283 if (!PyArg_ParseTuple(args, ""))
2284 return NULL;
2285 res = (*func)(self);
2286 if (res == -1 && PyErr_Occurred())
2287 return NULL;
2288 return PyInt_FromLong(res);
2289}
2290
Tim Peters6d6c1a32001-08-02 04:15:00 +00002291static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002292wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002293{
2294 ternaryfunc func = (ternaryfunc)wrapped;
2295
Guido van Rossumc8e56452001-10-22 00:43:43 +00002296 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297}
2298
Tim Peters6d6c1a32001-08-02 04:15:00 +00002299static PyObject *
2300wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2301{
2302 richcmpfunc func = (richcmpfunc)wrapped;
2303 PyObject *other;
2304
2305 if (!PyArg_ParseTuple(args, "O", &other))
2306 return NULL;
2307 return (*func)(self, other, op);
2308}
2309
2310#undef RICHCMP_WRAPPER
2311#define RICHCMP_WRAPPER(NAME, OP) \
2312static PyObject * \
2313richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2314{ \
2315 return wrap_richcmpfunc(self, args, wrapped, OP); \
2316}
2317
Jack Jansen8e938b42001-08-08 15:29:49 +00002318RICHCMP_WRAPPER(lt, Py_LT)
2319RICHCMP_WRAPPER(le, Py_LE)
2320RICHCMP_WRAPPER(eq, Py_EQ)
2321RICHCMP_WRAPPER(ne, Py_NE)
2322RICHCMP_WRAPPER(gt, Py_GT)
2323RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002324
Tim Peters6d6c1a32001-08-02 04:15:00 +00002325static PyObject *
2326wrap_next(PyObject *self, PyObject *args, void *wrapped)
2327{
2328 unaryfunc func = (unaryfunc)wrapped;
2329 PyObject *res;
2330
2331 if (!PyArg_ParseTuple(args, ""))
2332 return NULL;
2333 res = (*func)(self);
2334 if (res == NULL && !PyErr_Occurred())
2335 PyErr_SetNone(PyExc_StopIteration);
2336 return res;
2337}
2338
Tim Peters6d6c1a32001-08-02 04:15:00 +00002339static PyObject *
2340wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2341{
2342 descrgetfunc func = (descrgetfunc)wrapped;
2343 PyObject *obj;
2344 PyObject *type = NULL;
2345
2346 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2347 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002348 return (*func)(self, obj, type);
2349}
2350
Tim Peters6d6c1a32001-08-02 04:15:00 +00002351static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002352wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002353{
2354 descrsetfunc func = (descrsetfunc)wrapped;
2355 PyObject *obj, *value;
2356 int ret;
2357
2358 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2359 return NULL;
2360 ret = (*func)(self, obj, value);
2361 if (ret < 0)
2362 return NULL;
2363 Py_INCREF(Py_None);
2364 return Py_None;
2365}
2366
Tim Peters6d6c1a32001-08-02 04:15:00 +00002367static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002368wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002369{
2370 initproc func = (initproc)wrapped;
2371
Guido van Rossumc8e56452001-10-22 00:43:43 +00002372 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002373 return NULL;
2374 Py_INCREF(Py_None);
2375 return Py_None;
2376}
2377
Tim Peters6d6c1a32001-08-02 04:15:00 +00002378static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002379tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002380{
Barry Warsaw60f01882001-08-22 19:24:42 +00002381 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002382 PyObject *arg0, *res;
2383
2384 if (self == NULL || !PyType_Check(self))
2385 Py_FatalError("__new__() called with non-type 'self'");
2386 type = (PyTypeObject *)self;
2387 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002388 PyErr_Format(PyExc_TypeError,
2389 "%s.__new__(): not enough arguments",
2390 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002391 return NULL;
2392 }
2393 arg0 = PyTuple_GET_ITEM(args, 0);
2394 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002395 PyErr_Format(PyExc_TypeError,
2396 "%s.__new__(X): X is not a type object (%s)",
2397 type->tp_name,
2398 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002399 return NULL;
2400 }
2401 subtype = (PyTypeObject *)arg0;
2402 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002403 PyErr_Format(PyExc_TypeError,
2404 "%s.__new__(%s): %s is not a subtype of %s",
2405 type->tp_name,
2406 subtype->tp_name,
2407 subtype->tp_name,
2408 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002409 return NULL;
2410 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002411
2412 /* Check that the use doesn't do something silly and unsafe like
2413 object.__new__(dictionary). To do this, we check that the
2414 most derived base that's not a heap type is this type. */
2415 staticbase = subtype;
2416 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2417 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002418 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002419 PyErr_Format(PyExc_TypeError,
2420 "%s.__new__(%s) is not safe, use %s.__new__()",
2421 type->tp_name,
2422 subtype->tp_name,
2423 staticbase == NULL ? "?" : staticbase->tp_name);
2424 return NULL;
2425 }
2426
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002427 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2428 if (args == NULL)
2429 return NULL;
2430 res = type->tp_new(subtype, args, kwds);
2431 Py_DECREF(args);
2432 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433}
2434
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002435static struct PyMethodDef tp_new_methoddef[] = {
2436 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2437 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002438 {0}
2439};
2440
2441static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002442add_tp_new_wrapper(PyTypeObject *type)
2443{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002444 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002445
Guido van Rossum687ae002001-10-15 22:03:32 +00002446 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002447 return 0;
2448 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002449 if (func == NULL)
2450 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002451 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002452}
2453
Guido van Rossumf040ede2001-08-07 16:40:56 +00002454/* Slot wrappers that call the corresponding __foo__ slot. See comments
2455 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002456
Guido van Rossumdc91b992001-08-08 22:26:22 +00002457#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002458static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002459FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002460{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002461 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002462 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002463}
2464
Guido van Rossumdc91b992001-08-08 22:26:22 +00002465#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002466static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002467FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002468{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002469 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002470 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002471}
2472
Guido van Rossumdc91b992001-08-08 22:26:22 +00002473
2474#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002475static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002476FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002477{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002478 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002479 int do_other = self->ob_type != other->ob_type && \
2480 other->ob_type->tp_as_number != NULL && \
2481 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002482 if (self->ob_type->tp_as_number != NULL && \
2483 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2484 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002485 if (do_other && \
2486 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2487 r = call_maybe( \
2488 other, ROPSTR, &rcache_str, "(O)", self); \
2489 if (r != Py_NotImplemented) \
2490 return r; \
2491 Py_DECREF(r); \
2492 do_other = 0; \
2493 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002494 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002495 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002496 if (r != Py_NotImplemented || \
2497 other->ob_type == self->ob_type) \
2498 return r; \
2499 Py_DECREF(r); \
2500 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002501 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002502 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002503 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002504 } \
2505 Py_INCREF(Py_NotImplemented); \
2506 return Py_NotImplemented; \
2507}
2508
2509#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2510 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2511
2512#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2513static PyObject * \
2514FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2515{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002516 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002517 return call_method(self, OPSTR, &cache_str, \
2518 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002519}
2520
2521static int
2522slot_sq_length(PyObject *self)
2523{
Guido van Rossum2730b132001-08-28 18:22:14 +00002524 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002525 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002526 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002527
2528 if (res == NULL)
2529 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002530 len = (int)PyInt_AsLong(res);
2531 Py_DECREF(res);
2532 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002533}
2534
Guido van Rossumdc91b992001-08-08 22:26:22 +00002535SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2536SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002537
2538/* Super-optimized version of slot_sq_item.
2539 Other slots could do the same... */
2540static PyObject *
2541slot_sq_item(PyObject *self, int i)
2542{
2543 static PyObject *getitem_str;
2544 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2545 descrgetfunc f;
2546
2547 if (getitem_str == NULL) {
2548 getitem_str = PyString_InternFromString("__getitem__");
2549 if (getitem_str == NULL)
2550 return NULL;
2551 }
2552 func = _PyType_Lookup(self->ob_type, getitem_str);
2553 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002554 if ((f = func->ob_type->tp_descr_get) == NULL)
2555 Py_INCREF(func);
2556 else
2557 func = f(func, self, (PyObject *)(self->ob_type));
2558 ival = PyInt_FromLong(i);
2559 if (ival != NULL) {
2560 args = PyTuple_New(1);
2561 if (args != NULL) {
2562 PyTuple_SET_ITEM(args, 0, ival);
2563 retval = PyObject_Call(func, args, NULL);
2564 Py_XDECREF(args);
2565 Py_XDECREF(func);
2566 return retval;
2567 }
2568 }
2569 }
2570 else {
2571 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2572 }
2573 Py_XDECREF(args);
2574 Py_XDECREF(ival);
2575 Py_XDECREF(func);
2576 return NULL;
2577}
2578
Guido van Rossumdc91b992001-08-08 22:26:22 +00002579SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580
2581static int
2582slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2583{
2584 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002585 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002586
2587 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002588 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002589 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002590 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002591 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002592 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002593 if (res == NULL)
2594 return -1;
2595 Py_DECREF(res);
2596 return 0;
2597}
2598
2599static int
2600slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2601{
2602 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002603 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002604
2605 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002606 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002607 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002608 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002609 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002610 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002611 if (res == NULL)
2612 return -1;
2613 Py_DECREF(res);
2614 return 0;
2615}
2616
2617static int
2618slot_sq_contains(PyObject *self, PyObject *value)
2619{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002620 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002621 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002622
Guido van Rossum55f20992001-10-01 17:18:22 +00002623 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002624
2625 if (func != NULL) {
2626 args = Py_BuildValue("(O)", value);
2627 if (args == NULL)
2628 res = NULL;
2629 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002630 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002631 Py_DECREF(args);
2632 }
2633 Py_DECREF(func);
2634 if (res == NULL)
2635 return -1;
2636 return PyObject_IsTrue(res);
2637 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002638 else if (PyErr_Occurred())
2639 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002640 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002641 return _PySequence_IterSearch(self, value,
2642 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002643 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002644}
2645
Guido van Rossumdc91b992001-08-08 22:26:22 +00002646SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2647SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002648
2649#define slot_mp_length slot_sq_length
2650
Guido van Rossumdc91b992001-08-08 22:26:22 +00002651SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002652
2653static int
2654slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2655{
2656 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002657 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002658
2659 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002660 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002661 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002662 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002663 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002664 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002665 if (res == NULL)
2666 return -1;
2667 Py_DECREF(res);
2668 return 0;
2669}
2670
Guido van Rossumdc91b992001-08-08 22:26:22 +00002671SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2672SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2673SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2674SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2675SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2676SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2677
2678staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2679
2680SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2681 nb_power, "__pow__", "__rpow__")
2682
2683static PyObject *
2684slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2685{
Guido van Rossum2730b132001-08-28 18:22:14 +00002686 static PyObject *pow_str;
2687
Guido van Rossumdc91b992001-08-08 22:26:22 +00002688 if (modulus == Py_None)
2689 return slot_nb_power_binary(self, other);
2690 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002691 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002692 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002693}
2694
2695SLOT0(slot_nb_negative, "__neg__")
2696SLOT0(slot_nb_positive, "__pos__")
2697SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698
2699static int
2700slot_nb_nonzero(PyObject *self)
2701{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002702 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002703 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002704
Guido van Rossum55f20992001-10-01 17:18:22 +00002705 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002706 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002707 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002708 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002709 func = lookup_maybe(self, "__len__", &len_str);
2710 if (func == NULL) {
2711 if (PyErr_Occurred())
2712 return -1;
2713 else
2714 return 1;
2715 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002716 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002717 res = PyObject_CallObject(func, NULL);
2718 Py_DECREF(func);
2719 if (res == NULL)
2720 return -1;
2721 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002722}
2723
Guido van Rossumdc91b992001-08-08 22:26:22 +00002724SLOT0(slot_nb_invert, "__invert__")
2725SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2726SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2727SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2728SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2729SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002730
2731static int
2732slot_nb_coerce(PyObject **a, PyObject **b)
2733{
2734 static PyObject *coerce_str;
2735 PyObject *self = *a, *other = *b;
2736
2737 if (self->ob_type->tp_as_number != NULL &&
2738 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2739 PyObject *r;
2740 r = call_maybe(
2741 self, "__coerce__", &coerce_str, "(O)", other);
2742 if (r == NULL)
2743 return -1;
2744 if (r == Py_NotImplemented) {
2745 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002746 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002747 else {
2748 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2749 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002750 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002751 Py_DECREF(r);
2752 return -1;
2753 }
2754 *a = PyTuple_GET_ITEM(r, 0);
2755 Py_INCREF(*a);
2756 *b = PyTuple_GET_ITEM(r, 1);
2757 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002758 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002759 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002760 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002761 }
2762 if (other->ob_type->tp_as_number != NULL &&
2763 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2764 PyObject *r;
2765 r = call_maybe(
2766 other, "__coerce__", &coerce_str, "(O)", self);
2767 if (r == NULL)
2768 return -1;
2769 if (r == Py_NotImplemented) {
2770 Py_DECREF(r);
2771 return 1;
2772 }
2773 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2774 PyErr_SetString(PyExc_TypeError,
2775 "__coerce__ didn't return a 2-tuple");
2776 Py_DECREF(r);
2777 return -1;
2778 }
2779 *a = PyTuple_GET_ITEM(r, 1);
2780 Py_INCREF(*a);
2781 *b = PyTuple_GET_ITEM(r, 0);
2782 Py_INCREF(*b);
2783 Py_DECREF(r);
2784 return 0;
2785 }
2786 return 1;
2787}
2788
Guido van Rossumdc91b992001-08-08 22:26:22 +00002789SLOT0(slot_nb_int, "__int__")
2790SLOT0(slot_nb_long, "__long__")
2791SLOT0(slot_nb_float, "__float__")
2792SLOT0(slot_nb_oct, "__oct__")
2793SLOT0(slot_nb_hex, "__hex__")
2794SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2795SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2796SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2797SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2798SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2799SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2800SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2801SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2802SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2803SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2804SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2805SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2806 "__floordiv__", "__rfloordiv__")
2807SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2808SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2809SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002810
2811static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002812half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002813{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002814 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002815 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002816 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002817
Guido van Rossum60718732001-08-28 17:47:51 +00002818 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002819 if (func == NULL) {
2820 PyErr_Clear();
2821 }
2822 else {
2823 args = Py_BuildValue("(O)", other);
2824 if (args == NULL)
2825 res = NULL;
2826 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002827 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002828 Py_DECREF(args);
2829 }
2830 if (res != Py_NotImplemented) {
2831 if (res == NULL)
2832 return -2;
2833 c = PyInt_AsLong(res);
2834 Py_DECREF(res);
2835 if (c == -1 && PyErr_Occurred())
2836 return -2;
2837 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2838 }
2839 Py_DECREF(res);
2840 }
2841 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002842}
2843
Guido van Rossumab3b0342001-09-18 20:38:53 +00002844/* This slot is published for the benefit of try_3way_compare in object.c */
2845int
2846_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00002847{
2848 int c;
2849
Guido van Rossumab3b0342001-09-18 20:38:53 +00002850 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002851 c = half_compare(self, other);
2852 if (c <= 1)
2853 return c;
2854 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00002855 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002856 c = half_compare(other, self);
2857 if (c < -1)
2858 return -2;
2859 if (c <= 1)
2860 return -c;
2861 }
2862 return (void *)self < (void *)other ? -1 :
2863 (void *)self > (void *)other ? 1 : 0;
2864}
2865
2866static PyObject *
2867slot_tp_repr(PyObject *self)
2868{
2869 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002870 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002871
Guido van Rossum60718732001-08-28 17:47:51 +00002872 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002873 if (func != NULL) {
2874 res = PyEval_CallObject(func, NULL);
2875 Py_DECREF(func);
2876 return res;
2877 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002878 PyErr_Clear();
2879 return PyString_FromFormat("<%s object at %p>",
2880 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002881}
2882
2883static PyObject *
2884slot_tp_str(PyObject *self)
2885{
2886 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002887 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002888
Guido van Rossum60718732001-08-28 17:47:51 +00002889 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002890 if (func != NULL) {
2891 res = PyEval_CallObject(func, NULL);
2892 Py_DECREF(func);
2893 return res;
2894 }
2895 else {
2896 PyErr_Clear();
2897 return slot_tp_repr(self);
2898 }
2899}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002900
2901static long
2902slot_tp_hash(PyObject *self)
2903{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002904 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002905 static PyObject *hash_str, *eq_str, *cmp_str;
2906
Tim Peters6d6c1a32001-08-02 04:15:00 +00002907 long h;
2908
Guido van Rossum60718732001-08-28 17:47:51 +00002909 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002910
2911 if (func != NULL) {
2912 res = PyEval_CallObject(func, NULL);
2913 Py_DECREF(func);
2914 if (res == NULL)
2915 return -1;
2916 h = PyInt_AsLong(res);
2917 }
2918 else {
2919 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002920 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002921 if (func == NULL) {
2922 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002923 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002924 }
2925 if (func != NULL) {
2926 Py_DECREF(func);
2927 PyErr_SetString(PyExc_TypeError, "unhashable type");
2928 return -1;
2929 }
2930 PyErr_Clear();
2931 h = _Py_HashPointer((void *)self);
2932 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002933 if (h == -1 && !PyErr_Occurred())
2934 h = -2;
2935 return h;
2936}
2937
2938static PyObject *
2939slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2940{
Guido van Rossum60718732001-08-28 17:47:51 +00002941 static PyObject *call_str;
2942 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002943 PyObject *res;
2944
2945 if (meth == NULL)
2946 return NULL;
2947 res = PyObject_Call(meth, args, kwds);
2948 Py_DECREF(meth);
2949 return res;
2950}
2951
Guido van Rossum14a6f832001-10-17 13:59:09 +00002952/* There are two slot dispatch functions for tp_getattro.
2953
2954 - slot_tp_getattro() is used when __getattribute__ is overridden
2955 but no __getattr__ hook is present;
2956
2957 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
2958
2959 The code in update_slot() and fixup_slot_dispatchers() always installs
2960 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
2961 installs the simpler slot if necessary. */
2962
Tim Peters6d6c1a32001-08-02 04:15:00 +00002963static PyObject *
2964slot_tp_getattro(PyObject *self, PyObject *name)
2965{
Guido van Rossum14a6f832001-10-17 13:59:09 +00002966 static PyObject *getattribute_str = NULL;
2967 return call_method(self, "__getattribute__", &getattribute_str,
2968 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002969}
2970
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002971static PyObject *
2972slot_tp_getattr_hook(PyObject *self, PyObject *name)
2973{
2974 PyTypeObject *tp = self->ob_type;
2975 PyObject *getattr, *getattribute, *res;
2976 static PyObject *getattribute_str = NULL;
2977 static PyObject *getattr_str = NULL;
2978
2979 if (getattr_str == NULL) {
2980 getattr_str = PyString_InternFromString("__getattr__");
2981 if (getattr_str == NULL)
2982 return NULL;
2983 }
2984 if (getattribute_str == NULL) {
2985 getattribute_str =
2986 PyString_InternFromString("__getattribute__");
2987 if (getattribute_str == NULL)
2988 return NULL;
2989 }
2990 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002991 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00002992 /* No __getattr__ hook: use a simpler dispatcher */
2993 tp->tp_getattro = slot_tp_getattro;
2994 return slot_tp_getattro(self, name);
2995 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00002996 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00002997 if (getattribute == NULL ||
2998 (getattribute->ob_type == &PyWrapperDescr_Type &&
2999 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3000 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003001 res = PyObject_GenericGetAttr(self, name);
3002 else
3003 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003004 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003005 PyErr_Clear();
3006 res = PyObject_CallFunction(getattr, "OO", self, name);
3007 }
3008 return res;
3009}
3010
Tim Peters6d6c1a32001-08-02 04:15:00 +00003011static int
3012slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3013{
3014 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003015 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003016
3017 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003018 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003019 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003020 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003021 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003022 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 if (res == NULL)
3024 return -1;
3025 Py_DECREF(res);
3026 return 0;
3027}
3028
3029/* Map rich comparison operators to their __xx__ namesakes */
3030static char *name_op[] = {
3031 "__lt__",
3032 "__le__",
3033 "__eq__",
3034 "__ne__",
3035 "__gt__",
3036 "__ge__",
3037};
3038
3039static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003040half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003041{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003042 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003043 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003044
Guido van Rossum60718732001-08-28 17:47:51 +00003045 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003046 if (func == NULL) {
3047 PyErr_Clear();
3048 Py_INCREF(Py_NotImplemented);
3049 return Py_NotImplemented;
3050 }
3051 args = Py_BuildValue("(O)", other);
3052 if (args == NULL)
3053 res = NULL;
3054 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003055 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003056 Py_DECREF(args);
3057 }
3058 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003059 return res;
3060}
3061
Guido van Rossumb8f63662001-08-15 23:57:02 +00003062/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3063static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3064
3065static PyObject *
3066slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3067{
3068 PyObject *res;
3069
3070 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3071 res = half_richcompare(self, other, op);
3072 if (res != Py_NotImplemented)
3073 return res;
3074 Py_DECREF(res);
3075 }
3076 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3077 res = half_richcompare(other, self, swapped_op[op]);
3078 if (res != Py_NotImplemented) {
3079 return res;
3080 }
3081 Py_DECREF(res);
3082 }
3083 Py_INCREF(Py_NotImplemented);
3084 return Py_NotImplemented;
3085}
3086
3087static PyObject *
3088slot_tp_iter(PyObject *self)
3089{
3090 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003091 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003092
Guido van Rossum60718732001-08-28 17:47:51 +00003093 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003094 if (func != NULL) {
3095 res = PyObject_CallObject(func, NULL);
3096 Py_DECREF(func);
3097 return res;
3098 }
3099 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003100 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003101 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003102 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003103 return NULL;
3104 }
3105 Py_DECREF(func);
3106 return PySeqIter_New(self);
3107}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003108
3109static PyObject *
3110slot_tp_iternext(PyObject *self)
3111{
Guido van Rossum2730b132001-08-28 18:22:14 +00003112 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003113 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003114}
3115
Guido van Rossum1a493502001-08-17 16:47:50 +00003116static PyObject *
3117slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3118{
3119 PyTypeObject *tp = self->ob_type;
3120 PyObject *get;
3121 static PyObject *get_str = NULL;
3122
3123 if (get_str == NULL) {
3124 get_str = PyString_InternFromString("__get__");
3125 if (get_str == NULL)
3126 return NULL;
3127 }
3128 get = _PyType_Lookup(tp, get_str);
3129 if (get == NULL) {
3130 /* Avoid further slowdowns */
3131 if (tp->tp_descr_get == slot_tp_descr_get)
3132 tp->tp_descr_get = NULL;
3133 Py_INCREF(self);
3134 return self;
3135 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003136 if (obj == NULL)
3137 obj = Py_None;
3138 if (type == NULL)
3139 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003140 return PyObject_CallFunction(get, "OOO", self, obj, type);
3141}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003142
3143static int
3144slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3145{
Guido van Rossum2c252392001-08-24 10:13:31 +00003146 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003147 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003148
3149 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003150 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003151 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003152 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003153 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003154 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003155 if (res == NULL)
3156 return -1;
3157 Py_DECREF(res);
3158 return 0;
3159}
3160
3161static int
3162slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3163{
Guido van Rossum60718732001-08-28 17:47:51 +00003164 static PyObject *init_str;
3165 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003166 PyObject *res;
3167
3168 if (meth == NULL)
3169 return -1;
3170 res = PyObject_Call(meth, args, kwds);
3171 Py_DECREF(meth);
3172 if (res == NULL)
3173 return -1;
3174 Py_DECREF(res);
3175 return 0;
3176}
3177
3178static PyObject *
3179slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3180{
3181 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3182 PyObject *newargs, *x;
3183 int i, n;
3184
3185 if (func == NULL)
3186 return NULL;
3187 assert(PyTuple_Check(args));
3188 n = PyTuple_GET_SIZE(args);
3189 newargs = PyTuple_New(n+1);
3190 if (newargs == NULL)
3191 return NULL;
3192 Py_INCREF(type);
3193 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3194 for (i = 0; i < n; i++) {
3195 x = PyTuple_GET_ITEM(args, i);
3196 Py_INCREF(x);
3197 PyTuple_SET_ITEM(newargs, i+1, x);
3198 }
3199 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003200 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003201 Py_DECREF(func);
3202 return x;
3203}
3204
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003205
3206/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3207 functions. The offsets here are relative to the 'etype' structure, which
3208 incorporates the additional structures used for numbers, sequences and
3209 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3210 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3211 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3212
Guido van Rossum6d204072001-10-21 00:44:31 +00003213typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003214
3215#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003216#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003217#undef ETSLOT
3218#undef SQSLOT
3219#undef MPSLOT
3220#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003221#undef UNSLOT
3222#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003223#undef BINSLOT
3224#undef RBINSLOT
3225
Guido van Rossum6d204072001-10-21 00:44:31 +00003226#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3227 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003228#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3229 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3230 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003231#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3232 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3233#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3234 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3235#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3236 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3237#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3238 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3239#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3240 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3241 "x." NAME "() <==> " DOC)
3242#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3243 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3244 "x." NAME "(y) <==> x" DOC "y")
3245#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3246 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3247 "x." NAME "(y) <==> x" DOC "y")
3248#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3249 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3250 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003251
3252static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003253 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3254 "x.__len__() <==> len(x)"),
3255 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3256 "x.__add__(y) <==> x+y"),
3257 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3258 "x.__mul__(n) <==> x*n"),
3259 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3260 "x.__rmul__(n) <==> n*x"),
3261 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3262 "x.__getitem__(y) <==> x[y]"),
3263 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3264 "x.__getslice__(i, j) <==> x[i:j]"),
3265 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3266 "x.__setitem__(i, y) <==> x[i]=y"),
3267 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3268 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003269 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003270 wrap_intintobjargproc,
3271 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3272 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3273 "x.__delslice__(i, j) <==> del x[i:j]"),
3274 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3275 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003276 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003277 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003278 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003279 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003280
Guido van Rossum6d204072001-10-21 00:44:31 +00003281 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3282 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003283 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003284 wrap_binaryfunc,
3285 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003286 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003287 wrap_objobjargproc,
3288 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003289 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003290 wrap_delitem,
3291 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003292
Guido van Rossum6d204072001-10-21 00:44:31 +00003293 BINSLOT("__add__", nb_add, slot_nb_add,
3294 "+"),
3295 RBINSLOT("__radd__", nb_add, slot_nb_add,
3296 "+"),
3297 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3298 "-"),
3299 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3300 "-"),
3301 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3302 "*"),
3303 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3304 "*"),
3305 BINSLOT("__div__", nb_divide, slot_nb_divide,
3306 "/"),
3307 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3308 "/"),
3309 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3310 "%"),
3311 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3312 "%"),
3313 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3314 "divmod(x, y)"),
3315 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3316 "divmod(y, x)"),
3317 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3318 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3319 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3320 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3321 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3322 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3323 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3324 "abs(x)"),
3325 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3326 "x != 0"),
3327 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3328 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3329 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3330 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3331 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3332 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3333 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3334 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3335 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3336 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3337 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3338 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3339 "x.__coerce__(y) <==> coerce(x, y)"),
3340 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3341 "int(x)"),
3342 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3343 "long(x)"),
3344 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3345 "float(x)"),
3346 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3347 "oct(x)"),
3348 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3349 "hex(x)"),
3350 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3351 wrap_binaryfunc, "+"),
3352 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3353 wrap_binaryfunc, "-"),
3354 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3355 wrap_binaryfunc, "*"),
3356 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3357 wrap_binaryfunc, "/"),
3358 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3359 wrap_binaryfunc, "%"),
3360 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3361 wrap_ternaryfunc, "**"),
3362 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3363 wrap_binaryfunc, "<<"),
3364 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3365 wrap_binaryfunc, ">>"),
3366 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3367 wrap_binaryfunc, "&"),
3368 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3369 wrap_binaryfunc, "^"),
3370 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3371 wrap_binaryfunc, "|"),
3372 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3373 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3374 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3375 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3376 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3377 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3378 IBSLOT("__itruediv__", nb_inplace_true_divide,
3379 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003380
Guido van Rossum6d204072001-10-21 00:44:31 +00003381 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3382 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003383 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003384 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3385 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003386 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003387 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3388 "x.__cmp__(y) <==> cmp(x,y)"),
3389 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3390 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003391 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3392 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003393 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003394 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3395 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3396 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3397 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3398 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3399 "x.__setattr__('name', value) <==> x.name = value"),
3400 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3401 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3402 "x.__delattr__('name') <==> del x.name"),
3403 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3404 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3405 "x.__lt__(y) <==> x<y"),
3406 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3407 "x.__le__(y) <==> x<=y"),
3408 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3409 "x.__eq__(y) <==> x==y"),
3410 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3411 "x.__ne__(y) <==> x!=y"),
3412 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3413 "x.__gt__(y) <==> x>y"),
3414 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3415 "x.__ge__(y) <==> x>=y"),
3416 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3417 "x.__iter__() <==> iter(x)"),
3418 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3419 "x.next() -> the next value, or raise StopIteration"),
3420 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3421 "descr.__get__(obj[, type]) -> value"),
3422 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3423 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003424 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003425 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003426 "see x.__class__.__doc__ for signature",
3427 PyWrapperFlag_KEYWORDS),
3428 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003429 {NULL}
3430};
3431
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003432static void **
3433slotptr(PyTypeObject *type, int offset)
3434{
3435 char *ptr;
3436
3437 assert(offset >= 0);
3438 assert(offset < offsetof(etype, as_buffer));
3439 if (offset >= offsetof(etype, as_mapping)) {
3440 ptr = (void *)type->tp_as_mapping;
3441 offset -= offsetof(etype, as_mapping);
3442 }
3443 else if (offset >= offsetof(etype, as_sequence)) {
3444 ptr = (void *)type->tp_as_sequence;
3445 offset -= offsetof(etype, as_sequence);
3446 }
3447 else if (offset >= offsetof(etype, as_number)) {
3448 ptr = (void *)type->tp_as_number;
3449 offset -= offsetof(etype, as_number);
3450 }
3451 else {
3452 ptr = (void *)type;
3453 }
3454 if (ptr != NULL)
3455 ptr += offset;
3456 return (void **)ptr;
3457}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003458
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003459staticforward int recurse_down_subclasses(PyTypeObject *type,
3460 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003461
3462static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003463update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003464{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003465 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003466
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003467 for (pp = pp0; *pp; pp++) {
3468 slotdef *p = *pp;
3469 PyObject *descr;
3470 PyWrapperDescrObject *d;
3471 void *generic = NULL, *specific = NULL;
3472 int use_generic = 0;
3473 int offset = p->offset;
3474 void **ptr = slotptr(type, offset);
3475 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003476 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003477 do {
3478 descr = _PyType_Lookup(type, p->name_strobj);
3479 if (descr == NULL)
3480 continue;
3481 generic = p->function;
3482 if (descr->ob_type == &PyWrapperDescr_Type) {
3483 d = (PyWrapperDescrObject *)descr;
3484 if (d->d_base->wrapper == p->wrapper &&
3485 PyType_IsSubtype(type, d->d_type)) {
3486 if (specific == NULL ||
3487 specific == d->d_wrapped)
3488 specific = d->d_wrapped;
3489 else
3490 use_generic = 1;
3491 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003492 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003493 else
3494 use_generic = 1;
3495 } while ((++p)->offset == offset);
3496 if (specific && !use_generic)
3497 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003498 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003499 *ptr = generic;
3500 }
3501 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003502}
3503
3504static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003505recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003506{
3507 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003508 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003509 int i, n;
3510
3511 subclasses = type->tp_subclasses;
3512 if (subclasses == NULL)
3513 return 0;
3514 assert(PyList_Check(subclasses));
3515 n = PyList_GET_SIZE(subclasses);
3516 for (i = 0; i < n; i++) {
3517 ref = PyList_GET_ITEM(subclasses, i);
3518 assert(PyWeakref_CheckRef(ref));
3519 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3520 if (subclass == NULL)
3521 continue;
3522 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003523 /* Avoid recursing down into unaffected classes */
3524 dict = subclass->tp_dict;
3525 if (dict != NULL && PyDict_Check(dict) &&
3526 PyDict_GetItem(dict, name) != NULL)
3527 continue;
3528 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003529 return -1;
3530 }
3531 return 0;
3532}
3533
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003534static int
3535slotdef_cmp(const void *aa, const void *bb)
3536{
3537 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3538 int c = a->offset - b->offset;
3539 if (c != 0)
3540 return c;
3541 else
3542 return a - b;
3543}
3544
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003545static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003546init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003547{
3548 slotdef *p;
3549 static int initialized = 0;
3550
3551 if (initialized)
3552 return;
3553 for (p = slotdefs; p->name; p++) {
3554 p->name_strobj = PyString_InternFromString(p->name);
3555 if (!p->name_strobj)
3556 Py_FatalError("XXX ouch");
3557 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003558 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3559 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003560 initialized = 1;
3561}
3562
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003563static int
3564update_slot(PyTypeObject *type, PyObject *name)
3565{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003566 slotdef *ptrs[10];
3567 slotdef *p;
3568 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003569 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003570
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003571 init_slotdefs();
3572 pp = ptrs;
3573 for (p = slotdefs; p->name; p++) {
3574 /* XXX assume name is interned! */
3575 if (p->name_strobj == name)
3576 *pp++ = p;
3577 }
3578 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003579 for (pp = ptrs; *pp; pp++) {
3580 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003581 offset = p->offset;
3582 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003583 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003584 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003585 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003586 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003587}
3588
Tim Peters6d6c1a32001-08-02 04:15:00 +00003589static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003590fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003591{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003592 slotdef *p;
3593 PyObject *mro, *descr;
3594 PyTypeObject *base;
3595 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003596 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003597 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003598 void *generic, *specific;
3599 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003600
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003601 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003602 mro = type->tp_mro;
3603 assert(PyTuple_Check(mro));
3604 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003605 for (p = slotdefs; p->name; ) {
3606 offset = p->offset;
3607 ptr = slotptr(type, offset);
3608 if (!ptr) {
3609 do {
3610 ++p;
3611 } while (p->offset == offset);
3612 continue;
3613 }
3614 generic = specific = NULL;
3615 use_generic = 0;
3616 do {
3617 descr = NULL;
3618 for (i = 0; i < n; i++) {
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003619 base = (PyTypeObject *)
3620 PyTuple_GET_ITEM(mro, i);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003621 assert(PyType_Check(base));
3622 descr = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003623 base->tp_dict, p->name_strobj);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003624 if (descr != NULL)
3625 break;
3626 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003627 if (descr == NULL)
3628 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003629 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003630 if (descr->ob_type == &PyWrapperDescr_Type) {
3631 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003632 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003633 PyType_IsSubtype(type, d->d_type))
3634 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003635 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003636 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003637 specific = d->d_wrapped;
3638 else
3639 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003640 }
3641 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003642 else
3643 use_generic = 1;
3644 } while ((++p)->offset == offset);
3645 if (specific && !use_generic)
3646 *ptr = specific;
3647 else
3648 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003649 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003650}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003651
Guido van Rossum6d204072001-10-21 00:44:31 +00003652/* This function is called by PyType_Ready() to populate the type's
3653 dictionary with method descriptors for function slots. For each
3654 function slot (like tp_repr) that's defined in the type, one or
3655 more corresponding descriptors are added in the type's tp_dict
3656 dictionary under the appropriate name (like __repr__). Some
3657 function slots cause more than one descriptor to be added (for
3658 example, the nb_add slot adds both __add__ and __radd__
3659 descriptors) and some function slots compete for the same
3660 descriptor (for example both sq_item and mp_subscript generate a
3661 __getitem__ descriptor). This only adds new descriptors and
3662 doesn't overwrite entries in tp_dict that were previously
3663 defined. The descriptors contain a reference to the C function
3664 they must call, so that it's safe if they are copied into a
3665 subtype's __dict__ and the subtype has a different C function in
3666 its slot -- calling the method defined by the descriptor will call
3667 the C function that was used to create it, rather than the C
3668 function present in the slot when it is called. (This is important
3669 because a subtype may have a C function in the slot that calls the
3670 method from the dictionary, and we want to avoid infinite recursion
3671 here.) */
3672
3673static int
3674add_operators(PyTypeObject *type)
3675{
3676 PyObject *dict = type->tp_dict;
3677 slotdef *p;
3678 PyObject *descr;
3679 void **ptr;
3680
3681 init_slotdefs();
3682 for (p = slotdefs; p->name; p++) {
3683 if (p->wrapper == NULL)
3684 continue;
3685 ptr = slotptr(type, p->offset);
3686 if (!ptr || !*ptr)
3687 continue;
3688 if (PyDict_GetItem(dict, p->name_strobj))
3689 continue;
3690 descr = PyDescr_NewWrapper(type, p, *ptr);
3691 if (descr == NULL)
3692 return -1;
3693 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3694 return -1;
3695 Py_DECREF(descr);
3696 }
3697 if (type->tp_new != NULL) {
3698 if (add_tp_new_wrapper(type) < 0)
3699 return -1;
3700 }
3701 return 0;
3702}
3703
Guido van Rossum705f0f52001-08-24 16:47:00 +00003704
3705/* Cooperative 'super' */
3706
3707typedef struct {
3708 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003709 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003710 PyObject *obj;
3711} superobject;
3712
Guido van Rossum6f799372001-09-20 20:46:19 +00003713static PyMemberDef super_members[] = {
3714 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3715 "the class invoking super()"},
3716 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3717 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003718 {0}
3719};
3720
Guido van Rossum705f0f52001-08-24 16:47:00 +00003721static void
3722super_dealloc(PyObject *self)
3723{
3724 superobject *su = (superobject *)self;
3725
Guido van Rossum048eb752001-10-02 21:24:57 +00003726 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003727 Py_XDECREF(su->obj);
3728 Py_XDECREF(su->type);
3729 self->ob_type->tp_free(self);
3730}
3731
3732static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003733super_repr(PyObject *self)
3734{
3735 superobject *su = (superobject *)self;
3736
3737 if (su->obj)
3738 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003739 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003740 su->type ? su->type->tp_name : "NULL",
3741 su->obj->ob_type->tp_name);
3742 else
3743 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003744 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003745 su->type ? su->type->tp_name : "NULL");
3746}
3747
3748static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003749super_getattro(PyObject *self, PyObject *name)
3750{
3751 superobject *su = (superobject *)self;
3752
3753 if (su->obj != NULL) {
3754 PyObject *mro, *res, *tmp;
3755 descrgetfunc f;
3756 int i, n;
3757
Guido van Rossume705ef12001-08-29 15:47:06 +00003758 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003759 if (mro == NULL)
3760 n = 0;
3761 else {
3762 assert(PyTuple_Check(mro));
3763 n = PyTuple_GET_SIZE(mro);
3764 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003765 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003766 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003767 break;
3768 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003769 if (i >= n && PyType_Check(su->obj)) {
3770 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003771 if (mro == NULL)
3772 n = 0;
3773 else {
3774 assert(PyTuple_Check(mro));
3775 n = PyTuple_GET_SIZE(mro);
3776 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003777 for (i = 0; i < n; i++) {
3778 if ((PyObject *)(su->type) ==
3779 PyTuple_GET_ITEM(mro, i))
3780 break;
3781 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003782 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003783 i++;
3784 res = NULL;
3785 for (; i < n; i++) {
3786 tmp = PyTuple_GET_ITEM(mro, i);
3787 assert(PyType_Check(tmp));
3788 res = PyDict_GetItem(
Guido van Rossum687ae002001-10-15 22:03:32 +00003789 ((PyTypeObject *)tmp)->tp_dict, name);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003790 if (res != NULL) {
3791 Py_INCREF(res);
3792 f = res->ob_type->tp_descr_get;
3793 if (f != NULL) {
3794 tmp = f(res, su->obj, res);
3795 Py_DECREF(res);
3796 res = tmp;
3797 }
3798 return res;
3799 }
3800 }
3801 }
3802 return PyObject_GenericGetAttr(self, name);
3803}
3804
3805static PyObject *
3806super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3807{
3808 superobject *su = (superobject *)self;
3809 superobject *new;
3810
3811 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3812 /* Not binding to an object, or already bound */
3813 Py_INCREF(self);
3814 return self;
3815 }
3816 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3817 if (new == NULL)
3818 return NULL;
3819 Py_INCREF(su->type);
3820 Py_INCREF(obj);
3821 new->type = su->type;
3822 new->obj = obj;
3823 return (PyObject *)new;
3824}
3825
3826static int
3827super_init(PyObject *self, PyObject *args, PyObject *kwds)
3828{
3829 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003830 PyTypeObject *type;
3831 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003832
3833 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3834 return -1;
3835 if (obj == Py_None)
3836 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003837 if (obj != NULL &&
3838 !PyType_IsSubtype(obj->ob_type, type) &&
3839 !(PyType_Check(obj) &&
3840 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003841 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003842 "super(type, obj): "
3843 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003844 return -1;
3845 }
3846 Py_INCREF(type);
3847 Py_XINCREF(obj);
3848 su->type = type;
3849 su->obj = obj;
3850 return 0;
3851}
3852
3853static char super_doc[] =
3854"super(type) -> unbound super object\n"
3855"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003856"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003857"Typical use to call a cooperative superclass method:\n"
3858"class C(B):\n"
3859" def meth(self, arg):\n"
3860" super(C, self).meth(arg)";
3861
Guido van Rossum048eb752001-10-02 21:24:57 +00003862static int
3863super_traverse(PyObject *self, visitproc visit, void *arg)
3864{
3865 superobject *su = (superobject *)self;
3866 int err;
3867
3868#define VISIT(SLOT) \
3869 if (SLOT) { \
3870 err = visit((PyObject *)(SLOT), arg); \
3871 if (err) \
3872 return err; \
3873 }
3874
3875 VISIT(su->obj);
3876 VISIT(su->type);
3877
3878#undef VISIT
3879
3880 return 0;
3881}
3882
Guido van Rossum705f0f52001-08-24 16:47:00 +00003883PyTypeObject PySuper_Type = {
3884 PyObject_HEAD_INIT(&PyType_Type)
3885 0, /* ob_size */
3886 "super", /* tp_name */
3887 sizeof(superobject), /* tp_basicsize */
3888 0, /* tp_itemsize */
3889 /* methods */
3890 super_dealloc, /* tp_dealloc */
3891 0, /* tp_print */
3892 0, /* tp_getattr */
3893 0, /* tp_setattr */
3894 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003895 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003896 0, /* tp_as_number */
3897 0, /* tp_as_sequence */
3898 0, /* tp_as_mapping */
3899 0, /* tp_hash */
3900 0, /* tp_call */
3901 0, /* tp_str */
3902 super_getattro, /* tp_getattro */
3903 0, /* tp_setattro */
3904 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003905 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3906 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003907 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003908 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003909 0, /* tp_clear */
3910 0, /* tp_richcompare */
3911 0, /* tp_weaklistoffset */
3912 0, /* tp_iter */
3913 0, /* tp_iternext */
3914 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003915 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003916 0, /* tp_getset */
3917 0, /* tp_base */
3918 0, /* tp_dict */
3919 super_descr_get, /* tp_descr_get */
3920 0, /* tp_descr_set */
3921 0, /* tp_dictoffset */
3922 super_init, /* tp_init */
3923 PyType_GenericAlloc, /* tp_alloc */
3924 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003925 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003926};