blob: cb2c791dcb36665f6bcba907143f97e65227c86d [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 Rossum9fc8a292002-05-24 21:40:08 +00001312 if (mro == NULL) {
Guido van Rossumb65c65b2002-06-03 19:52:41 +00001313 if (PyType_Ready(type) < 0) {
1314 /* It's not ideal to clear the error condition,
1315 but this function is documented as not setting
1316 an exception, and I don't want to change that.
1317 When PyType_Ready() can't proceed, it won't
1318 set the "ready" flag, so future attempts to ready
1319 the same type will call it again -- hopefully
1320 in a context that propagates the exception out.
1321 */
1322 PyErr_Clear();
Guido van Rossum9fc8a292002-05-24 21:40:08 +00001323 return NULL;
Guido van Rossumb65c65b2002-06-03 19:52:41 +00001324 }
Guido van Rossum9fc8a292002-05-24 21:40:08 +00001325 mro = type->tp_mro;
1326 assert(mro != NULL);
1327 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001328 assert(PyTuple_Check(mro));
1329 n = PyTuple_GET_SIZE(mro);
1330 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00001331 base = PyTuple_GET_ITEM(mro, i);
1332 if (PyClass_Check(base))
1333 dict = ((PyClassObject *)base)->cl_dict;
1334 else {
1335 assert(PyType_Check(base));
1336 dict = ((PyTypeObject *)base)->tp_dict;
1337 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001338 assert(dict && PyDict_Check(dict));
1339 res = PyDict_GetItem(dict, name);
1340 if (res != NULL)
1341 return res;
1342 }
1343 return NULL;
1344}
1345
1346/* This is similar to PyObject_GenericGetAttr(),
1347 but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
1348static PyObject *
1349type_getattro(PyTypeObject *type, PyObject *name)
1350{
1351 PyTypeObject *metatype = type->ob_type;
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001352 PyObject *meta_attribute, *attribute;
1353 descrgetfunc meta_get;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001354
1355 /* Initialize this type (we'll assume the metatype is initialized) */
1356 if (type->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00001357 if (PyType_Ready(type) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001358 return NULL;
1359 }
1360
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001361 /* No readable descriptor found yet */
1362 meta_get = NULL;
1363
1364 /* Look for the attribute in the metatype */
1365 meta_attribute = _PyType_Lookup(metatype, name);
1366
1367 if (meta_attribute != NULL) {
1368 meta_get = meta_attribute->ob_type->tp_descr_get;
1369
1370 if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
1371 /* Data descriptors implement tp_descr_set to intercept
1372 * writes. Assume the attribute is not overridden in
1373 * type's tp_dict (and bases): call the descriptor now.
1374 */
1375 return meta_get(meta_attribute, (PyObject *)type,
1376 (PyObject *)metatype);
1377 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001378 }
1379
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001380 /* No data descriptor found on metatype. Look in tp_dict of this
1381 * type and its bases */
1382 attribute = _PyType_Lookup(type, name);
1383 if (attribute != NULL) {
1384 /* Implement descriptor functionality, if any */
1385 descrgetfunc local_get = attribute->ob_type->tp_descr_get;
1386 if (local_get != NULL) {
1387 /* NULL 2nd argument indicates the descriptor was
1388 * found on the target object itself (or a base) */
1389 return local_get(attribute, (PyObject *)NULL,
1390 (PyObject *)type);
1391 }
1392
1393 Py_INCREF(attribute);
1394 return attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001395 }
1396
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001397 /* No attribute found in local __dict__ (or bases): use the
1398 * descriptor from the metatype, if any */
1399 if (meta_get != NULL)
1400 return meta_get(meta_attribute, (PyObject *)type,
1401 (PyObject *)metatype);
1402
1403 /* If an ordinary attribute was found on the metatype, return it now */
1404 if (meta_attribute != NULL) {
1405 Py_INCREF(meta_attribute);
1406 return meta_attribute;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001407 }
1408
1409 /* Give up */
1410 PyErr_Format(PyExc_AttributeError,
Guido van Rossumbfc2e5e2002-04-04 17:50:54 +00001411 "type object '%.50s' has no attribute '%.400s'",
1412 type->tp_name, PyString_AS_STRING(name));
Tim Peters6d6c1a32001-08-02 04:15:00 +00001413 return NULL;
1414}
1415
1416static int
1417type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
1418{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001419 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1420 PyErr_Format(
1421 PyExc_TypeError,
1422 "can't set attributes of built-in/extension type '%s'",
1423 type->tp_name);
1424 return -1;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00001425 }
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00001426 if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
1427 return -1;
1428 return update_slot(type, name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001429}
1430
1431static void
1432type_dealloc(PyTypeObject *type)
1433{
1434 etype *et;
1435
1436 /* Assert this is a heap-allocated type object */
1437 assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
Guido van Rossum048eb752001-10-02 21:24:57 +00001438 _PyObject_GC_UNTRACK(type);
Guido van Rossum1c450732001-10-08 15:18:27 +00001439 PyObject_ClearWeakRefs((PyObject *)type);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001440 et = (etype *)type;
1441 Py_XDECREF(type->tp_base);
1442 Py_XDECREF(type->tp_dict);
1443 Py_XDECREF(type->tp_bases);
1444 Py_XDECREF(type->tp_mro);
Guido van Rossum687ae002001-10-15 22:03:32 +00001445 Py_XDECREF(type->tp_cache);
Guido van Rossum1c450732001-10-08 15:18:27 +00001446 Py_XDECREF(type->tp_subclasses);
Tim Peters6d6c1a32001-08-02 04:15:00 +00001447 Py_XDECREF(et->name);
1448 Py_XDECREF(et->slots);
1449 type->ob_type->tp_free((PyObject *)type);
1450}
1451
Guido van Rossum1c450732001-10-08 15:18:27 +00001452static PyObject *
1453type_subclasses(PyTypeObject *type, PyObject *args_ignored)
1454{
1455 PyObject *list, *raw, *ref;
1456 int i, n;
1457
1458 list = PyList_New(0);
1459 if (list == NULL)
1460 return NULL;
1461 raw = type->tp_subclasses;
1462 if (raw == NULL)
1463 return list;
1464 assert(PyList_Check(raw));
1465 n = PyList_GET_SIZE(raw);
1466 for (i = 0; i < n; i++) {
1467 ref = PyList_GET_ITEM(raw, i);
Tim Peters44383382001-10-08 16:49:26 +00001468 assert(PyWeakref_CheckRef(ref));
Guido van Rossum1c450732001-10-08 15:18:27 +00001469 ref = PyWeakref_GET_OBJECT(ref);
1470 if (ref != Py_None) {
1471 if (PyList_Append(list, ref) < 0) {
1472 Py_DECREF(list);
1473 return NULL;
1474 }
1475 }
1476 }
1477 return list;
1478}
1479
Tim Peters6d6c1a32001-08-02 04:15:00 +00001480static PyMethodDef type_methods[] = {
Martin v. Löwise3eb1f22001-08-16 13:15:00 +00001481 {"mro", (PyCFunction)mro_external, METH_NOARGS,
Tim Peters6d6c1a32001-08-02 04:15:00 +00001482 "mro() -> list\nreturn a type's method resolution order"},
Guido van Rossum1c450732001-10-08 15:18:27 +00001483 {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
1484 "__subclasses__() -> list of immediate subclasses"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001485 {0}
1486};
1487
1488static char type_doc[] =
1489"type(object) -> the object's type\n"
1490"type(name, bases, dict) -> a new type";
1491
Guido van Rossum048eb752001-10-02 21:24:57 +00001492static int
1493type_traverse(PyTypeObject *type, visitproc visit, void *arg)
1494{
1495 etype *et;
1496 int err;
1497
1498 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1499 return 0;
1500
1501 et = (etype *)type;
1502
1503#define VISIT(SLOT) \
1504 if (SLOT) { \
1505 err = visit((PyObject *)(SLOT), arg); \
1506 if (err) \
1507 return err; \
1508 }
1509
1510 VISIT(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001511 VISIT(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001512 VISIT(type->tp_mro);
1513 VISIT(type->tp_bases);
1514 VISIT(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001515 VISIT(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001516 VISIT(et->slots);
1517
1518#undef VISIT
1519
1520 return 0;
1521}
1522
1523static int
1524type_clear(PyTypeObject *type)
1525{
1526 etype *et;
1527 PyObject *tmp;
1528
1529 if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1530 return 0;
1531
1532 et = (etype *)type;
1533
1534#define CLEAR(SLOT) \
1535 if (SLOT) { \
1536 tmp = (PyObject *)(SLOT); \
1537 SLOT = NULL; \
1538 Py_DECREF(tmp); \
1539 }
1540
1541 CLEAR(type->tp_dict);
Guido van Rossum687ae002001-10-15 22:03:32 +00001542 CLEAR(type->tp_cache);
Guido van Rossum048eb752001-10-02 21:24:57 +00001543 CLEAR(type->tp_mro);
1544 CLEAR(type->tp_bases);
1545 CLEAR(type->tp_base);
Guido van Rossum1c450732001-10-08 15:18:27 +00001546 CLEAR(type->tp_subclasses);
Guido van Rossum048eb752001-10-02 21:24:57 +00001547 CLEAR(et->slots);
1548
Tim Peters2f93e282001-10-04 05:27:00 +00001549 if (type->tp_doc != NULL) {
1550 PyObject_FREE(type->tp_doc);
1551 type->tp_doc = NULL;
1552 }
1553
Guido van Rossum048eb752001-10-02 21:24:57 +00001554#undef CLEAR
1555
1556 return 0;
1557}
1558
1559static int
1560type_is_gc(PyTypeObject *type)
1561{
1562 return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
1563}
1564
Guido van Rossumc0b618a1997-05-02 03:12:38 +00001565PyTypeObject PyType_Type = {
1566 PyObject_HEAD_INIT(&PyType_Type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001567 0, /* ob_size */
1568 "type", /* tp_name */
1569 sizeof(etype), /* tp_basicsize */
Guido van Rossum6f799372001-09-20 20:46:19 +00001570 sizeof(PyMemberDef), /* tp_itemsize */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001571 (destructor)type_dealloc, /* tp_dealloc */
1572 0, /* tp_print */
1573 0, /* tp_getattr */
1574 0, /* tp_setattr */
1575 type_compare, /* tp_compare */
1576 (reprfunc)type_repr, /* tp_repr */
1577 0, /* tp_as_number */
1578 0, /* tp_as_sequence */
1579 0, /* tp_as_mapping */
1580 (hashfunc)_Py_HashPointer, /* tp_hash */
1581 (ternaryfunc)type_call, /* tp_call */
1582 0, /* tp_str */
1583 (getattrofunc)type_getattro, /* tp_getattro */
1584 (setattrofunc)type_setattro, /* tp_setattro */
1585 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00001586 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
1587 Py_TPFLAGS_BASETYPE, /* tp_flags */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001588 type_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00001589 (traverseproc)type_traverse, /* tp_traverse */
1590 (inquiry)type_clear, /* tp_clear */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001591 0, /* tp_richcompare */
Guido van Rossum1c450732001-10-08 15:18:27 +00001592 offsetof(PyTypeObject, tp_weaklist), /* tp_weaklistoffset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001593 0, /* tp_iter */
1594 0, /* tp_iternext */
1595 type_methods, /* tp_methods */
1596 type_members, /* tp_members */
1597 type_getsets, /* tp_getset */
1598 0, /* tp_base */
1599 0, /* tp_dict */
1600 0, /* tp_descr_get */
1601 0, /* tp_descr_set */
1602 offsetof(PyTypeObject, tp_dict), /* tp_dictoffset */
1603 0, /* tp_init */
1604 0, /* tp_alloc */
1605 type_new, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001606 PyObject_GC_Del, /* tp_free */
Guido van Rossum048eb752001-10-02 21:24:57 +00001607 (inquiry)type_is_gc, /* tp_is_gc */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001608};
Tim Peters6d6c1a32001-08-02 04:15:00 +00001609
1610
1611/* The base type of all types (eventually)... except itself. */
1612
1613static int
1614object_init(PyObject *self, PyObject *args, PyObject *kwds)
1615{
1616 return 0;
1617}
1618
1619static void
1620object_dealloc(PyObject *self)
1621{
1622 self->ob_type->tp_free(self);
1623}
1624
Guido van Rossum8e248182001-08-12 05:17:56 +00001625static PyObject *
1626object_repr(PyObject *self)
1627{
Guido van Rossum76e69632001-08-16 18:52:43 +00001628 PyTypeObject *type;
Barry Warsaw7ce36942001-08-24 18:34:26 +00001629 PyObject *mod, *name, *rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001630
Guido van Rossum76e69632001-08-16 18:52:43 +00001631 type = self->ob_type;
1632 mod = type_module(type, NULL);
1633 if (mod == NULL)
1634 PyErr_Clear();
1635 else if (!PyString_Check(mod)) {
1636 Py_DECREF(mod);
1637 mod = NULL;
1638 }
1639 name = type_name(type, NULL);
1640 if (name == NULL)
1641 return NULL;
1642 if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001643 rtn = PyString_FromFormat("<%s.%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001644 PyString_AS_STRING(mod),
1645 PyString_AS_STRING(name),
1646 self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001647 else
Guido van Rossumff0e6d62001-09-24 16:03:59 +00001648 rtn = PyString_FromFormat("<%s object at %p>",
Barry Warsaw7ce36942001-08-24 18:34:26 +00001649 type->tp_name, self);
Guido van Rossum76e69632001-08-16 18:52:43 +00001650 Py_XDECREF(mod);
1651 Py_DECREF(name);
Barry Warsaw7ce36942001-08-24 18:34:26 +00001652 return rtn;
Guido van Rossum8e248182001-08-12 05:17:56 +00001653}
1654
Guido van Rossumb8f63662001-08-15 23:57:02 +00001655static PyObject *
1656object_str(PyObject *self)
1657{
1658 unaryfunc f;
1659
1660 f = self->ob_type->tp_repr;
1661 if (f == NULL)
1662 f = object_repr;
1663 return f(self);
1664}
1665
Guido van Rossum8e248182001-08-12 05:17:56 +00001666static long
1667object_hash(PyObject *self)
1668{
1669 return _Py_HashPointer(self);
1670}
Guido van Rossum8e248182001-08-12 05:17:56 +00001671
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001672static PyObject *
1673object_get_class(PyObject *self, void *closure)
1674{
1675 Py_INCREF(self->ob_type);
1676 return (PyObject *)(self->ob_type);
1677}
1678
1679static int
1680equiv_structs(PyTypeObject *a, PyTypeObject *b)
1681{
1682 return a == b ||
1683 (a != NULL &&
1684 b != NULL &&
1685 a->tp_basicsize == b->tp_basicsize &&
1686 a->tp_itemsize == b->tp_itemsize &&
1687 a->tp_dictoffset == b->tp_dictoffset &&
1688 a->tp_weaklistoffset == b->tp_weaklistoffset &&
1689 ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
1690 (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
1691}
1692
1693static int
1694same_slots_added(PyTypeObject *a, PyTypeObject *b)
1695{
1696 PyTypeObject *base = a->tp_base;
1697 int size;
1698
1699 if (base != b->tp_base)
1700 return 0;
1701 if (equiv_structs(a, base) && equiv_structs(b, base))
1702 return 1;
1703 size = base->tp_basicsize;
1704 if (a->tp_dictoffset == size && b->tp_dictoffset == size)
1705 size += sizeof(PyObject *);
1706 if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
1707 size += sizeof(PyObject *);
1708 return size == a->tp_basicsize && size == b->tp_basicsize;
1709}
1710
1711static int
1712object_set_class(PyObject *self, PyObject *value, void *closure)
1713{
1714 PyTypeObject *old = self->ob_type;
1715 PyTypeObject *new, *newbase, *oldbase;
1716
Guido van Rossumb6b89422002-04-15 01:03:30 +00001717 if (value == NULL) {
1718 PyErr_SetString(PyExc_TypeError,
1719 "can't delete __class__ attribute");
1720 return -1;
1721 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001722 if (!PyType_Check(value)) {
1723 PyErr_Format(PyExc_TypeError,
1724 "__class__ must be set to new-style class, not '%s' object",
1725 value->ob_type->tp_name);
1726 return -1;
1727 }
1728 new = (PyTypeObject *)value;
Guido van Rossum9ee4b942002-05-24 18:47:47 +00001729 if (new->tp_dealloc != old->tp_dealloc ||
1730 new->tp_free != old->tp_free)
1731 {
1732 PyErr_Format(PyExc_TypeError,
1733 "__class__ assignment: "
1734 "'%s' deallocator differs from '%s'",
1735 new->tp_name,
1736 old->tp_name);
1737 return -1;
1738 }
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001739 newbase = new;
1740 oldbase = old;
1741 while (equiv_structs(newbase, newbase->tp_base))
1742 newbase = newbase->tp_base;
1743 while (equiv_structs(oldbase, oldbase->tp_base))
1744 oldbase = oldbase->tp_base;
1745 if (newbase != oldbase &&
1746 (newbase->tp_base != oldbase->tp_base ||
1747 !same_slots_added(newbase, oldbase))) {
1748 PyErr_Format(PyExc_TypeError,
1749 "__class__ assignment: "
1750 "'%s' object layout differs from '%s'",
Tim Peters2f93e282001-10-04 05:27:00 +00001751 new->tp_name,
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001752 old->tp_name);
1753 return -1;
1754 }
1755 if (new->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1756 Py_INCREF(new);
1757 }
1758 self->ob_type = new;
1759 if (old->tp_flags & Py_TPFLAGS_HEAPTYPE) {
1760 Py_DECREF(old);
1761 }
1762 return 0;
1763}
1764
1765static PyGetSetDef object_getsets[] = {
1766 {"__class__", object_get_class, object_set_class,
1767 "the object's class"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00001768 {0}
1769};
1770
Guido van Rossum3926a632001-09-25 16:25:58 +00001771static PyObject *
1772object_reduce(PyObject *self, PyObject *args)
1773{
1774 /* Call copy_reg._reduce(self) */
1775 static PyObject *copy_reg_str;
1776 PyObject *copy_reg, *res;
1777
1778 if (!copy_reg_str) {
1779 copy_reg_str = PyString_InternFromString("copy_reg");
1780 if (copy_reg_str == NULL)
1781 return NULL;
1782 }
1783 copy_reg = PyImport_Import(copy_reg_str);
1784 if (!copy_reg)
1785 return NULL;
1786 res = PyEval_CallMethod(copy_reg, "_reduce", "(O)", self);
1787 Py_DECREF(copy_reg);
1788 return res;
1789}
1790
1791static PyMethodDef object_methods[] = {
1792 {"__reduce__", object_reduce, METH_NOARGS, "helper for pickle"},
1793 {0}
1794};
1795
Tim Peters6d6c1a32001-08-02 04:15:00 +00001796PyTypeObject PyBaseObject_Type = {
1797 PyObject_HEAD_INIT(&PyType_Type)
1798 0, /* ob_size */
1799 "object", /* tp_name */
1800 sizeof(PyObject), /* tp_basicsize */
1801 0, /* tp_itemsize */
1802 (destructor)object_dealloc, /* tp_dealloc */
1803 0, /* tp_print */
1804 0, /* tp_getattr */
1805 0, /* tp_setattr */
1806 0, /* tp_compare */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001807 object_repr, /* tp_repr */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001808 0, /* tp_as_number */
1809 0, /* tp_as_sequence */
1810 0, /* tp_as_mapping */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001811 object_hash, /* tp_hash */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001812 0, /* tp_call */
Guido van Rossumb8f63662001-08-15 23:57:02 +00001813 object_str, /* tp_str */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001814 PyObject_GenericGetAttr, /* tp_getattro */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001815 PyObject_GenericSetAttr, /* tp_setattro */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001816 0, /* tp_as_buffer */
1817 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
1818 "The most base type", /* tp_doc */
1819 0, /* tp_traverse */
1820 0, /* tp_clear */
1821 0, /* tp_richcompare */
1822 0, /* tp_weaklistoffset */
1823 0, /* tp_iter */
1824 0, /* tp_iternext */
Guido van Rossum3926a632001-09-25 16:25:58 +00001825 object_methods, /* tp_methods */
Guido van Rossum5c294fb2001-09-25 03:43:42 +00001826 0, /* tp_members */
1827 object_getsets, /* tp_getset */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001828 0, /* tp_base */
1829 0, /* tp_dict */
1830 0, /* tp_descr_get */
1831 0, /* tp_descr_set */
1832 0, /* tp_dictoffset */
1833 object_init, /* tp_init */
1834 PyType_GenericAlloc, /* tp_alloc */
Guido van Rossumc11e1922001-08-09 19:38:15 +00001835 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00001836 PyObject_Del, /* tp_free */
Tim Peters6d6c1a32001-08-02 04:15:00 +00001837};
1838
1839
1840/* Initialize the __dict__ in a type object */
1841
Fred Drake7bf97152002-03-28 05:33:33 +00001842static PyObject *
1843create_specialmethod(PyMethodDef *meth, PyObject *(*func)(PyObject *))
1844{
1845 PyObject *cfunc;
1846 PyObject *result;
1847
1848 cfunc = PyCFunction_New(meth, NULL);
1849 if (cfunc == NULL)
1850 return NULL;
1851 result = func(cfunc);
1852 Py_DECREF(cfunc);
1853 return result;
1854}
1855
Tim Peters6d6c1a32001-08-02 04:15:00 +00001856static int
1857add_methods(PyTypeObject *type, PyMethodDef *meth)
1858{
Guido van Rossum687ae002001-10-15 22:03:32 +00001859 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001860
1861 for (; meth->ml_name != NULL; meth++) {
1862 PyObject *descr;
1863 if (PyDict_GetItemString(dict, meth->ml_name))
1864 continue;
Fred Drake7bf97152002-03-28 05:33:33 +00001865 if (meth->ml_flags & METH_CLASS) {
1866 if (meth->ml_flags & METH_STATIC) {
1867 PyErr_SetString(PyExc_ValueError,
1868 "method cannot be both class and static");
1869 return -1;
1870 }
1871 descr = create_specialmethod(meth, PyClassMethod_New);
1872 }
1873 else if (meth->ml_flags & METH_STATIC) {
1874 descr = create_specialmethod(meth, PyStaticMethod_New);
1875 }
1876 else {
1877 descr = PyDescr_NewMethod(type, meth);
1878 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00001879 if (descr == NULL)
1880 return -1;
Fred Drake7bf97152002-03-28 05:33:33 +00001881 if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001882 return -1;
1883 Py_DECREF(descr);
1884 }
1885 return 0;
1886}
1887
1888static int
Guido van Rossum6f799372001-09-20 20:46:19 +00001889add_members(PyTypeObject *type, PyMemberDef *memb)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001890{
Guido van Rossum687ae002001-10-15 22:03:32 +00001891 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001892
1893 for (; memb->name != NULL; memb++) {
1894 PyObject *descr;
1895 if (PyDict_GetItemString(dict, memb->name))
1896 continue;
1897 descr = PyDescr_NewMember(type, memb);
1898 if (descr == NULL)
1899 return -1;
1900 if (PyDict_SetItemString(dict, memb->name, descr) < 0)
1901 return -1;
1902 Py_DECREF(descr);
1903 }
1904 return 0;
1905}
1906
1907static int
Guido van Rossum32d34c82001-09-20 21:45:26 +00001908add_getset(PyTypeObject *type, PyGetSetDef *gsp)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001909{
Guido van Rossum687ae002001-10-15 22:03:32 +00001910 PyObject *dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00001911
1912 for (; gsp->name != NULL; gsp++) {
1913 PyObject *descr;
1914 if (PyDict_GetItemString(dict, gsp->name))
1915 continue;
1916 descr = PyDescr_NewGetSet(type, gsp);
1917
1918 if (descr == NULL)
1919 return -1;
1920 if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
1921 return -1;
1922 Py_DECREF(descr);
1923 }
1924 return 0;
1925}
1926
Guido van Rossum13d52f02001-08-10 21:24:08 +00001927static void
1928inherit_special(PyTypeObject *type, PyTypeObject *base)
Tim Peters6d6c1a32001-08-02 04:15:00 +00001929{
1930 int oldsize, newsize;
1931
Guido van Rossum13d52f02001-08-10 21:24:08 +00001932 /* Special flag magic */
1933 if (!type->tp_as_buffer && base->tp_as_buffer) {
1934 type->tp_flags &= ~Py_TPFLAGS_HAVE_GETCHARBUFFER;
1935 type->tp_flags |=
1936 base->tp_flags & Py_TPFLAGS_HAVE_GETCHARBUFFER;
1937 }
1938 if (!type->tp_as_sequence && base->tp_as_sequence) {
1939 type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
1940 type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
1941 }
1942 if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
1943 (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
1944 if ((!type->tp_as_number && base->tp_as_number) ||
1945 (!type->tp_as_sequence && base->tp_as_sequence)) {
1946 type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
1947 if (!type->tp_as_number && !type->tp_as_sequence) {
1948 type->tp_flags |= base->tp_flags &
1949 Py_TPFLAGS_HAVE_INPLACEOPS;
1950 }
1951 }
1952 /* Wow */
1953 }
1954 if (!type->tp_as_number && base->tp_as_number) {
1955 type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
1956 type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
1957 }
1958
1959 /* Copying basicsize is connected to the GC flags */
Neil Schemenauerc806c882001-08-29 23:54:54 +00001960 oldsize = base->tp_basicsize;
1961 newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
1962 if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
1963 (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
Guido van Rossum13d52f02001-08-10 21:24:08 +00001964 (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
1965 (!type->tp_traverse && !type->tp_clear)) {
Neil Schemenauerc806c882001-08-29 23:54:54 +00001966 type->tp_flags |= Py_TPFLAGS_HAVE_GC;
Guido van Rossum13d52f02001-08-10 21:24:08 +00001967 if (type->tp_traverse == NULL)
1968 type->tp_traverse = base->tp_traverse;
1969 if (type->tp_clear == NULL)
1970 type->tp_clear = base->tp_clear;
1971 }
1972 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
Guido van Rossumf884b742001-12-17 17:14:22 +00001973 /* The condition below could use some explanation.
1974 It appears that tp_new is not inherited for static types
1975 whose base class is 'object'; this seems to be a precaution
1976 so that old extension types don't suddenly become
1977 callable (object.__new__ wouldn't insure the invariants
1978 that the extension type's own factory function ensures).
1979 Heap types, of course, are under our control, so they do
1980 inherit tp_new; static extension types that specify some
1981 other built-in type as the default are considered
1982 new-style-aware so they also inherit object.__new__. */
Guido van Rossum13d52f02001-08-10 21:24:08 +00001983 if (base != &PyBaseObject_Type ||
1984 (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
1985 if (type->tp_new == NULL)
1986 type->tp_new = base->tp_new;
1987 }
1988 }
Neil Schemenauerc806c882001-08-29 23:54:54 +00001989 type->tp_basicsize = newsize;
Guido van Rossum4dd64ab2001-08-14 20:04:48 +00001990
1991 /* Copy other non-function slots */
1992
1993#undef COPYVAL
1994#define COPYVAL(SLOT) \
1995 if (type->SLOT == 0) type->SLOT = base->SLOT
1996
1997 COPYVAL(tp_itemsize);
1998 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
1999 COPYVAL(tp_weaklistoffset);
2000 }
2001 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2002 COPYVAL(tp_dictoffset);
2003 }
Guido van Rossum13d52f02001-08-10 21:24:08 +00002004}
2005
2006static void
2007inherit_slots(PyTypeObject *type, PyTypeObject *base)
2008{
2009 PyTypeObject *basebase;
2010
2011#undef SLOTDEFINED
Tim Peters6d6c1a32001-08-02 04:15:00 +00002012#undef COPYSLOT
2013#undef COPYNUM
2014#undef COPYSEQ
2015#undef COPYMAP
Guido van Rossum5af588b2001-10-12 14:13:21 +00002016#undef COPYBUF
Guido van Rossum13d52f02001-08-10 21:24:08 +00002017
2018#define SLOTDEFINED(SLOT) \
2019 (base->SLOT != 0 && \
2020 (basebase == NULL || base->SLOT != basebase->SLOT))
2021
Tim Peters6d6c1a32001-08-02 04:15:00 +00002022#define COPYSLOT(SLOT) \
Guido van Rossum13d52f02001-08-10 21:24:08 +00002023 if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
Tim Peters6d6c1a32001-08-02 04:15:00 +00002024
2025#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
2026#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
2027#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
Tim Petersfc57ccb2001-10-12 02:38:24 +00002028#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002029
Guido van Rossum13d52f02001-08-10 21:24:08 +00002030 /* This won't inherit indirect slots (from tp_as_number etc.)
2031 if type doesn't provide the space. */
2032
2033 if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
2034 basebase = base->tp_base;
2035 if (basebase->tp_as_number == NULL)
2036 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002037 COPYNUM(nb_add);
2038 COPYNUM(nb_subtract);
2039 COPYNUM(nb_multiply);
2040 COPYNUM(nb_divide);
2041 COPYNUM(nb_remainder);
2042 COPYNUM(nb_divmod);
2043 COPYNUM(nb_power);
2044 COPYNUM(nb_negative);
2045 COPYNUM(nb_positive);
2046 COPYNUM(nb_absolute);
2047 COPYNUM(nb_nonzero);
2048 COPYNUM(nb_invert);
2049 COPYNUM(nb_lshift);
2050 COPYNUM(nb_rshift);
2051 COPYNUM(nb_and);
2052 COPYNUM(nb_xor);
2053 COPYNUM(nb_or);
2054 COPYNUM(nb_coerce);
2055 COPYNUM(nb_int);
2056 COPYNUM(nb_long);
2057 COPYNUM(nb_float);
2058 COPYNUM(nb_oct);
2059 COPYNUM(nb_hex);
2060 COPYNUM(nb_inplace_add);
2061 COPYNUM(nb_inplace_subtract);
2062 COPYNUM(nb_inplace_multiply);
2063 COPYNUM(nb_inplace_divide);
2064 COPYNUM(nb_inplace_remainder);
2065 COPYNUM(nb_inplace_power);
2066 COPYNUM(nb_inplace_lshift);
2067 COPYNUM(nb_inplace_rshift);
2068 COPYNUM(nb_inplace_and);
2069 COPYNUM(nb_inplace_xor);
2070 COPYNUM(nb_inplace_or);
Guido van Rossumdc91b992001-08-08 22:26:22 +00002071 if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
2072 COPYNUM(nb_true_divide);
2073 COPYNUM(nb_floor_divide);
2074 COPYNUM(nb_inplace_true_divide);
2075 COPYNUM(nb_inplace_floor_divide);
2076 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002077 }
2078
Guido van Rossum13d52f02001-08-10 21:24:08 +00002079 if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
2080 basebase = base->tp_base;
2081 if (basebase->tp_as_sequence == NULL)
2082 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002083 COPYSEQ(sq_length);
2084 COPYSEQ(sq_concat);
2085 COPYSEQ(sq_repeat);
2086 COPYSEQ(sq_item);
2087 COPYSEQ(sq_slice);
2088 COPYSEQ(sq_ass_item);
2089 COPYSEQ(sq_ass_slice);
2090 COPYSEQ(sq_contains);
2091 COPYSEQ(sq_inplace_concat);
2092 COPYSEQ(sq_inplace_repeat);
2093 }
2094
Guido van Rossum13d52f02001-08-10 21:24:08 +00002095 if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
2096 basebase = base->tp_base;
2097 if (basebase->tp_as_mapping == NULL)
2098 basebase = NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002099 COPYMAP(mp_length);
2100 COPYMAP(mp_subscript);
2101 COPYMAP(mp_ass_subscript);
2102 }
2103
Tim Petersfc57ccb2001-10-12 02:38:24 +00002104 if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
2105 basebase = base->tp_base;
2106 if (basebase->tp_as_buffer == NULL)
2107 basebase = NULL;
2108 COPYBUF(bf_getreadbuffer);
2109 COPYBUF(bf_getwritebuffer);
2110 COPYBUF(bf_getsegcount);
2111 COPYBUF(bf_getcharbuffer);
2112 }
2113
Guido van Rossum13d52f02001-08-10 21:24:08 +00002114 basebase = base->tp_base;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002115
Tim Peters6d6c1a32001-08-02 04:15:00 +00002116 COPYSLOT(tp_dealloc);
2117 COPYSLOT(tp_print);
2118 if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
2119 type->tp_getattr = base->tp_getattr;
2120 type->tp_getattro = base->tp_getattro;
2121 }
2122 if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
2123 type->tp_setattr = base->tp_setattr;
2124 type->tp_setattro = base->tp_setattro;
2125 }
2126 /* tp_compare see tp_richcompare */
2127 COPYSLOT(tp_repr);
Guido van Rossumb8f63662001-08-15 23:57:02 +00002128 /* tp_hash see tp_richcompare */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002129 COPYSLOT(tp_call);
2130 COPYSLOT(tp_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002131 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00002132 if (type->tp_compare == NULL &&
2133 type->tp_richcompare == NULL &&
2134 type->tp_hash == NULL)
2135 {
Tim Peters6d6c1a32001-08-02 04:15:00 +00002136 type->tp_compare = base->tp_compare;
2137 type->tp_richcompare = base->tp_richcompare;
Guido van Rossumb8f63662001-08-15 23:57:02 +00002138 type->tp_hash = base->tp_hash;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002139 }
2140 }
2141 else {
2142 COPYSLOT(tp_compare);
2143 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002144 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
2145 COPYSLOT(tp_iter);
2146 COPYSLOT(tp_iternext);
2147 }
2148 if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
2149 COPYSLOT(tp_descr_get);
2150 COPYSLOT(tp_descr_set);
2151 COPYSLOT(tp_dictoffset);
2152 COPYSLOT(tp_init);
2153 COPYSLOT(tp_alloc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002154 COPYSLOT(tp_free);
Guido van Rossumcc8fe042002-04-05 17:10:16 +00002155 COPYSLOT(tp_is_gc);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002156 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002157}
2158
Guido van Rossum13d52f02001-08-10 21:24:08 +00002159staticforward int add_operators(PyTypeObject *);
Guido van Rossum1c450732001-10-08 15:18:27 +00002160staticforward int add_subclass(PyTypeObject *base, PyTypeObject *type);
Guido van Rossum13d52f02001-08-10 21:24:08 +00002161
Tim Peters6d6c1a32001-08-02 04:15:00 +00002162int
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002163PyType_Ready(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002164{
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002165 PyObject *dict, *bases;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002166 PyTypeObject *base;
2167 int i, n;
2168
Guido van Rossumd614f972001-08-10 17:39:49 +00002169 if (type->tp_flags & Py_TPFLAGS_READY) {
2170 assert(type->tp_dict != NULL);
2171 return 0;
2172 }
2173 assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
Guido van Rossumd614f972001-08-10 17:39:49 +00002174
2175 type->tp_flags |= Py_TPFLAGS_READYING;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002176
2177 /* Initialize tp_base (defaults to BaseObject unless that's us) */
2178 base = type->tp_base;
2179 if (base == NULL && type != &PyBaseObject_Type)
2180 base = type->tp_base = &PyBaseObject_Type;
2181
Guido van Rossum0986d822002-04-08 01:38:42 +00002182 /* Initialize ob_type if NULL. This means extensions that want to be
2183 compilable separately on Windows can call PyType_Ready() instead of
2184 initializing the ob_type field of their type objects. */
2185 if (type->ob_type == NULL)
2186 type->ob_type = base->ob_type;
2187
Tim Peters6d6c1a32001-08-02 04:15:00 +00002188 /* Initialize tp_bases */
2189 bases = type->tp_bases;
2190 if (bases == NULL) {
2191 if (base == NULL)
2192 bases = PyTuple_New(0);
2193 else
2194 bases = Py_BuildValue("(O)", base);
2195 if (bases == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002196 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002197 type->tp_bases = bases;
2198 }
2199
2200 /* Initialize the base class */
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002201 if (base && base->tp_dict == NULL) {
Guido van Rossum528b7eb2001-08-07 17:24:28 +00002202 if (PyType_Ready(base) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002203 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002204 }
2205
Guido van Rossum687ae002001-10-15 22:03:32 +00002206 /* Initialize tp_dict */
2207 dict = type->tp_dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002208 if (dict == NULL) {
2209 dict = PyDict_New();
2210 if (dict == NULL)
Guido van Rossumd614f972001-08-10 17:39:49 +00002211 goto error;
Guido van Rossum687ae002001-10-15 22:03:32 +00002212 type->tp_dict = dict;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002213 }
2214
Guido van Rossum687ae002001-10-15 22:03:32 +00002215 /* Add type-specific descriptors to tp_dict */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002216 if (add_operators(type) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002217 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002218 if (type->tp_methods != NULL) {
2219 if (add_methods(type, type->tp_methods) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002220 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002221 }
2222 if (type->tp_members != NULL) {
2223 if (add_members(type, type->tp_members) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002224 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002225 }
2226 if (type->tp_getset != NULL) {
2227 if (add_getset(type, type->tp_getset) < 0)
Guido van Rossumd614f972001-08-10 17:39:49 +00002228 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002229 }
2230
Tim Peters6d6c1a32001-08-02 04:15:00 +00002231 /* Calculate method resolution order */
2232 if (mro_internal(type) < 0) {
Guido van Rossumd614f972001-08-10 17:39:49 +00002233 goto error;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002234 }
2235
Guido van Rossum13d52f02001-08-10 21:24:08 +00002236 /* Inherit special flags from dominant base */
2237 if (type->tp_base != NULL)
2238 inherit_special(type, type->tp_base);
2239
Tim Peters6d6c1a32001-08-02 04:15:00 +00002240 /* Initialize tp_dict properly */
Guido van Rossum2f3ca6e2001-10-15 21:05:10 +00002241 bases = type->tp_mro;
2242 assert(bases != NULL);
2243 assert(PyTuple_Check(bases));
2244 n = PyTuple_GET_SIZE(bases);
2245 for (i = 1; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002246 PyObject *b = PyTuple_GET_ITEM(bases, i);
2247 if (PyType_Check(b))
2248 inherit_slots(type, (PyTypeObject *)b);
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002249 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002250
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002251 /* if the type dictionary doesn't contain a __doc__, set it from
2252 the tp_doc slot.
2253 */
2254 if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
2255 if (type->tp_doc != NULL) {
2256 PyObject *doc = PyString_FromString(type->tp_doc);
2257 PyDict_SetItemString(type->tp_dict, "__doc__", doc);
2258 Py_DECREF(doc);
2259 } else {
Guido van Rossumd4641072002-04-03 02:13:37 +00002260 PyDict_SetItemString(type->tp_dict,
2261 "__doc__", Py_None);
Martin v. Löwisf9bd6b02002-02-18 17:46:48 +00002262 }
2263 }
2264
Guido van Rossum13d52f02001-08-10 21:24:08 +00002265 /* Some more special stuff */
2266 base = type->tp_base;
2267 if (base != NULL) {
2268 if (type->tp_as_number == NULL)
2269 type->tp_as_number = base->tp_as_number;
2270 if (type->tp_as_sequence == NULL)
2271 type->tp_as_sequence = base->tp_as_sequence;
2272 if (type->tp_as_mapping == NULL)
2273 type->tp_as_mapping = base->tp_as_mapping;
2274 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002275
Guido van Rossum1c450732001-10-08 15:18:27 +00002276 /* Link into each base class's list of subclasses */
2277 bases = type->tp_bases;
2278 n = PyTuple_GET_SIZE(bases);
2279 for (i = 0; i < n; i++) {
Tim Petersa91e9642001-11-14 23:32:33 +00002280 PyObject *b = PyTuple_GET_ITEM(bases, i);
2281 if (PyType_Check(b) &&
2282 add_subclass((PyTypeObject *)b, type) < 0)
Guido van Rossum1c450732001-10-08 15:18:27 +00002283 goto error;
2284 }
2285
Guido van Rossum13d52f02001-08-10 21:24:08 +00002286 /* All done -- set the ready flag */
Guido van Rossumd614f972001-08-10 17:39:49 +00002287 assert(type->tp_dict != NULL);
2288 type->tp_flags =
2289 (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002290 return 0;
Guido van Rossumd614f972001-08-10 17:39:49 +00002291
2292 error:
2293 type->tp_flags &= ~Py_TPFLAGS_READYING;
2294 return -1;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002295}
2296
Guido van Rossum1c450732001-10-08 15:18:27 +00002297static int
2298add_subclass(PyTypeObject *base, PyTypeObject *type)
2299{
2300 int i;
2301 PyObject *list, *ref, *new;
2302
2303 list = base->tp_subclasses;
2304 if (list == NULL) {
2305 base->tp_subclasses = list = PyList_New(0);
2306 if (list == NULL)
2307 return -1;
2308 }
2309 assert(PyList_Check(list));
2310 new = PyWeakref_NewRef((PyObject *)type, NULL);
2311 i = PyList_GET_SIZE(list);
2312 while (--i >= 0) {
2313 ref = PyList_GET_ITEM(list, i);
2314 assert(PyWeakref_CheckRef(ref));
2315 if (PyWeakref_GET_OBJECT(ref) == Py_None)
2316 return PyList_SetItem(list, i, new);
2317 }
2318 i = PyList_Append(list, new);
2319 Py_DECREF(new);
2320 return i;
2321}
2322
Tim Peters6d6c1a32001-08-02 04:15:00 +00002323
2324/* Generic wrappers for overloadable 'operators' such as __getitem__ */
2325
2326/* There's a wrapper *function* for each distinct function typedef used
2327 for type object slots (e.g. binaryfunc, ternaryfunc, etc.). There's a
2328 wrapper *table* for each distinct operation (e.g. __len__, __add__).
2329 Most tables have only one entry; the tables for binary operators have two
2330 entries, one regular and one with reversed arguments. */
2331
2332static PyObject *
2333wrap_inquiry(PyObject *self, PyObject *args, void *wrapped)
2334{
2335 inquiry func = (inquiry)wrapped;
2336 int res;
2337
2338 if (!PyArg_ParseTuple(args, ""))
2339 return NULL;
2340 res = (*func)(self);
2341 if (res == -1 && PyErr_Occurred())
2342 return NULL;
2343 return PyInt_FromLong((long)res);
2344}
2345
Tim Peters6d6c1a32001-08-02 04:15:00 +00002346static PyObject *
2347wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
2348{
2349 binaryfunc func = (binaryfunc)wrapped;
2350 PyObject *other;
2351
2352 if (!PyArg_ParseTuple(args, "O", &other))
2353 return NULL;
2354 return (*func)(self, other);
2355}
2356
2357static PyObject *
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002358wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
2359{
2360 binaryfunc func = (binaryfunc)wrapped;
2361 PyObject *other;
2362
2363 if (!PyArg_ParseTuple(args, "O", &other))
2364 return NULL;
2365 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002366 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002367 Py_INCREF(Py_NotImplemented);
2368 return Py_NotImplemented;
2369 }
2370 return (*func)(self, other);
2371}
2372
2373static PyObject *
Tim Peters6d6c1a32001-08-02 04:15:00 +00002374wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2375{
2376 binaryfunc func = (binaryfunc)wrapped;
2377 PyObject *other;
2378
2379 if (!PyArg_ParseTuple(args, "O", &other))
2380 return NULL;
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002381 if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002382 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossum0eb2a6e2001-10-09 11:07:24 +00002383 Py_INCREF(Py_NotImplemented);
2384 return Py_NotImplemented;
2385 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002386 return (*func)(other, self);
2387}
2388
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00002389static PyObject *
2390wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
2391{
2392 coercion func = (coercion)wrapped;
2393 PyObject *other, *res;
2394 int ok;
2395
2396 if (!PyArg_ParseTuple(args, "O", &other))
2397 return NULL;
2398 ok = func(&self, &other);
2399 if (ok < 0)
2400 return NULL;
2401 if (ok > 0) {
2402 Py_INCREF(Py_NotImplemented);
2403 return Py_NotImplemented;
2404 }
2405 res = PyTuple_New(2);
2406 if (res == NULL) {
2407 Py_DECREF(self);
2408 Py_DECREF(other);
2409 return NULL;
2410 }
2411 PyTuple_SET_ITEM(res, 0, self);
2412 PyTuple_SET_ITEM(res, 1, other);
2413 return res;
2414}
2415
Tim Peters6d6c1a32001-08-02 04:15:00 +00002416static PyObject *
2417wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
2418{
2419 ternaryfunc func = (ternaryfunc)wrapped;
2420 PyObject *other;
2421 PyObject *third = Py_None;
2422
2423 /* Note: This wrapper only works for __pow__() */
2424
2425 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2426 return NULL;
2427 return (*func)(self, other, third);
2428}
2429
Guido van Rossum9bea3ab2001-09-28 22:58:52 +00002430static PyObject *
2431wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
2432{
2433 ternaryfunc func = (ternaryfunc)wrapped;
2434 PyObject *other;
2435 PyObject *third = Py_None;
2436
2437 /* Note: This wrapper only works for __pow__() */
2438
2439 if (!PyArg_ParseTuple(args, "O|O", &other, &third))
2440 return NULL;
2441 return (*func)(other, self, third);
2442}
2443
Tim Peters6d6c1a32001-08-02 04:15:00 +00002444static PyObject *
2445wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
2446{
2447 unaryfunc func = (unaryfunc)wrapped;
2448
2449 if (!PyArg_ParseTuple(args, ""))
2450 return NULL;
2451 return (*func)(self);
2452}
2453
Tim Peters6d6c1a32001-08-02 04:15:00 +00002454static PyObject *
2455wrap_intargfunc(PyObject *self, PyObject *args, void *wrapped)
2456{
2457 intargfunc func = (intargfunc)wrapped;
2458 int i;
2459
2460 if (!PyArg_ParseTuple(args, "i", &i))
2461 return NULL;
2462 return (*func)(self, i);
2463}
2464
Guido van Rossum5d815f32001-08-17 21:57:47 +00002465static int
2466getindex(PyObject *self, PyObject *arg)
2467{
2468 int i;
2469
2470 i = PyInt_AsLong(arg);
2471 if (i == -1 && PyErr_Occurred())
2472 return -1;
2473 if (i < 0) {
2474 PySequenceMethods *sq = self->ob_type->tp_as_sequence;
2475 if (sq && sq->sq_length) {
2476 int n = (*sq->sq_length)(self);
2477 if (n < 0)
2478 return -1;
2479 i += n;
2480 }
2481 }
2482 return i;
2483}
2484
2485static PyObject *
2486wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
2487{
2488 intargfunc func = (intargfunc)wrapped;
2489 PyObject *arg;
2490 int i;
2491
Guido van Rossumf4593e02001-10-03 12:09:30 +00002492 if (PyTuple_GET_SIZE(args) == 1) {
2493 arg = PyTuple_GET_ITEM(args, 0);
2494 i = getindex(self, arg);
2495 if (i == -1 && PyErr_Occurred())
2496 return NULL;
2497 return (*func)(self, i);
2498 }
2499 PyArg_ParseTuple(args, "O", &arg);
2500 assert(PyErr_Occurred());
2501 return NULL;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002502}
2503
Tim Peters6d6c1a32001-08-02 04:15:00 +00002504static PyObject *
2505wrap_intintargfunc(PyObject *self, PyObject *args, void *wrapped)
2506{
2507 intintargfunc func = (intintargfunc)wrapped;
2508 int i, j;
2509
2510 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2511 return NULL;
2512 return (*func)(self, i, j);
2513}
2514
Tim Peters6d6c1a32001-08-02 04:15:00 +00002515static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002516wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002517{
2518 intobjargproc func = (intobjargproc)wrapped;
2519 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002520 PyObject *arg, *value;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002521
Guido van Rossum5d815f32001-08-17 21:57:47 +00002522 if (!PyArg_ParseTuple(args, "OO", &arg, &value))
2523 return NULL;
2524 i = getindex(self, arg);
2525 if (i == -1 && PyErr_Occurred())
Tim Peters6d6c1a32001-08-02 04:15:00 +00002526 return NULL;
2527 res = (*func)(self, i, value);
2528 if (res == -1 && PyErr_Occurred())
2529 return NULL;
2530 Py_INCREF(Py_None);
2531 return Py_None;
2532}
2533
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002534static PyObject *
Guido van Rossum5d815f32001-08-17 21:57:47 +00002535wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002536{
2537 intobjargproc func = (intobjargproc)wrapped;
2538 int i, res;
Guido van Rossum5d815f32001-08-17 21:57:47 +00002539 PyObject *arg;
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002540
Guido van Rossum5d815f32001-08-17 21:57:47 +00002541 if (!PyArg_ParseTuple(args, "O", &arg))
2542 return NULL;
2543 i = getindex(self, arg);
2544 if (i == -1 && PyErr_Occurred())
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002545 return NULL;
2546 res = (*func)(self, i, NULL);
2547 if (res == -1 && PyErr_Occurred())
2548 return NULL;
2549 Py_INCREF(Py_None);
2550 return Py_None;
2551}
2552
Tim Peters6d6c1a32001-08-02 04:15:00 +00002553static PyObject *
2554wrap_intintobjargproc(PyObject *self, PyObject *args, void *wrapped)
2555{
2556 intintobjargproc func = (intintobjargproc)wrapped;
2557 int i, j, res;
2558 PyObject *value;
2559
2560 if (!PyArg_ParseTuple(args, "iiO", &i, &j, &value))
2561 return NULL;
2562 res = (*func)(self, i, j, value);
2563 if (res == -1 && PyErr_Occurred())
2564 return NULL;
2565 Py_INCREF(Py_None);
2566 return Py_None;
2567}
2568
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002569static PyObject *
2570wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
2571{
2572 intintobjargproc func = (intintobjargproc)wrapped;
2573 int i, j, res;
2574
2575 if (!PyArg_ParseTuple(args, "ii", &i, &j))
2576 return NULL;
2577 res = (*func)(self, i, j, NULL);
2578 if (res == -1 && PyErr_Occurred())
2579 return NULL;
2580 Py_INCREF(Py_None);
2581 return Py_None;
2582}
2583
Tim Peters6d6c1a32001-08-02 04:15:00 +00002584/* XXX objobjproc is a misnomer; should be objargpred */
2585static PyObject *
2586wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
2587{
2588 objobjproc func = (objobjproc)wrapped;
2589 int res;
2590 PyObject *value;
2591
2592 if (!PyArg_ParseTuple(args, "O", &value))
2593 return NULL;
2594 res = (*func)(self, value);
2595 if (res == -1 && PyErr_Occurred())
2596 return NULL;
2597 return PyInt_FromLong((long)res);
2598}
2599
Tim Peters6d6c1a32001-08-02 04:15:00 +00002600static PyObject *
2601wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
2602{
2603 objobjargproc func = (objobjargproc)wrapped;
2604 int res;
2605 PyObject *key, *value;
2606
2607 if (!PyArg_ParseTuple(args, "OO", &key, &value))
2608 return NULL;
2609 res = (*func)(self, key, value);
2610 if (res == -1 && PyErr_Occurred())
2611 return NULL;
2612 Py_INCREF(Py_None);
2613 return Py_None;
2614}
2615
Guido van Rossum2b8d7bd2001-08-02 15:31:58 +00002616static PyObject *
2617wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
2618{
2619 objobjargproc func = (objobjargproc)wrapped;
2620 int res;
2621 PyObject *key;
2622
2623 if (!PyArg_ParseTuple(args, "O", &key))
2624 return NULL;
2625 res = (*func)(self, key, NULL);
2626 if (res == -1 && PyErr_Occurred())
2627 return NULL;
2628 Py_INCREF(Py_None);
2629 return Py_None;
2630}
2631
Tim Peters6d6c1a32001-08-02 04:15:00 +00002632static PyObject *
2633wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
2634{
2635 cmpfunc func = (cmpfunc)wrapped;
2636 int res;
2637 PyObject *other;
2638
2639 if (!PyArg_ParseTuple(args, "O", &other))
2640 return NULL;
Guido van Rossum3d45d8f2001-09-24 18:47:40 +00002641 if (other->ob_type->tp_compare != func &&
2642 !PyType_IsSubtype(other->ob_type, self->ob_type)) {
Guido van Rossumceccae52001-09-18 20:03:57 +00002643 PyErr_Format(
2644 PyExc_TypeError,
2645 "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
2646 self->ob_type->tp_name,
2647 self->ob_type->tp_name,
2648 other->ob_type->tp_name);
2649 return NULL;
2650 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00002651 res = (*func)(self, other);
2652 if (PyErr_Occurred())
2653 return NULL;
2654 return PyInt_FromLong((long)res);
2655}
2656
Tim Peters6d6c1a32001-08-02 04:15:00 +00002657static PyObject *
2658wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
2659{
2660 setattrofunc func = (setattrofunc)wrapped;
2661 int res;
2662 PyObject *name, *value;
2663
2664 if (!PyArg_ParseTuple(args, "OO", &name, &value))
2665 return NULL;
2666 res = (*func)(self, name, value);
2667 if (res < 0)
2668 return NULL;
2669 Py_INCREF(Py_None);
2670 return Py_None;
2671}
2672
2673static PyObject *
2674wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
2675{
2676 setattrofunc func = (setattrofunc)wrapped;
2677 int res;
2678 PyObject *name;
2679
2680 if (!PyArg_ParseTuple(args, "O", &name))
2681 return NULL;
2682 res = (*func)(self, name, NULL);
2683 if (res < 0)
2684 return NULL;
2685 Py_INCREF(Py_None);
2686 return Py_None;
2687}
2688
Tim Peters6d6c1a32001-08-02 04:15:00 +00002689static PyObject *
2690wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
2691{
2692 hashfunc func = (hashfunc)wrapped;
2693 long res;
2694
2695 if (!PyArg_ParseTuple(args, ""))
2696 return NULL;
2697 res = (*func)(self);
2698 if (res == -1 && PyErr_Occurred())
2699 return NULL;
2700 return PyInt_FromLong(res);
2701}
2702
Tim Peters6d6c1a32001-08-02 04:15:00 +00002703static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002704wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002705{
2706 ternaryfunc func = (ternaryfunc)wrapped;
2707
Guido van Rossumc8e56452001-10-22 00:43:43 +00002708 return (*func)(self, args, kwds);
Tim Peters6d6c1a32001-08-02 04:15:00 +00002709}
2710
Tim Peters6d6c1a32001-08-02 04:15:00 +00002711static PyObject *
2712wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
2713{
2714 richcmpfunc func = (richcmpfunc)wrapped;
2715 PyObject *other;
2716
2717 if (!PyArg_ParseTuple(args, "O", &other))
2718 return NULL;
2719 return (*func)(self, other, op);
2720}
2721
2722#undef RICHCMP_WRAPPER
2723#define RICHCMP_WRAPPER(NAME, OP) \
2724static PyObject * \
2725richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
2726{ \
2727 return wrap_richcmpfunc(self, args, wrapped, OP); \
2728}
2729
Jack Jansen8e938b42001-08-08 15:29:49 +00002730RICHCMP_WRAPPER(lt, Py_LT)
2731RICHCMP_WRAPPER(le, Py_LE)
2732RICHCMP_WRAPPER(eq, Py_EQ)
2733RICHCMP_WRAPPER(ne, Py_NE)
2734RICHCMP_WRAPPER(gt, Py_GT)
2735RICHCMP_WRAPPER(ge, Py_GE)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002736
Tim Peters6d6c1a32001-08-02 04:15:00 +00002737static PyObject *
2738wrap_next(PyObject *self, PyObject *args, void *wrapped)
2739{
2740 unaryfunc func = (unaryfunc)wrapped;
2741 PyObject *res;
2742
2743 if (!PyArg_ParseTuple(args, ""))
2744 return NULL;
2745 res = (*func)(self);
2746 if (res == NULL && !PyErr_Occurred())
2747 PyErr_SetNone(PyExc_StopIteration);
2748 return res;
2749}
2750
Tim Peters6d6c1a32001-08-02 04:15:00 +00002751static PyObject *
2752wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
2753{
2754 descrgetfunc func = (descrgetfunc)wrapped;
2755 PyObject *obj;
2756 PyObject *type = NULL;
2757
2758 if (!PyArg_ParseTuple(args, "O|O", &obj, &type))
2759 return NULL;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002760 return (*func)(self, obj, type);
2761}
2762
Tim Peters6d6c1a32001-08-02 04:15:00 +00002763static PyObject *
Guido van Rossum7b9144b2001-10-09 19:39:46 +00002764wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002765{
2766 descrsetfunc func = (descrsetfunc)wrapped;
2767 PyObject *obj, *value;
2768 int ret;
2769
2770 if (!PyArg_ParseTuple(args, "OO", &obj, &value))
2771 return NULL;
2772 ret = (*func)(self, obj, value);
2773 if (ret < 0)
2774 return NULL;
2775 Py_INCREF(Py_None);
2776 return Py_None;
2777}
2778
Tim Peters6d6c1a32001-08-02 04:15:00 +00002779static PyObject *
Guido van Rossumc8e56452001-10-22 00:43:43 +00002780wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002781{
2782 initproc func = (initproc)wrapped;
2783
Guido van Rossumc8e56452001-10-22 00:43:43 +00002784 if (func(self, args, kwds) < 0)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002785 return NULL;
2786 Py_INCREF(Py_None);
2787 return Py_None;
2788}
2789
Tim Peters6d6c1a32001-08-02 04:15:00 +00002790static PyObject *
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002791tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
Tim Peters6d6c1a32001-08-02 04:15:00 +00002792{
Barry Warsaw60f01882001-08-22 19:24:42 +00002793 PyTypeObject *type, *subtype, *staticbase;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002794 PyObject *arg0, *res;
2795
2796 if (self == NULL || !PyType_Check(self))
2797 Py_FatalError("__new__() called with non-type 'self'");
2798 type = (PyTypeObject *)self;
2799 if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002800 PyErr_Format(PyExc_TypeError,
2801 "%s.__new__(): not enough arguments",
2802 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002803 return NULL;
2804 }
2805 arg0 = PyTuple_GET_ITEM(args, 0);
2806 if (!PyType_Check(arg0)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002807 PyErr_Format(PyExc_TypeError,
2808 "%s.__new__(X): X is not a type object (%s)",
2809 type->tp_name,
2810 arg0->ob_type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002811 return NULL;
2812 }
2813 subtype = (PyTypeObject *)arg0;
2814 if (!PyType_IsSubtype(subtype, type)) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002815 PyErr_Format(PyExc_TypeError,
2816 "%s.__new__(%s): %s is not a subtype of %s",
2817 type->tp_name,
2818 subtype->tp_name,
2819 subtype->tp_name,
2820 type->tp_name);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002821 return NULL;
2822 }
Barry Warsaw60f01882001-08-22 19:24:42 +00002823
2824 /* Check that the use doesn't do something silly and unsafe like
Tim Petersa427a2b2001-10-29 22:25:45 +00002825 object.__new__(dict). To do this, we check that the
Barry Warsaw60f01882001-08-22 19:24:42 +00002826 most derived base that's not a heap type is this type. */
2827 staticbase = subtype;
2828 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
2829 staticbase = staticbase->tp_base;
Guido van Rossuma8c60f42001-09-14 19:43:36 +00002830 if (staticbase->tp_new != type->tp_new) {
Barry Warsaw60f01882001-08-22 19:24:42 +00002831 PyErr_Format(PyExc_TypeError,
2832 "%s.__new__(%s) is not safe, use %s.__new__()",
2833 type->tp_name,
2834 subtype->tp_name,
2835 staticbase == NULL ? "?" : staticbase->tp_name);
2836 return NULL;
2837 }
2838
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002839 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
2840 if (args == NULL)
2841 return NULL;
2842 res = type->tp_new(subtype, args, kwds);
2843 Py_DECREF(args);
2844 return res;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002845}
2846
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002847static struct PyMethodDef tp_new_methoddef[] = {
2848 {"__new__", (PyCFunction)tp_new_wrapper, METH_KEYWORDS,
2849 "T.__new__(S, ...) -> a new object with type S, a subtype of T"},
Tim Peters6d6c1a32001-08-02 04:15:00 +00002850 {0}
2851};
2852
2853static int
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002854add_tp_new_wrapper(PyTypeObject *type)
2855{
Guido van Rossumf040ede2001-08-07 16:40:56 +00002856 PyObject *func;
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002857
Guido van Rossum687ae002001-10-15 22:03:32 +00002858 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
Guido van Rossumf040ede2001-08-07 16:40:56 +00002859 return 0;
2860 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002861 if (func == NULL)
2862 return -1;
Guido van Rossum687ae002001-10-15 22:03:32 +00002863 return PyDict_SetItemString(type->tp_dict, "__new__", func);
Guido van Rossum0d231ed2001-08-06 16:50:37 +00002864}
2865
Guido van Rossumf040ede2001-08-07 16:40:56 +00002866/* Slot wrappers that call the corresponding __foo__ slot. See comments
2867 below at override_slots() for more explanation. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00002868
Guido van Rossumdc91b992001-08-08 22:26:22 +00002869#define SLOT0(FUNCNAME, OPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002870static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002871FUNCNAME(PyObject *self) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002872{ \
Guido van Rossum5592e4d2001-08-28 18:28:21 +00002873 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002874 return call_method(self, OPSTR, &cache_str, "()"); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002875}
2876
Guido van Rossumdc91b992001-08-08 22:26:22 +00002877#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002878static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002879FUNCNAME(PyObject *self, ARG1TYPE arg1) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002880{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002881 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002882 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002883}
2884
Guido van Rossumdc91b992001-08-08 22:26:22 +00002885
2886#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002887static PyObject * \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002888FUNCNAME(PyObject *self, PyObject *other) \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002889{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002890 static PyObject *cache_str, *rcache_str; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002891 int do_other = self->ob_type != other->ob_type && \
2892 other->ob_type->tp_as_number != NULL && \
2893 other->ob_type->tp_as_number->SLOTNAME == TESTFUNC; \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002894 if (self->ob_type->tp_as_number != NULL && \
2895 self->ob_type->tp_as_number->SLOTNAME == TESTFUNC) { \
2896 PyObject *r; \
Guido van Rossum55f20992001-10-01 17:18:22 +00002897 if (do_other && \
2898 PyType_IsSubtype(other->ob_type, self->ob_type)) { \
2899 r = call_maybe( \
2900 other, ROPSTR, &rcache_str, "(O)", self); \
2901 if (r != Py_NotImplemented) \
2902 return r; \
2903 Py_DECREF(r); \
2904 do_other = 0; \
2905 } \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002906 r = call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002907 self, OPSTR, &cache_str, "(O)", other); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002908 if (r != Py_NotImplemented || \
2909 other->ob_type == self->ob_type) \
2910 return r; \
2911 Py_DECREF(r); \
2912 } \
Guido van Rossum55f20992001-10-01 17:18:22 +00002913 if (do_other) { \
Guido van Rossumf21c6be2001-09-14 17:51:50 +00002914 return call_maybe( \
Guido van Rossum717ce002001-09-14 16:58:08 +00002915 other, ROPSTR, &rcache_str, "(O)", self); \
Guido van Rossumdc91b992001-08-08 22:26:22 +00002916 } \
2917 Py_INCREF(Py_NotImplemented); \
2918 return Py_NotImplemented; \
2919}
2920
2921#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
2922 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
2923
2924#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
2925static PyObject * \
2926FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
2927{ \
Guido van Rossum2730b132001-08-28 18:22:14 +00002928 static PyObject *cache_str; \
Guido van Rossum717ce002001-09-14 16:58:08 +00002929 return call_method(self, OPSTR, &cache_str, \
2930 "(" ARGCODES ")", arg1, arg2); \
Tim Peters6d6c1a32001-08-02 04:15:00 +00002931}
2932
2933static int
2934slot_sq_length(PyObject *self)
2935{
Guido van Rossum2730b132001-08-28 18:22:14 +00002936 static PyObject *len_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00002937 PyObject *res = call_method(self, "__len__", &len_str, "()");
Guido van Rossum26111622001-10-01 16:42:49 +00002938 int len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002939
2940 if (res == NULL)
2941 return -1;
Guido van Rossum26111622001-10-01 16:42:49 +00002942 len = (int)PyInt_AsLong(res);
2943 Py_DECREF(res);
2944 return len;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002945}
2946
Guido van Rossumdc91b992001-08-08 22:26:22 +00002947SLOT1(slot_sq_concat, "__add__", PyObject *, "O")
2948SLOT1(slot_sq_repeat, "__mul__", int, "i")
Guido van Rossumf4593e02001-10-03 12:09:30 +00002949
2950/* Super-optimized version of slot_sq_item.
2951 Other slots could do the same... */
2952static PyObject *
2953slot_sq_item(PyObject *self, int i)
2954{
2955 static PyObject *getitem_str;
2956 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
2957 descrgetfunc f;
2958
2959 if (getitem_str == NULL) {
2960 getitem_str = PyString_InternFromString("__getitem__");
2961 if (getitem_str == NULL)
2962 return NULL;
2963 }
2964 func = _PyType_Lookup(self->ob_type, getitem_str);
2965 if (func != NULL) {
Guido van Rossumf4593e02001-10-03 12:09:30 +00002966 if ((f = func->ob_type->tp_descr_get) == NULL)
2967 Py_INCREF(func);
2968 else
2969 func = f(func, self, (PyObject *)(self->ob_type));
2970 ival = PyInt_FromLong(i);
2971 if (ival != NULL) {
2972 args = PyTuple_New(1);
2973 if (args != NULL) {
2974 PyTuple_SET_ITEM(args, 0, ival);
2975 retval = PyObject_Call(func, args, NULL);
2976 Py_XDECREF(args);
2977 Py_XDECREF(func);
2978 return retval;
2979 }
2980 }
2981 }
2982 else {
2983 PyErr_SetObject(PyExc_AttributeError, getitem_str);
2984 }
2985 Py_XDECREF(args);
2986 Py_XDECREF(ival);
2987 Py_XDECREF(func);
2988 return NULL;
2989}
2990
Guido van Rossumdc91b992001-08-08 22:26:22 +00002991SLOT2(slot_sq_slice, "__getslice__", int, int, "ii")
Tim Peters6d6c1a32001-08-02 04:15:00 +00002992
2993static int
2994slot_sq_ass_item(PyObject *self, int index, PyObject *value)
2995{
2996 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00002997 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00002998
2999 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003000 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003001 "(i)", index);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003002 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003003 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003004 "(iO)", index, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003005 if (res == NULL)
3006 return -1;
3007 Py_DECREF(res);
3008 return 0;
3009}
3010
3011static int
3012slot_sq_ass_slice(PyObject *self, int i, int j, PyObject *value)
3013{
3014 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003015 static PyObject *delslice_str, *setslice_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003016
3017 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003018 res = call_method(self, "__delslice__", &delslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003019 "(ii)", i, j);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003020 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003021 res = call_method(self, "__setslice__", &setslice_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003022 "(iiO)", i, j, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003023 if (res == NULL)
3024 return -1;
3025 Py_DECREF(res);
3026 return 0;
3027}
3028
3029static int
3030slot_sq_contains(PyObject *self, PyObject *value)
3031{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003032 PyObject *func, *res, *args;
Guido van Rossum60718732001-08-28 17:47:51 +00003033 static PyObject *contains_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003034
Guido van Rossum55f20992001-10-01 17:18:22 +00003035 func = lookup_maybe(self, "__contains__", &contains_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003036
3037 if (func != NULL) {
3038 args = Py_BuildValue("(O)", value);
3039 if (args == NULL)
3040 res = NULL;
3041 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003042 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003043 Py_DECREF(args);
3044 }
3045 Py_DECREF(func);
3046 if (res == NULL)
3047 return -1;
3048 return PyObject_IsTrue(res);
3049 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003050 else if (PyErr_Occurred())
3051 return -1;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003052 else {
Tim Peters16a77ad2001-09-08 04:00:12 +00003053 return _PySequence_IterSearch(self, value,
3054 PY_ITERSEARCH_CONTAINS);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003055 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003056}
3057
Guido van Rossumdc91b992001-08-08 22:26:22 +00003058SLOT1(slot_sq_inplace_concat, "__iadd__", PyObject *, "O")
3059SLOT1(slot_sq_inplace_repeat, "__imul__", int, "i")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003060
3061#define slot_mp_length slot_sq_length
3062
Guido van Rossumdc91b992001-08-08 22:26:22 +00003063SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003064
3065static int
3066slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
3067{
3068 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003069 static PyObject *delitem_str, *setitem_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003070
3071 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003072 res = call_method(self, "__delitem__", &delitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003073 "(O)", key);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003074 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003075 res = call_method(self, "__setitem__", &setitem_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003076 "(OO)", key, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003077 if (res == NULL)
3078 return -1;
3079 Py_DECREF(res);
3080 return 0;
3081}
3082
Guido van Rossumdc91b992001-08-08 22:26:22 +00003083SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
3084SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
3085SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
3086SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
3087SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
3088SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
3089
3090staticforward PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
3091
3092SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
3093 nb_power, "__pow__", "__rpow__")
3094
3095static PyObject *
3096slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
3097{
Guido van Rossum2730b132001-08-28 18:22:14 +00003098 static PyObject *pow_str;
3099
Guido van Rossumdc91b992001-08-08 22:26:22 +00003100 if (modulus == Py_None)
3101 return slot_nb_power_binary(self, other);
3102 /* Three-arg power doesn't use __rpow__ */
Guido van Rossum2730b132001-08-28 18:22:14 +00003103 return call_method(self, "__pow__", &pow_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003104 "(OO)", other, modulus);
Guido van Rossumdc91b992001-08-08 22:26:22 +00003105}
3106
3107SLOT0(slot_nb_negative, "__neg__")
3108SLOT0(slot_nb_positive, "__pos__")
3109SLOT0(slot_nb_absolute, "__abs__")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003110
3111static int
3112slot_nb_nonzero(PyObject *self)
3113{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003114 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003115 static PyObject *nonzero_str, *len_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003116
Guido van Rossum55f20992001-10-01 17:18:22 +00003117 func = lookup_maybe(self, "__nonzero__", &nonzero_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003118 if (func == NULL) {
Guido van Rossum55f20992001-10-01 17:18:22 +00003119 if (PyErr_Occurred())
Guido van Rossumb8f63662001-08-15 23:57:02 +00003120 return -1;
Guido van Rossum55f20992001-10-01 17:18:22 +00003121 func = lookup_maybe(self, "__len__", &len_str);
3122 if (func == NULL) {
3123 if (PyErr_Occurred())
3124 return -1;
3125 else
3126 return 1;
3127 }
Guido van Rossumb8f63662001-08-15 23:57:02 +00003128 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003129 res = PyObject_CallObject(func, NULL);
3130 Py_DECREF(func);
3131 if (res == NULL)
3132 return -1;
3133 return PyObject_IsTrue(res);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003134}
3135
Guido van Rossumdc91b992001-08-08 22:26:22 +00003136SLOT0(slot_nb_invert, "__invert__")
3137SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
3138SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
3139SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
3140SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
3141SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003142
3143static int
3144slot_nb_coerce(PyObject **a, PyObject **b)
3145{
3146 static PyObject *coerce_str;
3147 PyObject *self = *a, *other = *b;
3148
3149 if (self->ob_type->tp_as_number != NULL &&
3150 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3151 PyObject *r;
3152 r = call_maybe(
3153 self, "__coerce__", &coerce_str, "(O)", other);
3154 if (r == NULL)
3155 return -1;
3156 if (r == Py_NotImplemented) {
3157 Py_DECREF(r);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003158 }
Guido van Rossum55f20992001-10-01 17:18:22 +00003159 else {
3160 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3161 PyErr_SetString(PyExc_TypeError,
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003162 "__coerce__ didn't return a 2-tuple");
Guido van Rossum55f20992001-10-01 17:18:22 +00003163 Py_DECREF(r);
3164 return -1;
3165 }
3166 *a = PyTuple_GET_ITEM(r, 0);
3167 Py_INCREF(*a);
3168 *b = PyTuple_GET_ITEM(r, 1);
3169 Py_INCREF(*b);
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003170 Py_DECREF(r);
Guido van Rossum55f20992001-10-01 17:18:22 +00003171 return 0;
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003172 }
Guido van Rossum2ed6bf82001-09-27 20:30:07 +00003173 }
3174 if (other->ob_type->tp_as_number != NULL &&
3175 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
3176 PyObject *r;
3177 r = call_maybe(
3178 other, "__coerce__", &coerce_str, "(O)", self);
3179 if (r == NULL)
3180 return -1;
3181 if (r == Py_NotImplemented) {
3182 Py_DECREF(r);
3183 return 1;
3184 }
3185 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
3186 PyErr_SetString(PyExc_TypeError,
3187 "__coerce__ didn't return a 2-tuple");
3188 Py_DECREF(r);
3189 return -1;
3190 }
3191 *a = PyTuple_GET_ITEM(r, 1);
3192 Py_INCREF(*a);
3193 *b = PyTuple_GET_ITEM(r, 0);
3194 Py_INCREF(*b);
3195 Py_DECREF(r);
3196 return 0;
3197 }
3198 return 1;
3199}
3200
Guido van Rossumdc91b992001-08-08 22:26:22 +00003201SLOT0(slot_nb_int, "__int__")
3202SLOT0(slot_nb_long, "__long__")
3203SLOT0(slot_nb_float, "__float__")
3204SLOT0(slot_nb_oct, "__oct__")
3205SLOT0(slot_nb_hex, "__hex__")
3206SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
3207SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
3208SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
3209SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
3210SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
3211SLOT2(slot_nb_inplace_power, "__ipow__", PyObject *, PyObject *, "OO")
3212SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
3213SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
3214SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
3215SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
3216SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
3217SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
3218 "__floordiv__", "__rfloordiv__")
3219SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
3220SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
3221SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
Tim Peters6d6c1a32001-08-02 04:15:00 +00003222
3223static int
Guido van Rossumb8f63662001-08-15 23:57:02 +00003224half_compare(PyObject *self, PyObject *other)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003225{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003226 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003227 static PyObject *cmp_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003228 int c;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003229
Guido van Rossum60718732001-08-28 17:47:51 +00003230 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003231 if (func == NULL) {
3232 PyErr_Clear();
3233 }
3234 else {
3235 args = Py_BuildValue("(O)", other);
3236 if (args == NULL)
3237 res = NULL;
3238 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003239 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003240 Py_DECREF(args);
3241 }
3242 if (res != Py_NotImplemented) {
3243 if (res == NULL)
3244 return -2;
3245 c = PyInt_AsLong(res);
3246 Py_DECREF(res);
3247 if (c == -1 && PyErr_Occurred())
3248 return -2;
3249 return (c < 0) ? -1 : (c > 0) ? 1 : 0;
3250 }
3251 Py_DECREF(res);
3252 }
3253 return 2;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003254}
3255
Guido van Rossumab3b0342001-09-18 20:38:53 +00003256/* This slot is published for the benefit of try_3way_compare in object.c */
3257int
3258_PyObject_SlotCompare(PyObject *self, PyObject *other)
Guido van Rossumb8f63662001-08-15 23:57:02 +00003259{
3260 int c;
3261
Guido van Rossumab3b0342001-09-18 20:38:53 +00003262 if (self->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003263 c = half_compare(self, other);
3264 if (c <= 1)
3265 return c;
3266 }
Guido van Rossumab3b0342001-09-18 20:38:53 +00003267 if (other->ob_type->tp_compare == _PyObject_SlotCompare) {
Guido van Rossumb8f63662001-08-15 23:57:02 +00003268 c = half_compare(other, self);
3269 if (c < -1)
3270 return -2;
3271 if (c <= 1)
3272 return -c;
3273 }
3274 return (void *)self < (void *)other ? -1 :
3275 (void *)self > (void *)other ? 1 : 0;
3276}
3277
3278static PyObject *
3279slot_tp_repr(PyObject *self)
3280{
3281 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003282 static PyObject *repr_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003283
Guido van Rossum60718732001-08-28 17:47:51 +00003284 func = lookup_method(self, "__repr__", &repr_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003285 if (func != NULL) {
3286 res = PyEval_CallObject(func, NULL);
3287 Py_DECREF(func);
3288 return res;
3289 }
Barry Warsaw7ce36942001-08-24 18:34:26 +00003290 PyErr_Clear();
3291 return PyString_FromFormat("<%s object at %p>",
3292 self->ob_type->tp_name, self);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003293}
3294
3295static PyObject *
3296slot_tp_str(PyObject *self)
3297{
3298 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003299 static PyObject *str_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003300
Guido van Rossum60718732001-08-28 17:47:51 +00003301 func = lookup_method(self, "__str__", &str_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003302 if (func != NULL) {
3303 res = PyEval_CallObject(func, NULL);
3304 Py_DECREF(func);
3305 return res;
3306 }
3307 else {
3308 PyErr_Clear();
3309 return slot_tp_repr(self);
3310 }
3311}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003312
3313static long
3314slot_tp_hash(PyObject *self)
3315{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003316 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003317 static PyObject *hash_str, *eq_str, *cmp_str;
3318
Tim Peters6d6c1a32001-08-02 04:15:00 +00003319 long h;
3320
Guido van Rossum60718732001-08-28 17:47:51 +00003321 func = lookup_method(self, "__hash__", &hash_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003322
3323 if (func != NULL) {
3324 res = PyEval_CallObject(func, NULL);
3325 Py_DECREF(func);
3326 if (res == NULL)
3327 return -1;
3328 h = PyInt_AsLong(res);
3329 }
3330 else {
3331 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003332 func = lookup_method(self, "__eq__", &eq_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003333 if (func == NULL) {
3334 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003335 func = lookup_method(self, "__cmp__", &cmp_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003336 }
3337 if (func != NULL) {
3338 Py_DECREF(func);
3339 PyErr_SetString(PyExc_TypeError, "unhashable type");
3340 return -1;
3341 }
3342 PyErr_Clear();
3343 h = _Py_HashPointer((void *)self);
3344 }
Tim Peters6d6c1a32001-08-02 04:15:00 +00003345 if (h == -1 && !PyErr_Occurred())
3346 h = -2;
3347 return h;
3348}
3349
3350static PyObject *
3351slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
3352{
Guido van Rossum60718732001-08-28 17:47:51 +00003353 static PyObject *call_str;
3354 PyObject *meth = lookup_method(self, "__call__", &call_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003355 PyObject *res;
3356
3357 if (meth == NULL)
3358 return NULL;
3359 res = PyObject_Call(meth, args, kwds);
3360 Py_DECREF(meth);
3361 return res;
3362}
3363
Guido van Rossum14a6f832001-10-17 13:59:09 +00003364/* There are two slot dispatch functions for tp_getattro.
3365
3366 - slot_tp_getattro() is used when __getattribute__ is overridden
3367 but no __getattr__ hook is present;
3368
3369 - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
3370
Guido van Rossumc334df52002-04-04 23:44:47 +00003371 The code in update_one_slot() always installs slot_tp_getattr_hook(); this
3372 detects the absence of __getattr__ and then installs the simpler slot if
3373 necessary. */
Guido van Rossum14a6f832001-10-17 13:59:09 +00003374
Tim Peters6d6c1a32001-08-02 04:15:00 +00003375static PyObject *
3376slot_tp_getattro(PyObject *self, PyObject *name)
3377{
Guido van Rossum14a6f832001-10-17 13:59:09 +00003378 static PyObject *getattribute_str = NULL;
3379 return call_method(self, "__getattribute__", &getattribute_str,
3380 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003381}
3382
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003383static PyObject *
3384slot_tp_getattr_hook(PyObject *self, PyObject *name)
3385{
3386 PyTypeObject *tp = self->ob_type;
3387 PyObject *getattr, *getattribute, *res;
3388 static PyObject *getattribute_str = NULL;
3389 static PyObject *getattr_str = NULL;
3390
3391 if (getattr_str == NULL) {
3392 getattr_str = PyString_InternFromString("__getattr__");
3393 if (getattr_str == NULL)
3394 return NULL;
3395 }
3396 if (getattribute_str == NULL) {
3397 getattribute_str =
3398 PyString_InternFromString("__getattribute__");
3399 if (getattribute_str == NULL)
3400 return NULL;
3401 }
3402 getattr = _PyType_Lookup(tp, getattr_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003403 if (getattr == NULL) {
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003404 /* No __getattr__ hook: use a simpler dispatcher */
3405 tp->tp_getattro = slot_tp_getattro;
3406 return slot_tp_getattro(self, name);
3407 }
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003408 getattribute = _PyType_Lookup(tp, getattribute_str);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003409 if (getattribute == NULL ||
3410 (getattribute->ob_type == &PyWrapperDescr_Type &&
3411 ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
3412 (void *)PyObject_GenericGetAttr))
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003413 res = PyObject_GenericGetAttr(self, name);
3414 else
3415 res = PyObject_CallFunction(getattribute, "OO", self, name);
Guido van Rossum14a6f832001-10-17 13:59:09 +00003416 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
Guido van Rossum19c1cd52001-09-21 21:24:49 +00003417 PyErr_Clear();
3418 res = PyObject_CallFunction(getattr, "OO", self, name);
3419 }
3420 return res;
3421}
3422
Tim Peters6d6c1a32001-08-02 04:15:00 +00003423static int
3424slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
3425{
3426 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003427 static PyObject *delattr_str, *setattr_str;
Tim Peters6d6c1a32001-08-02 04:15:00 +00003428
3429 if (value == NULL)
Guido van Rossum2730b132001-08-28 18:22:14 +00003430 res = call_method(self, "__delattr__", &delattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003431 "(O)", name);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003432 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003433 res = call_method(self, "__setattr__", &setattr_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003434 "(OO)", name, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003435 if (res == NULL)
3436 return -1;
3437 Py_DECREF(res);
3438 return 0;
3439}
3440
3441/* Map rich comparison operators to their __xx__ namesakes */
3442static char *name_op[] = {
3443 "__lt__",
3444 "__le__",
3445 "__eq__",
3446 "__ne__",
3447 "__gt__",
3448 "__ge__",
3449};
3450
3451static PyObject *
Guido van Rossumb8f63662001-08-15 23:57:02 +00003452half_richcompare(PyObject *self, PyObject *other, int op)
Tim Peters6d6c1a32001-08-02 04:15:00 +00003453{
Guido van Rossumb8f63662001-08-15 23:57:02 +00003454 PyObject *func, *args, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003455 static PyObject *op_str[6];
Tim Peters6d6c1a32001-08-02 04:15:00 +00003456
Guido van Rossum60718732001-08-28 17:47:51 +00003457 func = lookup_method(self, name_op[op], &op_str[op]);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003458 if (func == NULL) {
3459 PyErr_Clear();
3460 Py_INCREF(Py_NotImplemented);
3461 return Py_NotImplemented;
3462 }
3463 args = Py_BuildValue("(O)", other);
3464 if (args == NULL)
3465 res = NULL;
3466 else {
Guido van Rossum717ce002001-09-14 16:58:08 +00003467 res = PyObject_Call(func, args, NULL);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003468 Py_DECREF(args);
3469 }
3470 Py_DECREF(func);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003471 return res;
3472}
3473
Guido van Rossumb8f63662001-08-15 23:57:02 +00003474/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
3475static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
3476
3477static PyObject *
3478slot_tp_richcompare(PyObject *self, PyObject *other, int op)
3479{
3480 PyObject *res;
3481
3482 if (self->ob_type->tp_richcompare == slot_tp_richcompare) {
3483 res = half_richcompare(self, other, op);
3484 if (res != Py_NotImplemented)
3485 return res;
3486 Py_DECREF(res);
3487 }
3488 if (other->ob_type->tp_richcompare == slot_tp_richcompare) {
3489 res = half_richcompare(other, self, swapped_op[op]);
3490 if (res != Py_NotImplemented) {
3491 return res;
3492 }
3493 Py_DECREF(res);
3494 }
3495 Py_INCREF(Py_NotImplemented);
3496 return Py_NotImplemented;
3497}
3498
3499static PyObject *
3500slot_tp_iter(PyObject *self)
3501{
3502 PyObject *func, *res;
Guido van Rossum60718732001-08-28 17:47:51 +00003503 static PyObject *iter_str, *getitem_str;
Guido van Rossumb8f63662001-08-15 23:57:02 +00003504
Guido van Rossum60718732001-08-28 17:47:51 +00003505 func = lookup_method(self, "__iter__", &iter_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003506 if (func != NULL) {
3507 res = PyObject_CallObject(func, NULL);
3508 Py_DECREF(func);
3509 return res;
3510 }
3511 PyErr_Clear();
Guido van Rossum60718732001-08-28 17:47:51 +00003512 func = lookup_method(self, "__getitem__", &getitem_str);
Guido van Rossumb8f63662001-08-15 23:57:02 +00003513 if (func == NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00003514 PyErr_SetString(PyExc_TypeError,
3515 "iteration over non-sequence");
Guido van Rossumb8f63662001-08-15 23:57:02 +00003516 return NULL;
3517 }
3518 Py_DECREF(func);
3519 return PySeqIter_New(self);
3520}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003521
3522static PyObject *
3523slot_tp_iternext(PyObject *self)
3524{
Guido van Rossum2730b132001-08-28 18:22:14 +00003525 static PyObject *next_str;
Guido van Rossum717ce002001-09-14 16:58:08 +00003526 return call_method(self, "next", &next_str, "()");
Tim Peters6d6c1a32001-08-02 04:15:00 +00003527}
3528
Guido van Rossum1a493502001-08-17 16:47:50 +00003529static PyObject *
3530slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
3531{
3532 PyTypeObject *tp = self->ob_type;
3533 PyObject *get;
3534 static PyObject *get_str = NULL;
3535
3536 if (get_str == NULL) {
3537 get_str = PyString_InternFromString("__get__");
3538 if (get_str == NULL)
3539 return NULL;
3540 }
3541 get = _PyType_Lookup(tp, get_str);
3542 if (get == NULL) {
3543 /* Avoid further slowdowns */
3544 if (tp->tp_descr_get == slot_tp_descr_get)
3545 tp->tp_descr_get = NULL;
3546 Py_INCREF(self);
3547 return self;
3548 }
Guido van Rossum2c252392001-08-24 10:13:31 +00003549 if (obj == NULL)
3550 obj = Py_None;
3551 if (type == NULL)
3552 type = Py_None;
Guido van Rossum1a493502001-08-17 16:47:50 +00003553 return PyObject_CallFunction(get, "OOO", self, obj, type);
3554}
Tim Peters6d6c1a32001-08-02 04:15:00 +00003555
3556static int
3557slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
3558{
Guido van Rossum2c252392001-08-24 10:13:31 +00003559 PyObject *res;
Guido van Rossum2730b132001-08-28 18:22:14 +00003560 static PyObject *del_str, *set_str;
Guido van Rossum2c252392001-08-24 10:13:31 +00003561
3562 if (value == NULL)
Guido van Rossum1d5b3f22001-12-03 00:08:33 +00003563 res = call_method(self, "__delete__", &del_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003564 "(O)", target);
Guido van Rossum2c252392001-08-24 10:13:31 +00003565 else
Guido van Rossum2730b132001-08-28 18:22:14 +00003566 res = call_method(self, "__set__", &set_str,
Guido van Rossum717ce002001-09-14 16:58:08 +00003567 "(OO)", target, value);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003568 if (res == NULL)
3569 return -1;
3570 Py_DECREF(res);
3571 return 0;
3572}
3573
3574static int
3575slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
3576{
Guido van Rossum60718732001-08-28 17:47:51 +00003577 static PyObject *init_str;
3578 PyObject *meth = lookup_method(self, "__init__", &init_str);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003579 PyObject *res;
3580
3581 if (meth == NULL)
3582 return -1;
3583 res = PyObject_Call(meth, args, kwds);
3584 Py_DECREF(meth);
3585 if (res == NULL)
3586 return -1;
3587 Py_DECREF(res);
3588 return 0;
3589}
3590
3591static PyObject *
3592slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
3593{
3594 PyObject *func = PyObject_GetAttrString((PyObject *)type, "__new__");
3595 PyObject *newargs, *x;
3596 int i, n;
3597
3598 if (func == NULL)
3599 return NULL;
3600 assert(PyTuple_Check(args));
3601 n = PyTuple_GET_SIZE(args);
3602 newargs = PyTuple_New(n+1);
3603 if (newargs == NULL)
3604 return NULL;
3605 Py_INCREF(type);
3606 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
3607 for (i = 0; i < n; i++) {
3608 x = PyTuple_GET_ITEM(args, i);
3609 Py_INCREF(x);
3610 PyTuple_SET_ITEM(newargs, i+1, x);
3611 }
3612 x = PyObject_Call(func, newargs, kwds);
Guido van Rossum25d18072001-10-01 15:55:28 +00003613 Py_DECREF(newargs);
Tim Peters6d6c1a32001-08-02 04:15:00 +00003614 Py_DECREF(func);
3615 return x;
3616}
3617
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003618
3619/* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper
3620 functions. The offsets here are relative to the 'etype' structure, which
3621 incorporates the additional structures used for numbers, sequences and
3622 mappings. Note that multiple names may map to the same slot (e.g. __eq__,
3623 __ne__ etc. all map to tp_richcompare) and one name may map to multiple
Guido van Rossumc334df52002-04-04 23:44:47 +00003624 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is
3625 terminated with an all-zero entry. (This table is further initialized and
3626 sorted in init_slotdefs() below.) */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003627
Guido van Rossum6d204072001-10-21 00:44:31 +00003628typedef struct wrapperbase slotdef;
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003629
3630#undef TPSLOT
Guido van Rossumc8e56452001-10-22 00:43:43 +00003631#undef FLSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003632#undef ETSLOT
3633#undef SQSLOT
3634#undef MPSLOT
3635#undef NBSLOT
Guido van Rossum6d204072001-10-21 00:44:31 +00003636#undef UNSLOT
3637#undef IBSLOT
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003638#undef BINSLOT
3639#undef RBINSLOT
3640
Guido van Rossum6d204072001-10-21 00:44:31 +00003641#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3642 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
Guido van Rossumc8e56452001-10-22 00:43:43 +00003643#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
3644 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
3645 DOC, FLAGS}
Guido van Rossum6d204072001-10-21 00:44:31 +00003646#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3647 {NAME, offsetof(etype, SLOT), (void *)(FUNCTION), WRAPPER, DOC}
3648#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3649 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
3650#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3651 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
3652#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3653 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
3654#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3655 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3656 "x." NAME "() <==> " DOC)
3657#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
3658 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
3659 "x." NAME "(y) <==> x" DOC "y")
3660#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
3661 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
3662 "x." NAME "(y) <==> x" DOC "y")
3663#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
3664 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
3665 "x." NAME "(y) <==> y" DOC "x")
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003666
3667static slotdef slotdefs[] = {
Guido van Rossum6d204072001-10-21 00:44:31 +00003668 SQSLOT("__len__", sq_length, slot_sq_length, wrap_inquiry,
3669 "x.__len__() <==> len(x)"),
3670 SQSLOT("__add__", sq_concat, slot_sq_concat, wrap_binaryfunc,
3671 "x.__add__(y) <==> x+y"),
3672 SQSLOT("__mul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3673 "x.__mul__(n) <==> x*n"),
3674 SQSLOT("__rmul__", sq_repeat, slot_sq_repeat, wrap_intargfunc,
3675 "x.__rmul__(n) <==> n*x"),
3676 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
3677 "x.__getitem__(y) <==> x[y]"),
3678 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_intintargfunc,
3679 "x.__getslice__(i, j) <==> x[i:j]"),
3680 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
3681 "x.__setitem__(i, y) <==> x[i]=y"),
3682 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
3683 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003684 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
Guido van Rossum6d204072001-10-21 00:44:31 +00003685 wrap_intintobjargproc,
3686 "x.__setslice__(i, j, y) <==> x[i:j]=y"),
3687 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
3688 "x.__delslice__(i, j) <==> del x[i:j]"),
3689 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
3690 "x.__contains__(y) <==> y in x"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003691 SQSLOT("__iadd__", sq_inplace_concat, slot_sq_inplace_concat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003692 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003693 SQSLOT("__imul__", sq_inplace_repeat, slot_sq_inplace_repeat,
Guido van Rossum6d204072001-10-21 00:44:31 +00003694 wrap_intargfunc, "x.__imul__(y) <==> x*=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003695
Guido van Rossum6d204072001-10-21 00:44:31 +00003696 MPSLOT("__len__", mp_length, slot_mp_length, wrap_inquiry,
3697 "x.__len__() <==> len(x)"),
Guido van Rossumfd38f8e2001-10-09 20:17:57 +00003698 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003699 wrap_binaryfunc,
3700 "x.__getitem__(y) <==> x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003701 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003702 wrap_objobjargproc,
3703 "x.__setitem__(i, y) <==> x[i]=y"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003704 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
Guido van Rossum6d204072001-10-21 00:44:31 +00003705 wrap_delitem,
3706 "x.__delitem__(y) <==> del x[y]"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003707
Guido van Rossum6d204072001-10-21 00:44:31 +00003708 BINSLOT("__add__", nb_add, slot_nb_add,
3709 "+"),
3710 RBINSLOT("__radd__", nb_add, slot_nb_add,
3711 "+"),
3712 BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
3713 "-"),
3714 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
3715 "-"),
3716 BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
3717 "*"),
3718 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
3719 "*"),
3720 BINSLOT("__div__", nb_divide, slot_nb_divide,
3721 "/"),
3722 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
3723 "/"),
3724 BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
3725 "%"),
3726 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
3727 "%"),
3728 BINSLOT("__divmod__", nb_divmod, slot_nb_divmod,
3729 "divmod(x, y)"),
3730 RBINSLOT("__rdivmod__", nb_divmod, slot_nb_divmod,
3731 "divmod(y, x)"),
3732 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
3733 "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
3734 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
3735 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
3736 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
3737 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
3738 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
3739 "abs(x)"),
Guido van Rossumdfce3bf2002-03-10 14:11:16 +00003740 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquiry,
Guido van Rossum6d204072001-10-21 00:44:31 +00003741 "x != 0"),
3742 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
3743 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
3744 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
3745 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
3746 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
3747 BINSLOT("__and__", nb_and, slot_nb_and, "&"),
3748 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
3749 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
3750 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
3751 BINSLOT("__or__", nb_or, slot_nb_or, "|"),
3752 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
3753 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
3754 "x.__coerce__(y) <==> coerce(x, y)"),
3755 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
3756 "int(x)"),
3757 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
3758 "long(x)"),
3759 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
3760 "float(x)"),
3761 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
3762 "oct(x)"),
3763 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
3764 "hex(x)"),
3765 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
3766 wrap_binaryfunc, "+"),
3767 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
3768 wrap_binaryfunc, "-"),
3769 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
3770 wrap_binaryfunc, "*"),
3771 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
3772 wrap_binaryfunc, "/"),
3773 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
3774 wrap_binaryfunc, "%"),
3775 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
3776 wrap_ternaryfunc, "**"),
3777 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
3778 wrap_binaryfunc, "<<"),
3779 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
3780 wrap_binaryfunc, ">>"),
3781 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
3782 wrap_binaryfunc, "&"),
3783 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
3784 wrap_binaryfunc, "^"),
3785 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
3786 wrap_binaryfunc, "|"),
3787 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3788 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
3789 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
3790 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
3791 IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
3792 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"),
3793 IBSLOT("__itruediv__", nb_inplace_true_divide,
3794 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003795
Guido van Rossum6d204072001-10-21 00:44:31 +00003796 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
3797 "x.__str__() <==> str(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003798 TPSLOT("__str__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003799 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
3800 "x.__repr__() <==> repr(x)"),
Guido van Rossumafe7a942001-10-29 14:33:44 +00003801 TPSLOT("__repr__", tp_print, NULL, NULL, ""),
Guido van Rossum6d204072001-10-21 00:44:31 +00003802 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
3803 "x.__cmp__(y) <==> cmp(x,y)"),
3804 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
3805 "x.__hash__() <==> hash(x)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003806 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
3807 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
Guido van Rossumd396b9c2001-10-13 20:02:41 +00003808 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
Guido van Rossum6d204072001-10-21 00:44:31 +00003809 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
3810 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
3811 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
3812 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
3813 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
3814 "x.__setattr__('name', value) <==> x.name = value"),
3815 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
3816 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
3817 "x.__delattr__('name') <==> del x.name"),
3818 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
3819 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
3820 "x.__lt__(y) <==> x<y"),
3821 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
3822 "x.__le__(y) <==> x<=y"),
3823 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
3824 "x.__eq__(y) <==> x==y"),
3825 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
3826 "x.__ne__(y) <==> x!=y"),
3827 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
3828 "x.__gt__(y) <==> x>y"),
3829 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
3830 "x.__ge__(y) <==> x>=y"),
3831 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
3832 "x.__iter__() <==> iter(x)"),
3833 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
3834 "x.next() -> the next value, or raise StopIteration"),
3835 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
3836 "descr.__get__(obj[, type]) -> value"),
3837 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
3838 "descr.__set__(obj, value)"),
Guido van Rossumc8e56452001-10-22 00:43:43 +00003839 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
Guido van Rossum6d204072001-10-21 00:44:31 +00003840 "x.__init__(...) initializes x; "
Guido van Rossumc8e56452001-10-22 00:43:43 +00003841 "see x.__class__.__doc__ for signature",
3842 PyWrapperFlag_KEYWORDS),
3843 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003844 {NULL}
3845};
3846
Guido van Rossumc334df52002-04-04 23:44:47 +00003847/* Given a type pointer and an offset gotten from a slotdef entry, return a
3848 pointer to the actual slot. This is not quite the same as simply adding
3849 the offset to the type pointer, since it takes care to indirect through the
3850 proper indirection pointer (as_buffer, etc.); it returns NULL if the
3851 indirection pointer is NULL. */
Guido van Rossum7b9144b2001-10-09 19:39:46 +00003852static void **
3853slotptr(PyTypeObject *type, int offset)
3854{
3855 char *ptr;
3856
3857 assert(offset >= 0);
3858 assert(offset < offsetof(etype, as_buffer));
3859 if (offset >= offsetof(etype, as_mapping)) {
3860 ptr = (void *)type->tp_as_mapping;
3861 offset -= offsetof(etype, as_mapping);
3862 }
3863 else if (offset >= offsetof(etype, as_sequence)) {
3864 ptr = (void *)type->tp_as_sequence;
3865 offset -= offsetof(etype, as_sequence);
3866 }
3867 else if (offset >= offsetof(etype, as_number)) {
3868 ptr = (void *)type->tp_as_number;
3869 offset -= offsetof(etype, as_number);
3870 }
3871 else {
3872 ptr = (void *)type;
3873 }
3874 if (ptr != NULL)
3875 ptr += offset;
3876 return (void **)ptr;
3877}
Guido van Rossumf040ede2001-08-07 16:40:56 +00003878
Guido van Rossumc334df52002-04-04 23:44:47 +00003879/* Length of array of slotdef pointers used to store slots with the
3880 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with
3881 the same __name__, for any __name__. Since that's a static property, it is
3882 appropriate to declare fixed-size arrays for this. */
3883#define MAX_EQUIV 10
3884
3885/* Return a slot pointer for a given name, but ONLY if the attribute has
3886 exactly one slot function. The name must be an interned string. */
3887static void **
3888resolve_slotdups(PyTypeObject *type, PyObject *name)
3889{
3890 /* XXX Maybe this could be optimized more -- but is it worth it? */
3891
3892 /* pname and ptrs act as a little cache */
3893 static PyObject *pname;
3894 static slotdef *ptrs[MAX_EQUIV];
3895 slotdef *p, **pp;
3896 void **res, **ptr;
3897
3898 if (pname != name) {
3899 /* Collect all slotdefs that match name into ptrs. */
3900 pname = name;
3901 pp = ptrs;
3902 for (p = slotdefs; p->name_strobj; p++) {
3903 if (p->name_strobj == name)
3904 *pp++ = p;
3905 }
3906 *pp = NULL;
3907 }
3908
3909 /* Look in all matching slots of the type; if exactly one of these has
3910 a filled-in slot, return its value. Otherwise return NULL. */
3911 res = NULL;
3912 for (pp = ptrs; *pp; pp++) {
3913 ptr = slotptr(type, (*pp)->offset);
3914 if (ptr == NULL || *ptr == NULL)
3915 continue;
3916 if (res != NULL)
3917 return NULL;
3918 res = ptr;
3919 }
3920 return res;
3921}
3922
3923/* Common code for update_these_slots() and fixup_slot_dispatchers(). This
3924 does some incredibly complex thinking and then sticks something into the
3925 slot. (It sees if the adjacent slotdefs for the same slot have conflicting
3926 interests, and then stores a generic wrapper or a specific function into
3927 the slot.) Return a pointer to the next slotdef with a different offset,
3928 because that's convenient for fixup_slot_dispatchers(). */
3929static slotdef *
3930update_one_slot(PyTypeObject *type, slotdef *p)
3931{
3932 PyObject *descr;
3933 PyWrapperDescrObject *d;
3934 void *generic = NULL, *specific = NULL;
3935 int use_generic = 0;
3936 int offset = p->offset;
3937 void **ptr = slotptr(type, offset);
3938
3939 if (ptr == NULL) {
3940 do {
3941 ++p;
3942 } while (p->offset == offset);
3943 return p;
3944 }
3945 do {
3946 descr = _PyType_Lookup(type, p->name_strobj);
3947 if (descr == NULL)
3948 continue;
3949 if (descr->ob_type == &PyWrapperDescr_Type) {
3950 void **tptr = resolve_slotdups(type, p->name_strobj);
3951 if (tptr == NULL || tptr == ptr)
3952 generic = p->function;
3953 d = (PyWrapperDescrObject *)descr;
3954 if (d->d_base->wrapper == p->wrapper &&
3955 PyType_IsSubtype(type, d->d_type))
3956 {
3957 if (specific == NULL ||
3958 specific == d->d_wrapped)
3959 specific = d->d_wrapped;
3960 else
3961 use_generic = 1;
3962 }
3963 }
3964 else {
3965 use_generic = 1;
3966 generic = p->function;
3967 }
3968 } while ((++p)->offset == offset);
3969 if (specific && !use_generic)
3970 *ptr = specific;
3971 else
3972 *ptr = generic;
3973 return p;
3974}
3975
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003976staticforward int recurse_down_subclasses(PyTypeObject *type,
3977 slotdef **pp, PyObject *name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003978
Guido van Rossumc334df52002-04-04 23:44:47 +00003979/* In the type, update the slots whose slotdefs are gathered in the pp0 array,
3980 and then do the same for all this type's subtypes. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003981static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003982update_these_slots(PyTypeObject *type, slotdef **pp0, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003983{
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003984 slotdef **pp;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003985
Guido van Rossumc334df52002-04-04 23:44:47 +00003986 for (pp = pp0; *pp; pp++)
3987 update_one_slot(type, *pp);
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003988 return recurse_down_subclasses(type, pp0, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003989}
3990
Guido van Rossumc334df52002-04-04 23:44:47 +00003991/* Update the slots whose slotdefs are gathered in the pp array in all (direct
3992 or indirect) subclasses of type. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003993static int
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003994recurse_down_subclasses(PyTypeObject *type, slotdef **pp, PyObject *name)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003995{
3996 PyTypeObject *subclass;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00003997 PyObject *ref, *subclasses, *dict;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00003998 int i, n;
3999
4000 subclasses = type->tp_subclasses;
4001 if (subclasses == NULL)
4002 return 0;
4003 assert(PyList_Check(subclasses));
4004 n = PyList_GET_SIZE(subclasses);
4005 for (i = 0; i < n; i++) {
4006 ref = PyList_GET_ITEM(subclasses, i);
4007 assert(PyWeakref_CheckRef(ref));
4008 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
4009 if (subclass == NULL)
4010 continue;
4011 assert(PyType_Check(subclass));
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004012 /* Avoid recursing down into unaffected classes */
4013 dict = subclass->tp_dict;
4014 if (dict != NULL && PyDict_Check(dict) &&
4015 PyDict_GetItem(dict, name) != NULL)
4016 continue;
4017 if (update_these_slots(subclass, pp, name) < 0)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004018 return -1;
4019 }
4020 return 0;
4021}
4022
Guido van Rossumc334df52002-04-04 23:44:47 +00004023/* Comparison function for qsort() to compare slotdefs by their offset, and
4024 for equal offset by their address (to force a stable sort). */
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004025static int
4026slotdef_cmp(const void *aa, const void *bb)
4027{
4028 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb;
4029 int c = a->offset - b->offset;
4030 if (c != 0)
4031 return c;
4032 else
4033 return a - b;
4034}
4035
Guido van Rossumc334df52002-04-04 23:44:47 +00004036/* Initialize the slotdefs table by adding interned string objects for the
4037 names and sorting the entries. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004038static void
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004039init_slotdefs(void)
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004040{
4041 slotdef *p;
4042 static int initialized = 0;
4043
4044 if (initialized)
4045 return;
4046 for (p = slotdefs; p->name; p++) {
4047 p->name_strobj = PyString_InternFromString(p->name);
4048 if (!p->name_strobj)
Guido van Rossumc334df52002-04-04 23:44:47 +00004049 Py_FatalError("Out of memory interning slotdef names");
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004050 }
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004051 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef),
4052 slotdef_cmp);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004053 initialized = 1;
4054}
4055
Guido van Rossumc334df52002-04-04 23:44:47 +00004056/* Update the slots after assignment to a class (type) attribute. */
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004057static int
4058update_slot(PyTypeObject *type, PyObject *name)
4059{
Guido van Rossumc334df52002-04-04 23:44:47 +00004060 slotdef *ptrs[MAX_EQUIV];
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004061 slotdef *p;
4062 slotdef **pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004063 int offset;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004064
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004065 init_slotdefs();
4066 pp = ptrs;
4067 for (p = slotdefs; p->name; p++) {
4068 /* XXX assume name is interned! */
4069 if (p->name_strobj == name)
4070 *pp++ = p;
4071 }
4072 *pp = NULL;
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004073 for (pp = ptrs; *pp; pp++) {
4074 p = *pp;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004075 offset = p->offset;
4076 while (p > slotdefs && (p-1)->offset == offset)
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004077 --p;
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004078 *pp = p;
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004079 }
Guido van Rossumc334df52002-04-04 23:44:47 +00004080 if (ptrs[0] == NULL)
4081 return 0; /* Not an attribute that affects any slots */
Guido van Rossumb85a8b72001-10-16 17:00:48 +00004082 return update_these_slots(type, ptrs, name);
Guido van Rossum875eeaa2001-10-11 18:33:53 +00004083}
4084
Guido van Rossumc334df52002-04-04 23:44:47 +00004085/* Store the proper functions in the slot dispatches at class (type)
4086 definition time, based upon which operations the class overrides in its
4087 dict. */
Tim Peters6d6c1a32001-08-02 04:15:00 +00004088static void
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004089fixup_slot_dispatchers(PyTypeObject *type)
Tim Peters6d6c1a32001-08-02 04:15:00 +00004090{
Guido van Rossum7b9144b2001-10-09 19:39:46 +00004091 slotdef *p;
Tim Peters6d6c1a32001-08-02 04:15:00 +00004092
Guido van Rossumd396b9c2001-10-13 20:02:41 +00004093 init_slotdefs();
Guido van Rossumc334df52002-04-04 23:44:47 +00004094 for (p = slotdefs; p->name; )
4095 p = update_one_slot(type, p);
Tim Peters6d6c1a32001-08-02 04:15:00 +00004096}
Guido van Rossum705f0f52001-08-24 16:47:00 +00004097
Guido van Rossum6d204072001-10-21 00:44:31 +00004098/* This function is called by PyType_Ready() to populate the type's
4099 dictionary with method descriptors for function slots. For each
4100 function slot (like tp_repr) that's defined in the type, one or
4101 more corresponding descriptors are added in the type's tp_dict
4102 dictionary under the appropriate name (like __repr__). Some
4103 function slots cause more than one descriptor to be added (for
4104 example, the nb_add slot adds both __add__ and __radd__
4105 descriptors) and some function slots compete for the same
4106 descriptor (for example both sq_item and mp_subscript generate a
4107 __getitem__ descriptor). This only adds new descriptors and
4108 doesn't overwrite entries in tp_dict that were previously
4109 defined. The descriptors contain a reference to the C function
4110 they must call, so that it's safe if they are copied into a
4111 subtype's __dict__ and the subtype has a different C function in
4112 its slot -- calling the method defined by the descriptor will call
4113 the C function that was used to create it, rather than the C
4114 function present in the slot when it is called. (This is important
4115 because a subtype may have a C function in the slot that calls the
4116 method from the dictionary, and we want to avoid infinite recursion
4117 here.) */
4118
4119static int
4120add_operators(PyTypeObject *type)
4121{
4122 PyObject *dict = type->tp_dict;
4123 slotdef *p;
4124 PyObject *descr;
4125 void **ptr;
4126
4127 init_slotdefs();
4128 for (p = slotdefs; p->name; p++) {
4129 if (p->wrapper == NULL)
4130 continue;
4131 ptr = slotptr(type, p->offset);
4132 if (!ptr || !*ptr)
4133 continue;
4134 if (PyDict_GetItem(dict, p->name_strobj))
4135 continue;
4136 descr = PyDescr_NewWrapper(type, p, *ptr);
4137 if (descr == NULL)
4138 return -1;
4139 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0)
4140 return -1;
4141 Py_DECREF(descr);
4142 }
4143 if (type->tp_new != NULL) {
4144 if (add_tp_new_wrapper(type) < 0)
4145 return -1;
4146 }
4147 return 0;
4148}
4149
Guido van Rossum705f0f52001-08-24 16:47:00 +00004150
4151/* Cooperative 'super' */
4152
4153typedef struct {
4154 PyObject_HEAD
Guido van Rossume705ef12001-08-29 15:47:06 +00004155 PyTypeObject *type;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004156 PyObject *obj;
4157} superobject;
4158
Guido van Rossum6f799372001-09-20 20:46:19 +00004159static PyMemberDef super_members[] = {
4160 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
4161 "the class invoking super()"},
4162 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY,
4163 "the instance invoking super(); may be None"},
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004164 {0}
4165};
4166
Guido van Rossum705f0f52001-08-24 16:47:00 +00004167static void
4168super_dealloc(PyObject *self)
4169{
4170 superobject *su = (superobject *)self;
4171
Guido van Rossum048eb752001-10-02 21:24:57 +00004172 _PyObject_GC_UNTRACK(self);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004173 Py_XDECREF(su->obj);
4174 Py_XDECREF(su->type);
4175 self->ob_type->tp_free(self);
4176}
4177
4178static PyObject *
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004179super_repr(PyObject *self)
4180{
4181 superobject *su = (superobject *)self;
4182
4183 if (su->obj)
4184 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004185 "<super: <class '%s'>, <%s object>>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004186 su->type ? su->type->tp_name : "NULL",
4187 su->obj->ob_type->tp_name);
4188 else
4189 return PyString_FromFormat(
Guido van Rossuma4cb7882001-09-25 03:56:29 +00004190 "<super: <class '%s'>, NULL>",
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004191 su->type ? su->type->tp_name : "NULL");
4192}
4193
4194static PyObject *
Guido van Rossum705f0f52001-08-24 16:47:00 +00004195super_getattro(PyObject *self, PyObject *name)
4196{
4197 superobject *su = (superobject *)self;
4198
4199 if (su->obj != NULL) {
Tim Petersa91e9642001-11-14 23:32:33 +00004200 PyObject *mro, *res, *tmp, *dict;
Guido van Rossum155db9a2002-04-02 17:53:47 +00004201 PyTypeObject *starttype;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004202 descrgetfunc f;
4203 int i, n;
4204
Guido van Rossum155db9a2002-04-02 17:53:47 +00004205 starttype = su->obj->ob_type;
4206 mro = starttype->tp_mro;
4207
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004208 if (mro == NULL)
4209 n = 0;
4210 else {
4211 assert(PyTuple_Check(mro));
4212 n = PyTuple_GET_SIZE(mro);
4213 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004214 for (i = 0; i < n; i++) {
Guido van Rossume705ef12001-08-29 15:47:06 +00004215 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
Guido van Rossum705f0f52001-08-24 16:47:00 +00004216 break;
4217 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004218 if (i >= n && PyType_Check(su->obj)) {
Guido van Rossum155db9a2002-04-02 17:53:47 +00004219 starttype = (PyTypeObject *)(su->obj);
4220 mro = starttype->tp_mro;
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004221 if (mro == NULL)
4222 n = 0;
4223 else {
4224 assert(PyTuple_Check(mro));
4225 n = PyTuple_GET_SIZE(mro);
4226 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004227 for (i = 0; i < n; i++) {
4228 if ((PyObject *)(su->type) ==
4229 PyTuple_GET_ITEM(mro, i))
4230 break;
4231 }
Guido van Rossume705ef12001-08-29 15:47:06 +00004232 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004233 i++;
4234 res = NULL;
4235 for (; i < n; i++) {
4236 tmp = PyTuple_GET_ITEM(mro, i);
Tim Petersa91e9642001-11-14 23:32:33 +00004237 if (PyType_Check(tmp))
4238 dict = ((PyTypeObject *)tmp)->tp_dict;
4239 else if (PyClass_Check(tmp))
4240 dict = ((PyClassObject *)tmp)->cl_dict;
4241 else
4242 continue;
4243 res = PyDict_GetItem(dict, name);
Guido van Rossum5b443c62001-12-03 15:38:28 +00004244 if (res != NULL && !PyDescr_IsData(res)) {
Guido van Rossum705f0f52001-08-24 16:47:00 +00004245 Py_INCREF(res);
4246 f = res->ob_type->tp_descr_get;
4247 if (f != NULL) {
Guido van Rossumd4641072002-04-03 02:13:37 +00004248 tmp = f(res, su->obj,
4249 (PyObject *)starttype);
Guido van Rossum705f0f52001-08-24 16:47:00 +00004250 Py_DECREF(res);
4251 res = tmp;
4252 }
4253 return res;
4254 }
4255 }
4256 }
4257 return PyObject_GenericGetAttr(self, name);
4258}
4259
Guido van Rossum5b443c62001-12-03 15:38:28 +00004260static int
4261supercheck(PyTypeObject *type, PyObject *obj)
4262{
4263 if (!PyType_IsSubtype(obj->ob_type, type) &&
4264 !(PyType_Check(obj) &&
4265 PyType_IsSubtype((PyTypeObject *)obj, type))) {
4266 PyErr_SetString(PyExc_TypeError,
4267 "super(type, obj): "
4268 "obj must be an instance or subtype of type");
4269 return -1;
4270 }
4271 else
4272 return 0;
4273}
4274
Guido van Rossum705f0f52001-08-24 16:47:00 +00004275static PyObject *
4276super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
4277{
4278 superobject *su = (superobject *)self;
4279 superobject *new;
4280
4281 if (obj == NULL || obj == Py_None || su->obj != NULL) {
4282 /* Not binding to an object, or already bound */
4283 Py_INCREF(self);
4284 return self;
4285 }
Guido van Rossum5b443c62001-12-03 15:38:28 +00004286 if (su->ob_type != &PySuper_Type)
4287 /* If su is an instance of a subclass of super,
4288 call its type */
4289 return PyObject_CallFunction((PyObject *)su->ob_type,
4290 "OO", su->type, obj);
4291 else {
4292 /* Inline the common case */
4293 if (supercheck(su->type, obj) < 0)
4294 return NULL;
4295 new = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
4296 NULL, NULL);
4297 if (new == NULL)
4298 return NULL;
4299 Py_INCREF(su->type);
4300 Py_INCREF(obj);
4301 new->type = su->type;
4302 new->obj = obj;
4303 return (PyObject *)new;
4304 }
Guido van Rossum705f0f52001-08-24 16:47:00 +00004305}
4306
4307static int
4308super_init(PyObject *self, PyObject *args, PyObject *kwds)
4309{
4310 superobject *su = (superobject *)self;
Guido van Rossume705ef12001-08-29 15:47:06 +00004311 PyTypeObject *type;
4312 PyObject *obj = NULL;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004313
4314 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
4315 return -1;
4316 if (obj == Py_None)
4317 obj = NULL;
Guido van Rossum5b443c62001-12-03 15:38:28 +00004318 if (obj != NULL && supercheck(type, obj) < 0)
Guido van Rossum705f0f52001-08-24 16:47:00 +00004319 return -1;
Guido van Rossum705f0f52001-08-24 16:47:00 +00004320 Py_INCREF(type);
4321 Py_XINCREF(obj);
4322 su->type = type;
4323 su->obj = obj;
4324 return 0;
4325}
4326
4327static char super_doc[] =
4328"super(type) -> unbound super object\n"
4329"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
Guido van Rossume705ef12001-08-29 15:47:06 +00004330"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
Guido van Rossum705f0f52001-08-24 16:47:00 +00004331"Typical use to call a cooperative superclass method:\n"
4332"class C(B):\n"
4333" def meth(self, arg):\n"
4334" super(C, self).meth(arg)";
4335
Guido van Rossum048eb752001-10-02 21:24:57 +00004336static int
4337super_traverse(PyObject *self, visitproc visit, void *arg)
4338{
4339 superobject *su = (superobject *)self;
4340 int err;
4341
4342#define VISIT(SLOT) \
4343 if (SLOT) { \
4344 err = visit((PyObject *)(SLOT), arg); \
4345 if (err) \
4346 return err; \
4347 }
4348
4349 VISIT(su->obj);
4350 VISIT(su->type);
4351
4352#undef VISIT
4353
4354 return 0;
4355}
4356
Guido van Rossum705f0f52001-08-24 16:47:00 +00004357PyTypeObject PySuper_Type = {
4358 PyObject_HEAD_INIT(&PyType_Type)
4359 0, /* ob_size */
4360 "super", /* tp_name */
4361 sizeof(superobject), /* tp_basicsize */
4362 0, /* tp_itemsize */
4363 /* methods */
4364 super_dealloc, /* tp_dealloc */
4365 0, /* tp_print */
4366 0, /* tp_getattr */
4367 0, /* tp_setattr */
4368 0, /* tp_compare */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004369 super_repr, /* tp_repr */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004370 0, /* tp_as_number */
4371 0, /* tp_as_sequence */
4372 0, /* tp_as_mapping */
4373 0, /* tp_hash */
4374 0, /* tp_call */
4375 0, /* tp_str */
4376 super_getattro, /* tp_getattro */
4377 0, /* tp_setattro */
4378 0, /* tp_as_buffer */
Guido van Rossum048eb752001-10-02 21:24:57 +00004379 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
4380 Py_TPFLAGS_BASETYPE, /* tp_flags */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004381 super_doc, /* tp_doc */
Guido van Rossum048eb752001-10-02 21:24:57 +00004382 super_traverse, /* tp_traverse */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004383 0, /* tp_clear */
4384 0, /* tp_richcompare */
4385 0, /* tp_weaklistoffset */
4386 0, /* tp_iter */
4387 0, /* tp_iternext */
4388 0, /* tp_methods */
Guido van Rossum41eb14d2001-08-30 23:13:11 +00004389 super_members, /* tp_members */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004390 0, /* tp_getset */
4391 0, /* tp_base */
4392 0, /* tp_dict */
4393 super_descr_get, /* tp_descr_get */
4394 0, /* tp_descr_set */
4395 0, /* tp_dictoffset */
4396 super_init, /* tp_init */
4397 PyType_GenericAlloc, /* tp_alloc */
4398 PyType_GenericNew, /* tp_new */
Neil Schemenauer09a2ae52002-04-12 03:06:53 +00004399 PyObject_GC_Del, /* tp_free */
Guido van Rossum705f0f52001-08-24 16:47:00 +00004400};