blob: c38340e2b34d63ada61ba73b90549a3756f9b912 [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{
Guido van Rossum76e69632001-08-16 18:52:43 +0000915 PyTypeObject *type;
916 PyObject *mod, *name;
917 char buf[200];
Guido van Rossum8e248182001-08-12 05:17:56 +0000918
Guido van Rossum76e69632001-08-16 18:52:43 +0000919 type = self->ob_type;
920 mod = type_module(type, NULL);
921 if (mod == NULL)
922 PyErr_Clear();
923 else if (!PyString_Check(mod)) {
924 Py_DECREF(mod);
925 mod = NULL;
926 }
927 name = type_name(type, NULL);
928 if (name == NULL)
929 return NULL;
930 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
931 sprintf(buf, "<%.80s.%.80s instance at %p>",
932 PyString_AS_STRING(mod),
933 PyString_AS_STRING(name),
934 self);
935 else
936 sprintf(buf, "<%.80s instance at %p>", type->tp_name, self);
937 Py_XDECREF(mod);
938 Py_DECREF(name);
Guido van Rossum8e248182001-08-12 05:17:56 +0000939 return PyString_FromString(buf);
940}
941
Guido van Rossumb8f63662001-08-15 23:57:02 +0000942static PyObject *
943object_str(PyObject *self)
944{
945 unaryfunc f;
946
947 f = self->ob_type->tp_repr;
948 if (f == NULL)
949 f = object_repr;
950 return f(self);
951}
952
Guido van Rossum8e248182001-08-12 05:17:56 +0000953static long
954object_hash(PyObject *self)
955{
956 return _Py_HashPointer(self);
957}
Guido van Rossum8e248182001-08-12 05:17:56 +0000958
Tim Peters6d6c1a32001-08-02 04:15:00 +0000959static void
960object_free(PyObject *self)
961{
962 PyObject_Del(self);
963}
964
965static struct memberlist object_members[] = {
966 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
967 {0}
968};
969
970PyTypeObject PyBaseObject_Type = {
971 PyObject_HEAD_INIT(&PyType_Type)
972 0, /* ob_size */
973 "object", /* tp_name */
974 sizeof(PyObject), /* tp_basicsize */
975 0, /* tp_itemsize */
976 (destructor)object_dealloc, /* tp_dealloc */
977 0, /* tp_print */
978 0, /* tp_getattr */
979 0, /* tp_setattr */
980 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000981 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000982 0, /* tp_as_number */
983 0, /* tp_as_sequence */
984 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000985 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000987 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000988 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +0000989 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 0, /* tp_as_buffer */
991 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
992 "The most base type", /* tp_doc */
993 0, /* tp_traverse */
994 0, /* tp_clear */
995 0, /* tp_richcompare */
996 0, /* tp_weaklistoffset */
997 0, /* tp_iter */
998 0, /* tp_iternext */
999 0, /* tp_methods */
1000 object_members, /* tp_members */
1001 0, /* tp_getset */
1002 0, /* tp_base */
1003 0, /* tp_dict */
1004 0, /* tp_descr_get */
1005 0, /* tp_descr_set */
1006 0, /* tp_dictoffset */
1007 object_init, /* tp_init */
1008 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001009 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001010 object_free, /* tp_free */
1011};
1012
1013
1014/* Initialize the __dict__ in a type object */
1015
1016static int
1017add_methods(PyTypeObject *type, PyMethodDef *meth)
1018{
1019 PyObject *dict = type->tp_defined;
1020
1021 for (; meth->ml_name != NULL; meth++) {
1022 PyObject *descr;
1023 if (PyDict_GetItemString(dict, meth->ml_name))
1024 continue;
1025 descr = PyDescr_NewMethod(type, meth);
1026 if (descr == NULL)
1027 return -1;
1028 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1029 return -1;
1030 Py_DECREF(descr);
1031 }
1032 return 0;
1033}
1034
1035static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001036add_members(PyTypeObject *type, struct memberlist *memb)
1037{
1038 PyObject *dict = type->tp_defined;
1039
1040 for (; memb->name != NULL; memb++) {
1041 PyObject *descr;
1042 if (PyDict_GetItemString(dict, memb->name))
1043 continue;
1044 descr = PyDescr_NewMember(type, memb);
1045 if (descr == NULL)
1046 return -1;
1047 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1048 return -1;
1049 Py_DECREF(descr);
1050 }
1051 return 0;
1052}
1053
1054static int
1055add_getset(PyTypeObject *type, struct getsetlist *gsp)
1056{
1057 PyObject *dict = type->tp_defined;
1058
1059 for (; gsp->name != NULL; gsp++) {
1060 PyObject *descr;
1061 if (PyDict_GetItemString(dict, gsp->name))
1062 continue;
1063 descr = PyDescr_NewGetSet(type, gsp);
1064
1065 if (descr == NULL)
1066 return -1;
1067 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1068 return -1;
1069 Py_DECREF(descr);
1070 }
1071 return 0;
1072}
1073
Guido van Rossum13d52f02001-08-10 21:24:08 +00001074static void
1075inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076{
1077 int oldsize, newsize;
1078
Guido van Rossum13d52f02001-08-10 21:24:08 +00001079 /* Special flag magic */
1080 if (!type->tp_as_buffer && base->tp_as_buffer) {
1081 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1082 type->tp_flags |=
1083 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1084 }
1085 if (!type->tp_as_sequence && base->tp_as_sequence) {
1086 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1087 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1088 }
1089 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1090 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1091 if ((!type->tp_as_number && base->tp_as_number) ||
1092 (!type->tp_as_sequence && base->tp_as_sequence)) {
1093 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1094 if (!type->tp_as_number && !type->tp_as_sequence) {
1095 type->tp_flags |= base->tp_flags &
1096 Py_TPFLAGS_HAVE_INPLACEOPS;
1097 }
1098 }
1099 /* Wow */
1100 }
1101 if (!type->tp_as_number && base->tp_as_number) {
1102 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1103 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1104 }
1105
1106 /* Copying basicsize is connected to the GC flags */
1107 oldsize = PyType_BASICSIZE(base);
1108 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1109 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1110 (base->tp_flags & Py_TPFLAGS_GC) &&
1111 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1112 (!type->tp_traverse && !type->tp_clear)) {
1113 type->tp_flags |= Py_TPFLAGS_GC;
1114 if (type->tp_traverse == NULL)
1115 type->tp_traverse = base->tp_traverse;
1116 if (type->tp_clear == NULL)
1117 type->tp_clear = base->tp_clear;
1118 }
1119 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1120 if (base != &PyBaseObject_Type ||
1121 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1122 if (type->tp_new == NULL)
1123 type->tp_new = base->tp_new;
1124 }
1125 }
1126 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001127
1128 /* Copy other non-function slots */
1129
1130#undef COPYVAL
1131#define COPYVAL(SLOT) \
1132 if (type->SLOT == 0) type->SLOT = base->SLOT
1133
1134 COPYVAL(tp_itemsize);
1135 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1136 COPYVAL(tp_weaklistoffset);
1137 }
1138 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1139 COPYVAL(tp_dictoffset);
1140 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001141}
1142
1143static void
1144inherit_slots(PyTypeObject *type, PyTypeObject *base)
1145{
1146 PyTypeObject *basebase;
1147
1148#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001149#undef COPYSLOT
1150#undef COPYNUM
1151#undef COPYSEQ
1152#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001153
1154#define SLOTDEFINED(SLOT) \
1155 (base->SLOT != 0 && \
1156 (basebase == NULL || base->SLOT != basebase->SLOT))
1157
Tim Peters6d6c1a32001-08-02 04:15:00 +00001158#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001159 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001160
1161#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1162#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1163#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1164
Guido van Rossum13d52f02001-08-10 21:24:08 +00001165 /* This won't inherit indirect slots (from tp_as_number etc.)
1166 if type doesn't provide the space. */
1167
1168 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1169 basebase = base->tp_base;
1170 if (basebase->tp_as_number == NULL)
1171 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001172 COPYNUM(nb_add);
1173 COPYNUM(nb_subtract);
1174 COPYNUM(nb_multiply);
1175 COPYNUM(nb_divide);
1176 COPYNUM(nb_remainder);
1177 COPYNUM(nb_divmod);
1178 COPYNUM(nb_power);
1179 COPYNUM(nb_negative);
1180 COPYNUM(nb_positive);
1181 COPYNUM(nb_absolute);
1182 COPYNUM(nb_nonzero);
1183 COPYNUM(nb_invert);
1184 COPYNUM(nb_lshift);
1185 COPYNUM(nb_rshift);
1186 COPYNUM(nb_and);
1187 COPYNUM(nb_xor);
1188 COPYNUM(nb_or);
1189 COPYNUM(nb_coerce);
1190 COPYNUM(nb_int);
1191 COPYNUM(nb_long);
1192 COPYNUM(nb_float);
1193 COPYNUM(nb_oct);
1194 COPYNUM(nb_hex);
1195 COPYNUM(nb_inplace_add);
1196 COPYNUM(nb_inplace_subtract);
1197 COPYNUM(nb_inplace_multiply);
1198 COPYNUM(nb_inplace_divide);
1199 COPYNUM(nb_inplace_remainder);
1200 COPYNUM(nb_inplace_power);
1201 COPYNUM(nb_inplace_lshift);
1202 COPYNUM(nb_inplace_rshift);
1203 COPYNUM(nb_inplace_and);
1204 COPYNUM(nb_inplace_xor);
1205 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001206 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1207 COPYNUM(nb_true_divide);
1208 COPYNUM(nb_floor_divide);
1209 COPYNUM(nb_inplace_true_divide);
1210 COPYNUM(nb_inplace_floor_divide);
1211 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001212 }
1213
Guido van Rossum13d52f02001-08-10 21:24:08 +00001214 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1215 basebase = base->tp_base;
1216 if (basebase->tp_as_sequence == NULL)
1217 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001218 COPYSEQ(sq_length);
1219 COPYSEQ(sq_concat);
1220 COPYSEQ(sq_repeat);
1221 COPYSEQ(sq_item);
1222 COPYSEQ(sq_slice);
1223 COPYSEQ(sq_ass_item);
1224 COPYSEQ(sq_ass_slice);
1225 COPYSEQ(sq_contains);
1226 COPYSEQ(sq_inplace_concat);
1227 COPYSEQ(sq_inplace_repeat);
1228 }
1229
Guido van Rossum13d52f02001-08-10 21:24:08 +00001230 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1231 basebase = base->tp_base;
1232 if (basebase->tp_as_mapping == NULL)
1233 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001234 COPYMAP(mp_length);
1235 COPYMAP(mp_subscript);
1236 COPYMAP(mp_ass_subscript);
1237 }
1238
Guido van Rossum13d52f02001-08-10 21:24:08 +00001239 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001240
Tim Peters6d6c1a32001-08-02 04:15:00 +00001241 COPYSLOT(tp_dealloc);
1242 COPYSLOT(tp_print);
1243 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1244 type->tp_getattr = base->tp_getattr;
1245 type->tp_getattro = base->tp_getattro;
1246 }
1247 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1248 type->tp_setattr = base->tp_setattr;
1249 type->tp_setattro = base->tp_setattro;
1250 }
1251 /* tp_compare see tp_richcompare */
1252 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001253 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001254 COPYSLOT(tp_call);
1255 COPYSLOT(tp_str);
1256 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001257 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001258 if (type->tp_compare == NULL &&
1259 type->tp_richcompare == NULL &&
1260 type->tp_hash == NULL)
1261 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001262 type->tp_compare = base->tp_compare;
1263 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001264 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265 }
1266 }
1267 else {
1268 COPYSLOT(tp_compare);
1269 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1271 COPYSLOT(tp_iter);
1272 COPYSLOT(tp_iternext);
1273 }
1274 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1275 COPYSLOT(tp_descr_get);
1276 COPYSLOT(tp_descr_set);
1277 COPYSLOT(tp_dictoffset);
1278 COPYSLOT(tp_init);
1279 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280 COPYSLOT(tp_free);
1281 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001282}
1283
Guido van Rossum13d52f02001-08-10 21:24:08 +00001284staticforward int add_operators(PyTypeObject *);
1285
Tim Peters6d6c1a32001-08-02 04:15:00 +00001286int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001287PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001288{
1289 PyObject *dict, *bases, *x;
1290 PyTypeObject *base;
1291 int i, n;
1292
Guido van Rossumd614f972001-08-10 17:39:49 +00001293 if (type->tp_flags & Py_TPFLAGS_READY) {
1294 assert(type->tp_dict != NULL);
1295 return 0;
1296 }
1297 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1298 assert(type->tp_dict == NULL);
1299
1300 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301
1302 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1303 base = type->tp_base;
1304 if (base == NULL && type != &PyBaseObject_Type)
1305 base = type->tp_base = &PyBaseObject_Type;
1306
1307 /* Initialize tp_bases */
1308 bases = type->tp_bases;
1309 if (bases == NULL) {
1310 if (base == NULL)
1311 bases = PyTuple_New(0);
1312 else
1313 bases = Py_BuildValue("(O)", base);
1314 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001315 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316 type->tp_bases = bases;
1317 }
1318
1319 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001320 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001321 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001322 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001323 }
1324
1325 /* Initialize tp_defined */
1326 dict = type->tp_defined;
1327 if (dict == NULL) {
1328 dict = PyDict_New();
1329 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001330 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001331 type->tp_defined = dict;
1332 }
1333
1334 /* Add type-specific descriptors to tp_defined */
1335 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001336 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001337 if (type->tp_methods != NULL) {
1338 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001339 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001340 }
1341 if (type->tp_members != NULL) {
1342 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001343 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001344 }
1345 if (type->tp_getset != NULL) {
1346 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001347 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001348 }
1349
1350 /* Temporarily make tp_dict the same object as tp_defined.
1351 (This is needed to call mro(), and can stay this way for
1352 dynamic types). */
1353 Py_INCREF(type->tp_defined);
1354 type->tp_dict = type->tp_defined;
1355
1356 /* Calculate method resolution order */
1357 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001358 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001359 }
1360
Guido van Rossum13d52f02001-08-10 21:24:08 +00001361 /* Inherit special flags from dominant base */
1362 if (type->tp_base != NULL)
1363 inherit_special(type, type->tp_base);
1364
Tim Peters6d6c1a32001-08-02 04:15:00 +00001365 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001366 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001367 /* For a dynamic type, all slots are overridden */
1368 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001369 }
1370 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001371 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001372 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001373 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001374 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001375 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001376 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001377 bases = type->tp_mro;
1378 assert(bases != NULL);
1379 assert(PyTuple_Check(bases));
1380 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001381 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001382 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1383 assert(PyType_Check(base));
1384 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001385 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001386 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001387 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001388 }
1389 }
1390
Guido van Rossum13d52f02001-08-10 21:24:08 +00001391 /* Some more special stuff */
1392 base = type->tp_base;
1393 if (base != NULL) {
1394 if (type->tp_as_number == NULL)
1395 type->tp_as_number = base->tp_as_number;
1396 if (type->tp_as_sequence == NULL)
1397 type->tp_as_sequence = base->tp_as_sequence;
1398 if (type->tp_as_mapping == NULL)
1399 type->tp_as_mapping = base->tp_as_mapping;
1400 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001401
Guido van Rossum13d52f02001-08-10 21:24:08 +00001402 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001403 assert(type->tp_dict != NULL);
1404 type->tp_flags =
1405 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001406 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001407
1408 error:
1409 type->tp_flags &= ~Py_TPFLAGS_READYING;
1410 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001411}
1412
1413
1414/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1415
1416/* There's a wrapper *function* for each distinct function typedef used
1417 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1418 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1419 Most tables have only one entry; the tables for binary operators have two
1420 entries, one regular and one with reversed arguments. */
1421
1422static PyObject *
1423wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1424{
1425 inquiry func = (inquiry)wrapped;
1426 int res;
1427
1428 if (!PyArg_ParseTuple(args, ""))
1429 return NULL;
1430 res = (*func)(self);
1431 if (res == -1 && PyErr_Occurred())
1432 return NULL;
1433 return PyInt_FromLong((long)res);
1434}
1435
1436static struct wrapperbase tab_len[] = {
1437 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1438 {0}
1439};
1440
1441static PyObject *
1442wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1443{
1444 binaryfunc func = (binaryfunc)wrapped;
1445 PyObject *other;
1446
1447 if (!PyArg_ParseTuple(args, "O", &other))
1448 return NULL;
1449 return (*func)(self, other);
1450}
1451
1452static PyObject *
1453wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1454{
1455 binaryfunc func = (binaryfunc)wrapped;
1456 PyObject *other;
1457
1458 if (!PyArg_ParseTuple(args, "O", &other))
1459 return NULL;
1460 return (*func)(other, self);
1461}
1462
1463#undef BINARY
1464#define BINARY(NAME, OP) \
1465static struct wrapperbase tab_##NAME[] = { \
1466 {"__" #NAME "__", \
1467 (wrapperfunc)wrap_binaryfunc, \
1468 "x.__" #NAME "__(y) <==> " #OP}, \
1469 {"__r" #NAME "__", \
1470 (wrapperfunc)wrap_binaryfunc_r, \
1471 "y.__r" #NAME "__(x) <==> " #OP}, \
1472 {0} \
1473}
1474
1475BINARY(add, "x+y");
1476BINARY(sub, "x-y");
1477BINARY(mul, "x*y");
1478BINARY(div, "x/y");
1479BINARY(mod, "x%y");
1480BINARY(divmod, "divmod(x,y)");
1481BINARY(lshift, "x<<y");
1482BINARY(rshift, "x>>y");
1483BINARY(and, "x&y");
1484BINARY(xor, "x^y");
1485BINARY(or, "x|y");
1486
1487static PyObject *
1488wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1489{
1490 ternaryfunc func = (ternaryfunc)wrapped;
1491 PyObject *other;
1492 PyObject *third = Py_None;
1493
1494 /* Note: This wrapper only works for __pow__() */
1495
1496 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1497 return NULL;
1498 return (*func)(self, other, third);
1499}
1500
1501#undef TERNARY
1502#define TERNARY(NAME, OP) \
1503static struct wrapperbase tab_##NAME[] = { \
1504 {"__" #NAME "__", \
1505 (wrapperfunc)wrap_ternaryfunc, \
1506 "x.__" #NAME "__(y, z) <==> " #OP}, \
1507 {"__r" #NAME "__", \
1508 (wrapperfunc)wrap_ternaryfunc, \
1509 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1510 {0} \
1511}
1512
1513TERNARY(pow, "(x**y) % z");
1514
1515#undef UNARY
1516#define UNARY(NAME, OP) \
1517static struct wrapperbase tab_##NAME[] = { \
1518 {"__" #NAME "__", \
1519 (wrapperfunc)wrap_unaryfunc, \
1520 "x.__" #NAME "__() <==> " #OP}, \
1521 {0} \
1522}
1523
1524static PyObject *
1525wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1526{
1527 unaryfunc func = (unaryfunc)wrapped;
1528
1529 if (!PyArg_ParseTuple(args, ""))
1530 return NULL;
1531 return (*func)(self);
1532}
1533
1534UNARY(neg, "-x");
1535UNARY(pos, "+x");
1536UNARY(abs, "abs(x)");
1537UNARY(nonzero, "x != 0");
1538UNARY(invert, "~x");
1539UNARY(int, "int(x)");
1540UNARY(long, "long(x)");
1541UNARY(float, "float(x)");
1542UNARY(oct, "oct(x)");
1543UNARY(hex, "hex(x)");
1544
1545#undef IBINARY
1546#define IBINARY(NAME, OP) \
1547static struct wrapperbase tab_##NAME[] = { \
1548 {"__" #NAME "__", \
1549 (wrapperfunc)wrap_binaryfunc, \
1550 "x.__" #NAME "__(y) <==> " #OP}, \
1551 {0} \
1552}
1553
1554IBINARY(iadd, "x+=y");
1555IBINARY(isub, "x-=y");
1556IBINARY(imul, "x*=y");
1557IBINARY(idiv, "x/=y");
1558IBINARY(imod, "x%=y");
1559IBINARY(ilshift, "x<<=y");
1560IBINARY(irshift, "x>>=y");
1561IBINARY(iand, "x&=y");
1562IBINARY(ixor, "x^=y");
1563IBINARY(ior, "x|=y");
1564
1565#undef ITERNARY
1566#define ITERNARY(NAME, OP) \
1567static struct wrapperbase tab_##NAME[] = { \
1568 {"__" #NAME "__", \
1569 (wrapperfunc)wrap_ternaryfunc, \
1570 "x.__" #NAME "__(y) <==> " #OP}, \
1571 {0} \
1572}
1573
1574ITERNARY(ipow, "x = (x**y) % z");
1575
1576static struct wrapperbase tab_getitem[] = {
1577 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1578 "x.__getitem__(y) <==> x[y]"},
1579 {0}
1580};
1581
1582static PyObject *
1583wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1584{
1585 intargfunc func = (intargfunc)wrapped;
1586 int i;
1587
1588 if (!PyArg_ParseTuple(args, "i", &i))
1589 return NULL;
1590 return (*func)(self, i);
1591}
1592
1593static struct wrapperbase tab_mul_int[] = {
1594 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1595 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1596 {0}
1597};
1598
1599static struct wrapperbase tab_concat[] = {
1600 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1601 {0}
1602};
1603
1604static struct wrapperbase tab_imul_int[] = {
1605 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1606 {0}
1607};
1608
1609static struct wrapperbase tab_getitem_int[] = {
1610 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1611 "x.__getitem__(i) <==> x[i]"},
1612 {0}
1613};
1614
1615static PyObject *
1616wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1617{
1618 intintargfunc func = (intintargfunc)wrapped;
1619 int i, j;
1620
1621 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1622 return NULL;
1623 return (*func)(self, i, j);
1624}
1625
1626static struct wrapperbase tab_getslice[] = {
1627 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1628 "x.__getslice__(i, j) <==> x[i:j]"},
1629 {0}
1630};
1631
1632static PyObject *
1633wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1634{
1635 intobjargproc func = (intobjargproc)wrapped;
1636 int i, res;
1637 PyObject *value;
1638
1639 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1640 return NULL;
1641 res = (*func)(self, i, value);
1642 if (res == -1 && PyErr_Occurred())
1643 return NULL;
1644 Py_INCREF(Py_None);
1645 return Py_None;
1646}
1647
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001648static PyObject *
1649wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1650{
1651 intobjargproc func = (intobjargproc)wrapped;
1652 int i, res;
1653
1654 if (!PyArg_ParseTuple(args, "i", &i))
1655 return NULL;
1656 res = (*func)(self, i, NULL);
1657 if (res == -1 && PyErr_Occurred())
1658 return NULL;
1659 Py_INCREF(Py_None);
1660 return Py_None;
1661}
1662
Tim Peters6d6c1a32001-08-02 04:15:00 +00001663static struct wrapperbase tab_setitem_int[] = {
1664 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1665 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001666 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1667 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001668 {0}
1669};
1670
1671static PyObject *
1672wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1673{
1674 intintobjargproc func = (intintobjargproc)wrapped;
1675 int i, j, res;
1676 PyObject *value;
1677
1678 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1679 return NULL;
1680 res = (*func)(self, i, j, value);
1681 if (res == -1 && PyErr_Occurred())
1682 return NULL;
1683 Py_INCREF(Py_None);
1684 return Py_None;
1685}
1686
1687static struct wrapperbase tab_setslice[] = {
1688 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1689 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1690 {0}
1691};
1692
1693/* XXX objobjproc is a misnomer; should be objargpred */
1694static PyObject *
1695wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1696{
1697 objobjproc func = (objobjproc)wrapped;
1698 int res;
1699 PyObject *value;
1700
1701 if (!PyArg_ParseTuple(args, "O", &value))
1702 return NULL;
1703 res = (*func)(self, value);
1704 if (res == -1 && PyErr_Occurred())
1705 return NULL;
1706 return PyInt_FromLong((long)res);
1707}
1708
1709static struct wrapperbase tab_contains[] = {
1710 {"__contains__", (wrapperfunc)wrap_objobjproc,
1711 "x.__contains__(y) <==> y in x"},
1712 {0}
1713};
1714
1715static PyObject *
1716wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1717{
1718 objobjargproc func = (objobjargproc)wrapped;
1719 int res;
1720 PyObject *key, *value;
1721
1722 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1723 return NULL;
1724 res = (*func)(self, key, value);
1725 if (res == -1 && PyErr_Occurred())
1726 return NULL;
1727 Py_INCREF(Py_None);
1728 return Py_None;
1729}
1730
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001731static PyObject *
1732wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1733{
1734 objobjargproc func = (objobjargproc)wrapped;
1735 int res;
1736 PyObject *key;
1737
1738 if (!PyArg_ParseTuple(args, "O", &key))
1739 return NULL;
1740 res = (*func)(self, key, NULL);
1741 if (res == -1 && PyErr_Occurred())
1742 return NULL;
1743 Py_INCREF(Py_None);
1744 return Py_None;
1745}
1746
Tim Peters6d6c1a32001-08-02 04:15:00 +00001747static struct wrapperbase tab_setitem[] = {
1748 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1749 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001750 {"__delitem__", (wrapperfunc)wrap_delitem,
1751 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001752 {0}
1753};
1754
1755static PyObject *
1756wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1757{
1758 cmpfunc func = (cmpfunc)wrapped;
1759 int res;
1760 PyObject *other;
1761
1762 if (!PyArg_ParseTuple(args, "O", &other))
1763 return NULL;
1764 res = (*func)(self, other);
1765 if (PyErr_Occurred())
1766 return NULL;
1767 return PyInt_FromLong((long)res);
1768}
1769
1770static struct wrapperbase tab_cmp[] = {
1771 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1772 "x.__cmp__(y) <==> cmp(x,y)"},
1773 {0}
1774};
1775
1776static struct wrapperbase tab_repr[] = {
1777 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1778 "x.__repr__() <==> repr(x)"},
1779 {0}
1780};
1781
1782static struct wrapperbase tab_getattr[] = {
1783 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1784 "x.__getattr__('name') <==> x.name"},
1785 {0}
1786};
1787
1788static PyObject *
1789wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1790{
1791 setattrofunc func = (setattrofunc)wrapped;
1792 int res;
1793 PyObject *name, *value;
1794
1795 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1796 return NULL;
1797 res = (*func)(self, name, value);
1798 if (res < 0)
1799 return NULL;
1800 Py_INCREF(Py_None);
1801 return Py_None;
1802}
1803
1804static PyObject *
1805wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1806{
1807 setattrofunc func = (setattrofunc)wrapped;
1808 int res;
1809 PyObject *name;
1810
1811 if (!PyArg_ParseTuple(args, "O", &name))
1812 return NULL;
1813 res = (*func)(self, name, NULL);
1814 if (res < 0)
1815 return NULL;
1816 Py_INCREF(Py_None);
1817 return Py_None;
1818}
1819
1820static struct wrapperbase tab_setattr[] = {
1821 {"__setattr__", (wrapperfunc)wrap_setattr,
1822 "x.__setattr__('name', value) <==> x.name = value"},
1823 {"__delattr__", (wrapperfunc)wrap_delattr,
1824 "x.__delattr__('name') <==> del x.name"},
1825 {0}
1826};
1827
1828static PyObject *
1829wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1830{
1831 hashfunc func = (hashfunc)wrapped;
1832 long res;
1833
1834 if (!PyArg_ParseTuple(args, ""))
1835 return NULL;
1836 res = (*func)(self);
1837 if (res == -1 && PyErr_Occurred())
1838 return NULL;
1839 return PyInt_FromLong(res);
1840}
1841
1842static struct wrapperbase tab_hash[] = {
1843 {"__hash__", (wrapperfunc)wrap_hashfunc,
1844 "x.__hash__() <==> hash(x)"},
1845 {0}
1846};
1847
1848static PyObject *
1849wrap_call(PyObject *self, PyObject *args, void *wrapped)
1850{
1851 ternaryfunc func = (ternaryfunc)wrapped;
1852
1853 /* XXX What about keyword arguments? */
1854 return (*func)(self, args, NULL);
1855}
1856
1857static struct wrapperbase tab_call[] = {
1858 {"__call__", (wrapperfunc)wrap_call,
1859 "x.__call__(...) <==> x(...)"},
1860 {0}
1861};
1862
1863static struct wrapperbase tab_str[] = {
1864 {"__str__", (wrapperfunc)wrap_unaryfunc,
1865 "x.__str__() <==> str(x)"},
1866 {0}
1867};
1868
1869static PyObject *
1870wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1871{
1872 richcmpfunc func = (richcmpfunc)wrapped;
1873 PyObject *other;
1874
1875 if (!PyArg_ParseTuple(args, "O", &other))
1876 return NULL;
1877 return (*func)(self, other, op);
1878}
1879
1880#undef RICHCMP_WRAPPER
1881#define RICHCMP_WRAPPER(NAME, OP) \
1882static PyObject * \
1883richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1884{ \
1885 return wrap_richcmpfunc(self, args, wrapped, OP); \
1886}
1887
Jack Jansen8e938b42001-08-08 15:29:49 +00001888RICHCMP_WRAPPER(lt, Py_LT)
1889RICHCMP_WRAPPER(le, Py_LE)
1890RICHCMP_WRAPPER(eq, Py_EQ)
1891RICHCMP_WRAPPER(ne, Py_NE)
1892RICHCMP_WRAPPER(gt, Py_GT)
1893RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001894
1895#undef RICHCMP_ENTRY
1896#define RICHCMP_ENTRY(NAME, EXPR) \
1897 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1898 "x.__" #NAME "__(y) <==> " EXPR}
1899
1900static struct wrapperbase tab_richcmp[] = {
1901 RICHCMP_ENTRY(lt, "x<y"),
1902 RICHCMP_ENTRY(le, "x<=y"),
1903 RICHCMP_ENTRY(eq, "x==y"),
1904 RICHCMP_ENTRY(ne, "x!=y"),
1905 RICHCMP_ENTRY(gt, "x>y"),
1906 RICHCMP_ENTRY(ge, "x>=y"),
1907 {0}
1908};
1909
1910static struct wrapperbase tab_iter[] = {
1911 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1912 {0}
1913};
1914
1915static PyObject *
1916wrap_next(PyObject *self, PyObject *args, void *wrapped)
1917{
1918 unaryfunc func = (unaryfunc)wrapped;
1919 PyObject *res;
1920
1921 if (!PyArg_ParseTuple(args, ""))
1922 return NULL;
1923 res = (*func)(self);
1924 if (res == NULL && !PyErr_Occurred())
1925 PyErr_SetNone(PyExc_StopIteration);
1926 return res;
1927}
1928
1929static struct wrapperbase tab_next[] = {
1930 {"next", (wrapperfunc)wrap_next,
1931 "x.next() -> the next value, or raise StopIteration"},
1932 {0}
1933};
1934
1935static PyObject *
1936wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1937{
1938 descrgetfunc func = (descrgetfunc)wrapped;
1939 PyObject *obj;
1940 PyObject *type = NULL;
1941
1942 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1943 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001944 return (*func)(self, obj, type);
1945}
1946
1947static struct wrapperbase tab_descr_get[] = {
1948 {"__get__", (wrapperfunc)wrap_descr_get,
1949 "descr.__get__(obj, type) -> value"},
1950 {0}
1951};
1952
1953static PyObject *
1954wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
1955{
1956 descrsetfunc func = (descrsetfunc)wrapped;
1957 PyObject *obj, *value;
1958 int ret;
1959
1960 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
1961 return NULL;
1962 ret = (*func)(self, obj, value);
1963 if (ret < 0)
1964 return NULL;
1965 Py_INCREF(Py_None);
1966 return Py_None;
1967}
1968
1969static struct wrapperbase tab_descr_set[] = {
1970 {"__set__", (wrapperfunc)wrap_descrsetfunc,
1971 "descr.__set__(obj, value)"},
1972 {0}
1973};
1974
1975static PyObject *
1976wrap_init(PyObject *self, PyObject *args, void *wrapped)
1977{
1978 initproc func = (initproc)wrapped;
1979
1980 /* XXX What about keyword arguments? */
1981 if (func(self, args, NULL) < 0)
1982 return NULL;
1983 Py_INCREF(Py_None);
1984 return Py_None;
1985}
1986
1987static struct wrapperbase tab_init[] = {
1988 {"__init__", (wrapperfunc)wrap_init,
1989 "x.__init__(...) initializes x; "
1990 "see x.__type__.__doc__ for signature"},
1991 {0}
1992};
1993
1994static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001995tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001996{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001997 PyTypeObject *type, *subtype;
1998 PyObject *arg0, *res;
1999
2000 if (self == NULL || !PyType_Check(self))
2001 Py_FatalError("__new__() called with non-type 'self'");
2002 type = (PyTypeObject *)self;
2003 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
2004 PyErr_SetString(PyExc_TypeError,
2005 "T.__new__(): not enough arguments");
2006 return NULL;
2007 }
2008 arg0 = PyTuple_GET_ITEM(args, 0);
2009 if (!PyType_Check(arg0)) {
2010 PyErr_SetString(PyExc_TypeError,
2011 "T.__new__(S): S is not a type object");
2012 return NULL;
2013 }
2014 subtype = (PyTypeObject *)arg0;
2015 if (!PyType_IsSubtype(subtype, type)) {
2016 PyErr_SetString(PyExc_TypeError,
2017 "T.__new__(S): S is not a subtype of T");
2018 return NULL;
2019 }
2020 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2021 if (args == NULL)
2022 return NULL;
2023 res = type->tp_new(subtype, args, kwds);
2024 Py_DECREF(args);
2025 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002026}
2027
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002028static struct PyMethodDef tp_new_methoddef[] = {
2029 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2030 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002031 {0}
2032};
2033
2034static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002035add_tp_new_wrapper(PyTypeObject *type)
2036{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002037 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002038
Guido van Rossumf040ede2001-08-07 16:40:56 +00002039 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2040 return 0;
2041 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002042 if (func == NULL)
2043 return -1;
2044 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2045}
2046
Guido van Rossum13d52f02001-08-10 21:24:08 +00002047static int
2048add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2049{
2050 PyObject *dict = type->tp_defined;
2051
2052 for (; wraps->name != NULL; wraps++) {
2053 PyObject *descr;
2054 if (PyDict_GetItemString(dict, wraps->name))
2055 continue;
2056 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2057 if (descr == NULL)
2058 return -1;
2059 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2060 return -1;
2061 Py_DECREF(descr);
2062 }
2063 return 0;
2064}
2065
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002066/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002067 dictionary with method descriptors for function slots. For each
2068 function slot (like tp_repr) that's defined in the type, one or
2069 more corresponding descriptors are added in the type's tp_defined
2070 dictionary under the appropriate name (like __repr__). Some
2071 function slots cause more than one descriptor to be added (for
2072 example, the nb_add slot adds both __add__ and __radd__
2073 descriptors) and some function slots compete for the same
2074 descriptor (for example both sq_item and mp_subscript generate a
2075 __getitem__ descriptor). This only adds new descriptors and
2076 doesn't overwrite entries in tp_defined that were previously
2077 defined. The descriptors contain a reference to the C function
2078 they must call, so that it's safe if they are copied into a
2079 subtype's __dict__ and the subtype has a different C function in
2080 its slot -- calling the method defined by the descriptor will call
2081 the C function that was used to create it, rather than the C
2082 function present in the slot when it is called. (This is important
2083 because a subtype may have a C function in the slot that calls the
2084 method from the dictionary, and we want to avoid infinite recursion
2085 here.) */
2086
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002087static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002088add_operators(PyTypeObject *type)
2089{
2090 PySequenceMethods *sq;
2091 PyMappingMethods *mp;
2092 PyNumberMethods *nb;
2093
2094#undef ADD
2095#define ADD(SLOT, TABLE) \
2096 if (SLOT) { \
2097 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2098 return -1; \
2099 }
2100
2101 if ((sq = type->tp_as_sequence) != NULL) {
2102 ADD(sq->sq_length, tab_len);
2103 ADD(sq->sq_concat, tab_concat);
2104 ADD(sq->sq_repeat, tab_mul_int);
2105 ADD(sq->sq_item, tab_getitem_int);
2106 ADD(sq->sq_slice, tab_getslice);
2107 ADD(sq->sq_ass_item, tab_setitem_int);
2108 ADD(sq->sq_ass_slice, tab_setslice);
2109 ADD(sq->sq_contains, tab_contains);
2110 ADD(sq->sq_inplace_concat, tab_iadd);
2111 ADD(sq->sq_inplace_repeat, tab_imul_int);
2112 }
2113
2114 if ((mp = type->tp_as_mapping) != NULL) {
2115 if (sq->sq_length == NULL)
2116 ADD(mp->mp_length, tab_len);
2117 ADD(mp->mp_subscript, tab_getitem);
2118 ADD(mp->mp_ass_subscript, tab_setitem);
2119 }
2120
2121 /* We don't support "old-style numbers" because their binary
2122 operators require that both arguments have the same type;
2123 the wrappers here only work for new-style numbers. */
2124 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2125 (nb = type->tp_as_number) != NULL) {
2126 ADD(nb->nb_add, tab_add);
2127 ADD(nb->nb_subtract, tab_sub);
2128 ADD(nb->nb_multiply, tab_mul);
2129 ADD(nb->nb_divide, tab_div);
2130 ADD(nb->nb_remainder, tab_mod);
2131 ADD(nb->nb_divmod, tab_divmod);
2132 ADD(nb->nb_power, tab_pow);
2133 ADD(nb->nb_negative, tab_neg);
2134 ADD(nb->nb_positive, tab_pos);
2135 ADD(nb->nb_absolute, tab_abs);
2136 ADD(nb->nb_nonzero, tab_nonzero);
2137 ADD(nb->nb_invert, tab_invert);
2138 ADD(nb->nb_lshift, tab_lshift);
2139 ADD(nb->nb_rshift, tab_rshift);
2140 ADD(nb->nb_and, tab_and);
2141 ADD(nb->nb_xor, tab_xor);
2142 ADD(nb->nb_or, tab_or);
2143 /* We don't support coerce() -- see above comment */
2144 ADD(nb->nb_int, tab_int);
2145 ADD(nb->nb_long, tab_long);
2146 ADD(nb->nb_float, tab_float);
2147 ADD(nb->nb_oct, tab_oct);
2148 ADD(nb->nb_hex, tab_hex);
2149 ADD(nb->nb_inplace_add, tab_iadd);
2150 ADD(nb->nb_inplace_subtract, tab_isub);
2151 ADD(nb->nb_inplace_multiply, tab_imul);
2152 ADD(nb->nb_inplace_divide, tab_idiv);
2153 ADD(nb->nb_inplace_remainder, tab_imod);
2154 ADD(nb->nb_inplace_power, tab_ipow);
2155 ADD(nb->nb_inplace_lshift, tab_ilshift);
2156 ADD(nb->nb_inplace_rshift, tab_irshift);
2157 ADD(nb->nb_inplace_and, tab_iand);
2158 ADD(nb->nb_inplace_xor, tab_ixor);
2159 ADD(nb->nb_inplace_or, tab_ior);
2160 }
2161
2162 ADD(type->tp_getattro, tab_getattr);
2163 ADD(type->tp_setattro, tab_setattr);
2164 ADD(type->tp_compare, tab_cmp);
2165 ADD(type->tp_repr, tab_repr);
2166 ADD(type->tp_hash, tab_hash);
2167 ADD(type->tp_call, tab_call);
2168 ADD(type->tp_str, tab_str);
2169 ADD(type->tp_richcompare, tab_richcmp);
2170 ADD(type->tp_iter, tab_iter);
2171 ADD(type->tp_iternext, tab_next);
2172 ADD(type->tp_descr_get, tab_descr_get);
2173 ADD(type->tp_descr_set, tab_descr_set);
2174 ADD(type->tp_init, tab_init);
2175
Guido van Rossumf040ede2001-08-07 16:40:56 +00002176 if (type->tp_new != NULL) {
2177 if (add_tp_new_wrapper(type) < 0)
2178 return -1;
2179 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002180
2181 return 0;
2182}
2183
Guido van Rossumf040ede2001-08-07 16:40:56 +00002184/* Slot wrappers that call the corresponding __foo__ slot. See comments
2185 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002186
Guido van Rossumdc91b992001-08-08 22:26:22 +00002187#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002188static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002189FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002190{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002191 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002192}
2193
Guido van Rossumdc91b992001-08-08 22:26:22 +00002194#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002195static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002196FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002197{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002198 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199}
2200
Guido van Rossumdc91b992001-08-08 22:26:22 +00002201
2202#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002203static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002204FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002205{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002206 if (self->ob_type->tp_as_number != NULL && \
2207 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2208 PyObject *r; \
2209 r = PyObject_CallMethod( \
2210 self, OPSTR, "O", other); \
2211 if (r != Py_NotImplemented || \
2212 other->ob_type == self->ob_type) \
2213 return r; \
2214 Py_DECREF(r); \
2215 } \
2216 if (other->ob_type->tp_as_number != NULL && \
2217 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2218 return PyObject_CallMethod( \
2219 other, ROPSTR, "O", self); \
2220 } \
2221 Py_INCREF(Py_NotImplemented); \
2222 return Py_NotImplemented; \
2223}
2224
2225#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2226 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2227
2228#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2229static PyObject * \
2230FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2231{ \
2232 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002233}
2234
2235static int
2236slot_sq_length(PyObject *self)
2237{
2238 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2239
2240 if (res == NULL)
2241 return -1;
2242 return (int)PyInt_AsLong(res);
2243}
2244
Guido van Rossumdc91b992001-08-08 22:26:22 +00002245SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2246SLOT1(slot_sq_repeat, "__mul__", int, "i")
2247SLOT1(slot_sq_item, "__getitem__", int, "i")
2248SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002249
2250static int
2251slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2252{
2253 PyObject *res;
2254
2255 if (value == NULL)
2256 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2257 else
2258 res = PyObject_CallMethod(self, "__setitem__",
2259 "iO", index, value);
2260 if (res == NULL)
2261 return -1;
2262 Py_DECREF(res);
2263 return 0;
2264}
2265
2266static int
2267slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2268{
2269 PyObject *res;
2270
2271 if (value == NULL)
2272 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2273 else
2274 res = PyObject_CallMethod(self, "__setslice__",
2275 "iiO", i, j, value);
2276 if (res == NULL)
2277 return -1;
2278 Py_DECREF(res);
2279 return 0;
2280}
2281
2282static int
2283slot_sq_contains(PyObject *self, PyObject *value)
2284{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002285 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286
Guido van Rossumb8f63662001-08-15 23:57:02 +00002287 func = PyObject_GetAttrString(self, "__contains__");
2288
2289 if (func != NULL) {
2290 args = Py_BuildValue("(O)", value);
2291 if (args == NULL)
2292 res = NULL;
2293 else {
2294 res = PyEval_CallObject(func, args);
2295 Py_DECREF(args);
2296 }
2297 Py_DECREF(func);
2298 if (res == NULL)
2299 return -1;
2300 return PyObject_IsTrue(res);
2301 }
2302 else {
2303 PyErr_Clear();
2304 return _PySequence_IterContains(self, value);
2305 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002306}
2307
Guido van Rossumdc91b992001-08-08 22:26:22 +00002308SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2309SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310
2311#define slot_mp_length slot_sq_length
2312
Guido van Rossumdc91b992001-08-08 22:26:22 +00002313SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314
2315static int
2316slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2317{
2318 PyObject *res;
2319
2320 if (value == NULL)
2321 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2322 else
2323 res = PyObject_CallMethod(self, "__setitem__",
2324 "OO", key, value);
2325 if (res == NULL)
2326 return -1;
2327 Py_DECREF(res);
2328 return 0;
2329}
2330
Guido van Rossumdc91b992001-08-08 22:26:22 +00002331SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2332SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2333SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2334SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2335SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2336SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2337
2338staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2339
2340SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2341 nb_power, "__pow__", "__rpow__")
2342
2343static PyObject *
2344slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2345{
2346 if (modulus == Py_None)
2347 return slot_nb_power_binary(self, other);
2348 /* Three-arg power doesn't use __rpow__ */
2349 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2350}
2351
2352SLOT0(slot_nb_negative, "__neg__")
2353SLOT0(slot_nb_positive, "__pos__")
2354SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002355
2356static int
2357slot_nb_nonzero(PyObject *self)
2358{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002359 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002360
Guido van Rossumb8f63662001-08-15 23:57:02 +00002361 func = PyObject_GetAttrString(self, "__nonzero__");
2362 if (func == NULL) {
2363 PyErr_Clear();
2364 func = PyObject_GetAttrString(self, "__len__");
2365 }
2366
2367 if (func != NULL) {
2368 res = PyEval_CallObject(func, NULL);
2369 Py_DECREF(func);
2370 if (res == NULL)
2371 return -1;
2372 return PyObject_IsTrue(res);
2373 }
2374 else {
2375 PyErr_Clear();
2376 return 1;
2377 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002378}
2379
Guido van Rossumdc91b992001-08-08 22:26:22 +00002380SLOT0(slot_nb_invert, "__invert__")
2381SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2382SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2383SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2384SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2385SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002386/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002387SLOT0(slot_nb_int, "__int__")
2388SLOT0(slot_nb_long, "__long__")
2389SLOT0(slot_nb_float, "__float__")
2390SLOT0(slot_nb_oct, "__oct__")
2391SLOT0(slot_nb_hex, "__hex__")
2392SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2393SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2394SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2395SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2396SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2397SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2398SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2399SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2400SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2401SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2402SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2403SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2404 "__floordiv__", "__rfloordiv__")
2405SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2406SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2407SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408
2409static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002410half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002411{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002412 PyObject *func, *args, *res;
2413 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002414
Guido van Rossumb8f63662001-08-15 23:57:02 +00002415 func = PyObject_GetAttrString(self, "__cmp__");
2416 if (func == NULL) {
2417 PyErr_Clear();
2418 }
2419 else {
2420 args = Py_BuildValue("(O)", other);
2421 if (args == NULL)
2422 res = NULL;
2423 else {
2424 res = PyObject_CallObject(func, args);
2425 Py_DECREF(args);
2426 }
2427 if (res != Py_NotImplemented) {
2428 if (res == NULL)
2429 return -2;
2430 c = PyInt_AsLong(res);
2431 Py_DECREF(res);
2432 if (c == -1 && PyErr_Occurred())
2433 return -2;
2434 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2435 }
2436 Py_DECREF(res);
2437 }
2438 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002439}
2440
Guido van Rossumb8f63662001-08-15 23:57:02 +00002441static int
2442slot_tp_compare(PyObject *self, PyObject *other)
2443{
2444 int c;
2445
2446 if (self->ob_type->tp_compare == slot_tp_compare) {
2447 c = half_compare(self, other);
2448 if (c <= 1)
2449 return c;
2450 }
2451 if (other->ob_type->tp_compare == slot_tp_compare) {
2452 c = half_compare(other, self);
2453 if (c < -1)
2454 return -2;
2455 if (c <= 1)
2456 return -c;
2457 }
2458 return (void *)self < (void *)other ? -1 :
2459 (void *)self > (void *)other ? 1 : 0;
2460}
2461
2462static PyObject *
2463slot_tp_repr(PyObject *self)
2464{
2465 PyObject *func, *res;
2466
2467 func = PyObject_GetAttrString(self, "__repr__");
2468 if (func != NULL) {
2469 res = PyEval_CallObject(func, NULL);
2470 Py_DECREF(func);
2471 return res;
2472 }
2473 else {
2474 char buf[120];
2475 PyErr_Clear();
2476 sprintf(buf, "<%.80s object at %p>",
2477 self->ob_type->tp_name, self);
2478 return PyString_FromString(buf);
2479 }
2480}
2481
2482static PyObject *
2483slot_tp_str(PyObject *self)
2484{
2485 PyObject *func, *res;
2486
2487 func = PyObject_GetAttrString(self, "__str__");
2488 if (func != NULL) {
2489 res = PyEval_CallObject(func, NULL);
2490 Py_DECREF(func);
2491 return res;
2492 }
2493 else {
2494 PyErr_Clear();
2495 return slot_tp_repr(self);
2496 }
2497}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002498
2499static long
2500slot_tp_hash(PyObject *self)
2501{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002502 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002503 long h;
2504
Guido van Rossumb8f63662001-08-15 23:57:02 +00002505 func = PyObject_GetAttrString(self, "__hash__");
2506
2507 if (func != NULL) {
2508 res = PyEval_CallObject(func, NULL);
2509 Py_DECREF(func);
2510 if (res == NULL)
2511 return -1;
2512 h = PyInt_AsLong(res);
2513 }
2514 else {
2515 PyErr_Clear();
2516 func = PyObject_GetAttrString(self, "__eq__");
2517 if (func == NULL) {
2518 PyErr_Clear();
2519 func = PyObject_GetAttrString(self, "__cmp__");
2520 }
2521 if (func != NULL) {
2522 Py_DECREF(func);
2523 PyErr_SetString(PyExc_TypeError, "unhashable type");
2524 return -1;
2525 }
2526 PyErr_Clear();
2527 h = _Py_HashPointer((void *)self);
2528 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002529 if (h == -1 && !PyErr_Occurred())
2530 h = -2;
2531 return h;
2532}
2533
2534static PyObject *
2535slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2536{
2537 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2538 PyObject *res;
2539
2540 if (meth == NULL)
2541 return NULL;
2542 res = PyObject_Call(meth, args, kwds);
2543 Py_DECREF(meth);
2544 return res;
2545}
2546
Tim Peters6d6c1a32001-08-02 04:15:00 +00002547static PyObject *
2548slot_tp_getattro(PyObject *self, PyObject *name)
2549{
2550 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002551 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002552 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002553
Guido van Rossum8e248182001-08-12 05:17:56 +00002554 if (getattr_str == NULL) {
2555 getattr_str = PyString_InternFromString("__getattr__");
2556 if (getattr_str == NULL)
2557 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002558 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002559 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002560 if (getattr == NULL) {
2561 /* Avoid further slowdowns */
2562 if (tp->tp_getattro == slot_tp_getattro)
2563 tp->tp_getattro = PyObject_GenericGetAttr;
2564 else
2565 fprintf(stderr, "huh?\n");
Guido van Rossum8e248182001-08-12 05:17:56 +00002566 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002567 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002568 return PyObject_CallFunction(getattr, "OO", self, name);
2569}
2570
2571static int
2572slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2573{
2574 PyObject *res;
2575
2576 if (value == NULL)
2577 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2578 else
2579 res = PyObject_CallMethod(self, "__setattr__",
2580 "OO", name, value);
2581 if (res == NULL)
2582 return -1;
2583 Py_DECREF(res);
2584 return 0;
2585}
2586
2587/* Map rich comparison operators to their __xx__ namesakes */
2588static char *name_op[] = {
2589 "__lt__",
2590 "__le__",
2591 "__eq__",
2592 "__ne__",
2593 "__gt__",
2594 "__ge__",
2595};
2596
2597static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002598half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002599{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002600 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002601
Guido van Rossumb8f63662001-08-15 23:57:02 +00002602 func = PyObject_GetAttrString(self, name_op[op]);
2603 if (func == NULL) {
2604 PyErr_Clear();
2605 Py_INCREF(Py_NotImplemented);
2606 return Py_NotImplemented;
2607 }
2608 args = Py_BuildValue("(O)", other);
2609 if (args == NULL)
2610 res = NULL;
2611 else {
2612 res = PyObject_CallObject(func, args);
2613 Py_DECREF(args);
2614 }
2615 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002616 return res;
2617}
2618
Guido van Rossumb8f63662001-08-15 23:57:02 +00002619/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2620static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2621
2622static PyObject *
2623slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2624{
2625 PyObject *res;
2626
2627 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2628 res = half_richcompare(self, other, op);
2629 if (res != Py_NotImplemented)
2630 return res;
2631 Py_DECREF(res);
2632 }
2633 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2634 res = half_richcompare(other, self, swapped_op[op]);
2635 if (res != Py_NotImplemented) {
2636 return res;
2637 }
2638 Py_DECREF(res);
2639 }
2640 Py_INCREF(Py_NotImplemented);
2641 return Py_NotImplemented;
2642}
2643
2644static PyObject *
2645slot_tp_iter(PyObject *self)
2646{
2647 PyObject *func, *res;
2648
2649 func = PyObject_GetAttrString(self, "__iter__");
2650 if (func != NULL) {
2651 res = PyObject_CallObject(func, NULL);
2652 Py_DECREF(func);
2653 return res;
2654 }
2655 PyErr_Clear();
2656 func = PyObject_GetAttrString(self, "__getitem__");
2657 if (func == NULL) {
2658 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2659 return NULL;
2660 }
2661 Py_DECREF(func);
2662 return PySeqIter_New(self);
2663}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002664
2665static PyObject *
2666slot_tp_iternext(PyObject *self)
2667{
2668 return PyObject_CallMethod(self, "next", "");
2669}
2670
Guido van Rossumdc91b992001-08-08 22:26:22 +00002671SLOT2(slot_tp_descr_get, "__get__", PyObject *, PyObject *, "OO")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002672
2673static int
2674slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2675{
2676 PyObject *res = PyObject_CallMethod(self, "__set__",
2677 "OO", target, value);
2678 if (res == NULL)
2679 return -1;
2680 Py_DECREF(res);
2681 return 0;
2682}
2683
2684static int
2685slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2686{
2687 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2688 PyObject *res;
2689
2690 if (meth == NULL)
2691 return -1;
2692 res = PyObject_Call(meth, args, kwds);
2693 Py_DECREF(meth);
2694 if (res == NULL)
2695 return -1;
2696 Py_DECREF(res);
2697 return 0;
2698}
2699
2700static PyObject *
2701slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2702{
2703 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2704 PyObject *newargs, *x;
2705 int i, n;
2706
2707 if (func == NULL)
2708 return NULL;
2709 assert(PyTuple_Check(args));
2710 n = PyTuple_GET_SIZE(args);
2711 newargs = PyTuple_New(n+1);
2712 if (newargs == NULL)
2713 return NULL;
2714 Py_INCREF(type);
2715 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2716 for (i = 0; i < n; i++) {
2717 x = PyTuple_GET_ITEM(args, i);
2718 Py_INCREF(x);
2719 PyTuple_SET_ITEM(newargs, i+1, x);
2720 }
2721 x = PyObject_Call(func, newargs, kwds);
2722 Py_DECREF(func);
2723 return x;
2724}
2725
Guido van Rossumf040ede2001-08-07 16:40:56 +00002726/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002727 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002728 The dict argument is the dictionary argument passed to type_new(),
2729 which is the local namespace of the class statement, in other
2730 words, it contains the methods. For each special method (like
2731 __repr__) defined in the dictionary, the corresponding function
2732 slot in the type object (like tp_repr) is set to a special function
2733 whose name is 'slot_' followed by the slot name and whose signature
2734 is whatever is required for that slot. These slot functions look
2735 up the corresponding method in the type's dictionary and call it.
2736 The slot functions have to take care of the various peculiarities
2737 of the mapping between slots and special methods, such as mapping
2738 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2739 etc.) or mapping multiple slots to a single method (sq_item,
2740 mp_subscript <--> __getitem__). */
2741
Tim Peters6d6c1a32001-08-02 04:15:00 +00002742static void
2743override_slots(PyTypeObject *type, PyObject *dict)
2744{
2745 PySequenceMethods *sq = type->tp_as_sequence;
2746 PyMappingMethods *mp = type->tp_as_mapping;
2747 PyNumberMethods *nb = type->tp_as_number;
2748
Guido van Rossumdc91b992001-08-08 22:26:22 +00002749#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002750 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002751 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002752 }
2753
Guido van Rossumdc91b992001-08-08 22:26:22 +00002754#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002755 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002756 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002757 }
2758
Guido van Rossumdc91b992001-08-08 22:26:22 +00002759#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002760 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002761 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002762 }
2763
Guido van Rossumdc91b992001-08-08 22:26:22 +00002764#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002765 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002766 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002767 }
2768
Guido van Rossumdc91b992001-08-08 22:26:22 +00002769 SQSLOT("__len__", sq_length, slot_sq_length);
2770 SQSLOT("__add__", sq_concat, slot_sq_concat);
2771 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2772 SQSLOT("__getitem__", sq_item, slot_sq_item);
2773 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2774 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2775 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2776 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2777 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2778 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2779 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2780 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781
Guido van Rossumdc91b992001-08-08 22:26:22 +00002782 MPSLOT("__len__", mp_length, slot_mp_length);
2783 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2784 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2785 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002786
Guido van Rossumdc91b992001-08-08 22:26:22 +00002787 NBSLOT("__add__", nb_add, slot_nb_add);
2788 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2789 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2790 NBSLOT("__div__", nb_divide, slot_nb_divide);
2791 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2792 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2793 NBSLOT("__pow__", nb_power, slot_nb_power);
2794 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2795 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2796 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2797 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2798 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2799 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2800 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2801 NBSLOT("__and__", nb_and, slot_nb_and);
2802 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2803 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002804 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002805 NBSLOT("__int__", nb_int, slot_nb_int);
2806 NBSLOT("__long__", nb_long, slot_nb_long);
2807 NBSLOT("__float__", nb_float, slot_nb_float);
2808 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2809 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2810 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2811 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2812 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2813 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2814 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2815 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2816 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2817 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2818 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2819 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2820 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2821 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2822 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2823 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2824 slot_nb_inplace_floor_divide);
2825 NBSLOT("__itruediv__", nb_inplace_true_divide,
2826 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002827
Guido van Rossum8e248182001-08-12 05:17:56 +00002828 if (dict == NULL ||
2829 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002830 PyDict_GetItemString(dict, "__repr__"))
2831 type->tp_print = NULL;
2832
Guido van Rossumdc91b992001-08-08 22:26:22 +00002833 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2834 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2835 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2836 TPSLOT("__call__", tp_call, slot_tp_call);
2837 TPSLOT("__str__", tp_str, slot_tp_str);
2838 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2839 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2840 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2841 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2842 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2843 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2844 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2845 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2846 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2847 TPSLOT("next", tp_iternext, slot_tp_iternext);
2848 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2849 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2850 TPSLOT("__init__", tp_init, slot_tp_init);
2851 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002852}