blob: e8f436c770fd80fd0783abb81d1f3bf2a19c13d7 [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.
762 Otherwise, a default is calculated from the base classes:
763 if any base class is dynamic, this class is dynamic; otherwise
764 it is static. */
765 dynamic = -1; /* Not yet determined */
766 /* Look in the class */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767 tmp = PyDict_GetItemString(dict, "__dynamic__");
768 if (tmp != NULL) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000769 dynamic = PyInt_AsLong(tmp);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770 if (dynamic < 0)
Guido van Rossum1a493502001-08-17 16:47:50 +0000771 PyErr_Clear();
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772 }
Guido van Rossum1a493502001-08-17 16:47:50 +0000773 if (dynamic < 0) {
774 /* Look in the module globals */
775 tmp = PyEval_GetGlobals();
776 if (tmp != NULL) {
777 tmp = PyDict_GetItemString(tmp, "__dynamic__");
778 if (tmp != NULL) {
779 dynamic = PyInt_AsLong(tmp);
780 if (dynamic < 0)
781 PyErr_Clear();
782 }
783 }
784 }
785 if (dynamic < 0) {
786 /* Make a new class dynamic if any of its bases is
787 dynamic. This is not always the same as inheriting
788 the __dynamic__ class attribute! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000789 dynamic = 0;
790 for (i = 0; i < nbases; i++) {
Guido van Rossum1a493502001-08-17 16:47:50 +0000791 tmptype = (PyTypeObject *)
792 PyTuple_GET_ITEM(bases, i);
793 if (tmptype->tp_flags &
794 Py_TPFLAGS_DYNAMICTYPE) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795 dynamic = 1;
796 break;
797 }
798 }
799 }
800
801 /* Check for a __slots__ sequence variable in dict, and count it */
802 slots = PyDict_GetItemString(dict, "__slots__");
803 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000804 add_dict = 0;
805 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806 if (slots != NULL) {
807 /* Make it into a tuple */
808 if (PyString_Check(slots))
809 slots = Py_BuildValue("(O)", slots);
810 else
811 slots = PySequence_Tuple(slots);
812 if (slots == NULL)
813 return NULL;
814 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000815 if (nslots > 0 && base->tp_itemsize != 0) {
816 PyErr_Format(PyExc_TypeError,
817 "nonempty __slots__ "
818 "not supported for subtype of '%s'",
819 base->tp_name);
820 return NULL;
821 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000822 for (i = 0; i < nslots; i++) {
823 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
824 PyErr_SetString(PyExc_TypeError,
825 "__slots__ must be a sequence of strings");
826 Py_DECREF(slots);
827 return NULL;
828 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000829 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000830 }
831 }
832 if (slots == NULL && base->tp_dictoffset == 0 &&
833 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000834 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000835 add_dict++;
836 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000837 if (slots == NULL && base->tp_weaklistoffset == 0 &&
838 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000839 nslots++;
840 add_weak++;
841 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000842
843 /* XXX From here until type is safely allocated,
844 "return NULL" may leak slots! */
845
846 /* Allocate the type object */
847 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
848 if (type == NULL)
849 return NULL;
850
851 /* Keep name and slots alive in the extended type object */
852 et = (etype *)type;
853 Py_INCREF(name);
854 et->name = name;
855 et->slots = slots;
856
Guido van Rossumdc91b992001-08-08 22:26:22 +0000857 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000858 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
859 Py_TPFLAGS_BASETYPE;
860 if (dynamic)
861 type->tp_flags |= Py_TPFLAGS_DYNAMICTYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +0000862 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
863 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +0000864
865 /* It's a new-style number unless it specifically inherits any
866 old-style numeric behavior */
867 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
868 (base->tp_as_number == NULL))
869 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
870
871 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000872 type->tp_as_number = &et->as_number;
873 type->tp_as_sequence = &et->as_sequence;
874 type->tp_as_mapping = &et->as_mapping;
875 type->tp_as_buffer = &et->as_buffer;
876 type->tp_name = PyString_AS_STRING(name);
877
878 /* Set tp_base and tp_bases */
879 type->tp_bases = bases;
880 Py_INCREF(base);
881 type->tp_base = base;
882
883 /* Initialize tp_defined from passed-in dict */
884 type->tp_defined = dict = PyDict_Copy(dict);
885 if (dict == NULL) {
886 Py_DECREF(type);
887 return NULL;
888 }
889
Guido van Rossumc3542212001-08-16 09:18:56 +0000890 /* Set __module__ in the dict */
891 if (PyDict_GetItemString(dict, "__module__") == NULL) {
892 tmp = PyEval_GetGlobals();
893 if (tmp != NULL) {
894 tmp = PyDict_GetItemString(tmp, "__name__");
895 if (tmp != NULL) {
896 if (PyDict_SetItemString(dict, "__module__",
897 tmp) < 0)
898 return NULL;
899 }
900 }
901 }
902
Tim Peters2f93e282001-10-04 05:27:00 +0000903 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
904 and is a string (tp_doc is a char* -- can't copy a general object
905 into it).
906 XXX What if it's a Unicode string? Don't know -- this ignores it.
907 */
908 {
909 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
910 if (doc != NULL && PyString_Check(doc)) {
911 const size_t n = (size_t)PyString_GET_SIZE(doc);
912 type->tp_doc = PyObject_MALLOC(n+1);
913 if (type->tp_doc == NULL) {
914 Py_DECREF(type);
915 return NULL;
916 }
917 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
918 }
919 }
920
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921 /* Special-case __new__: if it's a plain function,
922 make it a static function */
923 tmp = PyDict_GetItemString(dict, "__new__");
924 if (tmp != NULL && PyFunction_Check(tmp)) {
925 tmp = PyStaticMethod_New(tmp);
926 if (tmp == NULL) {
927 Py_DECREF(type);
928 return NULL;
929 }
930 PyDict_SetItemString(dict, "__new__", tmp);
931 Py_DECREF(tmp);
932 }
933
934 /* Add descriptors for custom slots from __slots__, or for __dict__ */
935 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +0000936 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000937 if (slots != NULL) {
938 for (i = 0; i < nslots; i++, mp++) {
939 mp->name = PyString_AS_STRING(
940 PyTuple_GET_ITEM(slots, i));
941 mp->type = T_OBJECT;
942 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000943 if (base->tp_weaklistoffset == 0 &&
944 strcmp(mp->name, "__weakref__") == 0)
945 type->tp_weaklistoffset = slotoffset;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946 slotoffset += sizeof(PyObject *);
947 }
948 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000949 else {
950 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000951 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +0000952 type->tp_dictoffset =
953 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000954 else
955 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +0000956 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000957 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +0000958 }
959 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000960 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +0000961 type->tp_weaklistoffset = slotoffset;
962 mp->name = "__weakref__";
963 mp->type = T_OBJECT;
964 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +0000965 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +0000966 mp++;
967 slotoffset += sizeof(PyObject *);
968 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000969 }
970 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000971 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +0000972 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000973
974 /* Special case some slots */
975 if (type->tp_dictoffset != 0 || nslots > 0) {
976 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
977 type->tp_getattro = PyObject_GenericGetAttr;
978 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
979 type->tp_setattro = PyObject_GenericSetAttr;
980 }
981 type->tp_dealloc = subtype_dealloc;
982
983 /* Always override allocation strategy to use regular heap */
984 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +0000985 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
986 type->tp_free = _PyObject_GC_Del;
987 type->tp_traverse = base->tp_traverse;
988 type->tp_clear = base->tp_clear;
989 }
990 else
991 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000992
993 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000994 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000995 Py_DECREF(type);
996 return NULL;
997 }
998
999 /* Override slots that deserve it */
Guido van Rossum8e248182001-08-12 05:17:56 +00001000 if (!PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE))
1001 override_slots(type, type->tp_defined);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001002
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 return (PyObject *)type;
1004}
1005
1006/* Internal API to look for a name through the MRO.
1007 This returns a borrowed reference, and doesn't set an exception! */
1008PyObject *
1009_PyType_Lookup(PyTypeObject *type, PyObject *name)
1010{
1011 int i, n;
1012 PyObject *mro, *res, *dict;
1013
1014 /* For static types, look in tp_dict */
1015 if (!(type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)) {
1016 dict = type->tp_dict;
1017 assert(dict && PyDict_Check(dict));
1018 return PyDict_GetItem(dict, name);
1019 }
1020
1021 /* For dynamic types, look in tp_defined of types in MRO */
1022 mro = type->tp_mro;
1023 assert(PyTuple_Check(mro));
1024 n = PyTuple_GET_SIZE(mro);
1025 for (i = 0; i < n; i++) {
1026 type = (PyTypeObject *) PyTuple_GET_ITEM(mro, i);
1027 assert(PyType_Check(type));
1028 dict = type->tp_defined;
1029 assert(dict && PyDict_Check(dict));
1030 res = PyDict_GetItem(dict, name);
1031 if (res != NULL)
1032 return res;
1033 }
1034 return NULL;
1035}
1036
1037/* This is similar to PyObject_GenericGetAttr(),
1038 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1039static PyObject *
1040type_getattro(PyTypeObject *type, PyObject *name)
1041{
1042 PyTypeObject *metatype = type->ob_type;
1043 PyObject *descr, *res;
1044 descrgetfunc f;
1045
1046 /* Initialize this type (we'll assume the metatype is initialized) */
1047 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001048 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001049 return NULL;
1050 }
1051
1052 /* Get a descriptor from the metatype */
1053 descr = _PyType_Lookup(metatype, name);
1054 f = NULL;
1055 if (descr != NULL) {
1056 f = descr->ob_type->tp_descr_get;
1057 if (f != NULL && PyDescr_IsData(descr))
1058 return f(descr,
1059 (PyObject *)type, (PyObject *)metatype);
1060 }
1061
1062 /* Look in tp_defined of this type and its bases */
1063 res = _PyType_Lookup(type, name);
1064 if (res != NULL) {
1065 f = res->ob_type->tp_descr_get;
1066 if (f != NULL)
1067 return f(res, (PyObject *)NULL, (PyObject *)type);
1068 Py_INCREF(res);
1069 return res;
1070 }
1071
1072 /* Use the descriptor from the metatype */
1073 if (f != NULL) {
1074 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1075 return res;
1076 }
1077 if (descr != NULL) {
1078 Py_INCREF(descr);
1079 return descr;
1080 }
1081
1082 /* Give up */
1083 PyErr_Format(PyExc_AttributeError,
1084 "type object '%.50s' has no attribute '%.400s'",
1085 type->tp_name, PyString_AS_STRING(name));
1086 return NULL;
1087}
1088
1089static int
1090type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1091{
1092 if (type->tp_flags & Py_TPFLAGS_DYNAMICTYPE)
1093 return PyObject_GenericSetAttr((PyObject *)type, name, value);
1094 PyErr_SetString(PyExc_TypeError, "can't set type attributes");
1095 return -1;
1096}
1097
1098static void
1099type_dealloc(PyTypeObject *type)
1100{
1101 etype *et;
1102
1103 /* Assert this is a heap-allocated type object */
1104 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001105 _PyObject_GC_UNTRACK(type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106 et = (etype *)type;
1107 Py_XDECREF(type->tp_base);
1108 Py_XDECREF(type->tp_dict);
1109 Py_XDECREF(type->tp_bases);
1110 Py_XDECREF(type->tp_mro);
1111 Py_XDECREF(type->tp_defined);
1112 /* XXX more? */
1113 Py_XDECREF(et->name);
1114 Py_XDECREF(et->slots);
1115 type->ob_type->tp_free((PyObject *)type);
1116}
1117
1118static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001119 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001120 "mro() -> list\nreturn a type's method resolution order"},
1121 {0}
1122};
1123
1124static char type_doc[] =
1125"type(object) -> the object's type\n"
1126"type(name, bases, dict) -> a new type";
1127
Guido van Rossum048eb752001-10-02 21:24:57 +00001128static int
1129type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1130{
1131 etype *et;
1132 int err;
1133
1134 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1135 return 0;
1136
1137 et = (etype *)type;
1138
1139#define VISIT(SLOT) \
1140 if (SLOT) { \
1141 err = visit((PyObject *)(SLOT), arg); \
1142 if (err) \
1143 return err; \
1144 }
1145
1146 VISIT(type->tp_dict);
1147 VISIT(type->tp_defined);
1148 VISIT(type->tp_mro);
1149 VISIT(type->tp_bases);
1150 VISIT(type->tp_base);
1151 VISIT(et->slots);
1152
1153#undef VISIT
1154
1155 return 0;
1156}
1157
1158static int
1159type_clear(PyTypeObject *type)
1160{
1161 etype *et;
1162 PyObject *tmp;
1163
1164 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1165 return 0;
1166
1167 et = (etype *)type;
1168
1169#define CLEAR(SLOT) \
1170 if (SLOT) { \
1171 tmp = (PyObject *)(SLOT); \
1172 SLOT = NULL; \
1173 Py_DECREF(tmp); \
1174 }
1175
1176 CLEAR(type->tp_dict);
1177 CLEAR(type->tp_defined);
1178 CLEAR(type->tp_mro);
1179 CLEAR(type->tp_bases);
1180 CLEAR(type->tp_base);
1181 CLEAR(et->slots);
1182
Tim Peters2f93e282001-10-04 05:27:00 +00001183 if (type->tp_doc != NULL) {
1184 PyObject_FREE(type->tp_doc);
1185 type->tp_doc = NULL;
1186 }
1187
Guido van Rossum048eb752001-10-02 21:24:57 +00001188#undef CLEAR
1189
1190 return 0;
1191}
1192
1193static int
1194type_is_gc(PyTypeObject *type)
1195{
1196 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1197}
1198
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001199PyTypeObject PyType_Type = {
1200 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001201 0, /* ob_size */
1202 "type", /* tp_name */
1203 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001204 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001205 (destructor)type_dealloc, /* tp_dealloc */
1206 0, /* tp_print */
1207 0, /* tp_getattr */
1208 0, /* tp_setattr */
1209 type_compare, /* tp_compare */
1210 (reprfunc)type_repr, /* tp_repr */
1211 0, /* tp_as_number */
1212 0, /* tp_as_sequence */
1213 0, /* tp_as_mapping */
1214 (hashfunc)_Py_HashPointer, /* tp_hash */
1215 (ternaryfunc)type_call, /* tp_call */
1216 0, /* tp_str */
1217 (getattrofunc)type_getattro, /* tp_getattro */
1218 (setattrofunc)type_setattro, /* tp_setattro */
1219 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001220 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1221 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001222 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001223 (traverseproc)type_traverse, /* tp_traverse */
1224 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001225 0, /* tp_richcompare */
1226 0, /* tp_weaklistoffset */
1227 0, /* tp_iter */
1228 0, /* tp_iternext */
1229 type_methods, /* tp_methods */
1230 type_members, /* tp_members */
1231 type_getsets, /* tp_getset */
1232 0, /* tp_base */
1233 0, /* tp_dict */
1234 0, /* tp_descr_get */
1235 0, /* tp_descr_set */
1236 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1237 0, /* tp_init */
1238 0, /* tp_alloc */
1239 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001240 _PyObject_GC_Del, /* tp_free */
1241 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001242};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001243
1244
1245/* The base type of all types (eventually)... except itself. */
1246
1247static int
1248object_init(PyObject *self, PyObject *args, PyObject *kwds)
1249{
1250 return 0;
1251}
1252
1253static void
1254object_dealloc(PyObject *self)
1255{
1256 self->ob_type->tp_free(self);
1257}
1258
Guido van Rossum8e248182001-08-12 05:17:56 +00001259static PyObject *
1260object_repr(PyObject *self)
1261{
Guido van Rossum76e69632001-08-16 18:52:43 +00001262 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001263 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001264
Guido van Rossum76e69632001-08-16 18:52:43 +00001265 type = self->ob_type;
1266 mod = type_module(type, NULL);
1267 if (mod == NULL)
1268 PyErr_Clear();
1269 else if (!PyString_Check(mod)) {
1270 Py_DECREF(mod);
1271 mod = NULL;
1272 }
1273 name = type_name(type, NULL);
1274 if (name == NULL)
1275 return NULL;
1276 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001277 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001278 PyString_AS_STRING(mod),
1279 PyString_AS_STRING(name),
1280 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001281 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001282 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001283 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001284 Py_XDECREF(mod);
1285 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001286 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001287}
1288
Guido van Rossumb8f63662001-08-15 23:57:02 +00001289static PyObject *
1290object_str(PyObject *self)
1291{
1292 unaryfunc f;
1293
1294 f = self->ob_type->tp_repr;
1295 if (f == NULL)
1296 f = object_repr;
1297 return f(self);
1298}
1299
Guido van Rossum8e248182001-08-12 05:17:56 +00001300static long
1301object_hash(PyObject *self)
1302{
1303 return _Py_HashPointer(self);
1304}
Guido van Rossum8e248182001-08-12 05:17:56 +00001305
Tim Peters6d6c1a32001-08-02 04:15:00 +00001306static void
1307object_free(PyObject *self)
1308{
1309 PyObject_Del(self);
1310}
1311
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001312static PyObject *
1313object_get_class(PyObject *self, void *closure)
1314{
1315 Py_INCREF(self->ob_type);
1316 return (PyObject *)(self->ob_type);
1317}
1318
1319static int
1320equiv_structs(PyTypeObject *a, PyTypeObject *b)
1321{
1322 return a == b ||
1323 (a != NULL &&
1324 b != NULL &&
1325 a->tp_basicsize == b->tp_basicsize &&
1326 a->tp_itemsize == b->tp_itemsize &&
1327 a->tp_dictoffset == b->tp_dictoffset &&
1328 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1329 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1330 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1331}
1332
1333static int
1334same_slots_added(PyTypeObject *a, PyTypeObject *b)
1335{
1336 PyTypeObject *base = a->tp_base;
1337 int size;
1338
1339 if (base != b->tp_base)
1340 return 0;
1341 if (equiv_structs(a, base) && equiv_structs(b, base))
1342 return 1;
1343 size = base->tp_basicsize;
1344 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1345 size += sizeof(PyObject *);
1346 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1347 size += sizeof(PyObject *);
1348 return size == a->tp_basicsize && size == b->tp_basicsize;
1349}
1350
1351static int
1352object_set_class(PyObject *self, PyObject *value, void *closure)
1353{
1354 PyTypeObject *old = self->ob_type;
1355 PyTypeObject *new, *newbase, *oldbase;
1356
1357 if (!PyType_Check(value)) {
1358 PyErr_Format(PyExc_TypeError,
1359 "__class__ must be set to new-style class, not '%s' object",
1360 value->ob_type->tp_name);
1361 return -1;
1362 }
1363 new = (PyTypeObject *)value;
1364 newbase = new;
1365 oldbase = old;
1366 while (equiv_structs(newbase, newbase->tp_base))
1367 newbase = newbase->tp_base;
1368 while (equiv_structs(oldbase, oldbase->tp_base))
1369 oldbase = oldbase->tp_base;
1370 if (newbase != oldbase &&
1371 (newbase->tp_base != oldbase->tp_base ||
1372 !same_slots_added(newbase, oldbase))) {
1373 PyErr_Format(PyExc_TypeError,
1374 "__class__ assignment: "
1375 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001376 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001377 old->tp_name);
1378 return -1;
1379 }
1380 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1381 Py_INCREF(new);
1382 }
1383 self->ob_type = new;
1384 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1385 Py_DECREF(old);
1386 }
1387 return 0;
1388}
1389
1390static PyGetSetDef object_getsets[] = {
1391 {"__class__", object_get_class, object_set_class,
1392 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001393 {0}
1394};
1395
Guido van Rossum3926a632001-09-25 16:25:58 +00001396static PyObject *
1397object_reduce(PyObject *self, PyObject *args)
1398{
1399 /* Call copy_reg._reduce(self) */
1400 static PyObject *copy_reg_str;
1401 PyObject *copy_reg, *res;
1402
1403 if (!copy_reg_str) {
1404 copy_reg_str = PyString_InternFromString("copy_reg");
1405 if (copy_reg_str == NULL)
1406 return NULL;
1407 }
1408 copy_reg = PyImport_Import(copy_reg_str);
1409 if (!copy_reg)
1410 return NULL;
1411 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1412 Py_DECREF(copy_reg);
1413 return res;
1414}
1415
1416static PyMethodDef object_methods[] = {
1417 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1418 {0}
1419};
1420
Tim Peters6d6c1a32001-08-02 04:15:00 +00001421PyTypeObject PyBaseObject_Type = {
1422 PyObject_HEAD_INIT(&PyType_Type)
1423 0, /* ob_size */
1424 "object", /* tp_name */
1425 sizeof(PyObject), /* tp_basicsize */
1426 0, /* tp_itemsize */
1427 (destructor)object_dealloc, /* tp_dealloc */
1428 0, /* tp_print */
1429 0, /* tp_getattr */
1430 0, /* tp_setattr */
1431 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001432 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001433 0, /* tp_as_number */
1434 0, /* tp_as_sequence */
1435 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001436 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001437 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001438 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001439 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001440 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001441 0, /* tp_as_buffer */
1442 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1443 "The most base type", /* tp_doc */
1444 0, /* tp_traverse */
1445 0, /* tp_clear */
1446 0, /* tp_richcompare */
1447 0, /* tp_weaklistoffset */
1448 0, /* tp_iter */
1449 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001450 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001451 0, /* tp_members */
1452 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001453 0, /* tp_base */
1454 0, /* tp_dict */
1455 0, /* tp_descr_get */
1456 0, /* tp_descr_set */
1457 0, /* tp_dictoffset */
1458 object_init, /* tp_init */
1459 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001460 PyType_GenericNew, /* tp_new */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001461 object_free, /* tp_free */
1462};
1463
1464
1465/* Initialize the __dict__ in a type object */
1466
1467static int
1468add_methods(PyTypeObject *type, PyMethodDef *meth)
1469{
1470 PyObject *dict = type->tp_defined;
1471
1472 for (; meth->ml_name != NULL; meth++) {
1473 PyObject *descr;
1474 if (PyDict_GetItemString(dict, meth->ml_name))
1475 continue;
1476 descr = PyDescr_NewMethod(type, meth);
1477 if (descr == NULL)
1478 return -1;
1479 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1480 return -1;
1481 Py_DECREF(descr);
1482 }
1483 return 0;
1484}
1485
1486static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001487add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001488{
1489 PyObject *dict = type->tp_defined;
1490
1491 for (; memb->name != NULL; memb++) {
1492 PyObject *descr;
1493 if (PyDict_GetItemString(dict, memb->name))
1494 continue;
1495 descr = PyDescr_NewMember(type, memb);
1496 if (descr == NULL)
1497 return -1;
1498 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1499 return -1;
1500 Py_DECREF(descr);
1501 }
1502 return 0;
1503}
1504
1505static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001506add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001507{
1508 PyObject *dict = type->tp_defined;
1509
1510 for (; gsp->name != NULL; gsp++) {
1511 PyObject *descr;
1512 if (PyDict_GetItemString(dict, gsp->name))
1513 continue;
1514 descr = PyDescr_NewGetSet(type, gsp);
1515
1516 if (descr == NULL)
1517 return -1;
1518 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1519 return -1;
1520 Py_DECREF(descr);
1521 }
1522 return 0;
1523}
1524
Guido van Rossum13d52f02001-08-10 21:24:08 +00001525static void
1526inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001527{
1528 int oldsize, newsize;
1529
Guido van Rossum13d52f02001-08-10 21:24:08 +00001530 /* Special flag magic */
1531 if (!type->tp_as_buffer && base->tp_as_buffer) {
1532 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1533 type->tp_flags |=
1534 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1535 }
1536 if (!type->tp_as_sequence && base->tp_as_sequence) {
1537 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1538 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1539 }
1540 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1541 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1542 if ((!type->tp_as_number && base->tp_as_number) ||
1543 (!type->tp_as_sequence && base->tp_as_sequence)) {
1544 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1545 if (!type->tp_as_number && !type->tp_as_sequence) {
1546 type->tp_flags |= base->tp_flags &
1547 Py_TPFLAGS_HAVE_INPLACEOPS;
1548 }
1549 }
1550 /* Wow */
1551 }
1552 if (!type->tp_as_number && base->tp_as_number) {
1553 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1554 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1555 }
1556
1557 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001558 oldsize = base->tp_basicsize;
1559 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1560 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1561 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001562 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1563 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001564 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001565 if (type->tp_traverse == NULL)
1566 type->tp_traverse = base->tp_traverse;
1567 if (type->tp_clear == NULL)
1568 type->tp_clear = base->tp_clear;
1569 }
1570 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1571 if (base != &PyBaseObject_Type ||
1572 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1573 if (type->tp_new == NULL)
1574 type->tp_new = base->tp_new;
1575 }
1576 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001577 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001578
1579 /* Copy other non-function slots */
1580
1581#undef COPYVAL
1582#define COPYVAL(SLOT) \
1583 if (type->SLOT == 0) type->SLOT = base->SLOT
1584
1585 COPYVAL(tp_itemsize);
1586 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1587 COPYVAL(tp_weaklistoffset);
1588 }
1589 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1590 COPYVAL(tp_dictoffset);
1591 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001592}
1593
1594static void
1595inherit_slots(PyTypeObject *type, PyTypeObject *base)
1596{
1597 PyTypeObject *basebase;
1598
1599#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001600#undef COPYSLOT
1601#undef COPYNUM
1602#undef COPYSEQ
1603#undef COPYMAP
Guido van Rossum13d52f02001-08-10 21:24:08 +00001604
1605#define SLOTDEFINED(SLOT) \
1606 (base->SLOT != 0 && \
1607 (basebase == NULL || base->SLOT != basebase->SLOT))
1608
Tim Peters6d6c1a32001-08-02 04:15:00 +00001609#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001610 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001611
1612#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1613#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1614#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
1615
Guido van Rossum13d52f02001-08-10 21:24:08 +00001616 /* This won't inherit indirect slots (from tp_as_number etc.)
1617 if type doesn't provide the space. */
1618
1619 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1620 basebase = base->tp_base;
1621 if (basebase->tp_as_number == NULL)
1622 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001623 COPYNUM(nb_add);
1624 COPYNUM(nb_subtract);
1625 COPYNUM(nb_multiply);
1626 COPYNUM(nb_divide);
1627 COPYNUM(nb_remainder);
1628 COPYNUM(nb_divmod);
1629 COPYNUM(nb_power);
1630 COPYNUM(nb_negative);
1631 COPYNUM(nb_positive);
1632 COPYNUM(nb_absolute);
1633 COPYNUM(nb_nonzero);
1634 COPYNUM(nb_invert);
1635 COPYNUM(nb_lshift);
1636 COPYNUM(nb_rshift);
1637 COPYNUM(nb_and);
1638 COPYNUM(nb_xor);
1639 COPYNUM(nb_or);
1640 COPYNUM(nb_coerce);
1641 COPYNUM(nb_int);
1642 COPYNUM(nb_long);
1643 COPYNUM(nb_float);
1644 COPYNUM(nb_oct);
1645 COPYNUM(nb_hex);
1646 COPYNUM(nb_inplace_add);
1647 COPYNUM(nb_inplace_subtract);
1648 COPYNUM(nb_inplace_multiply);
1649 COPYNUM(nb_inplace_divide);
1650 COPYNUM(nb_inplace_remainder);
1651 COPYNUM(nb_inplace_power);
1652 COPYNUM(nb_inplace_lshift);
1653 COPYNUM(nb_inplace_rshift);
1654 COPYNUM(nb_inplace_and);
1655 COPYNUM(nb_inplace_xor);
1656 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001657 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1658 COPYNUM(nb_true_divide);
1659 COPYNUM(nb_floor_divide);
1660 COPYNUM(nb_inplace_true_divide);
1661 COPYNUM(nb_inplace_floor_divide);
1662 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001663 }
1664
Guido van Rossum13d52f02001-08-10 21:24:08 +00001665 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1666 basebase = base->tp_base;
1667 if (basebase->tp_as_sequence == NULL)
1668 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001669 COPYSEQ(sq_length);
1670 COPYSEQ(sq_concat);
1671 COPYSEQ(sq_repeat);
1672 COPYSEQ(sq_item);
1673 COPYSEQ(sq_slice);
1674 COPYSEQ(sq_ass_item);
1675 COPYSEQ(sq_ass_slice);
1676 COPYSEQ(sq_contains);
1677 COPYSEQ(sq_inplace_concat);
1678 COPYSEQ(sq_inplace_repeat);
1679 }
1680
Guido van Rossum13d52f02001-08-10 21:24:08 +00001681 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1682 basebase = base->tp_base;
1683 if (basebase->tp_as_mapping == NULL)
1684 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001685 COPYMAP(mp_length);
1686 COPYMAP(mp_subscript);
1687 COPYMAP(mp_ass_subscript);
1688 }
1689
Guido van Rossum13d52f02001-08-10 21:24:08 +00001690 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001691
Tim Peters6d6c1a32001-08-02 04:15:00 +00001692 COPYSLOT(tp_dealloc);
1693 COPYSLOT(tp_print);
1694 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1695 type->tp_getattr = base->tp_getattr;
1696 type->tp_getattro = base->tp_getattro;
1697 }
1698 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1699 type->tp_setattr = base->tp_setattr;
1700 type->tp_setattro = base->tp_setattro;
1701 }
1702 /* tp_compare see tp_richcompare */
1703 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001704 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001705 COPYSLOT(tp_call);
1706 COPYSLOT(tp_str);
1707 COPYSLOT(tp_as_buffer);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001708 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001709 if (type->tp_compare == NULL &&
1710 type->tp_richcompare == NULL &&
1711 type->tp_hash == NULL)
1712 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001713 type->tp_compare = base->tp_compare;
1714 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001715 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001716 }
1717 }
1718 else {
1719 COPYSLOT(tp_compare);
1720 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001721 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1722 COPYSLOT(tp_iter);
1723 COPYSLOT(tp_iternext);
1724 }
1725 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1726 COPYSLOT(tp_descr_get);
1727 COPYSLOT(tp_descr_set);
1728 COPYSLOT(tp_dictoffset);
1729 COPYSLOT(tp_init);
1730 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001731 COPYSLOT(tp_free);
1732 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001733}
1734
Guido van Rossum13d52f02001-08-10 21:24:08 +00001735staticforward int add_operators(PyTypeObject *);
1736
Tim Peters6d6c1a32001-08-02 04:15:00 +00001737int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001738PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001739{
1740 PyObject *dict, *bases, *x;
1741 PyTypeObject *base;
1742 int i, n;
1743
Guido van Rossumd614f972001-08-10 17:39:49 +00001744 if (type->tp_flags & Py_TPFLAGS_READY) {
1745 assert(type->tp_dict != NULL);
1746 return 0;
1747 }
1748 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
1749 assert(type->tp_dict == NULL);
1750
1751 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001752
1753 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1754 base = type->tp_base;
1755 if (base == NULL && type != &PyBaseObject_Type)
1756 base = type->tp_base = &PyBaseObject_Type;
1757
1758 /* Initialize tp_bases */
1759 bases = type->tp_bases;
1760 if (bases == NULL) {
1761 if (base == NULL)
1762 bases = PyTuple_New(0);
1763 else
1764 bases = Py_BuildValue("(O)", base);
1765 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001766 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001767 type->tp_bases = bases;
1768 }
1769
1770 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001771 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001772 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001773 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001774 }
1775
1776 /* Initialize tp_defined */
1777 dict = type->tp_defined;
1778 if (dict == NULL) {
1779 dict = PyDict_New();
1780 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001781 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001782 type->tp_defined = dict;
1783 }
1784
1785 /* Add type-specific descriptors to tp_defined */
1786 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001787 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001788 if (type->tp_methods != NULL) {
1789 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001790 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001791 }
1792 if (type->tp_members != NULL) {
1793 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001794 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001795 }
1796 if (type->tp_getset != NULL) {
1797 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001798 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001799 }
1800
1801 /* Temporarily make tp_dict the same object as tp_defined.
1802 (This is needed to call mro(), and can stay this way for
1803 dynamic types). */
1804 Py_INCREF(type->tp_defined);
1805 type->tp_dict = type->tp_defined;
1806
1807 /* Calculate method resolution order */
1808 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00001809 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001810 }
1811
Guido van Rossum13d52f02001-08-10 21:24:08 +00001812 /* Inherit special flags from dominant base */
1813 if (type->tp_base != NULL)
1814 inherit_special(type, type->tp_base);
1815
Tim Peters6d6c1a32001-08-02 04:15:00 +00001816 /* Initialize tp_dict properly */
Guido van Rossum8de86802001-08-12 03:43:35 +00001817 if (PyType_HasFeature(type, Py_TPFLAGS_DYNAMICTYPE)) {
Guido van Rossum8e248182001-08-12 05:17:56 +00001818 /* For a dynamic type, all slots are overridden */
1819 override_slots(type, NULL);
Guido van Rossum8de86802001-08-12 03:43:35 +00001820 }
1821 else {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001822 /* For a static type, tp_dict is the consolidation
Guido van Rossum13d52f02001-08-10 21:24:08 +00001823 of the tp_defined of its bases in MRO. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001824 Py_DECREF(type->tp_dict);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001825 type->tp_dict = PyDict_Copy(type->tp_defined);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001826 if (type->tp_dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001827 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828 bases = type->tp_mro;
1829 assert(bases != NULL);
1830 assert(PyTuple_Check(bases));
1831 n = PyTuple_GET_SIZE(bases);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001832 for (i = 1; i < n; i++) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001833 base = (PyTypeObject *)PyTuple_GET_ITEM(bases, i);
1834 assert(PyType_Check(base));
1835 x = base->tp_defined;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001836 if (x != NULL && PyDict_Merge(type->tp_dict, x, 0) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001837 goto error;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001838 inherit_slots(type, base);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001839 }
1840 }
1841
Guido van Rossum13d52f02001-08-10 21:24:08 +00001842 /* Some more special stuff */
1843 base = type->tp_base;
1844 if (base != NULL) {
1845 if (type->tp_as_number == NULL)
1846 type->tp_as_number = base->tp_as_number;
1847 if (type->tp_as_sequence == NULL)
1848 type->tp_as_sequence = base->tp_as_sequence;
1849 if (type->tp_as_mapping == NULL)
1850 type->tp_as_mapping = base->tp_as_mapping;
1851 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001852
Guido van Rossum13d52f02001-08-10 21:24:08 +00001853 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00001854 assert(type->tp_dict != NULL);
1855 type->tp_flags =
1856 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001857 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00001858
1859 error:
1860 type->tp_flags &= ~Py_TPFLAGS_READYING;
1861 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001862}
1863
1864
1865/* Generic wrappers for overloadable 'operators' such as __getitem__ */
1866
1867/* There's a wrapper *function* for each distinct function typedef used
1868 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
1869 wrapper *table* for each distinct operation (e.g. __len__, __add__).
1870 Most tables have only one entry; the tables for binary operators have two
1871 entries, one regular and one with reversed arguments. */
1872
1873static PyObject *
1874wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
1875{
1876 inquiry func = (inquiry)wrapped;
1877 int res;
1878
1879 if (!PyArg_ParseTuple(args, ""))
1880 return NULL;
1881 res = (*func)(self);
1882 if (res == -1 && PyErr_Occurred())
1883 return NULL;
1884 return PyInt_FromLong((long)res);
1885}
1886
1887static struct wrapperbase tab_len[] = {
1888 {"__len__", (wrapperfunc)wrap_inquiry, "x.__len__() <==> len(x)"},
1889 {0}
1890};
1891
1892static PyObject *
1893wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
1894{
1895 binaryfunc func = (binaryfunc)wrapped;
1896 PyObject *other;
1897
1898 if (!PyArg_ParseTuple(args, "O", &other))
1899 return NULL;
1900 return (*func)(self, other);
1901}
1902
1903static PyObject *
1904wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1905{
1906 binaryfunc func = (binaryfunc)wrapped;
1907 PyObject *other;
1908
1909 if (!PyArg_ParseTuple(args, "O", &other))
1910 return NULL;
1911 return (*func)(other, self);
1912}
1913
1914#undef BINARY
1915#define BINARY(NAME, OP) \
1916static struct wrapperbase tab_##NAME[] = { \
1917 {"__" #NAME "__", \
1918 (wrapperfunc)wrap_binaryfunc, \
1919 "x.__" #NAME "__(y) <==> " #OP}, \
1920 {"__r" #NAME "__", \
1921 (wrapperfunc)wrap_binaryfunc_r, \
1922 "y.__r" #NAME "__(x) <==> " #OP}, \
1923 {0} \
1924}
1925
1926BINARY(add, "x+y");
1927BINARY(sub, "x-y");
1928BINARY(mul, "x*y");
1929BINARY(div, "x/y");
1930BINARY(mod, "x%y");
1931BINARY(divmod, "divmod(x,y)");
1932BINARY(lshift, "x<<y");
1933BINARY(rshift, "x>>y");
1934BINARY(and, "x&y");
1935BINARY(xor, "x^y");
1936BINARY(or, "x|y");
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00001937
1938static PyObject *
1939wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
1940{
1941 coercion func = (coercion)wrapped;
1942 PyObject *other, *res;
1943 int ok;
1944
1945 if (!PyArg_ParseTuple(args, "O", &other))
1946 return NULL;
1947 ok = func(&self, &other);
1948 if (ok < 0)
1949 return NULL;
1950 if (ok > 0) {
1951 Py_INCREF(Py_NotImplemented);
1952 return Py_NotImplemented;
1953 }
1954 res = PyTuple_New(2);
1955 if (res == NULL) {
1956 Py_DECREF(self);
1957 Py_DECREF(other);
1958 return NULL;
1959 }
1960 PyTuple_SET_ITEM(res, 0, self);
1961 PyTuple_SET_ITEM(res, 1, other);
1962 return res;
1963}
1964
1965static struct wrapperbase tab_coerce[] = {
1966 {"__coerce__", (wrapperfunc)wrap_coercefunc,
1967 "x.__coerce__(y) <==> coerce(x, y)"},
1968 {0}
1969};
1970
Guido van Rossum874f15a2001-09-25 21:16:33 +00001971BINARY(floordiv, "x//y");
1972BINARY(truediv, "x/y # true division");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973
1974static PyObject *
1975wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
1976{
1977 ternaryfunc func = (ternaryfunc)wrapped;
1978 PyObject *other;
1979 PyObject *third = Py_None;
1980
1981 /* Note: This wrapper only works for __pow__() */
1982
1983 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1984 return NULL;
1985 return (*func)(self, other, third);
1986}
1987
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00001988static PyObject *
1989wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
1990{
1991 ternaryfunc func = (ternaryfunc)wrapped;
1992 PyObject *other;
1993 PyObject *third = Py_None;
1994
1995 /* Note: This wrapper only works for __pow__() */
1996
1997 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
1998 return NULL;
1999 return (*func)(other, self, third);
2000}
2001
Tim Peters6d6c1a32001-08-02 04:15:00 +00002002#undef TERNARY
2003#define TERNARY(NAME, OP) \
2004static struct wrapperbase tab_##NAME[] = { \
2005 {"__" #NAME "__", \
2006 (wrapperfunc)wrap_ternaryfunc, \
2007 "x.__" #NAME "__(y, z) <==> " #OP}, \
2008 {"__r" #NAME "__", \
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002009 (wrapperfunc)wrap_ternaryfunc_r, \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002010 "y.__r" #NAME "__(x, z) <==> " #OP}, \
2011 {0} \
2012}
2013
2014TERNARY(pow, "(x**y) % z");
2015
2016#undef UNARY
2017#define UNARY(NAME, OP) \
2018static struct wrapperbase tab_##NAME[] = { \
2019 {"__" #NAME "__", \
2020 (wrapperfunc)wrap_unaryfunc, \
2021 "x.__" #NAME "__() <==> " #OP}, \
2022 {0} \
2023}
2024
2025static PyObject *
2026wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2027{
2028 unaryfunc func = (unaryfunc)wrapped;
2029
2030 if (!PyArg_ParseTuple(args, ""))
2031 return NULL;
2032 return (*func)(self);
2033}
2034
2035UNARY(neg, "-x");
2036UNARY(pos, "+x");
2037UNARY(abs, "abs(x)");
2038UNARY(nonzero, "x != 0");
2039UNARY(invert, "~x");
2040UNARY(int, "int(x)");
2041UNARY(long, "long(x)");
2042UNARY(float, "float(x)");
2043UNARY(oct, "oct(x)");
2044UNARY(hex, "hex(x)");
2045
2046#undef IBINARY
2047#define IBINARY(NAME, OP) \
2048static struct wrapperbase tab_##NAME[] = { \
2049 {"__" #NAME "__", \
2050 (wrapperfunc)wrap_binaryfunc, \
2051 "x.__" #NAME "__(y) <==> " #OP}, \
2052 {0} \
2053}
2054
2055IBINARY(iadd, "x+=y");
2056IBINARY(isub, "x-=y");
2057IBINARY(imul, "x*=y");
2058IBINARY(idiv, "x/=y");
2059IBINARY(imod, "x%=y");
2060IBINARY(ilshift, "x<<=y");
2061IBINARY(irshift, "x>>=y");
2062IBINARY(iand, "x&=y");
2063IBINARY(ixor, "x^=y");
2064IBINARY(ior, "x|=y");
Guido van Rossum874f15a2001-09-25 21:16:33 +00002065IBINARY(ifloordiv, "x//=y");
2066IBINARY(itruediv, "x/=y # true division");
Tim Peters6d6c1a32001-08-02 04:15:00 +00002067
2068#undef ITERNARY
2069#define ITERNARY(NAME, OP) \
2070static struct wrapperbase tab_##NAME[] = { \
2071 {"__" #NAME "__", \
2072 (wrapperfunc)wrap_ternaryfunc, \
2073 "x.__" #NAME "__(y) <==> " #OP}, \
2074 {0} \
2075}
2076
2077ITERNARY(ipow, "x = (x**y) % z");
2078
2079static struct wrapperbase tab_getitem[] = {
2080 {"__getitem__", (wrapperfunc)wrap_binaryfunc,
2081 "x.__getitem__(y) <==> x[y]"},
2082 {0}
2083};
2084
2085static PyObject *
2086wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2087{
2088 intargfunc func = (intargfunc)wrapped;
2089 int i;
2090
2091 if (!PyArg_ParseTuple(args, "i", &i))
2092 return NULL;
2093 return (*func)(self, i);
2094}
2095
2096static struct wrapperbase tab_mul_int[] = {
2097 {"__mul__", (wrapperfunc)wrap_intargfunc, "x.__mul__(n) <==> x*n"},
2098 {"__rmul__", (wrapperfunc)wrap_intargfunc, "x.__rmul__(n) <==> n*x"},
2099 {0}
2100};
2101
2102static struct wrapperbase tab_concat[] = {
2103 {"__add__", (wrapperfunc)wrap_binaryfunc, "x.__add__(y) <==> x+y"},
2104 {0}
2105};
2106
2107static struct wrapperbase tab_imul_int[] = {
2108 {"__imul__", (wrapperfunc)wrap_intargfunc, "x.__imul__(n) <==> x*=n"},
2109 {0}
2110};
2111
Guido van Rossum5d815f32001-08-17 21:57:47 +00002112static int
2113getindex(PyObject *self, PyObject *arg)
2114{
2115 int i;
2116
2117 i = PyInt_AsLong(arg);
2118 if (i == -1 && PyErr_Occurred())
2119 return -1;
2120 if (i < 0) {
2121 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2122 if (sq && sq->sq_length) {
2123 int n = (*sq->sq_length)(self);
2124 if (n < 0)
2125 return -1;
2126 i += n;
2127 }
2128 }
2129 return i;
2130}
2131
2132static PyObject *
2133wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2134{
2135 intargfunc func = (intargfunc)wrapped;
2136 PyObject *arg;
2137 int i;
2138
Guido van Rossumf4593e02001-10-03 12:09:30 +00002139 if (PyTuple_GET_SIZE(args) == 1) {
2140 arg = PyTuple_GET_ITEM(args, 0);
2141 i = getindex(self, arg);
2142 if (i == -1 && PyErr_Occurred())
2143 return NULL;
2144 return (*func)(self, i);
2145 }
2146 PyArg_ParseTuple(args, "O", &arg);
2147 assert(PyErr_Occurred());
2148 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002149}
2150
Tim Peters6d6c1a32001-08-02 04:15:00 +00002151static struct wrapperbase tab_getitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00002152 {"__getitem__", (wrapperfunc)wrap_sq_item,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002153 "x.__getitem__(i) <==> x[i]"},
2154 {0}
2155};
2156
2157static PyObject *
2158wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2159{
2160 intintargfunc func = (intintargfunc)wrapped;
2161 int i, j;
2162
2163 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2164 return NULL;
2165 return (*func)(self, i, j);
2166}
2167
2168static struct wrapperbase tab_getslice[] = {
2169 {"__getslice__", (wrapperfunc)wrap_intintargfunc,
2170 "x.__getslice__(i, j) <==> x[i:j]"},
2171 {0}
2172};
2173
2174static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002175wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176{
2177 intobjargproc func = (intobjargproc)wrapped;
2178 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002179 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002180
Guido van Rossum5d815f32001-08-17 21:57:47 +00002181 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2182 return NULL;
2183 i = getindex(self, arg);
2184 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002185 return NULL;
2186 res = (*func)(self, i, value);
2187 if (res == -1 && PyErr_Occurred())
2188 return NULL;
2189 Py_INCREF(Py_None);
2190 return Py_None;
2191}
2192
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002193static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002194wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002195{
2196 intobjargproc func = (intobjargproc)wrapped;
2197 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002198 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002199
Guido van Rossum5d815f32001-08-17 21:57:47 +00002200 if (!PyArg_ParseTuple(args, "O", &arg))
2201 return NULL;
2202 i = getindex(self, arg);
2203 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002204 return NULL;
2205 res = (*func)(self, i, NULL);
2206 if (res == -1 && PyErr_Occurred())
2207 return NULL;
2208 Py_INCREF(Py_None);
2209 return Py_None;
2210}
2211
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212static struct wrapperbase tab_setitem_int[] = {
Guido van Rossum5d815f32001-08-17 21:57:47 +00002213 {"__setitem__", (wrapperfunc)wrap_sq_setitem,
Tim Peters6d6c1a32001-08-02 04:15:00 +00002214 "x.__setitem__(i, y) <==> x[i]=y"},
Guido van Rossum5d815f32001-08-17 21:57:47 +00002215 {"__delitem__", (wrapperfunc)wrap_sq_delitem,
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002216 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002217 {0}
2218};
2219
2220static PyObject *
2221wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2222{
2223 intintobjargproc func = (intintobjargproc)wrapped;
2224 int i, j, res;
2225 PyObject *value;
2226
2227 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2228 return NULL;
2229 res = (*func)(self, i, j, value);
2230 if (res == -1 && PyErr_Occurred())
2231 return NULL;
2232 Py_INCREF(Py_None);
2233 return Py_None;
2234}
2235
2236static struct wrapperbase tab_setslice[] = {
2237 {"__setslice__", (wrapperfunc)wrap_intintobjargproc,
2238 "x.__setslice__(i, j, y) <==> x[i:j]=y"},
2239 {0}
2240};
2241
2242/* XXX objobjproc is a misnomer; should be objargpred */
2243static PyObject *
2244wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2245{
2246 objobjproc func = (objobjproc)wrapped;
2247 int res;
2248 PyObject *value;
2249
2250 if (!PyArg_ParseTuple(args, "O", &value))
2251 return NULL;
2252 res = (*func)(self, value);
2253 if (res == -1 && PyErr_Occurred())
2254 return NULL;
2255 return PyInt_FromLong((long)res);
2256}
2257
2258static struct wrapperbase tab_contains[] = {
2259 {"__contains__", (wrapperfunc)wrap_objobjproc,
2260 "x.__contains__(y) <==> y in x"},
2261 {0}
2262};
2263
2264static PyObject *
2265wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2266{
2267 objobjargproc func = (objobjargproc)wrapped;
2268 int res;
2269 PyObject *key, *value;
2270
2271 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2272 return NULL;
2273 res = (*func)(self, key, value);
2274 if (res == -1 && PyErr_Occurred())
2275 return NULL;
2276 Py_INCREF(Py_None);
2277 return Py_None;
2278}
2279
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002280static PyObject *
2281wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2282{
2283 objobjargproc func = (objobjargproc)wrapped;
2284 int res;
2285 PyObject *key;
2286
2287 if (!PyArg_ParseTuple(args, "O", &key))
2288 return NULL;
2289 res = (*func)(self, key, NULL);
2290 if (res == -1 && PyErr_Occurred())
2291 return NULL;
2292 Py_INCREF(Py_None);
2293 return Py_None;
2294}
2295
Tim Peters6d6c1a32001-08-02 04:15:00 +00002296static struct wrapperbase tab_setitem[] = {
2297 {"__setitem__", (wrapperfunc)wrap_objobjargproc,
2298 "x.__setitem__(y, z) <==> x[y]=z"},
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002299 {"__delitem__", (wrapperfunc)wrap_delitem,
2300 "x.__delitem__(y) <==> del x[y]"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002301 {0}
2302};
2303
2304static PyObject *
2305wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2306{
2307 cmpfunc func = (cmpfunc)wrapped;
2308 int res;
2309 PyObject *other;
2310
2311 if (!PyArg_ParseTuple(args, "O", &other))
2312 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002313 if (other->ob_type->tp_compare != func &&
2314 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002315 PyErr_Format(
2316 PyExc_TypeError,
2317 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2318 self->ob_type->tp_name,
2319 self->ob_type->tp_name,
2320 other->ob_type->tp_name);
2321 return NULL;
2322 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002323 res = (*func)(self, other);
2324 if (PyErr_Occurred())
2325 return NULL;
2326 return PyInt_FromLong((long)res);
2327}
2328
2329static struct wrapperbase tab_cmp[] = {
2330 {"__cmp__", (wrapperfunc)wrap_cmpfunc,
2331 "x.__cmp__(y) <==> cmp(x,y)"},
2332 {0}
2333};
2334
2335static struct wrapperbase tab_repr[] = {
2336 {"__repr__", (wrapperfunc)wrap_unaryfunc,
2337 "x.__repr__() <==> repr(x)"},
2338 {0}
2339};
2340
2341static struct wrapperbase tab_getattr[] = {
Guido van Rossum867a8d22001-09-21 19:29:08 +00002342 {"__getattribute__", (wrapperfunc)wrap_binaryfunc,
2343 "x.__getattribute__('name') <==> x.name"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002344 {0}
2345};
2346
2347static PyObject *
2348wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2349{
2350 setattrofunc func = (setattrofunc)wrapped;
2351 int res;
2352 PyObject *name, *value;
2353
2354 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2355 return NULL;
2356 res = (*func)(self, name, value);
2357 if (res < 0)
2358 return NULL;
2359 Py_INCREF(Py_None);
2360 return Py_None;
2361}
2362
2363static PyObject *
2364wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2365{
2366 setattrofunc func = (setattrofunc)wrapped;
2367 int res;
2368 PyObject *name;
2369
2370 if (!PyArg_ParseTuple(args, "O", &name))
2371 return NULL;
2372 res = (*func)(self, name, NULL);
2373 if (res < 0)
2374 return NULL;
2375 Py_INCREF(Py_None);
2376 return Py_None;
2377}
2378
2379static struct wrapperbase tab_setattr[] = {
2380 {"__setattr__", (wrapperfunc)wrap_setattr,
2381 "x.__setattr__('name', value) <==> x.name = value"},
2382 {"__delattr__", (wrapperfunc)wrap_delattr,
2383 "x.__delattr__('name') <==> del x.name"},
2384 {0}
2385};
2386
2387static PyObject *
2388wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2389{
2390 hashfunc func = (hashfunc)wrapped;
2391 long res;
2392
2393 if (!PyArg_ParseTuple(args, ""))
2394 return NULL;
2395 res = (*func)(self);
2396 if (res == -1 && PyErr_Occurred())
2397 return NULL;
2398 return PyInt_FromLong(res);
2399}
2400
2401static struct wrapperbase tab_hash[] = {
2402 {"__hash__", (wrapperfunc)wrap_hashfunc,
2403 "x.__hash__() <==> hash(x)"},
2404 {0}
2405};
2406
2407static PyObject *
2408wrap_call(PyObject *self, PyObject *args, void *wrapped)
2409{
2410 ternaryfunc func = (ternaryfunc)wrapped;
2411
2412 /* XXX What about keyword arguments? */
2413 return (*func)(self, args, NULL);
2414}
2415
2416static struct wrapperbase tab_call[] = {
2417 {"__call__", (wrapperfunc)wrap_call,
2418 "x.__call__(...) <==> x(...)"},
2419 {0}
2420};
2421
2422static struct wrapperbase tab_str[] = {
2423 {"__str__", (wrapperfunc)wrap_unaryfunc,
2424 "x.__str__() <==> str(x)"},
2425 {0}
2426};
2427
2428static PyObject *
2429wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2430{
2431 richcmpfunc func = (richcmpfunc)wrapped;
2432 PyObject *other;
2433
2434 if (!PyArg_ParseTuple(args, "O", &other))
2435 return NULL;
2436 return (*func)(self, other, op);
2437}
2438
2439#undef RICHCMP_WRAPPER
2440#define RICHCMP_WRAPPER(NAME, OP) \
2441static PyObject * \
2442richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2443{ \
2444 return wrap_richcmpfunc(self, args, wrapped, OP); \
2445}
2446
Jack Jansen8e938b42001-08-08 15:29:49 +00002447RICHCMP_WRAPPER(lt, Py_LT)
2448RICHCMP_WRAPPER(le, Py_LE)
2449RICHCMP_WRAPPER(eq, Py_EQ)
2450RICHCMP_WRAPPER(ne, Py_NE)
2451RICHCMP_WRAPPER(gt, Py_GT)
2452RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453
2454#undef RICHCMP_ENTRY
2455#define RICHCMP_ENTRY(NAME, EXPR) \
2456 {"__" #NAME "__", (wrapperfunc)richcmp_##NAME, \
2457 "x.__" #NAME "__(y) <==> " EXPR}
2458
2459static struct wrapperbase tab_richcmp[] = {
2460 RICHCMP_ENTRY(lt, "x<y"),
2461 RICHCMP_ENTRY(le, "x<=y"),
2462 RICHCMP_ENTRY(eq, "x==y"),
2463 RICHCMP_ENTRY(ne, "x!=y"),
2464 RICHCMP_ENTRY(gt, "x>y"),
2465 RICHCMP_ENTRY(ge, "x>=y"),
2466 {0}
2467};
2468
2469static struct wrapperbase tab_iter[] = {
2470 {"__iter__", (wrapperfunc)wrap_unaryfunc, "x.__iter__() <==> iter(x)"},
2471 {0}
2472};
2473
2474static PyObject *
2475wrap_next(PyObject *self, PyObject *args, void *wrapped)
2476{
2477 unaryfunc func = (unaryfunc)wrapped;
2478 PyObject *res;
2479
2480 if (!PyArg_ParseTuple(args, ""))
2481 return NULL;
2482 res = (*func)(self);
2483 if (res == NULL && !PyErr_Occurred())
2484 PyErr_SetNone(PyExc_StopIteration);
2485 return res;
2486}
2487
2488static struct wrapperbase tab_next[] = {
2489 {"next", (wrapperfunc)wrap_next,
2490 "x.next() -> the next value, or raise StopIteration"},
2491 {0}
2492};
2493
2494static PyObject *
2495wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2496{
2497 descrgetfunc func = (descrgetfunc)wrapped;
2498 PyObject *obj;
2499 PyObject *type = NULL;
2500
2501 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2502 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002503 return (*func)(self, obj, type);
2504}
2505
2506static struct wrapperbase tab_descr_get[] = {
2507 {"__get__", (wrapperfunc)wrap_descr_get,
2508 "descr.__get__(obj, type) -> value"},
2509 {0}
2510};
2511
2512static PyObject *
2513wrap_descrsetfunc(PyObject *self, PyObject *args, void *wrapped)
2514{
2515 descrsetfunc func = (descrsetfunc)wrapped;
2516 PyObject *obj, *value;
2517 int ret;
2518
2519 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2520 return NULL;
2521 ret = (*func)(self, obj, value);
2522 if (ret < 0)
2523 return NULL;
2524 Py_INCREF(Py_None);
2525 return Py_None;
2526}
2527
2528static struct wrapperbase tab_descr_set[] = {
2529 {"__set__", (wrapperfunc)wrap_descrsetfunc,
2530 "descr.__set__(obj, value)"},
2531 {0}
2532};
2533
2534static PyObject *
2535wrap_init(PyObject *self, PyObject *args, void *wrapped)
2536{
2537 initproc func = (initproc)wrapped;
2538
2539 /* XXX What about keyword arguments? */
2540 if (func(self, args, NULL) < 0)
2541 return NULL;
2542 Py_INCREF(Py_None);
2543 return Py_None;
2544}
2545
2546static struct wrapperbase tab_init[] = {
2547 {"__init__", (wrapperfunc)wrap_init,
2548 "x.__init__(...) initializes x; "
Guido van Rossumd016e452001-10-01 13:17:24 +00002549 "see x.__class__.__doc__ for signature"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002550 {0}
2551};
2552
2553static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002554tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002555{
Barry Warsaw60f01882001-08-22 19:24:42 +00002556 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002557 PyObject *arg0, *res;
2558
2559 if (self == NULL || !PyType_Check(self))
2560 Py_FatalError("__new__() called with non-type 'self'");
2561 type = (PyTypeObject *)self;
2562 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002563 PyErr_Format(PyExc_TypeError,
2564 "%s.__new__(): not enough arguments",
2565 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002566 return NULL;
2567 }
2568 arg0 = PyTuple_GET_ITEM(args, 0);
2569 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002570 PyErr_Format(PyExc_TypeError,
2571 "%s.__new__(X): X is not a type object (%s)",
2572 type->tp_name,
2573 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002574 return NULL;
2575 }
2576 subtype = (PyTypeObject *)arg0;
2577 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002578 PyErr_Format(PyExc_TypeError,
2579 "%s.__new__(%s): %s is not a subtype of %s",
2580 type->tp_name,
2581 subtype->tp_name,
2582 subtype->tp_name,
2583 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002584 return NULL;
2585 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002586
2587 /* Check that the use doesn't do something silly and unsafe like
2588 object.__new__(dictionary). To do this, we check that the
2589 most derived base that's not a heap type is this type. */
2590 staticbase = subtype;
2591 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2592 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002593 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002594 PyErr_Format(PyExc_TypeError,
2595 "%s.__new__(%s) is not safe, use %s.__new__()",
2596 type->tp_name,
2597 subtype->tp_name,
2598 staticbase == NULL ? "?" : staticbase->tp_name);
2599 return NULL;
2600 }
2601
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002602 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2603 if (args == NULL)
2604 return NULL;
2605 res = type->tp_new(subtype, args, kwds);
2606 Py_DECREF(args);
2607 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002608}
2609
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002610static struct PyMethodDef tp_new_methoddef[] = {
2611 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2612 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002613 {0}
2614};
2615
2616static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002617add_tp_new_wrapper(PyTypeObject *type)
2618{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002619 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002620
Guido van Rossumf040ede2001-08-07 16:40:56 +00002621 if (PyDict_GetItemString(type->tp_defined, "__new__") != NULL)
2622 return 0;
2623 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002624 if (func == NULL)
2625 return -1;
2626 return PyDict_SetItemString(type->tp_defined, "__new__", func);
2627}
2628
Guido van Rossum13d52f02001-08-10 21:24:08 +00002629static int
2630add_wrappers(PyTypeObject *type, struct wrapperbase *wraps, void *wrapped)
2631{
2632 PyObject *dict = type->tp_defined;
2633
2634 for (; wraps->name != NULL; wraps++) {
2635 PyObject *descr;
2636 if (PyDict_GetItemString(dict, wraps->name))
2637 continue;
2638 descr = PyDescr_NewWrapper(type, wraps, wrapped);
2639 if (descr == NULL)
2640 return -1;
2641 if (PyDict_SetItemString(dict, wraps->name, descr) < 0)
2642 return -1;
2643 Py_DECREF(descr);
2644 }
2645 return 0;
2646}
2647
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002648/* This function is called by PyType_Ready() to populate the type's
Guido van Rossumf040ede2001-08-07 16:40:56 +00002649 dictionary with method descriptors for function slots. For each
2650 function slot (like tp_repr) that's defined in the type, one or
2651 more corresponding descriptors are added in the type's tp_defined
2652 dictionary under the appropriate name (like __repr__). Some
2653 function slots cause more than one descriptor to be added (for
2654 example, the nb_add slot adds both __add__ and __radd__
2655 descriptors) and some function slots compete for the same
2656 descriptor (for example both sq_item and mp_subscript generate a
2657 __getitem__ descriptor). This only adds new descriptors and
2658 doesn't overwrite entries in tp_defined that were previously
2659 defined. The descriptors contain a reference to the C function
2660 they must call, so that it's safe if they are copied into a
2661 subtype's __dict__ and the subtype has a different C function in
2662 its slot -- calling the method defined by the descriptor will call
2663 the C function that was used to create it, rather than the C
2664 function present in the slot when it is called. (This is important
2665 because a subtype may have a C function in the slot that calls the
2666 method from the dictionary, and we want to avoid infinite recursion
2667 here.) */
2668
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002669static int
Tim Peters6d6c1a32001-08-02 04:15:00 +00002670add_operators(PyTypeObject *type)
2671{
2672 PySequenceMethods *sq;
2673 PyMappingMethods *mp;
2674 PyNumberMethods *nb;
2675
2676#undef ADD
2677#define ADD(SLOT, TABLE) \
2678 if (SLOT) { \
2679 if (add_wrappers(type, TABLE, (void *)(SLOT)) < 0) \
2680 return -1; \
2681 }
2682
2683 if ((sq = type->tp_as_sequence) != NULL) {
2684 ADD(sq->sq_length, tab_len);
2685 ADD(sq->sq_concat, tab_concat);
2686 ADD(sq->sq_repeat, tab_mul_int);
2687 ADD(sq->sq_item, tab_getitem_int);
2688 ADD(sq->sq_slice, tab_getslice);
2689 ADD(sq->sq_ass_item, tab_setitem_int);
2690 ADD(sq->sq_ass_slice, tab_setslice);
2691 ADD(sq->sq_contains, tab_contains);
2692 ADD(sq->sq_inplace_concat, tab_iadd);
2693 ADD(sq->sq_inplace_repeat, tab_imul_int);
2694 }
2695
2696 if ((mp = type->tp_as_mapping) != NULL) {
2697 if (sq->sq_length == NULL)
2698 ADD(mp->mp_length, tab_len);
2699 ADD(mp->mp_subscript, tab_getitem);
2700 ADD(mp->mp_ass_subscript, tab_setitem);
2701 }
2702
2703 /* We don't support "old-style numbers" because their binary
2704 operators require that both arguments have the same type;
2705 the wrappers here only work for new-style numbers. */
2706 if ((type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
2707 (nb = type->tp_as_number) != NULL) {
2708 ADD(nb->nb_add, tab_add);
2709 ADD(nb->nb_subtract, tab_sub);
2710 ADD(nb->nb_multiply, tab_mul);
2711 ADD(nb->nb_divide, tab_div);
2712 ADD(nb->nb_remainder, tab_mod);
2713 ADD(nb->nb_divmod, tab_divmod);
2714 ADD(nb->nb_power, tab_pow);
2715 ADD(nb->nb_negative, tab_neg);
2716 ADD(nb->nb_positive, tab_pos);
2717 ADD(nb->nb_absolute, tab_abs);
2718 ADD(nb->nb_nonzero, tab_nonzero);
2719 ADD(nb->nb_invert, tab_invert);
2720 ADD(nb->nb_lshift, tab_lshift);
2721 ADD(nb->nb_rshift, tab_rshift);
2722 ADD(nb->nb_and, tab_and);
2723 ADD(nb->nb_xor, tab_xor);
2724 ADD(nb->nb_or, tab_or);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002725 ADD(nb->nb_coerce, tab_coerce);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002726 ADD(nb->nb_int, tab_int);
2727 ADD(nb->nb_long, tab_long);
2728 ADD(nb->nb_float, tab_float);
2729 ADD(nb->nb_oct, tab_oct);
2730 ADD(nb->nb_hex, tab_hex);
2731 ADD(nb->nb_inplace_add, tab_iadd);
2732 ADD(nb->nb_inplace_subtract, tab_isub);
2733 ADD(nb->nb_inplace_multiply, tab_imul);
2734 ADD(nb->nb_inplace_divide, tab_idiv);
2735 ADD(nb->nb_inplace_remainder, tab_imod);
2736 ADD(nb->nb_inplace_power, tab_ipow);
2737 ADD(nb->nb_inplace_lshift, tab_ilshift);
2738 ADD(nb->nb_inplace_rshift, tab_irshift);
2739 ADD(nb->nb_inplace_and, tab_iand);
2740 ADD(nb->nb_inplace_xor, tab_ixor);
2741 ADD(nb->nb_inplace_or, tab_ior);
Guido van Rossum874f15a2001-09-25 21:16:33 +00002742 if (type->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2743 ADD(nb->nb_floor_divide, tab_floordiv);
2744 ADD(nb->nb_true_divide, tab_truediv);
2745 ADD(nb->nb_inplace_floor_divide, tab_ifloordiv);
2746 ADD(nb->nb_inplace_true_divide, tab_itruediv);
2747 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002748 }
2749
2750 ADD(type->tp_getattro, tab_getattr);
2751 ADD(type->tp_setattro, tab_setattr);
2752 ADD(type->tp_compare, tab_cmp);
2753 ADD(type->tp_repr, tab_repr);
2754 ADD(type->tp_hash, tab_hash);
2755 ADD(type->tp_call, tab_call);
2756 ADD(type->tp_str, tab_str);
2757 ADD(type->tp_richcompare, tab_richcmp);
2758 ADD(type->tp_iter, tab_iter);
2759 ADD(type->tp_iternext, tab_next);
2760 ADD(type->tp_descr_get, tab_descr_get);
2761 ADD(type->tp_descr_set, tab_descr_set);
2762 ADD(type->tp_init, tab_init);
2763
Guido van Rossumf040ede2001-08-07 16:40:56 +00002764 if (type->tp_new != NULL) {
2765 if (add_tp_new_wrapper(type) < 0)
2766 return -1;
2767 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002768
2769 return 0;
2770}
2771
Guido van Rossumf040ede2001-08-07 16:40:56 +00002772/* Slot wrappers that call the corresponding __foo__ slot. See comments
2773 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002774
Guido van Rossumdc91b992001-08-08 22:26:22 +00002775#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002776static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002777FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002778{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002779 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002780 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781}
2782
Guido van Rossumdc91b992001-08-08 22:26:22 +00002783#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002784static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002785FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002786{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002787 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002788 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002789}
2790
Guido van Rossumdc91b992001-08-08 22:26:22 +00002791
2792#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002793static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002794FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002795{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002796 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002797 int do_other = self->ob_type != other->ob_type && \
2798 other->ob_type->tp_as_number != NULL && \
2799 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002800 if (self->ob_type->tp_as_number != NULL && \
2801 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2802 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002803 if (do_other && \
2804 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2805 r = call_maybe( \
2806 other, ROPSTR, &rcache_str, "(O)", self); \
2807 if (r != Py_NotImplemented) \
2808 return r; \
2809 Py_DECREF(r); \
2810 do_other = 0; \
2811 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002812 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002813 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002814 if (r != Py_NotImplemented || \
2815 other->ob_type == self->ob_type) \
2816 return r; \
2817 Py_DECREF(r); \
2818 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002819 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002820 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002821 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002822 } \
2823 Py_INCREF(Py_NotImplemented); \
2824 return Py_NotImplemented; \
2825}
2826
2827#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2828 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2829
2830#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2831static PyObject * \
2832FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2833{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002834 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002835 return call_method(self, OPSTR, &cache_str, \
2836 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002837}
2838
2839static int
2840slot_sq_length(PyObject *self)
2841{
Guido van Rossum2730b132001-08-28 18:22:14 +00002842 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002843 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002844 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002845
2846 if (res == NULL)
2847 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002848 len = (int)PyInt_AsLong(res);
2849 Py_DECREF(res);
2850 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002851}
2852
Guido van Rossumdc91b992001-08-08 22:26:22 +00002853SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2854SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002855
2856/* Super-optimized version of slot_sq_item.
2857 Other slots could do the same... */
2858static PyObject *
2859slot_sq_item(PyObject *self, int i)
2860{
2861 static PyObject *getitem_str;
2862 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2863 descrgetfunc f;
2864
2865 if (getitem_str == NULL) {
2866 getitem_str = PyString_InternFromString("__getitem__");
2867 if (getitem_str == NULL)
2868 return NULL;
2869 }
2870 func = _PyType_Lookup(self->ob_type, getitem_str);
2871 if (func != NULL) {
2872 if (func->ob_type == &PyWrapperDescr_Type) {
2873 PyWrapperDescrObject *wrapper =
2874 (PyWrapperDescrObject *)func;
2875 if (wrapper->d_base->wrapper == wrap_sq_item) {
2876 intargfunc f;
2877 f = (intargfunc)(wrapper->d_wrapped);
2878 return f(self, i);
2879 }
2880 }
2881 if ((f = func->ob_type->tp_descr_get) == NULL)
2882 Py_INCREF(func);
2883 else
2884 func = f(func, self, (PyObject *)(self->ob_type));
2885 ival = PyInt_FromLong(i);
2886 if (ival != NULL) {
2887 args = PyTuple_New(1);
2888 if (args != NULL) {
2889 PyTuple_SET_ITEM(args, 0, ival);
2890 retval = PyObject_Call(func, args, NULL);
2891 Py_XDECREF(args);
2892 Py_XDECREF(func);
2893 return retval;
2894 }
2895 }
2896 }
2897 else {
2898 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2899 }
2900 Py_XDECREF(args);
2901 Py_XDECREF(ival);
2902 Py_XDECREF(func);
2903 return NULL;
2904}
2905
Guido van Rossumdc91b992001-08-08 22:26:22 +00002906SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002907
2908static int
2909slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2910{
2911 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002912 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002913
2914 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002915 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002916 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002917 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002918 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002919 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002920 if (res == NULL)
2921 return -1;
2922 Py_DECREF(res);
2923 return 0;
2924}
2925
2926static int
2927slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2928{
2929 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002930 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002931
2932 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002933 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002934 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002935 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002936 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002937 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938 if (res == NULL)
2939 return -1;
2940 Py_DECREF(res);
2941 return 0;
2942}
2943
2944static int
2945slot_sq_contains(PyObject *self, PyObject *value)
2946{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002947 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002948 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002949
Guido van Rossum55f20992001-10-01 17:18:22 +00002950 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002951
2952 if (func != NULL) {
2953 args = Py_BuildValue("(O)", value);
2954 if (args == NULL)
2955 res = NULL;
2956 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002957 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002958 Py_DECREF(args);
2959 }
2960 Py_DECREF(func);
2961 if (res == NULL)
2962 return -1;
2963 return PyObject_IsTrue(res);
2964 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002965 else if (PyErr_Occurred())
2966 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002967 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002968 return _PySequence_IterSearch(self, value,
2969 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002970 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002971}
2972
Guido van Rossumdc91b992001-08-08 22:26:22 +00002973SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2974SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002975
2976#define slot_mp_length slot_sq_length
2977
Guido van Rossumdc91b992001-08-08 22:26:22 +00002978SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002979
2980static int
2981slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2982{
2983 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002984 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002985
2986 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002987 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002988 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002989 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002990 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002991 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002992 if (res == NULL)
2993 return -1;
2994 Py_DECREF(res);
2995 return 0;
2996}
2997
Guido van Rossumdc91b992001-08-08 22:26:22 +00002998SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2999SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
3000SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
3001SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
3002SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
3003SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
3004
3005staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
3006
3007SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
3008 nb_power, "__pow__", "__rpow__")
3009
3010static PyObject *
3011slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3012{
Guido van Rossum2730b132001-08-28 18:22:14 +00003013 static PyObject *pow_str;
3014
Guido van Rossumdc91b992001-08-08 22:26:22 +00003015 if (modulus == Py_None)
3016 return slot_nb_power_binary(self, other);
3017 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00003018 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003019 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003020}
3021
3022SLOT0(slot_nb_negative, "__neg__")
3023SLOT0(slot_nb_positive, "__pos__")
3024SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003025
3026static int
3027slot_nb_nonzero(PyObject *self)
3028{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003029 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003030 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003031
Guido van Rossum55f20992001-10-01 17:18:22 +00003032 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003033 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003034 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003035 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003036 func = lookup_maybe(self, "__len__", &len_str);
3037 if (func == NULL) {
3038 if (PyErr_Occurred())
3039 return -1;
3040 else
3041 return 1;
3042 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003043 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003044 res = PyObject_CallObject(func, NULL);
3045 Py_DECREF(func);
3046 if (res == NULL)
3047 return -1;
3048 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003049}
3050
Guido van Rossumdc91b992001-08-08 22:26:22 +00003051SLOT0(slot_nb_invert, "__invert__")
3052SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3053SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3054SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3055SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3056SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003057
3058static int
3059slot_nb_coerce(PyObject **a, PyObject **b)
3060{
3061 static PyObject *coerce_str;
3062 PyObject *self = *a, *other = *b;
3063
3064 if (self->ob_type->tp_as_number != NULL &&
3065 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3066 PyObject *r;
3067 r = call_maybe(
3068 self, "__coerce__", &coerce_str, "(O)", other);
3069 if (r == NULL)
3070 return -1;
3071 if (r == Py_NotImplemented) {
3072 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003073 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003074 else {
3075 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3076 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003077 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003078 Py_DECREF(r);
3079 return -1;
3080 }
3081 *a = PyTuple_GET_ITEM(r, 0);
3082 Py_INCREF(*a);
3083 *b = PyTuple_GET_ITEM(r, 1);
3084 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003085 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003086 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003087 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003088 }
3089 if (other->ob_type->tp_as_number != NULL &&
3090 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3091 PyObject *r;
3092 r = call_maybe(
3093 other, "__coerce__", &coerce_str, "(O)", self);
3094 if (r == NULL)
3095 return -1;
3096 if (r == Py_NotImplemented) {
3097 Py_DECREF(r);
3098 return 1;
3099 }
3100 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3101 PyErr_SetString(PyExc_TypeError,
3102 "__coerce__ didn't return a 2-tuple");
3103 Py_DECREF(r);
3104 return -1;
3105 }
3106 *a = PyTuple_GET_ITEM(r, 1);
3107 Py_INCREF(*a);
3108 *b = PyTuple_GET_ITEM(r, 0);
3109 Py_INCREF(*b);
3110 Py_DECREF(r);
3111 return 0;
3112 }
3113 return 1;
3114}
3115
Guido van Rossumdc91b992001-08-08 22:26:22 +00003116SLOT0(slot_nb_int, "__int__")
3117SLOT0(slot_nb_long, "__long__")
3118SLOT0(slot_nb_float, "__float__")
3119SLOT0(slot_nb_oct, "__oct__")
3120SLOT0(slot_nb_hex, "__hex__")
3121SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3122SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3123SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3124SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3125SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3126SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3127SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3128SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3129SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3130SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3131SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3132SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3133 "__floordiv__", "__rfloordiv__")
3134SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3135SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3136SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003137
3138static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003139half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003140{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003141 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003142 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003143 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003144
Guido van Rossum60718732001-08-28 17:47:51 +00003145 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003146 if (func == NULL) {
3147 PyErr_Clear();
3148 }
3149 else {
3150 args = Py_BuildValue("(O)", other);
3151 if (args == NULL)
3152 res = NULL;
3153 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003154 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003155 Py_DECREF(args);
3156 }
3157 if (res != Py_NotImplemented) {
3158 if (res == NULL)
3159 return -2;
3160 c = PyInt_AsLong(res);
3161 Py_DECREF(res);
3162 if (c == -1 && PyErr_Occurred())
3163 return -2;
3164 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3165 }
3166 Py_DECREF(res);
3167 }
3168 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003169}
3170
Guido van Rossumab3b0342001-09-18 20:38:53 +00003171/* This slot is published for the benefit of try_3way_compare in object.c */
3172int
3173_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003174{
3175 int c;
3176
Guido van Rossumab3b0342001-09-18 20:38:53 +00003177 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003178 c = half_compare(self, other);
3179 if (c <= 1)
3180 return c;
3181 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003182 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003183 c = half_compare(other, self);
3184 if (c < -1)
3185 return -2;
3186 if (c <= 1)
3187 return -c;
3188 }
3189 return (void *)self < (void *)other ? -1 :
3190 (void *)self > (void *)other ? 1 : 0;
3191}
3192
3193static PyObject *
3194slot_tp_repr(PyObject *self)
3195{
3196 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003197 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003198
Guido van Rossum60718732001-08-28 17:47:51 +00003199 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003200 if (func != NULL) {
3201 res = PyEval_CallObject(func, NULL);
3202 Py_DECREF(func);
3203 return res;
3204 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003205 PyErr_Clear();
3206 return PyString_FromFormat("<%s object at %p>",
3207 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003208}
3209
3210static PyObject *
3211slot_tp_str(PyObject *self)
3212{
3213 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003214 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003215
Guido van Rossum60718732001-08-28 17:47:51 +00003216 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003217 if (func != NULL) {
3218 res = PyEval_CallObject(func, NULL);
3219 Py_DECREF(func);
3220 return res;
3221 }
3222 else {
3223 PyErr_Clear();
3224 return slot_tp_repr(self);
3225 }
3226}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227
3228static long
3229slot_tp_hash(PyObject *self)
3230{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003231 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003232 static PyObject *hash_str, *eq_str, *cmp_str;
3233
Tim Peters6d6c1a32001-08-02 04:15:00 +00003234 long h;
3235
Guido van Rossum60718732001-08-28 17:47:51 +00003236 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003237
3238 if (func != NULL) {
3239 res = PyEval_CallObject(func, NULL);
3240 Py_DECREF(func);
3241 if (res == NULL)
3242 return -1;
3243 h = PyInt_AsLong(res);
3244 }
3245 else {
3246 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003247 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003248 if (func == NULL) {
3249 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003250 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003251 }
3252 if (func != NULL) {
3253 Py_DECREF(func);
3254 PyErr_SetString(PyExc_TypeError, "unhashable type");
3255 return -1;
3256 }
3257 PyErr_Clear();
3258 h = _Py_HashPointer((void *)self);
3259 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003260 if (h == -1 && !PyErr_Occurred())
3261 h = -2;
3262 return h;
3263}
3264
3265static PyObject *
3266slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3267{
Guido van Rossum60718732001-08-28 17:47:51 +00003268 static PyObject *call_str;
3269 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003270 PyObject *res;
3271
3272 if (meth == NULL)
3273 return NULL;
3274 res = PyObject_Call(meth, args, kwds);
3275 Py_DECREF(meth);
3276 return res;
3277}
3278
Tim Peters6d6c1a32001-08-02 04:15:00 +00003279static PyObject *
3280slot_tp_getattro(PyObject *self, PyObject *name)
3281{
3282 PyTypeObject *tp = self->ob_type;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003283 PyObject *getattr;
Guido van Rossum8e248182001-08-12 05:17:56 +00003284 static PyObject *getattr_str = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003285
Guido van Rossum8e248182001-08-12 05:17:56 +00003286 if (getattr_str == NULL) {
Guido van Rossum867a8d22001-09-21 19:29:08 +00003287 getattr_str = PyString_InternFromString("__getattribute__");
Guido van Rossum8e248182001-08-12 05:17:56 +00003288 if (getattr_str == NULL)
3289 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003290 }
Guido van Rossum8e248182001-08-12 05:17:56 +00003291 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossumc3542212001-08-16 09:18:56 +00003292 if (getattr == NULL) {
3293 /* Avoid further slowdowns */
3294 if (tp->tp_getattro == slot_tp_getattro)
3295 tp->tp_getattro = PyObject_GenericGetAttr;
Guido van Rossum8e248182001-08-12 05:17:56 +00003296 return PyObject_GenericGetAttr(self, name);
Guido van Rossumc3542212001-08-16 09:18:56 +00003297 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003298 return PyObject_CallFunction(getattr, "OO", self, name);
3299}
3300
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003301static PyObject *
3302slot_tp_getattr_hook(PyObject *self, PyObject *name)
3303{
3304 PyTypeObject *tp = self->ob_type;
3305 PyObject *getattr, *getattribute, *res;
3306 static PyObject *getattribute_str = NULL;
3307 static PyObject *getattr_str = NULL;
3308
3309 if (getattr_str == NULL) {
3310 getattr_str = PyString_InternFromString("__getattr__");
3311 if (getattr_str == NULL)
3312 return NULL;
3313 }
3314 if (getattribute_str == NULL) {
3315 getattribute_str =
3316 PyString_InternFromString("__getattribute__");
3317 if (getattribute_str == NULL)
3318 return NULL;
3319 }
3320 getattr = _PyType_Lookup(tp, getattr_str);
3321 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum1e1de1c2001-10-03 13:58:35 +00003322 if (getattribute != NULL &&
3323 getattribute->ob_type == &PyWrapperDescr_Type &&
3324 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3325 PyObject_GenericGetAttr)
3326 getattribute = NULL;
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003327 if (getattr == NULL && getattribute == NULL) {
3328 /* Avoid further slowdowns */
Guido van Rossum1e1de1c2001-10-03 13:58:35 +00003329 /* XXX This is questionable: it means that a class that
3330 isn't born with __getattr__ or __getattribute__ cannot
3331 acquire them in later life. But it's a relatively big
3332 speedup, so I'm keeping it in for now. If this is
3333 removed, you can also remove the "def __getattr__" from
3334 class C (marked with another XXX comment) in dynamics()
3335 in Lib/test/test_descr.py. */
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003336 if (tp->tp_getattro == slot_tp_getattr_hook)
3337 tp->tp_getattro = PyObject_GenericGetAttr;
3338 return PyObject_GenericGetAttr(self, name);
3339 }
3340 if (getattribute == NULL)
3341 res = PyObject_GenericGetAttr(self, name);
3342 else
3343 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum3926a632001-09-25 16:25:58 +00003344 if (getattr != NULL &&
3345 res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003346 PyErr_Clear();
3347 res = PyObject_CallFunction(getattr, "OO", self, name);
3348 }
3349 return res;
3350}
3351
Tim Peters6d6c1a32001-08-02 04:15:00 +00003352static int
3353slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3354{
3355 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003356 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003357
3358 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003359 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003360 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003361 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003362 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003363 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003364 if (res == NULL)
3365 return -1;
3366 Py_DECREF(res);
3367 return 0;
3368}
3369
3370/* Map rich comparison operators to their __xx__ namesakes */
3371static char *name_op[] = {
3372 "__lt__",
3373 "__le__",
3374 "__eq__",
3375 "__ne__",
3376 "__gt__",
3377 "__ge__",
3378};
3379
3380static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003381half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003382{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003383 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003384 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003385
Guido van Rossum60718732001-08-28 17:47:51 +00003386 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003387 if (func == NULL) {
3388 PyErr_Clear();
3389 Py_INCREF(Py_NotImplemented);
3390 return Py_NotImplemented;
3391 }
3392 args = Py_BuildValue("(O)", other);
3393 if (args == NULL)
3394 res = NULL;
3395 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003396 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003397 Py_DECREF(args);
3398 }
3399 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003400 return res;
3401}
3402
Guido van Rossumb8f63662001-08-15 23:57:02 +00003403/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3404static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3405
3406static PyObject *
3407slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3408{
3409 PyObject *res;
3410
3411 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3412 res = half_richcompare(self, other, op);
3413 if (res != Py_NotImplemented)
3414 return res;
3415 Py_DECREF(res);
3416 }
3417 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3418 res = half_richcompare(other, self, swapped_op[op]);
3419 if (res != Py_NotImplemented) {
3420 return res;
3421 }
3422 Py_DECREF(res);
3423 }
3424 Py_INCREF(Py_NotImplemented);
3425 return Py_NotImplemented;
3426}
3427
3428static PyObject *
3429slot_tp_iter(PyObject *self)
3430{
3431 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003432 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003433
Guido van Rossum60718732001-08-28 17:47:51 +00003434 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003435 if (func != NULL) {
3436 res = PyObject_CallObject(func, NULL);
3437 Py_DECREF(func);
3438 return res;
3439 }
3440 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003441 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003442 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003443 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003444 return NULL;
3445 }
3446 Py_DECREF(func);
3447 return PySeqIter_New(self);
3448}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003449
3450static PyObject *
3451slot_tp_iternext(PyObject *self)
3452{
Guido van Rossum2730b132001-08-28 18:22:14 +00003453 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003454 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003455}
3456
Guido van Rossum1a493502001-08-17 16:47:50 +00003457static PyObject *
3458slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3459{
3460 PyTypeObject *tp = self->ob_type;
3461 PyObject *get;
3462 static PyObject *get_str = NULL;
3463
3464 if (get_str == NULL) {
3465 get_str = PyString_InternFromString("__get__");
3466 if (get_str == NULL)
3467 return NULL;
3468 }
3469 get = _PyType_Lookup(tp, get_str);
3470 if (get == NULL) {
3471 /* Avoid further slowdowns */
3472 if (tp->tp_descr_get == slot_tp_descr_get)
3473 tp->tp_descr_get = NULL;
3474 Py_INCREF(self);
3475 return self;
3476 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003477 if (obj == NULL)
3478 obj = Py_None;
3479 if (type == NULL)
3480 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003481 return PyObject_CallFunction(get, "OOO", self, obj, type);
3482}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003483
3484static int
3485slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3486{
Guido van Rossum2c252392001-08-24 10:13:31 +00003487 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003488 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003489
3490 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003491 res = call_method(self, "__del__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003492 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003493 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003494 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003495 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003496 if (res == NULL)
3497 return -1;
3498 Py_DECREF(res);
3499 return 0;
3500}
3501
3502static int
3503slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3504{
Guido van Rossum60718732001-08-28 17:47:51 +00003505 static PyObject *init_str;
3506 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003507 PyObject *res;
3508
3509 if (meth == NULL)
3510 return -1;
3511 res = PyObject_Call(meth, args, kwds);
3512 Py_DECREF(meth);
3513 if (res == NULL)
3514 return -1;
3515 Py_DECREF(res);
3516 return 0;
3517}
3518
3519static PyObject *
3520slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3521{
3522 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3523 PyObject *newargs, *x;
3524 int i, n;
3525
3526 if (func == NULL)
3527 return NULL;
3528 assert(PyTuple_Check(args));
3529 n = PyTuple_GET_SIZE(args);
3530 newargs = PyTuple_New(n+1);
3531 if (newargs == NULL)
3532 return NULL;
3533 Py_INCREF(type);
3534 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3535 for (i = 0; i < n; i++) {
3536 x = PyTuple_GET_ITEM(args, i);
3537 Py_INCREF(x);
3538 PyTuple_SET_ITEM(newargs, i+1, x);
3539 }
3540 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003541 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003542 Py_DECREF(func);
3543 return x;
3544}
3545
Guido van Rossumf040ede2001-08-07 16:40:56 +00003546/* This is called at the very end of type_new() (even after
Guido van Rossum528b7eb2001-08-07 17:24:28 +00003547 PyType_Ready()) to complete the initialization of dynamic types.
Guido van Rossumf040ede2001-08-07 16:40:56 +00003548 The dict argument is the dictionary argument passed to type_new(),
3549 which is the local namespace of the class statement, in other
3550 words, it contains the methods. For each special method (like
3551 __repr__) defined in the dictionary, the corresponding function
3552 slot in the type object (like tp_repr) is set to a special function
3553 whose name is 'slot_' followed by the slot name and whose signature
3554 is whatever is required for that slot. These slot functions look
3555 up the corresponding method in the type's dictionary and call it.
3556 The slot functions have to take care of the various peculiarities
3557 of the mapping between slots and special methods, such as mapping
3558 one slot to multiple methods (tp_richcompare <--> __le__, __lt__
3559 etc.) or mapping multiple slots to a single method (sq_item,
3560 mp_subscript <--> __getitem__). */
3561
Tim Peters6d6c1a32001-08-02 04:15:00 +00003562static void
3563override_slots(PyTypeObject *type, PyObject *dict)
3564{
3565 PySequenceMethods *sq = type->tp_as_sequence;
3566 PyMappingMethods *mp = type->tp_as_mapping;
3567 PyNumberMethods *nb = type->tp_as_number;
3568
Guido van Rossumdc91b992001-08-08 22:26:22 +00003569#define SQSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003570 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003571 sq->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572 }
3573
Guido van Rossumdc91b992001-08-08 22:26:22 +00003574#define MPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003575 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003576 mp->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003577 }
3578
Guido van Rossumdc91b992001-08-08 22:26:22 +00003579#define NBSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003580 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003581 nb->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003582 }
3583
Guido van Rossumdc91b992001-08-08 22:26:22 +00003584#define TPSLOT(OPNAME, SLOTNAME, FUNCNAME) \
Guido van Rossum8e248182001-08-12 05:17:56 +00003585 if (dict == NULL || PyDict_GetItemString(dict, OPNAME)) { \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003586 type->SLOTNAME = FUNCNAME; \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003587 }
3588
Guido van Rossumdc91b992001-08-08 22:26:22 +00003589 SQSLOT("__len__", sq_length, slot_sq_length);
3590 SQSLOT("__add__", sq_concat, slot_sq_concat);
3591 SQSLOT("__mul__", sq_repeat, slot_sq_repeat);
3592 SQSLOT("__getitem__", sq_item, slot_sq_item);
3593 SQSLOT("__getslice__", sq_slice, slot_sq_slice);
3594 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item);
3595 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item);
3596 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice);
3597 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice);
3598 SQSLOT("__contains__", sq_contains, slot_sq_contains);
3599 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat);
3600 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003601
Guido van Rossumdc91b992001-08-08 22:26:22 +00003602 MPSLOT("__len__", mp_length, slot_mp_length);
3603 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript);
3604 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript);
3605 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003606
Guido van Rossumdc91b992001-08-08 22:26:22 +00003607 NBSLOT("__add__", nb_add, slot_nb_add);
3608 NBSLOT("__sub__", nb_subtract, slot_nb_subtract);
3609 NBSLOT("__mul__", nb_multiply, slot_nb_multiply);
3610 NBSLOT("__div__", nb_divide, slot_nb_divide);
3611 NBSLOT("__mod__", nb_remainder, slot_nb_remainder);
3612 NBSLOT("__divmod__", nb_divmod, slot_nb_divmod);
3613 NBSLOT("__pow__", nb_power, slot_nb_power);
3614 NBSLOT("__neg__", nb_negative, slot_nb_negative);
3615 NBSLOT("__pos__", nb_positive, slot_nb_positive);
3616 NBSLOT("__abs__", nb_absolute, slot_nb_absolute);
3617 NBSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero);
3618 NBSLOT("__invert__", nb_invert, slot_nb_invert);
3619 NBSLOT("__lshift__", nb_lshift, slot_nb_lshift);
3620 NBSLOT("__rshift__", nb_rshift, slot_nb_rshift);
3621 NBSLOT("__and__", nb_and, slot_nb_and);
3622 NBSLOT("__xor__", nb_xor, slot_nb_xor);
3623 NBSLOT("__or__", nb_or, slot_nb_or);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003624 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003625 NBSLOT("__int__", nb_int, slot_nb_int);
3626 NBSLOT("__long__", nb_long, slot_nb_long);
3627 NBSLOT("__float__", nb_float, slot_nb_float);
3628 NBSLOT("__oct__", nb_oct, slot_nb_oct);
3629 NBSLOT("__hex__", nb_hex, slot_nb_hex);
3630 NBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add);
3631 NBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract);
3632 NBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply);
3633 NBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide);
3634 NBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder);
3635 NBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power);
3636 NBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift);
3637 NBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift);
3638 NBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and);
3639 NBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor);
3640 NBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or);
3641 NBSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide);
3642 NBSLOT("__truediv__", nb_true_divide, slot_nb_true_divide);
3643 NBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3644 slot_nb_inplace_floor_divide);
3645 NBSLOT("__itruediv__", nb_inplace_true_divide,
3646 slot_nb_inplace_true_divide);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003647
Guido van Rossum8e248182001-08-12 05:17:56 +00003648 if (dict == NULL ||
3649 PyDict_GetItemString(dict, "__str__") ||
Tim Peters6d6c1a32001-08-02 04:15:00 +00003650 PyDict_GetItemString(dict, "__repr__"))
3651 type->tp_print = NULL;
3652
Guido van Rossumab3b0342001-09-18 20:38:53 +00003653 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003654 TPSLOT("__repr__", tp_repr, slot_tp_repr);
3655 TPSLOT("__hash__", tp_hash, slot_tp_hash);
3656 TPSLOT("__call__", tp_call, slot_tp_call);
3657 TPSLOT("__str__", tp_str, slot_tp_str);
Guido van Rossum867a8d22001-09-21 19:29:08 +00003658 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattro);
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003659 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003660 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro);
3661 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare);
3662 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare);
3663 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare);
3664 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare);
3665 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare);
3666 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare);
3667 TPSLOT("__iter__", tp_iter, slot_tp_iter);
3668 TPSLOT("next", tp_iternext, slot_tp_iternext);
3669 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get);
3670 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set);
3671 TPSLOT("__init__", tp_init, slot_tp_init);
3672 TPSLOT("__new__", tp_new, slot_tp_new);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003673}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003674
3675
3676/* Cooperative 'super' */
3677
3678typedef struct {
3679 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003680 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003681 PyObject *obj;
3682} superobject;
3683
Guido van Rossum6f799372001-09-20 20:46:19 +00003684static PyMemberDef super_members[] = {
3685 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3686 "the class invoking super()"},
3687 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3688 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003689 {0}
3690};
3691
Guido van Rossum705f0f52001-08-24 16:47:00 +00003692static void
3693super_dealloc(PyObject *self)
3694{
3695 superobject *su = (superobject *)self;
3696
Guido van Rossum048eb752001-10-02 21:24:57 +00003697 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003698 Py_XDECREF(su->obj);
3699 Py_XDECREF(su->type);
3700 self->ob_type->tp_free(self);
3701}
3702
3703static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003704super_repr(PyObject *self)
3705{
3706 superobject *su = (superobject *)self;
3707
3708 if (su->obj)
3709 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003710 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003711 su->type ? su->type->tp_name : "NULL",
3712 su->obj->ob_type->tp_name);
3713 else
3714 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003715 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003716 su->type ? su->type->tp_name : "NULL");
3717}
3718
3719static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003720super_getattro(PyObject *self, PyObject *name)
3721{
3722 superobject *su = (superobject *)self;
3723
3724 if (su->obj != NULL) {
3725 PyObject *mro, *res, *tmp;
3726 descrgetfunc f;
3727 int i, n;
3728
Guido van Rossume705ef12001-08-29 15:47:06 +00003729 mro = su->obj->ob_type->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 Rossum705f0f52001-08-24 16:47:00 +00003736 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003737 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003738 break;
3739 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003740 if (i >= n && PyType_Check(su->obj)) {
3741 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003742 if (mro == NULL)
3743 n = 0;
3744 else {
3745 assert(PyTuple_Check(mro));
3746 n = PyTuple_GET_SIZE(mro);
3747 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003748 for (i = 0; i < n; i++) {
3749 if ((PyObject *)(su->type) ==
3750 PyTuple_GET_ITEM(mro, i))
3751 break;
3752 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003753 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003754 i++;
3755 res = NULL;
3756 for (; i < n; i++) {
3757 tmp = PyTuple_GET_ITEM(mro, i);
3758 assert(PyType_Check(tmp));
3759 res = PyDict_GetItem(
3760 ((PyTypeObject *)tmp)->tp_defined, name);
3761 if (res != NULL) {
3762 Py_INCREF(res);
3763 f = res->ob_type->tp_descr_get;
3764 if (f != NULL) {
3765 tmp = f(res, su->obj, res);
3766 Py_DECREF(res);
3767 res = tmp;
3768 }
3769 return res;
3770 }
3771 }
3772 }
3773 return PyObject_GenericGetAttr(self, name);
3774}
3775
3776static PyObject *
3777super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3778{
3779 superobject *su = (superobject *)self;
3780 superobject *new;
3781
3782 if (obj == NULL || obj == Py_None || su->obj != NULL) {
3783 /* Not binding to an object, or already bound */
3784 Py_INCREF(self);
3785 return self;
3786 }
3787 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type, NULL, NULL);
3788 if (new == NULL)
3789 return NULL;
3790 Py_INCREF(su->type);
3791 Py_INCREF(obj);
3792 new->type = su->type;
3793 new->obj = obj;
3794 return (PyObject *)new;
3795}
3796
3797static int
3798super_init(PyObject *self, PyObject *args, PyObject *kwds)
3799{
3800 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00003801 PyTypeObject *type;
3802 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003803
3804 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
3805 return -1;
3806 if (obj == Py_None)
3807 obj = NULL;
Guido van Rossume705ef12001-08-29 15:47:06 +00003808 if (obj != NULL &&
3809 !PyType_IsSubtype(obj->ob_type, type) &&
3810 !(PyType_Check(obj) &&
3811 PyType_IsSubtype((PyTypeObject *)obj, type))) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003812 PyErr_SetString(PyExc_TypeError,
Guido van Rossume705ef12001-08-29 15:47:06 +00003813 "super(type, obj): "
3814 "obj must be an instance or subtype of type");
Guido van Rossum705f0f52001-08-24 16:47:00 +00003815 return -1;
3816 }
3817 Py_INCREF(type);
3818 Py_XINCREF(obj);
3819 su->type = type;
3820 su->obj = obj;
3821 return 0;
3822}
3823
3824static char super_doc[] =
3825"super(type) -> unbound super object\n"
3826"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00003827"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00003828"Typical use to call a cooperative superclass method:\n"
3829"class C(B):\n"
3830" def meth(self, arg):\n"
3831" super(C, self).meth(arg)";
3832
Guido van Rossum048eb752001-10-02 21:24:57 +00003833static int
3834super_traverse(PyObject *self, visitproc visit, void *arg)
3835{
3836 superobject *su = (superobject *)self;
3837 int err;
3838
3839#define VISIT(SLOT) \
3840 if (SLOT) { \
3841 err = visit((PyObject *)(SLOT), arg); \
3842 if (err) \
3843 return err; \
3844 }
3845
3846 VISIT(su->obj);
3847 VISIT(su->type);
3848
3849#undef VISIT
3850
3851 return 0;
3852}
3853
Guido van Rossum705f0f52001-08-24 16:47:00 +00003854PyTypeObject PySuper_Type = {
3855 PyObject_HEAD_INIT(&PyType_Type)
3856 0, /* ob_size */
3857 "super", /* tp_name */
3858 sizeof(superobject), /* tp_basicsize */
3859 0, /* tp_itemsize */
3860 /* methods */
3861 super_dealloc, /* tp_dealloc */
3862 0, /* tp_print */
3863 0, /* tp_getattr */
3864 0, /* tp_setattr */
3865 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003866 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003867 0, /* tp_as_number */
3868 0, /* tp_as_sequence */
3869 0, /* tp_as_mapping */
3870 0, /* tp_hash */
3871 0, /* tp_call */
3872 0, /* tp_str */
3873 super_getattro, /* tp_getattro */
3874 0, /* tp_setattro */
3875 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00003876 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
3877 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003878 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00003879 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003880 0, /* tp_clear */
3881 0, /* tp_richcompare */
3882 0, /* tp_weaklistoffset */
3883 0, /* tp_iter */
3884 0, /* tp_iternext */
3885 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003886 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003887 0, /* tp_getset */
3888 0, /* tp_base */
3889 0, /* tp_dict */
3890 super_descr_get, /* tp_descr_get */
3891 0, /* tp_descr_set */
3892 0, /* tp_dictoffset */
3893 super_init, /* tp_init */
3894 PyType_GenericAlloc, /* tp_alloc */
3895 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00003896 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00003897};