blob: 29cb2031c0fefc9e94d08c7c9eb392d9bb4d542b [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
Guido van Rossum1a493502001-08-17 16:47:50 +0000569 /* Should this be a dynamic class (i.e. modifiable __dict__)?
570 Look in two places for a variable named __dynamic__:
571 1) in the class dict
572 2) in the module dict (globals)
573 The first variable that is an int >= 0 is used.
574 Otherwise, a default is calculated from the base classes:
575 if any base class is dynamic, this class is dynamic; otherwise
576 it is static. */
577 dynamic = -1; /* Not yet determined */
578 /* Look in the class */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000579 tmp = PyDict_GetItemString(dict, "__dynamic__");
580 if (tmp != NULL) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000581 dynamic = PyInt_AsLong(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000582 if (dynamic < 0)
Guido van Rossum1a493502001-08-17 16:47:50 +0000583 PyErr_Clear();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000584 }
Guido van Rossum1a493502001-08-17 16:47:50 +0000585 if (dynamic < 0) {
586 /* Look in the module globals */
587 tmp = PyEval_GetGlobals();
588 if (tmp != NULL) {
589 tmp = PyDict_GetItemString(tmp, "__dynamic__");
590 if (tmp != NULL) {
591 dynamic = PyInt_AsLong(tmp);
592 if (dynamic < 0)
593 PyErr_Clear();
594 }
595 }
596 }
597 if (dynamic < 0) {
598 /* Make a new class dynamic if any of its bases is
599 dynamic. This is not always the same as inheriting
600 the __dynamic__ class attribute! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000601 dynamic = 0;
602 for (i = 0; i < nbases; i++) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000603 tmptype = (PyTypeObject *)
604 PyTuple_GET_ITEM(bases, i);
605 if (tmptype->tp_flags &
606 Py_TPFLAGS_DYNAMICTYPE) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000607 dynamic = 1;
608 break;
609 }
610 }
611 }
612
613 /* Check for a __slots__ sequence variable in dict, and count it */
614 slots = PyDict_GetItemString(dict, "__slots__");
615 nslots = 0;
616 if (slots != NULL) {
617 /* Make it into a tuple */
618 if (PyString_Check(slots))
619 slots = Py_BuildValue("(O)", slots);
620 else
621 slots = PySequence_Tuple(slots);
622 if (slots == NULL)
623 return NULL;
624 nslots = PyTuple_GET_SIZE(slots);
625 for (i = 0; i < nslots; i++) {
626 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
627 PyErr_SetString(PyExc_TypeError,
628 "__slots__ must be a sequence of strings");
629 Py_DECREF(slots);
630 return NULL;
631 }
632 }
633 }
634 if (slots == NULL && base->tp_dictoffset == 0 &&
635 (base->tp_setattro == PyObject_GenericSetAttr ||
636 base->tp_setattro == NULL))
637 nslots = 1;
638
639 /* XXX From here until type is safely allocated,
640 "return NULL" may leak slots! */
641
642 /* Allocate the type object */
643 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
644 if (type == NULL)
645 return NULL;
646
647 /* Keep name and slots alive in the extended type object */
648 et = (etype *)type;
649 Py_INCREF(name);
650 et->name = name;
651 et->slots = slots;
652
Guido van Rossumdc91b992001-08-08 22:26:22 +0000653 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000654 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
655 Py_TPFLAGS_BASETYPE;
656 if (dynamic)
657 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000658
659 /* It's a new-style number unless it specifically inherits any
660 old-style numeric behavior */
661 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
662 (base->tp_as_number == NULL))
663 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
664
665 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000666 type->tp_as_number = &et->as_number;
667 type->tp_as_sequence = &et->as_sequence;
668 type->tp_as_mapping = &et->as_mapping;
669 type->tp_as_buffer = &et->as_buffer;
670 type->tp_name = PyString_AS_STRING(name);
671
672 /* Set tp_base and tp_bases */
673 type->tp_bases = bases;
674 Py_INCREF(base);
675 type->tp_base = base;
676
677 /* Initialize tp_defined from passed-in dict */
678 type->tp_defined = dict = PyDict_Copy(dict);
679 if (dict == NULL) {
680 Py_DECREF(type);
681 return NULL;
682 }
683
Guido van Rossumc3542212001-08-16 09:18:56 +0000684 /* Set __module__ in the dict */
685 if (PyDict_GetItemString(dict, "__module__") == NULL) {
686 tmp = PyEval_GetGlobals();
687 if (tmp != NULL) {
688 tmp = PyDict_GetItemString(tmp, "__name__");
689 if (tmp != NULL) {
690 if (PyDict_SetItemString(dict, "__module__",
691 tmp) < 0)
692 return NULL;
693 }
694 }
695 }
696
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697 /* Special-case __new__: if it's a plain function,
698 make it a static function */
699 tmp = PyDict_GetItemString(dict, "__new__");
700 if (tmp != NULL && PyFunction_Check(tmp)) {
701 tmp = PyStaticMethod_New(tmp);
702 if (tmp == NULL) {
703 Py_DECREF(type);
704 return NULL;
705 }
706 PyDict_SetItemString(dict, "__new__", tmp);
707 Py_DECREF(tmp);
708 }
709
710 /* Add descriptors for custom slots from __slots__, or for __dict__ */
711 mp = et->members;
712 slotoffset = PyType_BASICSIZE(base);
713 if (slots != NULL) {
714 for (i = 0; i < nslots; i++, mp++) {
715 mp->name = PyString_AS_STRING(
716 PyTuple_GET_ITEM(slots, i));
717 mp->type = T_OBJECT;
718 mp->offset = slotoffset;
719 slotoffset += sizeof(PyObject *);
720 }
721 }
722 else if (nslots) {
723 type->tp_dictoffset = slotoffset;
724 mp->name = "__dict__";
725 mp->type = T_OBJECT;
726 mp->offset = slotoffset;
727 mp->readonly = 1;
728 slotoffset += sizeof(PyObject *);
729 }
730 type->tp_basicsize = slotoffset;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000731 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732
733 /* Special case some slots */
734 if (type->tp_dictoffset != 0 || nslots > 0) {
735 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
736 type->tp_getattro = PyObject_GenericGetAttr;
737 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
738 type->tp_setattro = PyObject_GenericSetAttr;
739 }
740 type->tp_dealloc = subtype_dealloc;
741
742 /* Always override allocation strategy to use regular heap */
743 type->tp_alloc = PyType_GenericAlloc;
744 type->tp_free = _PyObject_Del;
745
746 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000747 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000748 Py_DECREF(type);
749 return NULL;
750 }
751
752 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000753 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
754 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000755
Tim Peters6d6c1a32001-08-02 04:15:00 +0000756 return (PyObject *)type;
757}
758
759/* Internal API to look for a name through the MRO.
760 This returns a borrowed reference, and doesn't set an exception! */
761PyObject *
762_PyType_Lookup(PyTypeObject *type, PyObject *name)
763{
764 int i, n;
765 PyObject *mro, *res, *dict;
766
767 /* For static types, look in tp_dict */
768 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
769 dict = type->tp_dict;
770 assert(dict && PyDict_Check(dict));
771 return PyDict_GetItem(dict, name);
772 }
773
774 /* For dynamic types, look in tp_defined of types in MRO */
775 mro = type->tp_mro;
776 assert(PyTuple_Check(mro));
777 n = PyTuple_GET_SIZE(mro);
778 for (i = 0; i < n; i++) {
779 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
780 assert(PyType_Check(type));
781 dict = type->tp_defined;
782 assert(dict && PyDict_Check(dict));
783 res = PyDict_GetItem(dict, name);
784 if (res != NULL)
785 return res;
786 }
787 return NULL;
788}
789
790/* This is similar to PyObject_GenericGetAttr(),
791 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
792static PyObject *
793type_getattro(PyTypeObject *type, PyObject *name)
794{
795 PyTypeObject *metatype = type->ob_type;
796 PyObject *descr, *res;
797 descrgetfunc f;
798
799 /* Initialize this type (we'll assume the metatype is initialized) */
800 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000801 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000802 return NULL;
803 }
804
805 /* Get a descriptor from the metatype */
806 descr = _PyType_Lookup(metatype, name);
807 f = NULL;
808 if (descr != NULL) {
809 f = descr->ob_type->tp_descr_get;
810 if (f != NULL && PyDescr_IsData(descr))
811 return f(descr,
812 (PyObject *)type, (PyObject *)metatype);
813 }
814
815 /* Look in tp_defined of this type and its bases */
816 res = _PyType_Lookup(type, name);
817 if (res != NULL) {
818 f = res->ob_type->tp_descr_get;
819 if (f != NULL)
820 return f(res, (PyObject *)NULL, (PyObject *)type);
821 Py_INCREF(res);
822 return res;
823 }
824
825 /* Use the descriptor from the metatype */
826 if (f != NULL) {
827 res = f(descr, (PyObject *)type, (PyObject *)metatype);
828 return res;
829 }
830 if (descr != NULL) {
831 Py_INCREF(descr);
832 return descr;
833 }
834
835 /* Give up */
836 PyErr_Format(PyExc_AttributeError,
837 "type object '%.50s' has no attribute '%.400s'",
838 type->tp_name, PyString_AS_STRING(name));
839 return NULL;
840}
841
842static int
843type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
844{
845 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
846 return PyObject_GenericSetAttr((PyObject *)type, name, value);
847 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
848 return -1;
849}
850
851static void
852type_dealloc(PyTypeObject *type)
853{
854 etype *et;
855
856 /* Assert this is a heap-allocated type object */
857 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
858 et = (etype *)type;
859 Py_XDECREF(type->tp_base);
860 Py_XDECREF(type->tp_dict);
861 Py_XDECREF(type->tp_bases);
862 Py_XDECREF(type->tp_mro);
863 Py_XDECREF(type->tp_defined);
864 /* XXX more? */
865 Py_XDECREF(et->name);
866 Py_XDECREF(et->slots);
867 type->ob_type->tp_free((PyObject *)type);
868}
869
870static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000871 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872 "mro() -> list\nreturn a type's method resolution order"},
873 {0}
874};
875
876static char type_doc[] =
877"type(object) -> the object's type\n"
878"type(name, bases, dict) -> a new type";
879
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000880PyTypeObject PyType_Type = {
881 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000882 0, /* ob_size */
883 "type", /* tp_name */
884 sizeof(etype), /* tp_basicsize */
885 sizeof(struct memberlist), /* tp_itemsize */
886 (destructor)type_dealloc, /* tp_dealloc */
887 0, /* tp_print */
888 0, /* tp_getattr */
889 0, /* tp_setattr */
890 type_compare, /* tp_compare */
891 (reprfunc)type_repr, /* tp_repr */
892 0, /* tp_as_number */
893 0, /* tp_as_sequence */
894 0, /* tp_as_mapping */
895 (hashfunc)_Py_HashPointer, /* tp_hash */
896 (ternaryfunc)type_call, /* tp_call */
897 0, /* tp_str */
898 (getattrofunc)type_getattro, /* tp_getattro */
899 (setattrofunc)type_setattro, /* tp_setattro */
900 0, /* tp_as_buffer */
901 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
902 type_doc, /* tp_doc */
903 0, /* tp_traverse */
904 0, /* tp_clear */
905 0, /* tp_richcompare */
906 0, /* tp_weaklistoffset */
907 0, /* tp_iter */
908 0, /* tp_iternext */
909 type_methods, /* tp_methods */
910 type_members, /* tp_members */
911 type_getsets, /* tp_getset */
912 0, /* tp_base */
913 0, /* tp_dict */
914 0, /* tp_descr_get */
915 0, /* tp_descr_set */
916 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
917 0, /* tp_init */
918 0, /* tp_alloc */
919 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000920};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921
922
923/* The base type of all types (eventually)... except itself. */
924
925static int
926object_init(PyObject *self, PyObject *args, PyObject *kwds)
927{
928 return 0;
929}
930
931static void
932object_dealloc(PyObject *self)
933{
934 self->ob_type->tp_free(self);
935}
936
Guido van Rossum8e248182001-08-12 05:17:56 +0000937static PyObject *
938object_repr(PyObject *self)
939{
Guido van Rossum76e69632001-08-16 18:52:43 +0000940 PyTypeObject *type;
941 PyObject *mod, *name;
942 char buf[200];
Guido van Rossum8e248182001-08-12 05:17:56 +0000943
Guido van Rossum76e69632001-08-16 18:52:43 +0000944 type = self->ob_type;
945 mod = type_module(type, NULL);
946 if (mod == NULL)
947 PyErr_Clear();
948 else if (!PyString_Check(mod)) {
949 Py_DECREF(mod);
950 mod = NULL;
951 }
952 name = type_name(type, NULL);
953 if (name == NULL)
954 return NULL;
955 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
956 sprintf(buf, "<%.80s.%.80s instance at %p>",
957 PyString_AS_STRING(mod),
958 PyString_AS_STRING(name),
959 self);
960 else
961 sprintf(buf, "<%.80s instance at %p>", type->tp_name, self);
962 Py_XDECREF(mod);
963 Py_DECREF(name);
Guido van Rossum8e248182001-08-12 05:17:56 +0000964 return PyString_FromString(buf);
965}
966
Guido van Rossumb8f63662001-08-15 23:57:02 +0000967static PyObject *
968object_str(PyObject *self)
969{
970 unaryfunc f;
971
972 f = self->ob_type->tp_repr;
973 if (f == NULL)
974 f = object_repr;
975 return f(self);
976}
977
Guido van Rossum8e248182001-08-12 05:17:56 +0000978static long
979object_hash(PyObject *self)
980{
981 return _Py_HashPointer(self);
982}
Guido van Rossum8e248182001-08-12 05:17:56 +0000983
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984static void
985object_free(PyObject *self)
986{
987 PyObject_Del(self);
988}
989
990static struct memberlist object_members[] = {
991 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
992 {0}
993};
994
995PyTypeObject PyBaseObject_Type = {
996 PyObject_HEAD_INIT(&PyType_Type)
997 0, /* ob_size */
998 "object", /* tp_name */
999 sizeof(PyObject), /* tp_basicsize */
1000 0, /* tp_itemsize */
1001 (destructor)object_dealloc, /* tp_dealloc */
1002 0, /* tp_print */
1003 0, /* tp_getattr */
1004 0, /* tp_setattr */
1005 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001006 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001007 0, /* tp_as_number */
1008 0, /* tp_as_sequence */
1009 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001010 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001011 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001012 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001013 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001014 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 0, /* tp_as_buffer */
1016 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1017 "The most base type", /* tp_doc */
1018 0, /* tp_traverse */
1019 0, /* tp_clear */
1020 0, /* tp_richcompare */
1021 0, /* tp_weaklistoffset */
1022 0, /* tp_iter */
1023 0, /* tp_iternext */
1024 0, /* tp_methods */
1025 object_members, /* tp_members */
1026 0, /* tp_getset */
1027 0, /* tp_base */
1028 0, /* tp_dict */
1029 0, /* tp_descr_get */
1030 0, /* tp_descr_set */
1031 0, /* tp_dictoffset */
1032 object_init, /* tp_init */
1033 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001034 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001035 object_free, /* tp_free */
1036};
1037
1038
1039/* Initialize the __dict__ in a type object */
1040
1041static int
1042add_methods(PyTypeObject *type, PyMethodDef *meth)
1043{
1044 PyObject *dict = type->tp_defined;
1045
1046 for (; meth->ml_name != NULL; meth++) {
1047 PyObject *descr;
1048 if (PyDict_GetItemString(dict, meth->ml_name))
1049 continue;
1050 descr = PyDescr_NewMethod(type, meth);
1051 if (descr == NULL)
1052 return -1;
1053 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1054 return -1;
1055 Py_DECREF(descr);
1056 }
1057 return 0;
1058}
1059
1060static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001061add_members(PyTypeObject *type, struct memberlist *memb)
1062{
1063 PyObject *dict = type->tp_defined;
1064
1065 for (; memb->name != NULL; memb++) {
1066 PyObject *descr;
1067 if (PyDict_GetItemString(dict, memb->name))
1068 continue;
1069 descr = PyDescr_NewMember(type, memb);
1070 if (descr == NULL)
1071 return -1;
1072 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1073 return -1;
1074 Py_DECREF(descr);
1075 }
1076 return 0;
1077}
1078
1079static int
1080add_getset(PyTypeObject *type, struct getsetlist *gsp)
1081{
1082 PyObject *dict = type->tp_defined;
1083
1084 for (; gsp->name != NULL; gsp++) {
1085 PyObject *descr;
1086 if (PyDict_GetItemString(dict, gsp->name))
1087 continue;
1088 descr = PyDescr_NewGetSet(type, gsp);
1089
1090 if (descr == NULL)
1091 return -1;
1092 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1093 return -1;
1094 Py_DECREF(descr);
1095 }
1096 return 0;
1097}
1098
Guido van Rossum13d52f02001-08-10 21:24:08 +00001099static void
1100inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001101{
1102 int oldsize, newsize;
1103
Guido van Rossum13d52f02001-08-10 21:24:08 +00001104 /* Special flag magic */
1105 if (!type->tp_as_buffer && base->tp_as_buffer) {
1106 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1107 type->tp_flags |=
1108 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1109 }
1110 if (!type->tp_as_sequence && base->tp_as_sequence) {
1111 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1112 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1113 }
1114 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1115 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1116 if ((!type->tp_as_number && base->tp_as_number) ||
1117 (!type->tp_as_sequence && base->tp_as_sequence)) {
1118 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1119 if (!type->tp_as_number && !type->tp_as_sequence) {
1120 type->tp_flags |= base->tp_flags &
1121 Py_TPFLAGS_HAVE_INPLACEOPS;
1122 }
1123 }
1124 /* Wow */
1125 }
1126 if (!type->tp_as_number && base->tp_as_number) {
1127 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1128 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1129 }
1130
1131 /* Copying basicsize is connected to the GC flags */
1132 oldsize = PyType_BASICSIZE(base);
1133 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1134 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1135 (base->tp_flags & Py_TPFLAGS_GC) &&
1136 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1137 (!type->tp_traverse && !type->tp_clear)) {
1138 type->tp_flags |= Py_TPFLAGS_GC;
1139 if (type->tp_traverse == NULL)
1140 type->tp_traverse = base->tp_traverse;
1141 if (type->tp_clear == NULL)
1142 type->tp_clear = base->tp_clear;
1143 }
1144 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1145 if (base != &PyBaseObject_Type ||
1146 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1147 if (type->tp_new == NULL)
1148 type->tp_new = base->tp_new;
1149 }
1150 }
1151 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001152
1153 /* Copy other non-function slots */
1154
1155#undef COPYVAL
1156#define COPYVAL(SLOT) \
1157 if (type->SLOT == 0) type->SLOT = base->SLOT
1158
1159 COPYVAL(tp_itemsize);
1160 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1161 COPYVAL(tp_weaklistoffset);
1162 }
1163 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1164 COPYVAL(tp_dictoffset);
1165 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001166}
1167
1168static void
1169inherit_slots(PyTypeObject *type, PyTypeObject *base)
1170{
1171 PyTypeObject *basebase;
1172
1173#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001174#undef COPYSLOT
1175#undef COPYNUM
1176#undef COPYSEQ
1177#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001178
1179#define SLOTDEFINED(SLOT) \
1180 (base->SLOT != 0 && \
1181 (basebase == NULL || base->SLOT != basebase->SLOT))
1182
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001184 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001185
1186#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1187#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1188#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1189
Guido van Rossum13d52f02001-08-10 21:24:08 +00001190 /* This won't inherit indirect slots (from tp_as_number etc.)
1191 if type doesn't provide the space. */
1192
1193 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1194 basebase = base->tp_base;
1195 if (basebase->tp_as_number == NULL)
1196 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001197 COPYNUM(nb_add);
1198 COPYNUM(nb_subtract);
1199 COPYNUM(nb_multiply);
1200 COPYNUM(nb_divide);
1201 COPYNUM(nb_remainder);
1202 COPYNUM(nb_divmod);
1203 COPYNUM(nb_power);
1204 COPYNUM(nb_negative);
1205 COPYNUM(nb_positive);
1206 COPYNUM(nb_absolute);
1207 COPYNUM(nb_nonzero);
1208 COPYNUM(nb_invert);
1209 COPYNUM(nb_lshift);
1210 COPYNUM(nb_rshift);
1211 COPYNUM(nb_and);
1212 COPYNUM(nb_xor);
1213 COPYNUM(nb_or);
1214 COPYNUM(nb_coerce);
1215 COPYNUM(nb_int);
1216 COPYNUM(nb_long);
1217 COPYNUM(nb_float);
1218 COPYNUM(nb_oct);
1219 COPYNUM(nb_hex);
1220 COPYNUM(nb_inplace_add);
1221 COPYNUM(nb_inplace_subtract);
1222 COPYNUM(nb_inplace_multiply);
1223 COPYNUM(nb_inplace_divide);
1224 COPYNUM(nb_inplace_remainder);
1225 COPYNUM(nb_inplace_power);
1226 COPYNUM(nb_inplace_lshift);
1227 COPYNUM(nb_inplace_rshift);
1228 COPYNUM(nb_inplace_and);
1229 COPYNUM(nb_inplace_xor);
1230 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001231 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1232 COPYNUM(nb_true_divide);
1233 COPYNUM(nb_floor_divide);
1234 COPYNUM(nb_inplace_true_divide);
1235 COPYNUM(nb_inplace_floor_divide);
1236 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001237 }
1238
Guido van Rossum13d52f02001-08-10 21:24:08 +00001239 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1240 basebase = base->tp_base;
1241 if (basebase->tp_as_sequence == NULL)
1242 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243 COPYSEQ(sq_length);
1244 COPYSEQ(sq_concat);
1245 COPYSEQ(sq_repeat);
1246 COPYSEQ(sq_item);
1247 COPYSEQ(sq_slice);
1248 COPYSEQ(sq_ass_item);
1249 COPYSEQ(sq_ass_slice);
1250 COPYSEQ(sq_contains);
1251 COPYSEQ(sq_inplace_concat);
1252 COPYSEQ(sq_inplace_repeat);
1253 }
1254
Guido van Rossum13d52f02001-08-10 21:24:08 +00001255 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1256 basebase = base->tp_base;
1257 if (basebase->tp_as_mapping == NULL)
1258 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259 COPYMAP(mp_length);
1260 COPYMAP(mp_subscript);
1261 COPYMAP(mp_ass_subscript);
1262 }
1263
Guido van Rossum13d52f02001-08-10 21:24:08 +00001264 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266 COPYSLOT(tp_dealloc);
1267 COPYSLOT(tp_print);
1268 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1269 type->tp_getattr = base->tp_getattr;
1270 type->tp_getattro = base->tp_getattro;
1271 }
1272 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1273 type->tp_setattr = base->tp_setattr;
1274 type->tp_setattro = base->tp_setattro;
1275 }
1276 /* tp_compare see tp_richcompare */
1277 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001278 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001279 COPYSLOT(tp_call);
1280 COPYSLOT(tp_str);
1281 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001282 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001283 if (type->tp_compare == NULL &&
1284 type->tp_richcompare == NULL &&
1285 type->tp_hash == NULL)
1286 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001287 type->tp_compare = base->tp_compare;
1288 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001289 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290 }
1291 }
1292 else {
1293 COPYSLOT(tp_compare);
1294 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1296 COPYSLOT(tp_iter);
1297 COPYSLOT(tp_iternext);
1298 }
1299 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1300 COPYSLOT(tp_descr_get);
1301 COPYSLOT(tp_descr_set);
1302 COPYSLOT(tp_dictoffset);
1303 COPYSLOT(tp_init);
1304 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001305 COPYSLOT(tp_free);
1306 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001307}
1308
Guido van Rossum13d52f02001-08-10 21:24:08 +00001309staticforward int add_operators(PyTypeObject *);
1310
Tim Peters6d6c1a32001-08-02 04:15:00 +00001311int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001312PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001313{
1314 PyObject *dict, *bases, *x;
1315 PyTypeObject *base;
1316 int i, n;
1317
Guido van Rossumd614f972001-08-10 17:39:49 +00001318 if (type->tp_flags & Py_TPFLAGS_READY) {
1319 assert(type->tp_dict != NULL);
1320 return 0;
1321 }
1322 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1323 assert(type->tp_dict == NULL);
1324
1325 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001326
1327 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1328 base = type->tp_base;
1329 if (base == NULL && type != &PyBaseObject_Type)
1330 base = type->tp_base = &PyBaseObject_Type;
1331
1332 /* Initialize tp_bases */
1333 bases = type->tp_bases;
1334 if (bases == NULL) {
1335 if (base == NULL)
1336 bases = PyTuple_New(0);
1337 else
1338 bases = Py_BuildValue("(O)", base);
1339 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001340 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341 type->tp_bases = bases;
1342 }
1343
1344 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001345 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001346 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001347 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001348 }
1349
1350 /* Initialize tp_defined */
1351 dict = type->tp_defined;
1352 if (dict == NULL) {
1353 dict = PyDict_New();
1354 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001355 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356 type->tp_defined = dict;
1357 }
1358
1359 /* Add type-specific descriptors to tp_defined */
1360 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001361 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001362 if (type->tp_methods != NULL) {
1363 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001364 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001365 }
1366 if (type->tp_members != NULL) {
1367 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001368 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 }
1370 if (type->tp_getset != NULL) {
1371 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001372 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001373 }
1374
1375 /* Temporarily make tp_dict the same object as tp_defined.
1376 (This is needed to call mro(), and can stay this way for
1377 dynamic types). */
1378 Py_INCREF(type->tp_defined);
1379 type->tp_dict = type->tp_defined;
1380
1381 /* Calculate method resolution order */
1382 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001383 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001384 }
1385
Guido van Rossum13d52f02001-08-10 21:24:08 +00001386 /* Inherit special flags from dominant base */
1387 if (type->tp_base != NULL)
1388 inherit_special(type, type->tp_base);
1389
Tim Peters6d6c1a32001-08-02 04:15:00 +00001390 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001391 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001392 /* For a dynamic type, all slots are overridden */
1393 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001394 }
1395 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001396 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001397 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001399 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001400 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001401 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001402 bases = type->tp_mro;
1403 assert(bases != NULL);
1404 assert(PyTuple_Check(bases));
1405 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001406 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1408 assert(PyType_Check(base));
1409 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001410 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001411 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001412 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413 }
1414 }
1415
Guido van Rossum13d52f02001-08-10 21:24:08 +00001416 /* Some more special stuff */
1417 base = type->tp_base;
1418 if (base != NULL) {
1419 if (type->tp_as_number == NULL)
1420 type->tp_as_number = base->tp_as_number;
1421 if (type->tp_as_sequence == NULL)
1422 type->tp_as_sequence = base->tp_as_sequence;
1423 if (type->tp_as_mapping == NULL)
1424 type->tp_as_mapping = base->tp_as_mapping;
1425 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001426
Guido van Rossum13d52f02001-08-10 21:24:08 +00001427 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001428 assert(type->tp_dict != NULL);
1429 type->tp_flags =
1430 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001432
1433 error:
1434 type->tp_flags &= ~Py_TPFLAGS_READYING;
1435 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001436}
1437
1438
1439/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1440
1441/* There's a wrapper *function* for each distinct function typedef used
1442 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1443 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1444 Most tables have only one entry; the tables for binary operators have two
1445 entries, one regular and one with reversed arguments. */
1446
1447static PyObject *
1448wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1449{
1450 inquiry func = (inquiry)wrapped;
1451 int res;
1452
1453 if (!PyArg_ParseTuple(args, ""))
1454 return NULL;
1455 res = (*func)(self);
1456 if (res == -1 && PyErr_Occurred())
1457 return NULL;
1458 return PyInt_FromLong((long)res);
1459}
1460
1461static struct wrapperbase tab_len[] = {
1462 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1463 {0}
1464};
1465
1466static PyObject *
1467wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1468{
1469 binaryfunc func = (binaryfunc)wrapped;
1470 PyObject *other;
1471
1472 if (!PyArg_ParseTuple(args, "O", &other))
1473 return NULL;
1474 return (*func)(self, other);
1475}
1476
1477static PyObject *
1478wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1479{
1480 binaryfunc func = (binaryfunc)wrapped;
1481 PyObject *other;
1482
1483 if (!PyArg_ParseTuple(args, "O", &other))
1484 return NULL;
1485 return (*func)(other, self);
1486}
1487
1488#undef BINARY
1489#define BINARY(NAME, OP) \
1490static struct wrapperbase tab_##NAME[] = { \
1491 {"__" #NAME "__", \
1492 (wrapperfunc)wrap_binaryfunc, \
1493 "x.__" #NAME "__(y) <==> " #OP}, \
1494 {"__r" #NAME "__", \
1495 (wrapperfunc)wrap_binaryfunc_r, \
1496 "y.__r" #NAME "__(x) <==> " #OP}, \
1497 {0} \
1498}
1499
1500BINARY(add, "x+y");
1501BINARY(sub, "x-y");
1502BINARY(mul, "x*y");
1503BINARY(div, "x/y");
1504BINARY(mod, "x%y");
1505BINARY(divmod, "divmod(x,y)");
1506BINARY(lshift, "x<<y");
1507BINARY(rshift, "x>>y");
1508BINARY(and, "x&y");
1509BINARY(xor, "x^y");
1510BINARY(or, "x|y");
1511
1512static PyObject *
1513wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1514{
1515 ternaryfunc func = (ternaryfunc)wrapped;
1516 PyObject *other;
1517 PyObject *third = Py_None;
1518
1519 /* Note: This wrapper only works for __pow__() */
1520
1521 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1522 return NULL;
1523 return (*func)(self, other, third);
1524}
1525
1526#undef TERNARY
1527#define TERNARY(NAME, OP) \
1528static struct wrapperbase tab_##NAME[] = { \
1529 {"__" #NAME "__", \
1530 (wrapperfunc)wrap_ternaryfunc, \
1531 "x.__" #NAME "__(y, z) <==> " #OP}, \
1532 {"__r" #NAME "__", \
1533 (wrapperfunc)wrap_ternaryfunc, \
1534 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1535 {0} \
1536}
1537
1538TERNARY(pow, "(x**y) % z");
1539
1540#undef UNARY
1541#define UNARY(NAME, OP) \
1542static struct wrapperbase tab_##NAME[] = { \
1543 {"__" #NAME "__", \
1544 (wrapperfunc)wrap_unaryfunc, \
1545 "x.__" #NAME "__() <==> " #OP}, \
1546 {0} \
1547}
1548
1549static PyObject *
1550wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1551{
1552 unaryfunc func = (unaryfunc)wrapped;
1553
1554 if (!PyArg_ParseTuple(args, ""))
1555 return NULL;
1556 return (*func)(self);
1557}
1558
1559UNARY(neg, "-x");
1560UNARY(pos, "+x");
1561UNARY(abs, "abs(x)");
1562UNARY(nonzero, "x != 0");
1563UNARY(invert, "~x");
1564UNARY(int, "int(x)");
1565UNARY(long, "long(x)");
1566UNARY(float, "float(x)");
1567UNARY(oct, "oct(x)");
1568UNARY(hex, "hex(x)");
1569
1570#undef IBINARY
1571#define IBINARY(NAME, OP) \
1572static struct wrapperbase tab_##NAME[] = { \
1573 {"__" #NAME "__", \
1574 (wrapperfunc)wrap_binaryfunc, \
1575 "x.__" #NAME "__(y) <==> " #OP}, \
1576 {0} \
1577}
1578
1579IBINARY(iadd, "x+=y");
1580IBINARY(isub, "x-=y");
1581IBINARY(imul, "x*=y");
1582IBINARY(idiv, "x/=y");
1583IBINARY(imod, "x%=y");
1584IBINARY(ilshift, "x<<=y");
1585IBINARY(irshift, "x>>=y");
1586IBINARY(iand, "x&=y");
1587IBINARY(ixor, "x^=y");
1588IBINARY(ior, "x|=y");
1589
1590#undef ITERNARY
1591#define ITERNARY(NAME, OP) \
1592static struct wrapperbase tab_##NAME[] = { \
1593 {"__" #NAME "__", \
1594 (wrapperfunc)wrap_ternaryfunc, \
1595 "x.__" #NAME "__(y) <==> " #OP}, \
1596 {0} \
1597}
1598
1599ITERNARY(ipow, "x = (x**y) % z");
1600
1601static struct wrapperbase tab_getitem[] = {
1602 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1603 "x.__getitem__(y) <==> x[y]"},
1604 {0}
1605};
1606
1607static PyObject *
1608wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1609{
1610 intargfunc func = (intargfunc)wrapped;
1611 int i;
1612
1613 if (!PyArg_ParseTuple(args, "i", &i))
1614 return NULL;
1615 return (*func)(self, i);
1616}
1617
1618static struct wrapperbase tab_mul_int[] = {
1619 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1620 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1621 {0}
1622};
1623
1624static struct wrapperbase tab_concat[] = {
1625 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1626 {0}
1627};
1628
1629static struct wrapperbase tab_imul_int[] = {
1630 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1631 {0}
1632};
1633
1634static struct wrapperbase tab_getitem_int[] = {
1635 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1636 "x.__getitem__(i) <==> x[i]"},
1637 {0}
1638};
1639
1640static PyObject *
1641wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1642{
1643 intintargfunc func = (intintargfunc)wrapped;
1644 int i, j;
1645
1646 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1647 return NULL;
1648 return (*func)(self, i, j);
1649}
1650
1651static struct wrapperbase tab_getslice[] = {
1652 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1653 "x.__getslice__(i, j) <==> x[i:j]"},
1654 {0}
1655};
1656
1657static PyObject *
1658wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1659{
1660 intobjargproc func = (intobjargproc)wrapped;
1661 int i, res;
1662 PyObject *value;
1663
1664 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1665 return NULL;
1666 res = (*func)(self, i, value);
1667 if (res == -1 && PyErr_Occurred())
1668 return NULL;
1669 Py_INCREF(Py_None);
1670 return Py_None;
1671}
1672
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001673static PyObject *
1674wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1675{
1676 intobjargproc func = (intobjargproc)wrapped;
1677 int i, res;
1678
1679 if (!PyArg_ParseTuple(args, "i", &i))
1680 return NULL;
1681 res = (*func)(self, i, NULL);
1682 if (res == -1 && PyErr_Occurred())
1683 return NULL;
1684 Py_INCREF(Py_None);
1685 return Py_None;
1686}
1687
Tim Peters6d6c1a32001-08-02 04:15:00 +00001688static struct wrapperbase tab_setitem_int[] = {
1689 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1690 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001691 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1692 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001693 {0}
1694};
1695
1696static PyObject *
1697wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1698{
1699 intintobjargproc func = (intintobjargproc)wrapped;
1700 int i, j, res;
1701 PyObject *value;
1702
1703 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1704 return NULL;
1705 res = (*func)(self, i, j, value);
1706 if (res == -1 && PyErr_Occurred())
1707 return NULL;
1708 Py_INCREF(Py_None);
1709 return Py_None;
1710}
1711
1712static struct wrapperbase tab_setslice[] = {
1713 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1714 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1715 {0}
1716};
1717
1718/* XXX objobjproc is a misnomer; should be objargpred */
1719static PyObject *
1720wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1721{
1722 objobjproc func = (objobjproc)wrapped;
1723 int res;
1724 PyObject *value;
1725
1726 if (!PyArg_ParseTuple(args, "O", &value))
1727 return NULL;
1728 res = (*func)(self, value);
1729 if (res == -1 && PyErr_Occurred())
1730 return NULL;
1731 return PyInt_FromLong((long)res);
1732}
1733
1734static struct wrapperbase tab_contains[] = {
1735 {"__contains__", (wrapperfunc)wrap_objobjproc,
1736 "x.__contains__(y) <==> y in x"},
1737 {0}
1738};
1739
1740static PyObject *
1741wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1742{
1743 objobjargproc func = (objobjargproc)wrapped;
1744 int res;
1745 PyObject *key, *value;
1746
1747 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1748 return NULL;
1749 res = (*func)(self, key, value);
1750 if (res == -1 && PyErr_Occurred())
1751 return NULL;
1752 Py_INCREF(Py_None);
1753 return Py_None;
1754}
1755
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001756static PyObject *
1757wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1758{
1759 objobjargproc func = (objobjargproc)wrapped;
1760 int res;
1761 PyObject *key;
1762
1763 if (!PyArg_ParseTuple(args, "O", &key))
1764 return NULL;
1765 res = (*func)(self, key, NULL);
1766 if (res == -1 && PyErr_Occurred())
1767 return NULL;
1768 Py_INCREF(Py_None);
1769 return Py_None;
1770}
1771
Tim Peters6d6c1a32001-08-02 04:15:00 +00001772static struct wrapperbase tab_setitem[] = {
1773 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1774 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001775 {"__delitem__", (wrapperfunc)wrap_delitem,
1776 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001777 {0}
1778};
1779
1780static PyObject *
1781wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1782{
1783 cmpfunc func = (cmpfunc)wrapped;
1784 int res;
1785 PyObject *other;
1786
1787 if (!PyArg_ParseTuple(args, "O", &other))
1788 return NULL;
1789 res = (*func)(self, other);
1790 if (PyErr_Occurred())
1791 return NULL;
1792 return PyInt_FromLong((long)res);
1793}
1794
1795static struct wrapperbase tab_cmp[] = {
1796 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1797 "x.__cmp__(y) <==> cmp(x,y)"},
1798 {0}
1799};
1800
1801static struct wrapperbase tab_repr[] = {
1802 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1803 "x.__repr__() <==> repr(x)"},
1804 {0}
1805};
1806
1807static struct wrapperbase tab_getattr[] = {
1808 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1809 "x.__getattr__('name') <==> x.name"},
1810 {0}
1811};
1812
1813static PyObject *
1814wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1815{
1816 setattrofunc func = (setattrofunc)wrapped;
1817 int res;
1818 PyObject *name, *value;
1819
1820 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1821 return NULL;
1822 res = (*func)(self, name, value);
1823 if (res < 0)
1824 return NULL;
1825 Py_INCREF(Py_None);
1826 return Py_None;
1827}
1828
1829static PyObject *
1830wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1831{
1832 setattrofunc func = (setattrofunc)wrapped;
1833 int res;
1834 PyObject *name;
1835
1836 if (!PyArg_ParseTuple(args, "O", &name))
1837 return NULL;
1838 res = (*func)(self, name, NULL);
1839 if (res < 0)
1840 return NULL;
1841 Py_INCREF(Py_None);
1842 return Py_None;
1843}
1844
1845static struct wrapperbase tab_setattr[] = {
1846 {"__setattr__", (wrapperfunc)wrap_setattr,
1847 "x.__setattr__('name', value) <==> x.name = value"},
1848 {"__delattr__", (wrapperfunc)wrap_delattr,
1849 "x.__delattr__('name') <==> del x.name"},
1850 {0}
1851};
1852
1853static PyObject *
1854wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1855{
1856 hashfunc func = (hashfunc)wrapped;
1857 long res;
1858
1859 if (!PyArg_ParseTuple(args, ""))
1860 return NULL;
1861 res = (*func)(self);
1862 if (res == -1 && PyErr_Occurred())
1863 return NULL;
1864 return PyInt_FromLong(res);
1865}
1866
1867static struct wrapperbase tab_hash[] = {
1868 {"__hash__", (wrapperfunc)wrap_hashfunc,
1869 "x.__hash__() <==> hash(x)"},
1870 {0}
1871};
1872
1873static PyObject *
1874wrap_call(PyObject *self, PyObject *args, void *wrapped)
1875{
1876 ternaryfunc func = (ternaryfunc)wrapped;
1877
1878 /* XXX What about keyword arguments? */
1879 return (*func)(self, args, NULL);
1880}
1881
1882static struct wrapperbase tab_call[] = {
1883 {"__call__", (wrapperfunc)wrap_call,
1884 "x.__call__(...) <==> x(...)"},
1885 {0}
1886};
1887
1888static struct wrapperbase tab_str[] = {
1889 {"__str__", (wrapperfunc)wrap_unaryfunc,
1890 "x.__str__() <==> str(x)"},
1891 {0}
1892};
1893
1894static PyObject *
1895wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1896{
1897 richcmpfunc func = (richcmpfunc)wrapped;
1898 PyObject *other;
1899
1900 if (!PyArg_ParseTuple(args, "O", &other))
1901 return NULL;
1902 return (*func)(self, other, op);
1903}
1904
1905#undef RICHCMP_WRAPPER
1906#define RICHCMP_WRAPPER(NAME, OP) \
1907static PyObject * \
1908richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1909{ \
1910 return wrap_richcmpfunc(self, args, wrapped, OP); \
1911}
1912
Jack Jansen8e938b42001-08-08 15:29:49 +00001913RICHCMP_WRAPPER(lt, Py_LT)
1914RICHCMP_WRAPPER(le, Py_LE)
1915RICHCMP_WRAPPER(eq, Py_EQ)
1916RICHCMP_WRAPPER(ne, Py_NE)
1917RICHCMP_WRAPPER(gt, Py_GT)
1918RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001919
1920#undef RICHCMP_ENTRY
1921#define RICHCMP_ENTRY(NAME, EXPR) \
1922 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1923 "x.__" #NAME "__(y) <==> " EXPR}
1924
1925static struct wrapperbase tab_richcmp[] = {
1926 RICHCMP_ENTRY(lt, "x<y"),
1927 RICHCMP_ENTRY(le, "x<=y"),
1928 RICHCMP_ENTRY(eq, "x==y"),
1929 RICHCMP_ENTRY(ne, "x!=y"),
1930 RICHCMP_ENTRY(gt, "x>y"),
1931 RICHCMP_ENTRY(ge, "x>=y"),
1932 {0}
1933};
1934
1935static struct wrapperbase tab_iter[] = {
1936 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1937 {0}
1938};
1939
1940static PyObject *
1941wrap_next(PyObject *self, PyObject *args, void *wrapped)
1942{
1943 unaryfunc func = (unaryfunc)wrapped;
1944 PyObject *res;
1945
1946 if (!PyArg_ParseTuple(args, ""))
1947 return NULL;
1948 res = (*func)(self);
1949 if (res == NULL && !PyErr_Occurred())
1950 PyErr_SetNone(PyExc_StopIteration);
1951 return res;
1952}
1953
1954static struct wrapperbase tab_next[] = {
1955 {"next", (wrapperfunc)wrap_next,
1956 "x.next() -> the next value, or raise StopIteration"},
1957 {0}
1958};
1959
1960static PyObject *
1961wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1962{
1963 descrgetfunc func = (descrgetfunc)wrapped;
1964 PyObject *obj;
1965 PyObject *type = NULL;
1966
1967 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1968 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001969 return (*func)(self, obj, type);
1970}
1971
1972static struct wrapperbase tab_descr_get[] = {
1973 {"__get__", (wrapperfunc)wrap_descr_get,
1974 "descr.__get__(obj, type) -> value"},
1975 {0}
1976};
1977
1978static PyObject *
1979wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
1980{
1981 descrsetfunc func = (descrsetfunc)wrapped;
1982 PyObject *obj, *value;
1983 int ret;
1984
1985 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
1986 return NULL;
1987 ret = (*func)(self, obj, value);
1988 if (ret < 0)
1989 return NULL;
1990 Py_INCREF(Py_None);
1991 return Py_None;
1992}
1993
1994static struct wrapperbase tab_descr_set[] = {
1995 {"__set__", (wrapperfunc)wrap_descrsetfunc,
1996 "descr.__set__(obj, value)"},
1997 {0}
1998};
1999
2000static PyObject *
2001wrap_init(PyObject *self, PyObject *args, void *wrapped)
2002{
2003 initproc func = (initproc)wrapped;
2004
2005 /* XXX What about keyword arguments? */
2006 if (func(self, args, NULL) < 0)
2007 return NULL;
2008 Py_INCREF(Py_None);
2009 return Py_None;
2010}
2011
2012static struct wrapperbase tab_init[] = {
2013 {"__init__", (wrapperfunc)wrap_init,
2014 "x.__init__(...) initializes x; "
2015 "see x.__type__.__doc__ for signature"},
2016 {0}
2017};
2018
2019static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002020tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002021{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002022 PyTypeObject *type, *subtype;
2023 PyObject *arg0, *res;
2024
2025 if (self == NULL || !PyType_Check(self))
2026 Py_FatalError("__new__() called with non-type 'self'");
2027 type = (PyTypeObject *)self;
2028 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
2029 PyErr_SetString(PyExc_TypeError,
2030 "T.__new__(): not enough arguments");
2031 return NULL;
2032 }
2033 arg0 = PyTuple_GET_ITEM(args, 0);
2034 if (!PyType_Check(arg0)) {
2035 PyErr_SetString(PyExc_TypeError,
2036 "T.__new__(S): S is not a type object");
2037 return NULL;
2038 }
2039 subtype = (PyTypeObject *)arg0;
2040 if (!PyType_IsSubtype(subtype, type)) {
2041 PyErr_SetString(PyExc_TypeError,
2042 "T.__new__(S): S is not a subtype of T");
2043 return NULL;
2044 }
2045 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2046 if (args == NULL)
2047 return NULL;
2048 res = type->tp_new(subtype, args, kwds);
2049 Py_DECREF(args);
2050 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002051}
2052
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002053static struct PyMethodDef tp_new_methoddef[] = {
2054 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2055 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002056 {0}
2057};
2058
2059static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002060add_tp_new_wrapper(PyTypeObject *type)
2061{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002062 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002063
Guido van Rossumf040ede2001-08-07 16:40:56 +00002064 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2065 return 0;
2066 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002067 if (func == NULL)
2068 return -1;
2069 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2070}
2071
Guido van Rossum13d52f02001-08-10 21:24:08 +00002072static int
2073add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2074{
2075 PyObject *dict = type->tp_defined;
2076
2077 for (; wraps->name != NULL; wraps++) {
2078 PyObject *descr;
2079 if (PyDict_GetItemString(dict, wraps->name))
2080 continue;
2081 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2082 if (descr == NULL)
2083 return -1;
2084 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2085 return -1;
2086 Py_DECREF(descr);
2087 }
2088 return 0;
2089}
2090
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002091/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002092 dictionary with method descriptors for function slots. For each
2093 function slot (like tp_repr) that's defined in the type, one or
2094 more corresponding descriptors are added in the type's tp_defined
2095 dictionary under the appropriate name (like __repr__). Some
2096 function slots cause more than one descriptor to be added (for
2097 example, the nb_add slot adds both __add__ and __radd__
2098 descriptors) and some function slots compete for the same
2099 descriptor (for example both sq_item and mp_subscript generate a
2100 __getitem__ descriptor). This only adds new descriptors and
2101 doesn't overwrite entries in tp_defined that were previously
2102 defined. The descriptors contain a reference to the C function
2103 they must call, so that it's safe if they are copied into a
2104 subtype's __dict__ and the subtype has a different C function in
2105 its slot -- calling the method defined by the descriptor will call
2106 the C function that was used to create it, rather than the C
2107 function present in the slot when it is called. (This is important
2108 because a subtype may have a C function in the slot that calls the
2109 method from the dictionary, and we want to avoid infinite recursion
2110 here.) */
2111
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002112static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002113add_operators(PyTypeObject *type)
2114{
2115 PySequenceMethods *sq;
2116 PyMappingMethods *mp;
2117 PyNumberMethods *nb;
2118
2119#undef ADD
2120#define ADD(SLOT, TABLE) \
2121 if (SLOT) { \
2122 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2123 return -1; \
2124 }
2125
2126 if ((sq = type->tp_as_sequence) != NULL) {
2127 ADD(sq->sq_length, tab_len);
2128 ADD(sq->sq_concat, tab_concat);
2129 ADD(sq->sq_repeat, tab_mul_int);
2130 ADD(sq->sq_item, tab_getitem_int);
2131 ADD(sq->sq_slice, tab_getslice);
2132 ADD(sq->sq_ass_item, tab_setitem_int);
2133 ADD(sq->sq_ass_slice, tab_setslice);
2134 ADD(sq->sq_contains, tab_contains);
2135 ADD(sq->sq_inplace_concat, tab_iadd);
2136 ADD(sq->sq_inplace_repeat, tab_imul_int);
2137 }
2138
2139 if ((mp = type->tp_as_mapping) != NULL) {
2140 if (sq->sq_length == NULL)
2141 ADD(mp->mp_length, tab_len);
2142 ADD(mp->mp_subscript, tab_getitem);
2143 ADD(mp->mp_ass_subscript, tab_setitem);
2144 }
2145
2146 /* We don't support "old-style numbers" because their binary
2147 operators require that both arguments have the same type;
2148 the wrappers here only work for new-style numbers. */
2149 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2150 (nb = type->tp_as_number) != NULL) {
2151 ADD(nb->nb_add, tab_add);
2152 ADD(nb->nb_subtract, tab_sub);
2153 ADD(nb->nb_multiply, tab_mul);
2154 ADD(nb->nb_divide, tab_div);
2155 ADD(nb->nb_remainder, tab_mod);
2156 ADD(nb->nb_divmod, tab_divmod);
2157 ADD(nb->nb_power, tab_pow);
2158 ADD(nb->nb_negative, tab_neg);
2159 ADD(nb->nb_positive, tab_pos);
2160 ADD(nb->nb_absolute, tab_abs);
2161 ADD(nb->nb_nonzero, tab_nonzero);
2162 ADD(nb->nb_invert, tab_invert);
2163 ADD(nb->nb_lshift, tab_lshift);
2164 ADD(nb->nb_rshift, tab_rshift);
2165 ADD(nb->nb_and, tab_and);
2166 ADD(nb->nb_xor, tab_xor);
2167 ADD(nb->nb_or, tab_or);
2168 /* We don't support coerce() -- see above comment */
2169 ADD(nb->nb_int, tab_int);
2170 ADD(nb->nb_long, tab_long);
2171 ADD(nb->nb_float, tab_float);
2172 ADD(nb->nb_oct, tab_oct);
2173 ADD(nb->nb_hex, tab_hex);
2174 ADD(nb->nb_inplace_add, tab_iadd);
2175 ADD(nb->nb_inplace_subtract, tab_isub);
2176 ADD(nb->nb_inplace_multiply, tab_imul);
2177 ADD(nb->nb_inplace_divide, tab_idiv);
2178 ADD(nb->nb_inplace_remainder, tab_imod);
2179 ADD(nb->nb_inplace_power, tab_ipow);
2180 ADD(nb->nb_inplace_lshift, tab_ilshift);
2181 ADD(nb->nb_inplace_rshift, tab_irshift);
2182 ADD(nb->nb_inplace_and, tab_iand);
2183 ADD(nb->nb_inplace_xor, tab_ixor);
2184 ADD(nb->nb_inplace_or, tab_ior);
2185 }
2186
2187 ADD(type->tp_getattro, tab_getattr);
2188 ADD(type->tp_setattro, tab_setattr);
2189 ADD(type->tp_compare, tab_cmp);
2190 ADD(type->tp_repr, tab_repr);
2191 ADD(type->tp_hash, tab_hash);
2192 ADD(type->tp_call, tab_call);
2193 ADD(type->tp_str, tab_str);
2194 ADD(type->tp_richcompare, tab_richcmp);
2195 ADD(type->tp_iter, tab_iter);
2196 ADD(type->tp_iternext, tab_next);
2197 ADD(type->tp_descr_get, tab_descr_get);
2198 ADD(type->tp_descr_set, tab_descr_set);
2199 ADD(type->tp_init, tab_init);
2200
Guido van Rossumf040ede2001-08-07 16:40:56 +00002201 if (type->tp_new != NULL) {
2202 if (add_tp_new_wrapper(type) < 0)
2203 return -1;
2204 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002205
2206 return 0;
2207}
2208
Guido van Rossumf040ede2001-08-07 16:40:56 +00002209/* Slot wrappers that call the corresponding __foo__ slot. See comments
2210 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002211
Guido van Rossumdc91b992001-08-08 22:26:22 +00002212#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002213static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002214FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002215{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002216 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002217}
2218
Guido van Rossumdc91b992001-08-08 22:26:22 +00002219#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002220static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002221FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002222{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002223 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224}
2225
Guido van Rossumdc91b992001-08-08 22:26:22 +00002226
2227#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002228static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002229FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002230{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002231 if (self->ob_type->tp_as_number != NULL && \
2232 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2233 PyObject *r; \
2234 r = PyObject_CallMethod( \
2235 self, OPSTR, "O", other); \
2236 if (r != Py_NotImplemented || \
2237 other->ob_type == self->ob_type) \
2238 return r; \
2239 Py_DECREF(r); \
2240 } \
2241 if (other->ob_type->tp_as_number != NULL && \
2242 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2243 return PyObject_CallMethod( \
2244 other, ROPSTR, "O", self); \
2245 } \
2246 Py_INCREF(Py_NotImplemented); \
2247 return Py_NotImplemented; \
2248}
2249
2250#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2251 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2252
2253#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2254static PyObject * \
2255FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2256{ \
2257 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002258}
2259
2260static int
2261slot_sq_length(PyObject *self)
2262{
2263 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2264
2265 if (res == NULL)
2266 return -1;
2267 return (int)PyInt_AsLong(res);
2268}
2269
Guido van Rossumdc91b992001-08-08 22:26:22 +00002270SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2271SLOT1(slot_sq_repeat, "__mul__", int, "i")
2272SLOT1(slot_sq_item, "__getitem__", int, "i")
2273SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002274
2275static int
2276slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2277{
2278 PyObject *res;
2279
2280 if (value == NULL)
2281 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2282 else
2283 res = PyObject_CallMethod(self, "__setitem__",
2284 "iO", index, value);
2285 if (res == NULL)
2286 return -1;
2287 Py_DECREF(res);
2288 return 0;
2289}
2290
2291static int
2292slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2293{
2294 PyObject *res;
2295
2296 if (value == NULL)
2297 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2298 else
2299 res = PyObject_CallMethod(self, "__setslice__",
2300 "iiO", i, j, value);
2301 if (res == NULL)
2302 return -1;
2303 Py_DECREF(res);
2304 return 0;
2305}
2306
2307static int
2308slot_sq_contains(PyObject *self, PyObject *value)
2309{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002310 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311
Guido van Rossumb8f63662001-08-15 23:57:02 +00002312 func = PyObject_GetAttrString(self, "__contains__");
2313
2314 if (func != NULL) {
2315 args = Py_BuildValue("(O)", value);
2316 if (args == NULL)
2317 res = NULL;
2318 else {
2319 res = PyEval_CallObject(func, args);
2320 Py_DECREF(args);
2321 }
2322 Py_DECREF(func);
2323 if (res == NULL)
2324 return -1;
2325 return PyObject_IsTrue(res);
2326 }
2327 else {
2328 PyErr_Clear();
2329 return _PySequence_IterContains(self, value);
2330 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002331}
2332
Guido van Rossumdc91b992001-08-08 22:26:22 +00002333SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2334SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002335
2336#define slot_mp_length slot_sq_length
2337
Guido van Rossumdc91b992001-08-08 22:26:22 +00002338SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002339
2340static int
2341slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2342{
2343 PyObject *res;
2344
2345 if (value == NULL)
2346 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2347 else
2348 res = PyObject_CallMethod(self, "__setitem__",
2349 "OO", key, value);
2350 if (res == NULL)
2351 return -1;
2352 Py_DECREF(res);
2353 return 0;
2354}
2355
Guido van Rossumdc91b992001-08-08 22:26:22 +00002356SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2357SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2358SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2359SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2360SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2361SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2362
2363staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2364
2365SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2366 nb_power, "__pow__", "__rpow__")
2367
2368static PyObject *
2369slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2370{
2371 if (modulus == Py_None)
2372 return slot_nb_power_binary(self, other);
2373 /* Three-arg power doesn't use __rpow__ */
2374 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2375}
2376
2377SLOT0(slot_nb_negative, "__neg__")
2378SLOT0(slot_nb_positive, "__pos__")
2379SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002380
2381static int
2382slot_nb_nonzero(PyObject *self)
2383{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002384 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002385
Guido van Rossumb8f63662001-08-15 23:57:02 +00002386 func = PyObject_GetAttrString(self, "__nonzero__");
2387 if (func == NULL) {
2388 PyErr_Clear();
2389 func = PyObject_GetAttrString(self, "__len__");
2390 }
2391
2392 if (func != NULL) {
2393 res = PyEval_CallObject(func, NULL);
2394 Py_DECREF(func);
2395 if (res == NULL)
2396 return -1;
2397 return PyObject_IsTrue(res);
2398 }
2399 else {
2400 PyErr_Clear();
2401 return 1;
2402 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002403}
2404
Guido van Rossumdc91b992001-08-08 22:26:22 +00002405SLOT0(slot_nb_invert, "__invert__")
2406SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2407SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2408SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2409SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2410SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002411/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002412SLOT0(slot_nb_int, "__int__")
2413SLOT0(slot_nb_long, "__long__")
2414SLOT0(slot_nb_float, "__float__")
2415SLOT0(slot_nb_oct, "__oct__")
2416SLOT0(slot_nb_hex, "__hex__")
2417SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2418SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2419SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2420SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2421SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2422SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2423SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2424SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2425SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2426SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2427SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2428SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2429 "__floordiv__", "__rfloordiv__")
2430SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2431SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2432SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433
2434static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002435half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002436{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002437 PyObject *func, *args, *res;
2438 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002439
Guido van Rossumb8f63662001-08-15 23:57:02 +00002440 func = PyObject_GetAttrString(self, "__cmp__");
2441 if (func == NULL) {
2442 PyErr_Clear();
2443 }
2444 else {
2445 args = Py_BuildValue("(O)", other);
2446 if (args == NULL)
2447 res = NULL;
2448 else {
2449 res = PyObject_CallObject(func, args);
2450 Py_DECREF(args);
2451 }
2452 if (res != Py_NotImplemented) {
2453 if (res == NULL)
2454 return -2;
2455 c = PyInt_AsLong(res);
2456 Py_DECREF(res);
2457 if (c == -1 && PyErr_Occurred())
2458 return -2;
2459 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2460 }
2461 Py_DECREF(res);
2462 }
2463 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002464}
2465
Guido van Rossumb8f63662001-08-15 23:57:02 +00002466static int
2467slot_tp_compare(PyObject *self, PyObject *other)
2468{
2469 int c;
2470
2471 if (self->ob_type->tp_compare == slot_tp_compare) {
2472 c = half_compare(self, other);
2473 if (c <= 1)
2474 return c;
2475 }
2476 if (other->ob_type->tp_compare == slot_tp_compare) {
2477 c = half_compare(other, self);
2478 if (c < -1)
2479 return -2;
2480 if (c <= 1)
2481 return -c;
2482 }
2483 return (void *)self < (void *)other ? -1 :
2484 (void *)self > (void *)other ? 1 : 0;
2485}
2486
2487static PyObject *
2488slot_tp_repr(PyObject *self)
2489{
2490 PyObject *func, *res;
2491
2492 func = PyObject_GetAttrString(self, "__repr__");
2493 if (func != NULL) {
2494 res = PyEval_CallObject(func, NULL);
2495 Py_DECREF(func);
2496 return res;
2497 }
2498 else {
2499 char buf[120];
2500 PyErr_Clear();
2501 sprintf(buf, "<%.80s object at %p>",
2502 self->ob_type->tp_name, self);
2503 return PyString_FromString(buf);
2504 }
2505}
2506
2507static PyObject *
2508slot_tp_str(PyObject *self)
2509{
2510 PyObject *func, *res;
2511
2512 func = PyObject_GetAttrString(self, "__str__");
2513 if (func != NULL) {
2514 res = PyEval_CallObject(func, NULL);
2515 Py_DECREF(func);
2516 return res;
2517 }
2518 else {
2519 PyErr_Clear();
2520 return slot_tp_repr(self);
2521 }
2522}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002523
2524static long
2525slot_tp_hash(PyObject *self)
2526{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002527 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002528 long h;
2529
Guido van Rossumb8f63662001-08-15 23:57:02 +00002530 func = PyObject_GetAttrString(self, "__hash__");
2531
2532 if (func != NULL) {
2533 res = PyEval_CallObject(func, NULL);
2534 Py_DECREF(func);
2535 if (res == NULL)
2536 return -1;
2537 h = PyInt_AsLong(res);
2538 }
2539 else {
2540 PyErr_Clear();
2541 func = PyObject_GetAttrString(self, "__eq__");
2542 if (func == NULL) {
2543 PyErr_Clear();
2544 func = PyObject_GetAttrString(self, "__cmp__");
2545 }
2546 if (func != NULL) {
2547 Py_DECREF(func);
2548 PyErr_SetString(PyExc_TypeError, "unhashable type");
2549 return -1;
2550 }
2551 PyErr_Clear();
2552 h = _Py_HashPointer((void *)self);
2553 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002554 if (h == -1 && !PyErr_Occurred())
2555 h = -2;
2556 return h;
2557}
2558
2559static PyObject *
2560slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2561{
2562 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2563 PyObject *res;
2564
2565 if (meth == NULL)
2566 return NULL;
2567 res = PyObject_Call(meth, args, kwds);
2568 Py_DECREF(meth);
2569 return res;
2570}
2571
Tim Peters6d6c1a32001-08-02 04:15:00 +00002572static PyObject *
2573slot_tp_getattro(PyObject *self, PyObject *name)
2574{
2575 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002576 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002577 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002578
Guido van Rossum8e248182001-08-12 05:17:56 +00002579 if (getattr_str == NULL) {
2580 getattr_str = PyString_InternFromString("__getattr__");
2581 if (getattr_str == NULL)
2582 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002583 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002584 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002585 if (getattr == NULL) {
2586 /* Avoid further slowdowns */
2587 if (tp->tp_getattro == slot_tp_getattro)
2588 tp->tp_getattro = PyObject_GenericGetAttr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002589 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002590 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002591 return PyObject_CallFunction(getattr, "OO", self, name);
2592}
2593
2594static int
2595slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2596{
2597 PyObject *res;
2598
2599 if (value == NULL)
2600 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2601 else
2602 res = PyObject_CallMethod(self, "__setattr__",
2603 "OO", name, value);
2604 if (res == NULL)
2605 return -1;
2606 Py_DECREF(res);
2607 return 0;
2608}
2609
2610/* Map rich comparison operators to their __xx__ namesakes */
2611static char *name_op[] = {
2612 "__lt__",
2613 "__le__",
2614 "__eq__",
2615 "__ne__",
2616 "__gt__",
2617 "__ge__",
2618};
2619
2620static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002621half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002622{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002623 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002624
Guido van Rossumb8f63662001-08-15 23:57:02 +00002625 func = PyObject_GetAttrString(self, name_op[op]);
2626 if (func == NULL) {
2627 PyErr_Clear();
2628 Py_INCREF(Py_NotImplemented);
2629 return Py_NotImplemented;
2630 }
2631 args = Py_BuildValue("(O)", other);
2632 if (args == NULL)
2633 res = NULL;
2634 else {
2635 res = PyObject_CallObject(func, args);
2636 Py_DECREF(args);
2637 }
2638 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002639 return res;
2640}
2641
Guido van Rossumb8f63662001-08-15 23:57:02 +00002642/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2643static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2644
2645static PyObject *
2646slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2647{
2648 PyObject *res;
2649
2650 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2651 res = half_richcompare(self, other, op);
2652 if (res != Py_NotImplemented)
2653 return res;
2654 Py_DECREF(res);
2655 }
2656 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2657 res = half_richcompare(other, self, swapped_op[op]);
2658 if (res != Py_NotImplemented) {
2659 return res;
2660 }
2661 Py_DECREF(res);
2662 }
2663 Py_INCREF(Py_NotImplemented);
2664 return Py_NotImplemented;
2665}
2666
2667static PyObject *
2668slot_tp_iter(PyObject *self)
2669{
2670 PyObject *func, *res;
2671
2672 func = PyObject_GetAttrString(self, "__iter__");
2673 if (func != NULL) {
2674 res = PyObject_CallObject(func, NULL);
2675 Py_DECREF(func);
2676 return res;
2677 }
2678 PyErr_Clear();
2679 func = PyObject_GetAttrString(self, "__getitem__");
2680 if (func == NULL) {
2681 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2682 return NULL;
2683 }
2684 Py_DECREF(func);
2685 return PySeqIter_New(self);
2686}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002687
2688static PyObject *
2689slot_tp_iternext(PyObject *self)
2690{
2691 return PyObject_CallMethod(self, "next", "");
2692}
2693
Guido van Rossum1a493502001-08-17 16:47:50 +00002694static PyObject *
2695slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
2696{
2697 PyTypeObject *tp = self->ob_type;
2698 PyObject *get;
2699 static PyObject *get_str = NULL;
2700
2701 if (get_str == NULL) {
2702 get_str = PyString_InternFromString("__get__");
2703 if (get_str == NULL)
2704 return NULL;
2705 }
2706 get = _PyType_Lookup(tp, get_str);
2707 if (get == NULL) {
2708 /* Avoid further slowdowns */
2709 if (tp->tp_descr_get == slot_tp_descr_get)
2710 tp->tp_descr_get = NULL;
2711 Py_INCREF(self);
2712 return self;
2713 }
2714 return PyObject_CallFunction(get, "OOO", self, obj, type);
2715}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002716
2717static int
2718slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2719{
2720 PyObject *res = PyObject_CallMethod(self, "__set__",
2721 "OO", target, value);
2722 if (res == NULL)
2723 return -1;
2724 Py_DECREF(res);
2725 return 0;
2726}
2727
2728static int
2729slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2730{
2731 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2732 PyObject *res;
2733
2734 if (meth == NULL)
2735 return -1;
2736 res = PyObject_Call(meth, args, kwds);
2737 Py_DECREF(meth);
2738 if (res == NULL)
2739 return -1;
2740 Py_DECREF(res);
2741 return 0;
2742}
2743
2744static PyObject *
2745slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2746{
2747 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2748 PyObject *newargs, *x;
2749 int i, n;
2750
2751 if (func == NULL)
2752 return NULL;
2753 assert(PyTuple_Check(args));
2754 n = PyTuple_GET_SIZE(args);
2755 newargs = PyTuple_New(n+1);
2756 if (newargs == NULL)
2757 return NULL;
2758 Py_INCREF(type);
2759 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2760 for (i = 0; i < n; i++) {
2761 x = PyTuple_GET_ITEM(args, i);
2762 Py_INCREF(x);
2763 PyTuple_SET_ITEM(newargs, i+1, x);
2764 }
2765 x = PyObject_Call(func, newargs, kwds);
2766 Py_DECREF(func);
2767 return x;
2768}
2769
Guido van Rossumf040ede2001-08-07 16:40:56 +00002770/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002771 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002772 The dict argument is the dictionary argument passed to type_new(),
2773 which is the local namespace of the class statement, in other
2774 words, it contains the methods. For each special method (like
2775 __repr__) defined in the dictionary, the corresponding function
2776 slot in the type object (like tp_repr) is set to a special function
2777 whose name is 'slot_' followed by the slot name and whose signature
2778 is whatever is required for that slot. These slot functions look
2779 up the corresponding method in the type's dictionary and call it.
2780 The slot functions have to take care of the various peculiarities
2781 of the mapping between slots and special methods, such as mapping
2782 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2783 etc.) or mapping multiple slots to a single method (sq_item,
2784 mp_subscript <--> __getitem__). */
2785
Tim Peters6d6c1a32001-08-02 04:15:00 +00002786static void
2787override_slots(PyTypeObject *type, PyObject *dict)
2788{
2789 PySequenceMethods *sq = type->tp_as_sequence;
2790 PyMappingMethods *mp = type->tp_as_mapping;
2791 PyNumberMethods *nb = type->tp_as_number;
2792
Guido van Rossumdc91b992001-08-08 22:26:22 +00002793#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002794 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002795 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002796 }
2797
Guido van Rossumdc91b992001-08-08 22:26:22 +00002798#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002799 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002800 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002801 }
2802
Guido van Rossumdc91b992001-08-08 22:26:22 +00002803#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002804 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002805 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002806 }
2807
Guido van Rossumdc91b992001-08-08 22:26:22 +00002808#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002809 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002810 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002811 }
2812
Guido van Rossumdc91b992001-08-08 22:26:22 +00002813 SQSLOT("__len__", sq_length, slot_sq_length);
2814 SQSLOT("__add__", sq_concat, slot_sq_concat);
2815 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2816 SQSLOT("__getitem__", sq_item, slot_sq_item);
2817 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2818 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2819 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2820 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2821 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2822 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2823 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2824 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002825
Guido van Rossumdc91b992001-08-08 22:26:22 +00002826 MPSLOT("__len__", mp_length, slot_mp_length);
2827 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2828 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2829 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002830
Guido van Rossumdc91b992001-08-08 22:26:22 +00002831 NBSLOT("__add__", nb_add, slot_nb_add);
2832 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2833 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2834 NBSLOT("__div__", nb_divide, slot_nb_divide);
2835 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2836 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2837 NBSLOT("__pow__", nb_power, slot_nb_power);
2838 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2839 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2840 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2841 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2842 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2843 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2844 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2845 NBSLOT("__and__", nb_and, slot_nb_and);
2846 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2847 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002848 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002849 NBSLOT("__int__", nb_int, slot_nb_int);
2850 NBSLOT("__long__", nb_long, slot_nb_long);
2851 NBSLOT("__float__", nb_float, slot_nb_float);
2852 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2853 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2854 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2855 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2856 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2857 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2858 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2859 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2860 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2861 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2862 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2863 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2864 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2865 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2866 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2867 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2868 slot_nb_inplace_floor_divide);
2869 NBSLOT("__itruediv__", nb_inplace_true_divide,
2870 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002871
Guido van Rossum8e248182001-08-12 05:17:56 +00002872 if (dict == NULL ||
2873 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002874 PyDict_GetItemString(dict, "__repr__"))
2875 type->tp_print = NULL;
2876
Guido van Rossumdc91b992001-08-08 22:26:22 +00002877 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2878 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2879 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2880 TPSLOT("__call__", tp_call, slot_tp_call);
2881 TPSLOT("__str__", tp_str, slot_tp_str);
2882 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2883 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2884 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2885 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2886 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2887 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2888 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2889 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2890 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2891 TPSLOT("next", tp_iternext, slot_tp_iternext);
2892 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2893 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2894 TPSLOT("__init__", tp_init, slot_tp_init);
2895 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002896}