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