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