blob: e2dace8dd738c692f12304c28f51872986509ff4 [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 Rossum9923ffe2002-06-04 19:52:53 +00006#include <ctype.h>
7
8/* The *real* layout of a type object when allocated on the heap */
9/* XXX Should we publish this in a header file? */
10typedef struct {
Guido van Rossum09638c12002-06-13 19:17:46 +000011 /* Note: there's a dependency on the order of these members
12 in slotptr() below. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +000013 PyTypeObject type;
14 PyNumberMethods as_number;
Guido van Rossum9923ffe2002-06-04 19:52:53 +000015 PyMappingMethods as_mapping;
Guido van Rossum09638c12002-06-13 19:17:46 +000016 PySequenceMethods as_sequence; /* as_sequence comes after as_mapping,
17 so that the mapping wins when both
18 the mapping and the sequence define
19 a given operator (e.g. __getitem__).
20 see add_operators() below. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +000021 PyBufferProcs as_buffer;
22 PyObject *name, *slots;
23 PyMemberDef members[1];
24} etype;
25
Guido van Rossum6f799372001-09-20 20:46:19 +000026static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +000027 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
28 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
29 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000030 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000031 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
32 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
33 {"__dictoffset__", T_LONG,
34 offsetof(PyTypeObject, tp_dictoffset), READONLY},
35 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
36 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
37 {0}
38};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000039
Guido van Rossumc0b618a1997-05-02 03:12:38 +000040static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000041type_name(PyTypeObject *type, void *context)
42{
43 char *s;
44
45 s = strrchr(type->tp_name, '.');
46 if (s == NULL)
47 s = type->tp_name;
48 else
49 s++;
50 return PyString_FromString(s);
51}
52
53static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000054type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000055{
Guido van Rossumc3542212001-08-16 09:18:56 +000056 PyObject *mod;
57 char *s;
58
59 s = strrchr(type->tp_name, '.');
60 if (s != NULL)
61 return PyString_FromStringAndSize(type->tp_name,
62 (int)(s - type->tp_name));
63 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
64 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000065 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000066 if (mod != NULL && PyString_Check(mod)) {
67 Py_INCREF(mod);
68 return mod;
69 }
70 PyErr_SetString(PyExc_AttributeError, "__module__");
71 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000072}
73
Guido van Rossum3926a632001-09-25 16:25:58 +000074static int
75type_set_module(PyTypeObject *type, PyObject *value, void *context)
76{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000077 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000078 strrchr(type->tp_name, '.')) {
79 PyErr_Format(PyExc_TypeError,
80 "can't set %s.__module__", type->tp_name);
81 return -1;
82 }
83 if (!value) {
84 PyErr_Format(PyExc_TypeError,
85 "can't delete %s.__module__", type->tp_name);
86 return -1;
87 }
88 return PyDict_SetItemString(type->tp_dict, "__module__", value);
89}
90
Tim Peters6d6c1a32001-08-02 04:15:00 +000091static PyObject *
92type_dict(PyTypeObject *type, void *context)
93{
94 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000095 Py_INCREF(Py_None);
96 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000097 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000098 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000099}
100
Tim Peters24008312002-03-17 18:56:20 +0000101static PyObject *
102type_get_doc(PyTypeObject *type, void *context)
103{
104 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000105 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000106 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000107 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000108 if (result == NULL) {
109 result = Py_None;
110 Py_INCREF(result);
111 }
112 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000113 result = result->ob_type->tp_descr_get(result, NULL,
114 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000115 }
116 else {
117 Py_INCREF(result);
118 }
Tim Peters24008312002-03-17 18:56:20 +0000119 return result;
120}
121
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000122static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +0000123 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000124 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000125 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000126 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000127 {0}
128};
129
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000130static int
131type_compare(PyObject *v, PyObject *w)
132{
133 /* This is called with type objects only. So we
134 can just compare the addresses. */
135 Py_uintptr_t vv = (Py_uintptr_t)v;
136 Py_uintptr_t ww = (Py_uintptr_t)w;
137 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
138}
139
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000140static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000141type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000142{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000143 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000144 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000145
146 mod = type_module(type, NULL);
147 if (mod == NULL)
148 PyErr_Clear();
149 else if (!PyString_Check(mod)) {
150 Py_DECREF(mod);
151 mod = NULL;
152 }
153 name = type_name(type, NULL);
154 if (name == NULL)
155 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000156
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000157 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
158 kind = "class";
159 else
160 kind = "type";
161
Barry Warsaw7ce36942001-08-24 18:34:26 +0000162 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000163 rtn = PyString_FromFormat("<%s '%s.%s'>",
164 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000165 PyString_AS_STRING(mod),
166 PyString_AS_STRING(name));
167 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000168 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000169 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000170
Guido van Rossumc3542212001-08-16 09:18:56 +0000171 Py_XDECREF(mod);
172 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000173 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000174}
175
Tim Peters6d6c1a32001-08-02 04:15:00 +0000176static PyObject *
177type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
178{
179 PyObject *obj;
180
181 if (type->tp_new == NULL) {
182 PyErr_Format(PyExc_TypeError,
183 "cannot create '%.100s' instances",
184 type->tp_name);
185 return NULL;
186 }
187
Tim Peters3f996e72001-09-13 19:18:27 +0000188 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000189 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000190 /* Ugly exception: when the call was type(something),
191 don't call tp_init on the result. */
192 if (type == &PyType_Type &&
193 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
194 (kwds == NULL ||
195 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
196 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000197 /* If the returned object is not an instance of type,
198 it won't be initialized. */
199 if (!PyType_IsSubtype(obj->ob_type, type))
200 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000201 type = obj->ob_type;
Jeremy Hylton719841e2002-07-16 19:39:38 +0000202 if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
203 type->tp_init != NULL &&
Tim Peters6d6c1a32001-08-02 04:15:00 +0000204 type->tp_init(obj, args, kwds) < 0) {
205 Py_DECREF(obj);
206 obj = NULL;
207 }
208 }
209 return obj;
210}
211
212PyObject *
213PyType_GenericAlloc(PyTypeObject *type, int nitems)
214{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000215 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000216 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000217
218 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000219 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000220 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000221 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000222
Neil Schemenauerc806c882001-08-29 23:54:54 +0000223 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000225
Neil Schemenauerc806c882001-08-29 23:54:54 +0000226 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000227
Tim Peters6d6c1a32001-08-02 04:15:00 +0000228 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
229 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000230
Tim Peters6d6c1a32001-08-02 04:15:00 +0000231 if (type->tp_itemsize == 0)
232 PyObject_INIT(obj, type);
233 else
234 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000235
Tim Peters6d6c1a32001-08-02 04:15:00 +0000236 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000237 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000238 return obj;
239}
240
241PyObject *
242PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
243{
244 return type->tp_alloc(type, 0);
245}
246
Guido van Rossum9475a232001-10-05 20:51:39 +0000247/* Helpers for subtyping */
248
249static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000250traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
251{
252 int i, n;
253 PyMemberDef *mp;
254
255 n = type->ob_size;
256 mp = ((etype *)type)->members;
257 for (i = 0; i < n; i++, mp++) {
258 if (mp->type == T_OBJECT_EX) {
259 char *addr = (char *)self + mp->offset;
260 PyObject *obj = *(PyObject **)addr;
261 if (obj != NULL) {
262 int err = visit(obj, arg);
263 if (err)
264 return err;
265 }
266 }
267 }
268 return 0;
269}
270
271static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000272subtype_traverse(PyObject *self, visitproc visit, void *arg)
273{
274 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000275 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000276
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000277 /* Find the nearest base with a different tp_traverse,
278 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000279 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000280 base = type;
281 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
282 if (base->ob_size) {
283 int err = traverse_slots(base, self, visit, arg);
284 if (err)
285 return err;
286 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000287 base = base->tp_base;
288 assert(base);
289 }
290
291 if (type->tp_dictoffset != base->tp_dictoffset) {
292 PyObject **dictptr = _PyObject_GetDictPtr(self);
293 if (dictptr && *dictptr) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000294 int err = visit(*dictptr, arg);
Guido van Rossum9475a232001-10-05 20:51:39 +0000295 if (err)
296 return err;
297 }
298 }
299
Guido van Rossuma3862092002-06-10 15:24:42 +0000300 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
301 /* For a heaptype, the instances count as references
302 to the type. Traverse the type so the collector
303 can find cycles involving this link. */
304 int err = visit((PyObject *)type, arg);
305 if (err)
306 return err;
307 }
308
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000309 if (basetraverse)
310 return basetraverse(self, visit, arg);
311 return 0;
312}
313
314static void
315clear_slots(PyTypeObject *type, PyObject *self)
316{
317 int i, n;
318 PyMemberDef *mp;
319
320 n = type->ob_size;
321 mp = ((etype *)type)->members;
322 for (i = 0; i < n; i++, mp++) {
323 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
324 char *addr = (char *)self + mp->offset;
325 PyObject *obj = *(PyObject **)addr;
326 if (obj != NULL) {
327 Py_DECREF(obj);
328 *(PyObject **)addr = NULL;
329 }
330 }
331 }
332}
333
334static int
335subtype_clear(PyObject *self)
336{
337 PyTypeObject *type, *base;
338 inquiry baseclear;
339
340 /* Find the nearest base with a different tp_clear
341 and clear slots while we're at it */
342 type = self->ob_type;
343 base = type;
344 while ((baseclear = base->tp_clear) == subtype_clear) {
345 if (base->ob_size)
346 clear_slots(base, self);
347 base = base->tp_base;
348 assert(base);
349 }
350
Guido van Rossuma3862092002-06-10 15:24:42 +0000351 /* There's no need to clear the instance dict (if any);
352 the collector will call its tp_clear handler. */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000353
354 if (baseclear)
355 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000356 return 0;
357}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000358
359static void
360subtype_dealloc(PyObject *self)
361{
Guido van Rossum14227b42001-12-06 02:35:58 +0000362 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000363 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000364
Guido van Rossum22b13872002-08-06 21:41:44 +0000365 /* Extract the type; we expect it to be a heap type */
366 type = self->ob_type;
367 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000368
Guido van Rossum22b13872002-08-06 21:41:44 +0000369 /* Test whether the type has GC exactly once */
370
371 if (!PyType_IS_GC(type)) {
372 /* It's really rare to find a dynamic type that doesn't have
373 GC; it can only happen when deriving from 'object' and not
374 adding any slots or instance variables. This allows
375 certain simplifications: there's no need to call
376 clear_slots(), or DECREF the dict, or clear weakrefs. */
377
378 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000379 if (type->tp_del) {
380 type->tp_del(self);
381 if (self->ob_refcnt > 0)
382 return;
383 }
Guido van Rossum22b13872002-08-06 21:41:44 +0000384
385 /* Find the nearest base with a different tp_dealloc */
386 base = type;
387 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
388 assert(base->ob_size == 0);
389 base = base->tp_base;
390 assert(base);
391 }
392
393 /* Call the base tp_dealloc() */
394 assert(basedealloc);
395 basedealloc(self);
396
397 /* Can't reference self beyond this point */
398 Py_DECREF(type);
399
400 /* Done */
401 return;
402 }
403
404 /* We get here only if the type has GC */
405
406 /* UnTrack and re-Track around the trashcan macro, alas */
Guido van Rossum0906e072002-08-07 20:42:09 +0000407 PyObject_GC_UnTrack(self);
Guido van Rossum22b13872002-08-06 21:41:44 +0000408 Py_TRASHCAN_SAFE_BEGIN(self);
409 _PyObject_GC_TRACK(self); /* We'll untrack for real later */
410
411 /* Maybe call finalizer; exit early if resurrected */
Guido van Rossumfebd61d2002-08-08 20:55:20 +0000412 if (type->tp_del) {
413 type->tp_del(self);
414 if (self->ob_refcnt > 0)
415 goto endlabel;
416 }
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000417
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000418 /* Find the nearest base with a different tp_dealloc
419 and clear slots while we're at it */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000420 base = type;
421 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
422 if (base->ob_size)
423 clear_slots(base, self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000424 base = base->tp_base;
425 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000426 }
427
Tim Peters6d6c1a32001-08-02 04:15:00 +0000428 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000429 if (type->tp_dictoffset && !base->tp_dictoffset) {
430 PyObject **dictptr = _PyObject_GetDictPtr(self);
431 if (dictptr != NULL) {
432 PyObject *dict = *dictptr;
433 if (dict != NULL) {
434 Py_DECREF(dict);
435 *dictptr = NULL;
436 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000437 }
438 }
439
Guido van Rossum9676b222001-08-17 20:32:36 +0000440 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000441 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000442 PyObject_ClearWeakRefs(self);
443
Tim Peters6d6c1a32001-08-02 04:15:00 +0000444 /* Finalize GC if the base doesn't do GC and we do */
Guido van Rossum22b13872002-08-06 21:41:44 +0000445 if (!PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000446 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000447
448 /* Call the base tp_dealloc() */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000449 assert(basedealloc);
450 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000451
452 /* Can't reference self beyond this point */
Guido van Rossum22b13872002-08-06 21:41:44 +0000453 Py_DECREF(type);
454
Guido van Rossum0906e072002-08-07 20:42:09 +0000455 endlabel:
Guido van Rossum22b13872002-08-06 21:41:44 +0000456 Py_TRASHCAN_SAFE_END(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000457}
458
Jeremy Hylton938ace62002-07-17 16:30:39 +0000459static PyTypeObject *solid_base(PyTypeObject *type);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000460
Tim Peters6d6c1a32001-08-02 04:15:00 +0000461/* type test with subclassing support */
462
463int
464PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
465{
466 PyObject *mro;
467
Guido van Rossum9478d072001-09-07 18:52:13 +0000468 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
469 return b == a || b == &PyBaseObject_Type;
470
Tim Peters6d6c1a32001-08-02 04:15:00 +0000471 mro = a->tp_mro;
472 if (mro != NULL) {
473 /* Deal with multiple inheritance without recursion
474 by walking the MRO tuple */
475 int i, n;
476 assert(PyTuple_Check(mro));
477 n = PyTuple_GET_SIZE(mro);
478 for (i = 0; i < n; i++) {
479 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
480 return 1;
481 }
482 return 0;
483 }
484 else {
485 /* a is not completely initilized yet; follow tp_base */
486 do {
487 if (a == b)
488 return 1;
489 a = a->tp_base;
490 } while (a != NULL);
491 return b == &PyBaseObject_Type;
492 }
493}
494
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000495/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000496 without looking in the instance dictionary
497 (so we can't use PyObject_GetAttr) but still binding
498 it to the instance. The arguments are the object,
499 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000500 static variable used to cache the interned Python string.
501
502 Two variants:
503
504 - lookup_maybe() returns NULL without raising an exception
505 when the _PyType_Lookup() call fails;
506
507 - lookup_method() always raises an exception upon errors.
508*/
Guido van Rossum60718732001-08-28 17:47:51 +0000509
510static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000511lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000512{
513 PyObject *res;
514
515 if (*attrobj == NULL) {
516 *attrobj = PyString_InternFromString(attrstr);
517 if (*attrobj == NULL)
518 return NULL;
519 }
520 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000521 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000522 descrgetfunc f;
523 if ((f = res->ob_type->tp_descr_get) == NULL)
524 Py_INCREF(res);
525 else
526 res = f(res, self, (PyObject *)(self->ob_type));
527 }
528 return res;
529}
530
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000531static PyObject *
532lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
533{
534 PyObject *res = lookup_maybe(self, attrstr, attrobj);
535 if (res == NULL && !PyErr_Occurred())
536 PyErr_SetObject(PyExc_AttributeError, *attrobj);
537 return res;
538}
539
Guido van Rossum2730b132001-08-28 18:22:14 +0000540/* A variation of PyObject_CallMethod that uses lookup_method()
541 instead of PyObject_GetAttrString(). This uses the same convention
542 as lookup_method to cache the interned name string object. */
543
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000544static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000545call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
546{
547 va_list va;
548 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000549 va_start(va, format);
550
Guido van Rossumda21c012001-10-03 00:50:18 +0000551 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000552 if (func == NULL) {
553 va_end(va);
554 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000555 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000556 return NULL;
557 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000558
559 if (format && *format)
560 args = Py_VaBuildValue(format, va);
561 else
562 args = PyTuple_New(0);
563
564 va_end(va);
565
566 if (args == NULL)
567 return NULL;
568
569 assert(PyTuple_Check(args));
570 retval = PyObject_Call(func, args, NULL);
571
572 Py_DECREF(args);
573 Py_DECREF(func);
574
575 return retval;
576}
577
578/* Clone of call_method() that returns NotImplemented when the lookup fails. */
579
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000580static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000581call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
582{
583 va_list va;
584 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000585 va_start(va, format);
586
Guido van Rossumda21c012001-10-03 00:50:18 +0000587 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000588 if (func == NULL) {
589 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000590 if (!PyErr_Occurred()) {
591 Py_INCREF(Py_NotImplemented);
592 return Py_NotImplemented;
593 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000594 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000595 }
596
597 if (format && *format)
598 args = Py_VaBuildValue(format, va);
599 else
600 args = PyTuple_New(0);
601
602 va_end(va);
603
Guido van Rossum717ce002001-09-14 16:58:08 +0000604 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000605 return NULL;
606
Guido van Rossum717ce002001-09-14 16:58:08 +0000607 assert(PyTuple_Check(args));
608 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000609
610 Py_DECREF(args);
611 Py_DECREF(func);
612
613 return retval;
614}
615
Tim Petersa91e9642001-11-14 23:32:33 +0000616static int
617fill_classic_mro(PyObject *mro, PyObject *cls)
618{
619 PyObject *bases, *base;
620 int i, n;
621
622 assert(PyList_Check(mro));
623 assert(PyClass_Check(cls));
624 i = PySequence_Contains(mro, cls);
625 if (i < 0)
626 return -1;
627 if (!i) {
628 if (PyList_Append(mro, cls) < 0)
629 return -1;
630 }
631 bases = ((PyClassObject *)cls)->cl_bases;
632 assert(bases && PyTuple_Check(bases));
633 n = PyTuple_GET_SIZE(bases);
634 for (i = 0; i < n; i++) {
635 base = PyTuple_GET_ITEM(bases, i);
636 if (fill_classic_mro(mro, base) < 0)
637 return -1;
638 }
639 return 0;
640}
641
642static PyObject *
643classic_mro(PyObject *cls)
644{
645 PyObject *mro;
646
647 assert(PyClass_Check(cls));
648 mro = PyList_New(0);
649 if (mro != NULL) {
650 if (fill_classic_mro(mro, cls) == 0)
651 return mro;
652 Py_DECREF(mro);
653 }
654 return NULL;
655}
656
Guido van Rossum1f121312002-11-14 19:49:16 +0000657/*
658 Method resolution order algorithm C3 described in
659 "A Monotonic Superclass Linearization for Dylan",
660 by Kim Barrett, Bob Cassel, Paul Haahr,
661 David A. Moon, Keith Playford, and P. Tucker Withington.
662 (OOPSLA 1996)
663
664 */
665
666static int
667tail_contains(PyObject *list, int whence, PyObject *o) {
668 int j, size;
669 size = PyList_GET_SIZE(list);
670
671 for (j = whence+1; j < size; j++) {
672 if (PyList_GET_ITEM(list, j) == o)
673 return 1;
674 }
675 return 0;
676}
677
678static int
679pmerge(PyObject *acc, PyObject* to_merge) {
680 int i, j, to_merge_size;
681 int *remain;
682 int ok, empty_cnt;
683
684 to_merge_size = PyList_GET_SIZE(to_merge);
685
686 remain = PyMem_MALLOC(SIZEOF_INT*to_merge_size);
687 if (remain == NULL)
688 return -1;
689 for (i = 0; i < to_merge_size; i++)
690 remain[i] = 0;
691
692 again:
693 empty_cnt = 0;
694 for (i = 0; i < to_merge_size; i++) {
695 PyObject *candidate;
696
697 PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
698
699 if (remain[i] >= PyList_GET_SIZE(cur_list)) {
700 empty_cnt++;
701 continue;
702 }
703
704 candidate = PyList_GET_ITEM(cur_list, remain[i]);
705 for (j = 0; j < to_merge_size; j++) {
706 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
707 if (tail_contains(j_lst, remain[j], candidate))
708 goto skip; /* continue outer loop */
709 }
710 ok = PyList_Append(acc, candidate);
711 if (ok < 0) {
712 PyMem_Free(remain);
713 return -1;
714 }
715 for (j = 0; j < to_merge_size; j++) {
716 PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
717 if (PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
718 remain[j]++;
719 }
720 }
721 goto again;
722 skip:
723 }
724
725 PyMem_FREE(remain);
726 if (empty_cnt == to_merge_size)
727 return 0;
728 PyErr_SetString(PyExc_TypeError, "MRO order disagreement");
729 return -1;
730}
731
Tim Peters6d6c1a32001-08-02 04:15:00 +0000732static PyObject *
733mro_implementation(PyTypeObject *type)
734{
735 int i, n, ok;
736 PyObject *bases, *result;
Guido van Rossum1f121312002-11-14 19:49:16 +0000737 PyObject *to_merge, *bases_aslist;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000738
Guido van Rossum63517572002-06-18 16:44:57 +0000739 if(type->tp_dict == NULL) {
740 if(PyType_Ready(type) < 0)
741 return NULL;
742 }
743
Tim Peters6d6c1a32001-08-02 04:15:00 +0000744 bases = type->tp_bases;
745 n = PyTuple_GET_SIZE(bases);
Guido van Rossum1f121312002-11-14 19:49:16 +0000746
747 to_merge = PyList_New(n+1);
748 if (to_merge == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000749 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +0000750
Tim Peters6d6c1a32001-08-02 04:15:00 +0000751 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000752 PyObject *base = PyTuple_GET_ITEM(bases, i);
753 PyObject *parentMRO;
754 if (PyType_Check(base))
755 parentMRO = PySequence_List(
756 ((PyTypeObject*)base)->tp_mro);
757 else
758 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000759 if (parentMRO == NULL) {
Guido van Rossum1f121312002-11-14 19:49:16 +0000760 Py_DECREF(to_merge);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000761 return NULL;
Guido van Rossum1f121312002-11-14 19:49:16 +0000762 }
763
764 PyList_SET_ITEM(to_merge, i, parentMRO);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000765 }
Guido van Rossum1f121312002-11-14 19:49:16 +0000766
767 bases_aslist = PySequence_List(bases);
768 if (bases_aslist == NULL) {
769 Py_DECREF(to_merge);
770 return NULL;
771 }
772 PyList_SET_ITEM(to_merge, n, bases_aslist);
773
774 result = Py_BuildValue("[O]", (PyObject *)type);
775 if (result == NULL) {
776 Py_DECREF(to_merge);
777 return NULL;
778 }
779
780 ok = pmerge(result, to_merge);
781 Py_DECREF(to_merge);
782 if (ok < 0) {
783 Py_DECREF(result);
784 return NULL;
785 }
786
Tim Peters6d6c1a32001-08-02 04:15:00 +0000787 return result;
788}
789
790static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000791mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000792{
793 PyTypeObject *type = (PyTypeObject *)self;
794
Tim Peters6d6c1a32001-08-02 04:15:00 +0000795 return mro_implementation(type);
796}
797
798static int
799mro_internal(PyTypeObject *type)
800{
801 PyObject *mro, *result, *tuple;
802
803 if (type->ob_type == &PyType_Type) {
804 result = mro_implementation(type);
805 }
806 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000807 static PyObject *mro_str;
808 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000809 if (mro == NULL)
810 return -1;
811 result = PyObject_CallObject(mro, NULL);
812 Py_DECREF(mro);
813 }
814 if (result == NULL)
815 return -1;
816 tuple = PySequence_Tuple(result);
817 Py_DECREF(result);
818 type->tp_mro = tuple;
819 return 0;
820}
821
822
823/* Calculate the best base amongst multiple base classes.
824 This is the first one that's on the path to the "solid base". */
825
826static PyTypeObject *
827best_base(PyObject *bases)
828{
829 int i, n;
830 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000831 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000832
833 assert(PyTuple_Check(bases));
834 n = PyTuple_GET_SIZE(bases);
835 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000836 base = NULL;
837 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000838 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000839 base_proto = PyTuple_GET_ITEM(bases, i);
840 if (PyClass_Check(base_proto))
841 continue;
842 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000843 PyErr_SetString(
844 PyExc_TypeError,
845 "bases must be types");
846 return NULL;
847 }
Tim Petersa91e9642001-11-14 23:32:33 +0000848 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000849 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000850 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000851 return NULL;
852 }
853 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000854 if (winner == NULL) {
855 winner = candidate;
856 base = base_i;
857 }
858 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000859 ;
860 else if (PyType_IsSubtype(candidate, winner)) {
861 winner = candidate;
862 base = base_i;
863 }
864 else {
865 PyErr_SetString(
866 PyExc_TypeError,
867 "multiple bases have "
868 "instance lay-out conflict");
869 return NULL;
870 }
871 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000872 if (base == NULL)
873 PyErr_SetString(PyExc_TypeError,
874 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000875 return base;
876}
877
878static int
879extra_ivars(PyTypeObject *type, PyTypeObject *base)
880{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000881 size_t t_size = type->tp_basicsize;
882 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000883
Guido van Rossum9676b222001-08-17 20:32:36 +0000884 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000885 if (type->tp_itemsize || base->tp_itemsize) {
886 /* If itemsize is involved, stricter rules */
887 return t_size != b_size ||
888 type->tp_itemsize != base->tp_itemsize;
889 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000890 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
891 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
892 t_size -= sizeof(PyObject *);
893 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
894 type->tp_dictoffset + sizeof(PyObject *) == t_size)
895 t_size -= sizeof(PyObject *);
896
897 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000898}
899
900static PyTypeObject *
901solid_base(PyTypeObject *type)
902{
903 PyTypeObject *base;
904
905 if (type->tp_base)
906 base = solid_base(type->tp_base);
907 else
908 base = &PyBaseObject_Type;
909 if (extra_ivars(type, base))
910 return type;
911 else
912 return base;
913}
914
Jeremy Hylton938ace62002-07-17 16:30:39 +0000915static void object_dealloc(PyObject *);
916static int object_init(PyObject *, PyObject *, PyObject *);
917static int update_slot(PyTypeObject *, PyObject *);
918static void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000919
920static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000921subtype_dict(PyObject *obj, void *context)
922{
923 PyObject **dictptr = _PyObject_GetDictPtr(obj);
924 PyObject *dict;
925
926 if (dictptr == NULL) {
927 PyErr_SetString(PyExc_AttributeError,
928 "This object has no __dict__");
929 return NULL;
930 }
931 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000932 if (dict == NULL)
933 *dictptr = dict = PyDict_New();
934 Py_XINCREF(dict);
935 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000936}
937
Guido van Rossum6661be32001-10-26 04:26:12 +0000938static int
939subtype_setdict(PyObject *obj, PyObject *value, void *context)
940{
941 PyObject **dictptr = _PyObject_GetDictPtr(obj);
942 PyObject *dict;
943
944 if (dictptr == NULL) {
945 PyErr_SetString(PyExc_AttributeError,
946 "This object has no __dict__");
947 return -1;
948 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000949 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000950 PyErr_SetString(PyExc_TypeError,
951 "__dict__ must be set to a dictionary");
952 return -1;
953 }
954 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000955 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000956 *dictptr = value;
957 Py_XDECREF(dict);
958 return 0;
959}
960
Guido van Rossumad47da02002-08-12 19:05:44 +0000961static PyObject *
962subtype_getweakref(PyObject *obj, void *context)
963{
964 PyObject **weaklistptr;
965 PyObject *result;
966
967 if (obj->ob_type->tp_weaklistoffset == 0) {
968 PyErr_SetString(PyExc_AttributeError,
969 "This object has no __weaklist__");
970 return NULL;
971 }
972 assert(obj->ob_type->tp_weaklistoffset > 0);
973 assert(obj->ob_type->tp_weaklistoffset + sizeof(PyObject *) <=
Guido van Rossum3747a0f2002-08-12 19:25:08 +0000974 (size_t)(obj->ob_type->tp_basicsize));
Guido van Rossumad47da02002-08-12 19:05:44 +0000975 weaklistptr = (PyObject **)
Guido van Rossum3747a0f2002-08-12 19:25:08 +0000976 ((char *)obj + obj->ob_type->tp_weaklistoffset);
Guido van Rossumad47da02002-08-12 19:05:44 +0000977 if (*weaklistptr == NULL)
978 result = Py_None;
979 else
980 result = *weaklistptr;
981 Py_INCREF(result);
982 return result;
983}
984
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000985static PyGetSetDef subtype_getsets[] = {
Guido van Rossumad47da02002-08-12 19:05:44 +0000986 /* Not all objects have these attributes!
987 The descriptor's __get__ method may raise AttributeError. */
988 {"__dict__", subtype_dict, subtype_setdict,
Neal Norwitz858e34f2002-08-13 17:18:45 +0000989 PyDoc_STR("dictionary for instance variables (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +0000990 {"__weakref__", subtype_getweakref, NULL,
Neal Norwitz858e34f2002-08-13 17:18:45 +0000991 PyDoc_STR("list of weak references to the object (if defined)")},
Guido van Rossumad47da02002-08-12 19:05:44 +0000992 {0}
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000993};
994
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000995/* bozo: __getstate__ that raises TypeError */
996
997static PyObject *
998bozo_func(PyObject *self, PyObject *args)
999{
1000 PyErr_SetString(PyExc_TypeError,
1001 "a class that defines __slots__ without "
1002 "defining __getstate__ cannot be pickled");
1003 return NULL;
1004}
1005
Neal Norwitz93c1e232002-03-31 16:06:11 +00001006static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS};
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001007
1008static PyObject *bozo_obj = NULL;
1009
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001010static int
1011valid_identifier(PyObject *s)
1012{
Guido van Rossum03013a02002-07-16 14:30:28 +00001013 unsigned char *p;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001014 int i, n;
1015
1016 if (!PyString_Check(s)) {
1017 PyErr_SetString(PyExc_TypeError,
1018 "__slots__ must be strings");
1019 return 0;
1020 }
Guido van Rossum03013a02002-07-16 14:30:28 +00001021 p = (unsigned char *) PyString_AS_STRING(s);
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001022 n = PyString_GET_SIZE(s);
1023 /* We must reject an empty name. As a hack, we bump the
1024 length to 1 so that the loop will balk on the trailing \0. */
1025 if (n == 0)
1026 n = 1;
1027 for (i = 0; i < n; i++, p++) {
1028 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1029 PyErr_SetString(PyExc_TypeError,
1030 "__slots__ must be identifiers");
1031 return 0;
1032 }
1033 }
1034 return 1;
1035}
1036
Martin v. Löwisd919a592002-10-14 21:07:28 +00001037#ifdef Py_USING_UNICODE
1038/* Replace Unicode objects in slots. */
1039
1040static PyObject *
1041_unicode_to_string(PyObject *slots, int nslots)
1042{
1043 PyObject *tmp = slots;
1044 PyObject *o, *o1;
1045 int i;
1046 intintargfunc copy = slots->ob_type->tp_as_sequence->sq_slice;
1047 for (i = 0; i < nslots; i++) {
1048 if (PyUnicode_Check(o = PyTuple_GET_ITEM(tmp, i))) {
1049 if (tmp == slots) {
1050 tmp = copy(slots, 0, PyTuple_GET_SIZE(slots));
1051 if (tmp == NULL)
1052 return NULL;
1053 }
1054 o1 = _PyUnicode_AsDefaultEncodedString
1055 (o, NULL);
1056 if (o1 == NULL) {
1057 Py_DECREF(tmp);
1058 return 0;
1059 }
1060 Py_INCREF(o1);
1061 Py_DECREF(o);
1062 PyTuple_SET_ITEM(tmp, i, o1);
1063 }
1064 }
1065 return tmp;
1066}
1067#endif
1068
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001069static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00001070type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
1071{
1072 PyObject *name, *bases, *dict;
1073 static char *kwlist[] = {"name", "bases", "dict", 0};
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001074 PyObject *slots, *tmp, *newslots;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001075 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001076 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +00001077 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001078 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Guido van Rossumad47da02002-08-12 19:05:44 +00001079 int j, may_add_dict, may_add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080
Tim Peters3abca122001-10-27 19:37:48 +00001081 assert(args != NULL && PyTuple_Check(args));
1082 assert(kwds == NULL || PyDict_Check(kwds));
1083
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001084 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +00001085 {
1086 const int nargs = PyTuple_GET_SIZE(args);
1087 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1088
1089 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1090 PyObject *x = PyTuple_GET_ITEM(args, 0);
1091 Py_INCREF(x->ob_type);
1092 return (PyObject *) x->ob_type;
1093 }
1094
1095 /* SF bug 475327 -- if that didn't trigger, we need 3
1096 arguments. but PyArg_ParseTupleAndKeywords below may give
1097 a msg saying type() needs exactly 3. */
1098 if (nargs + nkwds != 3) {
1099 PyErr_SetString(PyExc_TypeError,
1100 "type() takes 1 or 3 arguments");
1101 return NULL;
1102 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001103 }
1104
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001105 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001106 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1107 &name,
1108 &PyTuple_Type, &bases,
1109 &PyDict_Type, &dict))
1110 return NULL;
1111
1112 /* Determine the proper metatype to deal with this,
1113 and check for metatype conflicts while we're at it.
1114 Note that if some other metatype wins to contract,
1115 it's possible that its instances are not types. */
1116 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001117 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001118 for (i = 0; i < nbases; i++) {
1119 tmp = PyTuple_GET_ITEM(bases, i);
1120 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +00001121 if (tmptype == &PyClass_Type)
1122 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001123 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001124 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001125 if (PyType_IsSubtype(tmptype, winner)) {
1126 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001127 continue;
1128 }
1129 PyErr_SetString(PyExc_TypeError,
1130 "metatype conflict among bases");
1131 return NULL;
1132 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001133 if (winner != metatype) {
1134 if (winner->tp_new != type_new) /* Pass it to the winner */
1135 return winner->tp_new(winner, args, kwds);
1136 metatype = winner;
1137 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001138
1139 /* Adjust for empty tuple bases */
1140 if (nbases == 0) {
1141 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
1142 if (bases == NULL)
1143 return NULL;
1144 nbases = 1;
1145 }
1146 else
1147 Py_INCREF(bases);
1148
1149 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1150
1151 /* Calculate best base, and check that all bases are type objects */
1152 base = best_base(bases);
1153 if (base == NULL)
1154 return NULL;
1155 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1156 PyErr_Format(PyExc_TypeError,
1157 "type '%.100s' is not an acceptable base type",
1158 base->tp_name);
1159 return NULL;
1160 }
1161
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162 /* Check for a __slots__ sequence variable in dict, and count it */
1163 slots = PyDict_GetItemString(dict, "__slots__");
1164 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001165 add_dict = 0;
1166 add_weak = 0;
Guido van Rossumad47da02002-08-12 19:05:44 +00001167 may_add_dict = base->tp_dictoffset == 0;
1168 may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
1169 if (slots == NULL) {
1170 if (may_add_dict) {
1171 add_dict++;
1172 }
1173 if (may_add_weak) {
1174 add_weak++;
1175 }
1176 }
1177 else {
1178 /* Have slots */
1179
Tim Peters6d6c1a32001-08-02 04:15:00 +00001180 /* Make it into a tuple */
1181 if (PyString_Check(slots))
1182 slots = Py_BuildValue("(O)", slots);
1183 else
1184 slots = PySequence_Tuple(slots);
1185 if (slots == NULL)
1186 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001187 assert(PyTuple_Check(slots));
1188
1189 /* Are slots allowed? */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001190 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001191 if (nslots > 0 && base->tp_itemsize != 0) {
1192 PyErr_Format(PyExc_TypeError,
1193 "nonempty __slots__ "
1194 "not supported for subtype of '%s'",
1195 base->tp_name);
Guido van Rossumad47da02002-08-12 19:05:44 +00001196 bad_slots:
1197 Py_DECREF(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001198 return NULL;
1199 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001200
Martin v. Löwisd919a592002-10-14 21:07:28 +00001201#ifdef Py_USING_UNICODE
1202 tmp = _unicode_to_string(slots, nslots);
Martin v. Löwis13b1a5c2002-10-14 21:11:34 +00001203 if (tmp != slots) {
1204 Py_DECREF(slots);
1205 slots = tmp;
1206 }
Martin v. Löwisd919a592002-10-14 21:07:28 +00001207 if (!tmp)
1208 return NULL;
1209#endif
Guido van Rossumad47da02002-08-12 19:05:44 +00001210 /* Check for valid slot names and two special cases */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001211 for (i = 0; i < nslots; i++) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001212 PyObject *tmp = PyTuple_GET_ITEM(slots, i);
1213 char *s;
1214 if (!valid_identifier(tmp))
1215 goto bad_slots;
1216 assert(PyString_Check(tmp));
1217 s = PyString_AS_STRING(tmp);
1218 if (strcmp(s, "__dict__") == 0) {
1219 if (!may_add_dict || add_dict) {
1220 PyErr_SetString(PyExc_TypeError,
1221 "__dict__ slot disallowed: "
1222 "we already got one");
1223 goto bad_slots;
1224 }
1225 add_dict++;
1226 }
1227 if (strcmp(s, "__weakref__") == 0) {
1228 if (!may_add_weak || add_weak) {
1229 PyErr_SetString(PyExc_TypeError,
1230 "__weakref__ slot disallowed: "
1231 "either we already got one, "
1232 "or __itemsize__ != 0");
1233 goto bad_slots;
1234 }
1235 add_weak++;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001236 }
1237 }
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001238
Guido van Rossumad47da02002-08-12 19:05:44 +00001239 /* Copy slots into yet another tuple, demangling names */
1240 newslots = PyTuple_New(nslots - add_dict - add_weak);
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001241 if (newslots == NULL)
Guido van Rossumad47da02002-08-12 19:05:44 +00001242 goto bad_slots;
1243 for (i = j = 0; i < nslots; i++) {
1244 char *s;
Guido van Rossum8e829202002-08-16 03:47:49 +00001245 char buffer[256];
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001246 tmp = PyTuple_GET_ITEM(slots, i);
Guido van Rossumad47da02002-08-12 19:05:44 +00001247 s = PyString_AS_STRING(tmp);
1248 if ((add_dict && strcmp(s, "__dict__") == 0) ||
1249 (add_weak && strcmp(s, "__weakref__") == 0))
1250 continue;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001251 if (_Py_Mangle(PyString_AS_STRING(name),
Guido van Rossumad47da02002-08-12 19:05:44 +00001252 PyString_AS_STRING(tmp),
1253 buffer, sizeof(buffer)))
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001254 {
1255 tmp = PyString_FromString(buffer);
1256 } else {
1257 Py_INCREF(tmp);
1258 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001259 PyTuple_SET_ITEM(newslots, j, tmp);
1260 j++;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001261 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001262 assert(j == nslots - add_dict - add_weak);
1263 nslots = j;
Raymond Hettinger0ae0c072002-06-20 22:23:15 +00001264 Py_DECREF(slots);
1265 slots = newslots;
1266
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001267 /* See if *this* class defines __getstate__ */
Guido van Rossumad47da02002-08-12 19:05:44 +00001268 if (PyDict_GetItemString(dict, "__getstate__") == NULL) {
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001269 /* If not, provide a bozo that raises TypeError */
1270 if (bozo_obj == NULL) {
1271 bozo_obj = PyCFunction_New(&bozo_ml, NULL);
Guido van Rossumad47da02002-08-12 19:05:44 +00001272 if (bozo_obj == NULL)
1273 goto bad_slots;
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001274 }
1275 if (PyDict_SetItemString(dict,
1276 "__getstate__",
Guido van Rossumad47da02002-08-12 19:05:44 +00001277 bozo_obj) < 0)
1278 {
1279 Py_DECREF(bozo_obj);
1280 goto bad_slots;
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001281 }
1282 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001283
1284 /* Secondary bases may provide weakrefs or dict */
1285 if (nbases > 1 &&
1286 ((may_add_dict && !add_dict) ||
1287 (may_add_weak && !add_weak))) {
1288 for (i = 0; i < nbases; i++) {
1289 tmp = PyTuple_GET_ITEM(bases, i);
1290 if (tmp == (PyObject *)base)
1291 continue; /* Skip primary base */
1292 if (PyClass_Check(tmp)) {
1293 /* Classic base class provides both */
1294 if (may_add_dict && !add_dict)
1295 add_dict++;
1296 if (may_add_weak && !add_weak)
1297 add_weak++;
1298 break;
1299 }
1300 assert(PyType_Check(tmp));
1301 tmptype = (PyTypeObject *)tmp;
1302 if (may_add_dict && !add_dict &&
1303 tmptype->tp_dictoffset != 0)
1304 add_dict++;
1305 if (may_add_weak && !add_weak &&
1306 tmptype->tp_weaklistoffset != 0)
1307 add_weak++;
1308 if (may_add_dict && !add_dict)
1309 continue;
1310 if (may_add_weak && !add_weak)
1311 continue;
1312 /* Nothing more to check */
1313 break;
1314 }
1315 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001316 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001317
1318 /* XXX From here until type is safely allocated,
1319 "return NULL" may leak slots! */
1320
1321 /* Allocate the type object */
1322 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
Guido van Rossumad47da02002-08-12 19:05:44 +00001323 if (type == NULL) {
1324 Py_XDECREF(slots);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001325 return NULL;
Guido van Rossumad47da02002-08-12 19:05:44 +00001326 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001327
1328 /* Keep name and slots alive in the extended type object */
1329 et = (etype *)type;
1330 Py_INCREF(name);
1331 et->name = name;
1332 et->slots = slots;
1333
Guido van Rossumdc91b992001-08-08 22:26:22 +00001334 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001335 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1336 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001337 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1338 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001339
1340 /* It's a new-style number unless it specifically inherits any
1341 old-style numeric behavior */
1342 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1343 (base->tp_as_number == NULL))
1344 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1345
1346 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001347 type->tp_as_number = &et->as_number;
1348 type->tp_as_sequence = &et->as_sequence;
1349 type->tp_as_mapping = &et->as_mapping;
1350 type->tp_as_buffer = &et->as_buffer;
1351 type->tp_name = PyString_AS_STRING(name);
1352
1353 /* Set tp_base and tp_bases */
1354 type->tp_bases = bases;
1355 Py_INCREF(base);
1356 type->tp_base = base;
1357
Guido van Rossum687ae002001-10-15 22:03:32 +00001358 /* Initialize tp_dict from passed-in dict */
1359 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001360 if (dict == NULL) {
1361 Py_DECREF(type);
1362 return NULL;
1363 }
1364
Guido van Rossumc3542212001-08-16 09:18:56 +00001365 /* Set __module__ in the dict */
1366 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1367 tmp = PyEval_GetGlobals();
1368 if (tmp != NULL) {
1369 tmp = PyDict_GetItemString(tmp, "__name__");
1370 if (tmp != NULL) {
1371 if (PyDict_SetItemString(dict, "__module__",
1372 tmp) < 0)
1373 return NULL;
1374 }
1375 }
1376 }
1377
Tim Peters2f93e282001-10-04 05:27:00 +00001378 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001379 and is a string. The __doc__ accessor will first look for tp_doc;
1380 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001381 */
1382 {
1383 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1384 if (doc != NULL && PyString_Check(doc)) {
1385 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001386 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001387 if (type->tp_doc == NULL) {
1388 Py_DECREF(type);
1389 return NULL;
1390 }
1391 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1392 }
1393 }
1394
Tim Peters6d6c1a32001-08-02 04:15:00 +00001395 /* Special-case __new__: if it's a plain function,
1396 make it a static function */
1397 tmp = PyDict_GetItemString(dict, "__new__");
1398 if (tmp != NULL && PyFunction_Check(tmp)) {
1399 tmp = PyStaticMethod_New(tmp);
1400 if (tmp == NULL) {
1401 Py_DECREF(type);
1402 return NULL;
1403 }
1404 PyDict_SetItemString(dict, "__new__", tmp);
1405 Py_DECREF(tmp);
1406 }
1407
1408 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1409 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001410 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001411 if (slots != NULL) {
1412 for (i = 0; i < nslots; i++, mp++) {
1413 mp->name = PyString_AS_STRING(
1414 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001415 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001416 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001417 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001418 strcmp(mp->name, "__weakref__") == 0) {
Guido van Rossumad47da02002-08-12 19:05:44 +00001419 add_weak++;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001420 mp->type = T_OBJECT;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001421 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001422 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001423 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001424 slotoffset += sizeof(PyObject *);
1425 }
1426 }
Guido van Rossumad47da02002-08-12 19:05:44 +00001427 if (add_dict) {
1428 if (base->tp_itemsize)
1429 type->tp_dictoffset = -(long)sizeof(PyObject *);
1430 else
1431 type->tp_dictoffset = slotoffset;
1432 slotoffset += sizeof(PyObject *);
1433 }
1434 if (add_weak) {
1435 assert(!base->tp_itemsize);
1436 type->tp_weaklistoffset = slotoffset;
1437 slotoffset += sizeof(PyObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 }
1439 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001440 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001441 type->tp_members = et->members;
Guido van Rossumad47da02002-08-12 19:05:44 +00001442 type->tp_getset = subtype_getsets;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001443
1444 /* Special case some slots */
1445 if (type->tp_dictoffset != 0 || nslots > 0) {
1446 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1447 type->tp_getattro = PyObject_GenericGetAttr;
1448 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1449 type->tp_setattro = PyObject_GenericSetAttr;
1450 }
1451 type->tp_dealloc = subtype_dealloc;
1452
Guido van Rossum9475a232001-10-05 20:51:39 +00001453 /* Enable GC unless there are really no instance variables possible */
1454 if (!(type->tp_basicsize == sizeof(PyObject) &&
1455 type->tp_itemsize == 0))
1456 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1457
Tim Peters6d6c1a32001-08-02 04:15:00 +00001458 /* Always override allocation strategy to use regular heap */
1459 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001460 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001461 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001462 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001463 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00001464 }
1465 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001466 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001467
1468 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001469 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001470 Py_DECREF(type);
1471 return NULL;
1472 }
1473
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001474 /* Put the proper slots in place */
1475 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001476
Tim Peters6d6c1a32001-08-02 04:15:00 +00001477 return (PyObject *)type;
1478}
1479
1480/* Internal API to look for a name through the MRO.
1481 This returns a borrowed reference, and doesn't set an exception! */
1482PyObject *
1483_PyType_Lookup(PyTypeObject *type, PyObject *name)
1484{
1485 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001486 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001487
Guido van Rossum687ae002001-10-15 22:03:32 +00001488 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001489 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00001490
1491 /* If mro is NULL, the type is either not yet initialized
1492 by PyType_Ready(), or already cleared by type_clear().
1493 Either way the safest thing to do is to return NULL. */
1494 if (mro == NULL)
1495 return NULL;
1496
Tim Peters6d6c1a32001-08-02 04:15:00 +00001497 assert(PyTuple_Check(mro));
1498 n = PyTuple_GET_SIZE(mro);
1499 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001500 base = PyTuple_GET_ITEM(mro, i);
1501 if (PyClass_Check(base))
1502 dict = ((PyClassObject *)base)->cl_dict;
1503 else {
1504 assert(PyType_Check(base));
1505 dict = ((PyTypeObject *)base)->tp_dict;
1506 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001507 assert(dict && PyDict_Check(dict));
1508 res = PyDict_GetItem(dict, name);
1509 if (res != NULL)
1510 return res;
1511 }
1512 return NULL;
1513}
1514
1515/* This is similar to PyObject_GenericGetAttr(),
1516 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1517static PyObject *
1518type_getattro(PyTypeObject *type, PyObject *name)
1519{
1520 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001521 PyObject *meta_attribute, *attribute;
1522 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001523
1524 /* Initialize this type (we'll assume the metatype is initialized) */
1525 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001526 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001527 return NULL;
1528 }
1529
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001530 /* No readable descriptor found yet */
1531 meta_get = NULL;
Tim Peters34592512002-07-11 06:23:50 +00001532
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001533 /* Look for the attribute in the metatype */
1534 meta_attribute = _PyType_Lookup(metatype, name);
1535
1536 if (meta_attribute != NULL) {
1537 meta_get = meta_attribute->ob_type->tp_descr_get;
Tim Peters34592512002-07-11 06:23:50 +00001538
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001539 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
1540 /* Data descriptors implement tp_descr_set to intercept
1541 * writes. Assume the attribute is not overridden in
1542 * type's tp_dict (and bases): call the descriptor now.
1543 */
1544 return meta_get(meta_attribute, (PyObject *)type,
1545 (PyObject *)metatype);
1546 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001547 }
1548
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001549 /* No data descriptor found on metatype. Look in tp_dict of this
1550 * type and its bases */
1551 attribute = _PyType_Lookup(type, name);
1552 if (attribute != NULL) {
1553 /* Implement descriptor functionality, if any */
1554 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
1555 if (local_get != NULL) {
1556 /* NULL 2nd argument indicates the descriptor was
1557 * found on the target object itself (or a base) */
1558 return local_get(attribute, (PyObject *)NULL,
1559 (PyObject *)type);
1560 }
Tim Peters34592512002-07-11 06:23:50 +00001561
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001562 Py_INCREF(attribute);
1563 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001564 }
1565
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001566 /* No attribute found in local __dict__ (or bases): use the
1567 * descriptor from the metatype, if any */
1568 if (meta_get != NULL)
1569 return meta_get(meta_attribute, (PyObject *)type,
1570 (PyObject *)metatype);
1571
1572 /* If an ordinary attribute was found on the metatype, return it now */
1573 if (meta_attribute != NULL) {
1574 Py_INCREF(meta_attribute);
1575 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001576 }
1577
1578 /* Give up */
1579 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001580 "type object '%.50s' has no attribute '%.400s'",
1581 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001582 return NULL;
1583}
1584
1585static int
1586type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1587{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001588 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1589 PyErr_Format(
1590 PyExc_TypeError,
1591 "can't set attributes of built-in/extension type '%s'",
1592 type->tp_name);
1593 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001594 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001595 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1596 return -1;
1597 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001598}
1599
1600static void
1601type_dealloc(PyTypeObject *type)
1602{
1603 etype *et;
1604
1605 /* Assert this is a heap-allocated type object */
1606 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001607 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001608 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001609 et = (etype *)type;
1610 Py_XDECREF(type->tp_base);
1611 Py_XDECREF(type->tp_dict);
1612 Py_XDECREF(type->tp_bases);
1613 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001614 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001615 Py_XDECREF(type->tp_subclasses);
Neal Norwitzcee5ca02002-07-30 00:42:06 +00001616 PyObject_Free(type->tp_doc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001617 Py_XDECREF(et->name);
1618 Py_XDECREF(et->slots);
1619 type->ob_type->tp_free((PyObject *)type);
1620}
1621
Guido van Rossum1c450732001-10-08 15:18:27 +00001622static PyObject *
1623type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1624{
1625 PyObject *list, *raw, *ref;
1626 int i, n;
1627
1628 list = PyList_New(0);
1629 if (list == NULL)
1630 return NULL;
1631 raw = type->tp_subclasses;
1632 if (raw == NULL)
1633 return list;
1634 assert(PyList_Check(raw));
1635 n = PyList_GET_SIZE(raw);
1636 for (i = 0; i < n; i++) {
1637 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001638 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001639 ref = PyWeakref_GET_OBJECT(ref);
1640 if (ref != Py_None) {
1641 if (PyList_Append(list, ref) < 0) {
1642 Py_DECREF(list);
1643 return NULL;
1644 }
1645 }
1646 }
1647 return list;
1648}
1649
Tim Peters6d6c1a32001-08-02 04:15:00 +00001650static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001651 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00001652 PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
Guido van Rossum1c450732001-10-08 15:18:27 +00001653 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00001654 PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001655 {0}
1656};
1657
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001658PyDoc_STRVAR(type_doc,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001659"type(object) -> the object's type\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001660"type(name, bases, dict) -> a new type");
Tim Peters6d6c1a32001-08-02 04:15:00 +00001661
Guido van Rossum048eb752001-10-02 21:24:57 +00001662static int
1663type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1664{
Guido van Rossum048eb752001-10-02 21:24:57 +00001665 int err;
1666
Guido van Rossuma3862092002-06-10 15:24:42 +00001667 /* Because of type_is_gc(), the collector only calls this
1668 for heaptypes. */
1669 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001670
1671#define VISIT(SLOT) \
1672 if (SLOT) { \
1673 err = visit((PyObject *)(SLOT), arg); \
1674 if (err) \
1675 return err; \
1676 }
1677
1678 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001679 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001680 VISIT(type->tp_mro);
1681 VISIT(type->tp_bases);
1682 VISIT(type->tp_base);
Guido van Rossuma3862092002-06-10 15:24:42 +00001683
1684 /* There's no need to visit type->tp_subclasses or
1685 ((etype *)type)->slots, because they can't be involved
1686 in cycles; tp_subclasses is a list of weak references,
1687 and slots is a tuple of strings. */
Guido van Rossum048eb752001-10-02 21:24:57 +00001688
1689#undef VISIT
1690
1691 return 0;
1692}
1693
1694static int
1695type_clear(PyTypeObject *type)
1696{
Guido van Rossum048eb752001-10-02 21:24:57 +00001697 PyObject *tmp;
1698
Guido van Rossuma3862092002-06-10 15:24:42 +00001699 /* Because of type_is_gc(), the collector only calls this
1700 for heaptypes. */
1701 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001702
1703#define CLEAR(SLOT) \
1704 if (SLOT) { \
1705 tmp = (PyObject *)(SLOT); \
1706 SLOT = NULL; \
1707 Py_DECREF(tmp); \
1708 }
1709
Guido van Rossuma3862092002-06-10 15:24:42 +00001710 /* The only field we need to clear is tp_mro, which is part of a
1711 hard cycle (its first element is the class itself) that won't
1712 be broken otherwise (it's a tuple and tuples don't have a
1713 tp_clear handler). None of the other fields need to be
1714 cleared, and here's why:
Guido van Rossum048eb752001-10-02 21:24:57 +00001715
Guido van Rossuma3862092002-06-10 15:24:42 +00001716 tp_dict:
1717 It is a dict, so the collector will call its tp_clear.
1718
1719 tp_cache:
1720 Not used; if it were, it would be a dict.
1721
1722 tp_bases, tp_base:
1723 If these are involved in a cycle, there must be at least
1724 one other, mutable object in the cycle, e.g. a base
1725 class's dict; the cycle will be broken that way.
1726
1727 tp_subclasses:
1728 A list of weak references can't be part of a cycle; and
1729 lists have their own tp_clear.
1730
1731 slots (in etype):
1732 A tuple of strings can't be part of a cycle.
1733 */
1734
1735 CLEAR(type->tp_mro);
Tim Peters2f93e282001-10-04 05:27:00 +00001736
Guido van Rossum048eb752001-10-02 21:24:57 +00001737#undef CLEAR
1738
1739 return 0;
1740}
1741
1742static int
1743type_is_gc(PyTypeObject *type)
1744{
1745 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1746}
1747
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001748PyTypeObject PyType_Type = {
1749 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001750 0, /* ob_size */
1751 "type", /* tp_name */
1752 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001753 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001754 (destructor)type_dealloc, /* tp_dealloc */
1755 0, /* tp_print */
1756 0, /* tp_getattr */
1757 0, /* tp_setattr */
1758 type_compare, /* tp_compare */
1759 (reprfunc)type_repr, /* tp_repr */
1760 0, /* tp_as_number */
1761 0, /* tp_as_sequence */
1762 0, /* tp_as_mapping */
1763 (hashfunc)_Py_HashPointer, /* tp_hash */
1764 (ternaryfunc)type_call, /* tp_call */
1765 0, /* tp_str */
1766 (getattrofunc)type_getattro, /* tp_getattro */
1767 (setattrofunc)type_setattro, /* tp_setattro */
1768 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001769 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1770 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001771 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001772 (traverseproc)type_traverse, /* tp_traverse */
1773 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001774 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001775 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001776 0, /* tp_iter */
1777 0, /* tp_iternext */
1778 type_methods, /* tp_methods */
1779 type_members, /* tp_members */
1780 type_getsets, /* tp_getset */
1781 0, /* tp_base */
1782 0, /* tp_dict */
1783 0, /* tp_descr_get */
1784 0, /* tp_descr_set */
1785 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1786 0, /* tp_init */
1787 0, /* tp_alloc */
1788 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001789 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00001790 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001791};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001792
1793
1794/* The base type of all types (eventually)... except itself. */
1795
1796static int
1797object_init(PyObject *self, PyObject *args, PyObject *kwds)
1798{
1799 return 0;
1800}
1801
1802static void
1803object_dealloc(PyObject *self)
1804{
1805 self->ob_type->tp_free(self);
1806}
1807
Guido van Rossum8e248182001-08-12 05:17:56 +00001808static PyObject *
1809object_repr(PyObject *self)
1810{
Guido van Rossum76e69632001-08-16 18:52:43 +00001811 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001812 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001813
Guido van Rossum76e69632001-08-16 18:52:43 +00001814 type = self->ob_type;
1815 mod = type_module(type, NULL);
1816 if (mod == NULL)
1817 PyErr_Clear();
1818 else if (!PyString_Check(mod)) {
1819 Py_DECREF(mod);
1820 mod = NULL;
1821 }
1822 name = type_name(type, NULL);
1823 if (name == NULL)
1824 return NULL;
1825 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001826 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001827 PyString_AS_STRING(mod),
1828 PyString_AS_STRING(name),
1829 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001830 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001831 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001832 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001833 Py_XDECREF(mod);
1834 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001835 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001836}
1837
Guido van Rossumb8f63662001-08-15 23:57:02 +00001838static PyObject *
1839object_str(PyObject *self)
1840{
1841 unaryfunc f;
1842
1843 f = self->ob_type->tp_repr;
1844 if (f == NULL)
1845 f = object_repr;
1846 return f(self);
1847}
1848
Guido van Rossum8e248182001-08-12 05:17:56 +00001849static long
1850object_hash(PyObject *self)
1851{
1852 return _Py_HashPointer(self);
1853}
Guido van Rossum8e248182001-08-12 05:17:56 +00001854
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001855static PyObject *
1856object_get_class(PyObject *self, void *closure)
1857{
1858 Py_INCREF(self->ob_type);
1859 return (PyObject *)(self->ob_type);
1860}
1861
1862static int
1863equiv_structs(PyTypeObject *a, PyTypeObject *b)
1864{
1865 return a == b ||
1866 (a != NULL &&
1867 b != NULL &&
1868 a->tp_basicsize == b->tp_basicsize &&
1869 a->tp_itemsize == b->tp_itemsize &&
1870 a->tp_dictoffset == b->tp_dictoffset &&
1871 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1872 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1873 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1874}
1875
1876static int
1877same_slots_added(PyTypeObject *a, PyTypeObject *b)
1878{
1879 PyTypeObject *base = a->tp_base;
1880 int size;
1881
1882 if (base != b->tp_base)
1883 return 0;
1884 if (equiv_structs(a, base) && equiv_structs(b, base))
1885 return 1;
1886 size = base->tp_basicsize;
1887 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1888 size += sizeof(PyObject *);
1889 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1890 size += sizeof(PyObject *);
1891 return size == a->tp_basicsize && size == b->tp_basicsize;
1892}
1893
1894static int
1895object_set_class(PyObject *self, PyObject *value, void *closure)
1896{
1897 PyTypeObject *old = self->ob_type;
1898 PyTypeObject *new, *newbase, *oldbase;
1899
Guido van Rossumb6b89422002-04-15 01:03:30 +00001900 if (value == NULL) {
1901 PyErr_SetString(PyExc_TypeError,
1902 "can't delete __class__ attribute");
1903 return -1;
1904 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001905 if (!PyType_Check(value)) {
1906 PyErr_Format(PyExc_TypeError,
1907 "__class__ must be set to new-style class, not '%s' object",
1908 value->ob_type->tp_name);
1909 return -1;
1910 }
1911 new = (PyTypeObject *)value;
Guido van Rossum40af8892002-08-10 05:42:07 +00001912 if (!(new->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
1913 !(old->tp_flags & Py_TPFLAGS_HEAPTYPE))
1914 {
1915 PyErr_Format(PyExc_TypeError,
1916 "__class__ assignment: only for heap types");
1917 return -1;
1918 }
Guido van Rossum9ee4b942002-05-24 18:47:47 +00001919 if (new->tp_dealloc != old->tp_dealloc ||
1920 new->tp_free != old->tp_free)
1921 {
1922 PyErr_Format(PyExc_TypeError,
1923 "__class__ assignment: "
1924 "'%s' deallocator differs from '%s'",
1925 new->tp_name,
1926 old->tp_name);
1927 return -1;
1928 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001929 newbase = new;
1930 oldbase = old;
1931 while (equiv_structs(newbase, newbase->tp_base))
1932 newbase = newbase->tp_base;
1933 while (equiv_structs(oldbase, oldbase->tp_base))
1934 oldbase = oldbase->tp_base;
1935 if (newbase != oldbase &&
1936 (newbase->tp_base != oldbase->tp_base ||
1937 !same_slots_added(newbase, oldbase))) {
1938 PyErr_Format(PyExc_TypeError,
1939 "__class__ assignment: "
1940 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001941 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001942 old->tp_name);
1943 return -1;
1944 }
Guido van Rossum40af8892002-08-10 05:42:07 +00001945 Py_INCREF(new);
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001946 self->ob_type = new;
Guido van Rossum40af8892002-08-10 05:42:07 +00001947 Py_DECREF(old);
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001948 return 0;
1949}
1950
1951static PyGetSetDef object_getsets[] = {
1952 {"__class__", object_get_class, object_set_class,
Neal Norwitz858e34f2002-08-13 17:18:45 +00001953 PyDoc_STR("the object's class")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001954 {0}
1955};
1956
Guido van Rossum3926a632001-09-25 16:25:58 +00001957static PyObject *
1958object_reduce(PyObject *self, PyObject *args)
1959{
1960 /* Call copy_reg._reduce(self) */
1961 static PyObject *copy_reg_str;
1962 PyObject *copy_reg, *res;
1963
1964 if (!copy_reg_str) {
1965 copy_reg_str = PyString_InternFromString("copy_reg");
1966 if (copy_reg_str == NULL)
1967 return NULL;
1968 }
1969 copy_reg = PyImport_Import(copy_reg_str);
1970 if (!copy_reg)
1971 return NULL;
1972 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1973 Py_DECREF(copy_reg);
1974 return res;
1975}
1976
1977static PyMethodDef object_methods[] = {
Neal Norwitz5dc2a372002-08-13 22:19:13 +00001978 {"__reduce__", object_reduce, METH_NOARGS,
1979 PyDoc_STR("helper for pickle")},
Guido van Rossum3926a632001-09-25 16:25:58 +00001980 {0}
1981};
1982
Tim Peters6d6c1a32001-08-02 04:15:00 +00001983PyTypeObject PyBaseObject_Type = {
1984 PyObject_HEAD_INIT(&PyType_Type)
1985 0, /* ob_size */
1986 "object", /* tp_name */
1987 sizeof(PyObject), /* tp_basicsize */
1988 0, /* tp_itemsize */
1989 (destructor)object_dealloc, /* tp_dealloc */
1990 0, /* tp_print */
1991 0, /* tp_getattr */
1992 0, /* tp_setattr */
1993 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001994 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001995 0, /* tp_as_number */
1996 0, /* tp_as_sequence */
1997 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001998 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001999 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00002000 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002001 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002002 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003 0, /* tp_as_buffer */
2004 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
Neal Norwitz5dc2a372002-08-13 22:19:13 +00002005 PyDoc_STR("The most base type"), /* tp_doc */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002006 0, /* tp_traverse */
2007 0, /* tp_clear */
2008 0, /* tp_richcompare */
2009 0, /* tp_weaklistoffset */
2010 0, /* tp_iter */
2011 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00002012 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00002013 0, /* tp_members */
2014 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002015 0, /* tp_base */
2016 0, /* tp_dict */
2017 0, /* tp_descr_get */
2018 0, /* tp_descr_set */
2019 0, /* tp_dictoffset */
2020 object_init, /* tp_init */
2021 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00002022 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00002023 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002024};
2025
2026
2027/* Initialize the __dict__ in a type object */
2028
Fred Drake7bf97152002-03-28 05:33:33 +00002029static PyObject *
2030create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *))
2031{
2032 PyObject *cfunc;
2033 PyObject *result;
2034
2035 cfunc = PyCFunction_New(meth, NULL);
2036 if (cfunc == NULL)
2037 return NULL;
2038 result = func(cfunc);
2039 Py_DECREF(cfunc);
2040 return result;
2041}
2042
Tim Peters6d6c1a32001-08-02 04:15:00 +00002043static int
2044add_methods(PyTypeObject *type, PyMethodDef *meth)
2045{
Guido van Rossum687ae002001-10-15 22:03:32 +00002046 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002047
2048 for (; meth->ml_name != NULL; meth++) {
2049 PyObject *descr;
2050 if (PyDict_GetItemString(dict, meth->ml_name))
2051 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00002052 if (meth->ml_flags & METH_CLASS) {
2053 if (meth->ml_flags & METH_STATIC) {
2054 PyErr_SetString(PyExc_ValueError,
2055 "method cannot be both class and static");
2056 return -1;
2057 }
2058 descr = create_specialmethod(meth, PyClassMethod_New);
2059 }
2060 else if (meth->ml_flags & METH_STATIC) {
2061 descr = create_specialmethod(meth, PyStaticMethod_New);
2062 }
2063 else {
2064 descr = PyDescr_NewMethod(type, meth);
2065 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002066 if (descr == NULL)
2067 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00002068 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002069 return -1;
2070 Py_DECREF(descr);
2071 }
2072 return 0;
2073}
2074
2075static int
Guido van Rossum6f799372001-09-20 20:46:19 +00002076add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002077{
Guido van Rossum687ae002001-10-15 22:03:32 +00002078 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002079
2080 for (; memb->name != NULL; memb++) {
2081 PyObject *descr;
2082 if (PyDict_GetItemString(dict, memb->name))
2083 continue;
2084 descr = PyDescr_NewMember(type, memb);
2085 if (descr == NULL)
2086 return -1;
2087 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
2088 return -1;
2089 Py_DECREF(descr);
2090 }
2091 return 0;
2092}
2093
2094static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00002095add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002096{
Guido van Rossum687ae002001-10-15 22:03:32 +00002097 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002098
2099 for (; gsp->name != NULL; gsp++) {
2100 PyObject *descr;
2101 if (PyDict_GetItemString(dict, gsp->name))
2102 continue;
2103 descr = PyDescr_NewGetSet(type, gsp);
2104
2105 if (descr == NULL)
2106 return -1;
2107 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
2108 return -1;
2109 Py_DECREF(descr);
2110 }
2111 return 0;
2112}
2113
Guido van Rossum13d52f02001-08-10 21:24:08 +00002114static void
2115inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116{
2117 int oldsize, newsize;
2118
Guido van Rossum13d52f02001-08-10 21:24:08 +00002119 /* Special flag magic */
2120 if (!type->tp_as_buffer && base->tp_as_buffer) {
2121 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
2122 type->tp_flags |=
2123 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
2124 }
2125 if (!type->tp_as_sequence && base->tp_as_sequence) {
2126 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
2127 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
2128 }
2129 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
2130 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
2131 if ((!type->tp_as_number && base->tp_as_number) ||
2132 (!type->tp_as_sequence && base->tp_as_sequence)) {
2133 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
2134 if (!type->tp_as_number && !type->tp_as_sequence) {
2135 type->tp_flags |= base->tp_flags &
2136 Py_TPFLAGS_HAVE_INPLACEOPS;
2137 }
2138 }
2139 /* Wow */
2140 }
2141 if (!type->tp_as_number && base->tp_as_number) {
2142 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
2143 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
2144 }
2145
2146 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00002147 oldsize = base->tp_basicsize;
2148 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
2149 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
2150 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00002151 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
2152 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00002153 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00002154 if (type->tp_traverse == NULL)
2155 type->tp_traverse = base->tp_traverse;
2156 if (type->tp_clear == NULL)
2157 type->tp_clear = base->tp_clear;
2158 }
2159 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00002160 /* The condition below could use some explanation.
2161 It appears that tp_new is not inherited for static types
2162 whose base class is 'object'; this seems to be a precaution
2163 so that old extension types don't suddenly become
2164 callable (object.__new__ wouldn't insure the invariants
2165 that the extension type's own factory function ensures).
2166 Heap types, of course, are under our control, so they do
2167 inherit tp_new; static extension types that specify some
2168 other built-in type as the default are considered
2169 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00002170 if (base != &PyBaseObject_Type ||
2171 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2172 if (type->tp_new == NULL)
2173 type->tp_new = base->tp_new;
2174 }
2175 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00002176 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00002177
2178 /* Copy other non-function slots */
2179
2180#undef COPYVAL
2181#define COPYVAL(SLOT) \
2182 if (type->SLOT == 0) type->SLOT = base->SLOT
2183
2184 COPYVAL(tp_itemsize);
2185 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
2186 COPYVAL(tp_weaklistoffset);
2187 }
2188 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2189 COPYVAL(tp_dictoffset);
2190 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00002191}
2192
2193static void
2194inherit_slots(PyTypeObject *type, PyTypeObject *base)
2195{
2196 PyTypeObject *basebase;
2197
2198#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199#undef COPYSLOT
2200#undef COPYNUM
2201#undef COPYSEQ
2202#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002203#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002204
2205#define SLOTDEFINED(SLOT) \
2206 (base->SLOT != 0 && \
2207 (basebase == NULL || base->SLOT != basebase->SLOT))
2208
Tim Peters6d6c1a32001-08-02 04:15:00 +00002209#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002210 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002211
2212#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2213#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2214#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002215#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216
Guido van Rossum13d52f02001-08-10 21:24:08 +00002217 /* This won't inherit indirect slots (from tp_as_number etc.)
2218 if type doesn't provide the space. */
2219
2220 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2221 basebase = base->tp_base;
2222 if (basebase->tp_as_number == NULL)
2223 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002224 COPYNUM(nb_add);
2225 COPYNUM(nb_subtract);
2226 COPYNUM(nb_multiply);
2227 COPYNUM(nb_divide);
2228 COPYNUM(nb_remainder);
2229 COPYNUM(nb_divmod);
2230 COPYNUM(nb_power);
2231 COPYNUM(nb_negative);
2232 COPYNUM(nb_positive);
2233 COPYNUM(nb_absolute);
2234 COPYNUM(nb_nonzero);
2235 COPYNUM(nb_invert);
2236 COPYNUM(nb_lshift);
2237 COPYNUM(nb_rshift);
2238 COPYNUM(nb_and);
2239 COPYNUM(nb_xor);
2240 COPYNUM(nb_or);
2241 COPYNUM(nb_coerce);
2242 COPYNUM(nb_int);
2243 COPYNUM(nb_long);
2244 COPYNUM(nb_float);
2245 COPYNUM(nb_oct);
2246 COPYNUM(nb_hex);
2247 COPYNUM(nb_inplace_add);
2248 COPYNUM(nb_inplace_subtract);
2249 COPYNUM(nb_inplace_multiply);
2250 COPYNUM(nb_inplace_divide);
2251 COPYNUM(nb_inplace_remainder);
2252 COPYNUM(nb_inplace_power);
2253 COPYNUM(nb_inplace_lshift);
2254 COPYNUM(nb_inplace_rshift);
2255 COPYNUM(nb_inplace_and);
2256 COPYNUM(nb_inplace_xor);
2257 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002258 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2259 COPYNUM(nb_true_divide);
2260 COPYNUM(nb_floor_divide);
2261 COPYNUM(nb_inplace_true_divide);
2262 COPYNUM(nb_inplace_floor_divide);
2263 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002264 }
2265
Guido van Rossum13d52f02001-08-10 21:24:08 +00002266 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
2267 basebase = base->tp_base;
2268 if (basebase->tp_as_sequence == NULL)
2269 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002270 COPYSEQ(sq_length);
2271 COPYSEQ(sq_concat);
2272 COPYSEQ(sq_repeat);
2273 COPYSEQ(sq_item);
2274 COPYSEQ(sq_slice);
2275 COPYSEQ(sq_ass_item);
2276 COPYSEQ(sq_ass_slice);
2277 COPYSEQ(sq_contains);
2278 COPYSEQ(sq_inplace_concat);
2279 COPYSEQ(sq_inplace_repeat);
2280 }
2281
Guido van Rossum13d52f02001-08-10 21:24:08 +00002282 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
2283 basebase = base->tp_base;
2284 if (basebase->tp_as_mapping == NULL)
2285 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286 COPYMAP(mp_length);
2287 COPYMAP(mp_subscript);
2288 COPYMAP(mp_ass_subscript);
2289 }
2290
Tim Petersfc57ccb2001-10-12 02:38:24 +00002291 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2292 basebase = base->tp_base;
2293 if (basebase->tp_as_buffer == NULL)
2294 basebase = NULL;
2295 COPYBUF(bf_getreadbuffer);
2296 COPYBUF(bf_getwritebuffer);
2297 COPYBUF(bf_getsegcount);
2298 COPYBUF(bf_getcharbuffer);
2299 }
2300
Guido van Rossum13d52f02001-08-10 21:24:08 +00002301 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002302
Tim Peters6d6c1a32001-08-02 04:15:00 +00002303 COPYSLOT(tp_dealloc);
2304 COPYSLOT(tp_print);
2305 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
2306 type->tp_getattr = base->tp_getattr;
2307 type->tp_getattro = base->tp_getattro;
2308 }
2309 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
2310 type->tp_setattr = base->tp_setattr;
2311 type->tp_setattro = base->tp_setattro;
2312 }
2313 /* tp_compare see tp_richcompare */
2314 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002315 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002316 COPYSLOT(tp_call);
2317 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002318 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002319 if (type->tp_compare == NULL &&
2320 type->tp_richcompare == NULL &&
2321 type->tp_hash == NULL)
2322 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002323 type->tp_compare = base->tp_compare;
2324 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002325 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002326 }
2327 }
2328 else {
2329 COPYSLOT(tp_compare);
2330 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002331 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
2332 COPYSLOT(tp_iter);
2333 COPYSLOT(tp_iternext);
2334 }
2335 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2336 COPYSLOT(tp_descr_get);
2337 COPYSLOT(tp_descr_set);
2338 COPYSLOT(tp_dictoffset);
2339 COPYSLOT(tp_init);
2340 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002341 COPYSLOT(tp_free);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00002342 COPYSLOT(tp_is_gc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002343 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002344}
2345
Jeremy Hylton938ace62002-07-17 16:30:39 +00002346static int add_operators(PyTypeObject *);
2347static int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002348
Tim Peters6d6c1a32001-08-02 04:15:00 +00002349int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002350PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002351{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002352 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002353 PyTypeObject *base;
2354 int i, n;
2355
Guido van Rossumcab05802002-06-10 15:29:03 +00002356 if (type->tp_flags & Py_TPFLAGS_READY) {
2357 assert(type->tp_dict != NULL);
Guido van Rossumd614f972001-08-10 17:39:49 +00002358 return 0;
Guido van Rossumcab05802002-06-10 15:29:03 +00002359 }
Guido van Rossumd614f972001-08-10 17:39:49 +00002360 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00002361
2362 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002363
2364 /* Initialize tp_base (defaults to BaseObject unless that's us) */
2365 base = type->tp_base;
2366 if (base == NULL && type != &PyBaseObject_Type)
2367 base = type->tp_base = &PyBaseObject_Type;
2368
Guido van Rossum323a9cf2002-08-14 17:26:30 +00002369 /* Initialize the base class */
2370 if (base && base->tp_dict == NULL) {
2371 if (PyType_Ready(base) < 0)
2372 goto error;
2373 }
2374
Guido van Rossum0986d822002-04-08 01:38:42 +00002375 /* Initialize ob_type if NULL. This means extensions that want to be
2376 compilable separately on Windows can call PyType_Ready() instead of
2377 initializing the ob_type field of their type objects. */
2378 if (type->ob_type == NULL)
2379 type->ob_type = base->ob_type;
2380
Tim Peters6d6c1a32001-08-02 04:15:00 +00002381 /* Initialize tp_bases */
2382 bases = type->tp_bases;
2383 if (bases == NULL) {
2384 if (base == NULL)
2385 bases = PyTuple_New(0);
2386 else
2387 bases = Py_BuildValue("(O)", base);
2388 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002389 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002390 type->tp_bases = bases;
2391 }
2392
Guido van Rossum687ae002001-10-15 22:03:32 +00002393 /* Initialize tp_dict */
2394 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002395 if (dict == NULL) {
2396 dict = PyDict_New();
2397 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002398 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00002399 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002400 }
2401
Guido van Rossum687ae002001-10-15 22:03:32 +00002402 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002403 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002404 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002405 if (type->tp_methods != NULL) {
2406 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002407 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002408 }
2409 if (type->tp_members != NULL) {
2410 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002411 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002412 }
2413 if (type->tp_getset != NULL) {
2414 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002415 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002416 }
2417
Tim Peters6d6c1a32001-08-02 04:15:00 +00002418 /* Calculate method resolution order */
2419 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002420 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002421 }
2422
Guido van Rossum13d52f02001-08-10 21:24:08 +00002423 /* Inherit special flags from dominant base */
2424 if (type->tp_base != NULL)
2425 inherit_special(type, type->tp_base);
2426
Tim Peters6d6c1a32001-08-02 04:15:00 +00002427 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002428 bases = type->tp_mro;
2429 assert(bases != NULL);
2430 assert(PyTuple_Check(bases));
2431 n = PyTuple_GET_SIZE(bases);
2432 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002433 PyObject *b = PyTuple_GET_ITEM(bases, i);
2434 if (PyType_Check(b))
2435 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002436 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002437
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002438 /* if the type dictionary doesn't contain a __doc__, set it from
2439 the tp_doc slot.
2440 */
2441 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2442 if (type->tp_doc != NULL) {
2443 PyObject *doc = PyString_FromString(type->tp_doc);
2444 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2445 Py_DECREF(doc);
2446 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00002447 PyDict_SetItemString(type->tp_dict,
2448 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002449 }
2450 }
2451
Guido van Rossum13d52f02001-08-10 21:24:08 +00002452 /* Some more special stuff */
2453 base = type->tp_base;
2454 if (base != NULL) {
2455 if (type->tp_as_number == NULL)
2456 type->tp_as_number = base->tp_as_number;
2457 if (type->tp_as_sequence == NULL)
2458 type->tp_as_sequence = base->tp_as_sequence;
2459 if (type->tp_as_mapping == NULL)
2460 type->tp_as_mapping = base->tp_as_mapping;
2461 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002462
Guido van Rossum1c450732001-10-08 15:18:27 +00002463 /* Link into each base class's list of subclasses */
2464 bases = type->tp_bases;
2465 n = PyTuple_GET_SIZE(bases);
2466 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002467 PyObject *b = PyTuple_GET_ITEM(bases, i);
2468 if (PyType_Check(b) &&
2469 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002470 goto error;
2471 }
2472
Guido van Rossum13d52f02001-08-10 21:24:08 +00002473 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002474 assert(type->tp_dict != NULL);
2475 type->tp_flags =
2476 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002477 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002478
2479 error:
2480 type->tp_flags &= ~Py_TPFLAGS_READYING;
2481 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002482}
2483
Guido van Rossum1c450732001-10-08 15:18:27 +00002484static int
2485add_subclass(PyTypeObject *base, PyTypeObject *type)
2486{
2487 int i;
2488 PyObject *list, *ref, *new;
2489
2490 list = base->tp_subclasses;
2491 if (list == NULL) {
2492 base->tp_subclasses = list = PyList_New(0);
2493 if (list == NULL)
2494 return -1;
2495 }
2496 assert(PyList_Check(list));
2497 new = PyWeakref_NewRef((PyObject *)type, NULL);
2498 i = PyList_GET_SIZE(list);
2499 while (--i >= 0) {
2500 ref = PyList_GET_ITEM(list, i);
2501 assert(PyWeakref_CheckRef(ref));
Guido van Rossum3930bc32002-10-18 13:51:49 +00002502 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2503 return PyList_SetItem(list, i, new);
Guido van Rossum1c450732001-10-08 15:18:27 +00002504 }
2505 i = PyList_Append(list, new);
2506 Py_DECREF(new);
2507 return i;
2508}
2509
Tim Peters6d6c1a32001-08-02 04:15:00 +00002510
2511/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2512
2513/* There's a wrapper *function* for each distinct function typedef used
2514 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2515 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2516 Most tables have only one entry; the tables for binary operators have two
2517 entries, one regular and one with reversed arguments. */
2518
2519static PyObject *
2520wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2521{
2522 inquiry func = (inquiry)wrapped;
2523 int res;
2524
2525 if (!PyArg_ParseTuple(args, ""))
2526 return NULL;
2527 res = (*func)(self);
2528 if (res == -1 && PyErr_Occurred())
2529 return NULL;
2530 return PyInt_FromLong((long)res);
2531}
2532
Tim Peters6d6c1a32001-08-02 04:15:00 +00002533static PyObject *
2534wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2535{
2536 binaryfunc func = (binaryfunc)wrapped;
2537 PyObject *other;
2538
2539 if (!PyArg_ParseTuple(args, "O", &other))
2540 return NULL;
2541 return (*func)(self, other);
2542}
2543
2544static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002545wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2546{
2547 binaryfunc func = (binaryfunc)wrapped;
2548 PyObject *other;
2549
2550 if (!PyArg_ParseTuple(args, "O", &other))
2551 return NULL;
2552 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002553 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002554 Py_INCREF(Py_NotImplemented);
2555 return Py_NotImplemented;
2556 }
2557 return (*func)(self, other);
2558}
2559
2560static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002561wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2562{
2563 binaryfunc func = (binaryfunc)wrapped;
2564 PyObject *other;
2565
2566 if (!PyArg_ParseTuple(args, "O", &other))
2567 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002568 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002569 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002570 Py_INCREF(Py_NotImplemented);
2571 return Py_NotImplemented;
2572 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002573 return (*func)(other, self);
2574}
2575
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002576static PyObject *
2577wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2578{
2579 coercion func = (coercion)wrapped;
2580 PyObject *other, *res;
2581 int ok;
2582
2583 if (!PyArg_ParseTuple(args, "O", &other))
2584 return NULL;
2585 ok = func(&self, &other);
2586 if (ok < 0)
2587 return NULL;
2588 if (ok > 0) {
2589 Py_INCREF(Py_NotImplemented);
2590 return Py_NotImplemented;
2591 }
2592 res = PyTuple_New(2);
2593 if (res == NULL) {
2594 Py_DECREF(self);
2595 Py_DECREF(other);
2596 return NULL;
2597 }
2598 PyTuple_SET_ITEM(res, 0, self);
2599 PyTuple_SET_ITEM(res, 1, other);
2600 return res;
2601}
2602
Tim Peters6d6c1a32001-08-02 04:15:00 +00002603static PyObject *
2604wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2605{
2606 ternaryfunc func = (ternaryfunc)wrapped;
2607 PyObject *other;
2608 PyObject *third = Py_None;
2609
2610 /* Note: This wrapper only works for __pow__() */
2611
2612 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2613 return NULL;
2614 return (*func)(self, other, third);
2615}
2616
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002617static PyObject *
2618wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2619{
2620 ternaryfunc func = (ternaryfunc)wrapped;
2621 PyObject *other;
2622 PyObject *third = Py_None;
2623
2624 /* Note: This wrapper only works for __pow__() */
2625
2626 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2627 return NULL;
2628 return (*func)(other, self, third);
2629}
2630
Tim Peters6d6c1a32001-08-02 04:15:00 +00002631static PyObject *
2632wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2633{
2634 unaryfunc func = (unaryfunc)wrapped;
2635
2636 if (!PyArg_ParseTuple(args, ""))
2637 return NULL;
2638 return (*func)(self);
2639}
2640
Tim Peters6d6c1a32001-08-02 04:15:00 +00002641static PyObject *
2642wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2643{
2644 intargfunc func = (intargfunc)wrapped;
2645 int i;
2646
2647 if (!PyArg_ParseTuple(args, "i", &i))
2648 return NULL;
2649 return (*func)(self, i);
2650}
2651
Guido van Rossum5d815f32001-08-17 21:57:47 +00002652static int
2653getindex(PyObject *self, PyObject *arg)
2654{
2655 int i;
2656
2657 i = PyInt_AsLong(arg);
2658 if (i == -1 && PyErr_Occurred())
2659 return -1;
2660 if (i < 0) {
2661 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2662 if (sq && sq->sq_length) {
2663 int n = (*sq->sq_length)(self);
2664 if (n < 0)
2665 return -1;
2666 i += n;
2667 }
2668 }
2669 return i;
2670}
2671
2672static PyObject *
2673wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2674{
2675 intargfunc func = (intargfunc)wrapped;
2676 PyObject *arg;
2677 int i;
2678
Guido van Rossumf4593e02001-10-03 12:09:30 +00002679 if (PyTuple_GET_SIZE(args) == 1) {
2680 arg = PyTuple_GET_ITEM(args, 0);
2681 i = getindex(self, arg);
2682 if (i == -1 && PyErr_Occurred())
2683 return NULL;
2684 return (*func)(self, i);
2685 }
2686 PyArg_ParseTuple(args, "O", &arg);
2687 assert(PyErr_Occurred());
2688 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002689}
2690
Tim Peters6d6c1a32001-08-02 04:15:00 +00002691static PyObject *
2692wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2693{
2694 intintargfunc func = (intintargfunc)wrapped;
2695 int i, j;
2696
2697 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2698 return NULL;
2699 return (*func)(self, i, j);
2700}
2701
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002703wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002704{
2705 intobjargproc func = (intobjargproc)wrapped;
2706 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002707 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002708
Guido van Rossum5d815f32001-08-17 21:57:47 +00002709 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2710 return NULL;
2711 i = getindex(self, arg);
2712 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002713 return NULL;
2714 res = (*func)(self, i, value);
2715 if (res == -1 && PyErr_Occurred())
2716 return NULL;
2717 Py_INCREF(Py_None);
2718 return Py_None;
2719}
2720
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002721static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002722wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002723{
2724 intobjargproc func = (intobjargproc)wrapped;
2725 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002726 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002727
Guido van Rossum5d815f32001-08-17 21:57:47 +00002728 if (!PyArg_ParseTuple(args, "O", &arg))
2729 return NULL;
2730 i = getindex(self, arg);
2731 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002732 return NULL;
2733 res = (*func)(self, i, NULL);
2734 if (res == -1 && PyErr_Occurred())
2735 return NULL;
2736 Py_INCREF(Py_None);
2737 return Py_None;
2738}
2739
Tim Peters6d6c1a32001-08-02 04:15:00 +00002740static PyObject *
2741wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2742{
2743 intintobjargproc func = (intintobjargproc)wrapped;
2744 int i, j, res;
2745 PyObject *value;
2746
2747 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2748 return NULL;
2749 res = (*func)(self, i, j, value);
2750 if (res == -1 && PyErr_Occurred())
2751 return NULL;
2752 Py_INCREF(Py_None);
2753 return Py_None;
2754}
2755
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002756static PyObject *
2757wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2758{
2759 intintobjargproc func = (intintobjargproc)wrapped;
2760 int i, j, res;
2761
2762 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2763 return NULL;
2764 res = (*func)(self, i, j, NULL);
2765 if (res == -1 && PyErr_Occurred())
2766 return NULL;
2767 Py_INCREF(Py_None);
2768 return Py_None;
2769}
2770
Tim Peters6d6c1a32001-08-02 04:15:00 +00002771/* XXX objobjproc is a misnomer; should be objargpred */
2772static PyObject *
2773wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2774{
2775 objobjproc func = (objobjproc)wrapped;
2776 int res;
2777 PyObject *value;
2778
2779 if (!PyArg_ParseTuple(args, "O", &value))
2780 return NULL;
2781 res = (*func)(self, value);
2782 if (res == -1 && PyErr_Occurred())
2783 return NULL;
2784 return PyInt_FromLong((long)res);
2785}
2786
Tim Peters6d6c1a32001-08-02 04:15:00 +00002787static PyObject *
2788wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2789{
2790 objobjargproc func = (objobjargproc)wrapped;
2791 int res;
2792 PyObject *key, *value;
2793
2794 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2795 return NULL;
2796 res = (*func)(self, key, value);
2797 if (res == -1 && PyErr_Occurred())
2798 return NULL;
2799 Py_INCREF(Py_None);
2800 return Py_None;
2801}
2802
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002803static PyObject *
2804wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2805{
2806 objobjargproc func = (objobjargproc)wrapped;
2807 int res;
2808 PyObject *key;
2809
2810 if (!PyArg_ParseTuple(args, "O", &key))
2811 return NULL;
2812 res = (*func)(self, key, NULL);
2813 if (res == -1 && PyErr_Occurred())
2814 return NULL;
2815 Py_INCREF(Py_None);
2816 return Py_None;
2817}
2818
Tim Peters6d6c1a32001-08-02 04:15:00 +00002819static PyObject *
2820wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2821{
2822 cmpfunc func = (cmpfunc)wrapped;
2823 int res;
2824 PyObject *other;
2825
2826 if (!PyArg_ParseTuple(args, "O", &other))
2827 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002828 if (other->ob_type->tp_compare != func &&
2829 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002830 PyErr_Format(
2831 PyExc_TypeError,
2832 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2833 self->ob_type->tp_name,
2834 self->ob_type->tp_name,
2835 other->ob_type->tp_name);
2836 return NULL;
2837 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002838 res = (*func)(self, other);
2839 if (PyErr_Occurred())
2840 return NULL;
2841 return PyInt_FromLong((long)res);
2842}
2843
Tim Peters6d6c1a32001-08-02 04:15:00 +00002844static PyObject *
2845wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2846{
2847 setattrofunc func = (setattrofunc)wrapped;
2848 int res;
2849 PyObject *name, *value;
2850
2851 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2852 return NULL;
2853 res = (*func)(self, name, value);
2854 if (res < 0)
2855 return NULL;
2856 Py_INCREF(Py_None);
2857 return Py_None;
2858}
2859
2860static PyObject *
2861wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2862{
2863 setattrofunc func = (setattrofunc)wrapped;
2864 int res;
2865 PyObject *name;
2866
2867 if (!PyArg_ParseTuple(args, "O", &name))
2868 return NULL;
2869 res = (*func)(self, name, NULL);
2870 if (res < 0)
2871 return NULL;
2872 Py_INCREF(Py_None);
2873 return Py_None;
2874}
2875
Tim Peters6d6c1a32001-08-02 04:15:00 +00002876static PyObject *
2877wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2878{
2879 hashfunc func = (hashfunc)wrapped;
2880 long res;
2881
2882 if (!PyArg_ParseTuple(args, ""))
2883 return NULL;
2884 res = (*func)(self);
2885 if (res == -1 && PyErr_Occurred())
2886 return NULL;
2887 return PyInt_FromLong(res);
2888}
2889
Tim Peters6d6c1a32001-08-02 04:15:00 +00002890static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002891wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002892{
2893 ternaryfunc func = (ternaryfunc)wrapped;
2894
Guido van Rossumc8e56452001-10-22 00:43:43 +00002895 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002896}
2897
Tim Peters6d6c1a32001-08-02 04:15:00 +00002898static PyObject *
2899wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2900{
2901 richcmpfunc func = (richcmpfunc)wrapped;
2902 PyObject *other;
2903
2904 if (!PyArg_ParseTuple(args, "O", &other))
2905 return NULL;
2906 return (*func)(self, other, op);
2907}
2908
2909#undef RICHCMP_WRAPPER
2910#define RICHCMP_WRAPPER(NAME, OP) \
2911static PyObject * \
2912richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2913{ \
2914 return wrap_richcmpfunc(self, args, wrapped, OP); \
2915}
2916
Jack Jansen8e938b42001-08-08 15:29:49 +00002917RICHCMP_WRAPPER(lt, Py_LT)
2918RICHCMP_WRAPPER(le, Py_LE)
2919RICHCMP_WRAPPER(eq, Py_EQ)
2920RICHCMP_WRAPPER(ne, Py_NE)
2921RICHCMP_WRAPPER(gt, Py_GT)
2922RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002923
Tim Peters6d6c1a32001-08-02 04:15:00 +00002924static PyObject *
2925wrap_next(PyObject *self, PyObject *args, void *wrapped)
2926{
2927 unaryfunc func = (unaryfunc)wrapped;
2928 PyObject *res;
2929
2930 if (!PyArg_ParseTuple(args, ""))
2931 return NULL;
2932 res = (*func)(self);
2933 if (res == NULL && !PyErr_Occurred())
2934 PyErr_SetNone(PyExc_StopIteration);
2935 return res;
2936}
2937
Tim Peters6d6c1a32001-08-02 04:15:00 +00002938static PyObject *
2939wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2940{
2941 descrgetfunc func = (descrgetfunc)wrapped;
2942 PyObject *obj;
2943 PyObject *type = NULL;
2944
2945 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2946 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002947 return (*func)(self, obj, type);
2948}
2949
Tim Peters6d6c1a32001-08-02 04:15:00 +00002950static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002951wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002952{
2953 descrsetfunc func = (descrsetfunc)wrapped;
2954 PyObject *obj, *value;
2955 int ret;
2956
2957 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2958 return NULL;
2959 ret = (*func)(self, obj, value);
2960 if (ret < 0)
2961 return NULL;
2962 Py_INCREF(Py_None);
2963 return Py_None;
2964}
Guido van Rossum22b13872002-08-06 21:41:44 +00002965
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00002966static PyObject *
2967wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
2968{
2969 descrsetfunc func = (descrsetfunc)wrapped;
2970 PyObject *obj;
2971 int ret;
2972
2973 if (!PyArg_ParseTuple(args, "O", &obj))
2974 return NULL;
2975 ret = (*func)(self, obj, NULL);
2976 if (ret < 0)
2977 return NULL;
2978 Py_INCREF(Py_None);
2979 return Py_None;
2980}
Tim Peters6d6c1a32001-08-02 04:15:00 +00002981
Tim Peters6d6c1a32001-08-02 04:15:00 +00002982static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002983wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002984{
2985 initproc func = (initproc)wrapped;
2986
Guido van Rossumc8e56452001-10-22 00:43:43 +00002987 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002988 return NULL;
2989 Py_INCREF(Py_None);
2990 return Py_None;
2991}
2992
Tim Peters6d6c1a32001-08-02 04:15:00 +00002993static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002994tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002995{
Barry Warsaw60f01882001-08-22 19:24:42 +00002996 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002997 PyObject *arg0, *res;
2998
2999 if (self == NULL || !PyType_Check(self))
3000 Py_FatalError("__new__() called with non-type 'self'");
3001 type = (PyTypeObject *)self;
3002 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003003 PyErr_Format(PyExc_TypeError,
3004 "%s.__new__(): not enough arguments",
3005 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003006 return NULL;
3007 }
3008 arg0 = PyTuple_GET_ITEM(args, 0);
3009 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003010 PyErr_Format(PyExc_TypeError,
3011 "%s.__new__(X): X is not a type object (%s)",
3012 type->tp_name,
3013 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003014 return NULL;
3015 }
3016 subtype = (PyTypeObject *)arg0;
3017 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003018 PyErr_Format(PyExc_TypeError,
3019 "%s.__new__(%s): %s is not a subtype of %s",
3020 type->tp_name,
3021 subtype->tp_name,
3022 subtype->tp_name,
3023 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003024 return NULL;
3025 }
Barry Warsaw60f01882001-08-22 19:24:42 +00003026
3027 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00003028 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00003029 most derived base that's not a heap type is this type. */
3030 staticbase = subtype;
3031 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
3032 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00003033 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00003034 PyErr_Format(PyExc_TypeError,
3035 "%s.__new__(%s) is not safe, use %s.__new__()",
3036 type->tp_name,
3037 subtype->tp_name,
3038 staticbase == NULL ? "?" : staticbase->tp_name);
3039 return NULL;
3040 }
3041
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003042 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
3043 if (args == NULL)
3044 return NULL;
3045 res = type->tp_new(subtype, args, kwds);
3046 Py_DECREF(args);
3047 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003048}
3049
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003050static struct PyMethodDef tp_new_methoddef[] = {
3051 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
Neal Norwitz5dc2a372002-08-13 22:19:13 +00003052 PyDoc_STR("T.__new__(S, ...) -> "
3053 "a new object with type S, a subtype of T")},
Tim Peters6d6c1a32001-08-02 04:15:00 +00003054 {0}
3055};
3056
3057static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003058add_tp_new_wrapper(PyTypeObject *type)
3059{
Guido van Rossumf040ede2001-08-07 16:40:56 +00003060 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003061
Guido van Rossum687ae002001-10-15 22:03:32 +00003062 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00003063 return 0;
3064 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003065 if (func == NULL)
3066 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00003067 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00003068}
3069
Guido van Rossumf040ede2001-08-07 16:40:56 +00003070/* Slot wrappers that call the corresponding __foo__ slot. See comments
3071 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00003072
Guido van Rossumdc91b992001-08-08 22:26:22 +00003073#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003075FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003076{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00003077 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003078 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003079}
3080
Guido van Rossumdc91b992001-08-08 22:26:22 +00003081#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003082static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003083FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003084{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003085 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003086 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003087}
3088
Guido van Rossumdc91b992001-08-08 22:26:22 +00003089
3090#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003091static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003092FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003093{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003094 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003095 int do_other = self->ob_type != other->ob_type && \
3096 other->ob_type->tp_as_number != NULL && \
3097 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003098 if (self->ob_type->tp_as_number != NULL && \
3099 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
3100 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00003101 if (do_other && \
3102 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
3103 r = call_maybe( \
3104 other, ROPSTR, &rcache_str, "(O)", self); \
3105 if (r != Py_NotImplemented) \
3106 return r; \
3107 Py_DECREF(r); \
3108 do_other = 0; \
3109 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003110 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003111 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003112 if (r != Py_NotImplemented || \
3113 other->ob_type == self->ob_type) \
3114 return r; \
3115 Py_DECREF(r); \
3116 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00003117 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00003118 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00003119 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00003120 } \
3121 Py_INCREF(Py_NotImplemented); \
3122 return Py_NotImplemented; \
3123}
3124
3125#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
3126 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
3127
3128#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
3129static PyObject * \
3130FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
3131{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00003132 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00003133 return call_method(self, OPSTR, &cache_str, \
3134 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00003135}
3136
3137static int
3138slot_sq_length(PyObject *self)
3139{
Guido van Rossum2730b132001-08-28 18:22:14 +00003140 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003141 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00003142 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003143
3144 if (res == NULL)
3145 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00003146 len = (int)PyInt_AsLong(res);
3147 Py_DECREF(res);
Jeremy Hylton73a088e2002-07-25 16:43:29 +00003148 if (len == -1 && PyErr_Occurred())
3149 return -1;
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00003150 if (len < 0) {
Guido van Rossum22b13872002-08-06 21:41:44 +00003151 PyErr_SetString(PyExc_ValueError,
Jeremy Hyltonf20fcf92002-07-25 16:06:15 +00003152 "__len__() should return >= 0");
3153 return -1;
3154 }
Guido van Rossum26111622001-10-01 16:42:49 +00003155 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003156}
3157
Guido van Rossumdc91b992001-08-08 22:26:22 +00003158SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
3159SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00003160
3161/* Super-optimized version of slot_sq_item.
3162 Other slots could do the same... */
3163static PyObject *
3164slot_sq_item(PyObject *self, int i)
3165{
3166 static PyObject *getitem_str;
3167 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
3168 descrgetfunc f;
3169
3170 if (getitem_str == NULL) {
3171 getitem_str = PyString_InternFromString("__getitem__");
3172 if (getitem_str == NULL)
3173 return NULL;
3174 }
3175 func = _PyType_Lookup(self->ob_type, getitem_str);
3176 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00003177 if ((f = func->ob_type->tp_descr_get) == NULL)
3178 Py_INCREF(func);
Neal Norwitz673cd822002-10-18 16:33:13 +00003179 else {
Guido van Rossumf4593e02001-10-03 12:09:30 +00003180 func = f(func, self, (PyObject *)(self->ob_type));
Neal Norwitz673cd822002-10-18 16:33:13 +00003181 if (func == NULL) {
3182 return NULL;
3183 }
3184 }
Guido van Rossumf4593e02001-10-03 12:09:30 +00003185 ival = PyInt_FromLong(i);
3186 if (ival != NULL) {
3187 args = PyTuple_New(1);
3188 if (args != NULL) {
3189 PyTuple_SET_ITEM(args, 0, ival);
3190 retval = PyObject_Call(func, args, NULL);
3191 Py_XDECREF(args);
3192 Py_XDECREF(func);
3193 return retval;
3194 }
3195 }
3196 }
3197 else {
3198 PyErr_SetObject(PyExc_AttributeError, getitem_str);
3199 }
3200 Py_XDECREF(args);
3201 Py_XDECREF(ival);
3202 Py_XDECREF(func);
3203 return NULL;
3204}
3205
Guido van Rossumdc91b992001-08-08 22:26:22 +00003206SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003207
3208static int
3209slot_sq_ass_item(PyObject *self, int index, PyObject *value)
3210{
3211 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003212 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003213
3214 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003215 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003216 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003217 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003218 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003219 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003220 if (res == NULL)
3221 return -1;
3222 Py_DECREF(res);
3223 return 0;
3224}
3225
3226static int
3227slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
3228{
3229 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003230 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003231
3232 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003233 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003234 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003235 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003236 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003237 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003238 if (res == NULL)
3239 return -1;
3240 Py_DECREF(res);
3241 return 0;
3242}
3243
3244static int
3245slot_sq_contains(PyObject *self, PyObject *value)
3246{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003247 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00003248 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003249
Guido van Rossum55f20992001-10-01 17:18:22 +00003250 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003251
3252 if (func != NULL) {
3253 args = Py_BuildValue("(O)", value);
3254 if (args == NULL)
3255 res = NULL;
3256 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003257 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003258 Py_DECREF(args);
3259 }
3260 Py_DECREF(func);
3261 if (res == NULL)
3262 return -1;
3263 return PyObject_IsTrue(res);
3264 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003265 else if (PyErr_Occurred())
3266 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003267 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00003268 return _PySequence_IterSearch(self, value,
3269 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003270 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003271}
3272
Guido van Rossumdc91b992001-08-08 22:26:22 +00003273SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
3274SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003275
3276#define slot_mp_length slot_sq_length
3277
Guido van Rossumdc91b992001-08-08 22:26:22 +00003278SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003279
3280static int
3281slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
3282{
3283 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003284 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003285
3286 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003287 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003288 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003289 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003290 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003291 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003292 if (res == NULL)
3293 return -1;
3294 Py_DECREF(res);
3295 return 0;
3296}
3297
Guido van Rossumdc91b992001-08-08 22:26:22 +00003298SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
3299SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
3300SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
3301SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
3302SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
3303SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
3304
Jeremy Hylton938ace62002-07-17 16:30:39 +00003305static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003306
3307SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
3308 nb_power, "__pow__", "__rpow__")
3309
3310static PyObject *
3311slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3312{
Guido van Rossum2730b132001-08-28 18:22:14 +00003313 static PyObject *pow_str;
3314
Guido van Rossumdc91b992001-08-08 22:26:22 +00003315 if (modulus == Py_None)
3316 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00003317 /* Three-arg power doesn't use __rpow__. But ternary_op
3318 can call this when the second argument's type uses
3319 slot_nb_power, so check before calling self.__pow__. */
3320 if (self->ob_type->tp_as_number != NULL &&
3321 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
3322 return call_method(self, "__pow__", &pow_str,
3323 "(OO)", other, modulus);
3324 }
3325 Py_INCREF(Py_NotImplemented);
3326 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00003327}
3328
3329SLOT0(slot_nb_negative, "__neg__")
3330SLOT0(slot_nb_positive, "__pos__")
3331SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003332
3333static int
3334slot_nb_nonzero(PyObject *self)
3335{
Guido van Rossum84b2bed2002-08-16 17:01:09 +00003336 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00003337 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003338
Guido van Rossum55f20992001-10-01 17:18:22 +00003339 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003340 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003341 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003342 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003343 func = lookup_maybe(self, "__len__", &len_str);
3344 if (func == NULL) {
3345 if (PyErr_Occurred())
3346 return -1;
3347 else
3348 return 1;
3349 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003350 }
Guido van Rossum84b2bed2002-08-16 17:01:09 +00003351 args = res = PyTuple_New(0);
3352 if (args != NULL) {
3353 res = PyObject_Call(func, args, NULL);
3354 Py_DECREF(args);
3355 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003356 Py_DECREF(func);
3357 if (res == NULL)
3358 return -1;
3359 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003360}
3361
Guido van Rossumdc91b992001-08-08 22:26:22 +00003362SLOT0(slot_nb_invert, "__invert__")
3363SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3364SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3365SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3366SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3367SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003368
3369static int
3370slot_nb_coerce(PyObject **a, PyObject **b)
3371{
3372 static PyObject *coerce_str;
3373 PyObject *self = *a, *other = *b;
3374
3375 if (self->ob_type->tp_as_number != NULL &&
3376 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3377 PyObject *r;
3378 r = call_maybe(
3379 self, "__coerce__", &coerce_str, "(O)", other);
3380 if (r == NULL)
3381 return -1;
3382 if (r == Py_NotImplemented) {
3383 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003384 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003385 else {
3386 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3387 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003388 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003389 Py_DECREF(r);
3390 return -1;
3391 }
3392 *a = PyTuple_GET_ITEM(r, 0);
3393 Py_INCREF(*a);
3394 *b = PyTuple_GET_ITEM(r, 1);
3395 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003396 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003397 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003398 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003399 }
3400 if (other->ob_type->tp_as_number != NULL &&
3401 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3402 PyObject *r;
3403 r = call_maybe(
3404 other, "__coerce__", &coerce_str, "(O)", self);
3405 if (r == NULL)
3406 return -1;
3407 if (r == Py_NotImplemented) {
3408 Py_DECREF(r);
3409 return 1;
3410 }
3411 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3412 PyErr_SetString(PyExc_TypeError,
3413 "__coerce__ didn't return a 2-tuple");
3414 Py_DECREF(r);
3415 return -1;
3416 }
3417 *a = PyTuple_GET_ITEM(r, 1);
3418 Py_INCREF(*a);
3419 *b = PyTuple_GET_ITEM(r, 0);
3420 Py_INCREF(*b);
3421 Py_DECREF(r);
3422 return 0;
3423 }
3424 return 1;
3425}
3426
Guido van Rossumdc91b992001-08-08 22:26:22 +00003427SLOT0(slot_nb_int, "__int__")
3428SLOT0(slot_nb_long, "__long__")
3429SLOT0(slot_nb_float, "__float__")
3430SLOT0(slot_nb_oct, "__oct__")
3431SLOT0(slot_nb_hex, "__hex__")
3432SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3433SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3434SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3435SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3436SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
Guido van Rossum6e5680f2002-10-15 01:01:53 +00003437SLOT1(slot_nb_inplace_power, "__ipow__", PyObject *, "O")
Guido van Rossumdc91b992001-08-08 22:26:22 +00003438SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3439SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3440SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3441SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3442SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3443SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3444 "__floordiv__", "__rfloordiv__")
3445SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3446SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3447SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003448
3449static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003450half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003451{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003452 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003453 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003454 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003455
Guido van Rossum60718732001-08-28 17:47:51 +00003456 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003457 if (func == NULL) {
3458 PyErr_Clear();
3459 }
3460 else {
3461 args = Py_BuildValue("(O)", other);
3462 if (args == NULL)
3463 res = NULL;
3464 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003465 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003466 Py_DECREF(args);
3467 }
Raymond Hettingerab5dae32002-06-24 13:08:16 +00003468 Py_DECREF(func);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003469 if (res != Py_NotImplemented) {
3470 if (res == NULL)
3471 return -2;
3472 c = PyInt_AsLong(res);
3473 Py_DECREF(res);
3474 if (c == -1 && PyErr_Occurred())
3475 return -2;
3476 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3477 }
3478 Py_DECREF(res);
3479 }
3480 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003481}
3482
Guido van Rossumab3b0342001-09-18 20:38:53 +00003483/* This slot is published for the benefit of try_3way_compare in object.c */
3484int
3485_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003486{
3487 int c;
3488
Guido van Rossumab3b0342001-09-18 20:38:53 +00003489 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003490 c = half_compare(self, other);
3491 if (c <= 1)
3492 return c;
3493 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003494 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003495 c = half_compare(other, self);
3496 if (c < -1)
3497 return -2;
3498 if (c <= 1)
3499 return -c;
3500 }
3501 return (void *)self < (void *)other ? -1 :
3502 (void *)self > (void *)other ? 1 : 0;
3503}
3504
3505static PyObject *
3506slot_tp_repr(PyObject *self)
3507{
3508 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003509 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003510
Guido van Rossum60718732001-08-28 17:47:51 +00003511 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003512 if (func != NULL) {
3513 res = PyEval_CallObject(func, NULL);
3514 Py_DECREF(func);
3515 return res;
3516 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003517 PyErr_Clear();
3518 return PyString_FromFormat("<%s object at %p>",
3519 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003520}
3521
3522static PyObject *
3523slot_tp_str(PyObject *self)
3524{
3525 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003526 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003527
Guido van Rossum60718732001-08-28 17:47:51 +00003528 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003529 if (func != NULL) {
3530 res = PyEval_CallObject(func, NULL);
3531 Py_DECREF(func);
3532 return res;
3533 }
3534 else {
3535 PyErr_Clear();
3536 return slot_tp_repr(self);
3537 }
3538}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003539
3540static long
3541slot_tp_hash(PyObject *self)
3542{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003543 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003544 static PyObject *hash_str, *eq_str, *cmp_str;
3545
Tim Peters6d6c1a32001-08-02 04:15:00 +00003546 long h;
3547
Guido van Rossum60718732001-08-28 17:47:51 +00003548 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003549
3550 if (func != NULL) {
3551 res = PyEval_CallObject(func, NULL);
3552 Py_DECREF(func);
3553 if (res == NULL)
3554 return -1;
3555 h = PyInt_AsLong(res);
3556 }
3557 else {
3558 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003559 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003560 if (func == NULL) {
3561 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003562 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003563 }
3564 if (func != NULL) {
3565 Py_DECREF(func);
3566 PyErr_SetString(PyExc_TypeError, "unhashable type");
3567 return -1;
3568 }
3569 PyErr_Clear();
3570 h = _Py_HashPointer((void *)self);
3571 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003572 if (h == -1 && !PyErr_Occurred())
3573 h = -2;
3574 return h;
3575}
3576
3577static PyObject *
3578slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3579{
Guido van Rossum60718732001-08-28 17:47:51 +00003580 static PyObject *call_str;
3581 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003582 PyObject *res;
3583
3584 if (meth == NULL)
3585 return NULL;
3586 res = PyObject_Call(meth, args, kwds);
3587 Py_DECREF(meth);
3588 return res;
3589}
3590
Guido van Rossum14a6f832001-10-17 13:59:09 +00003591/* There are two slot dispatch functions for tp_getattro.
3592
3593 - slot_tp_getattro() is used when __getattribute__ is overridden
3594 but no __getattr__ hook is present;
3595
3596 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3597
Guido van Rossumc334df52002-04-04 23:44:47 +00003598 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
3599 detects the absence of __getattr__ and then installs the simpler slot if
3600 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00003601
Tim Peters6d6c1a32001-08-02 04:15:00 +00003602static PyObject *
3603slot_tp_getattro(PyObject *self, PyObject *name)
3604{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003605 static PyObject *getattribute_str = NULL;
3606 return call_method(self, "__getattribute__", &getattribute_str,
3607 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003608}
3609
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003610static PyObject *
3611slot_tp_getattr_hook(PyObject *self, PyObject *name)
3612{
3613 PyTypeObject *tp = self->ob_type;
3614 PyObject *getattr, *getattribute, *res;
3615 static PyObject *getattribute_str = NULL;
3616 static PyObject *getattr_str = NULL;
3617
3618 if (getattr_str == NULL) {
3619 getattr_str = PyString_InternFromString("__getattr__");
3620 if (getattr_str == NULL)
3621 return NULL;
3622 }
3623 if (getattribute_str == NULL) {
3624 getattribute_str =
3625 PyString_InternFromString("__getattribute__");
3626 if (getattribute_str == NULL)
3627 return NULL;
3628 }
3629 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003630 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003631 /* No __getattr__ hook: use a simpler dispatcher */
3632 tp->tp_getattro = slot_tp_getattro;
3633 return slot_tp_getattro(self, name);
3634 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003635 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003636 if (getattribute == NULL ||
3637 (getattribute->ob_type == &PyWrapperDescr_Type &&
3638 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3639 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003640 res = PyObject_GenericGetAttr(self, name);
3641 else
3642 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003643 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003644 PyErr_Clear();
3645 res = PyObject_CallFunction(getattr, "OO", self, name);
3646 }
3647 return res;
3648}
3649
Tim Peters6d6c1a32001-08-02 04:15:00 +00003650static int
3651slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3652{
3653 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003654 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003655
3656 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003657 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003658 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003659 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003660 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003661 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003662 if (res == NULL)
3663 return -1;
3664 Py_DECREF(res);
3665 return 0;
3666}
3667
3668/* Map rich comparison operators to their __xx__ namesakes */
3669static char *name_op[] = {
3670 "__lt__",
3671 "__le__",
3672 "__eq__",
3673 "__ne__",
3674 "__gt__",
3675 "__ge__",
3676};
3677
3678static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003679half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003680{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003681 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003682 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003683
Guido van Rossum60718732001-08-28 17:47:51 +00003684 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003685 if (func == NULL) {
3686 PyErr_Clear();
3687 Py_INCREF(Py_NotImplemented);
3688 return Py_NotImplemented;
3689 }
3690 args = Py_BuildValue("(O)", other);
3691 if (args == NULL)
3692 res = NULL;
3693 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003694 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003695 Py_DECREF(args);
3696 }
3697 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003698 return res;
3699}
3700
Guido van Rossumb8f63662001-08-15 23:57:02 +00003701/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3702static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3703
3704static PyObject *
3705slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3706{
3707 PyObject *res;
3708
3709 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3710 res = half_richcompare(self, other, op);
3711 if (res != Py_NotImplemented)
3712 return res;
3713 Py_DECREF(res);
3714 }
3715 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3716 res = half_richcompare(other, self, swapped_op[op]);
3717 if (res != Py_NotImplemented) {
3718 return res;
3719 }
3720 Py_DECREF(res);
3721 }
3722 Py_INCREF(Py_NotImplemented);
3723 return Py_NotImplemented;
3724}
3725
3726static PyObject *
3727slot_tp_iter(PyObject *self)
3728{
3729 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003730 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003731
Guido van Rossum60718732001-08-28 17:47:51 +00003732 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003733 if (func != NULL) {
Guido van Rossum84b2bed2002-08-16 17:01:09 +00003734 PyObject *args;
3735 args = res = PyTuple_New(0);
3736 if (args != NULL) {
3737 res = PyObject_Call(func, args, NULL);
3738 Py_DECREF(args);
3739 }
3740 Py_DECREF(func);
3741 return res;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003742 }
3743 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003744 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003745 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00003746 PyErr_SetString(PyExc_TypeError,
3747 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003748 return NULL;
3749 }
3750 Py_DECREF(func);
3751 return PySeqIter_New(self);
3752}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003753
3754static PyObject *
3755slot_tp_iternext(PyObject *self)
3756{
Guido van Rossum2730b132001-08-28 18:22:14 +00003757 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003758 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003759}
3760
Guido van Rossum1a493502001-08-17 16:47:50 +00003761static PyObject *
3762slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3763{
3764 PyTypeObject *tp = self->ob_type;
3765 PyObject *get;
3766 static PyObject *get_str = NULL;
3767
3768 if (get_str == NULL) {
3769 get_str = PyString_InternFromString("__get__");
3770 if (get_str == NULL)
3771 return NULL;
3772 }
3773 get = _PyType_Lookup(tp, get_str);
3774 if (get == NULL) {
3775 /* Avoid further slowdowns */
3776 if (tp->tp_descr_get == slot_tp_descr_get)
3777 tp->tp_descr_get = NULL;
3778 Py_INCREF(self);
3779 return self;
3780 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003781 if (obj == NULL)
3782 obj = Py_None;
3783 if (type == NULL)
3784 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003785 return PyObject_CallFunction(get, "OOO", self, obj, type);
3786}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003787
3788static int
3789slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3790{
Guido van Rossum2c252392001-08-24 10:13:31 +00003791 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003792 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003793
3794 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003795 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003796 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003797 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003798 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003799 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003800 if (res == NULL)
3801 return -1;
3802 Py_DECREF(res);
3803 return 0;
3804}
3805
3806static int
3807slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3808{
Guido van Rossum60718732001-08-28 17:47:51 +00003809 static PyObject *init_str;
3810 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003811 PyObject *res;
3812
3813 if (meth == NULL)
3814 return -1;
3815 res = PyObject_Call(meth, args, kwds);
3816 Py_DECREF(meth);
3817 if (res == NULL)
3818 return -1;
3819 Py_DECREF(res);
3820 return 0;
3821}
3822
3823static PyObject *
3824slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3825{
Guido van Rossum7bed2132002-08-08 21:57:53 +00003826 static PyObject *new_str;
3827 PyObject *func;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003828 PyObject *newargs, *x;
3829 int i, n;
3830
Guido van Rossum7bed2132002-08-08 21:57:53 +00003831 if (new_str == NULL) {
3832 new_str = PyString_InternFromString("__new__");
3833 if (new_str == NULL)
3834 return NULL;
3835 }
3836 func = PyObject_GetAttr((PyObject *)type, new_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003837 if (func == NULL)
3838 return NULL;
3839 assert(PyTuple_Check(args));
3840 n = PyTuple_GET_SIZE(args);
3841 newargs = PyTuple_New(n+1);
3842 if (newargs == NULL)
3843 return NULL;
3844 Py_INCREF(type);
3845 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3846 for (i = 0; i < n; i++) {
3847 x = PyTuple_GET_ITEM(args, i);
3848 Py_INCREF(x);
3849 PyTuple_SET_ITEM(newargs, i+1, x);
3850 }
3851 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003852 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003853 Py_DECREF(func);
3854 return x;
3855}
3856
Guido van Rossumfebd61d2002-08-08 20:55:20 +00003857static void
3858slot_tp_del(PyObject *self)
3859{
3860 static PyObject *del_str = NULL;
3861 PyObject *del, *res;
3862 PyObject *error_type, *error_value, *error_traceback;
3863
3864 /* Temporarily resurrect the object. */
3865 assert(self->ob_refcnt == 0);
3866 self->ob_refcnt = 1;
3867
3868 /* Save the current exception, if any. */
3869 PyErr_Fetch(&error_type, &error_value, &error_traceback);
3870
3871 /* Execute __del__ method, if any. */
3872 del = lookup_maybe(self, "__del__", &del_str);
3873 if (del != NULL) {
3874 res = PyEval_CallObject(del, NULL);
3875 if (res == NULL)
3876 PyErr_WriteUnraisable(del);
3877 else
3878 Py_DECREF(res);
3879 Py_DECREF(del);
3880 }
3881
3882 /* Restore the saved exception. */
3883 PyErr_Restore(error_type, error_value, error_traceback);
3884
3885 /* Undo the temporary resurrection; can't use DECREF here, it would
3886 * cause a recursive call.
3887 */
3888 assert(self->ob_refcnt > 0);
3889 if (--self->ob_refcnt == 0)
3890 return; /* this is the normal path out */
3891
3892 /* __del__ resurrected it! Make it look like the original Py_DECREF
3893 * never happened.
3894 */
3895 {
3896 int refcnt = self->ob_refcnt;
3897 _Py_NewReference(self);
3898 self->ob_refcnt = refcnt;
3899 }
3900 assert(!PyType_IS_GC(self->ob_type) ||
3901 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
3902 /* If Py_REF_DEBUG, the original decref dropped _Py_RefTotal, but
3903 * _Py_NewReference bumped it again, so that's a wash.
3904 * If Py_TRACE_REFS, _Py_NewReference re-added self to the object
3905 * chain, so no more to do there either.
3906 * If COUNT_ALLOCS, the original decref bumped tp_frees, and
3907 * _Py_NewReference bumped tp_allocs: both of those need to be
3908 * undone.
3909 */
3910#ifdef COUNT_ALLOCS
3911 --self->ob_type->tp_frees;
3912 --self->ob_type->tp_allocs;
3913#endif
3914}
3915
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003916
3917/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3918 functions. The offsets here are relative to the 'etype' structure, which
3919 incorporates the additional structures used for numbers, sequences and
3920 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3921 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00003922 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
3923 terminated with an all-zero entry. (This table is further initialized and
3924 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003925
Guido van Rossum6d204072001-10-21 00:44:31 +00003926typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003927
3928#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003929#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003930#undef ETSLOT
3931#undef SQSLOT
3932#undef MPSLOT
3933#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003934#undef UNSLOT
3935#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003936#undef BINSLOT
3937#undef RBINSLOT
3938
Guido van Rossum6d204072001-10-21 00:44:31 +00003939#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00003940 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3941 PyDoc_STR(DOC)}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003942#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3943 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
Neal Norwitzd47714a2002-08-13 19:01:38 +00003944 PyDoc_STR(DOC), FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003945#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
Neal Norwitzd47714a2002-08-13 19:01:38 +00003946 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, \
3947 PyDoc_STR(DOC)}
Guido van Rossum6d204072001-10-21 00:44:31 +00003948#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3949 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3950#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3951 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3952#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3953 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3954#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3955 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3956 "x." NAME "() <==> " DOC)
3957#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3958 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3959 "x." NAME "(y) <==> x" DOC "y")
3960#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3961 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3962 "x." NAME "(y) <==> x" DOC "y")
3963#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3964 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3965 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003966
3967static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003968 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3969 "x.__len__() <==> len(x)"),
3970 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3971 "x.__add__(y) <==> x+y"),
3972 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3973 "x.__mul__(n) <==> x*n"),
3974 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3975 "x.__rmul__(n) <==> n*x"),
3976 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3977 "x.__getitem__(y) <==> x[y]"),
3978 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3979 "x.__getslice__(i, j) <==> x[i:j]"),
3980 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3981 "x.__setitem__(i, y) <==> x[i]=y"),
3982 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3983 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003984 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003985 wrap_intintobjargproc,
3986 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3987 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3988 "x.__delslice__(i, j) <==> del x[i:j]"),
3989 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3990 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003991 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003992 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003993 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003994 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003995
Guido van Rossum6d204072001-10-21 00:44:31 +00003996 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3997 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003998 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003999 wrap_binaryfunc,
4000 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004001 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004002 wrap_objobjargproc,
4003 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004004 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00004005 wrap_delitem,
4006 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004007
Guido van Rossum6d204072001-10-21 00:44:31 +00004008 BINSLOT("__add__", nb_add, slot_nb_add,
4009 "+"),
4010 RBINSLOT("__radd__", nb_add, slot_nb_add,
4011 "+"),
4012 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
4013 "-"),
4014 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
4015 "-"),
4016 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
4017 "*"),
4018 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
4019 "*"),
4020 BINSLOT("__div__", nb_divide, slot_nb_divide,
4021 "/"),
4022 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
4023 "/"),
4024 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
4025 "%"),
4026 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
4027 "%"),
4028 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
4029 "divmod(x, y)"),
4030 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
4031 "divmod(y, x)"),
4032 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
4033 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
4034 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
4035 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
4036 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
4037 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
4038 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
4039 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00004040 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00004041 "x != 0"),
4042 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
4043 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
4044 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
4045 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
4046 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
4047 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
4048 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
4049 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
4050 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
4051 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
4052 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
4053 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
4054 "x.__coerce__(y) <==> coerce(x, y)"),
4055 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
4056 "int(x)"),
4057 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
4058 "long(x)"),
4059 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
4060 "float(x)"),
4061 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
4062 "oct(x)"),
4063 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
4064 "hex(x)"),
4065 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
4066 wrap_binaryfunc, "+"),
4067 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
4068 wrap_binaryfunc, "-"),
4069 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
4070 wrap_binaryfunc, "*"),
4071 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
4072 wrap_binaryfunc, "/"),
4073 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
4074 wrap_binaryfunc, "%"),
4075 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
Guido van Rossum6e5680f2002-10-15 01:01:53 +00004076 wrap_binaryfunc, "**"),
Guido van Rossum6d204072001-10-21 00:44:31 +00004077 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
4078 wrap_binaryfunc, "<<"),
4079 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
4080 wrap_binaryfunc, ">>"),
4081 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
4082 wrap_binaryfunc, "&"),
4083 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
4084 wrap_binaryfunc, "^"),
4085 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
4086 wrap_binaryfunc, "|"),
4087 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4088 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
4089 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
4090 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
4091 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
4092 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
4093 IBSLOT("__itruediv__", nb_inplace_true_divide,
4094 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004095
Guido van Rossum6d204072001-10-21 00:44:31 +00004096 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
4097 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004098 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004099 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
4100 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00004101 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00004102 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
4103 "x.__cmp__(y) <==> cmp(x,y)"),
4104 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
4105 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00004106 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
4107 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004108 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00004109 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
4110 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
4111 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
4112 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
4113 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
4114 "x.__setattr__('name', value) <==> x.name = value"),
4115 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
4116 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
4117 "x.__delattr__('name') <==> del x.name"),
4118 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
4119 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
4120 "x.__lt__(y) <==> x<y"),
4121 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
4122 "x.__le__(y) <==> x<=y"),
4123 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
4124 "x.__eq__(y) <==> x==y"),
4125 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
4126 "x.__ne__(y) <==> x!=y"),
4127 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
4128 "x.__gt__(y) <==> x>y"),
4129 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
4130 "x.__ge__(y) <==> x>=y"),
4131 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
4132 "x.__iter__() <==> iter(x)"),
4133 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
4134 "x.next() -> the next value, or raise StopIteration"),
4135 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
4136 "descr.__get__(obj[, type]) -> value"),
4137 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
4138 "descr.__set__(obj, value)"),
Guido van Rossum0dbab4c52002-08-01 14:39:25 +00004139 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
4140 wrap_descr_delete, "descr.__delete__(obj)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00004141 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00004142 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00004143 "see x.__class__.__doc__ for signature",
4144 PyWrapperFlag_KEYWORDS),
4145 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossumfebd61d2002-08-08 20:55:20 +00004146 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004147 {NULL}
4148};
4149
Guido van Rossumc334df52002-04-04 23:44:47 +00004150/* Given a type pointer and an offset gotten from a slotdef entry, return a
4151 pointer to the actual slot. This is not quite the same as simply adding
4152 the offset to the type pointer, since it takes care to indirect through the
4153 proper indirection pointer (as_buffer, etc.); it returns NULL if the
4154 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004155static void **
4156slotptr(PyTypeObject *type, int offset)
4157{
4158 char *ptr;
4159
Guido van Rossum09638c12002-06-13 19:17:46 +00004160 /* Note: this depends on the order of the members of etype! */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004161 assert(offset >= 0);
4162 assert(offset < offsetof(etype, as_buffer));
Guido van Rossum09638c12002-06-13 19:17:46 +00004163 if (offset >= offsetof(etype, as_sequence)) {
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004164 ptr = (void *)type->tp_as_sequence;
4165 offset -= offsetof(etype, as_sequence);
4166 }
Guido van Rossum09638c12002-06-13 19:17:46 +00004167 else if (offset >= offsetof(etype, as_mapping)) {
4168 ptr = (void *)type->tp_as_mapping;
4169 offset -= offsetof(etype, as_mapping);
4170 }
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004171 else if (offset >= offsetof(etype, as_number)) {
4172 ptr = (void *)type->tp_as_number;
4173 offset -= offsetof(etype, as_number);
4174 }
4175 else {
4176 ptr = (void *)type;
4177 }
4178 if (ptr != NULL)
4179 ptr += offset;
4180 return (void **)ptr;
4181}
Guido van Rossumf040ede2001-08-07 16:40:56 +00004182
Guido van Rossumc334df52002-04-04 23:44:47 +00004183/* Length of array of slotdef pointers used to store slots with the
4184 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
4185 the same __name__, for any __name__. Since that's a static property, it is
4186 appropriate to declare fixed-size arrays for this. */
4187#define MAX_EQUIV 10
4188
4189/* Return a slot pointer for a given name, but ONLY if the attribute has
4190 exactly one slot function. The name must be an interned string. */
4191static void **
4192resolve_slotdups(PyTypeObject *type, PyObject *name)
4193{
4194 /* XXX Maybe this could be optimized more -- but is it worth it? */
4195
4196 /* pname and ptrs act as a little cache */
4197 static PyObject *pname;
4198 static slotdef *ptrs[MAX_EQUIV];
4199 slotdef *p, **pp;
4200 void **res, **ptr;
4201
4202 if (pname != name) {
4203 /* Collect all slotdefs that match name into ptrs. */
4204 pname = name;
4205 pp = ptrs;
4206 for (p = slotdefs; p->name_strobj; p++) {
4207 if (p->name_strobj == name)
4208 *pp++ = p;
4209 }
4210 *pp = NULL;
4211 }
4212
4213 /* Look in all matching slots of the type; if exactly one of these has
4214 a filled-in slot, return its value. Otherwise return NULL. */
4215 res = NULL;
4216 for (pp = ptrs; *pp; pp++) {
4217 ptr = slotptr(type, (*pp)->offset);
4218 if (ptr == NULL || *ptr == NULL)
4219 continue;
4220 if (res != NULL)
4221 return NULL;
4222 res = ptr;
4223 }
4224 return res;
4225}
4226
4227/* Common code for update_these_slots() and fixup_slot_dispatchers(). This
4228 does some incredibly complex thinking and then sticks something into the
4229 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
4230 interests, and then stores a generic wrapper or a specific function into
4231 the slot.) Return a pointer to the next slotdef with a different offset,
4232 because that's convenient for fixup_slot_dispatchers(). */
4233static slotdef *
4234update_one_slot(PyTypeObject *type, slotdef *p)
4235{
4236 PyObject *descr;
4237 PyWrapperDescrObject *d;
4238 void *generic = NULL, *specific = NULL;
4239 int use_generic = 0;
4240 int offset = p->offset;
4241 void **ptr = slotptr(type, offset);
4242
4243 if (ptr == NULL) {
4244 do {
4245 ++p;
4246 } while (p->offset == offset);
4247 return p;
4248 }
4249 do {
4250 descr = _PyType_Lookup(type, p->name_strobj);
4251 if (descr == NULL)
4252 continue;
4253 if (descr->ob_type == &PyWrapperDescr_Type) {
4254 void **tptr = resolve_slotdups(type, p->name_strobj);
4255 if (tptr == NULL || tptr == ptr)
4256 generic = p->function;
4257 d = (PyWrapperDescrObject *)descr;
4258 if (d->d_base->wrapper == p->wrapper &&
4259 PyType_IsSubtype(type, d->d_type))
4260 {
4261 if (specific == NULL ||
4262 specific == d->d_wrapped)
4263 specific = d->d_wrapped;
4264 else
4265 use_generic = 1;
4266 }
4267 }
Guido van Rossum721f62e2002-08-09 02:14:34 +00004268 else if (descr->ob_type == &PyCFunction_Type &&
4269 PyCFunction_GET_FUNCTION(descr) ==
4270 (PyCFunction)tp_new_wrapper &&
4271 strcmp(p->name, "__new__") == 0)
4272 {
4273 /* The __new__ wrapper is not a wrapper descriptor,
4274 so must be special-cased differently.
4275 If we don't do this, creating an instance will
4276 always use slot_tp_new which will look up
4277 __new__ in the MRO which will call tp_new_wrapper
4278 which will look through the base classes looking
4279 for a static base and call its tp_new (usually
4280 PyType_GenericNew), after performing various
4281 sanity checks and constructing a new argument
4282 list. Cut all that nonsense short -- this speeds
4283 up instance creation tremendously. */
4284 specific = type->tp_new;
4285 /* XXX I'm not 100% sure that there isn't a hole
4286 in this reasoning that requires additional
4287 sanity checks. I'll buy the first person to
4288 point out a bug in this reasoning a beer. */
4289 }
Guido van Rossumc334df52002-04-04 23:44:47 +00004290 else {
4291 use_generic = 1;
4292 generic = p->function;
4293 }
4294 } while ((++p)->offset == offset);
4295 if (specific && !use_generic)
4296 *ptr = specific;
4297 else
4298 *ptr = generic;
4299 return p;
4300}
4301
Guido van Rossum22b13872002-08-06 21:41:44 +00004302static int recurse_down_subclasses(PyTypeObject *type, slotdef **pp,
Jeremy Hylton938ace62002-07-17 16:30:39 +00004303 PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004304
Guido van Rossumc334df52002-04-04 23:44:47 +00004305/* In the type, update the slots whose slotdefs are gathered in the pp0 array,
4306 and then do the same for all this type's subtypes. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004307static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004308update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004309{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004310 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004311
Guido van Rossumc334df52002-04-04 23:44:47 +00004312 for (pp = pp0; *pp; pp++)
4313 update_one_slot(type, *pp);
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004314 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004315}
4316
Guido van Rossumc334df52002-04-04 23:44:47 +00004317/* Update the slots whose slotdefs are gathered in the pp array in all (direct
4318 or indirect) subclasses of type. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004319static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004320recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004321{
4322 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004323 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004324 int i, n;
4325
4326 subclasses = type->tp_subclasses;
4327 if (subclasses == NULL)
4328 return 0;
4329 assert(PyList_Check(subclasses));
4330 n = PyList_GET_SIZE(subclasses);
4331 for (i = 0; i < n; i++) {
4332 ref = PyList_GET_ITEM(subclasses, i);
4333 assert(PyWeakref_CheckRef(ref));
4334 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
Guido van Rossum59e6c532002-06-14 02:27:07 +00004335 assert(subclass != NULL);
4336 if ((PyObject *)subclass == Py_None)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004337 continue;
4338 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004339 /* Avoid recursing down into unaffected classes */
4340 dict = subclass->tp_dict;
4341 if (dict != NULL && PyDict_Check(dict) &&
4342 PyDict_GetItem(dict, name) != NULL)
4343 continue;
4344 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004345 return -1;
4346 }
4347 return 0;
4348}
4349
Guido van Rossumc334df52002-04-04 23:44:47 +00004350/* Comparison function for qsort() to compare slotdefs by their offset, and
4351 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004352static int
4353slotdef_cmp(const void *aa, const void *bb)
4354{
4355 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
4356 int c = a->offset - b->offset;
4357 if (c != 0)
4358 return c;
4359 else
4360 return a - b;
4361}
4362
Guido van Rossumc334df52002-04-04 23:44:47 +00004363/* Initialize the slotdefs table by adding interned string objects for the
4364 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004365static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004366init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004367{
4368 slotdef *p;
4369 static int initialized = 0;
4370
4371 if (initialized)
4372 return;
4373 for (p = slotdefs; p->name; p++) {
4374 p->name_strobj = PyString_InternFromString(p->name);
4375 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00004376 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004377 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004378 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
4379 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004380 initialized = 1;
4381}
4382
Guido van Rossumc334df52002-04-04 23:44:47 +00004383/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004384static int
4385update_slot(PyTypeObject *type, PyObject *name)
4386{
Guido van Rossumc334df52002-04-04 23:44:47 +00004387 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004388 slotdef *p;
4389 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004390 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004391
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004392 init_slotdefs();
4393 pp = ptrs;
4394 for (p = slotdefs; p->name; p++) {
4395 /* XXX assume name is interned! */
4396 if (p->name_strobj == name)
4397 *pp++ = p;
4398 }
4399 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004400 for (pp = ptrs; *pp; pp++) {
4401 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004402 offset = p->offset;
4403 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004404 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004405 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004406 }
Guido van Rossumc334df52002-04-04 23:44:47 +00004407 if (ptrs[0] == NULL)
4408 return 0; /* Not an attribute that affects any slots */
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004409 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004410}
4411
Guido van Rossumc334df52002-04-04 23:44:47 +00004412/* Store the proper functions in the slot dispatches at class (type)
4413 definition time, based upon which operations the class overrides in its
4414 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004415static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004416fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004417{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004418 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004419
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004420 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00004421 for (p = slotdefs; p->name; )
4422 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004423}
Guido van Rossum705f0f52001-08-24 16:47:00 +00004424
Guido van Rossum6d204072001-10-21 00:44:31 +00004425/* This function is called by PyType_Ready() to populate the type's
4426 dictionary with method descriptors for function slots. For each
Guido van Rossum09638c12002-06-13 19:17:46 +00004427 function slot (like tp_repr) that's defined in the type, one or more
4428 corresponding descriptors are added in the type's tp_dict dictionary
4429 under the appropriate name (like __repr__). Some function slots
4430 cause more than one descriptor to be added (for example, the nb_add
4431 slot adds both __add__ and __radd__ descriptors) and some function
4432 slots compete for the same descriptor (for example both sq_item and
4433 mp_subscript generate a __getitem__ descriptor).
4434
4435 In the latter case, the first slotdef entry encoutered wins. Since
4436 slotdef entries are sorted by the offset of the slot in the etype
4437 struct, this gives us some control over disambiguating between
4438 competing slots: the members of struct etype are listed from most
4439 general to least general, so the most general slot is preferred. In
4440 particular, because as_mapping comes before as_sequence, for a type
4441 that defines both mp_subscript and sq_item, mp_subscript wins.
4442
4443 This only adds new descriptors and doesn't overwrite entries in
4444 tp_dict that were previously defined. The descriptors contain a
4445 reference to the C function they must call, so that it's safe if they
4446 are copied into a subtype's __dict__ and the subtype has a different
4447 C function in its slot -- calling the method defined by the
4448 descriptor will call the C function that was used to create it,
4449 rather than the C function present in the slot when it is called.
4450 (This is important because a subtype may have a C function in the
4451 slot that calls the method from the dictionary, and we want to avoid
4452 infinite recursion here.) */
Guido van Rossum6d204072001-10-21 00:44:31 +00004453
4454static int
4455add_operators(PyTypeObject *type)
4456{
4457 PyObject *dict = type->tp_dict;
4458 slotdef *p;
4459 PyObject *descr;
4460 void **ptr;
4461
4462 init_slotdefs();
4463 for (p = slotdefs; p->name; p++) {
4464 if (p->wrapper == NULL)
4465 continue;
4466 ptr = slotptr(type, p->offset);
4467 if (!ptr || !*ptr)
4468 continue;
4469 if (PyDict_GetItem(dict, p->name_strobj))
4470 continue;
4471 descr = PyDescr_NewWrapper(type, p, *ptr);
4472 if (descr == NULL)
4473 return -1;
4474 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
4475 return -1;
4476 Py_DECREF(descr);
4477 }
4478 if (type->tp_new != NULL) {
4479 if (add_tp_new_wrapper(type) < 0)
4480 return -1;
4481 }
4482 return 0;
4483}
4484
Guido van Rossum705f0f52001-08-24 16:47:00 +00004485
4486/* Cooperative 'super' */
4487
4488typedef struct {
4489 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00004490 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004491 PyObject *obj;
4492} superobject;
4493
Guido van Rossum6f799372001-09-20 20:46:19 +00004494static PyMemberDef super_members[] = {
4495 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
4496 "the class invoking super()"},
4497 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
4498 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004499 {0}
4500};
4501
Guido van Rossum705f0f52001-08-24 16:47:00 +00004502static void
4503super_dealloc(PyObject *self)
4504{
4505 superobject *su = (superobject *)self;
4506
Guido van Rossum048eb752001-10-02 21:24:57 +00004507 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004508 Py_XDECREF(su->obj);
4509 Py_XDECREF(su->type);
4510 self->ob_type->tp_free(self);
4511}
4512
4513static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004514super_repr(PyObject *self)
4515{
4516 superobject *su = (superobject *)self;
4517
4518 if (su->obj)
4519 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004520 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004521 su->type ? su->type->tp_name : "NULL",
4522 su->obj->ob_type->tp_name);
4523 else
4524 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004525 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004526 su->type ? su->type->tp_name : "NULL");
4527}
4528
4529static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00004530super_getattro(PyObject *self, PyObject *name)
4531{
4532 superobject *su = (superobject *)self;
4533
4534 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00004535 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00004536 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004537 descrgetfunc f;
4538 int i, n;
4539
Guido van Rossum155db9a2002-04-02 17:53:47 +00004540 starttype = su->obj->ob_type;
4541 mro = starttype->tp_mro;
4542
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004543 if (mro == NULL)
4544 n = 0;
4545 else {
4546 assert(PyTuple_Check(mro));
4547 n = PyTuple_GET_SIZE(mro);
4548 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004549 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00004550 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00004551 break;
4552 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004553 if (i >= n && PyType_Check(su->obj)) {
Guido van Rossum155db9a2002-04-02 17:53:47 +00004554 starttype = (PyTypeObject *)(su->obj);
4555 mro = starttype->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004556 if (mro == NULL)
4557 n = 0;
4558 else {
4559 assert(PyTuple_Check(mro));
4560 n = PyTuple_GET_SIZE(mro);
4561 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004562 for (i = 0; i < n; i++) {
4563 if ((PyObject *)(su->type) ==
4564 PyTuple_GET_ITEM(mro, i))
4565 break;
4566 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004567 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004568 i++;
4569 res = NULL;
4570 for (; i < n; i++) {
4571 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00004572 if (PyType_Check(tmp))
4573 dict = ((PyTypeObject *)tmp)->tp_dict;
4574 else if (PyClass_Check(tmp))
4575 dict = ((PyClassObject *)tmp)->cl_dict;
4576 else
4577 continue;
4578 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00004579 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00004580 Py_INCREF(res);
4581 f = res->ob_type->tp_descr_get;
4582 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004583 tmp = f(res, su->obj,
4584 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004585 Py_DECREF(res);
4586 res = tmp;
4587 }
4588 return res;
4589 }
4590 }
4591 }
4592 return PyObject_GenericGetAttr(self, name);
4593}
4594
Guido van Rossum5b443c62001-12-03 15:38:28 +00004595static int
4596supercheck(PyTypeObject *type, PyObject *obj)
4597{
4598 if (!PyType_IsSubtype(obj->ob_type, type) &&
4599 !(PyType_Check(obj) &&
4600 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4601 PyErr_SetString(PyExc_TypeError,
4602 "super(type, obj): "
4603 "obj must be an instance or subtype of type");
4604 return -1;
4605 }
4606 else
4607 return 0;
4608}
4609
Guido van Rossum705f0f52001-08-24 16:47:00 +00004610static PyObject *
4611super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4612{
4613 superobject *su = (superobject *)self;
4614 superobject *new;
4615
4616 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4617 /* Not binding to an object, or already bound */
4618 Py_INCREF(self);
4619 return self;
4620 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004621 if (su->ob_type != &PySuper_Type)
4622 /* If su is an instance of a subclass of super,
4623 call its type */
4624 return PyObject_CallFunction((PyObject *)su->ob_type,
4625 "OO", su->type, obj);
4626 else {
4627 /* Inline the common case */
4628 if (supercheck(su->type, obj) < 0)
4629 return NULL;
4630 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4631 NULL, NULL);
4632 if (new == NULL)
4633 return NULL;
4634 Py_INCREF(su->type);
4635 Py_INCREF(obj);
4636 new->type = su->type;
4637 new->obj = obj;
4638 return (PyObject *)new;
4639 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004640}
4641
4642static int
4643super_init(PyObject *self, PyObject *args, PyObject *kwds)
4644{
4645 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004646 PyTypeObject *type;
4647 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004648
4649 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4650 return -1;
4651 if (obj == Py_None)
4652 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004653 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004654 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004655 Py_INCREF(type);
4656 Py_XINCREF(obj);
4657 su->type = type;
4658 su->obj = obj;
4659 return 0;
4660}
4661
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004662PyDoc_STRVAR(super_doc,
Guido van Rossum705f0f52001-08-24 16:47:00 +00004663"super(type) -> unbound super object\n"
4664"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004665"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004666"Typical use to call a cooperative superclass method:\n"
4667"class C(B):\n"
4668" def meth(self, arg):\n"
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00004669" super(C, self).meth(arg)");
Guido van Rossum705f0f52001-08-24 16:47:00 +00004670
Guido van Rossum048eb752001-10-02 21:24:57 +00004671static int
4672super_traverse(PyObject *self, visitproc visit, void *arg)
4673{
4674 superobject *su = (superobject *)self;
4675 int err;
4676
4677#define VISIT(SLOT) \
4678 if (SLOT) { \
4679 err = visit((PyObject *)(SLOT), arg); \
4680 if (err) \
4681 return err; \
4682 }
4683
4684 VISIT(su->obj);
4685 VISIT(su->type);
4686
4687#undef VISIT
4688
4689 return 0;
4690}
4691
Guido van Rossum705f0f52001-08-24 16:47:00 +00004692PyTypeObject PySuper_Type = {
4693 PyObject_HEAD_INIT(&PyType_Type)
4694 0, /* ob_size */
4695 "super", /* tp_name */
4696 sizeof(superobject), /* tp_basicsize */
4697 0, /* tp_itemsize */
4698 /* methods */
4699 super_dealloc, /* tp_dealloc */
4700 0, /* tp_print */
4701 0, /* tp_getattr */
4702 0, /* tp_setattr */
4703 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004704 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004705 0, /* tp_as_number */
4706 0, /* tp_as_sequence */
4707 0, /* tp_as_mapping */
4708 0, /* tp_hash */
4709 0, /* tp_call */
4710 0, /* tp_str */
4711 super_getattro, /* tp_getattro */
4712 0, /* tp_setattro */
4713 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004714 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4715 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004716 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004717 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004718 0, /* tp_clear */
4719 0, /* tp_richcompare */
4720 0, /* tp_weaklistoffset */
4721 0, /* tp_iter */
4722 0, /* tp_iternext */
4723 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004724 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004725 0, /* tp_getset */
4726 0, /* tp_base */
4727 0, /* tp_dict */
4728 super_descr_get, /* tp_descr_get */
4729 0, /* tp_descr_set */
4730 0, /* tp_dictoffset */
4731 super_init, /* tp_init */
4732 PyType_GenericAlloc, /* tp_alloc */
4733 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00004734 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004735};