blob: f7f2c9713bfdfbc733350be00c3029fb1f23886a [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 *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000379mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000380{
381 PyTypeObject *type = (PyTypeObject *)self;
382
Tim Peters6d6c1a32001-08-02 04:15:00 +0000383 return mro_implementation(type);
384}
385
386static int
387mro_internal(PyTypeObject *type)
388{
389 PyObject *mro, *result, *tuple;
390
391 if (type->ob_type == &PyType_Type) {
392 result = mro_implementation(type);
393 }
394 else {
395 mro = PyObject_GetAttrString((PyObject *)type, "mro");
396 if (mro == NULL)
397 return -1;
398 result = PyObject_CallObject(mro, NULL);
399 Py_DECREF(mro);
400 }
401 if (result == NULL)
402 return -1;
403 tuple = PySequence_Tuple(result);
404 Py_DECREF(result);
405 type->tp_mro = tuple;
406 return 0;
407}
408
409
410/* Calculate the best base amongst multiple base classes.
411 This is the first one that's on the path to the "solid base". */
412
413static PyTypeObject *
414best_base(PyObject *bases)
415{
416 int i, n;
417 PyTypeObject *base, *winner, *candidate, *base_i;
418
419 assert(PyTuple_Check(bases));
420 n = PyTuple_GET_SIZE(bases);
421 assert(n > 0);
422 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
423 winner = &PyBaseObject_Type;
424 for (i = 0; i < n; i++) {
425 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
426 if (!PyType_Check((PyObject *)base_i)) {
427 PyErr_SetString(
428 PyExc_TypeError,
429 "bases must be types");
430 return NULL;
431 }
432 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000433 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000434 return NULL;
435 }
436 candidate = solid_base(base_i);
437 if (PyType_IsSubtype(winner, candidate))
438 ;
439 else if (PyType_IsSubtype(candidate, winner)) {
440 winner = candidate;
441 base = base_i;
442 }
443 else {
444 PyErr_SetString(
445 PyExc_TypeError,
446 "multiple bases have "
447 "instance lay-out conflict");
448 return NULL;
449 }
450 }
451 assert(base != NULL);
452 return base;
453}
454
455static int
456extra_ivars(PyTypeObject *type, PyTypeObject *base)
457{
458 int t_size = PyType_BASICSIZE(type);
459 int b_size = PyType_BASICSIZE(base);
460
461 assert(t_size >= b_size); /* type smaller than base! */
462 if (type->tp_itemsize || base->tp_itemsize) {
463 /* If itemsize is involved, stricter rules */
464 return t_size != b_size ||
465 type->tp_itemsize != base->tp_itemsize;
466 }
467 if (t_size == b_size)
468 return 0;
469 if (type->tp_dictoffset != 0 && base->tp_dictoffset == 0 &&
470 type->tp_dictoffset == b_size &&
471 (size_t)t_size == b_size + sizeof(PyObject *))
472 return 0; /* "Forgive" adding a __dict__ only */
473 return 1;
474}
475
476static PyTypeObject *
477solid_base(PyTypeObject *type)
478{
479 PyTypeObject *base;
480
481 if (type->tp_base)
482 base = solid_base(type->tp_base);
483 else
484 base = &PyBaseObject_Type;
485 if (extra_ivars(type, base))
486 return type;
487 else
488 return base;
489}
490
491staticforward void object_dealloc(PyObject *);
492staticforward int object_init(PyObject *, PyObject *, PyObject *);
493
494static PyObject *
495type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
496{
497 PyObject *name, *bases, *dict;
498 static char *kwlist[] = {"name", "bases", "dict", 0};
499 PyObject *slots, *tmp;
500 PyTypeObject *type, *base, *tmptype;
501 etype *et;
502 struct memberlist *mp;
503 int i, nbases, nslots, slotoffset, dynamic;
504
505 if (metatype == &PyType_Type &&
506 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
507 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
508 /* type(x) -> x.__class__ */
509 PyObject *x = PyTuple_GET_ITEM(args, 0);
510 Py_INCREF(x->ob_type);
511 return (PyObject *) x->ob_type;
512 }
513
514 /* Check arguments */
515 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
516 &name,
517 &PyTuple_Type, &bases,
518 &PyDict_Type, &dict))
519 return NULL;
520
521 /* Determine the proper metatype to deal with this,
522 and check for metatype conflicts while we're at it.
523 Note that if some other metatype wins to contract,
524 it's possible that its instances are not types. */
525 nbases = PyTuple_GET_SIZE(bases);
526 for (i = 0; i < nbases; i++) {
527 tmp = PyTuple_GET_ITEM(bases, i);
528 tmptype = tmp->ob_type;
529 if (PyType_IsSubtype(metatype, tmptype))
530 continue;
531 if (PyType_IsSubtype(tmptype, metatype)) {
532 metatype = tmptype;
533 continue;
534 }
535 PyErr_SetString(PyExc_TypeError,
536 "metatype conflict among bases");
537 return NULL;
538 }
539 if (metatype->tp_new != type_new) /* Pass it to the winner */
540 return metatype->tp_new(metatype, args, kwds);
541
542 /* Adjust for empty tuple bases */
543 if (nbases == 0) {
544 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
545 if (bases == NULL)
546 return NULL;
547 nbases = 1;
548 }
549 else
550 Py_INCREF(bases);
551
552 /* XXX From here until type is allocated, "return NULL" leaks bases! */
553
554 /* Calculate best base, and check that all bases are type objects */
555 base = best_base(bases);
556 if (base == NULL)
557 return NULL;
558 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
559 PyErr_Format(PyExc_TypeError,
560 "type '%.100s' is not an acceptable base type",
561 base->tp_name);
562 return NULL;
563 }
564
565 /* Should this be a dynamic class (i.e. modifiable __dict__)? */
566 tmp = PyDict_GetItemString(dict, "__dynamic__");
567 if (tmp != NULL) {
568 /* The class author has a preference */
569 dynamic = PyObject_IsTrue(tmp);
570 Py_DECREF(tmp);
571 if (dynamic < 0)
572 return NULL;
573 }
574 else {
575 /* Make a new class dynamic if any of its bases is dynamic.
576 This is not always the same as inheriting the __dynamic__
577 class attribute! */
578 dynamic = 0;
579 for (i = 0; i < nbases; i++) {
580 tmptype = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
581 if (tmptype->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
582 dynamic = 1;
583 break;
584 }
585 }
586 }
587
588 /* Check for a __slots__ sequence variable in dict, and count it */
589 slots = PyDict_GetItemString(dict, "__slots__");
590 nslots = 0;
591 if (slots != NULL) {
592 /* Make it into a tuple */
593 if (PyString_Check(slots))
594 slots = Py_BuildValue("(O)", slots);
595 else
596 slots = PySequence_Tuple(slots);
597 if (slots == NULL)
598 return NULL;
599 nslots = PyTuple_GET_SIZE(slots);
600 for (i = 0; i < nslots; i++) {
601 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
602 PyErr_SetString(PyExc_TypeError,
603 "__slots__ must be a sequence of strings");
604 Py_DECREF(slots);
605 return NULL;
606 }
607 }
608 }
609 if (slots == NULL && base->tp_dictoffset == 0 &&
610 (base->tp_setattro == PyObject_GenericSetAttr ||
611 base->tp_setattro == NULL))
612 nslots = 1;
613
614 /* XXX From here until type is safely allocated,
615 "return NULL" may leak slots! */
616
617 /* Allocate the type object */
618 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
619 if (type == NULL)
620 return NULL;
621
622 /* Keep name and slots alive in the extended type object */
623 et = (etype *)type;
624 Py_INCREF(name);
625 et->name = name;
626 et->slots = slots;
627
Guido van Rossumdc91b992001-08-08 22:26:22 +0000628 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000629 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
630 Py_TPFLAGS_BASETYPE;
631 if (dynamic)
632 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000633
634 /* It's a new-style number unless it specifically inherits any
635 old-style numeric behavior */
636 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
637 (base->tp_as_number == NULL))
638 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
639
640 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641 type->tp_as_number = &et->as_number;
642 type->tp_as_sequence = &et->as_sequence;
643 type->tp_as_mapping = &et->as_mapping;
644 type->tp_as_buffer = &et->as_buffer;
645 type->tp_name = PyString_AS_STRING(name);
646
647 /* Set tp_base and tp_bases */
648 type->tp_bases = bases;
649 Py_INCREF(base);
650 type->tp_base = base;
651
652 /* Initialize tp_defined from passed-in dict */
653 type->tp_defined = dict = PyDict_Copy(dict);
654 if (dict == NULL) {
655 Py_DECREF(type);
656 return NULL;
657 }
658
Guido van Rossumc3542212001-08-16 09:18:56 +0000659 /* Set __module__ in the dict */
660 if (PyDict_GetItemString(dict, "__module__") == NULL) {
661 tmp = PyEval_GetGlobals();
662 if (tmp != NULL) {
663 tmp = PyDict_GetItemString(tmp, "__name__");
664 if (tmp != NULL) {
665 if (PyDict_SetItemString(dict, "__module__",
666 tmp) < 0)
667 return NULL;
668 }
669 }
670 }
671
Tim Peters6d6c1a32001-08-02 04:15:00 +0000672 /* Special-case __new__: if it's a plain function,
673 make it a static function */
674 tmp = PyDict_GetItemString(dict, "__new__");
675 if (tmp != NULL && PyFunction_Check(tmp)) {
676 tmp = PyStaticMethod_New(tmp);
677 if (tmp == NULL) {
678 Py_DECREF(type);
679 return NULL;
680 }
681 PyDict_SetItemString(dict, "__new__", tmp);
682 Py_DECREF(tmp);
683 }
684
685 /* Add descriptors for custom slots from __slots__, or for __dict__ */
686 mp = et->members;
687 slotoffset = PyType_BASICSIZE(base);
688 if (slots != NULL) {
689 for (i = 0; i < nslots; i++, mp++) {
690 mp->name = PyString_AS_STRING(
691 PyTuple_GET_ITEM(slots, i));
692 mp->type = T_OBJECT;
693 mp->offset = slotoffset;
694 slotoffset += sizeof(PyObject *);
695 }
696 }
697 else if (nslots) {
698 type->tp_dictoffset = slotoffset;
699 mp->name = "__dict__";
700 mp->type = T_OBJECT;
701 mp->offset = slotoffset;
702 mp->readonly = 1;
703 slotoffset += sizeof(PyObject *);
704 }
705 type->tp_basicsize = slotoffset;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000706 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707
708 /* Special case some slots */
709 if (type->tp_dictoffset != 0 || nslots > 0) {
710 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
711 type->tp_getattro = PyObject_GenericGetAttr;
712 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
713 type->tp_setattro = PyObject_GenericSetAttr;
714 }
715 type->tp_dealloc = subtype_dealloc;
716
717 /* Always override allocation strategy to use regular heap */
718 type->tp_alloc = PyType_GenericAlloc;
719 type->tp_free = _PyObject_Del;
720
721 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000722 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000723 Py_DECREF(type);
724 return NULL;
725 }
726
727 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000728 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
729 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000730
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 return (PyObject *)type;
732}
733
734/* Internal API to look for a name through the MRO.
735 This returns a borrowed reference, and doesn't set an exception! */
736PyObject *
737_PyType_Lookup(PyTypeObject *type, PyObject *name)
738{
739 int i, n;
740 PyObject *mro, *res, *dict;
741
742 /* For static types, look in tp_dict */
743 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
744 dict = type->tp_dict;
745 assert(dict && PyDict_Check(dict));
746 return PyDict_GetItem(dict, name);
747 }
748
749 /* For dynamic types, look in tp_defined of types in MRO */
750 mro = type->tp_mro;
751 assert(PyTuple_Check(mro));
752 n = PyTuple_GET_SIZE(mro);
753 for (i = 0; i < n; i++) {
754 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
755 assert(PyType_Check(type));
756 dict = type->tp_defined;
757 assert(dict && PyDict_Check(dict));
758 res = PyDict_GetItem(dict, name);
759 if (res != NULL)
760 return res;
761 }
762 return NULL;
763}
764
765/* This is similar to PyObject_GenericGetAttr(),
766 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
767static PyObject *
768type_getattro(PyTypeObject *type, PyObject *name)
769{
770 PyTypeObject *metatype = type->ob_type;
771 PyObject *descr, *res;
772 descrgetfunc f;
773
774 /* Initialize this type (we'll assume the metatype is initialized) */
775 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000776 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000777 return NULL;
778 }
779
780 /* Get a descriptor from the metatype */
781 descr = _PyType_Lookup(metatype, name);
782 f = NULL;
783 if (descr != NULL) {
784 f = descr->ob_type->tp_descr_get;
785 if (f != NULL && PyDescr_IsData(descr))
786 return f(descr,
787 (PyObject *)type, (PyObject *)metatype);
788 }
789
790 /* Look in tp_defined of this type and its bases */
791 res = _PyType_Lookup(type, name);
792 if (res != NULL) {
793 f = res->ob_type->tp_descr_get;
794 if (f != NULL)
795 return f(res, (PyObject *)NULL, (PyObject *)type);
796 Py_INCREF(res);
797 return res;
798 }
799
800 /* Use the descriptor from the metatype */
801 if (f != NULL) {
802 res = f(descr, (PyObject *)type, (PyObject *)metatype);
803 return res;
804 }
805 if (descr != NULL) {
806 Py_INCREF(descr);
807 return descr;
808 }
809
810 /* Give up */
811 PyErr_Format(PyExc_AttributeError,
812 "type object '%.50s' has no attribute '%.400s'",
813 type->tp_name, PyString_AS_STRING(name));
814 return NULL;
815}
816
817static int
818type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
819{
820 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
821 return PyObject_GenericSetAttr((PyObject *)type, name, value);
822 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
823 return -1;
824}
825
826static void
827type_dealloc(PyTypeObject *type)
828{
829 etype *et;
830
831 /* Assert this is a heap-allocated type object */
832 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
833 et = (etype *)type;
834 Py_XDECREF(type->tp_base);
835 Py_XDECREF(type->tp_dict);
836 Py_XDECREF(type->tp_bases);
837 Py_XDECREF(type->tp_mro);
838 Py_XDECREF(type->tp_defined);
839 /* XXX more? */
840 Py_XDECREF(et->name);
841 Py_XDECREF(et->slots);
842 type->ob_type->tp_free((PyObject *)type);
843}
844
845static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000846 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000847 "mro() -> list\nreturn a type's method resolution order"},
848 {0}
849};
850
851static char type_doc[] =
852"type(object) -> the object's type\n"
853"type(name, bases, dict) -> a new type";
854
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000855PyTypeObject PyType_Type = {
856 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000857 0, /* ob_size */
858 "type", /* tp_name */
859 sizeof(etype), /* tp_basicsize */
860 sizeof(struct memberlist), /* tp_itemsize */
861 (destructor)type_dealloc, /* tp_dealloc */
862 0, /* tp_print */
863 0, /* tp_getattr */
864 0, /* tp_setattr */
865 type_compare, /* tp_compare */
866 (reprfunc)type_repr, /* tp_repr */
867 0, /* tp_as_number */
868 0, /* tp_as_sequence */
869 0, /* tp_as_mapping */
870 (hashfunc)_Py_HashPointer, /* tp_hash */
871 (ternaryfunc)type_call, /* tp_call */
872 0, /* tp_str */
873 (getattrofunc)type_getattro, /* tp_getattro */
874 (setattrofunc)type_setattro, /* tp_setattro */
875 0, /* tp_as_buffer */
876 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
877 type_doc, /* tp_doc */
878 0, /* tp_traverse */
879 0, /* tp_clear */
880 0, /* tp_richcompare */
881 0, /* tp_weaklistoffset */
882 0, /* tp_iter */
883 0, /* tp_iternext */
884 type_methods, /* tp_methods */
885 type_members, /* tp_members */
886 type_getsets, /* tp_getset */
887 0, /* tp_base */
888 0, /* tp_dict */
889 0, /* tp_descr_get */
890 0, /* tp_descr_set */
891 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
892 0, /* tp_init */
893 0, /* tp_alloc */
894 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000895};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000896
897
898/* The base type of all types (eventually)... except itself. */
899
900static int
901object_init(PyObject *self, PyObject *args, PyObject *kwds)
902{
903 return 0;
904}
905
906static void
907object_dealloc(PyObject *self)
908{
909 self->ob_type->tp_free(self);
910}
911
Guido van Rossum8e248182001-08-12 05:17:56 +0000912static PyObject *
913object_repr(PyObject *self)
914{
915 char buf[120];
916
917 sprintf(buf, "<%.80s object at %p>", self->ob_type->tp_name, self);
918 return PyString_FromString(buf);
919}
920
Guido van Rossumb8f63662001-08-15 23:57:02 +0000921static PyObject *
922object_str(PyObject *self)
923{
924 unaryfunc f;
925
926 f = self->ob_type->tp_repr;
927 if (f == NULL)
928 f = object_repr;
929 return f(self);
930}
931
Guido van Rossum8e248182001-08-12 05:17:56 +0000932static long
933object_hash(PyObject *self)
934{
935 return _Py_HashPointer(self);
936}
Guido van Rossum8e248182001-08-12 05:17:56 +0000937
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938static void
939object_free(PyObject *self)
940{
941 PyObject_Del(self);
942}
943
944static struct memberlist object_members[] = {
945 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
946 {0}
947};
948
949PyTypeObject PyBaseObject_Type = {
950 PyObject_HEAD_INIT(&PyType_Type)
951 0, /* ob_size */
952 "object", /* tp_name */
953 sizeof(PyObject), /* tp_basicsize */
954 0, /* tp_itemsize */
955 (destructor)object_dealloc, /* tp_dealloc */
956 0, /* tp_print */
957 0, /* tp_getattr */
958 0, /* tp_setattr */
959 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000960 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961 0, /* tp_as_number */
962 0, /* tp_as_sequence */
963 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000964 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000965 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000966 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +0000968 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969 0, /* tp_as_buffer */
970 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
971 "The most base type", /* tp_doc */
972 0, /* tp_traverse */
973 0, /* tp_clear */
974 0, /* tp_richcompare */
975 0, /* tp_weaklistoffset */
976 0, /* tp_iter */
977 0, /* tp_iternext */
978 0, /* tp_methods */
979 object_members, /* tp_members */
980 0, /* tp_getset */
981 0, /* tp_base */
982 0, /* tp_dict */
983 0, /* tp_descr_get */
984 0, /* tp_descr_set */
985 0, /* tp_dictoffset */
986 object_init, /* tp_init */
987 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +0000988 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000989 object_free, /* tp_free */
990};
991
992
993/* Initialize the __dict__ in a type object */
994
995static int
996add_methods(PyTypeObject *type, PyMethodDef *meth)
997{
998 PyObject *dict = type->tp_defined;
999
1000 for (; meth->ml_name != NULL; meth++) {
1001 PyObject *descr;
1002 if (PyDict_GetItemString(dict, meth->ml_name))
1003 continue;
1004 descr = PyDescr_NewMethod(type, meth);
1005 if (descr == NULL)
1006 return -1;
1007 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1008 return -1;
1009 Py_DECREF(descr);
1010 }
1011 return 0;
1012}
1013
1014static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015add_members(PyTypeObject *type, struct memberlist *memb)
1016{
1017 PyObject *dict = type->tp_defined;
1018
1019 for (; memb->name != NULL; memb++) {
1020 PyObject *descr;
1021 if (PyDict_GetItemString(dict, memb->name))
1022 continue;
1023 descr = PyDescr_NewMember(type, memb);
1024 if (descr == NULL)
1025 return -1;
1026 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1027 return -1;
1028 Py_DECREF(descr);
1029 }
1030 return 0;
1031}
1032
1033static int
1034add_getset(PyTypeObject *type, struct getsetlist *gsp)
1035{
1036 PyObject *dict = type->tp_defined;
1037
1038 for (; gsp->name != NULL; gsp++) {
1039 PyObject *descr;
1040 if (PyDict_GetItemString(dict, gsp->name))
1041 continue;
1042 descr = PyDescr_NewGetSet(type, gsp);
1043
1044 if (descr == NULL)
1045 return -1;
1046 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1047 return -1;
1048 Py_DECREF(descr);
1049 }
1050 return 0;
1051}
1052
Guido van Rossum13d52f02001-08-10 21:24:08 +00001053static void
1054inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001055{
1056 int oldsize, newsize;
1057
Guido van Rossum13d52f02001-08-10 21:24:08 +00001058 /* Special flag magic */
1059 if (!type->tp_as_buffer && base->tp_as_buffer) {
1060 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1061 type->tp_flags |=
1062 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1063 }
1064 if (!type->tp_as_sequence && base->tp_as_sequence) {
1065 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1066 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1067 }
1068 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1069 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1070 if ((!type->tp_as_number && base->tp_as_number) ||
1071 (!type->tp_as_sequence && base->tp_as_sequence)) {
1072 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1073 if (!type->tp_as_number && !type->tp_as_sequence) {
1074 type->tp_flags |= base->tp_flags &
1075 Py_TPFLAGS_HAVE_INPLACEOPS;
1076 }
1077 }
1078 /* Wow */
1079 }
1080 if (!type->tp_as_number && base->tp_as_number) {
1081 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1082 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1083 }
1084
1085 /* Copying basicsize is connected to the GC flags */
1086 oldsize = PyType_BASICSIZE(base);
1087 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1088 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1089 (base->tp_flags & Py_TPFLAGS_GC) &&
1090 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1091 (!type->tp_traverse && !type->tp_clear)) {
1092 type->tp_flags |= Py_TPFLAGS_GC;
1093 if (type->tp_traverse == NULL)
1094 type->tp_traverse = base->tp_traverse;
1095 if (type->tp_clear == NULL)
1096 type->tp_clear = base->tp_clear;
1097 }
1098 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1099 if (base != &PyBaseObject_Type ||
1100 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1101 if (type->tp_new == NULL)
1102 type->tp_new = base->tp_new;
1103 }
1104 }
1105 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001106
1107 /* Copy other non-function slots */
1108
1109#undef COPYVAL
1110#define COPYVAL(SLOT) \
1111 if (type->SLOT == 0) type->SLOT = base->SLOT
1112
1113 COPYVAL(tp_itemsize);
1114 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1115 COPYVAL(tp_weaklistoffset);
1116 }
1117 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1118 COPYVAL(tp_dictoffset);
1119 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001120}
1121
1122static void
1123inherit_slots(PyTypeObject *type, PyTypeObject *base)
1124{
1125 PyTypeObject *basebase;
1126
1127#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001128#undef COPYSLOT
1129#undef COPYNUM
1130#undef COPYSEQ
1131#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001132
1133#define SLOTDEFINED(SLOT) \
1134 (base->SLOT != 0 && \
1135 (basebase == NULL || base->SLOT != basebase->SLOT))
1136
Tim Peters6d6c1a32001-08-02 04:15:00 +00001137#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001138 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139
1140#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1141#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1142#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1143
Guido van Rossum13d52f02001-08-10 21:24:08 +00001144 /* This won't inherit indirect slots (from tp_as_number etc.)
1145 if type doesn't provide the space. */
1146
1147 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1148 basebase = base->tp_base;
1149 if (basebase->tp_as_number == NULL)
1150 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001151 COPYNUM(nb_add);
1152 COPYNUM(nb_subtract);
1153 COPYNUM(nb_multiply);
1154 COPYNUM(nb_divide);
1155 COPYNUM(nb_remainder);
1156 COPYNUM(nb_divmod);
1157 COPYNUM(nb_power);
1158 COPYNUM(nb_negative);
1159 COPYNUM(nb_positive);
1160 COPYNUM(nb_absolute);
1161 COPYNUM(nb_nonzero);
1162 COPYNUM(nb_invert);
1163 COPYNUM(nb_lshift);
1164 COPYNUM(nb_rshift);
1165 COPYNUM(nb_and);
1166 COPYNUM(nb_xor);
1167 COPYNUM(nb_or);
1168 COPYNUM(nb_coerce);
1169 COPYNUM(nb_int);
1170 COPYNUM(nb_long);
1171 COPYNUM(nb_float);
1172 COPYNUM(nb_oct);
1173 COPYNUM(nb_hex);
1174 COPYNUM(nb_inplace_add);
1175 COPYNUM(nb_inplace_subtract);
1176 COPYNUM(nb_inplace_multiply);
1177 COPYNUM(nb_inplace_divide);
1178 COPYNUM(nb_inplace_remainder);
1179 COPYNUM(nb_inplace_power);
1180 COPYNUM(nb_inplace_lshift);
1181 COPYNUM(nb_inplace_rshift);
1182 COPYNUM(nb_inplace_and);
1183 COPYNUM(nb_inplace_xor);
1184 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001185 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1186 COPYNUM(nb_true_divide);
1187 COPYNUM(nb_floor_divide);
1188 COPYNUM(nb_inplace_true_divide);
1189 COPYNUM(nb_inplace_floor_divide);
1190 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001191 }
1192
Guido van Rossum13d52f02001-08-10 21:24:08 +00001193 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1194 basebase = base->tp_base;
1195 if (basebase->tp_as_sequence == NULL)
1196 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001197 COPYSEQ(sq_length);
1198 COPYSEQ(sq_concat);
1199 COPYSEQ(sq_repeat);
1200 COPYSEQ(sq_item);
1201 COPYSEQ(sq_slice);
1202 COPYSEQ(sq_ass_item);
1203 COPYSEQ(sq_ass_slice);
1204 COPYSEQ(sq_contains);
1205 COPYSEQ(sq_inplace_concat);
1206 COPYSEQ(sq_inplace_repeat);
1207 }
1208
Guido van Rossum13d52f02001-08-10 21:24:08 +00001209 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1210 basebase = base->tp_base;
1211 if (basebase->tp_as_mapping == NULL)
1212 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213 COPYMAP(mp_length);
1214 COPYMAP(mp_subscript);
1215 COPYMAP(mp_ass_subscript);
1216 }
1217
Guido van Rossum13d52f02001-08-10 21:24:08 +00001218 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001219
Tim Peters6d6c1a32001-08-02 04:15:00 +00001220 COPYSLOT(tp_dealloc);
1221 COPYSLOT(tp_print);
1222 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1223 type->tp_getattr = base->tp_getattr;
1224 type->tp_getattro = base->tp_getattro;
1225 }
1226 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1227 type->tp_setattr = base->tp_setattr;
1228 type->tp_setattro = base->tp_setattro;
1229 }
1230 /* tp_compare see tp_richcompare */
1231 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001232 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001233 COPYSLOT(tp_call);
1234 COPYSLOT(tp_str);
1235 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001236 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001237 if (type->tp_compare == NULL &&
1238 type->tp_richcompare == NULL &&
1239 type->tp_hash == NULL)
1240 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001241 type->tp_compare = base->tp_compare;
1242 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001243 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001244 }
1245 }
1246 else {
1247 COPYSLOT(tp_compare);
1248 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001249 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1250 COPYSLOT(tp_iter);
1251 COPYSLOT(tp_iternext);
1252 }
1253 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1254 COPYSLOT(tp_descr_get);
1255 COPYSLOT(tp_descr_set);
1256 COPYSLOT(tp_dictoffset);
1257 COPYSLOT(tp_init);
1258 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259 COPYSLOT(tp_free);
1260 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261}
1262
Guido van Rossum13d52f02001-08-10 21:24:08 +00001263staticforward int add_operators(PyTypeObject *);
1264
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001266PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001267{
1268 PyObject *dict, *bases, *x;
1269 PyTypeObject *base;
1270 int i, n;
1271
Guido van Rossumd614f972001-08-10 17:39:49 +00001272 if (type->tp_flags & Py_TPFLAGS_READY) {
1273 assert(type->tp_dict != NULL);
1274 return 0;
1275 }
1276 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1277 assert(type->tp_dict == NULL);
1278
1279 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280
1281 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1282 base = type->tp_base;
1283 if (base == NULL && type != &PyBaseObject_Type)
1284 base = type->tp_base = &PyBaseObject_Type;
1285
1286 /* Initialize tp_bases */
1287 bases = type->tp_bases;
1288 if (bases == NULL) {
1289 if (base == NULL)
1290 bases = PyTuple_New(0);
1291 else
1292 bases = Py_BuildValue("(O)", base);
1293 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001294 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295 type->tp_bases = bases;
1296 }
1297
1298 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001299 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001300 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001301 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001302 }
1303
1304 /* Initialize tp_defined */
1305 dict = type->tp_defined;
1306 if (dict == NULL) {
1307 dict = PyDict_New();
1308 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001309 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001310 type->tp_defined = dict;
1311 }
1312
1313 /* Add type-specific descriptors to tp_defined */
1314 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001315 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316 if (type->tp_methods != NULL) {
1317 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001318 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001319 }
1320 if (type->tp_members != NULL) {
1321 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001322 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001323 }
1324 if (type->tp_getset != NULL) {
1325 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001326 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001327 }
1328
1329 /* Temporarily make tp_dict the same object as tp_defined.
1330 (This is needed to call mro(), and can stay this way for
1331 dynamic types). */
1332 Py_INCREF(type->tp_defined);
1333 type->tp_dict = type->tp_defined;
1334
1335 /* Calculate method resolution order */
1336 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001337 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001338 }
1339
Guido van Rossum13d52f02001-08-10 21:24:08 +00001340 /* Inherit special flags from dominant base */
1341 if (type->tp_base != NULL)
1342 inherit_special(type, type->tp_base);
1343
Tim Peters6d6c1a32001-08-02 04:15:00 +00001344 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001345 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001346 /* For a dynamic type, all slots are overridden */
1347 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001348 }
1349 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001350 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001351 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001353 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001355 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356 bases = type->tp_mro;
1357 assert(bases != NULL);
1358 assert(PyTuple_Check(bases));
1359 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001360 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001361 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1362 assert(PyType_Check(base));
1363 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001364 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001365 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001366 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001367 }
1368 }
1369
Guido van Rossum13d52f02001-08-10 21:24:08 +00001370 /* Some more special stuff */
1371 base = type->tp_base;
1372 if (base != NULL) {
1373 if (type->tp_as_number == NULL)
1374 type->tp_as_number = base->tp_as_number;
1375 if (type->tp_as_sequence == NULL)
1376 type->tp_as_sequence = base->tp_as_sequence;
1377 if (type->tp_as_mapping == NULL)
1378 type->tp_as_mapping = base->tp_as_mapping;
1379 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001380
Guido van Rossum13d52f02001-08-10 21:24:08 +00001381 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001382 assert(type->tp_dict != NULL);
1383 type->tp_flags =
1384 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001385 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001386
1387 error:
1388 type->tp_flags &= ~Py_TPFLAGS_READYING;
1389 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001390}
1391
1392
1393/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1394
1395/* There's a wrapper *function* for each distinct function typedef used
1396 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1397 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1398 Most tables have only one entry; the tables for binary operators have two
1399 entries, one regular and one with reversed arguments. */
1400
1401static PyObject *
1402wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1403{
1404 inquiry func = (inquiry)wrapped;
1405 int res;
1406
1407 if (!PyArg_ParseTuple(args, ""))
1408 return NULL;
1409 res = (*func)(self);
1410 if (res == -1 && PyErr_Occurred())
1411 return NULL;
1412 return PyInt_FromLong((long)res);
1413}
1414
1415static struct wrapperbase tab_len[] = {
1416 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1417 {0}
1418};
1419
1420static PyObject *
1421wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1422{
1423 binaryfunc func = (binaryfunc)wrapped;
1424 PyObject *other;
1425
1426 if (!PyArg_ParseTuple(args, "O", &other))
1427 return NULL;
1428 return (*func)(self, other);
1429}
1430
1431static PyObject *
1432wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1433{
1434 binaryfunc func = (binaryfunc)wrapped;
1435 PyObject *other;
1436
1437 if (!PyArg_ParseTuple(args, "O", &other))
1438 return NULL;
1439 return (*func)(other, self);
1440}
1441
1442#undef BINARY
1443#define BINARY(NAME, OP) \
1444static struct wrapperbase tab_##NAME[] = { \
1445 {"__" #NAME "__", \
1446 (wrapperfunc)wrap_binaryfunc, \
1447 "x.__" #NAME "__(y) <==> " #OP}, \
1448 {"__r" #NAME "__", \
1449 (wrapperfunc)wrap_binaryfunc_r, \
1450 "y.__r" #NAME "__(x) <==> " #OP}, \
1451 {0} \
1452}
1453
1454BINARY(add, "x+y");
1455BINARY(sub, "x-y");
1456BINARY(mul, "x*y");
1457BINARY(div, "x/y");
1458BINARY(mod, "x%y");
1459BINARY(divmod, "divmod(x,y)");
1460BINARY(lshift, "x<<y");
1461BINARY(rshift, "x>>y");
1462BINARY(and, "x&y");
1463BINARY(xor, "x^y");
1464BINARY(or, "x|y");
1465
1466static PyObject *
1467wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1468{
1469 ternaryfunc func = (ternaryfunc)wrapped;
1470 PyObject *other;
1471 PyObject *third = Py_None;
1472
1473 /* Note: This wrapper only works for __pow__() */
1474
1475 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1476 return NULL;
1477 return (*func)(self, other, third);
1478}
1479
1480#undef TERNARY
1481#define TERNARY(NAME, OP) \
1482static struct wrapperbase tab_##NAME[] = { \
1483 {"__" #NAME "__", \
1484 (wrapperfunc)wrap_ternaryfunc, \
1485 "x.__" #NAME "__(y, z) <==> " #OP}, \
1486 {"__r" #NAME "__", \
1487 (wrapperfunc)wrap_ternaryfunc, \
1488 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1489 {0} \
1490}
1491
1492TERNARY(pow, "(x**y) % z");
1493
1494#undef UNARY
1495#define UNARY(NAME, OP) \
1496static struct wrapperbase tab_##NAME[] = { \
1497 {"__" #NAME "__", \
1498 (wrapperfunc)wrap_unaryfunc, \
1499 "x.__" #NAME "__() <==> " #OP}, \
1500 {0} \
1501}
1502
1503static PyObject *
1504wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1505{
1506 unaryfunc func = (unaryfunc)wrapped;
1507
1508 if (!PyArg_ParseTuple(args, ""))
1509 return NULL;
1510 return (*func)(self);
1511}
1512
1513UNARY(neg, "-x");
1514UNARY(pos, "+x");
1515UNARY(abs, "abs(x)");
1516UNARY(nonzero, "x != 0");
1517UNARY(invert, "~x");
1518UNARY(int, "int(x)");
1519UNARY(long, "long(x)");
1520UNARY(float, "float(x)");
1521UNARY(oct, "oct(x)");
1522UNARY(hex, "hex(x)");
1523
1524#undef IBINARY
1525#define IBINARY(NAME, OP) \
1526static struct wrapperbase tab_##NAME[] = { \
1527 {"__" #NAME "__", \
1528 (wrapperfunc)wrap_binaryfunc, \
1529 "x.__" #NAME "__(y) <==> " #OP}, \
1530 {0} \
1531}
1532
1533IBINARY(iadd, "x+=y");
1534IBINARY(isub, "x-=y");
1535IBINARY(imul, "x*=y");
1536IBINARY(idiv, "x/=y");
1537IBINARY(imod, "x%=y");
1538IBINARY(ilshift, "x<<=y");
1539IBINARY(irshift, "x>>=y");
1540IBINARY(iand, "x&=y");
1541IBINARY(ixor, "x^=y");
1542IBINARY(ior, "x|=y");
1543
1544#undef ITERNARY
1545#define ITERNARY(NAME, OP) \
1546static struct wrapperbase tab_##NAME[] = { \
1547 {"__" #NAME "__", \
1548 (wrapperfunc)wrap_ternaryfunc, \
1549 "x.__" #NAME "__(y) <==> " #OP}, \
1550 {0} \
1551}
1552
1553ITERNARY(ipow, "x = (x**y) % z");
1554
1555static struct wrapperbase tab_getitem[] = {
1556 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1557 "x.__getitem__(y) <==> x[y]"},
1558 {0}
1559};
1560
1561static PyObject *
1562wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1563{
1564 intargfunc func = (intargfunc)wrapped;
1565 int i;
1566
1567 if (!PyArg_ParseTuple(args, "i", &i))
1568 return NULL;
1569 return (*func)(self, i);
1570}
1571
1572static struct wrapperbase tab_mul_int[] = {
1573 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1574 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1575 {0}
1576};
1577
1578static struct wrapperbase tab_concat[] = {
1579 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1580 {0}
1581};
1582
1583static struct wrapperbase tab_imul_int[] = {
1584 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1585 {0}
1586};
1587
1588static struct wrapperbase tab_getitem_int[] = {
1589 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1590 "x.__getitem__(i) <==> x[i]"},
1591 {0}
1592};
1593
1594static PyObject *
1595wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1596{
1597 intintargfunc func = (intintargfunc)wrapped;
1598 int i, j;
1599
1600 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1601 return NULL;
1602 return (*func)(self, i, j);
1603}
1604
1605static struct wrapperbase tab_getslice[] = {
1606 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1607 "x.__getslice__(i, j) <==> x[i:j]"},
1608 {0}
1609};
1610
1611static PyObject *
1612wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1613{
1614 intobjargproc func = (intobjargproc)wrapped;
1615 int i, res;
1616 PyObject *value;
1617
1618 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1619 return NULL;
1620 res = (*func)(self, i, value);
1621 if (res == -1 && PyErr_Occurred())
1622 return NULL;
1623 Py_INCREF(Py_None);
1624 return Py_None;
1625}
1626
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001627static PyObject *
1628wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1629{
1630 intobjargproc func = (intobjargproc)wrapped;
1631 int i, res;
1632
1633 if (!PyArg_ParseTuple(args, "i", &i))
1634 return NULL;
1635 res = (*func)(self, i, NULL);
1636 if (res == -1 && PyErr_Occurred())
1637 return NULL;
1638 Py_INCREF(Py_None);
1639 return Py_None;
1640}
1641
Tim Peters6d6c1a32001-08-02 04:15:00 +00001642static struct wrapperbase tab_setitem_int[] = {
1643 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1644 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001645 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1646 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001647 {0}
1648};
1649
1650static PyObject *
1651wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1652{
1653 intintobjargproc func = (intintobjargproc)wrapped;
1654 int i, j, res;
1655 PyObject *value;
1656
1657 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1658 return NULL;
1659 res = (*func)(self, i, j, value);
1660 if (res == -1 && PyErr_Occurred())
1661 return NULL;
1662 Py_INCREF(Py_None);
1663 return Py_None;
1664}
1665
1666static struct wrapperbase tab_setslice[] = {
1667 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1668 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1669 {0}
1670};
1671
1672/* XXX objobjproc is a misnomer; should be objargpred */
1673static PyObject *
1674wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1675{
1676 objobjproc func = (objobjproc)wrapped;
1677 int res;
1678 PyObject *value;
1679
1680 if (!PyArg_ParseTuple(args, "O", &value))
1681 return NULL;
1682 res = (*func)(self, value);
1683 if (res == -1 && PyErr_Occurred())
1684 return NULL;
1685 return PyInt_FromLong((long)res);
1686}
1687
1688static struct wrapperbase tab_contains[] = {
1689 {"__contains__", (wrapperfunc)wrap_objobjproc,
1690 "x.__contains__(y) <==> y in x"},
1691 {0}
1692};
1693
1694static PyObject *
1695wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1696{
1697 objobjargproc func = (objobjargproc)wrapped;
1698 int res;
1699 PyObject *key, *value;
1700
1701 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1702 return NULL;
1703 res = (*func)(self, key, value);
1704 if (res == -1 && PyErr_Occurred())
1705 return NULL;
1706 Py_INCREF(Py_None);
1707 return Py_None;
1708}
1709
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001710static PyObject *
1711wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1712{
1713 objobjargproc func = (objobjargproc)wrapped;
1714 int res;
1715 PyObject *key;
1716
1717 if (!PyArg_ParseTuple(args, "O", &key))
1718 return NULL;
1719 res = (*func)(self, key, NULL);
1720 if (res == -1 && PyErr_Occurred())
1721 return NULL;
1722 Py_INCREF(Py_None);
1723 return Py_None;
1724}
1725
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726static struct wrapperbase tab_setitem[] = {
1727 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1728 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001729 {"__delitem__", (wrapperfunc)wrap_delitem,
1730 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001731 {0}
1732};
1733
1734static PyObject *
1735wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1736{
1737 cmpfunc func = (cmpfunc)wrapped;
1738 int res;
1739 PyObject *other;
1740
1741 if (!PyArg_ParseTuple(args, "O", &other))
1742 return NULL;
1743 res = (*func)(self, other);
1744 if (PyErr_Occurred())
1745 return NULL;
1746 return PyInt_FromLong((long)res);
1747}
1748
1749static struct wrapperbase tab_cmp[] = {
1750 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1751 "x.__cmp__(y) <==> cmp(x,y)"},
1752 {0}
1753};
1754
1755static struct wrapperbase tab_repr[] = {
1756 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1757 "x.__repr__() <==> repr(x)"},
1758 {0}
1759};
1760
1761static struct wrapperbase tab_getattr[] = {
1762 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1763 "x.__getattr__('name') <==> x.name"},
1764 {0}
1765};
1766
1767static PyObject *
1768wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1769{
1770 setattrofunc func = (setattrofunc)wrapped;
1771 int res;
1772 PyObject *name, *value;
1773
1774 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1775 return NULL;
1776 res = (*func)(self, name, value);
1777 if (res < 0)
1778 return NULL;
1779 Py_INCREF(Py_None);
1780 return Py_None;
1781}
1782
1783static PyObject *
1784wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1785{
1786 setattrofunc func = (setattrofunc)wrapped;
1787 int res;
1788 PyObject *name;
1789
1790 if (!PyArg_ParseTuple(args, "O", &name))
1791 return NULL;
1792 res = (*func)(self, name, NULL);
1793 if (res < 0)
1794 return NULL;
1795 Py_INCREF(Py_None);
1796 return Py_None;
1797}
1798
1799static struct wrapperbase tab_setattr[] = {
1800 {"__setattr__", (wrapperfunc)wrap_setattr,
1801 "x.__setattr__('name', value) <==> x.name = value"},
1802 {"__delattr__", (wrapperfunc)wrap_delattr,
1803 "x.__delattr__('name') <==> del x.name"},
1804 {0}
1805};
1806
1807static PyObject *
1808wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1809{
1810 hashfunc func = (hashfunc)wrapped;
1811 long res;
1812
1813 if (!PyArg_ParseTuple(args, ""))
1814 return NULL;
1815 res = (*func)(self);
1816 if (res == -1 && PyErr_Occurred())
1817 return NULL;
1818 return PyInt_FromLong(res);
1819}
1820
1821static struct wrapperbase tab_hash[] = {
1822 {"__hash__", (wrapperfunc)wrap_hashfunc,
1823 "x.__hash__() <==> hash(x)"},
1824 {0}
1825};
1826
1827static PyObject *
1828wrap_call(PyObject *self, PyObject *args, void *wrapped)
1829{
1830 ternaryfunc func = (ternaryfunc)wrapped;
1831
1832 /* XXX What about keyword arguments? */
1833 return (*func)(self, args, NULL);
1834}
1835
1836static struct wrapperbase tab_call[] = {
1837 {"__call__", (wrapperfunc)wrap_call,
1838 "x.__call__(...) <==> x(...)"},
1839 {0}
1840};
1841
1842static struct wrapperbase tab_str[] = {
1843 {"__str__", (wrapperfunc)wrap_unaryfunc,
1844 "x.__str__() <==> str(x)"},
1845 {0}
1846};
1847
1848static PyObject *
1849wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1850{
1851 richcmpfunc func = (richcmpfunc)wrapped;
1852 PyObject *other;
1853
1854 if (!PyArg_ParseTuple(args, "O", &other))
1855 return NULL;
1856 return (*func)(self, other, op);
1857}
1858
1859#undef RICHCMP_WRAPPER
1860#define RICHCMP_WRAPPER(NAME, OP) \
1861static PyObject * \
1862richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1863{ \
1864 return wrap_richcmpfunc(self, args, wrapped, OP); \
1865}
1866
Jack Jansen8e938b42001-08-08 15:29:49 +00001867RICHCMP_WRAPPER(lt, Py_LT)
1868RICHCMP_WRAPPER(le, Py_LE)
1869RICHCMP_WRAPPER(eq, Py_EQ)
1870RICHCMP_WRAPPER(ne, Py_NE)
1871RICHCMP_WRAPPER(gt, Py_GT)
1872RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001873
1874#undef RICHCMP_ENTRY
1875#define RICHCMP_ENTRY(NAME, EXPR) \
1876 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1877 "x.__" #NAME "__(y) <==> " EXPR}
1878
1879static struct wrapperbase tab_richcmp[] = {
1880 RICHCMP_ENTRY(lt, "x<y"),
1881 RICHCMP_ENTRY(le, "x<=y"),
1882 RICHCMP_ENTRY(eq, "x==y"),
1883 RICHCMP_ENTRY(ne, "x!=y"),
1884 RICHCMP_ENTRY(gt, "x>y"),
1885 RICHCMP_ENTRY(ge, "x>=y"),
1886 {0}
1887};
1888
1889static struct wrapperbase tab_iter[] = {
1890 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1891 {0}
1892};
1893
1894static PyObject *
1895wrap_next(PyObject *self, PyObject *args, void *wrapped)
1896{
1897 unaryfunc func = (unaryfunc)wrapped;
1898 PyObject *res;
1899
1900 if (!PyArg_ParseTuple(args, ""))
1901 return NULL;
1902 res = (*func)(self);
1903 if (res == NULL && !PyErr_Occurred())
1904 PyErr_SetNone(PyExc_StopIteration);
1905 return res;
1906}
1907
1908static struct wrapperbase tab_next[] = {
1909 {"next", (wrapperfunc)wrap_next,
1910 "x.next() -> the next value, or raise StopIteration"},
1911 {0}
1912};
1913
1914static PyObject *
1915wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1916{
1917 descrgetfunc func = (descrgetfunc)wrapped;
1918 PyObject *obj;
1919 PyObject *type = NULL;
1920
1921 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1922 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001923 return (*func)(self, obj, type);
1924}
1925
1926static struct wrapperbase tab_descr_get[] = {
1927 {"__get__", (wrapperfunc)wrap_descr_get,
1928 "descr.__get__(obj, type) -> value"},
1929 {0}
1930};
1931
1932static PyObject *
1933wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
1934{
1935 descrsetfunc func = (descrsetfunc)wrapped;
1936 PyObject *obj, *value;
1937 int ret;
1938
1939 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
1940 return NULL;
1941 ret = (*func)(self, obj, value);
1942 if (ret < 0)
1943 return NULL;
1944 Py_INCREF(Py_None);
1945 return Py_None;
1946}
1947
1948static struct wrapperbase tab_descr_set[] = {
1949 {"__set__", (wrapperfunc)wrap_descrsetfunc,
1950 "descr.__set__(obj, value)"},
1951 {0}
1952};
1953
1954static PyObject *
1955wrap_init(PyObject *self, PyObject *args, void *wrapped)
1956{
1957 initproc func = (initproc)wrapped;
1958
1959 /* XXX What about keyword arguments? */
1960 if (func(self, args, NULL) < 0)
1961 return NULL;
1962 Py_INCREF(Py_None);
1963 return Py_None;
1964}
1965
1966static struct wrapperbase tab_init[] = {
1967 {"__init__", (wrapperfunc)wrap_init,
1968 "x.__init__(...) initializes x; "
1969 "see x.__type__.__doc__ for signature"},
1970 {0}
1971};
1972
1973static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001974tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001975{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001976 PyTypeObject *type, *subtype;
1977 PyObject *arg0, *res;
1978
1979 if (self == NULL || !PyType_Check(self))
1980 Py_FatalError("__new__() called with non-type 'self'");
1981 type = (PyTypeObject *)self;
1982 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
1983 PyErr_SetString(PyExc_TypeError,
1984 "T.__new__(): not enough arguments");
1985 return NULL;
1986 }
1987 arg0 = PyTuple_GET_ITEM(args, 0);
1988 if (!PyType_Check(arg0)) {
1989 PyErr_SetString(PyExc_TypeError,
1990 "T.__new__(S): S is not a type object");
1991 return NULL;
1992 }
1993 subtype = (PyTypeObject *)arg0;
1994 if (!PyType_IsSubtype(subtype, type)) {
1995 PyErr_SetString(PyExc_TypeError,
1996 "T.__new__(S): S is not a subtype of T");
1997 return NULL;
1998 }
1999 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2000 if (args == NULL)
2001 return NULL;
2002 res = type->tp_new(subtype, args, kwds);
2003 Py_DECREF(args);
2004 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002005}
2006
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002007static struct PyMethodDef tp_new_methoddef[] = {
2008 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2009 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002010 {0}
2011};
2012
2013static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002014add_tp_new_wrapper(PyTypeObject *type)
2015{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002016 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002017
Guido van Rossumf040ede2001-08-07 16:40:56 +00002018 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2019 return 0;
2020 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002021 if (func == NULL)
2022 return -1;
2023 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2024}
2025
Guido van Rossum13d52f02001-08-10 21:24:08 +00002026static int
2027add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2028{
2029 PyObject *dict = type->tp_defined;
2030
2031 for (; wraps->name != NULL; wraps++) {
2032 PyObject *descr;
2033 if (PyDict_GetItemString(dict, wraps->name))
2034 continue;
2035 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2036 if (descr == NULL)
2037 return -1;
2038 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2039 return -1;
2040 Py_DECREF(descr);
2041 }
2042 return 0;
2043}
2044
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002045/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002046 dictionary with method descriptors for function slots. For each
2047 function slot (like tp_repr) that's defined in the type, one or
2048 more corresponding descriptors are added in the type's tp_defined
2049 dictionary under the appropriate name (like __repr__). Some
2050 function slots cause more than one descriptor to be added (for
2051 example, the nb_add slot adds both __add__ and __radd__
2052 descriptors) and some function slots compete for the same
2053 descriptor (for example both sq_item and mp_subscript generate a
2054 __getitem__ descriptor). This only adds new descriptors and
2055 doesn't overwrite entries in tp_defined that were previously
2056 defined. The descriptors contain a reference to the C function
2057 they must call, so that it's safe if they are copied into a
2058 subtype's __dict__ and the subtype has a different C function in
2059 its slot -- calling the method defined by the descriptor will call
2060 the C function that was used to create it, rather than the C
2061 function present in the slot when it is called. (This is important
2062 because a subtype may have a C function in the slot that calls the
2063 method from the dictionary, and we want to avoid infinite recursion
2064 here.) */
2065
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002066static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002067add_operators(PyTypeObject *type)
2068{
2069 PySequenceMethods *sq;
2070 PyMappingMethods *mp;
2071 PyNumberMethods *nb;
2072
2073#undef ADD
2074#define ADD(SLOT, TABLE) \
2075 if (SLOT) { \
2076 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2077 return -1; \
2078 }
2079
2080 if ((sq = type->tp_as_sequence) != NULL) {
2081 ADD(sq->sq_length, tab_len);
2082 ADD(sq->sq_concat, tab_concat);
2083 ADD(sq->sq_repeat, tab_mul_int);
2084 ADD(sq->sq_item, tab_getitem_int);
2085 ADD(sq->sq_slice, tab_getslice);
2086 ADD(sq->sq_ass_item, tab_setitem_int);
2087 ADD(sq->sq_ass_slice, tab_setslice);
2088 ADD(sq->sq_contains, tab_contains);
2089 ADD(sq->sq_inplace_concat, tab_iadd);
2090 ADD(sq->sq_inplace_repeat, tab_imul_int);
2091 }
2092
2093 if ((mp = type->tp_as_mapping) != NULL) {
2094 if (sq->sq_length == NULL)
2095 ADD(mp->mp_length, tab_len);
2096 ADD(mp->mp_subscript, tab_getitem);
2097 ADD(mp->mp_ass_subscript, tab_setitem);
2098 }
2099
2100 /* We don't support "old-style numbers" because their binary
2101 operators require that both arguments have the same type;
2102 the wrappers here only work for new-style numbers. */
2103 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2104 (nb = type->tp_as_number) != NULL) {
2105 ADD(nb->nb_add, tab_add);
2106 ADD(nb->nb_subtract, tab_sub);
2107 ADD(nb->nb_multiply, tab_mul);
2108 ADD(nb->nb_divide, tab_div);
2109 ADD(nb->nb_remainder, tab_mod);
2110 ADD(nb->nb_divmod, tab_divmod);
2111 ADD(nb->nb_power, tab_pow);
2112 ADD(nb->nb_negative, tab_neg);
2113 ADD(nb->nb_positive, tab_pos);
2114 ADD(nb->nb_absolute, tab_abs);
2115 ADD(nb->nb_nonzero, tab_nonzero);
2116 ADD(nb->nb_invert, tab_invert);
2117 ADD(nb->nb_lshift, tab_lshift);
2118 ADD(nb->nb_rshift, tab_rshift);
2119 ADD(nb->nb_and, tab_and);
2120 ADD(nb->nb_xor, tab_xor);
2121 ADD(nb->nb_or, tab_or);
2122 /* We don't support coerce() -- see above comment */
2123 ADD(nb->nb_int, tab_int);
2124 ADD(nb->nb_long, tab_long);
2125 ADD(nb->nb_float, tab_float);
2126 ADD(nb->nb_oct, tab_oct);
2127 ADD(nb->nb_hex, tab_hex);
2128 ADD(nb->nb_inplace_add, tab_iadd);
2129 ADD(nb->nb_inplace_subtract, tab_isub);
2130 ADD(nb->nb_inplace_multiply, tab_imul);
2131 ADD(nb->nb_inplace_divide, tab_idiv);
2132 ADD(nb->nb_inplace_remainder, tab_imod);
2133 ADD(nb->nb_inplace_power, tab_ipow);
2134 ADD(nb->nb_inplace_lshift, tab_ilshift);
2135 ADD(nb->nb_inplace_rshift, tab_irshift);
2136 ADD(nb->nb_inplace_and, tab_iand);
2137 ADD(nb->nb_inplace_xor, tab_ixor);
2138 ADD(nb->nb_inplace_or, tab_ior);
2139 }
2140
2141 ADD(type->tp_getattro, tab_getattr);
2142 ADD(type->tp_setattro, tab_setattr);
2143 ADD(type->tp_compare, tab_cmp);
2144 ADD(type->tp_repr, tab_repr);
2145 ADD(type->tp_hash, tab_hash);
2146 ADD(type->tp_call, tab_call);
2147 ADD(type->tp_str, tab_str);
2148 ADD(type->tp_richcompare, tab_richcmp);
2149 ADD(type->tp_iter, tab_iter);
2150 ADD(type->tp_iternext, tab_next);
2151 ADD(type->tp_descr_get, tab_descr_get);
2152 ADD(type->tp_descr_set, tab_descr_set);
2153 ADD(type->tp_init, tab_init);
2154
Guido van Rossumf040ede2001-08-07 16:40:56 +00002155 if (type->tp_new != NULL) {
2156 if (add_tp_new_wrapper(type) < 0)
2157 return -1;
2158 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002159
2160 return 0;
2161}
2162
Guido van Rossumf040ede2001-08-07 16:40:56 +00002163/* Slot wrappers that call the corresponding __foo__ slot. See comments
2164 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002165
Guido van Rossumdc91b992001-08-08 22:26:22 +00002166#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002167static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002168FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002169{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002170 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002171}
2172
Guido van Rossumdc91b992001-08-08 22:26:22 +00002173#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002174static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002175FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002177 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002178}
2179
Guido van Rossumdc91b992001-08-08 22:26:22 +00002180
2181#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002182static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002183FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002184{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002185 if (self->ob_type->tp_as_number != NULL && \
2186 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2187 PyObject *r; \
2188 r = PyObject_CallMethod( \
2189 self, OPSTR, "O", other); \
2190 if (r != Py_NotImplemented || \
2191 other->ob_type == self->ob_type) \
2192 return r; \
2193 Py_DECREF(r); \
2194 } \
2195 if (other->ob_type->tp_as_number != NULL && \
2196 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2197 return PyObject_CallMethod( \
2198 other, ROPSTR, "O", self); \
2199 } \
2200 Py_INCREF(Py_NotImplemented); \
2201 return Py_NotImplemented; \
2202}
2203
2204#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2205 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2206
2207#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2208static PyObject * \
2209FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2210{ \
2211 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212}
2213
2214static int
2215slot_sq_length(PyObject *self)
2216{
2217 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2218
2219 if (res == NULL)
2220 return -1;
2221 return (int)PyInt_AsLong(res);
2222}
2223
Guido van Rossumdc91b992001-08-08 22:26:22 +00002224SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2225SLOT1(slot_sq_repeat, "__mul__", int, "i")
2226SLOT1(slot_sq_item, "__getitem__", int, "i")
2227SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002228
2229static int
2230slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2231{
2232 PyObject *res;
2233
2234 if (value == NULL)
2235 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2236 else
2237 res = PyObject_CallMethod(self, "__setitem__",
2238 "iO", index, value);
2239 if (res == NULL)
2240 return -1;
2241 Py_DECREF(res);
2242 return 0;
2243}
2244
2245static int
2246slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2247{
2248 PyObject *res;
2249
2250 if (value == NULL)
2251 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2252 else
2253 res = PyObject_CallMethod(self, "__setslice__",
2254 "iiO", i, j, value);
2255 if (res == NULL)
2256 return -1;
2257 Py_DECREF(res);
2258 return 0;
2259}
2260
2261static int
2262slot_sq_contains(PyObject *self, PyObject *value)
2263{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002264 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002265
Guido van Rossumb8f63662001-08-15 23:57:02 +00002266 func = PyObject_GetAttrString(self, "__contains__");
2267
2268 if (func != NULL) {
2269 args = Py_BuildValue("(O)", value);
2270 if (args == NULL)
2271 res = NULL;
2272 else {
2273 res = PyEval_CallObject(func, args);
2274 Py_DECREF(args);
2275 }
2276 Py_DECREF(func);
2277 if (res == NULL)
2278 return -1;
2279 return PyObject_IsTrue(res);
2280 }
2281 else {
2282 PyErr_Clear();
2283 return _PySequence_IterContains(self, value);
2284 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002285}
2286
Guido van Rossumdc91b992001-08-08 22:26:22 +00002287SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2288SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002289
2290#define slot_mp_length slot_sq_length
2291
Guido van Rossumdc91b992001-08-08 22:26:22 +00002292SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002293
2294static int
2295slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2296{
2297 PyObject *res;
2298
2299 if (value == NULL)
2300 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2301 else
2302 res = PyObject_CallMethod(self, "__setitem__",
2303 "OO", key, value);
2304 if (res == NULL)
2305 return -1;
2306 Py_DECREF(res);
2307 return 0;
2308}
2309
Guido van Rossumdc91b992001-08-08 22:26:22 +00002310SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2311SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2312SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2313SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2314SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2315SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2316
2317staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2318
2319SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2320 nb_power, "__pow__", "__rpow__")
2321
2322static PyObject *
2323slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2324{
2325 if (modulus == Py_None)
2326 return slot_nb_power_binary(self, other);
2327 /* Three-arg power doesn't use __rpow__ */
2328 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2329}
2330
2331SLOT0(slot_nb_negative, "__neg__")
2332SLOT0(slot_nb_positive, "__pos__")
2333SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002334
2335static int
2336slot_nb_nonzero(PyObject *self)
2337{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002338 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002339
Guido van Rossumb8f63662001-08-15 23:57:02 +00002340 func = PyObject_GetAttrString(self, "__nonzero__");
2341 if (func == NULL) {
2342 PyErr_Clear();
2343 func = PyObject_GetAttrString(self, "__len__");
2344 }
2345
2346 if (func != NULL) {
2347 res = PyEval_CallObject(func, NULL);
2348 Py_DECREF(func);
2349 if (res == NULL)
2350 return -1;
2351 return PyObject_IsTrue(res);
2352 }
2353 else {
2354 PyErr_Clear();
2355 return 1;
2356 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002357}
2358
Guido van Rossumdc91b992001-08-08 22:26:22 +00002359SLOT0(slot_nb_invert, "__invert__")
2360SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2361SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2362SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2363SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2364SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002365/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002366SLOT0(slot_nb_int, "__int__")
2367SLOT0(slot_nb_long, "__long__")
2368SLOT0(slot_nb_float, "__float__")
2369SLOT0(slot_nb_oct, "__oct__")
2370SLOT0(slot_nb_hex, "__hex__")
2371SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2372SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2373SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2374SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2375SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2376SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2377SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2378SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2379SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2380SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2381SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2382SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2383 "__floordiv__", "__rfloordiv__")
2384SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2385SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2386SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002387
2388static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002389half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002390{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002391 PyObject *func, *args, *res;
2392 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002393
Guido van Rossumb8f63662001-08-15 23:57:02 +00002394 func = PyObject_GetAttrString(self, "__cmp__");
2395 if (func == NULL) {
2396 PyErr_Clear();
2397 }
2398 else {
2399 args = Py_BuildValue("(O)", other);
2400 if (args == NULL)
2401 res = NULL;
2402 else {
2403 res = PyObject_CallObject(func, args);
2404 Py_DECREF(args);
2405 }
2406 if (res != Py_NotImplemented) {
2407 if (res == NULL)
2408 return -2;
2409 c = PyInt_AsLong(res);
2410 Py_DECREF(res);
2411 if (c == -1 && PyErr_Occurred())
2412 return -2;
2413 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2414 }
2415 Py_DECREF(res);
2416 }
2417 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418}
2419
Guido van Rossumb8f63662001-08-15 23:57:02 +00002420static int
2421slot_tp_compare(PyObject *self, PyObject *other)
2422{
2423 int c;
2424
2425 if (self->ob_type->tp_compare == slot_tp_compare) {
2426 c = half_compare(self, other);
2427 if (c <= 1)
2428 return c;
2429 }
2430 if (other->ob_type->tp_compare == slot_tp_compare) {
2431 c = half_compare(other, self);
2432 if (c < -1)
2433 return -2;
2434 if (c <= 1)
2435 return -c;
2436 }
2437 return (void *)self < (void *)other ? -1 :
2438 (void *)self > (void *)other ? 1 : 0;
2439}
2440
2441static PyObject *
2442slot_tp_repr(PyObject *self)
2443{
2444 PyObject *func, *res;
2445
2446 func = PyObject_GetAttrString(self, "__repr__");
2447 if (func != NULL) {
2448 res = PyEval_CallObject(func, NULL);
2449 Py_DECREF(func);
2450 return res;
2451 }
2452 else {
2453 char buf[120];
2454 PyErr_Clear();
2455 sprintf(buf, "<%.80s object at %p>",
2456 self->ob_type->tp_name, self);
2457 return PyString_FromString(buf);
2458 }
2459}
2460
2461static PyObject *
2462slot_tp_str(PyObject *self)
2463{
2464 PyObject *func, *res;
2465
2466 func = PyObject_GetAttrString(self, "__str__");
2467 if (func != NULL) {
2468 res = PyEval_CallObject(func, NULL);
2469 Py_DECREF(func);
2470 return res;
2471 }
2472 else {
2473 PyErr_Clear();
2474 return slot_tp_repr(self);
2475 }
2476}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002477
2478static long
2479slot_tp_hash(PyObject *self)
2480{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002481 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002482 long h;
2483
Guido van Rossumb8f63662001-08-15 23:57:02 +00002484 func = PyObject_GetAttrString(self, "__hash__");
2485
2486 if (func != NULL) {
2487 res = PyEval_CallObject(func, NULL);
2488 Py_DECREF(func);
2489 if (res == NULL)
2490 return -1;
2491 h = PyInt_AsLong(res);
2492 }
2493 else {
2494 PyErr_Clear();
2495 func = PyObject_GetAttrString(self, "__eq__");
2496 if (func == NULL) {
2497 PyErr_Clear();
2498 func = PyObject_GetAttrString(self, "__cmp__");
2499 }
2500 if (func != NULL) {
2501 Py_DECREF(func);
2502 PyErr_SetString(PyExc_TypeError, "unhashable type");
2503 return -1;
2504 }
2505 PyErr_Clear();
2506 h = _Py_HashPointer((void *)self);
2507 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002508 if (h == -1 && !PyErr_Occurred())
2509 h = -2;
2510 return h;
2511}
2512
2513static PyObject *
2514slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2515{
2516 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2517 PyObject *res;
2518
2519 if (meth == NULL)
2520 return NULL;
2521 res = PyObject_Call(meth, args, kwds);
2522 Py_DECREF(meth);
2523 return res;
2524}
2525
Tim Peters6d6c1a32001-08-02 04:15:00 +00002526static PyObject *
2527slot_tp_getattro(PyObject *self, PyObject *name)
2528{
2529 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002530 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002531 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002532
Guido van Rossum8e248182001-08-12 05:17:56 +00002533 if (getattr_str == NULL) {
2534 getattr_str = PyString_InternFromString("__getattr__");
2535 if (getattr_str == NULL)
2536 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002537 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002538 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002539 if (getattr == NULL) {
2540 /* Avoid further slowdowns */
2541 if (tp->tp_getattro == slot_tp_getattro)
2542 tp->tp_getattro = PyObject_GenericGetAttr;
2543 else
2544 fprintf(stderr, "huh?\n");
Guido van Rossum8e248182001-08-12 05:17:56 +00002545 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002546 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002547 return PyObject_CallFunction(getattr, "OO", self, name);
2548}
2549
2550static int
2551slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2552{
2553 PyObject *res;
2554
2555 if (value == NULL)
2556 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2557 else
2558 res = PyObject_CallMethod(self, "__setattr__",
2559 "OO", name, value);
2560 if (res == NULL)
2561 return -1;
2562 Py_DECREF(res);
2563 return 0;
2564}
2565
2566/* Map rich comparison operators to their __xx__ namesakes */
2567static char *name_op[] = {
2568 "__lt__",
2569 "__le__",
2570 "__eq__",
2571 "__ne__",
2572 "__gt__",
2573 "__ge__",
2574};
2575
2576static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002577half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002578{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002579 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002580
Guido van Rossumb8f63662001-08-15 23:57:02 +00002581 func = PyObject_GetAttrString(self, name_op[op]);
2582 if (func == NULL) {
2583 PyErr_Clear();
2584 Py_INCREF(Py_NotImplemented);
2585 return Py_NotImplemented;
2586 }
2587 args = Py_BuildValue("(O)", other);
2588 if (args == NULL)
2589 res = NULL;
2590 else {
2591 res = PyObject_CallObject(func, args);
2592 Py_DECREF(args);
2593 }
2594 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002595 return res;
2596}
2597
Guido van Rossumb8f63662001-08-15 23:57:02 +00002598/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2599static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2600
2601static PyObject *
2602slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2603{
2604 PyObject *res;
2605
2606 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2607 res = half_richcompare(self, other, op);
2608 if (res != Py_NotImplemented)
2609 return res;
2610 Py_DECREF(res);
2611 }
2612 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2613 res = half_richcompare(other, self, swapped_op[op]);
2614 if (res != Py_NotImplemented) {
2615 return res;
2616 }
2617 Py_DECREF(res);
2618 }
2619 Py_INCREF(Py_NotImplemented);
2620 return Py_NotImplemented;
2621}
2622
2623static PyObject *
2624slot_tp_iter(PyObject *self)
2625{
2626 PyObject *func, *res;
2627
2628 func = PyObject_GetAttrString(self, "__iter__");
2629 if (func != NULL) {
2630 res = PyObject_CallObject(func, NULL);
2631 Py_DECREF(func);
2632 return res;
2633 }
2634 PyErr_Clear();
2635 func = PyObject_GetAttrString(self, "__getitem__");
2636 if (func == NULL) {
2637 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2638 return NULL;
2639 }
2640 Py_DECREF(func);
2641 return PySeqIter_New(self);
2642}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002643
2644static PyObject *
2645slot_tp_iternext(PyObject *self)
2646{
2647 return PyObject_CallMethod(self, "next", "");
2648}
2649
Guido van Rossumdc91b992001-08-08 22:26:22 +00002650SLOT2(slot_tp_descr_get, "__get__", PyObject *, PyObject *, "OO")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002651
2652static int
2653slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2654{
2655 PyObject *res = PyObject_CallMethod(self, "__set__",
2656 "OO", target, value);
2657 if (res == NULL)
2658 return -1;
2659 Py_DECREF(res);
2660 return 0;
2661}
2662
2663static int
2664slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2665{
2666 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2667 PyObject *res;
2668
2669 if (meth == NULL)
2670 return -1;
2671 res = PyObject_Call(meth, args, kwds);
2672 Py_DECREF(meth);
2673 if (res == NULL)
2674 return -1;
2675 Py_DECREF(res);
2676 return 0;
2677}
2678
2679static PyObject *
2680slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2681{
2682 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2683 PyObject *newargs, *x;
2684 int i, n;
2685
2686 if (func == NULL)
2687 return NULL;
2688 assert(PyTuple_Check(args));
2689 n = PyTuple_GET_SIZE(args);
2690 newargs = PyTuple_New(n+1);
2691 if (newargs == NULL)
2692 return NULL;
2693 Py_INCREF(type);
2694 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2695 for (i = 0; i < n; i++) {
2696 x = PyTuple_GET_ITEM(args, i);
2697 Py_INCREF(x);
2698 PyTuple_SET_ITEM(newargs, i+1, x);
2699 }
2700 x = PyObject_Call(func, newargs, kwds);
2701 Py_DECREF(func);
2702 return x;
2703}
2704
Guido van Rossumf040ede2001-08-07 16:40:56 +00002705/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002706 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002707 The dict argument is the dictionary argument passed to type_new(),
2708 which is the local namespace of the class statement, in other
2709 words, it contains the methods. For each special method (like
2710 __repr__) defined in the dictionary, the corresponding function
2711 slot in the type object (like tp_repr) is set to a special function
2712 whose name is 'slot_' followed by the slot name and whose signature
2713 is whatever is required for that slot. These slot functions look
2714 up the corresponding method in the type's dictionary and call it.
2715 The slot functions have to take care of the various peculiarities
2716 of the mapping between slots and special methods, such as mapping
2717 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2718 etc.) or mapping multiple slots to a single method (sq_item,
2719 mp_subscript <--> __getitem__). */
2720
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721static void
2722override_slots(PyTypeObject *type, PyObject *dict)
2723{
2724 PySequenceMethods *sq = type->tp_as_sequence;
2725 PyMappingMethods *mp = type->tp_as_mapping;
2726 PyNumberMethods *nb = type->tp_as_number;
2727
Guido van Rossumdc91b992001-08-08 22:26:22 +00002728#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002729 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002730 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002731 }
2732
Guido van Rossumdc91b992001-08-08 22:26:22 +00002733#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002734 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002735 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002736 }
2737
Guido van Rossumdc91b992001-08-08 22:26:22 +00002738#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002739 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002740 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002741 }
2742
Guido van Rossumdc91b992001-08-08 22:26:22 +00002743#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002744 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002745 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002746 }
2747
Guido van Rossumdc91b992001-08-08 22:26:22 +00002748 SQSLOT("__len__", sq_length, slot_sq_length);
2749 SQSLOT("__add__", sq_concat, slot_sq_concat);
2750 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2751 SQSLOT("__getitem__", sq_item, slot_sq_item);
2752 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2753 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2754 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2755 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2756 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2757 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2758 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2759 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002760
Guido van Rossumdc91b992001-08-08 22:26:22 +00002761 MPSLOT("__len__", mp_length, slot_mp_length);
2762 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2763 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2764 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002765
Guido van Rossumdc91b992001-08-08 22:26:22 +00002766 NBSLOT("__add__", nb_add, slot_nb_add);
2767 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2768 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2769 NBSLOT("__div__", nb_divide, slot_nb_divide);
2770 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2771 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2772 NBSLOT("__pow__", nb_power, slot_nb_power);
2773 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2774 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2775 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2776 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2777 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2778 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2779 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2780 NBSLOT("__and__", nb_and, slot_nb_and);
2781 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2782 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002783 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002784 NBSLOT("__int__", nb_int, slot_nb_int);
2785 NBSLOT("__long__", nb_long, slot_nb_long);
2786 NBSLOT("__float__", nb_float, slot_nb_float);
2787 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2788 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2789 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2790 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2791 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2792 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2793 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2794 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2795 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2796 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2797 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2798 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2799 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2800 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2801 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2802 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2803 slot_nb_inplace_floor_divide);
2804 NBSLOT("__itruediv__", nb_inplace_true_divide,
2805 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002806
Guido van Rossum8e248182001-08-12 05:17:56 +00002807 if (dict == NULL ||
2808 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002809 PyDict_GetItemString(dict, "__repr__"))
2810 type->tp_print = NULL;
2811
Guido van Rossumdc91b992001-08-08 22:26:22 +00002812 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2813 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2814 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2815 TPSLOT("__call__", tp_call, slot_tp_call);
2816 TPSLOT("__str__", tp_str, slot_tp_str);
2817 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2818 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2819 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2820 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2821 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2822 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2823 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2824 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2825 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2826 TPSLOT("next", tp_iternext, slot_tp_iternext);
2827 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2828 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2829 TPSLOT("__init__", tp_init, slot_tp_init);
2830 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002831}