typeobject.c revision daf82f7539218eb03385b51bffaceb6970fc76d8
1/* Type object implementation */
2
3#include "Python.h"
4#include "structmember.h"
5
6#include <ctype.h>
7
8
9/* Support type attribute cache */
10
11/* The cache can keep references to the names alive for longer than
12   they normally would.  This is why the maximum size is limited to
13   MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
14   strings are used as attribute names. */
15#define MCACHE_MAX_ATTR_SIZE    100
16#define MCACHE_SIZE_EXP         12
17#define MCACHE_HASH(version, name_hash)                                 \
18        (((unsigned int)(version) ^ (unsigned int)(name_hash))          \
19         & ((1 << MCACHE_SIZE_EXP) - 1))
20
21#define MCACHE_HASH_METHOD(type, name)                                  \
22        MCACHE_HASH((type)->tp_version_tag,                     \
23                    ((PyStringObject *)(name))->ob_shash)
24#define MCACHE_CACHEABLE_NAME(name)                                     \
25        PyString_CheckExact(name) &&                            \
26        PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
27
28struct method_cache_entry {
29    unsigned int version;
30    PyObject *name;             /* reference to exactly a str or None */
31    PyObject *value;            /* borrowed */
32};
33
34static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
35static unsigned int next_version_tag = 0;
36
37#define MCACHE_STATS 0
38
39#if MCACHE_STATS
40static size_t method_cache_hits = 0;
41static size_t method_cache_misses = 0;
42static size_t method_cache_collisions = 0;
43#endif
44
45unsigned int
46PyType_ClearCache(void)
47{
48    Py_ssize_t i;
49    unsigned int cur_version_tag = next_version_tag - 1;
50
51#if MCACHE_STATS
52    size_t total = method_cache_hits + method_cache_collisions + method_cache_misses;
53    fprintf(stderr, "-- Method cache hits        = %zd (%d%%)\n",
54            method_cache_hits, (int) (100.0 * method_cache_hits / total));
55    fprintf(stderr, "-- Method cache true misses = %zd (%d%%)\n",
56            method_cache_misses, (int) (100.0 * method_cache_misses / total));
57    fprintf(stderr, "-- Method cache collisions  = %zd (%d%%)\n",
58            method_cache_collisions, (int) (100.0 * method_cache_collisions / total));
59    fprintf(stderr, "-- Method cache size        = %zd KB\n",
60            sizeof(method_cache) / 1024);
61#endif
62
63    for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
64        method_cache[i].version = 0;
65        Py_CLEAR(method_cache[i].name);
66        method_cache[i].value = NULL;
67    }
68    next_version_tag = 0;
69    /* mark all version tags as invalid */
70    PyType_Modified(&PyBaseObject_Type);
71    return cur_version_tag;
72}
73
74void
75PyType_Modified(PyTypeObject *type)
76{
77    /* Invalidate any cached data for the specified type and all
78       subclasses.  This function is called after the base
79       classes, mro, or attributes of the type are altered.
80
81       Invariants:
82
83       - Py_TPFLAGS_VALID_VERSION_TAG is never set if
84         Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
85         objects coming from non-recompiled extension modules)
86
87       - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
88         it must first be set on all super types.
89
90       This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
91       type (so it must first clear it on all subclasses).  The
92       tp_version_tag value is meaningless unless this flag is set.
93       We don't assign new version tags eagerly, but only as
94       needed.
95     */
96    PyObject *raw, *ref;
97    Py_ssize_t i, n;
98
99    if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
100        return;
101
102    raw = type->tp_subclasses;
103    if (raw != NULL) {
104        n = PyList_GET_SIZE(raw);
105        for (i = 0; i < n; i++) {
106            ref = PyList_GET_ITEM(raw, i);
107            ref = PyWeakref_GET_OBJECT(ref);
108            if (ref != Py_None) {
109                PyType_Modified((PyTypeObject *)ref);
110            }
111        }
112    }
113    type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
114}
115
116static void
117type_mro_modified(PyTypeObject *type, PyObject *bases) {
118    /*
119       Check that all base classes or elements of the mro of type are
120       able to be cached.  This function is called after the base
121       classes or mro of the type are altered.
122
123       Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
124       inherits from an old-style class, either directly or if it
125       appears in the MRO of a new-style class.  No support either for
126       custom MROs that include types that are not officially super
127       types.
128
129       Called from mro_internal, which will subsequently be called on
130       each subclass when their mro is recursively updated.
131     */
132    Py_ssize_t i, n;
133    int clear = 0;
134
135    if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
136        return;
137
138    n = PyTuple_GET_SIZE(bases);
139    for (i = 0; i < n; i++) {
140        PyObject *b = PyTuple_GET_ITEM(bases, i);
141        PyTypeObject *cls;
142
143        if (!PyType_Check(b) ) {
144            clear = 1;
145            break;
146        }
147
148        cls = (PyTypeObject *)b;
149
150        if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
151            !PyType_IsSubtype(type, cls)) {
152            clear = 1;
153            break;
154        }
155    }
156
157    if (clear)
158        type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
159                            Py_TPFLAGS_VALID_VERSION_TAG);
160}
161
162static int
163assign_version_tag(PyTypeObject *type)
164{
165    /* Ensure that the tp_version_tag is valid and set
166       Py_TPFLAGS_VALID_VERSION_TAG.  To respect the invariant, this
167       must first be done on all super classes.  Return 0 if this
168       cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
169    */
170    Py_ssize_t i, n;
171    PyObject *bases;
172
173    if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
174        return 1;
175    if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
176        return 0;
177    if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
178        return 0;
179
180    type->tp_version_tag = next_version_tag++;
181    /* for stress-testing: next_version_tag &= 0xFF; */
182
183    if (type->tp_version_tag == 0) {
184        /* wrap-around or just starting Python - clear the whole
185           cache by filling names with references to Py_None.
186           Values are also set to NULL for added protection, as they
187           are borrowed reference */
188        for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
189            method_cache[i].value = NULL;
190            Py_INCREF(Py_None);
191            Py_XSETREF(method_cache[i].name, Py_None);
192        }
193        /* mark all version tags as invalid */
194        PyType_Modified(&PyBaseObject_Type);
195        return 1;
196    }
197    bases = type->tp_bases;
198    n = PyTuple_GET_SIZE(bases);
199    for (i = 0; i < n; i++) {
200        PyObject *b = PyTuple_GET_ITEM(bases, i);
201        assert(PyType_Check(b));
202        if (!assign_version_tag((PyTypeObject *)b))
203            return 0;
204    }
205    type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
206    return 1;
207}
208
209
210static PyMemberDef type_members[] = {
211    {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},
212    {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},
213    {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
214    {"__weakrefoffset__", T_LONG,
215     offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
216    {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
217    {"__dictoffset__", T_LONG,
218     offsetof(PyTypeObject, tp_dictoffset), READONLY},
219    {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
220    {0}
221};
222
223static PyObject *
224type_name(PyTypeObject *type, void *context)
225{
226    const char *s;
227
228    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
229        PyHeapTypeObject* et = (PyHeapTypeObject*)type;
230
231        Py_INCREF(et->ht_name);
232        return et->ht_name;
233    }
234    else {
235        s = strrchr(type->tp_name, '.');
236        if (s == NULL)
237            s = type->tp_name;
238        else
239            s++;
240        return PyString_FromString(s);
241    }
242}
243
244static int
245type_set_name(PyTypeObject *type, PyObject *value, void *context)
246{
247    PyHeapTypeObject* et;
248    PyObject *tmp;
249
250    if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
251        PyErr_Format(PyExc_TypeError,
252                     "can't set %s.__name__", type->tp_name);
253        return -1;
254    }
255    if (!value) {
256        PyErr_Format(PyExc_TypeError,
257                     "can't delete %s.__name__", type->tp_name);
258        return -1;
259    }
260    if (!PyString_Check(value)) {
261        PyErr_Format(PyExc_TypeError,
262                     "can only assign string to %s.__name__, not '%s'",
263                     type->tp_name, Py_TYPE(value)->tp_name);
264        return -1;
265    }
266    if (strlen(PyString_AS_STRING(value))
267        != (size_t)PyString_GET_SIZE(value)) {
268        PyErr_SetString(PyExc_ValueError,
269                        "type name must not contain null characters");
270        return -1;
271    }
272
273    et = (PyHeapTypeObject*)type;
274
275    Py_INCREF(value);
276
277    /* Wait until et is a sane state before Py_DECREF'ing the old et->ht_name
278       value.  (Bug #16447.)  */
279    tmp = et->ht_name;
280    et->ht_name = value;
281
282    type->tp_name = PyString_AS_STRING(value);
283    Py_DECREF(tmp);
284
285    return 0;
286}
287
288static PyObject *
289type_module(PyTypeObject *type, void *context)
290{
291    PyObject *mod;
292    char *s;
293
294    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
295        mod = PyDict_GetItemString(type->tp_dict, "__module__");
296        if (!mod) {
297            PyErr_Format(PyExc_AttributeError, "__module__");
298            return 0;
299        }
300        Py_XINCREF(mod);
301        return mod;
302    }
303    else {
304        s = strrchr(type->tp_name, '.');
305        if (s != NULL)
306            return PyString_FromStringAndSize(
307                type->tp_name, (Py_ssize_t)(s - type->tp_name));
308        return PyString_FromString("__builtin__");
309    }
310}
311
312static int
313type_set_module(PyTypeObject *type, PyObject *value, void *context)
314{
315    if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
316        PyErr_Format(PyExc_TypeError,
317                     "can't set %s.__module__", type->tp_name);
318        return -1;
319    }
320    if (!value) {
321        PyErr_Format(PyExc_TypeError,
322                     "can't delete %s.__module__", type->tp_name);
323        return -1;
324    }
325
326    PyType_Modified(type);
327
328    return PyDict_SetItemString(type->tp_dict, "__module__", value);
329}
330
331static PyObject *
332type_abstractmethods(PyTypeObject *type, void *context)
333{
334    PyObject *mod = NULL;
335    /* type itself has an __abstractmethods__ descriptor (this). Don't return
336       that. */
337    if (type != &PyType_Type)
338        mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");
339    if (!mod) {
340        PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
341        return NULL;
342    }
343    Py_XINCREF(mod);
344    return mod;
345}
346
347static int
348type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
349{
350    /* __abstractmethods__ should only be set once on a type, in
351       abc.ABCMeta.__new__, so this function doesn't do anything
352       special to update subclasses.
353    */
354    int abstract, res;
355    if (value != NULL) {
356        abstract = PyObject_IsTrue(value);
357        if (abstract < 0)
358            return -1;
359        res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
360    }
361    else {
362        abstract = 0;
363        res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
364        if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
365            PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
366            return -1;
367        }
368    }
369    if (res == 0) {
370        PyType_Modified(type);
371        if (abstract)
372            type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
373        else
374            type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
375    }
376    return res;
377}
378
379static PyObject *
380type_get_bases(PyTypeObject *type, void *context)
381{
382    Py_INCREF(type->tp_bases);
383    return type->tp_bases;
384}
385
386static PyTypeObject *best_base(PyObject *);
387static int mro_internal(PyTypeObject *);
388static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
389static int add_subclass(PyTypeObject*, PyTypeObject*);
390static void remove_subclass(PyTypeObject *, PyTypeObject *);
391static void update_all_slots(PyTypeObject *);
392
393typedef int (*update_callback)(PyTypeObject *, void *);
394static int update_subclasses(PyTypeObject *type, PyObject *name,
395                             update_callback callback, void *data);
396static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
397                                   update_callback callback, void *data);
398
399static int
400mro_subclasses(PyTypeObject *type, PyObject* temp)
401{
402    PyTypeObject *subclass;
403    PyObject *ref, *subclasses, *old_mro;
404    Py_ssize_t i, n;
405
406    subclasses = type->tp_subclasses;
407    if (subclasses == NULL)
408        return 0;
409    assert(PyList_Check(subclasses));
410    n = PyList_GET_SIZE(subclasses);
411    for (i = 0; i < n; i++) {
412        ref = PyList_GET_ITEM(subclasses, i);
413        assert(PyWeakref_CheckRef(ref));
414        subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
415        assert(subclass != NULL);
416        if ((PyObject *)subclass == Py_None)
417            continue;
418        assert(PyType_Check(subclass));
419        old_mro = subclass->tp_mro;
420        if (mro_internal(subclass) < 0) {
421            subclass->tp_mro = old_mro;
422            return -1;
423        }
424        else {
425            PyObject* tuple;
426            tuple = PyTuple_Pack(2, subclass, old_mro);
427            Py_DECREF(old_mro);
428            if (!tuple)
429                return -1;
430            if (PyList_Append(temp, tuple) < 0)
431                return -1;
432            Py_DECREF(tuple);
433        }
434        if (mro_subclasses(subclass, temp) < 0)
435            return -1;
436    }
437    return 0;
438}
439
440static int
441type_set_bases(PyTypeObject *type, PyObject *value, void *context)
442{
443    Py_ssize_t i;
444    int r = 0;
445    PyObject *ob, *temp;
446    PyTypeObject *new_base, *old_base;
447    PyObject *old_bases, *old_mro;
448
449    if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
450        PyErr_Format(PyExc_TypeError,
451                     "can't set %s.__bases__", type->tp_name);
452        return -1;
453    }
454    if (!value) {
455        PyErr_Format(PyExc_TypeError,
456                     "can't delete %s.__bases__", type->tp_name);
457        return -1;
458    }
459    if (!PyTuple_Check(value)) {
460        PyErr_Format(PyExc_TypeError,
461             "can only assign tuple to %s.__bases__, not %s",
462                 type->tp_name, Py_TYPE(value)->tp_name);
463        return -1;
464    }
465    if (PyTuple_GET_SIZE(value) == 0) {
466        PyErr_Format(PyExc_TypeError,
467             "can only assign non-empty tuple to %s.__bases__, not ()",
468                 type->tp_name);
469        return -1;
470    }
471    for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
472        ob = PyTuple_GET_ITEM(value, i);
473        if (!PyClass_Check(ob) && !PyType_Check(ob)) {
474            PyErr_Format(
475                PyExc_TypeError,
476    "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
477                            type->tp_name, Py_TYPE(ob)->tp_name);
478                    return -1;
479        }
480        if (PyType_Check(ob)) {
481            if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
482                PyErr_SetString(PyExc_TypeError,
483            "a __bases__ item causes an inheritance cycle");
484                return -1;
485            }
486        }
487    }
488
489    new_base = best_base(value);
490
491    if (!new_base) {
492        return -1;
493    }
494
495    if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
496        return -1;
497
498    Py_INCREF(new_base);
499    Py_INCREF(value);
500
501    old_bases = type->tp_bases;
502    old_base = type->tp_base;
503    old_mro = type->tp_mro;
504
505    type->tp_bases = value;
506    type->tp_base = new_base;
507
508    if (mro_internal(type) < 0) {
509        goto bail;
510    }
511
512    temp = PyList_New(0);
513    if (!temp)
514        goto bail;
515
516    r = mro_subclasses(type, temp);
517
518    if (r < 0) {
519        for (i = 0; i < PyList_Size(temp); i++) {
520            PyTypeObject* cls;
521            PyObject* mro;
522            PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
523                             "", 2, 2, &cls, &mro);
524            Py_INCREF(mro);
525            ob = cls->tp_mro;
526            cls->tp_mro = mro;
527            Py_DECREF(ob);
528        }
529        Py_DECREF(temp);
530        goto bail;
531    }
532
533    Py_DECREF(temp);
534
535    /* any base that was in __bases__ but now isn't, we
536       need to remove |type| from its tp_subclasses.
537       conversely, any class now in __bases__ that wasn't
538       needs to have |type| added to its subclasses. */
539
540    /* for now, sod that: just remove from all old_bases,
541       add to all new_bases */
542
543    for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
544        ob = PyTuple_GET_ITEM(old_bases, i);
545        if (PyType_Check(ob)) {
546            remove_subclass(
547                (PyTypeObject*)ob, type);
548        }
549    }
550
551    for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
552        ob = PyTuple_GET_ITEM(value, i);
553        if (PyType_Check(ob)) {
554            if (add_subclass((PyTypeObject*)ob, type) < 0)
555                r = -1;
556        }
557    }
558
559    update_all_slots(type);
560
561    Py_DECREF(old_bases);
562    Py_DECREF(old_base);
563    Py_DECREF(old_mro);
564
565    return r;
566
567  bail:
568    Py_DECREF(type->tp_bases);
569    Py_DECREF(type->tp_base);
570    if (type->tp_mro != old_mro) {
571        Py_DECREF(type->tp_mro);
572    }
573
574    type->tp_bases = old_bases;
575    type->tp_base = old_base;
576    type->tp_mro = old_mro;
577
578    return -1;
579}
580
581static PyObject *
582type_dict(PyTypeObject *type, void *context)
583{
584    if (type->tp_dict == NULL) {
585        Py_INCREF(Py_None);
586        return Py_None;
587    }
588    return PyDictProxy_New(type->tp_dict);
589}
590
591static PyObject *
592type_get_doc(PyTypeObject *type, void *context)
593{
594    PyObject *result;
595    if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
596        return PyString_FromString(type->tp_doc);
597    result = PyDict_GetItemString(type->tp_dict, "__doc__");
598    if (result == NULL) {
599        result = Py_None;
600        Py_INCREF(result);
601    }
602    else if (Py_TYPE(result)->tp_descr_get) {
603        result = Py_TYPE(result)->tp_descr_get(result, NULL,
604                                               (PyObject *)type);
605    }
606    else {
607        Py_INCREF(result);
608    }
609    return result;
610}
611
612static PyObject *
613type___instancecheck__(PyObject *type, PyObject *inst)
614{
615    switch (_PyObject_RealIsInstance(inst, type)) {
616    case -1:
617        return NULL;
618    case 0:
619        Py_RETURN_FALSE;
620    default:
621        Py_RETURN_TRUE;
622    }
623}
624
625
626static PyObject *
627type___subclasscheck__(PyObject *type, PyObject *inst)
628{
629    switch (_PyObject_RealIsSubclass(inst, type)) {
630    case -1:
631        return NULL;
632    case 0:
633        Py_RETURN_FALSE;
634    default:
635        Py_RETURN_TRUE;
636    }
637}
638
639
640static PyGetSetDef type_getsets[] = {
641    {"__name__", (getter)type_name, (setter)type_set_name, NULL},
642    {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
643    {"__module__", (getter)type_module, (setter)type_set_module, NULL},
644    {"__abstractmethods__", (getter)type_abstractmethods,
645     (setter)type_set_abstractmethods, NULL},
646    {"__dict__",  (getter)type_dict,  NULL, NULL},
647    {"__doc__", (getter)type_get_doc, NULL, NULL},
648    {0}
649};
650
651
652static PyObject*
653type_richcompare(PyObject *v, PyObject *w, int op)
654{
655    PyObject *result;
656    Py_uintptr_t vv, ww;
657    int c;
658
659    /* Make sure both arguments are types. */
660    if (!PyType_Check(v) || !PyType_Check(w) ||
661        /* If there is a __cmp__ method defined, let it be called instead
662           of our dumb function designed merely to warn.  See bug
663           #7491. */
664        Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {
665        result = Py_NotImplemented;
666        goto out;
667    }
668
669    /* Py3K warning if comparison isn't == or !=  */
670    if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
671        PyErr_WarnEx(PyExc_DeprecationWarning,
672                   "type inequality comparisons not supported "
673                   "in 3.x", 1) < 0) {
674        return NULL;
675    }
676
677    /* Compare addresses */
678    vv = (Py_uintptr_t)v;
679    ww = (Py_uintptr_t)w;
680    switch (op) {
681    case Py_LT: c = vv <  ww; break;
682    case Py_LE: c = vv <= ww; break;
683    case Py_EQ: c = vv == ww; break;
684    case Py_NE: c = vv != ww; break;
685    case Py_GT: c = vv >  ww; break;
686    case Py_GE: c = vv >= ww; break;
687    default:
688        result = Py_NotImplemented;
689        goto out;
690    }
691    result = c ? Py_True : Py_False;
692
693  /* incref and return */
694  out:
695    Py_INCREF(result);
696    return result;
697}
698
699static PyObject *
700type_repr(PyTypeObject *type)
701{
702    PyObject *mod, *name, *rtn;
703    char *kind;
704
705    mod = type_module(type, NULL);
706    if (mod == NULL)
707        PyErr_Clear();
708    else if (!PyString_Check(mod)) {
709        Py_DECREF(mod);
710        mod = NULL;
711    }
712    name = type_name(type, NULL);
713    if (name == NULL) {
714        Py_XDECREF(mod);
715        return NULL;
716    }
717
718    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
719        kind = "class";
720    else
721        kind = "type";
722
723    if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
724        rtn = PyString_FromFormat("<%s '%s.%s'>",
725                                  kind,
726                                  PyString_AS_STRING(mod),
727                                  PyString_AS_STRING(name));
728    }
729    else
730        rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
731
732    Py_XDECREF(mod);
733    Py_DECREF(name);
734    return rtn;
735}
736
737static PyObject *
738type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
739{
740    PyObject *obj;
741
742    if (type->tp_new == NULL) {
743        PyErr_Format(PyExc_TypeError,
744                     "cannot create '%.100s' instances",
745                     type->tp_name);
746        return NULL;
747    }
748
749    obj = type->tp_new(type, args, kwds);
750    if (obj != NULL) {
751        /* Ugly exception: when the call was type(something),
752           don't call tp_init on the result. */
753        if (type == &PyType_Type &&
754            PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
755            (kwds == NULL ||
756             (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
757            return obj;
758        /* If the returned object is not an instance of type,
759           it won't be initialized. */
760        if (!PyType_IsSubtype(obj->ob_type, type))
761            return obj;
762        type = obj->ob_type;
763        if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
764            type->tp_init != NULL &&
765            type->tp_init(obj, args, kwds) < 0) {
766            Py_DECREF(obj);
767            obj = NULL;
768        }
769    }
770    return obj;
771}
772
773PyObject *
774PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
775{
776    PyObject *obj;
777    const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
778    /* note that we need to add one, for the sentinel */
779
780    if (PyType_IS_GC(type))
781        obj = _PyObject_GC_Malloc(size);
782    else
783        obj = (PyObject *)PyObject_MALLOC(size);
784
785    if (obj == NULL)
786        return PyErr_NoMemory();
787
788    memset(obj, '\0', size);
789
790    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
791        Py_INCREF(type);
792
793    if (type->tp_itemsize == 0)
794        (void)PyObject_INIT(obj, type);
795    else
796        (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
797
798    if (PyType_IS_GC(type))
799        _PyObject_GC_TRACK(obj);
800    return obj;
801}
802
803PyObject *
804PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
805{
806    return type->tp_alloc(type, 0);
807}
808
809/* Helpers for subtyping */
810
811static int
812traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
813{
814    Py_ssize_t i, n;
815    PyMemberDef *mp;
816
817    n = Py_SIZE(type);
818    mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
819    for (i = 0; i < n; i++, mp++) {
820        if (mp->type == T_OBJECT_EX) {
821            char *addr = (char *)self + mp->offset;
822            PyObject *obj = *(PyObject **)addr;
823            if (obj != NULL) {
824                int err = visit(obj, arg);
825                if (err)
826                    return err;
827            }
828        }
829    }
830    return 0;
831}
832
833static int
834subtype_traverse(PyObject *self, visitproc visit, void *arg)
835{
836    PyTypeObject *type, *base;
837    traverseproc basetraverse;
838
839    /* Find the nearest base with a different tp_traverse,
840       and traverse slots while we're at it */
841    type = Py_TYPE(self);
842    base = type;
843    while ((basetraverse = base->tp_traverse) == subtype_traverse) {
844        if (Py_SIZE(base)) {
845            int err = traverse_slots(base, self, visit, arg);
846            if (err)
847                return err;
848        }
849        base = base->tp_base;
850        assert(base);
851    }
852
853    if (type->tp_dictoffset != base->tp_dictoffset) {
854        PyObject **dictptr = _PyObject_GetDictPtr(self);
855        if (dictptr && *dictptr)
856            Py_VISIT(*dictptr);
857    }
858
859    if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
860        /* For a heaptype, the instances count as references
861           to the type.          Traverse the type so the collector
862           can find cycles involving this link. */
863        Py_VISIT(type);
864
865    if (basetraverse)
866        return basetraverse(self, visit, arg);
867    return 0;
868}
869
870static void
871clear_slots(PyTypeObject *type, PyObject *self)
872{
873    Py_ssize_t i, n;
874    PyMemberDef *mp;
875
876    n = Py_SIZE(type);
877    mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
878    for (i = 0; i < n; i++, mp++) {
879        if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
880            char *addr = (char *)self + mp->offset;
881            PyObject *obj = *(PyObject **)addr;
882            if (obj != NULL) {
883                *(PyObject **)addr = NULL;
884                Py_DECREF(obj);
885            }
886        }
887    }
888}
889
890static int
891subtype_clear(PyObject *self)
892{
893    PyTypeObject *type, *base;
894    inquiry baseclear;
895
896    /* Find the nearest base with a different tp_clear
897       and clear slots while we're at it */
898    type = Py_TYPE(self);
899    base = type;
900    while ((baseclear = base->tp_clear) == subtype_clear) {
901        if (Py_SIZE(base))
902            clear_slots(base, self);
903        base = base->tp_base;
904        assert(base);
905    }
906
907    /* Clear the instance dict (if any), to break cycles involving only
908       __dict__ slots (as in the case 'self.__dict__ is self'). */
909    if (type->tp_dictoffset != base->tp_dictoffset) {
910        PyObject **dictptr = _PyObject_GetDictPtr(self);
911        if (dictptr && *dictptr)
912            Py_CLEAR(*dictptr);
913    }
914
915    if (baseclear)
916        return baseclear(self);
917    return 0;
918}
919
920static void
921subtype_dealloc(PyObject *self)
922{
923    PyTypeObject *type, *base;
924    destructor basedealloc;
925    PyThreadState *tstate = PyThreadState_GET();
926
927    /* Extract the type; we expect it to be a heap type */
928    type = Py_TYPE(self);
929    assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
930
931    /* Test whether the type has GC exactly once */
932
933    if (!PyType_IS_GC(type)) {
934        /* It's really rare to find a dynamic type that doesn't have
935           GC; it can only happen when deriving from 'object' and not
936           adding any slots or instance variables.  This allows
937           certain simplifications: there's no need to call
938           clear_slots(), or DECREF the dict, or clear weakrefs. */
939
940        /* Maybe call finalizer; exit early if resurrected */
941        if (type->tp_del) {
942            type->tp_del(self);
943            if (self->ob_refcnt > 0)
944                return;
945        }
946
947        /* Find the nearest base with a different tp_dealloc */
948        base = type;
949        while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
950            assert(Py_SIZE(base) == 0);
951            base = base->tp_base;
952            assert(base);
953        }
954
955        /* Extract the type again; tp_del may have changed it */
956        type = Py_TYPE(self);
957
958        /* Call the base tp_dealloc() */
959        assert(basedealloc);
960        basedealloc(self);
961
962        /* Can't reference self beyond this point */
963        Py_DECREF(type);
964
965        /* Done */
966        return;
967    }
968
969    /* We get here only if the type has GC */
970
971    /* UnTrack and re-Track around the trashcan macro, alas */
972    /* See explanation at end of function for full disclosure */
973    PyObject_GC_UnTrack(self);
974    ++_PyTrash_delete_nesting;
975    ++ tstate->trash_delete_nesting;
976    Py_TRASHCAN_SAFE_BEGIN(self);
977    --_PyTrash_delete_nesting;
978    -- tstate->trash_delete_nesting;
979    /* DO NOT restore GC tracking at this point.  weakref callbacks
980     * (if any, and whether directly here or indirectly in something we
981     * call) may trigger GC, and if self is tracked at that point, it
982     * will look like trash to GC and GC will try to delete self again.
983     */
984
985    /* Find the nearest base with a different tp_dealloc */
986    base = type;
987    while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
988        base = base->tp_base;
989        assert(base);
990    }
991
992    /* If we added a weaklist, we clear it.      Do this *before* calling
993       the finalizer (__del__), clearing slots, or clearing the instance
994       dict. */
995
996    if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
997        PyObject_ClearWeakRefs(self);
998
999    /* Maybe call finalizer; exit early if resurrected */
1000    if (type->tp_del) {
1001        _PyObject_GC_TRACK(self);
1002        type->tp_del(self);
1003        if (self->ob_refcnt > 0)
1004            goto endlabel;              /* resurrected */
1005        else
1006            _PyObject_GC_UNTRACK(self);
1007        /* New weakrefs could be created during the finalizer call.
1008            If this occurs, clear them out without calling their
1009            finalizers since they might rely on part of the object
1010            being finalized that has already been destroyed. */
1011        if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
1012            /* Modeled after GET_WEAKREFS_LISTPTR() */
1013            PyWeakReference **list = (PyWeakReference **) \
1014                PyObject_GET_WEAKREFS_LISTPTR(self);
1015            while (*list)
1016                _PyWeakref_ClearRef(*list);
1017        }
1018    }
1019
1020    /*  Clear slots up to the nearest base with a different tp_dealloc */
1021    base = type;
1022    while (base->tp_dealloc == subtype_dealloc) {
1023        if (Py_SIZE(base))
1024            clear_slots(base, self);
1025        base = base->tp_base;
1026        assert(base);
1027    }
1028
1029    /* If we added a dict, DECREF it */
1030    if (type->tp_dictoffset && !base->tp_dictoffset) {
1031        PyObject **dictptr = _PyObject_GetDictPtr(self);
1032        if (dictptr != NULL) {
1033            PyObject *dict = *dictptr;
1034            if (dict != NULL) {
1035                Py_DECREF(dict);
1036                *dictptr = NULL;
1037            }
1038        }
1039    }
1040
1041    /* Extract the type again; tp_del may have changed it */
1042    type = Py_TYPE(self);
1043
1044    /* Call the base tp_dealloc(); first retrack self if
1045     * basedealloc knows about gc.
1046     */
1047    if (PyType_IS_GC(base))
1048        _PyObject_GC_TRACK(self);
1049    assert(basedealloc);
1050    basedealloc(self);
1051
1052    /* Can't reference self beyond this point */
1053    Py_DECREF(type);
1054
1055  endlabel:
1056    ++_PyTrash_delete_nesting;
1057    ++ tstate->trash_delete_nesting;
1058    Py_TRASHCAN_SAFE_END(self);
1059    --_PyTrash_delete_nesting;
1060    -- tstate->trash_delete_nesting;
1061
1062    /* Explanation of the weirdness around the trashcan macros:
1063
1064       Q. What do the trashcan macros do?
1065
1066       A. Read the comment titled "Trashcan mechanism" in object.h.
1067          For one, this explains why there must be a call to GC-untrack
1068          before the trashcan begin macro.      Without understanding the
1069          trashcan code, the answers to the following questions don't make
1070          sense.
1071
1072       Q. Why do we GC-untrack before the trashcan and then immediately
1073          GC-track again afterward?
1074
1075       A. In the case that the base class is GC-aware, the base class
1076          probably GC-untracks the object.      If it does that using the
1077          UNTRACK macro, this will crash when the object is already
1078          untracked.  Because we don't know what the base class does, the
1079          only safe thing is to make sure the object is tracked when we
1080          call the base class dealloc.  But...  The trashcan begin macro
1081          requires that the object is *untracked* before it is called.  So
1082          the dance becomes:
1083
1084         GC untrack
1085         trashcan begin
1086         GC track
1087
1088       Q. Why did the last question say "immediately GC-track again"?
1089          It's nowhere near immediately.
1090
1091       A. Because the code *used* to re-track immediately.      Bad Idea.
1092          self has a refcount of 0, and if gc ever gets its hands on it
1093          (which can happen if any weakref callback gets invoked), it
1094          looks like trash to gc too, and gc also tries to delete self
1095          then.  But we're already deleting self.  Double deallocation is
1096          a subtle disaster.
1097
1098       Q. Why the bizarre (net-zero) manipulation of
1099          _PyTrash_delete_nesting around the trashcan macros?
1100
1101       A. Some base classes (e.g. list) also use the trashcan mechanism.
1102          The following scenario used to be possible:
1103
1104          - suppose the trashcan level is one below the trashcan limit
1105
1106          - subtype_dealloc() is called
1107
1108          - the trashcan limit is not yet reached, so the trashcan level
1109        is incremented and the code between trashcan begin and end is
1110        executed
1111
1112          - this destroys much of the object's contents, including its
1113        slots and __dict__
1114
1115          - basedealloc() is called; this is really list_dealloc(), or
1116        some other type which also uses the trashcan macros
1117
1118          - the trashcan limit is now reached, so the object is put on the
1119        trashcan's to-be-deleted-later list
1120
1121          - basedealloc() returns
1122
1123          - subtype_dealloc() decrefs the object's type
1124
1125          - subtype_dealloc() returns
1126
1127          - later, the trashcan code starts deleting the objects from its
1128        to-be-deleted-later list
1129
1130          - subtype_dealloc() is called *AGAIN* for the same object
1131
1132          - at the very least (if the destroyed slots and __dict__ don't
1133        cause problems) the object's type gets decref'ed a second
1134        time, which is *BAD*!!!
1135
1136          The remedy is to make sure that if the code between trashcan
1137          begin and end in subtype_dealloc() is called, the code between
1138          trashcan begin and end in basedealloc() will also be called.
1139          This is done by decrementing the level after passing into the
1140          trashcan block, and incrementing it just before leaving the
1141          block.
1142
1143          But now it's possible that a chain of objects consisting solely
1144          of objects whose deallocator is subtype_dealloc() will defeat
1145          the trashcan mechanism completely: the decremented level means
1146          that the effective level never reaches the limit.      Therefore, we
1147          *increment* the level *before* entering the trashcan block, and
1148          matchingly decrement it after leaving.  This means the trashcan
1149          code will trigger a little early, but that's no big deal.
1150
1151       Q. Are there any live examples of code in need of all this
1152          complexity?
1153
1154       A. Yes.  See SF bug 668433 for code that crashed (when Python was
1155          compiled in debug mode) before the trashcan level manipulations
1156          were added.  For more discussion, see SF patches 581742, 575073
1157          and bug 574207.
1158    */
1159}
1160
1161static PyTypeObject *solid_base(PyTypeObject *type);
1162
1163/* type test with subclassing support */
1164
1165int
1166PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1167{
1168    PyObject *mro;
1169
1170    if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1171        return b == a || b == &PyBaseObject_Type;
1172
1173    mro = a->tp_mro;
1174    if (mro != NULL) {
1175        /* Deal with multiple inheritance without recursion
1176           by walking the MRO tuple */
1177        Py_ssize_t i, n;
1178        assert(PyTuple_Check(mro));
1179        n = PyTuple_GET_SIZE(mro);
1180        for (i = 0; i < n; i++) {
1181            if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1182                return 1;
1183        }
1184        return 0;
1185    }
1186    else {
1187        /* a is not completely initilized yet; follow tp_base */
1188        do {
1189            if (a == b)
1190                return 1;
1191            a = a->tp_base;
1192        } while (a != NULL);
1193        return b == &PyBaseObject_Type;
1194    }
1195}
1196
1197/* Internal routines to do a method lookup in the type
1198   without looking in the instance dictionary
1199   (so we can't use PyObject_GetAttr) but still binding
1200   it to the instance.  The arguments are the object,
1201   the method name as a C string, and the address of a
1202   static variable used to cache the interned Python string.
1203
1204   Two variants:
1205
1206   - lookup_maybe() returns NULL without raising an exception
1207     when the _PyType_Lookup() call fails;
1208
1209   - lookup_method() always raises an exception upon errors.
1210
1211   - _PyObject_LookupSpecial() exported for the benefit of other places.
1212*/
1213
1214static PyObject *
1215lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
1216{
1217    PyObject *res;
1218
1219    if (*attrobj == NULL) {
1220        *attrobj = PyString_InternFromString(attrstr);
1221        if (*attrobj == NULL)
1222            return NULL;
1223    }
1224    res = _PyType_Lookup(Py_TYPE(self), *attrobj);
1225    if (res != NULL) {
1226        descrgetfunc f;
1227        if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
1228            Py_INCREF(res);
1229        else
1230            res = f(res, self, (PyObject *)(Py_TYPE(self)));
1231    }
1232    return res;
1233}
1234
1235static PyObject *
1236lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1237{
1238    PyObject *res = lookup_maybe(self, attrstr, attrobj);
1239    if (res == NULL && !PyErr_Occurred())
1240        PyErr_SetObject(PyExc_AttributeError, *attrobj);
1241    return res;
1242}
1243
1244PyObject *
1245_PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1246{
1247    assert(!PyInstance_Check(self));
1248    return lookup_maybe(self, attrstr, attrobj);
1249}
1250
1251/* A variation of PyObject_CallMethod that uses lookup_method()
1252   instead of PyObject_GetAttrString().  This uses the same convention
1253   as lookup_method to cache the interned name string object. */
1254
1255static PyObject *
1256call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1257{
1258    va_list va;
1259    PyObject *args, *func = 0, *retval;
1260    va_start(va, format);
1261
1262    func = lookup_maybe(o, name, nameobj);
1263    if (func == NULL) {
1264        va_end(va);
1265        if (!PyErr_Occurred())
1266            PyErr_SetObject(PyExc_AttributeError, *nameobj);
1267        return NULL;
1268    }
1269
1270    if (format && *format)
1271        args = Py_VaBuildValue(format, va);
1272    else
1273        args = PyTuple_New(0);
1274
1275    va_end(va);
1276
1277    if (args == NULL) {
1278        Py_DECREF(func);
1279        return NULL;
1280    }
1281
1282    assert(PyTuple_Check(args));
1283    retval = PyObject_Call(func, args, NULL);
1284
1285    Py_DECREF(args);
1286    Py_DECREF(func);
1287
1288    return retval;
1289}
1290
1291/* Clone of call_method() that returns NotImplemented when the lookup fails. */
1292
1293static PyObject *
1294call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1295{
1296    va_list va;
1297    PyObject *args, *func = 0, *retval;
1298    va_start(va, format);
1299
1300    func = lookup_maybe(o, name, nameobj);
1301    if (func == NULL) {
1302        va_end(va);
1303        if (!PyErr_Occurred()) {
1304            Py_INCREF(Py_NotImplemented);
1305            return Py_NotImplemented;
1306        }
1307        return NULL;
1308    }
1309
1310    if (format && *format)
1311        args = Py_VaBuildValue(format, va);
1312    else
1313        args = PyTuple_New(0);
1314
1315    va_end(va);
1316
1317    if (args == NULL) {
1318        Py_DECREF(func);
1319        return NULL;
1320    }
1321
1322    assert(PyTuple_Check(args));
1323    retval = PyObject_Call(func, args, NULL);
1324
1325    Py_DECREF(args);
1326    Py_DECREF(func);
1327
1328    return retval;
1329}
1330
1331static int
1332fill_classic_mro(PyObject *mro, PyObject *cls)
1333{
1334    PyObject *bases, *base;
1335    Py_ssize_t i, n;
1336
1337    assert(PyList_Check(mro));
1338    assert(PyClass_Check(cls));
1339    i = PySequence_Contains(mro, cls);
1340    if (i < 0)
1341        return -1;
1342    if (!i) {
1343        if (PyList_Append(mro, cls) < 0)
1344            return -1;
1345    }
1346    bases = ((PyClassObject *)cls)->cl_bases;
1347    assert(bases && PyTuple_Check(bases));
1348    n = PyTuple_GET_SIZE(bases);
1349    for (i = 0; i < n; i++) {
1350        base = PyTuple_GET_ITEM(bases, i);
1351        if (fill_classic_mro(mro, base) < 0)
1352            return -1;
1353    }
1354    return 0;
1355}
1356
1357static PyObject *
1358classic_mro(PyObject *cls)
1359{
1360    PyObject *mro;
1361
1362    assert(PyClass_Check(cls));
1363    mro = PyList_New(0);
1364    if (mro != NULL) {
1365        if (fill_classic_mro(mro, cls) == 0)
1366            return mro;
1367        Py_DECREF(mro);
1368    }
1369    return NULL;
1370}
1371
1372/*
1373    Method resolution order algorithm C3 described in
1374    "A Monotonic Superclass Linearization for Dylan",
1375    by Kim Barrett, Bob Cassel, Paul Haahr,
1376    David A. Moon, Keith Playford, and P. Tucker Withington.
1377    (OOPSLA 1996)
1378
1379    Some notes about the rules implied by C3:
1380
1381    No duplicate bases.
1382    It isn't legal to repeat a class in a list of base classes.
1383
1384    The next three properties are the 3 constraints in "C3".
1385
1386    Local precedence order.
1387    If A precedes B in C's MRO, then A will precede B in the MRO of all
1388    subclasses of C.
1389
1390    Monotonicity.
1391    The MRO of a class must be an extension without reordering of the
1392    MRO of each of its superclasses.
1393
1394    Extended Precedence Graph (EPG).
1395    Linearization is consistent if there is a path in the EPG from
1396    each class to all its successors in the linearization.  See
1397    the paper for definition of EPG.
1398 */
1399
1400static int
1401tail_contains(PyObject *list, int whence, PyObject *o) {
1402    Py_ssize_t j, size;
1403    size = PyList_GET_SIZE(list);
1404
1405    for (j = whence+1; j < size; j++) {
1406        if (PyList_GET_ITEM(list, j) == o)
1407            return 1;
1408    }
1409    return 0;
1410}
1411
1412static PyObject *
1413class_name(PyObject *cls)
1414{
1415    PyObject *name = PyObject_GetAttrString(cls, "__name__");
1416    if (name == NULL) {
1417        PyErr_Clear();
1418        Py_XDECREF(name);
1419        name = PyObject_Repr(cls);
1420    }
1421    if (name == NULL)
1422        return NULL;
1423    if (!PyString_Check(name)) {
1424        Py_DECREF(name);
1425        return NULL;
1426    }
1427    return name;
1428}
1429
1430static int
1431check_duplicates(PyObject *list)
1432{
1433    Py_ssize_t i, j, n;
1434    /* Let's use a quadratic time algorithm,
1435       assuming that the bases lists is short.
1436    */
1437    n = PyList_GET_SIZE(list);
1438    for (i = 0; i < n; i++) {
1439        PyObject *o = PyList_GET_ITEM(list, i);
1440        for (j = i + 1; j < n; j++) {
1441            if (PyList_GET_ITEM(list, j) == o) {
1442                o = class_name(o);
1443                PyErr_Format(PyExc_TypeError,
1444                             "duplicate base class %s",
1445                             o ? PyString_AS_STRING(o) : "?");
1446                Py_XDECREF(o);
1447                return -1;
1448            }
1449        }
1450    }
1451    return 0;
1452}
1453
1454/* Raise a TypeError for an MRO order disagreement.
1455
1456   It's hard to produce a good error message.  In the absence of better
1457   insight into error reporting, report the classes that were candidates
1458   to be put next into the MRO.  There is some conflict between the
1459   order in which they should be put in the MRO, but it's hard to
1460   diagnose what constraint can't be satisfied.
1461*/
1462
1463static void
1464set_mro_error(PyObject *to_merge, int *remain)
1465{
1466    Py_ssize_t i, n, off, to_merge_size;
1467    char buf[1000];
1468    PyObject *k, *v;
1469    PyObject *set = PyDict_New();
1470    if (!set) return;
1471
1472    to_merge_size = PyList_GET_SIZE(to_merge);
1473    for (i = 0; i < to_merge_size; i++) {
1474        PyObject *L = PyList_GET_ITEM(to_merge, i);
1475        if (remain[i] < PyList_GET_SIZE(L)) {
1476            PyObject *c = PyList_GET_ITEM(L, remain[i]);
1477            if (PyDict_SetItem(set, c, Py_None) < 0) {
1478                Py_DECREF(set);
1479                return;
1480            }
1481        }
1482    }
1483    n = PyDict_Size(set);
1484
1485    off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1486consistent method resolution\norder (MRO) for bases");
1487    i = 0;
1488    while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1489        PyObject *name = class_name(k);
1490        off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1491                             name ? PyString_AS_STRING(name) : "?");
1492        Py_XDECREF(name);
1493        if (--n && (size_t)(off+1) < sizeof(buf)) {
1494            buf[off++] = ',';
1495            buf[off] = '\0';
1496        }
1497    }
1498    PyErr_SetString(PyExc_TypeError, buf);
1499    Py_DECREF(set);
1500}
1501
1502static int
1503pmerge(PyObject *acc, PyObject* to_merge) {
1504    Py_ssize_t i, j, to_merge_size, empty_cnt;
1505    int *remain;
1506    int ok;
1507
1508    to_merge_size = PyList_GET_SIZE(to_merge);
1509
1510    /* remain stores an index into each sublist of to_merge.
1511       remain[i] is the index of the next base in to_merge[i]
1512       that is not included in acc.
1513    */
1514    remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1515    if (remain == NULL)
1516        return -1;
1517    for (i = 0; i < to_merge_size; i++)
1518        remain[i] = 0;
1519
1520  again:
1521    empty_cnt = 0;
1522    for (i = 0; i < to_merge_size; i++) {
1523        PyObject *candidate;
1524
1525        PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1526
1527        if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1528            empty_cnt++;
1529            continue;
1530        }
1531
1532        /* Choose next candidate for MRO.
1533
1534           The input sequences alone can determine the choice.
1535           If not, choose the class which appears in the MRO
1536           of the earliest direct superclass of the new class.
1537        */
1538
1539        candidate = PyList_GET_ITEM(cur_list, remain[i]);
1540        for (j = 0; j < to_merge_size; j++) {
1541            PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1542            if (tail_contains(j_lst, remain[j], candidate)) {
1543                goto skip; /* continue outer loop */
1544            }
1545        }
1546        ok = PyList_Append(acc, candidate);
1547        if (ok < 0) {
1548            PyMem_Free(remain);
1549            return -1;
1550        }
1551        for (j = 0; j < to_merge_size; j++) {
1552            PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1553            if (remain[j] < PyList_GET_SIZE(j_lst) &&
1554                PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1555                remain[j]++;
1556            }
1557        }
1558        goto again;
1559      skip: ;
1560    }
1561
1562    if (empty_cnt == to_merge_size) {
1563        PyMem_FREE(remain);
1564        return 0;
1565    }
1566    set_mro_error(to_merge, remain);
1567    PyMem_FREE(remain);
1568    return -1;
1569}
1570
1571static PyObject *
1572mro_implementation(PyTypeObject *type)
1573{
1574    Py_ssize_t i, n;
1575    int ok;
1576    PyObject *bases, *result;
1577    PyObject *to_merge, *bases_aslist;
1578
1579    if (type->tp_dict == NULL) {
1580        if (PyType_Ready(type) < 0)
1581            return NULL;
1582    }
1583
1584    /* Find a superclass linearization that honors the constraints
1585       of the explicit lists of bases and the constraints implied by
1586       each base class.
1587
1588       to_merge is a list of lists, where each list is a superclass
1589       linearization implied by a base class.  The last element of
1590       to_merge is the declared list of bases.
1591    */
1592
1593    bases = type->tp_bases;
1594    n = PyTuple_GET_SIZE(bases);
1595
1596    to_merge = PyList_New(n+1);
1597    if (to_merge == NULL)
1598        return NULL;
1599
1600    for (i = 0; i < n; i++) {
1601        PyObject *base = PyTuple_GET_ITEM(bases, i);
1602        PyObject *parentMRO;
1603        if (PyType_Check(base))
1604            parentMRO = PySequence_List(
1605                ((PyTypeObject*)base)->tp_mro);
1606        else
1607            parentMRO = classic_mro(base);
1608        if (parentMRO == NULL) {
1609            Py_DECREF(to_merge);
1610            return NULL;
1611        }
1612
1613        PyList_SET_ITEM(to_merge, i, parentMRO);
1614    }
1615
1616    bases_aslist = PySequence_List(bases);
1617    if (bases_aslist == NULL) {
1618        Py_DECREF(to_merge);
1619        return NULL;
1620    }
1621    /* This is just a basic sanity check. */
1622    if (check_duplicates(bases_aslist) < 0) {
1623        Py_DECREF(to_merge);
1624        Py_DECREF(bases_aslist);
1625        return NULL;
1626    }
1627    PyList_SET_ITEM(to_merge, n, bases_aslist);
1628
1629    result = Py_BuildValue("[O]", (PyObject *)type);
1630    if (result == NULL) {
1631        Py_DECREF(to_merge);
1632        return NULL;
1633    }
1634
1635    ok = pmerge(result, to_merge);
1636    Py_DECREF(to_merge);
1637    if (ok < 0) {
1638        Py_DECREF(result);
1639        return NULL;
1640    }
1641
1642    return result;
1643}
1644
1645static PyObject *
1646mro_external(PyObject *self)
1647{
1648    PyTypeObject *type = (PyTypeObject *)self;
1649
1650    return mro_implementation(type);
1651}
1652
1653static int
1654mro_internal(PyTypeObject *type)
1655{
1656    PyObject *mro, *result, *tuple;
1657    int checkit = 0;
1658
1659    if (Py_TYPE(type) == &PyType_Type) {
1660        result = mro_implementation(type);
1661    }
1662    else {
1663        static PyObject *mro_str;
1664        checkit = 1;
1665        mro = lookup_method((PyObject *)type, "mro", &mro_str);
1666        if (mro == NULL)
1667            return -1;
1668        result = PyObject_CallObject(mro, NULL);
1669        Py_DECREF(mro);
1670    }
1671    if (result == NULL)
1672        return -1;
1673    tuple = PySequence_Tuple(result);
1674    Py_DECREF(result);
1675    if (tuple == NULL)
1676        return -1;
1677    if (checkit) {
1678        Py_ssize_t i, len;
1679        PyObject *cls;
1680        PyTypeObject *solid;
1681
1682        solid = solid_base(type);
1683
1684        len = PyTuple_GET_SIZE(tuple);
1685
1686        for (i = 0; i < len; i++) {
1687            PyTypeObject *t;
1688            cls = PyTuple_GET_ITEM(tuple, i);
1689            if (PyClass_Check(cls))
1690                continue;
1691            else if (!PyType_Check(cls)) {
1692                PyErr_Format(PyExc_TypeError,
1693                 "mro() returned a non-class ('%.500s')",
1694                                 Py_TYPE(cls)->tp_name);
1695                Py_DECREF(tuple);
1696                return -1;
1697            }
1698            t = (PyTypeObject*)cls;
1699            if (!PyType_IsSubtype(solid, solid_base(t))) {
1700                PyErr_Format(PyExc_TypeError,
1701             "mro() returned base with unsuitable layout ('%.500s')",
1702                                     t->tp_name);
1703                        Py_DECREF(tuple);
1704                        return -1;
1705            }
1706        }
1707    }
1708    type->tp_mro = tuple;
1709
1710    type_mro_modified(type, type->tp_mro);
1711    /* corner case: the old-style super class might have been hidden
1712       from the custom MRO */
1713    type_mro_modified(type, type->tp_bases);
1714
1715    PyType_Modified(type);
1716
1717    return 0;
1718}
1719
1720
1721/* Calculate the best base amongst multiple base classes.
1722   This is the first one that's on the path to the "solid base". */
1723
1724static PyTypeObject *
1725best_base(PyObject *bases)
1726{
1727    Py_ssize_t i, n;
1728    PyTypeObject *base, *winner, *candidate, *base_i;
1729    PyObject *base_proto;
1730
1731    assert(PyTuple_Check(bases));
1732    n = PyTuple_GET_SIZE(bases);
1733    assert(n > 0);
1734    base = NULL;
1735    winner = NULL;
1736    for (i = 0; i < n; i++) {
1737        base_proto = PyTuple_GET_ITEM(bases, i);
1738        if (PyClass_Check(base_proto))
1739            continue;
1740        if (!PyType_Check(base_proto)) {
1741            PyErr_SetString(
1742                PyExc_TypeError,
1743                "bases must be types");
1744            return NULL;
1745        }
1746        base_i = (PyTypeObject *)base_proto;
1747        if (base_i->tp_dict == NULL) {
1748            if (PyType_Ready(base_i) < 0)
1749                return NULL;
1750        }
1751        if (!PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) {
1752            PyErr_Format(PyExc_TypeError,
1753                         "type '%.100s' is not an acceptable base type",
1754                         base_i->tp_name);
1755            return NULL;
1756        }
1757        candidate = solid_base(base_i);
1758        if (winner == NULL) {
1759            winner = candidate;
1760            base = base_i;
1761        }
1762        else if (PyType_IsSubtype(winner, candidate))
1763            ;
1764        else if (PyType_IsSubtype(candidate, winner)) {
1765            winner = candidate;
1766            base = base_i;
1767        }
1768        else {
1769            PyErr_SetString(
1770                PyExc_TypeError,
1771                "multiple bases have "
1772                "instance lay-out conflict");
1773            return NULL;
1774        }
1775    }
1776    if (base == NULL)
1777        PyErr_SetString(PyExc_TypeError,
1778            "a new-style class can't have only classic bases");
1779    return base;
1780}
1781
1782static int
1783extra_ivars(PyTypeObject *type, PyTypeObject *base)
1784{
1785    size_t t_size = type->tp_basicsize;
1786    size_t b_size = base->tp_basicsize;
1787
1788    assert(t_size >= b_size); /* Else type smaller than base! */
1789    if (type->tp_itemsize || base->tp_itemsize) {
1790        /* If itemsize is involved, stricter rules */
1791        return t_size != b_size ||
1792            type->tp_itemsize != base->tp_itemsize;
1793    }
1794    if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1795        type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1796        type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1797        t_size -= sizeof(PyObject *);
1798    if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1799        type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1800        type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1801        t_size -= sizeof(PyObject *);
1802
1803    return t_size != b_size;
1804}
1805
1806static PyTypeObject *
1807solid_base(PyTypeObject *type)
1808{
1809    PyTypeObject *base;
1810
1811    if (type->tp_base)
1812        base = solid_base(type->tp_base);
1813    else
1814        base = &PyBaseObject_Type;
1815    if (extra_ivars(type, base))
1816        return type;
1817    else
1818        return base;
1819}
1820
1821static void object_dealloc(PyObject *);
1822static int object_init(PyObject *, PyObject *, PyObject *);
1823static int update_slot(PyTypeObject *, PyObject *);
1824static void fixup_slot_dispatchers(PyTypeObject *);
1825
1826/*
1827 * Helpers for  __dict__ descriptor.  We don't want to expose the dicts
1828 * inherited from various builtin types.  The builtin base usually provides
1829 * its own __dict__ descriptor, so we use that when we can.
1830 */
1831static PyTypeObject *
1832get_builtin_base_with_dict(PyTypeObject *type)
1833{
1834    while (type->tp_base != NULL) {
1835        if (type->tp_dictoffset != 0 &&
1836            !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1837            return type;
1838        type = type->tp_base;
1839    }
1840    return NULL;
1841}
1842
1843static PyObject *
1844get_dict_descriptor(PyTypeObject *type)
1845{
1846    static PyObject *dict_str;
1847    PyObject *descr;
1848
1849    if (dict_str == NULL) {
1850        dict_str = PyString_InternFromString("__dict__");
1851        if (dict_str == NULL)
1852            return NULL;
1853    }
1854    descr = _PyType_Lookup(type, dict_str);
1855    if (descr == NULL || !PyDescr_IsData(descr))
1856        return NULL;
1857
1858    return descr;
1859}
1860
1861static void
1862raise_dict_descr_error(PyObject *obj)
1863{
1864    PyErr_Format(PyExc_TypeError,
1865                 "this __dict__ descriptor does not support "
1866                 "'%.200s' objects", obj->ob_type->tp_name);
1867}
1868
1869static PyObject *
1870subtype_dict(PyObject *obj, void *context)
1871{
1872    PyObject **dictptr;
1873    PyObject *dict;
1874    PyTypeObject *base;
1875
1876    base = get_builtin_base_with_dict(obj->ob_type);
1877    if (base != NULL) {
1878        descrgetfunc func;
1879        PyObject *descr = get_dict_descriptor(base);
1880        if (descr == NULL) {
1881            raise_dict_descr_error(obj);
1882            return NULL;
1883        }
1884        func = descr->ob_type->tp_descr_get;
1885        if (func == NULL) {
1886            raise_dict_descr_error(obj);
1887            return NULL;
1888        }
1889        return func(descr, obj, (PyObject *)(obj->ob_type));
1890    }
1891
1892    dictptr = _PyObject_GetDictPtr(obj);
1893    if (dictptr == NULL) {
1894        PyErr_SetString(PyExc_AttributeError,
1895                        "This object has no __dict__");
1896        return NULL;
1897    }
1898    dict = *dictptr;
1899    if (dict == NULL)
1900        *dictptr = dict = PyDict_New();
1901    Py_XINCREF(dict);
1902    return dict;
1903}
1904
1905static int
1906subtype_setdict(PyObject *obj, PyObject *value, void *context)
1907{
1908    PyObject **dictptr;
1909    PyObject *dict;
1910    PyTypeObject *base;
1911
1912    base = get_builtin_base_with_dict(obj->ob_type);
1913    if (base != NULL) {
1914        descrsetfunc func;
1915        PyObject *descr = get_dict_descriptor(base);
1916        if (descr == NULL) {
1917            raise_dict_descr_error(obj);
1918            return -1;
1919        }
1920        func = descr->ob_type->tp_descr_set;
1921        if (func == NULL) {
1922            raise_dict_descr_error(obj);
1923            return -1;
1924        }
1925        return func(descr, obj, value);
1926    }
1927
1928    dictptr = _PyObject_GetDictPtr(obj);
1929    if (dictptr == NULL) {
1930        PyErr_SetString(PyExc_AttributeError,
1931                        "This object has no __dict__");
1932        return -1;
1933    }
1934    if (value != NULL && !PyDict_Check(value)) {
1935        PyErr_Format(PyExc_TypeError,
1936                     "__dict__ must be set to a dictionary, "
1937                     "not a '%.200s'", Py_TYPE(value)->tp_name);
1938        return -1;
1939    }
1940    dict = *dictptr;
1941    Py_XINCREF(value);
1942    *dictptr = value;
1943    Py_XDECREF(dict);
1944    return 0;
1945}
1946
1947static PyObject *
1948subtype_getweakref(PyObject *obj, void *context)
1949{
1950    PyObject **weaklistptr;
1951    PyObject *result;
1952
1953    if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
1954        PyErr_SetString(PyExc_AttributeError,
1955                        "This object has no __weakref__");
1956        return NULL;
1957    }
1958    assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1959    assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1960           (size_t)(Py_TYPE(obj)->tp_basicsize));
1961    weaklistptr = (PyObject **)
1962        ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
1963    if (*weaklistptr == NULL)
1964        result = Py_None;
1965    else
1966        result = *weaklistptr;
1967    Py_INCREF(result);
1968    return result;
1969}
1970
1971/* Three variants on the subtype_getsets list. */
1972
1973static PyGetSetDef subtype_getsets_full[] = {
1974    {"__dict__", subtype_dict, subtype_setdict,
1975     PyDoc_STR("dictionary for instance variables (if defined)")},
1976    {"__weakref__", subtype_getweakref, NULL,
1977     PyDoc_STR("list of weak references to the object (if defined)")},
1978    {0}
1979};
1980
1981static PyGetSetDef subtype_getsets_dict_only[] = {
1982    {"__dict__", subtype_dict, subtype_setdict,
1983     PyDoc_STR("dictionary for instance variables (if defined)")},
1984    {0}
1985};
1986
1987static PyGetSetDef subtype_getsets_weakref_only[] = {
1988    {"__weakref__", subtype_getweakref, NULL,
1989     PyDoc_STR("list of weak references to the object (if defined)")},
1990    {0}
1991};
1992
1993static int
1994valid_identifier(PyObject *s)
1995{
1996    unsigned char *p;
1997    Py_ssize_t i, n;
1998
1999    if (!PyString_Check(s)) {
2000        PyErr_Format(PyExc_TypeError,
2001                     "__slots__ items must be strings, not '%.200s'",
2002                     Py_TYPE(s)->tp_name);
2003        return 0;
2004    }
2005    p = (unsigned char *) PyString_AS_STRING(s);
2006    n = PyString_GET_SIZE(s);
2007    /* We must reject an empty name.  As a hack, we bump the
2008       length to 1 so that the loop will balk on the trailing \0. */
2009    if (n == 0)
2010        n = 1;
2011    for (i = 0; i < n; i++, p++) {
2012        if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
2013            PyErr_SetString(PyExc_TypeError,
2014                            "__slots__ must be identifiers");
2015            return 0;
2016        }
2017    }
2018    return 1;
2019}
2020
2021#ifdef Py_USING_UNICODE
2022/* Replace Unicode objects in slots.  */
2023
2024static PyObject *
2025_unicode_to_string(PyObject *slots, Py_ssize_t nslots)
2026{
2027    PyObject *tmp = NULL;
2028    PyObject *slot_name, *new_name;
2029    Py_ssize_t i;
2030
2031    for (i = 0; i < nslots; i++) {
2032        if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
2033            if (tmp == NULL) {
2034                tmp = PySequence_List(slots);
2035                if (tmp == NULL)
2036                    return NULL;
2037            }
2038            new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
2039                                                         NULL);
2040            if (new_name == NULL) {
2041                Py_DECREF(tmp);
2042                return NULL;
2043            }
2044            Py_INCREF(new_name);
2045            PyList_SET_ITEM(tmp, i, new_name);
2046            Py_DECREF(slot_name);
2047        }
2048    }
2049    if (tmp != NULL) {
2050        slots = PyList_AsTuple(tmp);
2051        Py_DECREF(tmp);
2052    }
2053    return slots;
2054}
2055#endif
2056
2057/* Forward */
2058static int
2059object_init(PyObject *self, PyObject *args, PyObject *kwds);
2060
2061static int
2062type_init(PyObject *cls, PyObject *args, PyObject *kwds)
2063{
2064    int res;
2065
2066    assert(args != NULL && PyTuple_Check(args));
2067    assert(kwds == NULL || PyDict_Check(kwds));
2068
2069    if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
2070        PyErr_SetString(PyExc_TypeError,
2071                        "type.__init__() takes no keyword arguments");
2072        return -1;
2073    }
2074
2075    if (args != NULL && PyTuple_Check(args) &&
2076        (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
2077        PyErr_SetString(PyExc_TypeError,
2078                        "type.__init__() takes 1 or 3 arguments");
2079        return -1;
2080    }
2081
2082    /* Call object.__init__(self) now. */
2083    /* XXX Could call super(type, cls).__init__() but what's the point? */
2084    args = PyTuple_GetSlice(args, 0, 0);
2085    res = object_init(cls, args, NULL);
2086    Py_DECREF(args);
2087    return res;
2088}
2089
2090static PyObject *
2091type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2092{
2093    PyObject *name, *bases, *dict;
2094    static char *kwlist[] = {"name", "bases", "dict", 0};
2095    PyObject *slots, *tmp, *newslots;
2096    PyTypeObject *type, *base, *tmptype, *winner;
2097    PyHeapTypeObject *et;
2098    PyMemberDef *mp;
2099    Py_ssize_t i, nbases, nslots, slotoffset;
2100    int j, may_add_dict, may_add_weak, add_dict, add_weak;
2101
2102    assert(args != NULL && PyTuple_Check(args));
2103    assert(kwds == NULL || PyDict_Check(kwds));
2104
2105    /* Special case: type(x) should return x->ob_type */
2106    {
2107        const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2108        const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2109
2110        if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
2111            PyObject *x = PyTuple_GET_ITEM(args, 0);
2112            Py_INCREF(Py_TYPE(x));
2113            return (PyObject *) Py_TYPE(x);
2114        }
2115
2116        /* SF bug 475327 -- if that didn't trigger, we need 3
2117           arguments. but PyArg_ParseTupleAndKeywords below may give
2118           a msg saying type() needs exactly 3. */
2119        if (nargs + nkwds != 3) {
2120            PyErr_SetString(PyExc_TypeError,
2121                            "type() takes 1 or 3 arguments");
2122            return NULL;
2123        }
2124    }
2125
2126    /* Check arguments: (name, bases, dict) */
2127    if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
2128                                     &name,
2129                                     &PyTuple_Type, &bases,
2130                                     &PyDict_Type, &dict))
2131        return NULL;
2132
2133    /* Determine the proper metatype to deal with this,
2134       and check for metatype conflicts while we're at it.
2135       Note that if some other metatype wins to contract,
2136       it's possible that its instances are not types. */
2137    nbases = PyTuple_GET_SIZE(bases);
2138    winner = metatype;
2139    for (i = 0; i < nbases; i++) {
2140        tmp = PyTuple_GET_ITEM(bases, i);
2141        tmptype = tmp->ob_type;
2142        if (tmptype == &PyClass_Type)
2143            continue; /* Special case classic classes */
2144        if (PyType_IsSubtype(winner, tmptype))
2145            continue;
2146        if (PyType_IsSubtype(tmptype, winner)) {
2147            winner = tmptype;
2148            continue;
2149        }
2150        PyErr_SetString(PyExc_TypeError,
2151                        "metaclass conflict: "
2152                        "the metaclass of a derived class "
2153                        "must be a (non-strict) subclass "
2154                        "of the metaclasses of all its bases");
2155        return NULL;
2156    }
2157    if (winner != metatype) {
2158        if (winner->tp_new != type_new) /* Pass it to the winner */
2159            return winner->tp_new(winner, args, kwds);
2160        metatype = winner;
2161    }
2162
2163    /* Adjust for empty tuple bases */
2164    if (nbases == 0) {
2165        bases = PyTuple_Pack(1, &PyBaseObject_Type);
2166        if (bases == NULL)
2167            return NULL;
2168        nbases = 1;
2169    }
2170    else
2171        Py_INCREF(bases);
2172
2173    /* XXX From here until type is allocated, "return NULL" leaks bases! */
2174
2175    /* Calculate best base, and check that all bases are type objects */
2176    base = best_base(bases);
2177    if (base == NULL) {
2178        Py_DECREF(bases);
2179        return NULL;
2180    }
2181
2182    /* Check for a __slots__ sequence variable in dict, and count it */
2183    slots = PyDict_GetItemString(dict, "__slots__");
2184    nslots = 0;
2185    add_dict = 0;
2186    add_weak = 0;
2187    may_add_dict = base->tp_dictoffset == 0;
2188    may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
2189    if (slots == NULL) {
2190        if (may_add_dict) {
2191            add_dict++;
2192        }
2193        if (may_add_weak) {
2194            add_weak++;
2195        }
2196    }
2197    else {
2198        /* Have slots */
2199
2200        /* Make it into a tuple */
2201        if (PyString_Check(slots) || PyUnicode_Check(slots))
2202            slots = PyTuple_Pack(1, slots);
2203        else
2204            slots = PySequence_Tuple(slots);
2205        if (slots == NULL) {
2206            Py_DECREF(bases);
2207            return NULL;
2208        }
2209        assert(PyTuple_Check(slots));
2210
2211        /* Are slots allowed? */
2212        nslots = PyTuple_GET_SIZE(slots);
2213        if (nslots > 0 && base->tp_itemsize != 0) {
2214            PyErr_Format(PyExc_TypeError,
2215                         "nonempty __slots__ "
2216                         "not supported for subtype of '%s'",
2217                         base->tp_name);
2218          bad_slots:
2219            Py_DECREF(bases);
2220            Py_DECREF(slots);
2221            return NULL;
2222        }
2223
2224#ifdef Py_USING_UNICODE
2225        tmp = _unicode_to_string(slots, nslots);
2226        if (tmp == NULL)
2227            goto bad_slots;
2228        if (tmp != slots) {
2229            Py_DECREF(slots);
2230            slots = tmp;
2231        }
2232#endif
2233        /* Check for valid slot names and two special cases */
2234        for (i = 0; i < nslots; i++) {
2235            PyObject *tmp = PyTuple_GET_ITEM(slots, i);
2236            char *s;
2237            if (!valid_identifier(tmp))
2238                goto bad_slots;
2239            assert(PyString_Check(tmp));
2240            s = PyString_AS_STRING(tmp);
2241            if (strcmp(s, "__dict__") == 0) {
2242                if (!may_add_dict || add_dict) {
2243                    PyErr_SetString(PyExc_TypeError,
2244                        "__dict__ slot disallowed: "
2245                        "we already got one");
2246                    goto bad_slots;
2247                }
2248                add_dict++;
2249            }
2250            if (strcmp(s, "__weakref__") == 0) {
2251                if (!may_add_weak || add_weak) {
2252                    PyErr_SetString(PyExc_TypeError,
2253                        "__weakref__ slot disallowed: "
2254                        "either we already got one, "
2255                        "or __itemsize__ != 0");
2256                    goto bad_slots;
2257                }
2258                add_weak++;
2259            }
2260        }
2261
2262        /* Copy slots into a list, mangle names and sort them.
2263           Sorted names are needed for __class__ assignment.
2264           Convert them back to tuple at the end.
2265        */
2266        newslots = PyList_New(nslots - add_dict - add_weak);
2267        if (newslots == NULL)
2268            goto bad_slots;
2269        for (i = j = 0; i < nslots; i++) {
2270            char *s;
2271            tmp = PyTuple_GET_ITEM(slots, i);
2272            s = PyString_AS_STRING(tmp);
2273            if ((add_dict && strcmp(s, "__dict__") == 0) ||
2274                (add_weak && strcmp(s, "__weakref__") == 0))
2275                continue;
2276            tmp =_Py_Mangle(name, tmp);
2277            if (!tmp) {
2278                Py_DECREF(newslots);
2279                goto bad_slots;
2280            }
2281            PyList_SET_ITEM(newslots, j, tmp);
2282            j++;
2283        }
2284        assert(j == nslots - add_dict - add_weak);
2285        nslots = j;
2286        Py_DECREF(slots);
2287        if (PyList_Sort(newslots) == -1) {
2288            Py_DECREF(bases);
2289            Py_DECREF(newslots);
2290            return NULL;
2291        }
2292        slots = PyList_AsTuple(newslots);
2293        Py_DECREF(newslots);
2294        if (slots == NULL) {
2295            Py_DECREF(bases);
2296            return NULL;
2297        }
2298
2299        /* Secondary bases may provide weakrefs or dict */
2300        if (nbases > 1 &&
2301            ((may_add_dict && !add_dict) ||
2302             (may_add_weak && !add_weak))) {
2303            for (i = 0; i < nbases; i++) {
2304                tmp = PyTuple_GET_ITEM(bases, i);
2305                if (tmp == (PyObject *)base)
2306                    continue; /* Skip primary base */
2307                if (PyClass_Check(tmp)) {
2308                    /* Classic base class provides both */
2309                    if (may_add_dict && !add_dict)
2310                        add_dict++;
2311                    if (may_add_weak && !add_weak)
2312                        add_weak++;
2313                    break;
2314                }
2315                assert(PyType_Check(tmp));
2316                tmptype = (PyTypeObject *)tmp;
2317                if (may_add_dict && !add_dict &&
2318                    tmptype->tp_dictoffset != 0)
2319                    add_dict++;
2320                if (may_add_weak && !add_weak &&
2321                    tmptype->tp_weaklistoffset != 0)
2322                    add_weak++;
2323                if (may_add_dict && !add_dict)
2324                    continue;
2325                if (may_add_weak && !add_weak)
2326                    continue;
2327                /* Nothing more to check */
2328                break;
2329            }
2330        }
2331    }
2332
2333    /* XXX From here until type is safely allocated,
2334       "return NULL" may leak slots! */
2335
2336    /* Allocate the type object */
2337    type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
2338    if (type == NULL) {
2339        Py_XDECREF(slots);
2340        Py_DECREF(bases);
2341        return NULL;
2342    }
2343
2344    /* Keep name and slots alive in the extended type object */
2345    et = (PyHeapTypeObject *)type;
2346    Py_INCREF(name);
2347    et->ht_name = name;
2348    et->ht_slots = slots;
2349
2350    /* Initialize tp_flags */
2351    type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2352        Py_TPFLAGS_BASETYPE;
2353    if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2354        type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2355    if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
2356        type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
2357
2358    /* It's a new-style number unless it specifically inherits any
2359       old-style numeric behavior */
2360    if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
2361        (base->tp_as_number == NULL))
2362        type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
2363
2364    /* Initialize essential fields */
2365    type->tp_as_number = &et->as_number;
2366    type->tp_as_sequence = &et->as_sequence;
2367    type->tp_as_mapping = &et->as_mapping;
2368    type->tp_as_buffer = &et->as_buffer;
2369    type->tp_name = PyString_AS_STRING(name);
2370    if (!type->tp_name) {
2371        Py_DECREF(bases);
2372        Py_DECREF(type);
2373        return NULL;
2374    }
2375    if (strlen(type->tp_name) != (size_t)PyString_GET_SIZE(name)) {
2376        PyErr_SetString(PyExc_ValueError,
2377                        "type name must not contain null characters");
2378        Py_DECREF(bases);
2379        Py_DECREF(type);
2380        return NULL;
2381    }
2382
2383    /* Set tp_base and tp_bases */
2384    type->tp_bases = bases;
2385    Py_INCREF(base);
2386    type->tp_base = base;
2387
2388    /* Initialize tp_dict from passed-in dict */
2389    type->tp_dict = dict = PyDict_Copy(dict);
2390    if (dict == NULL) {
2391        Py_DECREF(type);
2392        return NULL;
2393    }
2394
2395    /* Set __module__ in the dict */
2396    if (PyDict_GetItemString(dict, "__module__") == NULL) {
2397        tmp = PyEval_GetGlobals();
2398        if (tmp != NULL) {
2399            tmp = PyDict_GetItemString(tmp, "__name__");
2400            if (tmp != NULL) {
2401                if (PyDict_SetItemString(dict, "__module__",
2402                                         tmp) < 0) {
2403                    Py_DECREF(type);
2404                    return NULL;
2405                }
2406            }
2407        }
2408    }
2409
2410    /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2411       and is a string.  The __doc__ accessor will first look for tp_doc;
2412       if that fails, it will still look into __dict__.
2413    */
2414    {
2415        PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2416        if (doc != NULL && PyString_Check(doc)) {
2417            const size_t n = (size_t)PyString_GET_SIZE(doc);
2418            char *tp_doc = (char *)PyObject_MALLOC(n+1);
2419            if (tp_doc == NULL) {
2420                Py_DECREF(type);
2421                return NULL;
2422            }
2423            memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
2424            type->tp_doc = tp_doc;
2425        }
2426    }
2427
2428    /* Special-case __new__: if it's a plain function,
2429       make it a static function */
2430    tmp = PyDict_GetItemString(dict, "__new__");
2431    if (tmp != NULL && PyFunction_Check(tmp)) {
2432        tmp = PyStaticMethod_New(tmp);
2433        if (tmp == NULL) {
2434            Py_DECREF(type);
2435            return NULL;
2436        }
2437        if (PyDict_SetItemString(dict, "__new__", tmp) < 0) {
2438            Py_DECREF(tmp);
2439            Py_DECREF(type);
2440            return NULL;
2441        }
2442        Py_DECREF(tmp);
2443    }
2444
2445    /* Add descriptors for custom slots from __slots__, or for __dict__ */
2446    mp = PyHeapType_GET_MEMBERS(et);
2447    slotoffset = base->tp_basicsize;
2448    if (slots != NULL) {
2449        for (i = 0; i < nslots; i++, mp++) {
2450            mp->name = PyString_AS_STRING(
2451                PyTuple_GET_ITEM(slots, i));
2452            mp->type = T_OBJECT_EX;
2453            mp->offset = slotoffset;
2454
2455            /* __dict__ and __weakref__ are already filtered out */
2456            assert(strcmp(mp->name, "__dict__") != 0);
2457            assert(strcmp(mp->name, "__weakref__") != 0);
2458
2459            slotoffset += sizeof(PyObject *);
2460        }
2461    }
2462    if (add_dict) {
2463        if (base->tp_itemsize)
2464            type->tp_dictoffset = -(long)sizeof(PyObject *);
2465        else
2466            type->tp_dictoffset = slotoffset;
2467        slotoffset += sizeof(PyObject *);
2468    }
2469    if (add_weak) {
2470        assert(!base->tp_itemsize);
2471        type->tp_weaklistoffset = slotoffset;
2472        slotoffset += sizeof(PyObject *);
2473    }
2474    type->tp_basicsize = slotoffset;
2475    type->tp_itemsize = base->tp_itemsize;
2476    type->tp_members = PyHeapType_GET_MEMBERS(et);
2477
2478    if (type->tp_weaklistoffset && type->tp_dictoffset)
2479        type->tp_getset = subtype_getsets_full;
2480    else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2481        type->tp_getset = subtype_getsets_weakref_only;
2482    else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2483        type->tp_getset = subtype_getsets_dict_only;
2484    else
2485        type->tp_getset = NULL;
2486
2487    /* Special case some slots */
2488    if (type->tp_dictoffset != 0 || nslots > 0) {
2489        if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2490            type->tp_getattro = PyObject_GenericGetAttr;
2491        if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2492            type->tp_setattro = PyObject_GenericSetAttr;
2493    }
2494    type->tp_dealloc = subtype_dealloc;
2495
2496    /* Enable GC unless there are really no instance variables possible */
2497    if (!(type->tp_basicsize == sizeof(PyObject) &&
2498          type->tp_itemsize == 0))
2499        type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2500
2501    /* Always override allocation strategy to use regular heap */
2502    type->tp_alloc = PyType_GenericAlloc;
2503    if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2504        type->tp_free = PyObject_GC_Del;
2505        type->tp_traverse = subtype_traverse;
2506        type->tp_clear = subtype_clear;
2507    }
2508    else
2509        type->tp_free = PyObject_Del;
2510
2511    /* Initialize the rest */
2512    if (PyType_Ready(type) < 0) {
2513        Py_DECREF(type);
2514        return NULL;
2515    }
2516
2517    /* Put the proper slots in place */
2518    fixup_slot_dispatchers(type);
2519
2520    return (PyObject *)type;
2521}
2522
2523/* Internal API to look for a name through the MRO.
2524   This returns a borrowed reference, and doesn't set an exception! */
2525PyObject *
2526_PyType_Lookup(PyTypeObject *type, PyObject *name)
2527{
2528    Py_ssize_t i, n;
2529    PyObject *mro, *res, *base, *dict;
2530    unsigned int h;
2531
2532    if (MCACHE_CACHEABLE_NAME(name) &&
2533        PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
2534        /* fast path */
2535        h = MCACHE_HASH_METHOD(type, name);
2536        if (method_cache[h].version == type->tp_version_tag &&
2537            method_cache[h].name == name) {
2538#if MCACHE_STATS
2539            method_cache_hits++;
2540#endif
2541            return method_cache[h].value;
2542        }
2543    }
2544
2545    /* Look in tp_dict of types in MRO */
2546    mro = type->tp_mro;
2547
2548    if (mro == NULL) {
2549        if ((type->tp_flags & Py_TPFLAGS_READYING) == 0 &&
2550            PyType_Ready(type) < 0) {
2551            /* It's not ideal to clear the error condition,
2552               but this function is documented as not setting
2553               an exception, and I don't want to change that.
2554               When PyType_Ready() can't proceed, it won't
2555               set the "ready" flag, so future attempts to ready
2556               the same type will call it again -- hopefully
2557               in a context that propagates the exception out.
2558            */
2559            PyErr_Clear();
2560            return NULL;
2561        }
2562        mro = type->tp_mro;
2563        if (mro == NULL) {
2564            return NULL;
2565        }
2566    }
2567
2568    res = NULL;
2569    assert(PyTuple_Check(mro));
2570    n = PyTuple_GET_SIZE(mro);
2571    for (i = 0; i < n; i++) {
2572        base = PyTuple_GET_ITEM(mro, i);
2573        if (PyClass_Check(base))
2574            dict = ((PyClassObject *)base)->cl_dict;
2575        else {
2576            assert(PyType_Check(base));
2577            dict = ((PyTypeObject *)base)->tp_dict;
2578        }
2579        assert(dict && PyDict_Check(dict));
2580        res = PyDict_GetItem(dict, name);
2581        if (res != NULL)
2582            break;
2583    }
2584
2585    if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2586        h = MCACHE_HASH_METHOD(type, name);
2587        method_cache[h].version = type->tp_version_tag;
2588        method_cache[h].value = res;  /* borrowed */
2589        Py_INCREF(name);
2590        assert(((PyStringObject *)(name))->ob_shash != -1);
2591#if MCACHE_STATS
2592        if (method_cache[h].name != Py_None && method_cache[h].name != name)
2593            method_cache_collisions++;
2594        else
2595            method_cache_misses++;
2596#endif
2597        Py_DECREF(method_cache[h].name);
2598        method_cache[h].name = name;
2599    }
2600    return res;
2601}
2602
2603/* This is similar to PyObject_GenericGetAttr(),
2604   but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2605static PyObject *
2606type_getattro(PyTypeObject *type, PyObject *name)
2607{
2608    PyTypeObject *metatype = Py_TYPE(type);
2609    PyObject *meta_attribute, *attribute;
2610    descrgetfunc meta_get;
2611
2612    if (!PyString_Check(name)) {
2613        PyErr_Format(PyExc_TypeError,
2614                     "attribute name must be string, not '%.200s'",
2615                     name->ob_type->tp_name);
2616        return NULL;
2617    }
2618
2619    /* Initialize this type (we'll assume the metatype is initialized) */
2620    if (type->tp_dict == NULL) {
2621        if (PyType_Ready(type) < 0)
2622            return NULL;
2623    }
2624
2625    /* No readable descriptor found yet */
2626    meta_get = NULL;
2627
2628    /* Look for the attribute in the metatype */
2629    meta_attribute = _PyType_Lookup(metatype, name);
2630
2631    if (meta_attribute != NULL) {
2632        meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
2633
2634        if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2635            /* Data descriptors implement tp_descr_set to intercept
2636             * writes. Assume the attribute is not overridden in
2637             * type's tp_dict (and bases): call the descriptor now.
2638             */
2639            return meta_get(meta_attribute, (PyObject *)type,
2640                            (PyObject *)metatype);
2641        }
2642        Py_INCREF(meta_attribute);
2643    }
2644
2645    /* No data descriptor found on metatype. Look in tp_dict of this
2646     * type and its bases */
2647    attribute = _PyType_Lookup(type, name);
2648    if (attribute != NULL) {
2649        /* Implement descriptor functionality, if any */
2650        descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
2651
2652        Py_XDECREF(meta_attribute);
2653
2654        if (local_get != NULL) {
2655            /* NULL 2nd argument indicates the descriptor was
2656             * found on the target object itself (or a base)  */
2657            return local_get(attribute, (PyObject *)NULL,
2658                             (PyObject *)type);
2659        }
2660
2661        Py_INCREF(attribute);
2662        return attribute;
2663    }
2664
2665    /* No attribute found in local __dict__ (or bases): use the
2666     * descriptor from the metatype, if any */
2667    if (meta_get != NULL) {
2668        PyObject *res;
2669        res = meta_get(meta_attribute, (PyObject *)type,
2670                       (PyObject *)metatype);
2671        Py_DECREF(meta_attribute);
2672        return res;
2673    }
2674
2675    /* If an ordinary attribute was found on the metatype, return it now */
2676    if (meta_attribute != NULL) {
2677        return meta_attribute;
2678    }
2679
2680    /* Give up */
2681    PyErr_Format(PyExc_AttributeError,
2682                     "type object '%.50s' has no attribute '%.400s'",
2683                     type->tp_name, PyString_AS_STRING(name));
2684    return NULL;
2685}
2686
2687static int
2688type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2689{
2690    if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2691        PyErr_Format(
2692            PyExc_TypeError,
2693            "can't set attributes of built-in/extension type '%s'",
2694            type->tp_name);
2695        return -1;
2696    }
2697    if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2698        return -1;
2699    return update_slot(type, name);
2700}
2701
2702static void
2703type_dealloc(PyTypeObject *type)
2704{
2705    PyHeapTypeObject *et;
2706
2707    /* Assert this is a heap-allocated type object */
2708    assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2709    _PyObject_GC_UNTRACK(type);
2710    PyObject_ClearWeakRefs((PyObject *)type);
2711    et = (PyHeapTypeObject *)type;
2712    Py_XDECREF(type->tp_base);
2713    Py_XDECREF(type->tp_dict);
2714    Py_XDECREF(type->tp_bases);
2715    Py_XDECREF(type->tp_mro);
2716    Py_XDECREF(type->tp_cache);
2717    Py_XDECREF(type->tp_subclasses);
2718    /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2719     * of most other objects.  It's okay to cast it to char *.
2720     */
2721    PyObject_Free((char *)type->tp_doc);
2722    Py_XDECREF(et->ht_name);
2723    Py_XDECREF(et->ht_slots);
2724    Py_TYPE(type)->tp_free((PyObject *)type);
2725}
2726
2727static PyObject *
2728type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2729{
2730    PyObject *list, *raw, *ref;
2731    Py_ssize_t i, n;
2732
2733    list = PyList_New(0);
2734    if (list == NULL)
2735        return NULL;
2736    raw = type->tp_subclasses;
2737    if (raw == NULL)
2738        return list;
2739    assert(PyList_Check(raw));
2740    n = PyList_GET_SIZE(raw);
2741    for (i = 0; i < n; i++) {
2742        ref = PyList_GET_ITEM(raw, i);
2743        assert(PyWeakref_CheckRef(ref));
2744        ref = PyWeakref_GET_OBJECT(ref);
2745        if (ref != Py_None) {
2746            if (PyList_Append(list, ref) < 0) {
2747                Py_DECREF(list);
2748                return NULL;
2749            }
2750        }
2751    }
2752    return list;
2753}
2754
2755static PyMethodDef type_methods[] = {
2756    {"mro", (PyCFunction)mro_external, METH_NOARGS,
2757     PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2758    {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2759     PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2760    {"__instancecheck__", type___instancecheck__, METH_O,
2761     PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")},
2762    {"__subclasscheck__", type___subclasscheck__, METH_O,
2763     PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},
2764    {0}
2765};
2766
2767PyDoc_STRVAR(type_doc,
2768"type(object) -> the object's type\n"
2769"type(name, bases, dict) -> a new type");
2770
2771static int
2772type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2773{
2774    /* Because of type_is_gc(), the collector only calls this
2775       for heaptypes. */
2776    assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2777
2778    Py_VISIT(type->tp_dict);
2779    Py_VISIT(type->tp_cache);
2780    Py_VISIT(type->tp_mro);
2781    Py_VISIT(type->tp_bases);
2782    Py_VISIT(type->tp_base);
2783
2784    /* There's no need to visit type->tp_subclasses or
2785       ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2786       in cycles; tp_subclasses is a list of weak references,
2787       and slots is a tuple of strings. */
2788
2789    return 0;
2790}
2791
2792static int
2793type_clear(PyTypeObject *type)
2794{
2795    /* Because of type_is_gc(), the collector only calls this
2796       for heaptypes. */
2797    assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2798
2799    /* We need to invalidate the method cache carefully before clearing
2800       the dict, so that other objects caught in a reference cycle
2801       don't start calling destroyed methods.
2802
2803       Otherwise, the only field we need to clear is tp_mro, which is
2804       part of a hard cycle (its first element is the class itself) that
2805       won't be broken otherwise (it's a tuple and tuples don't have a
2806       tp_clear handler).  None of the other fields need to be
2807       cleared, and here's why:
2808
2809       tp_cache:
2810           Not used; if it were, it would be a dict.
2811
2812       tp_bases, tp_base:
2813           If these are involved in a cycle, there must be at least
2814           one other, mutable object in the cycle, e.g. a base
2815           class's dict; the cycle will be broken that way.
2816
2817       tp_subclasses:
2818           A list of weak references can't be part of a cycle; and
2819           lists have their own tp_clear.
2820
2821       slots (in PyHeapTypeObject):
2822           A tuple of strings can't be part of a cycle.
2823    */
2824
2825    PyType_Modified(type);
2826    if (type->tp_dict)
2827        PyDict_Clear(type->tp_dict);
2828    Py_CLEAR(type->tp_mro);
2829
2830    return 0;
2831}
2832
2833static int
2834type_is_gc(PyTypeObject *type)
2835{
2836    return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2837}
2838
2839PyTypeObject PyType_Type = {
2840    PyVarObject_HEAD_INIT(&PyType_Type, 0)
2841    "type",                                     /* tp_name */
2842    sizeof(PyHeapTypeObject),                   /* tp_basicsize */
2843    sizeof(PyMemberDef),                        /* tp_itemsize */
2844    (destructor)type_dealloc,                   /* tp_dealloc */
2845    0,                                          /* tp_print */
2846    0,                                          /* tp_getattr */
2847    0,                                          /* tp_setattr */
2848    0,                                  /* tp_compare */
2849    (reprfunc)type_repr,                        /* tp_repr */
2850    0,                                          /* tp_as_number */
2851    0,                                          /* tp_as_sequence */
2852    0,                                          /* tp_as_mapping */
2853    (hashfunc)_Py_HashPointer,                  /* tp_hash */
2854    (ternaryfunc)type_call,                     /* tp_call */
2855    0,                                          /* tp_str */
2856    (getattrofunc)type_getattro,                /* tp_getattro */
2857    (setattrofunc)type_setattro,                /* tp_setattro */
2858    0,                                          /* tp_as_buffer */
2859    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2860        Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS,         /* tp_flags */
2861    type_doc,                                   /* tp_doc */
2862    (traverseproc)type_traverse,                /* tp_traverse */
2863    (inquiry)type_clear,                        /* tp_clear */
2864    type_richcompare,                                           /* tp_richcompare */
2865    offsetof(PyTypeObject, tp_weaklist),        /* tp_weaklistoffset */
2866    0,                                          /* tp_iter */
2867    0,                                          /* tp_iternext */
2868    type_methods,                               /* tp_methods */
2869    type_members,                               /* tp_members */
2870    type_getsets,                               /* tp_getset */
2871    0,                                          /* tp_base */
2872    0,                                          /* tp_dict */
2873    0,                                          /* tp_descr_get */
2874    0,                                          /* tp_descr_set */
2875    offsetof(PyTypeObject, tp_dict),            /* tp_dictoffset */
2876    type_init,                                  /* tp_init */
2877    0,                                          /* tp_alloc */
2878    type_new,                                   /* tp_new */
2879    PyObject_GC_Del,                            /* tp_free */
2880    (inquiry)type_is_gc,                        /* tp_is_gc */
2881};
2882
2883
2884/* The base type of all types (eventually)... except itself. */
2885
2886/* You may wonder why object.__new__() only complains about arguments
2887   when object.__init__() is not overridden, and vice versa.
2888
2889   Consider the use cases:
2890
2891   1. When neither is overridden, we want to hear complaints about
2892      excess (i.e., any) arguments, since their presence could
2893      indicate there's a bug.
2894
2895   2. When defining an Immutable type, we are likely to override only
2896      __new__(), since __init__() is called too late to initialize an
2897      Immutable object.  Since __new__() defines the signature for the
2898      type, it would be a pain to have to override __init__() just to
2899      stop it from complaining about excess arguments.
2900
2901   3. When defining a Mutable type, we are likely to override only
2902      __init__().  So here the converse reasoning applies: we don't
2903      want to have to override __new__() just to stop it from
2904      complaining.
2905
2906   4. When __init__() is overridden, and the subclass __init__() calls
2907      object.__init__(), the latter should complain about excess
2908      arguments; ditto for __new__().
2909
2910   Use cases 2 and 3 make it unattractive to unconditionally check for
2911   excess arguments.  The best solution that addresses all four use
2912   cases is as follows: __init__() complains about excess arguments
2913   unless __new__() is overridden and __init__() is not overridden
2914   (IOW, if __init__() is overridden or __new__() is not overridden);
2915   symmetrically, __new__() complains about excess arguments unless
2916   __init__() is overridden and __new__() is not overridden
2917   (IOW, if __new__() is overridden or __init__() is not overridden).
2918
2919   However, for backwards compatibility, this breaks too much code.
2920   Therefore, in 2.6, we'll *warn* about excess arguments when both
2921   methods are overridden; for all other cases we'll use the above
2922   rules.
2923
2924*/
2925
2926/* Forward */
2927static PyObject *
2928object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2929
2930static int
2931excess_args(PyObject *args, PyObject *kwds)
2932{
2933    return PyTuple_GET_SIZE(args) ||
2934        (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2935}
2936
2937static int
2938object_init(PyObject *self, PyObject *args, PyObject *kwds)
2939{
2940    int err = 0;
2941    if (excess_args(args, kwds)) {
2942        PyTypeObject *type = Py_TYPE(self);
2943        if (type->tp_init != object_init &&
2944            type->tp_new != object_new)
2945        {
2946            err = PyErr_WarnEx(PyExc_DeprecationWarning,
2947                       "object.__init__() takes no parameters",
2948                       1);
2949        }
2950        else if (type->tp_init != object_init ||
2951                 type->tp_new == object_new)
2952        {
2953            PyErr_SetString(PyExc_TypeError,
2954                "object.__init__() takes no parameters");
2955            err = -1;
2956        }
2957    }
2958    return err;
2959}
2960
2961static PyObject *
2962object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2963{
2964    int err = 0;
2965    if (excess_args(args, kwds)) {
2966        if (type->tp_new != object_new &&
2967            type->tp_init != object_init)
2968        {
2969            err = PyErr_WarnEx(PyExc_DeprecationWarning,
2970                       "object() takes no parameters",
2971                       1);
2972        }
2973        else if (type->tp_new != object_new ||
2974                 type->tp_init == object_init)
2975        {
2976            PyErr_SetString(PyExc_TypeError,
2977                "object() takes no parameters");
2978            err = -1;
2979        }
2980    }
2981    if (err < 0)
2982        return NULL;
2983
2984    if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2985        static PyObject *comma = NULL;
2986        PyObject *abstract_methods = NULL;
2987        PyObject *builtins;
2988        PyObject *sorted;
2989        PyObject *sorted_methods = NULL;
2990        PyObject *joined = NULL;
2991        const char *joined_str;
2992
2993        /* Compute ", ".join(sorted(type.__abstractmethods__))
2994           into joined. */
2995        abstract_methods = type_abstractmethods(type, NULL);
2996        if (abstract_methods == NULL)
2997            goto error;
2998        builtins = PyEval_GetBuiltins();
2999        if (builtins == NULL)
3000            goto error;
3001        sorted = PyDict_GetItemString(builtins, "sorted");
3002        if (sorted == NULL)
3003            goto error;
3004        sorted_methods = PyObject_CallFunctionObjArgs(sorted,
3005                                                      abstract_methods,
3006                                                      NULL);
3007        if (sorted_methods == NULL)
3008            goto error;
3009        if (comma == NULL) {
3010            comma = PyString_InternFromString(", ");
3011            if (comma == NULL)
3012                goto error;
3013        }
3014        joined = PyObject_CallMethod(comma, "join",
3015                                     "O",  sorted_methods);
3016        if (joined == NULL)
3017            goto error;
3018        joined_str = PyString_AsString(joined);
3019        if (joined_str == NULL)
3020            goto error;
3021
3022        PyErr_Format(PyExc_TypeError,
3023                     "Can't instantiate abstract class %s "
3024                     "with abstract methods %s",
3025                     type->tp_name,
3026                     joined_str);
3027    error:
3028        Py_XDECREF(joined);
3029        Py_XDECREF(sorted_methods);
3030        Py_XDECREF(abstract_methods);
3031        return NULL;
3032    }
3033    return type->tp_alloc(type, 0);
3034}
3035
3036static void
3037object_dealloc(PyObject *self)
3038{
3039    Py_TYPE(self)->tp_free(self);
3040}
3041
3042static PyObject *
3043object_repr(PyObject *self)
3044{
3045    PyTypeObject *type;
3046    PyObject *mod, *name, *rtn;
3047
3048    type = Py_TYPE(self);
3049    mod = type_module(type, NULL);
3050    if (mod == NULL)
3051        PyErr_Clear();
3052    else if (!PyString_Check(mod)) {
3053        Py_DECREF(mod);
3054        mod = NULL;
3055    }
3056    name = type_name(type, NULL);
3057    if (name == NULL) {
3058        Py_XDECREF(mod);
3059        return NULL;
3060    }
3061    if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
3062        rtn = PyString_FromFormat("<%s.%s object at %p>",
3063                                  PyString_AS_STRING(mod),
3064                                  PyString_AS_STRING(name),
3065                                  self);
3066    else
3067        rtn = PyString_FromFormat("<%s object at %p>",
3068                                  type->tp_name, self);
3069    Py_XDECREF(mod);
3070    Py_DECREF(name);
3071    return rtn;
3072}
3073
3074static PyObject *
3075object_str(PyObject *self)
3076{
3077    unaryfunc f;
3078
3079    f = Py_TYPE(self)->tp_repr;
3080    if (f == NULL)
3081        f = object_repr;
3082    return f(self);
3083}
3084
3085static PyObject *
3086object_get_class(PyObject *self, void *closure)
3087{
3088    Py_INCREF(Py_TYPE(self));
3089    return (PyObject *)(Py_TYPE(self));
3090}
3091
3092static int
3093equiv_structs(PyTypeObject *a, PyTypeObject *b)
3094{
3095    return a == b ||
3096           (a != NULL &&
3097        b != NULL &&
3098        a->tp_basicsize == b->tp_basicsize &&
3099        a->tp_itemsize == b->tp_itemsize &&
3100        a->tp_dictoffset == b->tp_dictoffset &&
3101        a->tp_weaklistoffset == b->tp_weaklistoffset &&
3102        ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3103         (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
3104}
3105
3106static int
3107same_slots_added(PyTypeObject *a, PyTypeObject *b)
3108{
3109    PyTypeObject *base = a->tp_base;
3110    Py_ssize_t size;
3111    PyObject *slots_a, *slots_b;
3112
3113    assert(base == b->tp_base);
3114    size = base->tp_basicsize;
3115    if (a->tp_dictoffset == size && b->tp_dictoffset == size)
3116        size += sizeof(PyObject *);
3117    if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
3118        size += sizeof(PyObject *);
3119
3120    /* Check slots compliance */
3121    slots_a = ((PyHeapTypeObject *)a)->ht_slots;
3122    slots_b = ((PyHeapTypeObject *)b)->ht_slots;
3123    if (slots_a && slots_b) {
3124        if (PyObject_Compare(slots_a, slots_b) != 0)
3125            return 0;
3126        size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
3127    }
3128    return size == a->tp_basicsize && size == b->tp_basicsize;
3129}
3130
3131static int
3132compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
3133{
3134    PyTypeObject *newbase, *oldbase;
3135
3136    if (newto->tp_dealloc != oldto->tp_dealloc ||
3137        newto->tp_free != oldto->tp_free)
3138    {
3139        PyErr_Format(PyExc_TypeError,
3140                     "%s assignment: "
3141                     "'%s' deallocator differs from '%s'",
3142                     attr,
3143                     newto->tp_name,
3144                     oldto->tp_name);
3145        return 0;
3146    }
3147    newbase = newto;
3148    oldbase = oldto;
3149    while (equiv_structs(newbase, newbase->tp_base))
3150        newbase = newbase->tp_base;
3151    while (equiv_structs(oldbase, oldbase->tp_base))
3152        oldbase = oldbase->tp_base;
3153    if (newbase != oldbase &&
3154        (newbase->tp_base != oldbase->tp_base ||
3155         !same_slots_added(newbase, oldbase))) {
3156        PyErr_Format(PyExc_TypeError,
3157                     "%s assignment: "
3158                     "'%s' object layout differs from '%s'",
3159                     attr,
3160                     newto->tp_name,
3161                     oldto->tp_name);
3162        return 0;
3163    }
3164
3165    return 1;
3166}
3167
3168static int
3169object_set_class(PyObject *self, PyObject *value, void *closure)
3170{
3171    PyTypeObject *oldto = Py_TYPE(self);
3172    PyTypeObject *newto;
3173
3174    if (value == NULL) {
3175        PyErr_SetString(PyExc_TypeError,
3176                        "can't delete __class__ attribute");
3177        return -1;
3178    }
3179    if (!PyType_Check(value)) {
3180        PyErr_Format(PyExc_TypeError,
3181          "__class__ must be set to new-style class, not '%s' object",
3182          Py_TYPE(value)->tp_name);
3183        return -1;
3184    }
3185    newto = (PyTypeObject *)value;
3186    if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
3187        !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
3188    {
3189        PyErr_Format(PyExc_TypeError,
3190                     "__class__ assignment: only for heap types");
3191        return -1;
3192    }
3193    if (compatible_for_assignment(newto, oldto, "__class__")) {
3194        Py_INCREF(newto);
3195        Py_TYPE(self) = newto;
3196        Py_DECREF(oldto);
3197        return 0;
3198    }
3199    else {
3200        return -1;
3201    }
3202}
3203
3204static PyGetSetDef object_getsets[] = {
3205    {"__class__", object_get_class, object_set_class,
3206     PyDoc_STR("the object's class")},
3207    {0}
3208};
3209
3210
3211/* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
3212   We fall back to helpers in copy_reg for:
3213   - pickle protocols < 2
3214   - calculating the list of slot names (done only once per class)
3215   - the __newobj__ function (which is used as a token but never called)
3216*/
3217
3218static PyObject *
3219import_copyreg(void)
3220{
3221    static PyObject *copyreg_str;
3222
3223    if (!copyreg_str) {
3224        copyreg_str = PyString_InternFromString("copy_reg");
3225        if (copyreg_str == NULL)
3226            return NULL;
3227    }
3228
3229    return PyImport_Import(copyreg_str);
3230}
3231
3232static PyObject *
3233slotnames(PyObject *cls)
3234{
3235    PyObject *clsdict;
3236    PyObject *copyreg;
3237    PyObject *slotnames;
3238
3239    if (!PyType_Check(cls)) {
3240        Py_INCREF(Py_None);
3241        return Py_None;
3242    }
3243
3244    clsdict = ((PyTypeObject *)cls)->tp_dict;
3245    slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
3246    if (slotnames != NULL && PyList_Check(slotnames)) {
3247        Py_INCREF(slotnames);
3248        return slotnames;
3249    }
3250
3251    copyreg = import_copyreg();
3252    if (copyreg == NULL)
3253        return NULL;
3254
3255    slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3256    Py_DECREF(copyreg);
3257    if (slotnames != NULL &&
3258        slotnames != Py_None &&
3259        !PyList_Check(slotnames))
3260    {
3261        PyErr_SetString(PyExc_TypeError,
3262            "copy_reg._slotnames didn't return a list or None");
3263        Py_DECREF(slotnames);
3264        slotnames = NULL;
3265    }
3266
3267    return slotnames;
3268}
3269
3270static PyObject *
3271reduce_2(PyObject *obj)
3272{
3273    PyObject *cls, *getnewargs;
3274    PyObject *args = NULL, *args2 = NULL;
3275    PyObject *getstate = NULL, *state = NULL, *names = NULL;
3276    PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
3277    PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
3278    Py_ssize_t i, n;
3279    int required_state = 0;
3280
3281    cls = PyObject_GetAttrString(obj, "__class__");
3282    if (cls == NULL)
3283        return NULL;
3284
3285    if (PyType_Check(cls) && ((PyTypeObject *)cls)->tp_new == NULL) {
3286        PyErr_Format(PyExc_TypeError,
3287                     "can't pickle %.200s objects",
3288                     ((PyTypeObject *)cls)->tp_name);
3289        return NULL;
3290    }
3291
3292    getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3293    if (getnewargs != NULL) {
3294        args = PyObject_CallObject(getnewargs, NULL);
3295        Py_DECREF(getnewargs);
3296        if (args == NULL)
3297            goto end;
3298        if (!PyTuple_Check(args)) {
3299            PyErr_Format(PyExc_TypeError,
3300                "__getnewargs__ should return a tuple, "
3301                "not '%.200s'", Py_TYPE(args)->tp_name);
3302            goto end;
3303        }
3304    }
3305    else {
3306        PyErr_Clear();
3307        required_state = !PyList_Check(obj) && !PyDict_Check(obj);
3308    }
3309
3310    getstate = PyObject_GetAttrString(obj, "__getstate__");
3311    if (getstate != NULL) {
3312        state = PyObject_CallObject(getstate, NULL);
3313        Py_DECREF(getstate);
3314        if (state == NULL)
3315            goto end;
3316    }
3317    else {
3318        PyErr_Clear();
3319
3320        if (required_state && obj->ob_type->tp_itemsize) {
3321            PyErr_Format(PyExc_TypeError,
3322                         "can't pickle %.200s objects",
3323                         Py_TYPE(obj)->tp_name);
3324            goto end;
3325        }
3326
3327        state = PyObject_GetAttrString(obj, "__dict__");
3328        if (state == NULL) {
3329            PyErr_Clear();
3330            state = Py_None;
3331            Py_INCREF(state);
3332        }
3333        names = slotnames(cls);
3334        if (names == NULL)
3335            goto end;
3336        assert(names == Py_None || PyList_Check(names));
3337
3338        if (names != Py_None) {
3339            slots = PyDict_New();
3340            if (slots == NULL)
3341                goto end;
3342            n = 0;
3343            /* Can't pre-compute the list size; the list
3344               is stored on the class so accessible to other
3345               threads, which may be run by DECREF */
3346            for (i = 0; i < PyList_GET_SIZE(names); i++) {
3347                PyObject *name, *value;
3348                name = PyList_GET_ITEM(names, i);
3349                Py_INCREF(name);
3350                value = PyObject_GetAttr(obj, name);
3351                if (value == NULL) {
3352                    Py_DECREF(name);
3353                    PyErr_Clear();
3354                }
3355                else {
3356                    int err = PyDict_SetItem(slots, name,
3357                                             value);
3358                    Py_DECREF(name);
3359                    Py_DECREF(value);
3360                    if (err)
3361                        goto end;
3362                    n++;
3363                }
3364            }
3365            if (n) {
3366                state = Py_BuildValue("(NO)", state, slots);
3367                if (state == NULL)
3368                    goto end;
3369            }
3370        }
3371    }
3372
3373    if (!PyList_Check(obj)) {
3374        listitems = Py_None;
3375        Py_INCREF(listitems);
3376    }
3377    else {
3378        listitems = PyObject_GetIter(obj);
3379        if (listitems == NULL)
3380            goto end;
3381    }
3382
3383    if (!PyDict_Check(obj)) {
3384        dictitems = Py_None;
3385        Py_INCREF(dictitems);
3386    }
3387    else {
3388        dictitems = PyObject_CallMethod(obj, "iteritems", "");
3389        if (dictitems == NULL)
3390            goto end;
3391    }
3392
3393    copyreg = import_copyreg();
3394    if (copyreg == NULL)
3395        goto end;
3396    newobj = PyObject_GetAttrString(copyreg, "__newobj__");
3397    if (newobj == NULL)
3398        goto end;
3399
3400    n = args ? PyTuple_GET_SIZE(args) : 0;
3401    args2 = PyTuple_New(n+1);
3402    if (args2 == NULL)
3403        goto end;
3404    PyTuple_SET_ITEM(args2, 0, cls);
3405    cls = NULL;
3406    for (i = 0; i < n; i++) {
3407        PyObject *v = PyTuple_GET_ITEM(args, i);
3408        Py_INCREF(v);
3409        PyTuple_SET_ITEM(args2, i+1, v);
3410    }
3411
3412    res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
3413
3414  end:
3415    Py_XDECREF(cls);
3416    Py_XDECREF(args);
3417    Py_XDECREF(args2);
3418    Py_XDECREF(slots);
3419    Py_XDECREF(state);
3420    Py_XDECREF(names);
3421    Py_XDECREF(listitems);
3422    Py_XDECREF(dictitems);
3423    Py_XDECREF(copyreg);
3424    Py_XDECREF(newobj);
3425    return res;
3426}
3427
3428/*
3429 * There were two problems when object.__reduce__ and object.__reduce_ex__
3430 * were implemented in the same function:
3431 *  - trying to pickle an object with a custom __reduce__ method that
3432 *    fell back to object.__reduce__ in certain circumstances led to
3433 *    infinite recursion at Python level and eventual RuntimeError.
3434 *  - Pickling objects that lied about their type by overwriting the
3435 *    __class__ descriptor could lead to infinite recursion at C level
3436 *    and eventual segfault.
3437 *
3438 * Because of backwards compatibility, the two methods still have to
3439 * behave in the same way, even if this is not required by the pickle
3440 * protocol. This common functionality was moved to the _common_reduce
3441 * function.
3442 */
3443static PyObject *
3444_common_reduce(PyObject *self, int proto)
3445{
3446    PyObject *copyreg, *res;
3447
3448    if (proto >= 2)
3449        return reduce_2(self);
3450
3451    copyreg = import_copyreg();
3452    if (!copyreg)
3453        return NULL;
3454
3455    res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3456    Py_DECREF(copyreg);
3457
3458    return res;
3459}
3460
3461static PyObject *
3462object_reduce(PyObject *self, PyObject *args)
3463{
3464    int proto = 0;
3465
3466    if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3467        return NULL;
3468
3469    return _common_reduce(self, proto);
3470}
3471
3472static PyObject *
3473object_reduce_ex(PyObject *self, PyObject *args)
3474{
3475    PyObject *reduce, *res;
3476    int proto = 0;
3477
3478    if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3479        return NULL;
3480
3481    reduce = PyObject_GetAttrString(self, "__reduce__");
3482    if (reduce == NULL)
3483        PyErr_Clear();
3484    else {
3485        PyObject *cls, *clsreduce, *objreduce;
3486        int override;
3487        cls = PyObject_GetAttrString(self, "__class__");
3488        if (cls == NULL) {
3489            Py_DECREF(reduce);
3490            return NULL;
3491        }
3492        clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3493        Py_DECREF(cls);
3494        if (clsreduce == NULL) {
3495            Py_DECREF(reduce);
3496            return NULL;
3497        }
3498        objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3499                                         "__reduce__");
3500        override = (clsreduce != objreduce);
3501        Py_DECREF(clsreduce);
3502        if (override) {
3503            res = PyObject_CallObject(reduce, NULL);
3504            Py_DECREF(reduce);
3505            return res;
3506        }
3507        else
3508            Py_DECREF(reduce);
3509    }
3510
3511    return _common_reduce(self, proto);
3512}
3513
3514static PyObject *
3515object_subclasshook(PyObject *cls, PyObject *args)
3516{
3517    Py_INCREF(Py_NotImplemented);
3518    return Py_NotImplemented;
3519}
3520
3521PyDoc_STRVAR(object_subclasshook_doc,
3522"Abstract classes can override this to customize issubclass().\n"
3523"\n"
3524"This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3525"It should return True, False or NotImplemented.  If it returns\n"
3526"NotImplemented, the normal algorithm is used.  Otherwise, it\n"
3527"overrides the normal algorithm (and the outcome is cached).\n");
3528
3529/*
3530   from PEP 3101, this code implements:
3531
3532   class object:
3533       def __format__(self, format_spec):
3534           if isinstance(format_spec, str):
3535               return format(str(self), format_spec)
3536           elif isinstance(format_spec, unicode):
3537               return format(unicode(self), format_spec)
3538*/
3539static PyObject *
3540object_format(PyObject *self, PyObject *args)
3541{
3542    PyObject *format_spec;
3543    PyObject *self_as_str = NULL;
3544    PyObject *result = NULL;
3545    Py_ssize_t format_len;
3546
3547    if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3548        return NULL;
3549#ifdef Py_USING_UNICODE
3550    if (PyUnicode_Check(format_spec)) {
3551        format_len = PyUnicode_GET_SIZE(format_spec);
3552        self_as_str = PyObject_Unicode(self);
3553    } else if (PyString_Check(format_spec)) {
3554#else
3555    if (PyString_Check(format_spec)) {
3556#endif
3557        format_len = PyString_GET_SIZE(format_spec);
3558        self_as_str = PyObject_Str(self);
3559    } else {
3560        PyErr_SetString(PyExc_TypeError,
3561                 "argument to __format__ must be unicode or str");
3562        return NULL;
3563    }
3564
3565    if (self_as_str != NULL) {
3566        /* Issue 7994: If we're converting to a string, we
3567           should reject format specifications */
3568        if (format_len > 0) {
3569            if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
3570             "object.__format__ with a non-empty format "
3571             "string is deprecated", 1) < 0) {
3572                goto done;
3573            }
3574            /* Eventually this will become an error:
3575            PyErr_Format(PyExc_TypeError,
3576               "non-empty format string passed to object.__format__");
3577            goto done;
3578            */
3579        }
3580        result = PyObject_Format(self_as_str, format_spec);
3581    }
3582
3583done:
3584    Py_XDECREF(self_as_str);
3585
3586    return result;
3587}
3588
3589static PyObject *
3590object_sizeof(PyObject *self, PyObject *args)
3591{
3592    Py_ssize_t res, isize;
3593
3594    res = 0;
3595    isize = self->ob_type->tp_itemsize;
3596    if (isize > 0)
3597        res = Py_SIZE(self) * isize;
3598    res += self->ob_type->tp_basicsize;
3599
3600    return PyInt_FromSsize_t(res);
3601}
3602
3603static PyMethodDef object_methods[] = {
3604    {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3605     PyDoc_STR("helper for pickle")},
3606    {"__reduce__", object_reduce, METH_VARARGS,
3607     PyDoc_STR("helper for pickle")},
3608    {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3609     object_subclasshook_doc},
3610    {"__format__", object_format, METH_VARARGS,
3611     PyDoc_STR("default object formatter")},
3612    {"__sizeof__", object_sizeof, METH_NOARGS,
3613     PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},
3614    {0}
3615};
3616
3617
3618PyTypeObject PyBaseObject_Type = {
3619    PyVarObject_HEAD_INIT(&PyType_Type, 0)
3620    "object",                                   /* tp_name */
3621    sizeof(PyObject),                           /* tp_basicsize */
3622    0,                                          /* tp_itemsize */
3623    object_dealloc,                             /* tp_dealloc */
3624    0,                                          /* tp_print */
3625    0,                                          /* tp_getattr */
3626    0,                                          /* tp_setattr */
3627    0,                                          /* tp_compare */
3628    object_repr,                                /* tp_repr */
3629    0,                                          /* tp_as_number */
3630    0,                                          /* tp_as_sequence */
3631    0,                                          /* tp_as_mapping */
3632    (hashfunc)_Py_HashPointer,                  /* tp_hash */
3633    0,                                          /* tp_call */
3634    object_str,                                 /* tp_str */
3635    PyObject_GenericGetAttr,                    /* tp_getattro */
3636    PyObject_GenericSetAttr,                    /* tp_setattro */
3637    0,                                          /* tp_as_buffer */
3638    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3639    PyDoc_STR("The most base type"),            /* tp_doc */
3640    0,                                          /* tp_traverse */
3641    0,                                          /* tp_clear */
3642    0,                                          /* tp_richcompare */
3643    0,                                          /* tp_weaklistoffset */
3644    0,                                          /* tp_iter */
3645    0,                                          /* tp_iternext */
3646    object_methods,                             /* tp_methods */
3647    0,                                          /* tp_members */
3648    object_getsets,                             /* tp_getset */
3649    0,                                          /* tp_base */
3650    0,                                          /* tp_dict */
3651    0,                                          /* tp_descr_get */
3652    0,                                          /* tp_descr_set */
3653    0,                                          /* tp_dictoffset */
3654    object_init,                                /* tp_init */
3655    PyType_GenericAlloc,                        /* tp_alloc */
3656    object_new,                                 /* tp_new */
3657    PyObject_Del,                               /* tp_free */
3658};
3659
3660
3661/* Initialize the __dict__ in a type object */
3662
3663static int
3664add_methods(PyTypeObject *type, PyMethodDef *meth)
3665{
3666    PyObject *dict = type->tp_dict;
3667
3668    for (; meth->ml_name != NULL; meth++) {
3669        PyObject *descr;
3670        int err;
3671        if (PyDict_GetItemString(dict, meth->ml_name) &&
3672            !(meth->ml_flags & METH_COEXIST))
3673                continue;
3674        if (meth->ml_flags & METH_CLASS) {
3675            if (meth->ml_flags & METH_STATIC) {
3676                PyErr_SetString(PyExc_ValueError,
3677                     "method cannot be both class and static");
3678                return -1;
3679            }
3680            descr = PyDescr_NewClassMethod(type, meth);
3681        }
3682        else if (meth->ml_flags & METH_STATIC) {
3683            PyObject *cfunc = PyCFunction_New(meth, NULL);
3684            if (cfunc == NULL)
3685                return -1;
3686            descr = PyStaticMethod_New(cfunc);
3687            Py_DECREF(cfunc);
3688        }
3689        else {
3690            descr = PyDescr_NewMethod(type, meth);
3691        }
3692        if (descr == NULL)
3693            return -1;
3694        err = PyDict_SetItemString(dict, meth->ml_name, descr);
3695        Py_DECREF(descr);
3696        if (err < 0)
3697            return -1;
3698    }
3699    return 0;
3700}
3701
3702static int
3703add_members(PyTypeObject *type, PyMemberDef *memb)
3704{
3705    PyObject *dict = type->tp_dict;
3706
3707    for (; memb->name != NULL; memb++) {
3708        PyObject *descr;
3709        if (PyDict_GetItemString(dict, memb->name))
3710            continue;
3711        descr = PyDescr_NewMember(type, memb);
3712        if (descr == NULL)
3713            return -1;
3714        if (PyDict_SetItemString(dict, memb->name, descr) < 0) {
3715            Py_DECREF(descr);
3716            return -1;
3717        }
3718        Py_DECREF(descr);
3719    }
3720    return 0;
3721}
3722
3723static int
3724add_getset(PyTypeObject *type, PyGetSetDef *gsp)
3725{
3726    PyObject *dict = type->tp_dict;
3727
3728    for (; gsp->name != NULL; gsp++) {
3729        PyObject *descr;
3730        if (PyDict_GetItemString(dict, gsp->name))
3731            continue;
3732        descr = PyDescr_NewGetSet(type, gsp);
3733
3734        if (descr == NULL)
3735            return -1;
3736        if (PyDict_SetItemString(dict, gsp->name, descr) < 0) {
3737            Py_DECREF(descr);
3738            return -1;
3739        }
3740        Py_DECREF(descr);
3741    }
3742    return 0;
3743}
3744
3745#define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)
3746
3747static void
3748inherit_special(PyTypeObject *type, PyTypeObject *base)
3749{
3750    Py_ssize_t oldsize, newsize;
3751
3752    /* Special flag magic */
3753    if (!type->tp_as_buffer && base->tp_as_buffer) {
3754        type->tp_flags &= ~BUFFER_FLAGS;
3755        type->tp_flags |=
3756            base->tp_flags & BUFFER_FLAGS;
3757    }
3758    if (!type->tp_as_sequence && base->tp_as_sequence) {
3759        type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
3760        type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
3761    }
3762    if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
3763        (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
3764        if ((!type->tp_as_number && base->tp_as_number) ||
3765            (!type->tp_as_sequence && base->tp_as_sequence)) {
3766            type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
3767            if (!type->tp_as_number && !type->tp_as_sequence) {
3768                type->tp_flags |= base->tp_flags &
3769                    Py_TPFLAGS_HAVE_INPLACEOPS;
3770            }
3771        }
3772        /* Wow */
3773    }
3774    if (!type->tp_as_number && base->tp_as_number) {
3775        type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
3776        type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
3777    }
3778
3779    /* Copying basicsize is connected to the GC flags */
3780    oldsize = base->tp_basicsize;
3781    newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3782    if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3783        (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3784        (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
3785        (!type->tp_traverse && !type->tp_clear)) {
3786        type->tp_flags |= Py_TPFLAGS_HAVE_GC;
3787        if (type->tp_traverse == NULL)
3788            type->tp_traverse = base->tp_traverse;
3789        if (type->tp_clear == NULL)
3790            type->tp_clear = base->tp_clear;
3791    }
3792    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3793        /* The condition below could use some explanation.
3794           It appears that tp_new is not inherited for static types
3795           whose base class is 'object'; this seems to be a precaution
3796           so that old extension types don't suddenly become
3797           callable (object.__new__ wouldn't insure the invariants
3798           that the extension type's own factory function ensures).
3799           Heap types, of course, are under our control, so they do
3800           inherit tp_new; static extension types that specify some
3801           other built-in type as the default are considered
3802           new-style-aware so they also inherit object.__new__. */
3803        if (base != &PyBaseObject_Type ||
3804            (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3805            if (type->tp_new == NULL)
3806                type->tp_new = base->tp_new;
3807        }
3808    }
3809    type->tp_basicsize = newsize;
3810
3811    /* Copy other non-function slots */
3812
3813#undef COPYVAL
3814#define COPYVAL(SLOT) \
3815    if (type->SLOT == 0) type->SLOT = base->SLOT
3816
3817    COPYVAL(tp_itemsize);
3818    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3819        COPYVAL(tp_weaklistoffset);
3820    }
3821    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3822        COPYVAL(tp_dictoffset);
3823    }
3824
3825    /* Setup fast subclass flags */
3826    if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3827        type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3828    else if (PyType_IsSubtype(base, &PyType_Type))
3829        type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3830    else if (PyType_IsSubtype(base, &PyInt_Type))
3831        type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
3832    else if (PyType_IsSubtype(base, &PyLong_Type))
3833        type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
3834    else if (PyType_IsSubtype(base, &PyString_Type))
3835        type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
3836#ifdef Py_USING_UNICODE
3837    else if (PyType_IsSubtype(base, &PyUnicode_Type))
3838        type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3839#endif
3840    else if (PyType_IsSubtype(base, &PyTuple_Type))
3841        type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3842    else if (PyType_IsSubtype(base, &PyList_Type))
3843        type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3844    else if (PyType_IsSubtype(base, &PyDict_Type))
3845        type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
3846}
3847
3848static int
3849overrides_name(PyTypeObject *type, char *name)
3850{
3851    PyObject *dict = type->tp_dict;
3852
3853    assert(dict != NULL);
3854    if (PyDict_GetItemString(dict, name) != NULL) {
3855        return 1;
3856    }
3857    return 0;
3858}
3859
3860#define OVERRIDES_HASH(x)       overrides_name(x, "__hash__")
3861#define OVERRIDES_EQ(x)         overrides_name(x, "__eq__")
3862
3863static void
3864inherit_slots(PyTypeObject *type, PyTypeObject *base)
3865{
3866    PyTypeObject *basebase;
3867
3868#undef SLOTDEFINED
3869#undef COPYSLOT
3870#undef COPYNUM
3871#undef COPYSEQ
3872#undef COPYMAP
3873#undef COPYBUF
3874
3875#define SLOTDEFINED(SLOT) \
3876    (base->SLOT != 0 && \
3877     (basebase == NULL || base->SLOT != basebase->SLOT))
3878
3879#define COPYSLOT(SLOT) \
3880    if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3881
3882#define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3883#define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3884#define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3885#define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3886
3887    /* This won't inherit indirect slots (from tp_as_number etc.)
3888       if type doesn't provide the space. */
3889
3890    if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3891        basebase = base->tp_base;
3892        if (basebase->tp_as_number == NULL)
3893            basebase = NULL;
3894        COPYNUM(nb_add);
3895        COPYNUM(nb_subtract);
3896        COPYNUM(nb_multiply);
3897        COPYNUM(nb_divide);
3898        COPYNUM(nb_remainder);
3899        COPYNUM(nb_divmod);
3900        COPYNUM(nb_power);
3901        COPYNUM(nb_negative);
3902        COPYNUM(nb_positive);
3903        COPYNUM(nb_absolute);
3904        COPYNUM(nb_nonzero);
3905        COPYNUM(nb_invert);
3906        COPYNUM(nb_lshift);
3907        COPYNUM(nb_rshift);
3908        COPYNUM(nb_and);
3909        COPYNUM(nb_xor);
3910        COPYNUM(nb_or);
3911        COPYNUM(nb_coerce);
3912        COPYNUM(nb_int);
3913        COPYNUM(nb_long);
3914        COPYNUM(nb_float);
3915        COPYNUM(nb_oct);
3916        COPYNUM(nb_hex);
3917        COPYNUM(nb_inplace_add);
3918        COPYNUM(nb_inplace_subtract);
3919        COPYNUM(nb_inplace_multiply);
3920        COPYNUM(nb_inplace_divide);
3921        COPYNUM(nb_inplace_remainder);
3922        COPYNUM(nb_inplace_power);
3923        COPYNUM(nb_inplace_lshift);
3924        COPYNUM(nb_inplace_rshift);
3925        COPYNUM(nb_inplace_and);
3926        COPYNUM(nb_inplace_xor);
3927        COPYNUM(nb_inplace_or);
3928        if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3929            COPYNUM(nb_true_divide);
3930            COPYNUM(nb_floor_divide);
3931            COPYNUM(nb_inplace_true_divide);
3932            COPYNUM(nb_inplace_floor_divide);
3933        }
3934        if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3935            COPYNUM(nb_index);
3936        }
3937    }
3938
3939    if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3940        basebase = base->tp_base;
3941        if (basebase->tp_as_sequence == NULL)
3942            basebase = NULL;
3943        COPYSEQ(sq_length);
3944        COPYSEQ(sq_concat);
3945        COPYSEQ(sq_repeat);
3946        COPYSEQ(sq_item);
3947        COPYSEQ(sq_slice);
3948        COPYSEQ(sq_ass_item);
3949        COPYSEQ(sq_ass_slice);
3950        COPYSEQ(sq_contains);
3951        COPYSEQ(sq_inplace_concat);
3952        COPYSEQ(sq_inplace_repeat);
3953    }
3954
3955    if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3956        basebase = base->tp_base;
3957        if (basebase->tp_as_mapping == NULL)
3958            basebase = NULL;
3959        COPYMAP(mp_length);
3960        COPYMAP(mp_subscript);
3961        COPYMAP(mp_ass_subscript);
3962    }
3963
3964    if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3965        basebase = base->tp_base;
3966        if (basebase->tp_as_buffer == NULL)
3967            basebase = NULL;
3968        COPYBUF(bf_getreadbuffer);
3969        COPYBUF(bf_getwritebuffer);
3970        COPYBUF(bf_getsegcount);
3971        COPYBUF(bf_getcharbuffer);
3972        COPYBUF(bf_getbuffer);
3973        COPYBUF(bf_releasebuffer);
3974    }
3975
3976    basebase = base->tp_base;
3977
3978    COPYSLOT(tp_dealloc);
3979    COPYSLOT(tp_print);
3980    if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3981        type->tp_getattr = base->tp_getattr;
3982        type->tp_getattro = base->tp_getattro;
3983    }
3984    if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3985        type->tp_setattr = base->tp_setattr;
3986        type->tp_setattro = base->tp_setattro;
3987    }
3988    /* tp_compare see tp_richcompare */
3989    COPYSLOT(tp_repr);
3990    /* tp_hash see tp_richcompare */
3991    COPYSLOT(tp_call);
3992    COPYSLOT(tp_str);
3993    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3994        if (type->tp_compare == NULL &&
3995            type->tp_richcompare == NULL &&
3996            type->tp_hash == NULL)
3997        {
3998            type->tp_compare = base->tp_compare;
3999            type->tp_richcompare = base->tp_richcompare;
4000            type->tp_hash = base->tp_hash;
4001            /* Check for changes to inherited methods in Py3k*/
4002            if (Py_Py3kWarningFlag) {
4003                if (base->tp_hash &&
4004                                (base->tp_hash != PyObject_HashNotImplemented) &&
4005                                !OVERRIDES_HASH(type)) {
4006                    if (OVERRIDES_EQ(type)) {
4007                        if (PyErr_WarnPy3k("Overriding "
4008                                           "__eq__ blocks inheritance "
4009                                           "of __hash__ in 3.x",
4010                                           1) < 0)
4011                            /* XXX This isn't right.  If the warning is turned
4012                               into an exception, we should be communicating
4013                               the error back to the caller, but figuring out
4014                               how to clean up in that case is tricky.  See
4015                               issue 8627 for more. */
4016                            PyErr_Clear();
4017                    }
4018                }
4019            }
4020        }
4021    }
4022    else {
4023        COPYSLOT(tp_compare);
4024    }
4025    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
4026        COPYSLOT(tp_iter);
4027        COPYSLOT(tp_iternext);
4028    }
4029    if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
4030        COPYSLOT(tp_descr_get);
4031        COPYSLOT(tp_descr_set);
4032        COPYSLOT(tp_dictoffset);
4033        COPYSLOT(tp_init);
4034        COPYSLOT(tp_alloc);
4035        COPYSLOT(tp_is_gc);
4036        if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
4037            (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
4038            /* They agree about gc. */
4039            COPYSLOT(tp_free);
4040        }
4041        else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
4042                 type->tp_free == NULL &&
4043                 base->tp_free == _PyObject_Del) {
4044            /* A bit of magic to plug in the correct default
4045             * tp_free function when a derived class adds gc,
4046             * didn't define tp_free, and the base uses the
4047             * default non-gc tp_free.
4048             */
4049            type->tp_free = PyObject_GC_Del;
4050        }
4051        /* else they didn't agree about gc, and there isn't something
4052         * obvious to be done -- the type is on its own.
4053         */
4054    }
4055}
4056
4057static int add_operators(PyTypeObject *);
4058
4059int
4060PyType_Ready(PyTypeObject *type)
4061{
4062    PyObject *dict, *bases;
4063    PyTypeObject *base;
4064    Py_ssize_t i, n;
4065
4066    if (type->tp_flags & Py_TPFLAGS_READY) {
4067        assert(type->tp_dict != NULL);
4068        return 0;
4069    }
4070    assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
4071
4072    type->tp_flags |= Py_TPFLAGS_READYING;
4073
4074#ifdef Py_TRACE_REFS
4075    /* PyType_Ready is the closest thing we have to a choke point
4076     * for type objects, so is the best place I can think of to try
4077     * to get type objects into the doubly-linked list of all objects.
4078     * Still, not all type objects go thru PyType_Ready.
4079     */
4080    _Py_AddToAllObjects((PyObject *)type, 0);
4081#endif
4082
4083    if (type->tp_name == NULL) {
4084        PyErr_Format(PyExc_SystemError,
4085                     "Type does not define the tp_name field.");
4086        goto error;
4087    }
4088
4089    /* Initialize tp_base (defaults to BaseObject unless that's us) */
4090    base = type->tp_base;
4091    if (base == NULL && type != &PyBaseObject_Type) {
4092        base = type->tp_base = &PyBaseObject_Type;
4093        Py_INCREF(base);
4094    }
4095
4096    /* Now the only way base can still be NULL is if type is
4097     * &PyBaseObject_Type.
4098     */
4099
4100    /* Initialize the base class */
4101    if (base && base->tp_dict == NULL) {
4102        if (PyType_Ready(base) < 0)
4103            goto error;
4104    }
4105
4106    /* Initialize ob_type if NULL.      This means extensions that want to be
4107       compilable separately on Windows can call PyType_Ready() instead of
4108       initializing the ob_type field of their type objects. */
4109    /* The test for base != NULL is really unnecessary, since base is only
4110       NULL when type is &PyBaseObject_Type, and we know its ob_type is
4111       not NULL (it's initialized to &PyType_Type).      But coverity doesn't
4112       know that. */
4113    if (Py_TYPE(type) == NULL && base != NULL)
4114        Py_TYPE(type) = Py_TYPE(base);
4115
4116    /* Initialize tp_bases */
4117    bases = type->tp_bases;
4118    if (bases == NULL) {
4119        if (base == NULL)
4120            bases = PyTuple_New(0);
4121        else
4122            bases = PyTuple_Pack(1, base);
4123        if (bases == NULL)
4124            goto error;
4125        type->tp_bases = bases;
4126    }
4127
4128    /* Initialize tp_dict */
4129    dict = type->tp_dict;
4130    if (dict == NULL) {
4131        dict = PyDict_New();
4132        if (dict == NULL)
4133            goto error;
4134        type->tp_dict = dict;
4135    }
4136
4137    /* Add type-specific descriptors to tp_dict */
4138    if (add_operators(type) < 0)
4139        goto error;
4140    if (type->tp_methods != NULL) {
4141        if (add_methods(type, type->tp_methods) < 0)
4142            goto error;
4143    }
4144    if (type->tp_members != NULL) {
4145        if (add_members(type, type->tp_members) < 0)
4146            goto error;
4147    }
4148    if (type->tp_getset != NULL) {
4149        if (add_getset(type, type->tp_getset) < 0)
4150            goto error;
4151    }
4152
4153    /* Calculate method resolution order */
4154    if (mro_internal(type) < 0) {
4155        goto error;
4156    }
4157
4158    /* Inherit special flags from dominant base */
4159    if (type->tp_base != NULL)
4160        inherit_special(type, type->tp_base);
4161
4162    /* Initialize tp_dict properly */
4163    bases = type->tp_mro;
4164    assert(bases != NULL);
4165    assert(PyTuple_Check(bases));
4166    n = PyTuple_GET_SIZE(bases);
4167    for (i = 1; i < n; i++) {
4168        PyObject *b = PyTuple_GET_ITEM(bases, i);
4169        if (PyType_Check(b))
4170            inherit_slots(type, (PyTypeObject *)b);
4171    }
4172
4173    /* All bases of statically allocated type should be statically allocated */
4174    if (Py_Py3kWarningFlag && !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
4175        for (i = 0; i < n; i++) {
4176            PyObject *b = PyTuple_GET_ITEM(bases, i);
4177            if (PyType_Check(b) &&
4178                (((PyTypeObject *)b)->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
4179                char buf[300];
4180                PyOS_snprintf(buf, sizeof(buf),
4181                              "type '%.100s' is not dynamically allocated but "
4182                              "its base type '%.100s' is dynamically allocated",
4183                              type->tp_name, ((PyTypeObject *)b)->tp_name);
4184                if (PyErr_WarnPy3k(buf, 1) < 0)
4185                    goto error;
4186                break;
4187            }
4188        }
4189
4190    /* Sanity check for tp_free. */
4191    if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
4192        (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
4193        /* This base class needs to call tp_free, but doesn't have
4194         * one, or its tp_free is for non-gc'ed objects.
4195         */
4196        PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
4197                     "gc and is a base type but has inappropriate "
4198                     "tp_free slot",
4199                     type->tp_name);
4200        goto error;
4201    }
4202
4203    /* if the type dictionary doesn't contain a __doc__, set it from
4204       the tp_doc slot.
4205     */
4206    if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
4207        if (type->tp_doc != NULL) {
4208            PyObject *doc = PyString_FromString(type->tp_doc);
4209            if (doc == NULL)
4210                goto error;
4211            PyDict_SetItemString(type->tp_dict, "__doc__", doc);
4212            Py_DECREF(doc);
4213        } else {
4214            PyDict_SetItemString(type->tp_dict,
4215                                 "__doc__", Py_None);
4216        }
4217    }
4218
4219    /* Some more special stuff */
4220    base = type->tp_base;
4221    if (base != NULL) {
4222        if (type->tp_as_number == NULL)
4223            type->tp_as_number = base->tp_as_number;
4224        if (type->tp_as_sequence == NULL)
4225            type->tp_as_sequence = base->tp_as_sequence;
4226        if (type->tp_as_mapping == NULL)
4227            type->tp_as_mapping = base->tp_as_mapping;
4228        if (type->tp_as_buffer == NULL)
4229            type->tp_as_buffer = base->tp_as_buffer;
4230    }
4231
4232    /* Link into each base class's list of subclasses */
4233    bases = type->tp_bases;
4234    n = PyTuple_GET_SIZE(bases);
4235    for (i = 0; i < n; i++) {
4236        PyObject *b = PyTuple_GET_ITEM(bases, i);
4237        if (PyType_Check(b) &&
4238            add_subclass((PyTypeObject *)b, type) < 0)
4239            goto error;
4240    }
4241
4242    /* All done -- set the ready flag */
4243    assert(type->tp_dict != NULL);
4244    type->tp_flags =
4245        (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
4246    return 0;
4247
4248  error:
4249    type->tp_flags &= ~Py_TPFLAGS_READYING;
4250    return -1;
4251}
4252
4253static int
4254add_subclass(PyTypeObject *base, PyTypeObject *type)
4255{
4256    Py_ssize_t i;
4257    int result;
4258    PyObject *list, *ref, *newobj;
4259
4260    list = base->tp_subclasses;
4261    if (list == NULL) {
4262        base->tp_subclasses = list = PyList_New(0);
4263        if (list == NULL)
4264            return -1;
4265    }
4266    assert(PyList_Check(list));
4267    newobj = PyWeakref_NewRef((PyObject *)type, NULL);
4268    i = PyList_GET_SIZE(list);
4269    while (--i >= 0) {
4270        ref = PyList_GET_ITEM(list, i);
4271        assert(PyWeakref_CheckRef(ref));
4272        if (PyWeakref_GET_OBJECT(ref) == Py_None)
4273            return PyList_SetItem(list, i, newobj);
4274    }
4275    result = PyList_Append(list, newobj);
4276    Py_DECREF(newobj);
4277    return result;
4278}
4279
4280static void
4281remove_subclass(PyTypeObject *base, PyTypeObject *type)
4282{
4283    Py_ssize_t i;
4284    PyObject *list, *ref;
4285
4286    list = base->tp_subclasses;
4287    if (list == NULL) {
4288        return;
4289    }
4290    assert(PyList_Check(list));
4291    i = PyList_GET_SIZE(list);
4292    while (--i >= 0) {
4293        ref = PyList_GET_ITEM(list, i);
4294        assert(PyWeakref_CheckRef(ref));
4295        if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
4296            /* this can't fail, right? */
4297            PySequence_DelItem(list, i);
4298            return;
4299        }
4300    }
4301}
4302
4303static int
4304check_num_args(PyObject *ob, int n)
4305{
4306    if (!PyTuple_CheckExact(ob)) {
4307        PyErr_SetString(PyExc_SystemError,
4308            "PyArg_UnpackTuple() argument list is not a tuple");
4309        return 0;
4310    }
4311    if (n == PyTuple_GET_SIZE(ob))
4312        return 1;
4313    PyErr_Format(
4314        PyExc_TypeError,
4315        "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
4316    return 0;
4317}
4318
4319/* Generic wrappers for overloadable 'operators' such as __getitem__ */
4320
4321/* There's a wrapper *function* for each distinct function typedef used
4322   for type object slots (e.g. binaryfunc, ternaryfunc, etc.).  There's a
4323   wrapper *table* for each distinct operation (e.g. __len__, __add__).
4324   Most tables have only one entry; the tables for binary operators have two
4325   entries, one regular and one with reversed arguments. */
4326
4327static PyObject *
4328wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
4329{
4330    lenfunc func = (lenfunc)wrapped;
4331    Py_ssize_t res;
4332
4333    if (!check_num_args(args, 0))
4334        return NULL;
4335    res = (*func)(self);
4336    if (res == -1 && PyErr_Occurred())
4337        return NULL;
4338    return PyInt_FromLong((long)res);
4339}
4340
4341static PyObject *
4342wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
4343{
4344    inquiry func = (inquiry)wrapped;
4345    int res;
4346
4347    if (!check_num_args(args, 0))
4348        return NULL;
4349    res = (*func)(self);
4350    if (res == -1 && PyErr_Occurred())
4351        return NULL;
4352    return PyBool_FromLong((long)res);
4353}
4354
4355static PyObject *
4356wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4357{
4358    binaryfunc func = (binaryfunc)wrapped;
4359    PyObject *other;
4360
4361    if (!check_num_args(args, 1))
4362        return NULL;
4363    other = PyTuple_GET_ITEM(args, 0);
4364    return (*func)(self, other);
4365}
4366
4367static PyObject *
4368wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4369{
4370    binaryfunc func = (binaryfunc)wrapped;
4371    PyObject *other;
4372
4373    if (!check_num_args(args, 1))
4374        return NULL;
4375    other = PyTuple_GET_ITEM(args, 0);
4376    if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4377        !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4378        Py_INCREF(Py_NotImplemented);
4379        return Py_NotImplemented;
4380    }
4381    return (*func)(self, other);
4382}
4383
4384static PyObject *
4385wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4386{
4387    binaryfunc func = (binaryfunc)wrapped;
4388    PyObject *other;
4389
4390    if (!check_num_args(args, 1))
4391        return NULL;
4392    other = PyTuple_GET_ITEM(args, 0);
4393    if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4394        !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4395        Py_INCREF(Py_NotImplemented);
4396        return Py_NotImplemented;
4397    }
4398    return (*func)(other, self);
4399}
4400
4401static PyObject *
4402wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
4403{
4404    coercion func = (coercion)wrapped;
4405    PyObject *other, *res;
4406    int ok;
4407
4408    if (!check_num_args(args, 1))
4409        return NULL;
4410    other = PyTuple_GET_ITEM(args, 0);
4411    ok = func(&self, &other);
4412    if (ok < 0)
4413        return NULL;
4414    if (ok > 0) {
4415        Py_INCREF(Py_NotImplemented);
4416        return Py_NotImplemented;
4417    }
4418    res = PyTuple_New(2);
4419    if (res == NULL) {
4420        Py_DECREF(self);
4421        Py_DECREF(other);
4422        return NULL;
4423    }
4424    PyTuple_SET_ITEM(res, 0, self);
4425    PyTuple_SET_ITEM(res, 1, other);
4426    return res;
4427}
4428
4429static PyObject *
4430wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4431{
4432    ternaryfunc func = (ternaryfunc)wrapped;
4433    PyObject *other;
4434    PyObject *third = Py_None;
4435
4436    /* Note: This wrapper only works for __pow__() */
4437
4438    if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4439        return NULL;
4440    return (*func)(self, other, third);
4441}
4442
4443static PyObject *
4444wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4445{
4446    ternaryfunc func = (ternaryfunc)wrapped;
4447    PyObject *other;
4448    PyObject *third = Py_None;
4449
4450    /* Note: This wrapper only works for __pow__() */
4451
4452    if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4453        return NULL;
4454    return (*func)(other, self, third);
4455}
4456
4457static PyObject *
4458wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4459{
4460    unaryfunc func = (unaryfunc)wrapped;
4461
4462    if (!check_num_args(args, 0))
4463        return NULL;
4464    return (*func)(self);
4465}
4466
4467static PyObject *
4468wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
4469{
4470    ssizeargfunc func = (ssizeargfunc)wrapped;
4471    PyObject* o;
4472    Py_ssize_t i;
4473
4474    if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4475        return NULL;
4476    i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
4477    if (i == -1 && PyErr_Occurred())
4478        return NULL;
4479    return (*func)(self, i);
4480}
4481
4482static Py_ssize_t
4483getindex(PyObject *self, PyObject *arg)
4484{
4485    Py_ssize_t i;
4486
4487    i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
4488    if (i == -1 && PyErr_Occurred())
4489        return -1;
4490    if (i < 0) {
4491        PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
4492        if (sq && sq->sq_length) {
4493            Py_ssize_t n = (*sq->sq_length)(self);
4494            if (n < 0)
4495                return -1;
4496            i += n;
4497        }
4498    }
4499    return i;
4500}
4501
4502static PyObject *
4503wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4504{
4505    ssizeargfunc func = (ssizeargfunc)wrapped;
4506    PyObject *arg;
4507    Py_ssize_t i;
4508
4509    if (PyTuple_GET_SIZE(args) == 1) {
4510        arg = PyTuple_GET_ITEM(args, 0);
4511        i = getindex(self, arg);
4512        if (i == -1 && PyErr_Occurred())
4513            return NULL;
4514        return (*func)(self, i);
4515    }
4516    check_num_args(args, 1);
4517    assert(PyErr_Occurred());
4518    return NULL;
4519}
4520
4521static PyObject *
4522wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
4523{
4524    ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
4525    Py_ssize_t i, j;
4526
4527    if (!PyArg_ParseTuple(args, "nn", &i, &j))
4528        return NULL;
4529    return (*func)(self, i, j);
4530}
4531
4532static PyObject *
4533wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
4534{
4535    ssizeobjargproc func = (ssizeobjargproc)wrapped;
4536    Py_ssize_t i;
4537    int res;
4538    PyObject *arg, *value;
4539
4540    if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
4541        return NULL;
4542    i = getindex(self, arg);
4543    if (i == -1 && PyErr_Occurred())
4544        return NULL;
4545    res = (*func)(self, i, value);
4546    if (res == -1 && PyErr_Occurred())
4547        return NULL;
4548    Py_INCREF(Py_None);
4549    return Py_None;
4550}
4551
4552static PyObject *
4553wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
4554{
4555    ssizeobjargproc func = (ssizeobjargproc)wrapped;
4556    Py_ssize_t i;
4557    int res;
4558    PyObject *arg;
4559
4560    if (!check_num_args(args, 1))
4561        return NULL;
4562    arg = PyTuple_GET_ITEM(args, 0);
4563    i = getindex(self, arg);
4564    if (i == -1 && PyErr_Occurred())
4565        return NULL;
4566    res = (*func)(self, i, NULL);
4567    if (res == -1 && PyErr_Occurred())
4568        return NULL;
4569    Py_INCREF(Py_None);
4570    return Py_None;
4571}
4572
4573static PyObject *
4574wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
4575{
4576    ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4577    Py_ssize_t i, j;
4578    int res;
4579    PyObject *value;
4580
4581    if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
4582        return NULL;
4583    res = (*func)(self, i, j, value);
4584    if (res == -1 && PyErr_Occurred())
4585        return NULL;
4586    Py_INCREF(Py_None);
4587    return Py_None;
4588}
4589
4590static PyObject *
4591wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
4592{
4593    ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4594    Py_ssize_t i, j;
4595    int res;
4596
4597    if (!PyArg_ParseTuple(args, "nn", &i, &j))
4598        return NULL;
4599    res = (*func)(self, i, j, NULL);
4600    if (res == -1 && PyErr_Occurred())
4601        return NULL;
4602    Py_INCREF(Py_None);
4603    return Py_None;
4604}
4605
4606/* XXX objobjproc is a misnomer; should be objargpred */
4607static PyObject *
4608wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4609{
4610    objobjproc func = (objobjproc)wrapped;
4611    int res;
4612    PyObject *value;
4613
4614    if (!check_num_args(args, 1))
4615        return NULL;
4616    value = PyTuple_GET_ITEM(args, 0);
4617    res = (*func)(self, value);
4618    if (res == -1 && PyErr_Occurred())
4619        return NULL;
4620    else
4621        return PyBool_FromLong(res);
4622}
4623
4624static PyObject *
4625wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4626{
4627    objobjargproc func = (objobjargproc)wrapped;
4628    int res;
4629    PyObject *key, *value;
4630
4631    if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
4632        return NULL;
4633    res = (*func)(self, key, value);
4634    if (res == -1 && PyErr_Occurred())
4635        return NULL;
4636    Py_INCREF(Py_None);
4637    return Py_None;
4638}
4639
4640static PyObject *
4641wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4642{
4643    objobjargproc func = (objobjargproc)wrapped;
4644    int res;
4645    PyObject *key;
4646
4647    if (!check_num_args(args, 1))
4648        return NULL;
4649    key = PyTuple_GET_ITEM(args, 0);
4650    res = (*func)(self, key, NULL);
4651    if (res == -1 && PyErr_Occurred())
4652        return NULL;
4653    Py_INCREF(Py_None);
4654    return Py_None;
4655}
4656
4657static PyObject *
4658wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
4659{
4660    cmpfunc func = (cmpfunc)wrapped;
4661    int res;
4662    PyObject *other;
4663
4664    if (!check_num_args(args, 1))
4665        return NULL;
4666    other = PyTuple_GET_ITEM(args, 0);
4667    if (Py_TYPE(other)->tp_compare != func &&
4668        !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
4669        PyErr_Format(
4670            PyExc_TypeError,
4671            "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
4672            Py_TYPE(self)->tp_name,
4673            Py_TYPE(self)->tp_name,
4674            Py_TYPE(other)->tp_name);
4675        return NULL;
4676    }
4677    res = (*func)(self, other);
4678    if (PyErr_Occurred())
4679        return NULL;
4680    return PyInt_FromLong((long)res);
4681}
4682
4683/* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4684   This is called the Carlo Verre hack after its discoverer. */
4685static int
4686hackcheck(PyObject *self, setattrofunc func, char *what)
4687{
4688    PyTypeObject *type = Py_TYPE(self);
4689    while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4690        type = type->tp_base;
4691    /* If type is NULL now, this is a really weird type.
4692       In the spirit of backwards compatibility (?), just shut up. */
4693    if (type && type->tp_setattro != func) {
4694        PyErr_Format(PyExc_TypeError,
4695                     "can't apply this %s to %s object",
4696                     what,
4697                     type->tp_name);
4698        return 0;
4699    }
4700    return 1;
4701}
4702
4703static PyObject *
4704wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4705{
4706    setattrofunc func = (setattrofunc)wrapped;
4707    int res;
4708    PyObject *name, *value;
4709
4710    if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
4711        return NULL;
4712    if (!hackcheck(self, func, "__setattr__"))
4713        return NULL;
4714    res = (*func)(self, name, value);
4715    if (res < 0)
4716        return NULL;
4717    Py_INCREF(Py_None);
4718    return Py_None;
4719}
4720
4721static PyObject *
4722wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4723{
4724    setattrofunc func = (setattrofunc)wrapped;
4725    int res;
4726    PyObject *name;
4727
4728    if (!check_num_args(args, 1))
4729        return NULL;
4730    name = PyTuple_GET_ITEM(args, 0);
4731    if (!hackcheck(self, func, "__delattr__"))
4732        return NULL;
4733    res = (*func)(self, name, NULL);
4734    if (res < 0)
4735        return NULL;
4736    Py_INCREF(Py_None);
4737    return Py_None;
4738}
4739
4740static PyObject *
4741wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4742{
4743    hashfunc func = (hashfunc)wrapped;
4744    long res;
4745
4746    if (!check_num_args(args, 0))
4747        return NULL;
4748    res = (*func)(self);
4749    if (res == -1 && PyErr_Occurred())
4750        return NULL;
4751    return PyInt_FromLong(res);
4752}
4753
4754static PyObject *
4755wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4756{
4757    ternaryfunc func = (ternaryfunc)wrapped;
4758
4759    return (*func)(self, args, kwds);
4760}
4761
4762static PyObject *
4763wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4764{
4765    richcmpfunc func = (richcmpfunc)wrapped;
4766    PyObject *other;
4767
4768    if (!check_num_args(args, 1))
4769        return NULL;
4770    other = PyTuple_GET_ITEM(args, 0);
4771    return (*func)(self, other, op);
4772}
4773
4774#undef RICHCMP_WRAPPER
4775#define RICHCMP_WRAPPER(NAME, OP) \
4776static PyObject * \
4777richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4778{ \
4779    return wrap_richcmpfunc(self, args, wrapped, OP); \
4780}
4781
4782RICHCMP_WRAPPER(lt, Py_LT)
4783RICHCMP_WRAPPER(le, Py_LE)
4784RICHCMP_WRAPPER(eq, Py_EQ)
4785RICHCMP_WRAPPER(ne, Py_NE)
4786RICHCMP_WRAPPER(gt, Py_GT)
4787RICHCMP_WRAPPER(ge, Py_GE)
4788
4789static PyObject *
4790wrap_next(PyObject *self, PyObject *args, void *wrapped)
4791{
4792    unaryfunc func = (unaryfunc)wrapped;
4793    PyObject *res;
4794
4795    if (!check_num_args(args, 0))
4796        return NULL;
4797    res = (*func)(self);
4798    if (res == NULL && !PyErr_Occurred())
4799        PyErr_SetNone(PyExc_StopIteration);
4800    return res;
4801}
4802
4803static PyObject *
4804wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4805{
4806    descrgetfunc func = (descrgetfunc)wrapped;
4807    PyObject *obj;
4808    PyObject *type = NULL;
4809
4810    if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
4811        return NULL;
4812    if (obj == Py_None)
4813        obj = NULL;
4814    if (type == Py_None)
4815        type = NULL;
4816    if (type == NULL &&obj == NULL) {
4817        PyErr_SetString(PyExc_TypeError,
4818                        "__get__(None, None) is invalid");
4819        return NULL;
4820    }
4821    return (*func)(self, obj, type);
4822}
4823
4824static PyObject *
4825wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
4826{
4827    descrsetfunc func = (descrsetfunc)wrapped;
4828    PyObject *obj, *value;
4829    int ret;
4830
4831    if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
4832        return NULL;
4833    ret = (*func)(self, obj, value);
4834    if (ret < 0)
4835        return NULL;
4836    Py_INCREF(Py_None);
4837    return Py_None;
4838}
4839
4840static PyObject *
4841wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4842{
4843    descrsetfunc func = (descrsetfunc)wrapped;
4844    PyObject *obj;
4845    int ret;
4846
4847    if (!check_num_args(args, 1))
4848        return NULL;
4849    obj = PyTuple_GET_ITEM(args, 0);
4850    ret = (*func)(self, obj, NULL);
4851    if (ret < 0)
4852        return NULL;
4853    Py_INCREF(Py_None);
4854    return Py_None;
4855}
4856
4857static PyObject *
4858wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4859{
4860    initproc func = (initproc)wrapped;
4861
4862    if (func(self, args, kwds) < 0)
4863        return NULL;
4864    Py_INCREF(Py_None);
4865    return Py_None;
4866}
4867
4868static PyObject *
4869tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
4870{
4871    PyTypeObject *type, *subtype, *staticbase;
4872    PyObject *arg0, *res;
4873
4874    if (self == NULL || !PyType_Check(self))
4875        Py_FatalError("__new__() called with non-type 'self'");
4876    type = (PyTypeObject *)self;
4877    if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
4878        PyErr_Format(PyExc_TypeError,
4879                     "%s.__new__(): not enough arguments",
4880                     type->tp_name);
4881        return NULL;
4882    }
4883    arg0 = PyTuple_GET_ITEM(args, 0);
4884    if (!PyType_Check(arg0)) {
4885        PyErr_Format(PyExc_TypeError,
4886                     "%s.__new__(X): X is not a type object (%s)",
4887                     type->tp_name,
4888                     Py_TYPE(arg0)->tp_name);
4889        return NULL;
4890    }
4891    subtype = (PyTypeObject *)arg0;
4892    if (!PyType_IsSubtype(subtype, type)) {
4893        PyErr_Format(PyExc_TypeError,
4894                     "%s.__new__(%s): %s is not a subtype of %s",
4895                     type->tp_name,
4896                     subtype->tp_name,
4897                     subtype->tp_name,
4898                     type->tp_name);
4899        return NULL;
4900    }
4901
4902    /* Check that the use doesn't do something silly and unsafe like
4903       object.__new__(dict).  To do this, we check that the
4904       most derived base that's not a heap type is this type. */
4905    staticbase = subtype;
4906    while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
4907        staticbase = staticbase->tp_base;
4908    /* If staticbase is NULL now, it is a really weird type.
4909       In the spirit of backwards compatibility (?), just shut up. */
4910    if (staticbase && staticbase->tp_new != type->tp_new) {
4911        PyErr_Format(PyExc_TypeError,
4912                     "%s.__new__(%s) is not safe, use %s.__new__()",
4913                     type->tp_name,
4914                     subtype->tp_name,
4915                     staticbase->tp_name);
4916        return NULL;
4917    }
4918
4919    args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
4920    if (args == NULL)
4921        return NULL;
4922    res = type->tp_new(subtype, args, kwds);
4923    Py_DECREF(args);
4924    return res;
4925}
4926
4927static struct PyMethodDef tp_new_methoddef[] = {
4928    {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS,
4929     PyDoc_STR("T.__new__(S, ...) -> "
4930               "a new object with type S, a subtype of T")},
4931    {0}
4932};
4933
4934static int
4935add_tp_new_wrapper(PyTypeObject *type)
4936{
4937    PyObject *func;
4938
4939    if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL)
4940        return 0;
4941    func = PyCFunction_New(tp_new_methoddef, (PyObject *)type);
4942    if (func == NULL)
4943        return -1;
4944    if (PyDict_SetItemString(type->tp_dict, "__new__", func)) {
4945        Py_DECREF(func);
4946        return -1;
4947    }
4948    Py_DECREF(func);
4949    return 0;
4950}
4951
4952/* Slot wrappers that call the corresponding __foo__ slot.  See comments
4953   below at override_slots() for more explanation. */
4954
4955#define SLOT0(FUNCNAME, OPSTR) \
4956static PyObject * \
4957FUNCNAME(PyObject *self) \
4958{ \
4959    static PyObject *cache_str; \
4960    return call_method(self, OPSTR, &cache_str, "()"); \
4961}
4962
4963#define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \
4964static PyObject * \
4965FUNCNAME(PyObject *self, ARG1TYPE arg1) \
4966{ \
4967    static PyObject *cache_str; \
4968    return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \
4969}
4970
4971/* Boolean helper for SLOT1BINFULL().
4972   right.__class__ is a nontrivial subclass of left.__class__. */
4973static int
4974method_is_overloaded(PyObject *left, PyObject *right, char *name)
4975{
4976    PyObject *a, *b;
4977    int ok;
4978
4979    b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name);
4980    if (b == NULL) {
4981        PyErr_Clear();
4982        /* If right doesn't have it, it's not overloaded */
4983        return 0;
4984    }
4985
4986    a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name);
4987    if (a == NULL) {
4988        PyErr_Clear();
4989        Py_DECREF(b);
4990        /* If right has it but left doesn't, it's overloaded */
4991        return 1;
4992    }
4993
4994    ok = PyObject_RichCompareBool(a, b, Py_NE);
4995    Py_DECREF(a);
4996    Py_DECREF(b);
4997    if (ok < 0) {
4998        PyErr_Clear();
4999        return 0;
5000    }
5001
5002    return ok;
5003}
5004
5005
5006#define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \
5007static PyObject * \
5008FUNCNAME(PyObject *self, PyObject *other) \
5009{ \
5010    static PyObject *cache_str, *rcache_str; \
5011    int do_other = Py_TYPE(self) != Py_TYPE(other) && \
5012        Py_TYPE(other)->tp_as_number != NULL && \
5013        Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
5014    if (Py_TYPE(self)->tp_as_number != NULL && \
5015        Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
5016        PyObject *r; \
5017        if (do_other && \
5018            PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \
5019            method_is_overloaded(self, other, ROPSTR)) { \
5020            r = call_maybe( \
5021                other, ROPSTR, &rcache_str, "(O)", self); \
5022            if (r != Py_NotImplemented) \
5023                return r; \
5024            Py_DECREF(r); \
5025            do_other = 0; \
5026        } \
5027        r = call_maybe( \
5028            self, OPSTR, &cache_str, "(O)", other); \
5029        if (r != Py_NotImplemented || \
5030            Py_TYPE(other) == Py_TYPE(self)) \
5031            return r; \
5032        Py_DECREF(r); \
5033    } \
5034    if (do_other) { \
5035        return call_maybe( \
5036            other, ROPSTR, &rcache_str, "(O)", self); \
5037    } \
5038    Py_INCREF(Py_NotImplemented); \
5039    return Py_NotImplemented; \
5040}
5041
5042#define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \
5043    SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR)
5044
5045#define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \
5046static PyObject * \
5047FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \
5048{ \
5049    static PyObject *cache_str; \
5050    return call_method(self, OPSTR, &cache_str, \
5051                       "(" ARGCODES ")", arg1, arg2); \
5052}
5053
5054static Py_ssize_t
5055slot_sq_length(PyObject *self)
5056{
5057    static PyObject *len_str;
5058    PyObject *res = call_method(self, "__len__", &len_str, "()");
5059    Py_ssize_t len;
5060
5061    if (res == NULL)
5062        return -1;
5063    len = PyInt_AsSsize_t(res);
5064    Py_DECREF(res);
5065    if (len < 0) {
5066        if (!PyErr_Occurred())
5067            PyErr_SetString(PyExc_ValueError,
5068                            "__len__() should return >= 0");
5069        return -1;
5070    }
5071    return len;
5072}
5073
5074/* Super-optimized version of slot_sq_item.
5075   Other slots could do the same... */
5076static PyObject *
5077slot_sq_item(PyObject *self, Py_ssize_t i)
5078{
5079    static PyObject *getitem_str;
5080    PyObject *func, *args = NULL, *ival = NULL, *retval = NULL;
5081    descrgetfunc f;
5082
5083    if (getitem_str == NULL) {
5084        getitem_str = PyString_InternFromString("__getitem__");
5085        if (getitem_str == NULL)
5086            return NULL;
5087    }
5088    func = _PyType_Lookup(Py_TYPE(self), getitem_str);
5089    if (func != NULL) {
5090        if ((f = Py_TYPE(func)->tp_descr_get) == NULL)
5091            Py_INCREF(func);
5092        else {
5093            func = f(func, self, (PyObject *)(Py_TYPE(self)));
5094            if (func == NULL) {
5095                return NULL;
5096            }
5097        }
5098        ival = PyInt_FromSsize_t(i);
5099        if (ival != NULL) {
5100            args = PyTuple_New(1);
5101            if (args != NULL) {
5102                PyTuple_SET_ITEM(args, 0, ival);
5103                retval = PyObject_Call(func, args, NULL);
5104                Py_XDECREF(args);
5105                Py_XDECREF(func);
5106                return retval;
5107            }
5108        }
5109    }
5110    else {
5111        PyErr_SetObject(PyExc_AttributeError, getitem_str);
5112    }
5113    Py_XDECREF(args);
5114    Py_XDECREF(ival);
5115    Py_XDECREF(func);
5116    return NULL;
5117}
5118
5119static PyObject*
5120slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j)
5121{
5122    static PyObject *getslice_str;
5123
5124    if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; "
5125                        "use __getitem__", 1) < 0)
5126        return NULL;
5127    return call_method(self, "__getslice__", &getslice_str,
5128        "nn", i, j);
5129}
5130
5131static int
5132slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
5133{
5134    PyObject *res;
5135    static PyObject *delitem_str, *setitem_str;
5136
5137    if (value == NULL)
5138        res = call_method(self, "__delitem__", &delitem_str,
5139                          "(n)", index);
5140    else
5141        res = call_method(self, "__setitem__", &setitem_str,
5142                          "(nO)", index, value);
5143    if (res == NULL)
5144        return -1;
5145    Py_DECREF(res);
5146    return 0;
5147}
5148
5149static int
5150slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value)
5151{
5152    PyObject *res;
5153    static PyObject *delslice_str, *setslice_str;
5154
5155    if (value == NULL) {
5156        if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; "
5157                           "use __delitem__", 1) < 0)
5158            return -1;
5159        res = call_method(self, "__delslice__", &delslice_str,
5160                          "(nn)", i, j);
5161    }
5162    else {
5163        if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; "
5164                                "use __setitem__", 1) < 0)
5165            return -1;
5166        res = call_method(self, "__setslice__", &setslice_str,
5167                  "(nnO)", i, j, value);
5168    }
5169    if (res == NULL)
5170        return -1;
5171    Py_DECREF(res);
5172    return 0;
5173}
5174
5175static int
5176slot_sq_contains(PyObject *self, PyObject *value)
5177{
5178    PyObject *func, *res, *args;
5179    int result = -1;
5180
5181    static PyObject *contains_str;
5182
5183    func = lookup_maybe(self, "__contains__", &contains_str);
5184    if (func != NULL) {
5185        args = PyTuple_Pack(1, value);
5186        if (args == NULL)
5187            res = NULL;
5188        else {
5189            res = PyObject_Call(func, args, NULL);
5190            Py_DECREF(args);
5191        }
5192        Py_DECREF(func);
5193        if (res != NULL) {
5194            result = PyObject_IsTrue(res);
5195            Py_DECREF(res);
5196        }
5197    }
5198    else if (! PyErr_Occurred()) {
5199        /* Possible results: -1 and 1 */
5200        result = (int)_PySequence_IterSearch(self, value,
5201                                         PY_ITERSEARCH_CONTAINS);
5202    }
5203    return result;
5204}
5205
5206#define slot_mp_length slot_sq_length
5207
5208SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O")
5209
5210static int
5211slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
5212{
5213    PyObject *res;
5214    static PyObject *delitem_str, *setitem_str;
5215
5216    if (value == NULL)
5217        res = call_method(self, "__delitem__", &delitem_str,
5218                          "(O)", key);
5219    else
5220        res = call_method(self, "__setitem__", &setitem_str,
5221                         "(OO)", key, value);
5222    if (res == NULL)
5223        return -1;
5224    Py_DECREF(res);
5225    return 0;
5226}
5227
5228SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__")
5229SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__")
5230SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__")
5231SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__")
5232SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__")
5233SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__")
5234
5235static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
5236
5237SLOT1BINFULL(slot_nb_power_binary, slot_nb_power,
5238             nb_power, "__pow__", "__rpow__")
5239
5240static PyObject *
5241slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
5242{
5243    static PyObject *pow_str;
5244
5245    if (modulus == Py_None)
5246        return slot_nb_power_binary(self, other);
5247    /* Three-arg power doesn't use __rpow__.  But ternary_op
5248       can call this when the second argument's type uses
5249       slot_nb_power, so check before calling self.__pow__. */
5250    if (Py_TYPE(self)->tp_as_number != NULL &&
5251        Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
5252        return call_method(self, "__pow__", &pow_str,
5253                           "(OO)", other, modulus);
5254    }
5255    Py_INCREF(Py_NotImplemented);
5256    return Py_NotImplemented;
5257}
5258
5259SLOT0(slot_nb_negative, "__neg__")
5260SLOT0(slot_nb_positive, "__pos__")
5261SLOT0(slot_nb_absolute, "__abs__")
5262
5263static int
5264slot_nb_nonzero(PyObject *self)
5265{
5266    PyObject *func, *args;
5267    static PyObject *nonzero_str, *len_str;
5268    int result = -1;
5269    int using_len = 0;
5270
5271    func = lookup_maybe(self, "__nonzero__", &nonzero_str);
5272    if (func == NULL) {
5273        if (PyErr_Occurred())
5274            return -1;
5275        func = lookup_maybe(self, "__len__", &len_str);
5276        if (func == NULL)
5277            return PyErr_Occurred() ? -1 : 1;
5278        using_len = 1;
5279    }
5280    args = PyTuple_New(0);
5281    if (args != NULL) {
5282        PyObject *temp = PyObject_Call(func, args, NULL);
5283        Py_DECREF(args);
5284        if (temp != NULL) {
5285            if (PyInt_CheckExact(temp) || PyBool_Check(temp))
5286                result = PyObject_IsTrue(temp);
5287            else {
5288                PyErr_Format(PyExc_TypeError,
5289                             "%s should return "
5290                             "bool or int, returned %s",
5291                             (using_len ? "__len__"
5292                                        : "__nonzero__"),
5293                             temp->ob_type->tp_name);
5294                result = -1;
5295            }
5296            Py_DECREF(temp);
5297        }
5298    }
5299    Py_DECREF(func);
5300    return result;
5301}
5302
5303
5304static PyObject *
5305slot_nb_index(PyObject *self)
5306{
5307    static PyObject *index_str;
5308    return call_method(self, "__index__", &index_str, "()");
5309}
5310
5311
5312SLOT0(slot_nb_invert, "__invert__")
5313SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__")
5314SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__")
5315SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__")
5316SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__")
5317SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__")
5318
5319static int
5320slot_nb_coerce(PyObject **a, PyObject **b)
5321{
5322    static PyObject *coerce_str;
5323    PyObject *self = *a, *other = *b;
5324
5325    if (self->ob_type->tp_as_number != NULL &&
5326        self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
5327        PyObject *r;
5328        r = call_maybe(
5329            self, "__coerce__", &coerce_str, "(O)", other);
5330        if (r == NULL)
5331            return -1;
5332        if (r == Py_NotImplemented) {
5333            Py_DECREF(r);
5334        }
5335        else {
5336            if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
5337                PyErr_SetString(PyExc_TypeError,
5338                    "__coerce__ didn't return a 2-tuple");
5339                Py_DECREF(r);
5340                return -1;
5341            }
5342            *a = PyTuple_GET_ITEM(r, 0);
5343            Py_INCREF(*a);
5344            *b = PyTuple_GET_ITEM(r, 1);
5345            Py_INCREF(*b);
5346            Py_DECREF(r);
5347            return 0;
5348        }
5349    }
5350    if (other->ob_type->tp_as_number != NULL &&
5351        other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) {
5352        PyObject *r;
5353        r = call_maybe(
5354            other, "__coerce__", &coerce_str, "(O)", self);
5355        if (r == NULL)
5356            return -1;
5357        if (r == Py_NotImplemented) {
5358            Py_DECREF(r);
5359            return 1;
5360        }
5361        if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) {
5362            PyErr_SetString(PyExc_TypeError,
5363                            "__coerce__ didn't return a 2-tuple");
5364            Py_DECREF(r);
5365            return -1;
5366        }
5367        *a = PyTuple_GET_ITEM(r, 1);
5368        Py_INCREF(*a);
5369        *b = PyTuple_GET_ITEM(r, 0);
5370        Py_INCREF(*b);
5371        Py_DECREF(r);
5372        return 0;
5373    }
5374    return 1;
5375}
5376
5377SLOT0(slot_nb_int, "__int__")
5378SLOT0(slot_nb_long, "__long__")
5379SLOT0(slot_nb_float, "__float__")
5380SLOT0(slot_nb_oct, "__oct__")
5381SLOT0(slot_nb_hex, "__hex__")
5382SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O")
5383SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O")
5384SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O")
5385SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O")
5386SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O")
5387/* Can't use SLOT1 here, because nb_inplace_power is ternary */
5388static PyObject *
5389slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
5390{
5391  static PyObject *cache_str;
5392  return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1);
5393}
5394SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O")
5395SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O")
5396SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O")
5397SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O")
5398SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O")
5399SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
5400         "__floordiv__", "__rfloordiv__")
5401SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__")
5402SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O")
5403SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O")
5404
5405static int
5406half_compare(PyObject *self, PyObject *other)
5407{
5408    PyObject *func, *args, *res;
5409    static PyObject *cmp_str;
5410    Py_ssize_t c;
5411
5412    func = lookup_method(self, "__cmp__", &cmp_str);
5413    if (func == NULL) {
5414        PyErr_Clear();
5415    }
5416    else {
5417        args = PyTuple_Pack(1, other);
5418        if (args == NULL)
5419            res = NULL;
5420        else {
5421            res = PyObject_Call(func, args, NULL);
5422            Py_DECREF(args);
5423        }
5424        Py_DECREF(func);
5425        if (res != Py_NotImplemented) {
5426            if (res == NULL)
5427                return -2;
5428            c = PyInt_AsLong(res);
5429            Py_DECREF(res);
5430            if (c == -1 && PyErr_Occurred())
5431                return -2;
5432            return (c < 0) ? -1 : (c > 0) ? 1 : 0;
5433        }
5434        Py_DECREF(res);
5435    }
5436    return 2;
5437}
5438
5439/* This slot is published for the benefit of try_3way_compare in object.c */
5440int
5441_PyObject_SlotCompare(PyObject *self, PyObject *other)
5442{
5443    int c;
5444
5445    if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) {
5446        c = half_compare(self, other);
5447        if (c <= 1)
5448            return c;
5449    }
5450    if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) {
5451        c = half_compare(other, self);
5452        if (c < -1)
5453            return -2;
5454        if (c <= 1)
5455            return -c;
5456    }
5457    return (void *)self < (void *)other ? -1 :
5458        (void *)self > (void *)other ? 1 : 0;
5459}
5460
5461static PyObject *
5462slot_tp_repr(PyObject *self)
5463{
5464    PyObject *func, *res;
5465    static PyObject *repr_str;
5466
5467    func = lookup_method(self, "__repr__", &repr_str);
5468    if (func != NULL) {
5469        res = PyEval_CallObject(func, NULL);
5470        Py_DECREF(func);
5471        return res;
5472    }
5473    PyErr_Clear();
5474    return PyString_FromFormat("<%s object at %p>",
5475                               Py_TYPE(self)->tp_name, self);
5476}
5477
5478static PyObject *
5479slot_tp_str(PyObject *self)
5480{
5481    PyObject *func, *res;
5482    static PyObject *str_str;
5483
5484    func = lookup_method(self, "__str__", &str_str);
5485    if (func != NULL) {
5486        res = PyEval_CallObject(func, NULL);
5487        Py_DECREF(func);
5488        return res;
5489    }
5490    else {
5491        PyErr_Clear();
5492        return slot_tp_repr(self);
5493    }
5494}
5495
5496static long
5497slot_tp_hash(PyObject *self)
5498{
5499    PyObject *func;
5500    static PyObject *hash_str, *eq_str, *cmp_str;
5501    long h;
5502
5503    func = lookup_method(self, "__hash__", &hash_str);
5504
5505    if (func != NULL && func != Py_None) {
5506        PyObject *res = PyEval_CallObject(func, NULL);
5507        Py_DECREF(func);
5508        if (res == NULL)
5509            return -1;
5510        if (PyLong_Check(res))
5511            h = PyLong_Type.tp_hash(res);
5512        else
5513            h = PyInt_AsLong(res);
5514        Py_DECREF(res);
5515    }
5516    else {
5517        Py_XDECREF(func); /* may be None */
5518        PyErr_Clear();
5519        func = lookup_method(self, "__eq__", &eq_str);
5520        if (func == NULL) {
5521            PyErr_Clear();
5522            func = lookup_method(self, "__cmp__", &cmp_str);
5523        }
5524        if (func != NULL) {
5525            Py_DECREF(func);
5526            return PyObject_HashNotImplemented(self);
5527        }
5528        PyErr_Clear();
5529        h = _Py_HashPointer((void *)self);
5530    }
5531    if (h == -1 && !PyErr_Occurred())
5532        h = -2;
5533    return h;
5534}
5535
5536static PyObject *
5537slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
5538{
5539    static PyObject *call_str;
5540    PyObject *meth = lookup_method(self, "__call__", &call_str);
5541    PyObject *res;
5542
5543    if (meth == NULL)
5544        return NULL;
5545
5546    res = PyObject_Call(meth, args, kwds);
5547
5548    Py_DECREF(meth);
5549    return res;
5550}
5551
5552/* There are two slot dispatch functions for tp_getattro.
5553
5554   - slot_tp_getattro() is used when __getattribute__ is overridden
5555     but no __getattr__ hook is present;
5556
5557   - slot_tp_getattr_hook() is used when a __getattr__ hook is present.
5558
5559   The code in update_one_slot() always installs slot_tp_getattr_hook(); this
5560   detects the absence of __getattr__ and then installs the simpler slot if
5561   necessary. */
5562
5563static PyObject *
5564slot_tp_getattro(PyObject *self, PyObject *name)
5565{
5566    static PyObject *getattribute_str = NULL;
5567    return call_method(self, "__getattribute__", &getattribute_str,
5568                       "(O)", name);
5569}
5570
5571static PyObject *
5572call_attribute(PyObject *self, PyObject *attr, PyObject *name)
5573{
5574    PyObject *res, *descr = NULL;
5575    descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
5576
5577    if (f != NULL) {
5578        descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
5579        if (descr == NULL)
5580            return NULL;
5581        else
5582            attr = descr;
5583    }
5584    res = PyObject_CallFunctionObjArgs(attr, name, NULL);
5585    Py_XDECREF(descr);
5586    return res;
5587}
5588
5589static PyObject *
5590slot_tp_getattr_hook(PyObject *self, PyObject *name)
5591{
5592    PyTypeObject *tp = Py_TYPE(self);
5593    PyObject *getattr, *getattribute, *res;
5594    static PyObject *getattribute_str = NULL;
5595    static PyObject *getattr_str = NULL;
5596
5597    if (getattr_str == NULL) {
5598        getattr_str = PyString_InternFromString("__getattr__");
5599        if (getattr_str == NULL)
5600            return NULL;
5601    }
5602    if (getattribute_str == NULL) {
5603        getattribute_str =
5604            PyString_InternFromString("__getattribute__");
5605        if (getattribute_str == NULL)
5606            return NULL;
5607    }
5608    /* speed hack: we could use lookup_maybe, but that would resolve the
5609       method fully for each attribute lookup for classes with
5610       __getattr__, even when the attribute is present. So we use
5611       _PyType_Lookup and create the method only when needed, with
5612       call_attribute. */
5613    getattr = _PyType_Lookup(tp, getattr_str);
5614    if (getattr == NULL) {
5615        /* No __getattr__ hook: use a simpler dispatcher */
5616        tp->tp_getattro = slot_tp_getattro;
5617        return slot_tp_getattro(self, name);
5618    }
5619    Py_INCREF(getattr);
5620    /* speed hack: we could use lookup_maybe, but that would resolve the
5621       method fully for each attribute lookup for classes with
5622       __getattr__, even when self has the default __getattribute__
5623       method. So we use _PyType_Lookup and create the method only when
5624       needed, with call_attribute. */
5625    getattribute = _PyType_Lookup(tp, getattribute_str);
5626    if (getattribute == NULL ||
5627        (Py_TYPE(getattribute) == &PyWrapperDescr_Type &&
5628         ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
5629         (void *)PyObject_GenericGetAttr))
5630        res = PyObject_GenericGetAttr(self, name);
5631    else {
5632        Py_INCREF(getattribute);
5633        res = call_attribute(self, getattribute, name);
5634        Py_DECREF(getattribute);
5635    }
5636    if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) {
5637        PyErr_Clear();
5638        res = call_attribute(self, getattr, name);
5639    }
5640    Py_DECREF(getattr);
5641    return res;
5642}
5643
5644static int
5645slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
5646{
5647    PyObject *res;
5648    static PyObject *delattr_str, *setattr_str;
5649
5650    if (value == NULL)
5651        res = call_method(self, "__delattr__", &delattr_str,
5652                          "(O)", name);
5653    else
5654        res = call_method(self, "__setattr__", &setattr_str,
5655                          "(OO)", name, value);
5656    if (res == NULL)
5657        return -1;
5658    Py_DECREF(res);
5659    return 0;
5660}
5661
5662static char *name_op[] = {
5663    "__lt__",
5664    "__le__",
5665    "__eq__",
5666    "__ne__",
5667    "__gt__",
5668    "__ge__",
5669};
5670
5671static PyObject *
5672half_richcompare(PyObject *self, PyObject *other, int op)
5673{
5674    PyObject *func, *args, *res;
5675    static PyObject *op_str[6];
5676
5677    func = lookup_method(self, name_op[op], &op_str[op]);
5678    if (func == NULL) {
5679        PyErr_Clear();
5680        Py_INCREF(Py_NotImplemented);
5681        return Py_NotImplemented;
5682    }
5683    args = PyTuple_Pack(1, other);
5684    if (args == NULL)
5685        res = NULL;
5686    else {
5687        res = PyObject_Call(func, args, NULL);
5688        Py_DECREF(args);
5689    }
5690    Py_DECREF(func);
5691    return res;
5692}
5693
5694static PyObject *
5695slot_tp_richcompare(PyObject *self, PyObject *other, int op)
5696{
5697    PyObject *res;
5698
5699    if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) {
5700        res = half_richcompare(self, other, op);
5701        if (res != Py_NotImplemented)
5702            return res;
5703        Py_DECREF(res);
5704    }
5705    if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) {
5706        res = half_richcompare(other, self, _Py_SwappedOp[op]);
5707        if (res != Py_NotImplemented) {
5708            return res;
5709        }
5710        Py_DECREF(res);
5711    }
5712    Py_INCREF(Py_NotImplemented);
5713    return Py_NotImplemented;
5714}
5715
5716static PyObject *
5717slot_tp_iter(PyObject *self)
5718{
5719    PyObject *func, *res;
5720    static PyObject *iter_str, *getitem_str;
5721
5722    func = lookup_method(self, "__iter__", &iter_str);
5723    if (func != NULL) {
5724        PyObject *args;
5725        args = res = PyTuple_New(0);
5726        if (args != NULL) {
5727            res = PyObject_Call(func, args, NULL);
5728            Py_DECREF(args);
5729        }
5730        Py_DECREF(func);
5731        return res;
5732    }
5733    PyErr_Clear();
5734    func = lookup_method(self, "__getitem__", &getitem_str);
5735    if (func == NULL) {
5736        PyErr_Format(PyExc_TypeError,
5737                     "'%.200s' object is not iterable",
5738                     Py_TYPE(self)->tp_name);
5739        return NULL;
5740    }
5741    Py_DECREF(func);
5742    return PySeqIter_New(self);
5743}
5744
5745static PyObject *
5746slot_tp_iternext(PyObject *self)
5747{
5748    static PyObject *next_str;
5749    return call_method(self, "next", &next_str, "()");
5750}
5751
5752static PyObject *
5753slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
5754{
5755    PyTypeObject *tp = Py_TYPE(self);
5756    PyObject *get;
5757    static PyObject *get_str = NULL;
5758
5759    if (get_str == NULL) {
5760        get_str = PyString_InternFromString("__get__");
5761        if (get_str == NULL)
5762            return NULL;
5763    }
5764    get = _PyType_Lookup(tp, get_str);
5765    if (get == NULL) {
5766        /* Avoid further slowdowns */
5767        if (tp->tp_descr_get == slot_tp_descr_get)
5768            tp->tp_descr_get = NULL;
5769        Py_INCREF(self);
5770        return self;
5771    }
5772    if (obj == NULL)
5773        obj = Py_None;
5774    if (type == NULL)
5775        type = Py_None;
5776    return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL);
5777}
5778
5779static int
5780slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
5781{
5782    PyObject *res;
5783    static PyObject *del_str, *set_str;
5784
5785    if (value == NULL)
5786        res = call_method(self, "__delete__", &del_str,
5787                          "(O)", target);
5788    else
5789        res = call_method(self, "__set__", &set_str,
5790                          "(OO)", target, value);
5791    if (res == NULL)
5792        return -1;
5793    Py_DECREF(res);
5794    return 0;
5795}
5796
5797static int
5798slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
5799{
5800    static PyObject *init_str;
5801    PyObject *meth = lookup_method(self, "__init__", &init_str);
5802    PyObject *res;
5803
5804    if (meth == NULL)
5805        return -1;
5806    res = PyObject_Call(meth, args, kwds);
5807    Py_DECREF(meth);
5808    if (res == NULL)
5809        return -1;
5810    if (res != Py_None) {
5811        PyErr_Format(PyExc_TypeError,
5812                     "__init__() should return None, not '%.200s'",
5813                     Py_TYPE(res)->tp_name);
5814        Py_DECREF(res);
5815        return -1;
5816    }
5817    Py_DECREF(res);
5818    return 0;
5819}
5820
5821static PyObject *
5822slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
5823{
5824    static PyObject *new_str;
5825    PyObject *func;
5826    PyObject *newargs, *x;
5827    Py_ssize_t i, n;
5828
5829    if (new_str == NULL) {
5830        new_str = PyString_InternFromString("__new__");
5831        if (new_str == NULL)
5832            return NULL;
5833    }
5834    func = PyObject_GetAttr((PyObject *)type, new_str);
5835    if (func == NULL)
5836        return NULL;
5837    assert(PyTuple_Check(args));
5838    n = PyTuple_GET_SIZE(args);
5839    newargs = PyTuple_New(n+1);
5840    if (newargs == NULL)
5841        return NULL;
5842    Py_INCREF(type);
5843    PyTuple_SET_ITEM(newargs, 0, (PyObject *)type);
5844    for (i = 0; i < n; i++) {
5845        x = PyTuple_GET_ITEM(args, i);
5846        Py_INCREF(x);
5847        PyTuple_SET_ITEM(newargs, i+1, x);
5848    }
5849    x = PyObject_Call(func, newargs, kwds);
5850    Py_DECREF(newargs);
5851    Py_DECREF(func);
5852    return x;
5853}
5854
5855static void
5856slot_tp_del(PyObject *self)
5857{
5858    static PyObject *del_str = NULL;
5859    PyObject *del, *res;
5860    PyObject *error_type, *error_value, *error_traceback;
5861
5862    /* Temporarily resurrect the object. */
5863    assert(self->ob_refcnt == 0);
5864    self->ob_refcnt = 1;
5865
5866    /* Save the current exception, if any. */
5867    PyErr_Fetch(&error_type, &error_value, &error_traceback);
5868
5869    /* Execute __del__ method, if any. */
5870    del = lookup_maybe(self, "__del__", &del_str);
5871    if (del != NULL) {
5872        res = PyEval_CallObject(del, NULL);
5873        if (res == NULL)
5874            PyErr_WriteUnraisable(del);
5875        else
5876            Py_DECREF(res);
5877        Py_DECREF(del);
5878    }
5879
5880    /* Restore the saved exception. */
5881    PyErr_Restore(error_type, error_value, error_traceback);
5882
5883    /* Undo the temporary resurrection; can't use DECREF here, it would
5884     * cause a recursive call.
5885     */
5886    assert(self->ob_refcnt > 0);
5887    if (--self->ob_refcnt == 0)
5888        return;         /* this is the normal path out */
5889
5890    /* __del__ resurrected it!  Make it look like the original Py_DECREF
5891     * never happened.
5892     */
5893    {
5894        Py_ssize_t refcnt = self->ob_refcnt;
5895        _Py_NewReference(self);
5896        self->ob_refcnt = refcnt;
5897    }
5898    assert(!PyType_IS_GC(Py_TYPE(self)) ||
5899           _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED);
5900    /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so
5901     * we need to undo that. */
5902    _Py_DEC_REFTOTAL;
5903    /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object
5904     * chain, so no more to do there.
5905     * If COUNT_ALLOCS, the original decref bumped tp_frees, and
5906     * _Py_NewReference bumped tp_allocs:  both of those need to be
5907     * undone.
5908     */
5909#ifdef COUNT_ALLOCS
5910    --Py_TYPE(self)->tp_frees;
5911    --Py_TYPE(self)->tp_allocs;
5912#endif
5913}
5914
5915
5916/*
5917Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.
5918
5919The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,
5920which incorporates the additional structures used for numbers, sequences and
5921mappings.  Note that multiple names may map to the same slot (e.g. __eq__,
5922__ne__ etc. all map to tp_richcompare) and one name may map to multiple slots
5923(e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with
5924an all-zero entry.  (This table is further initialized in init_slotdefs().)
5925*/
5926
5927typedef struct wrapperbase slotdef;
5928
5929#undef TPSLOT
5930#undef FLSLOT
5931#undef ETSLOT
5932#undef SQSLOT
5933#undef MPSLOT
5934#undef NBSLOT
5935#undef UNSLOT
5936#undef IBSLOT
5937#undef BINSLOT
5938#undef RBINSLOT
5939
5940#define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5941    {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5942     PyDoc_STR(DOC)}
5943#define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
5944    {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5945     PyDoc_STR(DOC), FLAGS}
5946#define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5947    {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
5948     PyDoc_STR(DOC)}
5949#define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5950    ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
5951#define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5952    ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
5953#define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5954    ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
5955#define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5956    ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5957           "x." NAME "() <==> " DOC)
5958#define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
5959    ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
5960           "x." NAME "(y) <==> x" DOC "y")
5961#define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
5962    ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5963           "x." NAME "(y) <==> x" DOC "y")
5964#define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
5965    ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5966           "x." NAME "(y) <==> y" DOC "x")
5967#define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5968    ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
5969           "x." NAME "(y) <==> " DOC)
5970#define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
5971    ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
5972           "x." NAME "(y) <==> " DOC)
5973
5974static slotdef slotdefs[] = {
5975    TPSLOT("__str__", tp_print, NULL, NULL, ""),
5976    TPSLOT("__repr__", tp_print, NULL, NULL, ""),
5977    TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""),
5978    TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""),
5979    TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""),
5980    TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""),
5981    TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc,
5982           "x.__cmp__(y) <==> cmp(x,y)"),
5983    TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc,
5984           "x.__repr__() <==> repr(x)"),
5985    TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc,
5986           "x.__hash__() <==> hash(x)"),
5987    FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call,
5988           "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS),
5989    TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc,
5990           "x.__str__() <==> str(x)"),
5991    TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook,
5992           wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"),
5993    TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""),
5994    TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr,
5995           "x.__setattr__('name', value) <==> x.name = value"),
5996    TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr,
5997           "x.__delattr__('name') <==> del x.name"),
5998    TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt,
5999           "x.__lt__(y) <==> x<y"),
6000    TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le,
6001           "x.__le__(y) <==> x<=y"),
6002    TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq,
6003           "x.__eq__(y) <==> x==y"),
6004    TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne,
6005           "x.__ne__(y) <==> x!=y"),
6006    TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt,
6007           "x.__gt__(y) <==> x>y"),
6008    TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge,
6009           "x.__ge__(y) <==> x>=y"),
6010    TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc,
6011           "x.__iter__() <==> iter(x)"),
6012    TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next,
6013           "x.next() -> the next value, or raise StopIteration"),
6014    TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get,
6015           "descr.__get__(obj[, type]) -> value"),
6016    TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set,
6017           "descr.__set__(obj, value)"),
6018    TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set,
6019           wrap_descr_delete, "descr.__delete__(obj)"),
6020    FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init,
6021           "x.__init__(...) initializes x; "
6022           "see help(type(x)) for signature",
6023           PyWrapperFlag_KEYWORDS),
6024    TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""),
6025    TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""),
6026    BINSLOT("__add__", nb_add, slot_nb_add,
6027        "+"),
6028    RBINSLOT("__radd__", nb_add, slot_nb_add,
6029             "+"),
6030    BINSLOT("__sub__", nb_subtract, slot_nb_subtract,
6031        "-"),
6032    RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract,
6033             "-"),
6034    BINSLOT("__mul__", nb_multiply, slot_nb_multiply,
6035        "*"),
6036    RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply,
6037             "*"),
6038    BINSLOT("__div__", nb_divide, slot_nb_divide,
6039        "/"),
6040    RBINSLOT("__rdiv__", nb_divide, slot_nb_divide,
6041             "/"),
6042    BINSLOT("__mod__", nb_remainder, slot_nb_remainder,
6043        "%"),
6044    RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder,
6045             "%"),
6046    BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod,
6047        "divmod(x, y)"),
6048    RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod,
6049             "divmod(y, x)"),
6050    NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc,
6051           "x.__pow__(y[, z]) <==> pow(x, y[, z])"),
6052    NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r,
6053           "y.__rpow__(x[, z]) <==> pow(x, y[, z])"),
6054    UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"),
6055    UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"),
6056    UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc,
6057           "abs(x)"),
6058    UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred,
6059           "x != 0"),
6060    UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"),
6061    BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"),
6062    RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"),
6063    BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"),
6064    RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"),
6065    BINSLOT("__and__", nb_and, slot_nb_and, "&"),
6066    RBINSLOT("__rand__", nb_and, slot_nb_and, "&"),
6067    BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"),
6068    RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"),
6069    BINSLOT("__or__", nb_or, slot_nb_or, "|"),
6070    RBINSLOT("__ror__", nb_or, slot_nb_or, "|"),
6071    NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc,
6072           "x.__coerce__(y) <==> coerce(x, y)"),
6073    UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc,
6074           "int(x)"),
6075    UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc,
6076           "long(x)"),
6077    UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc,
6078           "float(x)"),
6079    UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc,
6080           "oct(x)"),
6081    UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc,
6082           "hex(x)"),
6083    IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add,
6084           wrap_binaryfunc, "+="),
6085    IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract,
6086           wrap_binaryfunc, "-="),
6087    IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply,
6088           wrap_binaryfunc, "*="),
6089    IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide,
6090           wrap_binaryfunc, "/="),
6091    IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder,
6092           wrap_binaryfunc, "%="),
6093    IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power,
6094           wrap_binaryfunc, "**="),
6095    IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift,
6096           wrap_binaryfunc, "<<="),
6097    IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift,
6098           wrap_binaryfunc, ">>="),
6099    IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and,
6100           wrap_binaryfunc, "&="),
6101    IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor,
6102           wrap_binaryfunc, "^="),
6103    IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or,
6104           wrap_binaryfunc, "|="),
6105    BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
6106    RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"),
6107    BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"),
6108    RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"),
6109    IBSLOT("__ifloordiv__", nb_inplace_floor_divide,
6110           slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="),
6111    IBSLOT("__itruediv__", nb_inplace_true_divide,
6112           slot_nb_inplace_true_divide, wrap_binaryfunc, "/="),
6113    NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc,
6114           "x[y:z] <==> x[y.__index__():z.__index__()]"),
6115    MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc,
6116           "x.__len__() <==> len(x)"),
6117    MPSLOT("__getitem__", mp_subscript, slot_mp_subscript,
6118           wrap_binaryfunc,
6119           "x.__getitem__(y) <==> x[y]"),
6120    MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript,
6121           wrap_objobjargproc,
6122           "x.__setitem__(i, y) <==> x[i]=y"),
6123    MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript,
6124           wrap_delitem,
6125           "x.__delitem__(y) <==> del x[y]"),
6126    SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc,
6127           "x.__len__() <==> len(x)"),
6128    /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
6129       The logic in abstract.c always falls back to nb_add/nb_multiply in
6130       this case.  Defining both the nb_* and the sq_* slots to call the
6131       user-defined methods has unexpected side-effects, as shown by
6132       test_descr.notimplemented() */
6133    SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc,
6134      "x.__add__(y) <==> x+y"),
6135    SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc,
6136      "x.__mul__(n) <==> x*n"),
6137    SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc,
6138      "x.__rmul__(n) <==> n*x"),
6139    SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item,
6140           "x.__getitem__(y) <==> x[y]"),
6141    SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc,
6142           "x.__getslice__(i, j) <==> x[i:j]\n\
6143           \n\
6144           Use of negative indices is not supported."),
6145    SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
6146           "x.__setitem__(i, y) <==> x[i]=y"),
6147    SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
6148           "x.__delitem__(y) <==> del x[y]"),
6149    SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice,
6150           wrap_ssizessizeobjargproc,
6151           "x.__setslice__(i, j, y) <==> x[i:j]=y\n\
6152           \n\
6153           Use  of negative indices is not supported."),
6154    SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice,
6155           "x.__delslice__(i, j) <==> del x[i:j]\n\
6156           \n\
6157           Use of negative indices is not supported."),
6158    SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc,
6159           "x.__contains__(y) <==> y in x"),
6160    SQSLOT("__iadd__", sq_inplace_concat, NULL,
6161      wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"),
6162    SQSLOT("__imul__", sq_inplace_repeat, NULL,
6163      wrap_indexargfunc, "x.__imul__(y) <==> x*=y"),
6164    {NULL}
6165};
6166
6167/* Given a type pointer and an offset gotten from a slotdef entry, return a
6168   pointer to the actual slot.  This is not quite the same as simply adding
6169   the offset to the type pointer, since it takes care to indirect through the
6170   proper indirection pointer (as_buffer, etc.); it returns NULL if the
6171   indirection pointer is NULL. */
6172static void **
6173slotptr(PyTypeObject *type, int ioffset)
6174{
6175    char *ptr;
6176    long offset = ioffset;
6177
6178    /* Note: this depends on the order of the members of PyHeapTypeObject! */
6179    assert(offset >= 0);
6180    assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
6181    if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
6182        ptr = (char *)type->tp_as_sequence;
6183        offset -= offsetof(PyHeapTypeObject, as_sequence);
6184    }
6185    else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
6186        ptr = (char *)type->tp_as_mapping;
6187        offset -= offsetof(PyHeapTypeObject, as_mapping);
6188    }
6189    else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
6190        ptr = (char *)type->tp_as_number;
6191        offset -= offsetof(PyHeapTypeObject, as_number);
6192    }
6193    else {
6194        ptr = (char *)type;
6195    }
6196    if (ptr != NULL)
6197        ptr += offset;
6198    return (void **)ptr;
6199}
6200
6201/* Length of array of slotdef pointers used to store slots with the
6202   same __name__.  There should be at most MAX_EQUIV-1 slotdef entries with
6203   the same __name__, for any __name__. Since that's a static property, it is
6204   appropriate to declare fixed-size arrays for this. */
6205#define MAX_EQUIV 10
6206
6207/* Return a slot pointer for a given name, but ONLY if the attribute has
6208   exactly one slot function.  The name must be an interned string. */
6209static void **
6210resolve_slotdups(PyTypeObject *type, PyObject *name)
6211{
6212    /* XXX Maybe this could be optimized more -- but is it worth it? */
6213
6214    /* pname and ptrs act as a little cache */
6215    static PyObject *pname;
6216    static slotdef *ptrs[MAX_EQUIV];
6217    slotdef *p, **pp;
6218    void **res, **ptr;
6219
6220    if (pname != name) {
6221        /* Collect all slotdefs that match name into ptrs. */
6222        pname = name;
6223        pp = ptrs;
6224        for (p = slotdefs; p->name_strobj; p++) {
6225            if (p->name_strobj == name)
6226                *pp++ = p;
6227        }
6228        *pp = NULL;
6229    }
6230
6231    /* Look in all matching slots of the type; if exactly one of these has
6232       a filled-in slot, return its value.      Otherwise return NULL. */
6233    res = NULL;
6234    for (pp = ptrs; *pp; pp++) {
6235        ptr = slotptr(type, (*pp)->offset);
6236        if (ptr == NULL || *ptr == NULL)
6237            continue;
6238        if (res != NULL)
6239            return NULL;
6240        res = ptr;
6241    }
6242    return res;
6243}
6244
6245/* Common code for update_slots_callback() and fixup_slot_dispatchers().  This
6246   does some incredibly complex thinking and then sticks something into the
6247   slot.  (It sees if the adjacent slotdefs for the same slot have conflicting
6248   interests, and then stores a generic wrapper or a specific function into
6249   the slot.)  Return a pointer to the next slotdef with a different offset,
6250   because that's convenient  for fixup_slot_dispatchers(). */
6251static slotdef *
6252update_one_slot(PyTypeObject *type, slotdef *p)
6253{
6254    PyObject *descr;
6255    PyWrapperDescrObject *d;
6256    void *generic = NULL, *specific = NULL;
6257    int use_generic = 0;
6258    int offset = p->offset;
6259    void **ptr = slotptr(type, offset);
6260
6261    if (ptr == NULL) {
6262        do {
6263            ++p;
6264        } while (p->offset == offset);
6265        return p;
6266    }
6267    do {
6268        descr = _PyType_Lookup(type, p->name_strobj);
6269        if (descr == NULL) {
6270            if (ptr == (void**)&type->tp_iternext) {
6271                specific = _PyObject_NextNotImplemented;
6272            }
6273            continue;
6274        }
6275        if (Py_TYPE(descr) == &PyWrapperDescr_Type &&
6276            ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) {
6277            void **tptr = resolve_slotdups(type, p->name_strobj);
6278            if (tptr == NULL || tptr == ptr)
6279                generic = p->function;
6280            d = (PyWrapperDescrObject *)descr;
6281            if (d->d_base->wrapper == p->wrapper &&
6282                PyType_IsSubtype(type, d->d_type))
6283            {
6284                if (specific == NULL ||
6285                    specific == d->d_wrapped)
6286                    specific = d->d_wrapped;
6287                else
6288                    use_generic = 1;
6289            }
6290        }
6291        else if (Py_TYPE(descr) == &PyCFunction_Type &&
6292                 PyCFunction_GET_FUNCTION(descr) ==
6293                 (PyCFunction)tp_new_wrapper &&
6294                 ptr == (void**)&type->tp_new)
6295        {
6296            /* The __new__ wrapper is not a wrapper descriptor,
6297               so must be special-cased differently.
6298               If we don't do this, creating an instance will
6299               always use slot_tp_new which will look up
6300               __new__ in the MRO which will call tp_new_wrapper
6301               which will look through the base classes looking
6302               for a static base and call its tp_new (usually
6303               PyType_GenericNew), after performing various
6304               sanity checks and constructing a new argument
6305               list.  Cut all that nonsense short -- this speeds
6306               up instance creation tremendously. */
6307            PyObject *self = PyCFunction_GET_SELF(descr);
6308            if (!self || !PyType_Check(self)) {
6309                /* This should never happen because
6310                   tp_new_wrapper expects a type for self.
6311                   Use slot_tp_new which will call
6312                   tp_new_wrapper which will raise an
6313                   exception. */
6314                specific = (void *)slot_tp_new;
6315            }
6316            else {
6317                specific = ((PyTypeObject *)self)->tp_new;
6318                /* Check that the user does not do anything
6319                   silly and unsafe like object.__new__(dict).
6320                   To do this, we check that the most derived
6321                   base that's not a heap type is this type. */
6322                PyTypeObject *staticbase = type->tp_base;
6323                while (staticbase &&
6324                       (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE))
6325                    staticbase = staticbase->tp_base;
6326                if (staticbase &&
6327                    staticbase->tp_new != specific)
6328                    /* Seems to be unsafe, better use
6329                       slot_tp_new which will call
6330                       tp_new_wrapper which will raise an
6331                       exception if it is unsafe. */
6332                    specific = (void *)slot_tp_new;
6333            }
6334            /* XXX I'm not 100% sure that there isn't a hole
6335               in this reasoning that requires additional
6336               sanity checks.  I'll buy the first person to
6337               point out a bug in this reasoning a beer. */
6338        }
6339        else if (descr == Py_None &&
6340                 ptr == (void**)&type->tp_hash) {
6341            /* We specifically allow __hash__ to be set to None
6342               to prevent inheritance of the default
6343               implementation from object.__hash__ */
6344            specific = PyObject_HashNotImplemented;
6345        }
6346        else {
6347            use_generic = 1;
6348            generic = p->function;
6349        }
6350    } while ((++p)->offset == offset);
6351    if (specific && !use_generic)
6352        *ptr = specific;
6353    else
6354        *ptr = generic;
6355    return p;
6356}
6357
6358/* In the type, update the slots whose slotdefs are gathered in the pp array.
6359   This is a callback for update_subclasses(). */
6360static int
6361update_slots_callback(PyTypeObject *type, void *data)
6362{
6363    slotdef **pp = (slotdef **)data;
6364
6365    for (; *pp; pp++)
6366        update_one_slot(type, *pp);
6367    return 0;
6368}
6369
6370/* Initialize the slotdefs table by adding interned string objects for the
6371   names and sorting the entries. */
6372static void
6373init_slotdefs(void)
6374{
6375    slotdef *p;
6376    static int initialized = 0;
6377
6378    if (initialized)
6379        return;
6380    for (p = slotdefs; p->name; p++) {
6381        /* Slots must be ordered by their offset in the PyHeapTypeObject. */
6382        assert(!p[1].name || p->offset <= p[1].offset);
6383        p->name_strobj = PyString_InternFromString(p->name);
6384        if (!p->name_strobj)
6385            Py_FatalError("Out of memory interning slotdef names");
6386    }
6387    initialized = 1;
6388}
6389
6390/* Update the slots after assignment to a class (type) attribute. */
6391static int
6392update_slot(PyTypeObject *type, PyObject *name)
6393{
6394    slotdef *ptrs[MAX_EQUIV];
6395    slotdef *p;
6396    slotdef **pp;
6397    int offset;
6398
6399    /* Clear the VALID_VERSION flag of 'type' and all its
6400       subclasses.  This could possibly be unified with the
6401       update_subclasses() recursion below, but carefully:
6402       they each have their own conditions on which to stop
6403       recursing into subclasses. */
6404    PyType_Modified(type);
6405
6406    init_slotdefs();
6407    pp = ptrs;
6408    for (p = slotdefs; p->name; p++) {
6409        /* XXX assume name is interned! */
6410        if (p->name_strobj == name)
6411            *pp++ = p;
6412    }
6413    *pp = NULL;
6414    for (pp = ptrs; *pp; pp++) {
6415        p = *pp;
6416        offset = p->offset;
6417        while (p > slotdefs && (p-1)->offset == offset)
6418            --p;
6419        *pp = p;
6420    }
6421    if (ptrs[0] == NULL)
6422        return 0; /* Not an attribute that affects any slots */
6423    return update_subclasses(type, name,
6424                             update_slots_callback, (void *)ptrs);
6425}
6426
6427/* Store the proper functions in the slot dispatches at class (type)
6428   definition time, based upon which operations the class overrides in its
6429   dict. */
6430static void
6431fixup_slot_dispatchers(PyTypeObject *type)
6432{
6433    slotdef *p;
6434
6435    init_slotdefs();
6436    for (p = slotdefs; p->name; )
6437        p = update_one_slot(type, p);
6438}
6439
6440static void
6441update_all_slots(PyTypeObject* type)
6442{
6443    slotdef *p;
6444
6445    init_slotdefs();
6446    for (p = slotdefs; p->name; p++) {
6447        /* update_slot returns int but can't actually fail */
6448        update_slot(type, p->name_strobj);
6449    }
6450}
6451
6452/* recurse_down_subclasses() and update_subclasses() are mutually
6453   recursive functions to call a callback for all subclasses,
6454   but refraining from recursing into subclasses that define 'name'. */
6455
6456static int
6457update_subclasses(PyTypeObject *type, PyObject *name,
6458                  update_callback callback, void *data)
6459{
6460    if (callback(type, data) < 0)
6461        return -1;
6462    return recurse_down_subclasses(type, name, callback, data);
6463}
6464
6465static int
6466recurse_down_subclasses(PyTypeObject *type, PyObject *name,
6467                        update_callback callback, void *data)
6468{
6469    PyTypeObject *subclass;
6470    PyObject *ref, *subclasses, *dict;
6471    Py_ssize_t i, n;
6472
6473    subclasses = type->tp_subclasses;
6474    if (subclasses == NULL)
6475        return 0;
6476    assert(PyList_Check(subclasses));
6477    n = PyList_GET_SIZE(subclasses);
6478    for (i = 0; i < n; i++) {
6479        ref = PyList_GET_ITEM(subclasses, i);
6480        assert(PyWeakref_CheckRef(ref));
6481        subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
6482        assert(subclass != NULL);
6483        if ((PyObject *)subclass == Py_None)
6484            continue;
6485        assert(PyType_Check(subclass));
6486        /* Avoid recursing down into unaffected classes */
6487        dict = subclass->tp_dict;
6488        if (dict != NULL && PyDict_Check(dict) &&
6489            PyDict_GetItem(dict, name) != NULL)
6490            continue;
6491        if (update_subclasses(subclass, name, callback, data) < 0)
6492            return -1;
6493    }
6494    return 0;
6495}
6496
6497/* This function is called by PyType_Ready() to populate the type's
6498   dictionary with method descriptors for function slots.  For each
6499   function slot (like tp_repr) that's defined in the type, one or more
6500   corresponding descriptors are added in the type's tp_dict dictionary
6501   under the appropriate name (like __repr__).  Some function slots
6502   cause more than one descriptor to be added (for example, the nb_add
6503   slot adds both __add__ and __radd__ descriptors) and some function
6504   slots compete for the same descriptor (for example both sq_item and
6505   mp_subscript generate a __getitem__ descriptor).
6506
6507   In the latter case, the first slotdef entry encountered wins.  Since
6508   slotdef entries are sorted by the offset of the slot in the
6509   PyHeapTypeObject, this gives us some control over disambiguating
6510   between competing slots: the members of PyHeapTypeObject are listed
6511   from most general to least general, so the most general slot is
6512   preferred.  In particular, because as_mapping comes before as_sequence,
6513   for a type that defines both mp_subscript and sq_item, mp_subscript
6514   wins.
6515
6516   This only adds new descriptors and doesn't overwrite entries in
6517   tp_dict that were previously defined.  The descriptors contain a
6518   reference to the C function they must call, so that it's safe if they
6519   are copied into a subtype's __dict__ and the subtype has a different
6520   C function in its slot -- calling the method defined by the
6521   descriptor will call the C function that was used to create it,
6522   rather than the C function present in the slot when it is called.
6523   (This is important because a subtype may have a C function in the
6524   slot that calls the method from the dictionary, and we want to avoid
6525   infinite recursion here.) */
6526
6527static int
6528add_operators(PyTypeObject *type)
6529{
6530    PyObject *dict = type->tp_dict;
6531    slotdef *p;
6532    PyObject *descr;
6533    void **ptr;
6534
6535    init_slotdefs();
6536    for (p = slotdefs; p->name; p++) {
6537        if (p->wrapper == NULL)
6538            continue;
6539        ptr = slotptr(type, p->offset);
6540        if (!ptr || !*ptr)
6541            continue;
6542        if (PyDict_GetItem(dict, p->name_strobj))
6543            continue;
6544        if (*ptr == PyObject_HashNotImplemented) {
6545            /* Classes may prevent the inheritance of the tp_hash
6546               slot by storing PyObject_HashNotImplemented in it. Make it
6547               visible as a None value for the __hash__ attribute. */
6548            if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
6549                return -1;
6550        }
6551        else {
6552            descr = PyDescr_NewWrapper(type, p, *ptr);
6553            if (descr == NULL)
6554                return -1;
6555            if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
6556                Py_DECREF(descr);
6557                return -1;
6558            }
6559            Py_DECREF(descr);
6560        }
6561    }
6562    if (type->tp_new != NULL) {
6563        if (add_tp_new_wrapper(type) < 0)
6564            return -1;
6565    }
6566    return 0;
6567}
6568
6569
6570/* Cooperative 'super' */
6571
6572typedef struct {
6573    PyObject_HEAD
6574    PyTypeObject *type;
6575    PyObject *obj;
6576    PyTypeObject *obj_type;
6577} superobject;
6578
6579static PyMemberDef super_members[] = {
6580    {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
6581     "the class invoking super()"},
6582    {"__self__",  T_OBJECT, offsetof(superobject, obj), READONLY,
6583     "the instance invoking super(); may be None"},
6584    {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
6585     "the type of the instance invoking super(); may be None"},
6586    {0}
6587};
6588
6589static void
6590super_dealloc(PyObject *self)
6591{
6592    superobject *su = (superobject *)self;
6593
6594    _PyObject_GC_UNTRACK(self);
6595    Py_XDECREF(su->obj);
6596    Py_XDECREF(su->type);
6597    Py_XDECREF(su->obj_type);
6598    Py_TYPE(self)->tp_free(self);
6599}
6600
6601static PyObject *
6602super_repr(PyObject *self)
6603{
6604    superobject *su = (superobject *)self;
6605
6606    if (su->obj_type)
6607        return PyString_FromFormat(
6608            "<super: <class '%s'>, <%s object>>",
6609            su->type ? su->type->tp_name : "NULL",
6610            su->obj_type->tp_name);
6611    else
6612        return PyString_FromFormat(
6613            "<super: <class '%s'>, NULL>",
6614            su->type ? su->type->tp_name : "NULL");
6615}
6616
6617static PyObject *
6618super_getattro(PyObject *self, PyObject *name)
6619{
6620    superobject *su = (superobject *)self;
6621    int skip = su->obj_type == NULL;
6622
6623    if (!skip) {
6624        /* We want __class__ to return the class of the super object
6625           (i.e. super, or a subclass), not the class of su->obj. */
6626        skip = (PyString_Check(name) &&
6627            PyString_GET_SIZE(name) == 9 &&
6628            strcmp(PyString_AS_STRING(name), "__class__") == 0);
6629    }
6630
6631    if (!skip) {
6632        PyObject *mro, *res, *tmp, *dict;
6633        PyTypeObject *starttype;
6634        descrgetfunc f;
6635        Py_ssize_t i, n;
6636
6637        starttype = su->obj_type;
6638        mro = starttype->tp_mro;
6639
6640        if (mro == NULL)
6641            n = 0;
6642        else {
6643            assert(PyTuple_Check(mro));
6644            n = PyTuple_GET_SIZE(mro);
6645        }
6646        for (i = 0; i < n; i++) {
6647            if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
6648                break;
6649        }
6650        i++;
6651        res = NULL;
6652        for (; i < n; i++) {
6653            tmp = PyTuple_GET_ITEM(mro, i);
6654            if (PyType_Check(tmp))
6655                dict = ((PyTypeObject *)tmp)->tp_dict;
6656            else if (PyClass_Check(tmp))
6657                dict = ((PyClassObject *)tmp)->cl_dict;
6658            else
6659                continue;
6660            res = PyDict_GetItem(dict, name);
6661            if (res != NULL) {
6662                Py_INCREF(res);
6663                f = Py_TYPE(res)->tp_descr_get;
6664                if (f != NULL) {
6665                    tmp = f(res,
6666                        /* Only pass 'obj' param if
6667                           this is instance-mode super
6668                           (See SF ID #743627)
6669                        */
6670                        (su->obj == (PyObject *)
6671                                    su->obj_type
6672                            ? (PyObject *)NULL
6673                            : su->obj),
6674                        (PyObject *)starttype);
6675                    Py_DECREF(res);
6676                    res = tmp;
6677                }
6678                return res;
6679            }
6680        }
6681    }
6682    return PyObject_GenericGetAttr(self, name);
6683}
6684
6685static PyTypeObject *
6686supercheck(PyTypeObject *type, PyObject *obj)
6687{
6688    /* Check that a super() call makes sense.  Return a type object.
6689
6690       obj can be a new-style class, or an instance of one:
6691
6692       - If it is a class, it must be a subclass of 'type'.      This case is
6693         used for class methods; the return value is obj.
6694
6695       - If it is an instance, it must be an instance of 'type'.  This is
6696         the normal case; the return value is obj.__class__.
6697
6698       But... when obj is an instance, we want to allow for the case where
6699       Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
6700       This will allow using super() with a proxy for obj.
6701    */
6702
6703    /* Check for first bullet above (special case) */
6704    if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
6705        Py_INCREF(obj);
6706        return (PyTypeObject *)obj;
6707    }
6708
6709    /* Normal case */
6710    if (PyType_IsSubtype(Py_TYPE(obj), type)) {
6711        Py_INCREF(Py_TYPE(obj));
6712        return Py_TYPE(obj);
6713    }
6714    else {
6715        /* Try the slow way */
6716        static PyObject *class_str = NULL;
6717        PyObject *class_attr;
6718
6719        if (class_str == NULL) {
6720            class_str = PyString_FromString("__class__");
6721            if (class_str == NULL)
6722                return NULL;
6723        }
6724
6725        class_attr = PyObject_GetAttr(obj, class_str);
6726
6727        if (class_attr != NULL &&
6728            PyType_Check(class_attr) &&
6729            (PyTypeObject *)class_attr != Py_TYPE(obj))
6730        {
6731            int ok = PyType_IsSubtype(
6732                (PyTypeObject *)class_attr, type);
6733            if (ok)
6734                return (PyTypeObject *)class_attr;
6735        }
6736
6737        if (class_attr == NULL)
6738            PyErr_Clear();
6739        else
6740            Py_DECREF(class_attr);
6741    }
6742
6743    PyErr_SetString(PyExc_TypeError,
6744                    "super(type, obj): "
6745                    "obj must be an instance or subtype of type");
6746    return NULL;
6747}
6748
6749static PyObject *
6750super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
6751{
6752    superobject *su = (superobject *)self;
6753    superobject *newobj;
6754
6755    if (obj == NULL || obj == Py_None || su->obj != NULL) {
6756        /* Not binding to an object, or already bound */
6757        Py_INCREF(self);
6758        return self;
6759    }
6760    if (Py_TYPE(su) != &PySuper_Type)
6761        /* If su is an instance of a (strict) subclass of super,
6762           call its type */
6763        return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
6764                                            su->type, obj, NULL);
6765    else {
6766        /* Inline the common case */
6767        PyTypeObject *obj_type = supercheck(su->type, obj);
6768        if (obj_type == NULL)
6769            return NULL;
6770        newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
6771                                                 NULL, NULL);
6772        if (newobj == NULL)
6773            return NULL;
6774        Py_INCREF(su->type);
6775        Py_INCREF(obj);
6776        newobj->type = su->type;
6777        newobj->obj = obj;
6778        newobj->obj_type = obj_type;
6779        return (PyObject *)newobj;
6780    }
6781}
6782
6783static int
6784super_init(PyObject *self, PyObject *args, PyObject *kwds)
6785{
6786    superobject *su = (superobject *)self;
6787    PyTypeObject *type;
6788    PyObject *obj = NULL;
6789    PyTypeObject *obj_type = NULL;
6790
6791    if (!_PyArg_NoKeywords("super", kwds))
6792        return -1;
6793    if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj))
6794        return -1;
6795    if (obj == Py_None)
6796        obj = NULL;
6797    if (obj != NULL) {
6798        obj_type = supercheck(type, obj);
6799        if (obj_type == NULL)
6800            return -1;
6801        Py_INCREF(obj);
6802    }
6803    Py_INCREF(type);
6804    Py_XSETREF(su->type, type);
6805    Py_XSETREF(su->obj, obj);
6806    Py_XSETREF(su->obj_type, obj_type);
6807    return 0;
6808}
6809
6810PyDoc_STRVAR(super_doc,
6811"super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
6812"super(type) -> unbound super object\n"
6813"super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
6814"Typical use to call a cooperative superclass method:\n"
6815"class C(B):\n"
6816"    def meth(self, arg):\n"
6817"        super(C, self).meth(arg)");
6818
6819static int
6820super_traverse(PyObject *self, visitproc visit, void *arg)
6821{
6822    superobject *su = (superobject *)self;
6823
6824    Py_VISIT(su->obj);
6825    Py_VISIT(su->type);
6826    Py_VISIT(su->obj_type);
6827
6828    return 0;
6829}
6830
6831PyTypeObject PySuper_Type = {
6832    PyVarObject_HEAD_INIT(&PyType_Type, 0)
6833    "super",                                    /* tp_name */
6834    sizeof(superobject),                        /* tp_basicsize */
6835    0,                                          /* tp_itemsize */
6836    /* methods */
6837    super_dealloc,                              /* tp_dealloc */
6838    0,                                          /* tp_print */
6839    0,                                          /* tp_getattr */
6840    0,                                          /* tp_setattr */
6841    0,                                          /* tp_compare */
6842    super_repr,                                 /* tp_repr */
6843    0,                                          /* tp_as_number */
6844    0,                                          /* tp_as_sequence */
6845    0,                                          /* tp_as_mapping */
6846    0,                                          /* tp_hash */
6847    0,                                          /* tp_call */
6848    0,                                          /* tp_str */
6849    super_getattro,                             /* tp_getattro */
6850    0,                                          /* tp_setattro */
6851    0,                                          /* tp_as_buffer */
6852    Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
6853        Py_TPFLAGS_BASETYPE,                    /* tp_flags */
6854    super_doc,                                  /* tp_doc */
6855    super_traverse,                             /* tp_traverse */
6856    0,                                          /* tp_clear */
6857    0,                                          /* tp_richcompare */
6858    0,                                          /* tp_weaklistoffset */
6859    0,                                          /* tp_iter */
6860    0,                                          /* tp_iternext */
6861    0,                                          /* tp_methods */
6862    super_members,                              /* tp_members */
6863    0,                                          /* tp_getset */
6864    0,                                          /* tp_base */
6865    0,                                          /* tp_dict */
6866    super_descr_get,                            /* tp_descr_get */
6867    0,                                          /* tp_descr_set */
6868    0,                                          /* tp_dictoffset */
6869    super_init,                                 /* tp_init */
6870    PyType_GenericAlloc,                        /* tp_alloc */
6871    PyType_GenericNew,                          /* tp_new */
6872    PyObject_GC_Del,                            /* tp_free */
6873};
6874