blob: dd6f1b5d5cfa1e9c88704b6a9d290090ab695a58 [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;
Guido van Rossum6ca7d412002-04-18 00:22:00 +000085 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +000086 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +000087 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +000088 if (result == NULL) {
89 result = Py_None;
90 Py_INCREF(result);
91 }
92 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +000093 result = result->ob_type->tp_descr_get(result, NULL,
94 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +000095 }
96 else {
97 Py_INCREF(result);
98 }
Tim Peters24008312002-03-17 18:56:20 +000099 return result;
100}
101
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000102static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +0000103 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000104 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000105 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000106 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000107 {0}
108};
109
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000110static int
111type_compare(PyObject *v, PyObject *w)
112{
113 /* This is called with type objects only. So we
114 can just compare the addresses. */
115 Py_uintptr_t vv = (Py_uintptr_t)v;
116 Py_uintptr_t ww = (Py_uintptr_t)w;
117 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
118}
119
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000120static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000121type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000122{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000123 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000124 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000125
126 mod = type_module(type, NULL);
127 if (mod == NULL)
128 PyErr_Clear();
129 else if (!PyString_Check(mod)) {
130 Py_DECREF(mod);
131 mod = NULL;
132 }
133 name = type_name(type, NULL);
134 if (name == NULL)
135 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000136
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000137 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
138 kind = "class";
139 else
140 kind = "type";
141
Barry Warsaw7ce36942001-08-24 18:34:26 +0000142 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000143 rtn = PyString_FromFormat("<%s '%s.%s'>",
144 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000145 PyString_AS_STRING(mod),
146 PyString_AS_STRING(name));
147 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000148 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000149 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000150
Guido van Rossumc3542212001-08-16 09:18:56 +0000151 Py_XDECREF(mod);
152 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000153 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154}
155
Tim Peters6d6c1a32001-08-02 04:15:00 +0000156static PyObject *
157type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
158{
159 PyObject *obj;
160
161 if (type->tp_new == NULL) {
162 PyErr_Format(PyExc_TypeError,
163 "cannot create '%.100s' instances",
164 type->tp_name);
165 return NULL;
166 }
167
Tim Peters3f996e72001-09-13 19:18:27 +0000168 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000169 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000170 /* Ugly exception: when the call was type(something),
171 don't call tp_init on the result. */
172 if (type == &PyType_Type &&
173 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
174 (kwds == NULL ||
175 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
176 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000177 /* If the returned object is not an instance of type,
178 it won't be initialized. */
179 if (!PyType_IsSubtype(obj->ob_type, type))
180 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000181 type = obj->ob_type;
182 if (type->tp_init != NULL &&
183 type->tp_init(obj, args, kwds) < 0) {
184 Py_DECREF(obj);
185 obj = NULL;
186 }
187 }
188 return obj;
189}
190
191PyObject *
192PyType_GenericAlloc(PyTypeObject *type, int nitems)
193{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000194 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000195 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000196
197 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000198 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000199 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000200 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000201
Neil Schemenauerc806c882001-08-29 23:54:54 +0000202 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000203 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000204
Neil Schemenauerc806c882001-08-29 23:54:54 +0000205 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000206
Tim Peters6d6c1a32001-08-02 04:15:00 +0000207 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
208 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000209
Tim Peters6d6c1a32001-08-02 04:15:00 +0000210 if (type->tp_itemsize == 0)
211 PyObject_INIT(obj, type);
212 else
213 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000214
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000216 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000217 return obj;
218}
219
220PyObject *
221PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
222{
223 return type->tp_alloc(type, 0);
224}
225
Guido van Rossum9475a232001-10-05 20:51:39 +0000226/* Helpers for subtyping */
227
228static int
229subtype_traverse(PyObject *self, visitproc visit, void *arg)
230{
231 PyTypeObject *type, *base;
232 traverseproc f;
233 int err;
234
235 /* Find the nearest base with a different tp_traverse */
236 type = self->ob_type;
237 base = type->tp_base;
238 while ((f = base->tp_traverse) == subtype_traverse) {
239 base = base->tp_base;
240 assert(base);
241 }
242
243 if (type->tp_dictoffset != base->tp_dictoffset) {
244 PyObject **dictptr = _PyObject_GetDictPtr(self);
245 if (dictptr && *dictptr) {
246 err = visit(*dictptr, arg);
247 if (err)
248 return err;
249 }
250 }
251
252 if (f)
253 return f(self, visit, arg);
254 return 0;
255}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000256
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000257staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
258
259static int
260call_finalizer(PyObject *self)
261{
262 static PyObject *del_str = NULL;
263 PyObject *del, *res;
264 PyObject *error_type, *error_value, *error_traceback;
265
266 /* Temporarily resurrect the object. */
267#ifdef Py_TRACE_REFS
268#ifndef Py_REF_DEBUG
269# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
270#endif
271 /* much too complicated if Py_TRACE_REFS defined */
272 _Py_NewReference((PyObject *)self);
273#ifdef COUNT_ALLOCS
274 /* compensate for boost in _Py_NewReference; note that
275 * _Py_RefTotal was also boosted; we'll knock that down later.
276 */
277 self->ob_type->tp_allocs--;
278#endif
279#else /* !Py_TRACE_REFS */
280 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
281 Py_INCREF(self);
282#endif /* !Py_TRACE_REFS */
283
284 /* Save the current exception, if any. */
285 PyErr_Fetch(&error_type, &error_value, &error_traceback);
286
287 /* Execute __del__ method, if any. */
288 del = lookup_maybe(self, "__del__", &del_str);
289 if (del != NULL) {
290 res = PyEval_CallObject(del, NULL);
291 if (res == NULL)
292 PyErr_WriteUnraisable(del);
293 else
294 Py_DECREF(res);
295 Py_DECREF(del);
296 }
297
298 /* Restore the saved exception. */
299 PyErr_Restore(error_type, error_value, error_traceback);
300
301 /* Undo the temporary resurrection; can't use DECREF here, it would
302 * cause a recursive call.
303 */
304#ifdef Py_REF_DEBUG
305 /* _Py_RefTotal was boosted either by _Py_NewReference or
306 * Py_INCREF above.
307 */
308 _Py_RefTotal--;
309#endif
310 if (--self->ob_refcnt > 0) {
311#ifdef COUNT_ALLOCS
312 self->ob_type->tp_frees--;
313#endif
314 _PyObject_GC_TRACK(self);
315 return -1; /* __del__ added a reference; don't delete now */
316 }
317#ifdef Py_TRACE_REFS
318 _Py_ForgetReference((PyObject *)self);
319#ifdef COUNT_ALLOCS
320 /* compensate for increment in _Py_ForgetReference */
321 self->ob_type->tp_frees--;
322#endif
323#endif
324
325 return 0;
326}
327
Tim Peters6d6c1a32001-08-02 04:15:00 +0000328static void
329subtype_dealloc(PyObject *self)
330{
Guido van Rossum14227b42001-12-06 02:35:58 +0000331 PyTypeObject *type, *base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000332 destructor f;
333
334 /* This exists so we can DECREF self->ob_type */
335
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000336 if (call_finalizer(self) < 0)
337 return;
338
Tim Peters6d6c1a32001-08-02 04:15:00 +0000339 /* Find the nearest base with a different tp_dealloc */
340 type = self->ob_type;
Guido van Rossum14227b42001-12-06 02:35:58 +0000341 base = type->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000342 while ((f = base->tp_dealloc) == subtype_dealloc) {
343 base = base->tp_base;
344 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000345 }
346
347 /* Clear __slots__ variables */
348 if (type->tp_basicsize != base->tp_basicsize &&
349 type->tp_itemsize == 0)
350 {
351 char *addr = ((char *)self);
352 char *p = addr + base->tp_basicsize;
353 char *q = addr + type->tp_basicsize;
354 for (; p < q; p += sizeof(PyObject *)) {
355 PyObject **pp;
356 if (p == addr + type->tp_dictoffset ||
357 p == addr + type->tp_weaklistoffset)
358 continue;
359 pp = (PyObject **)p;
360 if (*pp != NULL) {
361 Py_DECREF(*pp);
362 *pp = NULL;
Guido van Rossum33bab012001-12-05 22:45:48 +0000363 }
364 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000365 }
366
367 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000368 if (type->tp_dictoffset && !base->tp_dictoffset) {
369 PyObject **dictptr = _PyObject_GetDictPtr(self);
370 if (dictptr != NULL) {
371 PyObject *dict = *dictptr;
372 if (dict != NULL) {
373 Py_DECREF(dict);
374 *dictptr = NULL;
375 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000376 }
377 }
378
Guido van Rossum9676b222001-08-17 20:32:36 +0000379 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000380 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000381 PyObject_ClearWeakRefs(self);
382
Tim Peters6d6c1a32001-08-02 04:15:00 +0000383 /* Finalize GC if the base doesn't do GC and we do */
384 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000385 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000386
387 /* Call the base tp_dealloc() */
388 assert(f);
389 f(self);
390
391 /* Can't reference self beyond this point */
392 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
393 Py_DECREF(type);
394 }
395}
396
Tim Peters6d6c1a32001-08-02 04:15:00 +0000397staticforward PyTypeObject *solid_base(PyTypeObject *type);
398
399typedef struct {
400 PyTypeObject type;
401 PyNumberMethods as_number;
402 PySequenceMethods as_sequence;
403 PyMappingMethods as_mapping;
404 PyBufferProcs as_buffer;
405 PyObject *name, *slots;
Guido van Rossum6f799372001-09-20 20:46:19 +0000406 PyMemberDef members[1];
Tim Peters6d6c1a32001-08-02 04:15:00 +0000407} etype;
408
409/* type test with subclassing support */
410
411int
412PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
413{
414 PyObject *mro;
415
Guido van Rossum9478d072001-09-07 18:52:13 +0000416 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
417 return b == a || b == &PyBaseObject_Type;
418
Tim Peters6d6c1a32001-08-02 04:15:00 +0000419 mro = a->tp_mro;
420 if (mro != NULL) {
421 /* Deal with multiple inheritance without recursion
422 by walking the MRO tuple */
423 int i, n;
424 assert(PyTuple_Check(mro));
425 n = PyTuple_GET_SIZE(mro);
426 for (i = 0; i < n; i++) {
427 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
428 return 1;
429 }
430 return 0;
431 }
432 else {
433 /* a is not completely initilized yet; follow tp_base */
434 do {
435 if (a == b)
436 return 1;
437 a = a->tp_base;
438 } while (a != NULL);
439 return b == &PyBaseObject_Type;
440 }
441}
442
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000443/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000444 without looking in the instance dictionary
445 (so we can't use PyObject_GetAttr) but still binding
446 it to the instance. The arguments are the object,
447 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000448 static variable used to cache the interned Python string.
449
450 Two variants:
451
452 - lookup_maybe() returns NULL without raising an exception
453 when the _PyType_Lookup() call fails;
454
455 - lookup_method() always raises an exception upon errors.
456*/
Guido van Rossum60718732001-08-28 17:47:51 +0000457
458static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000459lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000460{
461 PyObject *res;
462
463 if (*attrobj == NULL) {
464 *attrobj = PyString_InternFromString(attrstr);
465 if (*attrobj == NULL)
466 return NULL;
467 }
468 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000469 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000470 descrgetfunc f;
471 if ((f = res->ob_type->tp_descr_get) == NULL)
472 Py_INCREF(res);
473 else
474 res = f(res, self, (PyObject *)(self->ob_type));
475 }
476 return res;
477}
478
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000479static PyObject *
480lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
481{
482 PyObject *res = lookup_maybe(self, attrstr, attrobj);
483 if (res == NULL && !PyErr_Occurred())
484 PyErr_SetObject(PyExc_AttributeError, *attrobj);
485 return res;
486}
487
Guido van Rossum2730b132001-08-28 18:22:14 +0000488/* A variation of PyObject_CallMethod that uses lookup_method()
489 instead of PyObject_GetAttrString(). This uses the same convention
490 as lookup_method to cache the interned name string object. */
491
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000492static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000493call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
494{
495 va_list va;
496 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000497 va_start(va, format);
498
Guido van Rossumda21c012001-10-03 00:50:18 +0000499 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000500 if (func == NULL) {
501 va_end(va);
502 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000503 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000504 return NULL;
505 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000506
507 if (format && *format)
508 args = Py_VaBuildValue(format, va);
509 else
510 args = PyTuple_New(0);
511
512 va_end(va);
513
514 if (args == NULL)
515 return NULL;
516
517 assert(PyTuple_Check(args));
518 retval = PyObject_Call(func, args, NULL);
519
520 Py_DECREF(args);
521 Py_DECREF(func);
522
523 return retval;
524}
525
526/* Clone of call_method() that returns NotImplemented when the lookup fails. */
527
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000528static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000529call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
530{
531 va_list va;
532 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000533 va_start(va, format);
534
Guido van Rossumda21c012001-10-03 00:50:18 +0000535 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000536 if (func == NULL) {
537 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000538 if (!PyErr_Occurred()) {
539 Py_INCREF(Py_NotImplemented);
540 return Py_NotImplemented;
541 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000542 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000543 }
544
545 if (format && *format)
546 args = Py_VaBuildValue(format, va);
547 else
548 args = PyTuple_New(0);
549
550 va_end(va);
551
Guido van Rossum717ce002001-09-14 16:58:08 +0000552 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000553 return NULL;
554
Guido van Rossum717ce002001-09-14 16:58:08 +0000555 assert(PyTuple_Check(args));
556 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000557
558 Py_DECREF(args);
559 Py_DECREF(func);
560
561 return retval;
562}
563
Tim Peters6d6c1a32001-08-02 04:15:00 +0000564/* Method resolution order algorithm from "Putting Metaclasses to Work"
565 by Forman and Danforth (Addison-Wesley 1999). */
566
567static int
568conservative_merge(PyObject *left, PyObject *right)
569{
570 int left_size;
571 int right_size;
572 int i, j, r, ok;
573 PyObject *temp, *rr;
574
575 assert(PyList_Check(left));
576 assert(PyList_Check(right));
577
578 again:
579 left_size = PyList_GET_SIZE(left);
580 right_size = PyList_GET_SIZE(right);
581 for (i = 0; i < left_size; i++) {
582 for (j = 0; j < right_size; j++) {
583 if (PyList_GET_ITEM(left, i) ==
584 PyList_GET_ITEM(right, j)) {
585 /* found a merge point */
586 temp = PyList_New(0);
587 if (temp == NULL)
588 return -1;
589 for (r = 0; r < j; r++) {
590 rr = PyList_GET_ITEM(right, r);
591 ok = PySequence_Contains(left, rr);
592 if (ok < 0) {
593 Py_DECREF(temp);
594 return -1;
595 }
596 if (!ok) {
597 ok = PyList_Append(temp, rr);
598 if (ok < 0) {
599 Py_DECREF(temp);
600 return -1;
601 }
602 }
603 }
604 ok = PyList_SetSlice(left, i, i, temp);
605 Py_DECREF(temp);
606 if (ok < 0)
607 return -1;
608 ok = PyList_SetSlice(right, 0, j+1, NULL);
609 if (ok < 0)
610 return -1;
611 goto again;
612 }
613 }
614 }
615 return PyList_SetSlice(left, left_size, left_size, right);
616}
617
618static int
619serious_order_disagreements(PyObject *left, PyObject *right)
620{
621 return 0; /* XXX later -- for now, we cheat: "don't do that" */
622}
623
Tim Petersa91e9642001-11-14 23:32:33 +0000624static int
625fill_classic_mro(PyObject *mro, PyObject *cls)
626{
627 PyObject *bases, *base;
628 int i, n;
629
630 assert(PyList_Check(mro));
631 assert(PyClass_Check(cls));
632 i = PySequence_Contains(mro, cls);
633 if (i < 0)
634 return -1;
635 if (!i) {
636 if (PyList_Append(mro, cls) < 0)
637 return -1;
638 }
639 bases = ((PyClassObject *)cls)->cl_bases;
640 assert(bases && PyTuple_Check(bases));
641 n = PyTuple_GET_SIZE(bases);
642 for (i = 0; i < n; i++) {
643 base = PyTuple_GET_ITEM(bases, i);
644 if (fill_classic_mro(mro, base) < 0)
645 return -1;
646 }
647 return 0;
648}
649
650static PyObject *
651classic_mro(PyObject *cls)
652{
653 PyObject *mro;
654
655 assert(PyClass_Check(cls));
656 mro = PyList_New(0);
657 if (mro != NULL) {
658 if (fill_classic_mro(mro, cls) == 0)
659 return mro;
660 Py_DECREF(mro);
661 }
662 return NULL;
663}
664
Tim Peters6d6c1a32001-08-02 04:15:00 +0000665static PyObject *
666mro_implementation(PyTypeObject *type)
667{
668 int i, n, ok;
669 PyObject *bases, *result;
670
671 bases = type->tp_bases;
672 n = PyTuple_GET_SIZE(bases);
673 result = Py_BuildValue("[O]", (PyObject *)type);
674 if (result == NULL)
675 return NULL;
676 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000677 PyObject *base = PyTuple_GET_ITEM(bases, i);
678 PyObject *parentMRO;
679 if (PyType_Check(base))
680 parentMRO = PySequence_List(
681 ((PyTypeObject*)base)->tp_mro);
682 else
683 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000684 if (parentMRO == NULL) {
685 Py_DECREF(result);
686 return NULL;
687 }
688 if (serious_order_disagreements(result, parentMRO)) {
689 Py_DECREF(result);
690 return NULL;
691 }
692 ok = conservative_merge(result, parentMRO);
693 Py_DECREF(parentMRO);
694 if (ok < 0) {
695 Py_DECREF(result);
696 return NULL;
697 }
698 }
699 return result;
700}
701
702static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000703mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000704{
705 PyTypeObject *type = (PyTypeObject *)self;
706
Tim Peters6d6c1a32001-08-02 04:15:00 +0000707 return mro_implementation(type);
708}
709
710static int
711mro_internal(PyTypeObject *type)
712{
713 PyObject *mro, *result, *tuple;
714
715 if (type->ob_type == &PyType_Type) {
716 result = mro_implementation(type);
717 }
718 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000719 static PyObject *mro_str;
720 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000721 if (mro == NULL)
722 return -1;
723 result = PyObject_CallObject(mro, NULL);
724 Py_DECREF(mro);
725 }
726 if (result == NULL)
727 return -1;
728 tuple = PySequence_Tuple(result);
729 Py_DECREF(result);
730 type->tp_mro = tuple;
731 return 0;
732}
733
734
735/* Calculate the best base amongst multiple base classes.
736 This is the first one that's on the path to the "solid base". */
737
738static PyTypeObject *
739best_base(PyObject *bases)
740{
741 int i, n;
742 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000743 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744
745 assert(PyTuple_Check(bases));
746 n = PyTuple_GET_SIZE(bases);
747 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000748 base = NULL;
749 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000750 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000751 base_proto = PyTuple_GET_ITEM(bases, i);
752 if (PyClass_Check(base_proto))
753 continue;
754 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000755 PyErr_SetString(
756 PyExc_TypeError,
757 "bases must be types");
758 return NULL;
759 }
Tim Petersa91e9642001-11-14 23:32:33 +0000760 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000761 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000762 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000763 return NULL;
764 }
765 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000766 if (winner == NULL) {
767 winner = candidate;
768 base = base_i;
769 }
770 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000771 ;
772 else if (PyType_IsSubtype(candidate, winner)) {
773 winner = candidate;
774 base = base_i;
775 }
776 else {
777 PyErr_SetString(
778 PyExc_TypeError,
779 "multiple bases have "
780 "instance lay-out conflict");
781 return NULL;
782 }
783 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000784 if (base == NULL)
785 PyErr_SetString(PyExc_TypeError,
786 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787 return base;
788}
789
790static int
791extra_ivars(PyTypeObject *type, PyTypeObject *base)
792{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000793 size_t t_size = type->tp_basicsize;
794 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795
Guido van Rossum9676b222001-08-17 20:32:36 +0000796 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000797 if (type->tp_itemsize || base->tp_itemsize) {
798 /* If itemsize is involved, stricter rules */
799 return t_size != b_size ||
800 type->tp_itemsize != base->tp_itemsize;
801 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000802 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
803 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
804 t_size -= sizeof(PyObject *);
805 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
806 type->tp_dictoffset + sizeof(PyObject *) == t_size)
807 t_size -= sizeof(PyObject *);
808
809 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000810}
811
812static PyTypeObject *
813solid_base(PyTypeObject *type)
814{
815 PyTypeObject *base;
816
817 if (type->tp_base)
818 base = solid_base(type->tp_base);
819 else
820 base = &PyBaseObject_Type;
821 if (extra_ivars(type, base))
822 return type;
823 else
824 return base;
825}
826
827staticforward void object_dealloc(PyObject *);
828staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000829staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000830staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000831
832static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000833subtype_dict(PyObject *obj, void *context)
834{
835 PyObject **dictptr = _PyObject_GetDictPtr(obj);
836 PyObject *dict;
837
838 if (dictptr == NULL) {
839 PyErr_SetString(PyExc_AttributeError,
840 "This object has no __dict__");
841 return NULL;
842 }
843 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000844 if (dict == NULL)
845 *dictptr = dict = PyDict_New();
846 Py_XINCREF(dict);
847 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000848}
849
Guido van Rossum6661be32001-10-26 04:26:12 +0000850static int
851subtype_setdict(PyObject *obj, PyObject *value, void *context)
852{
853 PyObject **dictptr = _PyObject_GetDictPtr(obj);
854 PyObject *dict;
855
856 if (dictptr == NULL) {
857 PyErr_SetString(PyExc_AttributeError,
858 "This object has no __dict__");
859 return -1;
860 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000861 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000862 PyErr_SetString(PyExc_TypeError,
863 "__dict__ must be set to a dictionary");
864 return -1;
865 }
866 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000867 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000868 *dictptr = value;
869 Py_XDECREF(dict);
870 return 0;
871}
872
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000873static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000874 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000875 {0},
876};
877
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000878/* bozo: __getstate__ that raises TypeError */
879
880static PyObject *
881bozo_func(PyObject *self, PyObject *args)
882{
883 PyErr_SetString(PyExc_TypeError,
884 "a class that defines __slots__ without "
885 "defining __getstate__ cannot be pickled");
886 return NULL;
887}
888
Neal Norwitz93c1e232002-03-31 16:06:11 +0000889static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS};
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000890
891static PyObject *bozo_obj = NULL;
892
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000893static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
895{
896 PyObject *name, *bases, *dict;
897 static char *kwlist[] = {"name", "bases", "dict", 0};
898 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000899 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000900 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000901 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000902 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000903
Tim Peters3abca122001-10-27 19:37:48 +0000904 assert(args != NULL && PyTuple_Check(args));
905 assert(kwds == NULL || PyDict_Check(kwds));
906
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000907 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000908 {
909 const int nargs = PyTuple_GET_SIZE(args);
910 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
911
912 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
913 PyObject *x = PyTuple_GET_ITEM(args, 0);
914 Py_INCREF(x->ob_type);
915 return (PyObject *) x->ob_type;
916 }
917
918 /* SF bug 475327 -- if that didn't trigger, we need 3
919 arguments. but PyArg_ParseTupleAndKeywords below may give
920 a msg saying type() needs exactly 3. */
921 if (nargs + nkwds != 3) {
922 PyErr_SetString(PyExc_TypeError,
923 "type() takes 1 or 3 arguments");
924 return NULL;
925 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000926 }
927
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000928 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000929 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
930 &name,
931 &PyTuple_Type, &bases,
932 &PyDict_Type, &dict))
933 return NULL;
934
935 /* Determine the proper metatype to deal with this,
936 and check for metatype conflicts while we're at it.
937 Note that if some other metatype wins to contract,
938 it's possible that its instances are not types. */
939 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000940 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000941 for (i = 0; i < nbases; i++) {
942 tmp = PyTuple_GET_ITEM(bases, i);
943 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +0000944 if (tmptype == &PyClass_Type)
945 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000946 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000947 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000948 if (PyType_IsSubtype(tmptype, winner)) {
949 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000950 continue;
951 }
952 PyErr_SetString(PyExc_TypeError,
953 "metatype conflict among bases");
954 return NULL;
955 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000956 if (winner != metatype) {
957 if (winner->tp_new != type_new) /* Pass it to the winner */
958 return winner->tp_new(winner, args, kwds);
959 metatype = winner;
960 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000961
962 /* Adjust for empty tuple bases */
963 if (nbases == 0) {
964 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
965 if (bases == NULL)
966 return NULL;
967 nbases = 1;
968 }
969 else
970 Py_INCREF(bases);
971
972 /* XXX From here until type is allocated, "return NULL" leaks bases! */
973
974 /* Calculate best base, and check that all bases are type objects */
975 base = best_base(bases);
976 if (base == NULL)
977 return NULL;
978 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
979 PyErr_Format(PyExc_TypeError,
980 "type '%.100s' is not an acceptable base type",
981 base->tp_name);
982 return NULL;
983 }
984
Tim Peters6d6c1a32001-08-02 04:15:00 +0000985 /* Check for a __slots__ sequence variable in dict, and count it */
986 slots = PyDict_GetItemString(dict, "__slots__");
987 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +0000988 add_dict = 0;
989 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 if (slots != NULL) {
991 /* Make it into a tuple */
992 if (PyString_Check(slots))
993 slots = Py_BuildValue("(O)", slots);
994 else
995 slots = PySequence_Tuple(slots);
996 if (slots == NULL)
997 return NULL;
998 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +0000999 if (nslots > 0 && base->tp_itemsize != 0) {
1000 PyErr_Format(PyExc_TypeError,
1001 "nonempty __slots__ "
1002 "not supported for subtype of '%s'",
1003 base->tp_name);
1004 return NULL;
1005 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001006 for (i = 0; i < nslots; i++) {
1007 if (!PyString_Check(PyTuple_GET_ITEM(slots, i))) {
1008 PyErr_SetString(PyExc_TypeError,
1009 "__slots__ must be a sequence of strings");
1010 Py_DECREF(slots);
1011 return NULL;
1012 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001013 /* XXX Check against null bytes in name */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001014 }
1015 }
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001016 if (slots != NULL) {
1017 /* See if *this* class defines __getstate__ */
1018 PyObject *getstate = PyDict_GetItemString(dict,
1019 "__getstate__");
1020 if (getstate == NULL) {
1021 /* If not, provide a bozo that raises TypeError */
1022 if (bozo_obj == NULL) {
1023 bozo_obj = PyCFunction_New(&bozo_ml, NULL);
1024 if (bozo_obj == NULL) {
1025 /* XXX decref various things */
1026 return NULL;
1027 }
1028 }
1029 if (PyDict_SetItemString(dict,
1030 "__getstate__",
1031 bozo_obj) < 0) {
1032 /* XXX decref various things */
1033 return NULL;
1034 }
1035 }
1036 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 if (slots == NULL && base->tp_dictoffset == 0 &&
1038 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +00001039 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001040 add_dict++;
1041 }
Guido van Rossumc4141872001-08-30 04:43:35 +00001042 if (slots == NULL && base->tp_weaklistoffset == 0 &&
1043 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001044 nslots++;
1045 add_weak++;
1046 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001047
1048 /* XXX From here until type is safely allocated,
1049 "return NULL" may leak slots! */
1050
1051 /* Allocate the type object */
1052 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1053 if (type == NULL)
1054 return NULL;
1055
1056 /* Keep name and slots alive in the extended type object */
1057 et = (etype *)type;
1058 Py_INCREF(name);
1059 et->name = name;
1060 et->slots = slots;
1061
Guido van Rossumdc91b992001-08-08 22:26:22 +00001062 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001063 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1064 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001065 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1066 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001067
1068 /* It's a new-style number unless it specifically inherits any
1069 old-style numeric behavior */
1070 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1071 (base->tp_as_number == NULL))
1072 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1073
1074 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075 type->tp_as_number = &et->as_number;
1076 type->tp_as_sequence = &et->as_sequence;
1077 type->tp_as_mapping = &et->as_mapping;
1078 type->tp_as_buffer = &et->as_buffer;
1079 type->tp_name = PyString_AS_STRING(name);
1080
1081 /* Set tp_base and tp_bases */
1082 type->tp_bases = bases;
1083 Py_INCREF(base);
1084 type->tp_base = base;
1085
Guido van Rossum687ae002001-10-15 22:03:32 +00001086 /* Initialize tp_dict from passed-in dict */
1087 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001088 if (dict == NULL) {
1089 Py_DECREF(type);
1090 return NULL;
1091 }
1092
Guido van Rossumc3542212001-08-16 09:18:56 +00001093 /* Set __module__ in the dict */
1094 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1095 tmp = PyEval_GetGlobals();
1096 if (tmp != NULL) {
1097 tmp = PyDict_GetItemString(tmp, "__name__");
1098 if (tmp != NULL) {
1099 if (PyDict_SetItemString(dict, "__module__",
1100 tmp) < 0)
1101 return NULL;
1102 }
1103 }
1104 }
1105
Tim Peters2f93e282001-10-04 05:27:00 +00001106 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001107 and is a string. The __doc__ accessor will first look for tp_doc;
1108 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001109 */
1110 {
1111 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1112 if (doc != NULL && PyString_Check(doc)) {
1113 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001114 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001115 if (type->tp_doc == NULL) {
1116 Py_DECREF(type);
1117 return NULL;
1118 }
1119 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1120 }
1121 }
1122
Tim Peters6d6c1a32001-08-02 04:15:00 +00001123 /* Special-case __new__: if it's a plain function,
1124 make it a static function */
1125 tmp = PyDict_GetItemString(dict, "__new__");
1126 if (tmp != NULL && PyFunction_Check(tmp)) {
1127 tmp = PyStaticMethod_New(tmp);
1128 if (tmp == NULL) {
1129 Py_DECREF(type);
1130 return NULL;
1131 }
1132 PyDict_SetItemString(dict, "__new__", tmp);
1133 Py_DECREF(tmp);
1134 }
1135
1136 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1137 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001138 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001139 if (slots != NULL) {
1140 for (i = 0; i < nslots; i++, mp++) {
1141 mp->name = PyString_AS_STRING(
1142 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001143 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001144 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001145 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001146 strcmp(mp->name, "__weakref__") == 0) {
1147 mp->type = T_OBJECT;
Guido van Rossum9676b222001-08-17 20:32:36 +00001148 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001149 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001150 slotoffset += sizeof(PyObject *);
1151 }
1152 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001153 else {
1154 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001155 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +00001156 type->tp_dictoffset =
1157 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001158 else
1159 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001160 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001161 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +00001162 }
1163 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001164 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +00001165 type->tp_weaklistoffset = slotoffset;
1166 mp->name = "__weakref__";
1167 mp->type = T_OBJECT;
1168 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +00001169 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001170 mp++;
1171 slotoffset += sizeof(PyObject *);
1172 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001173 }
1174 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001175 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001176 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001177
1178 /* Special case some slots */
1179 if (type->tp_dictoffset != 0 || nslots > 0) {
1180 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1181 type->tp_getattro = PyObject_GenericGetAttr;
1182 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1183 type->tp_setattro = PyObject_GenericSetAttr;
1184 }
1185 type->tp_dealloc = subtype_dealloc;
1186
Guido van Rossum9475a232001-10-05 20:51:39 +00001187 /* Enable GC unless there are really no instance variables possible */
1188 if (!(type->tp_basicsize == sizeof(PyObject) &&
1189 type->tp_itemsize == 0))
1190 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1191
Tim Peters6d6c1a32001-08-02 04:15:00 +00001192 /* Always override allocation strategy to use regular heap */
1193 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001194 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001195 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001196 type->tp_traverse = subtype_traverse;
Guido van Rossum048eb752001-10-02 21:24:57 +00001197 type->tp_clear = base->tp_clear;
1198 }
1199 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001200 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001201
1202 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001203 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001204 Py_DECREF(type);
1205 return NULL;
1206 }
1207
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001208 /* Put the proper slots in place */
1209 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001210
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211 return (PyObject *)type;
1212}
1213
1214/* Internal API to look for a name through the MRO.
1215 This returns a borrowed reference, and doesn't set an exception! */
1216PyObject *
1217_PyType_Lookup(PyTypeObject *type, PyObject *name)
1218{
1219 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001220 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001221
Guido van Rossum687ae002001-10-15 22:03:32 +00001222 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001223 mro = type->tp_mro;
Guido van Rossum9fc8a292002-05-24 21:40:08 +00001224 if (mro == NULL) {
Guido van Rossumb65c65b2002-06-03 19:52:41 +00001225 if (PyType_Ready(type) < 0) {
1226 /* It's not ideal to clear the error condition,
1227 but this function is documented as not setting
1228 an exception, and I don't want to change that.
1229 When PyType_Ready() can't proceed, it won't
1230 set the "ready" flag, so future attempts to ready
1231 the same type will call it again -- hopefully
1232 in a context that propagates the exception out.
1233 */
1234 PyErr_Clear();
Guido van Rossum9fc8a292002-05-24 21:40:08 +00001235 return NULL;
Guido van Rossumb65c65b2002-06-03 19:52:41 +00001236 }
Guido van Rossum9fc8a292002-05-24 21:40:08 +00001237 mro = type->tp_mro;
1238 assert(mro != NULL);
1239 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001240 assert(PyTuple_Check(mro));
1241 n = PyTuple_GET_SIZE(mro);
1242 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001243 base = PyTuple_GET_ITEM(mro, i);
1244 if (PyClass_Check(base))
1245 dict = ((PyClassObject *)base)->cl_dict;
1246 else {
1247 assert(PyType_Check(base));
1248 dict = ((PyTypeObject *)base)->tp_dict;
1249 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001250 assert(dict && PyDict_Check(dict));
1251 res = PyDict_GetItem(dict, name);
1252 if (res != NULL)
1253 return res;
1254 }
1255 return NULL;
1256}
1257
1258/* This is similar to PyObject_GenericGetAttr(),
1259 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1260static PyObject *
1261type_getattro(PyTypeObject *type, PyObject *name)
1262{
1263 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001264 PyObject *meta_attribute, *attribute;
1265 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001266
1267 /* Initialize this type (we'll assume the metatype is initialized) */
1268 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001269 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001270 return NULL;
1271 }
1272
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001273 /* No readable descriptor found yet */
1274 meta_get = NULL;
1275
1276 /* Look for the attribute in the metatype */
1277 meta_attribute = _PyType_Lookup(metatype, name);
1278
1279 if (meta_attribute != NULL) {
1280 meta_get = meta_attribute->ob_type->tp_descr_get;
1281
1282 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
1283 /* Data descriptors implement tp_descr_set to intercept
1284 * writes. Assume the attribute is not overridden in
1285 * type's tp_dict (and bases): call the descriptor now.
1286 */
1287 return meta_get(meta_attribute, (PyObject *)type,
1288 (PyObject *)metatype);
1289 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001290 }
1291
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001292 /* No data descriptor found on metatype. Look in tp_dict of this
1293 * type and its bases */
1294 attribute = _PyType_Lookup(type, name);
1295 if (attribute != NULL) {
1296 /* Implement descriptor functionality, if any */
1297 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
1298 if (local_get != NULL) {
1299 /* NULL 2nd argument indicates the descriptor was
1300 * found on the target object itself (or a base) */
1301 return local_get(attribute, (PyObject *)NULL,
1302 (PyObject *)type);
1303 }
1304
1305 Py_INCREF(attribute);
1306 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001307 }
1308
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001309 /* No attribute found in local __dict__ (or bases): use the
1310 * descriptor from the metatype, if any */
1311 if (meta_get != NULL)
1312 return meta_get(meta_attribute, (PyObject *)type,
1313 (PyObject *)metatype);
1314
1315 /* If an ordinary attribute was found on the metatype, return it now */
1316 if (meta_attribute != NULL) {
1317 Py_INCREF(meta_attribute);
1318 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001319 }
1320
1321 /* Give up */
1322 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001323 "type object '%.50s' has no attribute '%.400s'",
1324 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 return NULL;
1326}
1327
1328static int
1329type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1330{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001331 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1332 PyErr_Format(
1333 PyExc_TypeError,
1334 "can't set attributes of built-in/extension type '%s'",
1335 type->tp_name);
1336 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001337 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001338 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1339 return -1;
1340 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001341}
1342
1343static void
1344type_dealloc(PyTypeObject *type)
1345{
1346 etype *et;
1347
1348 /* Assert this is a heap-allocated type object */
1349 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001350 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001351 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001352 et = (etype *)type;
1353 Py_XDECREF(type->tp_base);
1354 Py_XDECREF(type->tp_dict);
1355 Py_XDECREF(type->tp_bases);
1356 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001357 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001358 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001359 Py_XDECREF(et->name);
1360 Py_XDECREF(et->slots);
1361 type->ob_type->tp_free((PyObject *)type);
1362}
1363
Guido van Rossum1c450732001-10-08 15:18:27 +00001364static PyObject *
1365type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1366{
1367 PyObject *list, *raw, *ref;
1368 int i, n;
1369
1370 list = PyList_New(0);
1371 if (list == NULL)
1372 return NULL;
1373 raw = type->tp_subclasses;
1374 if (raw == NULL)
1375 return list;
1376 assert(PyList_Check(raw));
1377 n = PyList_GET_SIZE(raw);
1378 for (i = 0; i < n; i++) {
1379 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001380 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001381 ref = PyWeakref_GET_OBJECT(ref);
1382 if (ref != Py_None) {
1383 if (PyList_Append(list, ref) < 0) {
1384 Py_DECREF(list);
1385 return NULL;
1386 }
1387 }
1388 }
1389 return list;
1390}
1391
Tim Peters6d6c1a32001-08-02 04:15:00 +00001392static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001393 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001394 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001395 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1396 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001397 {0}
1398};
1399
1400static char type_doc[] =
1401"type(object) -> the object's type\n"
1402"type(name, bases, dict) -> a new type";
1403
Guido van Rossum048eb752001-10-02 21:24:57 +00001404static int
1405type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1406{
1407 etype *et;
1408 int err;
1409
1410 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1411 return 0;
1412
1413 et = (etype *)type;
1414
1415#define VISIT(SLOT) \
1416 if (SLOT) { \
1417 err = visit((PyObject *)(SLOT), arg); \
1418 if (err) \
1419 return err; \
1420 }
1421
1422 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001423 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001424 VISIT(type->tp_mro);
1425 VISIT(type->tp_bases);
1426 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001427 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001428 VISIT(et->slots);
1429
1430#undef VISIT
1431
1432 return 0;
1433}
1434
1435static int
1436type_clear(PyTypeObject *type)
1437{
1438 etype *et;
1439 PyObject *tmp;
1440
1441 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1442 return 0;
1443
1444 et = (etype *)type;
1445
1446#define CLEAR(SLOT) \
1447 if (SLOT) { \
1448 tmp = (PyObject *)(SLOT); \
1449 SLOT = NULL; \
1450 Py_DECREF(tmp); \
1451 }
1452
1453 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001454 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001455 CLEAR(type->tp_mro);
1456 CLEAR(type->tp_bases);
1457 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001458 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001459 CLEAR(et->slots);
1460
Tim Peters2f93e282001-10-04 05:27:00 +00001461 if (type->tp_doc != NULL) {
1462 PyObject_FREE(type->tp_doc);
1463 type->tp_doc = NULL;
1464 }
1465
Guido van Rossum048eb752001-10-02 21:24:57 +00001466#undef CLEAR
1467
1468 return 0;
1469}
1470
1471static int
1472type_is_gc(PyTypeObject *type)
1473{
1474 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1475}
1476
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001477PyTypeObject PyType_Type = {
1478 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001479 0, /* ob_size */
1480 "type", /* tp_name */
1481 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001482 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001483 (destructor)type_dealloc, /* tp_dealloc */
1484 0, /* tp_print */
1485 0, /* tp_getattr */
1486 0, /* tp_setattr */
1487 type_compare, /* tp_compare */
1488 (reprfunc)type_repr, /* tp_repr */
1489 0, /* tp_as_number */
1490 0, /* tp_as_sequence */
1491 0, /* tp_as_mapping */
1492 (hashfunc)_Py_HashPointer, /* tp_hash */
1493 (ternaryfunc)type_call, /* tp_call */
1494 0, /* tp_str */
1495 (getattrofunc)type_getattro, /* tp_getattro */
1496 (setattrofunc)type_setattro, /* tp_setattro */
1497 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001498 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1499 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001500 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001501 (traverseproc)type_traverse, /* tp_traverse */
1502 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001503 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001504 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001505 0, /* tp_iter */
1506 0, /* tp_iternext */
1507 type_methods, /* tp_methods */
1508 type_members, /* tp_members */
1509 type_getsets, /* tp_getset */
1510 0, /* tp_base */
1511 0, /* tp_dict */
1512 0, /* tp_descr_get */
1513 0, /* tp_descr_set */
1514 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1515 0, /* tp_init */
1516 0, /* tp_alloc */
1517 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001518 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00001519 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001520};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001521
1522
1523/* The base type of all types (eventually)... except itself. */
1524
1525static int
1526object_init(PyObject *self, PyObject *args, PyObject *kwds)
1527{
1528 return 0;
1529}
1530
1531static void
1532object_dealloc(PyObject *self)
1533{
1534 self->ob_type->tp_free(self);
1535}
1536
Guido van Rossum8e248182001-08-12 05:17:56 +00001537static PyObject *
1538object_repr(PyObject *self)
1539{
Guido van Rossum76e69632001-08-16 18:52:43 +00001540 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001541 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001542
Guido van Rossum76e69632001-08-16 18:52:43 +00001543 type = self->ob_type;
1544 mod = type_module(type, NULL);
1545 if (mod == NULL)
1546 PyErr_Clear();
1547 else if (!PyString_Check(mod)) {
1548 Py_DECREF(mod);
1549 mod = NULL;
1550 }
1551 name = type_name(type, NULL);
1552 if (name == NULL)
1553 return NULL;
1554 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001555 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001556 PyString_AS_STRING(mod),
1557 PyString_AS_STRING(name),
1558 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001559 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001560 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001561 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001562 Py_XDECREF(mod);
1563 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001564 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001565}
1566
Guido van Rossumb8f63662001-08-15 23:57:02 +00001567static PyObject *
1568object_str(PyObject *self)
1569{
1570 unaryfunc f;
1571
1572 f = self->ob_type->tp_repr;
1573 if (f == NULL)
1574 f = object_repr;
1575 return f(self);
1576}
1577
Guido van Rossum8e248182001-08-12 05:17:56 +00001578static long
1579object_hash(PyObject *self)
1580{
1581 return _Py_HashPointer(self);
1582}
Guido van Rossum8e248182001-08-12 05:17:56 +00001583
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001584static PyObject *
1585object_get_class(PyObject *self, void *closure)
1586{
1587 Py_INCREF(self->ob_type);
1588 return (PyObject *)(self->ob_type);
1589}
1590
1591static int
1592equiv_structs(PyTypeObject *a, PyTypeObject *b)
1593{
1594 return a == b ||
1595 (a != NULL &&
1596 b != NULL &&
1597 a->tp_basicsize == b->tp_basicsize &&
1598 a->tp_itemsize == b->tp_itemsize &&
1599 a->tp_dictoffset == b->tp_dictoffset &&
1600 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1601 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1602 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1603}
1604
1605static int
1606same_slots_added(PyTypeObject *a, PyTypeObject *b)
1607{
1608 PyTypeObject *base = a->tp_base;
1609 int size;
1610
1611 if (base != b->tp_base)
1612 return 0;
1613 if (equiv_structs(a, base) && equiv_structs(b, base))
1614 return 1;
1615 size = base->tp_basicsize;
1616 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1617 size += sizeof(PyObject *);
1618 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1619 size += sizeof(PyObject *);
1620 return size == a->tp_basicsize && size == b->tp_basicsize;
1621}
1622
1623static int
1624object_set_class(PyObject *self, PyObject *value, void *closure)
1625{
1626 PyTypeObject *old = self->ob_type;
1627 PyTypeObject *new, *newbase, *oldbase;
1628
Guido van Rossumb6b89422002-04-15 01:03:30 +00001629 if (value == NULL) {
1630 PyErr_SetString(PyExc_TypeError,
1631 "can't delete __class__ attribute");
1632 return -1;
1633 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001634 if (!PyType_Check(value)) {
1635 PyErr_Format(PyExc_TypeError,
1636 "__class__ must be set to new-style class, not '%s' object",
1637 value->ob_type->tp_name);
1638 return -1;
1639 }
1640 new = (PyTypeObject *)value;
Guido van Rossum9ee4b942002-05-24 18:47:47 +00001641 if (new->tp_dealloc != old->tp_dealloc ||
1642 new->tp_free != old->tp_free)
1643 {
1644 PyErr_Format(PyExc_TypeError,
1645 "__class__ assignment: "
1646 "'%s' deallocator differs from '%s'",
1647 new->tp_name,
1648 old->tp_name);
1649 return -1;
1650 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001651 newbase = new;
1652 oldbase = old;
1653 while (equiv_structs(newbase, newbase->tp_base))
1654 newbase = newbase->tp_base;
1655 while (equiv_structs(oldbase, oldbase->tp_base))
1656 oldbase = oldbase->tp_base;
1657 if (newbase != oldbase &&
1658 (newbase->tp_base != oldbase->tp_base ||
1659 !same_slots_added(newbase, oldbase))) {
1660 PyErr_Format(PyExc_TypeError,
1661 "__class__ assignment: "
1662 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001663 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001664 old->tp_name);
1665 return -1;
1666 }
1667 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1668 Py_INCREF(new);
1669 }
1670 self->ob_type = new;
1671 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1672 Py_DECREF(old);
1673 }
1674 return 0;
1675}
1676
1677static PyGetSetDef object_getsets[] = {
1678 {"__class__", object_get_class, object_set_class,
1679 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001680 {0}
1681};
1682
Guido van Rossum3926a632001-09-25 16:25:58 +00001683static PyObject *
1684object_reduce(PyObject *self, PyObject *args)
1685{
1686 /* Call copy_reg._reduce(self) */
1687 static PyObject *copy_reg_str;
1688 PyObject *copy_reg, *res;
1689
1690 if (!copy_reg_str) {
1691 copy_reg_str = PyString_InternFromString("copy_reg");
1692 if (copy_reg_str == NULL)
1693 return NULL;
1694 }
1695 copy_reg = PyImport_Import(copy_reg_str);
1696 if (!copy_reg)
1697 return NULL;
1698 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1699 Py_DECREF(copy_reg);
1700 return res;
1701}
1702
1703static PyMethodDef object_methods[] = {
1704 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1705 {0}
1706};
1707
Tim Peters6d6c1a32001-08-02 04:15:00 +00001708PyTypeObject PyBaseObject_Type = {
1709 PyObject_HEAD_INIT(&PyType_Type)
1710 0, /* ob_size */
1711 "object", /* tp_name */
1712 sizeof(PyObject), /* tp_basicsize */
1713 0, /* tp_itemsize */
1714 (destructor)object_dealloc, /* tp_dealloc */
1715 0, /* tp_print */
1716 0, /* tp_getattr */
1717 0, /* tp_setattr */
1718 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001719 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001720 0, /* tp_as_number */
1721 0, /* tp_as_sequence */
1722 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001723 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001724 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001725 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001726 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001727 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001728 0, /* tp_as_buffer */
1729 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1730 "The most base type", /* tp_doc */
1731 0, /* tp_traverse */
1732 0, /* tp_clear */
1733 0, /* tp_richcompare */
1734 0, /* tp_weaklistoffset */
1735 0, /* tp_iter */
1736 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001737 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001738 0, /* tp_members */
1739 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001740 0, /* tp_base */
1741 0, /* tp_dict */
1742 0, /* tp_descr_get */
1743 0, /* tp_descr_set */
1744 0, /* tp_dictoffset */
1745 object_init, /* tp_init */
1746 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001747 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001748 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001749};
1750
1751
1752/* Initialize the __dict__ in a type object */
1753
Fred Drake7bf97152002-03-28 05:33:33 +00001754static PyObject *
1755create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *))
1756{
1757 PyObject *cfunc;
1758 PyObject *result;
1759
1760 cfunc = PyCFunction_New(meth, NULL);
1761 if (cfunc == NULL)
1762 return NULL;
1763 result = func(cfunc);
1764 Py_DECREF(cfunc);
1765 return result;
1766}
1767
Tim Peters6d6c1a32001-08-02 04:15:00 +00001768static int
1769add_methods(PyTypeObject *type, PyMethodDef *meth)
1770{
Guido van Rossum687ae002001-10-15 22:03:32 +00001771 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001772
1773 for (; meth->ml_name != NULL; meth++) {
1774 PyObject *descr;
1775 if (PyDict_GetItemString(dict, meth->ml_name))
1776 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00001777 if (meth->ml_flags & METH_CLASS) {
1778 if (meth->ml_flags & METH_STATIC) {
1779 PyErr_SetString(PyExc_ValueError,
1780 "method cannot be both class and static");
1781 return -1;
1782 }
1783 descr = create_specialmethod(meth, PyClassMethod_New);
1784 }
1785 else if (meth->ml_flags & METH_STATIC) {
1786 descr = create_specialmethod(meth, PyStaticMethod_New);
1787 }
1788 else {
1789 descr = PyDescr_NewMethod(type, meth);
1790 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001791 if (descr == NULL)
1792 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00001793 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001794 return -1;
1795 Py_DECREF(descr);
1796 }
1797 return 0;
1798}
1799
1800static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001801add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001802{
Guido van Rossum687ae002001-10-15 22:03:32 +00001803 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001804
1805 for (; memb->name != NULL; memb++) {
1806 PyObject *descr;
1807 if (PyDict_GetItemString(dict, memb->name))
1808 continue;
1809 descr = PyDescr_NewMember(type, memb);
1810 if (descr == NULL)
1811 return -1;
1812 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1813 return -1;
1814 Py_DECREF(descr);
1815 }
1816 return 0;
1817}
1818
1819static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001820add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001821{
Guido van Rossum687ae002001-10-15 22:03:32 +00001822 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001823
1824 for (; gsp->name != NULL; gsp++) {
1825 PyObject *descr;
1826 if (PyDict_GetItemString(dict, gsp->name))
1827 continue;
1828 descr = PyDescr_NewGetSet(type, gsp);
1829
1830 if (descr == NULL)
1831 return -1;
1832 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1833 return -1;
1834 Py_DECREF(descr);
1835 }
1836 return 0;
1837}
1838
Guido van Rossum13d52f02001-08-10 21:24:08 +00001839static void
1840inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001841{
1842 int oldsize, newsize;
1843
Guido van Rossum13d52f02001-08-10 21:24:08 +00001844 /* Special flag magic */
1845 if (!type->tp_as_buffer && base->tp_as_buffer) {
1846 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1847 type->tp_flags |=
1848 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1849 }
1850 if (!type->tp_as_sequence && base->tp_as_sequence) {
1851 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1852 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1853 }
1854 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1855 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1856 if ((!type->tp_as_number && base->tp_as_number) ||
1857 (!type->tp_as_sequence && base->tp_as_sequence)) {
1858 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1859 if (!type->tp_as_number && !type->tp_as_sequence) {
1860 type->tp_flags |= base->tp_flags &
1861 Py_TPFLAGS_HAVE_INPLACEOPS;
1862 }
1863 }
1864 /* Wow */
1865 }
1866 if (!type->tp_as_number && base->tp_as_number) {
1867 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1868 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1869 }
1870
1871 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001872 oldsize = base->tp_basicsize;
1873 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1874 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1875 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001876 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1877 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001878 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001879 if (type->tp_traverse == NULL)
1880 type->tp_traverse = base->tp_traverse;
1881 if (type->tp_clear == NULL)
1882 type->tp_clear = base->tp_clear;
1883 }
1884 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001885 /* The condition below could use some explanation.
1886 It appears that tp_new is not inherited for static types
1887 whose base class is 'object'; this seems to be a precaution
1888 so that old extension types don't suddenly become
1889 callable (object.__new__ wouldn't insure the invariants
1890 that the extension type's own factory function ensures).
1891 Heap types, of course, are under our control, so they do
1892 inherit tp_new; static extension types that specify some
1893 other built-in type as the default are considered
1894 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001895 if (base != &PyBaseObject_Type ||
1896 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1897 if (type->tp_new == NULL)
1898 type->tp_new = base->tp_new;
1899 }
1900 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001901 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001902
1903 /* Copy other non-function slots */
1904
1905#undef COPYVAL
1906#define COPYVAL(SLOT) \
1907 if (type->SLOT == 0) type->SLOT = base->SLOT
1908
1909 COPYVAL(tp_itemsize);
1910 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1911 COPYVAL(tp_weaklistoffset);
1912 }
1913 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1914 COPYVAL(tp_dictoffset);
1915 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001916}
1917
1918static void
1919inherit_slots(PyTypeObject *type, PyTypeObject *base)
1920{
1921 PyTypeObject *basebase;
1922
1923#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00001924#undef COPYSLOT
1925#undef COPYNUM
1926#undef COPYSEQ
1927#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00001928#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00001929
1930#define SLOTDEFINED(SLOT) \
1931 (base->SLOT != 0 && \
1932 (basebase == NULL || base->SLOT != basebase->SLOT))
1933
Tim Peters6d6c1a32001-08-02 04:15:00 +00001934#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00001935 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00001936
1937#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
1938#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
1939#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00001940#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001941
Guido van Rossum13d52f02001-08-10 21:24:08 +00001942 /* This won't inherit indirect slots (from tp_as_number etc.)
1943 if type doesn't provide the space. */
1944
1945 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
1946 basebase = base->tp_base;
1947 if (basebase->tp_as_number == NULL)
1948 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001949 COPYNUM(nb_add);
1950 COPYNUM(nb_subtract);
1951 COPYNUM(nb_multiply);
1952 COPYNUM(nb_divide);
1953 COPYNUM(nb_remainder);
1954 COPYNUM(nb_divmod);
1955 COPYNUM(nb_power);
1956 COPYNUM(nb_negative);
1957 COPYNUM(nb_positive);
1958 COPYNUM(nb_absolute);
1959 COPYNUM(nb_nonzero);
1960 COPYNUM(nb_invert);
1961 COPYNUM(nb_lshift);
1962 COPYNUM(nb_rshift);
1963 COPYNUM(nb_and);
1964 COPYNUM(nb_xor);
1965 COPYNUM(nb_or);
1966 COPYNUM(nb_coerce);
1967 COPYNUM(nb_int);
1968 COPYNUM(nb_long);
1969 COPYNUM(nb_float);
1970 COPYNUM(nb_oct);
1971 COPYNUM(nb_hex);
1972 COPYNUM(nb_inplace_add);
1973 COPYNUM(nb_inplace_subtract);
1974 COPYNUM(nb_inplace_multiply);
1975 COPYNUM(nb_inplace_divide);
1976 COPYNUM(nb_inplace_remainder);
1977 COPYNUM(nb_inplace_power);
1978 COPYNUM(nb_inplace_lshift);
1979 COPYNUM(nb_inplace_rshift);
1980 COPYNUM(nb_inplace_and);
1981 COPYNUM(nb_inplace_xor);
1982 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00001983 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
1984 COPYNUM(nb_true_divide);
1985 COPYNUM(nb_floor_divide);
1986 COPYNUM(nb_inplace_true_divide);
1987 COPYNUM(nb_inplace_floor_divide);
1988 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001989 }
1990
Guido van Rossum13d52f02001-08-10 21:24:08 +00001991 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
1992 basebase = base->tp_base;
1993 if (basebase->tp_as_sequence == NULL)
1994 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001995 COPYSEQ(sq_length);
1996 COPYSEQ(sq_concat);
1997 COPYSEQ(sq_repeat);
1998 COPYSEQ(sq_item);
1999 COPYSEQ(sq_slice);
2000 COPYSEQ(sq_ass_item);
2001 COPYSEQ(sq_ass_slice);
2002 COPYSEQ(sq_contains);
2003 COPYSEQ(sq_inplace_concat);
2004 COPYSEQ(sq_inplace_repeat);
2005 }
2006
Guido van Rossum13d52f02001-08-10 21:24:08 +00002007 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
2008 basebase = base->tp_base;
2009 if (basebase->tp_as_mapping == NULL)
2010 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002011 COPYMAP(mp_length);
2012 COPYMAP(mp_subscript);
2013 COPYMAP(mp_ass_subscript);
2014 }
2015
Tim Petersfc57ccb2001-10-12 02:38:24 +00002016 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2017 basebase = base->tp_base;
2018 if (basebase->tp_as_buffer == NULL)
2019 basebase = NULL;
2020 COPYBUF(bf_getreadbuffer);
2021 COPYBUF(bf_getwritebuffer);
2022 COPYBUF(bf_getsegcount);
2023 COPYBUF(bf_getcharbuffer);
2024 }
2025
Guido van Rossum13d52f02001-08-10 21:24:08 +00002026 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002027
Tim Peters6d6c1a32001-08-02 04:15:00 +00002028 COPYSLOT(tp_dealloc);
2029 COPYSLOT(tp_print);
2030 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
2031 type->tp_getattr = base->tp_getattr;
2032 type->tp_getattro = base->tp_getattro;
2033 }
2034 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
2035 type->tp_setattr = base->tp_setattr;
2036 type->tp_setattro = base->tp_setattro;
2037 }
2038 /* tp_compare see tp_richcompare */
2039 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002040 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002041 COPYSLOT(tp_call);
2042 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002043 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002044 if (type->tp_compare == NULL &&
2045 type->tp_richcompare == NULL &&
2046 type->tp_hash == NULL)
2047 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002048 type->tp_compare = base->tp_compare;
2049 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002050 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002051 }
2052 }
2053 else {
2054 COPYSLOT(tp_compare);
2055 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002056 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
2057 COPYSLOT(tp_iter);
2058 COPYSLOT(tp_iternext);
2059 }
2060 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2061 COPYSLOT(tp_descr_get);
2062 COPYSLOT(tp_descr_set);
2063 COPYSLOT(tp_dictoffset);
2064 COPYSLOT(tp_init);
2065 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002066 COPYSLOT(tp_free);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00002067 COPYSLOT(tp_is_gc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002068 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069}
2070
Guido van Rossum13d52f02001-08-10 21:24:08 +00002071staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00002072staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002073
Tim Peters6d6c1a32001-08-02 04:15:00 +00002074int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002075PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002076{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002077 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002078 PyTypeObject *base;
2079 int i, n;
2080
Guido van Rossumd614f972001-08-10 17:39:49 +00002081 if (type->tp_flags & Py_TPFLAGS_READY) {
2082 assert(type->tp_dict != NULL);
2083 return 0;
2084 }
2085 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00002086
2087 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002088
2089 /* Initialize tp_base (defaults to BaseObject unless that's us) */
2090 base = type->tp_base;
2091 if (base == NULL && type != &PyBaseObject_Type)
2092 base = type->tp_base = &PyBaseObject_Type;
2093
Guido van Rossum0986d822002-04-08 01:38:42 +00002094 /* Initialize ob_type if NULL. This means extensions that want to be
2095 compilable separately on Windows can call PyType_Ready() instead of
2096 initializing the ob_type field of their type objects. */
2097 if (type->ob_type == NULL)
2098 type->ob_type = base->ob_type;
2099
Tim Peters6d6c1a32001-08-02 04:15:00 +00002100 /* Initialize tp_bases */
2101 bases = type->tp_bases;
2102 if (bases == NULL) {
2103 if (base == NULL)
2104 bases = PyTuple_New(0);
2105 else
2106 bases = Py_BuildValue("(O)", base);
2107 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002108 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002109 type->tp_bases = bases;
2110 }
2111
2112 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002113 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002114 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002115 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116 }
2117
Guido van Rossum687ae002001-10-15 22:03:32 +00002118 /* Initialize tp_dict */
2119 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002120 if (dict == NULL) {
2121 dict = PyDict_New();
2122 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002123 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00002124 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002125 }
2126
Guido van Rossum687ae002001-10-15 22:03:32 +00002127 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002128 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002129 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002130 if (type->tp_methods != NULL) {
2131 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002132 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002133 }
2134 if (type->tp_members != NULL) {
2135 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002136 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002137 }
2138 if (type->tp_getset != NULL) {
2139 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002140 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002141 }
2142
Tim Peters6d6c1a32001-08-02 04:15:00 +00002143 /* Calculate method resolution order */
2144 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002145 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002146 }
2147
Guido van Rossum13d52f02001-08-10 21:24:08 +00002148 /* Inherit special flags from dominant base */
2149 if (type->tp_base != NULL)
2150 inherit_special(type, type->tp_base);
2151
Tim Peters6d6c1a32001-08-02 04:15:00 +00002152 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002153 bases = type->tp_mro;
2154 assert(bases != NULL);
2155 assert(PyTuple_Check(bases));
2156 n = PyTuple_GET_SIZE(bases);
2157 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002158 PyObject *b = PyTuple_GET_ITEM(bases, i);
2159 if (PyType_Check(b))
2160 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002161 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002162
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002163 /* if the type dictionary doesn't contain a __doc__, set it from
2164 the tp_doc slot.
2165 */
2166 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2167 if (type->tp_doc != NULL) {
2168 PyObject *doc = PyString_FromString(type->tp_doc);
2169 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2170 Py_DECREF(doc);
2171 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00002172 PyDict_SetItemString(type->tp_dict,
2173 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002174 }
2175 }
2176
Guido van Rossum13d52f02001-08-10 21:24:08 +00002177 /* Some more special stuff */
2178 base = type->tp_base;
2179 if (base != NULL) {
2180 if (type->tp_as_number == NULL)
2181 type->tp_as_number = base->tp_as_number;
2182 if (type->tp_as_sequence == NULL)
2183 type->tp_as_sequence = base->tp_as_sequence;
2184 if (type->tp_as_mapping == NULL)
2185 type->tp_as_mapping = base->tp_as_mapping;
2186 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002187
Guido van Rossum1c450732001-10-08 15:18:27 +00002188 /* Link into each base class's list of subclasses */
2189 bases = type->tp_bases;
2190 n = PyTuple_GET_SIZE(bases);
2191 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002192 PyObject *b = PyTuple_GET_ITEM(bases, i);
2193 if (PyType_Check(b) &&
2194 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002195 goto error;
2196 }
2197
Guido van Rossum13d52f02001-08-10 21:24:08 +00002198 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002199 assert(type->tp_dict != NULL);
2200 type->tp_flags =
2201 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002202 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002203
2204 error:
2205 type->tp_flags &= ~Py_TPFLAGS_READYING;
2206 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002207}
2208
Guido van Rossum1c450732001-10-08 15:18:27 +00002209static int
2210add_subclass(PyTypeObject *base, PyTypeObject *type)
2211{
2212 int i;
2213 PyObject *list, *ref, *new;
2214
2215 list = base->tp_subclasses;
2216 if (list == NULL) {
2217 base->tp_subclasses = list = PyList_New(0);
2218 if (list == NULL)
2219 return -1;
2220 }
2221 assert(PyList_Check(list));
2222 new = PyWeakref_NewRef((PyObject *)type, NULL);
2223 i = PyList_GET_SIZE(list);
2224 while (--i >= 0) {
2225 ref = PyList_GET_ITEM(list, i);
2226 assert(PyWeakref_CheckRef(ref));
2227 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2228 return PyList_SetItem(list, i, new);
2229 }
2230 i = PyList_Append(list, new);
2231 Py_DECREF(new);
2232 return i;
2233}
2234
Tim Peters6d6c1a32001-08-02 04:15:00 +00002235
2236/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2237
2238/* There's a wrapper *function* for each distinct function typedef used
2239 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2240 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2241 Most tables have only one entry; the tables for binary operators have two
2242 entries, one regular and one with reversed arguments. */
2243
2244static PyObject *
2245wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2246{
2247 inquiry func = (inquiry)wrapped;
2248 int res;
2249
2250 if (!PyArg_ParseTuple(args, ""))
2251 return NULL;
2252 res = (*func)(self);
2253 if (res == -1 && PyErr_Occurred())
2254 return NULL;
2255 return PyInt_FromLong((long)res);
2256}
2257
Tim Peters6d6c1a32001-08-02 04:15:00 +00002258static PyObject *
2259wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2260{
2261 binaryfunc func = (binaryfunc)wrapped;
2262 PyObject *other;
2263
2264 if (!PyArg_ParseTuple(args, "O", &other))
2265 return NULL;
2266 return (*func)(self, other);
2267}
2268
2269static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002270wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2271{
2272 binaryfunc func = (binaryfunc)wrapped;
2273 PyObject *other;
2274
2275 if (!PyArg_ParseTuple(args, "O", &other))
2276 return NULL;
2277 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002278 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002279 Py_INCREF(Py_NotImplemented);
2280 return Py_NotImplemented;
2281 }
2282 return (*func)(self, other);
2283}
2284
2285static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2287{
2288 binaryfunc func = (binaryfunc)wrapped;
2289 PyObject *other;
2290
2291 if (!PyArg_ParseTuple(args, "O", &other))
2292 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002293 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002294 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002295 Py_INCREF(Py_NotImplemented);
2296 return Py_NotImplemented;
2297 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002298 return (*func)(other, self);
2299}
2300
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002301static PyObject *
2302wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2303{
2304 coercion func = (coercion)wrapped;
2305 PyObject *other, *res;
2306 int ok;
2307
2308 if (!PyArg_ParseTuple(args, "O", &other))
2309 return NULL;
2310 ok = func(&self, &other);
2311 if (ok < 0)
2312 return NULL;
2313 if (ok > 0) {
2314 Py_INCREF(Py_NotImplemented);
2315 return Py_NotImplemented;
2316 }
2317 res = PyTuple_New(2);
2318 if (res == NULL) {
2319 Py_DECREF(self);
2320 Py_DECREF(other);
2321 return NULL;
2322 }
2323 PyTuple_SET_ITEM(res, 0, self);
2324 PyTuple_SET_ITEM(res, 1, other);
2325 return res;
2326}
2327
Tim Peters6d6c1a32001-08-02 04:15:00 +00002328static PyObject *
2329wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2330{
2331 ternaryfunc func = (ternaryfunc)wrapped;
2332 PyObject *other;
2333 PyObject *third = Py_None;
2334
2335 /* Note: This wrapper only works for __pow__() */
2336
2337 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2338 return NULL;
2339 return (*func)(self, other, third);
2340}
2341
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002342static PyObject *
2343wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2344{
2345 ternaryfunc func = (ternaryfunc)wrapped;
2346 PyObject *other;
2347 PyObject *third = Py_None;
2348
2349 /* Note: This wrapper only works for __pow__() */
2350
2351 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2352 return NULL;
2353 return (*func)(other, self, third);
2354}
2355
Tim Peters6d6c1a32001-08-02 04:15:00 +00002356static PyObject *
2357wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2358{
2359 unaryfunc func = (unaryfunc)wrapped;
2360
2361 if (!PyArg_ParseTuple(args, ""))
2362 return NULL;
2363 return (*func)(self);
2364}
2365
Tim Peters6d6c1a32001-08-02 04:15:00 +00002366static PyObject *
2367wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2368{
2369 intargfunc func = (intargfunc)wrapped;
2370 int i;
2371
2372 if (!PyArg_ParseTuple(args, "i", &i))
2373 return NULL;
2374 return (*func)(self, i);
2375}
2376
Guido van Rossum5d815f32001-08-17 21:57:47 +00002377static int
2378getindex(PyObject *self, PyObject *arg)
2379{
2380 int i;
2381
2382 i = PyInt_AsLong(arg);
2383 if (i == -1 && PyErr_Occurred())
2384 return -1;
2385 if (i < 0) {
2386 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2387 if (sq && sq->sq_length) {
2388 int n = (*sq->sq_length)(self);
2389 if (n < 0)
2390 return -1;
2391 i += n;
2392 }
2393 }
2394 return i;
2395}
2396
2397static PyObject *
2398wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2399{
2400 intargfunc func = (intargfunc)wrapped;
2401 PyObject *arg;
2402 int i;
2403
Guido van Rossumf4593e02001-10-03 12:09:30 +00002404 if (PyTuple_GET_SIZE(args) == 1) {
2405 arg = PyTuple_GET_ITEM(args, 0);
2406 i = getindex(self, arg);
2407 if (i == -1 && PyErr_Occurred())
2408 return NULL;
2409 return (*func)(self, i);
2410 }
2411 PyArg_ParseTuple(args, "O", &arg);
2412 assert(PyErr_Occurred());
2413 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002414}
2415
Tim Peters6d6c1a32001-08-02 04:15:00 +00002416static PyObject *
2417wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2418{
2419 intintargfunc func = (intintargfunc)wrapped;
2420 int i, j;
2421
2422 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2423 return NULL;
2424 return (*func)(self, i, j);
2425}
2426
Tim Peters6d6c1a32001-08-02 04:15:00 +00002427static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002428wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002429{
2430 intobjargproc func = (intobjargproc)wrapped;
2431 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002432 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002433
Guido van Rossum5d815f32001-08-17 21:57:47 +00002434 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2435 return NULL;
2436 i = getindex(self, arg);
2437 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002438 return NULL;
2439 res = (*func)(self, i, value);
2440 if (res == -1 && PyErr_Occurred())
2441 return NULL;
2442 Py_INCREF(Py_None);
2443 return Py_None;
2444}
2445
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002446static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002447wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002448{
2449 intobjargproc func = (intobjargproc)wrapped;
2450 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002451 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002452
Guido van Rossum5d815f32001-08-17 21:57:47 +00002453 if (!PyArg_ParseTuple(args, "O", &arg))
2454 return NULL;
2455 i = getindex(self, arg);
2456 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002457 return NULL;
2458 res = (*func)(self, i, NULL);
2459 if (res == -1 && PyErr_Occurred())
2460 return NULL;
2461 Py_INCREF(Py_None);
2462 return Py_None;
2463}
2464
Tim Peters6d6c1a32001-08-02 04:15:00 +00002465static PyObject *
2466wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2467{
2468 intintobjargproc func = (intintobjargproc)wrapped;
2469 int i, j, res;
2470 PyObject *value;
2471
2472 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2473 return NULL;
2474 res = (*func)(self, i, j, value);
2475 if (res == -1 && PyErr_Occurred())
2476 return NULL;
2477 Py_INCREF(Py_None);
2478 return Py_None;
2479}
2480
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002481static PyObject *
2482wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2483{
2484 intintobjargproc func = (intintobjargproc)wrapped;
2485 int i, j, res;
2486
2487 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2488 return NULL;
2489 res = (*func)(self, i, j, NULL);
2490 if (res == -1 && PyErr_Occurred())
2491 return NULL;
2492 Py_INCREF(Py_None);
2493 return Py_None;
2494}
2495
Tim Peters6d6c1a32001-08-02 04:15:00 +00002496/* XXX objobjproc is a misnomer; should be objargpred */
2497static PyObject *
2498wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2499{
2500 objobjproc func = (objobjproc)wrapped;
2501 int res;
2502 PyObject *value;
2503
2504 if (!PyArg_ParseTuple(args, "O", &value))
2505 return NULL;
2506 res = (*func)(self, value);
2507 if (res == -1 && PyErr_Occurred())
2508 return NULL;
2509 return PyInt_FromLong((long)res);
2510}
2511
Tim Peters6d6c1a32001-08-02 04:15:00 +00002512static PyObject *
2513wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2514{
2515 objobjargproc func = (objobjargproc)wrapped;
2516 int res;
2517 PyObject *key, *value;
2518
2519 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2520 return NULL;
2521 res = (*func)(self, key, value);
2522 if (res == -1 && PyErr_Occurred())
2523 return NULL;
2524 Py_INCREF(Py_None);
2525 return Py_None;
2526}
2527
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002528static PyObject *
2529wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2530{
2531 objobjargproc func = (objobjargproc)wrapped;
2532 int res;
2533 PyObject *key;
2534
2535 if (!PyArg_ParseTuple(args, "O", &key))
2536 return NULL;
2537 res = (*func)(self, key, NULL);
2538 if (res == -1 && PyErr_Occurred())
2539 return NULL;
2540 Py_INCREF(Py_None);
2541 return Py_None;
2542}
2543
Tim Peters6d6c1a32001-08-02 04:15:00 +00002544static PyObject *
2545wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2546{
2547 cmpfunc func = (cmpfunc)wrapped;
2548 int res;
2549 PyObject *other;
2550
2551 if (!PyArg_ParseTuple(args, "O", &other))
2552 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002553 if (other->ob_type->tp_compare != func &&
2554 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002555 PyErr_Format(
2556 PyExc_TypeError,
2557 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2558 self->ob_type->tp_name,
2559 self->ob_type->tp_name,
2560 other->ob_type->tp_name);
2561 return NULL;
2562 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002563 res = (*func)(self, other);
2564 if (PyErr_Occurred())
2565 return NULL;
2566 return PyInt_FromLong((long)res);
2567}
2568
Tim Peters6d6c1a32001-08-02 04:15:00 +00002569static PyObject *
2570wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2571{
2572 setattrofunc func = (setattrofunc)wrapped;
2573 int res;
2574 PyObject *name, *value;
2575
2576 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2577 return NULL;
2578 res = (*func)(self, name, value);
2579 if (res < 0)
2580 return NULL;
2581 Py_INCREF(Py_None);
2582 return Py_None;
2583}
2584
2585static PyObject *
2586wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2587{
2588 setattrofunc func = (setattrofunc)wrapped;
2589 int res;
2590 PyObject *name;
2591
2592 if (!PyArg_ParseTuple(args, "O", &name))
2593 return NULL;
2594 res = (*func)(self, name, NULL);
2595 if (res < 0)
2596 return NULL;
2597 Py_INCREF(Py_None);
2598 return Py_None;
2599}
2600
Tim Peters6d6c1a32001-08-02 04:15:00 +00002601static PyObject *
2602wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2603{
2604 hashfunc func = (hashfunc)wrapped;
2605 long res;
2606
2607 if (!PyArg_ParseTuple(args, ""))
2608 return NULL;
2609 res = (*func)(self);
2610 if (res == -1 && PyErr_Occurred())
2611 return NULL;
2612 return PyInt_FromLong(res);
2613}
2614
Tim Peters6d6c1a32001-08-02 04:15:00 +00002615static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002616wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002617{
2618 ternaryfunc func = (ternaryfunc)wrapped;
2619
Guido van Rossumc8e56452001-10-22 00:43:43 +00002620 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002621}
2622
Tim Peters6d6c1a32001-08-02 04:15:00 +00002623static PyObject *
2624wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2625{
2626 richcmpfunc func = (richcmpfunc)wrapped;
2627 PyObject *other;
2628
2629 if (!PyArg_ParseTuple(args, "O", &other))
2630 return NULL;
2631 return (*func)(self, other, op);
2632}
2633
2634#undef RICHCMP_WRAPPER
2635#define RICHCMP_WRAPPER(NAME, OP) \
2636static PyObject * \
2637richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2638{ \
2639 return wrap_richcmpfunc(self, args, wrapped, OP); \
2640}
2641
Jack Jansen8e938b42001-08-08 15:29:49 +00002642RICHCMP_WRAPPER(lt, Py_LT)
2643RICHCMP_WRAPPER(le, Py_LE)
2644RICHCMP_WRAPPER(eq, Py_EQ)
2645RICHCMP_WRAPPER(ne, Py_NE)
2646RICHCMP_WRAPPER(gt, Py_GT)
2647RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002648
Tim Peters6d6c1a32001-08-02 04:15:00 +00002649static PyObject *
2650wrap_next(PyObject *self, PyObject *args, void *wrapped)
2651{
2652 unaryfunc func = (unaryfunc)wrapped;
2653 PyObject *res;
2654
2655 if (!PyArg_ParseTuple(args, ""))
2656 return NULL;
2657 res = (*func)(self);
2658 if (res == NULL && !PyErr_Occurred())
2659 PyErr_SetNone(PyExc_StopIteration);
2660 return res;
2661}
2662
Tim Peters6d6c1a32001-08-02 04:15:00 +00002663static PyObject *
2664wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2665{
2666 descrgetfunc func = (descrgetfunc)wrapped;
2667 PyObject *obj;
2668 PyObject *type = NULL;
2669
2670 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2671 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002672 return (*func)(self, obj, type);
2673}
2674
Tim Peters6d6c1a32001-08-02 04:15:00 +00002675static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002676wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002677{
2678 descrsetfunc func = (descrsetfunc)wrapped;
2679 PyObject *obj, *value;
2680 int ret;
2681
2682 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2683 return NULL;
2684 ret = (*func)(self, obj, value);
2685 if (ret < 0)
2686 return NULL;
2687 Py_INCREF(Py_None);
2688 return Py_None;
2689}
2690
Tim Peters6d6c1a32001-08-02 04:15:00 +00002691static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002692wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002693{
2694 initproc func = (initproc)wrapped;
2695
Guido van Rossumc8e56452001-10-22 00:43:43 +00002696 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002697 return NULL;
2698 Py_INCREF(Py_None);
2699 return Py_None;
2700}
2701
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002703tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002704{
Barry Warsaw60f01882001-08-22 19:24:42 +00002705 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002706 PyObject *arg0, *res;
2707
2708 if (self == NULL || !PyType_Check(self))
2709 Py_FatalError("__new__() called with non-type 'self'");
2710 type = (PyTypeObject *)self;
2711 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002712 PyErr_Format(PyExc_TypeError,
2713 "%s.__new__(): not enough arguments",
2714 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002715 return NULL;
2716 }
2717 arg0 = PyTuple_GET_ITEM(args, 0);
2718 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002719 PyErr_Format(PyExc_TypeError,
2720 "%s.__new__(X): X is not a type object (%s)",
2721 type->tp_name,
2722 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002723 return NULL;
2724 }
2725 subtype = (PyTypeObject *)arg0;
2726 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002727 PyErr_Format(PyExc_TypeError,
2728 "%s.__new__(%s): %s is not a subtype of %s",
2729 type->tp_name,
2730 subtype->tp_name,
2731 subtype->tp_name,
2732 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002733 return NULL;
2734 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002735
2736 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002737 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002738 most derived base that's not a heap type is this type. */
2739 staticbase = subtype;
2740 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2741 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002742 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002743 PyErr_Format(PyExc_TypeError,
2744 "%s.__new__(%s) is not safe, use %s.__new__()",
2745 type->tp_name,
2746 subtype->tp_name,
2747 staticbase == NULL ? "?" : staticbase->tp_name);
2748 return NULL;
2749 }
2750
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002751 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2752 if (args == NULL)
2753 return NULL;
2754 res = type->tp_new(subtype, args, kwds);
2755 Py_DECREF(args);
2756 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002757}
2758
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002759static struct PyMethodDef tp_new_methoddef[] = {
2760 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2761 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002762 {0}
2763};
2764
2765static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002766add_tp_new_wrapper(PyTypeObject *type)
2767{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002768 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002769
Guido van Rossum687ae002001-10-15 22:03:32 +00002770 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002771 return 0;
2772 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002773 if (func == NULL)
2774 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002775 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002776}
2777
Guido van Rossumf040ede2001-08-07 16:40:56 +00002778/* Slot wrappers that call the corresponding __foo__ slot. See comments
2779 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002780
Guido van Rossumdc91b992001-08-08 22:26:22 +00002781#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002782static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002783FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002784{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002785 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002786 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002787}
2788
Guido van Rossumdc91b992001-08-08 22:26:22 +00002789#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002790static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002791FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002792{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002793 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002794 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002795}
2796
Guido van Rossumdc91b992001-08-08 22:26:22 +00002797
2798#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002799static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002800FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002801{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002802 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002803 int do_other = self->ob_type != other->ob_type && \
2804 other->ob_type->tp_as_number != NULL && \
2805 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002806 if (self->ob_type->tp_as_number != NULL && \
2807 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2808 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002809 if (do_other && \
2810 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2811 r = call_maybe( \
2812 other, ROPSTR, &rcache_str, "(O)", self); \
2813 if (r != Py_NotImplemented) \
2814 return r; \
2815 Py_DECREF(r); \
2816 do_other = 0; \
2817 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002818 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002819 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002820 if (r != Py_NotImplemented || \
2821 other->ob_type == self->ob_type) \
2822 return r; \
2823 Py_DECREF(r); \
2824 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002825 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002826 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002827 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002828 } \
2829 Py_INCREF(Py_NotImplemented); \
2830 return Py_NotImplemented; \
2831}
2832
2833#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2834 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2835
2836#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2837static PyObject * \
2838FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2839{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002840 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002841 return call_method(self, OPSTR, &cache_str, \
2842 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002843}
2844
2845static int
2846slot_sq_length(PyObject *self)
2847{
Guido van Rossum2730b132001-08-28 18:22:14 +00002848 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002849 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002850 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002851
2852 if (res == NULL)
2853 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002854 len = (int)PyInt_AsLong(res);
2855 Py_DECREF(res);
2856 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002857}
2858
Guido van Rossumdc91b992001-08-08 22:26:22 +00002859SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2860SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002861
2862/* Super-optimized version of slot_sq_item.
2863 Other slots could do the same... */
2864static PyObject *
2865slot_sq_item(PyObject *self, int i)
2866{
2867 static PyObject *getitem_str;
2868 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2869 descrgetfunc f;
2870
2871 if (getitem_str == NULL) {
2872 getitem_str = PyString_InternFromString("__getitem__");
2873 if (getitem_str == NULL)
2874 return NULL;
2875 }
2876 func = _PyType_Lookup(self->ob_type, getitem_str);
2877 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002878 if ((f = func->ob_type->tp_descr_get) == NULL)
2879 Py_INCREF(func);
2880 else
2881 func = f(func, self, (PyObject *)(self->ob_type));
2882 ival = PyInt_FromLong(i);
2883 if (ival != NULL) {
2884 args = PyTuple_New(1);
2885 if (args != NULL) {
2886 PyTuple_SET_ITEM(args, 0, ival);
2887 retval = PyObject_Call(func, args, NULL);
2888 Py_XDECREF(args);
2889 Py_XDECREF(func);
2890 return retval;
2891 }
2892 }
2893 }
2894 else {
2895 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2896 }
2897 Py_XDECREF(args);
2898 Py_XDECREF(ival);
2899 Py_XDECREF(func);
2900 return NULL;
2901}
2902
Guido van Rossumdc91b992001-08-08 22:26:22 +00002903SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002904
2905static int
2906slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2907{
2908 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002909 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002910
2911 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002912 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002913 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002914 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002915 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002916 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002917 if (res == NULL)
2918 return -1;
2919 Py_DECREF(res);
2920 return 0;
2921}
2922
2923static int
2924slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
2925{
2926 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002927 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002928
2929 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002930 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002931 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002932 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002933 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002934 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002935 if (res == NULL)
2936 return -1;
2937 Py_DECREF(res);
2938 return 0;
2939}
2940
2941static int
2942slot_sq_contains(PyObject *self, PyObject *value)
2943{
Guido van Rossumb8f63662001-08-15 23:57:02 +00002944 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00002945 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002946
Guido van Rossum55f20992001-10-01 17:18:22 +00002947 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002948
2949 if (func != NULL) {
2950 args = Py_BuildValue("(O)", value);
2951 if (args == NULL)
2952 res = NULL;
2953 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00002954 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002955 Py_DECREF(args);
2956 }
2957 Py_DECREF(func);
2958 if (res == NULL)
2959 return -1;
2960 return PyObject_IsTrue(res);
2961 }
Guido van Rossum55f20992001-10-01 17:18:22 +00002962 else if (PyErr_Occurred())
2963 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002964 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00002965 return _PySequence_IterSearch(self, value,
2966 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002967 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002968}
2969
Guido van Rossumdc91b992001-08-08 22:26:22 +00002970SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
2971SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002972
2973#define slot_mp_length slot_sq_length
2974
Guido van Rossumdc91b992001-08-08 22:26:22 +00002975SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002976
2977static int
2978slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
2979{
2980 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002981 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002982
2983 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002984 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002985 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002986 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002987 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002988 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002989 if (res == NULL)
2990 return -1;
2991 Py_DECREF(res);
2992 return 0;
2993}
2994
Guido van Rossumdc91b992001-08-08 22:26:22 +00002995SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
2996SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
2997SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
2998SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
2999SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
3000SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
3001
3002staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
3003
3004SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
3005 nb_power, "__pow__", "__rpow__")
3006
3007static PyObject *
3008slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3009{
Guido van Rossum2730b132001-08-28 18:22:14 +00003010 static PyObject *pow_str;
3011
Guido van Rossumdc91b992001-08-08 22:26:22 +00003012 if (modulus == Py_None)
3013 return slot_nb_power_binary(self, other);
3014 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00003015 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003016 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003017}
3018
3019SLOT0(slot_nb_negative, "__neg__")
3020SLOT0(slot_nb_positive, "__pos__")
3021SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003022
3023static int
3024slot_nb_nonzero(PyObject *self)
3025{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003026 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003027 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003028
Guido van Rossum55f20992001-10-01 17:18:22 +00003029 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003030 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003031 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003032 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003033 func = lookup_maybe(self, "__len__", &len_str);
3034 if (func == NULL) {
3035 if (PyErr_Occurred())
3036 return -1;
3037 else
3038 return 1;
3039 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003040 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003041 res = PyObject_CallObject(func, NULL);
3042 Py_DECREF(func);
3043 if (res == NULL)
3044 return -1;
3045 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003046}
3047
Guido van Rossumdc91b992001-08-08 22:26:22 +00003048SLOT0(slot_nb_invert, "__invert__")
3049SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3050SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3051SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3052SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3053SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003054
3055static int
3056slot_nb_coerce(PyObject **a, PyObject **b)
3057{
3058 static PyObject *coerce_str;
3059 PyObject *self = *a, *other = *b;
3060
3061 if (self->ob_type->tp_as_number != NULL &&
3062 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3063 PyObject *r;
3064 r = call_maybe(
3065 self, "__coerce__", &coerce_str, "(O)", other);
3066 if (r == NULL)
3067 return -1;
3068 if (r == Py_NotImplemented) {
3069 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003070 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003071 else {
3072 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3073 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003074 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003075 Py_DECREF(r);
3076 return -1;
3077 }
3078 *a = PyTuple_GET_ITEM(r, 0);
3079 Py_INCREF(*a);
3080 *b = PyTuple_GET_ITEM(r, 1);
3081 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003082 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003083 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003084 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003085 }
3086 if (other->ob_type->tp_as_number != NULL &&
3087 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3088 PyObject *r;
3089 r = call_maybe(
3090 other, "__coerce__", &coerce_str, "(O)", self);
3091 if (r == NULL)
3092 return -1;
3093 if (r == Py_NotImplemented) {
3094 Py_DECREF(r);
3095 return 1;
3096 }
3097 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3098 PyErr_SetString(PyExc_TypeError,
3099 "__coerce__ didn't return a 2-tuple");
3100 Py_DECREF(r);
3101 return -1;
3102 }
3103 *a = PyTuple_GET_ITEM(r, 1);
3104 Py_INCREF(*a);
3105 *b = PyTuple_GET_ITEM(r, 0);
3106 Py_INCREF(*b);
3107 Py_DECREF(r);
3108 return 0;
3109 }
3110 return 1;
3111}
3112
Guido van Rossumdc91b992001-08-08 22:26:22 +00003113SLOT0(slot_nb_int, "__int__")
3114SLOT0(slot_nb_long, "__long__")
3115SLOT0(slot_nb_float, "__float__")
3116SLOT0(slot_nb_oct, "__oct__")
3117SLOT0(slot_nb_hex, "__hex__")
3118SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3119SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3120SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3121SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3122SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3123SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3124SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3125SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3126SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3127SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3128SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3129SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3130 "__floordiv__", "__rfloordiv__")
3131SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3132SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3133SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003134
3135static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003136half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003137{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003138 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003139 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003140 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003141
Guido van Rossum60718732001-08-28 17:47:51 +00003142 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003143 if (func == NULL) {
3144 PyErr_Clear();
3145 }
3146 else {
3147 args = Py_BuildValue("(O)", other);
3148 if (args == NULL)
3149 res = NULL;
3150 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003151 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003152 Py_DECREF(args);
3153 }
3154 if (res != Py_NotImplemented) {
3155 if (res == NULL)
3156 return -2;
3157 c = PyInt_AsLong(res);
3158 Py_DECREF(res);
3159 if (c == -1 && PyErr_Occurred())
3160 return -2;
3161 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3162 }
3163 Py_DECREF(res);
3164 }
3165 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003166}
3167
Guido van Rossumab3b0342001-09-18 20:38:53 +00003168/* This slot is published for the benefit of try_3way_compare in object.c */
3169int
3170_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003171{
3172 int c;
3173
Guido van Rossumab3b0342001-09-18 20:38:53 +00003174 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003175 c = half_compare(self, other);
3176 if (c <= 1)
3177 return c;
3178 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003179 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003180 c = half_compare(other, self);
3181 if (c < -1)
3182 return -2;
3183 if (c <= 1)
3184 return -c;
3185 }
3186 return (void *)self < (void *)other ? -1 :
3187 (void *)self > (void *)other ? 1 : 0;
3188}
3189
3190static PyObject *
3191slot_tp_repr(PyObject *self)
3192{
3193 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003194 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003195
Guido van Rossum60718732001-08-28 17:47:51 +00003196 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003197 if (func != NULL) {
3198 res = PyEval_CallObject(func, NULL);
3199 Py_DECREF(func);
3200 return res;
3201 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003202 PyErr_Clear();
3203 return PyString_FromFormat("<%s object at %p>",
3204 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003205}
3206
3207static PyObject *
3208slot_tp_str(PyObject *self)
3209{
3210 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003211 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003212
Guido van Rossum60718732001-08-28 17:47:51 +00003213 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003214 if (func != NULL) {
3215 res = PyEval_CallObject(func, NULL);
3216 Py_DECREF(func);
3217 return res;
3218 }
3219 else {
3220 PyErr_Clear();
3221 return slot_tp_repr(self);
3222 }
3223}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003224
3225static long
3226slot_tp_hash(PyObject *self)
3227{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003228 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003229 static PyObject *hash_str, *eq_str, *cmp_str;
3230
Tim Peters6d6c1a32001-08-02 04:15:00 +00003231 long h;
3232
Guido van Rossum60718732001-08-28 17:47:51 +00003233 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003234
3235 if (func != NULL) {
3236 res = PyEval_CallObject(func, NULL);
3237 Py_DECREF(func);
3238 if (res == NULL)
3239 return -1;
3240 h = PyInt_AsLong(res);
3241 }
3242 else {
3243 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003244 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003245 if (func == NULL) {
3246 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003247 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003248 }
3249 if (func != NULL) {
3250 Py_DECREF(func);
3251 PyErr_SetString(PyExc_TypeError, "unhashable type");
3252 return -1;
3253 }
3254 PyErr_Clear();
3255 h = _Py_HashPointer((void *)self);
3256 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003257 if (h == -1 && !PyErr_Occurred())
3258 h = -2;
3259 return h;
3260}
3261
3262static PyObject *
3263slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3264{
Guido van Rossum60718732001-08-28 17:47:51 +00003265 static PyObject *call_str;
3266 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003267 PyObject *res;
3268
3269 if (meth == NULL)
3270 return NULL;
3271 res = PyObject_Call(meth, args, kwds);
3272 Py_DECREF(meth);
3273 return res;
3274}
3275
Guido van Rossum14a6f832001-10-17 13:59:09 +00003276/* There are two slot dispatch functions for tp_getattro.
3277
3278 - slot_tp_getattro() is used when __getattribute__ is overridden
3279 but no __getattr__ hook is present;
3280
3281 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3282
Guido van Rossumc334df52002-04-04 23:44:47 +00003283 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
3284 detects the absence of __getattr__ and then installs the simpler slot if
3285 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00003286
Tim Peters6d6c1a32001-08-02 04:15:00 +00003287static PyObject *
3288slot_tp_getattro(PyObject *self, PyObject *name)
3289{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003290 static PyObject *getattribute_str = NULL;
3291 return call_method(self, "__getattribute__", &getattribute_str,
3292 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003293}
3294
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003295static PyObject *
3296slot_tp_getattr_hook(PyObject *self, PyObject *name)
3297{
3298 PyTypeObject *tp = self->ob_type;
3299 PyObject *getattr, *getattribute, *res;
3300 static PyObject *getattribute_str = NULL;
3301 static PyObject *getattr_str = NULL;
3302
3303 if (getattr_str == NULL) {
3304 getattr_str = PyString_InternFromString("__getattr__");
3305 if (getattr_str == NULL)
3306 return NULL;
3307 }
3308 if (getattribute_str == NULL) {
3309 getattribute_str =
3310 PyString_InternFromString("__getattribute__");
3311 if (getattribute_str == NULL)
3312 return NULL;
3313 }
3314 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003315 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003316 /* No __getattr__ hook: use a simpler dispatcher */
3317 tp->tp_getattro = slot_tp_getattro;
3318 return slot_tp_getattro(self, name);
3319 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003320 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003321 if (getattribute == NULL ||
3322 (getattribute->ob_type == &PyWrapperDescr_Type &&
3323 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3324 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003325 res = PyObject_GenericGetAttr(self, name);
3326 else
3327 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003328 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003329 PyErr_Clear();
3330 res = PyObject_CallFunction(getattr, "OO", self, name);
3331 }
3332 return res;
3333}
3334
Tim Peters6d6c1a32001-08-02 04:15:00 +00003335static int
3336slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3337{
3338 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003339 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003340
3341 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003342 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003343 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003344 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003345 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003346 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003347 if (res == NULL)
3348 return -1;
3349 Py_DECREF(res);
3350 return 0;
3351}
3352
3353/* Map rich comparison operators to their __xx__ namesakes */
3354static char *name_op[] = {
3355 "__lt__",
3356 "__le__",
3357 "__eq__",
3358 "__ne__",
3359 "__gt__",
3360 "__ge__",
3361};
3362
3363static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003364half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003365{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003366 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003367 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003368
Guido van Rossum60718732001-08-28 17:47:51 +00003369 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003370 if (func == NULL) {
3371 PyErr_Clear();
3372 Py_INCREF(Py_NotImplemented);
3373 return Py_NotImplemented;
3374 }
3375 args = Py_BuildValue("(O)", other);
3376 if (args == NULL)
3377 res = NULL;
3378 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003379 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003380 Py_DECREF(args);
3381 }
3382 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003383 return res;
3384}
3385
Guido van Rossumb8f63662001-08-15 23:57:02 +00003386/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3387static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3388
3389static PyObject *
3390slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3391{
3392 PyObject *res;
3393
3394 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3395 res = half_richcompare(self, other, op);
3396 if (res != Py_NotImplemented)
3397 return res;
3398 Py_DECREF(res);
3399 }
3400 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3401 res = half_richcompare(other, self, swapped_op[op]);
3402 if (res != Py_NotImplemented) {
3403 return res;
3404 }
3405 Py_DECREF(res);
3406 }
3407 Py_INCREF(Py_NotImplemented);
3408 return Py_NotImplemented;
3409}
3410
3411static PyObject *
3412slot_tp_iter(PyObject *self)
3413{
3414 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003415 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003416
Guido van Rossum60718732001-08-28 17:47:51 +00003417 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003418 if (func != NULL) {
3419 res = PyObject_CallObject(func, NULL);
3420 Py_DECREF(func);
3421 return res;
3422 }
3423 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003424 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003425 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00003426 PyErr_SetString(PyExc_TypeError,
3427 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003428 return NULL;
3429 }
3430 Py_DECREF(func);
3431 return PySeqIter_New(self);
3432}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003433
3434static PyObject *
3435slot_tp_iternext(PyObject *self)
3436{
Guido van Rossum2730b132001-08-28 18:22:14 +00003437 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003438 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003439}
3440
Guido van Rossum1a493502001-08-17 16:47:50 +00003441static PyObject *
3442slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3443{
3444 PyTypeObject *tp = self->ob_type;
3445 PyObject *get;
3446 static PyObject *get_str = NULL;
3447
3448 if (get_str == NULL) {
3449 get_str = PyString_InternFromString("__get__");
3450 if (get_str == NULL)
3451 return NULL;
3452 }
3453 get = _PyType_Lookup(tp, get_str);
3454 if (get == NULL) {
3455 /* Avoid further slowdowns */
3456 if (tp->tp_descr_get == slot_tp_descr_get)
3457 tp->tp_descr_get = NULL;
3458 Py_INCREF(self);
3459 return self;
3460 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003461 if (obj == NULL)
3462 obj = Py_None;
3463 if (type == NULL)
3464 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003465 return PyObject_CallFunction(get, "OOO", self, obj, type);
3466}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003467
3468static int
3469slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3470{
Guido van Rossum2c252392001-08-24 10:13:31 +00003471 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003472 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003473
3474 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003475 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003476 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003477 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003478 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003479 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003480 if (res == NULL)
3481 return -1;
3482 Py_DECREF(res);
3483 return 0;
3484}
3485
3486static int
3487slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3488{
Guido van Rossum60718732001-08-28 17:47:51 +00003489 static PyObject *init_str;
3490 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003491 PyObject *res;
3492
3493 if (meth == NULL)
3494 return -1;
3495 res = PyObject_Call(meth, args, kwds);
3496 Py_DECREF(meth);
3497 if (res == NULL)
3498 return -1;
3499 Py_DECREF(res);
3500 return 0;
3501}
3502
3503static PyObject *
3504slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3505{
3506 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3507 PyObject *newargs, *x;
3508 int i, n;
3509
3510 if (func == NULL)
3511 return NULL;
3512 assert(PyTuple_Check(args));
3513 n = PyTuple_GET_SIZE(args);
3514 newargs = PyTuple_New(n+1);
3515 if (newargs == NULL)
3516 return NULL;
3517 Py_INCREF(type);
3518 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3519 for (i = 0; i < n; i++) {
3520 x = PyTuple_GET_ITEM(args, i);
3521 Py_INCREF(x);
3522 PyTuple_SET_ITEM(newargs, i+1, x);
3523 }
3524 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003525 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003526 Py_DECREF(func);
3527 return x;
3528}
3529
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003530
3531/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3532 functions. The offsets here are relative to the 'etype' structure, which
3533 incorporates the additional structures used for numbers, sequences and
3534 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3535 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00003536 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
3537 terminated with an all-zero entry. (This table is further initialized and
3538 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003539
Guido van Rossum6d204072001-10-21 00:44:31 +00003540typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003541
3542#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003543#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003544#undef ETSLOT
3545#undef SQSLOT
3546#undef MPSLOT
3547#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003548#undef UNSLOT
3549#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003550#undef BINSLOT
3551#undef RBINSLOT
3552
Guido van Rossum6d204072001-10-21 00:44:31 +00003553#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3554 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003555#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3556 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3557 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003558#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3559 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3560#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3561 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3562#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3563 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3564#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3565 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3566#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3567 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3568 "x." NAME "() <==> " DOC)
3569#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3570 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3571 "x." NAME "(y) <==> x" DOC "y")
3572#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3573 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3574 "x." NAME "(y) <==> x" DOC "y")
3575#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3576 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3577 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003578
3579static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003580 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3581 "x.__len__() <==> len(x)"),
3582 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3583 "x.__add__(y) <==> x+y"),
3584 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3585 "x.__mul__(n) <==> x*n"),
3586 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3587 "x.__rmul__(n) <==> n*x"),
3588 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3589 "x.__getitem__(y) <==> x[y]"),
3590 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3591 "x.__getslice__(i, j) <==> x[i:j]"),
3592 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3593 "x.__setitem__(i, y) <==> x[i]=y"),
3594 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3595 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003596 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003597 wrap_intintobjargproc,
3598 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3599 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3600 "x.__delslice__(i, j) <==> del x[i:j]"),
3601 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3602 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003603 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003604 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003605 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003606 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003607
Guido van Rossum6d204072001-10-21 00:44:31 +00003608 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3609 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003610 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003611 wrap_binaryfunc,
3612 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003613 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003614 wrap_objobjargproc,
3615 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003616 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003617 wrap_delitem,
3618 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003619
Guido van Rossum6d204072001-10-21 00:44:31 +00003620 BINSLOT("__add__", nb_add, slot_nb_add,
3621 "+"),
3622 RBINSLOT("__radd__", nb_add, slot_nb_add,
3623 "+"),
3624 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3625 "-"),
3626 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3627 "-"),
3628 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3629 "*"),
3630 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3631 "*"),
3632 BINSLOT("__div__", nb_divide, slot_nb_divide,
3633 "/"),
3634 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3635 "/"),
3636 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3637 "%"),
3638 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3639 "%"),
3640 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3641 "divmod(x, y)"),
3642 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3643 "divmod(y, x)"),
3644 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3645 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3646 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3647 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3648 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3649 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3650 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3651 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00003652 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00003653 "x != 0"),
3654 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3655 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3656 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3657 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3658 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3659 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3660 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3661 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3662 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3663 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3664 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3665 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3666 "x.__coerce__(y) <==> coerce(x, y)"),
3667 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3668 "int(x)"),
3669 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3670 "long(x)"),
3671 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3672 "float(x)"),
3673 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3674 "oct(x)"),
3675 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3676 "hex(x)"),
3677 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3678 wrap_binaryfunc, "+"),
3679 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3680 wrap_binaryfunc, "-"),
3681 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3682 wrap_binaryfunc, "*"),
3683 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3684 wrap_binaryfunc, "/"),
3685 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3686 wrap_binaryfunc, "%"),
3687 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3688 wrap_ternaryfunc, "**"),
3689 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3690 wrap_binaryfunc, "<<"),
3691 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3692 wrap_binaryfunc, ">>"),
3693 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3694 wrap_binaryfunc, "&"),
3695 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3696 wrap_binaryfunc, "^"),
3697 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3698 wrap_binaryfunc, "|"),
3699 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3700 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3701 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3702 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3703 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3704 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3705 IBSLOT("__itruediv__", nb_inplace_true_divide,
3706 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003707
Guido van Rossum6d204072001-10-21 00:44:31 +00003708 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3709 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003710 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003711 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3712 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003713 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003714 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3715 "x.__cmp__(y) <==> cmp(x,y)"),
3716 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3717 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003718 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3719 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003720 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003721 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3722 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3723 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3724 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3725 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3726 "x.__setattr__('name', value) <==> x.name = value"),
3727 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3728 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3729 "x.__delattr__('name') <==> del x.name"),
3730 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3731 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3732 "x.__lt__(y) <==> x<y"),
3733 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3734 "x.__le__(y) <==> x<=y"),
3735 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3736 "x.__eq__(y) <==> x==y"),
3737 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3738 "x.__ne__(y) <==> x!=y"),
3739 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3740 "x.__gt__(y) <==> x>y"),
3741 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3742 "x.__ge__(y) <==> x>=y"),
3743 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3744 "x.__iter__() <==> iter(x)"),
3745 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3746 "x.next() -> the next value, or raise StopIteration"),
3747 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3748 "descr.__get__(obj[, type]) -> value"),
3749 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3750 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003751 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003752 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003753 "see x.__class__.__doc__ for signature",
3754 PyWrapperFlag_KEYWORDS),
3755 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003756 {NULL}
3757};
3758
Guido van Rossumc334df52002-04-04 23:44:47 +00003759/* Given a type pointer and an offset gotten from a slotdef entry, return a
3760 pointer to the actual slot. This is not quite the same as simply adding
3761 the offset to the type pointer, since it takes care to indirect through the
3762 proper indirection pointer (as_buffer, etc.); it returns NULL if the
3763 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003764static void **
3765slotptr(PyTypeObject *type, int offset)
3766{
3767 char *ptr;
3768
3769 assert(offset >= 0);
3770 assert(offset < offsetof(etype, as_buffer));
3771 if (offset >= offsetof(etype, as_mapping)) {
3772 ptr = (void *)type->tp_as_mapping;
3773 offset -= offsetof(etype, as_mapping);
3774 }
3775 else if (offset >= offsetof(etype, as_sequence)) {
3776 ptr = (void *)type->tp_as_sequence;
3777 offset -= offsetof(etype, as_sequence);
3778 }
3779 else if (offset >= offsetof(etype, as_number)) {
3780 ptr = (void *)type->tp_as_number;
3781 offset -= offsetof(etype, as_number);
3782 }
3783 else {
3784 ptr = (void *)type;
3785 }
3786 if (ptr != NULL)
3787 ptr += offset;
3788 return (void **)ptr;
3789}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003790
Guido van Rossumc334df52002-04-04 23:44:47 +00003791/* Length of array of slotdef pointers used to store slots with the
3792 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
3793 the same __name__, for any __name__. Since that's a static property, it is
3794 appropriate to declare fixed-size arrays for this. */
3795#define MAX_EQUIV 10
3796
3797/* Return a slot pointer for a given name, but ONLY if the attribute has
3798 exactly one slot function. The name must be an interned string. */
3799static void **
3800resolve_slotdups(PyTypeObject *type, PyObject *name)
3801{
3802 /* XXX Maybe this could be optimized more -- but is it worth it? */
3803
3804 /* pname and ptrs act as a little cache */
3805 static PyObject *pname;
3806 static slotdef *ptrs[MAX_EQUIV];
3807 slotdef *p, **pp;
3808 void **res, **ptr;
3809
3810 if (pname != name) {
3811 /* Collect all slotdefs that match name into ptrs. */
3812 pname = name;
3813 pp = ptrs;
3814 for (p = slotdefs; p->name_strobj; p++) {
3815 if (p->name_strobj == name)
3816 *pp++ = p;
3817 }
3818 *pp = NULL;
3819 }
3820
3821 /* Look in all matching slots of the type; if exactly one of these has
3822 a filled-in slot, return its value. Otherwise return NULL. */
3823 res = NULL;
3824 for (pp = ptrs; *pp; pp++) {
3825 ptr = slotptr(type, (*pp)->offset);
3826 if (ptr == NULL || *ptr == NULL)
3827 continue;
3828 if (res != NULL)
3829 return NULL;
3830 res = ptr;
3831 }
3832 return res;
3833}
3834
3835/* Common code for update_these_slots() and fixup_slot_dispatchers(). This
3836 does some incredibly complex thinking and then sticks something into the
3837 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
3838 interests, and then stores a generic wrapper or a specific function into
3839 the slot.) Return a pointer to the next slotdef with a different offset,
3840 because that's convenient for fixup_slot_dispatchers(). */
3841static slotdef *
3842update_one_slot(PyTypeObject *type, slotdef *p)
3843{
3844 PyObject *descr;
3845 PyWrapperDescrObject *d;
3846 void *generic = NULL, *specific = NULL;
3847 int use_generic = 0;
3848 int offset = p->offset;
3849 void **ptr = slotptr(type, offset);
3850
3851 if (ptr == NULL) {
3852 do {
3853 ++p;
3854 } while (p->offset == offset);
3855 return p;
3856 }
3857 do {
3858 descr = _PyType_Lookup(type, p->name_strobj);
3859 if (descr == NULL)
3860 continue;
3861 if (descr->ob_type == &PyWrapperDescr_Type) {
3862 void **tptr = resolve_slotdups(type, p->name_strobj);
3863 if (tptr == NULL || tptr == ptr)
3864 generic = p->function;
3865 d = (PyWrapperDescrObject *)descr;
3866 if (d->d_base->wrapper == p->wrapper &&
3867 PyType_IsSubtype(type, d->d_type))
3868 {
3869 if (specific == NULL ||
3870 specific == d->d_wrapped)
3871 specific = d->d_wrapped;
3872 else
3873 use_generic = 1;
3874 }
3875 }
3876 else {
3877 use_generic = 1;
3878 generic = p->function;
3879 }
3880 } while ((++p)->offset == offset);
3881 if (specific && !use_generic)
3882 *ptr = specific;
3883 else
3884 *ptr = generic;
3885 return p;
3886}
3887
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003888staticforward int recurse_down_subclasses(PyTypeObject *type,
3889 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003890
Guido van Rossumc334df52002-04-04 23:44:47 +00003891/* In the type, update the slots whose slotdefs are gathered in the pp0 array,
3892 and then do the same for all this type's subtypes. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003893static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003894update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003895{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003896 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003897
Guido van Rossumc334df52002-04-04 23:44:47 +00003898 for (pp = pp0; *pp; pp++)
3899 update_one_slot(type, *pp);
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003900 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003901}
3902
Guido van Rossumc334df52002-04-04 23:44:47 +00003903/* Update the slots whose slotdefs are gathered in the pp array in all (direct
3904 or indirect) subclasses of type. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003905static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003906recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003907{
3908 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003909 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003910 int i, n;
3911
3912 subclasses = type->tp_subclasses;
3913 if (subclasses == NULL)
3914 return 0;
3915 assert(PyList_Check(subclasses));
3916 n = PyList_GET_SIZE(subclasses);
3917 for (i = 0; i < n; i++) {
3918 ref = PyList_GET_ITEM(subclasses, i);
3919 assert(PyWeakref_CheckRef(ref));
3920 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
3921 if (subclass == NULL)
3922 continue;
3923 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003924 /* Avoid recursing down into unaffected classes */
3925 dict = subclass->tp_dict;
3926 if (dict != NULL && PyDict_Check(dict) &&
3927 PyDict_GetItem(dict, name) != NULL)
3928 continue;
3929 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003930 return -1;
3931 }
3932 return 0;
3933}
3934
Guido van Rossumc334df52002-04-04 23:44:47 +00003935/* Comparison function for qsort() to compare slotdefs by their offset, and
3936 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003937static int
3938slotdef_cmp(const void *aa, const void *bb)
3939{
3940 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
3941 int c = a->offset - b->offset;
3942 if (c != 0)
3943 return c;
3944 else
3945 return a - b;
3946}
3947
Guido van Rossumc334df52002-04-04 23:44:47 +00003948/* Initialize the slotdefs table by adding interned string objects for the
3949 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003950static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003951init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003952{
3953 slotdef *p;
3954 static int initialized = 0;
3955
3956 if (initialized)
3957 return;
3958 for (p = slotdefs; p->name; p++) {
3959 p->name_strobj = PyString_InternFromString(p->name);
3960 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00003961 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003962 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003963 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
3964 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003965 initialized = 1;
3966}
3967
Guido van Rossumc334df52002-04-04 23:44:47 +00003968/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003969static int
3970update_slot(PyTypeObject *type, PyObject *name)
3971{
Guido van Rossumc334df52002-04-04 23:44:47 +00003972 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003973 slotdef *p;
3974 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003975 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003976
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003977 init_slotdefs();
3978 pp = ptrs;
3979 for (p = slotdefs; p->name; p++) {
3980 /* XXX assume name is interned! */
3981 if (p->name_strobj == name)
3982 *pp++ = p;
3983 }
3984 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003985 for (pp = ptrs; *pp; pp++) {
3986 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003987 offset = p->offset;
3988 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003989 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003990 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003991 }
Guido van Rossumc334df52002-04-04 23:44:47 +00003992 if (ptrs[0] == NULL)
3993 return 0; /* Not an attribute that affects any slots */
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003994 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003995}
3996
Guido van Rossumc334df52002-04-04 23:44:47 +00003997/* Store the proper functions in the slot dispatches at class (type)
3998 definition time, based upon which operations the class overrides in its
3999 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004000static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004001fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004002{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004003 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004004
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004005 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00004006 for (p = slotdefs; p->name; )
4007 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004008}
Guido van Rossum705f0f52001-08-24 16:47:00 +00004009
Guido van Rossum6d204072001-10-21 00:44:31 +00004010/* This function is called by PyType_Ready() to populate the type's
4011 dictionary with method descriptors for function slots. For each
4012 function slot (like tp_repr) that's defined in the type, one or
4013 more corresponding descriptors are added in the type's tp_dict
4014 dictionary under the appropriate name (like __repr__). Some
4015 function slots cause more than one descriptor to be added (for
4016 example, the nb_add slot adds both __add__ and __radd__
4017 descriptors) and some function slots compete for the same
4018 descriptor (for example both sq_item and mp_subscript generate a
4019 __getitem__ descriptor). This only adds new descriptors and
4020 doesn't overwrite entries in tp_dict that were previously
4021 defined. The descriptors contain a reference to the C function
4022 they must call, so that it's safe if they are copied into a
4023 subtype's __dict__ and the subtype has a different C function in
4024 its slot -- calling the method defined by the descriptor will call
4025 the C function that was used to create it, rather than the C
4026 function present in the slot when it is called. (This is important
4027 because a subtype may have a C function in the slot that calls the
4028 method from the dictionary, and we want to avoid infinite recursion
4029 here.) */
4030
4031static int
4032add_operators(PyTypeObject *type)
4033{
4034 PyObject *dict = type->tp_dict;
4035 slotdef *p;
4036 PyObject *descr;
4037 void **ptr;
4038
4039 init_slotdefs();
4040 for (p = slotdefs; p->name; p++) {
4041 if (p->wrapper == NULL)
4042 continue;
4043 ptr = slotptr(type, p->offset);
4044 if (!ptr || !*ptr)
4045 continue;
4046 if (PyDict_GetItem(dict, p->name_strobj))
4047 continue;
4048 descr = PyDescr_NewWrapper(type, p, *ptr);
4049 if (descr == NULL)
4050 return -1;
4051 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
4052 return -1;
4053 Py_DECREF(descr);
4054 }
4055 if (type->tp_new != NULL) {
4056 if (add_tp_new_wrapper(type) < 0)
4057 return -1;
4058 }
4059 return 0;
4060}
4061
Guido van Rossum705f0f52001-08-24 16:47:00 +00004062
4063/* Cooperative 'super' */
4064
4065typedef struct {
4066 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00004067 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004068 PyObject *obj;
4069} superobject;
4070
Guido van Rossum6f799372001-09-20 20:46:19 +00004071static PyMemberDef super_members[] = {
4072 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
4073 "the class invoking super()"},
4074 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
4075 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004076 {0}
4077};
4078
Guido van Rossum705f0f52001-08-24 16:47:00 +00004079static void
4080super_dealloc(PyObject *self)
4081{
4082 superobject *su = (superobject *)self;
4083
Guido van Rossum048eb752001-10-02 21:24:57 +00004084 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004085 Py_XDECREF(su->obj);
4086 Py_XDECREF(su->type);
4087 self->ob_type->tp_free(self);
4088}
4089
4090static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004091super_repr(PyObject *self)
4092{
4093 superobject *su = (superobject *)self;
4094
4095 if (su->obj)
4096 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004097 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004098 su->type ? su->type->tp_name : "NULL",
4099 su->obj->ob_type->tp_name);
4100 else
4101 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004102 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004103 su->type ? su->type->tp_name : "NULL");
4104}
4105
4106static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00004107super_getattro(PyObject *self, PyObject *name)
4108{
4109 superobject *su = (superobject *)self;
4110
4111 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00004112 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00004113 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004114 descrgetfunc f;
4115 int i, n;
4116
Guido van Rossum155db9a2002-04-02 17:53:47 +00004117 starttype = su->obj->ob_type;
4118 mro = starttype->tp_mro;
4119
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004120 if (mro == NULL)
4121 n = 0;
4122 else {
4123 assert(PyTuple_Check(mro));
4124 n = PyTuple_GET_SIZE(mro);
4125 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004126 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00004127 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00004128 break;
4129 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004130 if (i >= n && PyType_Check(su->obj)) {
Guido van Rossum155db9a2002-04-02 17:53:47 +00004131 starttype = (PyTypeObject *)(su->obj);
4132 mro = starttype->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004133 if (mro == NULL)
4134 n = 0;
4135 else {
4136 assert(PyTuple_Check(mro));
4137 n = PyTuple_GET_SIZE(mro);
4138 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004139 for (i = 0; i < n; i++) {
4140 if ((PyObject *)(su->type) ==
4141 PyTuple_GET_ITEM(mro, i))
4142 break;
4143 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004144 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004145 i++;
4146 res = NULL;
4147 for (; i < n; i++) {
4148 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00004149 if (PyType_Check(tmp))
4150 dict = ((PyTypeObject *)tmp)->tp_dict;
4151 else if (PyClass_Check(tmp))
4152 dict = ((PyClassObject *)tmp)->cl_dict;
4153 else
4154 continue;
4155 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00004156 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00004157 Py_INCREF(res);
4158 f = res->ob_type->tp_descr_get;
4159 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004160 tmp = f(res, su->obj,
4161 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004162 Py_DECREF(res);
4163 res = tmp;
4164 }
4165 return res;
4166 }
4167 }
4168 }
4169 return PyObject_GenericGetAttr(self, name);
4170}
4171
Guido van Rossum5b443c62001-12-03 15:38:28 +00004172static int
4173supercheck(PyTypeObject *type, PyObject *obj)
4174{
4175 if (!PyType_IsSubtype(obj->ob_type, type) &&
4176 !(PyType_Check(obj) &&
4177 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4178 PyErr_SetString(PyExc_TypeError,
4179 "super(type, obj): "
4180 "obj must be an instance or subtype of type");
4181 return -1;
4182 }
4183 else
4184 return 0;
4185}
4186
Guido van Rossum705f0f52001-08-24 16:47:00 +00004187static PyObject *
4188super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4189{
4190 superobject *su = (superobject *)self;
4191 superobject *new;
4192
4193 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4194 /* Not binding to an object, or already bound */
4195 Py_INCREF(self);
4196 return self;
4197 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004198 if (su->ob_type != &PySuper_Type)
4199 /* If su is an instance of a subclass of super,
4200 call its type */
4201 return PyObject_CallFunction((PyObject *)su->ob_type,
4202 "OO", su->type, obj);
4203 else {
4204 /* Inline the common case */
4205 if (supercheck(su->type, obj) < 0)
4206 return NULL;
4207 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4208 NULL, NULL);
4209 if (new == NULL)
4210 return NULL;
4211 Py_INCREF(su->type);
4212 Py_INCREF(obj);
4213 new->type = su->type;
4214 new->obj = obj;
4215 return (PyObject *)new;
4216 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004217}
4218
4219static int
4220super_init(PyObject *self, PyObject *args, PyObject *kwds)
4221{
4222 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004223 PyTypeObject *type;
4224 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004225
4226 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4227 return -1;
4228 if (obj == Py_None)
4229 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004230 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004231 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004232 Py_INCREF(type);
4233 Py_XINCREF(obj);
4234 su->type = type;
4235 su->obj = obj;
4236 return 0;
4237}
4238
4239static char super_doc[] =
4240"super(type) -> unbound super object\n"
4241"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004242"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004243"Typical use to call a cooperative superclass method:\n"
4244"class C(B):\n"
4245" def meth(self, arg):\n"
4246" super(C, self).meth(arg)";
4247
Guido van Rossum048eb752001-10-02 21:24:57 +00004248static int
4249super_traverse(PyObject *self, visitproc visit, void *arg)
4250{
4251 superobject *su = (superobject *)self;
4252 int err;
4253
4254#define VISIT(SLOT) \
4255 if (SLOT) { \
4256 err = visit((PyObject *)(SLOT), arg); \
4257 if (err) \
4258 return err; \
4259 }
4260
4261 VISIT(su->obj);
4262 VISIT(su->type);
4263
4264#undef VISIT
4265
4266 return 0;
4267}
4268
Guido van Rossum705f0f52001-08-24 16:47:00 +00004269PyTypeObject PySuper_Type = {
4270 PyObject_HEAD_INIT(&PyType_Type)
4271 0, /* ob_size */
4272 "super", /* tp_name */
4273 sizeof(superobject), /* tp_basicsize */
4274 0, /* tp_itemsize */
4275 /* methods */
4276 super_dealloc, /* tp_dealloc */
4277 0, /* tp_print */
4278 0, /* tp_getattr */
4279 0, /* tp_setattr */
4280 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004281 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004282 0, /* tp_as_number */
4283 0, /* tp_as_sequence */
4284 0, /* tp_as_mapping */
4285 0, /* tp_hash */
4286 0, /* tp_call */
4287 0, /* tp_str */
4288 super_getattro, /* tp_getattro */
4289 0, /* tp_setattro */
4290 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004291 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4292 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004293 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004294 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004295 0, /* tp_clear */
4296 0, /* tp_richcompare */
4297 0, /* tp_weaklistoffset */
4298 0, /* tp_iter */
4299 0, /* tp_iternext */
4300 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004301 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004302 0, /* tp_getset */
4303 0, /* tp_base */
4304 0, /* tp_dict */
4305 super_descr_get, /* tp_descr_get */
4306 0, /* tp_descr_set */
4307 0, /* tp_dictoffset */
4308 super_init, /* tp_init */
4309 PyType_GenericAlloc, /* tp_alloc */
4310 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00004311 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004312};