blob: 8b1ac54430fb1dfbba263a10e9f9227ab8a24e40 [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
Tim Peters6d6c1a32001-08-02 04:15:00 +00007static struct memberlist 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__");
47 mod = PyDict_GetItemString(type->tp_defined, "__module__");
48 if (mod != NULL && PyString_Check(mod)) {
49 Py_INCREF(mod);
50 return mod;
51 }
52 PyErr_SetString(PyExc_AttributeError, "__module__");
53 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000054}
55
56static PyObject *
57type_dict(PyTypeObject *type, void *context)
58{
59 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000060 Py_INCREF(Py_None);
61 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000062 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000063 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
64 Py_INCREF(type->tp_dict);
65 return type->tp_dict;
66 }
67 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000068}
69
Tim Peters6d6c1a32001-08-02 04:15:00 +000070static PyObject *
71type_defined(PyTypeObject *type, void *context)
72{
73 if (type->tp_defined == NULL) {
74 Py_INCREF(Py_None);
75 return Py_None;
76 }
77 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
78 Py_INCREF(type->tp_defined);
79 return type->tp_defined;
80 }
81 return PyDictProxy_New(type->tp_defined);
82}
83
84static PyObject *
85type_dynamic(PyTypeObject *type, void *context)
86{
87 PyObject *res;
88
89 res = (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ? Py_True : Py_False;
90 Py_INCREF(res);
91 return res;
92}
93
94struct getsetlist type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +000095 {"__name__", (getter)type_name, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000096 {"__module__", (getter)type_module, NULL, NULL},
97 {"__dict__", (getter)type_dict, NULL, NULL},
98 {"__defined__", (getter)type_defined, NULL, NULL},
99 {"__dynamic__", (getter)type_dynamic, NULL, NULL},
100 {0}
101};
102
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000103static int
104type_compare(PyObject *v, PyObject *w)
105{
106 /* This is called with type objects only. So we
107 can just compare the addresses. */
108 Py_uintptr_t vv = (Py_uintptr_t)v;
109 Py_uintptr_t ww = (Py_uintptr_t)w;
110 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
111}
112
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000113static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000114type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000116 PyObject *mod, *name, *rtn;
Guido van Rossumc3542212001-08-16 09:18:56 +0000117
118 mod = type_module(type, NULL);
119 if (mod == NULL)
120 PyErr_Clear();
121 else if (!PyString_Check(mod)) {
122 Py_DECREF(mod);
123 mod = NULL;
124 }
125 name = type_name(type, NULL);
126 if (name == NULL)
127 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000128
129 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
130 rtn = PyString_FromFormat("<type '%s.%s'>",
131 PyString_AS_STRING(mod),
132 PyString_AS_STRING(name));
133 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000134 else
Barry Warsaw7ce36942001-08-24 18:34:26 +0000135 rtn = PyString_FromFormat("<type '%s'>", type->tp_name);
136
Guido van Rossumc3542212001-08-16 09:18:56 +0000137 Py_XDECREF(mod);
138 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000139 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000140}
141
Tim Peters6d6c1a32001-08-02 04:15:00 +0000142static PyObject *
143type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
144{
145 PyObject *obj;
146
147 if (type->tp_new == NULL) {
148 PyErr_Format(PyExc_TypeError,
149 "cannot create '%.100s' instances",
150 type->tp_name);
151 return NULL;
152 }
153
Tim Peters3f996e72001-09-13 19:18:27 +0000154 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000155 if (obj != NULL) {
156 type = obj->ob_type;
157 if (type->tp_init != NULL &&
158 type->tp_init(obj, args, kwds) < 0) {
159 Py_DECREF(obj);
160 obj = NULL;
161 }
162 }
163 return obj;
164}
165
166PyObject *
167PyType_GenericAlloc(PyTypeObject *type, int nitems)
168{
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000169#define PTRSIZE (sizeof(PyObject *))
170
Tim Peters6d6c1a32001-08-02 04:15:00 +0000171 int size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172 PyObject *obj;
173
174 /* Inline PyObject_New() so we can zero the memory */
175 size = _PyObject_VAR_SIZE(type, nitems);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000176 /* Round up size, if necessary, so we fully zero out __dict__ */
177 if (type->tp_itemsize % PTRSIZE != 0) {
178 size += PTRSIZE - 1;
179 size /= PTRSIZE;
180 size *= PTRSIZE;
181 }
Neil Schemenauerc806c882001-08-29 23:54:54 +0000182 if (PyType_IS_GC(type)) {
183 obj = _PyObject_GC_Malloc(type, nitems);
184 }
185 else {
186 obj = PyObject_MALLOC(size);
187 }
188 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000189 return PyErr_NoMemory();
Neil Schemenauerc806c882001-08-29 23:54:54 +0000190 memset(obj, '\0', size);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000191 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
192 Py_INCREF(type);
193 if (type->tp_itemsize == 0)
194 PyObject_INIT(obj, type);
195 else
196 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
197 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000198 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000199 return obj;
200}
201
202PyObject *
203PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
204{
205 return type->tp_alloc(type, 0);
206}
207
208/* Helper for subtyping */
209
210static void
211subtype_dealloc(PyObject *self)
212{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000213 PyTypeObject *type, *base;
214 destructor f;
215
216 /* This exists so we can DECREF self->ob_type */
217
218 /* Find the nearest base with a different tp_dealloc */
219 type = self->ob_type;
220 base = type->tp_base;
221 while ((f = base->tp_dealloc) == subtype_dealloc) {
222 base = base->tp_base;
223 assert(base);
224 }
225
226 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000227 if (type->tp_dictoffset && !base->tp_dictoffset) {
228 PyObject **dictptr = _PyObject_GetDictPtr(self);
229 if (dictptr != NULL) {
230 PyObject *dict = *dictptr;
231 if (dict != NULL) {
232 Py_DECREF(dict);
233 *dictptr = NULL;
234 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000235 }
236 }
237
Guido van Rossum9676b222001-08-17 20:32:36 +0000238 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000239 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000240 PyObject_ClearWeakRefs(self);
241
Tim Peters6d6c1a32001-08-02 04:15:00 +0000242 /* Finalize GC if the base doesn't do GC and we do */
243 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
244 PyObject_GC_Fini(self);
245
246 /* Call the base tp_dealloc() */
247 assert(f);
248 f(self);
249
250 /* Can't reference self beyond this point */
251 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
252 Py_DECREF(type);
253 }
254}
255
256staticforward void override_slots(PyTypeObject *type, PyObject *dict);
257staticforward PyTypeObject *solid_base(PyTypeObject *type);
258
259typedef struct {
260 PyTypeObject type;
261 PyNumberMethods as_number;
262 PySequenceMethods as_sequence;
263 PyMappingMethods as_mapping;
264 PyBufferProcs as_buffer;
265 PyObject *name, *slots;
266 struct memberlist members[1];
267} etype;
268
269/* type test with subclassing support */
270
271int
272PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
273{
274 PyObject *mro;
275
Guido van Rossum9478d072001-09-07 18:52:13 +0000276 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
277 return b == a || b == &PyBaseObject_Type;
278
Tim Peters6d6c1a32001-08-02 04:15:00 +0000279 mro = a->tp_mro;
280 if (mro != NULL) {
281 /* Deal with multiple inheritance without recursion
282 by walking the MRO tuple */
283 int i, n;
284 assert(PyTuple_Check(mro));
285 n = PyTuple_GET_SIZE(mro);
286 for (i = 0; i < n; i++) {
287 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
288 return 1;
289 }
290 return 0;
291 }
292 else {
293 /* a is not completely initilized yet; follow tp_base */
294 do {
295 if (a == b)
296 return 1;
297 a = a->tp_base;
298 } while (a != NULL);
299 return b == &PyBaseObject_Type;
300 }
301}
302
Guido van Rossum60718732001-08-28 17:47:51 +0000303/* Internal routine to do a method lookup in the type
304 without looking in the instance dictionary
305 (so we can't use PyObject_GetAttr) but still binding
306 it to the instance. The arguments are the object,
307 the method name as a C string, and the address of a
308 static variable used to cache the interned Python string. */
309
310static PyObject *
311lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
312{
313 PyObject *res;
314
315 if (*attrobj == NULL) {
316 *attrobj = PyString_InternFromString(attrstr);
317 if (*attrobj == NULL)
318 return NULL;
319 }
320 res = _PyType_Lookup(self->ob_type, *attrobj);
321 if (res == NULL)
322 PyErr_SetObject(PyExc_AttributeError, *attrobj);
323 else {
324 descrgetfunc f;
325 if ((f = res->ob_type->tp_descr_get) == NULL)
326 Py_INCREF(res);
327 else
328 res = f(res, self, (PyObject *)(self->ob_type));
329 }
330 return res;
331}
332
Guido van Rossum2730b132001-08-28 18:22:14 +0000333/* A variation of PyObject_CallMethod that uses lookup_method()
334 instead of PyObject_GetAttrString(). This uses the same convention
335 as lookup_method to cache the interned name string object. */
336
337PyObject *
338call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
339{
340 va_list va;
341 PyObject *args, *func = 0, *retval;
342 PyObject *dummy_str = NULL;
343 va_start(va, format);
344
345 func = lookup_method(o, name, &dummy_str);
346 Py_XDECREF(dummy_str);
347 if (func == NULL) {
348 va_end(va);
349 PyErr_SetString(PyExc_AttributeError, name);
Guido van Rossum717ce002001-09-14 16:58:08 +0000350 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000351 }
352
353 if (format && *format)
354 args = Py_VaBuildValue(format, va);
355 else
356 args = PyTuple_New(0);
357
358 va_end(va);
359
Guido van Rossum717ce002001-09-14 16:58:08 +0000360 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000361 return NULL;
362
Guido van Rossum717ce002001-09-14 16:58:08 +0000363 assert(PyTuple_Check(args));
364 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000365
366 Py_DECREF(args);
367 Py_DECREF(func);
368
369 return retval;
370}
371
Tim Peters6d6c1a32001-08-02 04:15:00 +0000372/* Method resolution order algorithm from "Putting Metaclasses to Work"
373 by Forman and Danforth (Addison-Wesley 1999). */
374
375static int
376conservative_merge(PyObject *left, PyObject *right)
377{
378 int left_size;
379 int right_size;
380 int i, j, r, ok;
381 PyObject *temp, *rr;
382
383 assert(PyList_Check(left));
384 assert(PyList_Check(right));
385
386 again:
387 left_size = PyList_GET_SIZE(left);
388 right_size = PyList_GET_SIZE(right);
389 for (i = 0; i < left_size; i++) {
390 for (j = 0; j < right_size; j++) {
391 if (PyList_GET_ITEM(left, i) ==
392 PyList_GET_ITEM(right, j)) {
393 /* found a merge point */
394 temp = PyList_New(0);
395 if (temp == NULL)
396 return -1;
397 for (r = 0; r < j; r++) {
398 rr = PyList_GET_ITEM(right, r);
399 ok = PySequence_Contains(left, rr);
400 if (ok < 0) {
401 Py_DECREF(temp);
402 return -1;
403 }
404 if (!ok) {
405 ok = PyList_Append(temp, rr);
406 if (ok < 0) {
407 Py_DECREF(temp);
408 return -1;
409 }
410 }
411 }
412 ok = PyList_SetSlice(left, i, i, temp);
413 Py_DECREF(temp);
414 if (ok < 0)
415 return -1;
416 ok = PyList_SetSlice(right, 0, j+1, NULL);
417 if (ok < 0)
418 return -1;
419 goto again;
420 }
421 }
422 }
423 return PyList_SetSlice(left, left_size, left_size, right);
424}
425
426static int
427serious_order_disagreements(PyObject *left, PyObject *right)
428{
429 return 0; /* XXX later -- for now, we cheat: "don't do that" */
430}
431
432static PyObject *
433mro_implementation(PyTypeObject *type)
434{
435 int i, n, ok;
436 PyObject *bases, *result;
437
438 bases = type->tp_bases;
439 n = PyTuple_GET_SIZE(bases);
440 result = Py_BuildValue("[O]", (PyObject *)type);
441 if (result == NULL)
442 return NULL;
443 for (i = 0; i < n; i++) {
444 PyTypeObject *base =
445 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
446 PyObject *parentMRO = PySequence_List(base->tp_mro);
447 if (parentMRO == NULL) {
448 Py_DECREF(result);
449 return NULL;
450 }
451 if (serious_order_disagreements(result, parentMRO)) {
452 Py_DECREF(result);
453 return NULL;
454 }
455 ok = conservative_merge(result, parentMRO);
456 Py_DECREF(parentMRO);
457 if (ok < 0) {
458 Py_DECREF(result);
459 return NULL;
460 }
461 }
462 return result;
463}
464
465static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000466mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467{
468 PyTypeObject *type = (PyTypeObject *)self;
469
Tim Peters6d6c1a32001-08-02 04:15:00 +0000470 return mro_implementation(type);
471}
472
473static int
474mro_internal(PyTypeObject *type)
475{
476 PyObject *mro, *result, *tuple;
477
478 if (type->ob_type == &PyType_Type) {
479 result = mro_implementation(type);
480 }
481 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000482 static PyObject *mro_str;
483 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000484 if (mro == NULL)
485 return -1;
486 result = PyObject_CallObject(mro, NULL);
487 Py_DECREF(mro);
488 }
489 if (result == NULL)
490 return -1;
491 tuple = PySequence_Tuple(result);
492 Py_DECREF(result);
493 type->tp_mro = tuple;
494 return 0;
495}
496
497
498/* Calculate the best base amongst multiple base classes.
499 This is the first one that's on the path to the "solid base". */
500
501static PyTypeObject *
502best_base(PyObject *bases)
503{
504 int i, n;
505 PyTypeObject *base, *winner, *candidate, *base_i;
506
507 assert(PyTuple_Check(bases));
508 n = PyTuple_GET_SIZE(bases);
509 assert(n > 0);
510 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
511 winner = &PyBaseObject_Type;
512 for (i = 0; i < n; i++) {
513 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
514 if (!PyType_Check((PyObject *)base_i)) {
515 PyErr_SetString(
516 PyExc_TypeError,
517 "bases must be types");
518 return NULL;
519 }
520 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000521 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000522 return NULL;
523 }
524 candidate = solid_base(base_i);
525 if (PyType_IsSubtype(winner, candidate))
526 ;
527 else if (PyType_IsSubtype(candidate, winner)) {
528 winner = candidate;
529 base = base_i;
530 }
531 else {
532 PyErr_SetString(
533 PyExc_TypeError,
534 "multiple bases have "
535 "instance lay-out conflict");
536 return NULL;
537 }
538 }
539 assert(base != NULL);
540 return base;
541}
542
543static int
544extra_ivars(PyTypeObject *type, PyTypeObject *base)
545{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000546 size_t t_size = type->tp_basicsize;
547 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000548
Guido van Rossum9676b222001-08-17 20:32:36 +0000549 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000550 if (type->tp_itemsize || base->tp_itemsize) {
551 /* If itemsize is involved, stricter rules */
552 return t_size != b_size ||
553 type->tp_itemsize != base->tp_itemsize;
554 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000555 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
556 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
557 t_size -= sizeof(PyObject *);
558 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
559 type->tp_dictoffset + sizeof(PyObject *) == t_size)
560 t_size -= sizeof(PyObject *);
561
562 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000563}
564
565static PyTypeObject *
566solid_base(PyTypeObject *type)
567{
568 PyTypeObject *base;
569
570 if (type->tp_base)
571 base = solid_base(type->tp_base);
572 else
573 base = &PyBaseObject_Type;
574 if (extra_ivars(type, base))
575 return type;
576 else
577 return base;
578}
579
580staticforward void object_dealloc(PyObject *);
581staticforward int object_init(PyObject *, PyObject *, PyObject *);
582
583static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000584subtype_dict(PyObject *obj, void *context)
585{
586 PyObject **dictptr = _PyObject_GetDictPtr(obj);
587 PyObject *dict;
588
589 if (dictptr == NULL) {
590 PyErr_SetString(PyExc_AttributeError,
591 "This object has no __dict__");
592 return NULL;
593 }
594 dict = *dictptr;
595 if (dict == NULL) {
596 Py_INCREF(Py_None);
597 return Py_None;
598 }
599 else {
600 Py_INCREF(dict);
601 return dict;
602 }
603}
604
605struct getsetlist subtype_getsets[] = {
606 {"__dict__", subtype_dict, NULL, NULL},
607 {0},
608};
609
610static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000611type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
612{
613 PyObject *name, *bases, *dict;
614 static char *kwlist[] = {"name", "bases", "dict", 0};
615 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000616 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000617 etype *et;
618 struct memberlist *mp;
Guido van Rossum9676b222001-08-17 20:32:36 +0000619 int i, nbases, nslots, slotoffset, dynamic, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000620
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000621 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000622 if (metatype == &PyType_Type &&
623 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
624 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000625 PyObject *x = PyTuple_GET_ITEM(args, 0);
626 Py_INCREF(x->ob_type);
627 return (PyObject *) x->ob_type;
628 }
629
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000630 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
632 &name,
633 &PyTuple_Type, &bases,
634 &PyDict_Type, &dict))
635 return NULL;
636
637 /* Determine the proper metatype to deal with this,
638 and check for metatype conflicts while we're at it.
639 Note that if some other metatype wins to contract,
640 it's possible that its instances are not types. */
641 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000642 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643 for (i = 0; i < nbases; i++) {
644 tmp = PyTuple_GET_ITEM(bases, i);
645 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000646 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000647 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000648 if (PyType_IsSubtype(tmptype, winner)) {
649 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000650 continue;
651 }
652 PyErr_SetString(PyExc_TypeError,
653 "metatype conflict among bases");
654 return NULL;
655 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000656 if (winner != metatype) {
657 if (winner->tp_new != type_new) /* Pass it to the winner */
658 return winner->tp_new(winner, args, kwds);
659 metatype = winner;
660 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000661
662 /* Adjust for empty tuple bases */
663 if (nbases == 0) {
664 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
665 if (bases == NULL)
666 return NULL;
667 nbases = 1;
668 }
669 else
670 Py_INCREF(bases);
671
672 /* XXX From here until type is allocated, "return NULL" leaks bases! */
673
674 /* Calculate best base, and check that all bases are type objects */
675 base = best_base(bases);
676 if (base == NULL)
677 return NULL;
678 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
679 PyErr_Format(PyExc_TypeError,
680 "type '%.100s' is not an acceptable base type",
681 base->tp_name);
682 return NULL;
683 }
684
Guido van Rossum1a493502001-08-17 16:47:50 +0000685 /* Should this be a dynamic class (i.e. modifiable __dict__)?
686 Look in two places for a variable named __dynamic__:
687 1) in the class dict
688 2) in the module dict (globals)
689 The first variable that is an int >= 0 is used.
690 Otherwise, a default is calculated from the base classes:
691 if any base class is dynamic, this class is dynamic; otherwise
692 it is static. */
693 dynamic = -1; /* Not yet determined */
694 /* Look in the class */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000695 tmp = PyDict_GetItemString(dict, "__dynamic__");
696 if (tmp != NULL) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000697 dynamic = PyInt_AsLong(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698 if (dynamic < 0)
Guido van Rossum1a493502001-08-17 16:47:50 +0000699 PyErr_Clear();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000700 }
Guido van Rossum1a493502001-08-17 16:47:50 +0000701 if (dynamic < 0) {
702 /* Look in the module globals */
703 tmp = PyEval_GetGlobals();
704 if (tmp != NULL) {
705 tmp = PyDict_GetItemString(tmp, "__dynamic__");
706 if (tmp != NULL) {
707 dynamic = PyInt_AsLong(tmp);
708 if (dynamic < 0)
709 PyErr_Clear();
710 }
711 }
712 }
713 if (dynamic < 0) {
714 /* Make a new class dynamic if any of its bases is
715 dynamic. This is not always the same as inheriting
716 the __dynamic__ class attribute! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000717 dynamic = 0;
718 for (i = 0; i < nbases; i++) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000719 tmptype = (PyTypeObject *)
720 PyTuple_GET_ITEM(bases, i);
721 if (tmptype->tp_flags &
722 Py_TPFLAGS_DYNAMICTYPE) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000723 dynamic = 1;
724 break;
725 }
726 }
727 }
728
729 /* Check for a __slots__ sequence variable in dict, and count it */
730 slots = PyDict_GetItemString(dict, "__slots__");
731 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000732 add_dict = 0;
733 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000734 if (slots != NULL) {
735 /* Make it into a tuple */
736 if (PyString_Check(slots))
737 slots = Py_BuildValue("(O)", slots);
738 else
739 slots = PySequence_Tuple(slots);
740 if (slots == NULL)
741 return NULL;
742 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000743 if (nslots > 0 && base->tp_itemsize != 0) {
744 PyErr_Format(PyExc_TypeError,
745 "nonempty __slots__ "
746 "not supported for subtype of '%s'",
747 base->tp_name);
748 return NULL;
749 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 for (i = 0; i < nslots; i++) {
751 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
752 PyErr_SetString(PyExc_TypeError,
753 "__slots__ must be a sequence of strings");
754 Py_DECREF(slots);
755 return NULL;
756 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000757 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000758 }
759 }
760 if (slots == NULL && base->tp_dictoffset == 0 &&
761 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000762 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000763 add_dict++;
764 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000765 if (slots == NULL && base->tp_weaklistoffset == 0 &&
766 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000767 nslots++;
768 add_weak++;
769 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770
771 /* XXX From here until type is safely allocated,
772 "return NULL" may leak slots! */
773
774 /* Allocate the type object */
775 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
776 if (type == NULL)
777 return NULL;
778
779 /* Keep name and slots alive in the extended type object */
780 et = (etype *)type;
781 Py_INCREF(name);
782 et->name = name;
783 et->slots = slots;
784
Guido van Rossumdc91b992001-08-08 22:26:22 +0000785 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
787 Py_TPFLAGS_BASETYPE;
788 if (dynamic)
789 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000790
791 /* It's a new-style number unless it specifically inherits any
792 old-style numeric behavior */
793 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
794 (base->tp_as_number == NULL))
795 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
796
797 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000798 type->tp_as_number = &et->as_number;
799 type->tp_as_sequence = &et->as_sequence;
800 type->tp_as_mapping = &et->as_mapping;
801 type->tp_as_buffer = &et->as_buffer;
802 type->tp_name = PyString_AS_STRING(name);
803
804 /* Set tp_base and tp_bases */
805 type->tp_bases = bases;
806 Py_INCREF(base);
807 type->tp_base = base;
808
809 /* Initialize tp_defined from passed-in dict */
810 type->tp_defined = dict = PyDict_Copy(dict);
811 if (dict == NULL) {
812 Py_DECREF(type);
813 return NULL;
814 }
815
Guido van Rossumc3542212001-08-16 09:18:56 +0000816 /* Set __module__ in the dict */
817 if (PyDict_GetItemString(dict, "__module__") == NULL) {
818 tmp = PyEval_GetGlobals();
819 if (tmp != NULL) {
820 tmp = PyDict_GetItemString(tmp, "__name__");
821 if (tmp != NULL) {
822 if (PyDict_SetItemString(dict, "__module__",
823 tmp) < 0)
824 return NULL;
825 }
826 }
827 }
828
Tim Peters6d6c1a32001-08-02 04:15:00 +0000829 /* Special-case __new__: if it's a plain function,
830 make it a static function */
831 tmp = PyDict_GetItemString(dict, "__new__");
832 if (tmp != NULL && PyFunction_Check(tmp)) {
833 tmp = PyStaticMethod_New(tmp);
834 if (tmp == NULL) {
835 Py_DECREF(type);
836 return NULL;
837 }
838 PyDict_SetItemString(dict, "__new__", tmp);
839 Py_DECREF(tmp);
840 }
841
842 /* Add descriptors for custom slots from __slots__, or for __dict__ */
843 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000844 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000845 if (slots != NULL) {
846 for (i = 0; i < nslots; i++, mp++) {
847 mp->name = PyString_AS_STRING(
848 PyTuple_GET_ITEM(slots, i));
849 mp->type = T_OBJECT;
850 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000851 if (base->tp_weaklistoffset == 0 &&
852 strcmp(mp->name, "__weakref__") == 0)
853 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000854 slotoffset += sizeof(PyObject *);
855 }
856 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000857 else {
858 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000859 if (base->tp_itemsize)
Tim Peters017cb2c2001-08-30 20:07:55 +0000860 type->tp_dictoffset = -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000861 else
862 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000863 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000864 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000865 }
866 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000867 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000868 type->tp_weaklistoffset = slotoffset;
869 mp->name = "__weakref__";
870 mp->type = T_OBJECT;
871 mp->offset = slotoffset;
872 mp->readonly = 1;
873 mp++;
874 slotoffset += sizeof(PyObject *);
875 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000876 }
877 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000878 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000879 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000880
881 /* Special case some slots */
882 if (type->tp_dictoffset != 0 || nslots > 0) {
883 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
884 type->tp_getattro = PyObject_GenericGetAttr;
885 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
886 type->tp_setattro = PyObject_GenericSetAttr;
887 }
888 type->tp_dealloc = subtype_dealloc;
889
890 /* Always override allocation strategy to use regular heap */
891 type->tp_alloc = PyType_GenericAlloc;
892 type->tp_free = _PyObject_Del;
893
894 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000895 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896 Py_DECREF(type);
897 return NULL;
898 }
899
900 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000901 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
902 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000903
Tim Peters6d6c1a32001-08-02 04:15:00 +0000904 return (PyObject *)type;
905}
906
907/* Internal API to look for a name through the MRO.
908 This returns a borrowed reference, and doesn't set an exception! */
909PyObject *
910_PyType_Lookup(PyTypeObject *type, PyObject *name)
911{
912 int i, n;
913 PyObject *mro, *res, *dict;
914
915 /* For static types, look in tp_dict */
916 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
917 dict = type->tp_dict;
918 assert(dict && PyDict_Check(dict));
919 return PyDict_GetItem(dict, name);
920 }
921
922 /* For dynamic types, look in tp_defined of types in MRO */
923 mro = type->tp_mro;
924 assert(PyTuple_Check(mro));
925 n = PyTuple_GET_SIZE(mro);
926 for (i = 0; i < n; i++) {
927 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
928 assert(PyType_Check(type));
929 dict = type->tp_defined;
930 assert(dict && PyDict_Check(dict));
931 res = PyDict_GetItem(dict, name);
932 if (res != NULL)
933 return res;
934 }
935 return NULL;
936}
937
938/* This is similar to PyObject_GenericGetAttr(),
939 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
940static PyObject *
941type_getattro(PyTypeObject *type, PyObject *name)
942{
943 PyTypeObject *metatype = type->ob_type;
944 PyObject *descr, *res;
945 descrgetfunc f;
946
947 /* Initialize this type (we'll assume the metatype is initialized) */
948 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000949 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950 return NULL;
951 }
952
953 /* Get a descriptor from the metatype */
954 descr = _PyType_Lookup(metatype, name);
955 f = NULL;
956 if (descr != NULL) {
957 f = descr->ob_type->tp_descr_get;
958 if (f != NULL && PyDescr_IsData(descr))
959 return f(descr,
960 (PyObject *)type, (PyObject *)metatype);
961 }
962
963 /* Look in tp_defined of this type and its bases */
964 res = _PyType_Lookup(type, name);
965 if (res != NULL) {
966 f = res->ob_type->tp_descr_get;
967 if (f != NULL)
968 return f(res, (PyObject *)NULL, (PyObject *)type);
969 Py_INCREF(res);
970 return res;
971 }
972
973 /* Use the descriptor from the metatype */
974 if (f != NULL) {
975 res = f(descr, (PyObject *)type, (PyObject *)metatype);
976 return res;
977 }
978 if (descr != NULL) {
979 Py_INCREF(descr);
980 return descr;
981 }
982
983 /* Give up */
984 PyErr_Format(PyExc_AttributeError,
985 "type object '%.50s' has no attribute '%.400s'",
986 type->tp_name, PyString_AS_STRING(name));
987 return NULL;
988}
989
990static int
991type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
992{
993 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
994 return PyObject_GenericSetAttr((PyObject *)type, name, value);
995 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
996 return -1;
997}
998
999static void
1000type_dealloc(PyTypeObject *type)
1001{
1002 etype *et;
1003
1004 /* Assert this is a heap-allocated type object */
1005 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
1006 et = (etype *)type;
1007 Py_XDECREF(type->tp_base);
1008 Py_XDECREF(type->tp_dict);
1009 Py_XDECREF(type->tp_bases);
1010 Py_XDECREF(type->tp_mro);
1011 Py_XDECREF(type->tp_defined);
1012 /* XXX more? */
1013 Py_XDECREF(et->name);
1014 Py_XDECREF(et->slots);
1015 type->ob_type->tp_free((PyObject *)type);
1016}
1017
1018static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001019 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001020 "mro() -> list\nreturn a type's method resolution order"},
1021 {0}
1022};
1023
1024static char type_doc[] =
1025"type(object) -> the object's type\n"
1026"type(name, bases, dict) -> a new type";
1027
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001028PyTypeObject PyType_Type = {
1029 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001030 0, /* ob_size */
1031 "type", /* tp_name */
1032 sizeof(etype), /* tp_basicsize */
1033 sizeof(struct memberlist), /* tp_itemsize */
1034 (destructor)type_dealloc, /* tp_dealloc */
1035 0, /* tp_print */
1036 0, /* tp_getattr */
1037 0, /* tp_setattr */
1038 type_compare, /* tp_compare */
1039 (reprfunc)type_repr, /* tp_repr */
1040 0, /* tp_as_number */
1041 0, /* tp_as_sequence */
1042 0, /* tp_as_mapping */
1043 (hashfunc)_Py_HashPointer, /* tp_hash */
1044 (ternaryfunc)type_call, /* tp_call */
1045 0, /* tp_str */
1046 (getattrofunc)type_getattro, /* tp_getattro */
1047 (setattrofunc)type_setattro, /* tp_setattro */
1048 0, /* tp_as_buffer */
1049 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1050 type_doc, /* tp_doc */
1051 0, /* tp_traverse */
1052 0, /* tp_clear */
1053 0, /* tp_richcompare */
1054 0, /* tp_weaklistoffset */
1055 0, /* tp_iter */
1056 0, /* tp_iternext */
1057 type_methods, /* tp_methods */
1058 type_members, /* tp_members */
1059 type_getsets, /* tp_getset */
1060 0, /* tp_base */
1061 0, /* tp_dict */
1062 0, /* tp_descr_get */
1063 0, /* tp_descr_set */
1064 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1065 0, /* tp_init */
1066 0, /* tp_alloc */
1067 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001068};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001069
1070
1071/* The base type of all types (eventually)... except itself. */
1072
1073static int
1074object_init(PyObject *self, PyObject *args, PyObject *kwds)
1075{
1076 return 0;
1077}
1078
1079static void
1080object_dealloc(PyObject *self)
1081{
1082 self->ob_type->tp_free(self);
1083}
1084
Guido van Rossum8e248182001-08-12 05:17:56 +00001085static PyObject *
1086object_repr(PyObject *self)
1087{
Guido van Rossum76e69632001-08-16 18:52:43 +00001088 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001089 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001090
Guido van Rossum76e69632001-08-16 18:52:43 +00001091 type = self->ob_type;
1092 mod = type_module(type, NULL);
1093 if (mod == NULL)
1094 PyErr_Clear();
1095 else if (!PyString_Check(mod)) {
1096 Py_DECREF(mod);
1097 mod = NULL;
1098 }
1099 name = type_name(type, NULL);
1100 if (name == NULL)
1101 return NULL;
1102 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Barry Warsaw7ce36942001-08-24 18:34:26 +00001103 rtn = PyString_FromFormat("<%s.%s instance at %p>",
1104 PyString_AS_STRING(mod),
1105 PyString_AS_STRING(name),
1106 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001107 else
Barry Warsaw7ce36942001-08-24 18:34:26 +00001108 rtn = PyString_FromFormat("<%s instance at %p>",
1109 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001110 Py_XDECREF(mod);
1111 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001112 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001113}
1114
Guido van Rossumb8f63662001-08-15 23:57:02 +00001115static PyObject *
1116object_str(PyObject *self)
1117{
1118 unaryfunc f;
1119
1120 f = self->ob_type->tp_repr;
1121 if (f == NULL)
1122 f = object_repr;
1123 return f(self);
1124}
1125
Guido van Rossum8e248182001-08-12 05:17:56 +00001126static long
1127object_hash(PyObject *self)
1128{
1129 return _Py_HashPointer(self);
1130}
Guido van Rossum8e248182001-08-12 05:17:56 +00001131
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132static void
1133object_free(PyObject *self)
1134{
1135 PyObject_Del(self);
1136}
1137
1138static struct memberlist object_members[] = {
1139 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
1140 {0}
1141};
1142
1143PyTypeObject PyBaseObject_Type = {
1144 PyObject_HEAD_INIT(&PyType_Type)
1145 0, /* ob_size */
1146 "object", /* tp_name */
1147 sizeof(PyObject), /* tp_basicsize */
1148 0, /* tp_itemsize */
1149 (destructor)object_dealloc, /* tp_dealloc */
1150 0, /* tp_print */
1151 0, /* tp_getattr */
1152 0, /* tp_setattr */
1153 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001154 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001155 0, /* tp_as_number */
1156 0, /* tp_as_sequence */
1157 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001158 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001159 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001160 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001162 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001163 0, /* tp_as_buffer */
1164 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1165 "The most base type", /* tp_doc */
1166 0, /* tp_traverse */
1167 0, /* tp_clear */
1168 0, /* tp_richcompare */
1169 0, /* tp_weaklistoffset */
1170 0, /* tp_iter */
1171 0, /* tp_iternext */
1172 0, /* tp_methods */
1173 object_members, /* tp_members */
1174 0, /* tp_getset */
1175 0, /* tp_base */
1176 0, /* tp_dict */
1177 0, /* tp_descr_get */
1178 0, /* tp_descr_set */
1179 0, /* tp_dictoffset */
1180 object_init, /* tp_init */
1181 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001182 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183 object_free, /* tp_free */
1184};
1185
1186
1187/* Initialize the __dict__ in a type object */
1188
1189static int
1190add_methods(PyTypeObject *type, PyMethodDef *meth)
1191{
1192 PyObject *dict = type->tp_defined;
1193
1194 for (; meth->ml_name != NULL; meth++) {
1195 PyObject *descr;
1196 if (PyDict_GetItemString(dict, meth->ml_name))
1197 continue;
1198 descr = PyDescr_NewMethod(type, meth);
1199 if (descr == NULL)
1200 return -1;
1201 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1202 return -1;
1203 Py_DECREF(descr);
1204 }
1205 return 0;
1206}
1207
1208static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209add_members(PyTypeObject *type, struct memberlist *memb)
1210{
1211 PyObject *dict = type->tp_defined;
1212
1213 for (; memb->name != NULL; memb++) {
1214 PyObject *descr;
1215 if (PyDict_GetItemString(dict, memb->name))
1216 continue;
1217 descr = PyDescr_NewMember(type, memb);
1218 if (descr == NULL)
1219 return -1;
1220 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1221 return -1;
1222 Py_DECREF(descr);
1223 }
1224 return 0;
1225}
1226
1227static int
1228add_getset(PyTypeObject *type, struct getsetlist *gsp)
1229{
1230 PyObject *dict = type->tp_defined;
1231
1232 for (; gsp->name != NULL; gsp++) {
1233 PyObject *descr;
1234 if (PyDict_GetItemString(dict, gsp->name))
1235 continue;
1236 descr = PyDescr_NewGetSet(type, gsp);
1237
1238 if (descr == NULL)
1239 return -1;
1240 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1241 return -1;
1242 Py_DECREF(descr);
1243 }
1244 return 0;
1245}
1246
Guido van Rossum13d52f02001-08-10 21:24:08 +00001247static void
1248inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001249{
1250 int oldsize, newsize;
1251
Guido van Rossum13d52f02001-08-10 21:24:08 +00001252 /* Special flag magic */
1253 if (!type->tp_as_buffer && base->tp_as_buffer) {
1254 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1255 type->tp_flags |=
1256 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1257 }
1258 if (!type->tp_as_sequence && base->tp_as_sequence) {
1259 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1260 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1261 }
1262 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1263 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1264 if ((!type->tp_as_number && base->tp_as_number) ||
1265 (!type->tp_as_sequence && base->tp_as_sequence)) {
1266 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1267 if (!type->tp_as_number && !type->tp_as_sequence) {
1268 type->tp_flags |= base->tp_flags &
1269 Py_TPFLAGS_HAVE_INPLACEOPS;
1270 }
1271 }
1272 /* Wow */
1273 }
1274 if (!type->tp_as_number && base->tp_as_number) {
1275 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1276 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1277 }
1278
1279 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001280 oldsize = base->tp_basicsize;
1281 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1282 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1283 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001284 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1285 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001286 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001287 if (type->tp_traverse == NULL)
1288 type->tp_traverse = base->tp_traverse;
1289 if (type->tp_clear == NULL)
1290 type->tp_clear = base->tp_clear;
1291 }
1292 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1293 if (base != &PyBaseObject_Type ||
1294 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1295 if (type->tp_new == NULL)
1296 type->tp_new = base->tp_new;
1297 }
1298 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001299 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001300
1301 /* Copy other non-function slots */
1302
1303#undef COPYVAL
1304#define COPYVAL(SLOT) \
1305 if (type->SLOT == 0) type->SLOT = base->SLOT
1306
1307 COPYVAL(tp_itemsize);
1308 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1309 COPYVAL(tp_weaklistoffset);
1310 }
1311 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1312 COPYVAL(tp_dictoffset);
1313 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001314}
1315
1316static void
1317inherit_slots(PyTypeObject *type, PyTypeObject *base)
1318{
1319 PyTypeObject *basebase;
1320
1321#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001322#undef COPYSLOT
1323#undef COPYNUM
1324#undef COPYSEQ
1325#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001326
1327#define SLOTDEFINED(SLOT) \
1328 (base->SLOT != 0 && \
1329 (basebase == NULL || base->SLOT != basebase->SLOT))
1330
Tim Peters6d6c1a32001-08-02 04:15:00 +00001331#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001332 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001333
1334#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1335#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1336#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1337
Guido van Rossum13d52f02001-08-10 21:24:08 +00001338 /* This won't inherit indirect slots (from tp_as_number etc.)
1339 if type doesn't provide the space. */
1340
1341 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1342 basebase = base->tp_base;
1343 if (basebase->tp_as_number == NULL)
1344 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001345 COPYNUM(nb_add);
1346 COPYNUM(nb_subtract);
1347 COPYNUM(nb_multiply);
1348 COPYNUM(nb_divide);
1349 COPYNUM(nb_remainder);
1350 COPYNUM(nb_divmod);
1351 COPYNUM(nb_power);
1352 COPYNUM(nb_negative);
1353 COPYNUM(nb_positive);
1354 COPYNUM(nb_absolute);
1355 COPYNUM(nb_nonzero);
1356 COPYNUM(nb_invert);
1357 COPYNUM(nb_lshift);
1358 COPYNUM(nb_rshift);
1359 COPYNUM(nb_and);
1360 COPYNUM(nb_xor);
1361 COPYNUM(nb_or);
1362 COPYNUM(nb_coerce);
1363 COPYNUM(nb_int);
1364 COPYNUM(nb_long);
1365 COPYNUM(nb_float);
1366 COPYNUM(nb_oct);
1367 COPYNUM(nb_hex);
1368 COPYNUM(nb_inplace_add);
1369 COPYNUM(nb_inplace_subtract);
1370 COPYNUM(nb_inplace_multiply);
1371 COPYNUM(nb_inplace_divide);
1372 COPYNUM(nb_inplace_remainder);
1373 COPYNUM(nb_inplace_power);
1374 COPYNUM(nb_inplace_lshift);
1375 COPYNUM(nb_inplace_rshift);
1376 COPYNUM(nb_inplace_and);
1377 COPYNUM(nb_inplace_xor);
1378 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001379 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1380 COPYNUM(nb_true_divide);
1381 COPYNUM(nb_floor_divide);
1382 COPYNUM(nb_inplace_true_divide);
1383 COPYNUM(nb_inplace_floor_divide);
1384 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001385 }
1386
Guido van Rossum13d52f02001-08-10 21:24:08 +00001387 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1388 basebase = base->tp_base;
1389 if (basebase->tp_as_sequence == NULL)
1390 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001391 COPYSEQ(sq_length);
1392 COPYSEQ(sq_concat);
1393 COPYSEQ(sq_repeat);
1394 COPYSEQ(sq_item);
1395 COPYSEQ(sq_slice);
1396 COPYSEQ(sq_ass_item);
1397 COPYSEQ(sq_ass_slice);
1398 COPYSEQ(sq_contains);
1399 COPYSEQ(sq_inplace_concat);
1400 COPYSEQ(sq_inplace_repeat);
1401 }
1402
Guido van Rossum13d52f02001-08-10 21:24:08 +00001403 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1404 basebase = base->tp_base;
1405 if (basebase->tp_as_mapping == NULL)
1406 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 COPYMAP(mp_length);
1408 COPYMAP(mp_subscript);
1409 COPYMAP(mp_ass_subscript);
1410 }
1411
Guido van Rossum13d52f02001-08-10 21:24:08 +00001412 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413
Tim Peters6d6c1a32001-08-02 04:15:00 +00001414 COPYSLOT(tp_dealloc);
1415 COPYSLOT(tp_print);
1416 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1417 type->tp_getattr = base->tp_getattr;
1418 type->tp_getattro = base->tp_getattro;
1419 }
1420 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1421 type->tp_setattr = base->tp_setattr;
1422 type->tp_setattro = base->tp_setattro;
1423 }
1424 /* tp_compare see tp_richcompare */
1425 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001426 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427 COPYSLOT(tp_call);
1428 COPYSLOT(tp_str);
1429 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001430 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001431 if (type->tp_compare == NULL &&
1432 type->tp_richcompare == NULL &&
1433 type->tp_hash == NULL)
1434 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001435 type->tp_compare = base->tp_compare;
1436 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001437 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 }
1439 }
1440 else {
1441 COPYSLOT(tp_compare);
1442 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1444 COPYSLOT(tp_iter);
1445 COPYSLOT(tp_iternext);
1446 }
1447 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1448 COPYSLOT(tp_descr_get);
1449 COPYSLOT(tp_descr_set);
1450 COPYSLOT(tp_dictoffset);
1451 COPYSLOT(tp_init);
1452 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001453 COPYSLOT(tp_free);
1454 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001455}
1456
Guido van Rossum13d52f02001-08-10 21:24:08 +00001457staticforward int add_operators(PyTypeObject *);
1458
Tim Peters6d6c1a32001-08-02 04:15:00 +00001459int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001460PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461{
1462 PyObject *dict, *bases, *x;
1463 PyTypeObject *base;
1464 int i, n;
1465
Guido van Rossumd614f972001-08-10 17:39:49 +00001466 if (type->tp_flags & Py_TPFLAGS_READY) {
1467 assert(type->tp_dict != NULL);
1468 return 0;
1469 }
1470 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1471 assert(type->tp_dict == NULL);
1472
1473 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001474
1475 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1476 base = type->tp_base;
1477 if (base == NULL && type != &PyBaseObject_Type)
1478 base = type->tp_base = &PyBaseObject_Type;
1479
1480 /* Initialize tp_bases */
1481 bases = type->tp_bases;
1482 if (bases == NULL) {
1483 if (base == NULL)
1484 bases = PyTuple_New(0);
1485 else
1486 bases = Py_BuildValue("(O)", base);
1487 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001488 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001489 type->tp_bases = bases;
1490 }
1491
1492 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001493 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001494 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001495 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001496 }
1497
1498 /* Initialize tp_defined */
1499 dict = type->tp_defined;
1500 if (dict == NULL) {
1501 dict = PyDict_New();
1502 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001503 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001504 type->tp_defined = dict;
1505 }
1506
1507 /* Add type-specific descriptors to tp_defined */
1508 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001509 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001510 if (type->tp_methods != NULL) {
1511 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001512 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001513 }
1514 if (type->tp_members != NULL) {
1515 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001516 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001517 }
1518 if (type->tp_getset != NULL) {
1519 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001520 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001521 }
1522
1523 /* Temporarily make tp_dict the same object as tp_defined.
1524 (This is needed to call mro(), and can stay this way for
1525 dynamic types). */
1526 Py_INCREF(type->tp_defined);
1527 type->tp_dict = type->tp_defined;
1528
1529 /* Calculate method resolution order */
1530 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001531 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001532 }
1533
Guido van Rossum13d52f02001-08-10 21:24:08 +00001534 /* Inherit special flags from dominant base */
1535 if (type->tp_base != NULL)
1536 inherit_special(type, type->tp_base);
1537
Tim Peters6d6c1a32001-08-02 04:15:00 +00001538 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001539 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001540 /* For a dynamic type, all slots are overridden */
1541 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001542 }
1543 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001544 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001545 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001546 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001547 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001548 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001549 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001550 bases = type->tp_mro;
1551 assert(bases != NULL);
1552 assert(PyTuple_Check(bases));
1553 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001554 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001555 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1556 assert(PyType_Check(base));
1557 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001558 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001559 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001560 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001561 }
1562 }
1563
Guido van Rossum13d52f02001-08-10 21:24:08 +00001564 /* Some more special stuff */
1565 base = type->tp_base;
1566 if (base != NULL) {
1567 if (type->tp_as_number == NULL)
1568 type->tp_as_number = base->tp_as_number;
1569 if (type->tp_as_sequence == NULL)
1570 type->tp_as_sequence = base->tp_as_sequence;
1571 if (type->tp_as_mapping == NULL)
1572 type->tp_as_mapping = base->tp_as_mapping;
1573 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001574
Guido van Rossum13d52f02001-08-10 21:24:08 +00001575 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001576 assert(type->tp_dict != NULL);
1577 type->tp_flags =
1578 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001579 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001580
1581 error:
1582 type->tp_flags &= ~Py_TPFLAGS_READYING;
1583 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001584}
1585
1586
1587/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1588
1589/* There's a wrapper *function* for each distinct function typedef used
1590 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1591 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1592 Most tables have only one entry; the tables for binary operators have two
1593 entries, one regular and one with reversed arguments. */
1594
1595static PyObject *
1596wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1597{
1598 inquiry func = (inquiry)wrapped;
1599 int res;
1600
1601 if (!PyArg_ParseTuple(args, ""))
1602 return NULL;
1603 res = (*func)(self);
1604 if (res == -1 && PyErr_Occurred())
1605 return NULL;
1606 return PyInt_FromLong((long)res);
1607}
1608
1609static struct wrapperbase tab_len[] = {
1610 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1611 {0}
1612};
1613
1614static PyObject *
1615wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1616{
1617 binaryfunc func = (binaryfunc)wrapped;
1618 PyObject *other;
1619
1620 if (!PyArg_ParseTuple(args, "O", &other))
1621 return NULL;
1622 return (*func)(self, other);
1623}
1624
1625static PyObject *
1626wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1627{
1628 binaryfunc func = (binaryfunc)wrapped;
1629 PyObject *other;
1630
1631 if (!PyArg_ParseTuple(args, "O", &other))
1632 return NULL;
1633 return (*func)(other, self);
1634}
1635
1636#undef BINARY
1637#define BINARY(NAME, OP) \
1638static struct wrapperbase tab_##NAME[] = { \
1639 {"__" #NAME "__", \
1640 (wrapperfunc)wrap_binaryfunc, \
1641 "x.__" #NAME "__(y) <==> " #OP}, \
1642 {"__r" #NAME "__", \
1643 (wrapperfunc)wrap_binaryfunc_r, \
1644 "y.__r" #NAME "__(x) <==> " #OP}, \
1645 {0} \
1646}
1647
1648BINARY(add, "x+y");
1649BINARY(sub, "x-y");
1650BINARY(mul, "x*y");
1651BINARY(div, "x/y");
1652BINARY(mod, "x%y");
1653BINARY(divmod, "divmod(x,y)");
1654BINARY(lshift, "x<<y");
1655BINARY(rshift, "x>>y");
1656BINARY(and, "x&y");
1657BINARY(xor, "x^y");
1658BINARY(or, "x|y");
1659
1660static PyObject *
1661wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1662{
1663 ternaryfunc func = (ternaryfunc)wrapped;
1664 PyObject *other;
1665 PyObject *third = Py_None;
1666
1667 /* Note: This wrapper only works for __pow__() */
1668
1669 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1670 return NULL;
1671 return (*func)(self, other, third);
1672}
1673
1674#undef TERNARY
1675#define TERNARY(NAME, OP) \
1676static struct wrapperbase tab_##NAME[] = { \
1677 {"__" #NAME "__", \
1678 (wrapperfunc)wrap_ternaryfunc, \
1679 "x.__" #NAME "__(y, z) <==> " #OP}, \
1680 {"__r" #NAME "__", \
1681 (wrapperfunc)wrap_ternaryfunc, \
1682 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1683 {0} \
1684}
1685
1686TERNARY(pow, "(x**y) % z");
1687
1688#undef UNARY
1689#define UNARY(NAME, OP) \
1690static struct wrapperbase tab_##NAME[] = { \
1691 {"__" #NAME "__", \
1692 (wrapperfunc)wrap_unaryfunc, \
1693 "x.__" #NAME "__() <==> " #OP}, \
1694 {0} \
1695}
1696
1697static PyObject *
1698wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1699{
1700 unaryfunc func = (unaryfunc)wrapped;
1701
1702 if (!PyArg_ParseTuple(args, ""))
1703 return NULL;
1704 return (*func)(self);
1705}
1706
1707UNARY(neg, "-x");
1708UNARY(pos, "+x");
1709UNARY(abs, "abs(x)");
1710UNARY(nonzero, "x != 0");
1711UNARY(invert, "~x");
1712UNARY(int, "int(x)");
1713UNARY(long, "long(x)");
1714UNARY(float, "float(x)");
1715UNARY(oct, "oct(x)");
1716UNARY(hex, "hex(x)");
1717
1718#undef IBINARY
1719#define IBINARY(NAME, OP) \
1720static struct wrapperbase tab_##NAME[] = { \
1721 {"__" #NAME "__", \
1722 (wrapperfunc)wrap_binaryfunc, \
1723 "x.__" #NAME "__(y) <==> " #OP}, \
1724 {0} \
1725}
1726
1727IBINARY(iadd, "x+=y");
1728IBINARY(isub, "x-=y");
1729IBINARY(imul, "x*=y");
1730IBINARY(idiv, "x/=y");
1731IBINARY(imod, "x%=y");
1732IBINARY(ilshift, "x<<=y");
1733IBINARY(irshift, "x>>=y");
1734IBINARY(iand, "x&=y");
1735IBINARY(ixor, "x^=y");
1736IBINARY(ior, "x|=y");
1737
1738#undef ITERNARY
1739#define ITERNARY(NAME, OP) \
1740static struct wrapperbase tab_##NAME[] = { \
1741 {"__" #NAME "__", \
1742 (wrapperfunc)wrap_ternaryfunc, \
1743 "x.__" #NAME "__(y) <==> " #OP}, \
1744 {0} \
1745}
1746
1747ITERNARY(ipow, "x = (x**y) % z");
1748
1749static struct wrapperbase tab_getitem[] = {
1750 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1751 "x.__getitem__(y) <==> x[y]"},
1752 {0}
1753};
1754
1755static PyObject *
1756wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1757{
1758 intargfunc func = (intargfunc)wrapped;
1759 int i;
1760
1761 if (!PyArg_ParseTuple(args, "i", &i))
1762 return NULL;
1763 return (*func)(self, i);
1764}
1765
1766static struct wrapperbase tab_mul_int[] = {
1767 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1768 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1769 {0}
1770};
1771
1772static struct wrapperbase tab_concat[] = {
1773 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1774 {0}
1775};
1776
1777static struct wrapperbase tab_imul_int[] = {
1778 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1779 {0}
1780};
1781
Guido van Rossum5d815f32001-08-17 21:57:47 +00001782static int
1783getindex(PyObject *self, PyObject *arg)
1784{
1785 int i;
1786
1787 i = PyInt_AsLong(arg);
1788 if (i == -1 && PyErr_Occurred())
1789 return -1;
1790 if (i < 0) {
1791 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
1792 if (sq && sq->sq_length) {
1793 int n = (*sq->sq_length)(self);
1794 if (n < 0)
1795 return -1;
1796 i += n;
1797 }
1798 }
1799 return i;
1800}
1801
1802static PyObject *
1803wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
1804{
1805 intargfunc func = (intargfunc)wrapped;
1806 PyObject *arg;
1807 int i;
1808
1809 if (!PyArg_ParseTuple(args, "O", &arg))
1810 return NULL;
1811 i = getindex(self, arg);
1812 if (i == -1 && PyErr_Occurred())
1813 return NULL;
1814 return (*func)(self, i);
1815}
1816
Tim Peters6d6c1a32001-08-02 04:15:00 +00001817static struct wrapperbase tab_getitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00001818 {"__getitem__", (wrapperfunc)wrap_sq_item,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001819 "x.__getitem__(i) <==> x[i]"},
1820 {0}
1821};
1822
1823static PyObject *
1824wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1825{
1826 intintargfunc func = (intintargfunc)wrapped;
1827 int i, j;
1828
1829 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1830 return NULL;
1831 return (*func)(self, i, j);
1832}
1833
1834static struct wrapperbase tab_getslice[] = {
1835 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1836 "x.__getslice__(i, j) <==> x[i:j]"},
1837 {0}
1838};
1839
1840static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00001841wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001842{
1843 intobjargproc func = (intobjargproc)wrapped;
1844 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00001845 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001846
Guido van Rossum5d815f32001-08-17 21:57:47 +00001847 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
1848 return NULL;
1849 i = getindex(self, arg);
1850 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00001851 return NULL;
1852 res = (*func)(self, i, value);
1853 if (res == -1 && PyErr_Occurred())
1854 return NULL;
1855 Py_INCREF(Py_None);
1856 return Py_None;
1857}
1858
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001859static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00001860wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001861{
1862 intobjargproc func = (intobjargproc)wrapped;
1863 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00001864 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001865
Guido van Rossum5d815f32001-08-17 21:57:47 +00001866 if (!PyArg_ParseTuple(args, "O", &arg))
1867 return NULL;
1868 i = getindex(self, arg);
1869 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001870 return NULL;
1871 res = (*func)(self, i, NULL);
1872 if (res == -1 && PyErr_Occurred())
1873 return NULL;
1874 Py_INCREF(Py_None);
1875 return Py_None;
1876}
1877
Tim Peters6d6c1a32001-08-02 04:15:00 +00001878static struct wrapperbase tab_setitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00001879 {"__setitem__", (wrapperfunc)wrap_sq_setitem,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001880 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum5d815f32001-08-17 21:57:47 +00001881 {"__delitem__", (wrapperfunc)wrap_sq_delitem,
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001882 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883 {0}
1884};
1885
1886static PyObject *
1887wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1888{
1889 intintobjargproc func = (intintobjargproc)wrapped;
1890 int i, j, res;
1891 PyObject *value;
1892
1893 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1894 return NULL;
1895 res = (*func)(self, i, j, value);
1896 if (res == -1 && PyErr_Occurred())
1897 return NULL;
1898 Py_INCREF(Py_None);
1899 return Py_None;
1900}
1901
1902static struct wrapperbase tab_setslice[] = {
1903 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1904 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1905 {0}
1906};
1907
1908/* XXX objobjproc is a misnomer; should be objargpred */
1909static PyObject *
1910wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1911{
1912 objobjproc func = (objobjproc)wrapped;
1913 int res;
1914 PyObject *value;
1915
1916 if (!PyArg_ParseTuple(args, "O", &value))
1917 return NULL;
1918 res = (*func)(self, value);
1919 if (res == -1 && PyErr_Occurred())
1920 return NULL;
1921 return PyInt_FromLong((long)res);
1922}
1923
1924static struct wrapperbase tab_contains[] = {
1925 {"__contains__", (wrapperfunc)wrap_objobjproc,
1926 "x.__contains__(y) <==> y in x"},
1927 {0}
1928};
1929
1930static PyObject *
1931wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1932{
1933 objobjargproc func = (objobjargproc)wrapped;
1934 int res;
1935 PyObject *key, *value;
1936
1937 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1938 return NULL;
1939 res = (*func)(self, key, value);
1940 if (res == -1 && PyErr_Occurred())
1941 return NULL;
1942 Py_INCREF(Py_None);
1943 return Py_None;
1944}
1945
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001946static PyObject *
1947wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1948{
1949 objobjargproc func = (objobjargproc)wrapped;
1950 int res;
1951 PyObject *key;
1952
1953 if (!PyArg_ParseTuple(args, "O", &key))
1954 return NULL;
1955 res = (*func)(self, key, NULL);
1956 if (res == -1 && PyErr_Occurred())
1957 return NULL;
1958 Py_INCREF(Py_None);
1959 return Py_None;
1960}
1961
Tim Peters6d6c1a32001-08-02 04:15:00 +00001962static struct wrapperbase tab_setitem[] = {
1963 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1964 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001965 {"__delitem__", (wrapperfunc)wrap_delitem,
1966 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001967 {0}
1968};
1969
1970static PyObject *
1971wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1972{
1973 cmpfunc func = (cmpfunc)wrapped;
1974 int res;
1975 PyObject *other;
1976
1977 if (!PyArg_ParseTuple(args, "O", &other))
1978 return NULL;
1979 res = (*func)(self, other);
1980 if (PyErr_Occurred())
1981 return NULL;
1982 return PyInt_FromLong((long)res);
1983}
1984
1985static struct wrapperbase tab_cmp[] = {
1986 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1987 "x.__cmp__(y) <==> cmp(x,y)"},
1988 {0}
1989};
1990
1991static struct wrapperbase tab_repr[] = {
1992 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1993 "x.__repr__() <==> repr(x)"},
1994 {0}
1995};
1996
1997static struct wrapperbase tab_getattr[] = {
1998 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1999 "x.__getattr__('name') <==> x.name"},
2000 {0}
2001};
2002
2003static PyObject *
2004wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2005{
2006 setattrofunc func = (setattrofunc)wrapped;
2007 int res;
2008 PyObject *name, *value;
2009
2010 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2011 return NULL;
2012 res = (*func)(self, name, value);
2013 if (res < 0)
2014 return NULL;
2015 Py_INCREF(Py_None);
2016 return Py_None;
2017}
2018
2019static PyObject *
2020wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2021{
2022 setattrofunc func = (setattrofunc)wrapped;
2023 int res;
2024 PyObject *name;
2025
2026 if (!PyArg_ParseTuple(args, "O", &name))
2027 return NULL;
2028 res = (*func)(self, name, NULL);
2029 if (res < 0)
2030 return NULL;
2031 Py_INCREF(Py_None);
2032 return Py_None;
2033}
2034
2035static struct wrapperbase tab_setattr[] = {
2036 {"__setattr__", (wrapperfunc)wrap_setattr,
2037 "x.__setattr__('name', value) <==> x.name = value"},
2038 {"__delattr__", (wrapperfunc)wrap_delattr,
2039 "x.__delattr__('name') <==> del x.name"},
2040 {0}
2041};
2042
2043static PyObject *
2044wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2045{
2046 hashfunc func = (hashfunc)wrapped;
2047 long res;
2048
2049 if (!PyArg_ParseTuple(args, ""))
2050 return NULL;
2051 res = (*func)(self);
2052 if (res == -1 && PyErr_Occurred())
2053 return NULL;
2054 return PyInt_FromLong(res);
2055}
2056
2057static struct wrapperbase tab_hash[] = {
2058 {"__hash__", (wrapperfunc)wrap_hashfunc,
2059 "x.__hash__() <==> hash(x)"},
2060 {0}
2061};
2062
2063static PyObject *
2064wrap_call(PyObject *self, PyObject *args, void *wrapped)
2065{
2066 ternaryfunc func = (ternaryfunc)wrapped;
2067
2068 /* XXX What about keyword arguments? */
2069 return (*func)(self, args, NULL);
2070}
2071
2072static struct wrapperbase tab_call[] = {
2073 {"__call__", (wrapperfunc)wrap_call,
2074 "x.__call__(...) <==> x(...)"},
2075 {0}
2076};
2077
2078static struct wrapperbase tab_str[] = {
2079 {"__str__", (wrapperfunc)wrap_unaryfunc,
2080 "x.__str__() <==> str(x)"},
2081 {0}
2082};
2083
2084static PyObject *
2085wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2086{
2087 richcmpfunc func = (richcmpfunc)wrapped;
2088 PyObject *other;
2089
2090 if (!PyArg_ParseTuple(args, "O", &other))
2091 return NULL;
2092 return (*func)(self, other, op);
2093}
2094
2095#undef RICHCMP_WRAPPER
2096#define RICHCMP_WRAPPER(NAME, OP) \
2097static PyObject * \
2098richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2099{ \
2100 return wrap_richcmpfunc(self, args, wrapped, OP); \
2101}
2102
Jack Jansen8e938b42001-08-08 15:29:49 +00002103RICHCMP_WRAPPER(lt, Py_LT)
2104RICHCMP_WRAPPER(le, Py_LE)
2105RICHCMP_WRAPPER(eq, Py_EQ)
2106RICHCMP_WRAPPER(ne, Py_NE)
2107RICHCMP_WRAPPER(gt, Py_GT)
2108RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109
2110#undef RICHCMP_ENTRY
2111#define RICHCMP_ENTRY(NAME, EXPR) \
2112 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
2113 "x.__" #NAME "__(y) <==> " EXPR}
2114
2115static struct wrapperbase tab_richcmp[] = {
2116 RICHCMP_ENTRY(lt, "x<y"),
2117 RICHCMP_ENTRY(le, "x<=y"),
2118 RICHCMP_ENTRY(eq, "x==y"),
2119 RICHCMP_ENTRY(ne, "x!=y"),
2120 RICHCMP_ENTRY(gt, "x>y"),
2121 RICHCMP_ENTRY(ge, "x>=y"),
2122 {0}
2123};
2124
2125static struct wrapperbase tab_iter[] = {
2126 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
2127 {0}
2128};
2129
2130static PyObject *
2131wrap_next(PyObject *self, PyObject *args, void *wrapped)
2132{
2133 unaryfunc func = (unaryfunc)wrapped;
2134 PyObject *res;
2135
2136 if (!PyArg_ParseTuple(args, ""))
2137 return NULL;
2138 res = (*func)(self);
2139 if (res == NULL && !PyErr_Occurred())
2140 PyErr_SetNone(PyExc_StopIteration);
2141 return res;
2142}
2143
2144static struct wrapperbase tab_next[] = {
2145 {"next", (wrapperfunc)wrap_next,
2146 "x.next() -> the next value, or raise StopIteration"},
2147 {0}
2148};
2149
2150static PyObject *
2151wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2152{
2153 descrgetfunc func = (descrgetfunc)wrapped;
2154 PyObject *obj;
2155 PyObject *type = NULL;
2156
2157 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2158 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002159 return (*func)(self, obj, type);
2160}
2161
2162static struct wrapperbase tab_descr_get[] = {
2163 {"__get__", (wrapperfunc)wrap_descr_get,
2164 "descr.__get__(obj, type) -> value"},
2165 {0}
2166};
2167
2168static PyObject *
2169wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
2170{
2171 descrsetfunc func = (descrsetfunc)wrapped;
2172 PyObject *obj, *value;
2173 int ret;
2174
2175 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2176 return NULL;
2177 ret = (*func)(self, obj, value);
2178 if (ret < 0)
2179 return NULL;
2180 Py_INCREF(Py_None);
2181 return Py_None;
2182}
2183
2184static struct wrapperbase tab_descr_set[] = {
2185 {"__set__", (wrapperfunc)wrap_descrsetfunc,
2186 "descr.__set__(obj, value)"},
2187 {0}
2188};
2189
2190static PyObject *
2191wrap_init(PyObject *self, PyObject *args, void *wrapped)
2192{
2193 initproc func = (initproc)wrapped;
2194
2195 /* XXX What about keyword arguments? */
2196 if (func(self, args, NULL) < 0)
2197 return NULL;
2198 Py_INCREF(Py_None);
2199 return Py_None;
2200}
2201
2202static struct wrapperbase tab_init[] = {
2203 {"__init__", (wrapperfunc)wrap_init,
2204 "x.__init__(...) initializes x; "
2205 "see x.__type__.__doc__ for signature"},
2206 {0}
2207};
2208
2209static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002210tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002211{
Barry Warsaw60f01882001-08-22 19:24:42 +00002212 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002213 PyObject *arg0, *res;
2214
2215 if (self == NULL || !PyType_Check(self))
2216 Py_FatalError("__new__() called with non-type 'self'");
2217 type = (PyTypeObject *)self;
2218 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002219 PyErr_Format(PyExc_TypeError,
2220 "%s.__new__(): not enough arguments",
2221 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002222 return NULL;
2223 }
2224 arg0 = PyTuple_GET_ITEM(args, 0);
2225 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002226 PyErr_Format(PyExc_TypeError,
2227 "%s.__new__(X): X is not a type object (%s)",
2228 type->tp_name,
2229 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002230 return NULL;
2231 }
2232 subtype = (PyTypeObject *)arg0;
2233 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002234 PyErr_Format(PyExc_TypeError,
2235 "%s.__new__(%s): %s is not a subtype of %s",
2236 type->tp_name,
2237 subtype->tp_name,
2238 subtype->tp_name,
2239 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002240 return NULL;
2241 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002242
2243 /* Check that the use doesn't do something silly and unsafe like
2244 object.__new__(dictionary). To do this, we check that the
2245 most derived base that's not a heap type is this type. */
2246 staticbase = subtype;
2247 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2248 staticbase = staticbase->tp_base;
2249 if (staticbase != type) {
2250 PyErr_Format(PyExc_TypeError,
2251 "%s.__new__(%s) is not safe, use %s.__new__()",
2252 type->tp_name,
2253 subtype->tp_name,
2254 staticbase == NULL ? "?" : staticbase->tp_name);
2255 return NULL;
2256 }
2257
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002258 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2259 if (args == NULL)
2260 return NULL;
2261 res = type->tp_new(subtype, args, kwds);
2262 Py_DECREF(args);
2263 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002264}
2265
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002266static struct PyMethodDef tp_new_methoddef[] = {
2267 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2268 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002269 {0}
2270};
2271
2272static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002273add_tp_new_wrapper(PyTypeObject *type)
2274{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002275 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002276
Guido van Rossumf040ede2001-08-07 16:40:56 +00002277 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2278 return 0;
2279 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002280 if (func == NULL)
2281 return -1;
2282 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2283}
2284
Guido van Rossum13d52f02001-08-10 21:24:08 +00002285static int
2286add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2287{
2288 PyObject *dict = type->tp_defined;
2289
2290 for (; wraps->name != NULL; wraps++) {
2291 PyObject *descr;
2292 if (PyDict_GetItemString(dict, wraps->name))
2293 continue;
2294 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2295 if (descr == NULL)
2296 return -1;
2297 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2298 return -1;
2299 Py_DECREF(descr);
2300 }
2301 return 0;
2302}
2303
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002304/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002305 dictionary with method descriptors for function slots. For each
2306 function slot (like tp_repr) that's defined in the type, one or
2307 more corresponding descriptors are added in the type's tp_defined
2308 dictionary under the appropriate name (like __repr__). Some
2309 function slots cause more than one descriptor to be added (for
2310 example, the nb_add slot adds both __add__ and __radd__
2311 descriptors) and some function slots compete for the same
2312 descriptor (for example both sq_item and mp_subscript generate a
2313 __getitem__ descriptor). This only adds new descriptors and
2314 doesn't overwrite entries in tp_defined that were previously
2315 defined. The descriptors contain a reference to the C function
2316 they must call, so that it's safe if they are copied into a
2317 subtype's __dict__ and the subtype has a different C function in
2318 its slot -- calling the method defined by the descriptor will call
2319 the C function that was used to create it, rather than the C
2320 function present in the slot when it is called. (This is important
2321 because a subtype may have a C function in the slot that calls the
2322 method from the dictionary, and we want to avoid infinite recursion
2323 here.) */
2324
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002325static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002326add_operators(PyTypeObject *type)
2327{
2328 PySequenceMethods *sq;
2329 PyMappingMethods *mp;
2330 PyNumberMethods *nb;
2331
2332#undef ADD
2333#define ADD(SLOT, TABLE) \
2334 if (SLOT) { \
2335 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2336 return -1; \
2337 }
2338
2339 if ((sq = type->tp_as_sequence) != NULL) {
2340 ADD(sq->sq_length, tab_len);
2341 ADD(sq->sq_concat, tab_concat);
2342 ADD(sq->sq_repeat, tab_mul_int);
2343 ADD(sq->sq_item, tab_getitem_int);
2344 ADD(sq->sq_slice, tab_getslice);
2345 ADD(sq->sq_ass_item, tab_setitem_int);
2346 ADD(sq->sq_ass_slice, tab_setslice);
2347 ADD(sq->sq_contains, tab_contains);
2348 ADD(sq->sq_inplace_concat, tab_iadd);
2349 ADD(sq->sq_inplace_repeat, tab_imul_int);
2350 }
2351
2352 if ((mp = type->tp_as_mapping) != NULL) {
2353 if (sq->sq_length == NULL)
2354 ADD(mp->mp_length, tab_len);
2355 ADD(mp->mp_subscript, tab_getitem);
2356 ADD(mp->mp_ass_subscript, tab_setitem);
2357 }
2358
2359 /* We don't support "old-style numbers" because their binary
2360 operators require that both arguments have the same type;
2361 the wrappers here only work for new-style numbers. */
2362 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2363 (nb = type->tp_as_number) != NULL) {
2364 ADD(nb->nb_add, tab_add);
2365 ADD(nb->nb_subtract, tab_sub);
2366 ADD(nb->nb_multiply, tab_mul);
2367 ADD(nb->nb_divide, tab_div);
2368 ADD(nb->nb_remainder, tab_mod);
2369 ADD(nb->nb_divmod, tab_divmod);
2370 ADD(nb->nb_power, tab_pow);
2371 ADD(nb->nb_negative, tab_neg);
2372 ADD(nb->nb_positive, tab_pos);
2373 ADD(nb->nb_absolute, tab_abs);
2374 ADD(nb->nb_nonzero, tab_nonzero);
2375 ADD(nb->nb_invert, tab_invert);
2376 ADD(nb->nb_lshift, tab_lshift);
2377 ADD(nb->nb_rshift, tab_rshift);
2378 ADD(nb->nb_and, tab_and);
2379 ADD(nb->nb_xor, tab_xor);
2380 ADD(nb->nb_or, tab_or);
2381 /* We don't support coerce() -- see above comment */
2382 ADD(nb->nb_int, tab_int);
2383 ADD(nb->nb_long, tab_long);
2384 ADD(nb->nb_float, tab_float);
2385 ADD(nb->nb_oct, tab_oct);
2386 ADD(nb->nb_hex, tab_hex);
2387 ADD(nb->nb_inplace_add, tab_iadd);
2388 ADD(nb->nb_inplace_subtract, tab_isub);
2389 ADD(nb->nb_inplace_multiply, tab_imul);
2390 ADD(nb->nb_inplace_divide, tab_idiv);
2391 ADD(nb->nb_inplace_remainder, tab_imod);
2392 ADD(nb->nb_inplace_power, tab_ipow);
2393 ADD(nb->nb_inplace_lshift, tab_ilshift);
2394 ADD(nb->nb_inplace_rshift, tab_irshift);
2395 ADD(nb->nb_inplace_and, tab_iand);
2396 ADD(nb->nb_inplace_xor, tab_ixor);
2397 ADD(nb->nb_inplace_or, tab_ior);
2398 }
2399
2400 ADD(type->tp_getattro, tab_getattr);
2401 ADD(type->tp_setattro, tab_setattr);
2402 ADD(type->tp_compare, tab_cmp);
2403 ADD(type->tp_repr, tab_repr);
2404 ADD(type->tp_hash, tab_hash);
2405 ADD(type->tp_call, tab_call);
2406 ADD(type->tp_str, tab_str);
2407 ADD(type->tp_richcompare, tab_richcmp);
2408 ADD(type->tp_iter, tab_iter);
2409 ADD(type->tp_iternext, tab_next);
2410 ADD(type->tp_descr_get, tab_descr_get);
2411 ADD(type->tp_descr_set, tab_descr_set);
2412 ADD(type->tp_init, tab_init);
2413
Guido van Rossumf040ede2001-08-07 16:40:56 +00002414 if (type->tp_new != NULL) {
2415 if (add_tp_new_wrapper(type) < 0)
2416 return -1;
2417 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418
2419 return 0;
2420}
2421
Guido van Rossumf040ede2001-08-07 16:40:56 +00002422/* Slot wrappers that call the corresponding __foo__ slot. See comments
2423 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002424
Guido van Rossumdc91b992001-08-08 22:26:22 +00002425#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002426static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002427FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002428{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002429 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002430 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002431}
2432
Guido van Rossumdc91b992001-08-08 22:26:22 +00002433#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002434static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002435FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002436{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002437 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002438 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002439}
2440
Guido van Rossumdc91b992001-08-08 22:26:22 +00002441
2442#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002443static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002444FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002445{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002446 static PyObject *cache_str, *rcache_str; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002447 if (self->ob_type->tp_as_number != NULL && \
2448 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2449 PyObject *r; \
Guido van Rossum2730b132001-08-28 18:22:14 +00002450 r = call_method( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002451 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002452 if (r != Py_NotImplemented || \
2453 other->ob_type == self->ob_type) \
2454 return r; \
2455 Py_DECREF(r); \
2456 } \
2457 if (other->ob_type->tp_as_number != NULL && \
2458 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
Guido van Rossum2730b132001-08-28 18:22:14 +00002459 return call_method( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002460 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002461 } \
2462 Py_INCREF(Py_NotImplemented); \
2463 return Py_NotImplemented; \
2464}
2465
2466#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2467 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2468
2469#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2470static PyObject * \
2471FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2472{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002473 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002474 return call_method(self, OPSTR, &cache_str, \
2475 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002476}
2477
2478static int
2479slot_sq_length(PyObject *self)
2480{
Guido van Rossum2730b132001-08-28 18:22:14 +00002481 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002482 PyObject *res = call_method(self, "__len__", &len_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002483
2484 if (res == NULL)
2485 return -1;
2486 return (int)PyInt_AsLong(res);
2487}
2488
Guido van Rossumdc91b992001-08-08 22:26:22 +00002489SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2490SLOT1(slot_sq_repeat, "__mul__", int, "i")
2491SLOT1(slot_sq_item, "__getitem__", int, "i")
2492SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002493
2494static int
2495slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2496{
2497 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002498 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002499
2500 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002501 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002502 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002503 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002504 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002505 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002506 if (res == NULL)
2507 return -1;
2508 Py_DECREF(res);
2509 return 0;
2510}
2511
2512static int
2513slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2514{
2515 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002516 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002517
2518 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002519 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002520 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002521 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002522 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002523 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002524 if (res == NULL)
2525 return -1;
2526 Py_DECREF(res);
2527 return 0;
2528}
2529
2530static int
2531slot_sq_contains(PyObject *self, PyObject *value)
2532{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002533 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002534 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002535
Guido van Rossum60718732001-08-28 17:47:51 +00002536 func = lookup_method(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002537
2538 if (func != NULL) {
2539 args = Py_BuildValue("(O)", value);
2540 if (args == NULL)
2541 res = NULL;
2542 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002543 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002544 Py_DECREF(args);
2545 }
2546 Py_DECREF(func);
2547 if (res == NULL)
2548 return -1;
2549 return PyObject_IsTrue(res);
2550 }
2551 else {
2552 PyErr_Clear();
Tim Peters16a77ad2001-09-08 04:00:12 +00002553 return _PySequence_IterSearch(self, value,
2554 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002555 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002556}
2557
Guido van Rossumdc91b992001-08-08 22:26:22 +00002558SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2559SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002560
2561#define slot_mp_length slot_sq_length
2562
Guido van Rossumdc91b992001-08-08 22:26:22 +00002563SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002564
2565static int
2566slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2567{
2568 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002569 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002570
2571 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002572 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002573 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002574 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002575 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002576 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002577 if (res == NULL)
2578 return -1;
2579 Py_DECREF(res);
2580 return 0;
2581}
2582
Guido van Rossumdc91b992001-08-08 22:26:22 +00002583SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2584SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2585SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2586SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2587SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2588SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2589
2590staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2591
2592SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2593 nb_power, "__pow__", "__rpow__")
2594
2595static PyObject *
2596slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2597{
Guido van Rossum2730b132001-08-28 18:22:14 +00002598 static PyObject *pow_str;
2599
Guido van Rossumdc91b992001-08-08 22:26:22 +00002600 if (modulus == Py_None)
2601 return slot_nb_power_binary(self, other);
2602 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002603 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002604 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002605}
2606
2607SLOT0(slot_nb_negative, "__neg__")
2608SLOT0(slot_nb_positive, "__pos__")
2609SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002610
2611static int
2612slot_nb_nonzero(PyObject *self)
2613{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002614 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002615 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002616
Guido van Rossum60718732001-08-28 17:47:51 +00002617 func = lookup_method(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002618 if (func == NULL) {
2619 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002620 func = lookup_method(self, "__len__", &len_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002621 }
2622
2623 if (func != NULL) {
Guido van Rossum717ce002001-09-14 16:58:08 +00002624 res = PyObject_CallObject(func, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002625 Py_DECREF(func);
2626 if (res == NULL)
2627 return -1;
2628 return PyObject_IsTrue(res);
2629 }
2630 else {
2631 PyErr_Clear();
2632 return 1;
2633 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002634}
2635
Guido van Rossumdc91b992001-08-08 22:26:22 +00002636SLOT0(slot_nb_invert, "__invert__")
2637SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2638SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2639SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2640SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2641SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002643SLOT0(slot_nb_int, "__int__")
2644SLOT0(slot_nb_long, "__long__")
2645SLOT0(slot_nb_float, "__float__")
2646SLOT0(slot_nb_oct, "__oct__")
2647SLOT0(slot_nb_hex, "__hex__")
2648SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2649SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2650SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2651SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2652SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2653SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2654SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2655SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2656SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2657SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2658SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2659SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2660 "__floordiv__", "__rfloordiv__")
2661SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2662SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2663SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002664
2665static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002666half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002667{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002668 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002669 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002670 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002671
Guido van Rossum60718732001-08-28 17:47:51 +00002672 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002673 if (func == NULL) {
2674 PyErr_Clear();
2675 }
2676 else {
2677 args = Py_BuildValue("(O)", other);
2678 if (args == NULL)
2679 res = NULL;
2680 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002681 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002682 Py_DECREF(args);
2683 }
2684 if (res != Py_NotImplemented) {
2685 if (res == NULL)
2686 return -2;
2687 c = PyInt_AsLong(res);
2688 Py_DECREF(res);
2689 if (c == -1 && PyErr_Occurred())
2690 return -2;
2691 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2692 }
2693 Py_DECREF(res);
2694 }
2695 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002696}
2697
Guido van Rossumb8f63662001-08-15 23:57:02 +00002698static int
2699slot_tp_compare(PyObject *self, PyObject *other)
2700{
2701 int c;
2702
2703 if (self->ob_type->tp_compare == slot_tp_compare) {
2704 c = half_compare(self, other);
2705 if (c <= 1)
2706 return c;
2707 }
2708 if (other->ob_type->tp_compare == slot_tp_compare) {
2709 c = half_compare(other, self);
2710 if (c < -1)
2711 return -2;
2712 if (c <= 1)
2713 return -c;
2714 }
2715 return (void *)self < (void *)other ? -1 :
2716 (void *)self > (void *)other ? 1 : 0;
2717}
2718
2719static PyObject *
2720slot_tp_repr(PyObject *self)
2721{
2722 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002723 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002724
Guido van Rossum60718732001-08-28 17:47:51 +00002725 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002726 if (func != NULL) {
2727 res = PyEval_CallObject(func, NULL);
2728 Py_DECREF(func);
2729 return res;
2730 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00002731 PyErr_Clear();
2732 return PyString_FromFormat("<%s object at %p>",
2733 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002734}
2735
2736static PyObject *
2737slot_tp_str(PyObject *self)
2738{
2739 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002740 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002741
Guido van Rossum60718732001-08-28 17:47:51 +00002742 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002743 if (func != NULL) {
2744 res = PyEval_CallObject(func, NULL);
2745 Py_DECREF(func);
2746 return res;
2747 }
2748 else {
2749 PyErr_Clear();
2750 return slot_tp_repr(self);
2751 }
2752}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002753
2754static long
2755slot_tp_hash(PyObject *self)
2756{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002757 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002758 static PyObject *hash_str, *eq_str, *cmp_str;
2759
Tim Peters6d6c1a32001-08-02 04:15:00 +00002760 long h;
2761
Guido van Rossum60718732001-08-28 17:47:51 +00002762 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002763
2764 if (func != NULL) {
2765 res = PyEval_CallObject(func, NULL);
2766 Py_DECREF(func);
2767 if (res == NULL)
2768 return -1;
2769 h = PyInt_AsLong(res);
2770 }
2771 else {
2772 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002773 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002774 if (func == NULL) {
2775 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002776 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002777 }
2778 if (func != NULL) {
2779 Py_DECREF(func);
2780 PyErr_SetString(PyExc_TypeError, "unhashable type");
2781 return -1;
2782 }
2783 PyErr_Clear();
2784 h = _Py_HashPointer((void *)self);
2785 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002786 if (h == -1 && !PyErr_Occurred())
2787 h = -2;
2788 return h;
2789}
2790
2791static PyObject *
2792slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2793{
Guido van Rossum60718732001-08-28 17:47:51 +00002794 static PyObject *call_str;
2795 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002796 PyObject *res;
2797
2798 if (meth == NULL)
2799 return NULL;
2800 res = PyObject_Call(meth, args, kwds);
2801 Py_DECREF(meth);
2802 return res;
2803}
2804
Tim Peters6d6c1a32001-08-02 04:15:00 +00002805static PyObject *
2806slot_tp_getattro(PyObject *self, PyObject *name)
2807{
2808 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002809 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002810 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002811
Guido van Rossum8e248182001-08-12 05:17:56 +00002812 if (getattr_str == NULL) {
2813 getattr_str = PyString_InternFromString("__getattr__");
2814 if (getattr_str == NULL)
2815 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002816 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002817 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002818 if (getattr == NULL) {
2819 /* Avoid further slowdowns */
2820 if (tp->tp_getattro == slot_tp_getattro)
2821 tp->tp_getattro = PyObject_GenericGetAttr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002822 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002823 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002824 return PyObject_CallFunction(getattr, "OO", self, name);
2825}
2826
2827static int
2828slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2829{
2830 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002831 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002832
2833 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002834 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002835 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002836 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002837 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002838 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002839 if (res == NULL)
2840 return -1;
2841 Py_DECREF(res);
2842 return 0;
2843}
2844
2845/* Map rich comparison operators to their __xx__ namesakes */
2846static char *name_op[] = {
2847 "__lt__",
2848 "__le__",
2849 "__eq__",
2850 "__ne__",
2851 "__gt__",
2852 "__ge__",
2853};
2854
2855static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002856half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002857{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002858 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002859 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00002860
Guido van Rossum60718732001-08-28 17:47:51 +00002861 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002862 if (func == NULL) {
2863 PyErr_Clear();
2864 Py_INCREF(Py_NotImplemented);
2865 return Py_NotImplemented;
2866 }
2867 args = Py_BuildValue("(O)", other);
2868 if (args == NULL)
2869 res = NULL;
2870 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002871 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002872 Py_DECREF(args);
2873 }
2874 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002875 return res;
2876}
2877
Guido van Rossumb8f63662001-08-15 23:57:02 +00002878/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2879static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2880
2881static PyObject *
2882slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2883{
2884 PyObject *res;
2885
2886 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2887 res = half_richcompare(self, other, op);
2888 if (res != Py_NotImplemented)
2889 return res;
2890 Py_DECREF(res);
2891 }
2892 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2893 res = half_richcompare(other, self, swapped_op[op]);
2894 if (res != Py_NotImplemented) {
2895 return res;
2896 }
2897 Py_DECREF(res);
2898 }
2899 Py_INCREF(Py_NotImplemented);
2900 return Py_NotImplemented;
2901}
2902
2903static PyObject *
2904slot_tp_iter(PyObject *self)
2905{
2906 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002907 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002908
Guido van Rossum60718732001-08-28 17:47:51 +00002909 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002910 if (func != NULL) {
2911 res = PyObject_CallObject(func, NULL);
2912 Py_DECREF(func);
2913 return res;
2914 }
2915 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00002916 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002917 if (func == NULL) {
2918 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2919 return NULL;
2920 }
2921 Py_DECREF(func);
2922 return PySeqIter_New(self);
2923}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002924
2925static PyObject *
2926slot_tp_iternext(PyObject *self)
2927{
Guido van Rossum2730b132001-08-28 18:22:14 +00002928 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002929 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002930}
2931
Guido van Rossum1a493502001-08-17 16:47:50 +00002932static PyObject *
2933slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
2934{
2935 PyTypeObject *tp = self->ob_type;
2936 PyObject *get;
2937 static PyObject *get_str = NULL;
2938
2939 if (get_str == NULL) {
2940 get_str = PyString_InternFromString("__get__");
2941 if (get_str == NULL)
2942 return NULL;
2943 }
2944 get = _PyType_Lookup(tp, get_str);
2945 if (get == NULL) {
2946 /* Avoid further slowdowns */
2947 if (tp->tp_descr_get == slot_tp_descr_get)
2948 tp->tp_descr_get = NULL;
2949 Py_INCREF(self);
2950 return self;
2951 }
Guido van Rossum2c252392001-08-24 10:13:31 +00002952 if (obj == NULL)
2953 obj = Py_None;
2954 if (type == NULL)
2955 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00002956 return PyObject_CallFunction(get, "OOO", self, obj, type);
2957}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002958
2959static int
2960slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2961{
Guido van Rossum2c252392001-08-24 10:13:31 +00002962 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002963 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00002964
2965 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002966 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002967 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00002968 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002969 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002970 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002971 if (res == NULL)
2972 return -1;
2973 Py_DECREF(res);
2974 return 0;
2975}
2976
2977static int
2978slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2979{
Guido van Rossum60718732001-08-28 17:47:51 +00002980 static PyObject *init_str;
2981 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002982 PyObject *res;
2983
2984 if (meth == NULL)
2985 return -1;
2986 res = PyObject_Call(meth, args, kwds);
2987 Py_DECREF(meth);
2988 if (res == NULL)
2989 return -1;
2990 Py_DECREF(res);
2991 return 0;
2992}
2993
2994static PyObject *
2995slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2996{
2997 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2998 PyObject *newargs, *x;
2999 int i, n;
3000
3001 if (func == NULL)
3002 return NULL;
3003 assert(PyTuple_Check(args));
3004 n = PyTuple_GET_SIZE(args);
3005 newargs = PyTuple_New(n+1);
3006 if (newargs == NULL)
3007 return NULL;
3008 Py_INCREF(type);
3009 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3010 for (i = 0; i < n; i++) {
3011 x = PyTuple_GET_ITEM(args, i);
3012 Py_INCREF(x);
3013 PyTuple_SET_ITEM(newargs, i+1, x);
3014 }
3015 x = PyObject_Call(func, newargs, kwds);
3016 Py_DECREF(func);
3017 return x;
3018}
3019
Guido van Rossumf040ede2001-08-07 16:40:56 +00003020/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003021 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00003022 The dict argument is the dictionary argument passed to type_new(),
3023 which is the local namespace of the class statement, in other
3024 words, it contains the methods. For each special method (like
3025 __repr__) defined in the dictionary, the corresponding function
3026 slot in the type object (like tp_repr) is set to a special function
3027 whose name is 'slot_' followed by the slot name and whose signature
3028 is whatever is required for that slot. These slot functions look
3029 up the corresponding method in the type's dictionary and call it.
3030 The slot functions have to take care of the various peculiarities
3031 of the mapping between slots and special methods, such as mapping
3032 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
3033 etc.) or mapping multiple slots to a single method (sq_item,
3034 mp_subscript <--> __getitem__). */
3035
Tim Peters6d6c1a32001-08-02 04:15:00 +00003036static void
3037override_slots(PyTypeObject *type, PyObject *dict)
3038{
3039 PySequenceMethods *sq = type->tp_as_sequence;
3040 PyMappingMethods *mp = type->tp_as_mapping;
3041 PyNumberMethods *nb = type->tp_as_number;
3042
Guido van Rossumdc91b992001-08-08 22:26:22 +00003043#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003044 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003045 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003046 }
3047
Guido van Rossumdc91b992001-08-08 22:26:22 +00003048#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003049 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003050 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003051 }
3052
Guido van Rossumdc91b992001-08-08 22:26:22 +00003053#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003054 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003055 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003056 }
3057
Guido van Rossumdc91b992001-08-08 22:26:22 +00003058#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003059 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003060 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003061 }
3062
Guido van Rossumdc91b992001-08-08 22:26:22 +00003063 SQSLOT("__len__", sq_length, slot_sq_length);
3064 SQSLOT("__add__", sq_concat, slot_sq_concat);
3065 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
3066 SQSLOT("__getitem__", sq_item, slot_sq_item);
3067 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
3068 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
3069 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
3070 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
3071 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
3072 SQSLOT("__contains__", sq_contains, slot_sq_contains);
3073 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
3074 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003075
Guido van Rossumdc91b992001-08-08 22:26:22 +00003076 MPSLOT("__len__", mp_length, slot_mp_length);
3077 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
3078 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
3079 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003080
Guido van Rossumdc91b992001-08-08 22:26:22 +00003081 NBSLOT("__add__", nb_add, slot_nb_add);
3082 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
3083 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
3084 NBSLOT("__div__", nb_divide, slot_nb_divide);
3085 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
3086 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
3087 NBSLOT("__pow__", nb_power, slot_nb_power);
3088 NBSLOT("__neg__", nb_negative, slot_nb_negative);
3089 NBSLOT("__pos__", nb_positive, slot_nb_positive);
3090 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
3091 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
3092 NBSLOT("__invert__", nb_invert, slot_nb_invert);
3093 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
3094 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
3095 NBSLOT("__and__", nb_and, slot_nb_and);
3096 NBSLOT("__xor__", nb_xor, slot_nb_xor);
3097 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003098 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00003099 NBSLOT("__int__", nb_int, slot_nb_int);
3100 NBSLOT("__long__", nb_long, slot_nb_long);
3101 NBSLOT("__float__", nb_float, slot_nb_float);
3102 NBSLOT("__oct__", nb_oct, slot_nb_oct);
3103 NBSLOT("__hex__", nb_hex, slot_nb_hex);
3104 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
3105 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
3106 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
3107 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
3108 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
3109 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
3110 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
3111 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
3112 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
3113 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
3114 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
3115 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
3116 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
3117 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3118 slot_nb_inplace_floor_divide);
3119 NBSLOT("__itruediv__", nb_inplace_true_divide,
3120 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003121
Guido van Rossum8e248182001-08-12 05:17:56 +00003122 if (dict == NULL ||
3123 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00003124 PyDict_GetItemString(dict, "__repr__"))
3125 type->tp_print = NULL;
3126
Guido van Rossumdc91b992001-08-08 22:26:22 +00003127 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
3128 TPSLOT("__repr__", tp_repr, slot_tp_repr);
3129 TPSLOT("__hash__", tp_hash, slot_tp_hash);
3130 TPSLOT("__call__", tp_call, slot_tp_call);
3131 TPSLOT("__str__", tp_str, slot_tp_str);
3132 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
3133 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
3134 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
3135 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
3136 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
3137 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
3138 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
3139 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
3140 TPSLOT("__iter__", tp_iter, slot_tp_iter);
3141 TPSLOT("next", tp_iternext, slot_tp_iternext);
3142 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
3143 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
3144 TPSLOT("__init__", tp_init, slot_tp_init);
3145 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003146}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003147
3148
3149/* Cooperative 'super' */
3150
3151typedef struct {
3152 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003153 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003154 PyObject *obj;
3155} superobject;
3156
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003157static struct memberlist super_members[] = {
3158 {"__type__", T_OBJECT, offsetof(superobject, type), READONLY},
3159 {"__obj__", T_OBJECT, offsetof(superobject, obj), READONLY},
3160 {0}
3161};
3162
Guido van Rossum705f0f52001-08-24 16:47:00 +00003163static void
3164super_dealloc(PyObject *self)
3165{
3166 superobject *su = (superobject *)self;
3167
3168 Py_XDECREF(su->obj);
3169 Py_XDECREF(su->type);
3170 self->ob_type->tp_free(self);
3171}
3172
3173static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003174super_repr(PyObject *self)
3175{
3176 superobject *su = (superobject *)self;
3177
3178 if (su->obj)
3179 return PyString_FromFormat(
3180 "<super: <type '%s'>, <%s object>>",
3181 su->type ? su->type->tp_name : "NULL",
3182 su->obj->ob_type->tp_name);
3183 else
3184 return PyString_FromFormat(
3185 "<super: <type '%s'>, NULL>",
3186 su->type ? su->type->tp_name : "NULL");
3187}
3188
3189static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003190super_getattro(PyObject *self, PyObject *name)
3191{
3192 superobject *su = (superobject *)self;
3193
3194 if (su->obj != NULL) {
3195 PyObject *mro, *res, *tmp;
3196 descrgetfunc f;
3197 int i, n;
3198
Guido van Rossume705ef12001-08-29 15:47:06 +00003199 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003200 if (mro == NULL)
3201 n = 0;
3202 else {
3203 assert(PyTuple_Check(mro));
3204 n = PyTuple_GET_SIZE(mro);
3205 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003206 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003207 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003208 break;
3209 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003210 if (i >= n && PyType_Check(su->obj)) {
3211 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003212 if (mro == NULL)
3213 n = 0;
3214 else {
3215 assert(PyTuple_Check(mro));
3216 n = PyTuple_GET_SIZE(mro);
3217 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003218 for (i = 0; i < n; i++) {
3219 if ((PyObject *)(su->type) ==
3220 PyTuple_GET_ITEM(mro, i))
3221 break;
3222 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003223 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003224 i++;
3225 res = NULL;
3226 for (; i < n; i++) {
3227 tmp = PyTuple_GET_ITEM(mro, i);
3228 assert(PyType_Check(tmp));
3229 res = PyDict_GetItem(
3230 ((PyTypeObject *)tmp)->tp_defined, name);
3231 if (res != NULL) {
3232 Py_INCREF(res);
3233 f = res->ob_type->tp_descr_get;
3234 if (f != NULL) {
3235 tmp = f(res, su->obj, res);
3236 Py_DECREF(res);
3237 res = tmp;
3238 }
3239 return res;
3240 }
3241 }
3242 }
3243 return PyObject_GenericGetAttr(self, name);
3244}
3245
3246static PyObject *
3247super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3248{
3249 superobject *su = (superobject *)self;
3250 superobject *new;
3251
3252 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3253 /* Not binding to an object, or already bound */
3254 Py_INCREF(self);
3255 return self;
3256 }
3257 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3258 if (new == NULL)
3259 return NULL;
3260 Py_INCREF(su->type);
3261 Py_INCREF(obj);
3262 new->type = su->type;
3263 new->obj = obj;
3264 return (PyObject *)new;
3265}
3266
3267static int
3268super_init(PyObject *self, PyObject *args, PyObject *kwds)
3269{
3270 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003271 PyTypeObject *type;
3272 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003273
3274 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3275 return -1;
3276 if (obj == Py_None)
3277 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003278 if (obj != NULL &&
3279 !PyType_IsSubtype(obj->ob_type, type) &&
3280 !(PyType_Check(obj) &&
3281 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003282 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003283 "super(type, obj): "
3284 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003285 return -1;
3286 }
3287 Py_INCREF(type);
3288 Py_XINCREF(obj);
3289 su->type = type;
3290 su->obj = obj;
3291 return 0;
3292}
3293
3294static char super_doc[] =
3295"super(type) -> unbound super object\n"
3296"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003297"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003298"Typical use to call a cooperative superclass method:\n"
3299"class C(B):\n"
3300" def meth(self, arg):\n"
3301" super(C, self).meth(arg)";
3302
3303PyTypeObject PySuper_Type = {
3304 PyObject_HEAD_INIT(&PyType_Type)
3305 0, /* ob_size */
3306 "super", /* tp_name */
3307 sizeof(superobject), /* tp_basicsize */
3308 0, /* tp_itemsize */
3309 /* methods */
3310 super_dealloc, /* tp_dealloc */
3311 0, /* tp_print */
3312 0, /* tp_getattr */
3313 0, /* tp_setattr */
3314 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003315 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003316 0, /* tp_as_number */
3317 0, /* tp_as_sequence */
3318 0, /* tp_as_mapping */
3319 0, /* tp_hash */
3320 0, /* tp_call */
3321 0, /* tp_str */
3322 super_getattro, /* tp_getattro */
3323 0, /* tp_setattro */
3324 0, /* tp_as_buffer */
Guido van Rossum31bcff82001-08-30 04:37:15 +00003325 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003326 super_doc, /* tp_doc */
3327 0, /* tp_traverse */
3328 0, /* tp_clear */
3329 0, /* tp_richcompare */
3330 0, /* tp_weaklistoffset */
3331 0, /* tp_iter */
3332 0, /* tp_iternext */
3333 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003334 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003335 0, /* tp_getset */
3336 0, /* tp_base */
3337 0, /* tp_dict */
3338 super_descr_get, /* tp_descr_get */
3339 0, /* tp_descr_set */
3340 0, /* tp_dictoffset */
3341 super_init, /* tp_init */
3342 PyType_GenericAlloc, /* tp_alloc */
3343 PyType_GenericNew, /* tp_new */
3344 _PyObject_Del, /* tp_free */
3345};