blob: a3fefe66410d842aaf0db5e5ca5678c8f354b83d [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Type object implementation */
2
Guido van Rossumc0b618a1997-05-02 03:12:38 +00003#include "Python.h"
Tim Peters6d6c1a32001-08-02 04:15:00 +00004#include "structmember.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005
Guido van Rossum6f799372001-09-20 20:46:19 +00006static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +00007 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
8 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
9 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000010 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000011 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
12 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
13 {"__dictoffset__", T_LONG,
14 offsetof(PyTypeObject, tp_dictoffset), READONLY},
15 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
16 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
17 {0}
18};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000019
Guido van Rossumc0b618a1997-05-02 03:12:38 +000020static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000021type_name(PyTypeObject *type, void *context)
22{
23 char *s;
24
25 s = strrchr(type->tp_name, '.');
26 if (s == NULL)
27 s = type->tp_name;
28 else
29 s++;
30 return PyString_FromString(s);
31}
32
33static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000034type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000035{
Guido van Rossumc3542212001-08-16 09:18:56 +000036 PyObject *mod;
37 char *s;
38
39 s = strrchr(type->tp_name, '.');
40 if (s != NULL)
41 return PyString_FromStringAndSize(type->tp_name,
42 (int)(s - type->tp_name));
43 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
44 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000045 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000046 if (mod != NULL && PyString_Check(mod)) {
47 Py_INCREF(mod);
48 return mod;
49 }
50 PyErr_SetString(PyExc_AttributeError, "__module__");
51 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000052}
53
Guido van Rossum3926a632001-09-25 16:25:58 +000054static int
55type_set_module(PyTypeObject *type, PyObject *value, void *context)
56{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000057 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000058 strrchr(type->tp_name, '.')) {
59 PyErr_Format(PyExc_TypeError,
60 "can't set %s.__module__", type->tp_name);
61 return -1;
62 }
63 if (!value) {
64 PyErr_Format(PyExc_TypeError,
65 "can't delete %s.__module__", type->tp_name);
66 return -1;
67 }
68 return PyDict_SetItemString(type->tp_dict, "__module__", value);
69}
70
Tim Peters6d6c1a32001-08-02 04:15:00 +000071static PyObject *
72type_dict(PyTypeObject *type, void *context)
73{
74 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000075 Py_INCREF(Py_None);
76 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000077 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000078 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000079}
80
Tim Peters24008312002-03-17 18:56:20 +000081static PyObject *
82type_get_doc(PyTypeObject *type, void *context)
83{
84 PyObject *result;
85 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
86 if (type->tp_doc == NULL) {
87 Py_INCREF(Py_None);
88 return Py_None;
89 }
90 return PyString_FromString(type->tp_doc);
91 }
92 result = PyDict_GetItemString(type->tp_dict, "__doc__");
93 Py_INCREF(result);
94 return result;
95}
96
Neil Schemenauerf23473f2001-10-21 22:28:58 +000097static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +000098 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +000099 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000100 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000101 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000102 {0}
103};
104
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000105static int
106type_compare(PyObject *v, PyObject *w)
107{
108 /* This is called with type objects only. So we
109 can just compare the addresses. */
110 Py_uintptr_t vv = (Py_uintptr_t)v;
111 Py_uintptr_t ww = (Py_uintptr_t)w;
112 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
113}
114
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000115static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000116type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000117{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000118 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000119 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000120
121 mod = type_module(type, NULL);
122 if (mod == NULL)
123 PyErr_Clear();
124 else if (!PyString_Check(mod)) {
125 Py_DECREF(mod);
126 mod = NULL;
127 }
128 name = type_name(type, NULL);
129 if (name == NULL)
130 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000131
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000132 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
133 kind = "class";
134 else
135 kind = "type";
136
Barry Warsaw7ce36942001-08-24 18:34:26 +0000137 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000138 rtn = PyString_FromFormat("<%s '%s.%s'>",
139 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000140 PyString_AS_STRING(mod),
141 PyString_AS_STRING(name));
142 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000143 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000144 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000145
Guido van Rossumc3542212001-08-16 09:18:56 +0000146 Py_XDECREF(mod);
147 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000148 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000149}
150
Tim Peters6d6c1a32001-08-02 04:15:00 +0000151static PyObject *
152type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
153{
154 PyObject *obj;
155
156 if (type->tp_new == NULL) {
157 PyErr_Format(PyExc_TypeError,
158 "cannot create '%.100s' instances",
159 type->tp_name);
160 return NULL;
161 }
162
Tim Peters3f996e72001-09-13 19:18:27 +0000163 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000164 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000165 /* Ugly exception: when the call was type(something),
166 don't call tp_init on the result. */
167 if (type == &PyType_Type &&
168 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
169 (kwds == NULL ||
170 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
171 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000172 type = obj->ob_type;
173 if (type->tp_init != NULL &&
174 type->tp_init(obj, args, kwds) < 0) {
175 Py_DECREF(obj);
176 obj = NULL;
177 }
178 }
179 return obj;
180}
181
182PyObject *
183PyType_GenericAlloc(PyTypeObject *type, int nitems)
184{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000185 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000186 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000187
188 if (PyType_IS_GC(type))
Tim Peters6d483d32001-10-06 21:27:34 +0000189 obj = _PyObject_GC_Malloc(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000190 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000191 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000192
Neil Schemenauerc806c882001-08-29 23:54:54 +0000193 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000194 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000195
Neil Schemenauerc806c882001-08-29 23:54:54 +0000196 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000197
Tim Peters6d6c1a32001-08-02 04:15:00 +0000198 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
199 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000200
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201 if (type->tp_itemsize == 0)
202 PyObject_INIT(obj, type);
203 else
204 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000205
Tim Peters6d6c1a32001-08-02 04:15:00 +0000206 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000207 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208 return obj;
209}
210
211PyObject *
212PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
213{
214 return type->tp_alloc(type, 0);
215}
216
Guido van Rossum9475a232001-10-05 20:51:39 +0000217/* Helpers for subtyping */
218
219static int
220subtype_traverse(PyObject *self, visitproc visit, void *arg)
221{
222 PyTypeObject *type, *base;
223 traverseproc f;
224 int err;
225
226 /* Find the nearest base with a different tp_traverse */
227 type = self->ob_type;
228 base = type->tp_base;
229 while ((f = base->tp_traverse) == subtype_traverse) {
230 base = base->tp_base;
231 assert(base);
232 }
233
234 if (type->tp_dictoffset != base->tp_dictoffset) {
235 PyObject **dictptr = _PyObject_GetDictPtr(self);
236 if (dictptr && *dictptr) {
237 err = visit(*dictptr, arg);
238 if (err)
239 return err;
240 }
241 }
242
243 if (f)
244 return f(self, visit, arg);
245 return 0;
246}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000247
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000248staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
249
250static int
251call_finalizer(PyObject *self)
252{
253 static PyObject *del_str = NULL;
254 PyObject *del, *res;
255 PyObject *error_type, *error_value, *error_traceback;
256
257 /* Temporarily resurrect the object. */
258#ifdef Py_TRACE_REFS
259#ifndef Py_REF_DEBUG
260# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
261#endif
262 /* much too complicated if Py_TRACE_REFS defined */
263 _Py_NewReference((PyObject *)self);
264#ifdef COUNT_ALLOCS
265 /* compensate for boost in _Py_NewReference; note that
266 * _Py_RefTotal was also boosted; we'll knock that down later.
267 */
268 self->ob_type->tp_allocs--;
269#endif
270#else /* !Py_TRACE_REFS */
271 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
272 Py_INCREF(self);
273#endif /* !Py_TRACE_REFS */
274
275 /* Save the current exception, if any. */
276 PyErr_Fetch(&error_type, &error_value, &error_traceback);
277
278 /* Execute __del__ method, if any. */
279 del = lookup_maybe(self, "__del__", &del_str);
280 if (del != NULL) {
281 res = PyEval_CallObject(del, NULL);
282 if (res == NULL)
283 PyErr_WriteUnraisable(del);
284 else
285 Py_DECREF(res);
286 Py_DECREF(del);
287 }
288
289 /* Restore the saved exception. */
290 PyErr_Restore(error_type, error_value, error_traceback);
291
292 /* Undo the temporary resurrection; can't use DECREF here, it would
293 * cause a recursive call.
294 */
295#ifdef Py_REF_DEBUG
296 /* _Py_RefTotal was boosted either by _Py_NewReference or
297 * Py_INCREF above.
298 */
299 _Py_RefTotal--;
300#endif
301 if (--self->ob_refcnt > 0) {
302#ifdef COUNT_ALLOCS
303 self->ob_type->tp_frees--;
304#endif
305 _PyObject_GC_TRACK(self);
306 return -1; /* __del__ added a reference; don't delete now */
307 }
308#ifdef Py_TRACE_REFS
309 _Py_ForgetReference((PyObject *)self);
310#ifdef COUNT_ALLOCS
311 /* compensate for increment in _Py_ForgetReference */
312 self->ob_type->tp_frees--;
313#endif
314#endif
315
316 return 0;
317}
318
Tim Peters6d6c1a32001-08-02 04:15:00 +0000319static void
320subtype_dealloc(PyObject *self)
321{
Guido van Rossum14227b42001-12-06 02:35:58 +0000322 PyTypeObject *type, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000323 destructor f;
324
325 /* This exists so we can DECREF self->ob_type */
326
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000327 if (call_finalizer(self) < 0)
328 return;
329
Tim Peters6d6c1a32001-08-02 04:15:00 +0000330 /* Find the nearest base with a different tp_dealloc */
331 type = self->ob_type;
Guido van Rossum14227b42001-12-06 02:35:58 +0000332 base = type->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000333 while ((f = base->tp_dealloc) == subtype_dealloc) {
334 base = base->tp_base;
335 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000336 }
337
338 /* Clear __slots__ variables */
339 if (type->tp_basicsize != base->tp_basicsize &&
340 type->tp_itemsize == 0)
341 {
342 char *addr = ((char *)self);
343 char *p = addr + base->tp_basicsize;
344 char *q = addr + type->tp_basicsize;
345 for (; p < q; p += sizeof(PyObject *)) {
346 PyObject **pp;
347 if (p == addr + type->tp_dictoffset ||
348 p == addr + type->tp_weaklistoffset)
349 continue;
350 pp = (PyObject **)p;
351 if (*pp != NULL) {
352 Py_DECREF(*pp);
353 *pp = NULL;
Guido van Rossum33bab012001-12-05 22:45:48 +0000354 }
355 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000356 }
357
358 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000359 if (type->tp_dictoffset && !base->tp_dictoffset) {
360 PyObject **dictptr = _PyObject_GetDictPtr(self);
361 if (dictptr != NULL) {
362 PyObject *dict = *dictptr;
363 if (dict != NULL) {
364 Py_DECREF(dict);
365 *dictptr = NULL;
366 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000367 }
368 }
369
Guido van Rossum9676b222001-08-17 20:32:36 +0000370 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000371 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000372 PyObject_ClearWeakRefs(self);
373
Tim Peters6d6c1a32001-08-02 04:15:00 +0000374 /* Finalize GC if the base doesn't do GC and we do */
375 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000376 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000377
378 /* Call the base tp_dealloc() */
379 assert(f);
380 f(self);
381
382 /* Can't reference self beyond this point */
383 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
384 Py_DECREF(type);
385 }
386}
387
Tim Peters6d6c1a32001-08-02 04:15:00 +0000388staticforward PyTypeObject *solid_base(PyTypeObject *type);
389
390typedef struct {
391 PyTypeObject type;
392 PyNumberMethods as_number;
393 PySequenceMethods as_sequence;
394 PyMappingMethods as_mapping;
395 PyBufferProcs as_buffer;
396 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000397 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000398} etype;
399
400/* type test with subclassing support */
401
402int
403PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
404{
405 PyObject *mro;
406
Guido van Rossum9478d072001-09-07 18:52:13 +0000407 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
408 return b == a || b == &PyBaseObject_Type;
409
Tim Peters6d6c1a32001-08-02 04:15:00 +0000410 mro = a->tp_mro;
411 if (mro != NULL) {
412 /* Deal with multiple inheritance without recursion
413 by walking the MRO tuple */
414 int i, n;
415 assert(PyTuple_Check(mro));
416 n = PyTuple_GET_SIZE(mro);
417 for (i = 0; i < n; i++) {
418 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
419 return 1;
420 }
421 return 0;
422 }
423 else {
424 /* a is not completely initilized yet; follow tp_base */
425 do {
426 if (a == b)
427 return 1;
428 a = a->tp_base;
429 } while (a != NULL);
430 return b == &PyBaseObject_Type;
431 }
432}
433
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000434/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000435 without looking in the instance dictionary
436 (so we can't use PyObject_GetAttr) but still binding
437 it to the instance. The arguments are the object,
438 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000439 static variable used to cache the interned Python string.
440
441 Two variants:
442
443 - lookup_maybe() returns NULL without raising an exception
444 when the _PyType_Lookup() call fails;
445
446 - lookup_method() always raises an exception upon errors.
447*/
Guido van Rossum60718732001-08-28 17:47:51 +0000448
449static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000450lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000451{
452 PyObject *res;
453
454 if (*attrobj == NULL) {
455 *attrobj = PyString_InternFromString(attrstr);
456 if (*attrobj == NULL)
457 return NULL;
458 }
459 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000460 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000461 descrgetfunc f;
462 if ((f = res->ob_type->tp_descr_get) == NULL)
463 Py_INCREF(res);
464 else
465 res = f(res, self, (PyObject *)(self->ob_type));
466 }
467 return res;
468}
469
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000470static PyObject *
471lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
472{
473 PyObject *res = lookup_maybe(self, attrstr, attrobj);
474 if (res == NULL && !PyErr_Occurred())
475 PyErr_SetObject(PyExc_AttributeError, *attrobj);
476 return res;
477}
478
Guido van Rossum2730b132001-08-28 18:22:14 +0000479/* A variation of PyObject_CallMethod that uses lookup_method()
480 instead of PyObject_GetAttrString(). This uses the same convention
481 as lookup_method to cache the interned name string object. */
482
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000483static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000484call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
485{
486 va_list va;
487 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000488 va_start(va, format);
489
Guido van Rossumda21c012001-10-03 00:50:18 +0000490 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000491 if (func == NULL) {
492 va_end(va);
493 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000494 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000495 return NULL;
496 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000497
498 if (format && *format)
499 args = Py_VaBuildValue(format, va);
500 else
501 args = PyTuple_New(0);
502
503 va_end(va);
504
505 if (args == NULL)
506 return NULL;
507
508 assert(PyTuple_Check(args));
509 retval = PyObject_Call(func, args, NULL);
510
511 Py_DECREF(args);
512 Py_DECREF(func);
513
514 return retval;
515}
516
517/* Clone of call_method() that returns NotImplemented when the lookup fails. */
518
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000519static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000520call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
521{
522 va_list va;
523 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000524 va_start(va, format);
525
Guido van Rossumda21c012001-10-03 00:50:18 +0000526 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000527 if (func == NULL) {
528 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000529 if (!PyErr_Occurred()) {
530 Py_INCREF(Py_NotImplemented);
531 return Py_NotImplemented;
532 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000533 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000534 }
535
536 if (format && *format)
537 args = Py_VaBuildValue(format, va);
538 else
539 args = PyTuple_New(0);
540
541 va_end(va);
542
Guido van Rossum717ce002001-09-14 16:58:08 +0000543 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000544 return NULL;
545
Guido van Rossum717ce002001-09-14 16:58:08 +0000546 assert(PyTuple_Check(args));
547 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000548
549 Py_DECREF(args);
550 Py_DECREF(func);
551
552 return retval;
553}
554
Tim Peters6d6c1a32001-08-02 04:15:00 +0000555/* Method resolution order algorithm from "Putting Metaclasses to Work"
556 by Forman and Danforth (Addison-Wesley 1999). */
557
558static int
559conservative_merge(PyObject *left, PyObject *right)
560{
561 int left_size;
562 int right_size;
563 int i, j, r, ok;
564 PyObject *temp, *rr;
565
566 assert(PyList_Check(left));
567 assert(PyList_Check(right));
568
569 again:
570 left_size = PyList_GET_SIZE(left);
571 right_size = PyList_GET_SIZE(right);
572 for (i = 0; i < left_size; i++) {
573 for (j = 0; j < right_size; j++) {
574 if (PyList_GET_ITEM(left, i) ==
575 PyList_GET_ITEM(right, j)) {
576 /* found a merge point */
577 temp = PyList_New(0);
578 if (temp == NULL)
579 return -1;
580 for (r = 0; r < j; r++) {
581 rr = PyList_GET_ITEM(right, r);
582 ok = PySequence_Contains(left, rr);
583 if (ok < 0) {
584 Py_DECREF(temp);
585 return -1;
586 }
587 if (!ok) {
588 ok = PyList_Append(temp, rr);
589 if (ok < 0) {
590 Py_DECREF(temp);
591 return -1;
592 }
593 }
594 }
595 ok = PyList_SetSlice(left, i, i, temp);
596 Py_DECREF(temp);
597 if (ok < 0)
598 return -1;
599 ok = PyList_SetSlice(right, 0, j+1, NULL);
600 if (ok < 0)
601 return -1;
602 goto again;
603 }
604 }
605 }
606 return PyList_SetSlice(left, left_size, left_size, right);
607}
608
609static int
610serious_order_disagreements(PyObject *left, PyObject *right)
611{
612 return 0; /* XXX later -- for now, we cheat: "don't do that" */
613}
614
Tim Petersa91e9642001-11-14 23:32:33 +0000615static int
616fill_classic_mro(PyObject *mro, PyObject *cls)
617{
618 PyObject *bases, *base;
619 int i, n;
620
621 assert(PyList_Check(mro));
622 assert(PyClass_Check(cls));
623 i = PySequence_Contains(mro, cls);
624 if (i < 0)
625 return -1;
626 if (!i) {
627 if (PyList_Append(mro, cls) < 0)
628 return -1;
629 }
630 bases = ((PyClassObject *)cls)->cl_bases;
631 assert(bases && PyTuple_Check(bases));
632 n = PyTuple_GET_SIZE(bases);
633 for (i = 0; i < n; i++) {
634 base = PyTuple_GET_ITEM(bases, i);
635 if (fill_classic_mro(mro, base) < 0)
636 return -1;
637 }
638 return 0;
639}
640
641static PyObject *
642classic_mro(PyObject *cls)
643{
644 PyObject *mro;
645
646 assert(PyClass_Check(cls));
647 mro = PyList_New(0);
648 if (mro != NULL) {
649 if (fill_classic_mro(mro, cls) == 0)
650 return mro;
651 Py_DECREF(mro);
652 }
653 return NULL;
654}
655
Tim Peters6d6c1a32001-08-02 04:15:00 +0000656static PyObject *
657mro_implementation(PyTypeObject *type)
658{
659 int i, n, ok;
660 PyObject *bases, *result;
661
662 bases = type->tp_bases;
663 n = PyTuple_GET_SIZE(bases);
664 result = Py_BuildValue("[O]", (PyObject *)type);
665 if (result == NULL)
666 return NULL;
667 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000668 PyObject *base = PyTuple_GET_ITEM(bases, i);
669 PyObject *parentMRO;
670 if (PyType_Check(base))
671 parentMRO = PySequence_List(
672 ((PyTypeObject*)base)->tp_mro);
673 else
674 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000675 if (parentMRO == NULL) {
676 Py_DECREF(result);
677 return NULL;
678 }
679 if (serious_order_disagreements(result, parentMRO)) {
680 Py_DECREF(result);
681 return NULL;
682 }
683 ok = conservative_merge(result, parentMRO);
684 Py_DECREF(parentMRO);
685 if (ok < 0) {
686 Py_DECREF(result);
687 return NULL;
688 }
689 }
690 return result;
691}
692
693static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000694mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000695{
696 PyTypeObject *type = (PyTypeObject *)self;
697
Tim Peters6d6c1a32001-08-02 04:15:00 +0000698 return mro_implementation(type);
699}
700
701static int
702mro_internal(PyTypeObject *type)
703{
704 PyObject *mro, *result, *tuple;
705
706 if (type->ob_type == &PyType_Type) {
707 result = mro_implementation(type);
708 }
709 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000710 static PyObject *mro_str;
711 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000712 if (mro == NULL)
713 return -1;
714 result = PyObject_CallObject(mro, NULL);
715 Py_DECREF(mro);
716 }
717 if (result == NULL)
718 return -1;
719 tuple = PySequence_Tuple(result);
720 Py_DECREF(result);
721 type->tp_mro = tuple;
722 return 0;
723}
724
725
726/* Calculate the best base amongst multiple base classes.
727 This is the first one that's on the path to the "solid base". */
728
729static PyTypeObject *
730best_base(PyObject *bases)
731{
732 int i, n;
733 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000734 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000735
736 assert(PyTuple_Check(bases));
737 n = PyTuple_GET_SIZE(bases);
738 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000739 base = NULL;
740 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000741 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000742 base_proto = PyTuple_GET_ITEM(bases, i);
743 if (PyClass_Check(base_proto))
744 continue;
745 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000746 PyErr_SetString(
747 PyExc_TypeError,
748 "bases must be types");
749 return NULL;
750 }
Tim Petersa91e9642001-11-14 23:32:33 +0000751 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000752 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000753 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000754 return NULL;
755 }
756 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000757 if (winner == NULL) {
758 winner = candidate;
759 base = base_i;
760 }
761 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000762 ;
763 else if (PyType_IsSubtype(candidate, winner)) {
764 winner = candidate;
765 base = base_i;
766 }
767 else {
768 PyErr_SetString(
769 PyExc_TypeError,
770 "multiple bases have "
771 "instance lay-out conflict");
772 return NULL;
773 }
774 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000775 if (base == NULL)
776 PyErr_SetString(PyExc_TypeError,
777 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000778 return base;
779}
780
781static int
782extra_ivars(PyTypeObject *type, PyTypeObject *base)
783{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000784 size_t t_size = type->tp_basicsize;
785 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000786
Guido van Rossum9676b222001-08-17 20:32:36 +0000787 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000788 if (type->tp_itemsize || base->tp_itemsize) {
789 /* If itemsize is involved, stricter rules */
790 return t_size != b_size ||
791 type->tp_itemsize != base->tp_itemsize;
792 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000793 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
794 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
795 t_size -= sizeof(PyObject *);
796 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
797 type->tp_dictoffset + sizeof(PyObject *) == t_size)
798 t_size -= sizeof(PyObject *);
799
800 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000801}
802
803static PyTypeObject *
804solid_base(PyTypeObject *type)
805{
806 PyTypeObject *base;
807
808 if (type->tp_base)
809 base = solid_base(type->tp_base);
810 else
811 base = &PyBaseObject_Type;
812 if (extra_ivars(type, base))
813 return type;
814 else
815 return base;
816}
817
818staticforward void object_dealloc(PyObject *);
819staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000820staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000821staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000822
823static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000824subtype_dict(PyObject *obj, void *context)
825{
826 PyObject **dictptr = _PyObject_GetDictPtr(obj);
827 PyObject *dict;
828
829 if (dictptr == NULL) {
830 PyErr_SetString(PyExc_AttributeError,
831 "This object has no __dict__");
832 return NULL;
833 }
834 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000835 if (dict == NULL)
836 *dictptr = dict = PyDict_New();
837 Py_XINCREF(dict);
838 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000839}
840
Guido van Rossum6661be32001-10-26 04:26:12 +0000841static int
842subtype_setdict(PyObject *obj, PyObject *value, void *context)
843{
844 PyObject **dictptr = _PyObject_GetDictPtr(obj);
845 PyObject *dict;
846
847 if (dictptr == NULL) {
848 PyErr_SetString(PyExc_AttributeError,
849 "This object has no __dict__");
850 return -1;
851 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000852 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000853 PyErr_SetString(PyExc_TypeError,
854 "__dict__ must be set to a dictionary");
855 return -1;
856 }
857 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000858 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000859 *dictptr = value;
860 Py_XDECREF(dict);
861 return 0;
862}
863
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000864static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000865 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000866 {0},
867};
868
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000869/* bozo: __getstate__ that raises TypeError */
870
871static PyObject *
872bozo_func(PyObject *self, PyObject *args)
873{
874 PyErr_SetString(PyExc_TypeError,
875 "a class that defines __slots__ without "
876 "defining __getstate__ cannot be pickled");
877 return NULL;
878}
879
Neal Norwitz93c1e232002-03-31 16:06:11 +0000880static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS};
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000881
882static PyObject *bozo_obj = NULL;
883
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000884static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
886{
887 PyObject *name, *bases, *dict;
888 static char *kwlist[] = {"name", "bases", "dict", 0};
889 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000890 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000891 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000892 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000893 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894
Tim Peters3abca122001-10-27 19:37:48 +0000895 assert(args != NULL && PyTuple_Check(args));
896 assert(kwds == NULL || PyDict_Check(kwds));
897
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000898 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000899 {
900 const int nargs = PyTuple_GET_SIZE(args);
901 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
902
903 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
904 PyObject *x = PyTuple_GET_ITEM(args, 0);
905 Py_INCREF(x->ob_type);
906 return (PyObject *) x->ob_type;
907 }
908
909 /* SF bug 475327 -- if that didn't trigger, we need 3
910 arguments. but PyArg_ParseTupleAndKeywords below may give
911 a msg saying type() needs exactly 3. */
912 if (nargs + nkwds != 3) {
913 PyErr_SetString(PyExc_TypeError,
914 "type() takes 1 or 3 arguments");
915 return NULL;
916 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000917 }
918
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000919 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000920 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
921 &name,
922 &PyTuple_Type, &bases,
923 &PyDict_Type, &dict))
924 return NULL;
925
926 /* Determine the proper metatype to deal with this,
927 and check for metatype conflicts while we're at it.
928 Note that if some other metatype wins to contract,
929 it's possible that its instances are not types. */
930 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000931 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000932 for (i = 0; i < nbases; i++) {
933 tmp = PyTuple_GET_ITEM(bases, i);
934 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +0000935 if (tmptype == &PyClass_Type)
936 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000937 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000938 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000939 if (PyType_IsSubtype(tmptype, winner)) {
940 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000941 continue;
942 }
943 PyErr_SetString(PyExc_TypeError,
944 "metatype conflict among bases");
945 return NULL;
946 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000947 if (winner != metatype) {
948 if (winner->tp_new != type_new) /* Pass it to the winner */
949 return winner->tp_new(winner, args, kwds);
950 metatype = winner;
951 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000952
953 /* Adjust for empty tuple bases */
954 if (nbases == 0) {
955 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
956 if (bases == NULL)
957 return NULL;
958 nbases = 1;
959 }
960 else
961 Py_INCREF(bases);
962
963 /* XXX From here until type is allocated, "return NULL" leaks bases! */
964
965 /* Calculate best base, and check that all bases are type objects */
966 base = best_base(bases);
967 if (base == NULL)
968 return NULL;
969 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
970 PyErr_Format(PyExc_TypeError,
971 "type '%.100s' is not an acceptable base type",
972 base->tp_name);
973 return NULL;
974 }
975
Tim Peters6d6c1a32001-08-02 04:15:00 +0000976 /* Check for a __slots__ sequence variable in dict, and count it */
977 slots = PyDict_GetItemString(dict, "__slots__");
978 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000979 add_dict = 0;
980 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000981 if (slots != NULL) {
982 /* Make it into a tuple */
983 if (PyString_Check(slots))
984 slots = Py_BuildValue("(O)", slots);
985 else
986 slots = PySequence_Tuple(slots);
987 if (slots == NULL)
988 return NULL;
989 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000990 if (nslots > 0 && base->tp_itemsize != 0) {
991 PyErr_Format(PyExc_TypeError,
992 "nonempty __slots__ "
993 "not supported for subtype of '%s'",
994 base->tp_name);
995 return NULL;
996 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000997 for (i = 0; i < nslots; i++) {
998 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
999 PyErr_SetString(PyExc_TypeError,
1000 "__slots__ must be a sequence of strings");
1001 Py_DECREF(slots);
1002 return NULL;
1003 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001004 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001005 }
1006 }
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001007 if (slots != NULL) {
1008 /* See if *this* class defines __getstate__ */
1009 PyObject *getstate = PyDict_GetItemString(dict,
1010 "__getstate__");
1011 if (getstate == NULL) {
1012 /* If not, provide a bozo that raises TypeError */
1013 if (bozo_obj == NULL) {
1014 bozo_obj = PyCFunction_New(&bozo_ml, NULL);
1015 if (bozo_obj == NULL) {
1016 /* XXX decref various things */
1017 return NULL;
1018 }
1019 }
1020 if (PyDict_SetItemString(dict,
1021 "__getstate__",
1022 bozo_obj) < 0) {
1023 /* XXX decref various things */
1024 return NULL;
1025 }
1026 }
1027 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001028 if (slots == NULL && base->tp_dictoffset == 0 &&
1029 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +00001030 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001031 add_dict++;
1032 }
Guido van Rossumc4141872001-08-30 04:43:35 +00001033 if (slots == NULL && base->tp_weaklistoffset == 0 &&
1034 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001035 nslots++;
1036 add_weak++;
1037 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001038
1039 /* XXX From here until type is safely allocated,
1040 "return NULL" may leak slots! */
1041
1042 /* Allocate the type object */
1043 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1044 if (type == NULL)
1045 return NULL;
1046
1047 /* Keep name and slots alive in the extended type object */
1048 et = (etype *)type;
1049 Py_INCREF(name);
1050 et->name = name;
1051 et->slots = slots;
1052
Guido van Rossumdc91b992001-08-08 22:26:22 +00001053 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001054 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1055 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001056 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1057 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001058
1059 /* It's a new-style number unless it specifically inherits any
1060 old-style numeric behavior */
1061 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1062 (base->tp_as_number == NULL))
1063 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1064
1065 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001066 type->tp_as_number = &et->as_number;
1067 type->tp_as_sequence = &et->as_sequence;
1068 type->tp_as_mapping = &et->as_mapping;
1069 type->tp_as_buffer = &et->as_buffer;
1070 type->tp_name = PyString_AS_STRING(name);
1071
1072 /* Set tp_base and tp_bases */
1073 type->tp_bases = bases;
1074 Py_INCREF(base);
1075 type->tp_base = base;
1076
Guido van Rossum687ae002001-10-15 22:03:32 +00001077 /* Initialize tp_dict from passed-in dict */
1078 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001079 if (dict == NULL) {
1080 Py_DECREF(type);
1081 return NULL;
1082 }
1083
Guido van Rossumc3542212001-08-16 09:18:56 +00001084 /* Set __module__ in the dict */
1085 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1086 tmp = PyEval_GetGlobals();
1087 if (tmp != NULL) {
1088 tmp = PyDict_GetItemString(tmp, "__name__");
1089 if (tmp != NULL) {
1090 if (PyDict_SetItemString(dict, "__module__",
1091 tmp) < 0)
1092 return NULL;
1093 }
1094 }
1095 }
1096
Tim Peters2f93e282001-10-04 05:27:00 +00001097 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001098 and is a string. The __doc__ accessor will first look for tp_doc;
1099 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001100 */
1101 {
1102 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1103 if (doc != NULL && PyString_Check(doc)) {
1104 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001105 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001106 if (type->tp_doc == NULL) {
1107 Py_DECREF(type);
1108 return NULL;
1109 }
1110 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1111 }
1112 }
1113
Tim Peters6d6c1a32001-08-02 04:15:00 +00001114 /* Special-case __new__: if it's a plain function,
1115 make it a static function */
1116 tmp = PyDict_GetItemString(dict, "__new__");
1117 if (tmp != NULL && PyFunction_Check(tmp)) {
1118 tmp = PyStaticMethod_New(tmp);
1119 if (tmp == NULL) {
1120 Py_DECREF(type);
1121 return NULL;
1122 }
1123 PyDict_SetItemString(dict, "__new__", tmp);
1124 Py_DECREF(tmp);
1125 }
1126
1127 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1128 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001129 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001130 if (slots != NULL) {
1131 for (i = 0; i < nslots; i++, mp++) {
1132 mp->name = PyString_AS_STRING(
1133 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001134 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001135 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001136 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001137 strcmp(mp->name, "__weakref__") == 0) {
1138 mp->type = T_OBJECT;
Guido van Rossum9676b222001-08-17 20:32:36 +00001139 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001140 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001141 slotoffset += sizeof(PyObject *);
1142 }
1143 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001144 else {
1145 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001146 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +00001147 type->tp_dictoffset =
1148 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001149 else
1150 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001151 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001152 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +00001153 }
1154 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001155 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +00001156 type->tp_weaklistoffset = slotoffset;
1157 mp->name = "__weakref__";
1158 mp->type = T_OBJECT;
1159 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +00001160 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001161 mp++;
1162 slotoffset += sizeof(PyObject *);
1163 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001164 }
1165 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001166 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001167 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001168
1169 /* Special case some slots */
1170 if (type->tp_dictoffset != 0 || nslots > 0) {
1171 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1172 type->tp_getattro = PyObject_GenericGetAttr;
1173 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1174 type->tp_setattro = PyObject_GenericSetAttr;
1175 }
1176 type->tp_dealloc = subtype_dealloc;
1177
Guido van Rossum9475a232001-10-05 20:51:39 +00001178 /* Enable GC unless there are really no instance variables possible */
1179 if (!(type->tp_basicsize == sizeof(PyObject) &&
1180 type->tp_itemsize == 0))
1181 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1182
Tim Peters6d6c1a32001-08-02 04:15:00 +00001183 /* Always override allocation strategy to use regular heap */
1184 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001185 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
1186 type->tp_free = _PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001187 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +00001188 type->tp_clear = base->tp_clear;
1189 }
1190 else
1191 type->tp_free = _PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001192
1193 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001194 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001195 Py_DECREF(type);
1196 return NULL;
1197 }
1198
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001199 /* Put the proper slots in place */
1200 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001201
Tim Peters6d6c1a32001-08-02 04:15:00 +00001202 return (PyObject *)type;
1203}
1204
1205/* Internal API to look for a name through the MRO.
1206 This returns a borrowed reference, and doesn't set an exception! */
1207PyObject *
1208_PyType_Lookup(PyTypeObject *type, PyObject *name)
1209{
1210 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001211 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001212
Guido van Rossum687ae002001-10-15 22:03:32 +00001213 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001214 mro = type->tp_mro;
1215 assert(PyTuple_Check(mro));
1216 n = PyTuple_GET_SIZE(mro);
1217 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001218 base = PyTuple_GET_ITEM(mro, i);
1219 if (PyClass_Check(base))
1220 dict = ((PyClassObject *)base)->cl_dict;
1221 else {
1222 assert(PyType_Check(base));
1223 dict = ((PyTypeObject *)base)->tp_dict;
1224 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001225 assert(dict && PyDict_Check(dict));
1226 res = PyDict_GetItem(dict, name);
1227 if (res != NULL)
1228 return res;
1229 }
1230 return NULL;
1231}
1232
1233/* This is similar to PyObject_GenericGetAttr(),
1234 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1235static PyObject *
1236type_getattro(PyTypeObject *type, PyObject *name)
1237{
1238 PyTypeObject *metatype = type->ob_type;
1239 PyObject *descr, *res;
1240 descrgetfunc f;
1241
1242 /* Initialize this type (we'll assume the metatype is initialized) */
1243 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001244 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001245 return NULL;
1246 }
1247
1248 /* Get a descriptor from the metatype */
1249 descr = _PyType_Lookup(metatype, name);
1250 f = NULL;
1251 if (descr != NULL) {
1252 f = descr->ob_type->tp_descr_get;
1253 if (f != NULL && PyDescr_IsData(descr))
1254 return f(descr,
1255 (PyObject *)type, (PyObject *)metatype);
1256 }
1257
Guido van Rossum687ae002001-10-15 22:03:32 +00001258 /* Look in tp_dict of this type and its bases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001259 res = _PyType_Lookup(type, name);
1260 if (res != NULL) {
1261 f = res->ob_type->tp_descr_get;
1262 if (f != NULL)
1263 return f(res, (PyObject *)NULL, (PyObject *)type);
1264 Py_INCREF(res);
1265 return res;
1266 }
1267
1268 /* Use the descriptor from the metatype */
1269 if (f != NULL) {
1270 res = f(descr, (PyObject *)type, (PyObject *)metatype);
1271 return res;
1272 }
1273 if (descr != NULL) {
1274 Py_INCREF(descr);
1275 return descr;
1276 }
1277
1278 /* Give up */
1279 PyErr_Format(PyExc_AttributeError,
1280 "type object '%.50s' has no attribute '%.400s'",
1281 type->tp_name, PyString_AS_STRING(name));
1282 return NULL;
1283}
1284
1285static int
1286type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1287{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001288 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1289 PyErr_Format(
1290 PyExc_TypeError,
1291 "can't set attributes of built-in/extension type '%s'",
1292 type->tp_name);
1293 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001294 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001295 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1296 return -1;
1297 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001298}
1299
1300static void
1301type_dealloc(PyTypeObject *type)
1302{
1303 etype *et;
1304
1305 /* Assert this is a heap-allocated type object */
1306 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001307 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001308 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001309 et = (etype *)type;
1310 Py_XDECREF(type->tp_base);
1311 Py_XDECREF(type->tp_dict);
1312 Py_XDECREF(type->tp_bases);
1313 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001314 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001315 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001316 Py_XDECREF(et->name);
1317 Py_XDECREF(et->slots);
1318 type->ob_type->tp_free((PyObject *)type);
1319}
1320
Guido van Rossum1c450732001-10-08 15:18:27 +00001321static PyObject *
1322type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1323{
1324 PyObject *list, *raw, *ref;
1325 int i, n;
1326
1327 list = PyList_New(0);
1328 if (list == NULL)
1329 return NULL;
1330 raw = type->tp_subclasses;
1331 if (raw == NULL)
1332 return list;
1333 assert(PyList_Check(raw));
1334 n = PyList_GET_SIZE(raw);
1335 for (i = 0; i < n; i++) {
1336 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001337 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001338 ref = PyWeakref_GET_OBJECT(ref);
1339 if (ref != Py_None) {
1340 if (PyList_Append(list, ref) < 0) {
1341 Py_DECREF(list);
1342 return NULL;
1343 }
1344 }
1345 }
1346 return list;
1347}
1348
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001350 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001351 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001352 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1353 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354 {0}
1355};
1356
1357static char type_doc[] =
1358"type(object) -> the object's type\n"
1359"type(name, bases, dict) -> a new type";
1360
Guido van Rossum048eb752001-10-02 21:24:57 +00001361static int
1362type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1363{
1364 etype *et;
1365 int err;
1366
1367 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1368 return 0;
1369
1370 et = (etype *)type;
1371
1372#define VISIT(SLOT) \
1373 if (SLOT) { \
1374 err = visit((PyObject *)(SLOT), arg); \
1375 if (err) \
1376 return err; \
1377 }
1378
1379 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001380 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001381 VISIT(type->tp_mro);
1382 VISIT(type->tp_bases);
1383 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001384 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001385 VISIT(et->slots);
1386
1387#undef VISIT
1388
1389 return 0;
1390}
1391
1392static int
1393type_clear(PyTypeObject *type)
1394{
1395 etype *et;
1396 PyObject *tmp;
1397
1398 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1399 return 0;
1400
1401 et = (etype *)type;
1402
1403#define CLEAR(SLOT) \
1404 if (SLOT) { \
1405 tmp = (PyObject *)(SLOT); \
1406 SLOT = NULL; \
1407 Py_DECREF(tmp); \
1408 }
1409
1410 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001411 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001412 CLEAR(type->tp_mro);
1413 CLEAR(type->tp_bases);
1414 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001415 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001416 CLEAR(et->slots);
1417
Tim Peters2f93e282001-10-04 05:27:00 +00001418 if (type->tp_doc != NULL) {
1419 PyObject_FREE(type->tp_doc);
1420 type->tp_doc = NULL;
1421 }
1422
Guido van Rossum048eb752001-10-02 21:24:57 +00001423#undef CLEAR
1424
1425 return 0;
1426}
1427
1428static int
1429type_is_gc(PyTypeObject *type)
1430{
1431 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1432}
1433
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001434PyTypeObject PyType_Type = {
1435 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001436 0, /* ob_size */
1437 "type", /* tp_name */
1438 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001439 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 (destructor)type_dealloc, /* tp_dealloc */
1441 0, /* tp_print */
1442 0, /* tp_getattr */
1443 0, /* tp_setattr */
1444 type_compare, /* tp_compare */
1445 (reprfunc)type_repr, /* tp_repr */
1446 0, /* tp_as_number */
1447 0, /* tp_as_sequence */
1448 0, /* tp_as_mapping */
1449 (hashfunc)_Py_HashPointer, /* tp_hash */
1450 (ternaryfunc)type_call, /* tp_call */
1451 0, /* tp_str */
1452 (getattrofunc)type_getattro, /* tp_getattro */
1453 (setattrofunc)type_setattro, /* tp_setattro */
1454 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001455 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1456 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001457 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001458 (traverseproc)type_traverse, /* tp_traverse */
1459 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001460 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001461 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001462 0, /* tp_iter */
1463 0, /* tp_iternext */
1464 type_methods, /* tp_methods */
1465 type_members, /* tp_members */
1466 type_getsets, /* tp_getset */
1467 0, /* tp_base */
1468 0, /* tp_dict */
1469 0, /* tp_descr_get */
1470 0, /* tp_descr_set */
1471 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1472 0, /* tp_init */
1473 0, /* tp_alloc */
1474 type_new, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00001475 _PyObject_GC_Del, /* tp_free */
1476 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001477};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001478
1479
1480/* The base type of all types (eventually)... except itself. */
1481
1482static int
1483object_init(PyObject *self, PyObject *args, PyObject *kwds)
1484{
1485 return 0;
1486}
1487
1488static void
1489object_dealloc(PyObject *self)
1490{
1491 self->ob_type->tp_free(self);
1492}
1493
Guido van Rossum8e248182001-08-12 05:17:56 +00001494static PyObject *
1495object_repr(PyObject *self)
1496{
Guido van Rossum76e69632001-08-16 18:52:43 +00001497 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001498 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001499
Guido van Rossum76e69632001-08-16 18:52:43 +00001500 type = self->ob_type;
1501 mod = type_module(type, NULL);
1502 if (mod == NULL)
1503 PyErr_Clear();
1504 else if (!PyString_Check(mod)) {
1505 Py_DECREF(mod);
1506 mod = NULL;
1507 }
1508 name = type_name(type, NULL);
1509 if (name == NULL)
1510 return NULL;
1511 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001512 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001513 PyString_AS_STRING(mod),
1514 PyString_AS_STRING(name),
1515 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001516 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001517 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001518 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001519 Py_XDECREF(mod);
1520 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001521 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001522}
1523
Guido van Rossumb8f63662001-08-15 23:57:02 +00001524static PyObject *
1525object_str(PyObject *self)
1526{
1527 unaryfunc f;
1528
1529 f = self->ob_type->tp_repr;
1530 if (f == NULL)
1531 f = object_repr;
1532 return f(self);
1533}
1534
Guido van Rossum8e248182001-08-12 05:17:56 +00001535static long
1536object_hash(PyObject *self)
1537{
1538 return _Py_HashPointer(self);
1539}
Guido van Rossum8e248182001-08-12 05:17:56 +00001540
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001541static PyObject *
1542object_get_class(PyObject *self, void *closure)
1543{
1544 Py_INCREF(self->ob_type);
1545 return (PyObject *)(self->ob_type);
1546}
1547
1548static int
1549equiv_structs(PyTypeObject *a, PyTypeObject *b)
1550{
1551 return a == b ||
1552 (a != NULL &&
1553 b != NULL &&
1554 a->tp_basicsize == b->tp_basicsize &&
1555 a->tp_itemsize == b->tp_itemsize &&
1556 a->tp_dictoffset == b->tp_dictoffset &&
1557 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1558 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1559 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1560}
1561
1562static int
1563same_slots_added(PyTypeObject *a, PyTypeObject *b)
1564{
1565 PyTypeObject *base = a->tp_base;
1566 int size;
1567
1568 if (base != b->tp_base)
1569 return 0;
1570 if (equiv_structs(a, base) && equiv_structs(b, base))
1571 return 1;
1572 size = base->tp_basicsize;
1573 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1574 size += sizeof(PyObject *);
1575 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1576 size += sizeof(PyObject *);
1577 return size == a->tp_basicsize && size == b->tp_basicsize;
1578}
1579
1580static int
1581object_set_class(PyObject *self, PyObject *value, void *closure)
1582{
1583 PyTypeObject *old = self->ob_type;
1584 PyTypeObject *new, *newbase, *oldbase;
1585
1586 if (!PyType_Check(value)) {
1587 PyErr_Format(PyExc_TypeError,
1588 "__class__ must be set to new-style class, not '%s' object",
1589 value->ob_type->tp_name);
1590 return -1;
1591 }
1592 new = (PyTypeObject *)value;
1593 newbase = new;
1594 oldbase = old;
1595 while (equiv_structs(newbase, newbase->tp_base))
1596 newbase = newbase->tp_base;
1597 while (equiv_structs(oldbase, oldbase->tp_base))
1598 oldbase = oldbase->tp_base;
1599 if (newbase != oldbase &&
1600 (newbase->tp_base != oldbase->tp_base ||
1601 !same_slots_added(newbase, oldbase))) {
1602 PyErr_Format(PyExc_TypeError,
1603 "__class__ assignment: "
1604 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001605 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001606 old->tp_name);
1607 return -1;
1608 }
1609 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1610 Py_INCREF(new);
1611 }
1612 self->ob_type = new;
1613 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1614 Py_DECREF(old);
1615 }
1616 return 0;
1617}
1618
1619static PyGetSetDef object_getsets[] = {
1620 {"__class__", object_get_class, object_set_class,
1621 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001622 {0}
1623};
1624
Guido van Rossum3926a632001-09-25 16:25:58 +00001625static PyObject *
1626object_reduce(PyObject *self, PyObject *args)
1627{
1628 /* Call copy_reg._reduce(self) */
1629 static PyObject *copy_reg_str;
1630 PyObject *copy_reg, *res;
1631
1632 if (!copy_reg_str) {
1633 copy_reg_str = PyString_InternFromString("copy_reg");
1634 if (copy_reg_str == NULL)
1635 return NULL;
1636 }
1637 copy_reg = PyImport_Import(copy_reg_str);
1638 if (!copy_reg)
1639 return NULL;
1640 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1641 Py_DECREF(copy_reg);
1642 return res;
1643}
1644
1645static PyMethodDef object_methods[] = {
1646 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1647 {0}
1648};
1649
Tim Peters6d6c1a32001-08-02 04:15:00 +00001650PyTypeObject PyBaseObject_Type = {
1651 PyObject_HEAD_INIT(&PyType_Type)
1652 0, /* ob_size */
1653 "object", /* tp_name */
1654 sizeof(PyObject), /* tp_basicsize */
1655 0, /* tp_itemsize */
1656 (destructor)object_dealloc, /* tp_dealloc */
1657 0, /* tp_print */
1658 0, /* tp_getattr */
1659 0, /* tp_setattr */
1660 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001661 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001662 0, /* tp_as_number */
1663 0, /* tp_as_sequence */
1664 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001665 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001666 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001667 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001668 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001669 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001670 0, /* tp_as_buffer */
1671 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1672 "The most base type", /* tp_doc */
1673 0, /* tp_traverse */
1674 0, /* tp_clear */
1675 0, /* tp_richcompare */
1676 0, /* tp_weaklistoffset */
1677 0, /* tp_iter */
1678 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001679 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001680 0, /* tp_members */
1681 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001682 0, /* tp_base */
1683 0, /* tp_dict */
1684 0, /* tp_descr_get */
1685 0, /* tp_descr_set */
1686 0, /* tp_dictoffset */
1687 object_init, /* tp_init */
1688 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001689 PyType_GenericNew, /* tp_new */
Guido van Rossum9475a232001-10-05 20:51:39 +00001690 _PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001691};
1692
1693
1694/* Initialize the __dict__ in a type object */
1695
Fred Drake7bf97152002-03-28 05:33:33 +00001696static PyObject *
1697create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *))
1698{
1699 PyObject *cfunc;
1700 PyObject *result;
1701
1702 cfunc = PyCFunction_New(meth, NULL);
1703 if (cfunc == NULL)
1704 return NULL;
1705 result = func(cfunc);
1706 Py_DECREF(cfunc);
1707 return result;
1708}
1709
Tim Peters6d6c1a32001-08-02 04:15:00 +00001710static int
1711add_methods(PyTypeObject *type, PyMethodDef *meth)
1712{
Guido van Rossum687ae002001-10-15 22:03:32 +00001713 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001714
1715 for (; meth->ml_name != NULL; meth++) {
1716 PyObject *descr;
1717 if (PyDict_GetItemString(dict, meth->ml_name))
1718 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00001719 if (meth->ml_flags & METH_CLASS) {
1720 if (meth->ml_flags & METH_STATIC) {
1721 PyErr_SetString(PyExc_ValueError,
1722 "method cannot be both class and static");
1723 return -1;
1724 }
1725 descr = create_specialmethod(meth, PyClassMethod_New);
1726 }
1727 else if (meth->ml_flags & METH_STATIC) {
1728 descr = create_specialmethod(meth, PyStaticMethod_New);
1729 }
1730 else {
1731 descr = PyDescr_NewMethod(type, meth);
1732 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001733 if (descr == NULL)
1734 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00001735 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001736 return -1;
1737 Py_DECREF(descr);
1738 }
1739 return 0;
1740}
1741
1742static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001743add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001744{
Guido van Rossum687ae002001-10-15 22:03:32 +00001745 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001746
1747 for (; memb->name != NULL; memb++) {
1748 PyObject *descr;
1749 if (PyDict_GetItemString(dict, memb->name))
1750 continue;
1751 descr = PyDescr_NewMember(type, memb);
1752 if (descr == NULL)
1753 return -1;
1754 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1755 return -1;
1756 Py_DECREF(descr);
1757 }
1758 return 0;
1759}
1760
1761static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001762add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001763{
Guido van Rossum687ae002001-10-15 22:03:32 +00001764 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001765
1766 for (; gsp->name != NULL; gsp++) {
1767 PyObject *descr;
1768 if (PyDict_GetItemString(dict, gsp->name))
1769 continue;
1770 descr = PyDescr_NewGetSet(type, gsp);
1771
1772 if (descr == NULL)
1773 return -1;
1774 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1775 return -1;
1776 Py_DECREF(descr);
1777 }
1778 return 0;
1779}
1780
Guido van Rossum13d52f02001-08-10 21:24:08 +00001781static void
1782inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001783{
1784 int oldsize, newsize;
1785
Guido van Rossum13d52f02001-08-10 21:24:08 +00001786 /* Special flag magic */
1787 if (!type->tp_as_buffer && base->tp_as_buffer) {
1788 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1789 type->tp_flags |=
1790 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1791 }
1792 if (!type->tp_as_sequence && base->tp_as_sequence) {
1793 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1794 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1795 }
1796 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1797 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1798 if ((!type->tp_as_number && base->tp_as_number) ||
1799 (!type->tp_as_sequence && base->tp_as_sequence)) {
1800 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1801 if (!type->tp_as_number && !type->tp_as_sequence) {
1802 type->tp_flags |= base->tp_flags &
1803 Py_TPFLAGS_HAVE_INPLACEOPS;
1804 }
1805 }
1806 /* Wow */
1807 }
1808 if (!type->tp_as_number && base->tp_as_number) {
1809 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1810 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1811 }
1812
1813 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001814 oldsize = base->tp_basicsize;
1815 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1816 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1817 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001818 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1819 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001820 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001821 if (type->tp_traverse == NULL)
1822 type->tp_traverse = base->tp_traverse;
1823 if (type->tp_clear == NULL)
1824 type->tp_clear = base->tp_clear;
1825 }
1826 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001827 /* The condition below could use some explanation.
1828 It appears that tp_new is not inherited for static types
1829 whose base class is 'object'; this seems to be a precaution
1830 so that old extension types don't suddenly become
1831 callable (object.__new__ wouldn't insure the invariants
1832 that the extension type's own factory function ensures).
1833 Heap types, of course, are under our control, so they do
1834 inherit tp_new; static extension types that specify some
1835 other built-in type as the default are considered
1836 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001837 if (base != &PyBaseObject_Type ||
1838 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1839 if (type->tp_new == NULL)
1840 type->tp_new = base->tp_new;
1841 }
1842 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001843 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001844
1845 /* Copy other non-function slots */
1846
1847#undef COPYVAL
1848#define COPYVAL(SLOT) \
1849 if (type->SLOT == 0) type->SLOT = base->SLOT
1850
1851 COPYVAL(tp_itemsize);
1852 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1853 COPYVAL(tp_weaklistoffset);
1854 }
1855 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1856 COPYVAL(tp_dictoffset);
1857 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001858}
1859
1860static void
1861inherit_slots(PyTypeObject *type, PyTypeObject *base)
1862{
1863 PyTypeObject *basebase;
1864
1865#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001866#undef COPYSLOT
1867#undef COPYNUM
1868#undef COPYSEQ
1869#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001870#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001871
1872#define SLOTDEFINED(SLOT) \
1873 (base->SLOT != 0 && \
1874 (basebase == NULL || base->SLOT != basebase->SLOT))
1875
Tim Peters6d6c1a32001-08-02 04:15:00 +00001876#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001877 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001878
1879#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1880#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1881#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001882#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883
Guido van Rossum13d52f02001-08-10 21:24:08 +00001884 /* This won't inherit indirect slots (from tp_as_number etc.)
1885 if type doesn't provide the space. */
1886
1887 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1888 basebase = base->tp_base;
1889 if (basebase->tp_as_number == NULL)
1890 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001891 COPYNUM(nb_add);
1892 COPYNUM(nb_subtract);
1893 COPYNUM(nb_multiply);
1894 COPYNUM(nb_divide);
1895 COPYNUM(nb_remainder);
1896 COPYNUM(nb_divmod);
1897 COPYNUM(nb_power);
1898 COPYNUM(nb_negative);
1899 COPYNUM(nb_positive);
1900 COPYNUM(nb_absolute);
1901 COPYNUM(nb_nonzero);
1902 COPYNUM(nb_invert);
1903 COPYNUM(nb_lshift);
1904 COPYNUM(nb_rshift);
1905 COPYNUM(nb_and);
1906 COPYNUM(nb_xor);
1907 COPYNUM(nb_or);
1908 COPYNUM(nb_coerce);
1909 COPYNUM(nb_int);
1910 COPYNUM(nb_long);
1911 COPYNUM(nb_float);
1912 COPYNUM(nb_oct);
1913 COPYNUM(nb_hex);
1914 COPYNUM(nb_inplace_add);
1915 COPYNUM(nb_inplace_subtract);
1916 COPYNUM(nb_inplace_multiply);
1917 COPYNUM(nb_inplace_divide);
1918 COPYNUM(nb_inplace_remainder);
1919 COPYNUM(nb_inplace_power);
1920 COPYNUM(nb_inplace_lshift);
1921 COPYNUM(nb_inplace_rshift);
1922 COPYNUM(nb_inplace_and);
1923 COPYNUM(nb_inplace_xor);
1924 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001925 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1926 COPYNUM(nb_true_divide);
1927 COPYNUM(nb_floor_divide);
1928 COPYNUM(nb_inplace_true_divide);
1929 COPYNUM(nb_inplace_floor_divide);
1930 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001931 }
1932
Guido van Rossum13d52f02001-08-10 21:24:08 +00001933 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1934 basebase = base->tp_base;
1935 if (basebase->tp_as_sequence == NULL)
1936 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001937 COPYSEQ(sq_length);
1938 COPYSEQ(sq_concat);
1939 COPYSEQ(sq_repeat);
1940 COPYSEQ(sq_item);
1941 COPYSEQ(sq_slice);
1942 COPYSEQ(sq_ass_item);
1943 COPYSEQ(sq_ass_slice);
1944 COPYSEQ(sq_contains);
1945 COPYSEQ(sq_inplace_concat);
1946 COPYSEQ(sq_inplace_repeat);
1947 }
1948
Guido van Rossum13d52f02001-08-10 21:24:08 +00001949 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
1950 basebase = base->tp_base;
1951 if (basebase->tp_as_mapping == NULL)
1952 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001953 COPYMAP(mp_length);
1954 COPYMAP(mp_subscript);
1955 COPYMAP(mp_ass_subscript);
1956 }
1957
Tim Petersfc57ccb2001-10-12 02:38:24 +00001958 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
1959 basebase = base->tp_base;
1960 if (basebase->tp_as_buffer == NULL)
1961 basebase = NULL;
1962 COPYBUF(bf_getreadbuffer);
1963 COPYBUF(bf_getwritebuffer);
1964 COPYBUF(bf_getsegcount);
1965 COPYBUF(bf_getcharbuffer);
1966 }
1967
Guido van Rossum13d52f02001-08-10 21:24:08 +00001968 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001969
Tim Peters6d6c1a32001-08-02 04:15:00 +00001970 COPYSLOT(tp_dealloc);
1971 COPYSLOT(tp_print);
1972 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
1973 type->tp_getattr = base->tp_getattr;
1974 type->tp_getattro = base->tp_getattro;
1975 }
1976 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
1977 type->tp_setattr = base->tp_setattr;
1978 type->tp_setattro = base->tp_setattro;
1979 }
1980 /* tp_compare see tp_richcompare */
1981 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00001982 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001983 COPYSLOT(tp_call);
1984 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001985 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00001986 if (type->tp_compare == NULL &&
1987 type->tp_richcompare == NULL &&
1988 type->tp_hash == NULL)
1989 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001990 type->tp_compare = base->tp_compare;
1991 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00001992 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001993 }
1994 }
1995 else {
1996 COPYSLOT(tp_compare);
1997 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001998 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
1999 COPYSLOT(tp_iter);
2000 COPYSLOT(tp_iternext);
2001 }
2002 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2003 COPYSLOT(tp_descr_get);
2004 COPYSLOT(tp_descr_set);
2005 COPYSLOT(tp_dictoffset);
2006 COPYSLOT(tp_init);
2007 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002008 COPYSLOT(tp_free);
2009 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002010}
2011
Guido van Rossum13d52f02001-08-10 21:24:08 +00002012staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00002013staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002014
Tim Peters6d6c1a32001-08-02 04:15:00 +00002015int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002016PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002017{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002018 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002019 PyTypeObject *base;
2020 int i, n;
2021
Guido van Rossumd614f972001-08-10 17:39:49 +00002022 if (type->tp_flags & Py_TPFLAGS_READY) {
2023 assert(type->tp_dict != NULL);
2024 return 0;
2025 }
2026 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00002027
2028 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002029
Guido van Rossumf884b742001-12-17 17:14:22 +00002030 /* Initialize ob_type if NULL. This means extensions that want to be
2031 compilable separately on Windows can call PyType_Ready() instead of
2032 initializing the ob_type field of their type objects. */
2033 if (type->ob_type == NULL)
2034 type->ob_type = &PyType_Type;
2035
Tim Peters6d6c1a32001-08-02 04:15:00 +00002036 /* Initialize tp_base (defaults to BaseObject unless that's us) */
2037 base = type->tp_base;
2038 if (base == NULL && type != &PyBaseObject_Type)
2039 base = type->tp_base = &PyBaseObject_Type;
2040
2041 /* Initialize tp_bases */
2042 bases = type->tp_bases;
2043 if (bases == NULL) {
2044 if (base == NULL)
2045 bases = PyTuple_New(0);
2046 else
2047 bases = Py_BuildValue("(O)", base);
2048 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002049 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002050 type->tp_bases = bases;
2051 }
2052
2053 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002054 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002055 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002056 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002057 }
2058
Guido van Rossum687ae002001-10-15 22:03:32 +00002059 /* Initialize tp_dict */
2060 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002061 if (dict == NULL) {
2062 dict = PyDict_New();
2063 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002064 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00002065 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002066 }
2067
Guido van Rossum687ae002001-10-15 22:03:32 +00002068 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002070 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002071 if (type->tp_methods != NULL) {
2072 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002073 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002074 }
2075 if (type->tp_members != NULL) {
2076 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002077 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002078 }
2079 if (type->tp_getset != NULL) {
2080 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002081 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002082 }
2083
Tim Peters6d6c1a32001-08-02 04:15:00 +00002084 /* Calculate method resolution order */
2085 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002086 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002087 }
2088
Guido van Rossum13d52f02001-08-10 21:24:08 +00002089 /* Inherit special flags from dominant base */
2090 if (type->tp_base != NULL)
2091 inherit_special(type, type->tp_base);
2092
Tim Peters6d6c1a32001-08-02 04:15:00 +00002093 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002094 bases = type->tp_mro;
2095 assert(bases != NULL);
2096 assert(PyTuple_Check(bases));
2097 n = PyTuple_GET_SIZE(bases);
2098 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002099 PyObject *b = PyTuple_GET_ITEM(bases, i);
2100 if (PyType_Check(b))
2101 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002102 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002103
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002104 /* if the type dictionary doesn't contain a __doc__, set it from
2105 the tp_doc slot.
2106 */
2107 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2108 if (type->tp_doc != NULL) {
2109 PyObject *doc = PyString_FromString(type->tp_doc);
2110 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2111 Py_DECREF(doc);
2112 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00002113 PyDict_SetItemString(type->tp_dict,
2114 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002115 }
2116 }
2117
Guido van Rossum13d52f02001-08-10 21:24:08 +00002118 /* Some more special stuff */
2119 base = type->tp_base;
2120 if (base != NULL) {
2121 if (type->tp_as_number == NULL)
2122 type->tp_as_number = base->tp_as_number;
2123 if (type->tp_as_sequence == NULL)
2124 type->tp_as_sequence = base->tp_as_sequence;
2125 if (type->tp_as_mapping == NULL)
2126 type->tp_as_mapping = base->tp_as_mapping;
2127 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128
Guido van Rossum1c450732001-10-08 15:18:27 +00002129 /* Link into each base class's list of subclasses */
2130 bases = type->tp_bases;
2131 n = PyTuple_GET_SIZE(bases);
2132 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002133 PyObject *b = PyTuple_GET_ITEM(bases, i);
2134 if (PyType_Check(b) &&
2135 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002136 goto error;
2137 }
2138
Guido van Rossum13d52f02001-08-10 21:24:08 +00002139 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002140 assert(type->tp_dict != NULL);
2141 type->tp_flags =
2142 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002144
2145 error:
2146 type->tp_flags &= ~Py_TPFLAGS_READYING;
2147 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002148}
2149
Guido van Rossum1c450732001-10-08 15:18:27 +00002150static int
2151add_subclass(PyTypeObject *base, PyTypeObject *type)
2152{
2153 int i;
2154 PyObject *list, *ref, *new;
2155
2156 list = base->tp_subclasses;
2157 if (list == NULL) {
2158 base->tp_subclasses = list = PyList_New(0);
2159 if (list == NULL)
2160 return -1;
2161 }
2162 assert(PyList_Check(list));
2163 new = PyWeakref_NewRef((PyObject *)type, NULL);
2164 i = PyList_GET_SIZE(list);
2165 while (--i >= 0) {
2166 ref = PyList_GET_ITEM(list, i);
2167 assert(PyWeakref_CheckRef(ref));
2168 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2169 return PyList_SetItem(list, i, new);
2170 }
2171 i = PyList_Append(list, new);
2172 Py_DECREF(new);
2173 return i;
2174}
2175
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176
2177/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2178
2179/* There's a wrapper *function* for each distinct function typedef used
2180 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2181 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2182 Most tables have only one entry; the tables for binary operators have two
2183 entries, one regular and one with reversed arguments. */
2184
2185static PyObject *
2186wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2187{
2188 inquiry func = (inquiry)wrapped;
2189 int res;
2190
2191 if (!PyArg_ParseTuple(args, ""))
2192 return NULL;
2193 res = (*func)(self);
2194 if (res == -1 && PyErr_Occurred())
2195 return NULL;
2196 return PyInt_FromLong((long)res);
2197}
2198
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199static PyObject *
2200wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2201{
2202 binaryfunc func = (binaryfunc)wrapped;
2203 PyObject *other;
2204
2205 if (!PyArg_ParseTuple(args, "O", &other))
2206 return NULL;
2207 return (*func)(self, other);
2208}
2209
2210static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002211wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2212{
2213 binaryfunc func = (binaryfunc)wrapped;
2214 PyObject *other;
2215
2216 if (!PyArg_ParseTuple(args, "O", &other))
2217 return NULL;
2218 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002219 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002220 Py_INCREF(Py_NotImplemented);
2221 return Py_NotImplemented;
2222 }
2223 return (*func)(self, other);
2224}
2225
2226static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002227wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2228{
2229 binaryfunc func = (binaryfunc)wrapped;
2230 PyObject *other;
2231
2232 if (!PyArg_ParseTuple(args, "O", &other))
2233 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002234 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002235 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002236 Py_INCREF(Py_NotImplemented);
2237 return Py_NotImplemented;
2238 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002239 return (*func)(other, self);
2240}
2241
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002242static PyObject *
2243wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2244{
2245 coercion func = (coercion)wrapped;
2246 PyObject *other, *res;
2247 int ok;
2248
2249 if (!PyArg_ParseTuple(args, "O", &other))
2250 return NULL;
2251 ok = func(&self, &other);
2252 if (ok < 0)
2253 return NULL;
2254 if (ok > 0) {
2255 Py_INCREF(Py_NotImplemented);
2256 return Py_NotImplemented;
2257 }
2258 res = PyTuple_New(2);
2259 if (res == NULL) {
2260 Py_DECREF(self);
2261 Py_DECREF(other);
2262 return NULL;
2263 }
2264 PyTuple_SET_ITEM(res, 0, self);
2265 PyTuple_SET_ITEM(res, 1, other);
2266 return res;
2267}
2268
Tim Peters6d6c1a32001-08-02 04:15:00 +00002269static PyObject *
2270wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2271{
2272 ternaryfunc func = (ternaryfunc)wrapped;
2273 PyObject *other;
2274 PyObject *third = Py_None;
2275
2276 /* Note: This wrapper only works for __pow__() */
2277
2278 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2279 return NULL;
2280 return (*func)(self, other, third);
2281}
2282
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002283static PyObject *
2284wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2285{
2286 ternaryfunc func = (ternaryfunc)wrapped;
2287 PyObject *other;
2288 PyObject *third = Py_None;
2289
2290 /* Note: This wrapper only works for __pow__() */
2291
2292 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2293 return NULL;
2294 return (*func)(other, self, third);
2295}
2296
Tim Peters6d6c1a32001-08-02 04:15:00 +00002297static PyObject *
2298wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2299{
2300 unaryfunc func = (unaryfunc)wrapped;
2301
2302 if (!PyArg_ParseTuple(args, ""))
2303 return NULL;
2304 return (*func)(self);
2305}
2306
Tim Peters6d6c1a32001-08-02 04:15:00 +00002307static PyObject *
2308wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2309{
2310 intargfunc func = (intargfunc)wrapped;
2311 int i;
2312
2313 if (!PyArg_ParseTuple(args, "i", &i))
2314 return NULL;
2315 return (*func)(self, i);
2316}
2317
Guido van Rossum5d815f32001-08-17 21:57:47 +00002318static int
2319getindex(PyObject *self, PyObject *arg)
2320{
2321 int i;
2322
2323 i = PyInt_AsLong(arg);
2324 if (i == -1 && PyErr_Occurred())
2325 return -1;
2326 if (i < 0) {
2327 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2328 if (sq && sq->sq_length) {
2329 int n = (*sq->sq_length)(self);
2330 if (n < 0)
2331 return -1;
2332 i += n;
2333 }
2334 }
2335 return i;
2336}
2337
2338static PyObject *
2339wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2340{
2341 intargfunc func = (intargfunc)wrapped;
2342 PyObject *arg;
2343 int i;
2344
Guido van Rossumf4593e02001-10-03 12:09:30 +00002345 if (PyTuple_GET_SIZE(args) == 1) {
2346 arg = PyTuple_GET_ITEM(args, 0);
2347 i = getindex(self, arg);
2348 if (i == -1 && PyErr_Occurred())
2349 return NULL;
2350 return (*func)(self, i);
2351 }
2352 PyArg_ParseTuple(args, "O", &arg);
2353 assert(PyErr_Occurred());
2354 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002355}
2356
Tim Peters6d6c1a32001-08-02 04:15:00 +00002357static PyObject *
2358wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2359{
2360 intintargfunc func = (intintargfunc)wrapped;
2361 int i, j;
2362
2363 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2364 return NULL;
2365 return (*func)(self, i, j);
2366}
2367
Tim Peters6d6c1a32001-08-02 04:15:00 +00002368static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002369wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002370{
2371 intobjargproc func = (intobjargproc)wrapped;
2372 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002373 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002374
Guido van Rossum5d815f32001-08-17 21:57:47 +00002375 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2376 return NULL;
2377 i = getindex(self, arg);
2378 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002379 return NULL;
2380 res = (*func)(self, i, value);
2381 if (res == -1 && PyErr_Occurred())
2382 return NULL;
2383 Py_INCREF(Py_None);
2384 return Py_None;
2385}
2386
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002387static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002388wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002389{
2390 intobjargproc func = (intobjargproc)wrapped;
2391 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002392 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002393
Guido van Rossum5d815f32001-08-17 21:57:47 +00002394 if (!PyArg_ParseTuple(args, "O", &arg))
2395 return NULL;
2396 i = getindex(self, arg);
2397 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002398 return NULL;
2399 res = (*func)(self, i, NULL);
2400 if (res == -1 && PyErr_Occurred())
2401 return NULL;
2402 Py_INCREF(Py_None);
2403 return Py_None;
2404}
2405
Tim Peters6d6c1a32001-08-02 04:15:00 +00002406static PyObject *
2407wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2408{
2409 intintobjargproc func = (intintobjargproc)wrapped;
2410 int i, j, res;
2411 PyObject *value;
2412
2413 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2414 return NULL;
2415 res = (*func)(self, i, j, value);
2416 if (res == -1 && PyErr_Occurred())
2417 return NULL;
2418 Py_INCREF(Py_None);
2419 return Py_None;
2420}
2421
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002422static PyObject *
2423wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2424{
2425 intintobjargproc func = (intintobjargproc)wrapped;
2426 int i, j, res;
2427
2428 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2429 return NULL;
2430 res = (*func)(self, i, j, NULL);
2431 if (res == -1 && PyErr_Occurred())
2432 return NULL;
2433 Py_INCREF(Py_None);
2434 return Py_None;
2435}
2436
Tim Peters6d6c1a32001-08-02 04:15:00 +00002437/* XXX objobjproc is a misnomer; should be objargpred */
2438static PyObject *
2439wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2440{
2441 objobjproc func = (objobjproc)wrapped;
2442 int res;
2443 PyObject *value;
2444
2445 if (!PyArg_ParseTuple(args, "O", &value))
2446 return NULL;
2447 res = (*func)(self, value);
2448 if (res == -1 && PyErr_Occurred())
2449 return NULL;
2450 return PyInt_FromLong((long)res);
2451}
2452
Tim Peters6d6c1a32001-08-02 04:15:00 +00002453static PyObject *
2454wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2455{
2456 objobjargproc func = (objobjargproc)wrapped;
2457 int res;
2458 PyObject *key, *value;
2459
2460 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2461 return NULL;
2462 res = (*func)(self, key, value);
2463 if (res == -1 && PyErr_Occurred())
2464 return NULL;
2465 Py_INCREF(Py_None);
2466 return Py_None;
2467}
2468
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002469static PyObject *
2470wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2471{
2472 objobjargproc func = (objobjargproc)wrapped;
2473 int res;
2474 PyObject *key;
2475
2476 if (!PyArg_ParseTuple(args, "O", &key))
2477 return NULL;
2478 res = (*func)(self, key, NULL);
2479 if (res == -1 && PyErr_Occurred())
2480 return NULL;
2481 Py_INCREF(Py_None);
2482 return Py_None;
2483}
2484
Tim Peters6d6c1a32001-08-02 04:15:00 +00002485static PyObject *
2486wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2487{
2488 cmpfunc func = (cmpfunc)wrapped;
2489 int res;
2490 PyObject *other;
2491
2492 if (!PyArg_ParseTuple(args, "O", &other))
2493 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002494 if (other->ob_type->tp_compare != func &&
2495 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002496 PyErr_Format(
2497 PyExc_TypeError,
2498 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2499 self->ob_type->tp_name,
2500 self->ob_type->tp_name,
2501 other->ob_type->tp_name);
2502 return NULL;
2503 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504 res = (*func)(self, other);
2505 if (PyErr_Occurred())
2506 return NULL;
2507 return PyInt_FromLong((long)res);
2508}
2509
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510static PyObject *
2511wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2512{
2513 setattrofunc func = (setattrofunc)wrapped;
2514 int res;
2515 PyObject *name, *value;
2516
2517 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2518 return NULL;
2519 res = (*func)(self, name, value);
2520 if (res < 0)
2521 return NULL;
2522 Py_INCREF(Py_None);
2523 return Py_None;
2524}
2525
2526static PyObject *
2527wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2528{
2529 setattrofunc func = (setattrofunc)wrapped;
2530 int res;
2531 PyObject *name;
2532
2533 if (!PyArg_ParseTuple(args, "O", &name))
2534 return NULL;
2535 res = (*func)(self, name, NULL);
2536 if (res < 0)
2537 return NULL;
2538 Py_INCREF(Py_None);
2539 return Py_None;
2540}
2541
Tim Peters6d6c1a32001-08-02 04:15:00 +00002542static PyObject *
2543wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2544{
2545 hashfunc func = (hashfunc)wrapped;
2546 long res;
2547
2548 if (!PyArg_ParseTuple(args, ""))
2549 return NULL;
2550 res = (*func)(self);
2551 if (res == -1 && PyErr_Occurred())
2552 return NULL;
2553 return PyInt_FromLong(res);
2554}
2555
Tim Peters6d6c1a32001-08-02 04:15:00 +00002556static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002557wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002558{
2559 ternaryfunc func = (ternaryfunc)wrapped;
2560
Guido van Rossumc8e56452001-10-22 00:43:43 +00002561 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002562}
2563
Tim Peters6d6c1a32001-08-02 04:15:00 +00002564static PyObject *
2565wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2566{
2567 richcmpfunc func = (richcmpfunc)wrapped;
2568 PyObject *other;
2569
2570 if (!PyArg_ParseTuple(args, "O", &other))
2571 return NULL;
2572 return (*func)(self, other, op);
2573}
2574
2575#undef RICHCMP_WRAPPER
2576#define RICHCMP_WRAPPER(NAME, OP) \
2577static PyObject * \
2578richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2579{ \
2580 return wrap_richcmpfunc(self, args, wrapped, OP); \
2581}
2582
Jack Jansen8e938b42001-08-08 15:29:49 +00002583RICHCMP_WRAPPER(lt, Py_LT)
2584RICHCMP_WRAPPER(le, Py_LE)
2585RICHCMP_WRAPPER(eq, Py_EQ)
2586RICHCMP_WRAPPER(ne, Py_NE)
2587RICHCMP_WRAPPER(gt, Py_GT)
2588RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002589
Tim Peters6d6c1a32001-08-02 04:15:00 +00002590static PyObject *
2591wrap_next(PyObject *self, PyObject *args, void *wrapped)
2592{
2593 unaryfunc func = (unaryfunc)wrapped;
2594 PyObject *res;
2595
2596 if (!PyArg_ParseTuple(args, ""))
2597 return NULL;
2598 res = (*func)(self);
2599 if (res == NULL && !PyErr_Occurred())
2600 PyErr_SetNone(PyExc_StopIteration);
2601 return res;
2602}
2603
Tim Peters6d6c1a32001-08-02 04:15:00 +00002604static PyObject *
2605wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2606{
2607 descrgetfunc func = (descrgetfunc)wrapped;
2608 PyObject *obj;
2609 PyObject *type = NULL;
2610
2611 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2612 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002613 return (*func)(self, obj, type);
2614}
2615
Tim Peters6d6c1a32001-08-02 04:15:00 +00002616static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002617wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002618{
2619 descrsetfunc func = (descrsetfunc)wrapped;
2620 PyObject *obj, *value;
2621 int ret;
2622
2623 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2624 return NULL;
2625 ret = (*func)(self, obj, value);
2626 if (ret < 0)
2627 return NULL;
2628 Py_INCREF(Py_None);
2629 return Py_None;
2630}
2631
Tim Peters6d6c1a32001-08-02 04:15:00 +00002632static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002633wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002634{
2635 initproc func = (initproc)wrapped;
2636
Guido van Rossumc8e56452001-10-22 00:43:43 +00002637 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002638 return NULL;
2639 Py_INCREF(Py_None);
2640 return Py_None;
2641}
2642
Tim Peters6d6c1a32001-08-02 04:15:00 +00002643static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002644tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002645{
Barry Warsaw60f01882001-08-22 19:24:42 +00002646 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002647 PyObject *arg0, *res;
2648
2649 if (self == NULL || !PyType_Check(self))
2650 Py_FatalError("__new__() called with non-type 'self'");
2651 type = (PyTypeObject *)self;
2652 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002653 PyErr_Format(PyExc_TypeError,
2654 "%s.__new__(): not enough arguments",
2655 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002656 return NULL;
2657 }
2658 arg0 = PyTuple_GET_ITEM(args, 0);
2659 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002660 PyErr_Format(PyExc_TypeError,
2661 "%s.__new__(X): X is not a type object (%s)",
2662 type->tp_name,
2663 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002664 return NULL;
2665 }
2666 subtype = (PyTypeObject *)arg0;
2667 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002668 PyErr_Format(PyExc_TypeError,
2669 "%s.__new__(%s): %s is not a subtype of %s",
2670 type->tp_name,
2671 subtype->tp_name,
2672 subtype->tp_name,
2673 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002674 return NULL;
2675 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002676
2677 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002678 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002679 most derived base that's not a heap type is this type. */
2680 staticbase = subtype;
2681 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2682 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002683 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002684 PyErr_Format(PyExc_TypeError,
2685 "%s.__new__(%s) is not safe, use %s.__new__()",
2686 type->tp_name,
2687 subtype->tp_name,
2688 staticbase == NULL ? "?" : staticbase->tp_name);
2689 return NULL;
2690 }
2691
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002692 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2693 if (args == NULL)
2694 return NULL;
2695 res = type->tp_new(subtype, args, kwds);
2696 Py_DECREF(args);
2697 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002698}
2699
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002700static struct PyMethodDef tp_new_methoddef[] = {
2701 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2702 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002703 {0}
2704};
2705
2706static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002707add_tp_new_wrapper(PyTypeObject *type)
2708{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002709 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002710
Guido van Rossum687ae002001-10-15 22:03:32 +00002711 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002712 return 0;
2713 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002714 if (func == NULL)
2715 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002716 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002717}
2718
Guido van Rossumf040ede2001-08-07 16:40:56 +00002719/* Slot wrappers that call the corresponding __foo__ slot. See comments
2720 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002721
Guido van Rossumdc91b992001-08-08 22:26:22 +00002722#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002723static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002724FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002725{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002726 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002727 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002728}
2729
Guido van Rossumdc91b992001-08-08 22:26:22 +00002730#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002731static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002732FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002733{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002734 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002735 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002736}
2737
Guido van Rossumdc91b992001-08-08 22:26:22 +00002738
2739#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002740static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002741FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002742{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002743 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002744 int do_other = self->ob_type != other->ob_type && \
2745 other->ob_type->tp_as_number != NULL && \
2746 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002747 if (self->ob_type->tp_as_number != NULL && \
2748 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2749 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002750 if (do_other && \
2751 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2752 r = call_maybe( \
2753 other, ROPSTR, &rcache_str, "(O)", self); \
2754 if (r != Py_NotImplemented) \
2755 return r; \
2756 Py_DECREF(r); \
2757 do_other = 0; \
2758 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002759 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002760 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002761 if (r != Py_NotImplemented || \
2762 other->ob_type == self->ob_type) \
2763 return r; \
2764 Py_DECREF(r); \
2765 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002766 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002767 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002768 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002769 } \
2770 Py_INCREF(Py_NotImplemented); \
2771 return Py_NotImplemented; \
2772}
2773
2774#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2775 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2776
2777#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2778static PyObject * \
2779FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2780{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002781 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002782 return call_method(self, OPSTR, &cache_str, \
2783 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002784}
2785
2786static int
2787slot_sq_length(PyObject *self)
2788{
Guido van Rossum2730b132001-08-28 18:22:14 +00002789 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002790 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002791 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002792
2793 if (res == NULL)
2794 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002795 len = (int)PyInt_AsLong(res);
2796 Py_DECREF(res);
2797 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002798}
2799
Guido van Rossumdc91b992001-08-08 22:26:22 +00002800SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2801SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002802
2803/* Super-optimized version of slot_sq_item.
2804 Other slots could do the same... */
2805static PyObject *
2806slot_sq_item(PyObject *self, int i)
2807{
2808 static PyObject *getitem_str;
2809 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2810 descrgetfunc f;
2811
2812 if (getitem_str == NULL) {
2813 getitem_str = PyString_InternFromString("__getitem__");
2814 if (getitem_str == NULL)
2815 return NULL;
2816 }
2817 func = _PyType_Lookup(self->ob_type, getitem_str);
2818 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002819 if ((f = func->ob_type->tp_descr_get) == NULL)
2820 Py_INCREF(func);
2821 else
2822 func = f(func, self, (PyObject *)(self->ob_type));
2823 ival = PyInt_FromLong(i);
2824 if (ival != NULL) {
2825 args = PyTuple_New(1);
2826 if (args != NULL) {
2827 PyTuple_SET_ITEM(args, 0, ival);
2828 retval = PyObject_Call(func, args, NULL);
2829 Py_XDECREF(args);
2830 Py_XDECREF(func);
2831 return retval;
2832 }
2833 }
2834 }
2835 else {
2836 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2837 }
2838 Py_XDECREF(args);
2839 Py_XDECREF(ival);
2840 Py_XDECREF(func);
2841 return NULL;
2842}
2843
Guido van Rossumdc91b992001-08-08 22:26:22 +00002844SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002845
2846static int
2847slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2848{
2849 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002850 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002851
2852 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002853 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002854 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002855 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002856 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002857 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002858 if (res == NULL)
2859 return -1;
2860 Py_DECREF(res);
2861 return 0;
2862}
2863
2864static int
2865slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2866{
2867 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002868 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002869
2870 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002871 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002872 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002873 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002874 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002875 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002876 if (res == NULL)
2877 return -1;
2878 Py_DECREF(res);
2879 return 0;
2880}
2881
2882static int
2883slot_sq_contains(PyObject *self, PyObject *value)
2884{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002885 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002886 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002887
Guido van Rossum55f20992001-10-01 17:18:22 +00002888 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002889
2890 if (func != NULL) {
2891 args = Py_BuildValue("(O)", value);
2892 if (args == NULL)
2893 res = NULL;
2894 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002895 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002896 Py_DECREF(args);
2897 }
2898 Py_DECREF(func);
2899 if (res == NULL)
2900 return -1;
2901 return PyObject_IsTrue(res);
2902 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002903 else if (PyErr_Occurred())
2904 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002905 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002906 return _PySequence_IterSearch(self, value,
2907 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002908 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002909}
2910
Guido van Rossumdc91b992001-08-08 22:26:22 +00002911SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2912SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002913
2914#define slot_mp_length slot_sq_length
2915
Guido van Rossumdc91b992001-08-08 22:26:22 +00002916SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002917
2918static int
2919slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2920{
2921 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002922 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002923
2924 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002925 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002926 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002927 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002928 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002929 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002930 if (res == NULL)
2931 return -1;
2932 Py_DECREF(res);
2933 return 0;
2934}
2935
Guido van Rossumdc91b992001-08-08 22:26:22 +00002936SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2937SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2938SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2939SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2940SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
2941SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
2942
2943staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
2944
2945SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
2946 nb_power, "__pow__", "__rpow__")
2947
2948static PyObject *
2949slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
2950{
Guido van Rossum2730b132001-08-28 18:22:14 +00002951 static PyObject *pow_str;
2952
Guido van Rossumdc91b992001-08-08 22:26:22 +00002953 if (modulus == Py_None)
2954 return slot_nb_power_binary(self, other);
2955 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00002956 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002957 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002958}
2959
2960SLOT0(slot_nb_negative, "__neg__")
2961SLOT0(slot_nb_positive, "__pos__")
2962SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002963
2964static int
2965slot_nb_nonzero(PyObject *self)
2966{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002967 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00002968 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002969
Guido van Rossum55f20992001-10-01 17:18:22 +00002970 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002971 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00002972 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00002973 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00002974 func = lookup_maybe(self, "__len__", &len_str);
2975 if (func == NULL) {
2976 if (PyErr_Occurred())
2977 return -1;
2978 else
2979 return 1;
2980 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00002981 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002982 res = PyObject_CallObject(func, NULL);
2983 Py_DECREF(func);
2984 if (res == NULL)
2985 return -1;
2986 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002987}
2988
Guido van Rossumdc91b992001-08-08 22:26:22 +00002989SLOT0(slot_nb_invert, "__invert__")
2990SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
2991SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
2992SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
2993SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
2994SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002995
2996static int
2997slot_nb_coerce(PyObject **a, PyObject **b)
2998{
2999 static PyObject *coerce_str;
3000 PyObject *self = *a, *other = *b;
3001
3002 if (self->ob_type->tp_as_number != NULL &&
3003 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3004 PyObject *r;
3005 r = call_maybe(
3006 self, "__coerce__", &coerce_str, "(O)", other);
3007 if (r == NULL)
3008 return -1;
3009 if (r == Py_NotImplemented) {
3010 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003011 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003012 else {
3013 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3014 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003015 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003016 Py_DECREF(r);
3017 return -1;
3018 }
3019 *a = PyTuple_GET_ITEM(r, 0);
3020 Py_INCREF(*a);
3021 *b = PyTuple_GET_ITEM(r, 1);
3022 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003023 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003024 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003025 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003026 }
3027 if (other->ob_type->tp_as_number != NULL &&
3028 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3029 PyObject *r;
3030 r = call_maybe(
3031 other, "__coerce__", &coerce_str, "(O)", self);
3032 if (r == NULL)
3033 return -1;
3034 if (r == Py_NotImplemented) {
3035 Py_DECREF(r);
3036 return 1;
3037 }
3038 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3039 PyErr_SetString(PyExc_TypeError,
3040 "__coerce__ didn't return a 2-tuple");
3041 Py_DECREF(r);
3042 return -1;
3043 }
3044 *a = PyTuple_GET_ITEM(r, 1);
3045 Py_INCREF(*a);
3046 *b = PyTuple_GET_ITEM(r, 0);
3047 Py_INCREF(*b);
3048 Py_DECREF(r);
3049 return 0;
3050 }
3051 return 1;
3052}
3053
Guido van Rossumdc91b992001-08-08 22:26:22 +00003054SLOT0(slot_nb_int, "__int__")
3055SLOT0(slot_nb_long, "__long__")
3056SLOT0(slot_nb_float, "__float__")
3057SLOT0(slot_nb_oct, "__oct__")
3058SLOT0(slot_nb_hex, "__hex__")
3059SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3060SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3061SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3062SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3063SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3064SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3065SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3066SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3067SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3068SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3069SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3070SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3071 "__floordiv__", "__rfloordiv__")
3072SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3073SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3074SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003075
3076static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003077half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003078{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003079 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003080 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003081 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003082
Guido van Rossum60718732001-08-28 17:47:51 +00003083 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003084 if (func == NULL) {
3085 PyErr_Clear();
3086 }
3087 else {
3088 args = Py_BuildValue("(O)", other);
3089 if (args == NULL)
3090 res = NULL;
3091 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003092 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003093 Py_DECREF(args);
3094 }
3095 if (res != Py_NotImplemented) {
3096 if (res == NULL)
3097 return -2;
3098 c = PyInt_AsLong(res);
3099 Py_DECREF(res);
3100 if (c == -1 && PyErr_Occurred())
3101 return -2;
3102 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3103 }
3104 Py_DECREF(res);
3105 }
3106 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003107}
3108
Guido van Rossumab3b0342001-09-18 20:38:53 +00003109/* This slot is published for the benefit of try_3way_compare in object.c */
3110int
3111_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003112{
3113 int c;
3114
Guido van Rossumab3b0342001-09-18 20:38:53 +00003115 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003116 c = half_compare(self, other);
3117 if (c <= 1)
3118 return c;
3119 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003120 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003121 c = half_compare(other, self);
3122 if (c < -1)
3123 return -2;
3124 if (c <= 1)
3125 return -c;
3126 }
3127 return (void *)self < (void *)other ? -1 :
3128 (void *)self > (void *)other ? 1 : 0;
3129}
3130
3131static PyObject *
3132slot_tp_repr(PyObject *self)
3133{
3134 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003135 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003136
Guido van Rossum60718732001-08-28 17:47:51 +00003137 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003138 if (func != NULL) {
3139 res = PyEval_CallObject(func, NULL);
3140 Py_DECREF(func);
3141 return res;
3142 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003143 PyErr_Clear();
3144 return PyString_FromFormat("<%s object at %p>",
3145 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003146}
3147
3148static PyObject *
3149slot_tp_str(PyObject *self)
3150{
3151 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003152 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003153
Guido van Rossum60718732001-08-28 17:47:51 +00003154 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003155 if (func != NULL) {
3156 res = PyEval_CallObject(func, NULL);
3157 Py_DECREF(func);
3158 return res;
3159 }
3160 else {
3161 PyErr_Clear();
3162 return slot_tp_repr(self);
3163 }
3164}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003165
3166static long
3167slot_tp_hash(PyObject *self)
3168{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003169 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003170 static PyObject *hash_str, *eq_str, *cmp_str;
3171
Tim Peters6d6c1a32001-08-02 04:15:00 +00003172 long h;
3173
Guido van Rossum60718732001-08-28 17:47:51 +00003174 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003175
3176 if (func != NULL) {
3177 res = PyEval_CallObject(func, NULL);
3178 Py_DECREF(func);
3179 if (res == NULL)
3180 return -1;
3181 h = PyInt_AsLong(res);
3182 }
3183 else {
3184 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003185 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003186 if (func == NULL) {
3187 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003188 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003189 }
3190 if (func != NULL) {
3191 Py_DECREF(func);
3192 PyErr_SetString(PyExc_TypeError, "unhashable type");
3193 return -1;
3194 }
3195 PyErr_Clear();
3196 h = _Py_HashPointer((void *)self);
3197 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003198 if (h == -1 && !PyErr_Occurred())
3199 h = -2;
3200 return h;
3201}
3202
3203static PyObject *
3204slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3205{
Guido van Rossum60718732001-08-28 17:47:51 +00003206 static PyObject *call_str;
3207 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003208 PyObject *res;
3209
3210 if (meth == NULL)
3211 return NULL;
3212 res = PyObject_Call(meth, args, kwds);
3213 Py_DECREF(meth);
3214 return res;
3215}
3216
Guido van Rossum14a6f832001-10-17 13:59:09 +00003217/* There are two slot dispatch functions for tp_getattro.
3218
3219 - slot_tp_getattro() is used when __getattribute__ is overridden
3220 but no __getattr__ hook is present;
3221
3222 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3223
3224 The code in update_slot() and fixup_slot_dispatchers() always installs
3225 slot_tp_getattr_hook(); this detects the absence of __getattr__ and then
3226 installs the simpler slot if necessary. */
3227
Tim Peters6d6c1a32001-08-02 04:15:00 +00003228static PyObject *
3229slot_tp_getattro(PyObject *self, PyObject *name)
3230{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003231 static PyObject *getattribute_str = NULL;
3232 return call_method(self, "__getattribute__", &getattribute_str,
3233 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003234}
3235
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003236static PyObject *
3237slot_tp_getattr_hook(PyObject *self, PyObject *name)
3238{
3239 PyTypeObject *tp = self->ob_type;
3240 PyObject *getattr, *getattribute, *res;
3241 static PyObject *getattribute_str = NULL;
3242 static PyObject *getattr_str = NULL;
3243
3244 if (getattr_str == NULL) {
3245 getattr_str = PyString_InternFromString("__getattr__");
3246 if (getattr_str == NULL)
3247 return NULL;
3248 }
3249 if (getattribute_str == NULL) {
3250 getattribute_str =
3251 PyString_InternFromString("__getattribute__");
3252 if (getattribute_str == NULL)
3253 return NULL;
3254 }
3255 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003256 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003257 /* No __getattr__ hook: use a simpler dispatcher */
3258 tp->tp_getattro = slot_tp_getattro;
3259 return slot_tp_getattro(self, name);
3260 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003261 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003262 if (getattribute == NULL ||
3263 (getattribute->ob_type == &PyWrapperDescr_Type &&
3264 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3265 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003266 res = PyObject_GenericGetAttr(self, name);
3267 else
3268 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003269 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003270 PyErr_Clear();
3271 res = PyObject_CallFunction(getattr, "OO", self, name);
3272 }
3273 return res;
3274}
3275
Tim Peters6d6c1a32001-08-02 04:15:00 +00003276static int
3277slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3278{
3279 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003280 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003281
3282 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003283 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003284 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003285 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003286 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003287 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003288 if (res == NULL)
3289 return -1;
3290 Py_DECREF(res);
3291 return 0;
3292}
3293
3294/* Map rich comparison operators to their __xx__ namesakes */
3295static char *name_op[] = {
3296 "__lt__",
3297 "__le__",
3298 "__eq__",
3299 "__ne__",
3300 "__gt__",
3301 "__ge__",
3302};
3303
3304static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003305half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003306{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003307 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003308 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003309
Guido van Rossum60718732001-08-28 17:47:51 +00003310 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003311 if (func == NULL) {
3312 PyErr_Clear();
3313 Py_INCREF(Py_NotImplemented);
3314 return Py_NotImplemented;
3315 }
3316 args = Py_BuildValue("(O)", other);
3317 if (args == NULL)
3318 res = NULL;
3319 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003320 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003321 Py_DECREF(args);
3322 }
3323 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003324 return res;
3325}
3326
Guido van Rossumb8f63662001-08-15 23:57:02 +00003327/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3328static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3329
3330static PyObject *
3331slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3332{
3333 PyObject *res;
3334
3335 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3336 res = half_richcompare(self, other, op);
3337 if (res != Py_NotImplemented)
3338 return res;
3339 Py_DECREF(res);
3340 }
3341 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3342 res = half_richcompare(other, self, swapped_op[op]);
3343 if (res != Py_NotImplemented) {
3344 return res;
3345 }
3346 Py_DECREF(res);
3347 }
3348 Py_INCREF(Py_NotImplemented);
3349 return Py_NotImplemented;
3350}
3351
3352static PyObject *
3353slot_tp_iter(PyObject *self)
3354{
3355 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003356 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003357
Guido van Rossum60718732001-08-28 17:47:51 +00003358 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003359 if (func != NULL) {
3360 res = PyObject_CallObject(func, NULL);
3361 Py_DECREF(func);
3362 return res;
3363 }
3364 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003365 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003366 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00003367 PyErr_SetString(PyExc_TypeError,
3368 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003369 return NULL;
3370 }
3371 Py_DECREF(func);
3372 return PySeqIter_New(self);
3373}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003374
3375static PyObject *
3376slot_tp_iternext(PyObject *self)
3377{
Guido van Rossum2730b132001-08-28 18:22:14 +00003378 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003379 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003380}
3381
Guido van Rossum1a493502001-08-17 16:47:50 +00003382static PyObject *
3383slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3384{
3385 PyTypeObject *tp = self->ob_type;
3386 PyObject *get;
3387 static PyObject *get_str = NULL;
3388
3389 if (get_str == NULL) {
3390 get_str = PyString_InternFromString("__get__");
3391 if (get_str == NULL)
3392 return NULL;
3393 }
3394 get = _PyType_Lookup(tp, get_str);
3395 if (get == NULL) {
3396 /* Avoid further slowdowns */
3397 if (tp->tp_descr_get == slot_tp_descr_get)
3398 tp->tp_descr_get = NULL;
3399 Py_INCREF(self);
3400 return self;
3401 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003402 if (obj == NULL)
3403 obj = Py_None;
3404 if (type == NULL)
3405 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003406 return PyObject_CallFunction(get, "OOO", self, obj, type);
3407}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003408
3409static int
3410slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3411{
Guido van Rossum2c252392001-08-24 10:13:31 +00003412 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003413 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003414
3415 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003416 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003417 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003418 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003419 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003420 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003421 if (res == NULL)
3422 return -1;
3423 Py_DECREF(res);
3424 return 0;
3425}
3426
3427static int
3428slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3429{
Guido van Rossum60718732001-08-28 17:47:51 +00003430 static PyObject *init_str;
3431 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003432 PyObject *res;
3433
3434 if (meth == NULL)
3435 return -1;
3436 res = PyObject_Call(meth, args, kwds);
3437 Py_DECREF(meth);
3438 if (res == NULL)
3439 return -1;
3440 Py_DECREF(res);
3441 return 0;
3442}
3443
3444static PyObject *
3445slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3446{
3447 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3448 PyObject *newargs, *x;
3449 int i, n;
3450
3451 if (func == NULL)
3452 return NULL;
3453 assert(PyTuple_Check(args));
3454 n = PyTuple_GET_SIZE(args);
3455 newargs = PyTuple_New(n+1);
3456 if (newargs == NULL)
3457 return NULL;
3458 Py_INCREF(type);
3459 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3460 for (i = 0; i < n; i++) {
3461 x = PyTuple_GET_ITEM(args, i);
3462 Py_INCREF(x);
3463 PyTuple_SET_ITEM(newargs, i+1, x);
3464 }
3465 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003466 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003467 Py_DECREF(func);
3468 return x;
3469}
3470
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003471
3472/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3473 functions. The offsets here are relative to the 'etype' structure, which
3474 incorporates the additional structures used for numbers, sequences and
3475 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3476 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
3477 slots (e.g. __str__ affects tp_str as well as tp_repr). */
3478
Guido van Rossum6d204072001-10-21 00:44:31 +00003479typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003480
3481#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003482#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003483#undef ETSLOT
3484#undef SQSLOT
3485#undef MPSLOT
3486#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003487#undef UNSLOT
3488#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003489#undef BINSLOT
3490#undef RBINSLOT
3491
Guido van Rossum6d204072001-10-21 00:44:31 +00003492#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3493 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003494#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3495 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3496 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003497#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3498 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3499#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3500 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3501#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3502 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3503#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3504 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3505#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3506 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3507 "x." NAME "() <==> " DOC)
3508#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3509 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3510 "x." NAME "(y) <==> x" DOC "y")
3511#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3512 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3513 "x." NAME "(y) <==> x" DOC "y")
3514#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3515 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3516 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003517
3518static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003519 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3520 "x.__len__() <==> len(x)"),
3521 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3522 "x.__add__(y) <==> x+y"),
3523 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3524 "x.__mul__(n) <==> x*n"),
3525 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3526 "x.__rmul__(n) <==> n*x"),
3527 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3528 "x.__getitem__(y) <==> x[y]"),
3529 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3530 "x.__getslice__(i, j) <==> x[i:j]"),
3531 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3532 "x.__setitem__(i, y) <==> x[i]=y"),
3533 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3534 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003535 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003536 wrap_intintobjargproc,
3537 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3538 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3539 "x.__delslice__(i, j) <==> del x[i:j]"),
3540 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3541 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003542 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003543 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003544 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003545 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003546
Guido van Rossum6d204072001-10-21 00:44:31 +00003547 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3548 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003549 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003550 wrap_binaryfunc,
3551 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003552 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003553 wrap_objobjargproc,
3554 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003555 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003556 wrap_delitem,
3557 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003558
Guido van Rossum6d204072001-10-21 00:44:31 +00003559 BINSLOT("__add__", nb_add, slot_nb_add,
3560 "+"),
3561 RBINSLOT("__radd__", nb_add, slot_nb_add,
3562 "+"),
3563 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3564 "-"),
3565 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3566 "-"),
3567 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3568 "*"),
3569 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3570 "*"),
3571 BINSLOT("__div__", nb_divide, slot_nb_divide,
3572 "/"),
3573 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3574 "/"),
3575 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3576 "%"),
3577 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3578 "%"),
3579 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3580 "divmod(x, y)"),
3581 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3582 "divmod(y, x)"),
3583 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3584 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3585 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3586 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3587 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3588 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3589 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3590 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00003591 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00003592 "x != 0"),
3593 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3594 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3595 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3596 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3597 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3598 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3599 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3600 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3601 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3602 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3603 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3604 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3605 "x.__coerce__(y) <==> coerce(x, y)"),
3606 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3607 "int(x)"),
3608 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3609 "long(x)"),
3610 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3611 "float(x)"),
3612 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3613 "oct(x)"),
3614 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3615 "hex(x)"),
3616 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3617 wrap_binaryfunc, "+"),
3618 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3619 wrap_binaryfunc, "-"),
3620 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3621 wrap_binaryfunc, "*"),
3622 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3623 wrap_binaryfunc, "/"),
3624 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3625 wrap_binaryfunc, "%"),
3626 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3627 wrap_ternaryfunc, "**"),
3628 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3629 wrap_binaryfunc, "<<"),
3630 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3631 wrap_binaryfunc, ">>"),
3632 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3633 wrap_binaryfunc, "&"),
3634 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3635 wrap_binaryfunc, "^"),
3636 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3637 wrap_binaryfunc, "|"),
3638 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3639 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3640 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3641 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3642 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3643 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3644 IBSLOT("__itruediv__", nb_inplace_true_divide,
3645 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003646
Guido van Rossum6d204072001-10-21 00:44:31 +00003647 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3648 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003649 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003650 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3651 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003652 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003653 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3654 "x.__cmp__(y) <==> cmp(x,y)"),
3655 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3656 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003657 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3658 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003659 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003660 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3661 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3662 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3663 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3664 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3665 "x.__setattr__('name', value) <==> x.name = value"),
3666 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3667 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3668 "x.__delattr__('name') <==> del x.name"),
3669 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3670 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3671 "x.__lt__(y) <==> x<y"),
3672 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3673 "x.__le__(y) <==> x<=y"),
3674 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3675 "x.__eq__(y) <==> x==y"),
3676 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3677 "x.__ne__(y) <==> x!=y"),
3678 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3679 "x.__gt__(y) <==> x>y"),
3680 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3681 "x.__ge__(y) <==> x>=y"),
3682 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3683 "x.__iter__() <==> iter(x)"),
3684 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3685 "x.next() -> the next value, or raise StopIteration"),
3686 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3687 "descr.__get__(obj[, type]) -> value"),
3688 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3689 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003690 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003691 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003692 "see x.__class__.__doc__ for signature",
3693 PyWrapperFlag_KEYWORDS),
3694 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003695 {NULL}
3696};
3697
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003698static void **
3699slotptr(PyTypeObject *type, int offset)
3700{
3701 char *ptr;
3702
3703 assert(offset >= 0);
3704 assert(offset < offsetof(etype, as_buffer));
3705 if (offset >= offsetof(etype, as_mapping)) {
3706 ptr = (void *)type->tp_as_mapping;
3707 offset -= offsetof(etype, as_mapping);
3708 }
3709 else if (offset >= offsetof(etype, as_sequence)) {
3710 ptr = (void *)type->tp_as_sequence;
3711 offset -= offsetof(etype, as_sequence);
3712 }
3713 else if (offset >= offsetof(etype, as_number)) {
3714 ptr = (void *)type->tp_as_number;
3715 offset -= offsetof(etype, as_number);
3716 }
3717 else {
3718 ptr = (void *)type;
3719 }
3720 if (ptr != NULL)
3721 ptr += offset;
3722 return (void **)ptr;
3723}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003724
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003725staticforward int recurse_down_subclasses(PyTypeObject *type,
3726 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003727
3728static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003729update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003730{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003731 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003732
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003733 for (pp = pp0; *pp; pp++) {
3734 slotdef *p = *pp;
3735 PyObject *descr;
3736 PyWrapperDescrObject *d;
3737 void *generic = NULL, *specific = NULL;
3738 int use_generic = 0;
3739 int offset = p->offset;
3740 void **ptr = slotptr(type, offset);
3741 if (ptr == NULL)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003742 continue;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003743 do {
3744 descr = _PyType_Lookup(type, p->name_strobj);
3745 if (descr == NULL)
3746 continue;
3747 generic = p->function;
3748 if (descr->ob_type == &PyWrapperDescr_Type) {
3749 d = (PyWrapperDescrObject *)descr;
3750 if (d->d_base->wrapper == p->wrapper &&
3751 PyType_IsSubtype(type, d->d_type)) {
3752 if (specific == NULL ||
3753 specific == d->d_wrapped)
3754 specific = d->d_wrapped;
3755 else
3756 use_generic = 1;
3757 }
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003758 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003759 else
3760 use_generic = 1;
3761 } while ((++p)->offset == offset);
3762 if (specific && !use_generic)
3763 *ptr = specific;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003764 else
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003765 *ptr = generic;
3766 }
3767 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003768}
3769
3770static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003771recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003772{
3773 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003774 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003775 int i, n;
3776
3777 subclasses = type->tp_subclasses;
3778 if (subclasses == NULL)
3779 return 0;
3780 assert(PyList_Check(subclasses));
3781 n = PyList_GET_SIZE(subclasses);
3782 for (i = 0; i < n; i++) {
3783 ref = PyList_GET_ITEM(subclasses, i);
3784 assert(PyWeakref_CheckRef(ref));
3785 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3786 if (subclass == NULL)
3787 continue;
3788 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003789 /* Avoid recursing down into unaffected classes */
3790 dict = subclass->tp_dict;
3791 if (dict != NULL && PyDict_Check(dict) &&
3792 PyDict_GetItem(dict, name) != NULL)
3793 continue;
3794 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003795 return -1;
3796 }
3797 return 0;
3798}
3799
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003800static int
3801slotdef_cmp(const void *aa, const void *bb)
3802{
3803 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3804 int c = a->offset - b->offset;
3805 if (c != 0)
3806 return c;
3807 else
3808 return a - b;
3809}
3810
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003811static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003812init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003813{
3814 slotdef *p;
3815 static int initialized = 0;
3816
3817 if (initialized)
3818 return;
3819 for (p = slotdefs; p->name; p++) {
3820 p->name_strobj = PyString_InternFromString(p->name);
3821 if (!p->name_strobj)
3822 Py_FatalError("XXX ouch");
3823 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003824 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3825 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003826 initialized = 1;
3827}
3828
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003829static int
3830update_slot(PyTypeObject *type, PyObject *name)
3831{
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003832 slotdef *ptrs[10];
3833 slotdef *p;
3834 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003835 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003836
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003837 init_slotdefs();
3838 pp = ptrs;
3839 for (p = slotdefs; p->name; p++) {
3840 /* XXX assume name is interned! */
3841 if (p->name_strobj == name)
3842 *pp++ = p;
3843 }
3844 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003845 for (pp = ptrs; *pp; pp++) {
3846 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003847 offset = p->offset;
3848 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003849 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003850 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003851 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003852 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003853}
3854
Tim Peters6d6c1a32001-08-02 04:15:00 +00003855static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003856fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003857{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003858 slotdef *p;
3859 PyObject *mro, *descr;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003860 PyWrapperDescrObject *d;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003861 int i, n, offset;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003862 void **ptr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003863 void *generic, *specific;
3864 int use_generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003865
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003866 init_slotdefs();
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003867 mro = type->tp_mro;
3868 assert(PyTuple_Check(mro));
3869 n = PyTuple_GET_SIZE(mro);
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003870 for (p = slotdefs; p->name; ) {
3871 offset = p->offset;
3872 ptr = slotptr(type, offset);
3873 if (!ptr) {
3874 do {
3875 ++p;
3876 } while (p->offset == offset);
3877 continue;
3878 }
3879 generic = specific = NULL;
3880 use_generic = 0;
3881 do {
3882 descr = NULL;
3883 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00003884 PyObject *b = PyTuple_GET_ITEM(mro, i);
3885 PyObject *dict = NULL;
3886 if (PyType_Check(b))
3887 dict = ((PyTypeObject *)b)->tp_dict;
3888 else if (PyClass_Check(b))
3889 dict = ((PyClassObject *)b)->cl_dict;
3890 if (dict != NULL) {
3891 descr = PyDict_GetItem(
3892 dict, p->name_strobj);
3893 if (descr != NULL)
3894 break;
3895 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003896 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003897 if (descr == NULL)
3898 continue;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003899 generic = p->function;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003900 if (descr->ob_type == &PyWrapperDescr_Type) {
3901 d = (PyWrapperDescrObject *)descr;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003902 if (d->d_base->wrapper == p->wrapper &&
Guido van Rossumcaf59042001-10-17 07:15:43 +00003903 PyType_IsSubtype(type, d->d_type))
3904 {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003905 if (specific == NULL ||
Guido van Rossumcaf59042001-10-17 07:15:43 +00003906 specific == d->d_wrapped)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003907 specific = d->d_wrapped;
3908 else
3909 use_generic = 1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003910 }
3911 }
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003912 else
3913 use_generic = 1;
3914 } while ((++p)->offset == offset);
3915 if (specific && !use_generic)
3916 *ptr = specific;
3917 else
3918 *ptr = generic;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003919 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003920}
Guido van Rossum705f0f52001-08-24 16:47:00 +00003921
Guido van Rossum6d204072001-10-21 00:44:31 +00003922/* This function is called by PyType_Ready() to populate the type's
3923 dictionary with method descriptors for function slots. For each
3924 function slot (like tp_repr) that's defined in the type, one or
3925 more corresponding descriptors are added in the type's tp_dict
3926 dictionary under the appropriate name (like __repr__). Some
3927 function slots cause more than one descriptor to be added (for
3928 example, the nb_add slot adds both __add__ and __radd__
3929 descriptors) and some function slots compete for the same
3930 descriptor (for example both sq_item and mp_subscript generate a
3931 __getitem__ descriptor). This only adds new descriptors and
3932 doesn't overwrite entries in tp_dict that were previously
3933 defined. The descriptors contain a reference to the C function
3934 they must call, so that it's safe if they are copied into a
3935 subtype's __dict__ and the subtype has a different C function in
3936 its slot -- calling the method defined by the descriptor will call
3937 the C function that was used to create it, rather than the C
3938 function present in the slot when it is called. (This is important
3939 because a subtype may have a C function in the slot that calls the
3940 method from the dictionary, and we want to avoid infinite recursion
3941 here.) */
3942
3943static int
3944add_operators(PyTypeObject *type)
3945{
3946 PyObject *dict = type->tp_dict;
3947 slotdef *p;
3948 PyObject *descr;
3949 void **ptr;
3950
3951 init_slotdefs();
3952 for (p = slotdefs; p->name; p++) {
3953 if (p->wrapper == NULL)
3954 continue;
3955 ptr = slotptr(type, p->offset);
3956 if (!ptr || !*ptr)
3957 continue;
3958 if (PyDict_GetItem(dict, p->name_strobj))
3959 continue;
3960 descr = PyDescr_NewWrapper(type, p, *ptr);
3961 if (descr == NULL)
3962 return -1;
3963 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
3964 return -1;
3965 Py_DECREF(descr);
3966 }
3967 if (type->tp_new != NULL) {
3968 if (add_tp_new_wrapper(type) < 0)
3969 return -1;
3970 }
3971 return 0;
3972}
3973
Guido van Rossum705f0f52001-08-24 16:47:00 +00003974
3975/* Cooperative 'super' */
3976
3977typedef struct {
3978 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00003979 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00003980 PyObject *obj;
3981} superobject;
3982
Guido van Rossum6f799372001-09-20 20:46:19 +00003983static PyMemberDef super_members[] = {
3984 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
3985 "the class invoking super()"},
3986 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
3987 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00003988 {0}
3989};
3990
Guido van Rossum705f0f52001-08-24 16:47:00 +00003991static void
3992super_dealloc(PyObject *self)
3993{
3994 superobject *su = (superobject *)self;
3995
Guido van Rossum048eb752001-10-02 21:24:57 +00003996 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00003997 Py_XDECREF(su->obj);
3998 Py_XDECREF(su->type);
3999 self->ob_type->tp_free(self);
4000}
4001
4002static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004003super_repr(PyObject *self)
4004{
4005 superobject *su = (superobject *)self;
4006
4007 if (su->obj)
4008 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004009 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004010 su->type ? su->type->tp_name : "NULL",
4011 su->obj->ob_type->tp_name);
4012 else
4013 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004014 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004015 su->type ? su->type->tp_name : "NULL");
4016}
4017
4018static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00004019super_getattro(PyObject *self, PyObject *name)
4020{
4021 superobject *su = (superobject *)self;
4022
4023 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00004024 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00004025 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004026 descrgetfunc f;
4027 int i, n;
4028
Guido van Rossum155db9a2002-04-02 17:53:47 +00004029 starttype = su->obj->ob_type;
4030 mro = starttype->tp_mro;
4031
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004032 if (mro == NULL)
4033 n = 0;
4034 else {
4035 assert(PyTuple_Check(mro));
4036 n = PyTuple_GET_SIZE(mro);
4037 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004038 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00004039 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00004040 break;
4041 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004042 if (i >= n && PyType_Check(su->obj)) {
Guido van Rossum155db9a2002-04-02 17:53:47 +00004043 starttype = (PyTypeObject *)(su->obj);
4044 mro = starttype->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004045 if (mro == NULL)
4046 n = 0;
4047 else {
4048 assert(PyTuple_Check(mro));
4049 n = PyTuple_GET_SIZE(mro);
4050 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004051 for (i = 0; i < n; i++) {
4052 if ((PyObject *)(su->type) ==
4053 PyTuple_GET_ITEM(mro, i))
4054 break;
4055 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004056 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004057 i++;
4058 res = NULL;
4059 for (; i < n; i++) {
4060 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00004061 if (PyType_Check(tmp))
4062 dict = ((PyTypeObject *)tmp)->tp_dict;
4063 else if (PyClass_Check(tmp))
4064 dict = ((PyClassObject *)tmp)->cl_dict;
4065 else
4066 continue;
4067 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00004068 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00004069 Py_INCREF(res);
4070 f = res->ob_type->tp_descr_get;
4071 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004072 tmp = f(res, su->obj,
4073 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004074 Py_DECREF(res);
4075 res = tmp;
4076 }
4077 return res;
4078 }
4079 }
4080 }
4081 return PyObject_GenericGetAttr(self, name);
4082}
4083
Guido van Rossum5b443c62001-12-03 15:38:28 +00004084static int
4085supercheck(PyTypeObject *type, PyObject *obj)
4086{
4087 if (!PyType_IsSubtype(obj->ob_type, type) &&
4088 !(PyType_Check(obj) &&
4089 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4090 PyErr_SetString(PyExc_TypeError,
4091 "super(type, obj): "
4092 "obj must be an instance or subtype of type");
4093 return -1;
4094 }
4095 else
4096 return 0;
4097}
4098
Guido van Rossum705f0f52001-08-24 16:47:00 +00004099static PyObject *
4100super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4101{
4102 superobject *su = (superobject *)self;
4103 superobject *new;
4104
4105 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4106 /* Not binding to an object, or already bound */
4107 Py_INCREF(self);
4108 return self;
4109 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004110 if (su->ob_type != &PySuper_Type)
4111 /* If su is an instance of a subclass of super,
4112 call its type */
4113 return PyObject_CallFunction((PyObject *)su->ob_type,
4114 "OO", su->type, obj);
4115 else {
4116 /* Inline the common case */
4117 if (supercheck(su->type, obj) < 0)
4118 return NULL;
4119 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4120 NULL, NULL);
4121 if (new == NULL)
4122 return NULL;
4123 Py_INCREF(su->type);
4124 Py_INCREF(obj);
4125 new->type = su->type;
4126 new->obj = obj;
4127 return (PyObject *)new;
4128 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004129}
4130
4131static int
4132super_init(PyObject *self, PyObject *args, PyObject *kwds)
4133{
4134 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004135 PyTypeObject *type;
4136 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004137
4138 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4139 return -1;
4140 if (obj == Py_None)
4141 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004142 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004143 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004144 Py_INCREF(type);
4145 Py_XINCREF(obj);
4146 su->type = type;
4147 su->obj = obj;
4148 return 0;
4149}
4150
4151static char super_doc[] =
4152"super(type) -> unbound super object\n"
4153"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004154"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004155"Typical use to call a cooperative superclass method:\n"
4156"class C(B):\n"
4157" def meth(self, arg):\n"
4158" super(C, self).meth(arg)";
4159
Guido van Rossum048eb752001-10-02 21:24:57 +00004160static int
4161super_traverse(PyObject *self, visitproc visit, void *arg)
4162{
4163 superobject *su = (superobject *)self;
4164 int err;
4165
4166#define VISIT(SLOT) \
4167 if (SLOT) { \
4168 err = visit((PyObject *)(SLOT), arg); \
4169 if (err) \
4170 return err; \
4171 }
4172
4173 VISIT(su->obj);
4174 VISIT(su->type);
4175
4176#undef VISIT
4177
4178 return 0;
4179}
4180
Guido van Rossum705f0f52001-08-24 16:47:00 +00004181PyTypeObject PySuper_Type = {
4182 PyObject_HEAD_INIT(&PyType_Type)
4183 0, /* ob_size */
4184 "super", /* tp_name */
4185 sizeof(superobject), /* tp_basicsize */
4186 0, /* tp_itemsize */
4187 /* methods */
4188 super_dealloc, /* tp_dealloc */
4189 0, /* tp_print */
4190 0, /* tp_getattr */
4191 0, /* tp_setattr */
4192 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004193 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004194 0, /* tp_as_number */
4195 0, /* tp_as_sequence */
4196 0, /* tp_as_mapping */
4197 0, /* tp_hash */
4198 0, /* tp_call */
4199 0, /* tp_str */
4200 super_getattro, /* tp_getattro */
4201 0, /* tp_setattro */
4202 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004203 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4204 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004205 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004206 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004207 0, /* tp_clear */
4208 0, /* tp_richcompare */
4209 0, /* tp_weaklistoffset */
4210 0, /* tp_iter */
4211 0, /* tp_iternext */
4212 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004213 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004214 0, /* tp_getset */
4215 0, /* tp_base */
4216 0, /* tp_dict */
4217 super_descr_get, /* tp_descr_get */
4218 0, /* tp_descr_set */
4219 0, /* tp_dictoffset */
4220 super_init, /* tp_init */
4221 PyType_GenericAlloc, /* tp_alloc */
4222 PyType_GenericNew, /* tp_new */
Guido van Rossum048eb752001-10-02 21:24:57 +00004223 _PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004224};