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