blob: b8a33c89074eb133550cfe53251fde1fe027d1b5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00002/* Type object implementation */
3
Guido van Rossumc0b618a1997-05-02 03:12:38 +00004#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00005#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00006
Guido van Rossum6f799372001-09-20 20:46:19 +00007static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +00008 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
9 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
10 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
11 {"__doc__", T_STRING, offsetof(PyTypeObject, tp_doc), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000012 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000013 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
14 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
15 {"__dictoffset__", T_LONG,
16 offsetof(PyTypeObject, tp_dictoffset), READONLY},
17 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
18 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
19 {0}
20};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000021
Guido van Rossumc0b618a1997-05-02 03:12:38 +000022static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000023type_name(PyTypeObject *type, void *context)
24{
25 char *s;
26
27 s = strrchr(type->tp_name, '.');
28 if (s == NULL)
29 s = type->tp_name;
30 else
31 s++;
32 return PyString_FromString(s);
33}
34
35static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000036type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000037{
Guido van Rossumc3542212001-08-16 09:18:56 +000038 PyObject *mod;
39 char *s;
40
41 s = strrchr(type->tp_name, '.');
42 if (s != NULL)
43 return PyString_FromStringAndSize(type->tp_name,
44 (int)(s - type->tp_name));
45 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
46 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000047 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000048 if (mod != NULL && PyString_Check(mod)) {
49 Py_INCREF(mod);
50 return mod;
51 }
52 PyErr_SetString(PyExc_AttributeError, "__module__");
53 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000054}
55
Guido van Rossum3926a632001-09-25 16:25:58 +000056static int
57type_set_module(PyTypeObject *type, PyObject *value, void *context)
58{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000059 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000060 strrchr(type->tp_name, '.')) {
61 PyErr_Format(PyExc_TypeError,
62 "can't set %s.__module__", type->tp_name);
63 return -1;
64 }
65 if (!value) {
66 PyErr_Format(PyExc_TypeError,
67 "can't delete %s.__module__", type->tp_name);
68 return -1;
69 }
70 return PyDict_SetItemString(type->tp_dict, "__module__", value);
71}
72
Tim Peters6d6c1a32001-08-02 04:15:00 +000073static PyObject *
74type_dict(PyTypeObject *type, void *context)
75{
76 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000077 Py_INCREF(Py_None);
78 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000079 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000080 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000081}
82
Neil Schemenauerf23473f2001-10-21 22:28:58 +000083static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +000084 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +000085 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000086 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +000087 {0}
88};
89
Martin v. Löwis0163d6d2001-06-09 07:34:05 +000090static int
91type_compare(PyObject *v, PyObject *w)
92{
93 /* This is called with type objects only. So we
94 can just compare the addresses. */
95 Py_uintptr_t vv = (Py_uintptr_t)v;
96 Py_uintptr_t ww = (Py_uintptr_t)w;
97 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
98}
99
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000100static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000101type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000102{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000103 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000104 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000105
106 mod = type_module(type, NULL);
107 if (mod == NULL)
108 PyErr_Clear();
109 else if (!PyString_Check(mod)) {
110 Py_DECREF(mod);
111 mod = NULL;
112 }
113 name = type_name(type, NULL);
114 if (name == NULL)
115 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000116
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000117 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
118 kind = "class";
119 else
120 kind = "type";
121
Barry Warsaw7ce36942001-08-24 18:34:26 +0000122 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000123 rtn = PyString_FromFormat("<%s '%s.%s'>",
124 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000125 PyString_AS_STRING(mod),
126 PyString_AS_STRING(name));
127 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000128 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000129 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000130
Guido van Rossumc3542212001-08-16 09:18:56 +0000131 Py_XDECREF(mod);
132 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000133 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000134}
135
Tim Peters6d6c1a32001-08-02 04:15:00 +0000136static PyObject *
137type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
138{
139 PyObject *obj;
140
141 if (type->tp_new == NULL) {
142 PyErr_Format(PyExc_TypeError,
143 "cannot create '%.100s' instances",
144 type->tp_name);
145 return NULL;
146 }
147
Tim Peters3f996e72001-09-13 19:18:27 +0000148 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000149 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000150 /* Ugly exception: when the call was type(something),
151 don't call tp_init on the result. */
152 if (type == &PyType_Type &&
153 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
154 (kwds == NULL ||
155 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
156 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000157 type = obj->ob_type;
158 if (type->tp_init != NULL &&
159 type->tp_init(obj, args, kwds) < 0) {
160 Py_DECREF(obj);
161 obj = NULL;
162 }
163 }
164 return obj;
165}
166
167PyObject *
168PyType_GenericAlloc(PyTypeObject *type, int nitems)
169{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000171 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000172
173 if (PyType_IS_GC(type))
Tim Peters6d483d32001-10-06 21:27:34 +0000174 obj = _PyObject_GC_Malloc(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000175 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000176 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000177
Neil Schemenauerc806c882001-08-29 23:54:54 +0000178 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000179 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000180
Neil Schemenauerc806c882001-08-29 23:54:54 +0000181 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000182
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
184 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000185
Tim Peters6d6c1a32001-08-02 04:15:00 +0000186 if (type->tp_itemsize == 0)
187 PyObject_INIT(obj, type);
188 else
189 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000190
Tim Peters6d6c1a32001-08-02 04:15:00 +0000191 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000192 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000193 return obj;
194}
195
196PyObject *
197PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
198{
199 return type->tp_alloc(type, 0);
200}
201
Guido van Rossum9475a232001-10-05 20:51:39 +0000202/* Helpers for subtyping */
203
204static int
205subtype_traverse(PyObject *self, visitproc visit, void *arg)
206{
207 PyTypeObject *type, *base;
208 traverseproc f;
209 int err;
210
211 /* Find the nearest base with a different tp_traverse */
212 type = self->ob_type;
213 base = type->tp_base;
214 while ((f = base->tp_traverse) == subtype_traverse) {
215 base = base->tp_base;
216 assert(base);
217 }
218
219 if (type->tp_dictoffset != base->tp_dictoffset) {
220 PyObject **dictptr = _PyObject_GetDictPtr(self);
221 if (dictptr && *dictptr) {
222 err = visit(*dictptr, arg);
223 if (err)
224 return err;
225 }
226 }
227
228 if (f)
229 return f(self, visit, arg);
230 return 0;
231}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000232
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000233staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
234
235static int
236call_finalizer(PyObject *self)
237{
238 static PyObject *del_str = NULL;
239 PyObject *del, *res;
240 PyObject *error_type, *error_value, *error_traceback;
241
242 /* Temporarily resurrect the object. */
243#ifdef Py_TRACE_REFS
244#ifndef Py_REF_DEBUG
245# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
246#endif
247 /* much too complicated if Py_TRACE_REFS defined */
248 _Py_NewReference((PyObject *)self);
249#ifdef COUNT_ALLOCS
250 /* compensate for boost in _Py_NewReference; note that
251 * _Py_RefTotal was also boosted; we'll knock that down later.
252 */
253 self->ob_type->tp_allocs--;
254#endif
255#else /* !Py_TRACE_REFS */
256 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
257 Py_INCREF(self);
258#endif /* !Py_TRACE_REFS */
259
260 /* Save the current exception, if any. */
261 PyErr_Fetch(&error_type, &error_value, &error_traceback);
262
263 /* Execute __del__ method, if any. */
264 del = lookup_maybe(self, "__del__", &del_str);
265 if (del != NULL) {
266 res = PyEval_CallObject(del, NULL);
267 if (res == NULL)
268 PyErr_WriteUnraisable(del);
269 else
270 Py_DECREF(res);
271 Py_DECREF(del);
272 }
273
274 /* Restore the saved exception. */
275 PyErr_Restore(error_type, error_value, error_traceback);
276
277 /* Undo the temporary resurrection; can't use DECREF here, it would
278 * cause a recursive call.
279 */
280#ifdef Py_REF_DEBUG
281 /* _Py_RefTotal was boosted either by _Py_NewReference or
282 * Py_INCREF above.
283 */
284 _Py_RefTotal--;
285#endif
286 if (--self->ob_refcnt > 0) {
287#ifdef COUNT_ALLOCS
288 self->ob_type->tp_frees--;
289#endif
290 _PyObject_GC_TRACK(self);
291 return -1; /* __del__ added a reference; don't delete now */
292 }
293#ifdef Py_TRACE_REFS
294 _Py_ForgetReference((PyObject *)self);
295#ifdef COUNT_ALLOCS
296 /* compensate for increment in _Py_ForgetReference */
297 self->ob_type->tp_frees--;
298#endif
299#endif
300
301 return 0;
302}
303
Tim Peters6d6c1a32001-08-02 04:15:00 +0000304static void
305subtype_dealloc(PyObject *self)
306{
Guido van Rossum14227b42001-12-06 02:35:58 +0000307 PyTypeObject *type, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000308 destructor f;
309
310 /* This exists so we can DECREF self->ob_type */
311
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000312 if (call_finalizer(self) < 0)
313 return;
314
Tim Peters6d6c1a32001-08-02 04:15:00 +0000315 /* Find the nearest base with a different tp_dealloc */
316 type = self->ob_type;
Guido van Rossum14227b42001-12-06 02:35:58 +0000317 base = type->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000318 while ((f = base->tp_dealloc) == subtype_dealloc) {
319 base = base->tp_base;
320 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000321 }
322
323 /* Clear __slots__ variables */
324 if (type->tp_basicsize != base->tp_basicsize &&
325 type->tp_itemsize == 0)
326 {
327 char *addr = ((char *)self);
328 char *p = addr + base->tp_basicsize;
329 char *q = addr + type->tp_basicsize;
330 for (; p < q; p += sizeof(PyObject *)) {
331 PyObject **pp;
332 if (p == addr + type->tp_dictoffset ||
333 p == addr + type->tp_weaklistoffset)
334 continue;
335 pp = (PyObject **)p;
336 if (*pp != NULL) {
337 Py_DECREF(*pp);
338 *pp = NULL;
Guido van Rossum33bab012001-12-05 22:45:48 +0000339 }
340 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000341 }
342
343 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000344 if (type->tp_dictoffset && !base->tp_dictoffset) {
345 PyObject **dictptr = _PyObject_GetDictPtr(self);
346 if (dictptr != NULL) {
347 PyObject *dict = *dictptr;
348 if (dict != NULL) {
349 Py_DECREF(dict);
350 *dictptr = NULL;
351 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000352 }
353 }
354
Guido van Rossum9676b222001-08-17 20:32:36 +0000355 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000356 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000357 PyObject_ClearWeakRefs(self);
358
Tim Peters6d6c1a32001-08-02 04:15:00 +0000359 /* Finalize GC if the base doesn't do GC and we do */
360 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000361 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000362
363 /* Call the base tp_dealloc() */
364 assert(f);
365 f(self);
366
367 /* Can't reference self beyond this point */
368 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
369 Py_DECREF(type);
370 }
371}
372
Tim Peters6d6c1a32001-08-02 04:15:00 +0000373staticforward PyTypeObject *solid_base(PyTypeObject *type);
374
375typedef struct {
376 PyTypeObject type;
377 PyNumberMethods as_number;
378 PySequenceMethods as_sequence;
379 PyMappingMethods as_mapping;
380 PyBufferProcs as_buffer;
381 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000382 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000383} etype;
384
385/* type test with subclassing support */
386
387int
388PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
389{
390 PyObject *mro;
391
Guido van Rossum9478d072001-09-07 18:52:13 +0000392 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
393 return b == a || b == &PyBaseObject_Type;
394
Tim Peters6d6c1a32001-08-02 04:15:00 +0000395 mro = a->tp_mro;
396 if (mro != NULL) {
397 /* Deal with multiple inheritance without recursion
398 by walking the MRO tuple */
399 int i, n;
400 assert(PyTuple_Check(mro));
401 n = PyTuple_GET_SIZE(mro);
402 for (i = 0; i < n; i++) {
403 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
404 return 1;
405 }
406 return 0;
407 }
408 else {
409 /* a is not completely initilized yet; follow tp_base */
410 do {
411 if (a == b)
412 return 1;
413 a = a->tp_base;
414 } while (a != NULL);
415 return b == &PyBaseObject_Type;
416 }
417}
418
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000419/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000420 without looking in the instance dictionary
421 (so we can't use PyObject_GetAttr) but still binding
422 it to the instance. The arguments are the object,
423 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000424 static variable used to cache the interned Python string.
425
426 Two variants:
427
428 - lookup_maybe() returns NULL without raising an exception
429 when the _PyType_Lookup() call fails;
430
431 - lookup_method() always raises an exception upon errors.
432*/
Guido van Rossum60718732001-08-28 17:47:51 +0000433
434static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000435lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000436{
437 PyObject *res;
438
439 if (*attrobj == NULL) {
440 *attrobj = PyString_InternFromString(attrstr);
441 if (*attrobj == NULL)
442 return NULL;
443 }
444 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000445 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000446 descrgetfunc f;
447 if ((f = res->ob_type->tp_descr_get) == NULL)
448 Py_INCREF(res);
449 else
450 res = f(res, self, (PyObject *)(self->ob_type));
451 }
452 return res;
453}
454
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000455static PyObject *
456lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
457{
458 PyObject *res = lookup_maybe(self, attrstr, attrobj);
459 if (res == NULL && !PyErr_Occurred())
460 PyErr_SetObject(PyExc_AttributeError, *attrobj);
461 return res;
462}
463
Guido van Rossum2730b132001-08-28 18:22:14 +0000464/* A variation of PyObject_CallMethod that uses lookup_method()
465 instead of PyObject_GetAttrString(). This uses the same convention
466 as lookup_method to cache the interned name string object. */
467
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000468static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000469call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
470{
471 va_list va;
472 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000473 va_start(va, format);
474
Guido van Rossumda21c012001-10-03 00:50:18 +0000475 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000476 if (func == NULL) {
477 va_end(va);
478 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000479 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000480 return NULL;
481 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000482
483 if (format && *format)
484 args = Py_VaBuildValue(format, va);
485 else
486 args = PyTuple_New(0);
487
488 va_end(va);
489
490 if (args == NULL)
491 return NULL;
492
493 assert(PyTuple_Check(args));
494 retval = PyObject_Call(func, args, NULL);
495
496 Py_DECREF(args);
497 Py_DECREF(func);
498
499 return retval;
500}
501
502/* Clone of call_method() that returns NotImplemented when the lookup fails. */
503
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000504static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000505call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
506{
507 va_list va;
508 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000509 va_start(va, format);
510
Guido van Rossumda21c012001-10-03 00:50:18 +0000511 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000512 if (func == NULL) {
513 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000514 if (!PyErr_Occurred()) {
515 Py_INCREF(Py_NotImplemented);
516 return Py_NotImplemented;
517 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000518 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000519 }
520
521 if (format && *format)
522 args = Py_VaBuildValue(format, va);
523 else
524 args = PyTuple_New(0);
525
526 va_end(va);
527
Guido van Rossum717ce002001-09-14 16:58:08 +0000528 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000529 return NULL;
530
Guido van Rossum717ce002001-09-14 16:58:08 +0000531 assert(PyTuple_Check(args));
532 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000533
534 Py_DECREF(args);
535 Py_DECREF(func);
536
537 return retval;
538}
539
Tim Peters6d6c1a32001-08-02 04:15:00 +0000540/* Method resolution order algorithm from "Putting Metaclasses to Work"
541 by Forman and Danforth (Addison-Wesley 1999). */
542
543static int
544conservative_merge(PyObject *left, PyObject *right)
545{
546 int left_size;
547 int right_size;
548 int i, j, r, ok;
549 PyObject *temp, *rr;
550
551 assert(PyList_Check(left));
552 assert(PyList_Check(right));
553
554 again:
555 left_size = PyList_GET_SIZE(left);
556 right_size = PyList_GET_SIZE(right);
557 for (i = 0; i < left_size; i++) {
558 for (j = 0; j < right_size; j++) {
559 if (PyList_GET_ITEM(left, i) ==
560 PyList_GET_ITEM(right, j)) {
561 /* found a merge point */
562 temp = PyList_New(0);
563 if (temp == NULL)
564 return -1;
565 for (r = 0; r < j; r++) {
566 rr = PyList_GET_ITEM(right, r);
567 ok = PySequence_Contains(left, rr);
568 if (ok < 0) {
569 Py_DECREF(temp);
570 return -1;
571 }
572 if (!ok) {
573 ok = PyList_Append(temp, rr);
574 if (ok < 0) {
575 Py_DECREF(temp);
576 return -1;
577 }
578 }
579 }
580 ok = PyList_SetSlice(left, i, i, temp);
581 Py_DECREF(temp);
582 if (ok < 0)
583 return -1;
584 ok = PyList_SetSlice(right, 0, j+1, NULL);
585 if (ok < 0)
586 return -1;
587 goto again;
588 }
589 }
590 }
591 return PyList_SetSlice(left, left_size, left_size, right);
592}
593
594static int
595serious_order_disagreements(PyObject *left, PyObject *right)
596{
597 return 0; /* XXX later -- for now, we cheat: "don't do that" */
598}
599
Tim Petersa91e9642001-11-14 23:32:33 +0000600static int
601fill_classic_mro(PyObject *mro, PyObject *cls)
602{
603 PyObject *bases, *base;
604 int i, n;
605
606 assert(PyList_Check(mro));
607 assert(PyClass_Check(cls));
608 i = PySequence_Contains(mro, cls);
609 if (i < 0)
610 return -1;
611 if (!i) {
612 if (PyList_Append(mro, cls) < 0)
613 return -1;
614 }
615 bases = ((PyClassObject *)cls)->cl_bases;
616 assert(bases && PyTuple_Check(bases));
617 n = PyTuple_GET_SIZE(bases);
618 for (i = 0; i < n; i++) {
619 base = PyTuple_GET_ITEM(bases, i);
620 if (fill_classic_mro(mro, base) < 0)
621 return -1;
622 }
623 return 0;
624}
625
626static PyObject *
627classic_mro(PyObject *cls)
628{
629 PyObject *mro;
630
631 assert(PyClass_Check(cls));
632 mro = PyList_New(0);
633 if (mro != NULL) {
634 if (fill_classic_mro(mro, cls) == 0)
635 return mro;
636 Py_DECREF(mro);
637 }
638 return NULL;
639}
640
Tim Peters6d6c1a32001-08-02 04:15:00 +0000641static PyObject *
642mro_implementation(PyTypeObject *type)
643{
644 int i, n, ok;
645 PyObject *bases, *result;
646
647 bases = type->tp_bases;
648 n = PyTuple_GET_SIZE(bases);
649 result = Py_BuildValue("[O]", (PyObject *)type);
650 if (result == NULL)
651 return NULL;
652 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000653 PyObject *base = PyTuple_GET_ITEM(bases, i);
654 PyObject *parentMRO;
655 if (PyType_Check(base))
656 parentMRO = PySequence_List(
657 ((PyTypeObject*)base)->tp_mro);
658 else
659 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000660 if (parentMRO == NULL) {
661 Py_DECREF(result);
662 return NULL;
663 }
664 if (serious_order_disagreements(result, parentMRO)) {
665 Py_DECREF(result);
666 return NULL;
667 }
668 ok = conservative_merge(result, parentMRO);
669 Py_DECREF(parentMRO);
670 if (ok < 0) {
671 Py_DECREF(result);
672 return NULL;
673 }
674 }
675 return result;
676}
677
678static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000679mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000680{
681 PyTypeObject *type = (PyTypeObject *)self;
682
Tim Peters6d6c1a32001-08-02 04:15:00 +0000683 return mro_implementation(type);
684}
685
686static int
687mro_internal(PyTypeObject *type)
688{
689 PyObject *mro, *result, *tuple;
690
691 if (type->ob_type == &PyType_Type) {
692 result = mro_implementation(type);
693 }
694 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000695 static PyObject *mro_str;
696 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000697 if (mro == NULL)
698 return -1;
699 result = PyObject_CallObject(mro, NULL);
700 Py_DECREF(mro);
701 }
702 if (result == NULL)
703 return -1;
704 tuple = PySequence_Tuple(result);
705 Py_DECREF(result);
706 type->tp_mro = tuple;
707 return 0;
708}
709
710
711/* Calculate the best base amongst multiple base classes.
712 This is the first one that's on the path to the "solid base". */
713
714static PyTypeObject *
715best_base(PyObject *bases)
716{
717 int i, n;
718 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000719 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000720
721 assert(PyTuple_Check(bases));
722 n = PyTuple_GET_SIZE(bases);
723 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000724 base = NULL;
725 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000726 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000727 base_proto = PyTuple_GET_ITEM(bases, i);
728 if (PyClass_Check(base_proto))
729 continue;
730 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000731 PyErr_SetString(
732 PyExc_TypeError,
733 "bases must be types");
734 return NULL;
735 }
Tim Petersa91e9642001-11-14 23:32:33 +0000736 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000737 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000738 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000739 return NULL;
740 }
741 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000742 if (winner == NULL) {
743 winner = candidate;
744 base = base_i;
745 }
746 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747 ;
748 else if (PyType_IsSubtype(candidate, winner)) {
749 winner = candidate;
750 base = base_i;
751 }
752 else {
753 PyErr_SetString(
754 PyExc_TypeError,
755 "multiple bases have "
756 "instance lay-out conflict");
757 return NULL;
758 }
759 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000760 if (base == NULL)
761 PyErr_SetString(PyExc_TypeError,
762 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763 return base;
764}
765
766static int
767extra_ivars(PyTypeObject *type, PyTypeObject *base)
768{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000769 size_t t_size = type->tp_basicsize;
770 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771
Guido van Rossum9676b222001-08-17 20:32:36 +0000772 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000773 if (type->tp_itemsize || base->tp_itemsize) {
774 /* If itemsize is involved, stricter rules */
775 return t_size != b_size ||
776 type->tp_itemsize != base->tp_itemsize;
777 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000778 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
779 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
780 t_size -= sizeof(PyObject *);
781 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
782 type->tp_dictoffset + sizeof(PyObject *) == t_size)
783 t_size -= sizeof(PyObject *);
784
785 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786}
787
788static PyTypeObject *
789solid_base(PyTypeObject *type)
790{
791 PyTypeObject *base;
792
793 if (type->tp_base)
794 base = solid_base(type->tp_base);
795 else
796 base = &PyBaseObject_Type;
797 if (extra_ivars(type, base))
798 return type;
799 else
800 return base;
801}
802
803staticforward void object_dealloc(PyObject *);
804staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000805staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000806staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807
808static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000809subtype_dict(PyObject *obj, void *context)
810{
811 PyObject **dictptr = _PyObject_GetDictPtr(obj);
812 PyObject *dict;
813
814 if (dictptr == NULL) {
815 PyErr_SetString(PyExc_AttributeError,
816 "This object has no __dict__");
817 return NULL;
818 }
819 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000820 if (dict == NULL)
821 *dictptr = dict = PyDict_New();
822 Py_XINCREF(dict);
823 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000824}
825
Guido van Rossum6661be32001-10-26 04:26:12 +0000826static int
827subtype_setdict(PyObject *obj, PyObject *value, void *context)
828{
829 PyObject **dictptr = _PyObject_GetDictPtr(obj);
830 PyObject *dict;
831
832 if (dictptr == NULL) {
833 PyErr_SetString(PyExc_AttributeError,
834 "This object has no __dict__");
835 return -1;
836 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000837 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000838 PyErr_SetString(PyExc_TypeError,
839 "__dict__ must be set to a dictionary");
840 return -1;
841 }
842 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000843 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000844 *dictptr = value;
845 Py_XDECREF(dict);
846 return 0;
847}
848
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000849static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000850 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000851 {0},
852};
853
854static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000855type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
856{
857 PyObject *name, *bases, *dict;
858 static char *kwlist[] = {"name", "bases", "dict", 0};
859 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000860 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000861 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000862 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000863 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000864
Tim Peters3abca122001-10-27 19:37:48 +0000865 assert(args != NULL && PyTuple_Check(args));
866 assert(kwds == NULL || PyDict_Check(kwds));
867
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000868 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000869 {
870 const int nargs = PyTuple_GET_SIZE(args);
871 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
872
873 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
874 PyObject *x = PyTuple_GET_ITEM(args, 0);
875 Py_INCREF(x->ob_type);
876 return (PyObject *) x->ob_type;
877 }
878
879 /* SF bug 475327 -- if that didn't trigger, we need 3
880 arguments. but PyArg_ParseTupleAndKeywords below may give
881 a msg saying type() needs exactly 3. */
882 if (nargs + nkwds != 3) {
883 PyErr_SetString(PyExc_TypeError,
884 "type() takes 1 or 3 arguments");
885 return NULL;
886 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000887 }
888
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000889 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000890 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
891 &name,
892 &PyTuple_Type, &bases,
893 &PyDict_Type, &dict))
894 return NULL;
895
896 /* Determine the proper metatype to deal with this,
897 and check for metatype conflicts while we're at it.
898 Note that if some other metatype wins to contract,
899 it's possible that its instances are not types. */
900 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000901 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000902 for (i = 0; i < nbases; i++) {
903 tmp = PyTuple_GET_ITEM(bases, i);
904 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +0000905 if (tmptype == &PyClass_Type)
906 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000907 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000908 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000909 if (PyType_IsSubtype(tmptype, winner)) {
910 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000911 continue;
912 }
913 PyErr_SetString(PyExc_TypeError,
914 "metatype conflict among bases");
915 return NULL;
916 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000917 if (winner != metatype) {
918 if (winner->tp_new != type_new) /* Pass it to the winner */
919 return winner->tp_new(winner, args, kwds);
920 metatype = winner;
921 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000922
923 /* Adjust for empty tuple bases */
924 if (nbases == 0) {
925 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
926 if (bases == NULL)
927 return NULL;
928 nbases = 1;
929 }
930 else
931 Py_INCREF(bases);
932
933 /* XXX From here until type is allocated, "return NULL" leaks bases! */
934
935 /* Calculate best base, and check that all bases are type objects */
936 base = best_base(bases);
937 if (base == NULL)
938 return NULL;
939 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
940 PyErr_Format(PyExc_TypeError,
941 "type '%.100s' is not an acceptable base type",
942 base->tp_name);
943 return NULL;
944 }
945
Tim Peters6d6c1a32001-08-02 04:15:00 +0000946 /* Check for a __slots__ sequence variable in dict, and count it */
947 slots = PyDict_GetItemString(dict, "__slots__");
948 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000949 add_dict = 0;
950 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000951 if (slots != NULL) {
952 /* Make it into a tuple */
953 if (PyString_Check(slots))
954 slots = Py_BuildValue("(O)", slots);
955 else
956 slots = PySequence_Tuple(slots);
957 if (slots == NULL)
958 return NULL;
959 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000960 if (nslots > 0 && base->tp_itemsize != 0) {
961 PyErr_Format(PyExc_TypeError,
962 "nonempty __slots__ "
963 "not supported for subtype of '%s'",
964 base->tp_name);
965 return NULL;
966 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000967 for (i = 0; i < nslots; i++) {
968 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
969 PyErr_SetString(PyExc_TypeError,
970 "__slots__ must be a sequence of strings");
971 Py_DECREF(slots);
972 return NULL;
973 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000974 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000975 }
976 }
977 if (slots == NULL && base->tp_dictoffset == 0 &&
978 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +0000979 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000980 add_dict++;
981 }
Guido van Rossumc4141872001-08-30 04:43:35 +0000982 if (slots == NULL && base->tp_weaklistoffset == 0 &&
983 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +0000984 nslots++;
985 add_weak++;
986 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000987
988 /* XXX From here until type is safely allocated,
989 "return NULL" may leak slots! */
990
991 /* Allocate the type object */
992 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
993 if (type == NULL)
994 return NULL;
995
996 /* Keep name and slots alive in the extended type object */
997 et = (etype *)type;
998 Py_INCREF(name);
999 et->name = name;
1000 et->slots = slots;
1001
Guido van Rossumdc91b992001-08-08 22:26:22 +00001002 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001003 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1004 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001005 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1006 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001007
1008 /* It's a new-style number unless it specifically inherits any
1009 old-style numeric behavior */
1010 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1011 (base->tp_as_number == NULL))
1012 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1013
1014 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001015 type->tp_as_number = &et->as_number;
1016 type->tp_as_sequence = &et->as_sequence;
1017 type->tp_as_mapping = &et->as_mapping;
1018 type->tp_as_buffer = &et->as_buffer;
1019 type->tp_name = PyString_AS_STRING(name);
1020
1021 /* Set tp_base and tp_bases */
1022 type->tp_bases = bases;
1023 Py_INCREF(base);
1024 type->tp_base = base;
1025
Guido van Rossum687ae002001-10-15 22:03:32 +00001026 /* Initialize tp_dict from passed-in dict */
1027 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 if (dict == NULL) {
1029 Py_DECREF(type);
1030 return NULL;
1031 }
1032
Guido van Rossumc3542212001-08-16 09:18:56 +00001033 /* Set __module__ in the dict */
1034 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1035 tmp = PyEval_GetGlobals();
1036 if (tmp != NULL) {
1037 tmp = PyDict_GetItemString(tmp, "__name__");
1038 if (tmp != NULL) {
1039 if (PyDict_SetItemString(dict, "__module__",
1040 tmp) < 0)
1041 return NULL;
1042 }
1043 }
1044 }
1045
Tim Peters2f93e282001-10-04 05:27:00 +00001046 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
1047 and is a string (tp_doc is a char* -- can't copy a general object
1048 into it).
1049 XXX What if it's a Unicode string? Don't know -- this ignores it.
1050 */
1051 {
1052 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1053 if (doc != NULL && PyString_Check(doc)) {
1054 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001055 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001056 if (type->tp_doc == NULL) {
1057 Py_DECREF(type);
1058 return NULL;
1059 }
1060 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1061 }
1062 }
1063
Tim Peters6d6c1a32001-08-02 04:15:00 +00001064 /* Special-case __new__: if it's a plain function,
1065 make it a static function */
1066 tmp = PyDict_GetItemString(dict, "__new__");
1067 if (tmp != NULL && PyFunction_Check(tmp)) {
1068 tmp = PyStaticMethod_New(tmp);
1069 if (tmp == NULL) {
1070 Py_DECREF(type);
1071 return NULL;
1072 }
1073 PyDict_SetItemString(dict, "__new__", tmp);
1074 Py_DECREF(tmp);
1075 }
1076
1077 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1078 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001079 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080 if (slots != NULL) {
1081 for (i = 0; i < nslots; i++, mp++) {
1082 mp->name = PyString_AS_STRING(
1083 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001084 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001085 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001086 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001087 strcmp(mp->name, "__weakref__") == 0) {
1088 mp->type = T_OBJECT;
Guido van Rossum9676b222001-08-17 20:32:36 +00001089 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001090 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001091 slotoffset += sizeof(PyObject *);
1092 }
1093 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001094 else {
1095 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001096 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +00001097 type->tp_dictoffset =
1098 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001099 else
1100 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001101 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001102 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +00001103 }
1104 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001105 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +00001106 type->tp_weaklistoffset = slotoffset;
1107 mp->name = "__weakref__";
1108 mp->type = T_OBJECT;
1109 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +00001110 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001111 mp++;
1112 slotoffset += sizeof(PyObject *);
1113 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114 }
1115 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001116 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001117 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001118
1119 /* Special case some slots */
1120 if (type->tp_dictoffset != 0 || nslots > 0) {
1121 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1122 type->tp_getattro = PyObject_GenericGetAttr;
1123 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1124 type->tp_setattro = PyObject_GenericSetAttr;
1125 }
1126 type->tp_dealloc = subtype_dealloc;
1127
Guido van Rossum9475a232001-10-05 20:51:39 +00001128 /* Enable GC unless there are really no instance variables possible */
1129 if (!(type->tp_basicsize == sizeof(PyObject) &&
1130 type->tp_itemsize == 0))
1131 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1132
Tim Peters6d6c1a32001-08-02 04:15:00 +00001133 /* Always override allocation strategy to use regular heap */
1134 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001135 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1136 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001137 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +00001138 type->tp_clear = base->tp_clear;
1139 }
1140 else
1141 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001142
1143 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001144 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001145 Py_DECREF(type);
1146 return NULL;
1147 }
1148
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001149 /* Put the proper slots in place */
1150 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001151
Tim Peters6d6c1a32001-08-02 04:15:00 +00001152 return (PyObject *)type;
1153}
1154
1155/* Internal API to look for a name through the MRO.
1156 This returns a borrowed reference, and doesn't set an exception! */
1157PyObject *
1158_PyType_Lookup(PyTypeObject *type, PyObject *name)
1159{
1160 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001161 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162
Guido van Rossum687ae002001-10-15 22:03:32 +00001163 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001164 mro = type->tp_mro;
1165 assert(PyTuple_Check(mro));
1166 n = PyTuple_GET_SIZE(mro);
1167 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001168 base = PyTuple_GET_ITEM(mro, i);
1169 if (PyClass_Check(base))
1170 dict = ((PyClassObject *)base)->cl_dict;
1171 else {
1172 assert(PyType_Check(base));
1173 dict = ((PyTypeObject *)base)->tp_dict;
1174 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001175 assert(dict && PyDict_Check(dict));
1176 res = PyDict_GetItem(dict, name);
1177 if (res != NULL)
1178 return res;
1179 }
1180 return NULL;
1181}
1182
1183/* This is similar to PyObject_GenericGetAttr(),
1184 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1185static PyObject *
1186type_getattro(PyTypeObject *type, PyObject *name)
1187{
1188 PyTypeObject *metatype = type->ob_type;
1189 PyObject *descr, *res;
1190 descrgetfunc f;
1191
1192 /* Initialize this type (we'll assume the metatype is initialized) */
1193 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001194 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001195 return NULL;
1196 }
1197
1198 /* Get a descriptor from the metatype */
1199 descr = _PyType_Lookup(metatype, name);
1200 f = NULL;
1201 if (descr != NULL) {
1202 f = descr->ob_type->tp_descr_get;
1203 if (f != NULL && PyDescr_IsData(descr))
1204 return f(descr,
1205 (PyObject *)type, (PyObject *)metatype);
1206 }
1207
Guido van Rossum687ae002001-10-15 22:03:32 +00001208 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001209 res = _PyType_Lookup(type, name);
1210 if (res != NULL) {
1211 f = res->ob_type->tp_descr_get;
1212 if (f != NULL)
1213 return f(res, (PyObject *)NULL, (PyObject *)type);
1214 Py_INCREF(res);
1215 return res;
1216 }
1217
1218 /* Use the descriptor from the metatype */
1219 if (f != NULL) {
1220 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1221 return res;
1222 }
1223 if (descr != NULL) {
1224 Py_INCREF(descr);
1225 return descr;
1226 }
1227
1228 /* Give up */
1229 PyErr_Format(PyExc_AttributeError,
1230 "type object '%.50s' has no attribute '%.400s'",
1231 type->tp_name, PyString_AS_STRING(name));
1232 return NULL;
1233}
1234
1235static int
1236type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1237{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001238 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1239 PyErr_Format(
1240 PyExc_TypeError,
1241 "can't set attributes of built-in/extension type '%s'",
1242 type->tp_name);
1243 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001244 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001245 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1246 return -1;
1247 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001248}
1249
1250static void
1251type_dealloc(PyTypeObject *type)
1252{
1253 etype *et;
1254
1255 /* Assert this is a heap-allocated type object */
1256 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001257 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001258 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259 et = (etype *)type;
1260 Py_XDECREF(type->tp_base);
1261 Py_XDECREF(type->tp_dict);
1262 Py_XDECREF(type->tp_bases);
1263 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001264 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001265 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266 Py_XDECREF(et->name);
1267 Py_XDECREF(et->slots);
1268 type->ob_type->tp_free((PyObject *)type);
1269}
1270
Guido van Rossum1c450732001-10-08 15:18:27 +00001271static PyObject *
1272type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1273{
1274 PyObject *list, *raw, *ref;
1275 int i, n;
1276
1277 list = PyList_New(0);
1278 if (list == NULL)
1279 return NULL;
1280 raw = type->tp_subclasses;
1281 if (raw == NULL)
1282 return list;
1283 assert(PyList_Check(raw));
1284 n = PyList_GET_SIZE(raw);
1285 for (i = 0; i < n; i++) {
1286 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001287 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001288 ref = PyWeakref_GET_OBJECT(ref);
1289 if (ref != Py_None) {
1290 if (PyList_Append(list, ref) < 0) {
1291 Py_DECREF(list);
1292 return NULL;
1293 }
1294 }
1295 }
1296 return list;
1297}
1298
Tim Peters6d6c1a32001-08-02 04:15:00 +00001299static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001300 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001301 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001302 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1303 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001304 {0}
1305};
1306
1307static char type_doc[] =
1308"type(object) -> the object's type\n"
1309"type(name, bases, dict) -> a new type";
1310
Guido van Rossum048eb752001-10-02 21:24:57 +00001311static int
1312type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1313{
1314 etype *et;
1315 int err;
1316
1317 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1318 return 0;
1319
1320 et = (etype *)type;
1321
1322#define VISIT(SLOT) \
1323 if (SLOT) { \
1324 err = visit((PyObject *)(SLOT), arg); \
1325 if (err) \
1326 return err; \
1327 }
1328
1329 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001330 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001331 VISIT(type->tp_mro);
1332 VISIT(type->tp_bases);
1333 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001334 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001335 VISIT(et->slots);
1336
1337#undef VISIT
1338
1339 return 0;
1340}
1341
1342static int
1343type_clear(PyTypeObject *type)
1344{
1345 etype *et;
1346 PyObject *tmp;
1347
1348 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1349 return 0;
1350
1351 et = (etype *)type;
1352
1353#define CLEAR(SLOT) \
1354 if (SLOT) { \
1355 tmp = (PyObject *)(SLOT); \
1356 SLOT = NULL; \
1357 Py_DECREF(tmp); \
1358 }
1359
1360 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001361 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001362 CLEAR(type->tp_mro);
1363 CLEAR(type->tp_bases);
1364 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001365 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001366 CLEAR(et->slots);
1367
Tim Peters2f93e282001-10-04 05:27:00 +00001368 if (type->tp_doc != NULL) {
1369 PyObject_FREE(type->tp_doc);
1370 type->tp_doc = NULL;
1371 }
1372
Guido van Rossum048eb752001-10-02 21:24:57 +00001373#undef CLEAR
1374
1375 return 0;
1376}
1377
1378static int
1379type_is_gc(PyTypeObject *type)
1380{
1381 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1382}
1383
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001384PyTypeObject PyType_Type = {
1385 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 0, /* ob_size */
1387 "type", /* tp_name */
1388 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001389 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001390 (destructor)type_dealloc, /* tp_dealloc */
1391 0, /* tp_print */
1392 0, /* tp_getattr */
1393 0, /* tp_setattr */
1394 type_compare, /* tp_compare */
1395 (reprfunc)type_repr, /* tp_repr */
1396 0, /* tp_as_number */
1397 0, /* tp_as_sequence */
1398 0, /* tp_as_mapping */
1399 (hashfunc)_Py_HashPointer, /* tp_hash */
1400 (ternaryfunc)type_call, /* tp_call */
1401 0, /* tp_str */
1402 (getattrofunc)type_getattro, /* tp_getattro */
1403 (setattrofunc)type_setattro, /* tp_setattro */
1404 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001405 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1406 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001408 (traverseproc)type_traverse, /* tp_traverse */
1409 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001410 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001411 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001412 0, /* tp_iter */
1413 0, /* tp_iternext */
1414 type_methods, /* tp_methods */
1415 type_members, /* tp_members */
1416 type_getsets, /* tp_getset */
1417 0, /* tp_base */
1418 0, /* tp_dict */
1419 0, /* tp_descr_get */
1420 0, /* tp_descr_set */
1421 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1422 0, /* tp_init */
1423 0, /* tp_alloc */
1424 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001425 _PyObject_GC_Del, /* tp_free */
1426 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001427};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001428
1429
1430/* The base type of all types (eventually)... except itself. */
1431
1432static int
1433object_init(PyObject *self, PyObject *args, PyObject *kwds)
1434{
1435 return 0;
1436}
1437
1438static void
1439object_dealloc(PyObject *self)
1440{
1441 self->ob_type->tp_free(self);
1442}
1443
Guido van Rossum8e248182001-08-12 05:17:56 +00001444static PyObject *
1445object_repr(PyObject *self)
1446{
Guido van Rossum76e69632001-08-16 18:52:43 +00001447 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001448 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001449
Guido van Rossum76e69632001-08-16 18:52:43 +00001450 type = self->ob_type;
1451 mod = type_module(type, NULL);
1452 if (mod == NULL)
1453 PyErr_Clear();
1454 else if (!PyString_Check(mod)) {
1455 Py_DECREF(mod);
1456 mod = NULL;
1457 }
1458 name = type_name(type, NULL);
1459 if (name == NULL)
1460 return NULL;
1461 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001462 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001463 PyString_AS_STRING(mod),
1464 PyString_AS_STRING(name),
1465 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001466 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001467 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001468 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001469 Py_XDECREF(mod);
1470 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001471 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001472}
1473
Guido van Rossumb8f63662001-08-15 23:57:02 +00001474static PyObject *
1475object_str(PyObject *self)
1476{
1477 unaryfunc f;
1478
1479 f = self->ob_type->tp_repr;
1480 if (f == NULL)
1481 f = object_repr;
1482 return f(self);
1483}
1484
Guido van Rossum8e248182001-08-12 05:17:56 +00001485static long
1486object_hash(PyObject *self)
1487{
1488 return _Py_HashPointer(self);
1489}
Guido van Rossum8e248182001-08-12 05:17:56 +00001490
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001491static PyObject *
1492object_get_class(PyObject *self, void *closure)
1493{
1494 Py_INCREF(self->ob_type);
1495 return (PyObject *)(self->ob_type);
1496}
1497
1498static int
1499equiv_structs(PyTypeObject *a, PyTypeObject *b)
1500{
1501 return a == b ||
1502 (a != NULL &&
1503 b != NULL &&
1504 a->tp_basicsize == b->tp_basicsize &&
1505 a->tp_itemsize == b->tp_itemsize &&
1506 a->tp_dictoffset == b->tp_dictoffset &&
1507 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1508 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1509 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1510}
1511
1512static int
1513same_slots_added(PyTypeObject *a, PyTypeObject *b)
1514{
1515 PyTypeObject *base = a->tp_base;
1516 int size;
1517
1518 if (base != b->tp_base)
1519 return 0;
1520 if (equiv_structs(a, base) && equiv_structs(b, base))
1521 return 1;
1522 size = base->tp_basicsize;
1523 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1524 size += sizeof(PyObject *);
1525 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1526 size += sizeof(PyObject *);
1527 return size == a->tp_basicsize && size == b->tp_basicsize;
1528}
1529
1530static int
1531object_set_class(PyObject *self, PyObject *value, void *closure)
1532{
1533 PyTypeObject *old = self->ob_type;
1534 PyTypeObject *new, *newbase, *oldbase;
1535
1536 if (!PyType_Check(value)) {
1537 PyErr_Format(PyExc_TypeError,
1538 "__class__ must be set to new-style class, not '%s' object",
1539 value->ob_type->tp_name);
1540 return -1;
1541 }
1542 new = (PyTypeObject *)value;
1543 newbase = new;
1544 oldbase = old;
1545 while (equiv_structs(newbase, newbase->tp_base))
1546 newbase = newbase->tp_base;
1547 while (equiv_structs(oldbase, oldbase->tp_base))
1548 oldbase = oldbase->tp_base;
1549 if (newbase != oldbase &&
1550 (newbase->tp_base != oldbase->tp_base ||
1551 !same_slots_added(newbase, oldbase))) {
1552 PyErr_Format(PyExc_TypeError,
1553 "__class__ assignment: "
1554 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001555 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001556 old->tp_name);
1557 return -1;
1558 }
1559 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1560 Py_INCREF(new);
1561 }
1562 self->ob_type = new;
1563 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1564 Py_DECREF(old);
1565 }
1566 return 0;
1567}
1568
1569static PyGetSetDef object_getsets[] = {
1570 {"__class__", object_get_class, object_set_class,
1571 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001572 {0}
1573};
1574
Guido van Rossum3926a632001-09-25 16:25:58 +00001575static PyObject *
1576object_reduce(PyObject *self, PyObject *args)
1577{
1578 /* Call copy_reg._reduce(self) */
1579 static PyObject *copy_reg_str;
1580 PyObject *copy_reg, *res;
1581
1582 if (!copy_reg_str) {
1583 copy_reg_str = PyString_InternFromString("copy_reg");
1584 if (copy_reg_str == NULL)
1585 return NULL;
1586 }
1587 copy_reg = PyImport_Import(copy_reg_str);
1588 if (!copy_reg)
1589 return NULL;
1590 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1591 Py_DECREF(copy_reg);
1592 return res;
1593}
1594
1595static PyMethodDef object_methods[] = {
1596 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1597 {0}
1598};
1599
Tim Peters6d6c1a32001-08-02 04:15:00 +00001600PyTypeObject PyBaseObject_Type = {
1601 PyObject_HEAD_INIT(&PyType_Type)
1602 0, /* ob_size */
1603 "object", /* tp_name */
1604 sizeof(PyObject), /* tp_basicsize */
1605 0, /* tp_itemsize */
1606 (destructor)object_dealloc, /* tp_dealloc */
1607 0, /* tp_print */
1608 0, /* tp_getattr */
1609 0, /* tp_setattr */
1610 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001611 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001612 0, /* tp_as_number */
1613 0, /* tp_as_sequence */
1614 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001615 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001616 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001617 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001618 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001619 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001620 0, /* tp_as_buffer */
1621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1622 "The most base type", /* tp_doc */
1623 0, /* tp_traverse */
1624 0, /* tp_clear */
1625 0, /* tp_richcompare */
1626 0, /* tp_weaklistoffset */
1627 0, /* tp_iter */
1628 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001629 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001630 0, /* tp_members */
1631 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001632 0, /* tp_base */
1633 0, /* tp_dict */
1634 0, /* tp_descr_get */
1635 0, /* tp_descr_set */
1636 0, /* tp_dictoffset */
1637 object_init, /* tp_init */
1638 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001639 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001640 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001641};
1642
1643
1644/* Initialize the __dict__ in a type object */
1645
1646static int
1647add_methods(PyTypeObject *type, PyMethodDef *meth)
1648{
Guido van Rossum687ae002001-10-15 22:03:32 +00001649 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001650
1651 for (; meth->ml_name != NULL; meth++) {
1652 PyObject *descr;
1653 if (PyDict_GetItemString(dict, meth->ml_name))
1654 continue;
1655 descr = PyDescr_NewMethod(type, meth);
1656 if (descr == NULL)
1657 return -1;
1658 if (PyDict_SetItemString(dict,meth->ml_name,descr) < 0)
1659 return -1;
1660 Py_DECREF(descr);
1661 }
1662 return 0;
1663}
1664
1665static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001666add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001667{
Guido van Rossum687ae002001-10-15 22:03:32 +00001668 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001669
1670 for (; memb->name != NULL; memb++) {
1671 PyObject *descr;
1672 if (PyDict_GetItemString(dict, memb->name))
1673 continue;
1674 descr = PyDescr_NewMember(type, memb);
1675 if (descr == NULL)
1676 return -1;
1677 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1678 return -1;
1679 Py_DECREF(descr);
1680 }
1681 return 0;
1682}
1683
1684static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001685add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001686{
Guido van Rossum687ae002001-10-15 22:03:32 +00001687 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001688
1689 for (; gsp->name != NULL; gsp++) {
1690 PyObject *descr;
1691 if (PyDict_GetItemString(dict, gsp->name))
1692 continue;
1693 descr = PyDescr_NewGetSet(type, gsp);
1694
1695 if (descr == NULL)
1696 return -1;
1697 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1698 return -1;
1699 Py_DECREF(descr);
1700 }
1701 return 0;
1702}
1703
Guido van Rossum13d52f02001-08-10 21:24:08 +00001704static void
1705inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001706{
1707 int oldsize, newsize;
1708
Guido van Rossum13d52f02001-08-10 21:24:08 +00001709 /* Special flag magic */
1710 if (!type->tp_as_buffer && base->tp_as_buffer) {
1711 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1712 type->tp_flags |=
1713 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1714 }
1715 if (!type->tp_as_sequence && base->tp_as_sequence) {
1716 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1717 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1718 }
1719 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1720 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1721 if ((!type->tp_as_number && base->tp_as_number) ||
1722 (!type->tp_as_sequence && base->tp_as_sequence)) {
1723 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1724 if (!type->tp_as_number && !type->tp_as_sequence) {
1725 type->tp_flags |= base->tp_flags &
1726 Py_TPFLAGS_HAVE_INPLACEOPS;
1727 }
1728 }
1729 /* Wow */
1730 }
1731 if (!type->tp_as_number && base->tp_as_number) {
1732 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1733 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1734 }
1735
1736 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001737 oldsize = base->tp_basicsize;
1738 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1739 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1740 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001741 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1742 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001743 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001744 if (type->tp_traverse == NULL)
1745 type->tp_traverse = base->tp_traverse;
1746 if (type->tp_clear == NULL)
1747 type->tp_clear = base->tp_clear;
1748 }
1749 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001750 /* The condition below could use some explanation.
1751 It appears that tp_new is not inherited for static types
1752 whose base class is 'object'; this seems to be a precaution
1753 so that old extension types don't suddenly become
1754 callable (object.__new__ wouldn't insure the invariants
1755 that the extension type's own factory function ensures).
1756 Heap types, of course, are under our control, so they do
1757 inherit tp_new; static extension types that specify some
1758 other built-in type as the default are considered
1759 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001760 if (base != &PyBaseObject_Type ||
1761 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1762 if (type->tp_new == NULL)
1763 type->tp_new = base->tp_new;
1764 }
1765 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001766 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001767
1768 /* Copy other non-function slots */
1769
1770#undef COPYVAL
1771#define COPYVAL(SLOT) \
1772 if (type->SLOT == 0) type->SLOT = base->SLOT
1773
1774 COPYVAL(tp_itemsize);
1775 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1776 COPYVAL(tp_weaklistoffset);
1777 }
1778 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1779 COPYVAL(tp_dictoffset);
1780 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001781}
1782
1783static void
1784inherit_slots(PyTypeObject *type, PyTypeObject *base)
1785{
1786 PyTypeObject *basebase;
1787
1788#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001789#undef COPYSLOT
1790#undef COPYNUM
1791#undef COPYSEQ
1792#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001793#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001794
1795#define SLOTDEFINED(SLOT) \
1796 (base->SLOT != 0 && \
1797 (basebase == NULL || base->SLOT != basebase->SLOT))
1798
Tim Peters6d6c1a32001-08-02 04:15:00 +00001799#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001800 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001801
1802#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1803#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1804#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001805#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001806
Guido van Rossum13d52f02001-08-10 21:24:08 +00001807 /* This won't inherit indirect slots (from tp_as_number etc.)
1808 if type doesn't provide the space. */
1809
1810 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1811 basebase = base->tp_base;
1812 if (basebase->tp_as_number == NULL)
1813 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001814 COPYNUM(nb_add);
1815 COPYNUM(nb_subtract);
1816 COPYNUM(nb_multiply);
1817 COPYNUM(nb_divide);
1818 COPYNUM(nb_remainder);
1819 COPYNUM(nb_divmod);
1820 COPYNUM(nb_power);
1821 COPYNUM(nb_negative);
1822 COPYNUM(nb_positive);
1823 COPYNUM(nb_absolute);
1824 COPYNUM(nb_nonzero);
1825 COPYNUM(nb_invert);
1826 COPYNUM(nb_lshift);
1827 COPYNUM(nb_rshift);
1828 COPYNUM(nb_and);
1829 COPYNUM(nb_xor);
1830 COPYNUM(nb_or);
1831 COPYNUM(nb_coerce);
1832 COPYNUM(nb_int);
1833 COPYNUM(nb_long);
1834 COPYNUM(nb_float);
1835 COPYNUM(nb_oct);
1836 COPYNUM(nb_hex);
1837 COPYNUM(nb_inplace_add);
1838 COPYNUM(nb_inplace_subtract);
1839 COPYNUM(nb_inplace_multiply);
1840 COPYNUM(nb_inplace_divide);
1841 COPYNUM(nb_inplace_remainder);
1842 COPYNUM(nb_inplace_power);
1843 COPYNUM(nb_inplace_lshift);
1844 COPYNUM(nb_inplace_rshift);
1845 COPYNUM(nb_inplace_and);
1846 COPYNUM(nb_inplace_xor);
1847 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001848 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1849 COPYNUM(nb_true_divide);
1850 COPYNUM(nb_floor_divide);
1851 COPYNUM(nb_inplace_true_divide);
1852 COPYNUM(nb_inplace_floor_divide);
1853 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001854 }
1855
Guido van Rossum13d52f02001-08-10 21:24:08 +00001856 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1857 basebase = base->tp_base;
1858 if (basebase->tp_as_sequence == NULL)
1859 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001860 COPYSEQ(sq_length);
1861 COPYSEQ(sq_concat);
1862 COPYSEQ(sq_repeat);
1863 COPYSEQ(sq_item);
1864 COPYSEQ(sq_slice);
1865 COPYSEQ(sq_ass_item);
1866 COPYSEQ(sq_ass_slice);
1867 COPYSEQ(sq_contains);
1868 COPYSEQ(sq_inplace_concat);
1869 COPYSEQ(sq_inplace_repeat);
1870 }
1871
Guido van Rossum13d52f02001-08-10 21:24:08 +00001872 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1873 basebase = base->tp_base;
1874 if (basebase->tp_as_mapping == NULL)
1875 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001876 COPYMAP(mp_length);
1877 COPYMAP(mp_subscript);
1878 COPYMAP(mp_ass_subscript);
1879 }
1880
Tim Petersfc57ccb2001-10-12 02:38:24 +00001881 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1882 basebase = base->tp_base;
1883 if (basebase->tp_as_buffer == NULL)
1884 basebase = NULL;
1885 COPYBUF(bf_getreadbuffer);
1886 COPYBUF(bf_getwritebuffer);
1887 COPYBUF(bf_getsegcount);
1888 COPYBUF(bf_getcharbuffer);
1889 }
1890
Guido van Rossum13d52f02001-08-10 21:24:08 +00001891 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001892
Tim Peters6d6c1a32001-08-02 04:15:00 +00001893 COPYSLOT(tp_dealloc);
1894 COPYSLOT(tp_print);
1895 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1896 type->tp_getattr = base->tp_getattr;
1897 type->tp_getattro = base->tp_getattro;
1898 }
1899 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1900 type->tp_setattr = base->tp_setattr;
1901 type->tp_setattro = base->tp_setattro;
1902 }
1903 /* tp_compare see tp_richcompare */
1904 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001905 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001906 COPYSLOT(tp_call);
1907 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001908 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001909 if (type->tp_compare == NULL &&
1910 type->tp_richcompare == NULL &&
1911 type->tp_hash == NULL)
1912 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001913 type->tp_compare = base->tp_compare;
1914 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001915 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001916 }
1917 }
1918 else {
1919 COPYSLOT(tp_compare);
1920 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001921 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1922 COPYSLOT(tp_iter);
1923 COPYSLOT(tp_iternext);
1924 }
1925 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1926 COPYSLOT(tp_descr_get);
1927 COPYSLOT(tp_descr_set);
1928 COPYSLOT(tp_dictoffset);
1929 COPYSLOT(tp_init);
1930 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931 COPYSLOT(tp_free);
1932 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001933}
1934
Guido van Rossum13d52f02001-08-10 21:24:08 +00001935staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00001936staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00001937
Tim Peters6d6c1a32001-08-02 04:15:00 +00001938int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001939PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001940{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001941 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001942 PyTypeObject *base;
1943 int i, n;
1944
Guido van Rossumd614f972001-08-10 17:39:49 +00001945 if (type->tp_flags & Py_TPFLAGS_READY) {
1946 assert(type->tp_dict != NULL);
1947 return 0;
1948 }
1949 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00001950
1951 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001952
Guido van Rossumf884b742001-12-17 17:14:22 +00001953 /* Initialize ob_type if NULL. This means extensions that want to be
1954 compilable separately on Windows can call PyType_Ready() instead of
1955 initializing the ob_type field of their type objects. */
1956 if (type->ob_type == NULL)
1957 type->ob_type = &PyType_Type;
1958
Tim Peters6d6c1a32001-08-02 04:15:00 +00001959 /* Initialize tp_base (defaults to BaseObject unless that's us) */
1960 base = type->tp_base;
1961 if (base == NULL && type != &PyBaseObject_Type)
1962 base = type->tp_base = &PyBaseObject_Type;
1963
1964 /* Initialize tp_bases */
1965 bases = type->tp_bases;
1966 if (bases == NULL) {
1967 if (base == NULL)
1968 bases = PyTuple_New(0);
1969 else
1970 bases = Py_BuildValue("(O)", base);
1971 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001972 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001973 type->tp_bases = bases;
1974 }
1975
1976 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00001977 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001978 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001979 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001980 }
1981
Guido van Rossum687ae002001-10-15 22:03:32 +00001982 /* Initialize tp_dict */
1983 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001984 if (dict == NULL) {
1985 dict = PyDict_New();
1986 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00001987 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00001988 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001989 }
1990
Guido van Rossum687ae002001-10-15 22:03:32 +00001991 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001992 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001993 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001994 if (type->tp_methods != NULL) {
1995 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00001996 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001997 }
1998 if (type->tp_members != NULL) {
1999 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002000 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002001 }
2002 if (type->tp_getset != NULL) {
2003 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002004 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002005 }
2006
Tim Peters6d6c1a32001-08-02 04:15:00 +00002007 /* Calculate method resolution order */
2008 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002009 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002010 }
2011
Guido van Rossum13d52f02001-08-10 21:24:08 +00002012 /* Inherit special flags from dominant base */
2013 if (type->tp_base != NULL)
2014 inherit_special(type, type->tp_base);
2015
Tim Peters6d6c1a32001-08-02 04:15:00 +00002016 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002017 bases = type->tp_mro;
2018 assert(bases != NULL);
2019 assert(PyTuple_Check(bases));
2020 n = PyTuple_GET_SIZE(bases);
2021 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002022 PyObject *b = PyTuple_GET_ITEM(bases, i);
2023 if (PyType_Check(b))
2024 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002025 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002026
Guido van Rossum13d52f02001-08-10 21:24:08 +00002027 /* Some more special stuff */
2028 base = type->tp_base;
2029 if (base != NULL) {
2030 if (type->tp_as_number == NULL)
2031 type->tp_as_number = base->tp_as_number;
2032 if (type->tp_as_sequence == NULL)
2033 type->tp_as_sequence = base->tp_as_sequence;
2034 if (type->tp_as_mapping == NULL)
2035 type->tp_as_mapping = base->tp_as_mapping;
2036 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002037
Guido van Rossum1c450732001-10-08 15:18:27 +00002038 /* Link into each base class's list of subclasses */
2039 bases = type->tp_bases;
2040 n = PyTuple_GET_SIZE(bases);
2041 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002042 PyObject *b = PyTuple_GET_ITEM(bases, i);
2043 if (PyType_Check(b) &&
2044 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002045 goto error;
2046 }
2047
Guido van Rossum13d52f02001-08-10 21:24:08 +00002048 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002049 assert(type->tp_dict != NULL);
2050 type->tp_flags =
2051 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002052 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002053
2054 error:
2055 type->tp_flags &= ~Py_TPFLAGS_READYING;
2056 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002057}
2058
Guido van Rossum1c450732001-10-08 15:18:27 +00002059static int
2060add_subclass(PyTypeObject *base, PyTypeObject *type)
2061{
2062 int i;
2063 PyObject *list, *ref, *new;
2064
2065 list = base->tp_subclasses;
2066 if (list == NULL) {
2067 base->tp_subclasses = list = PyList_New(0);
2068 if (list == NULL)
2069 return -1;
2070 }
2071 assert(PyList_Check(list));
2072 new = PyWeakref_NewRef((PyObject *)type, NULL);
2073 i = PyList_GET_SIZE(list);
2074 while (--i >= 0) {
2075 ref = PyList_GET_ITEM(list, i);
2076 assert(PyWeakref_CheckRef(ref));
2077 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2078 return PyList_SetItem(list, i, new);
2079 }
2080 i = PyList_Append(list, new);
2081 Py_DECREF(new);
2082 return i;
2083}
2084
Tim Peters6d6c1a32001-08-02 04:15:00 +00002085
2086/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2087
2088/* There's a wrapper *function* for each distinct function typedef used
2089 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2090 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2091 Most tables have only one entry; the tables for binary operators have two
2092 entries, one regular and one with reversed arguments. */
2093
2094static PyObject *
2095wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2096{
2097 inquiry func = (inquiry)wrapped;
2098 int res;
2099
2100 if (!PyArg_ParseTuple(args, ""))
2101 return NULL;
2102 res = (*func)(self);
2103 if (res == -1 && PyErr_Occurred())
2104 return NULL;
2105 return PyInt_FromLong((long)res);
2106}
2107
Tim Peters6d6c1a32001-08-02 04:15:00 +00002108static PyObject *
2109wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2110{
2111 binaryfunc func = (binaryfunc)wrapped;
2112 PyObject *other;
2113
2114 if (!PyArg_ParseTuple(args, "O", &other))
2115 return NULL;
2116 return (*func)(self, other);
2117}
2118
2119static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002120wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2121{
2122 binaryfunc func = (binaryfunc)wrapped;
2123 PyObject *other;
2124
2125 if (!PyArg_ParseTuple(args, "O", &other))
2126 return NULL;
2127 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002128 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002129 Py_INCREF(Py_NotImplemented);
2130 return Py_NotImplemented;
2131 }
2132 return (*func)(self, other);
2133}
2134
2135static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002136wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2137{
2138 binaryfunc func = (binaryfunc)wrapped;
2139 PyObject *other;
2140
2141 if (!PyArg_ParseTuple(args, "O", &other))
2142 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002143 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002144 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002145 Py_INCREF(Py_NotImplemented);
2146 return Py_NotImplemented;
2147 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002148 return (*func)(other, self);
2149}
2150
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002151static PyObject *
2152wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2153{
2154 coercion func = (coercion)wrapped;
2155 PyObject *other, *res;
2156 int ok;
2157
2158 if (!PyArg_ParseTuple(args, "O", &other))
2159 return NULL;
2160 ok = func(&self, &other);
2161 if (ok < 0)
2162 return NULL;
2163 if (ok > 0) {
2164 Py_INCREF(Py_NotImplemented);
2165 return Py_NotImplemented;
2166 }
2167 res = PyTuple_New(2);
2168 if (res == NULL) {
2169 Py_DECREF(self);
2170 Py_DECREF(other);
2171 return NULL;
2172 }
2173 PyTuple_SET_ITEM(res, 0, self);
2174 PyTuple_SET_ITEM(res, 1, other);
2175 return res;
2176}
2177
Tim Peters6d6c1a32001-08-02 04:15:00 +00002178static PyObject *
2179wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2180{
2181 ternaryfunc func = (ternaryfunc)wrapped;
2182 PyObject *other;
2183 PyObject *third = Py_None;
2184
2185 /* Note: This wrapper only works for __pow__() */
2186
2187 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2188 return NULL;
2189 return (*func)(self, other, third);
2190}
2191
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002192static PyObject *
2193wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2194{
2195 ternaryfunc func = (ternaryfunc)wrapped;
2196 PyObject *other;
2197 PyObject *third = Py_None;
2198
2199 /* Note: This wrapper only works for __pow__() */
2200
2201 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2202 return NULL;
2203 return (*func)(other, self, third);
2204}
2205
Tim Peters6d6c1a32001-08-02 04:15:00 +00002206static PyObject *
2207wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2208{
2209 unaryfunc func = (unaryfunc)wrapped;
2210
2211 if (!PyArg_ParseTuple(args, ""))
2212 return NULL;
2213 return (*func)(self);
2214}
2215
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216static PyObject *
2217wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2218{
2219 intargfunc func = (intargfunc)wrapped;
2220 int i;
2221
2222 if (!PyArg_ParseTuple(args, "i", &i))
2223 return NULL;
2224 return (*func)(self, i);
2225}
2226
Guido van Rossum5d815f32001-08-17 21:57:47 +00002227static int
2228getindex(PyObject *self, PyObject *arg)
2229{
2230 int i;
2231
2232 i = PyInt_AsLong(arg);
2233 if (i == -1 && PyErr_Occurred())
2234 return -1;
2235 if (i < 0) {
2236 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2237 if (sq && sq->sq_length) {
2238 int n = (*sq->sq_length)(self);
2239 if (n < 0)
2240 return -1;
2241 i += n;
2242 }
2243 }
2244 return i;
2245}
2246
2247static PyObject *
2248wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2249{
2250 intargfunc func = (intargfunc)wrapped;
2251 PyObject *arg;
2252 int i;
2253
Guido van Rossumf4593e02001-10-03 12:09:30 +00002254 if (PyTuple_GET_SIZE(args) == 1) {
2255 arg = PyTuple_GET_ITEM(args, 0);
2256 i = getindex(self, arg);
2257 if (i == -1 && PyErr_Occurred())
2258 return NULL;
2259 return (*func)(self, i);
2260 }
2261 PyArg_ParseTuple(args, "O", &arg);
2262 assert(PyErr_Occurred());
2263 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002264}
2265
Tim Peters6d6c1a32001-08-02 04:15:00 +00002266static PyObject *
2267wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2268{
2269 intintargfunc func = (intintargfunc)wrapped;
2270 int i, j;
2271
2272 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2273 return NULL;
2274 return (*func)(self, i, j);
2275}
2276
Tim Peters6d6c1a32001-08-02 04:15:00 +00002277static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002278wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002279{
2280 intobjargproc func = (intobjargproc)wrapped;
2281 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002282 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002283
Guido van Rossum5d815f32001-08-17 21:57:47 +00002284 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2285 return NULL;
2286 i = getindex(self, arg);
2287 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002288 return NULL;
2289 res = (*func)(self, i, value);
2290 if (res == -1 && PyErr_Occurred())
2291 return NULL;
2292 Py_INCREF(Py_None);
2293 return Py_None;
2294}
2295
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002296static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002297wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002298{
2299 intobjargproc func = (intobjargproc)wrapped;
2300 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002301 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002302
Guido van Rossum5d815f32001-08-17 21:57:47 +00002303 if (!PyArg_ParseTuple(args, "O", &arg))
2304 return NULL;
2305 i = getindex(self, arg);
2306 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002307 return NULL;
2308 res = (*func)(self, i, NULL);
2309 if (res == -1 && PyErr_Occurred())
2310 return NULL;
2311 Py_INCREF(Py_None);
2312 return Py_None;
2313}
2314
Tim Peters6d6c1a32001-08-02 04:15:00 +00002315static PyObject *
2316wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2317{
2318 intintobjargproc func = (intintobjargproc)wrapped;
2319 int i, j, res;
2320 PyObject *value;
2321
2322 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2323 return NULL;
2324 res = (*func)(self, i, j, value);
2325 if (res == -1 && PyErr_Occurred())
2326 return NULL;
2327 Py_INCREF(Py_None);
2328 return Py_None;
2329}
2330
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002331static PyObject *
2332wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2333{
2334 intintobjargproc func = (intintobjargproc)wrapped;
2335 int i, j, res;
2336
2337 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2338 return NULL;
2339 res = (*func)(self, i, j, NULL);
2340 if (res == -1 && PyErr_Occurred())
2341 return NULL;
2342 Py_INCREF(Py_None);
2343 return Py_None;
2344}
2345
Tim Peters6d6c1a32001-08-02 04:15:00 +00002346/* XXX objobjproc is a misnomer; should be objargpred */
2347static PyObject *
2348wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2349{
2350 objobjproc func = (objobjproc)wrapped;
2351 int res;
2352 PyObject *value;
2353
2354 if (!PyArg_ParseTuple(args, "O", &value))
2355 return NULL;
2356 res = (*func)(self, value);
2357 if (res == -1 && PyErr_Occurred())
2358 return NULL;
2359 return PyInt_FromLong((long)res);
2360}
2361
Tim Peters6d6c1a32001-08-02 04:15:00 +00002362static PyObject *
2363wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2364{
2365 objobjargproc func = (objobjargproc)wrapped;
2366 int res;
2367 PyObject *key, *value;
2368
2369 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2370 return NULL;
2371 res = (*func)(self, key, value);
2372 if (res == -1 && PyErr_Occurred())
2373 return NULL;
2374 Py_INCREF(Py_None);
2375 return Py_None;
2376}
2377
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002378static PyObject *
2379wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2380{
2381 objobjargproc func = (objobjargproc)wrapped;
2382 int res;
2383 PyObject *key;
2384
2385 if (!PyArg_ParseTuple(args, "O", &key))
2386 return NULL;
2387 res = (*func)(self, key, NULL);
2388 if (res == -1 && PyErr_Occurred())
2389 return NULL;
2390 Py_INCREF(Py_None);
2391 return Py_None;
2392}
2393
Tim Peters6d6c1a32001-08-02 04:15:00 +00002394static PyObject *
2395wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2396{
2397 cmpfunc func = (cmpfunc)wrapped;
2398 int res;
2399 PyObject *other;
2400
2401 if (!PyArg_ParseTuple(args, "O", &other))
2402 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002403 if (other->ob_type->tp_compare != func &&
2404 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002405 PyErr_Format(
2406 PyExc_TypeError,
2407 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2408 self->ob_type->tp_name,
2409 self->ob_type->tp_name,
2410 other->ob_type->tp_name);
2411 return NULL;
2412 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002413 res = (*func)(self, other);
2414 if (PyErr_Occurred())
2415 return NULL;
2416 return PyInt_FromLong((long)res);
2417}
2418
Tim Peters6d6c1a32001-08-02 04:15:00 +00002419static PyObject *
2420wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2421{
2422 setattrofunc func = (setattrofunc)wrapped;
2423 int res;
2424 PyObject *name, *value;
2425
2426 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2427 return NULL;
2428 res = (*func)(self, name, value);
2429 if (res < 0)
2430 return NULL;
2431 Py_INCREF(Py_None);
2432 return Py_None;
2433}
2434
2435static PyObject *
2436wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2437{
2438 setattrofunc func = (setattrofunc)wrapped;
2439 int res;
2440 PyObject *name;
2441
2442 if (!PyArg_ParseTuple(args, "O", &name))
2443 return NULL;
2444 res = (*func)(self, name, NULL);
2445 if (res < 0)
2446 return NULL;
2447 Py_INCREF(Py_None);
2448 return Py_None;
2449}
2450
Tim Peters6d6c1a32001-08-02 04:15:00 +00002451static PyObject *
2452wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2453{
2454 hashfunc func = (hashfunc)wrapped;
2455 long res;
2456
2457 if (!PyArg_ParseTuple(args, ""))
2458 return NULL;
2459 res = (*func)(self);
2460 if (res == -1 && PyErr_Occurred())
2461 return NULL;
2462 return PyInt_FromLong(res);
2463}
2464
Tim Peters6d6c1a32001-08-02 04:15:00 +00002465static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002466wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002467{
2468 ternaryfunc func = (ternaryfunc)wrapped;
2469
Guido van Rossumc8e56452001-10-22 00:43:43 +00002470 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002471}
2472
Tim Peters6d6c1a32001-08-02 04:15:00 +00002473static PyObject *
2474wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2475{
2476 richcmpfunc func = (richcmpfunc)wrapped;
2477 PyObject *other;
2478
2479 if (!PyArg_ParseTuple(args, "O", &other))
2480 return NULL;
2481 return (*func)(self, other, op);
2482}
2483
2484#undef RICHCMP_WRAPPER
2485#define RICHCMP_WRAPPER(NAME, OP) \
2486static PyObject * \
2487richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2488{ \
2489 return wrap_richcmpfunc(self, args, wrapped, OP); \
2490}
2491
Jack Jansen8e938b42001-08-08 15:29:49 +00002492RICHCMP_WRAPPER(lt, Py_LT)
2493RICHCMP_WRAPPER(le, Py_LE)
2494RICHCMP_WRAPPER(eq, Py_EQ)
2495RICHCMP_WRAPPER(ne, Py_NE)
2496RICHCMP_WRAPPER(gt, Py_GT)
2497RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002498
Tim Peters6d6c1a32001-08-02 04:15:00 +00002499static PyObject *
2500wrap_next(PyObject *self, PyObject *args, void *wrapped)
2501{
2502 unaryfunc func = (unaryfunc)wrapped;
2503 PyObject *res;
2504
2505 if (!PyArg_ParseTuple(args, ""))
2506 return NULL;
2507 res = (*func)(self);
2508 if (res == NULL && !PyErr_Occurred())
2509 PyErr_SetNone(PyExc_StopIteration);
2510 return res;
2511}
2512
Tim Peters6d6c1a32001-08-02 04:15:00 +00002513static PyObject *
2514wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2515{
2516 descrgetfunc func = (descrgetfunc)wrapped;
2517 PyObject *obj;
2518 PyObject *type = NULL;
2519
2520 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2521 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002522 return (*func)(self, obj, type);
2523}
2524
Tim Peters6d6c1a32001-08-02 04:15:00 +00002525static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002526wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002527{
2528 descrsetfunc func = (descrsetfunc)wrapped;
2529 PyObject *obj, *value;
2530 int ret;
2531
2532 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2533 return NULL;
2534 ret = (*func)(self, obj, value);
2535 if (ret < 0)
2536 return NULL;
2537 Py_INCREF(Py_None);
2538 return Py_None;
2539}
2540
Tim Peters6d6c1a32001-08-02 04:15:00 +00002541static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002542wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002543{
2544 initproc func = (initproc)wrapped;
2545
Guido van Rossumc8e56452001-10-22 00:43:43 +00002546 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002547 return NULL;
2548 Py_INCREF(Py_None);
2549 return Py_None;
2550}
2551
Tim Peters6d6c1a32001-08-02 04:15:00 +00002552static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002553tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002554{
Barry Warsaw60f01882001-08-22 19:24:42 +00002555 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002556 PyObject *arg0, *res;
2557
2558 if (self == NULL || !PyType_Check(self))
2559 Py_FatalError("__new__() called with non-type 'self'");
2560 type = (PyTypeObject *)self;
2561 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002562 PyErr_Format(PyExc_TypeError,
2563 "%s.__new__(): not enough arguments",
2564 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002565 return NULL;
2566 }
2567 arg0 = PyTuple_GET_ITEM(args, 0);
2568 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002569 PyErr_Format(PyExc_TypeError,
2570 "%s.__new__(X): X is not a type object (%s)",
2571 type->tp_name,
2572 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002573 return NULL;
2574 }
2575 subtype = (PyTypeObject *)arg0;
2576 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002577 PyErr_Format(PyExc_TypeError,
2578 "%s.__new__(%s): %s is not a subtype of %s",
2579 type->tp_name,
2580 subtype->tp_name,
2581 subtype->tp_name,
2582 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002583 return NULL;
2584 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002585
2586 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002587 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002588 most derived base that's not a heap type is this type. */
2589 staticbase = subtype;
2590 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2591 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002592 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002593 PyErr_Format(PyExc_TypeError,
2594 "%s.__new__(%s) is not safe, use %s.__new__()",
2595 type->tp_name,
2596 subtype->tp_name,
2597 staticbase == NULL ? "?" : staticbase->tp_name);
2598 return NULL;
2599 }
2600
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002601 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2602 if (args == NULL)
2603 return NULL;
2604 res = type->tp_new(subtype, args, kwds);
2605 Py_DECREF(args);
2606 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002607}
2608
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002609static struct PyMethodDef tp_new_methoddef[] = {
2610 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2611 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002612 {0}
2613};
2614
2615static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002616add_tp_new_wrapper(PyTypeObject *type)
2617{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002618 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002619
Guido van Rossum687ae002001-10-15 22:03:32 +00002620 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002621 return 0;
2622 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002623 if (func == NULL)
2624 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002625 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002626}
2627
Guido van Rossumf040ede2001-08-07 16:40:56 +00002628/* Slot wrappers that call the corresponding __foo__ slot. See comments
2629 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002630
Guido van Rossumdc91b992001-08-08 22:26:22 +00002631#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002632static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002633FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002634{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002635 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002636 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002637}
2638
Guido van Rossumdc91b992001-08-08 22:26:22 +00002639#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002640static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002641FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002643 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002644 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002645}
2646
Guido van Rossumdc91b992001-08-08 22:26:22 +00002647
2648#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002649static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002650FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002651{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002652 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002653 int do_other = self->ob_type != other->ob_type && \
2654 other->ob_type->tp_as_number != NULL && \
2655 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002656 if (self->ob_type->tp_as_number != NULL && \
2657 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2658 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002659 if (do_other && \
2660 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2661 r = call_maybe( \
2662 other, ROPSTR, &rcache_str, "(O)", self); \
2663 if (r != Py_NotImplemented) \
2664 return r; \
2665 Py_DECREF(r); \
2666 do_other = 0; \
2667 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002668 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002669 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002670 if (r != Py_NotImplemented || \
2671 other->ob_type == self->ob_type) \
2672 return r; \
2673 Py_DECREF(r); \
2674 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002675 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002676 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002677 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002678 } \
2679 Py_INCREF(Py_NotImplemented); \
2680 return Py_NotImplemented; \
2681}
2682
2683#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2684 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2685
2686#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2687static PyObject * \
2688FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2689{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002690 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002691 return call_method(self, OPSTR, &cache_str, \
2692 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002693}
2694
2695static int
2696slot_sq_length(PyObject *self)
2697{
Guido van Rossum2730b132001-08-28 18:22:14 +00002698 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002699 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002700 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002701
2702 if (res == NULL)
2703 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002704 len = (int)PyInt_AsLong(res);
2705 Py_DECREF(res);
2706 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002707}
2708
Guido van Rossumdc91b992001-08-08 22:26:22 +00002709SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2710SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002711
2712/* Super-optimized version of slot_sq_item.
2713 Other slots could do the same... */
2714static PyObject *
2715slot_sq_item(PyObject *self, int i)
2716{
2717 static PyObject *getitem_str;
2718 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2719 descrgetfunc f;
2720
2721 if (getitem_str == NULL) {
2722 getitem_str = PyString_InternFromString("__getitem__");
2723 if (getitem_str == NULL)
2724 return NULL;
2725 }
2726 func = _PyType_Lookup(self->ob_type, getitem_str);
2727 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002728 if ((f = func->ob_type->tp_descr_get) == NULL)
2729 Py_INCREF(func);
2730 else
2731 func = f(func, self, (PyObject *)(self->ob_type));
2732 ival = PyInt_FromLong(i);
2733 if (ival != NULL) {
2734 args = PyTuple_New(1);
2735 if (args != NULL) {
2736 PyTuple_SET_ITEM(args, 0, ival);
2737 retval = PyObject_Call(func, args, NULL);
2738 Py_XDECREF(args);
2739 Py_XDECREF(func);
2740 return retval;
2741 }
2742 }
2743 }
2744 else {
2745 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2746 }
2747 Py_XDECREF(args);
2748 Py_XDECREF(ival);
2749 Py_XDECREF(func);
2750 return NULL;
2751}
2752
Guido van Rossumdc91b992001-08-08 22:26:22 +00002753SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002754
2755static int
2756slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2757{
2758 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002759 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002760
2761 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002762 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002763 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002764 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002765 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002766 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002767 if (res == NULL)
2768 return -1;
2769 Py_DECREF(res);
2770 return 0;
2771}
2772
2773static int
2774slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2775{
2776 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002777 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002778
2779 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002780 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002781 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002782 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002783 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002784 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002785 if (res == NULL)
2786 return -1;
2787 Py_DECREF(res);
2788 return 0;
2789}
2790
2791static int
2792slot_sq_contains(PyObject *self, PyObject *value)
2793{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002794 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002795 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002796
Guido van Rossum55f20992001-10-01 17:18:22 +00002797 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002798
2799 if (func != NULL) {
2800 args = Py_BuildValue("(O)", value);
2801 if (args == NULL)
2802 res = NULL;
2803 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002804 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002805 Py_DECREF(args);
2806 }
2807 Py_DECREF(func);
2808 if (res == NULL)
2809 return -1;
2810 return PyObject_IsTrue(res);
2811 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002812 else if (PyErr_Occurred())
2813 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002814 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002815 return _PySequence_IterSearch(self, value,
2816 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002817 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002818}
2819
Guido van Rossumdc91b992001-08-08 22:26:22 +00002820SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2821SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002822
2823#define slot_mp_length slot_sq_length
2824
Guido van Rossumdc91b992001-08-08 22:26:22 +00002825SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002826
2827static int
2828slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2829{
2830 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002831 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002832
2833 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002834 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002835 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002836 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002837 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002838 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002839 if (res == NULL)
2840 return -1;
2841 Py_DECREF(res);
2842 return 0;
2843}
2844
Guido van Rossumdc91b992001-08-08 22:26:22 +00002845SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2846SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2847SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2848SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2849SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2850SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2851
2852staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2853
2854SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2855 nb_power, "__pow__", "__rpow__")
2856
2857static PyObject *
2858slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2859{
Guido van Rossum2730b132001-08-28 18:22:14 +00002860 static PyObject *pow_str;
2861
Guido van Rossumdc91b992001-08-08 22:26:22 +00002862 if (modulus == Py_None)
2863 return slot_nb_power_binary(self, other);
2864 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002865 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002866 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002867}
2868
2869SLOT0(slot_nb_negative, "__neg__")
2870SLOT0(slot_nb_positive, "__pos__")
2871SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002872
2873static int
2874slot_nb_nonzero(PyObject *self)
2875{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002876 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002877 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002878
Guido van Rossum55f20992001-10-01 17:18:22 +00002879 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002880 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002881 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002882 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002883 func = lookup_maybe(self, "__len__", &len_str);
2884 if (func == NULL) {
2885 if (PyErr_Occurred())
2886 return -1;
2887 else
2888 return 1;
2889 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002890 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002891 res = PyObject_CallObject(func, NULL);
2892 Py_DECREF(func);
2893 if (res == NULL)
2894 return -1;
2895 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002896}
2897
Guido van Rossumdc91b992001-08-08 22:26:22 +00002898SLOT0(slot_nb_invert, "__invert__")
2899SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2900SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2901SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2902SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2903SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002904
2905static int
2906slot_nb_coerce(PyObject **a, PyObject **b)
2907{
2908 static PyObject *coerce_str;
2909 PyObject *self = *a, *other = *b;
2910
2911 if (self->ob_type->tp_as_number != NULL &&
2912 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2913 PyObject *r;
2914 r = call_maybe(
2915 self, "__coerce__", &coerce_str, "(O)", other);
2916 if (r == NULL)
2917 return -1;
2918 if (r == Py_NotImplemented) {
2919 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002920 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002921 else {
2922 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2923 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002924 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00002925 Py_DECREF(r);
2926 return -1;
2927 }
2928 *a = PyTuple_GET_ITEM(r, 0);
2929 Py_INCREF(*a);
2930 *b = PyTuple_GET_ITEM(r, 1);
2931 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002932 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00002933 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002934 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002935 }
2936 if (other->ob_type->tp_as_number != NULL &&
2937 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
2938 PyObject *r;
2939 r = call_maybe(
2940 other, "__coerce__", &coerce_str, "(O)", self);
2941 if (r == NULL)
2942 return -1;
2943 if (r == Py_NotImplemented) {
2944 Py_DECREF(r);
2945 return 1;
2946 }
2947 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
2948 PyErr_SetString(PyExc_TypeError,
2949 "__coerce__ didn't return a 2-tuple");
2950 Py_DECREF(r);
2951 return -1;
2952 }
2953 *a = PyTuple_GET_ITEM(r, 1);
2954 Py_INCREF(*a);
2955 *b = PyTuple_GET_ITEM(r, 0);
2956 Py_INCREF(*b);
2957 Py_DECREF(r);
2958 return 0;
2959 }
2960 return 1;
2961}
2962
Guido van Rossumdc91b992001-08-08 22:26:22 +00002963SLOT0(slot_nb_int, "__int__")
2964SLOT0(slot_nb_long, "__long__")
2965SLOT0(slot_nb_float, "__float__")
2966SLOT0(slot_nb_oct, "__oct__")
2967SLOT0(slot_nb_hex, "__hex__")
2968SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
2969SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
2970SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
2971SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
2972SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
2973SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
2974SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
2975SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
2976SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
2977SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
2978SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
2979SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
2980 "__floordiv__", "__rfloordiv__")
2981SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
2982SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
2983SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002984
2985static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00002986half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002987{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002988 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002989 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002990 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002991
Guido van Rossum60718732001-08-28 17:47:51 +00002992 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002993 if (func == NULL) {
2994 PyErr_Clear();
2995 }
2996 else {
2997 args = Py_BuildValue("(O)", other);
2998 if (args == NULL)
2999 res = NULL;
3000 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003001 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003002 Py_DECREF(args);
3003 }
3004 if (res != Py_NotImplemented) {
3005 if (res == NULL)
3006 return -2;
3007 c = PyInt_AsLong(res);
3008 Py_DECREF(res);
3009 if (c == -1 && PyErr_Occurred())
3010 return -2;
3011 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3012 }
3013 Py_DECREF(res);
3014 }
3015 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003016}
3017
Guido van Rossumab3b0342001-09-18 20:38:53 +00003018/* This slot is published for the benefit of try_3way_compare in object.c */
3019int
3020_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003021{
3022 int c;
3023
Guido van Rossumab3b0342001-09-18 20:38:53 +00003024 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003025 c = half_compare(self, other);
3026 if (c <= 1)
3027 return c;
3028 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003029 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003030 c = half_compare(other, self);
3031 if (c < -1)
3032 return -2;
3033 if (c <= 1)
3034 return -c;
3035 }
3036 return (void *)self < (void *)other ? -1 :
3037 (void *)self > (void *)other ? 1 : 0;
3038}
3039
3040static PyObject *
3041slot_tp_repr(PyObject *self)
3042{
3043 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003044 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003045
Guido van Rossum60718732001-08-28 17:47:51 +00003046 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003047 if (func != NULL) {
3048 res = PyEval_CallObject(func, NULL);
3049 Py_DECREF(func);
3050 return res;
3051 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003052 PyErr_Clear();
3053 return PyString_FromFormat("<%s object at %p>",
3054 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003055}
3056
3057static PyObject *
3058slot_tp_str(PyObject *self)
3059{
3060 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003061 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003062
Guido van Rossum60718732001-08-28 17:47:51 +00003063 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003064 if (func != NULL) {
3065 res = PyEval_CallObject(func, NULL);
3066 Py_DECREF(func);
3067 return res;
3068 }
3069 else {
3070 PyErr_Clear();
3071 return slot_tp_repr(self);
3072 }
3073}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074
3075static long
3076slot_tp_hash(PyObject *self)
3077{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003078 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003079 static PyObject *hash_str, *eq_str, *cmp_str;
3080
Tim Peters6d6c1a32001-08-02 04:15:00 +00003081 long h;
3082
Guido van Rossum60718732001-08-28 17:47:51 +00003083 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003084
3085 if (func != NULL) {
3086 res = PyEval_CallObject(func, NULL);
3087 Py_DECREF(func);
3088 if (res == NULL)
3089 return -1;
3090 h = PyInt_AsLong(res);
3091 }
3092 else {
3093 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003094 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003095 if (func == NULL) {
3096 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003097 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003098 }
3099 if (func != NULL) {
3100 Py_DECREF(func);
3101 PyErr_SetString(PyExc_TypeError, "unhashable type");
3102 return -1;
3103 }
3104 PyErr_Clear();
3105 h = _Py_HashPointer((void *)self);
3106 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003107 if (h == -1 && !PyErr_Occurred())
3108 h = -2;
3109 return h;
3110}
3111
3112static PyObject *
3113slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3114{
Guido van Rossum60718732001-08-28 17:47:51 +00003115 static PyObject *call_str;
3116 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003117 PyObject *res;
3118
3119 if (meth == NULL)
3120 return NULL;
3121 res = PyObject_Call(meth, args, kwds);
3122 Py_DECREF(meth);
3123 return res;
3124}
3125
Guido van Rossum14a6f832001-10-17 13:59:09 +00003126/* There are two slot dispatch functions for tp_getattro.
3127
3128 - slot_tp_getattro() is used when __getattribute__ is overridden
3129 but no __getattr__ hook is present;
3130
3131 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3132
3133 The code in update_slot() and fixup_slot_dispatchers() always installs
3134 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
3135 installs the simpler slot if necessary. */
3136
Tim Peters6d6c1a32001-08-02 04:15:00 +00003137static PyObject *
3138slot_tp_getattro(PyObject *self, PyObject *name)
3139{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003140 static PyObject *getattribute_str = NULL;
3141 return call_method(self, "__getattribute__", &getattribute_str,
3142 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003143}
3144
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003145static PyObject *
3146slot_tp_getattr_hook(PyObject *self, PyObject *name)
3147{
3148 PyTypeObject *tp = self->ob_type;
3149 PyObject *getattr, *getattribute, *res;
3150 static PyObject *getattribute_str = NULL;
3151 static PyObject *getattr_str = NULL;
3152
3153 if (getattr_str == NULL) {
3154 getattr_str = PyString_InternFromString("__getattr__");
3155 if (getattr_str == NULL)
3156 return NULL;
3157 }
3158 if (getattribute_str == NULL) {
3159 getattribute_str =
3160 PyString_InternFromString("__getattribute__");
3161 if (getattribute_str == NULL)
3162 return NULL;
3163 }
3164 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003165 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003166 /* No __getattr__ hook: use a simpler dispatcher */
3167 tp->tp_getattro = slot_tp_getattro;
3168 return slot_tp_getattro(self, name);
3169 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003170 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003171 if (getattribute == NULL ||
3172 (getattribute->ob_type == &PyWrapperDescr_Type &&
3173 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3174 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003175 res = PyObject_GenericGetAttr(self, name);
3176 else
3177 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003178 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003179 PyErr_Clear();
3180 res = PyObject_CallFunction(getattr, "OO", self, name);
3181 }
3182 return res;
3183}
3184
Tim Peters6d6c1a32001-08-02 04:15:00 +00003185static int
3186slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3187{
3188 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003189 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003190
3191 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003192 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003193 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003194 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003195 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003196 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003197 if (res == NULL)
3198 return -1;
3199 Py_DECREF(res);
3200 return 0;
3201}
3202
3203/* Map rich comparison operators to their __xx__ namesakes */
3204static char *name_op[] = {
3205 "__lt__",
3206 "__le__",
3207 "__eq__",
3208 "__ne__",
3209 "__gt__",
3210 "__ge__",
3211};
3212
3213static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003214half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003215{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003216 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003217 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003218
Guido van Rossum60718732001-08-28 17:47:51 +00003219 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003220 if (func == NULL) {
3221 PyErr_Clear();
3222 Py_INCREF(Py_NotImplemented);
3223 return Py_NotImplemented;
3224 }
3225 args = Py_BuildValue("(O)", other);
3226 if (args == NULL)
3227 res = NULL;
3228 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003229 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003230 Py_DECREF(args);
3231 }
3232 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003233 return res;
3234}
3235
Guido van Rossumb8f63662001-08-15 23:57:02 +00003236/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3237static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3238
3239static PyObject *
3240slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3241{
3242 PyObject *res;
3243
3244 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3245 res = half_richcompare(self, other, op);
3246 if (res != Py_NotImplemented)
3247 return res;
3248 Py_DECREF(res);
3249 }
3250 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3251 res = half_richcompare(other, self, swapped_op[op]);
3252 if (res != Py_NotImplemented) {
3253 return res;
3254 }
3255 Py_DECREF(res);
3256 }
3257 Py_INCREF(Py_NotImplemented);
3258 return Py_NotImplemented;
3259}
3260
3261static PyObject *
3262slot_tp_iter(PyObject *self)
3263{
3264 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003265 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003266
Guido van Rossum60718732001-08-28 17:47:51 +00003267 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003268 if (func != NULL) {
3269 res = PyObject_CallObject(func, NULL);
3270 Py_DECREF(func);
3271 return res;
3272 }
3273 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003274 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003275 if (func == NULL) {
Tim Peters8b13b3e2001-09-30 05:58:42 +00003276 PyErr_SetString(PyExc_TypeError, "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003277 return NULL;
3278 }
3279 Py_DECREF(func);
3280 return PySeqIter_New(self);
3281}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003282
3283static PyObject *
3284slot_tp_iternext(PyObject *self)
3285{
Guido van Rossum2730b132001-08-28 18:22:14 +00003286 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003287 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003288}
3289
Guido van Rossum1a493502001-08-17 16:47:50 +00003290static PyObject *
3291slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3292{
3293 PyTypeObject *tp = self->ob_type;
3294 PyObject *get;
3295 static PyObject *get_str = NULL;
3296
3297 if (get_str == NULL) {
3298 get_str = PyString_InternFromString("__get__");
3299 if (get_str == NULL)
3300 return NULL;
3301 }
3302 get = _PyType_Lookup(tp, get_str);
3303 if (get == NULL) {
3304 /* Avoid further slowdowns */
3305 if (tp->tp_descr_get == slot_tp_descr_get)
3306 tp->tp_descr_get = NULL;
3307 Py_INCREF(self);
3308 return self;
3309 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003310 if (obj == NULL)
3311 obj = Py_None;
3312 if (type == NULL)
3313 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003314 return PyObject_CallFunction(get, "OOO", self, obj, type);
3315}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003316
3317static int
3318slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3319{
Guido van Rossum2c252392001-08-24 10:13:31 +00003320 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003321 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003322
3323 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003324 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003325 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003326 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003327 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003328 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003329 if (res == NULL)
3330 return -1;
3331 Py_DECREF(res);
3332 return 0;
3333}
3334
3335static int
3336slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3337{
Guido van Rossum60718732001-08-28 17:47:51 +00003338 static PyObject *init_str;
3339 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003340 PyObject *res;
3341
3342 if (meth == NULL)
3343 return -1;
3344 res = PyObject_Call(meth, args, kwds);
3345 Py_DECREF(meth);
3346 if (res == NULL)
3347 return -1;
3348 Py_DECREF(res);
3349 return 0;
3350}
3351
3352static PyObject *
3353slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3354{
3355 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3356 PyObject *newargs, *x;
3357 int i, n;
3358
3359 if (func == NULL)
3360 return NULL;
3361 assert(PyTuple_Check(args));
3362 n = PyTuple_GET_SIZE(args);
3363 newargs = PyTuple_New(n+1);
3364 if (newargs == NULL)
3365 return NULL;
3366 Py_INCREF(type);
3367 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3368 for (i = 0; i < n; i++) {
3369 x = PyTuple_GET_ITEM(args, i);
3370 Py_INCREF(x);
3371 PyTuple_SET_ITEM(newargs, i+1, x);
3372 }
3373 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003374 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003375 Py_DECREF(func);
3376 return x;
3377}
3378
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003379
3380/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3381 functions. The offsets here are relative to the 'etype' structure, which
3382 incorporates the additional structures used for numbers, sequences and
3383 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3384 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3385 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3386
Guido van Rossum6d204072001-10-21 00:44:31 +00003387typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003388
3389#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003390#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003391#undef ETSLOT
3392#undef SQSLOT
3393#undef MPSLOT
3394#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003395#undef UNSLOT
3396#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003397#undef BINSLOT
3398#undef RBINSLOT
3399
Guido van Rossum6d204072001-10-21 00:44:31 +00003400#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3401 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003402#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3403 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3404 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003405#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3406 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3407#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3408 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3409#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3410 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3411#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3412 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3413#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3414 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3415 "x." NAME "() <==> " DOC)
3416#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3417 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3418 "x." NAME "(y) <==> x" DOC "y")
3419#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3420 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3421 "x." NAME "(y) <==> x" DOC "y")
3422#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3423 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3424 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003425
3426static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003427 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3428 "x.__len__() <==> len(x)"),
3429 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3430 "x.__add__(y) <==> x+y"),
3431 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3432 "x.__mul__(n) <==> x*n"),
3433 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3434 "x.__rmul__(n) <==> n*x"),
3435 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3436 "x.__getitem__(y) <==> x[y]"),
3437 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3438 "x.__getslice__(i, j) <==> x[i:j]"),
3439 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3440 "x.__setitem__(i, y) <==> x[i]=y"),
3441 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3442 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003443 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003444 wrap_intintobjargproc,
3445 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3446 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3447 "x.__delslice__(i, j) <==> del x[i:j]"),
3448 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3449 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003450 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003451 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003452 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003453 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003454
Guido van Rossum6d204072001-10-21 00:44:31 +00003455 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3456 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003457 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003458 wrap_binaryfunc,
3459 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003460 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003461 wrap_objobjargproc,
3462 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003463 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003464 wrap_delitem,
3465 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003466
Guido van Rossum6d204072001-10-21 00:44:31 +00003467 BINSLOT("__add__", nb_add, slot_nb_add,
3468 "+"),
3469 RBINSLOT("__radd__", nb_add, slot_nb_add,
3470 "+"),
3471 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3472 "-"),
3473 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3474 "-"),
3475 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3476 "*"),
3477 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3478 "*"),
3479 BINSLOT("__div__", nb_divide, slot_nb_divide,
3480 "/"),
3481 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3482 "/"),
3483 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3484 "%"),
3485 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3486 "%"),
3487 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3488 "divmod(x, y)"),
3489 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3490 "divmod(y, x)"),
3491 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3492 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3493 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3494 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3495 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3496 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3497 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3498 "abs(x)"),
3499 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_unaryfunc,
3500 "x != 0"),
3501 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3502 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3503 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3504 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3505 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3506 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3507 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3508 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3509 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3510 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3511 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3512 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3513 "x.__coerce__(y) <==> coerce(x, y)"),
3514 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3515 "int(x)"),
3516 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3517 "long(x)"),
3518 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3519 "float(x)"),
3520 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3521 "oct(x)"),
3522 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3523 "hex(x)"),
3524 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3525 wrap_binaryfunc, "+"),
3526 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3527 wrap_binaryfunc, "-"),
3528 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3529 wrap_binaryfunc, "*"),
3530 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3531 wrap_binaryfunc, "/"),
3532 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3533 wrap_binaryfunc, "%"),
3534 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3535 wrap_ternaryfunc, "**"),
3536 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3537 wrap_binaryfunc, "<<"),
3538 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3539 wrap_binaryfunc, ">>"),
3540 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3541 wrap_binaryfunc, "&"),
3542 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3543 wrap_binaryfunc, "^"),
3544 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3545 wrap_binaryfunc, "|"),
3546 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3547 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3548 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3549 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3550 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3551 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3552 IBSLOT("__itruediv__", nb_inplace_true_divide,
3553 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003554
Guido van Rossum6d204072001-10-21 00:44:31 +00003555 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3556 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003557 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003558 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3559 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003560 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003561 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3562 "x.__cmp__(y) <==> cmp(x,y)"),
3563 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3564 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003565 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3566 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003567 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003568 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3569 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3570 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3571 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3572 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3573 "x.__setattr__('name', value) <==> x.name = value"),
3574 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3575 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3576 "x.__delattr__('name') <==> del x.name"),
3577 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3578 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3579 "x.__lt__(y) <==> x<y"),
3580 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3581 "x.__le__(y) <==> x<=y"),
3582 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3583 "x.__eq__(y) <==> x==y"),
3584 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3585 "x.__ne__(y) <==> x!=y"),
3586 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3587 "x.__gt__(y) <==> x>y"),
3588 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3589 "x.__ge__(y) <==> x>=y"),
3590 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3591 "x.__iter__() <==> iter(x)"),
3592 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3593 "x.next() -> the next value, or raise StopIteration"),
3594 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3595 "descr.__get__(obj[, type]) -> value"),
3596 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3597 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003598 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003599 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003600 "see x.__class__.__doc__ for signature",
3601 PyWrapperFlag_KEYWORDS),
3602 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003603 {NULL}
3604};
3605
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003606static void **
3607slotptr(PyTypeObject *type, int offset)
3608{
3609 char *ptr;
3610
3611 assert(offset >= 0);
3612 assert(offset < offsetof(etype, as_buffer));
3613 if (offset >= offsetof(etype, as_mapping)) {
3614 ptr = (void *)type->tp_as_mapping;
3615 offset -= offsetof(etype, as_mapping);
3616 }
3617 else if (offset >= offsetof(etype, as_sequence)) {
3618 ptr = (void *)type->tp_as_sequence;
3619 offset -= offsetof(etype, as_sequence);
3620 }
3621 else if (offset >= offsetof(etype, as_number)) {
3622 ptr = (void *)type->tp_as_number;
3623 offset -= offsetof(etype, as_number);
3624 }
3625 else {
3626 ptr = (void *)type;
3627 }
3628 if (ptr != NULL)
3629 ptr += offset;
3630 return (void **)ptr;
3631}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003632
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003633staticforward int recurse_down_subclasses(PyTypeObject *type,
3634 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003635
3636static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003637update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003638{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003639 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003640
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003641 for (pp = pp0; *pp; pp++) {
3642 slotdef *p = *pp;
3643 PyObject *descr;
3644 PyWrapperDescrObject *d;
3645 void *generic = NULL, *specific = NULL;
3646 int use_generic = 0;
3647 int offset = p->offset;
3648 void **ptr = slotptr(type, offset);
3649 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003650 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003651 do {
3652 descr = _PyType_Lookup(type, p->name_strobj);
3653 if (descr == NULL)
3654 continue;
3655 generic = p->function;
3656 if (descr->ob_type == &PyWrapperDescr_Type) {
3657 d = (PyWrapperDescrObject *)descr;
3658 if (d->d_base->wrapper == p->wrapper &&
3659 PyType_IsSubtype(type, d->d_type)) {
3660 if (specific == NULL ||
3661 specific == d->d_wrapped)
3662 specific = d->d_wrapped;
3663 else
3664 use_generic = 1;
3665 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003666 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003667 else
3668 use_generic = 1;
3669 } while ((++p)->offset == offset);
3670 if (specific && !use_generic)
3671 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003672 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003673 *ptr = generic;
3674 }
3675 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003676}
3677
3678static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003679recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003680{
3681 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003682 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003683 int i, n;
3684
3685 subclasses = type->tp_subclasses;
3686 if (subclasses == NULL)
3687 return 0;
3688 assert(PyList_Check(subclasses));
3689 n = PyList_GET_SIZE(subclasses);
3690 for (i = 0; i < n; i++) {
3691 ref = PyList_GET_ITEM(subclasses, i);
3692 assert(PyWeakref_CheckRef(ref));
3693 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3694 if (subclass == NULL)
3695 continue;
3696 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003697 /* Avoid recursing down into unaffected classes */
3698 dict = subclass->tp_dict;
3699 if (dict != NULL && PyDict_Check(dict) &&
3700 PyDict_GetItem(dict, name) != NULL)
3701 continue;
3702 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003703 return -1;
3704 }
3705 return 0;
3706}
3707
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003708static int
3709slotdef_cmp(const void *aa, const void *bb)
3710{
3711 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3712 int c = a->offset - b->offset;
3713 if (c != 0)
3714 return c;
3715 else
3716 return a - b;
3717}
3718
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003719static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003720init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003721{
3722 slotdef *p;
3723 static int initialized = 0;
3724
3725 if (initialized)
3726 return;
3727 for (p = slotdefs; p->name; p++) {
3728 p->name_strobj = PyString_InternFromString(p->name);
3729 if (!p->name_strobj)
3730 Py_FatalError("XXX ouch");
3731 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003732 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3733 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003734 initialized = 1;
3735}
3736
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003737static int
3738update_slot(PyTypeObject *type, PyObject *name)
3739{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003740 slotdef *ptrs[10];
3741 slotdef *p;
3742 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003743 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003744
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003745 init_slotdefs();
3746 pp = ptrs;
3747 for (p = slotdefs; p->name; p++) {
3748 /* XXX assume name is interned! */
3749 if (p->name_strobj == name)
3750 *pp++ = p;
3751 }
3752 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003753 for (pp = ptrs; *pp; pp++) {
3754 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003755 offset = p->offset;
3756 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003757 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003758 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003759 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003760 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003761}
3762
Tim Peters6d6c1a32001-08-02 04:15:00 +00003763static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003764fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003765{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003766 slotdef *p;
3767 PyObject *mro, *descr;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003768 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003769 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003770 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003771 void *generic, *specific;
3772 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003773
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003774 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003775 mro = type->tp_mro;
3776 assert(PyTuple_Check(mro));
3777 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003778 for (p = slotdefs; p->name; ) {
3779 offset = p->offset;
3780 ptr = slotptr(type, offset);
3781 if (!ptr) {
3782 do {
3783 ++p;
3784 } while (p->offset == offset);
3785 continue;
3786 }
3787 generic = specific = NULL;
3788 use_generic = 0;
3789 do {
3790 descr = NULL;
3791 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003792 PyObject *b = PyTuple_GET_ITEM(mro, i);
3793 PyObject *dict = NULL;
3794 if (PyType_Check(b))
3795 dict = ((PyTypeObject *)b)->tp_dict;
3796 else if (PyClass_Check(b))
3797 dict = ((PyClassObject *)b)->cl_dict;
3798 if (dict != NULL) {
3799 descr = PyDict_GetItem(
3800 dict, p->name_strobj);
3801 if (descr != NULL)
3802 break;
3803 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003804 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003805 if (descr == NULL)
3806 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003807 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003808 if (descr->ob_type == &PyWrapperDescr_Type) {
3809 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003810 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003811 PyType_IsSubtype(type, d->d_type))
3812 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003813 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003814 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003815 specific = d->d_wrapped;
3816 else
3817 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003818 }
3819 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003820 else
3821 use_generic = 1;
3822 } while ((++p)->offset == offset);
3823 if (specific && !use_generic)
3824 *ptr = specific;
3825 else
3826 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003827 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003828}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003829
Guido van Rossum6d204072001-10-21 00:44:31 +00003830/* This function is called by PyType_Ready() to populate the type's
3831 dictionary with method descriptors for function slots. For each
3832 function slot (like tp_repr) that's defined in the type, one or
3833 more corresponding descriptors are added in the type's tp_dict
3834 dictionary under the appropriate name (like __repr__). Some
3835 function slots cause more than one descriptor to be added (for
3836 example, the nb_add slot adds both __add__ and __radd__
3837 descriptors) and some function slots compete for the same
3838 descriptor (for example both sq_item and mp_subscript generate a
3839 __getitem__ descriptor). This only adds new descriptors and
3840 doesn't overwrite entries in tp_dict that were previously
3841 defined. The descriptors contain a reference to the C function
3842 they must call, so that it's safe if they are copied into a
3843 subtype's __dict__ and the subtype has a different C function in
3844 its slot -- calling the method defined by the descriptor will call
3845 the C function that was used to create it, rather than the C
3846 function present in the slot when it is called. (This is important
3847 because a subtype may have a C function in the slot that calls the
3848 method from the dictionary, and we want to avoid infinite recursion
3849 here.) */
3850
3851static int
3852add_operators(PyTypeObject *type)
3853{
3854 PyObject *dict = type->tp_dict;
3855 slotdef *p;
3856 PyObject *descr;
3857 void **ptr;
3858
3859 init_slotdefs();
3860 for (p = slotdefs; p->name; p++) {
3861 if (p->wrapper == NULL)
3862 continue;
3863 ptr = slotptr(type, p->offset);
3864 if (!ptr || !*ptr)
3865 continue;
3866 if (PyDict_GetItem(dict, p->name_strobj))
3867 continue;
3868 descr = PyDescr_NewWrapper(type, p, *ptr);
3869 if (descr == NULL)
3870 return -1;
3871 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3872 return -1;
3873 Py_DECREF(descr);
3874 }
3875 if (type->tp_new != NULL) {
3876 if (add_tp_new_wrapper(type) < 0)
3877 return -1;
3878 }
3879 return 0;
3880}
3881
Guido van Rossum705f0f52001-08-24 16:47:00 +00003882
3883/* Cooperative 'super' */
3884
3885typedef struct {
3886 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003887 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003888 PyObject *obj;
3889} superobject;
3890
Guido van Rossum6f799372001-09-20 20:46:19 +00003891static PyMemberDef super_members[] = {
3892 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3893 "the class invoking super()"},
3894 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3895 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003896 {0}
3897};
3898
Guido van Rossum705f0f52001-08-24 16:47:00 +00003899static void
3900super_dealloc(PyObject *self)
3901{
3902 superobject *su = (superobject *)self;
3903
Guido van Rossum048eb752001-10-02 21:24:57 +00003904 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003905 Py_XDECREF(su->obj);
3906 Py_XDECREF(su->type);
3907 self->ob_type->tp_free(self);
3908}
3909
3910static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003911super_repr(PyObject *self)
3912{
3913 superobject *su = (superobject *)self;
3914
3915 if (su->obj)
3916 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003917 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003918 su->type ? su->type->tp_name : "NULL",
3919 su->obj->ob_type->tp_name);
3920 else
3921 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00003922 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003923 su->type ? su->type->tp_name : "NULL");
3924}
3925
3926static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00003927super_getattro(PyObject *self, PyObject *name)
3928{
3929 superobject *su = (superobject *)self;
3930
3931 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00003932 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003933 descrgetfunc f;
3934 int i, n;
3935
Guido van Rossume705ef12001-08-29 15:47:06 +00003936 mro = su->obj->ob_type->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003937 if (mro == NULL)
3938 n = 0;
3939 else {
3940 assert(PyTuple_Check(mro));
3941 n = PyTuple_GET_SIZE(mro);
3942 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003943 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00003944 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00003945 break;
3946 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003947 if (i >= n && PyType_Check(su->obj)) {
3948 mro = ((PyTypeObject *)(su->obj))->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 Rossume705ef12001-08-29 15:47:06 +00003955 for (i = 0; i < n; i++) {
3956 if ((PyObject *)(su->type) ==
3957 PyTuple_GET_ITEM(mro, i))
3958 break;
3959 }
Guido van Rossume705ef12001-08-29 15:47:06 +00003960 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00003961 i++;
3962 res = NULL;
3963 for (; i < n; i++) {
3964 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00003965 if (PyType_Check(tmp))
3966 dict = ((PyTypeObject *)tmp)->tp_dict;
3967 else if (PyClass_Check(tmp))
3968 dict = ((PyClassObject *)tmp)->cl_dict;
3969 else
3970 continue;
3971 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00003972 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00003973 Py_INCREF(res);
3974 f = res->ob_type->tp_descr_get;
3975 if (f != NULL) {
3976 tmp = f(res, su->obj, res);
3977 Py_DECREF(res);
3978 res = tmp;
3979 }
3980 return res;
3981 }
3982 }
3983 }
3984 return PyObject_GenericGetAttr(self, name);
3985}
3986
Guido van Rossum5b443c62001-12-03 15:38:28 +00003987static int
3988supercheck(PyTypeObject *type, PyObject *obj)
3989{
3990 if (!PyType_IsSubtype(obj->ob_type, type) &&
3991 !(PyType_Check(obj) &&
3992 PyType_IsSubtype((PyTypeObject *)obj, type))) {
3993 PyErr_SetString(PyExc_TypeError,
3994 "super(type, obj): "
3995 "obj must be an instance or subtype of type");
3996 return -1;
3997 }
3998 else
3999 return 0;
4000}
4001
Guido van Rossum705f0f52001-08-24 16:47:00 +00004002static PyObject *
4003super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4004{
4005 superobject *su = (superobject *)self;
4006 superobject *new;
4007
4008 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4009 /* Not binding to an object, or already bound */
4010 Py_INCREF(self);
4011 return self;
4012 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004013 if (su->ob_type != &PySuper_Type)
4014 /* If su is an instance of a subclass of super,
4015 call its type */
4016 return PyObject_CallFunction((PyObject *)su->ob_type,
4017 "OO", su->type, obj);
4018 else {
4019 /* Inline the common case */
4020 if (supercheck(su->type, obj) < 0)
4021 return NULL;
4022 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4023 NULL, NULL);
4024 if (new == NULL)
4025 return NULL;
4026 Py_INCREF(su->type);
4027 Py_INCREF(obj);
4028 new->type = su->type;
4029 new->obj = obj;
4030 return (PyObject *)new;
4031 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004032}
4033
4034static int
4035super_init(PyObject *self, PyObject *args, PyObject *kwds)
4036{
4037 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004038 PyTypeObject *type;
4039 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004040
4041 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4042 return -1;
4043 if (obj == Py_None)
4044 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004045 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004046 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004047 Py_INCREF(type);
4048 Py_XINCREF(obj);
4049 su->type = type;
4050 su->obj = obj;
4051 return 0;
4052}
4053
4054static char super_doc[] =
4055"super(type) -> unbound super object\n"
4056"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004057"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004058"Typical use to call a cooperative superclass method:\n"
4059"class C(B):\n"
4060" def meth(self, arg):\n"
4061" super(C, self).meth(arg)";
4062
Guido van Rossum048eb752001-10-02 21:24:57 +00004063static int
4064super_traverse(PyObject *self, visitproc visit, void *arg)
4065{
4066 superobject *su = (superobject *)self;
4067 int err;
4068
4069#define VISIT(SLOT) \
4070 if (SLOT) { \
4071 err = visit((PyObject *)(SLOT), arg); \
4072 if (err) \
4073 return err; \
4074 }
4075
4076 VISIT(su->obj);
4077 VISIT(su->type);
4078
4079#undef VISIT
4080
4081 return 0;
4082}
4083
Guido van Rossum705f0f52001-08-24 16:47:00 +00004084PyTypeObject PySuper_Type = {
4085 PyObject_HEAD_INIT(&PyType_Type)
4086 0, /* ob_size */
4087 "super", /* tp_name */
4088 sizeof(superobject), /* tp_basicsize */
4089 0, /* tp_itemsize */
4090 /* methods */
4091 super_dealloc, /* tp_dealloc */
4092 0, /* tp_print */
4093 0, /* tp_getattr */
4094 0, /* tp_setattr */
4095 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004096 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004097 0, /* tp_as_number */
4098 0, /* tp_as_sequence */
4099 0, /* tp_as_mapping */
4100 0, /* tp_hash */
4101 0, /* tp_call */
4102 0, /* tp_str */
4103 super_getattro, /* tp_getattro */
4104 0, /* tp_setattro */
4105 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004106 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4107 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004108 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004109 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004110 0, /* tp_clear */
4111 0, /* tp_richcompare */
4112 0, /* tp_weaklistoffset */
4113 0, /* tp_iter */
4114 0, /* tp_iternext */
4115 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004116 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004117 0, /* tp_getset */
4118 0, /* tp_base */
4119 0, /* tp_dict */
4120 super_descr_get, /* tp_descr_get */
4121 0, /* tp_descr_set */
4122 0, /* tp_dictoffset */
4123 super_init, /* tp_init */
4124 PyType_GenericAlloc, /* tp_alloc */
4125 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00004126 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004127};