blob: 474ff3ee2b30fd5738243e7e85f7ba31d0708658 [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;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000500 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000501 etype *et;
502 struct memberlist *mp;
503 int i, nbases, nslots, slotoffset, dynamic;
504
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000505 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000506 if (metatype == &PyType_Type &&
507 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
508 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000509 PyObject *x = PyTuple_GET_ITEM(args, 0);
510 Py_INCREF(x->ob_type);
511 return (PyObject *) x->ob_type;
512 }
513
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000514 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000515 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);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000526 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000527 for (i = 0; i < nbases; i++) {
528 tmp = PyTuple_GET_ITEM(bases, i);
529 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000530 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000531 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000532 if (PyType_IsSubtype(tmptype, winner)) {
533 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000534 continue;
535 }
536 PyErr_SetString(PyExc_TypeError,
537 "metatype conflict among bases");
538 return NULL;
539 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000540 if (winner != metatype) {
541 if (winner->tp_new != type_new) /* Pass it to the winner */
542 return winner->tp_new(winner, args, kwds);
543 metatype = winner;
544 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000545
546 /* Adjust for empty tuple bases */
547 if (nbases == 0) {
548 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
549 if (bases == NULL)
550 return NULL;
551 nbases = 1;
552 }
553 else
554 Py_INCREF(bases);
555
556 /* XXX From here until type is allocated, "return NULL" leaks bases! */
557
558 /* Calculate best base, and check that all bases are type objects */
559 base = best_base(bases);
560 if (base == NULL)
561 return NULL;
562 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
563 PyErr_Format(PyExc_TypeError,
564 "type '%.100s' is not an acceptable base type",
565 base->tp_name);
566 return NULL;
567 }
568
569 /* Should this be a dynamic class (i.e. modifiable __dict__)? */
570 tmp = PyDict_GetItemString(dict, "__dynamic__");
571 if (tmp != NULL) {
572 /* The class author has a preference */
573 dynamic = PyObject_IsTrue(tmp);
574 Py_DECREF(tmp);
575 if (dynamic < 0)
576 return NULL;
577 }
578 else {
579 /* Make a new class dynamic if any of its bases is dynamic.
580 This is not always the same as inheriting the __dynamic__
581 class attribute! */
582 dynamic = 0;
583 for (i = 0; i < nbases; i++) {
584 tmptype = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
585 if (tmptype->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
586 dynamic = 1;
587 break;
588 }
589 }
590 }
591
592 /* Check for a __slots__ sequence variable in dict, and count it */
593 slots = PyDict_GetItemString(dict, "__slots__");
594 nslots = 0;
595 if (slots != NULL) {
596 /* Make it into a tuple */
597 if (PyString_Check(slots))
598 slots = Py_BuildValue("(O)", slots);
599 else
600 slots = PySequence_Tuple(slots);
601 if (slots == NULL)
602 return NULL;
603 nslots = PyTuple_GET_SIZE(slots);
604 for (i = 0; i < nslots; i++) {
605 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
606 PyErr_SetString(PyExc_TypeError,
607 "__slots__ must be a sequence of strings");
608 Py_DECREF(slots);
609 return NULL;
610 }
611 }
612 }
613 if (slots == NULL && base->tp_dictoffset == 0 &&
614 (base->tp_setattro == PyObject_GenericSetAttr ||
615 base->tp_setattro == NULL))
616 nslots = 1;
617
618 /* XXX From here until type is safely allocated,
619 "return NULL" may leak slots! */
620
621 /* Allocate the type object */
622 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
623 if (type == NULL)
624 return NULL;
625
626 /* Keep name and slots alive in the extended type object */
627 et = (etype *)type;
628 Py_INCREF(name);
629 et->name = name;
630 et->slots = slots;
631
Guido van Rossumdc91b992001-08-08 22:26:22 +0000632 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000633 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
634 Py_TPFLAGS_BASETYPE;
635 if (dynamic)
636 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000637
638 /* It's a new-style number unless it specifically inherits any
639 old-style numeric behavior */
640 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
641 (base->tp_as_number == NULL))
642 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
643
644 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000645 type->tp_as_number = &et->as_number;
646 type->tp_as_sequence = &et->as_sequence;
647 type->tp_as_mapping = &et->as_mapping;
648 type->tp_as_buffer = &et->as_buffer;
649 type->tp_name = PyString_AS_STRING(name);
650
651 /* Set tp_base and tp_bases */
652 type->tp_bases = bases;
653 Py_INCREF(base);
654 type->tp_base = base;
655
656 /* Initialize tp_defined from passed-in dict */
657 type->tp_defined = dict = PyDict_Copy(dict);
658 if (dict == NULL) {
659 Py_DECREF(type);
660 return NULL;
661 }
662
Guido van Rossumc3542212001-08-16 09:18:56 +0000663 /* Set __module__ in the dict */
664 if (PyDict_GetItemString(dict, "__module__") == NULL) {
665 tmp = PyEval_GetGlobals();
666 if (tmp != NULL) {
667 tmp = PyDict_GetItemString(tmp, "__name__");
668 if (tmp != NULL) {
669 if (PyDict_SetItemString(dict, "__module__",
670 tmp) < 0)
671 return NULL;
672 }
673 }
674 }
675
Tim Peters6d6c1a32001-08-02 04:15:00 +0000676 /* Special-case __new__: if it's a plain function,
677 make it a static function */
678 tmp = PyDict_GetItemString(dict, "__new__");
679 if (tmp != NULL && PyFunction_Check(tmp)) {
680 tmp = PyStaticMethod_New(tmp);
681 if (tmp == NULL) {
682 Py_DECREF(type);
683 return NULL;
684 }
685 PyDict_SetItemString(dict, "__new__", tmp);
686 Py_DECREF(tmp);
687 }
688
689 /* Add descriptors for custom slots from __slots__, or for __dict__ */
690 mp = et->members;
691 slotoffset = PyType_BASICSIZE(base);
692 if (slots != NULL) {
693 for (i = 0; i < nslots; i++, mp++) {
694 mp->name = PyString_AS_STRING(
695 PyTuple_GET_ITEM(slots, i));
696 mp->type = T_OBJECT;
697 mp->offset = slotoffset;
698 slotoffset += sizeof(PyObject *);
699 }
700 }
701 else if (nslots) {
702 type->tp_dictoffset = slotoffset;
703 mp->name = "__dict__";
704 mp->type = T_OBJECT;
705 mp->offset = slotoffset;
706 mp->readonly = 1;
707 slotoffset += sizeof(PyObject *);
708 }
709 type->tp_basicsize = slotoffset;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000710 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000711
712 /* Special case some slots */
713 if (type->tp_dictoffset != 0 || nslots > 0) {
714 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
715 type->tp_getattro = PyObject_GenericGetAttr;
716 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
717 type->tp_setattro = PyObject_GenericSetAttr;
718 }
719 type->tp_dealloc = subtype_dealloc;
720
721 /* Always override allocation strategy to use regular heap */
722 type->tp_alloc = PyType_GenericAlloc;
723 type->tp_free = _PyObject_Del;
724
725 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000726 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000727 Py_DECREF(type);
728 return NULL;
729 }
730
731 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000732 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
733 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000734
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735 return (PyObject *)type;
736}
737
738/* Internal API to look for a name through the MRO.
739 This returns a borrowed reference, and doesn't set an exception! */
740PyObject *
741_PyType_Lookup(PyTypeObject *type, PyObject *name)
742{
743 int i, n;
744 PyObject *mro, *res, *dict;
745
746 /* For static types, look in tp_dict */
747 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
748 dict = type->tp_dict;
749 assert(dict && PyDict_Check(dict));
750 return PyDict_GetItem(dict, name);
751 }
752
753 /* For dynamic types, look in tp_defined of types in MRO */
754 mro = type->tp_mro;
755 assert(PyTuple_Check(mro));
756 n = PyTuple_GET_SIZE(mro);
757 for (i = 0; i < n; i++) {
758 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
759 assert(PyType_Check(type));
760 dict = type->tp_defined;
761 assert(dict && PyDict_Check(dict));
762 res = PyDict_GetItem(dict, name);
763 if (res != NULL)
764 return res;
765 }
766 return NULL;
767}
768
769/* This is similar to PyObject_GenericGetAttr(),
770 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
771static PyObject *
772type_getattro(PyTypeObject *type, PyObject *name)
773{
774 PyTypeObject *metatype = type->ob_type;
775 PyObject *descr, *res;
776 descrgetfunc f;
777
778 /* Initialize this type (we'll assume the metatype is initialized) */
779 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000780 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000781 return NULL;
782 }
783
784 /* Get a descriptor from the metatype */
785 descr = _PyType_Lookup(metatype, name);
786 f = NULL;
787 if (descr != NULL) {
788 f = descr->ob_type->tp_descr_get;
789 if (f != NULL && PyDescr_IsData(descr))
790 return f(descr,
791 (PyObject *)type, (PyObject *)metatype);
792 }
793
794 /* Look in tp_defined of this type and its bases */
795 res = _PyType_Lookup(type, name);
796 if (res != NULL) {
797 f = res->ob_type->tp_descr_get;
798 if (f != NULL)
799 return f(res, (PyObject *)NULL, (PyObject *)type);
800 Py_INCREF(res);
801 return res;
802 }
803
804 /* Use the descriptor from the metatype */
805 if (f != NULL) {
806 res = f(descr, (PyObject *)type, (PyObject *)metatype);
807 return res;
808 }
809 if (descr != NULL) {
810 Py_INCREF(descr);
811 return descr;
812 }
813
814 /* Give up */
815 PyErr_Format(PyExc_AttributeError,
816 "type object '%.50s' has no attribute '%.400s'",
817 type->tp_name, PyString_AS_STRING(name));
818 return NULL;
819}
820
821static int
822type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
823{
824 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
825 return PyObject_GenericSetAttr((PyObject *)type, name, value);
826 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
827 return -1;
828}
829
830static void
831type_dealloc(PyTypeObject *type)
832{
833 etype *et;
834
835 /* Assert this is a heap-allocated type object */
836 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
837 et = (etype *)type;
838 Py_XDECREF(type->tp_base);
839 Py_XDECREF(type->tp_dict);
840 Py_XDECREF(type->tp_bases);
841 Py_XDECREF(type->tp_mro);
842 Py_XDECREF(type->tp_defined);
843 /* XXX more? */
844 Py_XDECREF(et->name);
845 Py_XDECREF(et->slots);
846 type->ob_type->tp_free((PyObject *)type);
847}
848
849static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000850 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851 "mro() -> list\nreturn a type's method resolution order"},
852 {0}
853};
854
855static char type_doc[] =
856"type(object) -> the object's type\n"
857"type(name, bases, dict) -> a new type";
858
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000859PyTypeObject PyType_Type = {
860 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861 0, /* ob_size */
862 "type", /* tp_name */
863 sizeof(etype), /* tp_basicsize */
864 sizeof(struct memberlist), /* tp_itemsize */
865 (destructor)type_dealloc, /* tp_dealloc */
866 0, /* tp_print */
867 0, /* tp_getattr */
868 0, /* tp_setattr */
869 type_compare, /* tp_compare */
870 (reprfunc)type_repr, /* tp_repr */
871 0, /* tp_as_number */
872 0, /* tp_as_sequence */
873 0, /* tp_as_mapping */
874 (hashfunc)_Py_HashPointer, /* tp_hash */
875 (ternaryfunc)type_call, /* tp_call */
876 0, /* tp_str */
877 (getattrofunc)type_getattro, /* tp_getattro */
878 (setattrofunc)type_setattro, /* tp_setattro */
879 0, /* tp_as_buffer */
880 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
881 type_doc, /* tp_doc */
882 0, /* tp_traverse */
883 0, /* tp_clear */
884 0, /* tp_richcompare */
885 0, /* tp_weaklistoffset */
886 0, /* tp_iter */
887 0, /* tp_iternext */
888 type_methods, /* tp_methods */
889 type_members, /* tp_members */
890 type_getsets, /* tp_getset */
891 0, /* tp_base */
892 0, /* tp_dict */
893 0, /* tp_descr_get */
894 0, /* tp_descr_set */
895 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
896 0, /* tp_init */
897 0, /* tp_alloc */
898 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000899};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900
901
902/* The base type of all types (eventually)... except itself. */
903
904static int
905object_init(PyObject *self, PyObject *args, PyObject *kwds)
906{
907 return 0;
908}
909
910static void
911object_dealloc(PyObject *self)
912{
913 self->ob_type->tp_free(self);
914}
915
Guido van Rossum8e248182001-08-12 05:17:56 +0000916static PyObject *
917object_repr(PyObject *self)
918{
Guido van Rossum76e69632001-08-16 18:52:43 +0000919 PyTypeObject *type;
920 PyObject *mod, *name;
921 char buf[200];
Guido van Rossum8e248182001-08-12 05:17:56 +0000922
Guido van Rossum76e69632001-08-16 18:52:43 +0000923 type = self->ob_type;
924 mod = type_module(type, NULL);
925 if (mod == NULL)
926 PyErr_Clear();
927 else if (!PyString_Check(mod)) {
928 Py_DECREF(mod);
929 mod = NULL;
930 }
931 name = type_name(type, NULL);
932 if (name == NULL)
933 return NULL;
934 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
935 sprintf(buf, "<%.80s.%.80s instance at %p>",
936 PyString_AS_STRING(mod),
937 PyString_AS_STRING(name),
938 self);
939 else
940 sprintf(buf, "<%.80s instance at %p>", type->tp_name, self);
941 Py_XDECREF(mod);
942 Py_DECREF(name);
Guido van Rossum8e248182001-08-12 05:17:56 +0000943 return PyString_FromString(buf);
944}
945
Guido van Rossumb8f63662001-08-15 23:57:02 +0000946static PyObject *
947object_str(PyObject *self)
948{
949 unaryfunc f;
950
951 f = self->ob_type->tp_repr;
952 if (f == NULL)
953 f = object_repr;
954 return f(self);
955}
956
Guido van Rossum8e248182001-08-12 05:17:56 +0000957static long
958object_hash(PyObject *self)
959{
960 return _Py_HashPointer(self);
961}
Guido van Rossum8e248182001-08-12 05:17:56 +0000962
Tim Peters6d6c1a32001-08-02 04:15:00 +0000963static void
964object_free(PyObject *self)
965{
966 PyObject_Del(self);
967}
968
969static struct memberlist object_members[] = {
970 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
971 {0}
972};
973
974PyTypeObject PyBaseObject_Type = {
975 PyObject_HEAD_INIT(&PyType_Type)
976 0, /* ob_size */
977 "object", /* tp_name */
978 sizeof(PyObject), /* tp_basicsize */
979 0, /* tp_itemsize */
980 (destructor)object_dealloc, /* tp_dealloc */
981 0, /* tp_print */
982 0, /* tp_getattr */
983 0, /* tp_setattr */
984 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000985 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986 0, /* tp_as_number */
987 0, /* tp_as_sequence */
988 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000989 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +0000991 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +0000993 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000994 0, /* tp_as_buffer */
995 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
996 "The most base type", /* tp_doc */
997 0, /* tp_traverse */
998 0, /* tp_clear */
999 0, /* tp_richcompare */
1000 0, /* tp_weaklistoffset */
1001 0, /* tp_iter */
1002 0, /* tp_iternext */
1003 0, /* tp_methods */
1004 object_members, /* tp_members */
1005 0, /* tp_getset */
1006 0, /* tp_base */
1007 0, /* tp_dict */
1008 0, /* tp_descr_get */
1009 0, /* tp_descr_set */
1010 0, /* tp_dictoffset */
1011 object_init, /* tp_init */
1012 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001013 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 object_free, /* tp_free */
1015};
1016
1017
1018/* Initialize the __dict__ in a type object */
1019
1020static int
1021add_methods(PyTypeObject *type, PyMethodDef *meth)
1022{
1023 PyObject *dict = type->tp_defined;
1024
1025 for (; meth->ml_name != NULL; meth++) {
1026 PyObject *descr;
1027 if (PyDict_GetItemString(dict, meth->ml_name))
1028 continue;
1029 descr = PyDescr_NewMethod(type, meth);
1030 if (descr == NULL)
1031 return -1;
1032 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1033 return -1;
1034 Py_DECREF(descr);
1035 }
1036 return 0;
1037}
1038
1039static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040add_members(PyTypeObject *type, struct memberlist *memb)
1041{
1042 PyObject *dict = type->tp_defined;
1043
1044 for (; memb->name != NULL; memb++) {
1045 PyObject *descr;
1046 if (PyDict_GetItemString(dict, memb->name))
1047 continue;
1048 descr = PyDescr_NewMember(type, memb);
1049 if (descr == NULL)
1050 return -1;
1051 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1052 return -1;
1053 Py_DECREF(descr);
1054 }
1055 return 0;
1056}
1057
1058static int
1059add_getset(PyTypeObject *type, struct getsetlist *gsp)
1060{
1061 PyObject *dict = type->tp_defined;
1062
1063 for (; gsp->name != NULL; gsp++) {
1064 PyObject *descr;
1065 if (PyDict_GetItemString(dict, gsp->name))
1066 continue;
1067 descr = PyDescr_NewGetSet(type, gsp);
1068
1069 if (descr == NULL)
1070 return -1;
1071 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1072 return -1;
1073 Py_DECREF(descr);
1074 }
1075 return 0;
1076}
1077
Guido van Rossum13d52f02001-08-10 21:24:08 +00001078static void
1079inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080{
1081 int oldsize, newsize;
1082
Guido van Rossum13d52f02001-08-10 21:24:08 +00001083 /* Special flag magic */
1084 if (!type->tp_as_buffer && base->tp_as_buffer) {
1085 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1086 type->tp_flags |=
1087 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1088 }
1089 if (!type->tp_as_sequence && base->tp_as_sequence) {
1090 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1091 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1092 }
1093 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1094 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1095 if ((!type->tp_as_number && base->tp_as_number) ||
1096 (!type->tp_as_sequence && base->tp_as_sequence)) {
1097 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1098 if (!type->tp_as_number && !type->tp_as_sequence) {
1099 type->tp_flags |= base->tp_flags &
1100 Py_TPFLAGS_HAVE_INPLACEOPS;
1101 }
1102 }
1103 /* Wow */
1104 }
1105 if (!type->tp_as_number && base->tp_as_number) {
1106 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1107 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1108 }
1109
1110 /* Copying basicsize is connected to the GC flags */
1111 oldsize = PyType_BASICSIZE(base);
1112 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1113 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1114 (base->tp_flags & Py_TPFLAGS_GC) &&
1115 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1116 (!type->tp_traverse && !type->tp_clear)) {
1117 type->tp_flags |= Py_TPFLAGS_GC;
1118 if (type->tp_traverse == NULL)
1119 type->tp_traverse = base->tp_traverse;
1120 if (type->tp_clear == NULL)
1121 type->tp_clear = base->tp_clear;
1122 }
1123 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1124 if (base != &PyBaseObject_Type ||
1125 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1126 if (type->tp_new == NULL)
1127 type->tp_new = base->tp_new;
1128 }
1129 }
1130 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001131
1132 /* Copy other non-function slots */
1133
1134#undef COPYVAL
1135#define COPYVAL(SLOT) \
1136 if (type->SLOT == 0) type->SLOT = base->SLOT
1137
1138 COPYVAL(tp_itemsize);
1139 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1140 COPYVAL(tp_weaklistoffset);
1141 }
1142 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1143 COPYVAL(tp_dictoffset);
1144 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001145}
1146
1147static void
1148inherit_slots(PyTypeObject *type, PyTypeObject *base)
1149{
1150 PyTypeObject *basebase;
1151
1152#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001153#undef COPYSLOT
1154#undef COPYNUM
1155#undef COPYSEQ
1156#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001157
1158#define SLOTDEFINED(SLOT) \
1159 (base->SLOT != 0 && \
1160 (basebase == NULL || base->SLOT != basebase->SLOT))
1161
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001163 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001164
1165#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1166#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1167#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1168
Guido van Rossum13d52f02001-08-10 21:24:08 +00001169 /* This won't inherit indirect slots (from tp_as_number etc.)
1170 if type doesn't provide the space. */
1171
1172 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1173 basebase = base->tp_base;
1174 if (basebase->tp_as_number == NULL)
1175 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001176 COPYNUM(nb_add);
1177 COPYNUM(nb_subtract);
1178 COPYNUM(nb_multiply);
1179 COPYNUM(nb_divide);
1180 COPYNUM(nb_remainder);
1181 COPYNUM(nb_divmod);
1182 COPYNUM(nb_power);
1183 COPYNUM(nb_negative);
1184 COPYNUM(nb_positive);
1185 COPYNUM(nb_absolute);
1186 COPYNUM(nb_nonzero);
1187 COPYNUM(nb_invert);
1188 COPYNUM(nb_lshift);
1189 COPYNUM(nb_rshift);
1190 COPYNUM(nb_and);
1191 COPYNUM(nb_xor);
1192 COPYNUM(nb_or);
1193 COPYNUM(nb_coerce);
1194 COPYNUM(nb_int);
1195 COPYNUM(nb_long);
1196 COPYNUM(nb_float);
1197 COPYNUM(nb_oct);
1198 COPYNUM(nb_hex);
1199 COPYNUM(nb_inplace_add);
1200 COPYNUM(nb_inplace_subtract);
1201 COPYNUM(nb_inplace_multiply);
1202 COPYNUM(nb_inplace_divide);
1203 COPYNUM(nb_inplace_remainder);
1204 COPYNUM(nb_inplace_power);
1205 COPYNUM(nb_inplace_lshift);
1206 COPYNUM(nb_inplace_rshift);
1207 COPYNUM(nb_inplace_and);
1208 COPYNUM(nb_inplace_xor);
1209 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001210 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1211 COPYNUM(nb_true_divide);
1212 COPYNUM(nb_floor_divide);
1213 COPYNUM(nb_inplace_true_divide);
1214 COPYNUM(nb_inplace_floor_divide);
1215 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001216 }
1217
Guido van Rossum13d52f02001-08-10 21:24:08 +00001218 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1219 basebase = base->tp_base;
1220 if (basebase->tp_as_sequence == NULL)
1221 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222 COPYSEQ(sq_length);
1223 COPYSEQ(sq_concat);
1224 COPYSEQ(sq_repeat);
1225 COPYSEQ(sq_item);
1226 COPYSEQ(sq_slice);
1227 COPYSEQ(sq_ass_item);
1228 COPYSEQ(sq_ass_slice);
1229 COPYSEQ(sq_contains);
1230 COPYSEQ(sq_inplace_concat);
1231 COPYSEQ(sq_inplace_repeat);
1232 }
1233
Guido van Rossum13d52f02001-08-10 21:24:08 +00001234 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1235 basebase = base->tp_base;
1236 if (basebase->tp_as_mapping == NULL)
1237 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238 COPYMAP(mp_length);
1239 COPYMAP(mp_subscript);
1240 COPYMAP(mp_ass_subscript);
1241 }
1242
Guido van Rossum13d52f02001-08-10 21:24:08 +00001243 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001244
Tim Peters6d6c1a32001-08-02 04:15:00 +00001245 COPYSLOT(tp_dealloc);
1246 COPYSLOT(tp_print);
1247 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1248 type->tp_getattr = base->tp_getattr;
1249 type->tp_getattro = base->tp_getattro;
1250 }
1251 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1252 type->tp_setattr = base->tp_setattr;
1253 type->tp_setattro = base->tp_setattro;
1254 }
1255 /* tp_compare see tp_richcompare */
1256 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001257 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001258 COPYSLOT(tp_call);
1259 COPYSLOT(tp_str);
1260 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001262 if (type->tp_compare == NULL &&
1263 type->tp_richcompare == NULL &&
1264 type->tp_hash == NULL)
1265 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266 type->tp_compare = base->tp_compare;
1267 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001268 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001269 }
1270 }
1271 else {
1272 COPYSLOT(tp_compare);
1273 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001274 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1275 COPYSLOT(tp_iter);
1276 COPYSLOT(tp_iternext);
1277 }
1278 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1279 COPYSLOT(tp_descr_get);
1280 COPYSLOT(tp_descr_set);
1281 COPYSLOT(tp_dictoffset);
1282 COPYSLOT(tp_init);
1283 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001284 COPYSLOT(tp_free);
1285 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001286}
1287
Guido van Rossum13d52f02001-08-10 21:24:08 +00001288staticforward int add_operators(PyTypeObject *);
1289
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001291PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292{
1293 PyObject *dict, *bases, *x;
1294 PyTypeObject *base;
1295 int i, n;
1296
Guido van Rossumd614f972001-08-10 17:39:49 +00001297 if (type->tp_flags & Py_TPFLAGS_READY) {
1298 assert(type->tp_dict != NULL);
1299 return 0;
1300 }
1301 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1302 assert(type->tp_dict == NULL);
1303
1304 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305
1306 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1307 base = type->tp_base;
1308 if (base == NULL && type != &PyBaseObject_Type)
1309 base = type->tp_base = &PyBaseObject_Type;
1310
1311 /* Initialize tp_bases */
1312 bases = type->tp_bases;
1313 if (bases == NULL) {
1314 if (base == NULL)
1315 bases = PyTuple_New(0);
1316 else
1317 bases = Py_BuildValue("(O)", base);
1318 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001319 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001320 type->tp_bases = bases;
1321 }
1322
1323 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001324 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001325 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001326 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001327 }
1328
1329 /* Initialize tp_defined */
1330 dict = type->tp_defined;
1331 if (dict == NULL) {
1332 dict = PyDict_New();
1333 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001334 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001335 type->tp_defined = dict;
1336 }
1337
1338 /* Add type-specific descriptors to tp_defined */
1339 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001340 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341 if (type->tp_methods != NULL) {
1342 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001343 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001344 }
1345 if (type->tp_members != NULL) {
1346 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001347 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001348 }
1349 if (type->tp_getset != NULL) {
1350 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001351 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 }
1353
1354 /* Temporarily make tp_dict the same object as tp_defined.
1355 (This is needed to call mro(), and can stay this way for
1356 dynamic types). */
1357 Py_INCREF(type->tp_defined);
1358 type->tp_dict = type->tp_defined;
1359
1360 /* Calculate method resolution order */
1361 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001362 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001363 }
1364
Guido van Rossum13d52f02001-08-10 21:24:08 +00001365 /* Inherit special flags from dominant base */
1366 if (type->tp_base != NULL)
1367 inherit_special(type, type->tp_base);
1368
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001370 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001371 /* For a dynamic type, all slots are overridden */
1372 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001373 }
1374 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001375 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001376 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001377 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001378 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001379 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001380 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001381 bases = type->tp_mro;
1382 assert(bases != NULL);
1383 assert(PyTuple_Check(bases));
1384 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001385 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1387 assert(PyType_Check(base));
1388 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001389 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001390 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001391 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001392 }
1393 }
1394
Guido van Rossum13d52f02001-08-10 21:24:08 +00001395 /* Some more special stuff */
1396 base = type->tp_base;
1397 if (base != NULL) {
1398 if (type->tp_as_number == NULL)
1399 type->tp_as_number = base->tp_as_number;
1400 if (type->tp_as_sequence == NULL)
1401 type->tp_as_sequence = base->tp_as_sequence;
1402 if (type->tp_as_mapping == NULL)
1403 type->tp_as_mapping = base->tp_as_mapping;
1404 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001405
Guido van Rossum13d52f02001-08-10 21:24:08 +00001406 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001407 assert(type->tp_dict != NULL);
1408 type->tp_flags =
1409 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001410 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001411
1412 error:
1413 type->tp_flags &= ~Py_TPFLAGS_READYING;
1414 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001415}
1416
1417
1418/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1419
1420/* There's a wrapper *function* for each distinct function typedef used
1421 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1422 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1423 Most tables have only one entry; the tables for binary operators have two
1424 entries, one regular and one with reversed arguments. */
1425
1426static PyObject *
1427wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1428{
1429 inquiry func = (inquiry)wrapped;
1430 int res;
1431
1432 if (!PyArg_ParseTuple(args, ""))
1433 return NULL;
1434 res = (*func)(self);
1435 if (res == -1 && PyErr_Occurred())
1436 return NULL;
1437 return PyInt_FromLong((long)res);
1438}
1439
1440static struct wrapperbase tab_len[] = {
1441 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1442 {0}
1443};
1444
1445static PyObject *
1446wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1447{
1448 binaryfunc func = (binaryfunc)wrapped;
1449 PyObject *other;
1450
1451 if (!PyArg_ParseTuple(args, "O", &other))
1452 return NULL;
1453 return (*func)(self, other);
1454}
1455
1456static PyObject *
1457wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1458{
1459 binaryfunc func = (binaryfunc)wrapped;
1460 PyObject *other;
1461
1462 if (!PyArg_ParseTuple(args, "O", &other))
1463 return NULL;
1464 return (*func)(other, self);
1465}
1466
1467#undef BINARY
1468#define BINARY(NAME, OP) \
1469static struct wrapperbase tab_##NAME[] = { \
1470 {"__" #NAME "__", \
1471 (wrapperfunc)wrap_binaryfunc, \
1472 "x.__" #NAME "__(y) <==> " #OP}, \
1473 {"__r" #NAME "__", \
1474 (wrapperfunc)wrap_binaryfunc_r, \
1475 "y.__r" #NAME "__(x) <==> " #OP}, \
1476 {0} \
1477}
1478
1479BINARY(add, "x+y");
1480BINARY(sub, "x-y");
1481BINARY(mul, "x*y");
1482BINARY(div, "x/y");
1483BINARY(mod, "x%y");
1484BINARY(divmod, "divmod(x,y)");
1485BINARY(lshift, "x<<y");
1486BINARY(rshift, "x>>y");
1487BINARY(and, "x&y");
1488BINARY(xor, "x^y");
1489BINARY(or, "x|y");
1490
1491static PyObject *
1492wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1493{
1494 ternaryfunc func = (ternaryfunc)wrapped;
1495 PyObject *other;
1496 PyObject *third = Py_None;
1497
1498 /* Note: This wrapper only works for __pow__() */
1499
1500 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1501 return NULL;
1502 return (*func)(self, other, third);
1503}
1504
1505#undef TERNARY
1506#define TERNARY(NAME, OP) \
1507static struct wrapperbase tab_##NAME[] = { \
1508 {"__" #NAME "__", \
1509 (wrapperfunc)wrap_ternaryfunc, \
1510 "x.__" #NAME "__(y, z) <==> " #OP}, \
1511 {"__r" #NAME "__", \
1512 (wrapperfunc)wrap_ternaryfunc, \
1513 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1514 {0} \
1515}
1516
1517TERNARY(pow, "(x**y) % z");
1518
1519#undef UNARY
1520#define UNARY(NAME, OP) \
1521static struct wrapperbase tab_##NAME[] = { \
1522 {"__" #NAME "__", \
1523 (wrapperfunc)wrap_unaryfunc, \
1524 "x.__" #NAME "__() <==> " #OP}, \
1525 {0} \
1526}
1527
1528static PyObject *
1529wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1530{
1531 unaryfunc func = (unaryfunc)wrapped;
1532
1533 if (!PyArg_ParseTuple(args, ""))
1534 return NULL;
1535 return (*func)(self);
1536}
1537
1538UNARY(neg, "-x");
1539UNARY(pos, "+x");
1540UNARY(abs, "abs(x)");
1541UNARY(nonzero, "x != 0");
1542UNARY(invert, "~x");
1543UNARY(int, "int(x)");
1544UNARY(long, "long(x)");
1545UNARY(float, "float(x)");
1546UNARY(oct, "oct(x)");
1547UNARY(hex, "hex(x)");
1548
1549#undef IBINARY
1550#define IBINARY(NAME, OP) \
1551static struct wrapperbase tab_##NAME[] = { \
1552 {"__" #NAME "__", \
1553 (wrapperfunc)wrap_binaryfunc, \
1554 "x.__" #NAME "__(y) <==> " #OP}, \
1555 {0} \
1556}
1557
1558IBINARY(iadd, "x+=y");
1559IBINARY(isub, "x-=y");
1560IBINARY(imul, "x*=y");
1561IBINARY(idiv, "x/=y");
1562IBINARY(imod, "x%=y");
1563IBINARY(ilshift, "x<<=y");
1564IBINARY(irshift, "x>>=y");
1565IBINARY(iand, "x&=y");
1566IBINARY(ixor, "x^=y");
1567IBINARY(ior, "x|=y");
1568
1569#undef ITERNARY
1570#define ITERNARY(NAME, OP) \
1571static struct wrapperbase tab_##NAME[] = { \
1572 {"__" #NAME "__", \
1573 (wrapperfunc)wrap_ternaryfunc, \
1574 "x.__" #NAME "__(y) <==> " #OP}, \
1575 {0} \
1576}
1577
1578ITERNARY(ipow, "x = (x**y) % z");
1579
1580static struct wrapperbase tab_getitem[] = {
1581 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1582 "x.__getitem__(y) <==> x[y]"},
1583 {0}
1584};
1585
1586static PyObject *
1587wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1588{
1589 intargfunc func = (intargfunc)wrapped;
1590 int i;
1591
1592 if (!PyArg_ParseTuple(args, "i", &i))
1593 return NULL;
1594 return (*func)(self, i);
1595}
1596
1597static struct wrapperbase tab_mul_int[] = {
1598 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1599 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1600 {0}
1601};
1602
1603static struct wrapperbase tab_concat[] = {
1604 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1605 {0}
1606};
1607
1608static struct wrapperbase tab_imul_int[] = {
1609 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1610 {0}
1611};
1612
1613static struct wrapperbase tab_getitem_int[] = {
1614 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1615 "x.__getitem__(i) <==> x[i]"},
1616 {0}
1617};
1618
1619static PyObject *
1620wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1621{
1622 intintargfunc func = (intintargfunc)wrapped;
1623 int i, j;
1624
1625 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1626 return NULL;
1627 return (*func)(self, i, j);
1628}
1629
1630static struct wrapperbase tab_getslice[] = {
1631 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1632 "x.__getslice__(i, j) <==> x[i:j]"},
1633 {0}
1634};
1635
1636static PyObject *
1637wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1638{
1639 intobjargproc func = (intobjargproc)wrapped;
1640 int i, res;
1641 PyObject *value;
1642
1643 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1644 return NULL;
1645 res = (*func)(self, i, value);
1646 if (res == -1 && PyErr_Occurred())
1647 return NULL;
1648 Py_INCREF(Py_None);
1649 return Py_None;
1650}
1651
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001652static PyObject *
1653wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1654{
1655 intobjargproc func = (intobjargproc)wrapped;
1656 int i, res;
1657
1658 if (!PyArg_ParseTuple(args, "i", &i))
1659 return NULL;
1660 res = (*func)(self, i, NULL);
1661 if (res == -1 && PyErr_Occurred())
1662 return NULL;
1663 Py_INCREF(Py_None);
1664 return Py_None;
1665}
1666
Tim Peters6d6c1a32001-08-02 04:15:00 +00001667static struct wrapperbase tab_setitem_int[] = {
1668 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1669 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001670 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1671 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001672 {0}
1673};
1674
1675static PyObject *
1676wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1677{
1678 intintobjargproc func = (intintobjargproc)wrapped;
1679 int i, j, res;
1680 PyObject *value;
1681
1682 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1683 return NULL;
1684 res = (*func)(self, i, j, value);
1685 if (res == -1 && PyErr_Occurred())
1686 return NULL;
1687 Py_INCREF(Py_None);
1688 return Py_None;
1689}
1690
1691static struct wrapperbase tab_setslice[] = {
1692 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1693 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1694 {0}
1695};
1696
1697/* XXX objobjproc is a misnomer; should be objargpred */
1698static PyObject *
1699wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1700{
1701 objobjproc func = (objobjproc)wrapped;
1702 int res;
1703 PyObject *value;
1704
1705 if (!PyArg_ParseTuple(args, "O", &value))
1706 return NULL;
1707 res = (*func)(self, value);
1708 if (res == -1 && PyErr_Occurred())
1709 return NULL;
1710 return PyInt_FromLong((long)res);
1711}
1712
1713static struct wrapperbase tab_contains[] = {
1714 {"__contains__", (wrapperfunc)wrap_objobjproc,
1715 "x.__contains__(y) <==> y in x"},
1716 {0}
1717};
1718
1719static PyObject *
1720wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1721{
1722 objobjargproc func = (objobjargproc)wrapped;
1723 int res;
1724 PyObject *key, *value;
1725
1726 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1727 return NULL;
1728 res = (*func)(self, key, value);
1729 if (res == -1 && PyErr_Occurred())
1730 return NULL;
1731 Py_INCREF(Py_None);
1732 return Py_None;
1733}
1734
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001735static PyObject *
1736wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1737{
1738 objobjargproc func = (objobjargproc)wrapped;
1739 int res;
1740 PyObject *key;
1741
1742 if (!PyArg_ParseTuple(args, "O", &key))
1743 return NULL;
1744 res = (*func)(self, key, NULL);
1745 if (res == -1 && PyErr_Occurred())
1746 return NULL;
1747 Py_INCREF(Py_None);
1748 return Py_None;
1749}
1750
Tim Peters6d6c1a32001-08-02 04:15:00 +00001751static struct wrapperbase tab_setitem[] = {
1752 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1753 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001754 {"__delitem__", (wrapperfunc)wrap_delitem,
1755 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001756 {0}
1757};
1758
1759static PyObject *
1760wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1761{
1762 cmpfunc func = (cmpfunc)wrapped;
1763 int res;
1764 PyObject *other;
1765
1766 if (!PyArg_ParseTuple(args, "O", &other))
1767 return NULL;
1768 res = (*func)(self, other);
1769 if (PyErr_Occurred())
1770 return NULL;
1771 return PyInt_FromLong((long)res);
1772}
1773
1774static struct wrapperbase tab_cmp[] = {
1775 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1776 "x.__cmp__(y) <==> cmp(x,y)"},
1777 {0}
1778};
1779
1780static struct wrapperbase tab_repr[] = {
1781 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1782 "x.__repr__() <==> repr(x)"},
1783 {0}
1784};
1785
1786static struct wrapperbase tab_getattr[] = {
1787 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1788 "x.__getattr__('name') <==> x.name"},
1789 {0}
1790};
1791
1792static PyObject *
1793wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1794{
1795 setattrofunc func = (setattrofunc)wrapped;
1796 int res;
1797 PyObject *name, *value;
1798
1799 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1800 return NULL;
1801 res = (*func)(self, name, value);
1802 if (res < 0)
1803 return NULL;
1804 Py_INCREF(Py_None);
1805 return Py_None;
1806}
1807
1808static PyObject *
1809wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1810{
1811 setattrofunc func = (setattrofunc)wrapped;
1812 int res;
1813 PyObject *name;
1814
1815 if (!PyArg_ParseTuple(args, "O", &name))
1816 return NULL;
1817 res = (*func)(self, name, NULL);
1818 if (res < 0)
1819 return NULL;
1820 Py_INCREF(Py_None);
1821 return Py_None;
1822}
1823
1824static struct wrapperbase tab_setattr[] = {
1825 {"__setattr__", (wrapperfunc)wrap_setattr,
1826 "x.__setattr__('name', value) <==> x.name = value"},
1827 {"__delattr__", (wrapperfunc)wrap_delattr,
1828 "x.__delattr__('name') <==> del x.name"},
1829 {0}
1830};
1831
1832static PyObject *
1833wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1834{
1835 hashfunc func = (hashfunc)wrapped;
1836 long res;
1837
1838 if (!PyArg_ParseTuple(args, ""))
1839 return NULL;
1840 res = (*func)(self);
1841 if (res == -1 && PyErr_Occurred())
1842 return NULL;
1843 return PyInt_FromLong(res);
1844}
1845
1846static struct wrapperbase tab_hash[] = {
1847 {"__hash__", (wrapperfunc)wrap_hashfunc,
1848 "x.__hash__() <==> hash(x)"},
1849 {0}
1850};
1851
1852static PyObject *
1853wrap_call(PyObject *self, PyObject *args, void *wrapped)
1854{
1855 ternaryfunc func = (ternaryfunc)wrapped;
1856
1857 /* XXX What about keyword arguments? */
1858 return (*func)(self, args, NULL);
1859}
1860
1861static struct wrapperbase tab_call[] = {
1862 {"__call__", (wrapperfunc)wrap_call,
1863 "x.__call__(...) <==> x(...)"},
1864 {0}
1865};
1866
1867static struct wrapperbase tab_str[] = {
1868 {"__str__", (wrapperfunc)wrap_unaryfunc,
1869 "x.__str__() <==> str(x)"},
1870 {0}
1871};
1872
1873static PyObject *
1874wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1875{
1876 richcmpfunc func = (richcmpfunc)wrapped;
1877 PyObject *other;
1878
1879 if (!PyArg_ParseTuple(args, "O", &other))
1880 return NULL;
1881 return (*func)(self, other, op);
1882}
1883
1884#undef RICHCMP_WRAPPER
1885#define RICHCMP_WRAPPER(NAME, OP) \
1886static PyObject * \
1887richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1888{ \
1889 return wrap_richcmpfunc(self, args, wrapped, OP); \
1890}
1891
Jack Jansen8e938b42001-08-08 15:29:49 +00001892RICHCMP_WRAPPER(lt, Py_LT)
1893RICHCMP_WRAPPER(le, Py_LE)
1894RICHCMP_WRAPPER(eq, Py_EQ)
1895RICHCMP_WRAPPER(ne, Py_NE)
1896RICHCMP_WRAPPER(gt, Py_GT)
1897RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001898
1899#undef RICHCMP_ENTRY
1900#define RICHCMP_ENTRY(NAME, EXPR) \
1901 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1902 "x.__" #NAME "__(y) <==> " EXPR}
1903
1904static struct wrapperbase tab_richcmp[] = {
1905 RICHCMP_ENTRY(lt, "x<y"),
1906 RICHCMP_ENTRY(le, "x<=y"),
1907 RICHCMP_ENTRY(eq, "x==y"),
1908 RICHCMP_ENTRY(ne, "x!=y"),
1909 RICHCMP_ENTRY(gt, "x>y"),
1910 RICHCMP_ENTRY(ge, "x>=y"),
1911 {0}
1912};
1913
1914static struct wrapperbase tab_iter[] = {
1915 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1916 {0}
1917};
1918
1919static PyObject *
1920wrap_next(PyObject *self, PyObject *args, void *wrapped)
1921{
1922 unaryfunc func = (unaryfunc)wrapped;
1923 PyObject *res;
1924
1925 if (!PyArg_ParseTuple(args, ""))
1926 return NULL;
1927 res = (*func)(self);
1928 if (res == NULL && !PyErr_Occurred())
1929 PyErr_SetNone(PyExc_StopIteration);
1930 return res;
1931}
1932
1933static struct wrapperbase tab_next[] = {
1934 {"next", (wrapperfunc)wrap_next,
1935 "x.next() -> the next value, or raise StopIteration"},
1936 {0}
1937};
1938
1939static PyObject *
1940wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1941{
1942 descrgetfunc func = (descrgetfunc)wrapped;
1943 PyObject *obj;
1944 PyObject *type = NULL;
1945
1946 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1947 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001948 return (*func)(self, obj, type);
1949}
1950
1951static struct wrapperbase tab_descr_get[] = {
1952 {"__get__", (wrapperfunc)wrap_descr_get,
1953 "descr.__get__(obj, type) -> value"},
1954 {0}
1955};
1956
1957static PyObject *
1958wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
1959{
1960 descrsetfunc func = (descrsetfunc)wrapped;
1961 PyObject *obj, *value;
1962 int ret;
1963
1964 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
1965 return NULL;
1966 ret = (*func)(self, obj, value);
1967 if (ret < 0)
1968 return NULL;
1969 Py_INCREF(Py_None);
1970 return Py_None;
1971}
1972
1973static struct wrapperbase tab_descr_set[] = {
1974 {"__set__", (wrapperfunc)wrap_descrsetfunc,
1975 "descr.__set__(obj, value)"},
1976 {0}
1977};
1978
1979static PyObject *
1980wrap_init(PyObject *self, PyObject *args, void *wrapped)
1981{
1982 initproc func = (initproc)wrapped;
1983
1984 /* XXX What about keyword arguments? */
1985 if (func(self, args, NULL) < 0)
1986 return NULL;
1987 Py_INCREF(Py_None);
1988 return Py_None;
1989}
1990
1991static struct wrapperbase tab_init[] = {
1992 {"__init__", (wrapperfunc)wrap_init,
1993 "x.__init__(...) initializes x; "
1994 "see x.__type__.__doc__ for signature"},
1995 {0}
1996};
1997
1998static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001999tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002000{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002001 PyTypeObject *type, *subtype;
2002 PyObject *arg0, *res;
2003
2004 if (self == NULL || !PyType_Check(self))
2005 Py_FatalError("__new__() called with non-type 'self'");
2006 type = (PyTypeObject *)self;
2007 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
2008 PyErr_SetString(PyExc_TypeError,
2009 "T.__new__(): not enough arguments");
2010 return NULL;
2011 }
2012 arg0 = PyTuple_GET_ITEM(args, 0);
2013 if (!PyType_Check(arg0)) {
2014 PyErr_SetString(PyExc_TypeError,
2015 "T.__new__(S): S is not a type object");
2016 return NULL;
2017 }
2018 subtype = (PyTypeObject *)arg0;
2019 if (!PyType_IsSubtype(subtype, type)) {
2020 PyErr_SetString(PyExc_TypeError,
2021 "T.__new__(S): S is not a subtype of T");
2022 return NULL;
2023 }
2024 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2025 if (args == NULL)
2026 return NULL;
2027 res = type->tp_new(subtype, args, kwds);
2028 Py_DECREF(args);
2029 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002030}
2031
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002032static struct PyMethodDef tp_new_methoddef[] = {
2033 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2034 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002035 {0}
2036};
2037
2038static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002039add_tp_new_wrapper(PyTypeObject *type)
2040{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002041 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002042
Guido van Rossumf040ede2001-08-07 16:40:56 +00002043 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2044 return 0;
2045 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002046 if (func == NULL)
2047 return -1;
2048 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2049}
2050
Guido van Rossum13d52f02001-08-10 21:24:08 +00002051static int
2052add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2053{
2054 PyObject *dict = type->tp_defined;
2055
2056 for (; wraps->name != NULL; wraps++) {
2057 PyObject *descr;
2058 if (PyDict_GetItemString(dict, wraps->name))
2059 continue;
2060 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2061 if (descr == NULL)
2062 return -1;
2063 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2064 return -1;
2065 Py_DECREF(descr);
2066 }
2067 return 0;
2068}
2069
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002070/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002071 dictionary with method descriptors for function slots. For each
2072 function slot (like tp_repr) that's defined in the type, one or
2073 more corresponding descriptors are added in the type's tp_defined
2074 dictionary under the appropriate name (like __repr__). Some
2075 function slots cause more than one descriptor to be added (for
2076 example, the nb_add slot adds both __add__ and __radd__
2077 descriptors) and some function slots compete for the same
2078 descriptor (for example both sq_item and mp_subscript generate a
2079 __getitem__ descriptor). This only adds new descriptors and
2080 doesn't overwrite entries in tp_defined that were previously
2081 defined. The descriptors contain a reference to the C function
2082 they must call, so that it's safe if they are copied into a
2083 subtype's __dict__ and the subtype has a different C function in
2084 its slot -- calling the method defined by the descriptor will call
2085 the C function that was used to create it, rather than the C
2086 function present in the slot when it is called. (This is important
2087 because a subtype may have a C function in the slot that calls the
2088 method from the dictionary, and we want to avoid infinite recursion
2089 here.) */
2090
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002091static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002092add_operators(PyTypeObject *type)
2093{
2094 PySequenceMethods *sq;
2095 PyMappingMethods *mp;
2096 PyNumberMethods *nb;
2097
2098#undef ADD
2099#define ADD(SLOT, TABLE) \
2100 if (SLOT) { \
2101 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2102 return -1; \
2103 }
2104
2105 if ((sq = type->tp_as_sequence) != NULL) {
2106 ADD(sq->sq_length, tab_len);
2107 ADD(sq->sq_concat, tab_concat);
2108 ADD(sq->sq_repeat, tab_mul_int);
2109 ADD(sq->sq_item, tab_getitem_int);
2110 ADD(sq->sq_slice, tab_getslice);
2111 ADD(sq->sq_ass_item, tab_setitem_int);
2112 ADD(sq->sq_ass_slice, tab_setslice);
2113 ADD(sq->sq_contains, tab_contains);
2114 ADD(sq->sq_inplace_concat, tab_iadd);
2115 ADD(sq->sq_inplace_repeat, tab_imul_int);
2116 }
2117
2118 if ((mp = type->tp_as_mapping) != NULL) {
2119 if (sq->sq_length == NULL)
2120 ADD(mp->mp_length, tab_len);
2121 ADD(mp->mp_subscript, tab_getitem);
2122 ADD(mp->mp_ass_subscript, tab_setitem);
2123 }
2124
2125 /* We don't support "old-style numbers" because their binary
2126 operators require that both arguments have the same type;
2127 the wrappers here only work for new-style numbers. */
2128 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2129 (nb = type->tp_as_number) != NULL) {
2130 ADD(nb->nb_add, tab_add);
2131 ADD(nb->nb_subtract, tab_sub);
2132 ADD(nb->nb_multiply, tab_mul);
2133 ADD(nb->nb_divide, tab_div);
2134 ADD(nb->nb_remainder, tab_mod);
2135 ADD(nb->nb_divmod, tab_divmod);
2136 ADD(nb->nb_power, tab_pow);
2137 ADD(nb->nb_negative, tab_neg);
2138 ADD(nb->nb_positive, tab_pos);
2139 ADD(nb->nb_absolute, tab_abs);
2140 ADD(nb->nb_nonzero, tab_nonzero);
2141 ADD(nb->nb_invert, tab_invert);
2142 ADD(nb->nb_lshift, tab_lshift);
2143 ADD(nb->nb_rshift, tab_rshift);
2144 ADD(nb->nb_and, tab_and);
2145 ADD(nb->nb_xor, tab_xor);
2146 ADD(nb->nb_or, tab_or);
2147 /* We don't support coerce() -- see above comment */
2148 ADD(nb->nb_int, tab_int);
2149 ADD(nb->nb_long, tab_long);
2150 ADD(nb->nb_float, tab_float);
2151 ADD(nb->nb_oct, tab_oct);
2152 ADD(nb->nb_hex, tab_hex);
2153 ADD(nb->nb_inplace_add, tab_iadd);
2154 ADD(nb->nb_inplace_subtract, tab_isub);
2155 ADD(nb->nb_inplace_multiply, tab_imul);
2156 ADD(nb->nb_inplace_divide, tab_idiv);
2157 ADD(nb->nb_inplace_remainder, tab_imod);
2158 ADD(nb->nb_inplace_power, tab_ipow);
2159 ADD(nb->nb_inplace_lshift, tab_ilshift);
2160 ADD(nb->nb_inplace_rshift, tab_irshift);
2161 ADD(nb->nb_inplace_and, tab_iand);
2162 ADD(nb->nb_inplace_xor, tab_ixor);
2163 ADD(nb->nb_inplace_or, tab_ior);
2164 }
2165
2166 ADD(type->tp_getattro, tab_getattr);
2167 ADD(type->tp_setattro, tab_setattr);
2168 ADD(type->tp_compare, tab_cmp);
2169 ADD(type->tp_repr, tab_repr);
2170 ADD(type->tp_hash, tab_hash);
2171 ADD(type->tp_call, tab_call);
2172 ADD(type->tp_str, tab_str);
2173 ADD(type->tp_richcompare, tab_richcmp);
2174 ADD(type->tp_iter, tab_iter);
2175 ADD(type->tp_iternext, tab_next);
2176 ADD(type->tp_descr_get, tab_descr_get);
2177 ADD(type->tp_descr_set, tab_descr_set);
2178 ADD(type->tp_init, tab_init);
2179
Guido van Rossumf040ede2001-08-07 16:40:56 +00002180 if (type->tp_new != NULL) {
2181 if (add_tp_new_wrapper(type) < 0)
2182 return -1;
2183 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002184
2185 return 0;
2186}
2187
Guido van Rossumf040ede2001-08-07 16:40:56 +00002188/* Slot wrappers that call the corresponding __foo__ slot. See comments
2189 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002190
Guido van Rossumdc91b992001-08-08 22:26:22 +00002191#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002192static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002193FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002194{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002195 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002196}
2197
Guido van Rossumdc91b992001-08-08 22:26:22 +00002198#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002200FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002201{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002202 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002203}
2204
Guido van Rossumdc91b992001-08-08 22:26:22 +00002205
2206#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002207static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002208FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002209{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002210 if (self->ob_type->tp_as_number != NULL && \
2211 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2212 PyObject *r; \
2213 r = PyObject_CallMethod( \
2214 self, OPSTR, "O", other); \
2215 if (r != Py_NotImplemented || \
2216 other->ob_type == self->ob_type) \
2217 return r; \
2218 Py_DECREF(r); \
2219 } \
2220 if (other->ob_type->tp_as_number != NULL && \
2221 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2222 return PyObject_CallMethod( \
2223 other, ROPSTR, "O", self); \
2224 } \
2225 Py_INCREF(Py_NotImplemented); \
2226 return Py_NotImplemented; \
2227}
2228
2229#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2230 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2231
2232#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2233static PyObject * \
2234FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2235{ \
2236 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002237}
2238
2239static int
2240slot_sq_length(PyObject *self)
2241{
2242 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2243
2244 if (res == NULL)
2245 return -1;
2246 return (int)PyInt_AsLong(res);
2247}
2248
Guido van Rossumdc91b992001-08-08 22:26:22 +00002249SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2250SLOT1(slot_sq_repeat, "__mul__", int, "i")
2251SLOT1(slot_sq_item, "__getitem__", int, "i")
2252SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002253
2254static int
2255slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2256{
2257 PyObject *res;
2258
2259 if (value == NULL)
2260 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2261 else
2262 res = PyObject_CallMethod(self, "__setitem__",
2263 "iO", index, value);
2264 if (res == NULL)
2265 return -1;
2266 Py_DECREF(res);
2267 return 0;
2268}
2269
2270static int
2271slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2272{
2273 PyObject *res;
2274
2275 if (value == NULL)
2276 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2277 else
2278 res = PyObject_CallMethod(self, "__setslice__",
2279 "iiO", i, j, value);
2280 if (res == NULL)
2281 return -1;
2282 Py_DECREF(res);
2283 return 0;
2284}
2285
2286static int
2287slot_sq_contains(PyObject *self, PyObject *value)
2288{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002289 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002290
Guido van Rossumb8f63662001-08-15 23:57:02 +00002291 func = PyObject_GetAttrString(self, "__contains__");
2292
2293 if (func != NULL) {
2294 args = Py_BuildValue("(O)", value);
2295 if (args == NULL)
2296 res = NULL;
2297 else {
2298 res = PyEval_CallObject(func, args);
2299 Py_DECREF(args);
2300 }
2301 Py_DECREF(func);
2302 if (res == NULL)
2303 return -1;
2304 return PyObject_IsTrue(res);
2305 }
2306 else {
2307 PyErr_Clear();
2308 return _PySequence_IterContains(self, value);
2309 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002310}
2311
Guido van Rossumdc91b992001-08-08 22:26:22 +00002312SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2313SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314
2315#define slot_mp_length slot_sq_length
2316
Guido van Rossumdc91b992001-08-08 22:26:22 +00002317SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002318
2319static int
2320slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2321{
2322 PyObject *res;
2323
2324 if (value == NULL)
2325 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2326 else
2327 res = PyObject_CallMethod(self, "__setitem__",
2328 "OO", key, value);
2329 if (res == NULL)
2330 return -1;
2331 Py_DECREF(res);
2332 return 0;
2333}
2334
Guido van Rossumdc91b992001-08-08 22:26:22 +00002335SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2336SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2337SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2338SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2339SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2340SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2341
2342staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2343
2344SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2345 nb_power, "__pow__", "__rpow__")
2346
2347static PyObject *
2348slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2349{
2350 if (modulus == Py_None)
2351 return slot_nb_power_binary(self, other);
2352 /* Three-arg power doesn't use __rpow__ */
2353 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2354}
2355
2356SLOT0(slot_nb_negative, "__neg__")
2357SLOT0(slot_nb_positive, "__pos__")
2358SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002359
2360static int
2361slot_nb_nonzero(PyObject *self)
2362{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002363 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002364
Guido van Rossumb8f63662001-08-15 23:57:02 +00002365 func = PyObject_GetAttrString(self, "__nonzero__");
2366 if (func == NULL) {
2367 PyErr_Clear();
2368 func = PyObject_GetAttrString(self, "__len__");
2369 }
2370
2371 if (func != NULL) {
2372 res = PyEval_CallObject(func, NULL);
2373 Py_DECREF(func);
2374 if (res == NULL)
2375 return -1;
2376 return PyObject_IsTrue(res);
2377 }
2378 else {
2379 PyErr_Clear();
2380 return 1;
2381 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002382}
2383
Guido van Rossumdc91b992001-08-08 22:26:22 +00002384SLOT0(slot_nb_invert, "__invert__")
2385SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2386SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2387SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2388SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2389SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002390/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002391SLOT0(slot_nb_int, "__int__")
2392SLOT0(slot_nb_long, "__long__")
2393SLOT0(slot_nb_float, "__float__")
2394SLOT0(slot_nb_oct, "__oct__")
2395SLOT0(slot_nb_hex, "__hex__")
2396SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2397SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2398SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2399SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2400SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2401SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2402SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2403SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2404SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2405SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2406SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2407SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2408 "__floordiv__", "__rfloordiv__")
2409SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2410SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2411SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002412
2413static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002414half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002415{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002416 PyObject *func, *args, *res;
2417 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418
Guido van Rossumb8f63662001-08-15 23:57:02 +00002419 func = PyObject_GetAttrString(self, "__cmp__");
2420 if (func == NULL) {
2421 PyErr_Clear();
2422 }
2423 else {
2424 args = Py_BuildValue("(O)", other);
2425 if (args == NULL)
2426 res = NULL;
2427 else {
2428 res = PyObject_CallObject(func, args);
2429 Py_DECREF(args);
2430 }
2431 if (res != Py_NotImplemented) {
2432 if (res == NULL)
2433 return -2;
2434 c = PyInt_AsLong(res);
2435 Py_DECREF(res);
2436 if (c == -1 && PyErr_Occurred())
2437 return -2;
2438 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2439 }
2440 Py_DECREF(res);
2441 }
2442 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002443}
2444
Guido van Rossumb8f63662001-08-15 23:57:02 +00002445static int
2446slot_tp_compare(PyObject *self, PyObject *other)
2447{
2448 int c;
2449
2450 if (self->ob_type->tp_compare == slot_tp_compare) {
2451 c = half_compare(self, other);
2452 if (c <= 1)
2453 return c;
2454 }
2455 if (other->ob_type->tp_compare == slot_tp_compare) {
2456 c = half_compare(other, self);
2457 if (c < -1)
2458 return -2;
2459 if (c <= 1)
2460 return -c;
2461 }
2462 return (void *)self < (void *)other ? -1 :
2463 (void *)self > (void *)other ? 1 : 0;
2464}
2465
2466static PyObject *
2467slot_tp_repr(PyObject *self)
2468{
2469 PyObject *func, *res;
2470
2471 func = PyObject_GetAttrString(self, "__repr__");
2472 if (func != NULL) {
2473 res = PyEval_CallObject(func, NULL);
2474 Py_DECREF(func);
2475 return res;
2476 }
2477 else {
2478 char buf[120];
2479 PyErr_Clear();
2480 sprintf(buf, "<%.80s object at %p>",
2481 self->ob_type->tp_name, self);
2482 return PyString_FromString(buf);
2483 }
2484}
2485
2486static PyObject *
2487slot_tp_str(PyObject *self)
2488{
2489 PyObject *func, *res;
2490
2491 func = PyObject_GetAttrString(self, "__str__");
2492 if (func != NULL) {
2493 res = PyEval_CallObject(func, NULL);
2494 Py_DECREF(func);
2495 return res;
2496 }
2497 else {
2498 PyErr_Clear();
2499 return slot_tp_repr(self);
2500 }
2501}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002502
2503static long
2504slot_tp_hash(PyObject *self)
2505{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002506 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002507 long h;
2508
Guido van Rossumb8f63662001-08-15 23:57:02 +00002509 func = PyObject_GetAttrString(self, "__hash__");
2510
2511 if (func != NULL) {
2512 res = PyEval_CallObject(func, NULL);
2513 Py_DECREF(func);
2514 if (res == NULL)
2515 return -1;
2516 h = PyInt_AsLong(res);
2517 }
2518 else {
2519 PyErr_Clear();
2520 func = PyObject_GetAttrString(self, "__eq__");
2521 if (func == NULL) {
2522 PyErr_Clear();
2523 func = PyObject_GetAttrString(self, "__cmp__");
2524 }
2525 if (func != NULL) {
2526 Py_DECREF(func);
2527 PyErr_SetString(PyExc_TypeError, "unhashable type");
2528 return -1;
2529 }
2530 PyErr_Clear();
2531 h = _Py_HashPointer((void *)self);
2532 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002533 if (h == -1 && !PyErr_Occurred())
2534 h = -2;
2535 return h;
2536}
2537
2538static PyObject *
2539slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2540{
2541 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2542 PyObject *res;
2543
2544 if (meth == NULL)
2545 return NULL;
2546 res = PyObject_Call(meth, args, kwds);
2547 Py_DECREF(meth);
2548 return res;
2549}
2550
Tim Peters6d6c1a32001-08-02 04:15:00 +00002551static PyObject *
2552slot_tp_getattro(PyObject *self, PyObject *name)
2553{
2554 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002555 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002556 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002557
Guido van Rossum8e248182001-08-12 05:17:56 +00002558 if (getattr_str == NULL) {
2559 getattr_str = PyString_InternFromString("__getattr__");
2560 if (getattr_str == NULL)
2561 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002562 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002563 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002564 if (getattr == NULL) {
2565 /* Avoid further slowdowns */
2566 if (tp->tp_getattro == slot_tp_getattro)
2567 tp->tp_getattro = PyObject_GenericGetAttr;
2568 else
2569 fprintf(stderr, "huh?\n");
Guido van Rossum8e248182001-08-12 05:17:56 +00002570 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002571 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572 return PyObject_CallFunction(getattr, "OO", self, name);
2573}
2574
2575static int
2576slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2577{
2578 PyObject *res;
2579
2580 if (value == NULL)
2581 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2582 else
2583 res = PyObject_CallMethod(self, "__setattr__",
2584 "OO", name, value);
2585 if (res == NULL)
2586 return -1;
2587 Py_DECREF(res);
2588 return 0;
2589}
2590
2591/* Map rich comparison operators to their __xx__ namesakes */
2592static char *name_op[] = {
2593 "__lt__",
2594 "__le__",
2595 "__eq__",
2596 "__ne__",
2597 "__gt__",
2598 "__ge__",
2599};
2600
2601static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002602half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002603{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002604 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002605
Guido van Rossumb8f63662001-08-15 23:57:02 +00002606 func = PyObject_GetAttrString(self, name_op[op]);
2607 if (func == NULL) {
2608 PyErr_Clear();
2609 Py_INCREF(Py_NotImplemented);
2610 return Py_NotImplemented;
2611 }
2612 args = Py_BuildValue("(O)", other);
2613 if (args == NULL)
2614 res = NULL;
2615 else {
2616 res = PyObject_CallObject(func, args);
2617 Py_DECREF(args);
2618 }
2619 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002620 return res;
2621}
2622
Guido van Rossumb8f63662001-08-15 23:57:02 +00002623/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2624static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2625
2626static PyObject *
2627slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2628{
2629 PyObject *res;
2630
2631 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2632 res = half_richcompare(self, other, op);
2633 if (res != Py_NotImplemented)
2634 return res;
2635 Py_DECREF(res);
2636 }
2637 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2638 res = half_richcompare(other, self, swapped_op[op]);
2639 if (res != Py_NotImplemented) {
2640 return res;
2641 }
2642 Py_DECREF(res);
2643 }
2644 Py_INCREF(Py_NotImplemented);
2645 return Py_NotImplemented;
2646}
2647
2648static PyObject *
2649slot_tp_iter(PyObject *self)
2650{
2651 PyObject *func, *res;
2652
2653 func = PyObject_GetAttrString(self, "__iter__");
2654 if (func != NULL) {
2655 res = PyObject_CallObject(func, NULL);
2656 Py_DECREF(func);
2657 return res;
2658 }
2659 PyErr_Clear();
2660 func = PyObject_GetAttrString(self, "__getitem__");
2661 if (func == NULL) {
2662 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2663 return NULL;
2664 }
2665 Py_DECREF(func);
2666 return PySeqIter_New(self);
2667}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002668
2669static PyObject *
2670slot_tp_iternext(PyObject *self)
2671{
2672 return PyObject_CallMethod(self, "next", "");
2673}
2674
Guido van Rossumdc91b992001-08-08 22:26:22 +00002675SLOT2(slot_tp_descr_get, "__get__", PyObject *, PyObject *, "OO")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002676
2677static int
2678slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2679{
2680 PyObject *res = PyObject_CallMethod(self, "__set__",
2681 "OO", target, value);
2682 if (res == NULL)
2683 return -1;
2684 Py_DECREF(res);
2685 return 0;
2686}
2687
2688static int
2689slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2690{
2691 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2692 PyObject *res;
2693
2694 if (meth == NULL)
2695 return -1;
2696 res = PyObject_Call(meth, args, kwds);
2697 Py_DECREF(meth);
2698 if (res == NULL)
2699 return -1;
2700 Py_DECREF(res);
2701 return 0;
2702}
2703
2704static PyObject *
2705slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2706{
2707 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2708 PyObject *newargs, *x;
2709 int i, n;
2710
2711 if (func == NULL)
2712 return NULL;
2713 assert(PyTuple_Check(args));
2714 n = PyTuple_GET_SIZE(args);
2715 newargs = PyTuple_New(n+1);
2716 if (newargs == NULL)
2717 return NULL;
2718 Py_INCREF(type);
2719 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2720 for (i = 0; i < n; i++) {
2721 x = PyTuple_GET_ITEM(args, i);
2722 Py_INCREF(x);
2723 PyTuple_SET_ITEM(newargs, i+1, x);
2724 }
2725 x = PyObject_Call(func, newargs, kwds);
2726 Py_DECREF(func);
2727 return x;
2728}
2729
Guido van Rossumf040ede2001-08-07 16:40:56 +00002730/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002731 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002732 The dict argument is the dictionary argument passed to type_new(),
2733 which is the local namespace of the class statement, in other
2734 words, it contains the methods. For each special method (like
2735 __repr__) defined in the dictionary, the corresponding function
2736 slot in the type object (like tp_repr) is set to a special function
2737 whose name is 'slot_' followed by the slot name and whose signature
2738 is whatever is required for that slot. These slot functions look
2739 up the corresponding method in the type's dictionary and call it.
2740 The slot functions have to take care of the various peculiarities
2741 of the mapping between slots and special methods, such as mapping
2742 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2743 etc.) or mapping multiple slots to a single method (sq_item,
2744 mp_subscript <--> __getitem__). */
2745
Tim Peters6d6c1a32001-08-02 04:15:00 +00002746static void
2747override_slots(PyTypeObject *type, PyObject *dict)
2748{
2749 PySequenceMethods *sq = type->tp_as_sequence;
2750 PyMappingMethods *mp = type->tp_as_mapping;
2751 PyNumberMethods *nb = type->tp_as_number;
2752
Guido van Rossumdc91b992001-08-08 22:26:22 +00002753#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002754 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002755 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002756 }
2757
Guido van Rossumdc91b992001-08-08 22:26:22 +00002758#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002759 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002760 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002761 }
2762
Guido van Rossumdc91b992001-08-08 22:26:22 +00002763#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002764 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002765 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002766 }
2767
Guido van Rossumdc91b992001-08-08 22:26:22 +00002768#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002769 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002770 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002771 }
2772
Guido van Rossumdc91b992001-08-08 22:26:22 +00002773 SQSLOT("__len__", sq_length, slot_sq_length);
2774 SQSLOT("__add__", sq_concat, slot_sq_concat);
2775 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2776 SQSLOT("__getitem__", sq_item, slot_sq_item);
2777 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2778 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2779 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2780 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2781 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2782 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2783 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2784 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002785
Guido van Rossumdc91b992001-08-08 22:26:22 +00002786 MPSLOT("__len__", mp_length, slot_mp_length);
2787 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2788 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2789 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002790
Guido van Rossumdc91b992001-08-08 22:26:22 +00002791 NBSLOT("__add__", nb_add, slot_nb_add);
2792 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2793 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2794 NBSLOT("__div__", nb_divide, slot_nb_divide);
2795 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2796 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2797 NBSLOT("__pow__", nb_power, slot_nb_power);
2798 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2799 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2800 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2801 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2802 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2803 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2804 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2805 NBSLOT("__and__", nb_and, slot_nb_and);
2806 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2807 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002808 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002809 NBSLOT("__int__", nb_int, slot_nb_int);
2810 NBSLOT("__long__", nb_long, slot_nb_long);
2811 NBSLOT("__float__", nb_float, slot_nb_float);
2812 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2813 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2814 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2815 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2816 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2817 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2818 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2819 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2820 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2821 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2822 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2823 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2824 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2825 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2826 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2827 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2828 slot_nb_inplace_floor_divide);
2829 NBSLOT("__itruediv__", nb_inplace_true_divide,
2830 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002831
Guido van Rossum8e248182001-08-12 05:17:56 +00002832 if (dict == NULL ||
2833 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002834 PyDict_GetItemString(dict, "__repr__"))
2835 type->tp_print = NULL;
2836
Guido van Rossumdc91b992001-08-08 22:26:22 +00002837 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2838 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2839 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2840 TPSLOT("__call__", tp_call, slot_tp_call);
2841 TPSLOT("__str__", tp_str, slot_tp_str);
2842 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2843 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2844 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2845 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2846 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2847 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2848 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2849 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2850 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2851 TPSLOT("next", tp_iternext, slot_tp_iternext);
2852 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2853 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2854 TPSLOT("__init__", tp_init, slot_tp_init);
2855 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002856}