blob: c4dc3640e768054c2f3cd67490e508af403675b0 [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},
12 {"__weaklistoffset__", T_LONG,
13 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{
Guido van Rossumc3542212001-08-16 09:18:56 +0000116 PyObject *mod, *name;
117 char buf[200];
118
119 mod = type_module(type, NULL);
120 if (mod == NULL)
121 PyErr_Clear();
122 else if (!PyString_Check(mod)) {
123 Py_DECREF(mod);
124 mod = NULL;
125 }
126 name = type_name(type, NULL);
127 if (name == NULL)
128 return NULL;
129 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
130 sprintf(buf, "<type '%.80s.%.80s'>",
131 PyString_AS_STRING(mod),
132 PyString_AS_STRING(name));
133 else
134 sprintf(buf, "<type '%.80s'>", type->tp_name);
135 Py_XDECREF(mod);
136 Py_DECREF(name);
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000137 return PyString_FromString(buf);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000138}
139
Tim Peters6d6c1a32001-08-02 04:15:00 +0000140static PyObject *
141type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
142{
143 PyObject *obj;
144
145 if (type->tp_new == NULL) {
146 PyErr_Format(PyExc_TypeError,
147 "cannot create '%.100s' instances",
148 type->tp_name);
149 return NULL;
150 }
151
152 obj = type->tp_new(type, args, NULL);
153 if (obj != NULL) {
154 type = obj->ob_type;
155 if (type->tp_init != NULL &&
156 type->tp_init(obj, args, kwds) < 0) {
157 Py_DECREF(obj);
158 obj = NULL;
159 }
160 }
161 return obj;
162}
163
164PyObject *
165PyType_GenericAlloc(PyTypeObject *type, int nitems)
166{
167 int size;
168 void *mem;
169 PyObject *obj;
170
171 /* Inline PyObject_New() so we can zero the memory */
172 size = _PyObject_VAR_SIZE(type, nitems);
173 mem = PyObject_MALLOC(size);
174 if (mem == NULL)
175 return PyErr_NoMemory();
176 memset(mem, '\0', size);
177 if (PyType_IS_GC(type))
178 obj = PyObject_FROM_GC(mem);
179 else
180 obj = (PyObject *)mem;
181 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
182 Py_INCREF(type);
183 if (type->tp_itemsize == 0)
184 PyObject_INIT(obj, type);
185 else
186 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
187 if (PyType_IS_GC(type))
188 PyObject_GC_Init(obj);
189 return obj;
190}
191
192PyObject *
193PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
194{
195 return type->tp_alloc(type, 0);
196}
197
198/* Helper for subtyping */
199
200static void
201subtype_dealloc(PyObject *self)
202{
203 int dictoffset = self->ob_type->tp_dictoffset;
204 PyTypeObject *type, *base;
205 destructor f;
206
207 /* This exists so we can DECREF self->ob_type */
208
209 /* Find the nearest base with a different tp_dealloc */
210 type = self->ob_type;
211 base = type->tp_base;
212 while ((f = base->tp_dealloc) == subtype_dealloc) {
213 base = base->tp_base;
214 assert(base);
215 }
216
217 /* If we added a dict, DECREF it */
218 if (dictoffset && !base->tp_dictoffset) {
219 PyObject **dictptr = (PyObject **) ((char *)self + dictoffset);
220 PyObject *dict = *dictptr;
221 if (dict != NULL) {
222 Py_DECREF(dict);
223 *dictptr = NULL;
224 }
225 }
226
227 /* Finalize GC if the base doesn't do GC and we do */
228 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
229 PyObject_GC_Fini(self);
230
231 /* Call the base tp_dealloc() */
232 assert(f);
233 f(self);
234
235 /* Can't reference self beyond this point */
236 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
237 Py_DECREF(type);
238 }
239}
240
241staticforward void override_slots(PyTypeObject *type, PyObject *dict);
242staticforward PyTypeObject *solid_base(PyTypeObject *type);
243
244typedef struct {
245 PyTypeObject type;
246 PyNumberMethods as_number;
247 PySequenceMethods as_sequence;
248 PyMappingMethods as_mapping;
249 PyBufferProcs as_buffer;
250 PyObject *name, *slots;
251 struct memberlist members[1];
252} etype;
253
254/* type test with subclassing support */
255
256int
257PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
258{
259 PyObject *mro;
260
261 mro = a->tp_mro;
262 if (mro != NULL) {
263 /* Deal with multiple inheritance without recursion
264 by walking the MRO tuple */
265 int i, n;
266 assert(PyTuple_Check(mro));
267 n = PyTuple_GET_SIZE(mro);
268 for (i = 0; i < n; i++) {
269 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
270 return 1;
271 }
272 return 0;
273 }
274 else {
275 /* a is not completely initilized yet; follow tp_base */
276 do {
277 if (a == b)
278 return 1;
279 a = a->tp_base;
280 } while (a != NULL);
281 return b == &PyBaseObject_Type;
282 }
283}
284
285/* Method resolution order algorithm from "Putting Metaclasses to Work"
286 by Forman and Danforth (Addison-Wesley 1999). */
287
288static int
289conservative_merge(PyObject *left, PyObject *right)
290{
291 int left_size;
292 int right_size;
293 int i, j, r, ok;
294 PyObject *temp, *rr;
295
296 assert(PyList_Check(left));
297 assert(PyList_Check(right));
298
299 again:
300 left_size = PyList_GET_SIZE(left);
301 right_size = PyList_GET_SIZE(right);
302 for (i = 0; i < left_size; i++) {
303 for (j = 0; j < right_size; j++) {
304 if (PyList_GET_ITEM(left, i) ==
305 PyList_GET_ITEM(right, j)) {
306 /* found a merge point */
307 temp = PyList_New(0);
308 if (temp == NULL)
309 return -1;
310 for (r = 0; r < j; r++) {
311 rr = PyList_GET_ITEM(right, r);
312 ok = PySequence_Contains(left, rr);
313 if (ok < 0) {
314 Py_DECREF(temp);
315 return -1;
316 }
317 if (!ok) {
318 ok = PyList_Append(temp, rr);
319 if (ok < 0) {
320 Py_DECREF(temp);
321 return -1;
322 }
323 }
324 }
325 ok = PyList_SetSlice(left, i, i, temp);
326 Py_DECREF(temp);
327 if (ok < 0)
328 return -1;
329 ok = PyList_SetSlice(right, 0, j+1, NULL);
330 if (ok < 0)
331 return -1;
332 goto again;
333 }
334 }
335 }
336 return PyList_SetSlice(left, left_size, left_size, right);
337}
338
339static int
340serious_order_disagreements(PyObject *left, PyObject *right)
341{
342 return 0; /* XXX later -- for now, we cheat: "don't do that" */
343}
344
345static PyObject *
346mro_implementation(PyTypeObject *type)
347{
348 int i, n, ok;
349 PyObject *bases, *result;
350
351 bases = type->tp_bases;
352 n = PyTuple_GET_SIZE(bases);
353 result = Py_BuildValue("[O]", (PyObject *)type);
354 if (result == NULL)
355 return NULL;
356 for (i = 0; i < n; i++) {
357 PyTypeObject *base =
358 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
359 PyObject *parentMRO = PySequence_List(base->tp_mro);
360 if (parentMRO == NULL) {
361 Py_DECREF(result);
362 return NULL;
363 }
364 if (serious_order_disagreements(result, parentMRO)) {
365 Py_DECREF(result);
366 return NULL;
367 }
368 ok = conservative_merge(result, parentMRO);
369 Py_DECREF(parentMRO);
370 if (ok < 0) {
371 Py_DECREF(result);
372 return NULL;
373 }
374 }
375 return result;
376}
377
378static PyObject *
379mro_external(PyObject *self, PyObject *args)
380{
381 PyTypeObject *type = (PyTypeObject *)self;
382
383 if (!PyArg_ParseTuple(args, ""))
384 return NULL;
385 return mro_implementation(type);
386}
387
388static int
389mro_internal(PyTypeObject *type)
390{
391 PyObject *mro, *result, *tuple;
392
393 if (type->ob_type == &PyType_Type) {
394 result = mro_implementation(type);
395 }
396 else {
397 mro = PyObject_GetAttrString((PyObject *)type, "mro");
398 if (mro == NULL)
399 return -1;
400 result = PyObject_CallObject(mro, NULL);
401 Py_DECREF(mro);
402 }
403 if (result == NULL)
404 return -1;
405 tuple = PySequence_Tuple(result);
406 Py_DECREF(result);
407 type->tp_mro = tuple;
408 return 0;
409}
410
411
412/* Calculate the best base amongst multiple base classes.
413 This is the first one that's on the path to the "solid base". */
414
415static PyTypeObject *
416best_base(PyObject *bases)
417{
418 int i, n;
419 PyTypeObject *base, *winner, *candidate, *base_i;
420
421 assert(PyTuple_Check(bases));
422 n = PyTuple_GET_SIZE(bases);
423 assert(n > 0);
424 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
425 winner = &PyBaseObject_Type;
426 for (i = 0; i < n; i++) {
427 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
428 if (!PyType_Check((PyObject *)base_i)) {
429 PyErr_SetString(
430 PyExc_TypeError,
431 "bases must be types");
432 return NULL;
433 }
434 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000435 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000436 return NULL;
437 }
438 candidate = solid_base(base_i);
439 if (PyType_IsSubtype(winner, candidate))
440 ;
441 else if (PyType_IsSubtype(candidate, winner)) {
442 winner = candidate;
443 base = base_i;
444 }
445 else {
446 PyErr_SetString(
447 PyExc_TypeError,
448 "multiple bases have "
449 "instance lay-out conflict");
450 return NULL;
451 }
452 }
453 assert(base != NULL);
454 return base;
455}
456
457static int
458extra_ivars(PyTypeObject *type, PyTypeObject *base)
459{
460 int t_size = PyType_BASICSIZE(type);
461 int b_size = PyType_BASICSIZE(base);
462
463 assert(t_size >= b_size); /* type smaller than base! */
464 if (type->tp_itemsize || base->tp_itemsize) {
465 /* If itemsize is involved, stricter rules */
466 return t_size != b_size ||
467 type->tp_itemsize != base->tp_itemsize;
468 }
469 if (t_size == b_size)
470 return 0;
471 if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 &&
472 type->tp_dictoffset == b_size &&
473 (size_t)t_size == b_size + sizeof(PyObject *))
474 return 0; /* "Forgive" adding a __dict__ only */
475 return 1;
476}
477
478static PyTypeObject *
479solid_base(PyTypeObject *type)
480{
481 PyTypeObject *base;
482
483 if (type->tp_base)
484 base = solid_base(type->tp_base);
485 else
486 base = &PyBaseObject_Type;
487 if (extra_ivars(type, base))
488 return type;
489 else
490 return base;
491}
492
493staticforward void object_dealloc(PyObject *);
494staticforward int object_init(PyObject *, PyObject *, PyObject *);
495
496static PyObject *
497type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
498{
499 PyObject *name, *bases, *dict;
500 static char *kwlist[] = {"name", "bases", "dict", 0};
501 PyObject *slots, *tmp;
502 PyTypeObject *type, *base, *tmptype;
503 etype *et;
504 struct memberlist *mp;
505 int i, nbases, nslots, slotoffset, dynamic;
506
507 if (metatype == &PyType_Type &&
508 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
509 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
510 /* type(x) -> x.__class__ */
511 PyObject *x = PyTuple_GET_ITEM(args, 0);
512 Py_INCREF(x->ob_type);
513 return (PyObject *) x->ob_type;
514 }
515
516 /* Check arguments */
517 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
518 &name,
519 &PyTuple_Type, &bases,
520 &PyDict_Type, &dict))
521 return NULL;
522
523 /* Determine the proper metatype to deal with this,
524 and check for metatype conflicts while we're at it.
525 Note that if some other metatype wins to contract,
526 it's possible that its instances are not types. */
527 nbases = PyTuple_GET_SIZE(bases);
528 for (i = 0; i < nbases; i++) {
529 tmp = PyTuple_GET_ITEM(bases, i);
530 tmptype = tmp->ob_type;
531 if (PyType_IsSubtype(metatype, tmptype))
532 continue;
533 if (PyType_IsSubtype(tmptype, metatype)) {
534 metatype = tmptype;
535 continue;
536 }
537 PyErr_SetString(PyExc_TypeError,
538 "metatype conflict among bases");
539 return NULL;
540 }
541 if (metatype->tp_new != type_new) /* Pass it to the winner */
542 return metatype->tp_new(metatype, args, kwds);
543
544 /* Adjust for empty tuple bases */
545 if (nbases == 0) {
546 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
547 if (bases == NULL)
548 return NULL;
549 nbases = 1;
550 }
551 else
552 Py_INCREF(bases);
553
554 /* XXX From here until type is allocated, "return NULL" leaks bases! */
555
556 /* Calculate best base, and check that all bases are type objects */
557 base = best_base(bases);
558 if (base == NULL)
559 return NULL;
560 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
561 PyErr_Format(PyExc_TypeError,
562 "type '%.100s' is not an acceptable base type",
563 base->tp_name);
564 return NULL;
565 }
566
567 /* Should this be a dynamic class (i.e. modifiable __dict__)? */
568 tmp = PyDict_GetItemString(dict, "__dynamic__");
569 if (tmp != NULL) {
570 /* The class author has a preference */
571 dynamic = PyObject_IsTrue(tmp);
572 Py_DECREF(tmp);
573 if (dynamic < 0)
574 return NULL;
575 }
576 else {
577 /* Make a new class dynamic if any of its bases is dynamic.
578 This is not always the same as inheriting the __dynamic__
579 class attribute! */
580 dynamic = 0;
581 for (i = 0; i < nbases; i++) {
582 tmptype = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
583 if (tmptype->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
584 dynamic = 1;
585 break;
586 }
587 }
588 }
589
590 /* Check for a __slots__ sequence variable in dict, and count it */
591 slots = PyDict_GetItemString(dict, "__slots__");
592 nslots = 0;
593 if (slots != NULL) {
594 /* Make it into a tuple */
595 if (PyString_Check(slots))
596 slots = Py_BuildValue("(O)", slots);
597 else
598 slots = PySequence_Tuple(slots);
599 if (slots == NULL)
600 return NULL;
601 nslots = PyTuple_GET_SIZE(slots);
602 for (i = 0; i < nslots; i++) {
603 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
604 PyErr_SetString(PyExc_TypeError,
605 "__slots__ must be a sequence of strings");
606 Py_DECREF(slots);
607 return NULL;
608 }
609 }
610 }
611 if (slots == NULL && base->tp_dictoffset == 0 &&
612 (base->tp_setattro == PyObject_GenericSetAttr ||
613 base->tp_setattro == NULL))
614 nslots = 1;
615
616 /* XXX From here until type is safely allocated,
617 "return NULL" may leak slots! */
618
619 /* Allocate the type object */
620 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
621 if (type == NULL)
622 return NULL;
623
624 /* Keep name and slots alive in the extended type object */
625 et = (etype *)type;
626 Py_INCREF(name);
627 et->name = name;
628 et->slots = slots;
629
Guido van Rossumdc91b992001-08-08 22:26:22 +0000630 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000631 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
632 Py_TPFLAGS_BASETYPE;
633 if (dynamic)
634 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000635
636 /* It's a new-style number unless it specifically inherits any
637 old-style numeric behavior */
638 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
639 (base->tp_as_number == NULL))
640 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
641
642 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000643 type->tp_as_number = &et->as_number;
644 type->tp_as_sequence = &et->as_sequence;
645 type->tp_as_mapping = &et->as_mapping;
646 type->tp_as_buffer = &et->as_buffer;
647 type->tp_name = PyString_AS_STRING(name);
648
649 /* Set tp_base and tp_bases */
650 type->tp_bases = bases;
651 Py_INCREF(base);
652 type->tp_base = base;
653
654 /* Initialize tp_defined from passed-in dict */
655 type->tp_defined = dict = PyDict_Copy(dict);
656 if (dict == NULL) {
657 Py_DECREF(type);
658 return NULL;
659 }
660
Guido van Rossumc3542212001-08-16 09:18:56 +0000661 /* Set __module__ in the dict */
662 if (PyDict_GetItemString(dict, "__module__") == NULL) {
663 tmp = PyEval_GetGlobals();
664 if (tmp != NULL) {
665 tmp = PyDict_GetItemString(tmp, "__name__");
666 if (tmp != NULL) {
667 if (PyDict_SetItemString(dict, "__module__",
668 tmp) < 0)
669 return NULL;
670 }
671 }
672 }
673
Tim Peters6d6c1a32001-08-02 04:15:00 +0000674 /* Special-case __new__: if it's a plain function,
675 make it a static function */
676 tmp = PyDict_GetItemString(dict, "__new__");
677 if (tmp != NULL && PyFunction_Check(tmp)) {
678 tmp = PyStaticMethod_New(tmp);
679 if (tmp == NULL) {
680 Py_DECREF(type);
681 return NULL;
682 }
683 PyDict_SetItemString(dict, "__new__", tmp);
684 Py_DECREF(tmp);
685 }
686
687 /* Add descriptors for custom slots from __slots__, or for __dict__ */
688 mp = et->members;
689 slotoffset = PyType_BASICSIZE(base);
690 if (slots != NULL) {
691 for (i = 0; i < nslots; i++, mp++) {
692 mp->name = PyString_AS_STRING(
693 PyTuple_GET_ITEM(slots, i));
694 mp->type = T_OBJECT;
695 mp->offset = slotoffset;
696 slotoffset += sizeof(PyObject *);
697 }
698 }
699 else if (nslots) {
700 type->tp_dictoffset = slotoffset;
701 mp->name = "__dict__";
702 mp->type = T_OBJECT;
703 mp->offset = slotoffset;
704 mp->readonly = 1;
705 slotoffset += sizeof(PyObject *);
706 }
707 type->tp_basicsize = slotoffset;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000708 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000709
710 /* Special case some slots */
711 if (type->tp_dictoffset != 0 || nslots > 0) {
712 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
713 type->tp_getattro = PyObject_GenericGetAttr;
714 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
715 type->tp_setattro = PyObject_GenericSetAttr;
716 }
717 type->tp_dealloc = subtype_dealloc;
718
719 /* Always override allocation strategy to use regular heap */
720 type->tp_alloc = PyType_GenericAlloc;
721 type->tp_free = _PyObject_Del;
722
723 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000724 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 Py_DECREF(type);
726 return NULL;
727 }
728
729 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000730 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
731 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000732
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733 return (PyObject *)type;
734}
735
736/* Internal API to look for a name through the MRO.
737 This returns a borrowed reference, and doesn't set an exception! */
738PyObject *
739_PyType_Lookup(PyTypeObject *type, PyObject *name)
740{
741 int i, n;
742 PyObject *mro, *res, *dict;
743
744 /* For static types, look in tp_dict */
745 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
746 dict = type->tp_dict;
747 assert(dict && PyDict_Check(dict));
748 return PyDict_GetItem(dict, name);
749 }
750
751 /* For dynamic types, look in tp_defined of types in MRO */
752 mro = type->tp_mro;
753 assert(PyTuple_Check(mro));
754 n = PyTuple_GET_SIZE(mro);
755 for (i = 0; i < n; i++) {
756 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
757 assert(PyType_Check(type));
758 dict = type->tp_defined;
759 assert(dict && PyDict_Check(dict));
760 res = PyDict_GetItem(dict, name);
761 if (res != NULL)
762 return res;
763 }
764 return NULL;
765}
766
767/* This is similar to PyObject_GenericGetAttr(),
768 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
769static PyObject *
770type_getattro(PyTypeObject *type, PyObject *name)
771{
772 PyTypeObject *metatype = type->ob_type;
773 PyObject *descr, *res;
774 descrgetfunc f;
775
776 /* Initialize this type (we'll assume the metatype is initialized) */
777 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000778 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000779 return NULL;
780 }
781
782 /* Get a descriptor from the metatype */
783 descr = _PyType_Lookup(metatype, name);
784 f = NULL;
785 if (descr != NULL) {
786 f = descr->ob_type->tp_descr_get;
787 if (f != NULL && PyDescr_IsData(descr))
788 return f(descr,
789 (PyObject *)type, (PyObject *)metatype);
790 }
791
792 /* Look in tp_defined of this type and its bases */
793 res = _PyType_Lookup(type, name);
794 if (res != NULL) {
795 f = res->ob_type->tp_descr_get;
796 if (f != NULL)
797 return f(res, (PyObject *)NULL, (PyObject *)type);
798 Py_INCREF(res);
799 return res;
800 }
801
802 /* Use the descriptor from the metatype */
803 if (f != NULL) {
804 res = f(descr, (PyObject *)type, (PyObject *)metatype);
805 return res;
806 }
807 if (descr != NULL) {
808 Py_INCREF(descr);
809 return descr;
810 }
811
812 /* Give up */
813 PyErr_Format(PyExc_AttributeError,
814 "type object '%.50s' has no attribute '%.400s'",
815 type->tp_name, PyString_AS_STRING(name));
816 return NULL;
817}
818
819static int
820type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
821{
822 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
823 return PyObject_GenericSetAttr((PyObject *)type, name, value);
824 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
825 return -1;
826}
827
828static void
829type_dealloc(PyTypeObject *type)
830{
831 etype *et;
832
833 /* Assert this is a heap-allocated type object */
834 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
835 et = (etype *)type;
836 Py_XDECREF(type->tp_base);
837 Py_XDECREF(type->tp_dict);
838 Py_XDECREF(type->tp_bases);
839 Py_XDECREF(type->tp_mro);
840 Py_XDECREF(type->tp_defined);
841 /* XXX more? */
842 Py_XDECREF(et->name);
843 Py_XDECREF(et->slots);
844 type->ob_type->tp_free((PyObject *)type);
845}
846
847static PyMethodDef type_methods[] = {
848 {"mro", mro_external, METH_VARARGS,
849 "mro() -> list\nreturn a type's method resolution order"},
850 {0}
851};
852
853static char type_doc[] =
854"type(object) -> the object's type\n"
855"type(name, bases, dict) -> a new type";
856
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000857PyTypeObject PyType_Type = {
858 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000859 0, /* ob_size */
860 "type", /* tp_name */
861 sizeof(etype), /* tp_basicsize */
862 sizeof(struct memberlist), /* tp_itemsize */
863 (destructor)type_dealloc, /* tp_dealloc */
864 0, /* tp_print */
865 0, /* tp_getattr */
866 0, /* tp_setattr */
867 type_compare, /* tp_compare */
868 (reprfunc)type_repr, /* tp_repr */
869 0, /* tp_as_number */
870 0, /* tp_as_sequence */
871 0, /* tp_as_mapping */
872 (hashfunc)_Py_HashPointer, /* tp_hash */
873 (ternaryfunc)type_call, /* tp_call */
874 0, /* tp_str */
875 (getattrofunc)type_getattro, /* tp_getattro */
876 (setattrofunc)type_setattro, /* tp_setattro */
877 0, /* tp_as_buffer */
878 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
879 type_doc, /* tp_doc */
880 0, /* tp_traverse */
881 0, /* tp_clear */
882 0, /* tp_richcompare */
883 0, /* tp_weaklistoffset */
884 0, /* tp_iter */
885 0, /* tp_iternext */
886 type_methods, /* tp_methods */
887 type_members, /* tp_members */
888 type_getsets, /* tp_getset */
889 0, /* tp_base */
890 0, /* tp_dict */
891 0, /* tp_descr_get */
892 0, /* tp_descr_set */
893 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
894 0, /* tp_init */
895 0, /* tp_alloc */
896 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000897};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898
899
900/* The base type of all types (eventually)... except itself. */
901
902static int
903object_init(PyObject *self, PyObject *args, PyObject *kwds)
904{
905 return 0;
906}
907
908static void
909object_dealloc(PyObject *self)
910{
911 self->ob_type->tp_free(self);
912}
913
Guido van Rossum8e248182001-08-12 05:17:56 +0000914static PyObject *
915object_repr(PyObject *self)
916{
917 char buf[120];
918
919 sprintf(buf, "<%.80s object at %p>", self->ob_type->tp_name, self);
920 return PyString_FromString(buf);
921}
922
Guido van Rossumb8f63662001-08-15 23:57:02 +0000923static PyObject *
924object_str(PyObject *self)
925{
926 unaryfunc f;
927
928 f = self->ob_type->tp_repr;
929 if (f == NULL)
930 f = object_repr;
931 return f(self);
932}
933
Guido van Rossum8e248182001-08-12 05:17:56 +0000934static long
935object_hash(PyObject *self)
936{
937 return _Py_HashPointer(self);
938}
Guido van Rossum8e248182001-08-12 05:17:56 +0000939
Tim Peters6d6c1a32001-08-02 04:15:00 +0000940static void
941object_free(PyObject *self)
942{
943 PyObject_Del(self);
944}
945
946static struct memberlist object_members[] = {
947 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
948 {0}
949};
950
951PyTypeObject PyBaseObject_Type = {
952 PyObject_HEAD_INIT(&PyType_Type)
953 0, /* ob_size */
954 "object", /* tp_name */
955 sizeof(PyObject), /* tp_basicsize */
956 0, /* tp_itemsize */
957 (destructor)object_dealloc, /* tp_dealloc */
958 0, /* tp_print */
959 0, /* tp_getattr */
960 0, /* tp_setattr */
961 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000962 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963 0, /* tp_as_number */
964 0, /* tp_as_sequence */
965 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000966 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000968 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +0000970 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000971 0, /* tp_as_buffer */
972 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
973 "The most base type", /* tp_doc */
974 0, /* tp_traverse */
975 0, /* tp_clear */
976 0, /* tp_richcompare */
977 0, /* tp_weaklistoffset */
978 0, /* tp_iter */
979 0, /* tp_iternext */
980 0, /* tp_methods */
981 object_members, /* tp_members */
982 0, /* tp_getset */
983 0, /* tp_base */
984 0, /* tp_dict */
985 0, /* tp_descr_get */
986 0, /* tp_descr_set */
987 0, /* tp_dictoffset */
988 object_init, /* tp_init */
989 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +0000990 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991 object_free, /* tp_free */
992};
993
994
995/* Initialize the __dict__ in a type object */
996
997static int
998add_methods(PyTypeObject *type, PyMethodDef *meth)
999{
1000 PyObject *dict = type->tp_defined;
1001
1002 for (; meth->ml_name != NULL; meth++) {
1003 PyObject *descr;
1004 if (PyDict_GetItemString(dict, meth->ml_name))
1005 continue;
1006 descr = PyDescr_NewMethod(type, meth);
1007 if (descr == NULL)
1008 return -1;
1009 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1010 return -1;
1011 Py_DECREF(descr);
1012 }
1013 return 0;
1014}
1015
1016static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001017add_members(PyTypeObject *type, struct memberlist *memb)
1018{
1019 PyObject *dict = type->tp_defined;
1020
1021 for (; memb->name != NULL; memb++) {
1022 PyObject *descr;
1023 if (PyDict_GetItemString(dict, memb->name))
1024 continue;
1025 descr = PyDescr_NewMember(type, memb);
1026 if (descr == NULL)
1027 return -1;
1028 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1029 return -1;
1030 Py_DECREF(descr);
1031 }
1032 return 0;
1033}
1034
1035static int
1036add_getset(PyTypeObject *type, struct getsetlist *gsp)
1037{
1038 PyObject *dict = type->tp_defined;
1039
1040 for (; gsp->name != NULL; gsp++) {
1041 PyObject *descr;
1042 if (PyDict_GetItemString(dict, gsp->name))
1043 continue;
1044 descr = PyDescr_NewGetSet(type, gsp);
1045
1046 if (descr == NULL)
1047 return -1;
1048 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1049 return -1;
1050 Py_DECREF(descr);
1051 }
1052 return 0;
1053}
1054
Guido van Rossum13d52f02001-08-10 21:24:08 +00001055static void
1056inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001057{
1058 int oldsize, newsize;
1059
Guido van Rossum13d52f02001-08-10 21:24:08 +00001060 /* Special flag magic */
1061 if (!type->tp_as_buffer && base->tp_as_buffer) {
1062 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1063 type->tp_flags |=
1064 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1065 }
1066 if (!type->tp_as_sequence && base->tp_as_sequence) {
1067 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1068 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1069 }
1070 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1071 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1072 if ((!type->tp_as_number && base->tp_as_number) ||
1073 (!type->tp_as_sequence && base->tp_as_sequence)) {
1074 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1075 if (!type->tp_as_number && !type->tp_as_sequence) {
1076 type->tp_flags |= base->tp_flags &
1077 Py_TPFLAGS_HAVE_INPLACEOPS;
1078 }
1079 }
1080 /* Wow */
1081 }
1082 if (!type->tp_as_number && base->tp_as_number) {
1083 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1084 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1085 }
1086
1087 /* Copying basicsize is connected to the GC flags */
1088 oldsize = PyType_BASICSIZE(base);
1089 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1090 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1091 (base->tp_flags & Py_TPFLAGS_GC) &&
1092 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1093 (!type->tp_traverse && !type->tp_clear)) {
1094 type->tp_flags |= Py_TPFLAGS_GC;
1095 if (type->tp_traverse == NULL)
1096 type->tp_traverse = base->tp_traverse;
1097 if (type->tp_clear == NULL)
1098 type->tp_clear = base->tp_clear;
1099 }
1100 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1101 if (base != &PyBaseObject_Type ||
1102 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1103 if (type->tp_new == NULL)
1104 type->tp_new = base->tp_new;
1105 }
1106 }
1107 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001108
1109 /* Copy other non-function slots */
1110
1111#undef COPYVAL
1112#define COPYVAL(SLOT) \
1113 if (type->SLOT == 0) type->SLOT = base->SLOT
1114
1115 COPYVAL(tp_itemsize);
1116 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1117 COPYVAL(tp_weaklistoffset);
1118 }
1119 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1120 COPYVAL(tp_dictoffset);
1121 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001122}
1123
1124static void
1125inherit_slots(PyTypeObject *type, PyTypeObject *base)
1126{
1127 PyTypeObject *basebase;
1128
1129#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001130#undef COPYSLOT
1131#undef COPYNUM
1132#undef COPYSEQ
1133#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001134
1135#define SLOTDEFINED(SLOT) \
1136 (base->SLOT != 0 && \
1137 (basebase == NULL || base->SLOT != basebase->SLOT))
1138
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001140 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141
1142#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1143#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1144#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1145
Guido van Rossum13d52f02001-08-10 21:24:08 +00001146 /* This won't inherit indirect slots (from tp_as_number etc.)
1147 if type doesn't provide the space. */
1148
1149 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1150 basebase = base->tp_base;
1151 if (basebase->tp_as_number == NULL)
1152 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001153 COPYNUM(nb_add);
1154 COPYNUM(nb_subtract);
1155 COPYNUM(nb_multiply);
1156 COPYNUM(nb_divide);
1157 COPYNUM(nb_remainder);
1158 COPYNUM(nb_divmod);
1159 COPYNUM(nb_power);
1160 COPYNUM(nb_negative);
1161 COPYNUM(nb_positive);
1162 COPYNUM(nb_absolute);
1163 COPYNUM(nb_nonzero);
1164 COPYNUM(nb_invert);
1165 COPYNUM(nb_lshift);
1166 COPYNUM(nb_rshift);
1167 COPYNUM(nb_and);
1168 COPYNUM(nb_xor);
1169 COPYNUM(nb_or);
1170 COPYNUM(nb_coerce);
1171 COPYNUM(nb_int);
1172 COPYNUM(nb_long);
1173 COPYNUM(nb_float);
1174 COPYNUM(nb_oct);
1175 COPYNUM(nb_hex);
1176 COPYNUM(nb_inplace_add);
1177 COPYNUM(nb_inplace_subtract);
1178 COPYNUM(nb_inplace_multiply);
1179 COPYNUM(nb_inplace_divide);
1180 COPYNUM(nb_inplace_remainder);
1181 COPYNUM(nb_inplace_power);
1182 COPYNUM(nb_inplace_lshift);
1183 COPYNUM(nb_inplace_rshift);
1184 COPYNUM(nb_inplace_and);
1185 COPYNUM(nb_inplace_xor);
1186 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001187 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1188 COPYNUM(nb_true_divide);
1189 COPYNUM(nb_floor_divide);
1190 COPYNUM(nb_inplace_true_divide);
1191 COPYNUM(nb_inplace_floor_divide);
1192 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193 }
1194
Guido van Rossum13d52f02001-08-10 21:24:08 +00001195 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1196 basebase = base->tp_base;
1197 if (basebase->tp_as_sequence == NULL)
1198 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001199 COPYSEQ(sq_length);
1200 COPYSEQ(sq_concat);
1201 COPYSEQ(sq_repeat);
1202 COPYSEQ(sq_item);
1203 COPYSEQ(sq_slice);
1204 COPYSEQ(sq_ass_item);
1205 COPYSEQ(sq_ass_slice);
1206 COPYSEQ(sq_contains);
1207 COPYSEQ(sq_inplace_concat);
1208 COPYSEQ(sq_inplace_repeat);
1209 }
1210
Guido van Rossum13d52f02001-08-10 21:24:08 +00001211 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1212 basebase = base->tp_base;
1213 if (basebase->tp_as_mapping == NULL)
1214 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001215 COPYMAP(mp_length);
1216 COPYMAP(mp_subscript);
1217 COPYMAP(mp_ass_subscript);
1218 }
1219
Guido van Rossum13d52f02001-08-10 21:24:08 +00001220 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001221
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222 COPYSLOT(tp_dealloc);
1223 COPYSLOT(tp_print);
1224 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1225 type->tp_getattr = base->tp_getattr;
1226 type->tp_getattro = base->tp_getattro;
1227 }
1228 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1229 type->tp_setattr = base->tp_setattr;
1230 type->tp_setattro = base->tp_setattro;
1231 }
1232 /* tp_compare see tp_richcompare */
1233 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001234 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001235 COPYSLOT(tp_call);
1236 COPYSLOT(tp_str);
1237 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001239 if (type->tp_compare == NULL &&
1240 type->tp_richcompare == NULL &&
1241 type->tp_hash == NULL)
1242 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243 type->tp_compare = base->tp_compare;
1244 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001245 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001246 }
1247 }
1248 else {
1249 COPYSLOT(tp_compare);
1250 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001251 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1252 COPYSLOT(tp_iter);
1253 COPYSLOT(tp_iternext);
1254 }
1255 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1256 COPYSLOT(tp_descr_get);
1257 COPYSLOT(tp_descr_set);
1258 COPYSLOT(tp_dictoffset);
1259 COPYSLOT(tp_init);
1260 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261 COPYSLOT(tp_free);
1262 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001263}
1264
Guido van Rossum13d52f02001-08-10 21:24:08 +00001265staticforward int add_operators(PyTypeObject *);
1266
Tim Peters6d6c1a32001-08-02 04:15:00 +00001267int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001268PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001269{
1270 PyObject *dict, *bases, *x;
1271 PyTypeObject *base;
1272 int i, n;
1273
Guido van Rossumd614f972001-08-10 17:39:49 +00001274 if (type->tp_flags & Py_TPFLAGS_READY) {
1275 assert(type->tp_dict != NULL);
1276 return 0;
1277 }
1278 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1279 assert(type->tp_dict == NULL);
1280
1281 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001282
1283 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1284 base = type->tp_base;
1285 if (base == NULL && type != &PyBaseObject_Type)
1286 base = type->tp_base = &PyBaseObject_Type;
1287
1288 /* Initialize tp_bases */
1289 bases = type->tp_bases;
1290 if (bases == NULL) {
1291 if (base == NULL)
1292 bases = PyTuple_New(0);
1293 else
1294 bases = Py_BuildValue("(O)", base);
1295 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001296 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001297 type->tp_bases = bases;
1298 }
1299
1300 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001301 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001302 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001303 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001304 }
1305
1306 /* Initialize tp_defined */
1307 dict = type->tp_defined;
1308 if (dict == NULL) {
1309 dict = PyDict_New();
1310 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001311 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312 type->tp_defined = dict;
1313 }
1314
1315 /* Add type-specific descriptors to tp_defined */
1316 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001317 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001318 if (type->tp_methods != NULL) {
1319 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001320 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001321 }
1322 if (type->tp_members != NULL) {
1323 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001324 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 }
1326 if (type->tp_getset != NULL) {
1327 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001328 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001329 }
1330
1331 /* Temporarily make tp_dict the same object as tp_defined.
1332 (This is needed to call mro(), and can stay this way for
1333 dynamic types). */
1334 Py_INCREF(type->tp_defined);
1335 type->tp_dict = type->tp_defined;
1336
1337 /* Calculate method resolution order */
1338 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001339 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001340 }
1341
Guido van Rossum13d52f02001-08-10 21:24:08 +00001342 /* Inherit special flags from dominant base */
1343 if (type->tp_base != NULL)
1344 inherit_special(type, type->tp_base);
1345
Tim Peters6d6c1a32001-08-02 04:15:00 +00001346 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001347 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001348 /* For a dynamic type, all slots are overridden */
1349 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001350 }
1351 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001353 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001355 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001357 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001358 bases = type->tp_mro;
1359 assert(bases != NULL);
1360 assert(PyTuple_Check(bases));
1361 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001362 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001363 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1364 assert(PyType_Check(base));
1365 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001366 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001367 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001368 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 }
1370 }
1371
Guido van Rossum13d52f02001-08-10 21:24:08 +00001372 /* Some more special stuff */
1373 base = type->tp_base;
1374 if (base != NULL) {
1375 if (type->tp_as_number == NULL)
1376 type->tp_as_number = base->tp_as_number;
1377 if (type->tp_as_sequence == NULL)
1378 type->tp_as_sequence = base->tp_as_sequence;
1379 if (type->tp_as_mapping == NULL)
1380 type->tp_as_mapping = base->tp_as_mapping;
1381 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001382
Guido van Rossum13d52f02001-08-10 21:24:08 +00001383 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001384 assert(type->tp_dict != NULL);
1385 type->tp_flags =
1386 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001387 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001388
1389 error:
1390 type->tp_flags &= ~Py_TPFLAGS_READYING;
1391 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001392}
1393
1394
1395/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1396
1397/* There's a wrapper *function* for each distinct function typedef used
1398 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1399 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1400 Most tables have only one entry; the tables for binary operators have two
1401 entries, one regular and one with reversed arguments. */
1402
1403static PyObject *
1404wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1405{
1406 inquiry func = (inquiry)wrapped;
1407 int res;
1408
1409 if (!PyArg_ParseTuple(args, ""))
1410 return NULL;
1411 res = (*func)(self);
1412 if (res == -1 && PyErr_Occurred())
1413 return NULL;
1414 return PyInt_FromLong((long)res);
1415}
1416
1417static struct wrapperbase tab_len[] = {
1418 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1419 {0}
1420};
1421
1422static PyObject *
1423wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1424{
1425 binaryfunc func = (binaryfunc)wrapped;
1426 PyObject *other;
1427
1428 if (!PyArg_ParseTuple(args, "O", &other))
1429 return NULL;
1430 return (*func)(self, other);
1431}
1432
1433static PyObject *
1434wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1435{
1436 binaryfunc func = (binaryfunc)wrapped;
1437 PyObject *other;
1438
1439 if (!PyArg_ParseTuple(args, "O", &other))
1440 return NULL;
1441 return (*func)(other, self);
1442}
1443
1444#undef BINARY
1445#define BINARY(NAME, OP) \
1446static struct wrapperbase tab_##NAME[] = { \
1447 {"__" #NAME "__", \
1448 (wrapperfunc)wrap_binaryfunc, \
1449 "x.__" #NAME "__(y) <==> " #OP}, \
1450 {"__r" #NAME "__", \
1451 (wrapperfunc)wrap_binaryfunc_r, \
1452 "y.__r" #NAME "__(x) <==> " #OP}, \
1453 {0} \
1454}
1455
1456BINARY(add, "x+y");
1457BINARY(sub, "x-y");
1458BINARY(mul, "x*y");
1459BINARY(div, "x/y");
1460BINARY(mod, "x%y");
1461BINARY(divmod, "divmod(x,y)");
1462BINARY(lshift, "x<<y");
1463BINARY(rshift, "x>>y");
1464BINARY(and, "x&y");
1465BINARY(xor, "x^y");
1466BINARY(or, "x|y");
1467
1468static PyObject *
1469wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1470{
1471 ternaryfunc func = (ternaryfunc)wrapped;
1472 PyObject *other;
1473 PyObject *third = Py_None;
1474
1475 /* Note: This wrapper only works for __pow__() */
1476
1477 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1478 return NULL;
1479 return (*func)(self, other, third);
1480}
1481
1482#undef TERNARY
1483#define TERNARY(NAME, OP) \
1484static struct wrapperbase tab_##NAME[] = { \
1485 {"__" #NAME "__", \
1486 (wrapperfunc)wrap_ternaryfunc, \
1487 "x.__" #NAME "__(y, z) <==> " #OP}, \
1488 {"__r" #NAME "__", \
1489 (wrapperfunc)wrap_ternaryfunc, \
1490 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1491 {0} \
1492}
1493
1494TERNARY(pow, "(x**y) % z");
1495
1496#undef UNARY
1497#define UNARY(NAME, OP) \
1498static struct wrapperbase tab_##NAME[] = { \
1499 {"__" #NAME "__", \
1500 (wrapperfunc)wrap_unaryfunc, \
1501 "x.__" #NAME "__() <==> " #OP}, \
1502 {0} \
1503}
1504
1505static PyObject *
1506wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1507{
1508 unaryfunc func = (unaryfunc)wrapped;
1509
1510 if (!PyArg_ParseTuple(args, ""))
1511 return NULL;
1512 return (*func)(self);
1513}
1514
1515UNARY(neg, "-x");
1516UNARY(pos, "+x");
1517UNARY(abs, "abs(x)");
1518UNARY(nonzero, "x != 0");
1519UNARY(invert, "~x");
1520UNARY(int, "int(x)");
1521UNARY(long, "long(x)");
1522UNARY(float, "float(x)");
1523UNARY(oct, "oct(x)");
1524UNARY(hex, "hex(x)");
1525
1526#undef IBINARY
1527#define IBINARY(NAME, OP) \
1528static struct wrapperbase tab_##NAME[] = { \
1529 {"__" #NAME "__", \
1530 (wrapperfunc)wrap_binaryfunc, \
1531 "x.__" #NAME "__(y) <==> " #OP}, \
1532 {0} \
1533}
1534
1535IBINARY(iadd, "x+=y");
1536IBINARY(isub, "x-=y");
1537IBINARY(imul, "x*=y");
1538IBINARY(idiv, "x/=y");
1539IBINARY(imod, "x%=y");
1540IBINARY(ilshift, "x<<=y");
1541IBINARY(irshift, "x>>=y");
1542IBINARY(iand, "x&=y");
1543IBINARY(ixor, "x^=y");
1544IBINARY(ior, "x|=y");
1545
1546#undef ITERNARY
1547#define ITERNARY(NAME, OP) \
1548static struct wrapperbase tab_##NAME[] = { \
1549 {"__" #NAME "__", \
1550 (wrapperfunc)wrap_ternaryfunc, \
1551 "x.__" #NAME "__(y) <==> " #OP}, \
1552 {0} \
1553}
1554
1555ITERNARY(ipow, "x = (x**y) % z");
1556
1557static struct wrapperbase tab_getitem[] = {
1558 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1559 "x.__getitem__(y) <==> x[y]"},
1560 {0}
1561};
1562
1563static PyObject *
1564wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1565{
1566 intargfunc func = (intargfunc)wrapped;
1567 int i;
1568
1569 if (!PyArg_ParseTuple(args, "i", &i))
1570 return NULL;
1571 return (*func)(self, i);
1572}
1573
1574static struct wrapperbase tab_mul_int[] = {
1575 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1576 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1577 {0}
1578};
1579
1580static struct wrapperbase tab_concat[] = {
1581 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1582 {0}
1583};
1584
1585static struct wrapperbase tab_imul_int[] = {
1586 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1587 {0}
1588};
1589
1590static struct wrapperbase tab_getitem_int[] = {
1591 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1592 "x.__getitem__(i) <==> x[i]"},
1593 {0}
1594};
1595
1596static PyObject *
1597wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1598{
1599 intintargfunc func = (intintargfunc)wrapped;
1600 int i, j;
1601
1602 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1603 return NULL;
1604 return (*func)(self, i, j);
1605}
1606
1607static struct wrapperbase tab_getslice[] = {
1608 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1609 "x.__getslice__(i, j) <==> x[i:j]"},
1610 {0}
1611};
1612
1613static PyObject *
1614wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1615{
1616 intobjargproc func = (intobjargproc)wrapped;
1617 int i, res;
1618 PyObject *value;
1619
1620 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1621 return NULL;
1622 res = (*func)(self, i, value);
1623 if (res == -1 && PyErr_Occurred())
1624 return NULL;
1625 Py_INCREF(Py_None);
1626 return Py_None;
1627}
1628
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001629static PyObject *
1630wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1631{
1632 intobjargproc func = (intobjargproc)wrapped;
1633 int i, res;
1634
1635 if (!PyArg_ParseTuple(args, "i", &i))
1636 return NULL;
1637 res = (*func)(self, i, NULL);
1638 if (res == -1 && PyErr_Occurred())
1639 return NULL;
1640 Py_INCREF(Py_None);
1641 return Py_None;
1642}
1643
Tim Peters6d6c1a32001-08-02 04:15:00 +00001644static struct wrapperbase tab_setitem_int[] = {
1645 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1646 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001647 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1648 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001649 {0}
1650};
1651
1652static PyObject *
1653wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1654{
1655 intintobjargproc func = (intintobjargproc)wrapped;
1656 int i, j, res;
1657 PyObject *value;
1658
1659 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1660 return NULL;
1661 res = (*func)(self, i, j, value);
1662 if (res == -1 && PyErr_Occurred())
1663 return NULL;
1664 Py_INCREF(Py_None);
1665 return Py_None;
1666}
1667
1668static struct wrapperbase tab_setslice[] = {
1669 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1670 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1671 {0}
1672};
1673
1674/* XXX objobjproc is a misnomer; should be objargpred */
1675static PyObject *
1676wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1677{
1678 objobjproc func = (objobjproc)wrapped;
1679 int res;
1680 PyObject *value;
1681
1682 if (!PyArg_ParseTuple(args, "O", &value))
1683 return NULL;
1684 res = (*func)(self, value);
1685 if (res == -1 && PyErr_Occurred())
1686 return NULL;
1687 return PyInt_FromLong((long)res);
1688}
1689
1690static struct wrapperbase tab_contains[] = {
1691 {"__contains__", (wrapperfunc)wrap_objobjproc,
1692 "x.__contains__(y) <==> y in x"},
1693 {0}
1694};
1695
1696static PyObject *
1697wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1698{
1699 objobjargproc func = (objobjargproc)wrapped;
1700 int res;
1701 PyObject *key, *value;
1702
1703 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1704 return NULL;
1705 res = (*func)(self, key, value);
1706 if (res == -1 && PyErr_Occurred())
1707 return NULL;
1708 Py_INCREF(Py_None);
1709 return Py_None;
1710}
1711
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001712static PyObject *
1713wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1714{
1715 objobjargproc func = (objobjargproc)wrapped;
1716 int res;
1717 PyObject *key;
1718
1719 if (!PyArg_ParseTuple(args, "O", &key))
1720 return NULL;
1721 res = (*func)(self, key, NULL);
1722 if (res == -1 && PyErr_Occurred())
1723 return NULL;
1724 Py_INCREF(Py_None);
1725 return Py_None;
1726}
1727
Tim Peters6d6c1a32001-08-02 04:15:00 +00001728static struct wrapperbase tab_setitem[] = {
1729 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1730 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001731 {"__delitem__", (wrapperfunc)wrap_delitem,
1732 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001733 {0}
1734};
1735
1736static PyObject *
1737wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1738{
1739 cmpfunc func = (cmpfunc)wrapped;
1740 int res;
1741 PyObject *other;
1742
1743 if (!PyArg_ParseTuple(args, "O", &other))
1744 return NULL;
1745 res = (*func)(self, other);
1746 if (PyErr_Occurred())
1747 return NULL;
1748 return PyInt_FromLong((long)res);
1749}
1750
1751static struct wrapperbase tab_cmp[] = {
1752 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1753 "x.__cmp__(y) <==> cmp(x,y)"},
1754 {0}
1755};
1756
1757static struct wrapperbase tab_repr[] = {
1758 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1759 "x.__repr__() <==> repr(x)"},
1760 {0}
1761};
1762
1763static struct wrapperbase tab_getattr[] = {
1764 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1765 "x.__getattr__('name') <==> x.name"},
1766 {0}
1767};
1768
1769static PyObject *
1770wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1771{
1772 setattrofunc func = (setattrofunc)wrapped;
1773 int res;
1774 PyObject *name, *value;
1775
1776 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1777 return NULL;
1778 res = (*func)(self, name, value);
1779 if (res < 0)
1780 return NULL;
1781 Py_INCREF(Py_None);
1782 return Py_None;
1783}
1784
1785static PyObject *
1786wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1787{
1788 setattrofunc func = (setattrofunc)wrapped;
1789 int res;
1790 PyObject *name;
1791
1792 if (!PyArg_ParseTuple(args, "O", &name))
1793 return NULL;
1794 res = (*func)(self, name, NULL);
1795 if (res < 0)
1796 return NULL;
1797 Py_INCREF(Py_None);
1798 return Py_None;
1799}
1800
1801static struct wrapperbase tab_setattr[] = {
1802 {"__setattr__", (wrapperfunc)wrap_setattr,
1803 "x.__setattr__('name', value) <==> x.name = value"},
1804 {"__delattr__", (wrapperfunc)wrap_delattr,
1805 "x.__delattr__('name') <==> del x.name"},
1806 {0}
1807};
1808
1809static PyObject *
1810wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1811{
1812 hashfunc func = (hashfunc)wrapped;
1813 long res;
1814
1815 if (!PyArg_ParseTuple(args, ""))
1816 return NULL;
1817 res = (*func)(self);
1818 if (res == -1 && PyErr_Occurred())
1819 return NULL;
1820 return PyInt_FromLong(res);
1821}
1822
1823static struct wrapperbase tab_hash[] = {
1824 {"__hash__", (wrapperfunc)wrap_hashfunc,
1825 "x.__hash__() <==> hash(x)"},
1826 {0}
1827};
1828
1829static PyObject *
1830wrap_call(PyObject *self, PyObject *args, void *wrapped)
1831{
1832 ternaryfunc func = (ternaryfunc)wrapped;
1833
1834 /* XXX What about keyword arguments? */
1835 return (*func)(self, args, NULL);
1836}
1837
1838static struct wrapperbase tab_call[] = {
1839 {"__call__", (wrapperfunc)wrap_call,
1840 "x.__call__(...) <==> x(...)"},
1841 {0}
1842};
1843
1844static struct wrapperbase tab_str[] = {
1845 {"__str__", (wrapperfunc)wrap_unaryfunc,
1846 "x.__str__() <==> str(x)"},
1847 {0}
1848};
1849
1850static PyObject *
1851wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1852{
1853 richcmpfunc func = (richcmpfunc)wrapped;
1854 PyObject *other;
1855
1856 if (!PyArg_ParseTuple(args, "O", &other))
1857 return NULL;
1858 return (*func)(self, other, op);
1859}
1860
1861#undef RICHCMP_WRAPPER
1862#define RICHCMP_WRAPPER(NAME, OP) \
1863static PyObject * \
1864richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1865{ \
1866 return wrap_richcmpfunc(self, args, wrapped, OP); \
1867}
1868
Jack Jansen8e938b42001-08-08 15:29:49 +00001869RICHCMP_WRAPPER(lt, Py_LT)
1870RICHCMP_WRAPPER(le, Py_LE)
1871RICHCMP_WRAPPER(eq, Py_EQ)
1872RICHCMP_WRAPPER(ne, Py_NE)
1873RICHCMP_WRAPPER(gt, Py_GT)
1874RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001875
1876#undef RICHCMP_ENTRY
1877#define RICHCMP_ENTRY(NAME, EXPR) \
1878 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1879 "x.__" #NAME "__(y) <==> " EXPR}
1880
1881static struct wrapperbase tab_richcmp[] = {
1882 RICHCMP_ENTRY(lt, "x<y"),
1883 RICHCMP_ENTRY(le, "x<=y"),
1884 RICHCMP_ENTRY(eq, "x==y"),
1885 RICHCMP_ENTRY(ne, "x!=y"),
1886 RICHCMP_ENTRY(gt, "x>y"),
1887 RICHCMP_ENTRY(ge, "x>=y"),
1888 {0}
1889};
1890
1891static struct wrapperbase tab_iter[] = {
1892 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1893 {0}
1894};
1895
1896static PyObject *
1897wrap_next(PyObject *self, PyObject *args, void *wrapped)
1898{
1899 unaryfunc func = (unaryfunc)wrapped;
1900 PyObject *res;
1901
1902 if (!PyArg_ParseTuple(args, ""))
1903 return NULL;
1904 res = (*func)(self);
1905 if (res == NULL && !PyErr_Occurred())
1906 PyErr_SetNone(PyExc_StopIteration);
1907 return res;
1908}
1909
1910static struct wrapperbase tab_next[] = {
1911 {"next", (wrapperfunc)wrap_next,
1912 "x.next() -> the next value, or raise StopIteration"},
1913 {0}
1914};
1915
1916static PyObject *
1917wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1918{
1919 descrgetfunc func = (descrgetfunc)wrapped;
1920 PyObject *obj;
1921 PyObject *type = NULL;
1922
1923 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1924 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001925 return (*func)(self, obj, type);
1926}
1927
1928static struct wrapperbase tab_descr_get[] = {
1929 {"__get__", (wrapperfunc)wrap_descr_get,
1930 "descr.__get__(obj, type) -> value"},
1931 {0}
1932};
1933
1934static PyObject *
1935wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
1936{
1937 descrsetfunc func = (descrsetfunc)wrapped;
1938 PyObject *obj, *value;
1939 int ret;
1940
1941 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
1942 return NULL;
1943 ret = (*func)(self, obj, value);
1944 if (ret < 0)
1945 return NULL;
1946 Py_INCREF(Py_None);
1947 return Py_None;
1948}
1949
1950static struct wrapperbase tab_descr_set[] = {
1951 {"__set__", (wrapperfunc)wrap_descrsetfunc,
1952 "descr.__set__(obj, value)"},
1953 {0}
1954};
1955
1956static PyObject *
1957wrap_init(PyObject *self, PyObject *args, void *wrapped)
1958{
1959 initproc func = (initproc)wrapped;
1960
1961 /* XXX What about keyword arguments? */
1962 if (func(self, args, NULL) < 0)
1963 return NULL;
1964 Py_INCREF(Py_None);
1965 return Py_None;
1966}
1967
1968static struct wrapperbase tab_init[] = {
1969 {"__init__", (wrapperfunc)wrap_init,
1970 "x.__init__(...) initializes x; "
1971 "see x.__type__.__doc__ for signature"},
1972 {0}
1973};
1974
1975static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001976tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001977{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001978 PyTypeObject *type, *subtype;
1979 PyObject *arg0, *res;
1980
1981 if (self == NULL || !PyType_Check(self))
1982 Py_FatalError("__new__() called with non-type 'self'");
1983 type = (PyTypeObject *)self;
1984 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
1985 PyErr_SetString(PyExc_TypeError,
1986 "T.__new__(): not enough arguments");
1987 return NULL;
1988 }
1989 arg0 = PyTuple_GET_ITEM(args, 0);
1990 if (!PyType_Check(arg0)) {
1991 PyErr_SetString(PyExc_TypeError,
1992 "T.__new__(S): S is not a type object");
1993 return NULL;
1994 }
1995 subtype = (PyTypeObject *)arg0;
1996 if (!PyType_IsSubtype(subtype, type)) {
1997 PyErr_SetString(PyExc_TypeError,
1998 "T.__new__(S): S is not a subtype of T");
1999 return NULL;
2000 }
2001 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2002 if (args == NULL)
2003 return NULL;
2004 res = type->tp_new(subtype, args, kwds);
2005 Py_DECREF(args);
2006 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002007}
2008
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002009static struct PyMethodDef tp_new_methoddef[] = {
2010 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2011 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002012 {0}
2013};
2014
2015static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002016add_tp_new_wrapper(PyTypeObject *type)
2017{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002018 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002019
Guido van Rossumf040ede2001-08-07 16:40:56 +00002020 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2021 return 0;
2022 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002023 if (func == NULL)
2024 return -1;
2025 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2026}
2027
Guido van Rossum13d52f02001-08-10 21:24:08 +00002028static int
2029add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2030{
2031 PyObject *dict = type->tp_defined;
2032
2033 for (; wraps->name != NULL; wraps++) {
2034 PyObject *descr;
2035 if (PyDict_GetItemString(dict, wraps->name))
2036 continue;
2037 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2038 if (descr == NULL)
2039 return -1;
2040 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2041 return -1;
2042 Py_DECREF(descr);
2043 }
2044 return 0;
2045}
2046
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002047/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002048 dictionary with method descriptors for function slots. For each
2049 function slot (like tp_repr) that's defined in the type, one or
2050 more corresponding descriptors are added in the type's tp_defined
2051 dictionary under the appropriate name (like __repr__). Some
2052 function slots cause more than one descriptor to be added (for
2053 example, the nb_add slot adds both __add__ and __radd__
2054 descriptors) and some function slots compete for the same
2055 descriptor (for example both sq_item and mp_subscript generate a
2056 __getitem__ descriptor). This only adds new descriptors and
2057 doesn't overwrite entries in tp_defined that were previously
2058 defined. The descriptors contain a reference to the C function
2059 they must call, so that it's safe if they are copied into a
2060 subtype's __dict__ and the subtype has a different C function in
2061 its slot -- calling the method defined by the descriptor will call
2062 the C function that was used to create it, rather than the C
2063 function present in the slot when it is called. (This is important
2064 because a subtype may have a C function in the slot that calls the
2065 method from the dictionary, and we want to avoid infinite recursion
2066 here.) */
2067
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002068static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069add_operators(PyTypeObject *type)
2070{
2071 PySequenceMethods *sq;
2072 PyMappingMethods *mp;
2073 PyNumberMethods *nb;
2074
2075#undef ADD
2076#define ADD(SLOT, TABLE) \
2077 if (SLOT) { \
2078 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2079 return -1; \
2080 }
2081
2082 if ((sq = type->tp_as_sequence) != NULL) {
2083 ADD(sq->sq_length, tab_len);
2084 ADD(sq->sq_concat, tab_concat);
2085 ADD(sq->sq_repeat, tab_mul_int);
2086 ADD(sq->sq_item, tab_getitem_int);
2087 ADD(sq->sq_slice, tab_getslice);
2088 ADD(sq->sq_ass_item, tab_setitem_int);
2089 ADD(sq->sq_ass_slice, tab_setslice);
2090 ADD(sq->sq_contains, tab_contains);
2091 ADD(sq->sq_inplace_concat, tab_iadd);
2092 ADD(sq->sq_inplace_repeat, tab_imul_int);
2093 }
2094
2095 if ((mp = type->tp_as_mapping) != NULL) {
2096 if (sq->sq_length == NULL)
2097 ADD(mp->mp_length, tab_len);
2098 ADD(mp->mp_subscript, tab_getitem);
2099 ADD(mp->mp_ass_subscript, tab_setitem);
2100 }
2101
2102 /* We don't support "old-style numbers" because their binary
2103 operators require that both arguments have the same type;
2104 the wrappers here only work for new-style numbers. */
2105 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2106 (nb = type->tp_as_number) != NULL) {
2107 ADD(nb->nb_add, tab_add);
2108 ADD(nb->nb_subtract, tab_sub);
2109 ADD(nb->nb_multiply, tab_mul);
2110 ADD(nb->nb_divide, tab_div);
2111 ADD(nb->nb_remainder, tab_mod);
2112 ADD(nb->nb_divmod, tab_divmod);
2113 ADD(nb->nb_power, tab_pow);
2114 ADD(nb->nb_negative, tab_neg);
2115 ADD(nb->nb_positive, tab_pos);
2116 ADD(nb->nb_absolute, tab_abs);
2117 ADD(nb->nb_nonzero, tab_nonzero);
2118 ADD(nb->nb_invert, tab_invert);
2119 ADD(nb->nb_lshift, tab_lshift);
2120 ADD(nb->nb_rshift, tab_rshift);
2121 ADD(nb->nb_and, tab_and);
2122 ADD(nb->nb_xor, tab_xor);
2123 ADD(nb->nb_or, tab_or);
2124 /* We don't support coerce() -- see above comment */
2125 ADD(nb->nb_int, tab_int);
2126 ADD(nb->nb_long, tab_long);
2127 ADD(nb->nb_float, tab_float);
2128 ADD(nb->nb_oct, tab_oct);
2129 ADD(nb->nb_hex, tab_hex);
2130 ADD(nb->nb_inplace_add, tab_iadd);
2131 ADD(nb->nb_inplace_subtract, tab_isub);
2132 ADD(nb->nb_inplace_multiply, tab_imul);
2133 ADD(nb->nb_inplace_divide, tab_idiv);
2134 ADD(nb->nb_inplace_remainder, tab_imod);
2135 ADD(nb->nb_inplace_power, tab_ipow);
2136 ADD(nb->nb_inplace_lshift, tab_ilshift);
2137 ADD(nb->nb_inplace_rshift, tab_irshift);
2138 ADD(nb->nb_inplace_and, tab_iand);
2139 ADD(nb->nb_inplace_xor, tab_ixor);
2140 ADD(nb->nb_inplace_or, tab_ior);
2141 }
2142
2143 ADD(type->tp_getattro, tab_getattr);
2144 ADD(type->tp_setattro, tab_setattr);
2145 ADD(type->tp_compare, tab_cmp);
2146 ADD(type->tp_repr, tab_repr);
2147 ADD(type->tp_hash, tab_hash);
2148 ADD(type->tp_call, tab_call);
2149 ADD(type->tp_str, tab_str);
2150 ADD(type->tp_richcompare, tab_richcmp);
2151 ADD(type->tp_iter, tab_iter);
2152 ADD(type->tp_iternext, tab_next);
2153 ADD(type->tp_descr_get, tab_descr_get);
2154 ADD(type->tp_descr_set, tab_descr_set);
2155 ADD(type->tp_init, tab_init);
2156
Guido van Rossumf040ede2001-08-07 16:40:56 +00002157 if (type->tp_new != NULL) {
2158 if (add_tp_new_wrapper(type) < 0)
2159 return -1;
2160 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002161
2162 return 0;
2163}
2164
Guido van Rossumf040ede2001-08-07 16:40:56 +00002165/* Slot wrappers that call the corresponding __foo__ slot. See comments
2166 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002167
Guido van Rossumdc91b992001-08-08 22:26:22 +00002168#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002169static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002170FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002171{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002172 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002173}
2174
Guido van Rossumdc91b992001-08-08 22:26:22 +00002175#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002177FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002178{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002179 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002180}
2181
Guido van Rossumdc91b992001-08-08 22:26:22 +00002182
2183#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002184static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002185FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002186{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002187 if (self->ob_type->tp_as_number != NULL && \
2188 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2189 PyObject *r; \
2190 r = PyObject_CallMethod( \
2191 self, OPSTR, "O", other); \
2192 if (r != Py_NotImplemented || \
2193 other->ob_type == self->ob_type) \
2194 return r; \
2195 Py_DECREF(r); \
2196 } \
2197 if (other->ob_type->tp_as_number != NULL && \
2198 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2199 return PyObject_CallMethod( \
2200 other, ROPSTR, "O", self); \
2201 } \
2202 Py_INCREF(Py_NotImplemented); \
2203 return Py_NotImplemented; \
2204}
2205
2206#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2207 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2208
2209#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2210static PyObject * \
2211FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2212{ \
2213 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214}
2215
2216static int
2217slot_sq_length(PyObject *self)
2218{
2219 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2220
2221 if (res == NULL)
2222 return -1;
2223 return (int)PyInt_AsLong(res);
2224}
2225
Guido van Rossumdc91b992001-08-08 22:26:22 +00002226SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2227SLOT1(slot_sq_repeat, "__mul__", int, "i")
2228SLOT1(slot_sq_item, "__getitem__", int, "i")
2229SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002230
2231static int
2232slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2233{
2234 PyObject *res;
2235
2236 if (value == NULL)
2237 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2238 else
2239 res = PyObject_CallMethod(self, "__setitem__",
2240 "iO", index, value);
2241 if (res == NULL)
2242 return -1;
2243 Py_DECREF(res);
2244 return 0;
2245}
2246
2247static int
2248slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2249{
2250 PyObject *res;
2251
2252 if (value == NULL)
2253 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2254 else
2255 res = PyObject_CallMethod(self, "__setslice__",
2256 "iiO", i, j, value);
2257 if (res == NULL)
2258 return -1;
2259 Py_DECREF(res);
2260 return 0;
2261}
2262
2263static int
2264slot_sq_contains(PyObject *self, PyObject *value)
2265{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002266 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002267
Guido van Rossumb8f63662001-08-15 23:57:02 +00002268 func = PyObject_GetAttrString(self, "__contains__");
2269
2270 if (func != NULL) {
2271 args = Py_BuildValue("(O)", value);
2272 if (args == NULL)
2273 res = NULL;
2274 else {
2275 res = PyEval_CallObject(func, args);
2276 Py_DECREF(args);
2277 }
2278 Py_DECREF(func);
2279 if (res == NULL)
2280 return -1;
2281 return PyObject_IsTrue(res);
2282 }
2283 else {
2284 PyErr_Clear();
2285 return _PySequence_IterContains(self, value);
2286 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002287}
2288
Guido van Rossumdc91b992001-08-08 22:26:22 +00002289SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2290SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002291
2292#define slot_mp_length slot_sq_length
2293
Guido van Rossumdc91b992001-08-08 22:26:22 +00002294SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002295
2296static int
2297slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2298{
2299 PyObject *res;
2300
2301 if (value == NULL)
2302 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2303 else
2304 res = PyObject_CallMethod(self, "__setitem__",
2305 "OO", key, value);
2306 if (res == NULL)
2307 return -1;
2308 Py_DECREF(res);
2309 return 0;
2310}
2311
Guido van Rossumdc91b992001-08-08 22:26:22 +00002312SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2313SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2314SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2315SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2316SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2317SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2318
2319staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2320
2321SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2322 nb_power, "__pow__", "__rpow__")
2323
2324static PyObject *
2325slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2326{
2327 if (modulus == Py_None)
2328 return slot_nb_power_binary(self, other);
2329 /* Three-arg power doesn't use __rpow__ */
2330 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2331}
2332
2333SLOT0(slot_nb_negative, "__neg__")
2334SLOT0(slot_nb_positive, "__pos__")
2335SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002336
2337static int
2338slot_nb_nonzero(PyObject *self)
2339{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002340 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002341
Guido van Rossumb8f63662001-08-15 23:57:02 +00002342 func = PyObject_GetAttrString(self, "__nonzero__");
2343 if (func == NULL) {
2344 PyErr_Clear();
2345 func = PyObject_GetAttrString(self, "__len__");
2346 }
2347
2348 if (func != NULL) {
2349 res = PyEval_CallObject(func, NULL);
2350 Py_DECREF(func);
2351 if (res == NULL)
2352 return -1;
2353 return PyObject_IsTrue(res);
2354 }
2355 else {
2356 PyErr_Clear();
2357 return 1;
2358 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002359}
2360
Guido van Rossumdc91b992001-08-08 22:26:22 +00002361SLOT0(slot_nb_invert, "__invert__")
2362SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2363SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2364SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2365SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2366SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002367/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002368SLOT0(slot_nb_int, "__int__")
2369SLOT0(slot_nb_long, "__long__")
2370SLOT0(slot_nb_float, "__float__")
2371SLOT0(slot_nb_oct, "__oct__")
2372SLOT0(slot_nb_hex, "__hex__")
2373SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2374SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2375SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2376SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2377SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2378SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2379SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2380SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2381SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2382SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2383SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2384SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2385 "__floordiv__", "__rfloordiv__")
2386SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2387SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2388SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002389
2390static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002391half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002392{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002393 PyObject *func, *args, *res;
2394 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002395
Guido van Rossumb8f63662001-08-15 23:57:02 +00002396 func = PyObject_GetAttrString(self, "__cmp__");
2397 if (func == NULL) {
2398 PyErr_Clear();
2399 }
2400 else {
2401 args = Py_BuildValue("(O)", other);
2402 if (args == NULL)
2403 res = NULL;
2404 else {
2405 res = PyObject_CallObject(func, args);
2406 Py_DECREF(args);
2407 }
2408 if (res != Py_NotImplemented) {
2409 if (res == NULL)
2410 return -2;
2411 c = PyInt_AsLong(res);
2412 Py_DECREF(res);
2413 if (c == -1 && PyErr_Occurred())
2414 return -2;
2415 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2416 }
2417 Py_DECREF(res);
2418 }
2419 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002420}
2421
Guido van Rossumb8f63662001-08-15 23:57:02 +00002422static int
2423slot_tp_compare(PyObject *self, PyObject *other)
2424{
2425 int c;
2426
2427 if (self->ob_type->tp_compare == slot_tp_compare) {
2428 c = half_compare(self, other);
2429 if (c <= 1)
2430 return c;
2431 }
2432 if (other->ob_type->tp_compare == slot_tp_compare) {
2433 c = half_compare(other, self);
2434 if (c < -1)
2435 return -2;
2436 if (c <= 1)
2437 return -c;
2438 }
2439 return (void *)self < (void *)other ? -1 :
2440 (void *)self > (void *)other ? 1 : 0;
2441}
2442
2443static PyObject *
2444slot_tp_repr(PyObject *self)
2445{
2446 PyObject *func, *res;
2447
2448 func = PyObject_GetAttrString(self, "__repr__");
2449 if (func != NULL) {
2450 res = PyEval_CallObject(func, NULL);
2451 Py_DECREF(func);
2452 return res;
2453 }
2454 else {
2455 char buf[120];
2456 PyErr_Clear();
2457 sprintf(buf, "<%.80s object at %p>",
2458 self->ob_type->tp_name, self);
2459 return PyString_FromString(buf);
2460 }
2461}
2462
2463static PyObject *
2464slot_tp_str(PyObject *self)
2465{
2466 PyObject *func, *res;
2467
2468 func = PyObject_GetAttrString(self, "__str__");
2469 if (func != NULL) {
2470 res = PyEval_CallObject(func, NULL);
2471 Py_DECREF(func);
2472 return res;
2473 }
2474 else {
2475 PyErr_Clear();
2476 return slot_tp_repr(self);
2477 }
2478}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002479
2480static long
2481slot_tp_hash(PyObject *self)
2482{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002483 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002484 long h;
2485
Guido van Rossumb8f63662001-08-15 23:57:02 +00002486 func = PyObject_GetAttrString(self, "__hash__");
2487
2488 if (func != NULL) {
2489 res = PyEval_CallObject(func, NULL);
2490 Py_DECREF(func);
2491 if (res == NULL)
2492 return -1;
2493 h = PyInt_AsLong(res);
2494 }
2495 else {
2496 PyErr_Clear();
2497 func = PyObject_GetAttrString(self, "__eq__");
2498 if (func == NULL) {
2499 PyErr_Clear();
2500 func = PyObject_GetAttrString(self, "__cmp__");
2501 }
2502 if (func != NULL) {
2503 Py_DECREF(func);
2504 PyErr_SetString(PyExc_TypeError, "unhashable type");
2505 return -1;
2506 }
2507 PyErr_Clear();
2508 h = _Py_HashPointer((void *)self);
2509 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510 if (h == -1 && !PyErr_Occurred())
2511 h = -2;
2512 return h;
2513}
2514
2515static PyObject *
2516slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2517{
2518 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2519 PyObject *res;
2520
2521 if (meth == NULL)
2522 return NULL;
2523 res = PyObject_Call(meth, args, kwds);
2524 Py_DECREF(meth);
2525 return res;
2526}
2527
Tim Peters6d6c1a32001-08-02 04:15:00 +00002528static PyObject *
2529slot_tp_getattro(PyObject *self, PyObject *name)
2530{
2531 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002532 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002533 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002534
Guido van Rossum8e248182001-08-12 05:17:56 +00002535 if (getattr_str == NULL) {
2536 getattr_str = PyString_InternFromString("__getattr__");
2537 if (getattr_str == NULL)
2538 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002539 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002540 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002541 if (getattr == NULL) {
2542 /* Avoid further slowdowns */
2543 if (tp->tp_getattro == slot_tp_getattro)
2544 tp->tp_getattro = PyObject_GenericGetAttr;
2545 else
2546 fprintf(stderr, "huh?\n");
Guido van Rossum8e248182001-08-12 05:17:56 +00002547 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002548 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002549 return PyObject_CallFunction(getattr, "OO", self, name);
2550}
2551
2552static int
2553slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2554{
2555 PyObject *res;
2556
2557 if (value == NULL)
2558 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2559 else
2560 res = PyObject_CallMethod(self, "__setattr__",
2561 "OO", name, value);
2562 if (res == NULL)
2563 return -1;
2564 Py_DECREF(res);
2565 return 0;
2566}
2567
2568/* Map rich comparison operators to their __xx__ namesakes */
2569static char *name_op[] = {
2570 "__lt__",
2571 "__le__",
2572 "__eq__",
2573 "__ne__",
2574 "__gt__",
2575 "__ge__",
2576};
2577
2578static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002579half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002581 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002582
Guido van Rossumb8f63662001-08-15 23:57:02 +00002583 func = PyObject_GetAttrString(self, name_op[op]);
2584 if (func == NULL) {
2585 PyErr_Clear();
2586 Py_INCREF(Py_NotImplemented);
2587 return Py_NotImplemented;
2588 }
2589 args = Py_BuildValue("(O)", other);
2590 if (args == NULL)
2591 res = NULL;
2592 else {
2593 res = PyObject_CallObject(func, args);
2594 Py_DECREF(args);
2595 }
2596 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002597 return res;
2598}
2599
Guido van Rossumb8f63662001-08-15 23:57:02 +00002600/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2601static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2602
2603static PyObject *
2604slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2605{
2606 PyObject *res;
2607
2608 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2609 res = half_richcompare(self, other, op);
2610 if (res != Py_NotImplemented)
2611 return res;
2612 Py_DECREF(res);
2613 }
2614 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2615 res = half_richcompare(other, self, swapped_op[op]);
2616 if (res != Py_NotImplemented) {
2617 return res;
2618 }
2619 Py_DECREF(res);
2620 }
2621 Py_INCREF(Py_NotImplemented);
2622 return Py_NotImplemented;
2623}
2624
2625static PyObject *
2626slot_tp_iter(PyObject *self)
2627{
2628 PyObject *func, *res;
2629
2630 func = PyObject_GetAttrString(self, "__iter__");
2631 if (func != NULL) {
2632 res = PyObject_CallObject(func, NULL);
2633 Py_DECREF(func);
2634 return res;
2635 }
2636 PyErr_Clear();
2637 func = PyObject_GetAttrString(self, "__getitem__");
2638 if (func == NULL) {
2639 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2640 return NULL;
2641 }
2642 Py_DECREF(func);
2643 return PySeqIter_New(self);
2644}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002645
2646static PyObject *
2647slot_tp_iternext(PyObject *self)
2648{
2649 return PyObject_CallMethod(self, "next", "");
2650}
2651
Guido van Rossumdc91b992001-08-08 22:26:22 +00002652SLOT2(slot_tp_descr_get, "__get__", PyObject *, PyObject *, "OO")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002653
2654static int
2655slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2656{
2657 PyObject *res = PyObject_CallMethod(self, "__set__",
2658 "OO", target, value);
2659 if (res == NULL)
2660 return -1;
2661 Py_DECREF(res);
2662 return 0;
2663}
2664
2665static int
2666slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2667{
2668 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2669 PyObject *res;
2670
2671 if (meth == NULL)
2672 return -1;
2673 res = PyObject_Call(meth, args, kwds);
2674 Py_DECREF(meth);
2675 if (res == NULL)
2676 return -1;
2677 Py_DECREF(res);
2678 return 0;
2679}
2680
2681static PyObject *
2682slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2683{
2684 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2685 PyObject *newargs, *x;
2686 int i, n;
2687
2688 if (func == NULL)
2689 return NULL;
2690 assert(PyTuple_Check(args));
2691 n = PyTuple_GET_SIZE(args);
2692 newargs = PyTuple_New(n+1);
2693 if (newargs == NULL)
2694 return NULL;
2695 Py_INCREF(type);
2696 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2697 for (i = 0; i < n; i++) {
2698 x = PyTuple_GET_ITEM(args, i);
2699 Py_INCREF(x);
2700 PyTuple_SET_ITEM(newargs, i+1, x);
2701 }
2702 x = PyObject_Call(func, newargs, kwds);
2703 Py_DECREF(func);
2704 return x;
2705}
2706
Guido van Rossumf040ede2001-08-07 16:40:56 +00002707/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002708 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002709 The dict argument is the dictionary argument passed to type_new(),
2710 which is the local namespace of the class statement, in other
2711 words, it contains the methods. For each special method (like
2712 __repr__) defined in the dictionary, the corresponding function
2713 slot in the type object (like tp_repr) is set to a special function
2714 whose name is 'slot_' followed by the slot name and whose signature
2715 is whatever is required for that slot. These slot functions look
2716 up the corresponding method in the type's dictionary and call it.
2717 The slot functions have to take care of the various peculiarities
2718 of the mapping between slots and special methods, such as mapping
2719 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2720 etc.) or mapping multiple slots to a single method (sq_item,
2721 mp_subscript <--> __getitem__). */
2722
Tim Peters6d6c1a32001-08-02 04:15:00 +00002723static void
2724override_slots(PyTypeObject *type, PyObject *dict)
2725{
2726 PySequenceMethods *sq = type->tp_as_sequence;
2727 PyMappingMethods *mp = type->tp_as_mapping;
2728 PyNumberMethods *nb = type->tp_as_number;
2729
Guido van Rossumdc91b992001-08-08 22:26:22 +00002730#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002731 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002732 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002733 }
2734
Guido van Rossumdc91b992001-08-08 22:26:22 +00002735#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002736 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002737 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002738 }
2739
Guido van Rossumdc91b992001-08-08 22:26:22 +00002740#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002741 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002742 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002743 }
2744
Guido van Rossumdc91b992001-08-08 22:26:22 +00002745#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002746 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002747 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002748 }
2749
Guido van Rossumdc91b992001-08-08 22:26:22 +00002750 SQSLOT("__len__", sq_length, slot_sq_length);
2751 SQSLOT("__add__", sq_concat, slot_sq_concat);
2752 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2753 SQSLOT("__getitem__", sq_item, slot_sq_item);
2754 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2755 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2756 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2757 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2758 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2759 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2760 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2761 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002762
Guido van Rossumdc91b992001-08-08 22:26:22 +00002763 MPSLOT("__len__", mp_length, slot_mp_length);
2764 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2765 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2766 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002767
Guido van Rossumdc91b992001-08-08 22:26:22 +00002768 NBSLOT("__add__", nb_add, slot_nb_add);
2769 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2770 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2771 NBSLOT("__div__", nb_divide, slot_nb_divide);
2772 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2773 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2774 NBSLOT("__pow__", nb_power, slot_nb_power);
2775 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2776 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2777 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2778 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2779 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2780 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2781 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2782 NBSLOT("__and__", nb_and, slot_nb_and);
2783 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2784 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002785 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002786 NBSLOT("__int__", nb_int, slot_nb_int);
2787 NBSLOT("__long__", nb_long, slot_nb_long);
2788 NBSLOT("__float__", nb_float, slot_nb_float);
2789 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2790 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2791 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2792 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2793 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2794 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2795 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2796 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2797 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2798 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2799 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2800 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2801 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2802 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2803 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2804 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2805 slot_nb_inplace_floor_divide);
2806 NBSLOT("__itruediv__", nb_inplace_true_divide,
2807 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002808
Guido van Rossum8e248182001-08-12 05:17:56 +00002809 if (dict == NULL ||
2810 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002811 PyDict_GetItemString(dict, "__repr__"))
2812 type->tp_print = NULL;
2813
Guido van Rossumdc91b992001-08-08 22:26:22 +00002814 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2815 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2816 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2817 TPSLOT("__call__", tp_call, slot_tp_call);
2818 TPSLOT("__str__", tp_str, slot_tp_str);
2819 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2820 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2821 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2822 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2823 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2824 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2825 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2826 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2827 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2828 TPSLOT("next", tp_iternext, slot_tp_iternext);
2829 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2830 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2831 TPSLOT("__init__", tp_init, slot_tp_init);
2832 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002833}