blob: 07de6dc60239c1c0907927269b027e46561a2ce4 [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 {
11 PyTypeObject type;
12 PyNumberMethods as_number;
13 PySequenceMethods as_sequence;
14 PyMappingMethods as_mapping;
15 PyBufferProcs as_buffer;
16 PyObject *name, *slots;
17 PyMemberDef members[1];
18} etype;
19
Guido van Rossum6f799372001-09-20 20:46:19 +000020static PyMemberDef type_members[] = {
Tim Peters6d6c1a32001-08-02 04:15:00 +000021 {"__basicsize__", T_INT, offsetof(PyTypeObject,tp_basicsize),READONLY},
22 {"__itemsize__", T_INT, offsetof(PyTypeObject, tp_itemsize), READONLY},
23 {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
Guido van Rossum9676b222001-08-17 20:32:36 +000024 {"__weakrefoffset__", T_LONG,
Tim Peters6d6c1a32001-08-02 04:15:00 +000025 offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
26 {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
27 {"__dictoffset__", T_LONG,
28 offsetof(PyTypeObject, tp_dictoffset), READONLY},
29 {"__bases__", T_OBJECT, offsetof(PyTypeObject, tp_bases), READONLY},
30 {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
31 {0}
32};
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000033
Guido van Rossumc0b618a1997-05-02 03:12:38 +000034static PyObject *
Guido van Rossumc3542212001-08-16 09:18:56 +000035type_name(PyTypeObject *type, void *context)
36{
37 char *s;
38
39 s = strrchr(type->tp_name, '.');
40 if (s == NULL)
41 s = type->tp_name;
42 else
43 s++;
44 return PyString_FromString(s);
45}
46
47static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +000048type_module(PyTypeObject *type, void *context)
Guido van Rossum29ca26e1995-01-07 11:58:15 +000049{
Guido van Rossumc3542212001-08-16 09:18:56 +000050 PyObject *mod;
51 char *s;
52
53 s = strrchr(type->tp_name, '.');
54 if (s != NULL)
55 return PyString_FromStringAndSize(type->tp_name,
56 (int)(s - type->tp_name));
57 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
58 return PyString_FromString("__builtin__");
Guido van Rossum687ae002001-10-15 22:03:32 +000059 mod = PyDict_GetItemString(type->tp_dict, "__module__");
Guido van Rossumc3542212001-08-16 09:18:56 +000060 if (mod != NULL && PyString_Check(mod)) {
61 Py_INCREF(mod);
62 return mod;
63 }
64 PyErr_SetString(PyExc_AttributeError, "__module__");
65 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +000066}
67
Guido van Rossum3926a632001-09-25 16:25:58 +000068static int
69type_set_module(PyTypeObject *type, PyObject *value, void *context)
70{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +000071 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
Guido van Rossum3926a632001-09-25 16:25:58 +000072 strrchr(type->tp_name, '.')) {
73 PyErr_Format(PyExc_TypeError,
74 "can't set %s.__module__", type->tp_name);
75 return -1;
76 }
77 if (!value) {
78 PyErr_Format(PyExc_TypeError,
79 "can't delete %s.__module__", type->tp_name);
80 return -1;
81 }
82 return PyDict_SetItemString(type->tp_dict, "__module__", value);
83}
84
Tim Peters6d6c1a32001-08-02 04:15:00 +000085static PyObject *
86type_dict(PyTypeObject *type, void *context)
87{
88 if (type->tp_dict == NULL) {
Guido van Rossumc0b618a1997-05-02 03:12:38 +000089 Py_INCREF(Py_None);
90 return Py_None;
Guido van Rossum29ca26e1995-01-07 11:58:15 +000091 }
Tim Peters6d6c1a32001-08-02 04:15:00 +000092 return PyDictProxy_New(type->tp_dict);
Guido van Rossum29ca26e1995-01-07 11:58:15 +000093}
94
Tim Peters24008312002-03-17 18:56:20 +000095static PyObject *
96type_get_doc(PyTypeObject *type, void *context)
97{
98 PyObject *result;
Guido van Rossum6ca7d412002-04-18 00:22:00 +000099 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
Tim Peters24008312002-03-17 18:56:20 +0000100 return PyString_FromString(type->tp_doc);
Tim Peters24008312002-03-17 18:56:20 +0000101 result = PyDict_GetItemString(type->tp_dict, "__doc__");
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000102 if (result == NULL) {
103 result = Py_None;
104 Py_INCREF(result);
105 }
106 else if (result->ob_type->tp_descr_get) {
Tim Peters2b858972002-04-18 04:12:28 +0000107 result = result->ob_type->tp_descr_get(result, NULL,
108 (PyObject *)type);
Guido van Rossum6ca7d412002-04-18 00:22:00 +0000109 }
110 else {
111 Py_INCREF(result);
112 }
Tim Peters24008312002-03-17 18:56:20 +0000113 return result;
114}
115
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000116static PyGetSetDef type_getsets[] = {
Guido van Rossumc3542212001-08-16 09:18:56 +0000117 {"__name__", (getter)type_name, NULL, NULL},
Guido van Rossum3926a632001-09-25 16:25:58 +0000118 {"__module__", (getter)type_module, (setter)type_set_module, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000119 {"__dict__", (getter)type_dict, NULL, NULL},
Tim Peters24008312002-03-17 18:56:20 +0000120 {"__doc__", (getter)type_get_doc, NULL, NULL},
Tim Peters6d6c1a32001-08-02 04:15:00 +0000121 {0}
122};
123
Martin v. Löwis0163d6d2001-06-09 07:34:05 +0000124static int
125type_compare(PyObject *v, PyObject *w)
126{
127 /* This is called with type objects only. So we
128 can just compare the addresses. */
129 Py_uintptr_t vv = (Py_uintptr_t)v;
130 Py_uintptr_t ww = (Py_uintptr_t)w;
131 return (vv < ww) ? -1 : (vv > ww) ? 1 : 0;
132}
133
Guido van Rossumc0b618a1997-05-02 03:12:38 +0000134static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000135type_repr(PyTypeObject *type)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000136{
Barry Warsaw7ce36942001-08-24 18:34:26 +0000137 PyObject *mod, *name, *rtn;
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000138 char *kind;
Guido van Rossumc3542212001-08-16 09:18:56 +0000139
140 mod = type_module(type, NULL);
141 if (mod == NULL)
142 PyErr_Clear();
143 else if (!PyString_Check(mod)) {
144 Py_DECREF(mod);
145 mod = NULL;
146 }
147 name = type_name(type, NULL);
148 if (name == NULL)
149 return NULL;
Barry Warsaw7ce36942001-08-24 18:34:26 +0000150
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000151 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
152 kind = "class";
153 else
154 kind = "type";
155
Barry Warsaw7ce36942001-08-24 18:34:26 +0000156 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000157 rtn = PyString_FromFormat("<%s '%s.%s'>",
158 kind,
Barry Warsaw7ce36942001-08-24 18:34:26 +0000159 PyString_AS_STRING(mod),
160 PyString_AS_STRING(name));
161 }
Guido van Rossumc3542212001-08-16 09:18:56 +0000162 else
Guido van Rossuma4cb7882001-09-25 03:56:29 +0000163 rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000164
Guido van Rossumc3542212001-08-16 09:18:56 +0000165 Py_XDECREF(mod);
166 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +0000167 return rtn;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000168}
169
Tim Peters6d6c1a32001-08-02 04:15:00 +0000170static PyObject *
171type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
172{
173 PyObject *obj;
174
175 if (type->tp_new == NULL) {
176 PyErr_Format(PyExc_TypeError,
177 "cannot create '%.100s' instances",
178 type->tp_name);
179 return NULL;
180 }
181
Tim Peters3f996e72001-09-13 19:18:27 +0000182 obj = type->tp_new(type, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000183 if (obj != NULL) {
Guido van Rossumf76de622001-10-18 15:49:21 +0000184 /* Ugly exception: when the call was type(something),
185 don't call tp_init on the result. */
186 if (type == &PyType_Type &&
187 PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
188 (kwds == NULL ||
189 (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
190 return obj;
Guido van Rossum8ace1ab2002-04-06 01:05:01 +0000191 /* If the returned object is not an instance of type,
192 it won't be initialized. */
193 if (!PyType_IsSubtype(obj->ob_type, type))
194 return obj;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000195 type = obj->ob_type;
196 if (type->tp_init != NULL &&
197 type->tp_init(obj, args, kwds) < 0) {
198 Py_DECREF(obj);
199 obj = NULL;
200 }
201 }
202 return obj;
203}
204
205PyObject *
206PyType_GenericAlloc(PyTypeObject *type, int nitems)
207{
Tim Peters6d6c1a32001-08-02 04:15:00 +0000208 PyObject *obj;
Tim Petersf2a67da2001-10-07 03:54:51 +0000209 const size_t size = _PyObject_VAR_SIZE(type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000210
211 if (PyType_IS_GC(type))
Neil Schemenauer09a2ae52002-04-12 03:06:53 +0000212 obj = _PyObject_GC_Malloc(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000213 else
Neil Schemenauerc806c882001-08-29 23:54:54 +0000214 obj = PyObject_MALLOC(size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000215
Neil Schemenauerc806c882001-08-29 23:54:54 +0000216 if (obj == NULL)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000217 return PyErr_NoMemory();
Tim Peters406fe3b2001-10-06 19:04:01 +0000218
Neil Schemenauerc806c882001-08-29 23:54:54 +0000219 memset(obj, '\0', size);
Tim Peters406fe3b2001-10-06 19:04:01 +0000220
Tim Peters6d6c1a32001-08-02 04:15:00 +0000221 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
222 Py_INCREF(type);
Tim Peters406fe3b2001-10-06 19:04:01 +0000223
Tim Peters6d6c1a32001-08-02 04:15:00 +0000224 if (type->tp_itemsize == 0)
225 PyObject_INIT(obj, type);
226 else
227 (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
Tim Peters406fe3b2001-10-06 19:04:01 +0000228
Tim Peters6d6c1a32001-08-02 04:15:00 +0000229 if (PyType_IS_GC(type))
Neil Schemenauerc806c882001-08-29 23:54:54 +0000230 _PyObject_GC_TRACK(obj);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000231 return obj;
232}
233
234PyObject *
235PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
236{
237 return type->tp_alloc(type, 0);
238}
239
Guido van Rossum9475a232001-10-05 20:51:39 +0000240/* Helpers for subtyping */
241
242static int
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000243traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
244{
245 int i, n;
246 PyMemberDef *mp;
247
248 n = type->ob_size;
249 mp = ((etype *)type)->members;
250 for (i = 0; i < n; i++, mp++) {
251 if (mp->type == T_OBJECT_EX) {
252 char *addr = (char *)self + mp->offset;
253 PyObject *obj = *(PyObject **)addr;
254 if (obj != NULL) {
255 int err = visit(obj, arg);
256 if (err)
257 return err;
258 }
259 }
260 }
261 return 0;
262}
263
264static int
Guido van Rossum9475a232001-10-05 20:51:39 +0000265subtype_traverse(PyObject *self, visitproc visit, void *arg)
266{
267 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000268 traverseproc basetraverse;
Guido van Rossum9475a232001-10-05 20:51:39 +0000269
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000270 /* Find the nearest base with a different tp_traverse,
271 and traverse slots while we're at it */
Guido van Rossum9475a232001-10-05 20:51:39 +0000272 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000273 base = type;
274 while ((basetraverse = base->tp_traverse) == subtype_traverse) {
275 if (base->ob_size) {
276 int err = traverse_slots(base, self, visit, arg);
277 if (err)
278 return err;
279 }
Guido van Rossum9475a232001-10-05 20:51:39 +0000280 base = base->tp_base;
281 assert(base);
282 }
283
284 if (type->tp_dictoffset != base->tp_dictoffset) {
285 PyObject **dictptr = _PyObject_GetDictPtr(self);
286 if (dictptr && *dictptr) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000287 int err = visit(*dictptr, arg);
Guido van Rossum9475a232001-10-05 20:51:39 +0000288 if (err)
289 return err;
290 }
291 }
292
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000293 if (basetraverse)
294 return basetraverse(self, visit, arg);
295 return 0;
296}
297
298static void
299clear_slots(PyTypeObject *type, PyObject *self)
300{
301 int i, n;
302 PyMemberDef *mp;
303
304 n = type->ob_size;
305 mp = ((etype *)type)->members;
306 for (i = 0; i < n; i++, mp++) {
307 if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
308 char *addr = (char *)self + mp->offset;
309 PyObject *obj = *(PyObject **)addr;
310 if (obj != NULL) {
311 Py_DECREF(obj);
312 *(PyObject **)addr = NULL;
313 }
314 }
315 }
316}
317
318static int
319subtype_clear(PyObject *self)
320{
321 PyTypeObject *type, *base;
322 inquiry baseclear;
323
324 /* Find the nearest base with a different tp_clear
325 and clear slots while we're at it */
326 type = self->ob_type;
327 base = type;
328 while ((baseclear = base->tp_clear) == subtype_clear) {
329 if (base->ob_size)
330 clear_slots(base, self);
331 base = base->tp_base;
332 assert(base);
333 }
334
335 if (type->tp_dictoffset != base->tp_dictoffset) {
336 PyObject **dictptr = _PyObject_GetDictPtr(self);
337 if (dictptr && *dictptr) {
338 PyDict_Clear(*dictptr);
339 }
340 }
341
342 if (baseclear)
343 return baseclear(self);
Guido van Rossum9475a232001-10-05 20:51:39 +0000344 return 0;
345}
Tim Peters6d6c1a32001-08-02 04:15:00 +0000346
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000347staticforward PyObject *lookup_maybe(PyObject *, char *, PyObject **);
348
349static int
350call_finalizer(PyObject *self)
351{
352 static PyObject *del_str = NULL;
353 PyObject *del, *res;
354 PyObject *error_type, *error_value, *error_traceback;
355
356 /* Temporarily resurrect the object. */
357#ifdef Py_TRACE_REFS
358#ifndef Py_REF_DEBUG
359# error "Py_TRACE_REFS defined but Py_REF_DEBUG not."
360#endif
361 /* much too complicated if Py_TRACE_REFS defined */
362 _Py_NewReference((PyObject *)self);
363#ifdef COUNT_ALLOCS
364 /* compensate for boost in _Py_NewReference; note that
365 * _Py_RefTotal was also boosted; we'll knock that down later.
366 */
367 self->ob_type->tp_allocs--;
368#endif
369#else /* !Py_TRACE_REFS */
370 /* Py_INCREF boosts _Py_RefTotal if Py_REF_DEBUG is defined */
371 Py_INCREF(self);
372#endif /* !Py_TRACE_REFS */
373
374 /* Save the current exception, if any. */
375 PyErr_Fetch(&error_type, &error_value, &error_traceback);
376
377 /* Execute __del__ method, if any. */
378 del = lookup_maybe(self, "__del__", &del_str);
379 if (del != NULL) {
380 res = PyEval_CallObject(del, NULL);
381 if (res == NULL)
382 PyErr_WriteUnraisable(del);
383 else
384 Py_DECREF(res);
385 Py_DECREF(del);
386 }
387
388 /* Restore the saved exception. */
389 PyErr_Restore(error_type, error_value, error_traceback);
390
391 /* Undo the temporary resurrection; can't use DECREF here, it would
392 * cause a recursive call.
393 */
394#ifdef Py_REF_DEBUG
395 /* _Py_RefTotal was boosted either by _Py_NewReference or
396 * Py_INCREF above.
397 */
398 _Py_RefTotal--;
399#endif
400 if (--self->ob_refcnt > 0) {
401#ifdef COUNT_ALLOCS
402 self->ob_type->tp_frees--;
403#endif
404 _PyObject_GC_TRACK(self);
405 return -1; /* __del__ added a reference; don't delete now */
406 }
407#ifdef Py_TRACE_REFS
408 _Py_ForgetReference((PyObject *)self);
409#ifdef COUNT_ALLOCS
410 /* compensate for increment in _Py_ForgetReference */
411 self->ob_type->tp_frees--;
412#endif
413#endif
414
415 return 0;
416}
417
Tim Peters6d6c1a32001-08-02 04:15:00 +0000418static void
419subtype_dealloc(PyObject *self)
420{
Guido van Rossum14227b42001-12-06 02:35:58 +0000421 PyTypeObject *type, *base;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000422 destructor basedealloc;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000423
424 /* This exists so we can DECREF self->ob_type */
425
Guido van Rossum7ad2d1e2001-10-29 22:11:00 +0000426 if (call_finalizer(self) < 0)
427 return;
428
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000429 /* Find the nearest base with a different tp_dealloc
430 and clear slots while we're at it */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000431 type = self->ob_type;
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000432 base = type;
433 while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
434 if (base->ob_size)
435 clear_slots(base, self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000436 base = base->tp_base;
437 assert(base);
Guido van Rossum14227b42001-12-06 02:35:58 +0000438 }
439
Tim Peters6d6c1a32001-08-02 04:15:00 +0000440 /* If we added a dict, DECREF it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000441 if (type->tp_dictoffset && !base->tp_dictoffset) {
442 PyObject **dictptr = _PyObject_GetDictPtr(self);
443 if (dictptr != NULL) {
444 PyObject *dict = *dictptr;
445 if (dict != NULL) {
446 Py_DECREF(dict);
447 *dictptr = NULL;
448 }
Tim Peters6d6c1a32001-08-02 04:15:00 +0000449 }
450 }
451
Guido van Rossum9676b222001-08-17 20:32:36 +0000452 /* If we added weaklist, we clear it */
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000453 if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
Guido van Rossum9676b222001-08-17 20:32:36 +0000454 PyObject_ClearWeakRefs(self);
455
Tim Peters6d6c1a32001-08-02 04:15:00 +0000456 /* Finalize GC if the base doesn't do GC and we do */
457 if (PyType_IS_GC(type) && !PyType_IS_GC(base))
Guido van Rossum048eb752001-10-02 21:24:57 +0000458 _PyObject_GC_UNTRACK(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000459
460 /* Call the base tp_dealloc() */
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000461 assert(basedealloc);
462 basedealloc(self);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000463
464 /* Can't reference self beyond this point */
465 if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
466 Py_DECREF(type);
467 }
468}
469
Tim Peters6d6c1a32001-08-02 04:15:00 +0000470staticforward PyTypeObject *solid_base(PyTypeObject *type);
471
Tim Peters6d6c1a32001-08-02 04:15:00 +0000472/* type test with subclassing support */
473
474int
475PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
476{
477 PyObject *mro;
478
Guido van Rossum9478d072001-09-07 18:52:13 +0000479 if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
480 return b == a || b == &PyBaseObject_Type;
481
Tim Peters6d6c1a32001-08-02 04:15:00 +0000482 mro = a->tp_mro;
483 if (mro != NULL) {
484 /* Deal with multiple inheritance without recursion
485 by walking the MRO tuple */
486 int i, n;
487 assert(PyTuple_Check(mro));
488 n = PyTuple_GET_SIZE(mro);
489 for (i = 0; i < n; i++) {
490 if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
491 return 1;
492 }
493 return 0;
494 }
495 else {
496 /* a is not completely initilized yet; follow tp_base */
497 do {
498 if (a == b)
499 return 1;
500 a = a->tp_base;
501 } while (a != NULL);
502 return b == &PyBaseObject_Type;
503 }
504}
505
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000506/* Internal routines to do a method lookup in the type
Guido van Rossum60718732001-08-28 17:47:51 +0000507 without looking in the instance dictionary
508 (so we can't use PyObject_GetAttr) but still binding
509 it to the instance. The arguments are the object,
510 the method name as a C string, and the address of a
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000511 static variable used to cache the interned Python string.
512
513 Two variants:
514
515 - lookup_maybe() returns NULL without raising an exception
516 when the _PyType_Lookup() call fails;
517
518 - lookup_method() always raises an exception upon errors.
519*/
Guido van Rossum60718732001-08-28 17:47:51 +0000520
521static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000522lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
Guido van Rossum60718732001-08-28 17:47:51 +0000523{
524 PyObject *res;
525
526 if (*attrobj == NULL) {
527 *attrobj = PyString_InternFromString(attrstr);
528 if (*attrobj == NULL)
529 return NULL;
530 }
531 res = _PyType_Lookup(self->ob_type, *attrobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000532 if (res != NULL) {
Guido van Rossum60718732001-08-28 17:47:51 +0000533 descrgetfunc f;
534 if ((f = res->ob_type->tp_descr_get) == NULL)
535 Py_INCREF(res);
536 else
537 res = f(res, self, (PyObject *)(self->ob_type));
538 }
539 return res;
540}
541
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000542static PyObject *
543lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
544{
545 PyObject *res = lookup_maybe(self, attrstr, attrobj);
546 if (res == NULL && !PyErr_Occurred())
547 PyErr_SetObject(PyExc_AttributeError, *attrobj);
548 return res;
549}
550
Guido van Rossum2730b132001-08-28 18:22:14 +0000551/* A variation of PyObject_CallMethod that uses lookup_method()
552 instead of PyObject_GetAttrString(). This uses the same convention
553 as lookup_method to cache the interned name string object. */
554
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000555static PyObject *
Guido van Rossum2730b132001-08-28 18:22:14 +0000556call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
557{
558 va_list va;
559 PyObject *args, *func = 0, *retval;
Guido van Rossum2730b132001-08-28 18:22:14 +0000560 va_start(va, format);
561
Guido van Rossumda21c012001-10-03 00:50:18 +0000562 func = lookup_maybe(o, name, nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000563 if (func == NULL) {
564 va_end(va);
565 if (!PyErr_Occurred())
Guido van Rossumda21c012001-10-03 00:50:18 +0000566 PyErr_SetObject(PyExc_AttributeError, *nameobj);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000567 return NULL;
568 }
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000569
570 if (format && *format)
571 args = Py_VaBuildValue(format, va);
572 else
573 args = PyTuple_New(0);
574
575 va_end(va);
576
577 if (args == NULL)
578 return NULL;
579
580 assert(PyTuple_Check(args));
581 retval = PyObject_Call(func, args, NULL);
582
583 Py_DECREF(args);
584 Py_DECREF(func);
585
586 return retval;
587}
588
589/* Clone of call_method() that returns NotImplemented when the lookup fails. */
590
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000591static PyObject *
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000592call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
593{
594 va_list va;
595 PyObject *args, *func = 0, *retval;
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000596 va_start(va, format);
597
Guido van Rossumda21c012001-10-03 00:50:18 +0000598 func = lookup_maybe(o, name, nameobj);
Guido van Rossum2730b132001-08-28 18:22:14 +0000599 if (func == NULL) {
600 va_end(va);
Guido van Rossumf21c6be2001-09-14 17:51:50 +0000601 if (!PyErr_Occurred()) {
602 Py_INCREF(Py_NotImplemented);
603 return Py_NotImplemented;
604 }
Guido van Rossum717ce002001-09-14 16:58:08 +0000605 return NULL;
Guido van Rossum2730b132001-08-28 18:22:14 +0000606 }
607
608 if (format && *format)
609 args = Py_VaBuildValue(format, va);
610 else
611 args = PyTuple_New(0);
612
613 va_end(va);
614
Guido van Rossum717ce002001-09-14 16:58:08 +0000615 if (args == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +0000616 return NULL;
617
Guido van Rossum717ce002001-09-14 16:58:08 +0000618 assert(PyTuple_Check(args));
619 retval = PyObject_Call(func, args, NULL);
Guido van Rossum2730b132001-08-28 18:22:14 +0000620
621 Py_DECREF(args);
622 Py_DECREF(func);
623
624 return retval;
625}
626
Tim Peters6d6c1a32001-08-02 04:15:00 +0000627/* Method resolution order algorithm from "Putting Metaclasses to Work"
628 by Forman and Danforth (Addison-Wesley 1999). */
629
630static int
631conservative_merge(PyObject *left, PyObject *right)
632{
633 int left_size;
634 int right_size;
635 int i, j, r, ok;
636 PyObject *temp, *rr;
637
638 assert(PyList_Check(left));
639 assert(PyList_Check(right));
640
641 again:
642 left_size = PyList_GET_SIZE(left);
643 right_size = PyList_GET_SIZE(right);
644 for (i = 0; i < left_size; i++) {
645 for (j = 0; j < right_size; j++) {
646 if (PyList_GET_ITEM(left, i) ==
647 PyList_GET_ITEM(right, j)) {
648 /* found a merge point */
649 temp = PyList_New(0);
650 if (temp == NULL)
651 return -1;
652 for (r = 0; r < j; r++) {
653 rr = PyList_GET_ITEM(right, r);
654 ok = PySequence_Contains(left, rr);
655 if (ok < 0) {
656 Py_DECREF(temp);
657 return -1;
658 }
659 if (!ok) {
660 ok = PyList_Append(temp, rr);
661 if (ok < 0) {
662 Py_DECREF(temp);
663 return -1;
664 }
665 }
666 }
667 ok = PyList_SetSlice(left, i, i, temp);
668 Py_DECREF(temp);
669 if (ok < 0)
670 return -1;
671 ok = PyList_SetSlice(right, 0, j+1, NULL);
672 if (ok < 0)
673 return -1;
674 goto again;
675 }
676 }
677 }
678 return PyList_SetSlice(left, left_size, left_size, right);
679}
680
681static int
682serious_order_disagreements(PyObject *left, PyObject *right)
683{
684 return 0; /* XXX later -- for now, we cheat: "don't do that" */
685}
686
Tim Petersa91e9642001-11-14 23:32:33 +0000687static int
688fill_classic_mro(PyObject *mro, PyObject *cls)
689{
690 PyObject *bases, *base;
691 int i, n;
692
693 assert(PyList_Check(mro));
694 assert(PyClass_Check(cls));
695 i = PySequence_Contains(mro, cls);
696 if (i < 0)
697 return -1;
698 if (!i) {
699 if (PyList_Append(mro, cls) < 0)
700 return -1;
701 }
702 bases = ((PyClassObject *)cls)->cl_bases;
703 assert(bases && PyTuple_Check(bases));
704 n = PyTuple_GET_SIZE(bases);
705 for (i = 0; i < n; i++) {
706 base = PyTuple_GET_ITEM(bases, i);
707 if (fill_classic_mro(mro, base) < 0)
708 return -1;
709 }
710 return 0;
711}
712
713static PyObject *
714classic_mro(PyObject *cls)
715{
716 PyObject *mro;
717
718 assert(PyClass_Check(cls));
719 mro = PyList_New(0);
720 if (mro != NULL) {
721 if (fill_classic_mro(mro, cls) == 0)
722 return mro;
723 Py_DECREF(mro);
724 }
725 return NULL;
726}
727
Tim Peters6d6c1a32001-08-02 04:15:00 +0000728static PyObject *
729mro_implementation(PyTypeObject *type)
730{
731 int i, n, ok;
732 PyObject *bases, *result;
733
734 bases = type->tp_bases;
735 n = PyTuple_GET_SIZE(bases);
736 result = Py_BuildValue("[O]", (PyObject *)type);
737 if (result == NULL)
738 return NULL;
739 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000740 PyObject *base = PyTuple_GET_ITEM(bases, i);
741 PyObject *parentMRO;
742 if (PyType_Check(base))
743 parentMRO = PySequence_List(
744 ((PyTypeObject*)base)->tp_mro);
745 else
746 parentMRO = classic_mro(base);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000747 if (parentMRO == NULL) {
748 Py_DECREF(result);
749 return NULL;
750 }
751 if (serious_order_disagreements(result, parentMRO)) {
752 Py_DECREF(result);
753 return NULL;
754 }
755 ok = conservative_merge(result, parentMRO);
756 Py_DECREF(parentMRO);
757 if (ok < 0) {
758 Py_DECREF(result);
759 return NULL;
760 }
761 }
762 return result;
763}
764
765static PyObject *
Martin v. Löwise3eb1f22001-08-16 13:15:00 +0000766mro_external(PyObject *self)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000767{
768 PyTypeObject *type = (PyTypeObject *)self;
769
Tim Peters6d6c1a32001-08-02 04:15:00 +0000770 return mro_implementation(type);
771}
772
773static int
774mro_internal(PyTypeObject *type)
775{
776 PyObject *mro, *result, *tuple;
777
778 if (type->ob_type == &PyType_Type) {
779 result = mro_implementation(type);
780 }
781 else {
Guido van Rossum60718732001-08-28 17:47:51 +0000782 static PyObject *mro_str;
783 mro = lookup_method((PyObject *)type, "mro", &mro_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000784 if (mro == NULL)
785 return -1;
786 result = PyObject_CallObject(mro, NULL);
787 Py_DECREF(mro);
788 }
789 if (result == NULL)
790 return -1;
791 tuple = PySequence_Tuple(result);
792 Py_DECREF(result);
793 type->tp_mro = tuple;
794 return 0;
795}
796
797
798/* Calculate the best base amongst multiple base classes.
799 This is the first one that's on the path to the "solid base". */
800
801static PyTypeObject *
802best_base(PyObject *bases)
803{
804 int i, n;
805 PyTypeObject *base, *winner, *candidate, *base_i;
Tim Petersa91e9642001-11-14 23:32:33 +0000806 PyObject *base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000807
808 assert(PyTuple_Check(bases));
809 n = PyTuple_GET_SIZE(bases);
810 assert(n > 0);
Tim Petersa91e9642001-11-14 23:32:33 +0000811 base = NULL;
812 winner = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000813 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +0000814 base_proto = PyTuple_GET_ITEM(bases, i);
815 if (PyClass_Check(base_proto))
816 continue;
817 if (!PyType_Check(base_proto)) {
Tim Peters6d6c1a32001-08-02 04:15:00 +0000818 PyErr_SetString(
819 PyExc_TypeError,
820 "bases must be types");
821 return NULL;
822 }
Tim Petersa91e9642001-11-14 23:32:33 +0000823 base_i = (PyTypeObject *)base_proto;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000824 if (base_i->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +0000825 if (PyType_Ready(base_i) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +0000826 return NULL;
827 }
828 candidate = solid_base(base_i);
Tim Petersa91e9642001-11-14 23:32:33 +0000829 if (winner == NULL) {
830 winner = candidate;
831 base = base_i;
832 }
833 else if (PyType_IsSubtype(winner, candidate))
Tim Peters6d6c1a32001-08-02 04:15:00 +0000834 ;
835 else if (PyType_IsSubtype(candidate, winner)) {
836 winner = candidate;
837 base = base_i;
838 }
839 else {
840 PyErr_SetString(
841 PyExc_TypeError,
842 "multiple bases have "
843 "instance lay-out conflict");
844 return NULL;
845 }
846 }
Guido van Rossume54616c2001-12-14 04:19:56 +0000847 if (base == NULL)
848 PyErr_SetString(PyExc_TypeError,
849 "a new-style class can't have only classic bases");
Tim Peters6d6c1a32001-08-02 04:15:00 +0000850 return base;
851}
852
853static int
854extra_ivars(PyTypeObject *type, PyTypeObject *base)
855{
Neil Schemenauerc806c882001-08-29 23:54:54 +0000856 size_t t_size = type->tp_basicsize;
857 size_t b_size = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000858
Guido van Rossum9676b222001-08-17 20:32:36 +0000859 assert(t_size >= b_size); /* Else type smaller than base! */
Tim Peters6d6c1a32001-08-02 04:15:00 +0000860 if (type->tp_itemsize || base->tp_itemsize) {
861 /* If itemsize is involved, stricter rules */
862 return t_size != b_size ||
863 type->tp_itemsize != base->tp_itemsize;
864 }
Guido van Rossum9676b222001-08-17 20:32:36 +0000865 if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
866 type->tp_weaklistoffset + sizeof(PyObject *) == t_size)
867 t_size -= sizeof(PyObject *);
868 if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
869 type->tp_dictoffset + sizeof(PyObject *) == t_size)
870 t_size -= sizeof(PyObject *);
871
872 return t_size != b_size;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000873}
874
875static PyTypeObject *
876solid_base(PyTypeObject *type)
877{
878 PyTypeObject *base;
879
880 if (type->tp_base)
881 base = solid_base(type->tp_base);
882 else
883 base = &PyBaseObject_Type;
884 if (extra_ivars(type, base))
885 return type;
886 else
887 return base;
888}
889
890staticforward void object_dealloc(PyObject *);
891staticforward int object_init(PyObject *, PyObject *, PyObject *);
Guido van Rossum875eeaa2001-10-11 18:33:53 +0000892staticforward int update_slot(PyTypeObject *, PyObject *);
Guido van Rossum7b9144b2001-10-09 19:39:46 +0000893staticforward void fixup_slot_dispatchers(PyTypeObject *);
Tim Peters6d6c1a32001-08-02 04:15:00 +0000894
895static PyObject *
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000896subtype_dict(PyObject *obj, void *context)
897{
898 PyObject **dictptr = _PyObject_GetDictPtr(obj);
899 PyObject *dict;
900
901 if (dictptr == NULL) {
902 PyErr_SetString(PyExc_AttributeError,
903 "This object has no __dict__");
904 return NULL;
905 }
906 dict = *dictptr;
Guido van Rossum3926a632001-09-25 16:25:58 +0000907 if (dict == NULL)
908 *dictptr = dict = PyDict_New();
909 Py_XINCREF(dict);
910 return dict;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000911}
912
Guido van Rossum6661be32001-10-26 04:26:12 +0000913static int
914subtype_setdict(PyObject *obj, PyObject *value, void *context)
915{
916 PyObject **dictptr = _PyObject_GetDictPtr(obj);
917 PyObject *dict;
918
919 if (dictptr == NULL) {
920 PyErr_SetString(PyExc_AttributeError,
921 "This object has no __dict__");
922 return -1;
923 }
Guido van Rossumd331cb52001-12-05 19:46:42 +0000924 if (value != NULL && !PyDict_Check(value)) {
Guido van Rossum6661be32001-10-26 04:26:12 +0000925 PyErr_SetString(PyExc_TypeError,
926 "__dict__ must be set to a dictionary");
927 return -1;
928 }
929 dict = *dictptr;
Guido van Rossumd331cb52001-12-05 19:46:42 +0000930 Py_XINCREF(value);
Guido van Rossum6661be32001-10-26 04:26:12 +0000931 *dictptr = value;
932 Py_XDECREF(dict);
933 return 0;
934}
935
Neil Schemenauerf23473f2001-10-21 22:28:58 +0000936static PyGetSetDef subtype_getsets[] = {
Guido van Rossum6661be32001-10-26 04:26:12 +0000937 {"__dict__", subtype_dict, subtype_setdict, NULL},
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000938 {0},
939};
940
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000941/* bozo: __getstate__ that raises TypeError */
942
943static PyObject *
944bozo_func(PyObject *self, PyObject *args)
945{
946 PyErr_SetString(PyExc_TypeError,
947 "a class that defines __slots__ without "
948 "defining __getstate__ cannot be pickled");
949 return NULL;
950}
951
Neal Norwitz93c1e232002-03-31 16:06:11 +0000952static PyMethodDef bozo_ml = {"__getstate__", bozo_func, METH_VARARGS};
Guido van Rossum0628dcf2002-03-14 23:03:14 +0000953
954static PyObject *bozo_obj = NULL;
955
Guido van Rossum9923ffe2002-06-04 19:52:53 +0000956static int
957valid_identifier(PyObject *s)
958{
959 char *p;
960 int i, n;
961
962 if (!PyString_Check(s)) {
963 PyErr_SetString(PyExc_TypeError,
964 "__slots__ must be strings");
965 return 0;
966 }
967 p = PyString_AS_STRING(s);
968 n = PyString_GET_SIZE(s);
969 /* We must reject an empty name. As a hack, we bump the
970 length to 1 so that the loop will balk on the trailing \0. */
971 if (n == 0)
972 n = 1;
973 for (i = 0; i < n; i++, p++) {
974 if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
975 PyErr_SetString(PyExc_TypeError,
976 "__slots__ must be identifiers");
977 return 0;
978 }
979 }
980 return 1;
981}
982
Guido van Rossum6fb3fde2001-08-30 20:00:07 +0000983static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +0000984type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
985{
986 PyObject *name, *bases, *dict;
987 static char *kwlist[] = {"name", "bases", "dict", 0};
988 PyObject *slots, *tmp;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000989 PyTypeObject *type, *base, *tmptype, *winner;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000990 etype *et;
Guido van Rossum6f799372001-09-20 20:46:19 +0000991 PyMemberDef *mp;
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +0000992 int i, nbases, nslots, slotoffset, add_dict, add_weak;
Tim Peters6d6c1a32001-08-02 04:15:00 +0000993
Tim Peters3abca122001-10-27 19:37:48 +0000994 assert(args != NULL && PyTuple_Check(args));
995 assert(kwds == NULL || PyDict_Check(kwds));
996
Guido van Rossum8d32c8b2001-08-17 11:18:38 +0000997 /* Special case: type(x) should return x->ob_type */
Tim Peters3abca122001-10-27 19:37:48 +0000998 {
999 const int nargs = PyTuple_GET_SIZE(args);
1000 const int nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
1001
1002 if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
1003 PyObject *x = PyTuple_GET_ITEM(args, 0);
1004 Py_INCREF(x->ob_type);
1005 return (PyObject *) x->ob_type;
1006 }
1007
1008 /* SF bug 475327 -- if that didn't trigger, we need 3
1009 arguments. but PyArg_ParseTupleAndKeywords below may give
1010 a msg saying type() needs exactly 3. */
1011 if (nargs + nkwds != 3) {
1012 PyErr_SetString(PyExc_TypeError,
1013 "type() takes 1 or 3 arguments");
1014 return NULL;
1015 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001016 }
1017
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001018 /* Check arguments: (name, bases, dict) */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001019 if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
1020 &name,
1021 &PyTuple_Type, &bases,
1022 &PyDict_Type, &dict))
1023 return NULL;
1024
1025 /* Determine the proper metatype to deal with this,
1026 and check for metatype conflicts while we're at it.
1027 Note that if some other metatype wins to contract,
1028 it's possible that its instances are not types. */
1029 nbases = PyTuple_GET_SIZE(bases);
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001030 winner = metatype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001031 for (i = 0; i < nbases; i++) {
1032 tmp = PyTuple_GET_ITEM(bases, i);
1033 tmptype = tmp->ob_type;
Tim Petersa91e9642001-11-14 23:32:33 +00001034 if (tmptype == &PyClass_Type)
1035 continue; /* Special case classic classes */
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001036 if (PyType_IsSubtype(winner, tmptype))
Tim Peters6d6c1a32001-08-02 04:15:00 +00001037 continue;
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001038 if (PyType_IsSubtype(tmptype, winner)) {
1039 winner = tmptype;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001040 continue;
1041 }
1042 PyErr_SetString(PyExc_TypeError,
1043 "metatype conflict among bases");
1044 return NULL;
1045 }
Guido van Rossum8d32c8b2001-08-17 11:18:38 +00001046 if (winner != metatype) {
1047 if (winner->tp_new != type_new) /* Pass it to the winner */
1048 return winner->tp_new(winner, args, kwds);
1049 metatype = winner;
1050 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001051
1052 /* Adjust for empty tuple bases */
1053 if (nbases == 0) {
1054 bases = Py_BuildValue("(O)", &PyBaseObject_Type);
1055 if (bases == NULL)
1056 return NULL;
1057 nbases = 1;
1058 }
1059 else
1060 Py_INCREF(bases);
1061
1062 /* XXX From here until type is allocated, "return NULL" leaks bases! */
1063
1064 /* Calculate best base, and check that all bases are type objects */
1065 base = best_base(bases);
1066 if (base == NULL)
1067 return NULL;
1068 if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
1069 PyErr_Format(PyExc_TypeError,
1070 "type '%.100s' is not an acceptable base type",
1071 base->tp_name);
1072 return NULL;
1073 }
1074
Tim Peters6d6c1a32001-08-02 04:15:00 +00001075 /* Check for a __slots__ sequence variable in dict, and count it */
1076 slots = PyDict_GetItemString(dict, "__slots__");
1077 nslots = 0;
Guido van Rossum9676b222001-08-17 20:32:36 +00001078 add_dict = 0;
1079 add_weak = 0;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001080 if (slots != NULL) {
1081 /* Make it into a tuple */
1082 if (PyString_Check(slots))
1083 slots = Py_BuildValue("(O)", slots);
1084 else
1085 slots = PySequence_Tuple(slots);
1086 if (slots == NULL)
1087 return NULL;
1088 nslots = PyTuple_GET_SIZE(slots);
Guido van Rossumc4141872001-08-30 04:43:35 +00001089 if (nslots > 0 && base->tp_itemsize != 0) {
1090 PyErr_Format(PyExc_TypeError,
1091 "nonempty __slots__ "
1092 "not supported for subtype of '%s'",
1093 base->tp_name);
1094 return NULL;
1095 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001096 for (i = 0; i < nslots; i++) {
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001097 if (!valid_identifier(PyTuple_GET_ITEM(slots, i))) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001098 Py_DECREF(slots);
1099 return NULL;
1100 }
1101 }
1102 }
Guido van Rossum0628dcf2002-03-14 23:03:14 +00001103 if (slots != NULL) {
1104 /* See if *this* class defines __getstate__ */
1105 PyObject *getstate = PyDict_GetItemString(dict,
1106 "__getstate__");
1107 if (getstate == NULL) {
1108 /* If not, provide a bozo that raises TypeError */
1109 if (bozo_obj == NULL) {
1110 bozo_obj = PyCFunction_New(&bozo_ml, NULL);
1111 if (bozo_obj == NULL) {
1112 /* XXX decref various things */
1113 return NULL;
1114 }
1115 }
1116 if (PyDict_SetItemString(dict,
1117 "__getstate__",
1118 bozo_obj) < 0) {
1119 /* XXX decref various things */
1120 return NULL;
1121 }
1122 }
1123 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001124 if (slots == NULL && base->tp_dictoffset == 0 &&
1125 (base->tp_setattro == PyObject_GenericSetAttr ||
Guido van Rossum9676b222001-08-17 20:32:36 +00001126 base->tp_setattro == NULL)) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001127 add_dict++;
1128 }
Guido van Rossumc4141872001-08-30 04:43:35 +00001129 if (slots == NULL && base->tp_weaklistoffset == 0 &&
1130 base->tp_itemsize == 0) {
Guido van Rossum9676b222001-08-17 20:32:36 +00001131 nslots++;
1132 add_weak++;
1133 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001134
1135 /* XXX From here until type is safely allocated,
1136 "return NULL" may leak slots! */
1137
1138 /* Allocate the type object */
1139 type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
1140 if (type == NULL)
1141 return NULL;
1142
1143 /* Keep name and slots alive in the extended type object */
1144 et = (etype *)type;
1145 Py_INCREF(name);
1146 et->name = name;
1147 et->slots = slots;
1148
Guido van Rossumdc91b992001-08-08 22:26:22 +00001149 /* Initialize tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001150 type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
1151 Py_TPFLAGS_BASETYPE;
Guido van Rossum048eb752001-10-02 21:24:57 +00001152 if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
1153 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossumdc91b992001-08-08 22:26:22 +00001154
1155 /* It's a new-style number unless it specifically inherits any
1156 old-style numeric behavior */
1157 if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
1158 (base->tp_as_number == NULL))
1159 type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
1160
1161 /* Initialize essential fields */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001162 type->tp_as_number = &et->as_number;
1163 type->tp_as_sequence = &et->as_sequence;
1164 type->tp_as_mapping = &et->as_mapping;
1165 type->tp_as_buffer = &et->as_buffer;
1166 type->tp_name = PyString_AS_STRING(name);
1167
1168 /* Set tp_base and tp_bases */
1169 type->tp_bases = bases;
1170 Py_INCREF(base);
1171 type->tp_base = base;
1172
Guido van Rossum687ae002001-10-15 22:03:32 +00001173 /* Initialize tp_dict from passed-in dict */
1174 type->tp_dict = dict = PyDict_Copy(dict);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001175 if (dict == NULL) {
1176 Py_DECREF(type);
1177 return NULL;
1178 }
1179
Guido van Rossumc3542212001-08-16 09:18:56 +00001180 /* Set __module__ in the dict */
1181 if (PyDict_GetItemString(dict, "__module__") == NULL) {
1182 tmp = PyEval_GetGlobals();
1183 if (tmp != NULL) {
1184 tmp = PyDict_GetItemString(tmp, "__name__");
1185 if (tmp != NULL) {
1186 if (PyDict_SetItemString(dict, "__module__",
1187 tmp) < 0)
1188 return NULL;
1189 }
1190 }
1191 }
1192
Tim Peters2f93e282001-10-04 05:27:00 +00001193 /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
Tim Peters24008312002-03-17 18:56:20 +00001194 and is a string. The __doc__ accessor will first look for tp_doc;
1195 if that fails, it will still look into __dict__.
Tim Peters2f93e282001-10-04 05:27:00 +00001196 */
1197 {
1198 PyObject *doc = PyDict_GetItemString(dict, "__doc__");
1199 if (doc != NULL && PyString_Check(doc)) {
1200 const size_t n = (size_t)PyString_GET_SIZE(doc);
Tim Peters59f809d2001-10-04 05:43:02 +00001201 type->tp_doc = (char *)PyObject_MALLOC(n+1);
Tim Peters2f93e282001-10-04 05:27:00 +00001202 if (type->tp_doc == NULL) {
1203 Py_DECREF(type);
1204 return NULL;
1205 }
1206 memcpy(type->tp_doc, PyString_AS_STRING(doc), n+1);
1207 }
1208 }
1209
Tim Peters6d6c1a32001-08-02 04:15:00 +00001210 /* Special-case __new__: if it's a plain function,
1211 make it a static function */
1212 tmp = PyDict_GetItemString(dict, "__new__");
1213 if (tmp != NULL && PyFunction_Check(tmp)) {
1214 tmp = PyStaticMethod_New(tmp);
1215 if (tmp == NULL) {
1216 Py_DECREF(type);
1217 return NULL;
1218 }
1219 PyDict_SetItemString(dict, "__new__", tmp);
1220 Py_DECREF(tmp);
1221 }
1222
1223 /* Add descriptors for custom slots from __slots__, or for __dict__ */
1224 mp = et->members;
Neil Schemenauerc806c882001-08-29 23:54:54 +00001225 slotoffset = base->tp_basicsize;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001226 if (slots != NULL) {
1227 for (i = 0; i < nslots; i++, mp++) {
1228 mp->name = PyString_AS_STRING(
1229 PyTuple_GET_ITEM(slots, i));
Guido van Rossum64b206c2001-12-04 17:13:22 +00001230 mp->type = T_OBJECT_EX;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001231 mp->offset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001232 if (base->tp_weaklistoffset == 0 &&
Guido van Rossum64b206c2001-12-04 17:13:22 +00001233 strcmp(mp->name, "__weakref__") == 0) {
1234 mp->type = T_OBJECT;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001235 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001236 type->tp_weaklistoffset = slotoffset;
Guido van Rossum64b206c2001-12-04 17:13:22 +00001237 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001238 slotoffset += sizeof(PyObject *);
1239 }
1240 }
Guido van Rossum9676b222001-08-17 20:32:36 +00001241 else {
1242 if (add_dict) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001243 if (base->tp_itemsize)
Guido van Rossum048eb752001-10-02 21:24:57 +00001244 type->tp_dictoffset =
1245 -(long)sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001246 else
1247 type->tp_dictoffset = slotoffset;
Guido van Rossum9676b222001-08-17 20:32:36 +00001248 slotoffset += sizeof(PyObject *);
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001249 type->tp_getset = subtype_getsets;
Guido van Rossum9676b222001-08-17 20:32:36 +00001250 }
1251 if (add_weak) {
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001252 assert(!base->tp_itemsize);
Guido van Rossum9676b222001-08-17 20:32:36 +00001253 type->tp_weaklistoffset = slotoffset;
1254 mp->name = "__weakref__";
1255 mp->type = T_OBJECT;
1256 mp->offset = slotoffset;
Tim Peters26f68f52001-09-18 00:23:33 +00001257 mp->flags = READONLY;
Guido van Rossum9676b222001-08-17 20:32:36 +00001258 mp++;
1259 slotoffset += sizeof(PyObject *);
1260 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001261 }
1262 type->tp_basicsize = slotoffset;
Guido van Rossum6fb3fde2001-08-30 20:00:07 +00001263 type->tp_itemsize = base->tp_itemsize;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001264 type->tp_members = et->members;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001265
1266 /* Special case some slots */
1267 if (type->tp_dictoffset != 0 || nslots > 0) {
1268 if (base->tp_getattr == NULL && base->tp_getattro == NULL)
1269 type->tp_getattro = PyObject_GenericGetAttr;
1270 if (base->tp_setattr == NULL && base->tp_setattro == NULL)
1271 type->tp_setattro = PyObject_GenericSetAttr;
1272 }
1273 type->tp_dealloc = subtype_dealloc;
1274
Guido van Rossum9475a232001-10-05 20:51:39 +00001275 /* Enable GC unless there are really no instance variables possible */
1276 if (!(type->tp_basicsize == sizeof(PyObject) &&
1277 type->tp_itemsize == 0))
1278 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
1279
Tim Peters6d6c1a32001-08-02 04:15:00 +00001280 /* Always override allocation strategy to use regular heap */
1281 type->tp_alloc = PyType_GenericAlloc;
Guido van Rossum048eb752001-10-02 21:24:57 +00001282 if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001283 type->tp_free = PyObject_GC_Del;
Guido van Rossum9475a232001-10-05 20:51:39 +00001284 type->tp_traverse = subtype_traverse;
Guido van Rossum9923ffe2002-06-04 19:52:53 +00001285 type->tp_clear = subtype_clear;
Guido van Rossum048eb752001-10-02 21:24:57 +00001286 }
1287 else
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001288 type->tp_free = PyObject_Del;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001289
1290 /* Initialize the rest */
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001291 if (PyType_Ready(type) < 0) {
Tim Peters6d6c1a32001-08-02 04:15:00 +00001292 Py_DECREF(type);
1293 return NULL;
1294 }
1295
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001296 /* Put the proper slots in place */
1297 fixup_slot_dispatchers(type);
Guido van Rossumf040ede2001-08-07 16:40:56 +00001298
Tim Peters6d6c1a32001-08-02 04:15:00 +00001299 return (PyObject *)type;
1300}
1301
1302/* Internal API to look for a name through the MRO.
1303 This returns a borrowed reference, and doesn't set an exception! */
1304PyObject *
1305_PyType_Lookup(PyTypeObject *type, PyObject *name)
1306{
1307 int i, n;
Tim Petersa91e9642001-11-14 23:32:33 +00001308 PyObject *mro, *res, *base, *dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001309
Guido van Rossum687ae002001-10-15 22:03:32 +00001310 /* Look in tp_dict of types in MRO */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001311 mro = type->tp_mro;
Guido van Rossum23094982002-06-10 14:30:43 +00001312
1313 /* If mro is NULL, the type is either not yet initialized
1314 by PyType_Ready(), or already cleared by type_clear().
1315 Either way the safest thing to do is to return NULL. */
1316 if (mro == NULL)
1317 return NULL;
1318
Tim Peters6d6c1a32001-08-02 04:15:00 +00001319 assert(PyTuple_Check(mro));
1320 n = PyTuple_GET_SIZE(mro);
1321 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001322 base = PyTuple_GET_ITEM(mro, i);
1323 if (PyClass_Check(base))
1324 dict = ((PyClassObject *)base)->cl_dict;
1325 else {
1326 assert(PyType_Check(base));
1327 dict = ((PyTypeObject *)base)->tp_dict;
1328 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001329 assert(dict && PyDict_Check(dict));
1330 res = PyDict_GetItem(dict, name);
1331 if (res != NULL)
1332 return res;
1333 }
1334 return NULL;
1335}
1336
1337/* This is similar to PyObject_GenericGetAttr(),
1338 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1339static PyObject *
1340type_getattro(PyTypeObject *type, PyObject *name)
1341{
1342 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001343 PyObject *meta_attribute, *attribute;
1344 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001345
1346 /* Initialize this type (we'll assume the metatype is initialized) */
1347 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001348 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001349 return NULL;
1350 }
1351
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001352 /* No readable descriptor found yet */
1353 meta_get = NULL;
1354
1355 /* Look for the attribute in the metatype */
1356 meta_attribute = _PyType_Lookup(metatype, name);
1357
1358 if (meta_attribute != NULL) {
1359 meta_get = meta_attribute->ob_type->tp_descr_get;
1360
1361 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
1362 /* Data descriptors implement tp_descr_set to intercept
1363 * writes. Assume the attribute is not overridden in
1364 * type's tp_dict (and bases): call the descriptor now.
1365 */
1366 return meta_get(meta_attribute, (PyObject *)type,
1367 (PyObject *)metatype);
1368 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001369 }
1370
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001371 /* No data descriptor found on metatype. Look in tp_dict of this
1372 * type and its bases */
1373 attribute = _PyType_Lookup(type, name);
1374 if (attribute != NULL) {
1375 /* Implement descriptor functionality, if any */
1376 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
1377 if (local_get != NULL) {
1378 /* NULL 2nd argument indicates the descriptor was
1379 * found on the target object itself (or a base) */
1380 return local_get(attribute, (PyObject *)NULL,
1381 (PyObject *)type);
1382 }
1383
1384 Py_INCREF(attribute);
1385 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001386 }
1387
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001388 /* No attribute found in local __dict__ (or bases): use the
1389 * descriptor from the metatype, if any */
1390 if (meta_get != NULL)
1391 return meta_get(meta_attribute, (PyObject *)type,
1392 (PyObject *)metatype);
1393
1394 /* If an ordinary attribute was found on the metatype, return it now */
1395 if (meta_attribute != NULL) {
1396 Py_INCREF(meta_attribute);
1397 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001398 }
1399
1400 /* Give up */
1401 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001402 "type object '%.50s' has no attribute '%.400s'",
1403 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001404 return NULL;
1405}
1406
1407static int
1408type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1409{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001410 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1411 PyErr_Format(
1412 PyExc_TypeError,
1413 "can't set attributes of built-in/extension type '%s'",
1414 type->tp_name);
1415 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001416 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001417 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1418 return -1;
1419 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001420}
1421
1422static void
1423type_dealloc(PyTypeObject *type)
1424{
1425 etype *et;
1426
1427 /* Assert this is a heap-allocated type object */
1428 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001429 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001430 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001431 et = (etype *)type;
1432 Py_XDECREF(type->tp_base);
1433 Py_XDECREF(type->tp_dict);
1434 Py_XDECREF(type->tp_bases);
1435 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001436 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001437 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001438 Py_XDECREF(et->name);
1439 Py_XDECREF(et->slots);
1440 type->ob_type->tp_free((PyObject *)type);
1441}
1442
Guido van Rossum1c450732001-10-08 15:18:27 +00001443static PyObject *
1444type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1445{
1446 PyObject *list, *raw, *ref;
1447 int i, n;
1448
1449 list = PyList_New(0);
1450 if (list == NULL)
1451 return NULL;
1452 raw = type->tp_subclasses;
1453 if (raw == NULL)
1454 return list;
1455 assert(PyList_Check(raw));
1456 n = PyList_GET_SIZE(raw);
1457 for (i = 0; i < n; i++) {
1458 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001459 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001460 ref = PyWeakref_GET_OBJECT(ref);
1461 if (ref != Py_None) {
1462 if (PyList_Append(list, ref) < 0) {
1463 Py_DECREF(list);
1464 return NULL;
1465 }
1466 }
1467 }
1468 return list;
1469}
1470
Tim Peters6d6c1a32001-08-02 04:15:00 +00001471static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001472 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001473 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001474 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1475 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001476 {0}
1477};
1478
1479static char type_doc[] =
1480"type(object) -> the object's type\n"
1481"type(name, bases, dict) -> a new type";
1482
Guido van Rossum048eb752001-10-02 21:24:57 +00001483static int
1484type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1485{
1486 etype *et;
1487 int err;
1488
1489 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1490 return 0;
1491
1492 et = (etype *)type;
1493
1494#define VISIT(SLOT) \
1495 if (SLOT) { \
1496 err = visit((PyObject *)(SLOT), arg); \
1497 if (err) \
1498 return err; \
1499 }
1500
1501 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001502 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001503 VISIT(type->tp_mro);
1504 VISIT(type->tp_bases);
1505 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001506 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001507 VISIT(et->slots);
1508
1509#undef VISIT
1510
1511 return 0;
1512}
1513
1514static int
1515type_clear(PyTypeObject *type)
1516{
1517 etype *et;
1518 PyObject *tmp;
1519
1520 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1521 return 0;
1522
1523 et = (etype *)type;
1524
1525#define CLEAR(SLOT) \
1526 if (SLOT) { \
1527 tmp = (PyObject *)(SLOT); \
1528 SLOT = NULL; \
1529 Py_DECREF(tmp); \
1530 }
1531
1532 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001533 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001534 CLEAR(type->tp_mro);
1535 CLEAR(type->tp_bases);
1536 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001537 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001538 CLEAR(et->slots);
1539
Tim Peters2f93e282001-10-04 05:27:00 +00001540 if (type->tp_doc != NULL) {
1541 PyObject_FREE(type->tp_doc);
1542 type->tp_doc = NULL;
1543 }
1544
Guido van Rossum048eb752001-10-02 21:24:57 +00001545#undef CLEAR
1546
1547 return 0;
1548}
1549
1550static int
1551type_is_gc(PyTypeObject *type)
1552{
1553 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1554}
1555
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001556PyTypeObject PyType_Type = {
1557 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001558 0, /* ob_size */
1559 "type", /* tp_name */
1560 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001561 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001562 (destructor)type_dealloc, /* tp_dealloc */
1563 0, /* tp_print */
1564 0, /* tp_getattr */
1565 0, /* tp_setattr */
1566 type_compare, /* tp_compare */
1567 (reprfunc)type_repr, /* tp_repr */
1568 0, /* tp_as_number */
1569 0, /* tp_as_sequence */
1570 0, /* tp_as_mapping */
1571 (hashfunc)_Py_HashPointer, /* tp_hash */
1572 (ternaryfunc)type_call, /* tp_call */
1573 0, /* tp_str */
1574 (getattrofunc)type_getattro, /* tp_getattro */
1575 (setattrofunc)type_setattro, /* tp_setattro */
1576 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001577 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1578 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001579 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001580 (traverseproc)type_traverse, /* tp_traverse */
1581 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001582 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001583 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001584 0, /* tp_iter */
1585 0, /* tp_iternext */
1586 type_methods, /* tp_methods */
1587 type_members, /* tp_members */
1588 type_getsets, /* tp_getset */
1589 0, /* tp_base */
1590 0, /* tp_dict */
1591 0, /* tp_descr_get */
1592 0, /* tp_descr_set */
1593 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1594 0, /* tp_init */
1595 0, /* tp_alloc */
1596 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001597 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00001598 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001599};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001600
1601
1602/* The base type of all types (eventually)... except itself. */
1603
1604static int
1605object_init(PyObject *self, PyObject *args, PyObject *kwds)
1606{
1607 return 0;
1608}
1609
1610static void
1611object_dealloc(PyObject *self)
1612{
1613 self->ob_type->tp_free(self);
1614}
1615
Guido van Rossum8e248182001-08-12 05:17:56 +00001616static PyObject *
1617object_repr(PyObject *self)
1618{
Guido van Rossum76e69632001-08-16 18:52:43 +00001619 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001620 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001621
Guido van Rossum76e69632001-08-16 18:52:43 +00001622 type = self->ob_type;
1623 mod = type_module(type, NULL);
1624 if (mod == NULL)
1625 PyErr_Clear();
1626 else if (!PyString_Check(mod)) {
1627 Py_DECREF(mod);
1628 mod = NULL;
1629 }
1630 name = type_name(type, NULL);
1631 if (name == NULL)
1632 return NULL;
1633 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001634 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001635 PyString_AS_STRING(mod),
1636 PyString_AS_STRING(name),
1637 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001638 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001639 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001640 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001641 Py_XDECREF(mod);
1642 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001643 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001644}
1645
Guido van Rossumb8f63662001-08-15 23:57:02 +00001646static PyObject *
1647object_str(PyObject *self)
1648{
1649 unaryfunc f;
1650
1651 f = self->ob_type->tp_repr;
1652 if (f == NULL)
1653 f = object_repr;
1654 return f(self);
1655}
1656
Guido van Rossum8e248182001-08-12 05:17:56 +00001657static long
1658object_hash(PyObject *self)
1659{
1660 return _Py_HashPointer(self);
1661}
Guido van Rossum8e248182001-08-12 05:17:56 +00001662
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001663static PyObject *
1664object_get_class(PyObject *self, void *closure)
1665{
1666 Py_INCREF(self->ob_type);
1667 return (PyObject *)(self->ob_type);
1668}
1669
1670static int
1671equiv_structs(PyTypeObject *a, PyTypeObject *b)
1672{
1673 return a == b ||
1674 (a != NULL &&
1675 b != NULL &&
1676 a->tp_basicsize == b->tp_basicsize &&
1677 a->tp_itemsize == b->tp_itemsize &&
1678 a->tp_dictoffset == b->tp_dictoffset &&
1679 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1680 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1681 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1682}
1683
1684static int
1685same_slots_added(PyTypeObject *a, PyTypeObject *b)
1686{
1687 PyTypeObject *base = a->tp_base;
1688 int size;
1689
1690 if (base != b->tp_base)
1691 return 0;
1692 if (equiv_structs(a, base) && equiv_structs(b, base))
1693 return 1;
1694 size = base->tp_basicsize;
1695 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1696 size += sizeof(PyObject *);
1697 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1698 size += sizeof(PyObject *);
1699 return size == a->tp_basicsize && size == b->tp_basicsize;
1700}
1701
1702static int
1703object_set_class(PyObject *self, PyObject *value, void *closure)
1704{
1705 PyTypeObject *old = self->ob_type;
1706 PyTypeObject *new, *newbase, *oldbase;
1707
Guido van Rossumb6b89422002-04-15 01:03:30 +00001708 if (value == NULL) {
1709 PyErr_SetString(PyExc_TypeError,
1710 "can't delete __class__ attribute");
1711 return -1;
1712 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001713 if (!PyType_Check(value)) {
1714 PyErr_Format(PyExc_TypeError,
1715 "__class__ must be set to new-style class, not '%s' object",
1716 value->ob_type->tp_name);
1717 return -1;
1718 }
1719 new = (PyTypeObject *)value;
Guido van Rossum9ee4b942002-05-24 18:47:47 +00001720 if (new->tp_dealloc != old->tp_dealloc ||
1721 new->tp_free != old->tp_free)
1722 {
1723 PyErr_Format(PyExc_TypeError,
1724 "__class__ assignment: "
1725 "'%s' deallocator differs from '%s'",
1726 new->tp_name,
1727 old->tp_name);
1728 return -1;
1729 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001730 newbase = new;
1731 oldbase = old;
1732 while (equiv_structs(newbase, newbase->tp_base))
1733 newbase = newbase->tp_base;
1734 while (equiv_structs(oldbase, oldbase->tp_base))
1735 oldbase = oldbase->tp_base;
1736 if (newbase != oldbase &&
1737 (newbase->tp_base != oldbase->tp_base ||
1738 !same_slots_added(newbase, oldbase))) {
1739 PyErr_Format(PyExc_TypeError,
1740 "__class__ assignment: "
1741 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001742 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001743 old->tp_name);
1744 return -1;
1745 }
1746 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1747 Py_INCREF(new);
1748 }
1749 self->ob_type = new;
1750 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1751 Py_DECREF(old);
1752 }
1753 return 0;
1754}
1755
1756static PyGetSetDef object_getsets[] = {
1757 {"__class__", object_get_class, object_set_class,
1758 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001759 {0}
1760};
1761
Guido van Rossum3926a632001-09-25 16:25:58 +00001762static PyObject *
1763object_reduce(PyObject *self, PyObject *args)
1764{
1765 /* Call copy_reg._reduce(self) */
1766 static PyObject *copy_reg_str;
1767 PyObject *copy_reg, *res;
1768
1769 if (!copy_reg_str) {
1770 copy_reg_str = PyString_InternFromString("copy_reg");
1771 if (copy_reg_str == NULL)
1772 return NULL;
1773 }
1774 copy_reg = PyImport_Import(copy_reg_str);
1775 if (!copy_reg)
1776 return NULL;
1777 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1778 Py_DECREF(copy_reg);
1779 return res;
1780}
1781
1782static PyMethodDef object_methods[] = {
1783 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1784 {0}
1785};
1786
Tim Peters6d6c1a32001-08-02 04:15:00 +00001787PyTypeObject PyBaseObject_Type = {
1788 PyObject_HEAD_INIT(&PyType_Type)
1789 0, /* ob_size */
1790 "object", /* tp_name */
1791 sizeof(PyObject), /* tp_basicsize */
1792 0, /* tp_itemsize */
1793 (destructor)object_dealloc, /* tp_dealloc */
1794 0, /* tp_print */
1795 0, /* tp_getattr */
1796 0, /* tp_setattr */
1797 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001798 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001799 0, /* tp_as_number */
1800 0, /* tp_as_sequence */
1801 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001802 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001803 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001804 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001805 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001806 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001807 0, /* tp_as_buffer */
1808 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1809 "The most base type", /* tp_doc */
1810 0, /* tp_traverse */
1811 0, /* tp_clear */
1812 0, /* tp_richcompare */
1813 0, /* tp_weaklistoffset */
1814 0, /* tp_iter */
1815 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001816 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001817 0, /* tp_members */
1818 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001819 0, /* tp_base */
1820 0, /* tp_dict */
1821 0, /* tp_descr_get */
1822 0, /* tp_descr_set */
1823 0, /* tp_dictoffset */
1824 object_init, /* tp_init */
1825 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001826 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001827 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828};
1829
1830
1831/* Initialize the __dict__ in a type object */
1832
Fred Drake7bf97152002-03-28 05:33:33 +00001833static PyObject *
1834create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *))
1835{
1836 PyObject *cfunc;
1837 PyObject *result;
1838
1839 cfunc = PyCFunction_New(meth, NULL);
1840 if (cfunc == NULL)
1841 return NULL;
1842 result = func(cfunc);
1843 Py_DECREF(cfunc);
1844 return result;
1845}
1846
Tim Peters6d6c1a32001-08-02 04:15:00 +00001847static int
1848add_methods(PyTypeObject *type, PyMethodDef *meth)
1849{
Guido van Rossum687ae002001-10-15 22:03:32 +00001850 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001851
1852 for (; meth->ml_name != NULL; meth++) {
1853 PyObject *descr;
1854 if (PyDict_GetItemString(dict, meth->ml_name))
1855 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00001856 if (meth->ml_flags & METH_CLASS) {
1857 if (meth->ml_flags & METH_STATIC) {
1858 PyErr_SetString(PyExc_ValueError,
1859 "method cannot be both class and static");
1860 return -1;
1861 }
1862 descr = create_specialmethod(meth, PyClassMethod_New);
1863 }
1864 else if (meth->ml_flags & METH_STATIC) {
1865 descr = create_specialmethod(meth, PyStaticMethod_New);
1866 }
1867 else {
1868 descr = PyDescr_NewMethod(type, meth);
1869 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001870 if (descr == NULL)
1871 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00001872 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001873 return -1;
1874 Py_DECREF(descr);
1875 }
1876 return 0;
1877}
1878
1879static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001880add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001881{
Guido van Rossum687ae002001-10-15 22:03:32 +00001882 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001883
1884 for (; memb->name != NULL; memb++) {
1885 PyObject *descr;
1886 if (PyDict_GetItemString(dict, memb->name))
1887 continue;
1888 descr = PyDescr_NewMember(type, memb);
1889 if (descr == NULL)
1890 return -1;
1891 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1892 return -1;
1893 Py_DECREF(descr);
1894 }
1895 return 0;
1896}
1897
1898static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001899add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001900{
Guido van Rossum687ae002001-10-15 22:03:32 +00001901 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001902
1903 for (; gsp->name != NULL; gsp++) {
1904 PyObject *descr;
1905 if (PyDict_GetItemString(dict, gsp->name))
1906 continue;
1907 descr = PyDescr_NewGetSet(type, gsp);
1908
1909 if (descr == NULL)
1910 return -1;
1911 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1912 return -1;
1913 Py_DECREF(descr);
1914 }
1915 return 0;
1916}
1917
Guido van Rossum13d52f02001-08-10 21:24:08 +00001918static void
1919inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001920{
1921 int oldsize, newsize;
1922
Guido van Rossum13d52f02001-08-10 21:24:08 +00001923 /* Special flag magic */
1924 if (!type->tp_as_buffer && base->tp_as_buffer) {
1925 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1926 type->tp_flags |=
1927 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1928 }
1929 if (!type->tp_as_sequence && base->tp_as_sequence) {
1930 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1931 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1932 }
1933 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1934 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1935 if ((!type->tp_as_number && base->tp_as_number) ||
1936 (!type->tp_as_sequence && base->tp_as_sequence)) {
1937 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1938 if (!type->tp_as_number && !type->tp_as_sequence) {
1939 type->tp_flags |= base->tp_flags &
1940 Py_TPFLAGS_HAVE_INPLACEOPS;
1941 }
1942 }
1943 /* Wow */
1944 }
1945 if (!type->tp_as_number && base->tp_as_number) {
1946 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1947 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1948 }
1949
1950 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001951 oldsize = base->tp_basicsize;
1952 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1953 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1954 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001955 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1956 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001957 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001958 if (type->tp_traverse == NULL)
1959 type->tp_traverse = base->tp_traverse;
1960 if (type->tp_clear == NULL)
1961 type->tp_clear = base->tp_clear;
1962 }
1963 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001964 /* The condition below could use some explanation.
1965 It appears that tp_new is not inherited for static types
1966 whose base class is 'object'; this seems to be a precaution
1967 so that old extension types don't suddenly become
1968 callable (object.__new__ wouldn't insure the invariants
1969 that the extension type's own factory function ensures).
1970 Heap types, of course, are under our control, so they do
1971 inherit tp_new; static extension types that specify some
1972 other built-in type as the default are considered
1973 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001974 if (base != &PyBaseObject_Type ||
1975 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1976 if (type->tp_new == NULL)
1977 type->tp_new = base->tp_new;
1978 }
1979 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001980 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001981
1982 /* Copy other non-function slots */
1983
1984#undef COPYVAL
1985#define COPYVAL(SLOT) \
1986 if (type->SLOT == 0) type->SLOT = base->SLOT
1987
1988 COPYVAL(tp_itemsize);
1989 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1990 COPYVAL(tp_weaklistoffset);
1991 }
1992 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
1993 COPYVAL(tp_dictoffset);
1994 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00001995}
1996
1997static void
1998inherit_slots(PyTypeObject *type, PyTypeObject *base)
1999{
2000 PyTypeObject *basebase;
2001
2002#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002003#undef COPYSLOT
2004#undef COPYNUM
2005#undef COPYSEQ
2006#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002007#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002008
2009#define SLOTDEFINED(SLOT) \
2010 (base->SLOT != 0 && \
2011 (basebase == NULL || base->SLOT != basebase->SLOT))
2012
Tim Peters6d6c1a32001-08-02 04:15:00 +00002013#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002014 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002015
2016#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2017#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2018#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002019#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002020
Guido van Rossum13d52f02001-08-10 21:24:08 +00002021 /* This won't inherit indirect slots (from tp_as_number etc.)
2022 if type doesn't provide the space. */
2023
2024 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2025 basebase = base->tp_base;
2026 if (basebase->tp_as_number == NULL)
2027 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002028 COPYNUM(nb_add);
2029 COPYNUM(nb_subtract);
2030 COPYNUM(nb_multiply);
2031 COPYNUM(nb_divide);
2032 COPYNUM(nb_remainder);
2033 COPYNUM(nb_divmod);
2034 COPYNUM(nb_power);
2035 COPYNUM(nb_negative);
2036 COPYNUM(nb_positive);
2037 COPYNUM(nb_absolute);
2038 COPYNUM(nb_nonzero);
2039 COPYNUM(nb_invert);
2040 COPYNUM(nb_lshift);
2041 COPYNUM(nb_rshift);
2042 COPYNUM(nb_and);
2043 COPYNUM(nb_xor);
2044 COPYNUM(nb_or);
2045 COPYNUM(nb_coerce);
2046 COPYNUM(nb_int);
2047 COPYNUM(nb_long);
2048 COPYNUM(nb_float);
2049 COPYNUM(nb_oct);
2050 COPYNUM(nb_hex);
2051 COPYNUM(nb_inplace_add);
2052 COPYNUM(nb_inplace_subtract);
2053 COPYNUM(nb_inplace_multiply);
2054 COPYNUM(nb_inplace_divide);
2055 COPYNUM(nb_inplace_remainder);
2056 COPYNUM(nb_inplace_power);
2057 COPYNUM(nb_inplace_lshift);
2058 COPYNUM(nb_inplace_rshift);
2059 COPYNUM(nb_inplace_and);
2060 COPYNUM(nb_inplace_xor);
2061 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002062 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2063 COPYNUM(nb_true_divide);
2064 COPYNUM(nb_floor_divide);
2065 COPYNUM(nb_inplace_true_divide);
2066 COPYNUM(nb_inplace_floor_divide);
2067 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002068 }
2069
Guido van Rossum13d52f02001-08-10 21:24:08 +00002070 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
2071 basebase = base->tp_base;
2072 if (basebase->tp_as_sequence == NULL)
2073 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002074 COPYSEQ(sq_length);
2075 COPYSEQ(sq_concat);
2076 COPYSEQ(sq_repeat);
2077 COPYSEQ(sq_item);
2078 COPYSEQ(sq_slice);
2079 COPYSEQ(sq_ass_item);
2080 COPYSEQ(sq_ass_slice);
2081 COPYSEQ(sq_contains);
2082 COPYSEQ(sq_inplace_concat);
2083 COPYSEQ(sq_inplace_repeat);
2084 }
2085
Guido van Rossum13d52f02001-08-10 21:24:08 +00002086 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
2087 basebase = base->tp_base;
2088 if (basebase->tp_as_mapping == NULL)
2089 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002090 COPYMAP(mp_length);
2091 COPYMAP(mp_subscript);
2092 COPYMAP(mp_ass_subscript);
2093 }
2094
Tim Petersfc57ccb2001-10-12 02:38:24 +00002095 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2096 basebase = base->tp_base;
2097 if (basebase->tp_as_buffer == NULL)
2098 basebase = NULL;
2099 COPYBUF(bf_getreadbuffer);
2100 COPYBUF(bf_getwritebuffer);
2101 COPYBUF(bf_getsegcount);
2102 COPYBUF(bf_getcharbuffer);
2103 }
2104
Guido van Rossum13d52f02001-08-10 21:24:08 +00002105 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002106
Tim Peters6d6c1a32001-08-02 04:15:00 +00002107 COPYSLOT(tp_dealloc);
2108 COPYSLOT(tp_print);
2109 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
2110 type->tp_getattr = base->tp_getattr;
2111 type->tp_getattro = base->tp_getattro;
2112 }
2113 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
2114 type->tp_setattr = base->tp_setattr;
2115 type->tp_setattro = base->tp_setattro;
2116 }
2117 /* tp_compare see tp_richcompare */
2118 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002119 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002120 COPYSLOT(tp_call);
2121 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002122 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002123 if (type->tp_compare == NULL &&
2124 type->tp_richcompare == NULL &&
2125 type->tp_hash == NULL)
2126 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002127 type->tp_compare = base->tp_compare;
2128 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002129 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002130 }
2131 }
2132 else {
2133 COPYSLOT(tp_compare);
2134 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002135 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
2136 COPYSLOT(tp_iter);
2137 COPYSLOT(tp_iternext);
2138 }
2139 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2140 COPYSLOT(tp_descr_get);
2141 COPYSLOT(tp_descr_set);
2142 COPYSLOT(tp_dictoffset);
2143 COPYSLOT(tp_init);
2144 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002145 COPYSLOT(tp_free);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00002146 COPYSLOT(tp_is_gc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002147 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002148}
2149
Guido van Rossum13d52f02001-08-10 21:24:08 +00002150staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00002151staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002152
Tim Peters6d6c1a32001-08-02 04:15:00 +00002153int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002154PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002155{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002156 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002157 PyTypeObject *base;
2158 int i, n;
2159
Guido van Rossumd614f972001-08-10 17:39:49 +00002160 if (type->tp_flags & Py_TPFLAGS_READY) {
2161 assert(type->tp_dict != NULL);
2162 return 0;
2163 }
2164 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00002165
2166 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002167
2168 /* Initialize tp_base (defaults to BaseObject unless that's us) */
2169 base = type->tp_base;
2170 if (base == NULL && type != &PyBaseObject_Type)
2171 base = type->tp_base = &PyBaseObject_Type;
2172
Guido van Rossum0986d822002-04-08 01:38:42 +00002173 /* Initialize ob_type if NULL. This means extensions that want to be
2174 compilable separately on Windows can call PyType_Ready() instead of
2175 initializing the ob_type field of their type objects. */
2176 if (type->ob_type == NULL)
2177 type->ob_type = base->ob_type;
2178
Tim Peters6d6c1a32001-08-02 04:15:00 +00002179 /* Initialize tp_bases */
2180 bases = type->tp_bases;
2181 if (bases == NULL) {
2182 if (base == NULL)
2183 bases = PyTuple_New(0);
2184 else
2185 bases = Py_BuildValue("(O)", base);
2186 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002187 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002188 type->tp_bases = bases;
2189 }
2190
2191 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002192 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002193 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002194 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002195 }
2196
Guido van Rossum687ae002001-10-15 22:03:32 +00002197 /* Initialize tp_dict */
2198 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002199 if (dict == NULL) {
2200 dict = PyDict_New();
2201 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002202 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00002203 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002204 }
2205
Guido van Rossum687ae002001-10-15 22:03:32 +00002206 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002207 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002208 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002209 if (type->tp_methods != NULL) {
2210 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002211 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002212 }
2213 if (type->tp_members != NULL) {
2214 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002215 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216 }
2217 if (type->tp_getset != NULL) {
2218 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002219 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002220 }
2221
Tim Peters6d6c1a32001-08-02 04:15:00 +00002222 /* Calculate method resolution order */
2223 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002224 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002225 }
2226
Guido van Rossum13d52f02001-08-10 21:24:08 +00002227 /* Inherit special flags from dominant base */
2228 if (type->tp_base != NULL)
2229 inherit_special(type, type->tp_base);
2230
Tim Peters6d6c1a32001-08-02 04:15:00 +00002231 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002232 bases = type->tp_mro;
2233 assert(bases != NULL);
2234 assert(PyTuple_Check(bases));
2235 n = PyTuple_GET_SIZE(bases);
2236 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002237 PyObject *b = PyTuple_GET_ITEM(bases, i);
2238 if (PyType_Check(b))
2239 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002240 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002241
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002242 /* if the type dictionary doesn't contain a __doc__, set it from
2243 the tp_doc slot.
2244 */
2245 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2246 if (type->tp_doc != NULL) {
2247 PyObject *doc = PyString_FromString(type->tp_doc);
2248 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2249 Py_DECREF(doc);
2250 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00002251 PyDict_SetItemString(type->tp_dict,
2252 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002253 }
2254 }
2255
Guido van Rossum13d52f02001-08-10 21:24:08 +00002256 /* Some more special stuff */
2257 base = type->tp_base;
2258 if (base != NULL) {
2259 if (type->tp_as_number == NULL)
2260 type->tp_as_number = base->tp_as_number;
2261 if (type->tp_as_sequence == NULL)
2262 type->tp_as_sequence = base->tp_as_sequence;
2263 if (type->tp_as_mapping == NULL)
2264 type->tp_as_mapping = base->tp_as_mapping;
2265 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002266
Guido van Rossum1c450732001-10-08 15:18:27 +00002267 /* Link into each base class's list of subclasses */
2268 bases = type->tp_bases;
2269 n = PyTuple_GET_SIZE(bases);
2270 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002271 PyObject *b = PyTuple_GET_ITEM(bases, i);
2272 if (PyType_Check(b) &&
2273 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002274 goto error;
2275 }
2276
Guido van Rossum13d52f02001-08-10 21:24:08 +00002277 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002278 assert(type->tp_dict != NULL);
2279 type->tp_flags =
2280 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002281 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002282
2283 error:
2284 type->tp_flags &= ~Py_TPFLAGS_READYING;
2285 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002286}
2287
Guido van Rossum1c450732001-10-08 15:18:27 +00002288static int
2289add_subclass(PyTypeObject *base, PyTypeObject *type)
2290{
2291 int i;
2292 PyObject *list, *ref, *new;
2293
2294 list = base->tp_subclasses;
2295 if (list == NULL) {
2296 base->tp_subclasses = list = PyList_New(0);
2297 if (list == NULL)
2298 return -1;
2299 }
2300 assert(PyList_Check(list));
2301 new = PyWeakref_NewRef((PyObject *)type, NULL);
2302 i = PyList_GET_SIZE(list);
2303 while (--i >= 0) {
2304 ref = PyList_GET_ITEM(list, i);
2305 assert(PyWeakref_CheckRef(ref));
2306 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2307 return PyList_SetItem(list, i, new);
2308 }
2309 i = PyList_Append(list, new);
2310 Py_DECREF(new);
2311 return i;
2312}
2313
Tim Peters6d6c1a32001-08-02 04:15:00 +00002314
2315/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2316
2317/* There's a wrapper *function* for each distinct function typedef used
2318 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2319 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2320 Most tables have only one entry; the tables for binary operators have two
2321 entries, one regular and one with reversed arguments. */
2322
2323static PyObject *
2324wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2325{
2326 inquiry func = (inquiry)wrapped;
2327 int res;
2328
2329 if (!PyArg_ParseTuple(args, ""))
2330 return NULL;
2331 res = (*func)(self);
2332 if (res == -1 && PyErr_Occurred())
2333 return NULL;
2334 return PyInt_FromLong((long)res);
2335}
2336
Tim Peters6d6c1a32001-08-02 04:15:00 +00002337static PyObject *
2338wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2339{
2340 binaryfunc func = (binaryfunc)wrapped;
2341 PyObject *other;
2342
2343 if (!PyArg_ParseTuple(args, "O", &other))
2344 return NULL;
2345 return (*func)(self, other);
2346}
2347
2348static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002349wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2350{
2351 binaryfunc func = (binaryfunc)wrapped;
2352 PyObject *other;
2353
2354 if (!PyArg_ParseTuple(args, "O", &other))
2355 return NULL;
2356 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002357 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002358 Py_INCREF(Py_NotImplemented);
2359 return Py_NotImplemented;
2360 }
2361 return (*func)(self, other);
2362}
2363
2364static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002365wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2366{
2367 binaryfunc func = (binaryfunc)wrapped;
2368 PyObject *other;
2369
2370 if (!PyArg_ParseTuple(args, "O", &other))
2371 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002372 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002373 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002374 Py_INCREF(Py_NotImplemented);
2375 return Py_NotImplemented;
2376 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002377 return (*func)(other, self);
2378}
2379
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002380static PyObject *
2381wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2382{
2383 coercion func = (coercion)wrapped;
2384 PyObject *other, *res;
2385 int ok;
2386
2387 if (!PyArg_ParseTuple(args, "O", &other))
2388 return NULL;
2389 ok = func(&self, &other);
2390 if (ok < 0)
2391 return NULL;
2392 if (ok > 0) {
2393 Py_INCREF(Py_NotImplemented);
2394 return Py_NotImplemented;
2395 }
2396 res = PyTuple_New(2);
2397 if (res == NULL) {
2398 Py_DECREF(self);
2399 Py_DECREF(other);
2400 return NULL;
2401 }
2402 PyTuple_SET_ITEM(res, 0, self);
2403 PyTuple_SET_ITEM(res, 1, other);
2404 return res;
2405}
2406
Tim Peters6d6c1a32001-08-02 04:15:00 +00002407static PyObject *
2408wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2409{
2410 ternaryfunc func = (ternaryfunc)wrapped;
2411 PyObject *other;
2412 PyObject *third = Py_None;
2413
2414 /* Note: This wrapper only works for __pow__() */
2415
2416 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2417 return NULL;
2418 return (*func)(self, other, third);
2419}
2420
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002421static PyObject *
2422wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2423{
2424 ternaryfunc func = (ternaryfunc)wrapped;
2425 PyObject *other;
2426 PyObject *third = Py_None;
2427
2428 /* Note: This wrapper only works for __pow__() */
2429
2430 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2431 return NULL;
2432 return (*func)(other, self, third);
2433}
2434
Tim Peters6d6c1a32001-08-02 04:15:00 +00002435static PyObject *
2436wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2437{
2438 unaryfunc func = (unaryfunc)wrapped;
2439
2440 if (!PyArg_ParseTuple(args, ""))
2441 return NULL;
2442 return (*func)(self);
2443}
2444
Tim Peters6d6c1a32001-08-02 04:15:00 +00002445static PyObject *
2446wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2447{
2448 intargfunc func = (intargfunc)wrapped;
2449 int i;
2450
2451 if (!PyArg_ParseTuple(args, "i", &i))
2452 return NULL;
2453 return (*func)(self, i);
2454}
2455
Guido van Rossum5d815f32001-08-17 21:57:47 +00002456static int
2457getindex(PyObject *self, PyObject *arg)
2458{
2459 int i;
2460
2461 i = PyInt_AsLong(arg);
2462 if (i == -1 && PyErr_Occurred())
2463 return -1;
2464 if (i < 0) {
2465 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2466 if (sq && sq->sq_length) {
2467 int n = (*sq->sq_length)(self);
2468 if (n < 0)
2469 return -1;
2470 i += n;
2471 }
2472 }
2473 return i;
2474}
2475
2476static PyObject *
2477wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2478{
2479 intargfunc func = (intargfunc)wrapped;
2480 PyObject *arg;
2481 int i;
2482
Guido van Rossumf4593e02001-10-03 12:09:30 +00002483 if (PyTuple_GET_SIZE(args) == 1) {
2484 arg = PyTuple_GET_ITEM(args, 0);
2485 i = getindex(self, arg);
2486 if (i == -1 && PyErr_Occurred())
2487 return NULL;
2488 return (*func)(self, i);
2489 }
2490 PyArg_ParseTuple(args, "O", &arg);
2491 assert(PyErr_Occurred());
2492 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002493}
2494
Tim Peters6d6c1a32001-08-02 04:15:00 +00002495static PyObject *
2496wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2497{
2498 intintargfunc func = (intintargfunc)wrapped;
2499 int i, j;
2500
2501 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2502 return NULL;
2503 return (*func)(self, i, j);
2504}
2505
Tim Peters6d6c1a32001-08-02 04:15:00 +00002506static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002507wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002508{
2509 intobjargproc func = (intobjargproc)wrapped;
2510 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002511 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002512
Guido van Rossum5d815f32001-08-17 21:57:47 +00002513 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2514 return NULL;
2515 i = getindex(self, arg);
2516 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002517 return NULL;
2518 res = (*func)(self, i, value);
2519 if (res == -1 && PyErr_Occurred())
2520 return NULL;
2521 Py_INCREF(Py_None);
2522 return Py_None;
2523}
2524
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002525static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002526wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002527{
2528 intobjargproc func = (intobjargproc)wrapped;
2529 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002530 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002531
Guido van Rossum5d815f32001-08-17 21:57:47 +00002532 if (!PyArg_ParseTuple(args, "O", &arg))
2533 return NULL;
2534 i = getindex(self, arg);
2535 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002536 return NULL;
2537 res = (*func)(self, i, NULL);
2538 if (res == -1 && PyErr_Occurred())
2539 return NULL;
2540 Py_INCREF(Py_None);
2541 return Py_None;
2542}
2543
Tim Peters6d6c1a32001-08-02 04:15:00 +00002544static PyObject *
2545wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2546{
2547 intintobjargproc func = (intintobjargproc)wrapped;
2548 int i, j, res;
2549 PyObject *value;
2550
2551 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2552 return NULL;
2553 res = (*func)(self, i, j, value);
2554 if (res == -1 && PyErr_Occurred())
2555 return NULL;
2556 Py_INCREF(Py_None);
2557 return Py_None;
2558}
2559
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002560static PyObject *
2561wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2562{
2563 intintobjargproc func = (intintobjargproc)wrapped;
2564 int i, j, res;
2565
2566 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2567 return NULL;
2568 res = (*func)(self, i, j, NULL);
2569 if (res == -1 && PyErr_Occurred())
2570 return NULL;
2571 Py_INCREF(Py_None);
2572 return Py_None;
2573}
2574
Tim Peters6d6c1a32001-08-02 04:15:00 +00002575/* XXX objobjproc is a misnomer; should be objargpred */
2576static PyObject *
2577wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2578{
2579 objobjproc func = (objobjproc)wrapped;
2580 int res;
2581 PyObject *value;
2582
2583 if (!PyArg_ParseTuple(args, "O", &value))
2584 return NULL;
2585 res = (*func)(self, value);
2586 if (res == -1 && PyErr_Occurred())
2587 return NULL;
2588 return PyInt_FromLong((long)res);
2589}
2590
Tim Peters6d6c1a32001-08-02 04:15:00 +00002591static PyObject *
2592wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2593{
2594 objobjargproc func = (objobjargproc)wrapped;
2595 int res;
2596 PyObject *key, *value;
2597
2598 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2599 return NULL;
2600 res = (*func)(self, key, value);
2601 if (res == -1 && PyErr_Occurred())
2602 return NULL;
2603 Py_INCREF(Py_None);
2604 return Py_None;
2605}
2606
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002607static PyObject *
2608wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2609{
2610 objobjargproc func = (objobjargproc)wrapped;
2611 int res;
2612 PyObject *key;
2613
2614 if (!PyArg_ParseTuple(args, "O", &key))
2615 return NULL;
2616 res = (*func)(self, key, NULL);
2617 if (res == -1 && PyErr_Occurred())
2618 return NULL;
2619 Py_INCREF(Py_None);
2620 return Py_None;
2621}
2622
Tim Peters6d6c1a32001-08-02 04:15:00 +00002623static PyObject *
2624wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2625{
2626 cmpfunc func = (cmpfunc)wrapped;
2627 int res;
2628 PyObject *other;
2629
2630 if (!PyArg_ParseTuple(args, "O", &other))
2631 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002632 if (other->ob_type->tp_compare != func &&
2633 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002634 PyErr_Format(
2635 PyExc_TypeError,
2636 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2637 self->ob_type->tp_name,
2638 self->ob_type->tp_name,
2639 other->ob_type->tp_name);
2640 return NULL;
2641 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002642 res = (*func)(self, other);
2643 if (PyErr_Occurred())
2644 return NULL;
2645 return PyInt_FromLong((long)res);
2646}
2647
Tim Peters6d6c1a32001-08-02 04:15:00 +00002648static PyObject *
2649wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2650{
2651 setattrofunc func = (setattrofunc)wrapped;
2652 int res;
2653 PyObject *name, *value;
2654
2655 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2656 return NULL;
2657 res = (*func)(self, name, value);
2658 if (res < 0)
2659 return NULL;
2660 Py_INCREF(Py_None);
2661 return Py_None;
2662}
2663
2664static PyObject *
2665wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2666{
2667 setattrofunc func = (setattrofunc)wrapped;
2668 int res;
2669 PyObject *name;
2670
2671 if (!PyArg_ParseTuple(args, "O", &name))
2672 return NULL;
2673 res = (*func)(self, name, NULL);
2674 if (res < 0)
2675 return NULL;
2676 Py_INCREF(Py_None);
2677 return Py_None;
2678}
2679
Tim Peters6d6c1a32001-08-02 04:15:00 +00002680static PyObject *
2681wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2682{
2683 hashfunc func = (hashfunc)wrapped;
2684 long res;
2685
2686 if (!PyArg_ParseTuple(args, ""))
2687 return NULL;
2688 res = (*func)(self);
2689 if (res == -1 && PyErr_Occurred())
2690 return NULL;
2691 return PyInt_FromLong(res);
2692}
2693
Tim Peters6d6c1a32001-08-02 04:15:00 +00002694static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002695wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002696{
2697 ternaryfunc func = (ternaryfunc)wrapped;
2698
Guido van Rossumc8e56452001-10-22 00:43:43 +00002699 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002700}
2701
Tim Peters6d6c1a32001-08-02 04:15:00 +00002702static PyObject *
2703wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2704{
2705 richcmpfunc func = (richcmpfunc)wrapped;
2706 PyObject *other;
2707
2708 if (!PyArg_ParseTuple(args, "O", &other))
2709 return NULL;
2710 return (*func)(self, other, op);
2711}
2712
2713#undef RICHCMP_WRAPPER
2714#define RICHCMP_WRAPPER(NAME, OP) \
2715static PyObject * \
2716richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2717{ \
2718 return wrap_richcmpfunc(self, args, wrapped, OP); \
2719}
2720
Jack Jansen8e938b42001-08-08 15:29:49 +00002721RICHCMP_WRAPPER(lt, Py_LT)
2722RICHCMP_WRAPPER(le, Py_LE)
2723RICHCMP_WRAPPER(eq, Py_EQ)
2724RICHCMP_WRAPPER(ne, Py_NE)
2725RICHCMP_WRAPPER(gt, Py_GT)
2726RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002727
Tim Peters6d6c1a32001-08-02 04:15:00 +00002728static PyObject *
2729wrap_next(PyObject *self, PyObject *args, void *wrapped)
2730{
2731 unaryfunc func = (unaryfunc)wrapped;
2732 PyObject *res;
2733
2734 if (!PyArg_ParseTuple(args, ""))
2735 return NULL;
2736 res = (*func)(self);
2737 if (res == NULL && !PyErr_Occurred())
2738 PyErr_SetNone(PyExc_StopIteration);
2739 return res;
2740}
2741
Tim Peters6d6c1a32001-08-02 04:15:00 +00002742static PyObject *
2743wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2744{
2745 descrgetfunc func = (descrgetfunc)wrapped;
2746 PyObject *obj;
2747 PyObject *type = NULL;
2748
2749 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2750 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002751 return (*func)(self, obj, type);
2752}
2753
Tim Peters6d6c1a32001-08-02 04:15:00 +00002754static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002755wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002756{
2757 descrsetfunc func = (descrsetfunc)wrapped;
2758 PyObject *obj, *value;
2759 int ret;
2760
2761 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2762 return NULL;
2763 ret = (*func)(self, obj, value);
2764 if (ret < 0)
2765 return NULL;
2766 Py_INCREF(Py_None);
2767 return Py_None;
2768}
2769
Tim Peters6d6c1a32001-08-02 04:15:00 +00002770static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002771wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002772{
2773 initproc func = (initproc)wrapped;
2774
Guido van Rossumc8e56452001-10-22 00:43:43 +00002775 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002776 return NULL;
2777 Py_INCREF(Py_None);
2778 return Py_None;
2779}
2780
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002782tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002783{
Barry Warsaw60f01882001-08-22 19:24:42 +00002784 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002785 PyObject *arg0, *res;
2786
2787 if (self == NULL || !PyType_Check(self))
2788 Py_FatalError("__new__() called with non-type 'self'");
2789 type = (PyTypeObject *)self;
2790 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002791 PyErr_Format(PyExc_TypeError,
2792 "%s.__new__(): not enough arguments",
2793 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002794 return NULL;
2795 }
2796 arg0 = PyTuple_GET_ITEM(args, 0);
2797 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002798 PyErr_Format(PyExc_TypeError,
2799 "%s.__new__(X): X is not a type object (%s)",
2800 type->tp_name,
2801 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002802 return NULL;
2803 }
2804 subtype = (PyTypeObject *)arg0;
2805 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002806 PyErr_Format(PyExc_TypeError,
2807 "%s.__new__(%s): %s is not a subtype of %s",
2808 type->tp_name,
2809 subtype->tp_name,
2810 subtype->tp_name,
2811 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002812 return NULL;
2813 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002814
2815 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002816 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002817 most derived base that's not a heap type is this type. */
2818 staticbase = subtype;
2819 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2820 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002821 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002822 PyErr_Format(PyExc_TypeError,
2823 "%s.__new__(%s) is not safe, use %s.__new__()",
2824 type->tp_name,
2825 subtype->tp_name,
2826 staticbase == NULL ? "?" : staticbase->tp_name);
2827 return NULL;
2828 }
2829
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002830 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2831 if (args == NULL)
2832 return NULL;
2833 res = type->tp_new(subtype, args, kwds);
2834 Py_DECREF(args);
2835 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002836}
2837
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002838static struct PyMethodDef tp_new_methoddef[] = {
2839 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2840 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002841 {0}
2842};
2843
2844static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002845add_tp_new_wrapper(PyTypeObject *type)
2846{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002847 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002848
Guido van Rossum687ae002001-10-15 22:03:32 +00002849 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002850 return 0;
2851 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002852 if (func == NULL)
2853 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002854 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002855}
2856
Guido van Rossumf040ede2001-08-07 16:40:56 +00002857/* Slot wrappers that call the corresponding __foo__ slot. See comments
2858 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002859
Guido van Rossumdc91b992001-08-08 22:26:22 +00002860#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002861static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002862FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002863{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002864 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002865 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002866}
2867
Guido van Rossumdc91b992001-08-08 22:26:22 +00002868#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002869static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002870FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002871{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002872 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002873 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002874}
2875
Guido van Rossumdc91b992001-08-08 22:26:22 +00002876
2877#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002878static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002879FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002880{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002881 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002882 int do_other = self->ob_type != other->ob_type && \
2883 other->ob_type->tp_as_number != NULL && \
2884 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002885 if (self->ob_type->tp_as_number != NULL && \
2886 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2887 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002888 if (do_other && \
2889 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2890 r = call_maybe( \
2891 other, ROPSTR, &rcache_str, "(O)", self); \
2892 if (r != Py_NotImplemented) \
2893 return r; \
2894 Py_DECREF(r); \
2895 do_other = 0; \
2896 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002897 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002898 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002899 if (r != Py_NotImplemented || \
2900 other->ob_type == self->ob_type) \
2901 return r; \
2902 Py_DECREF(r); \
2903 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002904 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002905 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002906 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002907 } \
2908 Py_INCREF(Py_NotImplemented); \
2909 return Py_NotImplemented; \
2910}
2911
2912#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2913 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2914
2915#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2916static PyObject * \
2917FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2918{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002919 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002920 return call_method(self, OPSTR, &cache_str, \
2921 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002922}
2923
2924static int
2925slot_sq_length(PyObject *self)
2926{
Guido van Rossum2730b132001-08-28 18:22:14 +00002927 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002928 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002929 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002930
2931 if (res == NULL)
2932 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002933 len = (int)PyInt_AsLong(res);
2934 Py_DECREF(res);
2935 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002936}
2937
Guido van Rossumdc91b992001-08-08 22:26:22 +00002938SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2939SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002940
2941/* Super-optimized version of slot_sq_item.
2942 Other slots could do the same... */
2943static PyObject *
2944slot_sq_item(PyObject *self, int i)
2945{
2946 static PyObject *getitem_str;
2947 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2948 descrgetfunc f;
2949
2950 if (getitem_str == NULL) {
2951 getitem_str = PyString_InternFromString("__getitem__");
2952 if (getitem_str == NULL)
2953 return NULL;
2954 }
2955 func = _PyType_Lookup(self->ob_type, getitem_str);
2956 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002957 if ((f = func->ob_type->tp_descr_get) == NULL)
2958 Py_INCREF(func);
2959 else
2960 func = f(func, self, (PyObject *)(self->ob_type));
2961 ival = PyInt_FromLong(i);
2962 if (ival != NULL) {
2963 args = PyTuple_New(1);
2964 if (args != NULL) {
2965 PyTuple_SET_ITEM(args, 0, ival);
2966 retval = PyObject_Call(func, args, NULL);
2967 Py_XDECREF(args);
2968 Py_XDECREF(func);
2969 return retval;
2970 }
2971 }
2972 }
2973 else {
2974 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2975 }
2976 Py_XDECREF(args);
2977 Py_XDECREF(ival);
2978 Py_XDECREF(func);
2979 return NULL;
2980}
2981
Guido van Rossumdc91b992001-08-08 22:26:22 +00002982SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002983
2984static int
2985slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2986{
2987 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002988 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002989
2990 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00002991 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002992 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002993 else
Guido van Rossum2730b132001-08-28 18:22:14 +00002994 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00002995 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002996 if (res == NULL)
2997 return -1;
2998 Py_DECREF(res);
2999 return 0;
3000}
3001
3002static int
3003slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
3004{
3005 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003006 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003007
3008 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003009 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003010 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003011 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003012 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003013 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003014 if (res == NULL)
3015 return -1;
3016 Py_DECREF(res);
3017 return 0;
3018}
3019
3020static int
3021slot_sq_contains(PyObject *self, PyObject *value)
3022{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003023 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00003024 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003025
Guido van Rossum55f20992001-10-01 17:18:22 +00003026 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003027
3028 if (func != NULL) {
3029 args = Py_BuildValue("(O)", value);
3030 if (args == NULL)
3031 res = NULL;
3032 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003033 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003034 Py_DECREF(args);
3035 }
3036 Py_DECREF(func);
3037 if (res == NULL)
3038 return -1;
3039 return PyObject_IsTrue(res);
3040 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003041 else if (PyErr_Occurred())
3042 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003043 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00003044 return _PySequence_IterSearch(self, value,
3045 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003046 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003047}
3048
Guido van Rossumdc91b992001-08-08 22:26:22 +00003049SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
3050SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003051
3052#define slot_mp_length slot_sq_length
3053
Guido van Rossumdc91b992001-08-08 22:26:22 +00003054SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003055
3056static int
3057slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
3058{
3059 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003060 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003061
3062 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003063 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003064 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003065 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003066 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003067 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003068 if (res == NULL)
3069 return -1;
3070 Py_DECREF(res);
3071 return 0;
3072}
3073
Guido van Rossumdc91b992001-08-08 22:26:22 +00003074SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
3075SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
3076SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
3077SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
3078SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
3079SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
3080
3081staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
3082
3083SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
3084 nb_power, "__pow__", "__rpow__")
3085
3086static PyObject *
3087slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3088{
Guido van Rossum2730b132001-08-28 18:22:14 +00003089 static PyObject *pow_str;
3090
Guido van Rossumdc91b992001-08-08 22:26:22 +00003091 if (modulus == Py_None)
3092 return slot_nb_power_binary(self, other);
Guido van Rossum23094982002-06-10 14:30:43 +00003093 /* Three-arg power doesn't use __rpow__. But ternary_op
3094 can call this when the second argument's type uses
3095 slot_nb_power, so check before calling self.__pow__. */
3096 if (self->ob_type->tp_as_number != NULL &&
3097 self->ob_type->tp_as_number->nb_power == slot_nb_power) {
3098 return call_method(self, "__pow__", &pow_str,
3099 "(OO)", other, modulus);
3100 }
3101 Py_INCREF(Py_NotImplemented);
3102 return Py_NotImplemented;
Guido van Rossumdc91b992001-08-08 22:26:22 +00003103}
3104
3105SLOT0(slot_nb_negative, "__neg__")
3106SLOT0(slot_nb_positive, "__pos__")
3107SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003108
3109static int
3110slot_nb_nonzero(PyObject *self)
3111{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003112 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003113 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003114
Guido van Rossum55f20992001-10-01 17:18:22 +00003115 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003116 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003117 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003118 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003119 func = lookup_maybe(self, "__len__", &len_str);
3120 if (func == NULL) {
3121 if (PyErr_Occurred())
3122 return -1;
3123 else
3124 return 1;
3125 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003126 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003127 res = PyObject_CallObject(func, NULL);
3128 Py_DECREF(func);
3129 if (res == NULL)
3130 return -1;
3131 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003132}
3133
Guido van Rossumdc91b992001-08-08 22:26:22 +00003134SLOT0(slot_nb_invert, "__invert__")
3135SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3136SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3137SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3138SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3139SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003140
3141static int
3142slot_nb_coerce(PyObject **a, PyObject **b)
3143{
3144 static PyObject *coerce_str;
3145 PyObject *self = *a, *other = *b;
3146
3147 if (self->ob_type->tp_as_number != NULL &&
3148 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3149 PyObject *r;
3150 r = call_maybe(
3151 self, "__coerce__", &coerce_str, "(O)", other);
3152 if (r == NULL)
3153 return -1;
3154 if (r == Py_NotImplemented) {
3155 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003156 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003157 else {
3158 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3159 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003160 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003161 Py_DECREF(r);
3162 return -1;
3163 }
3164 *a = PyTuple_GET_ITEM(r, 0);
3165 Py_INCREF(*a);
3166 *b = PyTuple_GET_ITEM(r, 1);
3167 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003168 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003169 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003170 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003171 }
3172 if (other->ob_type->tp_as_number != NULL &&
3173 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3174 PyObject *r;
3175 r = call_maybe(
3176 other, "__coerce__", &coerce_str, "(O)", self);
3177 if (r == NULL)
3178 return -1;
3179 if (r == Py_NotImplemented) {
3180 Py_DECREF(r);
3181 return 1;
3182 }
3183 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3184 PyErr_SetString(PyExc_TypeError,
3185 "__coerce__ didn't return a 2-tuple");
3186 Py_DECREF(r);
3187 return -1;
3188 }
3189 *a = PyTuple_GET_ITEM(r, 1);
3190 Py_INCREF(*a);
3191 *b = PyTuple_GET_ITEM(r, 0);
3192 Py_INCREF(*b);
3193 Py_DECREF(r);
3194 return 0;
3195 }
3196 return 1;
3197}
3198
Guido van Rossumdc91b992001-08-08 22:26:22 +00003199SLOT0(slot_nb_int, "__int__")
3200SLOT0(slot_nb_long, "__long__")
3201SLOT0(slot_nb_float, "__float__")
3202SLOT0(slot_nb_oct, "__oct__")
3203SLOT0(slot_nb_hex, "__hex__")
3204SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3205SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3206SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3207SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3208SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3209SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3210SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3211SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3212SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3213SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3214SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3215SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3216 "__floordiv__", "__rfloordiv__")
3217SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3218SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3219SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003220
3221static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003222half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003223{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003224 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003225 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003226 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003227
Guido van Rossum60718732001-08-28 17:47:51 +00003228 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003229 if (func == NULL) {
3230 PyErr_Clear();
3231 }
3232 else {
3233 args = Py_BuildValue("(O)", other);
3234 if (args == NULL)
3235 res = NULL;
3236 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003237 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003238 Py_DECREF(args);
3239 }
3240 if (res != Py_NotImplemented) {
3241 if (res == NULL)
3242 return -2;
3243 c = PyInt_AsLong(res);
3244 Py_DECREF(res);
3245 if (c == -1 && PyErr_Occurred())
3246 return -2;
3247 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3248 }
3249 Py_DECREF(res);
3250 }
3251 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003252}
3253
Guido van Rossumab3b0342001-09-18 20:38:53 +00003254/* This slot is published for the benefit of try_3way_compare in object.c */
3255int
3256_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003257{
3258 int c;
3259
Guido van Rossumab3b0342001-09-18 20:38:53 +00003260 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003261 c = half_compare(self, other);
3262 if (c <= 1)
3263 return c;
3264 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003265 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003266 c = half_compare(other, self);
3267 if (c < -1)
3268 return -2;
3269 if (c <= 1)
3270 return -c;
3271 }
3272 return (void *)self < (void *)other ? -1 :
3273 (void *)self > (void *)other ? 1 : 0;
3274}
3275
3276static PyObject *
3277slot_tp_repr(PyObject *self)
3278{
3279 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003280 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003281
Guido van Rossum60718732001-08-28 17:47:51 +00003282 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003283 if (func != NULL) {
3284 res = PyEval_CallObject(func, NULL);
3285 Py_DECREF(func);
3286 return res;
3287 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003288 PyErr_Clear();
3289 return PyString_FromFormat("<%s object at %p>",
3290 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003291}
3292
3293static PyObject *
3294slot_tp_str(PyObject *self)
3295{
3296 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003297 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003298
Guido van Rossum60718732001-08-28 17:47:51 +00003299 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003300 if (func != NULL) {
3301 res = PyEval_CallObject(func, NULL);
3302 Py_DECREF(func);
3303 return res;
3304 }
3305 else {
3306 PyErr_Clear();
3307 return slot_tp_repr(self);
3308 }
3309}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003310
3311static long
3312slot_tp_hash(PyObject *self)
3313{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003314 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003315 static PyObject *hash_str, *eq_str, *cmp_str;
3316
Tim Peters6d6c1a32001-08-02 04:15:00 +00003317 long h;
3318
Guido van Rossum60718732001-08-28 17:47:51 +00003319 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003320
3321 if (func != NULL) {
3322 res = PyEval_CallObject(func, NULL);
3323 Py_DECREF(func);
3324 if (res == NULL)
3325 return -1;
3326 h = PyInt_AsLong(res);
3327 }
3328 else {
3329 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003330 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003331 if (func == NULL) {
3332 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003333 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003334 }
3335 if (func != NULL) {
3336 Py_DECREF(func);
3337 PyErr_SetString(PyExc_TypeError, "unhashable type");
3338 return -1;
3339 }
3340 PyErr_Clear();
3341 h = _Py_HashPointer((void *)self);
3342 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003343 if (h == -1 && !PyErr_Occurred())
3344 h = -2;
3345 return h;
3346}
3347
3348static PyObject *
3349slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3350{
Guido van Rossum60718732001-08-28 17:47:51 +00003351 static PyObject *call_str;
3352 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003353 PyObject *res;
3354
3355 if (meth == NULL)
3356 return NULL;
3357 res = PyObject_Call(meth, args, kwds);
3358 Py_DECREF(meth);
3359 return res;
3360}
3361
Guido van Rossum14a6f832001-10-17 13:59:09 +00003362/* There are two slot dispatch functions for tp_getattro.
3363
3364 - slot_tp_getattro() is used when __getattribute__ is overridden
3365 but no __getattr__ hook is present;
3366
3367 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3368
Guido van Rossumc334df52002-04-04 23:44:47 +00003369 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
3370 detects the absence of __getattr__ and then installs the simpler slot if
3371 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00003372
Tim Peters6d6c1a32001-08-02 04:15:00 +00003373static PyObject *
3374slot_tp_getattro(PyObject *self, PyObject *name)
3375{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003376 static PyObject *getattribute_str = NULL;
3377 return call_method(self, "__getattribute__", &getattribute_str,
3378 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003379}
3380
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003381static PyObject *
3382slot_tp_getattr_hook(PyObject *self, PyObject *name)
3383{
3384 PyTypeObject *tp = self->ob_type;
3385 PyObject *getattr, *getattribute, *res;
3386 static PyObject *getattribute_str = NULL;
3387 static PyObject *getattr_str = NULL;
3388
3389 if (getattr_str == NULL) {
3390 getattr_str = PyString_InternFromString("__getattr__");
3391 if (getattr_str == NULL)
3392 return NULL;
3393 }
3394 if (getattribute_str == NULL) {
3395 getattribute_str =
3396 PyString_InternFromString("__getattribute__");
3397 if (getattribute_str == NULL)
3398 return NULL;
3399 }
3400 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003401 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003402 /* No __getattr__ hook: use a simpler dispatcher */
3403 tp->tp_getattro = slot_tp_getattro;
3404 return slot_tp_getattro(self, name);
3405 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003406 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003407 if (getattribute == NULL ||
3408 (getattribute->ob_type == &PyWrapperDescr_Type &&
3409 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3410 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003411 res = PyObject_GenericGetAttr(self, name);
3412 else
3413 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003414 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003415 PyErr_Clear();
3416 res = PyObject_CallFunction(getattr, "OO", self, name);
3417 }
3418 return res;
3419}
3420
Tim Peters6d6c1a32001-08-02 04:15:00 +00003421static int
3422slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3423{
3424 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003425 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003426
3427 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003428 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003429 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003430 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003431 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003432 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003433 if (res == NULL)
3434 return -1;
3435 Py_DECREF(res);
3436 return 0;
3437}
3438
3439/* Map rich comparison operators to their __xx__ namesakes */
3440static char *name_op[] = {
3441 "__lt__",
3442 "__le__",
3443 "__eq__",
3444 "__ne__",
3445 "__gt__",
3446 "__ge__",
3447};
3448
3449static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003450half_richcompare(PyObject *self, PyObject *other, int op)
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 *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003454
Guido van Rossum60718732001-08-28 17:47:51 +00003455 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003456 if (func == NULL) {
3457 PyErr_Clear();
3458 Py_INCREF(Py_NotImplemented);
3459 return Py_NotImplemented;
3460 }
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 }
3468 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003469 return res;
3470}
3471
Guido van Rossumb8f63662001-08-15 23:57:02 +00003472/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3473static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3474
3475static PyObject *
3476slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3477{
3478 PyObject *res;
3479
3480 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3481 res = half_richcompare(self, other, op);
3482 if (res != Py_NotImplemented)
3483 return res;
3484 Py_DECREF(res);
3485 }
3486 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3487 res = half_richcompare(other, self, swapped_op[op]);
3488 if (res != Py_NotImplemented) {
3489 return res;
3490 }
3491 Py_DECREF(res);
3492 }
3493 Py_INCREF(Py_NotImplemented);
3494 return Py_NotImplemented;
3495}
3496
3497static PyObject *
3498slot_tp_iter(PyObject *self)
3499{
3500 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003501 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003502
Guido van Rossum60718732001-08-28 17:47:51 +00003503 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003504 if (func != NULL) {
3505 res = PyObject_CallObject(func, NULL);
3506 Py_DECREF(func);
3507 return res;
3508 }
3509 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003510 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003511 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00003512 PyErr_SetString(PyExc_TypeError,
3513 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003514 return NULL;
3515 }
3516 Py_DECREF(func);
3517 return PySeqIter_New(self);
3518}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003519
3520static PyObject *
3521slot_tp_iternext(PyObject *self)
3522{
Guido van Rossum2730b132001-08-28 18:22:14 +00003523 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003524 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003525}
3526
Guido van Rossum1a493502001-08-17 16:47:50 +00003527static PyObject *
3528slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3529{
3530 PyTypeObject *tp = self->ob_type;
3531 PyObject *get;
3532 static PyObject *get_str = NULL;
3533
3534 if (get_str == NULL) {
3535 get_str = PyString_InternFromString("__get__");
3536 if (get_str == NULL)
3537 return NULL;
3538 }
3539 get = _PyType_Lookup(tp, get_str);
3540 if (get == NULL) {
3541 /* Avoid further slowdowns */
3542 if (tp->tp_descr_get == slot_tp_descr_get)
3543 tp->tp_descr_get = NULL;
3544 Py_INCREF(self);
3545 return self;
3546 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003547 if (obj == NULL)
3548 obj = Py_None;
3549 if (type == NULL)
3550 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003551 return PyObject_CallFunction(get, "OOO", self, obj, type);
3552}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003553
3554static int
3555slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3556{
Guido van Rossum2c252392001-08-24 10:13:31 +00003557 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003558 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003559
3560 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003561 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003562 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003563 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003564 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003565 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003566 if (res == NULL)
3567 return -1;
3568 Py_DECREF(res);
3569 return 0;
3570}
3571
3572static int
3573slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3574{
Guido van Rossum60718732001-08-28 17:47:51 +00003575 static PyObject *init_str;
3576 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003577 PyObject *res;
3578
3579 if (meth == NULL)
3580 return -1;
3581 res = PyObject_Call(meth, args, kwds);
3582 Py_DECREF(meth);
3583 if (res == NULL)
3584 return -1;
3585 Py_DECREF(res);
3586 return 0;
3587}
3588
3589static PyObject *
3590slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3591{
3592 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3593 PyObject *newargs, *x;
3594 int i, n;
3595
3596 if (func == NULL)
3597 return NULL;
3598 assert(PyTuple_Check(args));
3599 n = PyTuple_GET_SIZE(args);
3600 newargs = PyTuple_New(n+1);
3601 if (newargs == NULL)
3602 return NULL;
3603 Py_INCREF(type);
3604 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3605 for (i = 0; i < n; i++) {
3606 x = PyTuple_GET_ITEM(args, i);
3607 Py_INCREF(x);
3608 PyTuple_SET_ITEM(newargs, i+1, x);
3609 }
3610 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003611 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003612 Py_DECREF(func);
3613 return x;
3614}
3615
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003616
3617/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3618 functions. The offsets here are relative to the 'etype' structure, which
3619 incorporates the additional structures used for numbers, sequences and
3620 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3621 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00003622 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
3623 terminated with an all-zero entry. (This table is further initialized and
3624 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003625
Guido van Rossum6d204072001-10-21 00:44:31 +00003626typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003627
3628#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003629#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003630#undef ETSLOT
3631#undef SQSLOT
3632#undef MPSLOT
3633#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003634#undef UNSLOT
3635#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003636#undef BINSLOT
3637#undef RBINSLOT
3638
Guido van Rossum6d204072001-10-21 00:44:31 +00003639#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3640 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003641#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3642 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3643 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003644#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3645 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3646#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3647 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3648#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3649 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3650#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3651 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3652#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3653 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3654 "x." NAME "() <==> " DOC)
3655#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3656 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3657 "x." NAME "(y) <==> x" DOC "y")
3658#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3659 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3660 "x." NAME "(y) <==> x" DOC "y")
3661#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3662 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3663 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003664
3665static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003666 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3667 "x.__len__() <==> len(x)"),
3668 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3669 "x.__add__(y) <==> x+y"),
3670 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3671 "x.__mul__(n) <==> x*n"),
3672 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3673 "x.__rmul__(n) <==> n*x"),
3674 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3675 "x.__getitem__(y) <==> x[y]"),
3676 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3677 "x.__getslice__(i, j) <==> x[i:j]"),
3678 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3679 "x.__setitem__(i, y) <==> x[i]=y"),
3680 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3681 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003682 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003683 wrap_intintobjargproc,
3684 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3685 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3686 "x.__delslice__(i, j) <==> del x[i:j]"),
3687 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3688 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003689 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003690 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003691 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003692 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003693
Guido van Rossum6d204072001-10-21 00:44:31 +00003694 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3695 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003696 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003697 wrap_binaryfunc,
3698 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003699 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003700 wrap_objobjargproc,
3701 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003702 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003703 wrap_delitem,
3704 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003705
Guido van Rossum6d204072001-10-21 00:44:31 +00003706 BINSLOT("__add__", nb_add, slot_nb_add,
3707 "+"),
3708 RBINSLOT("__radd__", nb_add, slot_nb_add,
3709 "+"),
3710 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3711 "-"),
3712 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3713 "-"),
3714 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3715 "*"),
3716 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3717 "*"),
3718 BINSLOT("__div__", nb_divide, slot_nb_divide,
3719 "/"),
3720 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3721 "/"),
3722 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3723 "%"),
3724 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3725 "%"),
3726 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3727 "divmod(x, y)"),
3728 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3729 "divmod(y, x)"),
3730 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3731 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3732 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3733 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3734 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3735 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3736 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3737 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00003738 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00003739 "x != 0"),
3740 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3741 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3742 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3743 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3744 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3745 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3746 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3747 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3748 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3749 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3750 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3751 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3752 "x.__coerce__(y) <==> coerce(x, y)"),
3753 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3754 "int(x)"),
3755 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3756 "long(x)"),
3757 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3758 "float(x)"),
3759 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3760 "oct(x)"),
3761 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3762 "hex(x)"),
3763 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3764 wrap_binaryfunc, "+"),
3765 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3766 wrap_binaryfunc, "-"),
3767 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3768 wrap_binaryfunc, "*"),
3769 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3770 wrap_binaryfunc, "/"),
3771 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3772 wrap_binaryfunc, "%"),
3773 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3774 wrap_ternaryfunc, "**"),
3775 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3776 wrap_binaryfunc, "<<"),
3777 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3778 wrap_binaryfunc, ">>"),
3779 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3780 wrap_binaryfunc, "&"),
3781 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3782 wrap_binaryfunc, "^"),
3783 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3784 wrap_binaryfunc, "|"),
3785 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3786 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3787 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3788 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3789 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3790 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3791 IBSLOT("__itruediv__", nb_inplace_true_divide,
3792 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003793
Guido van Rossum6d204072001-10-21 00:44:31 +00003794 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3795 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003796 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003797 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3798 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003799 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003800 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3801 "x.__cmp__(y) <==> cmp(x,y)"),
3802 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3803 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003804 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3805 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003806 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003807 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3808 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3809 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3810 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3811 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3812 "x.__setattr__('name', value) <==> x.name = value"),
3813 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3814 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3815 "x.__delattr__('name') <==> del x.name"),
3816 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3817 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3818 "x.__lt__(y) <==> x<y"),
3819 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3820 "x.__le__(y) <==> x<=y"),
3821 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3822 "x.__eq__(y) <==> x==y"),
3823 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3824 "x.__ne__(y) <==> x!=y"),
3825 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3826 "x.__gt__(y) <==> x>y"),
3827 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3828 "x.__ge__(y) <==> x>=y"),
3829 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3830 "x.__iter__() <==> iter(x)"),
3831 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3832 "x.next() -> the next value, or raise StopIteration"),
3833 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3834 "descr.__get__(obj[, type]) -> value"),
3835 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3836 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003837 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003838 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003839 "see x.__class__.__doc__ for signature",
3840 PyWrapperFlag_KEYWORDS),
3841 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003842 {NULL}
3843};
3844
Guido van Rossumc334df52002-04-04 23:44:47 +00003845/* Given a type pointer and an offset gotten from a slotdef entry, return a
3846 pointer to the actual slot. This is not quite the same as simply adding
3847 the offset to the type pointer, since it takes care to indirect through the
3848 proper indirection pointer (as_buffer, etc.); it returns NULL if the
3849 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003850static void **
3851slotptr(PyTypeObject *type, int offset)
3852{
3853 char *ptr;
3854
3855 assert(offset >= 0);
3856 assert(offset < offsetof(etype, as_buffer));
3857 if (offset >= offsetof(etype, as_mapping)) {
3858 ptr = (void *)type->tp_as_mapping;
3859 offset -= offsetof(etype, as_mapping);
3860 }
3861 else if (offset >= offsetof(etype, as_sequence)) {
3862 ptr = (void *)type->tp_as_sequence;
3863 offset -= offsetof(etype, as_sequence);
3864 }
3865 else if (offset >= offsetof(etype, as_number)) {
3866 ptr = (void *)type->tp_as_number;
3867 offset -= offsetof(etype, as_number);
3868 }
3869 else {
3870 ptr = (void *)type;
3871 }
3872 if (ptr != NULL)
3873 ptr += offset;
3874 return (void **)ptr;
3875}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003876
Guido van Rossumc334df52002-04-04 23:44:47 +00003877/* Length of array of slotdef pointers used to store slots with the
3878 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
3879 the same __name__, for any __name__. Since that's a static property, it is
3880 appropriate to declare fixed-size arrays for this. */
3881#define MAX_EQUIV 10
3882
3883/* Return a slot pointer for a given name, but ONLY if the attribute has
3884 exactly one slot function. The name must be an interned string. */
3885static void **
3886resolve_slotdups(PyTypeObject *type, PyObject *name)
3887{
3888 /* XXX Maybe this could be optimized more -- but is it worth it? */
3889
3890 /* pname and ptrs act as a little cache */
3891 static PyObject *pname;
3892 static slotdef *ptrs[MAX_EQUIV];
3893 slotdef *p, **pp;
3894 void **res, **ptr;
3895
3896 if (pname != name) {
3897 /* Collect all slotdefs that match name into ptrs. */
3898 pname = name;
3899 pp = ptrs;
3900 for (p = slotdefs; p->name_strobj; p++) {
3901 if (p->name_strobj == name)
3902 *pp++ = p;
3903 }
3904 *pp = NULL;
3905 }
3906
3907 /* Look in all matching slots of the type; if exactly one of these has
3908 a filled-in slot, return its value. Otherwise return NULL. */
3909 res = NULL;
3910 for (pp = ptrs; *pp; pp++) {
3911 ptr = slotptr(type, (*pp)->offset);
3912 if (ptr == NULL || *ptr == NULL)
3913 continue;
3914 if (res != NULL)
3915 return NULL;
3916 res = ptr;
3917 }
3918 return res;
3919}
3920
3921/* Common code for update_these_slots() and fixup_slot_dispatchers(). This
3922 does some incredibly complex thinking and then sticks something into the
3923 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
3924 interests, and then stores a generic wrapper or a specific function into
3925 the slot.) Return a pointer to the next slotdef with a different offset,
3926 because that's convenient for fixup_slot_dispatchers(). */
3927static slotdef *
3928update_one_slot(PyTypeObject *type, slotdef *p)
3929{
3930 PyObject *descr;
3931 PyWrapperDescrObject *d;
3932 void *generic = NULL, *specific = NULL;
3933 int use_generic = 0;
3934 int offset = p->offset;
3935 void **ptr = slotptr(type, offset);
3936
3937 if (ptr == NULL) {
3938 do {
3939 ++p;
3940 } while (p->offset == offset);
3941 return p;
3942 }
3943 do {
3944 descr = _PyType_Lookup(type, p->name_strobj);
3945 if (descr == NULL)
3946 continue;
3947 if (descr->ob_type == &PyWrapperDescr_Type) {
3948 void **tptr = resolve_slotdups(type, p->name_strobj);
3949 if (tptr == NULL || tptr == ptr)
3950 generic = p->function;
3951 d = (PyWrapperDescrObject *)descr;
3952 if (d->d_base->wrapper == p->wrapper &&
3953 PyType_IsSubtype(type, d->d_type))
3954 {
3955 if (specific == NULL ||
3956 specific == d->d_wrapped)
3957 specific = d->d_wrapped;
3958 else
3959 use_generic = 1;
3960 }
3961 }
3962 else {
3963 use_generic = 1;
3964 generic = p->function;
3965 }
3966 } while ((++p)->offset == offset);
3967 if (specific && !use_generic)
3968 *ptr = specific;
3969 else
3970 *ptr = generic;
3971 return p;
3972}
3973
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003974staticforward int recurse_down_subclasses(PyTypeObject *type,
3975 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003976
Guido van Rossumc334df52002-04-04 23:44:47 +00003977/* In the type, update the slots whose slotdefs are gathered in the pp0 array,
3978 and then do the same for all this type's subtypes. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003979static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003980update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003981{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003982 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003983
Guido van Rossumc334df52002-04-04 23:44:47 +00003984 for (pp = pp0; *pp; pp++)
3985 update_one_slot(type, *pp);
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003986 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003987}
3988
Guido van Rossumc334df52002-04-04 23:44:47 +00003989/* Update the slots whose slotdefs are gathered in the pp array in all (direct
3990 or indirect) subclasses of type. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003991static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003992recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003993{
3994 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003995 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003996 int i, n;
3997
3998 subclasses = type->tp_subclasses;
3999 if (subclasses == NULL)
4000 return 0;
4001 assert(PyList_Check(subclasses));
4002 n = PyList_GET_SIZE(subclasses);
4003 for (i = 0; i < n; i++) {
4004 ref = PyList_GET_ITEM(subclasses, i);
4005 assert(PyWeakref_CheckRef(ref));
4006 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
4007 if (subclass == NULL)
4008 continue;
4009 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004010 /* Avoid recursing down into unaffected classes */
4011 dict = subclass->tp_dict;
4012 if (dict != NULL && PyDict_Check(dict) &&
4013 PyDict_GetItem(dict, name) != NULL)
4014 continue;
4015 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004016 return -1;
4017 }
4018 return 0;
4019}
4020
Guido van Rossumc334df52002-04-04 23:44:47 +00004021/* Comparison function for qsort() to compare slotdefs by their offset, and
4022 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004023static int
4024slotdef_cmp(const void *aa, const void *bb)
4025{
4026 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
4027 int c = a->offset - b->offset;
4028 if (c != 0)
4029 return c;
4030 else
4031 return a - b;
4032}
4033
Guido van Rossumc334df52002-04-04 23:44:47 +00004034/* Initialize the slotdefs table by adding interned string objects for the
4035 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004036static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004037init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004038{
4039 slotdef *p;
4040 static int initialized = 0;
4041
4042 if (initialized)
4043 return;
4044 for (p = slotdefs; p->name; p++) {
4045 p->name_strobj = PyString_InternFromString(p->name);
4046 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00004047 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004048 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004049 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
4050 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004051 initialized = 1;
4052}
4053
Guido van Rossumc334df52002-04-04 23:44:47 +00004054/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004055static int
4056update_slot(PyTypeObject *type, PyObject *name)
4057{
Guido van Rossumc334df52002-04-04 23:44:47 +00004058 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004059 slotdef *p;
4060 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004061 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004062
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004063 init_slotdefs();
4064 pp = ptrs;
4065 for (p = slotdefs; p->name; p++) {
4066 /* XXX assume name is interned! */
4067 if (p->name_strobj == name)
4068 *pp++ = p;
4069 }
4070 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004071 for (pp = ptrs; *pp; pp++) {
4072 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004073 offset = p->offset;
4074 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004075 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004076 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004077 }
Guido van Rossumc334df52002-04-04 23:44:47 +00004078 if (ptrs[0] == NULL)
4079 return 0; /* Not an attribute that affects any slots */
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004080 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004081}
4082
Guido van Rossumc334df52002-04-04 23:44:47 +00004083/* Store the proper functions in the slot dispatches at class (type)
4084 definition time, based upon which operations the class overrides in its
4085 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004086static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004087fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004088{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004089 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004090
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004091 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00004092 for (p = slotdefs; p->name; )
4093 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004094}
Guido van Rossum705f0f52001-08-24 16:47:00 +00004095
Guido van Rossum6d204072001-10-21 00:44:31 +00004096/* This function is called by PyType_Ready() to populate the type's
4097 dictionary with method descriptors for function slots. For each
4098 function slot (like tp_repr) that's defined in the type, one or
4099 more corresponding descriptors are added in the type's tp_dict
4100 dictionary under the appropriate name (like __repr__). Some
4101 function slots cause more than one descriptor to be added (for
4102 example, the nb_add slot adds both __add__ and __radd__
4103 descriptors) and some function slots compete for the same
4104 descriptor (for example both sq_item and mp_subscript generate a
4105 __getitem__ descriptor). This only adds new descriptors and
4106 doesn't overwrite entries in tp_dict that were previously
4107 defined. The descriptors contain a reference to the C function
4108 they must call, so that it's safe if they are copied into a
4109 subtype's __dict__ and the subtype has a different C function in
4110 its slot -- calling the method defined by the descriptor will call
4111 the C function that was used to create it, rather than the C
4112 function present in the slot when it is called. (This is important
4113 because a subtype may have a C function in the slot that calls the
4114 method from the dictionary, and we want to avoid infinite recursion
4115 here.) */
4116
4117static int
4118add_operators(PyTypeObject *type)
4119{
4120 PyObject *dict = type->tp_dict;
4121 slotdef *p;
4122 PyObject *descr;
4123 void **ptr;
4124
4125 init_slotdefs();
4126 for (p = slotdefs; p->name; p++) {
4127 if (p->wrapper == NULL)
4128 continue;
4129 ptr = slotptr(type, p->offset);
4130 if (!ptr || !*ptr)
4131 continue;
4132 if (PyDict_GetItem(dict, p->name_strobj))
4133 continue;
4134 descr = PyDescr_NewWrapper(type, p, *ptr);
4135 if (descr == NULL)
4136 return -1;
4137 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
4138 return -1;
4139 Py_DECREF(descr);
4140 }
4141 if (type->tp_new != NULL) {
4142 if (add_tp_new_wrapper(type) < 0)
4143 return -1;
4144 }
4145 return 0;
4146}
4147
Guido van Rossum705f0f52001-08-24 16:47:00 +00004148
4149/* Cooperative 'super' */
4150
4151typedef struct {
4152 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00004153 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004154 PyObject *obj;
4155} superobject;
4156
Guido van Rossum6f799372001-09-20 20:46:19 +00004157static PyMemberDef super_members[] = {
4158 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
4159 "the class invoking super()"},
4160 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
4161 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004162 {0}
4163};
4164
Guido van Rossum705f0f52001-08-24 16:47:00 +00004165static void
4166super_dealloc(PyObject *self)
4167{
4168 superobject *su = (superobject *)self;
4169
Guido van Rossum048eb752001-10-02 21:24:57 +00004170 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004171 Py_XDECREF(su->obj);
4172 Py_XDECREF(su->type);
4173 self->ob_type->tp_free(self);
4174}
4175
4176static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004177super_repr(PyObject *self)
4178{
4179 superobject *su = (superobject *)self;
4180
4181 if (su->obj)
4182 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004183 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004184 su->type ? su->type->tp_name : "NULL",
4185 su->obj->ob_type->tp_name);
4186 else
4187 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004188 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004189 su->type ? su->type->tp_name : "NULL");
4190}
4191
4192static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00004193super_getattro(PyObject *self, PyObject *name)
4194{
4195 superobject *su = (superobject *)self;
4196
4197 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00004198 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00004199 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004200 descrgetfunc f;
4201 int i, n;
4202
Guido van Rossum155db9a2002-04-02 17:53:47 +00004203 starttype = su->obj->ob_type;
4204 mro = starttype->tp_mro;
4205
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004206 if (mro == NULL)
4207 n = 0;
4208 else {
4209 assert(PyTuple_Check(mro));
4210 n = PyTuple_GET_SIZE(mro);
4211 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004212 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00004213 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00004214 break;
4215 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004216 if (i >= n && PyType_Check(su->obj)) {
Guido van Rossum155db9a2002-04-02 17:53:47 +00004217 starttype = (PyTypeObject *)(su->obj);
4218 mro = starttype->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004219 if (mro == NULL)
4220 n = 0;
4221 else {
4222 assert(PyTuple_Check(mro));
4223 n = PyTuple_GET_SIZE(mro);
4224 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004225 for (i = 0; i < n; i++) {
4226 if ((PyObject *)(su->type) ==
4227 PyTuple_GET_ITEM(mro, i))
4228 break;
4229 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004230 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004231 i++;
4232 res = NULL;
4233 for (; i < n; i++) {
4234 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00004235 if (PyType_Check(tmp))
4236 dict = ((PyTypeObject *)tmp)->tp_dict;
4237 else if (PyClass_Check(tmp))
4238 dict = ((PyClassObject *)tmp)->cl_dict;
4239 else
4240 continue;
4241 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00004242 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00004243 Py_INCREF(res);
4244 f = res->ob_type->tp_descr_get;
4245 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004246 tmp = f(res, su->obj,
4247 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004248 Py_DECREF(res);
4249 res = tmp;
4250 }
4251 return res;
4252 }
4253 }
4254 }
4255 return PyObject_GenericGetAttr(self, name);
4256}
4257
Guido van Rossum5b443c62001-12-03 15:38:28 +00004258static int
4259supercheck(PyTypeObject *type, PyObject *obj)
4260{
4261 if (!PyType_IsSubtype(obj->ob_type, type) &&
4262 !(PyType_Check(obj) &&
4263 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4264 PyErr_SetString(PyExc_TypeError,
4265 "super(type, obj): "
4266 "obj must be an instance or subtype of type");
4267 return -1;
4268 }
4269 else
4270 return 0;
4271}
4272
Guido van Rossum705f0f52001-08-24 16:47:00 +00004273static PyObject *
4274super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4275{
4276 superobject *su = (superobject *)self;
4277 superobject *new;
4278
4279 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4280 /* Not binding to an object, or already bound */
4281 Py_INCREF(self);
4282 return self;
4283 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004284 if (su->ob_type != &PySuper_Type)
4285 /* If su is an instance of a subclass of super,
4286 call its type */
4287 return PyObject_CallFunction((PyObject *)su->ob_type,
4288 "OO", su->type, obj);
4289 else {
4290 /* Inline the common case */
4291 if (supercheck(su->type, obj) < 0)
4292 return NULL;
4293 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4294 NULL, NULL);
4295 if (new == NULL)
4296 return NULL;
4297 Py_INCREF(su->type);
4298 Py_INCREF(obj);
4299 new->type = su->type;
4300 new->obj = obj;
4301 return (PyObject *)new;
4302 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004303}
4304
4305static int
4306super_init(PyObject *self, PyObject *args, PyObject *kwds)
4307{
4308 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004309 PyTypeObject *type;
4310 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004311
4312 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4313 return -1;
4314 if (obj == Py_None)
4315 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004316 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004317 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004318 Py_INCREF(type);
4319 Py_XINCREF(obj);
4320 su->type = type;
4321 su->obj = obj;
4322 return 0;
4323}
4324
4325static char super_doc[] =
4326"super(type) -> unbound super object\n"
4327"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004328"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004329"Typical use to call a cooperative superclass method:\n"
4330"class C(B):\n"
4331" def meth(self, arg):\n"
4332" super(C, self).meth(arg)";
4333
Guido van Rossum048eb752001-10-02 21:24:57 +00004334static int
4335super_traverse(PyObject *self, visitproc visit, void *arg)
4336{
4337 superobject *su = (superobject *)self;
4338 int err;
4339
4340#define VISIT(SLOT) \
4341 if (SLOT) { \
4342 err = visit((PyObject *)(SLOT), arg); \
4343 if (err) \
4344 return err; \
4345 }
4346
4347 VISIT(su->obj);
4348 VISIT(su->type);
4349
4350#undef VISIT
4351
4352 return 0;
4353}
4354
Guido van Rossum705f0f52001-08-24 16:47:00 +00004355PyTypeObject PySuper_Type = {
4356 PyObject_HEAD_INIT(&PyType_Type)
4357 0, /* ob_size */
4358 "super", /* tp_name */
4359 sizeof(superobject), /* tp_basicsize */
4360 0, /* tp_itemsize */
4361 /* methods */
4362 super_dealloc, /* tp_dealloc */
4363 0, /* tp_print */
4364 0, /* tp_getattr */
4365 0, /* tp_setattr */
4366 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004367 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004368 0, /* tp_as_number */
4369 0, /* tp_as_sequence */
4370 0, /* tp_as_mapping */
4371 0, /* tp_hash */
4372 0, /* tp_call */
4373 0, /* tp_str */
4374 super_getattro, /* tp_getattro */
4375 0, /* tp_setattro */
4376 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004377 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4378 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004379 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004380 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004381 0, /* tp_clear */
4382 0, /* tp_richcompare */
4383 0, /* tp_weaklistoffset */
4384 0, /* tp_iter */
4385 0, /* tp_iternext */
4386 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004387 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004388 0, /* tp_getset */
4389 0, /* tp_base */
4390 0, /* tp_dict */
4391 super_descr_get, /* tp_descr_get */
4392 0, /* tp_descr_set */
4393 0, /* tp_dictoffset */
4394 super_init, /* tp_init */
4395 PyType_GenericAlloc, /* tp_alloc */
4396 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00004397 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004398};