blob: df6d6f291d03e1ea901acf4ed8599d19b319f6a1 [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},
Guido van Rossum9676b222001-08-17 20:32:36 +000011 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000012 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
13 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
14 {"__dictoffset__", T_LONG,
15 offsetof(PyTypeObject, tp_dictoffset), READONLY},
16 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
17 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
18 {0}
19};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000020
Guido van Rossumc0b618a1997-05-02 03:12:38 +000021static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000022type_name(PyTypeObject *type, void *context)
23{
24 char *s;
25
26 s = strrchr(type->tp_name, '.');
27 if (s == NULL)
28 s = type->tp_name;
29 else
30 s++;
31 return PyString_FromString(s);
32}
33
34static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000035type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000036{
Guido van Rossumc3542212001-08-16 09:18:56 +000037 PyObject *mod;
38 char *s;
39
40 s = strrchr(type->tp_name, '.');
41 if (s != NULL)
42 return PyString_FromStringAndSize(type->tp_name,
43 (int)(s - type->tp_name));
44 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
45 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000046 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000047 if (mod != NULL && PyString_Check(mod)) {
48 Py_INCREF(mod);
49 return mod;
50 }
51 PyErr_SetString(PyExc_AttributeError, "__module__");
52 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000053}
54
Guido van Rossum3926a632001-09-25 16:25:58 +000055static int
56type_set_module(PyTypeObject *type, PyObject *value, void *context)
57{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000058 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000059 strrchr(type->tp_name, '.')) {
60 PyErr_Format(PyExc_TypeError,
61 "can't set %s.__module__", type->tp_name);
62 return -1;
63 }
64 if (!value) {
65 PyErr_Format(PyExc_TypeError,
66 "can't delete %s.__module__", type->tp_name);
67 return -1;
68 }
69 return PyDict_SetItemString(type->tp_dict, "__module__", value);
70}
71
Tim Peters6d6c1a32001-08-02 04:15:00 +000072static PyObject *
73type_dict(PyTypeObject *type, void *context)
74{
75 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000076 Py_INCREF(Py_None);
77 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000078 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000079 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000080}
81
Neil Schemenauerf23473f2001-10-21 22:28:58 +000082static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +000083 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +000084 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000085 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000086 {0}
87};
88
Martin v. Löwis0163d6d2001-06-09 07:34:05 +000089static int
90type_compare(PyObject *v, PyObject *w)
91{
92 /* This is called with type objects only. So we
93 can just compare the addresses. */
94 Py_uintptr_t vv = (Py_uintptr_t)v;
95 Py_uintptr_t ww = (Py_uintptr_t)w;
96 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
97}
98
Guido van Rossumc0b618a1997-05-02 03:12:38 +000099static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000100type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000101{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000102 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000103 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000104
105 mod = type_module(type, NULL);
106 if (mod == NULL)
107 PyErr_Clear();
108 else if (!PyString_Check(mod)) {
109 Py_DECREF(mod);
110 mod = NULL;
111 }
112 name = type_name(type, NULL);
113 if (name == NULL)
114 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000115
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000116 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
117 kind = "class";
118 else
119 kind = "type";
120
Barry Warsaw7ce36942001-08-24 18:34:26 +0000121 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000122 rtn = PyString_FromFormat("<%s '%s.%s'>",
123 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000124 PyString_AS_STRING(mod),
125 PyString_AS_STRING(name));
126 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000127 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000128 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000129
Guido van Rossumc3542212001-08-16 09:18:56 +0000130 Py_XDECREF(mod);
131 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000132 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000133}
134
Tim Peters6d6c1a32001-08-02 04:15:00 +0000135static PyObject *
136type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
137{
138 PyObject *obj;
139
140 if (type->tp_new == NULL) {
141 PyErr_Format(PyExc_TypeError,
142 "cannot create '%.100s' instances",
143 type->tp_name);
144 return NULL;
145 }
146
Tim Peters3f996e72001-09-13 19:18:27 +0000147 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000148 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000149 /* Ugly exception: when the call was type(something),
150 don't call tp_init on the result. */
151 if (type == &PyType_Type &&
152 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
153 (kwds == NULL ||
154 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
155 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156 type = obj->ob_type;
157 if (type->tp_init != NULL &&
158 type->tp_init(obj, args, kwds) < 0) {
159 Py_DECREF(obj);
160 obj = NULL;
161 }
162 }
163 return obj;
164}
165
166PyObject *
167PyType_GenericAlloc(PyTypeObject *type, int nitems)
168{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000170 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000171
172 if (PyType_IS_GC(type))
Tim Peters6d483d32001-10-06 21:27:34 +0000173 obj = _PyObject_GC_Malloc(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000174 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000175 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000176
Neil Schemenauerc806c882001-08-29 23:54:54 +0000177 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000178 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000179
Neil Schemenauerc806c882001-08-29 23:54:54 +0000180 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000181
Tim Peters6d6c1a32001-08-02 04:15:00 +0000182 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
183 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000184
Tim Peters6d6c1a32001-08-02 04:15:00 +0000185 if (type->tp_itemsize == 0)
186 PyObject_INIT(obj, type);
187 else
188 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000189
Tim Peters6d6c1a32001-08-02 04:15:00 +0000190 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000191 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000192 return obj;
193}
194
195PyObject *
196PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
197{
198 return type->tp_alloc(type, 0);
199}
200
Guido van Rossum9475a232001-10-05 20:51:39 +0000201/* Helpers for subtyping */
202
203static int
204subtype_traverse(PyObject *self, visitproc visit, void *arg)
205{
206 PyTypeObject *type, *base;
207 traverseproc f;
208 int err;
209
210 /* Find the nearest base with a different tp_traverse */
211 type = self->ob_type;
212 base = type->tp_base;
213 while ((f = base->tp_traverse) == subtype_traverse) {
214 base = base->tp_base;
215 assert(base);
216 }
217
218 if (type->tp_dictoffset != base->tp_dictoffset) {
219 PyObject **dictptr = _PyObject_GetDictPtr(self);
220 if (dictptr && *dictptr) {
221 err = visit(*dictptr, arg);
222 if (err)
223 return err;
224 }
225 }
226
227 if (f)
228 return f(self, visit, arg);
229 return 0;
230}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000231
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000232staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
233
234static int
235call_finalizer(PyObject *self)
236{
237 static PyObject *del_str = NULL;
238 PyObject *del, *res;
239 PyObject *error_type, *error_value, *error_traceback;
240
241 /* Temporarily resurrect the object. */
242#ifdef Py_TRACE_REFS
243#ifndef Py_REF_DEBUG
244# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
245#endif
246 /* much too complicated if Py_TRACE_REFS defined */
247 _Py_NewReference((PyObject *)self);
248#ifdef COUNT_ALLOCS
249 /* compensate for boost in _Py_NewReference; note that
250 * _Py_RefTotal was also boosted; we'll knock that down later.
251 */
252 self->ob_type->tp_allocs--;
253#endif
254#else /* !Py_TRACE_REFS */
255 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
256 Py_INCREF(self);
257#endif /* !Py_TRACE_REFS */
258
259 /* Save the current exception, if any. */
260 PyErr_Fetch(&error_type, &error_value, &error_traceback);
261
262 /* Execute __del__ method, if any. */
263 del = lookup_maybe(self, "__del__", &del_str);
264 if (del != NULL) {
265 res = PyEval_CallObject(del, NULL);
266 if (res == NULL)
267 PyErr_WriteUnraisable(del);
268 else
269 Py_DECREF(res);
270 Py_DECREF(del);
271 }
272
273 /* Restore the saved exception. */
274 PyErr_Restore(error_type, error_value, error_traceback);
275
276 /* Undo the temporary resurrection; can't use DECREF here, it would
277 * cause a recursive call.
278 */
279#ifdef Py_REF_DEBUG
280 /* _Py_RefTotal was boosted either by _Py_NewReference or
281 * Py_INCREF above.
282 */
283 _Py_RefTotal--;
284#endif
285 if (--self->ob_refcnt > 0) {
286#ifdef COUNT_ALLOCS
287 self->ob_type->tp_frees--;
288#endif
289 _PyObject_GC_TRACK(self);
290 return -1; /* __del__ added a reference; don't delete now */
291 }
292#ifdef Py_TRACE_REFS
293 _Py_ForgetReference((PyObject *)self);
294#ifdef COUNT_ALLOCS
295 /* compensate for increment in _Py_ForgetReference */
296 self->ob_type->tp_frees--;
297#endif
298#endif
299
300 return 0;
301}
302
Tim Peters6d6c1a32001-08-02 04:15:00 +0000303static void
304subtype_dealloc(PyObject *self)
305{
Guido van Rossum14227b42001-12-06 02:35:58 +0000306 PyTypeObject *type, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000307 destructor f;
308
309 /* This exists so we can DECREF self->ob_type */
310
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000311 if (call_finalizer(self) < 0)
312 return;
313
Tim Peters6d6c1a32001-08-02 04:15:00 +0000314 /* Find the nearest base with a different tp_dealloc */
315 type = self->ob_type;
Guido van Rossum14227b42001-12-06 02:35:58 +0000316 base = type->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000317 while ((f = base->tp_dealloc) == subtype_dealloc) {
318 base = base->tp_base;
319 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000320 }
321
322 /* Clear __slots__ variables */
323 if (type->tp_basicsize != base->tp_basicsize &&
324 type->tp_itemsize == 0)
325 {
326 char *addr = ((char *)self);
327 char *p = addr + base->tp_basicsize;
328 char *q = addr + type->tp_basicsize;
329 for (; p < q; p += sizeof(PyObject *)) {
330 PyObject **pp;
331 if (p == addr + type->tp_dictoffset ||
332 p == addr + type->tp_weaklistoffset)
333 continue;
334 pp = (PyObject **)p;
335 if (*pp != NULL) {
336 Py_DECREF(*pp);
337 *pp = NULL;
Guido van Rossum33bab012001-12-05 22:45:48 +0000338 }
339 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000340 }
341
342 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000343 if (type->tp_dictoffset && !base->tp_dictoffset) {
344 PyObject **dictptr = _PyObject_GetDictPtr(self);
345 if (dictptr != NULL) {
346 PyObject *dict = *dictptr;
347 if (dict != NULL) {
348 Py_DECREF(dict);
349 *dictptr = NULL;
350 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000351 }
352 }
353
Guido van Rossum9676b222001-08-17 20:32:36 +0000354 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000355 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000356 PyObject_ClearWeakRefs(self);
357
Tim Peters6d6c1a32001-08-02 04:15:00 +0000358 /* Finalize GC if the base doesn't do GC and we do */
359 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000360 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000361
362 /* Call the base tp_dealloc() */
363 assert(f);
364 f(self);
365
366 /* Can't reference self beyond this point */
367 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
368 Py_DECREF(type);
369 }
370}
371
Tim Peters6d6c1a32001-08-02 04:15:00 +0000372staticforward PyTypeObject *solid_base(PyTypeObject *type);
373
374typedef struct {
375 PyTypeObject type;
376 PyNumberMethods as_number;
377 PySequenceMethods as_sequence;
378 PyMappingMethods as_mapping;
379 PyBufferProcs as_buffer;
380 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000381 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000382} etype;
383
384/* type test with subclassing support */
385
386int
387PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
388{
389 PyObject *mro;
390
Guido van Rossum9478d072001-09-07 18:52:13 +0000391 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
392 return b == a || b == &PyBaseObject_Type;
393
Tim Peters6d6c1a32001-08-02 04:15:00 +0000394 mro = a->tp_mro;
395 if (mro != NULL) {
396 /* Deal with multiple inheritance without recursion
397 by walking the MRO tuple */
398 int i, n;
399 assert(PyTuple_Check(mro));
400 n = PyTuple_GET_SIZE(mro);
401 for (i = 0; i < n; i++) {
402 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
403 return 1;
404 }
405 return 0;
406 }
407 else {
408 /* a is not completely initilized yet; follow tp_base */
409 do {
410 if (a == b)
411 return 1;
412 a = a->tp_base;
413 } while (a != NULL);
414 return b == &PyBaseObject_Type;
415 }
416}
417
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000418/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000419 without looking in the instance dictionary
420 (so we can't use PyObject_GetAttr) but still binding
421 it to the instance. The arguments are the object,
422 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000423 static variable used to cache the interned Python string.
424
425 Two variants:
426
427 - lookup_maybe() returns NULL without raising an exception
428 when the _PyType_Lookup() call fails;
429
430 - lookup_method() always raises an exception upon errors.
431*/
Guido van Rossum60718732001-08-28 17:47:51 +0000432
433static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000434lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000435{
436 PyObject *res;
437
438 if (*attrobj == NULL) {
439 *attrobj = PyString_InternFromString(attrstr);
440 if (*attrobj == NULL)
441 return NULL;
442 }
443 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000444 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000445 descrgetfunc f;
446 if ((f = res->ob_type->tp_descr_get) == NULL)
447 Py_INCREF(res);
448 else
449 res = f(res, self, (PyObject *)(self->ob_type));
450 }
451 return res;
452}
453
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000454static PyObject *
455lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
456{
457 PyObject *res = lookup_maybe(self, attrstr, attrobj);
458 if (res == NULL && !PyErr_Occurred())
459 PyErr_SetObject(PyExc_AttributeError, *attrobj);
460 return res;
461}
462
Guido van Rossum2730b132001-08-28 18:22:14 +0000463/* A variation of PyObject_CallMethod that uses lookup_method()
464 instead of PyObject_GetAttrString(). This uses the same convention
465 as lookup_method to cache the interned name string object. */
466
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000467static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000468call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
469{
470 va_list va;
471 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000472 va_start(va, format);
473
Guido van Rossumda21c012001-10-03 00:50:18 +0000474 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000475 if (func == NULL) {
476 va_end(va);
477 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000478 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000479 return NULL;
480 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000481
482 if (format && *format)
483 args = Py_VaBuildValue(format, va);
484 else
485 args = PyTuple_New(0);
486
487 va_end(va);
488
489 if (args == NULL)
490 return NULL;
491
492 assert(PyTuple_Check(args));
493 retval = PyObject_Call(func, args, NULL);
494
495 Py_DECREF(args);
496 Py_DECREF(func);
497
498 return retval;
499}
500
501/* Clone of call_method() that returns NotImplemented when the lookup fails. */
502
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000503static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000504call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
505{
506 va_list va;
507 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000508 va_start(va, format);
509
Guido van Rossumda21c012001-10-03 00:50:18 +0000510 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000511 if (func == NULL) {
512 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000513 if (!PyErr_Occurred()) {
514 Py_INCREF(Py_NotImplemented);
515 return Py_NotImplemented;
516 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000517 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000518 }
519
520 if (format && *format)
521 args = Py_VaBuildValue(format, va);
522 else
523 args = PyTuple_New(0);
524
525 va_end(va);
526
Guido van Rossum717ce002001-09-14 16:58:08 +0000527 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000528 return NULL;
529
Guido van Rossum717ce002001-09-14 16:58:08 +0000530 assert(PyTuple_Check(args));
531 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000532
533 Py_DECREF(args);
534 Py_DECREF(func);
535
536 return retval;
537}
538
Tim Peters6d6c1a32001-08-02 04:15:00 +0000539/* Method resolution order algorithm from "Putting Metaclasses to Work"
540 by Forman and Danforth (Addison-Wesley 1999). */
541
542static int
543conservative_merge(PyObject *left, PyObject *right)
544{
545 int left_size;
546 int right_size;
547 int i, j, r, ok;
548 PyObject *temp, *rr;
549
550 assert(PyList_Check(left));
551 assert(PyList_Check(right));
552
553 again:
554 left_size = PyList_GET_SIZE(left);
555 right_size = PyList_GET_SIZE(right);
556 for (i = 0; i < left_size; i++) {
557 for (j = 0; j < right_size; j++) {
558 if (PyList_GET_ITEM(left, i) ==
559 PyList_GET_ITEM(right, j)) {
560 /* found a merge point */
561 temp = PyList_New(0);
562 if (temp == NULL)
563 return -1;
564 for (r = 0; r < j; r++) {
565 rr = PyList_GET_ITEM(right, r);
566 ok = PySequence_Contains(left, rr);
567 if (ok < 0) {
568 Py_DECREF(temp);
569 return -1;
570 }
571 if (!ok) {
572 ok = PyList_Append(temp, rr);
573 if (ok < 0) {
574 Py_DECREF(temp);
575 return -1;
576 }
577 }
578 }
579 ok = PyList_SetSlice(left, i, i, temp);
580 Py_DECREF(temp);
581 if (ok < 0)
582 return -1;
583 ok = PyList_SetSlice(right, 0, j+1, NULL);
584 if (ok < 0)
585 return -1;
586 goto again;
587 }
588 }
589 }
590 return PyList_SetSlice(left, left_size, left_size, right);
591}
592
593static int
594serious_order_disagreements(PyObject *left, PyObject *right)
595{
596 return 0; /* XXX later -- for now, we cheat: "don't do that" */
597}
598
Tim Petersa91e9642001-11-14 23:32:33 +0000599static int
600fill_classic_mro(PyObject *mro, PyObject *cls)
601{
602 PyObject *bases, *base;
603 int i, n;
604
605 assert(PyList_Check(mro));
606 assert(PyClass_Check(cls));
607 i = PySequence_Contains(mro, cls);
608 if (i < 0)
609 return -1;
610 if (!i) {
611 if (PyList_Append(mro, cls) < 0)
612 return -1;
613 }
614 bases = ((PyClassObject *)cls)->cl_bases;
615 assert(bases && PyTuple_Check(bases));
616 n = PyTuple_GET_SIZE(bases);
617 for (i = 0; i < n; i++) {
618 base = PyTuple_GET_ITEM(bases, i);
619 if (fill_classic_mro(mro, base) < 0)
620 return -1;
621 }
622 return 0;
623}
624
625static PyObject *
626classic_mro(PyObject *cls)
627{
628 PyObject *mro;
629
630 assert(PyClass_Check(cls));
631 mro = PyList_New(0);
632 if (mro != NULL) {
633 if (fill_classic_mro(mro, cls) == 0)
634 return mro;
635 Py_DECREF(mro);
636 }
637 return NULL;
638}
639
Tim Peters6d6c1a32001-08-02 04:15:00 +0000640static PyObject *
641mro_implementation(PyTypeObject *type)
642{
643 int i, n, ok;
644 PyObject *bases, *result;
645
646 bases = type->tp_bases;
647 n = PyTuple_GET_SIZE(bases);
648 result = Py_BuildValue("[O]", (PyObject *)type);
649 if (result == NULL)
650 return NULL;
651 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000652 PyObject *base = PyTuple_GET_ITEM(bases, i);
653 PyObject *parentMRO;
654 if (PyType_Check(base))
655 parentMRO = PySequence_List(
656 ((PyTypeObject*)base)->tp_mro);
657 else
658 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000659 if (parentMRO == NULL) {
660 Py_DECREF(result);
661 return NULL;
662 }
663 if (serious_order_disagreements(result, parentMRO)) {
664 Py_DECREF(result);
665 return NULL;
666 }
667 ok = conservative_merge(result, parentMRO);
668 Py_DECREF(parentMRO);
669 if (ok < 0) {
670 Py_DECREF(result);
671 return NULL;
672 }
673 }
674 return result;
675}
676
677static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000678mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000679{
680 PyTypeObject *type = (PyTypeObject *)self;
681
Tim Peters6d6c1a32001-08-02 04:15:00 +0000682 return mro_implementation(type);
683}
684
685static int
686mro_internal(PyTypeObject *type)
687{
688 PyObject *mro, *result, *tuple;
689
690 if (type->ob_type == &PyType_Type) {
691 result = mro_implementation(type);
692 }
693 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000694 static PyObject *mro_str;
695 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000696 if (mro == NULL)
697 return -1;
698 result = PyObject_CallObject(mro, NULL);
699 Py_DECREF(mro);
700 }
701 if (result == NULL)
702 return -1;
703 tuple = PySequence_Tuple(result);
704 Py_DECREF(result);
705 type->tp_mro = tuple;
706 return 0;
707}
708
709
710/* Calculate the best base amongst multiple base classes.
711 This is the first one that's on the path to the "solid base". */
712
713static PyTypeObject *
714best_base(PyObject *bases)
715{
716 int i, n;
717 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000718 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000719
720 assert(PyTuple_Check(bases));
721 n = PyTuple_GET_SIZE(bases);
722 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000723 base = NULL;
724 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000725 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000726 base_proto = PyTuple_GET_ITEM(bases, i);
727 if (PyClass_Check(base_proto))
728 continue;
729 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000730 PyErr_SetString(
731 PyExc_TypeError,
732 "bases must be types");
733 return NULL;
734 }
Tim Petersa91e9642001-11-14 23:32:33 +0000735 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000736 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000737 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738 return NULL;
739 }
740 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000741 if (winner == NULL) {
742 winner = candidate;
743 base = base_i;
744 }
745 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746 ;
747 else if (PyType_IsSubtype(candidate, winner)) {
748 winner = candidate;
749 base = base_i;
750 }
751 else {
752 PyErr_SetString(
753 PyExc_TypeError,
754 "multiple bases have "
755 "instance lay-out conflict");
756 return NULL;
757 }
758 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000759 if (base == NULL)
760 PyErr_SetString(PyExc_TypeError,
761 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762 return base;
763}
764
765static int
766extra_ivars(PyTypeObject *type, PyTypeObject *base)
767{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000768 size_t t_size = type->tp_basicsize;
769 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770
Guido van Rossum9676b222001-08-17 20:32:36 +0000771 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000772 if (type->tp_itemsize || base->tp_itemsize) {
773 /* If itemsize is involved, stricter rules */
774 return t_size != b_size ||
775 type->tp_itemsize != base->tp_itemsize;
776 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000777 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
778 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
779 t_size -= sizeof(PyObject *);
780 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
781 type->tp_dictoffset + sizeof(PyObject *) == t_size)
782 t_size -= sizeof(PyObject *);
783
784 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000785}
786
787static PyTypeObject *
788solid_base(PyTypeObject *type)
789{
790 PyTypeObject *base;
791
792 if (type->tp_base)
793 base = solid_base(type->tp_base);
794 else
795 base = &PyBaseObject_Type;
796 if (extra_ivars(type, base))
797 return type;
798 else
799 return base;
800}
801
802staticforward void object_dealloc(PyObject *);
803staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000804staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000805staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000806
807static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000808subtype_dict(PyObject *obj, void *context)
809{
810 PyObject **dictptr = _PyObject_GetDictPtr(obj);
811 PyObject *dict;
812
813 if (dictptr == NULL) {
814 PyErr_SetString(PyExc_AttributeError,
815 "This object has no __dict__");
816 return NULL;
817 }
818 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000819 if (dict == NULL)
820 *dictptr = dict = PyDict_New();
821 Py_XINCREF(dict);
822 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000823}
824
Guido van Rossum6661be32001-10-26 04:26:12 +0000825static int
826subtype_setdict(PyObject *obj, PyObject *value, void *context)
827{
828 PyObject **dictptr = _PyObject_GetDictPtr(obj);
829 PyObject *dict;
830
831 if (dictptr == NULL) {
832 PyErr_SetString(PyExc_AttributeError,
833 "This object has no __dict__");
834 return -1;
835 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000836 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000837 PyErr_SetString(PyExc_TypeError,
838 "__dict__ must be set to a dictionary");
839 return -1;
840 }
841 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000842 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000843 *dictptr = value;
844 Py_XDECREF(dict);
845 return 0;
846}
847
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000848static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000849 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000850 {0},
851};
852
853static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000854type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
855{
856 PyObject *name, *bases, *dict;
857 static char *kwlist[] = {"name", "bases", "dict", 0};
858 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000859 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000861 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000862 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000863
Tim Peters3abca122001-10-27 19:37:48 +0000864 assert(args != NULL && PyTuple_Check(args));
865 assert(kwds == NULL || PyDict_Check(kwds));
866
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000867 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000868 {
869 const int nargs = PyTuple_GET_SIZE(args);
870 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
871
872 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
873 PyObject *x = PyTuple_GET_ITEM(args, 0);
874 Py_INCREF(x->ob_type);
875 return (PyObject *) x->ob_type;
876 }
877
878 /* SF bug 475327 -- if that didn't trigger, we need 3
879 arguments. but PyArg_ParseTupleAndKeywords below may give
880 a msg saying type() needs exactly 3. */
881 if (nargs + nkwds != 3) {
882 PyErr_SetString(PyExc_TypeError,
883 "type() takes 1 or 3 arguments");
884 return NULL;
885 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000886 }
887
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000888 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000889 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
890 &name,
891 &PyTuple_Type, &bases,
892 &PyDict_Type, &dict))
893 return NULL;
894
895 /* Determine the proper metatype to deal with this,
896 and check for metatype conflicts while we're at it.
897 Note that if some other metatype wins to contract,
898 it's possible that its instances are not types. */
899 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000900 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000901 for (i = 0; i < nbases; i++) {
902 tmp = PyTuple_GET_ITEM(bases, i);
903 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +0000904 if (tmptype == &PyClass_Type)
905 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000906 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000907 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000908 if (PyType_IsSubtype(tmptype, winner)) {
909 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000910 continue;
911 }
912 PyErr_SetString(PyExc_TypeError,
913 "metatype conflict among bases");
914 return NULL;
915 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000916 if (winner != metatype) {
917 if (winner->tp_new != type_new) /* Pass it to the winner */
918 return winner->tp_new(winner, args, kwds);
919 metatype = winner;
920 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000921
922 /* Adjust for empty tuple bases */
923 if (nbases == 0) {
924 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
925 if (bases == NULL)
926 return NULL;
927 nbases = 1;
928 }
929 else
930 Py_INCREF(bases);
931
932 /* XXX From here until type is allocated, "return NULL" leaks bases! */
933
934 /* Calculate best base, and check that all bases are type objects */
935 base = best_base(bases);
936 if (base == NULL)
937 return NULL;
938 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
939 PyErr_Format(PyExc_TypeError,
940 "type '%.100s' is not an acceptable base type",
941 base->tp_name);
942 return NULL;
943 }
944
Tim Peters6d6c1a32001-08-02 04:15:00 +0000945 /* Check for a __slots__ sequence variable in dict, and count it */
946 slots = PyDict_GetItemString(dict, "__slots__");
947 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000948 add_dict = 0;
949 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950 if (slots != NULL) {
951 /* Make it into a tuple */
952 if (PyString_Check(slots))
953 slots = Py_BuildValue("(O)", slots);
954 else
955 slots = PySequence_Tuple(slots);
956 if (slots == NULL)
957 return NULL;
958 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000959 if (nslots > 0 && base->tp_itemsize != 0) {
960 PyErr_Format(PyExc_TypeError,
961 "nonempty __slots__ "
962 "not supported for subtype of '%s'",
963 base->tp_name);
964 return NULL;
965 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000966 for (i = 0; i < nslots; i++) {
967 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
968 PyErr_SetString(PyExc_TypeError,
969 "__slots__ must be a sequence of strings");
970 Py_DECREF(slots);
971 return NULL;
972 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000973 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000974 }
975 }
976 if (slots == NULL && base->tp_dictoffset == 0 &&
977 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000978 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000979 add_dict++;
980 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000981 if (slots == NULL && base->tp_weaklistoffset == 0 &&
982 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000983 nslots++;
984 add_weak++;
985 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000986
987 /* XXX From here until type is safely allocated,
988 "return NULL" may leak slots! */
989
990 /* Allocate the type object */
991 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
992 if (type == NULL)
993 return NULL;
994
995 /* Keep name and slots alive in the extended type object */
996 et = (etype *)type;
997 Py_INCREF(name);
998 et->name = name;
999 et->slots = slots;
1000
Guido van Rossumdc91b992001-08-08 22:26:22 +00001001 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001002 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1003 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001004 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1005 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001006
1007 /* It's a new-style number unless it specifically inherits any
1008 old-style numeric behavior */
1009 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1010 (base->tp_as_number == NULL))
1011 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1012
1013 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 type->tp_as_number = &et->as_number;
1015 type->tp_as_sequence = &et->as_sequence;
1016 type->tp_as_mapping = &et->as_mapping;
1017 type->tp_as_buffer = &et->as_buffer;
1018 type->tp_name = PyString_AS_STRING(name);
1019
1020 /* Set tp_base and tp_bases */
1021 type->tp_bases = bases;
1022 Py_INCREF(base);
1023 type->tp_base = base;
1024
Guido van Rossum687ae002001-10-15 22:03:32 +00001025 /* Initialize tp_dict from passed-in dict */
1026 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001027 if (dict == NULL) {
1028 Py_DECREF(type);
1029 return NULL;
1030 }
1031
Guido van Rossumc3542212001-08-16 09:18:56 +00001032 /* Set __module__ in the dict */
1033 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1034 tmp = PyEval_GetGlobals();
1035 if (tmp != NULL) {
1036 tmp = PyDict_GetItemString(tmp, "__name__");
1037 if (tmp != NULL) {
1038 if (PyDict_SetItemString(dict, "__module__",
1039 tmp) < 0)
1040 return NULL;
1041 }
1042 }
1043 }
1044
Tim Peters2f93e282001-10-04 05:27:00 +00001045 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00001046 and is a string. Note that the tp_doc slot will only be used
1047 by C code -- python code will use the version in tp_dict, so
1048 it isn't that important that non string __doc__'s are ignored.
Tim Peters2f93e282001-10-04 05:27:00 +00001049 */
1050 {
1051 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1052 if (doc != NULL && PyString_Check(doc)) {
1053 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001054 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001055 if (type->tp_doc == NULL) {
1056 Py_DECREF(type);
1057 return NULL;
1058 }
1059 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1060 }
1061 }
1062
Tim Peters6d6c1a32001-08-02 04:15:00 +00001063 /* Special-case __new__: if it's a plain function,
1064 make it a static function */
1065 tmp = PyDict_GetItemString(dict, "__new__");
1066 if (tmp != NULL && PyFunction_Check(tmp)) {
1067 tmp = PyStaticMethod_New(tmp);
1068 if (tmp == NULL) {
1069 Py_DECREF(type);
1070 return NULL;
1071 }
1072 PyDict_SetItemString(dict, "__new__", tmp);
1073 Py_DECREF(tmp);
1074 }
1075
1076 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1077 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001078 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079 if (slots != NULL) {
1080 for (i = 0; i < nslots; i++, mp++) {
1081 mp->name = PyString_AS_STRING(
1082 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001083 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001084 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001085 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001086 strcmp(mp->name, "__weakref__") == 0) {
1087 mp->type = T_OBJECT;
Guido van Rossum9676b222001-08-17 20:32:36 +00001088 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001089 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001090 slotoffset += sizeof(PyObject *);
1091 }
1092 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001093 else {
1094 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001095 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +00001096 type->tp_dictoffset =
1097 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001098 else
1099 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001100 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001101 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +00001102 }
1103 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001104 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +00001105 type->tp_weaklistoffset = slotoffset;
1106 mp->name = "__weakref__";
1107 mp->type = T_OBJECT;
1108 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +00001109 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001110 mp++;
1111 slotoffset += sizeof(PyObject *);
1112 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001113 }
1114 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001115 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001116 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001117
1118 /* Special case some slots */
1119 if (type->tp_dictoffset != 0 || nslots > 0) {
1120 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1121 type->tp_getattro = PyObject_GenericGetAttr;
1122 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1123 type->tp_setattro = PyObject_GenericSetAttr;
1124 }
1125 type->tp_dealloc = subtype_dealloc;
1126
Guido van Rossum9475a232001-10-05 20:51:39 +00001127 /* Enable GC unless there are really no instance variables possible */
1128 if (!(type->tp_basicsize == sizeof(PyObject) &&
1129 type->tp_itemsize == 0))
1130 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1131
Tim Peters6d6c1a32001-08-02 04:15:00 +00001132 /* Always override allocation strategy to use regular heap */
1133 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001134 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1135 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001136 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +00001137 type->tp_clear = base->tp_clear;
1138 }
1139 else
1140 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141
1142 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001143 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001144 Py_DECREF(type);
1145 return NULL;
1146 }
1147
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001148 /* Put the proper slots in place */
1149 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001150
Tim Peters6d6c1a32001-08-02 04:15:00 +00001151 return (PyObject *)type;
1152}
1153
1154/* Internal API to look for a name through the MRO.
1155 This returns a borrowed reference, and doesn't set an exception! */
1156PyObject *
1157_PyType_Lookup(PyTypeObject *type, PyObject *name)
1158{
1159 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001160 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001161
Guido van Rossum687ae002001-10-15 22:03:32 +00001162 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001163 mro = type->tp_mro;
1164 assert(PyTuple_Check(mro));
1165 n = PyTuple_GET_SIZE(mro);
1166 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001167 base = PyTuple_GET_ITEM(mro, i);
1168 if (PyClass_Check(base))
1169 dict = ((PyClassObject *)base)->cl_dict;
1170 else {
1171 assert(PyType_Check(base));
1172 dict = ((PyTypeObject *)base)->tp_dict;
1173 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001174 assert(dict && PyDict_Check(dict));
1175 res = PyDict_GetItem(dict, name);
1176 if (res != NULL)
1177 return res;
1178 }
1179 return NULL;
1180}
1181
1182/* This is similar to PyObject_GenericGetAttr(),
1183 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1184static PyObject *
1185type_getattro(PyTypeObject *type, PyObject *name)
1186{
1187 PyTypeObject *metatype = type->ob_type;
1188 PyObject *descr, *res;
1189 descrgetfunc f;
1190
1191 /* Initialize this type (we'll assume the metatype is initialized) */
1192 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001193 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001194 return NULL;
1195 }
1196
1197 /* Get a descriptor from the metatype */
1198 descr = _PyType_Lookup(metatype, name);
1199 f = NULL;
1200 if (descr != NULL) {
1201 f = descr->ob_type->tp_descr_get;
1202 if (f != NULL && PyDescr_IsData(descr))
1203 return f(descr,
1204 (PyObject *)type, (PyObject *)metatype);
1205 }
1206
Guido van Rossum687ae002001-10-15 22:03:32 +00001207 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001208 res = _PyType_Lookup(type, name);
1209 if (res != NULL) {
1210 f = res->ob_type->tp_descr_get;
1211 if (f != NULL)
1212 return f(res, (PyObject *)NULL, (PyObject *)type);
1213 Py_INCREF(res);
1214 return res;
1215 }
1216
1217 /* Use the descriptor from the metatype */
1218 if (f != NULL) {
1219 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1220 return res;
1221 }
1222 if (descr != NULL) {
1223 Py_INCREF(descr);
1224 return descr;
1225 }
1226
1227 /* Give up */
1228 PyErr_Format(PyExc_AttributeError,
1229 "type object '%.50s' has no attribute '%.400s'",
1230 type->tp_name, PyString_AS_STRING(name));
1231 return NULL;
1232}
1233
1234static int
1235type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1236{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001237 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1238 PyErr_Format(
1239 PyExc_TypeError,
1240 "can't set attributes of built-in/extension type '%s'",
1241 type->tp_name);
1242 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001243 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001244 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1245 return -1;
1246 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001247}
1248
1249static void
1250type_dealloc(PyTypeObject *type)
1251{
1252 etype *et;
1253
1254 /* Assert this is a heap-allocated type object */
1255 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001256 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001257 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001258 et = (etype *)type;
1259 Py_XDECREF(type->tp_base);
1260 Py_XDECREF(type->tp_dict);
1261 Py_XDECREF(type->tp_bases);
1262 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001263 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001264 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265 Py_XDECREF(et->name);
1266 Py_XDECREF(et->slots);
1267 type->ob_type->tp_free((PyObject *)type);
1268}
1269
Guido van Rossum1c450732001-10-08 15:18:27 +00001270static PyObject *
1271type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1272{
1273 PyObject *list, *raw, *ref;
1274 int i, n;
1275
1276 list = PyList_New(0);
1277 if (list == NULL)
1278 return NULL;
1279 raw = type->tp_subclasses;
1280 if (raw == NULL)
1281 return list;
1282 assert(PyList_Check(raw));
1283 n = PyList_GET_SIZE(raw);
1284 for (i = 0; i < n; i++) {
1285 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001286 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001287 ref = PyWeakref_GET_OBJECT(ref);
1288 if (ref != Py_None) {
1289 if (PyList_Append(list, ref) < 0) {
1290 Py_DECREF(list);
1291 return NULL;
1292 }
1293 }
1294 }
1295 return list;
1296}
1297
Tim Peters6d6c1a32001-08-02 04:15:00 +00001298static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001299 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001300 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001301 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1302 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001303 {0}
1304};
1305
1306static char type_doc[] =
1307"type(object) -> the object's type\n"
1308"type(name, bases, dict) -> a new type";
1309
Guido van Rossum048eb752001-10-02 21:24:57 +00001310static int
1311type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1312{
1313 etype *et;
1314 int err;
1315
1316 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1317 return 0;
1318
1319 et = (etype *)type;
1320
1321#define VISIT(SLOT) \
1322 if (SLOT) { \
1323 err = visit((PyObject *)(SLOT), arg); \
1324 if (err) \
1325 return err; \
1326 }
1327
1328 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001329 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001330 VISIT(type->tp_mro);
1331 VISIT(type->tp_bases);
1332 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001333 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001334 VISIT(et->slots);
1335
1336#undef VISIT
1337
1338 return 0;
1339}
1340
1341static int
1342type_clear(PyTypeObject *type)
1343{
1344 etype *et;
1345 PyObject *tmp;
1346
1347 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1348 return 0;
1349
1350 et = (etype *)type;
1351
1352#define CLEAR(SLOT) \
1353 if (SLOT) { \
1354 tmp = (PyObject *)(SLOT); \
1355 SLOT = NULL; \
1356 Py_DECREF(tmp); \
1357 }
1358
1359 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001360 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001361 CLEAR(type->tp_mro);
1362 CLEAR(type->tp_bases);
1363 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001364 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001365 CLEAR(et->slots);
1366
Tim Peters2f93e282001-10-04 05:27:00 +00001367 if (type->tp_doc != NULL) {
1368 PyObject_FREE(type->tp_doc);
1369 type->tp_doc = NULL;
1370 }
1371
Guido van Rossum048eb752001-10-02 21:24:57 +00001372#undef CLEAR
1373
1374 return 0;
1375}
1376
1377static int
1378type_is_gc(PyTypeObject *type)
1379{
1380 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1381}
1382
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001383PyTypeObject PyType_Type = {
1384 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001385 0, /* ob_size */
1386 "type", /* tp_name */
1387 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001388 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001389 (destructor)type_dealloc, /* tp_dealloc */
1390 0, /* tp_print */
1391 0, /* tp_getattr */
1392 0, /* tp_setattr */
1393 type_compare, /* tp_compare */
1394 (reprfunc)type_repr, /* tp_repr */
1395 0, /* tp_as_number */
1396 0, /* tp_as_sequence */
1397 0, /* tp_as_mapping */
1398 (hashfunc)_Py_HashPointer, /* tp_hash */
1399 (ternaryfunc)type_call, /* tp_call */
1400 0, /* tp_str */
1401 (getattrofunc)type_getattro, /* tp_getattro */
1402 (setattrofunc)type_setattro, /* tp_setattro */
1403 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001404 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1405 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001406 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001407 (traverseproc)type_traverse, /* tp_traverse */
1408 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001409 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001410 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001411 0, /* tp_iter */
1412 0, /* tp_iternext */
1413 type_methods, /* tp_methods */
1414 type_members, /* tp_members */
1415 type_getsets, /* tp_getset */
1416 0, /* tp_base */
1417 0, /* tp_dict */
1418 0, /* tp_descr_get */
1419 0, /* tp_descr_set */
1420 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1421 0, /* tp_init */
1422 0, /* tp_alloc */
1423 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001424 _PyObject_GC_Del, /* tp_free */
1425 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001426};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001427
1428
1429/* The base type of all types (eventually)... except itself. */
1430
1431static int
1432object_init(PyObject *self, PyObject *args, PyObject *kwds)
1433{
1434 return 0;
1435}
1436
1437static void
1438object_dealloc(PyObject *self)
1439{
1440 self->ob_type->tp_free(self);
1441}
1442
Guido van Rossum8e248182001-08-12 05:17:56 +00001443static PyObject *
1444object_repr(PyObject *self)
1445{
Guido van Rossum76e69632001-08-16 18:52:43 +00001446 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001447 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001448
Guido van Rossum76e69632001-08-16 18:52:43 +00001449 type = self->ob_type;
1450 mod = type_module(type, NULL);
1451 if (mod == NULL)
1452 PyErr_Clear();
1453 else if (!PyString_Check(mod)) {
1454 Py_DECREF(mod);
1455 mod = NULL;
1456 }
1457 name = type_name(type, NULL);
1458 if (name == NULL)
1459 return NULL;
1460 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001461 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001462 PyString_AS_STRING(mod),
1463 PyString_AS_STRING(name),
1464 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001465 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001466 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001467 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001468 Py_XDECREF(mod);
1469 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001470 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001471}
1472
Guido van Rossumb8f63662001-08-15 23:57:02 +00001473static PyObject *
1474object_str(PyObject *self)
1475{
1476 unaryfunc f;
1477
1478 f = self->ob_type->tp_repr;
1479 if (f == NULL)
1480 f = object_repr;
1481 return f(self);
1482}
1483
Guido van Rossum8e248182001-08-12 05:17:56 +00001484static long
1485object_hash(PyObject *self)
1486{
1487 return _Py_HashPointer(self);
1488}
Guido van Rossum8e248182001-08-12 05:17:56 +00001489
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001490static PyObject *
1491object_get_class(PyObject *self, void *closure)
1492{
1493 Py_INCREF(self->ob_type);
1494 return (PyObject *)(self->ob_type);
1495}
1496
1497static int
1498equiv_structs(PyTypeObject *a, PyTypeObject *b)
1499{
1500 return a == b ||
1501 (a != NULL &&
1502 b != NULL &&
1503 a->tp_basicsize == b->tp_basicsize &&
1504 a->tp_itemsize == b->tp_itemsize &&
1505 a->tp_dictoffset == b->tp_dictoffset &&
1506 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1507 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1508 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1509}
1510
1511static int
1512same_slots_added(PyTypeObject *a, PyTypeObject *b)
1513{
1514 PyTypeObject *base = a->tp_base;
1515 int size;
1516
1517 if (base != b->tp_base)
1518 return 0;
1519 if (equiv_structs(a, base) && equiv_structs(b, base))
1520 return 1;
1521 size = base->tp_basicsize;
1522 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1523 size += sizeof(PyObject *);
1524 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1525 size += sizeof(PyObject *);
1526 return size == a->tp_basicsize && size == b->tp_basicsize;
1527}
1528
1529static int
1530object_set_class(PyObject *self, PyObject *value, void *closure)
1531{
1532 PyTypeObject *old = self->ob_type;
1533 PyTypeObject *new, *newbase, *oldbase;
1534
1535 if (!PyType_Check(value)) {
1536 PyErr_Format(PyExc_TypeError,
1537 "__class__ must be set to new-style class, not '%s' object",
1538 value->ob_type->tp_name);
1539 return -1;
1540 }
1541 new = (PyTypeObject *)value;
1542 newbase = new;
1543 oldbase = old;
1544 while (equiv_structs(newbase, newbase->tp_base))
1545 newbase = newbase->tp_base;
1546 while (equiv_structs(oldbase, oldbase->tp_base))
1547 oldbase = oldbase->tp_base;
1548 if (newbase != oldbase &&
1549 (newbase->tp_base != oldbase->tp_base ||
1550 !same_slots_added(newbase, oldbase))) {
1551 PyErr_Format(PyExc_TypeError,
1552 "__class__ assignment: "
1553 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001554 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001555 old->tp_name);
1556 return -1;
1557 }
1558 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1559 Py_INCREF(new);
1560 }
1561 self->ob_type = new;
1562 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1563 Py_DECREF(old);
1564 }
1565 return 0;
1566}
1567
1568static PyGetSetDef object_getsets[] = {
1569 {"__class__", object_get_class, object_set_class,
1570 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001571 {0}
1572};
1573
Guido van Rossum3926a632001-09-25 16:25:58 +00001574static PyObject *
1575object_reduce(PyObject *self, PyObject *args)
1576{
1577 /* Call copy_reg._reduce(self) */
1578 static PyObject *copy_reg_str;
1579 PyObject *copy_reg, *res;
1580
1581 if (!copy_reg_str) {
1582 copy_reg_str = PyString_InternFromString("copy_reg");
1583 if (copy_reg_str == NULL)
1584 return NULL;
1585 }
1586 copy_reg = PyImport_Import(copy_reg_str);
1587 if (!copy_reg)
1588 return NULL;
1589 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1590 Py_DECREF(copy_reg);
1591 return res;
1592}
1593
1594static PyMethodDef object_methods[] = {
1595 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1596 {0}
1597};
1598
Tim Peters6d6c1a32001-08-02 04:15:00 +00001599PyTypeObject PyBaseObject_Type = {
1600 PyObject_HEAD_INIT(&PyType_Type)
1601 0, /* ob_size */
1602 "object", /* tp_name */
1603 sizeof(PyObject), /* tp_basicsize */
1604 0, /* tp_itemsize */
1605 (destructor)object_dealloc, /* tp_dealloc */
1606 0, /* tp_print */
1607 0, /* tp_getattr */
1608 0, /* tp_setattr */
1609 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001610 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001611 0, /* tp_as_number */
1612 0, /* tp_as_sequence */
1613 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001614 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001615 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001616 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001617 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001618 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001619 0, /* tp_as_buffer */
1620 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1621 "The most base type", /* tp_doc */
1622 0, /* tp_traverse */
1623 0, /* tp_clear */
1624 0, /* tp_richcompare */
1625 0, /* tp_weaklistoffset */
1626 0, /* tp_iter */
1627 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001628 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001629 0, /* tp_members */
1630 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001631 0, /* tp_base */
1632 0, /* tp_dict */
1633 0, /* tp_descr_get */
1634 0, /* tp_descr_set */
1635 0, /* tp_dictoffset */
1636 object_init, /* tp_init */
1637 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001638 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001639 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001640};
1641
1642
1643/* Initialize the __dict__ in a type object */
1644
1645static int
1646add_methods(PyTypeObject *type, PyMethodDef *meth)
1647{
Guido van Rossum687ae002001-10-15 22:03:32 +00001648 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001649
1650 for (; meth->ml_name != NULL; meth++) {
1651 PyObject *descr;
1652 if (PyDict_GetItemString(dict, meth->ml_name))
1653 continue;
1654 descr = PyDescr_NewMethod(type, meth);
1655 if (descr == NULL)
1656 return -1;
1657 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1658 return -1;
1659 Py_DECREF(descr);
1660 }
1661 return 0;
1662}
1663
1664static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001665add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001666{
Guido van Rossum687ae002001-10-15 22:03:32 +00001667 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001668
1669 for (; memb->name != NULL; memb++) {
1670 PyObject *descr;
1671 if (PyDict_GetItemString(dict, memb->name))
1672 continue;
1673 descr = PyDescr_NewMember(type, memb);
1674 if (descr == NULL)
1675 return -1;
1676 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1677 return -1;
1678 Py_DECREF(descr);
1679 }
1680 return 0;
1681}
1682
1683static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001684add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001685{
Guido van Rossum687ae002001-10-15 22:03:32 +00001686 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001687
1688 for (; gsp->name != NULL; gsp++) {
1689 PyObject *descr;
1690 if (PyDict_GetItemString(dict, gsp->name))
1691 continue;
1692 descr = PyDescr_NewGetSet(type, gsp);
1693
1694 if (descr == NULL)
1695 return -1;
1696 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1697 return -1;
1698 Py_DECREF(descr);
1699 }
1700 return 0;
1701}
1702
Guido van Rossum13d52f02001-08-10 21:24:08 +00001703static void
1704inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001705{
1706 int oldsize, newsize;
1707
Guido van Rossum13d52f02001-08-10 21:24:08 +00001708 /* Special flag magic */
1709 if (!type->tp_as_buffer && base->tp_as_buffer) {
1710 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1711 type->tp_flags |=
1712 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1713 }
1714 if (!type->tp_as_sequence && base->tp_as_sequence) {
1715 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1716 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1717 }
1718 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1719 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1720 if ((!type->tp_as_number && base->tp_as_number) ||
1721 (!type->tp_as_sequence && base->tp_as_sequence)) {
1722 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1723 if (!type->tp_as_number && !type->tp_as_sequence) {
1724 type->tp_flags |= base->tp_flags &
1725 Py_TPFLAGS_HAVE_INPLACEOPS;
1726 }
1727 }
1728 /* Wow */
1729 }
1730 if (!type->tp_as_number && base->tp_as_number) {
1731 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1732 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1733 }
1734
1735 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001736 oldsize = base->tp_basicsize;
1737 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1738 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1739 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001740 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1741 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001742 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001743 if (type->tp_traverse == NULL)
1744 type->tp_traverse = base->tp_traverse;
1745 if (type->tp_clear == NULL)
1746 type->tp_clear = base->tp_clear;
1747 }
1748 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001749 /* The condition below could use some explanation.
1750 It appears that tp_new is not inherited for static types
1751 whose base class is 'object'; this seems to be a precaution
1752 so that old extension types don't suddenly become
1753 callable (object.__new__ wouldn't insure the invariants
1754 that the extension type's own factory function ensures).
1755 Heap types, of course, are under our control, so they do
1756 inherit tp_new; static extension types that specify some
1757 other built-in type as the default are considered
1758 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001759 if (base != &PyBaseObject_Type ||
1760 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1761 if (type->tp_new == NULL)
1762 type->tp_new = base->tp_new;
1763 }
1764 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001765 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001766
1767 /* Copy other non-function slots */
1768
1769#undef COPYVAL
1770#define COPYVAL(SLOT) \
1771 if (type->SLOT == 0) type->SLOT = base->SLOT
1772
1773 COPYVAL(tp_itemsize);
1774 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1775 COPYVAL(tp_weaklistoffset);
1776 }
1777 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1778 COPYVAL(tp_dictoffset);
1779 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001780}
1781
1782static void
1783inherit_slots(PyTypeObject *type, PyTypeObject *base)
1784{
1785 PyTypeObject *basebase;
1786
1787#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001788#undef COPYSLOT
1789#undef COPYNUM
1790#undef COPYSEQ
1791#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001792#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001793
1794#define SLOTDEFINED(SLOT) \
1795 (base->SLOT != 0 && \
1796 (basebase == NULL || base->SLOT != basebase->SLOT))
1797
Tim Peters6d6c1a32001-08-02 04:15:00 +00001798#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001799 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001800
1801#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1802#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1803#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001804#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001805
Guido van Rossum13d52f02001-08-10 21:24:08 +00001806 /* This won't inherit indirect slots (from tp_as_number etc.)
1807 if type doesn't provide the space. */
1808
1809 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1810 basebase = base->tp_base;
1811 if (basebase->tp_as_number == NULL)
1812 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001813 COPYNUM(nb_add);
1814 COPYNUM(nb_subtract);
1815 COPYNUM(nb_multiply);
1816 COPYNUM(nb_divide);
1817 COPYNUM(nb_remainder);
1818 COPYNUM(nb_divmod);
1819 COPYNUM(nb_power);
1820 COPYNUM(nb_negative);
1821 COPYNUM(nb_positive);
1822 COPYNUM(nb_absolute);
1823 COPYNUM(nb_nonzero);
1824 COPYNUM(nb_invert);
1825 COPYNUM(nb_lshift);
1826 COPYNUM(nb_rshift);
1827 COPYNUM(nb_and);
1828 COPYNUM(nb_xor);
1829 COPYNUM(nb_or);
1830 COPYNUM(nb_coerce);
1831 COPYNUM(nb_int);
1832 COPYNUM(nb_long);
1833 COPYNUM(nb_float);
1834 COPYNUM(nb_oct);
1835 COPYNUM(nb_hex);
1836 COPYNUM(nb_inplace_add);
1837 COPYNUM(nb_inplace_subtract);
1838 COPYNUM(nb_inplace_multiply);
1839 COPYNUM(nb_inplace_divide);
1840 COPYNUM(nb_inplace_remainder);
1841 COPYNUM(nb_inplace_power);
1842 COPYNUM(nb_inplace_lshift);
1843 COPYNUM(nb_inplace_rshift);
1844 COPYNUM(nb_inplace_and);
1845 COPYNUM(nb_inplace_xor);
1846 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001847 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1848 COPYNUM(nb_true_divide);
1849 COPYNUM(nb_floor_divide);
1850 COPYNUM(nb_inplace_true_divide);
1851 COPYNUM(nb_inplace_floor_divide);
1852 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001853 }
1854
Guido van Rossum13d52f02001-08-10 21:24:08 +00001855 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1856 basebase = base->tp_base;
1857 if (basebase->tp_as_sequence == NULL)
1858 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001859 COPYSEQ(sq_length);
1860 COPYSEQ(sq_concat);
1861 COPYSEQ(sq_repeat);
1862 COPYSEQ(sq_item);
1863 COPYSEQ(sq_slice);
1864 COPYSEQ(sq_ass_item);
1865 COPYSEQ(sq_ass_slice);
1866 COPYSEQ(sq_contains);
1867 COPYSEQ(sq_inplace_concat);
1868 COPYSEQ(sq_inplace_repeat);
1869 }
1870
Guido van Rossum13d52f02001-08-10 21:24:08 +00001871 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1872 basebase = base->tp_base;
1873 if (basebase->tp_as_mapping == NULL)
1874 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001875 COPYMAP(mp_length);
1876 COPYMAP(mp_subscript);
1877 COPYMAP(mp_ass_subscript);
1878 }
1879
Tim Petersfc57ccb2001-10-12 02:38:24 +00001880 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1881 basebase = base->tp_base;
1882 if (basebase->tp_as_buffer == NULL)
1883 basebase = NULL;
1884 COPYBUF(bf_getreadbuffer);
1885 COPYBUF(bf_getwritebuffer);
1886 COPYBUF(bf_getsegcount);
1887 COPYBUF(bf_getcharbuffer);
1888 }
1889
Guido van Rossum13d52f02001-08-10 21:24:08 +00001890 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001891
Tim Peters6d6c1a32001-08-02 04:15:00 +00001892 COPYSLOT(tp_dealloc);
1893 COPYSLOT(tp_print);
1894 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1895 type->tp_getattr = base->tp_getattr;
1896 type->tp_getattro = base->tp_getattro;
1897 }
1898 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1899 type->tp_setattr = base->tp_setattr;
1900 type->tp_setattro = base->tp_setattro;
1901 }
1902 /* tp_compare see tp_richcompare */
1903 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001904 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001905 COPYSLOT(tp_call);
1906 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001907 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001908 if (type->tp_compare == NULL &&
1909 type->tp_richcompare == NULL &&
1910 type->tp_hash == NULL)
1911 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001912 type->tp_compare = base->tp_compare;
1913 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001914 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001915 }
1916 }
1917 else {
1918 COPYSLOT(tp_compare);
1919 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001920 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1921 COPYSLOT(tp_iter);
1922 COPYSLOT(tp_iternext);
1923 }
1924 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1925 COPYSLOT(tp_descr_get);
1926 COPYSLOT(tp_descr_set);
1927 COPYSLOT(tp_dictoffset);
1928 COPYSLOT(tp_init);
1929 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001930 COPYSLOT(tp_free);
1931 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001932}
1933
Guido van Rossum13d52f02001-08-10 21:24:08 +00001934staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00001935staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001936
Tim Peters6d6c1a32001-08-02 04:15:00 +00001937int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001938PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001939{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001940 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001941 PyTypeObject *base;
1942 int i, n;
1943
Guido van Rossumd614f972001-08-10 17:39:49 +00001944 if (type->tp_flags & Py_TPFLAGS_READY) {
1945 assert(type->tp_dict != NULL);
1946 return 0;
1947 }
1948 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00001949
1950 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001951
Guido van Rossumf884b742001-12-17 17:14:22 +00001952 /* Initialize ob_type if NULL. This means extensions that want to be
1953 compilable separately on Windows can call PyType_Ready() instead of
1954 initializing the ob_type field of their type objects. */
1955 if (type->ob_type == NULL)
1956 type->ob_type = &PyType_Type;
1957
Tim Peters6d6c1a32001-08-02 04:15:00 +00001958 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1959 base = type->tp_base;
1960 if (base == NULL && type != &PyBaseObject_Type)
1961 base = type->tp_base = &PyBaseObject_Type;
1962
1963 /* Initialize tp_bases */
1964 bases = type->tp_bases;
1965 if (bases == NULL) {
1966 if (base == NULL)
1967 bases = PyTuple_New(0);
1968 else
1969 bases = Py_BuildValue("(O)", base);
1970 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001971 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001972 type->tp_bases = bases;
1973 }
1974
1975 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001976 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001977 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001978 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001979 }
1980
Guido van Rossum687ae002001-10-15 22:03:32 +00001981 /* Initialize tp_dict */
1982 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001983 if (dict == NULL) {
1984 dict = PyDict_New();
1985 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001986 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00001987 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001988 }
1989
Guido van Rossum687ae002001-10-15 22:03:32 +00001990 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001991 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001992 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001993 if (type->tp_methods != NULL) {
1994 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001995 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001996 }
1997 if (type->tp_members != NULL) {
1998 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001999 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002000 }
2001 if (type->tp_getset != NULL) {
2002 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002003 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002004 }
2005
Tim Peters6d6c1a32001-08-02 04:15:00 +00002006 /* Calculate method resolution order */
2007 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002008 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002009 }
2010
Guido van Rossum13d52f02001-08-10 21:24:08 +00002011 /* Inherit special flags from dominant base */
2012 if (type->tp_base != NULL)
2013 inherit_special(type, type->tp_base);
2014
Tim Peters6d6c1a32001-08-02 04:15:00 +00002015 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002016 bases = type->tp_mro;
2017 assert(bases != NULL);
2018 assert(PyTuple_Check(bases));
2019 n = PyTuple_GET_SIZE(bases);
2020 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002021 PyObject *b = PyTuple_GET_ITEM(bases, i);
2022 if (PyType_Check(b))
2023 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002024 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002025
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002026 /* if the type dictionary doesn't contain a __doc__, set it from
2027 the tp_doc slot.
2028 */
2029 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2030 if (type->tp_doc != NULL) {
2031 PyObject *doc = PyString_FromString(type->tp_doc);
2032 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2033 Py_DECREF(doc);
2034 } else {
2035 PyDict_SetItemString(type->tp_dict, "__doc__", Py_None);
2036 }
2037 }
2038
Guido van Rossum13d52f02001-08-10 21:24:08 +00002039 /* Some more special stuff */
2040 base = type->tp_base;
2041 if (base != NULL) {
2042 if (type->tp_as_number == NULL)
2043 type->tp_as_number = base->tp_as_number;
2044 if (type->tp_as_sequence == NULL)
2045 type->tp_as_sequence = base->tp_as_sequence;
2046 if (type->tp_as_mapping == NULL)
2047 type->tp_as_mapping = base->tp_as_mapping;
2048 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002049
Guido van Rossum1c450732001-10-08 15:18:27 +00002050 /* Link into each base class's list of subclasses */
2051 bases = type->tp_bases;
2052 n = PyTuple_GET_SIZE(bases);
2053 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002054 PyObject *b = PyTuple_GET_ITEM(bases, i);
2055 if (PyType_Check(b) &&
2056 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002057 goto error;
2058 }
2059
Guido van Rossum13d52f02001-08-10 21:24:08 +00002060 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002061 assert(type->tp_dict != NULL);
2062 type->tp_flags =
2063 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002064 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002065
2066 error:
2067 type->tp_flags &= ~Py_TPFLAGS_READYING;
2068 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069}
2070
Guido van Rossum1c450732001-10-08 15:18:27 +00002071static int
2072add_subclass(PyTypeObject *base, PyTypeObject *type)
2073{
2074 int i;
2075 PyObject *list, *ref, *new;
2076
2077 list = base->tp_subclasses;
2078 if (list == NULL) {
2079 base->tp_subclasses = list = PyList_New(0);
2080 if (list == NULL)
2081 return -1;
2082 }
2083 assert(PyList_Check(list));
2084 new = PyWeakref_NewRef((PyObject *)type, NULL);
2085 i = PyList_GET_SIZE(list);
2086 while (--i >= 0) {
2087 ref = PyList_GET_ITEM(list, i);
2088 assert(PyWeakref_CheckRef(ref));
2089 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2090 return PyList_SetItem(list, i, new);
2091 }
2092 i = PyList_Append(list, new);
2093 Py_DECREF(new);
2094 return i;
2095}
2096
Tim Peters6d6c1a32001-08-02 04:15:00 +00002097
2098/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2099
2100/* There's a wrapper *function* for each distinct function typedef used
2101 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2102 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2103 Most tables have only one entry; the tables for binary operators have two
2104 entries, one regular and one with reversed arguments. */
2105
2106static PyObject *
2107wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2108{
2109 inquiry func = (inquiry)wrapped;
2110 int res;
2111
2112 if (!PyArg_ParseTuple(args, ""))
2113 return NULL;
2114 res = (*func)(self);
2115 if (res == -1 && PyErr_Occurred())
2116 return NULL;
2117 return PyInt_FromLong((long)res);
2118}
2119
Tim Peters6d6c1a32001-08-02 04:15:00 +00002120static PyObject *
2121wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2122{
2123 binaryfunc func = (binaryfunc)wrapped;
2124 PyObject *other;
2125
2126 if (!PyArg_ParseTuple(args, "O", &other))
2127 return NULL;
2128 return (*func)(self, other);
2129}
2130
2131static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002132wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2133{
2134 binaryfunc func = (binaryfunc)wrapped;
2135 PyObject *other;
2136
2137 if (!PyArg_ParseTuple(args, "O", &other))
2138 return NULL;
2139 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002140 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002141 Py_INCREF(Py_NotImplemented);
2142 return Py_NotImplemented;
2143 }
2144 return (*func)(self, other);
2145}
2146
2147static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002148wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2149{
2150 binaryfunc func = (binaryfunc)wrapped;
2151 PyObject *other;
2152
2153 if (!PyArg_ParseTuple(args, "O", &other))
2154 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002155 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002156 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002157 Py_INCREF(Py_NotImplemented);
2158 return Py_NotImplemented;
2159 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002160 return (*func)(other, self);
2161}
2162
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002163static PyObject *
2164wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2165{
2166 coercion func = (coercion)wrapped;
2167 PyObject *other, *res;
2168 int ok;
2169
2170 if (!PyArg_ParseTuple(args, "O", &other))
2171 return NULL;
2172 ok = func(&self, &other);
2173 if (ok < 0)
2174 return NULL;
2175 if (ok > 0) {
2176 Py_INCREF(Py_NotImplemented);
2177 return Py_NotImplemented;
2178 }
2179 res = PyTuple_New(2);
2180 if (res == NULL) {
2181 Py_DECREF(self);
2182 Py_DECREF(other);
2183 return NULL;
2184 }
2185 PyTuple_SET_ITEM(res, 0, self);
2186 PyTuple_SET_ITEM(res, 1, other);
2187 return res;
2188}
2189
Tim Peters6d6c1a32001-08-02 04:15:00 +00002190static PyObject *
2191wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2192{
2193 ternaryfunc func = (ternaryfunc)wrapped;
2194 PyObject *other;
2195 PyObject *third = Py_None;
2196
2197 /* Note: This wrapper only works for __pow__() */
2198
2199 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2200 return NULL;
2201 return (*func)(self, other, third);
2202}
2203
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002204static PyObject *
2205wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2206{
2207 ternaryfunc func = (ternaryfunc)wrapped;
2208 PyObject *other;
2209 PyObject *third = Py_None;
2210
2211 /* Note: This wrapper only works for __pow__() */
2212
2213 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2214 return NULL;
2215 return (*func)(other, self, third);
2216}
2217
Tim Peters6d6c1a32001-08-02 04:15:00 +00002218static PyObject *
2219wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2220{
2221 unaryfunc func = (unaryfunc)wrapped;
2222
2223 if (!PyArg_ParseTuple(args, ""))
2224 return NULL;
2225 return (*func)(self);
2226}
2227
Tim Peters6d6c1a32001-08-02 04:15:00 +00002228static PyObject *
2229wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2230{
2231 intargfunc func = (intargfunc)wrapped;
2232 int i;
2233
2234 if (!PyArg_ParseTuple(args, "i", &i))
2235 return NULL;
2236 return (*func)(self, i);
2237}
2238
Guido van Rossum5d815f32001-08-17 21:57:47 +00002239static int
2240getindex(PyObject *self, PyObject *arg)
2241{
2242 int i;
2243
2244 i = PyInt_AsLong(arg);
2245 if (i == -1 && PyErr_Occurred())
2246 return -1;
2247 if (i < 0) {
2248 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2249 if (sq && sq->sq_length) {
2250 int n = (*sq->sq_length)(self);
2251 if (n < 0)
2252 return -1;
2253 i += n;
2254 }
2255 }
2256 return i;
2257}
2258
2259static PyObject *
2260wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2261{
2262 intargfunc func = (intargfunc)wrapped;
2263 PyObject *arg;
2264 int i;
2265
Guido van Rossumf4593e02001-10-03 12:09:30 +00002266 if (PyTuple_GET_SIZE(args) == 1) {
2267 arg = PyTuple_GET_ITEM(args, 0);
2268 i = getindex(self, arg);
2269 if (i == -1 && PyErr_Occurred())
2270 return NULL;
2271 return (*func)(self, i);
2272 }
2273 PyArg_ParseTuple(args, "O", &arg);
2274 assert(PyErr_Occurred());
2275 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002276}
2277
Tim Peters6d6c1a32001-08-02 04:15:00 +00002278static PyObject *
2279wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2280{
2281 intintargfunc func = (intintargfunc)wrapped;
2282 int i, j;
2283
2284 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2285 return NULL;
2286 return (*func)(self, i, j);
2287}
2288
Tim Peters6d6c1a32001-08-02 04:15:00 +00002289static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002290wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002291{
2292 intobjargproc func = (intobjargproc)wrapped;
2293 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002294 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002295
Guido van Rossum5d815f32001-08-17 21:57:47 +00002296 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2297 return NULL;
2298 i = getindex(self, arg);
2299 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002300 return NULL;
2301 res = (*func)(self, i, value);
2302 if (res == -1 && PyErr_Occurred())
2303 return NULL;
2304 Py_INCREF(Py_None);
2305 return Py_None;
2306}
2307
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002308static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002309wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002310{
2311 intobjargproc func = (intobjargproc)wrapped;
2312 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002313 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002314
Guido van Rossum5d815f32001-08-17 21:57:47 +00002315 if (!PyArg_ParseTuple(args, "O", &arg))
2316 return NULL;
2317 i = getindex(self, arg);
2318 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002319 return NULL;
2320 res = (*func)(self, i, NULL);
2321 if (res == -1 && PyErr_Occurred())
2322 return NULL;
2323 Py_INCREF(Py_None);
2324 return Py_None;
2325}
2326
Tim Peters6d6c1a32001-08-02 04:15:00 +00002327static PyObject *
2328wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2329{
2330 intintobjargproc func = (intintobjargproc)wrapped;
2331 int i, j, res;
2332 PyObject *value;
2333
2334 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2335 return NULL;
2336 res = (*func)(self, i, j, value);
2337 if (res == -1 && PyErr_Occurred())
2338 return NULL;
2339 Py_INCREF(Py_None);
2340 return Py_None;
2341}
2342
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002343static PyObject *
2344wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2345{
2346 intintobjargproc func = (intintobjargproc)wrapped;
2347 int i, j, res;
2348
2349 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2350 return NULL;
2351 res = (*func)(self, i, j, NULL);
2352 if (res == -1 && PyErr_Occurred())
2353 return NULL;
2354 Py_INCREF(Py_None);
2355 return Py_None;
2356}
2357
Tim Peters6d6c1a32001-08-02 04:15:00 +00002358/* XXX objobjproc is a misnomer; should be objargpred */
2359static PyObject *
2360wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2361{
2362 objobjproc func = (objobjproc)wrapped;
2363 int res;
2364 PyObject *value;
2365
2366 if (!PyArg_ParseTuple(args, "O", &value))
2367 return NULL;
2368 res = (*func)(self, value);
2369 if (res == -1 && PyErr_Occurred())
2370 return NULL;
2371 return PyInt_FromLong((long)res);
2372}
2373
Tim Peters6d6c1a32001-08-02 04:15:00 +00002374static PyObject *
2375wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2376{
2377 objobjargproc func = (objobjargproc)wrapped;
2378 int res;
2379 PyObject *key, *value;
2380
2381 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2382 return NULL;
2383 res = (*func)(self, key, value);
2384 if (res == -1 && PyErr_Occurred())
2385 return NULL;
2386 Py_INCREF(Py_None);
2387 return Py_None;
2388}
2389
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002390static PyObject *
2391wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2392{
2393 objobjargproc func = (objobjargproc)wrapped;
2394 int res;
2395 PyObject *key;
2396
2397 if (!PyArg_ParseTuple(args, "O", &key))
2398 return NULL;
2399 res = (*func)(self, key, NULL);
2400 if (res == -1 && PyErr_Occurred())
2401 return NULL;
2402 Py_INCREF(Py_None);
2403 return Py_None;
2404}
2405
Tim Peters6d6c1a32001-08-02 04:15:00 +00002406static PyObject *
2407wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2408{
2409 cmpfunc func = (cmpfunc)wrapped;
2410 int res;
2411 PyObject *other;
2412
2413 if (!PyArg_ParseTuple(args, "O", &other))
2414 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002415 if (other->ob_type->tp_compare != func &&
2416 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002417 PyErr_Format(
2418 PyExc_TypeError,
2419 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2420 self->ob_type->tp_name,
2421 self->ob_type->tp_name,
2422 other->ob_type->tp_name);
2423 return NULL;
2424 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002425 res = (*func)(self, other);
2426 if (PyErr_Occurred())
2427 return NULL;
2428 return PyInt_FromLong((long)res);
2429}
2430
Tim Peters6d6c1a32001-08-02 04:15:00 +00002431static PyObject *
2432wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2433{
2434 setattrofunc func = (setattrofunc)wrapped;
2435 int res;
2436 PyObject *name, *value;
2437
2438 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2439 return NULL;
2440 res = (*func)(self, name, value);
2441 if (res < 0)
2442 return NULL;
2443 Py_INCREF(Py_None);
2444 return Py_None;
2445}
2446
2447static PyObject *
2448wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2449{
2450 setattrofunc func = (setattrofunc)wrapped;
2451 int res;
2452 PyObject *name;
2453
2454 if (!PyArg_ParseTuple(args, "O", &name))
2455 return NULL;
2456 res = (*func)(self, name, NULL);
2457 if (res < 0)
2458 return NULL;
2459 Py_INCREF(Py_None);
2460 return Py_None;
2461}
2462
Tim Peters6d6c1a32001-08-02 04:15:00 +00002463static PyObject *
2464wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2465{
2466 hashfunc func = (hashfunc)wrapped;
2467 long res;
2468
2469 if (!PyArg_ParseTuple(args, ""))
2470 return NULL;
2471 res = (*func)(self);
2472 if (res == -1 && PyErr_Occurred())
2473 return NULL;
2474 return PyInt_FromLong(res);
2475}
2476
Tim Peters6d6c1a32001-08-02 04:15:00 +00002477static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002478wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002479{
2480 ternaryfunc func = (ternaryfunc)wrapped;
2481
Guido van Rossumc8e56452001-10-22 00:43:43 +00002482 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002483}
2484
Tim Peters6d6c1a32001-08-02 04:15:00 +00002485static PyObject *
2486wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2487{
2488 richcmpfunc func = (richcmpfunc)wrapped;
2489 PyObject *other;
2490
2491 if (!PyArg_ParseTuple(args, "O", &other))
2492 return NULL;
2493 return (*func)(self, other, op);
2494}
2495
2496#undef RICHCMP_WRAPPER
2497#define RICHCMP_WRAPPER(NAME, OP) \
2498static PyObject * \
2499richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2500{ \
2501 return wrap_richcmpfunc(self, args, wrapped, OP); \
2502}
2503
Jack Jansen8e938b42001-08-08 15:29:49 +00002504RICHCMP_WRAPPER(lt, Py_LT)
2505RICHCMP_WRAPPER(le, Py_LE)
2506RICHCMP_WRAPPER(eq, Py_EQ)
2507RICHCMP_WRAPPER(ne, Py_NE)
2508RICHCMP_WRAPPER(gt, Py_GT)
2509RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510
Tim Peters6d6c1a32001-08-02 04:15:00 +00002511static PyObject *
2512wrap_next(PyObject *self, PyObject *args, void *wrapped)
2513{
2514 unaryfunc func = (unaryfunc)wrapped;
2515 PyObject *res;
2516
2517 if (!PyArg_ParseTuple(args, ""))
2518 return NULL;
2519 res = (*func)(self);
2520 if (res == NULL && !PyErr_Occurred())
2521 PyErr_SetNone(PyExc_StopIteration);
2522 return res;
2523}
2524
Tim Peters6d6c1a32001-08-02 04:15:00 +00002525static PyObject *
2526wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2527{
2528 descrgetfunc func = (descrgetfunc)wrapped;
2529 PyObject *obj;
2530 PyObject *type = NULL;
2531
2532 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2533 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002534 return (*func)(self, obj, type);
2535}
2536
Tim Peters6d6c1a32001-08-02 04:15:00 +00002537static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002538wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002539{
2540 descrsetfunc func = (descrsetfunc)wrapped;
2541 PyObject *obj, *value;
2542 int ret;
2543
2544 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2545 return NULL;
2546 ret = (*func)(self, obj, value);
2547 if (ret < 0)
2548 return NULL;
2549 Py_INCREF(Py_None);
2550 return Py_None;
2551}
2552
Tim Peters6d6c1a32001-08-02 04:15:00 +00002553static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002554wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002555{
2556 initproc func = (initproc)wrapped;
2557
Guido van Rossumc8e56452001-10-22 00:43:43 +00002558 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002559 return NULL;
2560 Py_INCREF(Py_None);
2561 return Py_None;
2562}
2563
Tim Peters6d6c1a32001-08-02 04:15:00 +00002564static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002565tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002566{
Barry Warsaw60f01882001-08-22 19:24:42 +00002567 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002568 PyObject *arg0, *res;
2569
2570 if (self == NULL || !PyType_Check(self))
2571 Py_FatalError("__new__() called with non-type 'self'");
2572 type = (PyTypeObject *)self;
2573 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002574 PyErr_Format(PyExc_TypeError,
2575 "%s.__new__(): not enough arguments",
2576 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002577 return NULL;
2578 }
2579 arg0 = PyTuple_GET_ITEM(args, 0);
2580 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002581 PyErr_Format(PyExc_TypeError,
2582 "%s.__new__(X): X is not a type object (%s)",
2583 type->tp_name,
2584 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002585 return NULL;
2586 }
2587 subtype = (PyTypeObject *)arg0;
2588 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002589 PyErr_Format(PyExc_TypeError,
2590 "%s.__new__(%s): %s is not a subtype of %s",
2591 type->tp_name,
2592 subtype->tp_name,
2593 subtype->tp_name,
2594 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002595 return NULL;
2596 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002597
2598 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002599 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002600 most derived base that's not a heap type is this type. */
2601 staticbase = subtype;
2602 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2603 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002604 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002605 PyErr_Format(PyExc_TypeError,
2606 "%s.__new__(%s) is not safe, use %s.__new__()",
2607 type->tp_name,
2608 subtype->tp_name,
2609 staticbase == NULL ? "?" : staticbase->tp_name);
2610 return NULL;
2611 }
2612
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002613 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2614 if (args == NULL)
2615 return NULL;
2616 res = type->tp_new(subtype, args, kwds);
2617 Py_DECREF(args);
2618 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002619}
2620
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002621static struct PyMethodDef tp_new_methoddef[] = {
2622 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2623 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002624 {0}
2625};
2626
2627static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002628add_tp_new_wrapper(PyTypeObject *type)
2629{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002630 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002631
Guido van Rossum687ae002001-10-15 22:03:32 +00002632 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002633 return 0;
2634 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002635 if (func == NULL)
2636 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002637 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002638}
2639
Guido van Rossumf040ede2001-08-07 16:40:56 +00002640/* Slot wrappers that call the corresponding __foo__ slot. See comments
2641 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642
Guido van Rossumdc91b992001-08-08 22:26:22 +00002643#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002644static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002645FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002646{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002647 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002648 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002649}
2650
Guido van Rossumdc91b992001-08-08 22:26:22 +00002651#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002652static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002653FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002654{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002655 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002656 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002657}
2658
Guido van Rossumdc91b992001-08-08 22:26:22 +00002659
2660#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002661static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002662FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002663{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002664 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002665 int do_other = self->ob_type != other->ob_type && \
2666 other->ob_type->tp_as_number != NULL && \
2667 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002668 if (self->ob_type->tp_as_number != NULL && \
2669 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2670 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002671 if (do_other && \
2672 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2673 r = call_maybe( \
2674 other, ROPSTR, &rcache_str, "(O)", self); \
2675 if (r != Py_NotImplemented) \
2676 return r; \
2677 Py_DECREF(r); \
2678 do_other = 0; \
2679 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002680 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002681 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002682 if (r != Py_NotImplemented || \
2683 other->ob_type == self->ob_type) \
2684 return r; \
2685 Py_DECREF(r); \
2686 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002687 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002688 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002689 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002690 } \
2691 Py_INCREF(Py_NotImplemented); \
2692 return Py_NotImplemented; \
2693}
2694
2695#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2696 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2697
2698#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2699static PyObject * \
2700FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2701{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002702 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002703 return call_method(self, OPSTR, &cache_str, \
2704 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002705}
2706
2707static int
2708slot_sq_length(PyObject *self)
2709{
Guido van Rossum2730b132001-08-28 18:22:14 +00002710 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002711 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002712 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002713
2714 if (res == NULL)
2715 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002716 len = (int)PyInt_AsLong(res);
2717 Py_DECREF(res);
2718 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002719}
2720
Guido van Rossumdc91b992001-08-08 22:26:22 +00002721SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2722SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002723
2724/* Super-optimized version of slot_sq_item.
2725 Other slots could do the same... */
2726static PyObject *
2727slot_sq_item(PyObject *self, int i)
2728{
2729 static PyObject *getitem_str;
2730 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2731 descrgetfunc f;
2732
2733 if (getitem_str == NULL) {
2734 getitem_str = PyString_InternFromString("__getitem__");
2735 if (getitem_str == NULL)
2736 return NULL;
2737 }
2738 func = _PyType_Lookup(self->ob_type, getitem_str);
2739 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002740 if ((f = func->ob_type->tp_descr_get) == NULL)
2741 Py_INCREF(func);
2742 else
2743 func = f(func, self, (PyObject *)(self->ob_type));
2744 ival = PyInt_FromLong(i);
2745 if (ival != NULL) {
2746 args = PyTuple_New(1);
2747 if (args != NULL) {
2748 PyTuple_SET_ITEM(args, 0, ival);
2749 retval = PyObject_Call(func, args, NULL);
2750 Py_XDECREF(args);
2751 Py_XDECREF(func);
2752 return retval;
2753 }
2754 }
2755 }
2756 else {
2757 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2758 }
2759 Py_XDECREF(args);
2760 Py_XDECREF(ival);
2761 Py_XDECREF(func);
2762 return NULL;
2763}
2764
Guido van Rossumdc91b992001-08-08 22:26:22 +00002765SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002766
2767static int
2768slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2769{
2770 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002771 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772
2773 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002774 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002775 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002776 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002777 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002778 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002779 if (res == NULL)
2780 return -1;
2781 Py_DECREF(res);
2782 return 0;
2783}
2784
2785static int
2786slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2787{
2788 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002789 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002790
2791 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002792 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002793 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002794 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002795 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002796 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002797 if (res == NULL)
2798 return -1;
2799 Py_DECREF(res);
2800 return 0;
2801}
2802
2803static int
2804slot_sq_contains(PyObject *self, PyObject *value)
2805{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002806 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002807 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002808
Guido van Rossum55f20992001-10-01 17:18:22 +00002809 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002810
2811 if (func != NULL) {
2812 args = Py_BuildValue("(O)", value);
2813 if (args == NULL)
2814 res = NULL;
2815 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002816 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002817 Py_DECREF(args);
2818 }
2819 Py_DECREF(func);
2820 if (res == NULL)
2821 return -1;
2822 return PyObject_IsTrue(res);
2823 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002824 else if (PyErr_Occurred())
2825 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002826 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002827 return _PySequence_IterSearch(self, value,
2828 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002829 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002830}
2831
Guido van Rossumdc91b992001-08-08 22:26:22 +00002832SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2833SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002834
2835#define slot_mp_length slot_sq_length
2836
Guido van Rossumdc91b992001-08-08 22:26:22 +00002837SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002838
2839static int
2840slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2841{
2842 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002843 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002844
2845 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002846 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002847 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002848 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002849 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002850 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002851 if (res == NULL)
2852 return -1;
2853 Py_DECREF(res);
2854 return 0;
2855}
2856
Guido van Rossumdc91b992001-08-08 22:26:22 +00002857SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2858SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2859SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2860SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2861SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2862SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2863
2864staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2865
2866SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2867 nb_power, "__pow__", "__rpow__")
2868
2869static PyObject *
2870slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2871{
Guido van Rossum2730b132001-08-28 18:22:14 +00002872 static PyObject *pow_str;
2873
Guido van Rossumdc91b992001-08-08 22:26:22 +00002874 if (modulus == Py_None)
2875 return slot_nb_power_binary(self, other);
2876 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002877 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002878 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002879}
2880
2881SLOT0(slot_nb_negative, "__neg__")
2882SLOT0(slot_nb_positive, "__pos__")
2883SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002884
2885static int
2886slot_nb_nonzero(PyObject *self)
2887{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002888 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002889 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002890
Guido van Rossum55f20992001-10-01 17:18:22 +00002891 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002892 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002893 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002894 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002895 func = lookup_maybe(self, "__len__", &len_str);
2896 if (func == NULL) {
2897 if (PyErr_Occurred())
2898 return -1;
2899 else
2900 return 1;
2901 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002902 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002903 res = PyObject_CallObject(func, NULL);
2904 Py_DECREF(func);
2905 if (res == NULL)
2906 return -1;
2907 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002908}
2909
Guido van Rossumdc91b992001-08-08 22:26:22 +00002910SLOT0(slot_nb_invert, "__invert__")
2911SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2912SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2913SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2914SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2915SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002916
2917static int
2918slot_nb_coerce(PyObject **a, PyObject **b)
2919{
2920 static PyObject *coerce_str;
2921 PyObject *self = *a, *other = *b;
2922
2923 if (self->ob_type->tp_as_number != NULL &&
2924 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2925 PyObject *r;
2926 r = call_maybe(
2927 self, "__coerce__", &coerce_str, "(O)", other);
2928 if (r == NULL)
2929 return -1;
2930 if (r == Py_NotImplemented) {
2931 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002932 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002933 else {
2934 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2935 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002936 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002937 Py_DECREF(r);
2938 return -1;
2939 }
2940 *a = PyTuple_GET_ITEM(r, 0);
2941 Py_INCREF(*a);
2942 *b = PyTuple_GET_ITEM(r, 1);
2943 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002944 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002945 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002946 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002947 }
2948 if (other->ob_type->tp_as_number != NULL &&
2949 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2950 PyObject *r;
2951 r = call_maybe(
2952 other, "__coerce__", &coerce_str, "(O)", self);
2953 if (r == NULL)
2954 return -1;
2955 if (r == Py_NotImplemented) {
2956 Py_DECREF(r);
2957 return 1;
2958 }
2959 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2960 PyErr_SetString(PyExc_TypeError,
2961 "__coerce__ didn't return a 2-tuple");
2962 Py_DECREF(r);
2963 return -1;
2964 }
2965 *a = PyTuple_GET_ITEM(r, 1);
2966 Py_INCREF(*a);
2967 *b = PyTuple_GET_ITEM(r, 0);
2968 Py_INCREF(*b);
2969 Py_DECREF(r);
2970 return 0;
2971 }
2972 return 1;
2973}
2974
Guido van Rossumdc91b992001-08-08 22:26:22 +00002975SLOT0(slot_nb_int, "__int__")
2976SLOT0(slot_nb_long, "__long__")
2977SLOT0(slot_nb_float, "__float__")
2978SLOT0(slot_nb_oct, "__oct__")
2979SLOT0(slot_nb_hex, "__hex__")
2980SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2981SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2982SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2983SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2984SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2985SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2986SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2987SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2988SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2989SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2990SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2991SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2992 "__floordiv__", "__rfloordiv__")
2993SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2994SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2995SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002996
2997static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002998half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002999{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003000 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003001 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003002 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003003
Guido van Rossum60718732001-08-28 17:47:51 +00003004 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003005 if (func == NULL) {
3006 PyErr_Clear();
3007 }
3008 else {
3009 args = Py_BuildValue("(O)", other);
3010 if (args == NULL)
3011 res = NULL;
3012 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003013 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003014 Py_DECREF(args);
3015 }
3016 if (res != Py_NotImplemented) {
3017 if (res == NULL)
3018 return -2;
3019 c = PyInt_AsLong(res);
3020 Py_DECREF(res);
3021 if (c == -1 && PyErr_Occurred())
3022 return -2;
3023 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3024 }
3025 Py_DECREF(res);
3026 }
3027 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003028}
3029
Guido van Rossumab3b0342001-09-18 20:38:53 +00003030/* This slot is published for the benefit of try_3way_compare in object.c */
3031int
3032_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003033{
3034 int c;
3035
Guido van Rossumab3b0342001-09-18 20:38:53 +00003036 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003037 c = half_compare(self, other);
3038 if (c <= 1)
3039 return c;
3040 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003041 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003042 c = half_compare(other, self);
3043 if (c < -1)
3044 return -2;
3045 if (c <= 1)
3046 return -c;
3047 }
3048 return (void *)self < (void *)other ? -1 :
3049 (void *)self > (void *)other ? 1 : 0;
3050}
3051
3052static PyObject *
3053slot_tp_repr(PyObject *self)
3054{
3055 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003056 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003057
Guido van Rossum60718732001-08-28 17:47:51 +00003058 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003059 if (func != NULL) {
3060 res = PyEval_CallObject(func, NULL);
3061 Py_DECREF(func);
3062 return res;
3063 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003064 PyErr_Clear();
3065 return PyString_FromFormat("<%s object at %p>",
3066 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003067}
3068
3069static PyObject *
3070slot_tp_str(PyObject *self)
3071{
3072 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003073 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003074
Guido van Rossum60718732001-08-28 17:47:51 +00003075 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003076 if (func != NULL) {
3077 res = PyEval_CallObject(func, NULL);
3078 Py_DECREF(func);
3079 return res;
3080 }
3081 else {
3082 PyErr_Clear();
3083 return slot_tp_repr(self);
3084 }
3085}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003086
3087static long
3088slot_tp_hash(PyObject *self)
3089{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003090 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003091 static PyObject *hash_str, *eq_str, *cmp_str;
3092
Tim Peters6d6c1a32001-08-02 04:15:00 +00003093 long h;
3094
Guido van Rossum60718732001-08-28 17:47:51 +00003095 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003096
3097 if (func != NULL) {
3098 res = PyEval_CallObject(func, NULL);
3099 Py_DECREF(func);
3100 if (res == NULL)
3101 return -1;
3102 h = PyInt_AsLong(res);
3103 }
3104 else {
3105 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003106 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003107 if (func == NULL) {
3108 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003109 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003110 }
3111 if (func != NULL) {
3112 Py_DECREF(func);
3113 PyErr_SetString(PyExc_TypeError, "unhashable type");
3114 return -1;
3115 }
3116 PyErr_Clear();
3117 h = _Py_HashPointer((void *)self);
3118 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003119 if (h == -1 && !PyErr_Occurred())
3120 h = -2;
3121 return h;
3122}
3123
3124static PyObject *
3125slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3126{
Guido van Rossum60718732001-08-28 17:47:51 +00003127 static PyObject *call_str;
3128 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003129 PyObject *res;
3130
3131 if (meth == NULL)
3132 return NULL;
3133 res = PyObject_Call(meth, args, kwds);
3134 Py_DECREF(meth);
3135 return res;
3136}
3137
Guido van Rossum14a6f832001-10-17 13:59:09 +00003138/* There are two slot dispatch functions for tp_getattro.
3139
3140 - slot_tp_getattro() is used when __getattribute__ is overridden
3141 but no __getattr__ hook is present;
3142
3143 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3144
3145 The code in update_slot() and fixup_slot_dispatchers() always installs
3146 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
3147 installs the simpler slot if necessary. */
3148
Tim Peters6d6c1a32001-08-02 04:15:00 +00003149static PyObject *
3150slot_tp_getattro(PyObject *self, PyObject *name)
3151{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003152 static PyObject *getattribute_str = NULL;
3153 return call_method(self, "__getattribute__", &getattribute_str,
3154 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003155}
3156
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003157static PyObject *
3158slot_tp_getattr_hook(PyObject *self, PyObject *name)
3159{
3160 PyTypeObject *tp = self->ob_type;
3161 PyObject *getattr, *getattribute, *res;
3162 static PyObject *getattribute_str = NULL;
3163 static PyObject *getattr_str = NULL;
3164
3165 if (getattr_str == NULL) {
3166 getattr_str = PyString_InternFromString("__getattr__");
3167 if (getattr_str == NULL)
3168 return NULL;
3169 }
3170 if (getattribute_str == NULL) {
3171 getattribute_str =
3172 PyString_InternFromString("__getattribute__");
3173 if (getattribute_str == NULL)
3174 return NULL;
3175 }
3176 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003177 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003178 /* No __getattr__ hook: use a simpler dispatcher */
3179 tp->tp_getattro = slot_tp_getattro;
3180 return slot_tp_getattro(self, name);
3181 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003182 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003183 if (getattribute == NULL ||
3184 (getattribute->ob_type == &PyWrapperDescr_Type &&
3185 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3186 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003187 res = PyObject_GenericGetAttr(self, name);
3188 else
3189 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003190 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003191 PyErr_Clear();
3192 res = PyObject_CallFunction(getattr, "OO", self, name);
3193 }
3194 return res;
3195}
3196
Tim Peters6d6c1a32001-08-02 04:15:00 +00003197static int
3198slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3199{
3200 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003201 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003202
3203 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003204 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003205 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003206 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003207 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003208 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003209 if (res == NULL)
3210 return -1;
3211 Py_DECREF(res);
3212 return 0;
3213}
3214
3215/* Map rich comparison operators to their __xx__ namesakes */
3216static char *name_op[] = {
3217 "__lt__",
3218 "__le__",
3219 "__eq__",
3220 "__ne__",
3221 "__gt__",
3222 "__ge__",
3223};
3224
3225static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003226half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003228 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003229 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003230
Guido van Rossum60718732001-08-28 17:47:51 +00003231 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003232 if (func == NULL) {
3233 PyErr_Clear();
3234 Py_INCREF(Py_NotImplemented);
3235 return Py_NotImplemented;
3236 }
3237 args = Py_BuildValue("(O)", other);
3238 if (args == NULL)
3239 res = NULL;
3240 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003241 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003242 Py_DECREF(args);
3243 }
3244 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003245 return res;
3246}
3247
Guido van Rossumb8f63662001-08-15 23:57:02 +00003248/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3249static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3250
3251static PyObject *
3252slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3253{
3254 PyObject *res;
3255
3256 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3257 res = half_richcompare(self, other, op);
3258 if (res != Py_NotImplemented)
3259 return res;
3260 Py_DECREF(res);
3261 }
3262 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3263 res = half_richcompare(other, self, swapped_op[op]);
3264 if (res != Py_NotImplemented) {
3265 return res;
3266 }
3267 Py_DECREF(res);
3268 }
3269 Py_INCREF(Py_NotImplemented);
3270 return Py_NotImplemented;
3271}
3272
3273static PyObject *
3274slot_tp_iter(PyObject *self)
3275{
3276 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003277 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003278
Guido van Rossum60718732001-08-28 17:47:51 +00003279 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003280 if (func != NULL) {
3281 res = PyObject_CallObject(func, NULL);
3282 Py_DECREF(func);
3283 return res;
3284 }
3285 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003286 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003287 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003288 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003289 return NULL;
3290 }
3291 Py_DECREF(func);
3292 return PySeqIter_New(self);
3293}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003294
3295static PyObject *
3296slot_tp_iternext(PyObject *self)
3297{
Guido van Rossum2730b132001-08-28 18:22:14 +00003298 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003299 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003300}
3301
Guido van Rossum1a493502001-08-17 16:47:50 +00003302static PyObject *
3303slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3304{
3305 PyTypeObject *tp = self->ob_type;
3306 PyObject *get;
3307 static PyObject *get_str = NULL;
3308
3309 if (get_str == NULL) {
3310 get_str = PyString_InternFromString("__get__");
3311 if (get_str == NULL)
3312 return NULL;
3313 }
3314 get = _PyType_Lookup(tp, get_str);
3315 if (get == NULL) {
3316 /* Avoid further slowdowns */
3317 if (tp->tp_descr_get == slot_tp_descr_get)
3318 tp->tp_descr_get = NULL;
3319 Py_INCREF(self);
3320 return self;
3321 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003322 if (obj == NULL)
3323 obj = Py_None;
3324 if (type == NULL)
3325 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003326 return PyObject_CallFunction(get, "OOO", self, obj, type);
3327}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003328
3329static int
3330slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3331{
Guido van Rossum2c252392001-08-24 10:13:31 +00003332 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003333 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003334
3335 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003336 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003337 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003338 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003339 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003340 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003341 if (res == NULL)
3342 return -1;
3343 Py_DECREF(res);
3344 return 0;
3345}
3346
3347static int
3348slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3349{
Guido van Rossum60718732001-08-28 17:47:51 +00003350 static PyObject *init_str;
3351 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003352 PyObject *res;
3353
3354 if (meth == NULL)
3355 return -1;
3356 res = PyObject_Call(meth, args, kwds);
3357 Py_DECREF(meth);
3358 if (res == NULL)
3359 return -1;
3360 Py_DECREF(res);
3361 return 0;
3362}
3363
3364static PyObject *
3365slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3366{
3367 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3368 PyObject *newargs, *x;
3369 int i, n;
3370
3371 if (func == NULL)
3372 return NULL;
3373 assert(PyTuple_Check(args));
3374 n = PyTuple_GET_SIZE(args);
3375 newargs = PyTuple_New(n+1);
3376 if (newargs == NULL)
3377 return NULL;
3378 Py_INCREF(type);
3379 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3380 for (i = 0; i < n; i++) {
3381 x = PyTuple_GET_ITEM(args, i);
3382 Py_INCREF(x);
3383 PyTuple_SET_ITEM(newargs, i+1, x);
3384 }
3385 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003386 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003387 Py_DECREF(func);
3388 return x;
3389}
3390
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003391
3392/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3393 functions. The offsets here are relative to the 'etype' structure, which
3394 incorporates the additional structures used for numbers, sequences and
3395 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3396 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3397 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3398
Guido van Rossum6d204072001-10-21 00:44:31 +00003399typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003400
3401#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003402#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003403#undef ETSLOT
3404#undef SQSLOT
3405#undef MPSLOT
3406#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003407#undef UNSLOT
3408#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003409#undef BINSLOT
3410#undef RBINSLOT
3411
Guido van Rossum6d204072001-10-21 00:44:31 +00003412#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3413 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003414#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3415 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3416 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003417#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3418 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3419#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3420 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3421#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3422 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3423#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3424 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3425#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3426 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3427 "x." NAME "() <==> " DOC)
3428#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3429 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3430 "x." NAME "(y) <==> x" DOC "y")
3431#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3432 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3433 "x." NAME "(y) <==> x" DOC "y")
3434#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3435 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3436 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003437
3438static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003439 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3440 "x.__len__() <==> len(x)"),
3441 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3442 "x.__add__(y) <==> x+y"),
3443 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3444 "x.__mul__(n) <==> x*n"),
3445 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3446 "x.__rmul__(n) <==> n*x"),
3447 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3448 "x.__getitem__(y) <==> x[y]"),
3449 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3450 "x.__getslice__(i, j) <==> x[i:j]"),
3451 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3452 "x.__setitem__(i, y) <==> x[i]=y"),
3453 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3454 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003455 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003456 wrap_intintobjargproc,
3457 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3458 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3459 "x.__delslice__(i, j) <==> del x[i:j]"),
3460 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3461 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003462 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003463 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003464 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003465 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003466
Guido van Rossum6d204072001-10-21 00:44:31 +00003467 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3468 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003469 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003470 wrap_binaryfunc,
3471 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003472 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003473 wrap_objobjargproc,
3474 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003475 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003476 wrap_delitem,
3477 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003478
Guido van Rossum6d204072001-10-21 00:44:31 +00003479 BINSLOT("__add__", nb_add, slot_nb_add,
3480 "+"),
3481 RBINSLOT("__radd__", nb_add, slot_nb_add,
3482 "+"),
3483 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3484 "-"),
3485 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3486 "-"),
3487 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3488 "*"),
3489 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3490 "*"),
3491 BINSLOT("__div__", nb_divide, slot_nb_divide,
3492 "/"),
3493 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3494 "/"),
3495 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3496 "%"),
3497 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3498 "%"),
3499 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3500 "divmod(x, y)"),
3501 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3502 "divmod(y, x)"),
3503 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3504 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3505 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3506 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3507 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3508 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3509 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3510 "abs(x)"),
3511 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3512 "x != 0"),
3513 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3514 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3515 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3516 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3517 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3518 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3519 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3520 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3521 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3522 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3523 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3524 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3525 "x.__coerce__(y) <==> coerce(x, y)"),
3526 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3527 "int(x)"),
3528 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3529 "long(x)"),
3530 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3531 "float(x)"),
3532 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3533 "oct(x)"),
3534 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3535 "hex(x)"),
3536 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3537 wrap_binaryfunc, "+"),
3538 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3539 wrap_binaryfunc, "-"),
3540 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3541 wrap_binaryfunc, "*"),
3542 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3543 wrap_binaryfunc, "/"),
3544 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3545 wrap_binaryfunc, "%"),
3546 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3547 wrap_ternaryfunc, "**"),
3548 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3549 wrap_binaryfunc, "<<"),
3550 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3551 wrap_binaryfunc, ">>"),
3552 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3553 wrap_binaryfunc, "&"),
3554 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3555 wrap_binaryfunc, "^"),
3556 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3557 wrap_binaryfunc, "|"),
3558 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3559 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3560 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3561 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3562 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3563 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3564 IBSLOT("__itruediv__", nb_inplace_true_divide,
3565 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003566
Guido van Rossum6d204072001-10-21 00:44:31 +00003567 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3568 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003569 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003570 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3571 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003572 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003573 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3574 "x.__cmp__(y) <==> cmp(x,y)"),
3575 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3576 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003577 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3578 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003579 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003580 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3581 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3582 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3583 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3584 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3585 "x.__setattr__('name', value) <==> x.name = value"),
3586 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3587 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3588 "x.__delattr__('name') <==> del x.name"),
3589 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3590 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3591 "x.__lt__(y) <==> x<y"),
3592 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3593 "x.__le__(y) <==> x<=y"),
3594 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3595 "x.__eq__(y) <==> x==y"),
3596 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3597 "x.__ne__(y) <==> x!=y"),
3598 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3599 "x.__gt__(y) <==> x>y"),
3600 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3601 "x.__ge__(y) <==> x>=y"),
3602 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3603 "x.__iter__() <==> iter(x)"),
3604 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3605 "x.next() -> the next value, or raise StopIteration"),
3606 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3607 "descr.__get__(obj[, type]) -> value"),
3608 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3609 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003610 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003611 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003612 "see x.__class__.__doc__ for signature",
3613 PyWrapperFlag_KEYWORDS),
3614 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003615 {NULL}
3616};
3617
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003618static void **
3619slotptr(PyTypeObject *type, int offset)
3620{
3621 char *ptr;
3622
3623 assert(offset >= 0);
3624 assert(offset < offsetof(etype, as_buffer));
3625 if (offset >= offsetof(etype, as_mapping)) {
3626 ptr = (void *)type->tp_as_mapping;
3627 offset -= offsetof(etype, as_mapping);
3628 }
3629 else if (offset >= offsetof(etype, as_sequence)) {
3630 ptr = (void *)type->tp_as_sequence;
3631 offset -= offsetof(etype, as_sequence);
3632 }
3633 else if (offset >= offsetof(etype, as_number)) {
3634 ptr = (void *)type->tp_as_number;
3635 offset -= offsetof(etype, as_number);
3636 }
3637 else {
3638 ptr = (void *)type;
3639 }
3640 if (ptr != NULL)
3641 ptr += offset;
3642 return (void **)ptr;
3643}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003644
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003645staticforward int recurse_down_subclasses(PyTypeObject *type,
3646 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003647
3648static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003649update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003650{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003651 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003652
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003653 for (pp = pp0; *pp; pp++) {
3654 slotdef *p = *pp;
3655 PyObject *descr;
3656 PyWrapperDescrObject *d;
3657 void *generic = NULL, *specific = NULL;
3658 int use_generic = 0;
3659 int offset = p->offset;
3660 void **ptr = slotptr(type, offset);
3661 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003662 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003663 do {
3664 descr = _PyType_Lookup(type, p->name_strobj);
3665 if (descr == NULL)
3666 continue;
3667 generic = p->function;
3668 if (descr->ob_type == &PyWrapperDescr_Type) {
3669 d = (PyWrapperDescrObject *)descr;
3670 if (d->d_base->wrapper == p->wrapper &&
3671 PyType_IsSubtype(type, d->d_type)) {
3672 if (specific == NULL ||
3673 specific == d->d_wrapped)
3674 specific = d->d_wrapped;
3675 else
3676 use_generic = 1;
3677 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003678 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003679 else
3680 use_generic = 1;
3681 } while ((++p)->offset == offset);
3682 if (specific && !use_generic)
3683 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003684 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003685 *ptr = generic;
3686 }
3687 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003688}
3689
3690static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003691recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003692{
3693 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003694 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003695 int i, n;
3696
3697 subclasses = type->tp_subclasses;
3698 if (subclasses == NULL)
3699 return 0;
3700 assert(PyList_Check(subclasses));
3701 n = PyList_GET_SIZE(subclasses);
3702 for (i = 0; i < n; i++) {
3703 ref = PyList_GET_ITEM(subclasses, i);
3704 assert(PyWeakref_CheckRef(ref));
3705 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3706 if (subclass == NULL)
3707 continue;
3708 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003709 /* Avoid recursing down into unaffected classes */
3710 dict = subclass->tp_dict;
3711 if (dict != NULL && PyDict_Check(dict) &&
3712 PyDict_GetItem(dict, name) != NULL)
3713 continue;
3714 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003715 return -1;
3716 }
3717 return 0;
3718}
3719
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003720static int
3721slotdef_cmp(const void *aa, const void *bb)
3722{
3723 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3724 int c = a->offset - b->offset;
3725 if (c != 0)
3726 return c;
3727 else
3728 return a - b;
3729}
3730
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003731static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003732init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003733{
3734 slotdef *p;
3735 static int initialized = 0;
3736
3737 if (initialized)
3738 return;
3739 for (p = slotdefs; p->name; p++) {
3740 p->name_strobj = PyString_InternFromString(p->name);
3741 if (!p->name_strobj)
3742 Py_FatalError("XXX ouch");
3743 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003744 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3745 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003746 initialized = 1;
3747}
3748
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003749static int
3750update_slot(PyTypeObject *type, PyObject *name)
3751{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003752 slotdef *ptrs[10];
3753 slotdef *p;
3754 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003755 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003756
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003757 init_slotdefs();
3758 pp = ptrs;
3759 for (p = slotdefs; p->name; p++) {
3760 /* XXX assume name is interned! */
3761 if (p->name_strobj == name)
3762 *pp++ = p;
3763 }
3764 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003765 for (pp = ptrs; *pp; pp++) {
3766 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003767 offset = p->offset;
3768 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003769 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003770 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003771 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003772 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003773}
3774
Tim Peters6d6c1a32001-08-02 04:15:00 +00003775static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003776fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003777{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003778 slotdef *p;
3779 PyObject *mro, *descr;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003780 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003781 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003782 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003783 void *generic, *specific;
3784 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003785
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003786 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003787 mro = type->tp_mro;
3788 assert(PyTuple_Check(mro));
3789 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003790 for (p = slotdefs; p->name; ) {
3791 offset = p->offset;
3792 ptr = slotptr(type, offset);
3793 if (!ptr) {
3794 do {
3795 ++p;
3796 } while (p->offset == offset);
3797 continue;
3798 }
3799 generic = specific = NULL;
3800 use_generic = 0;
3801 do {
3802 descr = NULL;
3803 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003804 PyObject *b = PyTuple_GET_ITEM(mro, i);
3805 PyObject *dict = NULL;
3806 if (PyType_Check(b))
3807 dict = ((PyTypeObject *)b)->tp_dict;
3808 else if (PyClass_Check(b))
3809 dict = ((PyClassObject *)b)->cl_dict;
3810 if (dict != NULL) {
3811 descr = PyDict_GetItem(
3812 dict, p->name_strobj);
3813 if (descr != NULL)
3814 break;
3815 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003816 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003817 if (descr == NULL)
3818 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003819 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003820 if (descr->ob_type == &PyWrapperDescr_Type) {
3821 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003822 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003823 PyType_IsSubtype(type, d->d_type))
3824 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003825 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003826 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003827 specific = d->d_wrapped;
3828 else
3829 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003830 }
3831 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003832 else
3833 use_generic = 1;
3834 } while ((++p)->offset == offset);
3835 if (specific && !use_generic)
3836 *ptr = specific;
3837 else
3838 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003839 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003840}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003841
Guido van Rossum6d204072001-10-21 00:44:31 +00003842/* This function is called by PyType_Ready() to populate the type's
3843 dictionary with method descriptors for function slots. For each
3844 function slot (like tp_repr) that's defined in the type, one or
3845 more corresponding descriptors are added in the type's tp_dict
3846 dictionary under the appropriate name (like __repr__). Some
3847 function slots cause more than one descriptor to be added (for
3848 example, the nb_add slot adds both __add__ and __radd__
3849 descriptors) and some function slots compete for the same
3850 descriptor (for example both sq_item and mp_subscript generate a
3851 __getitem__ descriptor). This only adds new descriptors and
3852 doesn't overwrite entries in tp_dict that were previously
3853 defined. The descriptors contain a reference to the C function
3854 they must call, so that it's safe if they are copied into a
3855 subtype's __dict__ and the subtype has a different C function in
3856 its slot -- calling the method defined by the descriptor will call
3857 the C function that was used to create it, rather than the C
3858 function present in the slot when it is called. (This is important
3859 because a subtype may have a C function in the slot that calls the
3860 method from the dictionary, and we want to avoid infinite recursion
3861 here.) */
3862
3863static int
3864add_operators(PyTypeObject *type)
3865{
3866 PyObject *dict = type->tp_dict;
3867 slotdef *p;
3868 PyObject *descr;
3869 void **ptr;
3870
3871 init_slotdefs();
3872 for (p = slotdefs; p->name; p++) {
3873 if (p->wrapper == NULL)
3874 continue;
3875 ptr = slotptr(type, p->offset);
3876 if (!ptr || !*ptr)
3877 continue;
3878 if (PyDict_GetItem(dict, p->name_strobj))
3879 continue;
3880 descr = PyDescr_NewWrapper(type, p, *ptr);
3881 if (descr == NULL)
3882 return -1;
3883 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3884 return -1;
3885 Py_DECREF(descr);
3886 }
3887 if (type->tp_new != NULL) {
3888 if (add_tp_new_wrapper(type) < 0)
3889 return -1;
3890 }
3891 return 0;
3892}
3893
Guido van Rossum705f0f52001-08-24 16:47:00 +00003894
3895/* Cooperative 'super' */
3896
3897typedef struct {
3898 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003899 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003900 PyObject *obj;
3901} superobject;
3902
Guido van Rossum6f799372001-09-20 20:46:19 +00003903static PyMemberDef super_members[] = {
3904 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3905 "the class invoking super()"},
3906 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3907 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003908 {0}
3909};
3910
Guido van Rossum705f0f52001-08-24 16:47:00 +00003911static void
3912super_dealloc(PyObject *self)
3913{
3914 superobject *su = (superobject *)self;
3915
Guido van Rossum048eb752001-10-02 21:24:57 +00003916 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003917 Py_XDECREF(su->obj);
3918 Py_XDECREF(su->type);
3919 self->ob_type->tp_free(self);
3920}
3921
3922static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003923super_repr(PyObject *self)
3924{
3925 superobject *su = (superobject *)self;
3926
3927 if (su->obj)
3928 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003929 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003930 su->type ? su->type->tp_name : "NULL",
3931 su->obj->ob_type->tp_name);
3932 else
3933 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003934 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003935 su->type ? su->type->tp_name : "NULL");
3936}
3937
3938static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003939super_getattro(PyObject *self, PyObject *name)
3940{
3941 superobject *su = (superobject *)self;
3942
3943 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00003944 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003945 descrgetfunc f;
3946 int i, n;
3947
Guido van Rossume705ef12001-08-29 15:47:06 +00003948 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003949 if (mro == NULL)
3950 n = 0;
3951 else {
3952 assert(PyTuple_Check(mro));
3953 n = PyTuple_GET_SIZE(mro);
3954 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003955 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003956 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003957 break;
3958 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003959 if (i >= n && PyType_Check(su->obj)) {
3960 mro = ((PyTypeObject *)(su->obj))->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003961 if (mro == NULL)
3962 n = 0;
3963 else {
3964 assert(PyTuple_Check(mro));
3965 n = PyTuple_GET_SIZE(mro);
3966 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003967 for (i = 0; i < n; i++) {
3968 if ((PyObject *)(su->type) ==
3969 PyTuple_GET_ITEM(mro, i))
3970 break;
3971 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003972 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003973 i++;
3974 res = NULL;
3975 for (; i < n; i++) {
3976 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00003977 if (PyType_Check(tmp))
3978 dict = ((PyTypeObject *)tmp)->tp_dict;
3979 else if (PyClass_Check(tmp))
3980 dict = ((PyClassObject *)tmp)->cl_dict;
3981 else
3982 continue;
3983 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00003984 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003985 Py_INCREF(res);
3986 f = res->ob_type->tp_descr_get;
3987 if (f != NULL) {
3988 tmp = f(res, su->obj, res);
3989 Py_DECREF(res);
3990 res = tmp;
3991 }
3992 return res;
3993 }
3994 }
3995 }
3996 return PyObject_GenericGetAttr(self, name);
3997}
3998
Guido van Rossum5b443c62001-12-03 15:38:28 +00003999static int
4000supercheck(PyTypeObject *type, PyObject *obj)
4001{
4002 if (!PyType_IsSubtype(obj->ob_type, type) &&
4003 !(PyType_Check(obj) &&
4004 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4005 PyErr_SetString(PyExc_TypeError,
4006 "super(type, obj): "
4007 "obj must be an instance or subtype of type");
4008 return -1;
4009 }
4010 else
4011 return 0;
4012}
4013
Guido van Rossum705f0f52001-08-24 16:47:00 +00004014static PyObject *
4015super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4016{
4017 superobject *su = (superobject *)self;
4018 superobject *new;
4019
4020 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4021 /* Not binding to an object, or already bound */
4022 Py_INCREF(self);
4023 return self;
4024 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004025 if (su->ob_type != &PySuper_Type)
4026 /* If su is an instance of a subclass of super,
4027 call its type */
4028 return PyObject_CallFunction((PyObject *)su->ob_type,
4029 "OO", su->type, obj);
4030 else {
4031 /* Inline the common case */
4032 if (supercheck(su->type, obj) < 0)
4033 return NULL;
4034 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4035 NULL, NULL);
4036 if (new == NULL)
4037 return NULL;
4038 Py_INCREF(su->type);
4039 Py_INCREF(obj);
4040 new->type = su->type;
4041 new->obj = obj;
4042 return (PyObject *)new;
4043 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004044}
4045
4046static int
4047super_init(PyObject *self, PyObject *args, PyObject *kwds)
4048{
4049 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004050 PyTypeObject *type;
4051 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004052
4053 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4054 return -1;
4055 if (obj == Py_None)
4056 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004057 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004058 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004059 Py_INCREF(type);
4060 Py_XINCREF(obj);
4061 su->type = type;
4062 su->obj = obj;
4063 return 0;
4064}
4065
4066static char super_doc[] =
4067"super(type) -> unbound super object\n"
4068"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004069"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004070"Typical use to call a cooperative superclass method:\n"
4071"class C(B):\n"
4072" def meth(self, arg):\n"
4073" super(C, self).meth(arg)";
4074
Guido van Rossum048eb752001-10-02 21:24:57 +00004075static int
4076super_traverse(PyObject *self, visitproc visit, void *arg)
4077{
4078 superobject *su = (superobject *)self;
4079 int err;
4080
4081#define VISIT(SLOT) \
4082 if (SLOT) { \
4083 err = visit((PyObject *)(SLOT), arg); \
4084 if (err) \
4085 return err; \
4086 }
4087
4088 VISIT(su->obj);
4089 VISIT(su->type);
4090
4091#undef VISIT
4092
4093 return 0;
4094}
4095
Guido van Rossum705f0f52001-08-24 16:47:00 +00004096PyTypeObject PySuper_Type = {
4097 PyObject_HEAD_INIT(&PyType_Type)
4098 0, /* ob_size */
4099 "super", /* tp_name */
4100 sizeof(superobject), /* tp_basicsize */
4101 0, /* tp_itemsize */
4102 /* methods */
4103 super_dealloc, /* tp_dealloc */
4104 0, /* tp_print */
4105 0, /* tp_getattr */
4106 0, /* tp_setattr */
4107 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004108 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004109 0, /* tp_as_number */
4110 0, /* tp_as_sequence */
4111 0, /* tp_as_mapping */
4112 0, /* tp_hash */
4113 0, /* tp_call */
4114 0, /* tp_str */
4115 super_getattro, /* tp_getattro */
4116 0, /* tp_setattro */
4117 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004118 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4119 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004120 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004121 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004122 0, /* tp_clear */
4123 0, /* tp_richcompare */
4124 0, /* tp_weaklistoffset */
4125 0, /* tp_iter */
4126 0, /* tp_iternext */
4127 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004128 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004129 0, /* tp_getset */
4130 0, /* tp_base */
4131 0, /* tp_dict */
4132 super_descr_get, /* tp_descr_get */
4133 0, /* tp_descr_set */
4134 0, /* tp_dictoffset */
4135 super_init, /* tp_init */
4136 PyType_GenericAlloc, /* tp_alloc */
4137 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00004138 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004139};