blob: c49275bc792d7a1ca58871a381cd6cb6a56af622 [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},
Guido van Rossum9676b222001-08-17 20:32:36 +000012 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 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;
Guido van Rossum9676b222001-08-17 20:32:36 +0000204 int weaklistoffset = self->ob_type->tp_weaklistoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000205 PyTypeObject *type, *base;
206 destructor f;
207
208 /* This exists so we can DECREF self->ob_type */
209
210 /* Find the nearest base with a different tp_dealloc */
211 type = self->ob_type;
212 base = type->tp_base;
213 while ((f = base->tp_dealloc) == subtype_dealloc) {
214 base = base->tp_base;
215 assert(base);
216 }
217
218 /* If we added a dict, DECREF it */
219 if (dictoffset && !base->tp_dictoffset) {
220 PyObject **dictptr = (PyObject **) ((char *)self + dictoffset);
221 PyObject *dict = *dictptr;
222 if (dict != NULL) {
223 Py_DECREF(dict);
224 *dictptr = NULL;
225 }
226 }
227
Guido van Rossum9676b222001-08-17 20:32:36 +0000228 /* If we added weaklist, we clear it */
229 if (weaklistoffset && !base->tp_weaklistoffset)
230 PyObject_ClearWeakRefs(self);
231
Tim Peters6d6c1a32001-08-02 04:15:00 +0000232 /* Finalize GC if the base doesn't do GC and we do */
233 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
234 PyObject_GC_Fini(self);
235
236 /* Call the base tp_dealloc() */
237 assert(f);
238 f(self);
239
240 /* Can't reference self beyond this point */
241 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
242 Py_DECREF(type);
243 }
244}
245
246staticforward void override_slots(PyTypeObject *type, PyObject *dict);
247staticforward PyTypeObject *solid_base(PyTypeObject *type);
248
249typedef struct {
250 PyTypeObject type;
251 PyNumberMethods as_number;
252 PySequenceMethods as_sequence;
253 PyMappingMethods as_mapping;
254 PyBufferProcs as_buffer;
255 PyObject *name, *slots;
256 struct memberlist members[1];
257} etype;
258
259/* type test with subclassing support */
260
261int
262PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
263{
264 PyObject *mro;
265
266 mro = a->tp_mro;
267 if (mro != NULL) {
268 /* Deal with multiple inheritance without recursion
269 by walking the MRO tuple */
270 int i, n;
271 assert(PyTuple_Check(mro));
272 n = PyTuple_GET_SIZE(mro);
273 for (i = 0; i < n; i++) {
274 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
275 return 1;
276 }
277 return 0;
278 }
279 else {
280 /* a is not completely initilized yet; follow tp_base */
281 do {
282 if (a == b)
283 return 1;
284 a = a->tp_base;
285 } while (a != NULL);
286 return b == &PyBaseObject_Type;
287 }
288}
289
290/* Method resolution order algorithm from "Putting Metaclasses to Work"
291 by Forman and Danforth (Addison-Wesley 1999). */
292
293static int
294conservative_merge(PyObject *left, PyObject *right)
295{
296 int left_size;
297 int right_size;
298 int i, j, r, ok;
299 PyObject *temp, *rr;
300
301 assert(PyList_Check(left));
302 assert(PyList_Check(right));
303
304 again:
305 left_size = PyList_GET_SIZE(left);
306 right_size = PyList_GET_SIZE(right);
307 for (i = 0; i < left_size; i++) {
308 for (j = 0; j < right_size; j++) {
309 if (PyList_GET_ITEM(left, i) ==
310 PyList_GET_ITEM(right, j)) {
311 /* found a merge point */
312 temp = PyList_New(0);
313 if (temp == NULL)
314 return -1;
315 for (r = 0; r < j; r++) {
316 rr = PyList_GET_ITEM(right, r);
317 ok = PySequence_Contains(left, rr);
318 if (ok < 0) {
319 Py_DECREF(temp);
320 return -1;
321 }
322 if (!ok) {
323 ok = PyList_Append(temp, rr);
324 if (ok < 0) {
325 Py_DECREF(temp);
326 return -1;
327 }
328 }
329 }
330 ok = PyList_SetSlice(left, i, i, temp);
331 Py_DECREF(temp);
332 if (ok < 0)
333 return -1;
334 ok = PyList_SetSlice(right, 0, j+1, NULL);
335 if (ok < 0)
336 return -1;
337 goto again;
338 }
339 }
340 }
341 return PyList_SetSlice(left, left_size, left_size, right);
342}
343
344static int
345serious_order_disagreements(PyObject *left, PyObject *right)
346{
347 return 0; /* XXX later -- for now, we cheat: "don't do that" */
348}
349
350static PyObject *
351mro_implementation(PyTypeObject *type)
352{
353 int i, n, ok;
354 PyObject *bases, *result;
355
356 bases = type->tp_bases;
357 n = PyTuple_GET_SIZE(bases);
358 result = Py_BuildValue("[O]", (PyObject *)type);
359 if (result == NULL)
360 return NULL;
361 for (i = 0; i < n; i++) {
362 PyTypeObject *base =
363 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
364 PyObject *parentMRO = PySequence_List(base->tp_mro);
365 if (parentMRO == NULL) {
366 Py_DECREF(result);
367 return NULL;
368 }
369 if (serious_order_disagreements(result, parentMRO)) {
370 Py_DECREF(result);
371 return NULL;
372 }
373 ok = conservative_merge(result, parentMRO);
374 Py_DECREF(parentMRO);
375 if (ok < 0) {
376 Py_DECREF(result);
377 return NULL;
378 }
379 }
380 return result;
381}
382
383static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000384mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000385{
386 PyTypeObject *type = (PyTypeObject *)self;
387
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388 return mro_implementation(type);
389}
390
391static int
392mro_internal(PyTypeObject *type)
393{
394 PyObject *mro, *result, *tuple;
395
396 if (type->ob_type == &PyType_Type) {
397 result = mro_implementation(type);
398 }
399 else {
400 mro = PyObject_GetAttrString((PyObject *)type, "mro");
401 if (mro == NULL)
402 return -1;
403 result = PyObject_CallObject(mro, NULL);
404 Py_DECREF(mro);
405 }
406 if (result == NULL)
407 return -1;
408 tuple = PySequence_Tuple(result);
409 Py_DECREF(result);
410 type->tp_mro = tuple;
411 return 0;
412}
413
414
415/* Calculate the best base amongst multiple base classes.
416 This is the first one that's on the path to the "solid base". */
417
418static PyTypeObject *
419best_base(PyObject *bases)
420{
421 int i, n;
422 PyTypeObject *base, *winner, *candidate, *base_i;
423
424 assert(PyTuple_Check(bases));
425 n = PyTuple_GET_SIZE(bases);
426 assert(n > 0);
427 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
428 winner = &PyBaseObject_Type;
429 for (i = 0; i < n; i++) {
430 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
431 if (!PyType_Check((PyObject *)base_i)) {
432 PyErr_SetString(
433 PyExc_TypeError,
434 "bases must be types");
435 return NULL;
436 }
437 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000438 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000439 return NULL;
440 }
441 candidate = solid_base(base_i);
442 if (PyType_IsSubtype(winner, candidate))
443 ;
444 else if (PyType_IsSubtype(candidate, winner)) {
445 winner = candidate;
446 base = base_i;
447 }
448 else {
449 PyErr_SetString(
450 PyExc_TypeError,
451 "multiple bases have "
452 "instance lay-out conflict");
453 return NULL;
454 }
455 }
456 assert(base != NULL);
457 return base;
458}
459
460static int
461extra_ivars(PyTypeObject *type, PyTypeObject *base)
462{
Guido van Rossum9676b222001-08-17 20:32:36 +0000463 size_t t_size = PyType_BASICSIZE(type);
464 size_t b_size = PyType_BASICSIZE(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000465
Guido van Rossum9676b222001-08-17 20:32:36 +0000466 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000467 if (type->tp_itemsize || base->tp_itemsize) {
468 /* If itemsize is involved, stricter rules */
469 return t_size != b_size ||
470 type->tp_itemsize != base->tp_itemsize;
471 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000472 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
473 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
474 t_size -= sizeof(PyObject *);
475 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
476 type->tp_dictoffset + sizeof(PyObject *) == t_size)
477 t_size -= sizeof(PyObject *);
478
479 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000480}
481
482static PyTypeObject *
483solid_base(PyTypeObject *type)
484{
485 PyTypeObject *base;
486
487 if (type->tp_base)
488 base = solid_base(type->tp_base);
489 else
490 base = &PyBaseObject_Type;
491 if (extra_ivars(type, base))
492 return type;
493 else
494 return base;
495}
496
497staticforward void object_dealloc(PyObject *);
498staticforward int object_init(PyObject *, PyObject *, PyObject *);
499
500static PyObject *
501type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
502{
503 PyObject *name, *bases, *dict;
504 static char *kwlist[] = {"name", "bases", "dict", 0};
505 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000506 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000507 etype *et;
508 struct memberlist *mp;
Guido van Rossum9676b222001-08-17 20:32:36 +0000509 int i, nbases, nslots, slotoffset, dynamic, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000510
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000511 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000512 if (metatype == &PyType_Type &&
513 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
514 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000515 PyObject *x = PyTuple_GET_ITEM(args, 0);
516 Py_INCREF(x->ob_type);
517 return (PyObject *) x->ob_type;
518 }
519
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000520 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000521 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
522 &name,
523 &PyTuple_Type, &bases,
524 &PyDict_Type, &dict))
525 return NULL;
526
527 /* Determine the proper metatype to deal with this,
528 and check for metatype conflicts while we're at it.
529 Note that if some other metatype wins to contract,
530 it's possible that its instances are not types. */
531 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000532 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000533 for (i = 0; i < nbases; i++) {
534 tmp = PyTuple_GET_ITEM(bases, i);
535 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000536 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000537 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000538 if (PyType_IsSubtype(tmptype, winner)) {
539 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000540 continue;
541 }
542 PyErr_SetString(PyExc_TypeError,
543 "metatype conflict among bases");
544 return NULL;
545 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000546 if (winner != metatype) {
547 if (winner->tp_new != type_new) /* Pass it to the winner */
548 return winner->tp_new(winner, args, kwds);
549 metatype = winner;
550 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000551
552 /* Adjust for empty tuple bases */
553 if (nbases == 0) {
554 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
555 if (bases == NULL)
556 return NULL;
557 nbases = 1;
558 }
559 else
560 Py_INCREF(bases);
561
562 /* XXX From here until type is allocated, "return NULL" leaks bases! */
563
564 /* Calculate best base, and check that all bases are type objects */
565 base = best_base(bases);
566 if (base == NULL)
567 return NULL;
568 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
569 PyErr_Format(PyExc_TypeError,
570 "type '%.100s' is not an acceptable base type",
571 base->tp_name);
572 return NULL;
573 }
574
Guido van Rossum1a493502001-08-17 16:47:50 +0000575 /* Should this be a dynamic class (i.e. modifiable __dict__)?
576 Look in two places for a variable named __dynamic__:
577 1) in the class dict
578 2) in the module dict (globals)
579 The first variable that is an int >= 0 is used.
580 Otherwise, a default is calculated from the base classes:
581 if any base class is dynamic, this class is dynamic; otherwise
582 it is static. */
583 dynamic = -1; /* Not yet determined */
584 /* Look in the class */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000585 tmp = PyDict_GetItemString(dict, "__dynamic__");
586 if (tmp != NULL) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000587 dynamic = PyInt_AsLong(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000588 if (dynamic < 0)
Guido van Rossum1a493502001-08-17 16:47:50 +0000589 PyErr_Clear();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000590 }
Guido van Rossum1a493502001-08-17 16:47:50 +0000591 if (dynamic < 0) {
592 /* Look in the module globals */
593 tmp = PyEval_GetGlobals();
594 if (tmp != NULL) {
595 tmp = PyDict_GetItemString(tmp, "__dynamic__");
596 if (tmp != NULL) {
597 dynamic = PyInt_AsLong(tmp);
598 if (dynamic < 0)
599 PyErr_Clear();
600 }
601 }
602 }
603 if (dynamic < 0) {
604 /* Make a new class dynamic if any of its bases is
605 dynamic. This is not always the same as inheriting
606 the __dynamic__ class attribute! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000607 dynamic = 0;
608 for (i = 0; i < nbases; i++) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000609 tmptype = (PyTypeObject *)
610 PyTuple_GET_ITEM(bases, i);
611 if (tmptype->tp_flags &
612 Py_TPFLAGS_DYNAMICTYPE) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000613 dynamic = 1;
614 break;
615 }
616 }
617 }
618
619 /* Check for a __slots__ sequence variable in dict, and count it */
620 slots = PyDict_GetItemString(dict, "__slots__");
621 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000622 add_dict = 0;
623 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624 if (slots != NULL) {
625 /* Make it into a tuple */
626 if (PyString_Check(slots))
627 slots = Py_BuildValue("(O)", slots);
628 else
629 slots = PySequence_Tuple(slots);
630 if (slots == NULL)
631 return NULL;
632 nslots = PyTuple_GET_SIZE(slots);
633 for (i = 0; i < nslots; i++) {
634 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
635 PyErr_SetString(PyExc_TypeError,
636 "__slots__ must be a sequence of strings");
637 Py_DECREF(slots);
638 return NULL;
639 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000640 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641 }
642 }
643 if (slots == NULL && base->tp_dictoffset == 0 &&
644 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000645 base->tp_setattro == NULL)) {
646 nslots++;
647 add_dict++;
648 }
649 if (slots == NULL && base->tp_weaklistoffset == 0) {
650 nslots++;
651 add_weak++;
652 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000653
654 /* XXX From here until type is safely allocated,
655 "return NULL" may leak slots! */
656
657 /* Allocate the type object */
658 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
659 if (type == NULL)
660 return NULL;
661
662 /* Keep name and slots alive in the extended type object */
663 et = (etype *)type;
664 Py_INCREF(name);
665 et->name = name;
666 et->slots = slots;
667
Guido van Rossumdc91b992001-08-08 22:26:22 +0000668 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000669 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
670 Py_TPFLAGS_BASETYPE;
671 if (dynamic)
672 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000673
674 /* It's a new-style number unless it specifically inherits any
675 old-style numeric behavior */
676 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
677 (base->tp_as_number == NULL))
678 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
679
680 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000681 type->tp_as_number = &et->as_number;
682 type->tp_as_sequence = &et->as_sequence;
683 type->tp_as_mapping = &et->as_mapping;
684 type->tp_as_buffer = &et->as_buffer;
685 type->tp_name = PyString_AS_STRING(name);
686
687 /* Set tp_base and tp_bases */
688 type->tp_bases = bases;
689 Py_INCREF(base);
690 type->tp_base = base;
691
692 /* Initialize tp_defined from passed-in dict */
693 type->tp_defined = dict = PyDict_Copy(dict);
694 if (dict == NULL) {
695 Py_DECREF(type);
696 return NULL;
697 }
698
Guido van Rossumc3542212001-08-16 09:18:56 +0000699 /* Set __module__ in the dict */
700 if (PyDict_GetItemString(dict, "__module__") == NULL) {
701 tmp = PyEval_GetGlobals();
702 if (tmp != NULL) {
703 tmp = PyDict_GetItemString(tmp, "__name__");
704 if (tmp != NULL) {
705 if (PyDict_SetItemString(dict, "__module__",
706 tmp) < 0)
707 return NULL;
708 }
709 }
710 }
711
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 /* Special-case __new__: if it's a plain function,
713 make it a static function */
714 tmp = PyDict_GetItemString(dict, "__new__");
715 if (tmp != NULL && PyFunction_Check(tmp)) {
716 tmp = PyStaticMethod_New(tmp);
717 if (tmp == NULL) {
718 Py_DECREF(type);
719 return NULL;
720 }
721 PyDict_SetItemString(dict, "__new__", tmp);
722 Py_DECREF(tmp);
723 }
724
725 /* Add descriptors for custom slots from __slots__, or for __dict__ */
726 mp = et->members;
727 slotoffset = PyType_BASICSIZE(base);
728 if (slots != NULL) {
729 for (i = 0; i < nslots; i++, mp++) {
730 mp->name = PyString_AS_STRING(
731 PyTuple_GET_ITEM(slots, i));
732 mp->type = T_OBJECT;
733 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000734 if (base->tp_weaklistoffset == 0 &&
735 strcmp(mp->name, "__weakref__") == 0)
736 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 slotoffset += sizeof(PyObject *);
738 }
739 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000740 else {
741 if (add_dict) {
742 type->tp_dictoffset = slotoffset;
743 mp->name = "__dict__";
744 mp->type = T_OBJECT;
745 mp->offset = slotoffset;
746 mp->readonly = 1;
747 mp++;
748 slotoffset += sizeof(PyObject *);
749 }
750 if (add_weak) {
751 type->tp_weaklistoffset = slotoffset;
752 mp->name = "__weakref__";
753 mp->type = T_OBJECT;
754 mp->offset = slotoffset;
755 mp->readonly = 1;
756 mp++;
757 slotoffset += sizeof(PyObject *);
758 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759 }
760 type->tp_basicsize = slotoffset;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000761 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762
763 /* Special case some slots */
764 if (type->tp_dictoffset != 0 || nslots > 0) {
765 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
766 type->tp_getattro = PyObject_GenericGetAttr;
767 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
768 type->tp_setattro = PyObject_GenericSetAttr;
769 }
770 type->tp_dealloc = subtype_dealloc;
771
772 /* Always override allocation strategy to use regular heap */
773 type->tp_alloc = PyType_GenericAlloc;
774 type->tp_free = _PyObject_Del;
775
776 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000777 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000778 Py_DECREF(type);
779 return NULL;
780 }
781
782 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000783 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
784 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000785
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786 return (PyObject *)type;
787}
788
789/* Internal API to look for a name through the MRO.
790 This returns a borrowed reference, and doesn't set an exception! */
791PyObject *
792_PyType_Lookup(PyTypeObject *type, PyObject *name)
793{
794 int i, n;
795 PyObject *mro, *res, *dict;
796
797 /* For static types, look in tp_dict */
798 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
799 dict = type->tp_dict;
800 assert(dict && PyDict_Check(dict));
801 return PyDict_GetItem(dict, name);
802 }
803
804 /* For dynamic types, look in tp_defined of types in MRO */
805 mro = type->tp_mro;
806 assert(PyTuple_Check(mro));
807 n = PyTuple_GET_SIZE(mro);
808 for (i = 0; i < n; i++) {
809 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
810 assert(PyType_Check(type));
811 dict = type->tp_defined;
812 assert(dict && PyDict_Check(dict));
813 res = PyDict_GetItem(dict, name);
814 if (res != NULL)
815 return res;
816 }
817 return NULL;
818}
819
820/* This is similar to PyObject_GenericGetAttr(),
821 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
822static PyObject *
823type_getattro(PyTypeObject *type, PyObject *name)
824{
825 PyTypeObject *metatype = type->ob_type;
826 PyObject *descr, *res;
827 descrgetfunc f;
828
829 /* Initialize this type (we'll assume the metatype is initialized) */
830 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000831 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000832 return NULL;
833 }
834
835 /* Get a descriptor from the metatype */
836 descr = _PyType_Lookup(metatype, name);
837 f = NULL;
838 if (descr != NULL) {
839 f = descr->ob_type->tp_descr_get;
840 if (f != NULL && PyDescr_IsData(descr))
841 return f(descr,
842 (PyObject *)type, (PyObject *)metatype);
843 }
844
845 /* Look in tp_defined of this type and its bases */
846 res = _PyType_Lookup(type, name);
847 if (res != NULL) {
848 f = res->ob_type->tp_descr_get;
849 if (f != NULL)
850 return f(res, (PyObject *)NULL, (PyObject *)type);
851 Py_INCREF(res);
852 return res;
853 }
854
855 /* Use the descriptor from the metatype */
856 if (f != NULL) {
857 res = f(descr, (PyObject *)type, (PyObject *)metatype);
858 return res;
859 }
860 if (descr != NULL) {
861 Py_INCREF(descr);
862 return descr;
863 }
864
865 /* Give up */
866 PyErr_Format(PyExc_AttributeError,
867 "type object '%.50s' has no attribute '%.400s'",
868 type->tp_name, PyString_AS_STRING(name));
869 return NULL;
870}
871
872static int
873type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
874{
875 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
876 return PyObject_GenericSetAttr((PyObject *)type, name, value);
877 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
878 return -1;
879}
880
881static void
882type_dealloc(PyTypeObject *type)
883{
884 etype *et;
885
886 /* Assert this is a heap-allocated type object */
887 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
888 et = (etype *)type;
889 Py_XDECREF(type->tp_base);
890 Py_XDECREF(type->tp_dict);
891 Py_XDECREF(type->tp_bases);
892 Py_XDECREF(type->tp_mro);
893 Py_XDECREF(type->tp_defined);
894 /* XXX more? */
895 Py_XDECREF(et->name);
896 Py_XDECREF(et->slots);
897 type->ob_type->tp_free((PyObject *)type);
898}
899
900static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000901 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902 "mro() -> list\nreturn a type's method resolution order"},
903 {0}
904};
905
906static char type_doc[] =
907"type(object) -> the object's type\n"
908"type(name, bases, dict) -> a new type";
909
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000910PyTypeObject PyType_Type = {
911 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000912 0, /* ob_size */
913 "type", /* tp_name */
914 sizeof(etype), /* tp_basicsize */
915 sizeof(struct memberlist), /* tp_itemsize */
916 (destructor)type_dealloc, /* tp_dealloc */
917 0, /* tp_print */
918 0, /* tp_getattr */
919 0, /* tp_setattr */
920 type_compare, /* tp_compare */
921 (reprfunc)type_repr, /* tp_repr */
922 0, /* tp_as_number */
923 0, /* tp_as_sequence */
924 0, /* tp_as_mapping */
925 (hashfunc)_Py_HashPointer, /* tp_hash */
926 (ternaryfunc)type_call, /* tp_call */
927 0, /* tp_str */
928 (getattrofunc)type_getattro, /* tp_getattro */
929 (setattrofunc)type_setattro, /* tp_setattro */
930 0, /* tp_as_buffer */
931 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
932 type_doc, /* tp_doc */
933 0, /* tp_traverse */
934 0, /* tp_clear */
935 0, /* tp_richcompare */
936 0, /* tp_weaklistoffset */
937 0, /* tp_iter */
938 0, /* tp_iternext */
939 type_methods, /* tp_methods */
940 type_members, /* tp_members */
941 type_getsets, /* tp_getset */
942 0, /* tp_base */
943 0, /* tp_dict */
944 0, /* tp_descr_get */
945 0, /* tp_descr_set */
946 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
947 0, /* tp_init */
948 0, /* tp_alloc */
949 type_new, /* tp_new */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000950};
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951
952
953/* The base type of all types (eventually)... except itself. */
954
955static int
956object_init(PyObject *self, PyObject *args, PyObject *kwds)
957{
958 return 0;
959}
960
961static void
962object_dealloc(PyObject *self)
963{
964 self->ob_type->tp_free(self);
965}
966
Guido van Rossum8e248182001-08-12 05:17:56 +0000967static PyObject *
968object_repr(PyObject *self)
969{
Guido van Rossum76e69632001-08-16 18:52:43 +0000970 PyTypeObject *type;
971 PyObject *mod, *name;
972 char buf[200];
Guido van Rossum8e248182001-08-12 05:17:56 +0000973
Guido van Rossum76e69632001-08-16 18:52:43 +0000974 type = self->ob_type;
975 mod = type_module(type, NULL);
976 if (mod == NULL)
977 PyErr_Clear();
978 else if (!PyString_Check(mod)) {
979 Py_DECREF(mod);
980 mod = NULL;
981 }
982 name = type_name(type, NULL);
983 if (name == NULL)
984 return NULL;
985 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
986 sprintf(buf, "<%.80s.%.80s instance at %p>",
987 PyString_AS_STRING(mod),
988 PyString_AS_STRING(name),
989 self);
990 else
991 sprintf(buf, "<%.80s instance at %p>", type->tp_name, self);
992 Py_XDECREF(mod);
993 Py_DECREF(name);
Guido van Rossum8e248182001-08-12 05:17:56 +0000994 return PyString_FromString(buf);
995}
996
Guido van Rossumb8f63662001-08-15 23:57:02 +0000997static PyObject *
998object_str(PyObject *self)
999{
1000 unaryfunc f;
1001
1002 f = self->ob_type->tp_repr;
1003 if (f == NULL)
1004 f = object_repr;
1005 return f(self);
1006}
1007
Guido van Rossum8e248182001-08-12 05:17:56 +00001008static long
1009object_hash(PyObject *self)
1010{
1011 return _Py_HashPointer(self);
1012}
Guido van Rossum8e248182001-08-12 05:17:56 +00001013
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014static void
1015object_free(PyObject *self)
1016{
1017 PyObject_Del(self);
1018}
1019
1020static struct memberlist object_members[] = {
1021 {"__class__", T_OBJECT, offsetof(PyObject, ob_type), READONLY},
1022 {0}
1023};
1024
1025PyTypeObject PyBaseObject_Type = {
1026 PyObject_HEAD_INIT(&PyType_Type)
1027 0, /* ob_size */
1028 "object", /* tp_name */
1029 sizeof(PyObject), /* tp_basicsize */
1030 0, /* tp_itemsize */
1031 (destructor)object_dealloc, /* tp_dealloc */
1032 0, /* tp_print */
1033 0, /* tp_getattr */
1034 0, /* tp_setattr */
1035 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001036 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 0, /* tp_as_number */
1038 0, /* tp_as_sequence */
1039 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001040 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001041 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001042 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001043 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001044 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001045 0, /* tp_as_buffer */
1046 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1047 "The most base type", /* tp_doc */
1048 0, /* tp_traverse */
1049 0, /* tp_clear */
1050 0, /* tp_richcompare */
1051 0, /* tp_weaklistoffset */
1052 0, /* tp_iter */
1053 0, /* tp_iternext */
1054 0, /* tp_methods */
1055 object_members, /* tp_members */
1056 0, /* tp_getset */
1057 0, /* tp_base */
1058 0, /* tp_dict */
1059 0, /* tp_descr_get */
1060 0, /* tp_descr_set */
1061 0, /* tp_dictoffset */
1062 object_init, /* tp_init */
1063 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001064 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001065 object_free, /* tp_free */
1066};
1067
1068
1069/* Initialize the __dict__ in a type object */
1070
1071static int
1072add_methods(PyTypeObject *type, PyMethodDef *meth)
1073{
1074 PyObject *dict = type->tp_defined;
1075
1076 for (; meth->ml_name != NULL; meth++) {
1077 PyObject *descr;
1078 if (PyDict_GetItemString(dict, meth->ml_name))
1079 continue;
1080 descr = PyDescr_NewMethod(type, meth);
1081 if (descr == NULL)
1082 return -1;
1083 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1084 return -1;
1085 Py_DECREF(descr);
1086 }
1087 return 0;
1088}
1089
1090static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091add_members(PyTypeObject *type, struct memberlist *memb)
1092{
1093 PyObject *dict = type->tp_defined;
1094
1095 for (; memb->name != NULL; memb++) {
1096 PyObject *descr;
1097 if (PyDict_GetItemString(dict, memb->name))
1098 continue;
1099 descr = PyDescr_NewMember(type, memb);
1100 if (descr == NULL)
1101 return -1;
1102 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1103 return -1;
1104 Py_DECREF(descr);
1105 }
1106 return 0;
1107}
1108
1109static int
1110add_getset(PyTypeObject *type, struct getsetlist *gsp)
1111{
1112 PyObject *dict = type->tp_defined;
1113
1114 for (; gsp->name != NULL; gsp++) {
1115 PyObject *descr;
1116 if (PyDict_GetItemString(dict, gsp->name))
1117 continue;
1118 descr = PyDescr_NewGetSet(type, gsp);
1119
1120 if (descr == NULL)
1121 return -1;
1122 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1123 return -1;
1124 Py_DECREF(descr);
1125 }
1126 return 0;
1127}
1128
Guido van Rossum13d52f02001-08-10 21:24:08 +00001129static void
1130inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001131{
1132 int oldsize, newsize;
1133
Guido van Rossum13d52f02001-08-10 21:24:08 +00001134 /* Special flag magic */
1135 if (!type->tp_as_buffer && base->tp_as_buffer) {
1136 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1137 type->tp_flags |=
1138 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1139 }
1140 if (!type->tp_as_sequence && base->tp_as_sequence) {
1141 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1142 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1143 }
1144 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1145 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1146 if ((!type->tp_as_number && base->tp_as_number) ||
1147 (!type->tp_as_sequence && base->tp_as_sequence)) {
1148 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1149 if (!type->tp_as_number && !type->tp_as_sequence) {
1150 type->tp_flags |= base->tp_flags &
1151 Py_TPFLAGS_HAVE_INPLACEOPS;
1152 }
1153 }
1154 /* Wow */
1155 }
1156 if (!type->tp_as_number && base->tp_as_number) {
1157 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1158 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1159 }
1160
1161 /* Copying basicsize is connected to the GC flags */
1162 oldsize = PyType_BASICSIZE(base);
1163 newsize = type->tp_basicsize ? PyType_BASICSIZE(type) : oldsize;
1164 if (!(type->tp_flags & Py_TPFLAGS_GC) &&
1165 (base->tp_flags & Py_TPFLAGS_GC) &&
1166 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1167 (!type->tp_traverse && !type->tp_clear)) {
1168 type->tp_flags |= Py_TPFLAGS_GC;
1169 if (type->tp_traverse == NULL)
1170 type->tp_traverse = base->tp_traverse;
1171 if (type->tp_clear == NULL)
1172 type->tp_clear = base->tp_clear;
1173 }
1174 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1175 if (base != &PyBaseObject_Type ||
1176 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1177 if (type->tp_new == NULL)
1178 type->tp_new = base->tp_new;
1179 }
1180 }
1181 PyType_SET_BASICSIZE(type, newsize);
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001182
1183 /* Copy other non-function slots */
1184
1185#undef COPYVAL
1186#define COPYVAL(SLOT) \
1187 if (type->SLOT == 0) type->SLOT = base->SLOT
1188
1189 COPYVAL(tp_itemsize);
1190 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1191 COPYVAL(tp_weaklistoffset);
1192 }
1193 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1194 COPYVAL(tp_dictoffset);
1195 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001196}
1197
1198static void
1199inherit_slots(PyTypeObject *type, PyTypeObject *base)
1200{
1201 PyTypeObject *basebase;
1202
1203#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001204#undef COPYSLOT
1205#undef COPYNUM
1206#undef COPYSEQ
1207#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001208
1209#define SLOTDEFINED(SLOT) \
1210 (base->SLOT != 0 && \
1211 (basebase == NULL || base->SLOT != basebase->SLOT))
1212
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001214 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001215
1216#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1217#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1218#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1219
Guido van Rossum13d52f02001-08-10 21:24:08 +00001220 /* This won't inherit indirect slots (from tp_as_number etc.)
1221 if type doesn't provide the space. */
1222
1223 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1224 basebase = base->tp_base;
1225 if (basebase->tp_as_number == NULL)
1226 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001227 COPYNUM(nb_add);
1228 COPYNUM(nb_subtract);
1229 COPYNUM(nb_multiply);
1230 COPYNUM(nb_divide);
1231 COPYNUM(nb_remainder);
1232 COPYNUM(nb_divmod);
1233 COPYNUM(nb_power);
1234 COPYNUM(nb_negative);
1235 COPYNUM(nb_positive);
1236 COPYNUM(nb_absolute);
1237 COPYNUM(nb_nonzero);
1238 COPYNUM(nb_invert);
1239 COPYNUM(nb_lshift);
1240 COPYNUM(nb_rshift);
1241 COPYNUM(nb_and);
1242 COPYNUM(nb_xor);
1243 COPYNUM(nb_or);
1244 COPYNUM(nb_coerce);
1245 COPYNUM(nb_int);
1246 COPYNUM(nb_long);
1247 COPYNUM(nb_float);
1248 COPYNUM(nb_oct);
1249 COPYNUM(nb_hex);
1250 COPYNUM(nb_inplace_add);
1251 COPYNUM(nb_inplace_subtract);
1252 COPYNUM(nb_inplace_multiply);
1253 COPYNUM(nb_inplace_divide);
1254 COPYNUM(nb_inplace_remainder);
1255 COPYNUM(nb_inplace_power);
1256 COPYNUM(nb_inplace_lshift);
1257 COPYNUM(nb_inplace_rshift);
1258 COPYNUM(nb_inplace_and);
1259 COPYNUM(nb_inplace_xor);
1260 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001261 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1262 COPYNUM(nb_true_divide);
1263 COPYNUM(nb_floor_divide);
1264 COPYNUM(nb_inplace_true_divide);
1265 COPYNUM(nb_inplace_floor_divide);
1266 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001267 }
1268
Guido van Rossum13d52f02001-08-10 21:24:08 +00001269 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1270 basebase = base->tp_base;
1271 if (basebase->tp_as_sequence == NULL)
1272 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001273 COPYSEQ(sq_length);
1274 COPYSEQ(sq_concat);
1275 COPYSEQ(sq_repeat);
1276 COPYSEQ(sq_item);
1277 COPYSEQ(sq_slice);
1278 COPYSEQ(sq_ass_item);
1279 COPYSEQ(sq_ass_slice);
1280 COPYSEQ(sq_contains);
1281 COPYSEQ(sq_inplace_concat);
1282 COPYSEQ(sq_inplace_repeat);
1283 }
1284
Guido van Rossum13d52f02001-08-10 21:24:08 +00001285 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1286 basebase = base->tp_base;
1287 if (basebase->tp_as_mapping == NULL)
1288 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001289 COPYMAP(mp_length);
1290 COPYMAP(mp_subscript);
1291 COPYMAP(mp_ass_subscript);
1292 }
1293
Guido van Rossum13d52f02001-08-10 21:24:08 +00001294 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001295
Tim Peters6d6c1a32001-08-02 04:15:00 +00001296 COPYSLOT(tp_dealloc);
1297 COPYSLOT(tp_print);
1298 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1299 type->tp_getattr = base->tp_getattr;
1300 type->tp_getattro = base->tp_getattro;
1301 }
1302 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1303 type->tp_setattr = base->tp_setattr;
1304 type->tp_setattro = base->tp_setattro;
1305 }
1306 /* tp_compare see tp_richcompare */
1307 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001308 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001309 COPYSLOT(tp_call);
1310 COPYSLOT(tp_str);
1311 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001312 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001313 if (type->tp_compare == NULL &&
1314 type->tp_richcompare == NULL &&
1315 type->tp_hash == NULL)
1316 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001317 type->tp_compare = base->tp_compare;
1318 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001319 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001320 }
1321 }
1322 else {
1323 COPYSLOT(tp_compare);
1324 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1326 COPYSLOT(tp_iter);
1327 COPYSLOT(tp_iternext);
1328 }
1329 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1330 COPYSLOT(tp_descr_get);
1331 COPYSLOT(tp_descr_set);
1332 COPYSLOT(tp_dictoffset);
1333 COPYSLOT(tp_init);
1334 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001335 COPYSLOT(tp_free);
1336 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001337}
1338
Guido van Rossum13d52f02001-08-10 21:24:08 +00001339staticforward int add_operators(PyTypeObject *);
1340
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001342PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001343{
1344 PyObject *dict, *bases, *x;
1345 PyTypeObject *base;
1346 int i, n;
1347
Guido van Rossumd614f972001-08-10 17:39:49 +00001348 if (type->tp_flags & Py_TPFLAGS_READY) {
1349 assert(type->tp_dict != NULL);
1350 return 0;
1351 }
1352 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1353 assert(type->tp_dict == NULL);
1354
1355 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001356
1357 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1358 base = type->tp_base;
1359 if (base == NULL && type != &PyBaseObject_Type)
1360 base = type->tp_base = &PyBaseObject_Type;
1361
1362 /* Initialize tp_bases */
1363 bases = type->tp_bases;
1364 if (bases == NULL) {
1365 if (base == NULL)
1366 bases = PyTuple_New(0);
1367 else
1368 bases = Py_BuildValue("(O)", base);
1369 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001370 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001371 type->tp_bases = bases;
1372 }
1373
1374 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001375 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001376 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001377 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 }
1379
1380 /* Initialize tp_defined */
1381 dict = type->tp_defined;
1382 if (dict == NULL) {
1383 dict = PyDict_New();
1384 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001385 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 type->tp_defined = dict;
1387 }
1388
1389 /* Add type-specific descriptors to tp_defined */
1390 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001391 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001392 if (type->tp_methods != NULL) {
1393 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001394 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001395 }
1396 if (type->tp_members != NULL) {
1397 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001398 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001399 }
1400 if (type->tp_getset != NULL) {
1401 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001402 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001403 }
1404
1405 /* Temporarily make tp_dict the same object as tp_defined.
1406 (This is needed to call mro(), and can stay this way for
1407 dynamic types). */
1408 Py_INCREF(type->tp_defined);
1409 type->tp_dict = type->tp_defined;
1410
1411 /* Calculate method resolution order */
1412 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001413 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001414 }
1415
Guido van Rossum13d52f02001-08-10 21:24:08 +00001416 /* Inherit special flags from dominant base */
1417 if (type->tp_base != NULL)
1418 inherit_special(type, type->tp_base);
1419
Tim Peters6d6c1a32001-08-02 04:15:00 +00001420 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001421 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001422 /* For a dynamic type, all slots are overridden */
1423 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001424 }
1425 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001426 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001427 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001429 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001430 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001431 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001432 bases = type->tp_mro;
1433 assert(bases != NULL);
1434 assert(PyTuple_Check(bases));
1435 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001436 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001437 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1438 assert(PyType_Check(base));
1439 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001440 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001441 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001442 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443 }
1444 }
1445
Guido van Rossum13d52f02001-08-10 21:24:08 +00001446 /* Some more special stuff */
1447 base = type->tp_base;
1448 if (base != NULL) {
1449 if (type->tp_as_number == NULL)
1450 type->tp_as_number = base->tp_as_number;
1451 if (type->tp_as_sequence == NULL)
1452 type->tp_as_sequence = base->tp_as_sequence;
1453 if (type->tp_as_mapping == NULL)
1454 type->tp_as_mapping = base->tp_as_mapping;
1455 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001456
Guido van Rossum13d52f02001-08-10 21:24:08 +00001457 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001458 assert(type->tp_dict != NULL);
1459 type->tp_flags =
1460 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001462
1463 error:
1464 type->tp_flags &= ~Py_TPFLAGS_READYING;
1465 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001466}
1467
1468
1469/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1470
1471/* There's a wrapper *function* for each distinct function typedef used
1472 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1473 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1474 Most tables have only one entry; the tables for binary operators have two
1475 entries, one regular and one with reversed arguments. */
1476
1477static PyObject *
1478wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1479{
1480 inquiry func = (inquiry)wrapped;
1481 int res;
1482
1483 if (!PyArg_ParseTuple(args, ""))
1484 return NULL;
1485 res = (*func)(self);
1486 if (res == -1 && PyErr_Occurred())
1487 return NULL;
1488 return PyInt_FromLong((long)res);
1489}
1490
1491static struct wrapperbase tab_len[] = {
1492 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1493 {0}
1494};
1495
1496static PyObject *
1497wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1498{
1499 binaryfunc func = (binaryfunc)wrapped;
1500 PyObject *other;
1501
1502 if (!PyArg_ParseTuple(args, "O", &other))
1503 return NULL;
1504 return (*func)(self, other);
1505}
1506
1507static PyObject *
1508wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1509{
1510 binaryfunc func = (binaryfunc)wrapped;
1511 PyObject *other;
1512
1513 if (!PyArg_ParseTuple(args, "O", &other))
1514 return NULL;
1515 return (*func)(other, self);
1516}
1517
1518#undef BINARY
1519#define BINARY(NAME, OP) \
1520static struct wrapperbase tab_##NAME[] = { \
1521 {"__" #NAME "__", \
1522 (wrapperfunc)wrap_binaryfunc, \
1523 "x.__" #NAME "__(y) <==> " #OP}, \
1524 {"__r" #NAME "__", \
1525 (wrapperfunc)wrap_binaryfunc_r, \
1526 "y.__r" #NAME "__(x) <==> " #OP}, \
1527 {0} \
1528}
1529
1530BINARY(add, "x+y");
1531BINARY(sub, "x-y");
1532BINARY(mul, "x*y");
1533BINARY(div, "x/y");
1534BINARY(mod, "x%y");
1535BINARY(divmod, "divmod(x,y)");
1536BINARY(lshift, "x<<y");
1537BINARY(rshift, "x>>y");
1538BINARY(and, "x&y");
1539BINARY(xor, "x^y");
1540BINARY(or, "x|y");
1541
1542static PyObject *
1543wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1544{
1545 ternaryfunc func = (ternaryfunc)wrapped;
1546 PyObject *other;
1547 PyObject *third = Py_None;
1548
1549 /* Note: This wrapper only works for __pow__() */
1550
1551 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1552 return NULL;
1553 return (*func)(self, other, third);
1554}
1555
1556#undef TERNARY
1557#define TERNARY(NAME, OP) \
1558static struct wrapperbase tab_##NAME[] = { \
1559 {"__" #NAME "__", \
1560 (wrapperfunc)wrap_ternaryfunc, \
1561 "x.__" #NAME "__(y, z) <==> " #OP}, \
1562 {"__r" #NAME "__", \
1563 (wrapperfunc)wrap_ternaryfunc, \
1564 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1565 {0} \
1566}
1567
1568TERNARY(pow, "(x**y) % z");
1569
1570#undef UNARY
1571#define UNARY(NAME, OP) \
1572static struct wrapperbase tab_##NAME[] = { \
1573 {"__" #NAME "__", \
1574 (wrapperfunc)wrap_unaryfunc, \
1575 "x.__" #NAME "__() <==> " #OP}, \
1576 {0} \
1577}
1578
1579static PyObject *
1580wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
1581{
1582 unaryfunc func = (unaryfunc)wrapped;
1583
1584 if (!PyArg_ParseTuple(args, ""))
1585 return NULL;
1586 return (*func)(self);
1587}
1588
1589UNARY(neg, "-x");
1590UNARY(pos, "+x");
1591UNARY(abs, "abs(x)");
1592UNARY(nonzero, "x != 0");
1593UNARY(invert, "~x");
1594UNARY(int, "int(x)");
1595UNARY(long, "long(x)");
1596UNARY(float, "float(x)");
1597UNARY(oct, "oct(x)");
1598UNARY(hex, "hex(x)");
1599
1600#undef IBINARY
1601#define IBINARY(NAME, OP) \
1602static struct wrapperbase tab_##NAME[] = { \
1603 {"__" #NAME "__", \
1604 (wrapperfunc)wrap_binaryfunc, \
1605 "x.__" #NAME "__(y) <==> " #OP}, \
1606 {0} \
1607}
1608
1609IBINARY(iadd, "x+=y");
1610IBINARY(isub, "x-=y");
1611IBINARY(imul, "x*=y");
1612IBINARY(idiv, "x/=y");
1613IBINARY(imod, "x%=y");
1614IBINARY(ilshift, "x<<=y");
1615IBINARY(irshift, "x>>=y");
1616IBINARY(iand, "x&=y");
1617IBINARY(ixor, "x^=y");
1618IBINARY(ior, "x|=y");
1619
1620#undef ITERNARY
1621#define ITERNARY(NAME, OP) \
1622static struct wrapperbase tab_##NAME[] = { \
1623 {"__" #NAME "__", \
1624 (wrapperfunc)wrap_ternaryfunc, \
1625 "x.__" #NAME "__(y) <==> " #OP}, \
1626 {0} \
1627}
1628
1629ITERNARY(ipow, "x = (x**y) % z");
1630
1631static struct wrapperbase tab_getitem[] = {
1632 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
1633 "x.__getitem__(y) <==> x[y]"},
1634 {0}
1635};
1636
1637static PyObject *
1638wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
1639{
1640 intargfunc func = (intargfunc)wrapped;
1641 int i;
1642
1643 if (!PyArg_ParseTuple(args, "i", &i))
1644 return NULL;
1645 return (*func)(self, i);
1646}
1647
1648static struct wrapperbase tab_mul_int[] = {
1649 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
1650 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
1651 {0}
1652};
1653
1654static struct wrapperbase tab_concat[] = {
1655 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
1656 {0}
1657};
1658
1659static struct wrapperbase tab_imul_int[] = {
1660 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
1661 {0}
1662};
1663
1664static struct wrapperbase tab_getitem_int[] = {
1665 {"__getitem__", (wrapperfunc)wrap_intargfunc,
1666 "x.__getitem__(i) <==> x[i]"},
1667 {0}
1668};
1669
1670static PyObject *
1671wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
1672{
1673 intintargfunc func = (intintargfunc)wrapped;
1674 int i, j;
1675
1676 if (!PyArg_ParseTuple(args, "ii", &i, &j))
1677 return NULL;
1678 return (*func)(self, i, j);
1679}
1680
1681static struct wrapperbase tab_getslice[] = {
1682 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
1683 "x.__getslice__(i, j) <==> x[i:j]"},
1684 {0}
1685};
1686
1687static PyObject *
1688wrap_intobjargproc(PyObject *self, PyObject *args, void *wrapped)
1689{
1690 intobjargproc func = (intobjargproc)wrapped;
1691 int i, res;
1692 PyObject *value;
1693
1694 if (!PyArg_ParseTuple(args, "iO", &i, &value))
1695 return NULL;
1696 res = (*func)(self, i, value);
1697 if (res == -1 && PyErr_Occurred())
1698 return NULL;
1699 Py_INCREF(Py_None);
1700 return Py_None;
1701}
1702
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001703static PyObject *
1704wrap_delitem_int(PyObject *self, PyObject *args, void *wrapped)
1705{
1706 intobjargproc func = (intobjargproc)wrapped;
1707 int i, res;
1708
1709 if (!PyArg_ParseTuple(args, "i", &i))
1710 return NULL;
1711 res = (*func)(self, i, NULL);
1712 if (res == -1 && PyErr_Occurred())
1713 return NULL;
1714 Py_INCREF(Py_None);
1715 return Py_None;
1716}
1717
Tim Peters6d6c1a32001-08-02 04:15:00 +00001718static struct wrapperbase tab_setitem_int[] = {
1719 {"__setitem__", (wrapperfunc)wrap_intobjargproc,
1720 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001721 {"__delitem__", (wrapperfunc)wrap_delitem_int,
1722 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001723 {0}
1724};
1725
1726static PyObject *
1727wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
1728{
1729 intintobjargproc func = (intintobjargproc)wrapped;
1730 int i, j, res;
1731 PyObject *value;
1732
1733 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
1734 return NULL;
1735 res = (*func)(self, i, j, value);
1736 if (res == -1 && PyErr_Occurred())
1737 return NULL;
1738 Py_INCREF(Py_None);
1739 return Py_None;
1740}
1741
1742static struct wrapperbase tab_setslice[] = {
1743 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
1744 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
1745 {0}
1746};
1747
1748/* XXX objobjproc is a misnomer; should be objargpred */
1749static PyObject *
1750wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
1751{
1752 objobjproc func = (objobjproc)wrapped;
1753 int res;
1754 PyObject *value;
1755
1756 if (!PyArg_ParseTuple(args, "O", &value))
1757 return NULL;
1758 res = (*func)(self, value);
1759 if (res == -1 && PyErr_Occurred())
1760 return NULL;
1761 return PyInt_FromLong((long)res);
1762}
1763
1764static struct wrapperbase tab_contains[] = {
1765 {"__contains__", (wrapperfunc)wrap_objobjproc,
1766 "x.__contains__(y) <==> y in x"},
1767 {0}
1768};
1769
1770static PyObject *
1771wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
1772{
1773 objobjargproc func = (objobjargproc)wrapped;
1774 int res;
1775 PyObject *key, *value;
1776
1777 if (!PyArg_ParseTuple(args, "OO", &key, &value))
1778 return NULL;
1779 res = (*func)(self, key, value);
1780 if (res == -1 && PyErr_Occurred())
1781 return NULL;
1782 Py_INCREF(Py_None);
1783 return Py_None;
1784}
1785
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001786static PyObject *
1787wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
1788{
1789 objobjargproc func = (objobjargproc)wrapped;
1790 int res;
1791 PyObject *key;
1792
1793 if (!PyArg_ParseTuple(args, "O", &key))
1794 return NULL;
1795 res = (*func)(self, key, NULL);
1796 if (res == -1 && PyErr_Occurred())
1797 return NULL;
1798 Py_INCREF(Py_None);
1799 return Py_None;
1800}
1801
Tim Peters6d6c1a32001-08-02 04:15:00 +00001802static struct wrapperbase tab_setitem[] = {
1803 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
1804 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00001805 {"__delitem__", (wrapperfunc)wrap_delitem,
1806 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001807 {0}
1808};
1809
1810static PyObject *
1811wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
1812{
1813 cmpfunc func = (cmpfunc)wrapped;
1814 int res;
1815 PyObject *other;
1816
1817 if (!PyArg_ParseTuple(args, "O", &other))
1818 return NULL;
1819 res = (*func)(self, other);
1820 if (PyErr_Occurred())
1821 return NULL;
1822 return PyInt_FromLong((long)res);
1823}
1824
1825static struct wrapperbase tab_cmp[] = {
1826 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
1827 "x.__cmp__(y) <==> cmp(x,y)"},
1828 {0}
1829};
1830
1831static struct wrapperbase tab_repr[] = {
1832 {"__repr__", (wrapperfunc)wrap_unaryfunc,
1833 "x.__repr__() <==> repr(x)"},
1834 {0}
1835};
1836
1837static struct wrapperbase tab_getattr[] = {
1838 {"__getattr__", (wrapperfunc)wrap_binaryfunc,
1839 "x.__getattr__('name') <==> x.name"},
1840 {0}
1841};
1842
1843static PyObject *
1844wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
1845{
1846 setattrofunc func = (setattrofunc)wrapped;
1847 int res;
1848 PyObject *name, *value;
1849
1850 if (!PyArg_ParseTuple(args, "OO", &name, &value))
1851 return NULL;
1852 res = (*func)(self, name, value);
1853 if (res < 0)
1854 return NULL;
1855 Py_INCREF(Py_None);
1856 return Py_None;
1857}
1858
1859static PyObject *
1860wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
1861{
1862 setattrofunc func = (setattrofunc)wrapped;
1863 int res;
1864 PyObject *name;
1865
1866 if (!PyArg_ParseTuple(args, "O", &name))
1867 return NULL;
1868 res = (*func)(self, name, NULL);
1869 if (res < 0)
1870 return NULL;
1871 Py_INCREF(Py_None);
1872 return Py_None;
1873}
1874
1875static struct wrapperbase tab_setattr[] = {
1876 {"__setattr__", (wrapperfunc)wrap_setattr,
1877 "x.__setattr__('name', value) <==> x.name = value"},
1878 {"__delattr__", (wrapperfunc)wrap_delattr,
1879 "x.__delattr__('name') <==> del x.name"},
1880 {0}
1881};
1882
1883static PyObject *
1884wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
1885{
1886 hashfunc func = (hashfunc)wrapped;
1887 long res;
1888
1889 if (!PyArg_ParseTuple(args, ""))
1890 return NULL;
1891 res = (*func)(self);
1892 if (res == -1 && PyErr_Occurred())
1893 return NULL;
1894 return PyInt_FromLong(res);
1895}
1896
1897static struct wrapperbase tab_hash[] = {
1898 {"__hash__", (wrapperfunc)wrap_hashfunc,
1899 "x.__hash__() <==> hash(x)"},
1900 {0}
1901};
1902
1903static PyObject *
1904wrap_call(PyObject *self, PyObject *args, void *wrapped)
1905{
1906 ternaryfunc func = (ternaryfunc)wrapped;
1907
1908 /* XXX What about keyword arguments? */
1909 return (*func)(self, args, NULL);
1910}
1911
1912static struct wrapperbase tab_call[] = {
1913 {"__call__", (wrapperfunc)wrap_call,
1914 "x.__call__(...) <==> x(...)"},
1915 {0}
1916};
1917
1918static struct wrapperbase tab_str[] = {
1919 {"__str__", (wrapperfunc)wrap_unaryfunc,
1920 "x.__str__() <==> str(x)"},
1921 {0}
1922};
1923
1924static PyObject *
1925wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
1926{
1927 richcmpfunc func = (richcmpfunc)wrapped;
1928 PyObject *other;
1929
1930 if (!PyArg_ParseTuple(args, "O", &other))
1931 return NULL;
1932 return (*func)(self, other, op);
1933}
1934
1935#undef RICHCMP_WRAPPER
1936#define RICHCMP_WRAPPER(NAME, OP) \
1937static PyObject * \
1938richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
1939{ \
1940 return wrap_richcmpfunc(self, args, wrapped, OP); \
1941}
1942
Jack Jansen8e938b42001-08-08 15:29:49 +00001943RICHCMP_WRAPPER(lt, Py_LT)
1944RICHCMP_WRAPPER(le, Py_LE)
1945RICHCMP_WRAPPER(eq, Py_EQ)
1946RICHCMP_WRAPPER(ne, Py_NE)
1947RICHCMP_WRAPPER(gt, Py_GT)
1948RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001949
1950#undef RICHCMP_ENTRY
1951#define RICHCMP_ENTRY(NAME, EXPR) \
1952 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
1953 "x.__" #NAME "__(y) <==> " EXPR}
1954
1955static struct wrapperbase tab_richcmp[] = {
1956 RICHCMP_ENTRY(lt, "x<y"),
1957 RICHCMP_ENTRY(le, "x<=y"),
1958 RICHCMP_ENTRY(eq, "x==y"),
1959 RICHCMP_ENTRY(ne, "x!=y"),
1960 RICHCMP_ENTRY(gt, "x>y"),
1961 RICHCMP_ENTRY(ge, "x>=y"),
1962 {0}
1963};
1964
1965static struct wrapperbase tab_iter[] = {
1966 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
1967 {0}
1968};
1969
1970static PyObject *
1971wrap_next(PyObject *self, PyObject *args, void *wrapped)
1972{
1973 unaryfunc func = (unaryfunc)wrapped;
1974 PyObject *res;
1975
1976 if (!PyArg_ParseTuple(args, ""))
1977 return NULL;
1978 res = (*func)(self);
1979 if (res == NULL && !PyErr_Occurred())
1980 PyErr_SetNone(PyExc_StopIteration);
1981 return res;
1982}
1983
1984static struct wrapperbase tab_next[] = {
1985 {"next", (wrapperfunc)wrap_next,
1986 "x.next() -> the next value, or raise StopIteration"},
1987 {0}
1988};
1989
1990static PyObject *
1991wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
1992{
1993 descrgetfunc func = (descrgetfunc)wrapped;
1994 PyObject *obj;
1995 PyObject *type = NULL;
1996
1997 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
1998 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001999 return (*func)(self, obj, type);
2000}
2001
2002static struct wrapperbase tab_descr_get[] = {
2003 {"__get__", (wrapperfunc)wrap_descr_get,
2004 "descr.__get__(obj, type) -> value"},
2005 {0}
2006};
2007
2008static PyObject *
2009wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
2010{
2011 descrsetfunc func = (descrsetfunc)wrapped;
2012 PyObject *obj, *value;
2013 int ret;
2014
2015 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2016 return NULL;
2017 ret = (*func)(self, obj, value);
2018 if (ret < 0)
2019 return NULL;
2020 Py_INCREF(Py_None);
2021 return Py_None;
2022}
2023
2024static struct wrapperbase tab_descr_set[] = {
2025 {"__set__", (wrapperfunc)wrap_descrsetfunc,
2026 "descr.__set__(obj, value)"},
2027 {0}
2028};
2029
2030static PyObject *
2031wrap_init(PyObject *self, PyObject *args, void *wrapped)
2032{
2033 initproc func = (initproc)wrapped;
2034
2035 /* XXX What about keyword arguments? */
2036 if (func(self, args, NULL) < 0)
2037 return NULL;
2038 Py_INCREF(Py_None);
2039 return Py_None;
2040}
2041
2042static struct wrapperbase tab_init[] = {
2043 {"__init__", (wrapperfunc)wrap_init,
2044 "x.__init__(...) initializes x; "
2045 "see x.__type__.__doc__ for signature"},
2046 {0}
2047};
2048
2049static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002050tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002051{
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002052 PyTypeObject *type, *subtype;
2053 PyObject *arg0, *res;
2054
2055 if (self == NULL || !PyType_Check(self))
2056 Py_FatalError("__new__() called with non-type 'self'");
2057 type = (PyTypeObject *)self;
2058 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
2059 PyErr_SetString(PyExc_TypeError,
2060 "T.__new__(): not enough arguments");
2061 return NULL;
2062 }
2063 arg0 = PyTuple_GET_ITEM(args, 0);
2064 if (!PyType_Check(arg0)) {
2065 PyErr_SetString(PyExc_TypeError,
2066 "T.__new__(S): S is not a type object");
2067 return NULL;
2068 }
2069 subtype = (PyTypeObject *)arg0;
2070 if (!PyType_IsSubtype(subtype, type)) {
2071 PyErr_SetString(PyExc_TypeError,
2072 "T.__new__(S): S is not a subtype of T");
2073 return NULL;
2074 }
2075 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2076 if (args == NULL)
2077 return NULL;
2078 res = type->tp_new(subtype, args, kwds);
2079 Py_DECREF(args);
2080 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002081}
2082
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002083static struct PyMethodDef tp_new_methoddef[] = {
2084 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2085 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002086 {0}
2087};
2088
2089static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002090add_tp_new_wrapper(PyTypeObject *type)
2091{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002092 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002093
Guido van Rossumf040ede2001-08-07 16:40:56 +00002094 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2095 return 0;
2096 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002097 if (func == NULL)
2098 return -1;
2099 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2100}
2101
Guido van Rossum13d52f02001-08-10 21:24:08 +00002102static int
2103add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2104{
2105 PyObject *dict = type->tp_defined;
2106
2107 for (; wraps->name != NULL; wraps++) {
2108 PyObject *descr;
2109 if (PyDict_GetItemString(dict, wraps->name))
2110 continue;
2111 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2112 if (descr == NULL)
2113 return -1;
2114 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2115 return -1;
2116 Py_DECREF(descr);
2117 }
2118 return 0;
2119}
2120
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002121/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002122 dictionary with method descriptors for function slots. For each
2123 function slot (like tp_repr) that's defined in the type, one or
2124 more corresponding descriptors are added in the type's tp_defined
2125 dictionary under the appropriate name (like __repr__). Some
2126 function slots cause more than one descriptor to be added (for
2127 example, the nb_add slot adds both __add__ and __radd__
2128 descriptors) and some function slots compete for the same
2129 descriptor (for example both sq_item and mp_subscript generate a
2130 __getitem__ descriptor). This only adds new descriptors and
2131 doesn't overwrite entries in tp_defined that were previously
2132 defined. The descriptors contain a reference to the C function
2133 they must call, so that it's safe if they are copied into a
2134 subtype's __dict__ and the subtype has a different C function in
2135 its slot -- calling the method defined by the descriptor will call
2136 the C function that was used to create it, rather than the C
2137 function present in the slot when it is called. (This is important
2138 because a subtype may have a C function in the slot that calls the
2139 method from the dictionary, and we want to avoid infinite recursion
2140 here.) */
2141
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002142static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143add_operators(PyTypeObject *type)
2144{
2145 PySequenceMethods *sq;
2146 PyMappingMethods *mp;
2147 PyNumberMethods *nb;
2148
2149#undef ADD
2150#define ADD(SLOT, TABLE) \
2151 if (SLOT) { \
2152 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2153 return -1; \
2154 }
2155
2156 if ((sq = type->tp_as_sequence) != NULL) {
2157 ADD(sq->sq_length, tab_len);
2158 ADD(sq->sq_concat, tab_concat);
2159 ADD(sq->sq_repeat, tab_mul_int);
2160 ADD(sq->sq_item, tab_getitem_int);
2161 ADD(sq->sq_slice, tab_getslice);
2162 ADD(sq->sq_ass_item, tab_setitem_int);
2163 ADD(sq->sq_ass_slice, tab_setslice);
2164 ADD(sq->sq_contains, tab_contains);
2165 ADD(sq->sq_inplace_concat, tab_iadd);
2166 ADD(sq->sq_inplace_repeat, tab_imul_int);
2167 }
2168
2169 if ((mp = type->tp_as_mapping) != NULL) {
2170 if (sq->sq_length == NULL)
2171 ADD(mp->mp_length, tab_len);
2172 ADD(mp->mp_subscript, tab_getitem);
2173 ADD(mp->mp_ass_subscript, tab_setitem);
2174 }
2175
2176 /* We don't support "old-style numbers" because their binary
2177 operators require that both arguments have the same type;
2178 the wrappers here only work for new-style numbers. */
2179 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2180 (nb = type->tp_as_number) != NULL) {
2181 ADD(nb->nb_add, tab_add);
2182 ADD(nb->nb_subtract, tab_sub);
2183 ADD(nb->nb_multiply, tab_mul);
2184 ADD(nb->nb_divide, tab_div);
2185 ADD(nb->nb_remainder, tab_mod);
2186 ADD(nb->nb_divmod, tab_divmod);
2187 ADD(nb->nb_power, tab_pow);
2188 ADD(nb->nb_negative, tab_neg);
2189 ADD(nb->nb_positive, tab_pos);
2190 ADD(nb->nb_absolute, tab_abs);
2191 ADD(nb->nb_nonzero, tab_nonzero);
2192 ADD(nb->nb_invert, tab_invert);
2193 ADD(nb->nb_lshift, tab_lshift);
2194 ADD(nb->nb_rshift, tab_rshift);
2195 ADD(nb->nb_and, tab_and);
2196 ADD(nb->nb_xor, tab_xor);
2197 ADD(nb->nb_or, tab_or);
2198 /* We don't support coerce() -- see above comment */
2199 ADD(nb->nb_int, tab_int);
2200 ADD(nb->nb_long, tab_long);
2201 ADD(nb->nb_float, tab_float);
2202 ADD(nb->nb_oct, tab_oct);
2203 ADD(nb->nb_hex, tab_hex);
2204 ADD(nb->nb_inplace_add, tab_iadd);
2205 ADD(nb->nb_inplace_subtract, tab_isub);
2206 ADD(nb->nb_inplace_multiply, tab_imul);
2207 ADD(nb->nb_inplace_divide, tab_idiv);
2208 ADD(nb->nb_inplace_remainder, tab_imod);
2209 ADD(nb->nb_inplace_power, tab_ipow);
2210 ADD(nb->nb_inplace_lshift, tab_ilshift);
2211 ADD(nb->nb_inplace_rshift, tab_irshift);
2212 ADD(nb->nb_inplace_and, tab_iand);
2213 ADD(nb->nb_inplace_xor, tab_ixor);
2214 ADD(nb->nb_inplace_or, tab_ior);
2215 }
2216
2217 ADD(type->tp_getattro, tab_getattr);
2218 ADD(type->tp_setattro, tab_setattr);
2219 ADD(type->tp_compare, tab_cmp);
2220 ADD(type->tp_repr, tab_repr);
2221 ADD(type->tp_hash, tab_hash);
2222 ADD(type->tp_call, tab_call);
2223 ADD(type->tp_str, tab_str);
2224 ADD(type->tp_richcompare, tab_richcmp);
2225 ADD(type->tp_iter, tab_iter);
2226 ADD(type->tp_iternext, tab_next);
2227 ADD(type->tp_descr_get, tab_descr_get);
2228 ADD(type->tp_descr_set, tab_descr_set);
2229 ADD(type->tp_init, tab_init);
2230
Guido van Rossumf040ede2001-08-07 16:40:56 +00002231 if (type->tp_new != NULL) {
2232 if (add_tp_new_wrapper(type) < 0)
2233 return -1;
2234 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002235
2236 return 0;
2237}
2238
Guido van Rossumf040ede2001-08-07 16:40:56 +00002239/* Slot wrappers that call the corresponding __foo__ slot. See comments
2240 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002241
Guido van Rossumdc91b992001-08-08 22:26:22 +00002242#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002243static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002244FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002245{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002246 return PyObject_CallMethod(self, OPSTR, ""); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002247}
2248
Guido van Rossumdc91b992001-08-08 22:26:22 +00002249#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002250static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002251FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002252{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002253 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002254}
2255
Guido van Rossumdc91b992001-08-08 22:26:22 +00002256
2257#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002258static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002259FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002260{ \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002261 if (self->ob_type->tp_as_number != NULL && \
2262 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2263 PyObject *r; \
2264 r = PyObject_CallMethod( \
2265 self, OPSTR, "O", other); \
2266 if (r != Py_NotImplemented || \
2267 other->ob_type == self->ob_type) \
2268 return r; \
2269 Py_DECREF(r); \
2270 } \
2271 if (other->ob_type->tp_as_number != NULL && \
2272 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2273 return PyObject_CallMethod( \
2274 other, ROPSTR, "O", self); \
2275 } \
2276 Py_INCREF(Py_NotImplemented); \
2277 return Py_NotImplemented; \
2278}
2279
2280#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2281 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2282
2283#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2284static PyObject * \
2285FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2286{ \
2287 return PyObject_CallMethod(self, OPSTR, ARGCODES, arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002288}
2289
2290static int
2291slot_sq_length(PyObject *self)
2292{
2293 PyObject *res = PyObject_CallMethod(self, "__len__", "");
2294
2295 if (res == NULL)
2296 return -1;
2297 return (int)PyInt_AsLong(res);
2298}
2299
Guido van Rossumdc91b992001-08-08 22:26:22 +00002300SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2301SLOT1(slot_sq_repeat, "__mul__", int, "i")
2302SLOT1(slot_sq_item, "__getitem__", int, "i")
2303SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002304
2305static int
2306slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2307{
2308 PyObject *res;
2309
2310 if (value == NULL)
2311 res = PyObject_CallMethod(self, "__delitem__", "i", index);
2312 else
2313 res = PyObject_CallMethod(self, "__setitem__",
2314 "iO", index, value);
2315 if (res == NULL)
2316 return -1;
2317 Py_DECREF(res);
2318 return 0;
2319}
2320
2321static int
2322slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2323{
2324 PyObject *res;
2325
2326 if (value == NULL)
2327 res = PyObject_CallMethod(self, "__delslice__", "ii", i, j);
2328 else
2329 res = PyObject_CallMethod(self, "__setslice__",
2330 "iiO", i, j, value);
2331 if (res == NULL)
2332 return -1;
2333 Py_DECREF(res);
2334 return 0;
2335}
2336
2337static int
2338slot_sq_contains(PyObject *self, PyObject *value)
2339{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002340 PyObject *func, *res, *args;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002341
Guido van Rossumb8f63662001-08-15 23:57:02 +00002342 func = PyObject_GetAttrString(self, "__contains__");
2343
2344 if (func != NULL) {
2345 args = Py_BuildValue("(O)", value);
2346 if (args == NULL)
2347 res = NULL;
2348 else {
2349 res = PyEval_CallObject(func, args);
2350 Py_DECREF(args);
2351 }
2352 Py_DECREF(func);
2353 if (res == NULL)
2354 return -1;
2355 return PyObject_IsTrue(res);
2356 }
2357 else {
2358 PyErr_Clear();
2359 return _PySequence_IterContains(self, value);
2360 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002361}
2362
Guido van Rossumdc91b992001-08-08 22:26:22 +00002363SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2364SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002365
2366#define slot_mp_length slot_sq_length
2367
Guido van Rossumdc91b992001-08-08 22:26:22 +00002368SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002369
2370static int
2371slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2372{
2373 PyObject *res;
2374
2375 if (value == NULL)
2376 res = PyObject_CallMethod(self, "__delitem__", "O", key);
2377 else
2378 res = PyObject_CallMethod(self, "__setitem__",
2379 "OO", key, value);
2380 if (res == NULL)
2381 return -1;
2382 Py_DECREF(res);
2383 return 0;
2384}
2385
Guido van Rossumdc91b992001-08-08 22:26:22 +00002386SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2387SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2388SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2389SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2390SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2391SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2392
2393staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2394
2395SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2396 nb_power, "__pow__", "__rpow__")
2397
2398static PyObject *
2399slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2400{
2401 if (modulus == Py_None)
2402 return slot_nb_power_binary(self, other);
2403 /* Three-arg power doesn't use __rpow__ */
2404 return PyObject_CallMethod(self, "__pow__", "OO", other, modulus);
2405}
2406
2407SLOT0(slot_nb_negative, "__neg__")
2408SLOT0(slot_nb_positive, "__pos__")
2409SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002410
2411static int
2412slot_nb_nonzero(PyObject *self)
2413{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002414 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002415
Guido van Rossumb8f63662001-08-15 23:57:02 +00002416 func = PyObject_GetAttrString(self, "__nonzero__");
2417 if (func == NULL) {
2418 PyErr_Clear();
2419 func = PyObject_GetAttrString(self, "__len__");
2420 }
2421
2422 if (func != NULL) {
2423 res = PyEval_CallObject(func, NULL);
2424 Py_DECREF(func);
2425 if (res == NULL)
2426 return -1;
2427 return PyObject_IsTrue(res);
2428 }
2429 else {
2430 PyErr_Clear();
2431 return 1;
2432 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433}
2434
Guido van Rossumdc91b992001-08-08 22:26:22 +00002435SLOT0(slot_nb_invert, "__invert__")
2436SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2437SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2438SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2439SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2440SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002441/* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002442SLOT0(slot_nb_int, "__int__")
2443SLOT0(slot_nb_long, "__long__")
2444SLOT0(slot_nb_float, "__float__")
2445SLOT0(slot_nb_oct, "__oct__")
2446SLOT0(slot_nb_hex, "__hex__")
2447SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2448SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2449SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2450SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2451SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2452SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2453SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2454SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2455SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2456SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2457SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2458SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2459 "__floordiv__", "__rfloordiv__")
2460SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2461SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2462SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002463
2464static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002465half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002466{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002467 PyObject *func, *args, *res;
2468 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002469
Guido van Rossumb8f63662001-08-15 23:57:02 +00002470 func = PyObject_GetAttrString(self, "__cmp__");
2471 if (func == NULL) {
2472 PyErr_Clear();
2473 }
2474 else {
2475 args = Py_BuildValue("(O)", other);
2476 if (args == NULL)
2477 res = NULL;
2478 else {
2479 res = PyObject_CallObject(func, args);
2480 Py_DECREF(args);
2481 }
2482 if (res != Py_NotImplemented) {
2483 if (res == NULL)
2484 return -2;
2485 c = PyInt_AsLong(res);
2486 Py_DECREF(res);
2487 if (c == -1 && PyErr_Occurred())
2488 return -2;
2489 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
2490 }
2491 Py_DECREF(res);
2492 }
2493 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002494}
2495
Guido van Rossumb8f63662001-08-15 23:57:02 +00002496static int
2497slot_tp_compare(PyObject *self, PyObject *other)
2498{
2499 int c;
2500
2501 if (self->ob_type->tp_compare == slot_tp_compare) {
2502 c = half_compare(self, other);
2503 if (c <= 1)
2504 return c;
2505 }
2506 if (other->ob_type->tp_compare == slot_tp_compare) {
2507 c = half_compare(other, self);
2508 if (c < -1)
2509 return -2;
2510 if (c <= 1)
2511 return -c;
2512 }
2513 return (void *)self < (void *)other ? -1 :
2514 (void *)self > (void *)other ? 1 : 0;
2515}
2516
2517static PyObject *
2518slot_tp_repr(PyObject *self)
2519{
2520 PyObject *func, *res;
2521
2522 func = PyObject_GetAttrString(self, "__repr__");
2523 if (func != NULL) {
2524 res = PyEval_CallObject(func, NULL);
2525 Py_DECREF(func);
2526 return res;
2527 }
2528 else {
2529 char buf[120];
2530 PyErr_Clear();
2531 sprintf(buf, "<%.80s object at %p>",
2532 self->ob_type->tp_name, self);
2533 return PyString_FromString(buf);
2534 }
2535}
2536
2537static PyObject *
2538slot_tp_str(PyObject *self)
2539{
2540 PyObject *func, *res;
2541
2542 func = PyObject_GetAttrString(self, "__str__");
2543 if (func != NULL) {
2544 res = PyEval_CallObject(func, NULL);
2545 Py_DECREF(func);
2546 return res;
2547 }
2548 else {
2549 PyErr_Clear();
2550 return slot_tp_repr(self);
2551 }
2552}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002553
2554static long
2555slot_tp_hash(PyObject *self)
2556{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002557 PyObject *func, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002558 long h;
2559
Guido van Rossumb8f63662001-08-15 23:57:02 +00002560 func = PyObject_GetAttrString(self, "__hash__");
2561
2562 if (func != NULL) {
2563 res = PyEval_CallObject(func, NULL);
2564 Py_DECREF(func);
2565 if (res == NULL)
2566 return -1;
2567 h = PyInt_AsLong(res);
2568 }
2569 else {
2570 PyErr_Clear();
2571 func = PyObject_GetAttrString(self, "__eq__");
2572 if (func == NULL) {
2573 PyErr_Clear();
2574 func = PyObject_GetAttrString(self, "__cmp__");
2575 }
2576 if (func != NULL) {
2577 Py_DECREF(func);
2578 PyErr_SetString(PyExc_TypeError, "unhashable type");
2579 return -1;
2580 }
2581 PyErr_Clear();
2582 h = _Py_HashPointer((void *)self);
2583 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002584 if (h == -1 && !PyErr_Occurred())
2585 h = -2;
2586 return h;
2587}
2588
2589static PyObject *
2590slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
2591{
2592 PyObject *meth = PyObject_GetAttrString(self, "__call__");
2593 PyObject *res;
2594
2595 if (meth == NULL)
2596 return NULL;
2597 res = PyObject_Call(meth, args, kwds);
2598 Py_DECREF(meth);
2599 return res;
2600}
2601
Tim Peters6d6c1a32001-08-02 04:15:00 +00002602static PyObject *
2603slot_tp_getattro(PyObject *self, PyObject *name)
2604{
2605 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002606 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002607 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002608
Guido van Rossum8e248182001-08-12 05:17:56 +00002609 if (getattr_str == NULL) {
2610 getattr_str = PyString_InternFromString("__getattr__");
2611 if (getattr_str == NULL)
2612 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002613 }
Guido van Rossum8e248182001-08-12 05:17:56 +00002614 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00002615 if (getattr == NULL) {
2616 /* Avoid further slowdowns */
2617 if (tp->tp_getattro == slot_tp_getattro)
2618 tp->tp_getattro = PyObject_GenericGetAttr;
Guido van Rossum8e248182001-08-12 05:17:56 +00002619 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00002620 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002621 return PyObject_CallFunction(getattr, "OO", self, name);
2622}
2623
2624static int
2625slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
2626{
2627 PyObject *res;
2628
2629 if (value == NULL)
2630 res = PyObject_CallMethod(self, "__delattr__", "O", name);
2631 else
2632 res = PyObject_CallMethod(self, "__setattr__",
2633 "OO", name, value);
2634 if (res == NULL)
2635 return -1;
2636 Py_DECREF(res);
2637 return 0;
2638}
2639
2640/* Map rich comparison operators to their __xx__ namesakes */
2641static char *name_op[] = {
2642 "__lt__",
2643 "__le__",
2644 "__eq__",
2645 "__ne__",
2646 "__gt__",
2647 "__ge__",
2648};
2649
2650static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00002651half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002652{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002653 PyObject *func, *args, *res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002654
Guido van Rossumb8f63662001-08-15 23:57:02 +00002655 func = PyObject_GetAttrString(self, name_op[op]);
2656 if (func == NULL) {
2657 PyErr_Clear();
2658 Py_INCREF(Py_NotImplemented);
2659 return Py_NotImplemented;
2660 }
2661 args = Py_BuildValue("(O)", other);
2662 if (args == NULL)
2663 res = NULL;
2664 else {
2665 res = PyObject_CallObject(func, args);
2666 Py_DECREF(args);
2667 }
2668 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002669 return res;
2670}
2671
Guido van Rossumb8f63662001-08-15 23:57:02 +00002672/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
2673static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
2674
2675static PyObject *
2676slot_tp_richcompare(PyObject *self, PyObject *other, int op)
2677{
2678 PyObject *res;
2679
2680 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
2681 res = half_richcompare(self, other, op);
2682 if (res != Py_NotImplemented)
2683 return res;
2684 Py_DECREF(res);
2685 }
2686 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
2687 res = half_richcompare(other, self, swapped_op[op]);
2688 if (res != Py_NotImplemented) {
2689 return res;
2690 }
2691 Py_DECREF(res);
2692 }
2693 Py_INCREF(Py_NotImplemented);
2694 return Py_NotImplemented;
2695}
2696
2697static PyObject *
2698slot_tp_iter(PyObject *self)
2699{
2700 PyObject *func, *res;
2701
2702 func = PyObject_GetAttrString(self, "__iter__");
2703 if (func != NULL) {
2704 res = PyObject_CallObject(func, NULL);
2705 Py_DECREF(func);
2706 return res;
2707 }
2708 PyErr_Clear();
2709 func = PyObject_GetAttrString(self, "__getitem__");
2710 if (func == NULL) {
2711 PyErr_SetString(PyExc_TypeError, "iter() of non-sequence");
2712 return NULL;
2713 }
2714 Py_DECREF(func);
2715 return PySeqIter_New(self);
2716}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002717
2718static PyObject *
2719slot_tp_iternext(PyObject *self)
2720{
2721 return PyObject_CallMethod(self, "next", "");
2722}
2723
Guido van Rossum1a493502001-08-17 16:47:50 +00002724static PyObject *
2725slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
2726{
2727 PyTypeObject *tp = self->ob_type;
2728 PyObject *get;
2729 static PyObject *get_str = NULL;
2730
2731 if (get_str == NULL) {
2732 get_str = PyString_InternFromString("__get__");
2733 if (get_str == NULL)
2734 return NULL;
2735 }
2736 get = _PyType_Lookup(tp, get_str);
2737 if (get == NULL) {
2738 /* Avoid further slowdowns */
2739 if (tp->tp_descr_get == slot_tp_descr_get)
2740 tp->tp_descr_get = NULL;
2741 Py_INCREF(self);
2742 return self;
2743 }
2744 return PyObject_CallFunction(get, "OOO", self, obj, type);
2745}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002746
2747static int
2748slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
2749{
2750 PyObject *res = PyObject_CallMethod(self, "__set__",
2751 "OO", target, value);
2752 if (res == NULL)
2753 return -1;
2754 Py_DECREF(res);
2755 return 0;
2756}
2757
2758static int
2759slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
2760{
2761 PyObject *meth = PyObject_GetAttrString(self, "__init__");
2762 PyObject *res;
2763
2764 if (meth == NULL)
2765 return -1;
2766 res = PyObject_Call(meth, args, kwds);
2767 Py_DECREF(meth);
2768 if (res == NULL)
2769 return -1;
2770 Py_DECREF(res);
2771 return 0;
2772}
2773
2774static PyObject *
2775slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2776{
2777 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
2778 PyObject *newargs, *x;
2779 int i, n;
2780
2781 if (func == NULL)
2782 return NULL;
2783 assert(PyTuple_Check(args));
2784 n = PyTuple_GET_SIZE(args);
2785 newargs = PyTuple_New(n+1);
2786 if (newargs == NULL)
2787 return NULL;
2788 Py_INCREF(type);
2789 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
2790 for (i = 0; i < n; i++) {
2791 x = PyTuple_GET_ITEM(args, i);
2792 Py_INCREF(x);
2793 PyTuple_SET_ITEM(newargs, i+1, x);
2794 }
2795 x = PyObject_Call(func, newargs, kwds);
2796 Py_DECREF(func);
2797 return x;
2798}
2799
Guido van Rossumf040ede2001-08-07 16:40:56 +00002800/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002801 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00002802 The dict argument is the dictionary argument passed to type_new(),
2803 which is the local namespace of the class statement, in other
2804 words, it contains the methods. For each special method (like
2805 __repr__) defined in the dictionary, the corresponding function
2806 slot in the type object (like tp_repr) is set to a special function
2807 whose name is 'slot_' followed by the slot name and whose signature
2808 is whatever is required for that slot. These slot functions look
2809 up the corresponding method in the type's dictionary and call it.
2810 The slot functions have to take care of the various peculiarities
2811 of the mapping between slots and special methods, such as mapping
2812 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
2813 etc.) or mapping multiple slots to a single method (sq_item,
2814 mp_subscript <--> __getitem__). */
2815
Tim Peters6d6c1a32001-08-02 04:15:00 +00002816static void
2817override_slots(PyTypeObject *type, PyObject *dict)
2818{
2819 PySequenceMethods *sq = type->tp_as_sequence;
2820 PyMappingMethods *mp = type->tp_as_mapping;
2821 PyNumberMethods *nb = type->tp_as_number;
2822
Guido van Rossumdc91b992001-08-08 22:26:22 +00002823#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002824 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002825 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002826 }
2827
Guido van Rossumdc91b992001-08-08 22:26:22 +00002828#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002829 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002830 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002831 }
2832
Guido van Rossumdc91b992001-08-08 22:26:22 +00002833#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002834 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002835 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002836 }
2837
Guido van Rossumdc91b992001-08-08 22:26:22 +00002838#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00002839 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002840 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002841 }
2842
Guido van Rossumdc91b992001-08-08 22:26:22 +00002843 SQSLOT("__len__", sq_length, slot_sq_length);
2844 SQSLOT("__add__", sq_concat, slot_sq_concat);
2845 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
2846 SQSLOT("__getitem__", sq_item, slot_sq_item);
2847 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
2848 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
2849 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
2850 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
2851 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
2852 SQSLOT("__contains__", sq_contains, slot_sq_contains);
2853 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
2854 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002855
Guido van Rossumdc91b992001-08-08 22:26:22 +00002856 MPSLOT("__len__", mp_length, slot_mp_length);
2857 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
2858 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
2859 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002860
Guido van Rossumdc91b992001-08-08 22:26:22 +00002861 NBSLOT("__add__", nb_add, slot_nb_add);
2862 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
2863 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
2864 NBSLOT("__div__", nb_divide, slot_nb_divide);
2865 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
2866 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
2867 NBSLOT("__pow__", nb_power, slot_nb_power);
2868 NBSLOT("__neg__", nb_negative, slot_nb_negative);
2869 NBSLOT("__pos__", nb_positive, slot_nb_positive);
2870 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
2871 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
2872 NBSLOT("__invert__", nb_invert, slot_nb_invert);
2873 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
2874 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
2875 NBSLOT("__and__", nb_and, slot_nb_and);
2876 NBSLOT("__xor__", nb_xor, slot_nb_xor);
2877 NBSLOT("__or__", nb_or, slot_nb_or);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002878 /* Not coerce() */
Guido van Rossumdc91b992001-08-08 22:26:22 +00002879 NBSLOT("__int__", nb_int, slot_nb_int);
2880 NBSLOT("__long__", nb_long, slot_nb_long);
2881 NBSLOT("__float__", nb_float, slot_nb_float);
2882 NBSLOT("__oct__", nb_oct, slot_nb_oct);
2883 NBSLOT("__hex__", nb_hex, slot_nb_hex);
2884 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
2885 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
2886 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
2887 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
2888 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
2889 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
2890 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
2891 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
2892 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
2893 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
2894 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
2895 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
2896 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
2897 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
2898 slot_nb_inplace_floor_divide);
2899 NBSLOT("__itruediv__", nb_inplace_true_divide,
2900 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002901
Guido van Rossum8e248182001-08-12 05:17:56 +00002902 if (dict == NULL ||
2903 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00002904 PyDict_GetItemString(dict, "__repr__"))
2905 type->tp_print = NULL;
2906
Guido van Rossumdc91b992001-08-08 22:26:22 +00002907 TPSLOT("__cmp__", tp_compare, slot_tp_compare);
2908 TPSLOT("__repr__", tp_repr, slot_tp_repr);
2909 TPSLOT("__hash__", tp_hash, slot_tp_hash);
2910 TPSLOT("__call__", tp_call, slot_tp_call);
2911 TPSLOT("__str__", tp_str, slot_tp_str);
2912 TPSLOT("__getattr__", tp_getattro, slot_tp_getattro);
2913 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
2914 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
2915 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
2916 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
2917 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
2918 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
2919 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
2920 TPSLOT("__iter__", tp_iter, slot_tp_iter);
2921 TPSLOT("next", tp_iternext, slot_tp_iternext);
2922 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
2923 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
2924 TPSLOT("__init__", tp_init, slot_tp_init);
2925 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002926}