blob: e4f07e534e31839d82148a17927bfcf471d10cac [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
Guido van Rossum6f799372001-09-20 20:46:19 +00007static PyMemberDef 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
Guido van Rossum3926a632001-09-25 16:25:58 +000056static int
57type_set_module(PyTypeObject *type, PyObject *value, void *context)
58{
59 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ||
60 strrchr(type->tp_name, '.')) {
61 PyErr_Format(PyExc_TypeError,
62 "can't set %s.__module__", type->tp_name);
63 return -1;
64 }
65 if (!value) {
66 PyErr_Format(PyExc_TypeError,
67 "can't delete %s.__module__", type->tp_name);
68 return -1;
69 }
70 return PyDict_SetItemString(type->tp_dict, "__module__", value);
71}
72
Tim Peters6d6c1a32001-08-02 04:15:00 +000073static PyObject *
74type_dict(PyTypeObject *type, void *context)
75{
76 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 Py_INCREF(Py_None);
78 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000079 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000080 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
81 Py_INCREF(type->tp_dict);
82 return type->tp_dict;
83 }
84 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000085}
86
Tim Peters6d6c1a32001-08-02 04:15:00 +000087static PyObject *
88type_defined(PyTypeObject *type, void *context)
89{
90 if (type->tp_defined == NULL) {
91 Py_INCREF(Py_None);
92 return Py_None;
93 }
94 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) {
95 Py_INCREF(type->tp_defined);
96 return type->tp_defined;
97 }
98 return PyDictProxy_New(type->tp_defined);
99}
100
101static PyObject *
102type_dynamic(PyTypeObject *type, void *context)
103{
104 PyObject *res;
105
106 res = (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE) ? Py_True : Py_False;
107 Py_INCREF(res);
108 return res;
109}
110
Guido van Rossum32d34c82001-09-20 21:45:26 +0000111PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +0000112 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000113 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000114 {"__dict__", (getter)type_dict, NULL, NULL},
115 {"__defined__", (getter)type_defined, NULL, NULL},
116 {"__dynamic__", (getter)type_dynamic, NULL, NULL},
117 {0}
118};
119
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000120static int
121type_compare(PyObject *v, PyObject *w)
122{
123 /* This is called with type objects only. So we
124 can just compare the addresses. */
125 Py_uintptr_t vv = (Py_uintptr_t)v;
126 Py_uintptr_t ww = (Py_uintptr_t)w;
127 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
128}
129
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000130static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000131type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000132{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000133 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000134 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000135
136 mod = type_module(type, NULL);
137 if (mod == NULL)
138 PyErr_Clear();
139 else if (!PyString_Check(mod)) {
140 Py_DECREF(mod);
141 mod = NULL;
142 }
143 name = type_name(type, NULL);
144 if (name == NULL)
145 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000146
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000147 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
148 kind = "class";
149 else
150 kind = "type";
151
Barry Warsaw7ce36942001-08-24 18:34:26 +0000152 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000153 rtn = PyString_FromFormat("<%s '%s.%s'>",
154 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000155 PyString_AS_STRING(mod),
156 PyString_AS_STRING(name));
157 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000158 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000159 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000160
Guido van Rossumc3542212001-08-16 09:18:56 +0000161 Py_XDECREF(mod);
162 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000163 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000164}
165
Tim Peters6d6c1a32001-08-02 04:15:00 +0000166static PyObject *
167type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
168{
169 PyObject *obj;
170
171 if (type->tp_new == NULL) {
172 PyErr_Format(PyExc_TypeError,
173 "cannot create '%.100s' instances",
174 type->tp_name);
175 return NULL;
176 }
177
Tim Peters3f996e72001-09-13 19:18:27 +0000178 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000179 if (obj != NULL) {
180 type = obj->ob_type;
181 if (type->tp_init != NULL &&
182 type->tp_init(obj, args, kwds) < 0) {
183 Py_DECREF(obj);
184 obj = NULL;
185 }
186 }
187 return obj;
188}
189
190PyObject *
191PyType_GenericAlloc(PyTypeObject *type, int nitems)
192{
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000193#define PTRSIZE (sizeof(PyObject *))
194
Tim Peters6d6c1a32001-08-02 04:15:00 +0000195 int size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000196 PyObject *obj;
197
198 /* Inline PyObject_New() so we can zero the memory */
199 size = _PyObject_VAR_SIZE(type, nitems);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000200 /* Round up size, if necessary, so we fully zero out __dict__ */
201 if (type->tp_itemsize % PTRSIZE != 0) {
202 size += PTRSIZE - 1;
203 size /= PTRSIZE;
204 size *= PTRSIZE;
205 }
Neil Schemenauerc806c882001-08-29 23:54:54 +0000206 if (PyType_IS_GC(type)) {
207 obj = _PyObject_GC_Malloc(type, nitems);
208 }
209 else {
210 obj = PyObject_MALLOC(size);
211 }
212 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000213 return PyErr_NoMemory();
Neil Schemenauerc806c882001-08-29 23:54:54 +0000214 memset(obj, '\0', size);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
216 Py_INCREF(type);
217 if (type->tp_itemsize == 0)
218 PyObject_INIT(obj, type);
219 else
220 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
221 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000222 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000223 return obj;
224}
225
226PyObject *
227PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
228{
229 return type->tp_alloc(type, 0);
230}
231
232/* Helper for subtyping */
233
234static void
235subtype_dealloc(PyObject *self)
236{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000237 PyTypeObject *type, *base;
238 destructor f;
239
240 /* This exists so we can DECREF self->ob_type */
241
242 /* Find the nearest base with a different tp_dealloc */
243 type = self->ob_type;
244 base = type->tp_base;
245 while ((f = base->tp_dealloc) == subtype_dealloc) {
246 base = base->tp_base;
247 assert(base);
248 }
249
250 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000251 if (type->tp_dictoffset && !base->tp_dictoffset) {
252 PyObject **dictptr = _PyObject_GetDictPtr(self);
253 if (dictptr != NULL) {
254 PyObject *dict = *dictptr;
255 if (dict != NULL) {
256 Py_DECREF(dict);
257 *dictptr = NULL;
258 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000259 }
260 }
261
Guido van Rossum9676b222001-08-17 20:32:36 +0000262 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000263 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000264 PyObject_ClearWeakRefs(self);
265
Tim Peters6d6c1a32001-08-02 04:15:00 +0000266 /* Finalize GC if the base doesn't do GC and we do */
267 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000268 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000269
270 /* Call the base tp_dealloc() */
271 assert(f);
272 f(self);
273
274 /* Can't reference self beyond this point */
275 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
276 Py_DECREF(type);
277 }
278}
279
280staticforward void override_slots(PyTypeObject *type, PyObject *dict);
281staticforward PyTypeObject *solid_base(PyTypeObject *type);
282
283typedef struct {
284 PyTypeObject type;
285 PyNumberMethods as_number;
286 PySequenceMethods as_sequence;
287 PyMappingMethods as_mapping;
288 PyBufferProcs as_buffer;
289 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000290 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000291} etype;
292
293/* type test with subclassing support */
294
295int
296PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
297{
298 PyObject *mro;
299
Guido van Rossum9478d072001-09-07 18:52:13 +0000300 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
301 return b == a || b == &PyBaseObject_Type;
302
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303 mro = a->tp_mro;
304 if (mro != NULL) {
305 /* Deal with multiple inheritance without recursion
306 by walking the MRO tuple */
307 int i, n;
308 assert(PyTuple_Check(mro));
309 n = PyTuple_GET_SIZE(mro);
310 for (i = 0; i < n; i++) {
311 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
312 return 1;
313 }
314 return 0;
315 }
316 else {
317 /* a is not completely initilized yet; follow tp_base */
318 do {
319 if (a == b)
320 return 1;
321 a = a->tp_base;
322 } while (a != NULL);
323 return b == &PyBaseObject_Type;
324 }
325}
326
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000327/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000328 without looking in the instance dictionary
329 (so we can't use PyObject_GetAttr) but still binding
330 it to the instance. The arguments are the object,
331 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000332 static variable used to cache the interned Python string.
333
334 Two variants:
335
336 - lookup_maybe() returns NULL without raising an exception
337 when the _PyType_Lookup() call fails;
338
339 - lookup_method() always raises an exception upon errors.
340*/
Guido van Rossum60718732001-08-28 17:47:51 +0000341
342static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000343lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000344{
345 PyObject *res;
346
347 if (*attrobj == NULL) {
348 *attrobj = PyString_InternFromString(attrstr);
349 if (*attrobj == NULL)
350 return NULL;
351 }
352 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000353 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000354 descrgetfunc f;
355 if ((f = res->ob_type->tp_descr_get) == NULL)
356 Py_INCREF(res);
357 else
358 res = f(res, self, (PyObject *)(self->ob_type));
359 }
360 return res;
361}
362
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000363static PyObject *
364lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
365{
366 PyObject *res = lookup_maybe(self, attrstr, attrobj);
367 if (res == NULL && !PyErr_Occurred())
368 PyErr_SetObject(PyExc_AttributeError, *attrobj);
369 return res;
370}
371
Guido van Rossum2730b132001-08-28 18:22:14 +0000372/* A variation of PyObject_CallMethod that uses lookup_method()
373 instead of PyObject_GetAttrString(). This uses the same convention
374 as lookup_method to cache the interned name string object. */
375
376PyObject *
377call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
378{
379 va_list va;
380 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000381 va_start(va, format);
382
Guido van Rossumda21c012001-10-03 00:50:18 +0000383 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000384 if (func == NULL) {
385 va_end(va);
386 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000387 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000388 return NULL;
389 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000390
391 if (format && *format)
392 args = Py_VaBuildValue(format, va);
393 else
394 args = PyTuple_New(0);
395
396 va_end(va);
397
398 if (args == NULL)
399 return NULL;
400
401 assert(PyTuple_Check(args));
402 retval = PyObject_Call(func, args, NULL);
403
404 Py_DECREF(args);
405 Py_DECREF(func);
406
407 return retval;
408}
409
410/* Clone of call_method() that returns NotImplemented when the lookup fails. */
411
412PyObject *
413call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
414{
415 va_list va;
416 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000417 va_start(va, format);
418
Guido van Rossumda21c012001-10-03 00:50:18 +0000419 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000420 if (func == NULL) {
421 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000422 if (!PyErr_Occurred()) {
423 Py_INCREF(Py_NotImplemented);
424 return Py_NotImplemented;
425 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000426 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000427 }
428
429 if (format && *format)
430 args = Py_VaBuildValue(format, va);
431 else
432 args = PyTuple_New(0);
433
434 va_end(va);
435
Guido van Rossum717ce002001-09-14 16:58:08 +0000436 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000437 return NULL;
438
Guido van Rossum717ce002001-09-14 16:58:08 +0000439 assert(PyTuple_Check(args));
440 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000441
442 Py_DECREF(args);
443 Py_DECREF(func);
444
445 return retval;
446}
447
Tim Peters6d6c1a32001-08-02 04:15:00 +0000448/* Method resolution order algorithm from "Putting Metaclasses to Work"
449 by Forman and Danforth (Addison-Wesley 1999). */
450
451static int
452conservative_merge(PyObject *left, PyObject *right)
453{
454 int left_size;
455 int right_size;
456 int i, j, r, ok;
457 PyObject *temp, *rr;
458
459 assert(PyList_Check(left));
460 assert(PyList_Check(right));
461
462 again:
463 left_size = PyList_GET_SIZE(left);
464 right_size = PyList_GET_SIZE(right);
465 for (i = 0; i < left_size; i++) {
466 for (j = 0; j < right_size; j++) {
467 if (PyList_GET_ITEM(left, i) ==
468 PyList_GET_ITEM(right, j)) {
469 /* found a merge point */
470 temp = PyList_New(0);
471 if (temp == NULL)
472 return -1;
473 for (r = 0; r < j; r++) {
474 rr = PyList_GET_ITEM(right, r);
475 ok = PySequence_Contains(left, rr);
476 if (ok < 0) {
477 Py_DECREF(temp);
478 return -1;
479 }
480 if (!ok) {
481 ok = PyList_Append(temp, rr);
482 if (ok < 0) {
483 Py_DECREF(temp);
484 return -1;
485 }
486 }
487 }
488 ok = PyList_SetSlice(left, i, i, temp);
489 Py_DECREF(temp);
490 if (ok < 0)
491 return -1;
492 ok = PyList_SetSlice(right, 0, j+1, NULL);
493 if (ok < 0)
494 return -1;
495 goto again;
496 }
497 }
498 }
499 return PyList_SetSlice(left, left_size, left_size, right);
500}
501
502static int
503serious_order_disagreements(PyObject *left, PyObject *right)
504{
505 return 0; /* XXX later -- for now, we cheat: "don't do that" */
506}
507
508static PyObject *
509mro_implementation(PyTypeObject *type)
510{
511 int i, n, ok;
512 PyObject *bases, *result;
513
514 bases = type->tp_bases;
515 n = PyTuple_GET_SIZE(bases);
516 result = Py_BuildValue("[O]", (PyObject *)type);
517 if (result == NULL)
518 return NULL;
519 for (i = 0; i < n; i++) {
520 PyTypeObject *base =
521 (PyTypeObject *) PyTuple_GET_ITEM(bases, i);
522 PyObject *parentMRO = PySequence_List(base->tp_mro);
523 if (parentMRO == NULL) {
524 Py_DECREF(result);
525 return NULL;
526 }
527 if (serious_order_disagreements(result, parentMRO)) {
528 Py_DECREF(result);
529 return NULL;
530 }
531 ok = conservative_merge(result, parentMRO);
532 Py_DECREF(parentMRO);
533 if (ok < 0) {
534 Py_DECREF(result);
535 return NULL;
536 }
537 }
538 return result;
539}
540
541static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000542mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000543{
544 PyTypeObject *type = (PyTypeObject *)self;
545
Tim Peters6d6c1a32001-08-02 04:15:00 +0000546 return mro_implementation(type);
547}
548
549static int
550mro_internal(PyTypeObject *type)
551{
552 PyObject *mro, *result, *tuple;
553
554 if (type->ob_type == &PyType_Type) {
555 result = mro_implementation(type);
556 }
557 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000558 static PyObject *mro_str;
559 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000560 if (mro == NULL)
561 return -1;
562 result = PyObject_CallObject(mro, NULL);
563 Py_DECREF(mro);
564 }
565 if (result == NULL)
566 return -1;
567 tuple = PySequence_Tuple(result);
568 Py_DECREF(result);
569 type->tp_mro = tuple;
570 return 0;
571}
572
573
574/* Calculate the best base amongst multiple base classes.
575 This is the first one that's on the path to the "solid base". */
576
577static PyTypeObject *
578best_base(PyObject *bases)
579{
580 int i, n;
581 PyTypeObject *base, *winner, *candidate, *base_i;
582
583 assert(PyTuple_Check(bases));
584 n = PyTuple_GET_SIZE(bases);
585 assert(n > 0);
586 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, 0);
587 winner = &PyBaseObject_Type;
588 for (i = 0; i < n; i++) {
589 base_i = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
590 if (!PyType_Check((PyObject *)base_i)) {
591 PyErr_SetString(
592 PyExc_TypeError,
593 "bases must be types");
594 return NULL;
595 }
596 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000597 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000598 return NULL;
599 }
600 candidate = solid_base(base_i);
601 if (PyType_IsSubtype(winner, candidate))
602 ;
603 else if (PyType_IsSubtype(candidate, winner)) {
604 winner = candidate;
605 base = base_i;
606 }
607 else {
608 PyErr_SetString(
609 PyExc_TypeError,
610 "multiple bases have "
611 "instance lay-out conflict");
612 return NULL;
613 }
614 }
615 assert(base != NULL);
616 return base;
617}
618
619static int
620extra_ivars(PyTypeObject *type, PyTypeObject *base)
621{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000622 size_t t_size = type->tp_basicsize;
623 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000624
Guido van Rossum9676b222001-08-17 20:32:36 +0000625 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000626 if (type->tp_itemsize || base->tp_itemsize) {
627 /* If itemsize is involved, stricter rules */
628 return t_size != b_size ||
629 type->tp_itemsize != base->tp_itemsize;
630 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000631 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
632 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
633 t_size -= sizeof(PyObject *);
634 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
635 type->tp_dictoffset + sizeof(PyObject *) == t_size)
636 t_size -= sizeof(PyObject *);
637
638 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000639}
640
641static PyTypeObject *
642solid_base(PyTypeObject *type)
643{
644 PyTypeObject *base;
645
646 if (type->tp_base)
647 base = solid_base(type->tp_base);
648 else
649 base = &PyBaseObject_Type;
650 if (extra_ivars(type, base))
651 return type;
652 else
653 return base;
654}
655
656staticforward void object_dealloc(PyObject *);
657staticforward int object_init(PyObject *, PyObject *, PyObject *);
658
659static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000660subtype_dict(PyObject *obj, void *context)
661{
662 PyObject **dictptr = _PyObject_GetDictPtr(obj);
663 PyObject *dict;
664
665 if (dictptr == NULL) {
666 PyErr_SetString(PyExc_AttributeError,
667 "This object has no __dict__");
668 return NULL;
669 }
670 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000671 if (dict == NULL)
672 *dictptr = dict = PyDict_New();
673 Py_XINCREF(dict);
674 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000675}
676
Guido van Rossum32d34c82001-09-20 21:45:26 +0000677PyGetSetDef subtype_getsets[] = {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000678 {"__dict__", subtype_dict, NULL, NULL},
679 {0},
680};
681
682static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
684{
685 PyObject *name, *bases, *dict;
686 static char *kwlist[] = {"name", "bases", "dict", 0};
687 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000688 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000689 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000690 PyMemberDef *mp;
Guido van Rossum9676b222001-08-17 20:32:36 +0000691 int i, nbases, nslots, slotoffset, dynamic, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000692
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000693 /* Special case: type(x) should return x->ob_type */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000694 if (metatype == &PyType_Type &&
695 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
696 (kwds == NULL || (PyDict_Check(kwds) && PyDict_Size(kwds) == 0))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697 PyObject *x = PyTuple_GET_ITEM(args, 0);
698 Py_INCREF(x->ob_type);
699 return (PyObject *) x->ob_type;
700 }
701
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000702 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000703 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
704 &name,
705 &PyTuple_Type, &bases,
706 &PyDict_Type, &dict))
707 return NULL;
708
709 /* Determine the proper metatype to deal with this,
710 and check for metatype conflicts while we're at it.
711 Note that if some other metatype wins to contract,
712 it's possible that its instances are not types. */
713 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000714 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000715 for (i = 0; i < nbases; i++) {
716 tmp = PyTuple_GET_ITEM(bases, i);
717 tmptype = tmp->ob_type;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000718 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000720 if (PyType_IsSubtype(tmptype, winner)) {
721 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000722 continue;
723 }
724 PyErr_SetString(PyExc_TypeError,
725 "metatype conflict among bases");
726 return NULL;
727 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000728 if (winner != metatype) {
729 if (winner->tp_new != type_new) /* Pass it to the winner */
730 return winner->tp_new(winner, args, kwds);
731 metatype = winner;
732 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000733
734 /* Adjust for empty tuple bases */
735 if (nbases == 0) {
736 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
737 if (bases == NULL)
738 return NULL;
739 nbases = 1;
740 }
741 else
742 Py_INCREF(bases);
743
744 /* XXX From here until type is allocated, "return NULL" leaks bases! */
745
746 /* Calculate best base, and check that all bases are type objects */
747 base = best_base(bases);
748 if (base == NULL)
749 return NULL;
750 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
751 PyErr_Format(PyExc_TypeError,
752 "type '%.100s' is not an acceptable base type",
753 base->tp_name);
754 return NULL;
755 }
756
Guido van Rossum1a493502001-08-17 16:47:50 +0000757 /* Should this be a dynamic class (i.e. modifiable __dict__)?
758 Look in two places for a variable named __dynamic__:
759 1) in the class dict
760 2) in the module dict (globals)
761 The first variable that is an int >= 0 is used.
Guido van Rossum50fda3b2001-10-04 19:46:06 +0000762 Otherwise, the default is dynamic. */
Guido van Rossum1a493502001-08-17 16:47:50 +0000763 dynamic = -1; /* Not yet determined */
764 /* Look in the class */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765 tmp = PyDict_GetItemString(dict, "__dynamic__");
766 if (tmp != NULL) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000767 dynamic = PyInt_AsLong(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000768 if (dynamic < 0)
Guido van Rossum1a493502001-08-17 16:47:50 +0000769 PyErr_Clear();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770 }
Guido van Rossum1a493502001-08-17 16:47:50 +0000771 if (dynamic < 0) {
772 /* Look in the module globals */
773 tmp = PyEval_GetGlobals();
774 if (tmp != NULL) {
775 tmp = PyDict_GetItemString(tmp, "__dynamic__");
776 if (tmp != NULL) {
777 dynamic = PyInt_AsLong(tmp);
778 if (dynamic < 0)
779 PyErr_Clear();
780 }
781 }
782 }
783 if (dynamic < 0) {
Guido van Rossum50fda3b2001-10-04 19:46:06 +0000784 /* Default to dynamic */
785 dynamic = 1;
786
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787 }
788
789 /* Check for a __slots__ sequence variable in dict, and count it */
790 slots = PyDict_GetItemString(dict, "__slots__");
791 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000792 add_dict = 0;
793 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000794 if (slots != NULL) {
795 /* Make it into a tuple */
796 if (PyString_Check(slots))
797 slots = Py_BuildValue("(O)", slots);
798 else
799 slots = PySequence_Tuple(slots);
800 if (slots == NULL)
801 return NULL;
802 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000803 if (nslots > 0 && base->tp_itemsize != 0) {
804 PyErr_Format(PyExc_TypeError,
805 "nonempty __slots__ "
806 "not supported for subtype of '%s'",
807 base->tp_name);
808 return NULL;
809 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810 for (i = 0; i < nslots; i++) {
811 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
812 PyErr_SetString(PyExc_TypeError,
813 "__slots__ must be a sequence of strings");
814 Py_DECREF(slots);
815 return NULL;
816 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000817 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818 }
819 }
820 if (slots == NULL && base->tp_dictoffset == 0 &&
821 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000822 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000823 add_dict++;
824 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000825 if (slots == NULL && base->tp_weaklistoffset == 0 &&
826 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000827 nslots++;
828 add_weak++;
829 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830
831 /* XXX From here until type is safely allocated,
832 "return NULL" may leak slots! */
833
834 /* Allocate the type object */
835 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
836 if (type == NULL)
837 return NULL;
838
839 /* Keep name and slots alive in the extended type object */
840 et = (etype *)type;
841 Py_INCREF(name);
842 et->name = name;
843 et->slots = slots;
844
Guido van Rossumdc91b992001-08-08 22:26:22 +0000845 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000846 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
847 Py_TPFLAGS_BASETYPE;
848 if (dynamic)
849 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +0000850 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
851 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000852
853 /* It's a new-style number unless it specifically inherits any
854 old-style numeric behavior */
855 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
856 (base->tp_as_number == NULL))
857 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
858
859 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860 type->tp_as_number = &et->as_number;
861 type->tp_as_sequence = &et->as_sequence;
862 type->tp_as_mapping = &et->as_mapping;
863 type->tp_as_buffer = &et->as_buffer;
864 type->tp_name = PyString_AS_STRING(name);
865
866 /* Set tp_base and tp_bases */
867 type->tp_bases = bases;
868 Py_INCREF(base);
869 type->tp_base = base;
870
871 /* Initialize tp_defined from passed-in dict */
872 type->tp_defined = dict = PyDict_Copy(dict);
873 if (dict == NULL) {
874 Py_DECREF(type);
875 return NULL;
876 }
877
Guido van Rossumc3542212001-08-16 09:18:56 +0000878 /* Set __module__ in the dict */
879 if (PyDict_GetItemString(dict, "__module__") == NULL) {
880 tmp = PyEval_GetGlobals();
881 if (tmp != NULL) {
882 tmp = PyDict_GetItemString(tmp, "__name__");
883 if (tmp != NULL) {
884 if (PyDict_SetItemString(dict, "__module__",
885 tmp) < 0)
886 return NULL;
887 }
888 }
889 }
890
Tim Peters2f93e282001-10-04 05:27:00 +0000891 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
892 and is a string (tp_doc is a char* -- can't copy a general object
893 into it).
894 XXX What if it's a Unicode string? Don't know -- this ignores it.
895 */
896 {
897 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
898 if (doc != NULL && PyString_Check(doc)) {
899 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +0000900 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +0000901 if (type->tp_doc == NULL) {
902 Py_DECREF(type);
903 return NULL;
904 }
905 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
906 }
907 }
908
Tim Peters6d6c1a32001-08-02 04:15:00 +0000909 /* Special-case __new__: if it's a plain function,
910 make it a static function */
911 tmp = PyDict_GetItemString(dict, "__new__");
912 if (tmp != NULL && PyFunction_Check(tmp)) {
913 tmp = PyStaticMethod_New(tmp);
914 if (tmp == NULL) {
915 Py_DECREF(type);
916 return NULL;
917 }
918 PyDict_SetItemString(dict, "__new__", tmp);
919 Py_DECREF(tmp);
920 }
921
922 /* Add descriptors for custom slots from __slots__, or for __dict__ */
923 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000924 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000925 if (slots != NULL) {
926 for (i = 0; i < nslots; i++, mp++) {
927 mp->name = PyString_AS_STRING(
928 PyTuple_GET_ITEM(slots, i));
929 mp->type = T_OBJECT;
930 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000931 if (base->tp_weaklistoffset == 0 &&
932 strcmp(mp->name, "__weakref__") == 0)
933 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000934 slotoffset += sizeof(PyObject *);
935 }
936 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000937 else {
938 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000939 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +0000940 type->tp_dictoffset =
941 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000942 else
943 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000944 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000945 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000946 }
947 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000948 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000949 type->tp_weaklistoffset = slotoffset;
950 mp->name = "__weakref__";
951 mp->type = T_OBJECT;
952 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +0000953 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +0000954 mp++;
955 slotoffset += sizeof(PyObject *);
956 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000957 }
958 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000959 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000960 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961
962 /* Special case some slots */
963 if (type->tp_dictoffset != 0 || nslots > 0) {
964 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
965 type->tp_getattro = PyObject_GenericGetAttr;
966 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
967 type->tp_setattro = PyObject_GenericSetAttr;
968 }
969 type->tp_dealloc = subtype_dealloc;
970
971 /* Always override allocation strategy to use regular heap */
972 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +0000973 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
974 type->tp_free = _PyObject_GC_Del;
975 type->tp_traverse = base->tp_traverse;
976 type->tp_clear = base->tp_clear;
977 }
978 else
979 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000980
981 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000982 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000983 Py_DECREF(type);
984 return NULL;
985 }
986
987 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +0000988 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
989 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +0000990
Tim Peters6d6c1a32001-08-02 04:15:00 +0000991 return (PyObject *)type;
992}
993
994/* Internal API to look for a name through the MRO.
995 This returns a borrowed reference, and doesn't set an exception! */
996PyObject *
997_PyType_Lookup(PyTypeObject *type, PyObject *name)
998{
999 int i, n;
1000 PyObject *mro, *res, *dict;
1001
1002 /* For static types, look in tp_dict */
1003 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
1004 dict = type->tp_dict;
1005 assert(dict && PyDict_Check(dict));
1006 return PyDict_GetItem(dict, name);
1007 }
1008
1009 /* For dynamic types, look in tp_defined of types in MRO */
1010 mro = type->tp_mro;
1011 assert(PyTuple_Check(mro));
1012 n = PyTuple_GET_SIZE(mro);
1013 for (i = 0; i < n; i++) {
1014 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
1015 assert(PyType_Check(type));
1016 dict = type->tp_defined;
1017 assert(dict && PyDict_Check(dict));
1018 res = PyDict_GetItem(dict, name);
1019 if (res != NULL)
1020 return res;
1021 }
1022 return NULL;
1023}
1024
1025/* This is similar to PyObject_GenericGetAttr(),
1026 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1027static PyObject *
1028type_getattro(PyTypeObject *type, PyObject *name)
1029{
1030 PyTypeObject *metatype = type->ob_type;
1031 PyObject *descr, *res;
1032 descrgetfunc f;
1033
1034 /* Initialize this type (we'll assume the metatype is initialized) */
1035 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001036 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 return NULL;
1038 }
1039
1040 /* Get a descriptor from the metatype */
1041 descr = _PyType_Lookup(metatype, name);
1042 f = NULL;
1043 if (descr != NULL) {
1044 f = descr->ob_type->tp_descr_get;
1045 if (f != NULL && PyDescr_IsData(descr))
1046 return f(descr,
1047 (PyObject *)type, (PyObject *)metatype);
1048 }
1049
1050 /* Look in tp_defined of this type and its bases */
1051 res = _PyType_Lookup(type, name);
1052 if (res != NULL) {
1053 f = res->ob_type->tp_descr_get;
1054 if (f != NULL)
1055 return f(res, (PyObject *)NULL, (PyObject *)type);
1056 Py_INCREF(res);
1057 return res;
1058 }
1059
1060 /* Use the descriptor from the metatype */
1061 if (f != NULL) {
1062 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1063 return res;
1064 }
1065 if (descr != NULL) {
1066 Py_INCREF(descr);
1067 return descr;
1068 }
1069
1070 /* Give up */
1071 PyErr_Format(PyExc_AttributeError,
1072 "type object '%.50s' has no attribute '%.400s'",
1073 type->tp_name, PyString_AS_STRING(name));
1074 return NULL;
1075}
1076
1077static int
1078type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1079{
1080 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
1081 return PyObject_GenericSetAttr((PyObject *)type, name, value);
1082 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
1083 return -1;
1084}
1085
1086static void
1087type_dealloc(PyTypeObject *type)
1088{
1089 etype *et;
1090
1091 /* Assert this is a heap-allocated type object */
1092 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001093 _PyObject_GC_UNTRACK(type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001094 et = (etype *)type;
1095 Py_XDECREF(type->tp_base);
1096 Py_XDECREF(type->tp_dict);
1097 Py_XDECREF(type->tp_bases);
1098 Py_XDECREF(type->tp_mro);
1099 Py_XDECREF(type->tp_defined);
1100 /* XXX more? */
1101 Py_XDECREF(et->name);
1102 Py_XDECREF(et->slots);
1103 type->ob_type->tp_free((PyObject *)type);
1104}
1105
1106static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001107 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001108 "mro() -> list\nreturn a type's method resolution order"},
1109 {0}
1110};
1111
1112static char type_doc[] =
1113"type(object) -> the object's type\n"
1114"type(name, bases, dict) -> a new type";
1115
Guido van Rossum048eb752001-10-02 21:24:57 +00001116static int
1117type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1118{
1119 etype *et;
1120 int err;
1121
1122 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1123 return 0;
1124
1125 et = (etype *)type;
1126
1127#define VISIT(SLOT) \
1128 if (SLOT) { \
1129 err = visit((PyObject *)(SLOT), arg); \
1130 if (err) \
1131 return err; \
1132 }
1133
1134 VISIT(type->tp_dict);
1135 VISIT(type->tp_defined);
1136 VISIT(type->tp_mro);
1137 VISIT(type->tp_bases);
1138 VISIT(type->tp_base);
1139 VISIT(et->slots);
1140
1141#undef VISIT
1142
1143 return 0;
1144}
1145
1146static int
1147type_clear(PyTypeObject *type)
1148{
1149 etype *et;
1150 PyObject *tmp;
1151
1152 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1153 return 0;
1154
1155 et = (etype *)type;
1156
1157#define CLEAR(SLOT) \
1158 if (SLOT) { \
1159 tmp = (PyObject *)(SLOT); \
1160 SLOT = NULL; \
1161 Py_DECREF(tmp); \
1162 }
1163
1164 CLEAR(type->tp_dict);
1165 CLEAR(type->tp_defined);
1166 CLEAR(type->tp_mro);
1167 CLEAR(type->tp_bases);
1168 CLEAR(type->tp_base);
1169 CLEAR(et->slots);
1170
Tim Peters2f93e282001-10-04 05:27:00 +00001171 if (type->tp_doc != NULL) {
1172 PyObject_FREE(type->tp_doc);
1173 type->tp_doc = NULL;
1174 }
1175
Guido van Rossum048eb752001-10-02 21:24:57 +00001176#undef CLEAR
1177
1178 return 0;
1179}
1180
1181static int
1182type_is_gc(PyTypeObject *type)
1183{
1184 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1185}
1186
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001187PyTypeObject PyType_Type = {
1188 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001189 0, /* ob_size */
1190 "type", /* tp_name */
1191 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001192 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001193 (destructor)type_dealloc, /* tp_dealloc */
1194 0, /* tp_print */
1195 0, /* tp_getattr */
1196 0, /* tp_setattr */
1197 type_compare, /* tp_compare */
1198 (reprfunc)type_repr, /* tp_repr */
1199 0, /* tp_as_number */
1200 0, /* tp_as_sequence */
1201 0, /* tp_as_mapping */
1202 (hashfunc)_Py_HashPointer, /* tp_hash */
1203 (ternaryfunc)type_call, /* tp_call */
1204 0, /* tp_str */
1205 (getattrofunc)type_getattro, /* tp_getattro */
1206 (setattrofunc)type_setattro, /* tp_setattro */
1207 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001208 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1209 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001211 (traverseproc)type_traverse, /* tp_traverse */
1212 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001213 0, /* tp_richcompare */
1214 0, /* tp_weaklistoffset */
1215 0, /* tp_iter */
1216 0, /* tp_iternext */
1217 type_methods, /* tp_methods */
1218 type_members, /* tp_members */
1219 type_getsets, /* tp_getset */
1220 0, /* tp_base */
1221 0, /* tp_dict */
1222 0, /* tp_descr_get */
1223 0, /* tp_descr_set */
1224 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1225 0, /* tp_init */
1226 0, /* tp_alloc */
1227 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001228 _PyObject_GC_Del, /* tp_free */
1229 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001230};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001231
1232
1233/* The base type of all types (eventually)... except itself. */
1234
1235static int
1236object_init(PyObject *self, PyObject *args, PyObject *kwds)
1237{
1238 return 0;
1239}
1240
1241static void
1242object_dealloc(PyObject *self)
1243{
1244 self->ob_type->tp_free(self);
1245}
1246
Guido van Rossum8e248182001-08-12 05:17:56 +00001247static PyObject *
1248object_repr(PyObject *self)
1249{
Guido van Rossum76e69632001-08-16 18:52:43 +00001250 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001251 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001252
Guido van Rossum76e69632001-08-16 18:52:43 +00001253 type = self->ob_type;
1254 mod = type_module(type, NULL);
1255 if (mod == NULL)
1256 PyErr_Clear();
1257 else if (!PyString_Check(mod)) {
1258 Py_DECREF(mod);
1259 mod = NULL;
1260 }
1261 name = type_name(type, NULL);
1262 if (name == NULL)
1263 return NULL;
1264 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001265 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001266 PyString_AS_STRING(mod),
1267 PyString_AS_STRING(name),
1268 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001269 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001270 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001271 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001272 Py_XDECREF(mod);
1273 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001274 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001275}
1276
Guido van Rossumb8f63662001-08-15 23:57:02 +00001277static PyObject *
1278object_str(PyObject *self)
1279{
1280 unaryfunc f;
1281
1282 f = self->ob_type->tp_repr;
1283 if (f == NULL)
1284 f = object_repr;
1285 return f(self);
1286}
1287
Guido van Rossum8e248182001-08-12 05:17:56 +00001288static long
1289object_hash(PyObject *self)
1290{
1291 return _Py_HashPointer(self);
1292}
Guido van Rossum8e248182001-08-12 05:17:56 +00001293
Tim Peters6d6c1a32001-08-02 04:15:00 +00001294static void
1295object_free(PyObject *self)
1296{
1297 PyObject_Del(self);
1298}
1299
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001300static PyObject *
1301object_get_class(PyObject *self, void *closure)
1302{
1303 Py_INCREF(self->ob_type);
1304 return (PyObject *)(self->ob_type);
1305}
1306
1307static int
1308equiv_structs(PyTypeObject *a, PyTypeObject *b)
1309{
1310 return a == b ||
1311 (a != NULL &&
1312 b != NULL &&
1313 a->tp_basicsize == b->tp_basicsize &&
1314 a->tp_itemsize == b->tp_itemsize &&
1315 a->tp_dictoffset == b->tp_dictoffset &&
1316 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1317 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1318 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1319}
1320
1321static int
1322same_slots_added(PyTypeObject *a, PyTypeObject *b)
1323{
1324 PyTypeObject *base = a->tp_base;
1325 int size;
1326
1327 if (base != b->tp_base)
1328 return 0;
1329 if (equiv_structs(a, base) && equiv_structs(b, base))
1330 return 1;
1331 size = base->tp_basicsize;
1332 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1333 size += sizeof(PyObject *);
1334 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1335 size += sizeof(PyObject *);
1336 return size == a->tp_basicsize && size == b->tp_basicsize;
1337}
1338
1339static int
1340object_set_class(PyObject *self, PyObject *value, void *closure)
1341{
1342 PyTypeObject *old = self->ob_type;
1343 PyTypeObject *new, *newbase, *oldbase;
1344
1345 if (!PyType_Check(value)) {
1346 PyErr_Format(PyExc_TypeError,
1347 "__class__ must be set to new-style class, not '%s' object",
1348 value->ob_type->tp_name);
1349 return -1;
1350 }
1351 new = (PyTypeObject *)value;
1352 newbase = new;
1353 oldbase = old;
1354 while (equiv_structs(newbase, newbase->tp_base))
1355 newbase = newbase->tp_base;
1356 while (equiv_structs(oldbase, oldbase->tp_base))
1357 oldbase = oldbase->tp_base;
1358 if (newbase != oldbase &&
1359 (newbase->tp_base != oldbase->tp_base ||
1360 !same_slots_added(newbase, oldbase))) {
1361 PyErr_Format(PyExc_TypeError,
1362 "__class__ assignment: "
1363 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001364 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001365 old->tp_name);
1366 return -1;
1367 }
1368 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1369 Py_INCREF(new);
1370 }
1371 self->ob_type = new;
1372 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1373 Py_DECREF(old);
1374 }
1375 return 0;
1376}
1377
1378static PyGetSetDef object_getsets[] = {
1379 {"__class__", object_get_class, object_set_class,
1380 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001381 {0}
1382};
1383
Guido van Rossum3926a632001-09-25 16:25:58 +00001384static PyObject *
1385object_reduce(PyObject *self, PyObject *args)
1386{
1387 /* Call copy_reg._reduce(self) */
1388 static PyObject *copy_reg_str;
1389 PyObject *copy_reg, *res;
1390
1391 if (!copy_reg_str) {
1392 copy_reg_str = PyString_InternFromString("copy_reg");
1393 if (copy_reg_str == NULL)
1394 return NULL;
1395 }
1396 copy_reg = PyImport_Import(copy_reg_str);
1397 if (!copy_reg)
1398 return NULL;
1399 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1400 Py_DECREF(copy_reg);
1401 return res;
1402}
1403
1404static PyMethodDef object_methods[] = {
1405 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1406 {0}
1407};
1408
Tim Peters6d6c1a32001-08-02 04:15:00 +00001409PyTypeObject PyBaseObject_Type = {
1410 PyObject_HEAD_INIT(&PyType_Type)
1411 0, /* ob_size */
1412 "object", /* tp_name */
1413 sizeof(PyObject), /* tp_basicsize */
1414 0, /* tp_itemsize */
1415 (destructor)object_dealloc, /* tp_dealloc */
1416 0, /* tp_print */
1417 0, /* tp_getattr */
1418 0, /* tp_setattr */
1419 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001420 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421 0, /* tp_as_number */
1422 0, /* tp_as_sequence */
1423 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001424 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001425 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001426 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001428 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001429 0, /* tp_as_buffer */
1430 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1431 "The most base type", /* tp_doc */
1432 0, /* tp_traverse */
1433 0, /* tp_clear */
1434 0, /* tp_richcompare */
1435 0, /* tp_weaklistoffset */
1436 0, /* tp_iter */
1437 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001438 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001439 0, /* tp_members */
1440 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001441 0, /* tp_base */
1442 0, /* tp_dict */
1443 0, /* tp_descr_get */
1444 0, /* tp_descr_set */
1445 0, /* tp_dictoffset */
1446 object_init, /* tp_init */
1447 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001448 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001449 object_free, /* tp_free */
1450};
1451
1452
1453/* Initialize the __dict__ in a type object */
1454
1455static int
1456add_methods(PyTypeObject *type, PyMethodDef *meth)
1457{
1458 PyObject *dict = type->tp_defined;
1459
1460 for (; meth->ml_name != NULL; meth++) {
1461 PyObject *descr;
1462 if (PyDict_GetItemString(dict, meth->ml_name))
1463 continue;
1464 descr = PyDescr_NewMethod(type, meth);
1465 if (descr == NULL)
1466 return -1;
1467 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1468 return -1;
1469 Py_DECREF(descr);
1470 }
1471 return 0;
1472}
1473
1474static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001475add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001476{
1477 PyObject *dict = type->tp_defined;
1478
1479 for (; memb->name != NULL; memb++) {
1480 PyObject *descr;
1481 if (PyDict_GetItemString(dict, memb->name))
1482 continue;
1483 descr = PyDescr_NewMember(type, memb);
1484 if (descr == NULL)
1485 return -1;
1486 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1487 return -1;
1488 Py_DECREF(descr);
1489 }
1490 return 0;
1491}
1492
1493static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001494add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001495{
1496 PyObject *dict = type->tp_defined;
1497
1498 for (; gsp->name != NULL; gsp++) {
1499 PyObject *descr;
1500 if (PyDict_GetItemString(dict, gsp->name))
1501 continue;
1502 descr = PyDescr_NewGetSet(type, gsp);
1503
1504 if (descr == NULL)
1505 return -1;
1506 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1507 return -1;
1508 Py_DECREF(descr);
1509 }
1510 return 0;
1511}
1512
Guido van Rossum13d52f02001-08-10 21:24:08 +00001513static void
1514inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001515{
1516 int oldsize, newsize;
1517
Guido van Rossum13d52f02001-08-10 21:24:08 +00001518 /* Special flag magic */
1519 if (!type->tp_as_buffer && base->tp_as_buffer) {
1520 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1521 type->tp_flags |=
1522 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1523 }
1524 if (!type->tp_as_sequence && base->tp_as_sequence) {
1525 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1526 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1527 }
1528 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1529 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1530 if ((!type->tp_as_number && base->tp_as_number) ||
1531 (!type->tp_as_sequence && base->tp_as_sequence)) {
1532 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1533 if (!type->tp_as_number && !type->tp_as_sequence) {
1534 type->tp_flags |= base->tp_flags &
1535 Py_TPFLAGS_HAVE_INPLACEOPS;
1536 }
1537 }
1538 /* Wow */
1539 }
1540 if (!type->tp_as_number && base->tp_as_number) {
1541 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1542 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1543 }
1544
1545 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001546 oldsize = base->tp_basicsize;
1547 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1548 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1549 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001550 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1551 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001552 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001553 if (type->tp_traverse == NULL)
1554 type->tp_traverse = base->tp_traverse;
1555 if (type->tp_clear == NULL)
1556 type->tp_clear = base->tp_clear;
1557 }
1558 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1559 if (base != &PyBaseObject_Type ||
1560 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1561 if (type->tp_new == NULL)
1562 type->tp_new = base->tp_new;
1563 }
1564 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001565 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001566
1567 /* Copy other non-function slots */
1568
1569#undef COPYVAL
1570#define COPYVAL(SLOT) \
1571 if (type->SLOT == 0) type->SLOT = base->SLOT
1572
1573 COPYVAL(tp_itemsize);
1574 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1575 COPYVAL(tp_weaklistoffset);
1576 }
1577 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1578 COPYVAL(tp_dictoffset);
1579 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001580}
1581
1582static void
1583inherit_slots(PyTypeObject *type, PyTypeObject *base)
1584{
1585 PyTypeObject *basebase;
1586
1587#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588#undef COPYSLOT
1589#undef COPYNUM
1590#undef COPYSEQ
1591#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001592
1593#define SLOTDEFINED(SLOT) \
1594 (base->SLOT != 0 && \
1595 (basebase == NULL || base->SLOT != basebase->SLOT))
1596
Tim Peters6d6c1a32001-08-02 04:15:00 +00001597#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001598 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001599
1600#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1601#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1602#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1603
Guido van Rossum13d52f02001-08-10 21:24:08 +00001604 /* This won't inherit indirect slots (from tp_as_number etc.)
1605 if type doesn't provide the space. */
1606
1607 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1608 basebase = base->tp_base;
1609 if (basebase->tp_as_number == NULL)
1610 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001611 COPYNUM(nb_add);
1612 COPYNUM(nb_subtract);
1613 COPYNUM(nb_multiply);
1614 COPYNUM(nb_divide);
1615 COPYNUM(nb_remainder);
1616 COPYNUM(nb_divmod);
1617 COPYNUM(nb_power);
1618 COPYNUM(nb_negative);
1619 COPYNUM(nb_positive);
1620 COPYNUM(nb_absolute);
1621 COPYNUM(nb_nonzero);
1622 COPYNUM(nb_invert);
1623 COPYNUM(nb_lshift);
1624 COPYNUM(nb_rshift);
1625 COPYNUM(nb_and);
1626 COPYNUM(nb_xor);
1627 COPYNUM(nb_or);
1628 COPYNUM(nb_coerce);
1629 COPYNUM(nb_int);
1630 COPYNUM(nb_long);
1631 COPYNUM(nb_float);
1632 COPYNUM(nb_oct);
1633 COPYNUM(nb_hex);
1634 COPYNUM(nb_inplace_add);
1635 COPYNUM(nb_inplace_subtract);
1636 COPYNUM(nb_inplace_multiply);
1637 COPYNUM(nb_inplace_divide);
1638 COPYNUM(nb_inplace_remainder);
1639 COPYNUM(nb_inplace_power);
1640 COPYNUM(nb_inplace_lshift);
1641 COPYNUM(nb_inplace_rshift);
1642 COPYNUM(nb_inplace_and);
1643 COPYNUM(nb_inplace_xor);
1644 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001645 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1646 COPYNUM(nb_true_divide);
1647 COPYNUM(nb_floor_divide);
1648 COPYNUM(nb_inplace_true_divide);
1649 COPYNUM(nb_inplace_floor_divide);
1650 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001651 }
1652
Guido van Rossum13d52f02001-08-10 21:24:08 +00001653 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1654 basebase = base->tp_base;
1655 if (basebase->tp_as_sequence == NULL)
1656 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001657 COPYSEQ(sq_length);
1658 COPYSEQ(sq_concat);
1659 COPYSEQ(sq_repeat);
1660 COPYSEQ(sq_item);
1661 COPYSEQ(sq_slice);
1662 COPYSEQ(sq_ass_item);
1663 COPYSEQ(sq_ass_slice);
1664 COPYSEQ(sq_contains);
1665 COPYSEQ(sq_inplace_concat);
1666 COPYSEQ(sq_inplace_repeat);
1667 }
1668
Guido van Rossum13d52f02001-08-10 21:24:08 +00001669 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1670 basebase = base->tp_base;
1671 if (basebase->tp_as_mapping == NULL)
1672 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001673 COPYMAP(mp_length);
1674 COPYMAP(mp_subscript);
1675 COPYMAP(mp_ass_subscript);
1676 }
1677
Guido van Rossum13d52f02001-08-10 21:24:08 +00001678 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001679
Tim Peters6d6c1a32001-08-02 04:15:00 +00001680 COPYSLOT(tp_dealloc);
1681 COPYSLOT(tp_print);
1682 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1683 type->tp_getattr = base->tp_getattr;
1684 type->tp_getattro = base->tp_getattro;
1685 }
1686 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1687 type->tp_setattr = base->tp_setattr;
1688 type->tp_setattro = base->tp_setattro;
1689 }
1690 /* tp_compare see tp_richcompare */
1691 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001692 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001693 COPYSLOT(tp_call);
1694 COPYSLOT(tp_str);
1695 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001696 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001697 if (type->tp_compare == NULL &&
1698 type->tp_richcompare == NULL &&
1699 type->tp_hash == NULL)
1700 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001701 type->tp_compare = base->tp_compare;
1702 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001703 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001704 }
1705 }
1706 else {
1707 COPYSLOT(tp_compare);
1708 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001709 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1710 COPYSLOT(tp_iter);
1711 COPYSLOT(tp_iternext);
1712 }
1713 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1714 COPYSLOT(tp_descr_get);
1715 COPYSLOT(tp_descr_set);
1716 COPYSLOT(tp_dictoffset);
1717 COPYSLOT(tp_init);
1718 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001719 COPYSLOT(tp_free);
1720 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001721}
1722
Guido van Rossum13d52f02001-08-10 21:24:08 +00001723staticforward int add_operators(PyTypeObject *);
1724
Tim Peters6d6c1a32001-08-02 04:15:00 +00001725int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001726PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001727{
1728 PyObject *dict, *bases, *x;
1729 PyTypeObject *base;
1730 int i, n;
1731
Guido van Rossumd614f972001-08-10 17:39:49 +00001732 if (type->tp_flags & Py_TPFLAGS_READY) {
1733 assert(type->tp_dict != NULL);
1734 return 0;
1735 }
1736 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1737 assert(type->tp_dict == NULL);
1738
1739 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001740
1741 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1742 base = type->tp_base;
1743 if (base == NULL && type != &PyBaseObject_Type)
1744 base = type->tp_base = &PyBaseObject_Type;
1745
1746 /* Initialize tp_bases */
1747 bases = type->tp_bases;
1748 if (bases == NULL) {
1749 if (base == NULL)
1750 bases = PyTuple_New(0);
1751 else
1752 bases = Py_BuildValue("(O)", base);
1753 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001754 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001755 type->tp_bases = bases;
1756 }
1757
1758 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001759 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001760 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001761 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001762 }
1763
1764 /* Initialize tp_defined */
1765 dict = type->tp_defined;
1766 if (dict == NULL) {
1767 dict = PyDict_New();
1768 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001769 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001770 type->tp_defined = dict;
1771 }
1772
1773 /* Add type-specific descriptors to tp_defined */
1774 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001775 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001776 if (type->tp_methods != NULL) {
1777 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001778 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001779 }
1780 if (type->tp_members != NULL) {
1781 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001782 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001783 }
1784 if (type->tp_getset != NULL) {
1785 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001786 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001787 }
1788
1789 /* Temporarily make tp_dict the same object as tp_defined.
1790 (This is needed to call mro(), and can stay this way for
1791 dynamic types). */
1792 Py_INCREF(type->tp_defined);
1793 type->tp_dict = type->tp_defined;
1794
1795 /* Calculate method resolution order */
1796 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001797 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001798 }
1799
Guido van Rossum13d52f02001-08-10 21:24:08 +00001800 /* Inherit special flags from dominant base */
1801 if (type->tp_base != NULL)
1802 inherit_special(type, type->tp_base);
1803
Tim Peters6d6c1a32001-08-02 04:15:00 +00001804 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001805 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001806 /* For a dynamic type, all slots are overridden */
1807 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001808 }
1809 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001810 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001811 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001812 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001813 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001814 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001815 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001816 bases = type->tp_mro;
1817 assert(bases != NULL);
1818 assert(PyTuple_Check(bases));
1819 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001820 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001821 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1822 assert(PyType_Check(base));
1823 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001824 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001825 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001826 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001827 }
1828 }
1829
Guido van Rossum13d52f02001-08-10 21:24:08 +00001830 /* Some more special stuff */
1831 base = type->tp_base;
1832 if (base != NULL) {
1833 if (type->tp_as_number == NULL)
1834 type->tp_as_number = base->tp_as_number;
1835 if (type->tp_as_sequence == NULL)
1836 type->tp_as_sequence = base->tp_as_sequence;
1837 if (type->tp_as_mapping == NULL)
1838 type->tp_as_mapping = base->tp_as_mapping;
1839 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001840
Guido van Rossum13d52f02001-08-10 21:24:08 +00001841 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001842 assert(type->tp_dict != NULL);
1843 type->tp_flags =
1844 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001845 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001846
1847 error:
1848 type->tp_flags &= ~Py_TPFLAGS_READYING;
1849 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001850}
1851
1852
1853/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1854
1855/* There's a wrapper *function* for each distinct function typedef used
1856 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1857 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1858 Most tables have only one entry; the tables for binary operators have two
1859 entries, one regular and one with reversed arguments. */
1860
1861static PyObject *
1862wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1863{
1864 inquiry func = (inquiry)wrapped;
1865 int res;
1866
1867 if (!PyArg_ParseTuple(args, ""))
1868 return NULL;
1869 res = (*func)(self);
1870 if (res == -1 && PyErr_Occurred())
1871 return NULL;
1872 return PyInt_FromLong((long)res);
1873}
1874
1875static struct wrapperbase tab_len[] = {
1876 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1877 {0}
1878};
1879
1880static PyObject *
1881wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1882{
1883 binaryfunc func = (binaryfunc)wrapped;
1884 PyObject *other;
1885
1886 if (!PyArg_ParseTuple(args, "O", &other))
1887 return NULL;
1888 return (*func)(self, other);
1889}
1890
1891static PyObject *
1892wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1893{
1894 binaryfunc func = (binaryfunc)wrapped;
1895 PyObject *other;
1896
1897 if (!PyArg_ParseTuple(args, "O", &other))
1898 return NULL;
1899 return (*func)(other, self);
1900}
1901
1902#undef BINARY
1903#define BINARY(NAME, OP) \
1904static struct wrapperbase tab_##NAME[] = { \
1905 {"__" #NAME "__", \
1906 (wrapperfunc)wrap_binaryfunc, \
1907 "x.__" #NAME "__(y) <==> " #OP}, \
1908 {"__r" #NAME "__", \
1909 (wrapperfunc)wrap_binaryfunc_r, \
1910 "y.__r" #NAME "__(x) <==> " #OP}, \
1911 {0} \
1912}
1913
1914BINARY(add, "x+y");
1915BINARY(sub, "x-y");
1916BINARY(mul, "x*y");
1917BINARY(div, "x/y");
1918BINARY(mod, "x%y");
1919BINARY(divmod, "divmod(x,y)");
1920BINARY(lshift, "x<<y");
1921BINARY(rshift, "x>>y");
1922BINARY(and, "x&y");
1923BINARY(xor, "x^y");
1924BINARY(or, "x|y");
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00001925
1926static PyObject *
1927wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
1928{
1929 coercion func = (coercion)wrapped;
1930 PyObject *other, *res;
1931 int ok;
1932
1933 if (!PyArg_ParseTuple(args, "O", &other))
1934 return NULL;
1935 ok = func(&self, &other);
1936 if (ok < 0)
1937 return NULL;
1938 if (ok > 0) {
1939 Py_INCREF(Py_NotImplemented);
1940 return Py_NotImplemented;
1941 }
1942 res = PyTuple_New(2);
1943 if (res == NULL) {
1944 Py_DECREF(self);
1945 Py_DECREF(other);
1946 return NULL;
1947 }
1948 PyTuple_SET_ITEM(res, 0, self);
1949 PyTuple_SET_ITEM(res, 1, other);
1950 return res;
1951}
1952
1953static struct wrapperbase tab_coerce[] = {
1954 {"__coerce__", (wrapperfunc)wrap_coercefunc,
1955 "x.__coerce__(y) <==> coerce(x, y)"},
1956 {0}
1957};
1958
Guido van Rossum874f15a2001-09-25 21:16:33 +00001959BINARY(floordiv, "x//y");
1960BINARY(truediv, "x/y # true division");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001961
1962static PyObject *
1963wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1964{
1965 ternaryfunc func = (ternaryfunc)wrapped;
1966 PyObject *other;
1967 PyObject *third = Py_None;
1968
1969 /* Note: This wrapper only works for __pow__() */
1970
1971 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1972 return NULL;
1973 return (*func)(self, other, third);
1974}
1975
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00001976static PyObject *
1977wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1978{
1979 ternaryfunc func = (ternaryfunc)wrapped;
1980 PyObject *other;
1981 PyObject *third = Py_None;
1982
1983 /* Note: This wrapper only works for __pow__() */
1984
1985 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1986 return NULL;
1987 return (*func)(other, self, third);
1988}
1989
Tim Peters6d6c1a32001-08-02 04:15:00 +00001990#undef TERNARY
1991#define TERNARY(NAME, OP) \
1992static struct wrapperbase tab_##NAME[] = { \
1993 {"__" #NAME "__", \
1994 (wrapperfunc)wrap_ternaryfunc, \
1995 "x.__" #NAME "__(y, z) <==> " #OP}, \
1996 {"__r" #NAME "__", \
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00001997 (wrapperfunc)wrap_ternaryfunc_r, \
Tim Peters6d6c1a32001-08-02 04:15:00 +00001998 "y.__r" #NAME "__(x, z) <==> " #OP}, \
1999 {0} \
2000}
2001
2002TERNARY(pow, "(x**y) % z");
2003
2004#undef UNARY
2005#define UNARY(NAME, OP) \
2006static struct wrapperbase tab_##NAME[] = { \
2007 {"__" #NAME "__", \
2008 (wrapperfunc)wrap_unaryfunc, \
2009 "x.__" #NAME "__() <==> " #OP}, \
2010 {0} \
2011}
2012
2013static PyObject *
2014wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2015{
2016 unaryfunc func = (unaryfunc)wrapped;
2017
2018 if (!PyArg_ParseTuple(args, ""))
2019 return NULL;
2020 return (*func)(self);
2021}
2022
2023UNARY(neg, "-x");
2024UNARY(pos, "+x");
2025UNARY(abs, "abs(x)");
2026UNARY(nonzero, "x != 0");
2027UNARY(invert, "~x");
2028UNARY(int, "int(x)");
2029UNARY(long, "long(x)");
2030UNARY(float, "float(x)");
2031UNARY(oct, "oct(x)");
2032UNARY(hex, "hex(x)");
2033
2034#undef IBINARY
2035#define IBINARY(NAME, OP) \
2036static struct wrapperbase tab_##NAME[] = { \
2037 {"__" #NAME "__", \
2038 (wrapperfunc)wrap_binaryfunc, \
2039 "x.__" #NAME "__(y) <==> " #OP}, \
2040 {0} \
2041}
2042
2043IBINARY(iadd, "x+=y");
2044IBINARY(isub, "x-=y");
2045IBINARY(imul, "x*=y");
2046IBINARY(idiv, "x/=y");
2047IBINARY(imod, "x%=y");
2048IBINARY(ilshift, "x<<=y");
2049IBINARY(irshift, "x>>=y");
2050IBINARY(iand, "x&=y");
2051IBINARY(ixor, "x^=y");
2052IBINARY(ior, "x|=y");
Guido van Rossum874f15a2001-09-25 21:16:33 +00002053IBINARY(ifloordiv, "x//=y");
2054IBINARY(itruediv, "x/=y # true division");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002055
2056#undef ITERNARY
2057#define ITERNARY(NAME, OP) \
2058static struct wrapperbase tab_##NAME[] = { \
2059 {"__" #NAME "__", \
2060 (wrapperfunc)wrap_ternaryfunc, \
2061 "x.__" #NAME "__(y) <==> " #OP}, \
2062 {0} \
2063}
2064
2065ITERNARY(ipow, "x = (x**y) % z");
2066
2067static struct wrapperbase tab_getitem[] = {
2068 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
2069 "x.__getitem__(y) <==> x[y]"},
2070 {0}
2071};
2072
2073static PyObject *
2074wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2075{
2076 intargfunc func = (intargfunc)wrapped;
2077 int i;
2078
2079 if (!PyArg_ParseTuple(args, "i", &i))
2080 return NULL;
2081 return (*func)(self, i);
2082}
2083
2084static struct wrapperbase tab_mul_int[] = {
2085 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
2086 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
2087 {0}
2088};
2089
2090static struct wrapperbase tab_concat[] = {
2091 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
2092 {0}
2093};
2094
2095static struct wrapperbase tab_imul_int[] = {
2096 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
2097 {0}
2098};
2099
Guido van Rossum5d815f32001-08-17 21:57:47 +00002100static int
2101getindex(PyObject *self, PyObject *arg)
2102{
2103 int i;
2104
2105 i = PyInt_AsLong(arg);
2106 if (i == -1 && PyErr_Occurred())
2107 return -1;
2108 if (i < 0) {
2109 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2110 if (sq && sq->sq_length) {
2111 int n = (*sq->sq_length)(self);
2112 if (n < 0)
2113 return -1;
2114 i += n;
2115 }
2116 }
2117 return i;
2118}
2119
2120static PyObject *
2121wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2122{
2123 intargfunc func = (intargfunc)wrapped;
2124 PyObject *arg;
2125 int i;
2126
Guido van Rossumf4593e02001-10-03 12:09:30 +00002127 if (PyTuple_GET_SIZE(args) == 1) {
2128 arg = PyTuple_GET_ITEM(args, 0);
2129 i = getindex(self, arg);
2130 if (i == -1 && PyErr_Occurred())
2131 return NULL;
2132 return (*func)(self, i);
2133 }
2134 PyArg_ParseTuple(args, "O", &arg);
2135 assert(PyErr_Occurred());
2136 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002137}
2138
Tim Peters6d6c1a32001-08-02 04:15:00 +00002139static struct wrapperbase tab_getitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00002140 {"__getitem__", (wrapperfunc)wrap_sq_item,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002141 "x.__getitem__(i) <==> x[i]"},
2142 {0}
2143};
2144
2145static PyObject *
2146wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2147{
2148 intintargfunc func = (intintargfunc)wrapped;
2149 int i, j;
2150
2151 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2152 return NULL;
2153 return (*func)(self, i, j);
2154}
2155
2156static struct wrapperbase tab_getslice[] = {
2157 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
2158 "x.__getslice__(i, j) <==> x[i:j]"},
2159 {0}
2160};
2161
2162static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002163wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002164{
2165 intobjargproc func = (intobjargproc)wrapped;
2166 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002167 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002168
Guido van Rossum5d815f32001-08-17 21:57:47 +00002169 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2170 return NULL;
2171 i = getindex(self, arg);
2172 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002173 return NULL;
2174 res = (*func)(self, i, value);
2175 if (res == -1 && PyErr_Occurred())
2176 return NULL;
2177 Py_INCREF(Py_None);
2178 return Py_None;
2179}
2180
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002181static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002182wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002183{
2184 intobjargproc func = (intobjargproc)wrapped;
2185 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002186 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002187
Guido van Rossum5d815f32001-08-17 21:57:47 +00002188 if (!PyArg_ParseTuple(args, "O", &arg))
2189 return NULL;
2190 i = getindex(self, arg);
2191 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002192 return NULL;
2193 res = (*func)(self, i, NULL);
2194 if (res == -1 && PyErr_Occurred())
2195 return NULL;
2196 Py_INCREF(Py_None);
2197 return Py_None;
2198}
2199
Tim Peters6d6c1a32001-08-02 04:15:00 +00002200static struct wrapperbase tab_setitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00002201 {"__setitem__", (wrapperfunc)wrap_sq_setitem,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002202 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum5d815f32001-08-17 21:57:47 +00002203 {"__delitem__", (wrapperfunc)wrap_sq_delitem,
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002204 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002205 {0}
2206};
2207
2208static PyObject *
2209wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2210{
2211 intintobjargproc func = (intintobjargproc)wrapped;
2212 int i, j, res;
2213 PyObject *value;
2214
2215 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2216 return NULL;
2217 res = (*func)(self, i, j, value);
2218 if (res == -1 && PyErr_Occurred())
2219 return NULL;
2220 Py_INCREF(Py_None);
2221 return Py_None;
2222}
2223
2224static struct wrapperbase tab_setslice[] = {
2225 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
2226 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
2227 {0}
2228};
2229
2230/* XXX objobjproc is a misnomer; should be objargpred */
2231static PyObject *
2232wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2233{
2234 objobjproc func = (objobjproc)wrapped;
2235 int res;
2236 PyObject *value;
2237
2238 if (!PyArg_ParseTuple(args, "O", &value))
2239 return NULL;
2240 res = (*func)(self, value);
2241 if (res == -1 && PyErr_Occurred())
2242 return NULL;
2243 return PyInt_FromLong((long)res);
2244}
2245
2246static struct wrapperbase tab_contains[] = {
2247 {"__contains__", (wrapperfunc)wrap_objobjproc,
2248 "x.__contains__(y) <==> y in x"},
2249 {0}
2250};
2251
2252static PyObject *
2253wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2254{
2255 objobjargproc func = (objobjargproc)wrapped;
2256 int res;
2257 PyObject *key, *value;
2258
2259 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2260 return NULL;
2261 res = (*func)(self, key, value);
2262 if (res == -1 && PyErr_Occurred())
2263 return NULL;
2264 Py_INCREF(Py_None);
2265 return Py_None;
2266}
2267
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002268static PyObject *
2269wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2270{
2271 objobjargproc func = (objobjargproc)wrapped;
2272 int res;
2273 PyObject *key;
2274
2275 if (!PyArg_ParseTuple(args, "O", &key))
2276 return NULL;
2277 res = (*func)(self, key, NULL);
2278 if (res == -1 && PyErr_Occurred())
2279 return NULL;
2280 Py_INCREF(Py_None);
2281 return Py_None;
2282}
2283
Tim Peters6d6c1a32001-08-02 04:15:00 +00002284static struct wrapperbase tab_setitem[] = {
2285 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
2286 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002287 {"__delitem__", (wrapperfunc)wrap_delitem,
2288 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002289 {0}
2290};
2291
2292static PyObject *
2293wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2294{
2295 cmpfunc func = (cmpfunc)wrapped;
2296 int res;
2297 PyObject *other;
2298
2299 if (!PyArg_ParseTuple(args, "O", &other))
2300 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002301 if (other->ob_type->tp_compare != func &&
2302 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002303 PyErr_Format(
2304 PyExc_TypeError,
2305 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2306 self->ob_type->tp_name,
2307 self->ob_type->tp_name,
2308 other->ob_type->tp_name);
2309 return NULL;
2310 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002311 res = (*func)(self, other);
2312 if (PyErr_Occurred())
2313 return NULL;
2314 return PyInt_FromLong((long)res);
2315}
2316
2317static struct wrapperbase tab_cmp[] = {
2318 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
2319 "x.__cmp__(y) <==> cmp(x,y)"},
2320 {0}
2321};
2322
2323static struct wrapperbase tab_repr[] = {
2324 {"__repr__", (wrapperfunc)wrap_unaryfunc,
2325 "x.__repr__() <==> repr(x)"},
2326 {0}
2327};
2328
2329static struct wrapperbase tab_getattr[] = {
Guido van Rossum867a8d22001-09-21 19:29:08 +00002330 {"__getattribute__", (wrapperfunc)wrap_binaryfunc,
2331 "x.__getattribute__('name') <==> x.name"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002332 {0}
2333};
2334
2335static PyObject *
2336wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2337{
2338 setattrofunc func = (setattrofunc)wrapped;
2339 int res;
2340 PyObject *name, *value;
2341
2342 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2343 return NULL;
2344 res = (*func)(self, name, value);
2345 if (res < 0)
2346 return NULL;
2347 Py_INCREF(Py_None);
2348 return Py_None;
2349}
2350
2351static PyObject *
2352wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2353{
2354 setattrofunc func = (setattrofunc)wrapped;
2355 int res;
2356 PyObject *name;
2357
2358 if (!PyArg_ParseTuple(args, "O", &name))
2359 return NULL;
2360 res = (*func)(self, name, NULL);
2361 if (res < 0)
2362 return NULL;
2363 Py_INCREF(Py_None);
2364 return Py_None;
2365}
2366
2367static struct wrapperbase tab_setattr[] = {
2368 {"__setattr__", (wrapperfunc)wrap_setattr,
2369 "x.__setattr__('name', value) <==> x.name = value"},
2370 {"__delattr__", (wrapperfunc)wrap_delattr,
2371 "x.__delattr__('name') <==> del x.name"},
2372 {0}
2373};
2374
2375static PyObject *
2376wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2377{
2378 hashfunc func = (hashfunc)wrapped;
2379 long res;
2380
2381 if (!PyArg_ParseTuple(args, ""))
2382 return NULL;
2383 res = (*func)(self);
2384 if (res == -1 && PyErr_Occurred())
2385 return NULL;
2386 return PyInt_FromLong(res);
2387}
2388
2389static struct wrapperbase tab_hash[] = {
2390 {"__hash__", (wrapperfunc)wrap_hashfunc,
2391 "x.__hash__() <==> hash(x)"},
2392 {0}
2393};
2394
2395static PyObject *
2396wrap_call(PyObject *self, PyObject *args, void *wrapped)
2397{
2398 ternaryfunc func = (ternaryfunc)wrapped;
2399
2400 /* XXX What about keyword arguments? */
2401 return (*func)(self, args, NULL);
2402}
2403
2404static struct wrapperbase tab_call[] = {
2405 {"__call__", (wrapperfunc)wrap_call,
2406 "x.__call__(...) <==> x(...)"},
2407 {0}
2408};
2409
2410static struct wrapperbase tab_str[] = {
2411 {"__str__", (wrapperfunc)wrap_unaryfunc,
2412 "x.__str__() <==> str(x)"},
2413 {0}
2414};
2415
2416static PyObject *
2417wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2418{
2419 richcmpfunc func = (richcmpfunc)wrapped;
2420 PyObject *other;
2421
2422 if (!PyArg_ParseTuple(args, "O", &other))
2423 return NULL;
2424 return (*func)(self, other, op);
2425}
2426
2427#undef RICHCMP_WRAPPER
2428#define RICHCMP_WRAPPER(NAME, OP) \
2429static PyObject * \
2430richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2431{ \
2432 return wrap_richcmpfunc(self, args, wrapped, OP); \
2433}
2434
Jack Jansen8e938b42001-08-08 15:29:49 +00002435RICHCMP_WRAPPER(lt, Py_LT)
2436RICHCMP_WRAPPER(le, Py_LE)
2437RICHCMP_WRAPPER(eq, Py_EQ)
2438RICHCMP_WRAPPER(ne, Py_NE)
2439RICHCMP_WRAPPER(gt, Py_GT)
2440RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002441
2442#undef RICHCMP_ENTRY
2443#define RICHCMP_ENTRY(NAME, EXPR) \
2444 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
2445 "x.__" #NAME "__(y) <==> " EXPR}
2446
2447static struct wrapperbase tab_richcmp[] = {
2448 RICHCMP_ENTRY(lt, "x<y"),
2449 RICHCMP_ENTRY(le, "x<=y"),
2450 RICHCMP_ENTRY(eq, "x==y"),
2451 RICHCMP_ENTRY(ne, "x!=y"),
2452 RICHCMP_ENTRY(gt, "x>y"),
2453 RICHCMP_ENTRY(ge, "x>=y"),
2454 {0}
2455};
2456
2457static struct wrapperbase tab_iter[] = {
2458 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
2459 {0}
2460};
2461
2462static PyObject *
2463wrap_next(PyObject *self, PyObject *args, void *wrapped)
2464{
2465 unaryfunc func = (unaryfunc)wrapped;
2466 PyObject *res;
2467
2468 if (!PyArg_ParseTuple(args, ""))
2469 return NULL;
2470 res = (*func)(self);
2471 if (res == NULL && !PyErr_Occurred())
2472 PyErr_SetNone(PyExc_StopIteration);
2473 return res;
2474}
2475
2476static struct wrapperbase tab_next[] = {
2477 {"next", (wrapperfunc)wrap_next,
2478 "x.next() -> the next value, or raise StopIteration"},
2479 {0}
2480};
2481
2482static PyObject *
2483wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2484{
2485 descrgetfunc func = (descrgetfunc)wrapped;
2486 PyObject *obj;
2487 PyObject *type = NULL;
2488
2489 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2490 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002491 return (*func)(self, obj, type);
2492}
2493
2494static struct wrapperbase tab_descr_get[] = {
2495 {"__get__", (wrapperfunc)wrap_descr_get,
2496 "descr.__get__(obj, type) -> value"},
2497 {0}
2498};
2499
2500static PyObject *
2501wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
2502{
2503 descrsetfunc func = (descrsetfunc)wrapped;
2504 PyObject *obj, *value;
2505 int ret;
2506
2507 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2508 return NULL;
2509 ret = (*func)(self, obj, value);
2510 if (ret < 0)
2511 return NULL;
2512 Py_INCREF(Py_None);
2513 return Py_None;
2514}
2515
2516static struct wrapperbase tab_descr_set[] = {
2517 {"__set__", (wrapperfunc)wrap_descrsetfunc,
2518 "descr.__set__(obj, value)"},
2519 {0}
2520};
2521
2522static PyObject *
2523wrap_init(PyObject *self, PyObject *args, void *wrapped)
2524{
2525 initproc func = (initproc)wrapped;
2526
2527 /* XXX What about keyword arguments? */
2528 if (func(self, args, NULL) < 0)
2529 return NULL;
2530 Py_INCREF(Py_None);
2531 return Py_None;
2532}
2533
2534static struct wrapperbase tab_init[] = {
2535 {"__init__", (wrapperfunc)wrap_init,
2536 "x.__init__(...) initializes x; "
Guido van Rossumd016e452001-10-01 13:17:24 +00002537 "see x.__class__.__doc__ for signature"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002538 {0}
2539};
2540
2541static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002542tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002543{
Barry Warsaw60f01882001-08-22 19:24:42 +00002544 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002545 PyObject *arg0, *res;
2546
2547 if (self == NULL || !PyType_Check(self))
2548 Py_FatalError("__new__() called with non-type 'self'");
2549 type = (PyTypeObject *)self;
2550 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002551 PyErr_Format(PyExc_TypeError,
2552 "%s.__new__(): not enough arguments",
2553 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002554 return NULL;
2555 }
2556 arg0 = PyTuple_GET_ITEM(args, 0);
2557 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002558 PyErr_Format(PyExc_TypeError,
2559 "%s.__new__(X): X is not a type object (%s)",
2560 type->tp_name,
2561 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002562 return NULL;
2563 }
2564 subtype = (PyTypeObject *)arg0;
2565 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002566 PyErr_Format(PyExc_TypeError,
2567 "%s.__new__(%s): %s is not a subtype of %s",
2568 type->tp_name,
2569 subtype->tp_name,
2570 subtype->tp_name,
2571 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002572 return NULL;
2573 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002574
2575 /* Check that the use doesn't do something silly and unsafe like
2576 object.__new__(dictionary). To do this, we check that the
2577 most derived base that's not a heap type is this type. */
2578 staticbase = subtype;
2579 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2580 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002581 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002582 PyErr_Format(PyExc_TypeError,
2583 "%s.__new__(%s) is not safe, use %s.__new__()",
2584 type->tp_name,
2585 subtype->tp_name,
2586 staticbase == NULL ? "?" : staticbase->tp_name);
2587 return NULL;
2588 }
2589
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002590 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2591 if (args == NULL)
2592 return NULL;
2593 res = type->tp_new(subtype, args, kwds);
2594 Py_DECREF(args);
2595 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002596}
2597
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002598static struct PyMethodDef tp_new_methoddef[] = {
2599 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2600 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002601 {0}
2602};
2603
2604static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002605add_tp_new_wrapper(PyTypeObject *type)
2606{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002607 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002608
Guido van Rossumf040ede2001-08-07 16:40:56 +00002609 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2610 return 0;
2611 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002612 if (func == NULL)
2613 return -1;
2614 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2615}
2616
Guido van Rossum13d52f02001-08-10 21:24:08 +00002617static int
2618add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2619{
2620 PyObject *dict = type->tp_defined;
2621
2622 for (; wraps->name != NULL; wraps++) {
2623 PyObject *descr;
2624 if (PyDict_GetItemString(dict, wraps->name))
2625 continue;
2626 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2627 if (descr == NULL)
2628 return -1;
2629 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2630 return -1;
2631 Py_DECREF(descr);
2632 }
2633 return 0;
2634}
2635
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002636/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002637 dictionary with method descriptors for function slots. For each
2638 function slot (like tp_repr) that's defined in the type, one or
2639 more corresponding descriptors are added in the type's tp_defined
2640 dictionary under the appropriate name (like __repr__). Some
2641 function slots cause more than one descriptor to be added (for
2642 example, the nb_add slot adds both __add__ and __radd__
2643 descriptors) and some function slots compete for the same
2644 descriptor (for example both sq_item and mp_subscript generate a
2645 __getitem__ descriptor). This only adds new descriptors and
2646 doesn't overwrite entries in tp_defined that were previously
2647 defined. The descriptors contain a reference to the C function
2648 they must call, so that it's safe if they are copied into a
2649 subtype's __dict__ and the subtype has a different C function in
2650 its slot -- calling the method defined by the descriptor will call
2651 the C function that was used to create it, rather than the C
2652 function present in the slot when it is called. (This is important
2653 because a subtype may have a C function in the slot that calls the
2654 method from the dictionary, and we want to avoid infinite recursion
2655 here.) */
2656
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002657static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002658add_operators(PyTypeObject *type)
2659{
2660 PySequenceMethods *sq;
2661 PyMappingMethods *mp;
2662 PyNumberMethods *nb;
2663
2664#undef ADD
2665#define ADD(SLOT, TABLE) \
2666 if (SLOT) { \
2667 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2668 return -1; \
2669 }
2670
2671 if ((sq = type->tp_as_sequence) != NULL) {
2672 ADD(sq->sq_length, tab_len);
2673 ADD(sq->sq_concat, tab_concat);
2674 ADD(sq->sq_repeat, tab_mul_int);
2675 ADD(sq->sq_item, tab_getitem_int);
2676 ADD(sq->sq_slice, tab_getslice);
2677 ADD(sq->sq_ass_item, tab_setitem_int);
2678 ADD(sq->sq_ass_slice, tab_setslice);
2679 ADD(sq->sq_contains, tab_contains);
2680 ADD(sq->sq_inplace_concat, tab_iadd);
2681 ADD(sq->sq_inplace_repeat, tab_imul_int);
2682 }
2683
2684 if ((mp = type->tp_as_mapping) != NULL) {
2685 if (sq->sq_length == NULL)
2686 ADD(mp->mp_length, tab_len);
2687 ADD(mp->mp_subscript, tab_getitem);
2688 ADD(mp->mp_ass_subscript, tab_setitem);
2689 }
2690
2691 /* We don't support "old-style numbers" because their binary
2692 operators require that both arguments have the same type;
2693 the wrappers here only work for new-style numbers. */
2694 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2695 (nb = type->tp_as_number) != NULL) {
2696 ADD(nb->nb_add, tab_add);
2697 ADD(nb->nb_subtract, tab_sub);
2698 ADD(nb->nb_multiply, tab_mul);
2699 ADD(nb->nb_divide, tab_div);
2700 ADD(nb->nb_remainder, tab_mod);
2701 ADD(nb->nb_divmod, tab_divmod);
2702 ADD(nb->nb_power, tab_pow);
2703 ADD(nb->nb_negative, tab_neg);
2704 ADD(nb->nb_positive, tab_pos);
2705 ADD(nb->nb_absolute, tab_abs);
2706 ADD(nb->nb_nonzero, tab_nonzero);
2707 ADD(nb->nb_invert, tab_invert);
2708 ADD(nb->nb_lshift, tab_lshift);
2709 ADD(nb->nb_rshift, tab_rshift);
2710 ADD(nb->nb_and, tab_and);
2711 ADD(nb->nb_xor, tab_xor);
2712 ADD(nb->nb_or, tab_or);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002713 ADD(nb->nb_coerce, tab_coerce);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002714 ADD(nb->nb_int, tab_int);
2715 ADD(nb->nb_long, tab_long);
2716 ADD(nb->nb_float, tab_float);
2717 ADD(nb->nb_oct, tab_oct);
2718 ADD(nb->nb_hex, tab_hex);
2719 ADD(nb->nb_inplace_add, tab_iadd);
2720 ADD(nb->nb_inplace_subtract, tab_isub);
2721 ADD(nb->nb_inplace_multiply, tab_imul);
2722 ADD(nb->nb_inplace_divide, tab_idiv);
2723 ADD(nb->nb_inplace_remainder, tab_imod);
2724 ADD(nb->nb_inplace_power, tab_ipow);
2725 ADD(nb->nb_inplace_lshift, tab_ilshift);
2726 ADD(nb->nb_inplace_rshift, tab_irshift);
2727 ADD(nb->nb_inplace_and, tab_iand);
2728 ADD(nb->nb_inplace_xor, tab_ixor);
2729 ADD(nb->nb_inplace_or, tab_ior);
Guido van Rossum874f15a2001-09-25 21:16:33 +00002730 if (type->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2731 ADD(nb->nb_floor_divide, tab_floordiv);
2732 ADD(nb->nb_true_divide, tab_truediv);
2733 ADD(nb->nb_inplace_floor_divide, tab_ifloordiv);
2734 ADD(nb->nb_inplace_true_divide, tab_itruediv);
2735 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002736 }
2737
2738 ADD(type->tp_getattro, tab_getattr);
2739 ADD(type->tp_setattro, tab_setattr);
2740 ADD(type->tp_compare, tab_cmp);
2741 ADD(type->tp_repr, tab_repr);
2742 ADD(type->tp_hash, tab_hash);
2743 ADD(type->tp_call, tab_call);
2744 ADD(type->tp_str, tab_str);
2745 ADD(type->tp_richcompare, tab_richcmp);
2746 ADD(type->tp_iter, tab_iter);
2747 ADD(type->tp_iternext, tab_next);
2748 ADD(type->tp_descr_get, tab_descr_get);
2749 ADD(type->tp_descr_set, tab_descr_set);
2750 ADD(type->tp_init, tab_init);
2751
Guido van Rossumf040ede2001-08-07 16:40:56 +00002752 if (type->tp_new != NULL) {
2753 if (add_tp_new_wrapper(type) < 0)
2754 return -1;
2755 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002756
2757 return 0;
2758}
2759
Guido van Rossumf040ede2001-08-07 16:40:56 +00002760/* Slot wrappers that call the corresponding __foo__ slot. See comments
2761 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002762
Guido van Rossumdc91b992001-08-08 22:26:22 +00002763#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002764static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002765FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002766{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002767 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002768 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002769}
2770
Guido van Rossumdc91b992001-08-08 22:26:22 +00002771#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002773FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002774{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002775 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002776 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002777}
2778
Guido van Rossumdc91b992001-08-08 22:26:22 +00002779
2780#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002782FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002783{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002784 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002785 int do_other = self->ob_type != other->ob_type && \
2786 other->ob_type->tp_as_number != NULL && \
2787 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002788 if (self->ob_type->tp_as_number != NULL && \
2789 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2790 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002791 if (do_other && \
2792 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2793 r = call_maybe( \
2794 other, ROPSTR, &rcache_str, "(O)", self); \
2795 if (r != Py_NotImplemented) \
2796 return r; \
2797 Py_DECREF(r); \
2798 do_other = 0; \
2799 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002800 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002801 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002802 if (r != Py_NotImplemented || \
2803 other->ob_type == self->ob_type) \
2804 return r; \
2805 Py_DECREF(r); \
2806 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002807 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002808 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002809 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002810 } \
2811 Py_INCREF(Py_NotImplemented); \
2812 return Py_NotImplemented; \
2813}
2814
2815#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2816 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2817
2818#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2819static PyObject * \
2820FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2821{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002822 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002823 return call_method(self, OPSTR, &cache_str, \
2824 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002825}
2826
2827static int
2828slot_sq_length(PyObject *self)
2829{
Guido van Rossum2730b132001-08-28 18:22:14 +00002830 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002831 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002832 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002833
2834 if (res == NULL)
2835 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002836 len = (int)PyInt_AsLong(res);
2837 Py_DECREF(res);
2838 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002839}
2840
Guido van Rossumdc91b992001-08-08 22:26:22 +00002841SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2842SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002843
2844/* Super-optimized version of slot_sq_item.
2845 Other slots could do the same... */
2846static PyObject *
2847slot_sq_item(PyObject *self, int i)
2848{
2849 static PyObject *getitem_str;
2850 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2851 descrgetfunc f;
2852
2853 if (getitem_str == NULL) {
2854 getitem_str = PyString_InternFromString("__getitem__");
2855 if (getitem_str == NULL)
2856 return NULL;
2857 }
2858 func = _PyType_Lookup(self->ob_type, getitem_str);
2859 if (func != NULL) {
2860 if (func->ob_type == &PyWrapperDescr_Type) {
2861 PyWrapperDescrObject *wrapper =
2862 (PyWrapperDescrObject *)func;
2863 if (wrapper->d_base->wrapper == wrap_sq_item) {
2864 intargfunc f;
2865 f = (intargfunc)(wrapper->d_wrapped);
2866 return f(self, i);
2867 }
2868 }
2869 if ((f = func->ob_type->tp_descr_get) == NULL)
2870 Py_INCREF(func);
2871 else
2872 func = f(func, self, (PyObject *)(self->ob_type));
2873 ival = PyInt_FromLong(i);
2874 if (ival != NULL) {
2875 args = PyTuple_New(1);
2876 if (args != NULL) {
2877 PyTuple_SET_ITEM(args, 0, ival);
2878 retval = PyObject_Call(func, args, NULL);
2879 Py_XDECREF(args);
2880 Py_XDECREF(func);
2881 return retval;
2882 }
2883 }
2884 }
2885 else {
2886 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2887 }
2888 Py_XDECREF(args);
2889 Py_XDECREF(ival);
2890 Py_XDECREF(func);
2891 return NULL;
2892}
2893
Guido van Rossumdc91b992001-08-08 22:26:22 +00002894SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002895
2896static int
2897slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2898{
2899 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002900 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002901
2902 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002903 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002904 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002905 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002906 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002907 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002908 if (res == NULL)
2909 return -1;
2910 Py_DECREF(res);
2911 return 0;
2912}
2913
2914static int
2915slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2916{
2917 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002918 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002919
2920 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002921 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002922 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002923 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002924 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002925 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002926 if (res == NULL)
2927 return -1;
2928 Py_DECREF(res);
2929 return 0;
2930}
2931
2932static int
2933slot_sq_contains(PyObject *self, PyObject *value)
2934{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002935 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002936 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002937
Guido van Rossum55f20992001-10-01 17:18:22 +00002938 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002939
2940 if (func != NULL) {
2941 args = Py_BuildValue("(O)", value);
2942 if (args == NULL)
2943 res = NULL;
2944 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002945 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002946 Py_DECREF(args);
2947 }
2948 Py_DECREF(func);
2949 if (res == NULL)
2950 return -1;
2951 return PyObject_IsTrue(res);
2952 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002953 else if (PyErr_Occurred())
2954 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002955 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002956 return _PySequence_IterSearch(self, value,
2957 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002958 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002959}
2960
Guido van Rossumdc91b992001-08-08 22:26:22 +00002961SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2962SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002963
2964#define slot_mp_length slot_sq_length
2965
Guido van Rossumdc91b992001-08-08 22:26:22 +00002966SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002967
2968static int
2969slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2970{
2971 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002972 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002973
2974 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002975 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002976 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002977 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002978 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002979 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002980 if (res == NULL)
2981 return -1;
2982 Py_DECREF(res);
2983 return 0;
2984}
2985
Guido van Rossumdc91b992001-08-08 22:26:22 +00002986SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2987SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2988SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2989SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2990SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2991SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2992
2993staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2994
2995SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2996 nb_power, "__pow__", "__rpow__")
2997
2998static PyObject *
2999slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3000{
Guido van Rossum2730b132001-08-28 18:22:14 +00003001 static PyObject *pow_str;
3002
Guido van Rossumdc91b992001-08-08 22:26:22 +00003003 if (modulus == Py_None)
3004 return slot_nb_power_binary(self, other);
3005 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00003006 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003007 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003008}
3009
3010SLOT0(slot_nb_negative, "__neg__")
3011SLOT0(slot_nb_positive, "__pos__")
3012SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003013
3014static int
3015slot_nb_nonzero(PyObject *self)
3016{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003017 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003018 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003019
Guido van Rossum55f20992001-10-01 17:18:22 +00003020 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003021 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003022 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003023 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003024 func = lookup_maybe(self, "__len__", &len_str);
3025 if (func == NULL) {
3026 if (PyErr_Occurred())
3027 return -1;
3028 else
3029 return 1;
3030 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003031 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003032 res = PyObject_CallObject(func, NULL);
3033 Py_DECREF(func);
3034 if (res == NULL)
3035 return -1;
3036 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003037}
3038
Guido van Rossumdc91b992001-08-08 22:26:22 +00003039SLOT0(slot_nb_invert, "__invert__")
3040SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3041SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3042SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3043SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3044SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003045
3046static int
3047slot_nb_coerce(PyObject **a, PyObject **b)
3048{
3049 static PyObject *coerce_str;
3050 PyObject *self = *a, *other = *b;
3051
3052 if (self->ob_type->tp_as_number != NULL &&
3053 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3054 PyObject *r;
3055 r = call_maybe(
3056 self, "__coerce__", &coerce_str, "(O)", other);
3057 if (r == NULL)
3058 return -1;
3059 if (r == Py_NotImplemented) {
3060 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003061 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003062 else {
3063 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3064 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003065 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003066 Py_DECREF(r);
3067 return -1;
3068 }
3069 *a = PyTuple_GET_ITEM(r, 0);
3070 Py_INCREF(*a);
3071 *b = PyTuple_GET_ITEM(r, 1);
3072 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003073 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003074 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003075 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003076 }
3077 if (other->ob_type->tp_as_number != NULL &&
3078 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3079 PyObject *r;
3080 r = call_maybe(
3081 other, "__coerce__", &coerce_str, "(O)", self);
3082 if (r == NULL)
3083 return -1;
3084 if (r == Py_NotImplemented) {
3085 Py_DECREF(r);
3086 return 1;
3087 }
3088 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3089 PyErr_SetString(PyExc_TypeError,
3090 "__coerce__ didn't return a 2-tuple");
3091 Py_DECREF(r);
3092 return -1;
3093 }
3094 *a = PyTuple_GET_ITEM(r, 1);
3095 Py_INCREF(*a);
3096 *b = PyTuple_GET_ITEM(r, 0);
3097 Py_INCREF(*b);
3098 Py_DECREF(r);
3099 return 0;
3100 }
3101 return 1;
3102}
3103
Guido van Rossumdc91b992001-08-08 22:26:22 +00003104SLOT0(slot_nb_int, "__int__")
3105SLOT0(slot_nb_long, "__long__")
3106SLOT0(slot_nb_float, "__float__")
3107SLOT0(slot_nb_oct, "__oct__")
3108SLOT0(slot_nb_hex, "__hex__")
3109SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3110SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3111SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3112SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3113SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3114SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3115SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3116SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3117SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3118SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3119SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3120SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3121 "__floordiv__", "__rfloordiv__")
3122SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3123SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3124SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003125
3126static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003127half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003128{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003129 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003130 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003131 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003132
Guido van Rossum60718732001-08-28 17:47:51 +00003133 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003134 if (func == NULL) {
3135 PyErr_Clear();
3136 }
3137 else {
3138 args = Py_BuildValue("(O)", other);
3139 if (args == NULL)
3140 res = NULL;
3141 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003142 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003143 Py_DECREF(args);
3144 }
3145 if (res != Py_NotImplemented) {
3146 if (res == NULL)
3147 return -2;
3148 c = PyInt_AsLong(res);
3149 Py_DECREF(res);
3150 if (c == -1 && PyErr_Occurred())
3151 return -2;
3152 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3153 }
3154 Py_DECREF(res);
3155 }
3156 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003157}
3158
Guido van Rossumab3b0342001-09-18 20:38:53 +00003159/* This slot is published for the benefit of try_3way_compare in object.c */
3160int
3161_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003162{
3163 int c;
3164
Guido van Rossumab3b0342001-09-18 20:38:53 +00003165 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003166 c = half_compare(self, other);
3167 if (c <= 1)
3168 return c;
3169 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003170 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003171 c = half_compare(other, self);
3172 if (c < -1)
3173 return -2;
3174 if (c <= 1)
3175 return -c;
3176 }
3177 return (void *)self < (void *)other ? -1 :
3178 (void *)self > (void *)other ? 1 : 0;
3179}
3180
3181static PyObject *
3182slot_tp_repr(PyObject *self)
3183{
3184 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003185 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003186
Guido van Rossum60718732001-08-28 17:47:51 +00003187 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003188 if (func != NULL) {
3189 res = PyEval_CallObject(func, NULL);
3190 Py_DECREF(func);
3191 return res;
3192 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003193 PyErr_Clear();
3194 return PyString_FromFormat("<%s object at %p>",
3195 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003196}
3197
3198static PyObject *
3199slot_tp_str(PyObject *self)
3200{
3201 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003202 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003203
Guido van Rossum60718732001-08-28 17:47:51 +00003204 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003205 if (func != NULL) {
3206 res = PyEval_CallObject(func, NULL);
3207 Py_DECREF(func);
3208 return res;
3209 }
3210 else {
3211 PyErr_Clear();
3212 return slot_tp_repr(self);
3213 }
3214}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003215
3216static long
3217slot_tp_hash(PyObject *self)
3218{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003219 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003220 static PyObject *hash_str, *eq_str, *cmp_str;
3221
Tim Peters6d6c1a32001-08-02 04:15:00 +00003222 long h;
3223
Guido van Rossum60718732001-08-28 17:47:51 +00003224 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003225
3226 if (func != NULL) {
3227 res = PyEval_CallObject(func, NULL);
3228 Py_DECREF(func);
3229 if (res == NULL)
3230 return -1;
3231 h = PyInt_AsLong(res);
3232 }
3233 else {
3234 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003235 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003236 if (func == NULL) {
3237 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003238 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003239 }
3240 if (func != NULL) {
3241 Py_DECREF(func);
3242 PyErr_SetString(PyExc_TypeError, "unhashable type");
3243 return -1;
3244 }
3245 PyErr_Clear();
3246 h = _Py_HashPointer((void *)self);
3247 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003248 if (h == -1 && !PyErr_Occurred())
3249 h = -2;
3250 return h;
3251}
3252
3253static PyObject *
3254slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3255{
Guido van Rossum60718732001-08-28 17:47:51 +00003256 static PyObject *call_str;
3257 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003258 PyObject *res;
3259
3260 if (meth == NULL)
3261 return NULL;
3262 res = PyObject_Call(meth, args, kwds);
3263 Py_DECREF(meth);
3264 return res;
3265}
3266
Tim Peters6d6c1a32001-08-02 04:15:00 +00003267static PyObject *
3268slot_tp_getattro(PyObject *self, PyObject *name)
3269{
3270 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003271 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00003272 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003273
Guido van Rossum8e248182001-08-12 05:17:56 +00003274 if (getattr_str == NULL) {
Guido van Rossum867a8d22001-09-21 19:29:08 +00003275 getattr_str = PyString_InternFromString("__getattribute__");
Guido van Rossum8e248182001-08-12 05:17:56 +00003276 if (getattr_str == NULL)
3277 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003278 }
Guido van Rossum8e248182001-08-12 05:17:56 +00003279 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00003280 if (getattr == NULL) {
3281 /* Avoid further slowdowns */
3282 if (tp->tp_getattro == slot_tp_getattro)
3283 tp->tp_getattro = PyObject_GenericGetAttr;
Guido van Rossum8e248182001-08-12 05:17:56 +00003284 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00003285 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003286 return PyObject_CallFunction(getattr, "OO", self, name);
3287}
3288
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003289static PyObject *
3290slot_tp_getattr_hook(PyObject *self, PyObject *name)
3291{
3292 PyTypeObject *tp = self->ob_type;
3293 PyObject *getattr, *getattribute, *res;
3294 static PyObject *getattribute_str = NULL;
3295 static PyObject *getattr_str = NULL;
3296
3297 if (getattr_str == NULL) {
3298 getattr_str = PyString_InternFromString("__getattr__");
3299 if (getattr_str == NULL)
3300 return NULL;
3301 }
3302 if (getattribute_str == NULL) {
3303 getattribute_str =
3304 PyString_InternFromString("__getattribute__");
3305 if (getattribute_str == NULL)
3306 return NULL;
3307 }
3308 getattr = _PyType_Lookup(tp, getattr_str);
3309 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum1e1de1c2001-10-03 13:58:35 +00003310 if (getattribute != NULL &&
3311 getattribute->ob_type == &PyWrapperDescr_Type &&
3312 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3313 PyObject_GenericGetAttr)
3314 getattribute = NULL;
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003315 if (getattr == NULL && getattribute == NULL) {
3316 /* Avoid further slowdowns */
Guido van Rossum1e1de1c2001-10-03 13:58:35 +00003317 /* XXX This is questionable: it means that a class that
3318 isn't born with __getattr__ or __getattribute__ cannot
3319 acquire them in later life. But it's a relatively big
3320 speedup, so I'm keeping it in for now. If this is
3321 removed, you can also remove the "def __getattr__" from
3322 class C (marked with another XXX comment) in dynamics()
3323 in Lib/test/test_descr.py. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003324 if (tp->tp_getattro == slot_tp_getattr_hook)
3325 tp->tp_getattro = PyObject_GenericGetAttr;
3326 return PyObject_GenericGetAttr(self, name);
3327 }
3328 if (getattribute == NULL)
3329 res = PyObject_GenericGetAttr(self, name);
3330 else
3331 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum3926a632001-09-25 16:25:58 +00003332 if (getattr != NULL &&
3333 res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003334 PyErr_Clear();
3335 res = PyObject_CallFunction(getattr, "OO", self, name);
3336 }
3337 return res;
3338}
3339
Tim Peters6d6c1a32001-08-02 04:15:00 +00003340static int
3341slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3342{
3343 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003344 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345
3346 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003347 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003348 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003349 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003350 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003351 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003352 if (res == NULL)
3353 return -1;
3354 Py_DECREF(res);
3355 return 0;
3356}
3357
3358/* Map rich comparison operators to their __xx__ namesakes */
3359static char *name_op[] = {
3360 "__lt__",
3361 "__le__",
3362 "__eq__",
3363 "__ne__",
3364 "__gt__",
3365 "__ge__",
3366};
3367
3368static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003369half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003370{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003371 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003372 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373
Guido van Rossum60718732001-08-28 17:47:51 +00003374 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003375 if (func == NULL) {
3376 PyErr_Clear();
3377 Py_INCREF(Py_NotImplemented);
3378 return Py_NotImplemented;
3379 }
3380 args = Py_BuildValue("(O)", other);
3381 if (args == NULL)
3382 res = NULL;
3383 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003384 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003385 Py_DECREF(args);
3386 }
3387 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003388 return res;
3389}
3390
Guido van Rossumb8f63662001-08-15 23:57:02 +00003391/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3392static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3393
3394static PyObject *
3395slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3396{
3397 PyObject *res;
3398
3399 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3400 res = half_richcompare(self, other, op);
3401 if (res != Py_NotImplemented)
3402 return res;
3403 Py_DECREF(res);
3404 }
3405 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3406 res = half_richcompare(other, self, swapped_op[op]);
3407 if (res != Py_NotImplemented) {
3408 return res;
3409 }
3410 Py_DECREF(res);
3411 }
3412 Py_INCREF(Py_NotImplemented);
3413 return Py_NotImplemented;
3414}
3415
3416static PyObject *
3417slot_tp_iter(PyObject *self)
3418{
3419 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003420 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003421
Guido van Rossum60718732001-08-28 17:47:51 +00003422 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003423 if (func != NULL) {
3424 res = PyObject_CallObject(func, NULL);
3425 Py_DECREF(func);
3426 return res;
3427 }
3428 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003429 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003430 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003431 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003432 return NULL;
3433 }
3434 Py_DECREF(func);
3435 return PySeqIter_New(self);
3436}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003437
3438static PyObject *
3439slot_tp_iternext(PyObject *self)
3440{
Guido van Rossum2730b132001-08-28 18:22:14 +00003441 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003442 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003443}
3444
Guido van Rossum1a493502001-08-17 16:47:50 +00003445static PyObject *
3446slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3447{
3448 PyTypeObject *tp = self->ob_type;
3449 PyObject *get;
3450 static PyObject *get_str = NULL;
3451
3452 if (get_str == NULL) {
3453 get_str = PyString_InternFromString("__get__");
3454 if (get_str == NULL)
3455 return NULL;
3456 }
3457 get = _PyType_Lookup(tp, get_str);
3458 if (get == NULL) {
3459 /* Avoid further slowdowns */
3460 if (tp->tp_descr_get == slot_tp_descr_get)
3461 tp->tp_descr_get = NULL;
3462 Py_INCREF(self);
3463 return self;
3464 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003465 if (obj == NULL)
3466 obj = Py_None;
3467 if (type == NULL)
3468 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003469 return PyObject_CallFunction(get, "OOO", self, obj, type);
3470}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003471
3472static int
3473slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3474{
Guido van Rossum2c252392001-08-24 10:13:31 +00003475 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003476 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003477
3478 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003479 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003480 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003481 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003482 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003483 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003484 if (res == NULL)
3485 return -1;
3486 Py_DECREF(res);
3487 return 0;
3488}
3489
3490static int
3491slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3492{
Guido van Rossum60718732001-08-28 17:47:51 +00003493 static PyObject *init_str;
3494 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003495 PyObject *res;
3496
3497 if (meth == NULL)
3498 return -1;
3499 res = PyObject_Call(meth, args, kwds);
3500 Py_DECREF(meth);
3501 if (res == NULL)
3502 return -1;
3503 Py_DECREF(res);
3504 return 0;
3505}
3506
3507static PyObject *
3508slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3509{
3510 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3511 PyObject *newargs, *x;
3512 int i, n;
3513
3514 if (func == NULL)
3515 return NULL;
3516 assert(PyTuple_Check(args));
3517 n = PyTuple_GET_SIZE(args);
3518 newargs = PyTuple_New(n+1);
3519 if (newargs == NULL)
3520 return NULL;
3521 Py_INCREF(type);
3522 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3523 for (i = 0; i < n; i++) {
3524 x = PyTuple_GET_ITEM(args, i);
3525 Py_INCREF(x);
3526 PyTuple_SET_ITEM(newargs, i+1, x);
3527 }
3528 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003529 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003530 Py_DECREF(func);
3531 return x;
3532}
3533
Guido van Rossumf040ede2001-08-07 16:40:56 +00003534/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003535 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00003536 The dict argument is the dictionary argument passed to type_new(),
3537 which is the local namespace of the class statement, in other
3538 words, it contains the methods. For each special method (like
3539 __repr__) defined in the dictionary, the corresponding function
3540 slot in the type object (like tp_repr) is set to a special function
3541 whose name is 'slot_' followed by the slot name and whose signature
3542 is whatever is required for that slot. These slot functions look
3543 up the corresponding method in the type's dictionary and call it.
3544 The slot functions have to take care of the various peculiarities
3545 of the mapping between slots and special methods, such as mapping
3546 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
3547 etc.) or mapping multiple slots to a single method (sq_item,
3548 mp_subscript <--> __getitem__). */
3549
Tim Peters6d6c1a32001-08-02 04:15:00 +00003550static void
3551override_slots(PyTypeObject *type, PyObject *dict)
3552{
3553 PySequenceMethods *sq = type->tp_as_sequence;
3554 PyMappingMethods *mp = type->tp_as_mapping;
3555 PyNumberMethods *nb = type->tp_as_number;
3556
Guido van Rossumdc91b992001-08-08 22:26:22 +00003557#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003558 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003559 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003560 }
3561
Guido van Rossumdc91b992001-08-08 22:26:22 +00003562#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003563 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003564 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003565 }
3566
Guido van Rossumdc91b992001-08-08 22:26:22 +00003567#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003568 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003569 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003570 }
3571
Guido van Rossumdc91b992001-08-08 22:26:22 +00003572#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003573 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003574 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003575 }
3576
Guido van Rossumdc91b992001-08-08 22:26:22 +00003577 SQSLOT("__len__", sq_length, slot_sq_length);
3578 SQSLOT("__add__", sq_concat, slot_sq_concat);
3579 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
3580 SQSLOT("__getitem__", sq_item, slot_sq_item);
3581 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
3582 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
3583 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
3584 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
3585 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
3586 SQSLOT("__contains__", sq_contains, slot_sq_contains);
3587 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
3588 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003589
Guido van Rossumdc91b992001-08-08 22:26:22 +00003590 MPSLOT("__len__", mp_length, slot_mp_length);
3591 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
3592 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
3593 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003594
Guido van Rossumdc91b992001-08-08 22:26:22 +00003595 NBSLOT("__add__", nb_add, slot_nb_add);
3596 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
3597 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
3598 NBSLOT("__div__", nb_divide, slot_nb_divide);
3599 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
3600 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
3601 NBSLOT("__pow__", nb_power, slot_nb_power);
3602 NBSLOT("__neg__", nb_negative, slot_nb_negative);
3603 NBSLOT("__pos__", nb_positive, slot_nb_positive);
3604 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
3605 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
3606 NBSLOT("__invert__", nb_invert, slot_nb_invert);
3607 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
3608 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
3609 NBSLOT("__and__", nb_and, slot_nb_and);
3610 NBSLOT("__xor__", nb_xor, slot_nb_xor);
3611 NBSLOT("__or__", nb_or, slot_nb_or);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003612 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003613 NBSLOT("__int__", nb_int, slot_nb_int);
3614 NBSLOT("__long__", nb_long, slot_nb_long);
3615 NBSLOT("__float__", nb_float, slot_nb_float);
3616 NBSLOT("__oct__", nb_oct, slot_nb_oct);
3617 NBSLOT("__hex__", nb_hex, slot_nb_hex);
3618 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
3619 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
3620 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
3621 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
3622 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
3623 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
3624 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
3625 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
3626 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
3627 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
3628 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
3629 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
3630 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
3631 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3632 slot_nb_inplace_floor_divide);
3633 NBSLOT("__itruediv__", nb_inplace_true_divide,
3634 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003635
Guido van Rossum8e248182001-08-12 05:17:56 +00003636 if (dict == NULL ||
3637 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00003638 PyDict_GetItemString(dict, "__repr__"))
3639 type->tp_print = NULL;
3640
Guido van Rossumab3b0342001-09-18 20:38:53 +00003641 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003642 TPSLOT("__repr__", tp_repr, slot_tp_repr);
3643 TPSLOT("__hash__", tp_hash, slot_tp_hash);
3644 TPSLOT("__call__", tp_call, slot_tp_call);
3645 TPSLOT("__str__", tp_str, slot_tp_str);
Guido van Rossum867a8d22001-09-21 19:29:08 +00003646 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattro);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003647 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003648 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
3649 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
3650 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
3651 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
3652 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
3653 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
3654 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
3655 TPSLOT("__iter__", tp_iter, slot_tp_iter);
3656 TPSLOT("next", tp_iternext, slot_tp_iternext);
3657 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
3658 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
3659 TPSLOT("__init__", tp_init, slot_tp_init);
3660 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003661}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003662
3663
3664/* Cooperative 'super' */
3665
3666typedef struct {
3667 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003668 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003669 PyObject *obj;
3670} superobject;
3671
Guido van Rossum6f799372001-09-20 20:46:19 +00003672static PyMemberDef super_members[] = {
3673 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3674 "the class invoking super()"},
3675 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3676 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003677 {0}
3678};
3679
Guido van Rossum705f0f52001-08-24 16:47:00 +00003680static void
3681super_dealloc(PyObject *self)
3682{
3683 superobject *su = (superobject *)self;
3684
Guido van Rossum048eb752001-10-02 21:24:57 +00003685 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003686 Py_XDECREF(su->obj);
3687 Py_XDECREF(su->type);
3688 self->ob_type->tp_free(self);
3689}
3690
3691static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003692super_repr(PyObject *self)
3693{
3694 superobject *su = (superobject *)self;
3695
3696 if (su->obj)
3697 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003698 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003699 su->type ? su->type->tp_name : "NULL",
3700 su->obj->ob_type->tp_name);
3701 else
3702 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003703 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003704 su->type ? su->type->tp_name : "NULL");
3705}
3706
3707static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003708super_getattro(PyObject *self, PyObject *name)
3709{
3710 superobject *su = (superobject *)self;
3711
3712 if (su->obj != NULL) {
3713 PyObject *mro, *res, *tmp;
3714 descrgetfunc f;
3715 int i, n;
3716
Guido van Rossume705ef12001-08-29 15:47:06 +00003717 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003718 if (mro == NULL)
3719 n = 0;
3720 else {
3721 assert(PyTuple_Check(mro));
3722 n = PyTuple_GET_SIZE(mro);
3723 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003724 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003725 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003726 break;
3727 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003728 if (i >= n && PyType_Check(su->obj)) {
3729 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003730 if (mro == NULL)
3731 n = 0;
3732 else {
3733 assert(PyTuple_Check(mro));
3734 n = PyTuple_GET_SIZE(mro);
3735 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003736 for (i = 0; i < n; i++) {
3737 if ((PyObject *)(su->type) ==
3738 PyTuple_GET_ITEM(mro, i))
3739 break;
3740 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003741 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003742 i++;
3743 res = NULL;
3744 for (; i < n; i++) {
3745 tmp = PyTuple_GET_ITEM(mro, i);
3746 assert(PyType_Check(tmp));
3747 res = PyDict_GetItem(
3748 ((PyTypeObject *)tmp)->tp_defined, name);
3749 if (res != NULL) {
3750 Py_INCREF(res);
3751 f = res->ob_type->tp_descr_get;
3752 if (f != NULL) {
3753 tmp = f(res, su->obj, res);
3754 Py_DECREF(res);
3755 res = tmp;
3756 }
3757 return res;
3758 }
3759 }
3760 }
3761 return PyObject_GenericGetAttr(self, name);
3762}
3763
3764static PyObject *
3765super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3766{
3767 superobject *su = (superobject *)self;
3768 superobject *new;
3769
3770 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3771 /* Not binding to an object, or already bound */
3772 Py_INCREF(self);
3773 return self;
3774 }
3775 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3776 if (new == NULL)
3777 return NULL;
3778 Py_INCREF(su->type);
3779 Py_INCREF(obj);
3780 new->type = su->type;
3781 new->obj = obj;
3782 return (PyObject *)new;
3783}
3784
3785static int
3786super_init(PyObject *self, PyObject *args, PyObject *kwds)
3787{
3788 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003789 PyTypeObject *type;
3790 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003791
3792 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3793 return -1;
3794 if (obj == Py_None)
3795 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003796 if (obj != NULL &&
3797 !PyType_IsSubtype(obj->ob_type, type) &&
3798 !(PyType_Check(obj) &&
3799 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003800 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003801 "super(type, obj): "
3802 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003803 return -1;
3804 }
3805 Py_INCREF(type);
3806 Py_XINCREF(obj);
3807 su->type = type;
3808 su->obj = obj;
3809 return 0;
3810}
3811
3812static char super_doc[] =
3813"super(type) -> unbound super object\n"
3814"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003815"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003816"Typical use to call a cooperative superclass method:\n"
3817"class C(B):\n"
3818" def meth(self, arg):\n"
3819" super(C, self).meth(arg)";
3820
Guido van Rossum048eb752001-10-02 21:24:57 +00003821static int
3822super_traverse(PyObject *self, visitproc visit, void *arg)
3823{
3824 superobject *su = (superobject *)self;
3825 int err;
3826
3827#define VISIT(SLOT) \
3828 if (SLOT) { \
3829 err = visit((PyObject *)(SLOT), arg); \
3830 if (err) \
3831 return err; \
3832 }
3833
3834 VISIT(su->obj);
3835 VISIT(su->type);
3836
3837#undef VISIT
3838
3839 return 0;
3840}
3841
Guido van Rossum705f0f52001-08-24 16:47:00 +00003842PyTypeObject PySuper_Type = {
3843 PyObject_HEAD_INIT(&PyType_Type)
3844 0, /* ob_size */
3845 "super", /* tp_name */
3846 sizeof(superobject), /* tp_basicsize */
3847 0, /* tp_itemsize */
3848 /* methods */
3849 super_dealloc, /* tp_dealloc */
3850 0, /* tp_print */
3851 0, /* tp_getattr */
3852 0, /* tp_setattr */
3853 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003854 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003855 0, /* tp_as_number */
3856 0, /* tp_as_sequence */
3857 0, /* tp_as_mapping */
3858 0, /* tp_hash */
3859 0, /* tp_call */
3860 0, /* tp_str */
3861 super_getattro, /* tp_getattro */
3862 0, /* tp_setattro */
3863 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003864 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3865 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003866 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003867 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003868 0, /* tp_clear */
3869 0, /* tp_richcompare */
3870 0, /* tp_weaklistoffset */
3871 0, /* tp_iter */
3872 0, /* tp_iternext */
3873 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003874 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003875 0, /* tp_getset */
3876 0, /* tp_base */
3877 0, /* tp_dict */
3878 super_descr_get, /* tp_descr_get */
3879 0, /* tp_descr_set */
3880 0, /* tp_dictoffset */
3881 super_init, /* tp_init */
3882 PyType_GenericAlloc, /* tp_alloc */
3883 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003884 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003885};