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