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