LCOV - code coverage report
Current view: top level - Objects - typeobject.c (source / functions) Hit Total Coverage
Test: CPython 3.12 LCOV report [commit 5e6661bce9] Lines: 2284 4098 55.7 %
Date: 2023-03-20 08:15:36 Functions: 220 331 66.5 %
Branches: 1698 3472 48.9 %

           Branch data     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_symtable.h"      // _Py_Mangle()
       7                 :            : #include "pycore_dict.h"          // _PyDict_KeysSize()
       8                 :            : #include "pycore_initconfig.h"    // _PyStatus_OK()
       9                 :            : #include "pycore_moduleobject.h"  // _PyModule_GetDef()
      10                 :            : #include "pycore_object.h"        // _PyType_HasFeature()
      11                 :            : #include "pycore_pyerrors.h"      // _PyErr_Occurred()
      12                 :            : #include "pycore_pystate.h"       // _PyThreadState_GET()
      13                 :            : #include "pycore_typeobject.h"    // struct type_cache
      14                 :            : #include "pycore_unionobject.h"   // _Py_union_type_or
      15                 :            : #include "pycore_frame.h"         // _PyInterpreterFrame
      16                 :            : #include "opcode.h"               // MAKE_CELL
      17                 :            : #include "structmember.h"         // PyMemberDef
      18                 :            : 
      19                 :            : #include <ctype.h>
      20                 :            : 
      21                 :            : /*[clinic input]
      22                 :            : class type "PyTypeObject *" "&PyType_Type"
      23                 :            : class object "PyObject *" "&PyBaseObject_Type"
      24                 :            : [clinic start generated code]*/
      25                 :            : /*[clinic end generated code: output=da39a3ee5e6b4b0d input=4b94608d231c434b]*/
      26                 :            : 
      27                 :            : #include "clinic/typeobject.c.h"
      28                 :            : 
      29                 :            : /* Support type attribute lookup cache */
      30                 :            : 
      31                 :            : /* The cache can keep references to the names alive for longer than
      32                 :            :    they normally would.  This is why the maximum size is limited to
      33                 :            :    MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
      34                 :            :    strings are used as attribute names. */
      35                 :            : #define MCACHE_MAX_ATTR_SIZE    100
      36                 :            : #define MCACHE_HASH(version, name_hash)                                 \
      37                 :            :         (((unsigned int)(version) ^ (unsigned int)(name_hash))          \
      38                 :            :          & ((1 << MCACHE_SIZE_EXP) - 1))
      39                 :            : 
      40                 :            : #define MCACHE_HASH_METHOD(type, name)                                  \
      41                 :            :     MCACHE_HASH((type)->tp_version_tag, ((Py_ssize_t)(name)) >> 3)
      42                 :            : #define MCACHE_CACHEABLE_NAME(name)                             \
      43                 :            :         PyUnicode_CheckExact(name) &&                           \
      44                 :            :         PyUnicode_IS_READY(name) &&                             \
      45                 :            :         (PyUnicode_GET_LENGTH(name) <= MCACHE_MAX_ATTR_SIZE)
      46                 :            : 
      47                 :            : #define next_version_tag (_PyRuntime.types.next_version_tag)
      48                 :            : 
      49                 :            : typedef struct PySlot_Offset {
      50                 :            :     short subslot_offset;
      51                 :            :     short slot_offset;
      52                 :            : } PySlot_Offset;
      53                 :            : 
      54                 :            : 
      55                 :            : static PyObject *
      56                 :            : slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
      57                 :            : 
      58                 :            : static PyObject *
      59                 :            : lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound);
      60                 :            : 
      61                 :            : static int
      62                 :            : slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value);
      63                 :            : 
      64                 :            : static inline PyTypeObject * subclass_from_ref(PyObject *ref);
      65                 :            : 
      66                 :            : 
      67                 :            : /* helpers for for static builtin types */
      68                 :            : 
      69                 :            : #ifndef NDEBUG
      70                 :            : static inline int
      71                 :            : static_builtin_index_is_set(PyTypeObject *self)
      72                 :            : {
      73                 :            :     return self->tp_subclasses != NULL;
      74                 :            : }
      75                 :            : #endif
      76                 :            : 
      77                 :            : static inline size_t
      78                 :     129520 : static_builtin_index_get(PyTypeObject *self)
      79                 :            : {
      80                 :            :     assert(static_builtin_index_is_set(self));
      81                 :            :     /* We store a 1-based index so 0 can mean "not initialized". */
      82                 :     129520 :     return (size_t)self->tp_subclasses - 1;
      83                 :            : }
      84                 :            : 
      85                 :            : static inline void
      86                 :       5365 : static_builtin_index_set(PyTypeObject *self, size_t index)
      87                 :            : {
      88                 :            :     assert(index < _Py_MAX_STATIC_BUILTIN_TYPES);
      89                 :            :     /* We store a 1-based index so 0 can mean "not initialized". */
      90                 :       5365 :     self->tp_subclasses = (PyObject *)(index + 1);
      91                 :       5365 : }
      92                 :            : 
      93                 :            : static inline void
      94                 :       4625 : static_builtin_index_clear(PyTypeObject *self)
      95                 :            : {
      96                 :       4625 :     self->tp_subclasses = NULL;
      97                 :       4625 : }
      98                 :            : 
      99                 :            : static inline static_builtin_state *
     100                 :     129520 : static_builtin_state_get(PyInterpreterState *interp, PyTypeObject *self)
     101                 :            : {
     102                 :     129520 :     return &(interp->types.builtins[static_builtin_index_get(self)]);
     103                 :            : }
     104                 :            : 
     105                 :            : /* For static types we store some state in an array on each interpreter. */
     106                 :            : static_builtin_state *
     107                 :     119530 : _PyStaticType_GetState(PyTypeObject *self)
     108                 :            : {
     109                 :            :     assert(self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN);
     110                 :     119530 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     111                 :     119530 :     return static_builtin_state_get(interp, self);
     112                 :            : }
     113                 :            : 
     114                 :            : static void
     115                 :       5365 : static_builtin_state_init(PyTypeObject *self)
     116                 :            : {
     117                 :            :     /* Set the type's per-interpreter state. */
     118                 :       5365 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     119                 :            : 
     120                 :            :     /* It should only be called once for each builtin type. */
     121                 :            :     assert(!static_builtin_index_is_set(self));
     122                 :            : 
     123                 :       5365 :     static_builtin_index_set(self, interp->types.num_builtins_initialized);
     124                 :       5365 :     interp->types.num_builtins_initialized++;
     125                 :            : 
     126                 :       5365 :     static_builtin_state *state = static_builtin_state_get(interp, self);
     127                 :       5365 :     state->type = self;
     128                 :            :     /* state->tp_subclasses is left NULL until init_subclasses() sets it. */
     129                 :            :     /* state->tp_weaklist is left NULL until insert_head() or insert_after()
     130                 :            :        (in weakrefobject.c) sets it. */
     131                 :       5365 : }
     132                 :            : 
     133                 :            : static void
     134                 :       4625 : static_builtin_state_clear(PyTypeObject *self)
     135                 :            : {
     136                 :            :     /* Reset the type's per-interpreter state.
     137                 :            :        This basically undoes what static_builtin_state_init() did. */
     138                 :       4625 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     139                 :            : 
     140                 :       4625 :     static_builtin_state *state = static_builtin_state_get(interp, self);
     141                 :       4625 :     state->type = NULL;
     142                 :            :     assert(state->tp_weaklist == NULL);  // It was already cleared out.
     143                 :       4625 :     static_builtin_index_clear(self);
     144                 :            : 
     145                 :            :     assert(interp->types.num_builtins_initialized > 0);
     146                 :       4625 :     interp->types.num_builtins_initialized--;
     147                 :       4625 : }
     148                 :            : 
     149                 :            : // Also see _PyStaticType_InitBuiltin() and _PyStaticType_Dealloc().
     150                 :            : 
     151                 :            : /* end static builtin helpers */
     152                 :            : 
     153                 :            : 
     154                 :            : /*
     155                 :            :  * finds the beginning of the docstring's introspection signature.
     156                 :            :  * if present, returns a pointer pointing to the first '('.
     157                 :            :  * otherwise returns NULL.
     158                 :            :  *
     159                 :            :  * doesn't guarantee that the signature is valid, only that it
     160                 :            :  * has a valid prefix.  (the signature must also pass skip_signature.)
     161                 :            :  */
     162                 :            : static const char *
     163                 :       5288 : find_signature(const char *name, const char *doc)
     164                 :            : {
     165                 :            :     const char *dot;
     166                 :            :     size_t length;
     167                 :            : 
     168         [ -  + ]:       5288 :     if (!doc)
     169                 :          0 :         return NULL;
     170                 :            : 
     171                 :            :     assert(name != NULL);
     172                 :            : 
     173                 :            :     /* for dotted names like classes, only use the last component */
     174                 :       5288 :     dot = strrchr(name, '.');
     175         [ +  + ]:       5288 :     if (dot)
     176                 :       2388 :         name = dot + 1;
     177                 :            : 
     178                 :       5288 :     length = strlen(name);
     179         [ +  + ]:       5288 :     if (strncmp(doc, name, length))
     180                 :       3238 :         return NULL;
     181                 :       2050 :     doc += length;
     182         [ +  + ]:       2050 :     if (*doc != '(')
     183                 :        367 :         return NULL;
     184                 :       1683 :     return doc;
     185                 :            : }
     186                 :            : 
     187                 :            : #define SIGNATURE_END_MARKER         ")\n--\n\n"
     188                 :            : #define SIGNATURE_END_MARKER_LENGTH  6
     189                 :            : /*
     190                 :            :  * skips past the end of the docstring's introspection signature.
     191                 :            :  * (assumes doc starts with a valid signature prefix.)
     192                 :            :  */
     193                 :            : static const char *
     194                 :       1683 : skip_signature(const char *doc)
     195                 :            : {
     196         [ +  + ]:     101542 :     while (*doc) {
     197         [ +  + ]:     101455 :         if ((*doc == *SIGNATURE_END_MARKER) &&
     198         [ +  + ]:       2770 :             !strncmp(doc, SIGNATURE_END_MARKER, SIGNATURE_END_MARKER_LENGTH))
     199                 :       1099 :             return doc + SIGNATURE_END_MARKER_LENGTH;
     200   [ +  +  +  + ]:     100356 :         if ((*doc == '\n') && (doc[1] == '\n'))
     201                 :        497 :             return NULL;
     202                 :      99859 :         doc++;
     203                 :            :     }
     204                 :         87 :     return NULL;
     205                 :            : }
     206                 :            : 
     207                 :            : int
     208                 :          0 : _PyType_CheckConsistency(PyTypeObject *type)
     209                 :            : {
     210                 :            : #define CHECK(expr) \
     211                 :            :     do { if (!(expr)) { _PyObject_ASSERT_FAILED_MSG((PyObject *)type, Py_STRINGIFY(expr)); } } while (0)
     212                 :            : 
     213         [ #  # ]:          0 :     CHECK(!_PyObject_IsFreed((PyObject *)type));
     214                 :            : 
     215         [ #  # ]:          0 :     if (!(type->tp_flags & Py_TPFLAGS_READY)) {
     216                 :            :         /* don't check static types before PyType_Ready() */
     217                 :          0 :         return 1;
     218                 :            :     }
     219                 :            : 
     220         [ #  # ]:          0 :     CHECK(Py_REFCNT(type) >= 1);
     221         [ #  # ]:          0 :     CHECK(PyType_Check(type));
     222                 :            : 
     223         [ #  # ]:          0 :     CHECK(!(type->tp_flags & Py_TPFLAGS_READYING));
     224         [ #  # ]:          0 :     CHECK(type->tp_dict != NULL);
     225                 :            : 
     226         [ #  # ]:          0 :     if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
     227                 :            :         // bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.
     228                 :            :         // Note: tp_clear is optional.
     229         [ #  # ]:          0 :         CHECK(type->tp_traverse != NULL);
     230                 :            :     }
     231                 :            : 
     232         [ #  # ]:          0 :     if (type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION) {
     233         [ #  # ]:          0 :         CHECK(type->tp_new == NULL);
     234         [ #  # ]:          0 :         CHECK(PyDict_Contains(type->tp_dict, &_Py_ID(__new__)) == 0);
     235                 :            :     }
     236                 :            : 
     237                 :          0 :     return 1;
     238                 :            : #undef CHECK
     239                 :            : }
     240                 :            : 
     241                 :            : static const char *
     242                 :       5288 : _PyType_DocWithoutSignature(const char *name, const char *internal_doc)
     243                 :            : {
     244                 :       5288 :     const char *doc = find_signature(name, internal_doc);
     245                 :            : 
     246         [ +  + ]:       5288 :     if (doc) {
     247                 :       1683 :         doc = skip_signature(doc);
     248         [ +  + ]:       1683 :         if (doc)
     249                 :       1099 :             return doc;
     250                 :            :         }
     251                 :       4189 :     return internal_doc;
     252                 :            : }
     253                 :            : 
     254                 :            : PyObject *
     255                 :        303 : _PyType_GetDocFromInternalDoc(const char *name, const char *internal_doc)
     256                 :            : {
     257                 :        303 :     const char *doc = _PyType_DocWithoutSignature(name, internal_doc);
     258                 :            : 
     259   [ +  -  -  + ]:        303 :     if (!doc || *doc == '\0') {
     260                 :          0 :         Py_RETURN_NONE;
     261                 :            :     }
     262                 :            : 
     263                 :        303 :     return PyUnicode_FromString(doc);
     264                 :            : }
     265                 :            : 
     266                 :            : PyObject *
     267                 :          0 : _PyType_GetTextSignatureFromInternalDoc(const char *name, const char *internal_doc)
     268                 :            : {
     269                 :          0 :     const char *start = find_signature(name, internal_doc);
     270                 :            :     const char *end;
     271                 :            : 
     272         [ #  # ]:          0 :     if (start)
     273                 :          0 :         end = skip_signature(start);
     274                 :            :     else
     275                 :          0 :         end = NULL;
     276         [ #  # ]:          0 :     if (!end) {
     277                 :          0 :         Py_RETURN_NONE;
     278                 :            :     }
     279                 :            : 
     280                 :            :     /* back "end" up until it points just past the final ')' */
     281                 :          0 :     end -= SIGNATURE_END_MARKER_LENGTH - 1;
     282                 :            :     assert((end - start) >= 2); /* should be "()" at least */
     283                 :            :     assert(end[-1] == ')');
     284                 :            :     assert(end[0] == '\n');
     285                 :          0 :     return PyUnicode_FromStringAndSize(start, end - start);
     286                 :            : }
     287                 :            : 
     288                 :            : 
     289                 :            : static struct type_cache*
     290                 :    9040504 : get_type_cache(void)
     291                 :            : {
     292                 :    9040504 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     293                 :    9040504 :     return &interp->types.type_cache;
     294                 :            : }
     295                 :            : 
     296                 :            : 
     297                 :            : static void
     298                 :         25 : type_cache_clear(struct type_cache *cache, PyObject *value)
     299                 :            : {
     300         [ +  + ]:     102425 :     for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
     301                 :     102400 :         struct type_cache_entry *entry = &cache->hashtable[i];
     302                 :     102400 :         entry->version = 0;
     303                 :     102400 :         Py_XSETREF(entry->name, _Py_XNewRef(value));
     304                 :     102400 :         entry->value = NULL;
     305                 :            :     }
     306                 :         25 : }
     307                 :            : 
     308                 :            : 
     309                 :            : void
     310                 :         29 : _PyType_InitCache(PyInterpreterState *interp)
     311                 :            : {
     312                 :         29 :     struct type_cache *cache = &interp->types.type_cache;
     313         [ +  + ]:     118813 :     for (Py_ssize_t i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
     314                 :     118784 :         struct type_cache_entry *entry = &cache->hashtable[i];
     315                 :            :         assert(entry->name == NULL);
     316                 :            : 
     317                 :     118784 :         entry->version = 0;
     318                 :            :         // Set to None so _PyType_Lookup() can use Py_SETREF(),
     319                 :            :         // rather than using slower Py_XSETREF().
     320                 :     118784 :         entry->name = Py_NewRef(Py_None);
     321                 :     118784 :         entry->value = NULL;
     322                 :            :     }
     323                 :         29 : }
     324                 :            : 
     325                 :            : 
     326                 :            : static unsigned int
     327                 :          0 : _PyType_ClearCache(PyInterpreterState *interp)
     328                 :            : {
     329                 :          0 :     struct type_cache *cache = &interp->types.type_cache;
     330                 :            :     // Set to None, rather than NULL, so _PyType_Lookup() can
     331                 :            :     // use Py_SETREF() rather than using slower Py_XSETREF().
     332                 :          0 :     type_cache_clear(cache, Py_None);
     333                 :            : 
     334                 :          0 :     return next_version_tag - 1;
     335                 :            : }
     336                 :            : 
     337                 :            : 
     338                 :            : unsigned int
     339                 :          0 : PyType_ClearCache(void)
     340                 :            : {
     341                 :          0 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     342                 :          0 :     return _PyType_ClearCache(interp);
     343                 :            : }
     344                 :            : 
     345                 :            : 
     346                 :            : void
     347                 :         25 : _PyTypes_Fini(PyInterpreterState *interp)
     348                 :            : {
     349                 :         25 :     struct type_cache *cache = &interp->types.type_cache;
     350                 :         25 :     type_cache_clear(cache, NULL);
     351                 :            : 
     352                 :            :     assert(interp->types.num_builtins_initialized == 0);
     353                 :            :     // All the static builtin types should have been finalized already.
     354         [ +  + ]:       5025 :     for (size_t i = 0; i < _Py_MAX_STATIC_BUILTIN_TYPES; i++) {
     355                 :            :         assert(interp->types.builtins[i].type == NULL);
     356                 :            :     }
     357                 :         25 : }
     358                 :            : 
     359                 :            : 
     360                 :            : static PyObject * lookup_subclasses(PyTypeObject *);
     361                 :            : 
     362                 :            : int
     363                 :          0 : PyType_AddWatcher(PyType_WatchCallback callback)
     364                 :            : {
     365                 :          0 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     366                 :            : 
     367         [ #  # ]:          0 :     for (int i = 0; i < TYPE_MAX_WATCHERS; i++) {
     368         [ #  # ]:          0 :         if (!interp->type_watchers[i]) {
     369                 :          0 :             interp->type_watchers[i] = callback;
     370                 :          0 :             return i;
     371                 :            :         }
     372                 :            :     }
     373                 :            : 
     374                 :          0 :     PyErr_SetString(PyExc_RuntimeError, "no more type watcher IDs available");
     375                 :          0 :     return -1;
     376                 :            : }
     377                 :            : 
     378                 :            : static inline int
     379                 :          0 : validate_watcher_id(PyInterpreterState *interp, int watcher_id)
     380                 :            : {
     381   [ #  #  #  # ]:          0 :     if (watcher_id < 0 || watcher_id >= TYPE_MAX_WATCHERS) {
     382                 :          0 :         PyErr_Format(PyExc_ValueError, "Invalid type watcher ID %d", watcher_id);
     383                 :          0 :         return -1;
     384                 :            :     }
     385         [ #  # ]:          0 :     if (!interp->type_watchers[watcher_id]) {
     386                 :          0 :         PyErr_Format(PyExc_ValueError, "No type watcher set for ID %d", watcher_id);
     387                 :          0 :         return -1;
     388                 :            :     }
     389                 :          0 :     return 0;
     390                 :            : }
     391                 :            : 
     392                 :            : int
     393                 :          0 : PyType_ClearWatcher(int watcher_id)
     394                 :            : {
     395                 :          0 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     396         [ #  # ]:          0 :     if (validate_watcher_id(interp, watcher_id) < 0) {
     397                 :          0 :         return -1;
     398                 :            :     }
     399                 :          0 :     interp->type_watchers[watcher_id] = NULL;
     400                 :          0 :     return 0;
     401                 :            : }
     402                 :            : 
     403                 :            : static int assign_version_tag(PyTypeObject *type);
     404                 :            : 
     405                 :            : int
     406                 :          0 : PyType_Watch(int watcher_id, PyObject* obj)
     407                 :            : {
     408         [ #  # ]:          0 :     if (!PyType_Check(obj)) {
     409                 :          0 :         PyErr_SetString(PyExc_ValueError, "Cannot watch non-type");
     410                 :          0 :         return -1;
     411                 :            :     }
     412                 :          0 :     PyTypeObject *type = (PyTypeObject *)obj;
     413                 :          0 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     414         [ #  # ]:          0 :     if (validate_watcher_id(interp, watcher_id) < 0) {
     415                 :          0 :         return -1;
     416                 :            :     }
     417                 :            :     // ensure we will get a callback on the next modification
     418                 :          0 :     assign_version_tag(type);
     419                 :          0 :     type->tp_watched |= (1 << watcher_id);
     420                 :          0 :     return 0;
     421                 :            : }
     422                 :            : 
     423                 :            : int
     424                 :          0 : PyType_Unwatch(int watcher_id, PyObject* obj)
     425                 :            : {
     426         [ #  # ]:          0 :     if (!PyType_Check(obj)) {
     427                 :          0 :         PyErr_SetString(PyExc_ValueError, "Cannot watch non-type");
     428                 :          0 :         return -1;
     429                 :            :     }
     430                 :          0 :     PyTypeObject *type = (PyTypeObject *)obj;
     431                 :          0 :     PyInterpreterState *interp = _PyInterpreterState_GET();
     432         [ #  # ]:          0 :     if (validate_watcher_id(interp, watcher_id)) {
     433                 :          0 :         return -1;
     434                 :            :     }
     435                 :          0 :     type->tp_watched &= ~(1 << watcher_id);
     436                 :          0 :     return 0;
     437                 :            : }
     438                 :            : 
     439                 :            : void
     440                 :      25956 : PyType_Modified(PyTypeObject *type)
     441                 :            : {
     442                 :            :     /* Invalidate any cached data for the specified type and all
     443                 :            :        subclasses.  This function is called after the base
     444                 :            :        classes, mro, or attributes of the type are altered.
     445                 :            : 
     446                 :            :        Invariants:
     447                 :            : 
     448                 :            :        - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
     449                 :            :          it must first be set on all super types.
     450                 :            : 
     451                 :            :        This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
     452                 :            :        type (so it must first clear it on all subclasses).  The
     453                 :            :        tp_version_tag value is meaningless unless this flag is set.
     454                 :            :        We don't assign new version tags eagerly, but only as
     455                 :            :        needed.
     456                 :            :      */
     457         [ +  + ]:      25956 :     if (!_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
     458                 :      22582 :         return;
     459                 :            :     }
     460                 :            : 
     461                 :       3374 :     PyObject *subclasses = lookup_subclasses(type);
     462         [ +  + ]:       3374 :     if (subclasses != NULL) {
     463                 :            :         assert(PyDict_CheckExact(subclasses));
     464                 :            : 
     465                 :        563 :         Py_ssize_t i = 0;
     466                 :            :         PyObject *ref;
     467         [ +  + ]:       1875 :         while (PyDict_Next(subclasses, &i, NULL, &ref)) {
     468                 :       1312 :             PyTypeObject *subclass = subclass_from_ref(ref);  // borrowed
     469         [ +  + ]:       1312 :             if (subclass == NULL) {
     470                 :       1310 :                 continue;
     471                 :            :             }
     472                 :          2 :             PyType_Modified(subclass);
     473                 :            :         }
     474                 :            :     }
     475                 :            : 
     476                 :            :     // Notify registered type watchers, if any
     477         [ -  + ]:       3374 :     if (type->tp_watched) {
     478                 :          0 :         PyInterpreterState *interp = _PyInterpreterState_GET();
     479                 :          0 :         int bits = type->tp_watched;
     480                 :          0 :         int i = 0;
     481         [ #  # ]:          0 :         while (bits) {
     482                 :            :             assert(i < TYPE_MAX_WATCHERS);
     483         [ #  # ]:          0 :             if (bits & 1) {
     484                 :          0 :                 PyType_WatchCallback cb = interp->type_watchers[i];
     485   [ #  #  #  # ]:          0 :                 if (cb && (cb(type) < 0)) {
     486                 :          0 :                     PyErr_WriteUnraisable((PyObject *)type);
     487                 :            :                 }
     488                 :            :             }
     489                 :          0 :             i++;
     490                 :          0 :             bits >>= 1;
     491                 :            :         }
     492                 :            :     }
     493                 :            : 
     494                 :       3374 :     type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
     495                 :       3374 :     type->tp_version_tag = 0; /* 0 is not a valid version tag */
     496                 :            : }
     497                 :            : 
     498                 :            : static void
     499                 :      25816 : type_mro_modified(PyTypeObject *type, PyObject *bases) {
     500                 :            :     /*
     501                 :            :        Check that all base classes or elements of the MRO of type are
     502                 :            :        able to be cached.  This function is called after the base
     503                 :            :        classes or mro of the type are altered.
     504                 :            : 
     505                 :            :        Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
     506                 :            :        has a custom MRO that includes a type which is not officially
     507                 :            :        super type, or if the type implements its own mro() method.
     508                 :            : 
     509                 :            :        Called from mro_internal, which will subsequently be called on
     510                 :            :        each subclass when their mro is recursively updated.
     511                 :            :      */
     512                 :            :     Py_ssize_t i, n;
     513                 :      25816 :     int custom = !Py_IS_TYPE(type, &PyType_Type);
     514                 :            :     int unbound;
     515                 :            : 
     516         [ +  + ]:      25816 :     if (custom) {
     517                 :            :         PyObject *mro_meth, *type_mro_meth;
     518                 :       2236 :         mro_meth = lookup_maybe_method(
     519                 :            :             (PyObject *)type, &_Py_ID(mro), &unbound);
     520         [ -  + ]:       2236 :         if (mro_meth == NULL) {
     521                 :          0 :             goto clear;
     522                 :            :         }
     523                 :       2236 :         type_mro_meth = lookup_maybe_method(
     524                 :            :             (PyObject *)&PyType_Type, &_Py_ID(mro), &unbound);
     525         [ -  + ]:       2236 :         if (type_mro_meth == NULL) {
     526                 :          0 :             Py_DECREF(mro_meth);
     527                 :          0 :             goto clear;
     528                 :            :         }
     529                 :       2236 :         int custom_mro = (mro_meth != type_mro_meth);
     530                 :       2236 :         Py_DECREF(mro_meth);
     531                 :       2236 :         Py_DECREF(type_mro_meth);
     532         [ -  + ]:       2236 :         if (custom_mro) {
     533                 :          0 :             goto clear;
     534                 :            :         }
     535                 :            :     }
     536                 :      25816 :     n = PyTuple_GET_SIZE(bases);
     537         [ +  + ]:      82139 :     for (i = 0; i < n; i++) {
     538                 :      56323 :         PyObject *b = PyTuple_GET_ITEM(bases, i);
     539                 :      56323 :         PyTypeObject *cls = _PyType_CAST(b);
     540                 :            : 
     541         [ -  + ]:      56323 :         if (!PyType_IsSubtype(type, cls)) {
     542                 :          0 :             goto clear;
     543                 :            :         }
     544                 :            :     }
     545                 :      25816 :     return;
     546                 :          0 :  clear:
     547                 :          0 :     type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
     548                 :          0 :     type->tp_version_tag = 0; /* 0 is not a valid version tag */
     549                 :            : }
     550                 :            : 
     551                 :            : static int
     552                 :    1505742 : assign_version_tag(PyTypeObject *type)
     553                 :            : {
     554                 :            :     /* Ensure that the tp_version_tag is valid and set
     555                 :            :        Py_TPFLAGS_VALID_VERSION_TAG.  To respect the invariant, this
     556                 :            :        must first be done on all super classes.  Return 0 if this
     557                 :            :        cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
     558                 :            :     */
     559         [ +  + ]:    1505742 :     if (_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
     560                 :    1501270 :         return 1;
     561                 :            :     }
     562         [ -  + ]:       4472 :     if (!_PyType_HasFeature(type, Py_TPFLAGS_READY)) {
     563                 :          0 :         return 0;
     564                 :            :     }
     565                 :            : 
     566         [ -  + ]:       4472 :     if (next_version_tag == 0) {
     567                 :            :         /* We have run out of version numbers */
     568                 :          0 :         return 0;
     569                 :            :     }
     570                 :       4472 :     type->tp_version_tag = next_version_tag++;
     571                 :            :     assert (type->tp_version_tag != 0);
     572                 :            : 
     573                 :       4472 :     PyObject *bases = type->tp_bases;
     574                 :       4472 :     Py_ssize_t n = PyTuple_GET_SIZE(bases);
     575         [ +  + ]:       9494 :     for (Py_ssize_t i = 0; i < n; i++) {
     576                 :       5022 :         PyObject *b = PyTuple_GET_ITEM(bases, i);
     577         [ -  + ]:       5022 :         if (!assign_version_tag(_PyType_CAST(b)))
     578                 :          0 :             return 0;
     579                 :            :     }
     580                 :       4472 :     type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
     581                 :       4472 :     return 1;
     582                 :            : }
     583                 :            : 
     584                 :            : 
     585                 :            : static PyMemberDef type_members[] = {
     586                 :            :     {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},
     587                 :            :     {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},
     588                 :            :     {"__flags__", T_ULONG, offsetof(PyTypeObject, tp_flags), READONLY},
     589                 :            :     /* Note that this value is misleading for static builtin types,
     590                 :            :        since the memory at this offset will always be NULL. */
     591                 :            :     {"__weakrefoffset__", T_PYSSIZET,
     592                 :            :      offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
     593                 :            :     {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
     594                 :            :     {"__dictoffset__", T_PYSSIZET,
     595                 :            :      offsetof(PyTypeObject, tp_dictoffset), READONLY},
     596                 :            :     {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
     597                 :            :     {0}
     598                 :            : };
     599                 :            : 
     600                 :            : static int
     601                 :         92 : check_set_special_type_attr(PyTypeObject *type, PyObject *value, const char *name)
     602                 :            : {
     603         [ -  + ]:         92 :     if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
     604                 :          0 :         PyErr_Format(PyExc_TypeError,
     605                 :            :                      "cannot set '%s' attribute of immutable type '%s'",
     606                 :            :                      name, type->tp_name);
     607                 :          0 :         return 0;
     608                 :            :     }
     609         [ -  + ]:         92 :     if (!value) {
     610                 :          0 :         PyErr_Format(PyExc_TypeError,
     611                 :            :                      "cannot delete '%s' attribute of immutable type '%s'",
     612                 :            :                      name, type->tp_name);
     613                 :          0 :         return 0;
     614                 :            :     }
     615                 :            : 
     616         [ -  + ]:         92 :     if (PySys_Audit("object.__setattr__", "OsO",
     617                 :            :                     type, name, value) < 0) {
     618                 :          0 :         return 0;
     619                 :            :     }
     620                 :            : 
     621                 :         92 :     return 1;
     622                 :            : }
     623                 :            : 
     624                 :            : const char *
     625                 :       1245 : _PyType_Name(PyTypeObject *type)
     626                 :            : {
     627                 :            :     assert(type->tp_name != NULL);
     628                 :       1245 :     const char *s = strrchr(type->tp_name, '.');
     629         [ +  + ]:       1245 :     if (s == NULL) {
     630                 :         71 :         s = type->tp_name;
     631                 :            :     }
     632                 :            :     else {
     633                 :       1174 :         s++;
     634                 :            :     }
     635                 :       1245 :     return s;
     636                 :            : }
     637                 :            : 
     638                 :            : static PyObject *
     639                 :        854 : type_name(PyTypeObject *type, void *context)
     640                 :            : {
     641         [ +  + ]:        854 :     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
     642                 :        659 :         PyHeapTypeObject* et = (PyHeapTypeObject*)type;
     643                 :            : 
     644                 :        659 :         return Py_NewRef(et->ht_name);
     645                 :            :     }
     646                 :            :     else {
     647                 :        195 :         return PyUnicode_FromString(_PyType_Name(type));
     648                 :            :     }
     649                 :            : }
     650                 :            : 
     651                 :            : static PyObject *
     652                 :        428 : type_qualname(PyTypeObject *type, void *context)
     653                 :            : {
     654         [ +  + ]:        428 :     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
     655                 :        195 :         PyHeapTypeObject* et = (PyHeapTypeObject*)type;
     656                 :        195 :         return Py_NewRef(et->ht_qualname);
     657                 :            :     }
     658                 :            :     else {
     659                 :        233 :         return PyUnicode_FromString(_PyType_Name(type));
     660                 :            :     }
     661                 :            : }
     662                 :            : 
     663                 :            : static int
     664                 :          6 : type_set_name(PyTypeObject *type, PyObject *value, void *context)
     665                 :            : {
     666                 :            :     const char *tp_name;
     667                 :            :     Py_ssize_t name_size;
     668                 :            : 
     669         [ -  + ]:          6 :     if (!check_set_special_type_attr(type, value, "__name__"))
     670                 :          0 :         return -1;
     671         [ -  + ]:          6 :     if (!PyUnicode_Check(value)) {
     672                 :          0 :         PyErr_Format(PyExc_TypeError,
     673                 :            :                      "can only assign string to %s.__name__, not '%s'",
     674                 :          0 :                      type->tp_name, Py_TYPE(value)->tp_name);
     675                 :          0 :         return -1;
     676                 :            :     }
     677                 :            : 
     678                 :          6 :     tp_name = PyUnicode_AsUTF8AndSize(value, &name_size);
     679         [ -  + ]:          6 :     if (tp_name == NULL)
     680                 :          0 :         return -1;
     681         [ -  + ]:          6 :     if (strlen(tp_name) != (size_t)name_size) {
     682                 :          0 :         PyErr_SetString(PyExc_ValueError,
     683                 :            :                         "type name must not contain null characters");
     684                 :          0 :         return -1;
     685                 :            :     }
     686                 :            : 
     687                 :          6 :     type->tp_name = tp_name;
     688                 :          6 :     Py_SETREF(((PyHeapTypeObject*)type)->ht_name, Py_NewRef(value));
     689                 :            : 
     690                 :          6 :     return 0;
     691                 :            : }
     692                 :            : 
     693                 :            : static int
     694                 :          0 : type_set_qualname(PyTypeObject *type, PyObject *value, void *context)
     695                 :            : {
     696                 :            :     PyHeapTypeObject* et;
     697                 :            : 
     698         [ #  # ]:          0 :     if (!check_set_special_type_attr(type, value, "__qualname__"))
     699                 :          0 :         return -1;
     700         [ #  # ]:          0 :     if (!PyUnicode_Check(value)) {
     701                 :          0 :         PyErr_Format(PyExc_TypeError,
     702                 :            :                      "can only assign string to %s.__qualname__, not '%s'",
     703                 :          0 :                      type->tp_name, Py_TYPE(value)->tp_name);
     704                 :          0 :         return -1;
     705                 :            :     }
     706                 :            : 
     707                 :          0 :     et = (PyHeapTypeObject*)type;
     708                 :          0 :     Py_SETREF(et->ht_qualname, Py_NewRef(value));
     709                 :          0 :     return 0;
     710                 :            : }
     711                 :            : 
     712                 :            : static PyObject *
     713                 :        534 : type_module(PyTypeObject *type, void *context)
     714                 :            : {
     715                 :            :     PyObject *mod;
     716                 :            : 
     717         [ +  + ]:        534 :     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
     718                 :        292 :         mod = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__module__));
     719         [ -  + ]:        292 :         if (mod == NULL) {
     720         [ #  # ]:          0 :             if (!PyErr_Occurred()) {
     721                 :          0 :                 PyErr_Format(PyExc_AttributeError, "__module__");
     722                 :            :             }
     723                 :          0 :             return NULL;
     724                 :            :         }
     725                 :        292 :         Py_INCREF(mod);
     726                 :            :     }
     727                 :            :     else {
     728                 :        242 :         const char *s = strrchr(type->tp_name, '.');
     729         [ +  + ]:        242 :         if (s != NULL) {
     730                 :        412 :             mod = PyUnicode_FromStringAndSize(
     731                 :        206 :                 type->tp_name, (Py_ssize_t)(s - type->tp_name));
     732         [ +  - ]:        206 :             if (mod != NULL)
     733                 :        206 :                 PyUnicode_InternInPlace(&mod);
     734                 :            :         }
     735                 :            :         else {
     736                 :         36 :             mod = Py_NewRef(&_Py_ID(builtins));
     737                 :            :         }
     738                 :            :     }
     739                 :        534 :     return mod;
     740                 :            : }
     741                 :            : 
     742                 :            : static int
     743                 :         82 : type_set_module(PyTypeObject *type, PyObject *value, void *context)
     744                 :            : {
     745         [ -  + ]:         82 :     if (!check_set_special_type_attr(type, value, "__module__"))
     746                 :          0 :         return -1;
     747                 :            : 
     748                 :         82 :     PyType_Modified(type);
     749                 :            : 
     750                 :         82 :     return PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), value);
     751                 :            : }
     752                 :            : 
     753                 :            : static PyObject *
     754                 :       1217 : type_abstractmethods(PyTypeObject *type, void *context)
     755                 :            : {
     756                 :       1217 :     PyObject *mod = NULL;
     757                 :            :     /* type itself has an __abstractmethods__ descriptor (this). Don't return
     758                 :            :        that. */
     759         [ +  - ]:       1217 :     if (type != &PyType_Type)
     760                 :       1217 :         mod = PyDict_GetItemWithError(type->tp_dict,
     761                 :            :                                       &_Py_ID(__abstractmethods__));
     762         [ +  + ]:       1217 :     if (!mod) {
     763         [ +  - ]:        335 :         if (!PyErr_Occurred()) {
     764                 :        335 :             PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
     765                 :            :         }
     766                 :        335 :         return NULL;
     767                 :            :     }
     768                 :        882 :     return Py_NewRef(mod);
     769                 :            : }
     770                 :            : 
     771                 :            : static int
     772                 :        962 : type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
     773                 :            : {
     774                 :            :     /* __abstractmethods__ should only be set once on a type, in
     775                 :            :        abc.ABCMeta.__new__, so this function doesn't do anything
     776                 :            :        special to update subclasses.
     777                 :            :     */
     778                 :            :     int abstract, res;
     779         [ +  - ]:        962 :     if (value != NULL) {
     780                 :        962 :         abstract = PyObject_IsTrue(value);
     781         [ -  + ]:        962 :         if (abstract < 0)
     782                 :          0 :             return -1;
     783                 :        962 :         res = PyDict_SetItem(type->tp_dict, &_Py_ID(__abstractmethods__), value);
     784                 :            :     }
     785                 :            :     else {
     786                 :          0 :         abstract = 0;
     787                 :          0 :         res = PyDict_DelItem(type->tp_dict, &_Py_ID(__abstractmethods__));
     788   [ #  #  #  # ]:          0 :         if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
     789                 :          0 :             PyErr_SetObject(PyExc_AttributeError, &_Py_ID(__abstractmethods__));
     790                 :          0 :             return -1;
     791                 :            :         }
     792                 :            :     }
     793         [ +  - ]:        962 :     if (res == 0) {
     794                 :        962 :         PyType_Modified(type);
     795         [ +  + ]:        962 :         if (abstract)
     796                 :        589 :             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
     797                 :            :         else
     798                 :        373 :             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
     799                 :            :     }
     800                 :        962 :     return res;
     801                 :            : }
     802                 :            : 
     803                 :            : static PyObject *
     804                 :       1028 : type_get_bases(PyTypeObject *type, void *context)
     805                 :            : {
     806                 :       1028 :     return Py_NewRef(type->tp_bases);
     807                 :            : }
     808                 :            : 
     809                 :            : static PyTypeObject *best_base(PyObject *);
     810                 :            : static int mro_internal(PyTypeObject *, PyObject **);
     811                 :            : static int type_is_subtype_base_chain(PyTypeObject *, PyTypeObject *);
     812                 :            : static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, const char *);
     813                 :            : static int add_subclass(PyTypeObject*, PyTypeObject*);
     814                 :            : static int add_all_subclasses(PyTypeObject *type, PyObject *bases);
     815                 :            : static void remove_subclass(PyTypeObject *, PyTypeObject *);
     816                 :            : static void remove_all_subclasses(PyTypeObject *type, PyObject *bases);
     817                 :            : static void update_all_slots(PyTypeObject *);
     818                 :            : 
     819                 :            : typedef int (*update_callback)(PyTypeObject *, void *);
     820                 :            : static int update_subclasses(PyTypeObject *type, PyObject *attr_name,
     821                 :            :                              update_callback callback, void *data);
     822                 :            : static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
     823                 :            :                                    update_callback callback, void *data);
     824                 :            : 
     825                 :            : static int
     826                 :          0 : mro_hierarchy(PyTypeObject *type, PyObject *temp)
     827                 :            : {
     828                 :            :     PyObject *old_mro;
     829                 :          0 :     int res = mro_internal(type, &old_mro);
     830         [ #  # ]:          0 :     if (res <= 0) {
     831                 :            :         /* error / reentrance */
     832                 :          0 :         return res;
     833                 :            :     }
     834                 :          0 :     PyObject *new_mro = type->tp_mro;
     835                 :            : 
     836                 :            :     PyObject *tuple;
     837         [ #  # ]:          0 :     if (old_mro != NULL) {
     838                 :          0 :         tuple = PyTuple_Pack(3, type, new_mro, old_mro);
     839                 :            :     }
     840                 :            :     else {
     841                 :          0 :         tuple = PyTuple_Pack(2, type, new_mro);
     842                 :            :     }
     843                 :            : 
     844         [ #  # ]:          0 :     if (tuple != NULL) {
     845                 :          0 :         res = PyList_Append(temp, tuple);
     846                 :            :     }
     847                 :            :     else {
     848                 :          0 :         res = -1;
     849                 :            :     }
     850                 :          0 :     Py_XDECREF(tuple);
     851                 :            : 
     852         [ #  # ]:          0 :     if (res < 0) {
     853                 :          0 :         type->tp_mro = old_mro;
     854                 :          0 :         Py_DECREF(new_mro);
     855                 :          0 :         return -1;
     856                 :            :     }
     857                 :          0 :     Py_XDECREF(old_mro);
     858                 :            : 
     859                 :            :     // Avoid creating an empty list if there is no subclass
     860         [ #  # ]:          0 :     if (_PyType_HasSubclasses(type)) {
     861                 :            :         /* Obtain a copy of subclasses list to iterate over.
     862                 :            : 
     863                 :            :            Otherwise type->tp_subclasses might be altered
     864                 :            :            in the middle of the loop, for example, through a custom mro(),
     865                 :            :            by invoking type_set_bases on some subclass of the type
     866                 :            :            which in turn calls remove_subclass/add_subclass on this type.
     867                 :            : 
     868                 :            :            Finally, this makes things simple avoiding the need to deal
     869                 :            :            with dictionary iterators and weak references.
     870                 :            :         */
     871                 :          0 :         PyObject *subclasses = _PyType_GetSubclasses(type);
     872         [ #  # ]:          0 :         if (subclasses == NULL) {
     873                 :          0 :             return -1;
     874                 :            :         }
     875                 :            : 
     876                 :          0 :         Py_ssize_t n = PyList_GET_SIZE(subclasses);
     877         [ #  # ]:          0 :         for (Py_ssize_t i = 0; i < n; i++) {
     878                 :          0 :             PyTypeObject *subclass = _PyType_CAST(PyList_GET_ITEM(subclasses, i));
     879                 :          0 :             res = mro_hierarchy(subclass, temp);
     880         [ #  # ]:          0 :             if (res < 0) {
     881                 :          0 :                 break;
     882                 :            :             }
     883                 :            :         }
     884                 :          0 :         Py_DECREF(subclasses);
     885                 :            :     }
     886                 :            : 
     887                 :          0 :     return res;
     888                 :            : }
     889                 :            : 
     890                 :            : static int
     891                 :          0 : type_set_bases(PyTypeObject *type, PyObject *new_bases, void *context)
     892                 :            : {
     893                 :            :     // Check arguments
     894         [ #  # ]:          0 :     if (!check_set_special_type_attr(type, new_bases, "__bases__")) {
     895                 :          0 :         return -1;
     896                 :            :     }
     897                 :            :     assert(new_bases != NULL);
     898                 :            : 
     899         [ #  # ]:          0 :     if (!PyTuple_Check(new_bases)) {
     900                 :          0 :         PyErr_Format(PyExc_TypeError,
     901                 :            :              "can only assign tuple to %s.__bases__, not %s",
     902                 :          0 :                  type->tp_name, Py_TYPE(new_bases)->tp_name);
     903                 :          0 :         return -1;
     904                 :            :     }
     905         [ #  # ]:          0 :     if (PyTuple_GET_SIZE(new_bases) == 0) {
     906                 :          0 :         PyErr_Format(PyExc_TypeError,
     907                 :            :              "can only assign non-empty tuple to %s.__bases__, not ()",
     908                 :            :                  type->tp_name);
     909                 :          0 :         return -1;
     910                 :            :     }
     911                 :          0 :     Py_ssize_t n = PyTuple_GET_SIZE(new_bases);
     912         [ #  # ]:          0 :     for (Py_ssize_t i = 0; i < n; i++) {
     913                 :          0 :         PyObject *ob = PyTuple_GET_ITEM(new_bases, i);
     914         [ #  # ]:          0 :         if (!PyType_Check(ob)) {
     915                 :          0 :             PyErr_Format(PyExc_TypeError,
     916                 :            :                          "%s.__bases__ must be tuple of classes, not '%s'",
     917                 :          0 :                          type->tp_name, Py_TYPE(ob)->tp_name);
     918                 :          0 :             return -1;
     919                 :            :         }
     920                 :          0 :         PyTypeObject *base = (PyTypeObject*)ob;
     921                 :            : 
     922         [ #  # ]:          0 :         if (PyType_IsSubtype(base, type) ||
     923                 :            :             /* In case of reentering here again through a custom mro()
     924                 :            :                the above check is not enough since it relies on
     925                 :            :                base->tp_mro which would gonna be updated inside
     926                 :            :                mro_internal only upon returning from the mro().
     927                 :            : 
     928                 :            :                However, base->tp_base has already been assigned (see
     929                 :            :                below), which in turn may cause an inheritance cycle
     930                 :            :                through tp_base chain.  And this is definitely
     931                 :            :                not what you want to ever happen.  */
     932   [ #  #  #  # ]:          0 :             (base->tp_mro != NULL && type_is_subtype_base_chain(base, type)))
     933                 :            :         {
     934                 :          0 :             PyErr_SetString(PyExc_TypeError,
     935                 :            :                             "a __bases__ item causes an inheritance cycle");
     936                 :          0 :             return -1;
     937                 :            :         }
     938                 :            :     }
     939                 :            : 
     940                 :            :     // Compute the new MRO and the new base class
     941                 :          0 :     PyTypeObject *new_base = best_base(new_bases);
     942         [ #  # ]:          0 :     if (new_base == NULL)
     943                 :          0 :         return -1;
     944                 :            : 
     945         [ #  # ]:          0 :     if (!compatible_for_assignment(type->tp_base, new_base, "__bases__")) {
     946                 :          0 :         return -1;
     947                 :            :     }
     948                 :            : 
     949                 :          0 :     PyObject *old_bases = type->tp_bases;
     950                 :            :     assert(old_bases != NULL);
     951                 :          0 :     PyTypeObject *old_base = type->tp_base;
     952                 :            : 
     953                 :          0 :     type->tp_bases = Py_NewRef(new_bases);
     954                 :          0 :     type->tp_base = (PyTypeObject *)Py_NewRef(new_base);
     955                 :            : 
     956                 :          0 :     PyObject *temp = PyList_New(0);
     957         [ #  # ]:          0 :     if (temp == NULL) {
     958                 :          0 :         goto bail;
     959                 :            :     }
     960         [ #  # ]:          0 :     if (mro_hierarchy(type, temp) < 0) {
     961                 :          0 :         goto undo;
     962                 :            :     }
     963                 :          0 :     Py_DECREF(temp);
     964                 :            : 
     965                 :            :     /* Take no action in case if type->tp_bases has been replaced
     966                 :            :        through reentrance.  */
     967                 :            :     int res;
     968         [ #  # ]:          0 :     if (type->tp_bases == new_bases) {
     969                 :            :         /* any base that was in __bases__ but now isn't, we
     970                 :            :            need to remove |type| from its tp_subclasses.
     971                 :            :            conversely, any class now in __bases__ that wasn't
     972                 :            :            needs to have |type| added to its subclasses. */
     973                 :            : 
     974                 :            :         /* for now, sod that: just remove from all old_bases,
     975                 :            :            add to all new_bases */
     976                 :          0 :         remove_all_subclasses(type, old_bases);
     977                 :          0 :         res = add_all_subclasses(type, new_bases);
     978                 :          0 :         update_all_slots(type);
     979                 :            :     }
     980                 :            :     else {
     981                 :          0 :         res = 0;
     982                 :            :     }
     983                 :            : 
     984                 :          0 :     Py_DECREF(old_bases);
     985                 :          0 :     Py_DECREF(old_base);
     986                 :            : 
     987                 :            :     assert(_PyType_CheckConsistency(type));
     988                 :          0 :     return res;
     989                 :            : 
     990                 :          0 :   undo:
     991                 :          0 :     n = PyList_GET_SIZE(temp);
     992         [ #  # ]:          0 :     for (Py_ssize_t i = n - 1; i >= 0; i--) {
     993                 :            :         PyTypeObject *cls;
     994                 :          0 :         PyObject *new_mro, *old_mro = NULL;
     995                 :            : 
     996                 :          0 :         PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
     997                 :            :                           "", 2, 3, &cls, &new_mro, &old_mro);
     998                 :            :         /* Do not rollback if cls has a newer version of MRO.  */
     999         [ #  # ]:          0 :         if (cls->tp_mro == new_mro) {
    1000                 :          0 :             cls->tp_mro = Py_XNewRef(old_mro);
    1001                 :          0 :             Py_DECREF(new_mro);
    1002                 :            :         }
    1003                 :            :     }
    1004                 :          0 :     Py_DECREF(temp);
    1005                 :            : 
    1006                 :          0 :   bail:
    1007         [ #  # ]:          0 :     if (type->tp_bases == new_bases) {
    1008                 :            :         assert(type->tp_base == new_base);
    1009                 :            : 
    1010                 :          0 :         type->tp_bases = old_bases;
    1011                 :          0 :         type->tp_base = old_base;
    1012                 :            : 
    1013                 :          0 :         Py_DECREF(new_bases);
    1014                 :          0 :         Py_DECREF(new_base);
    1015                 :            :     }
    1016                 :            :     else {
    1017                 :          0 :         Py_DECREF(old_bases);
    1018                 :          0 :         Py_DECREF(old_base);
    1019                 :            :     }
    1020                 :            : 
    1021                 :            :     assert(_PyType_CheckConsistency(type));
    1022                 :          0 :     return -1;
    1023                 :            : }
    1024                 :            : 
    1025                 :            : static PyObject *
    1026                 :       4281 : type_dict(PyTypeObject *type, void *context)
    1027                 :            : {
    1028         [ -  + ]:       4281 :     if (type->tp_dict == NULL) {
    1029                 :          0 :         Py_RETURN_NONE;
    1030                 :            :     }
    1031                 :       4281 :     return PyDictProxy_New(type->tp_dict);
    1032                 :            : }
    1033                 :            : 
    1034                 :            : static PyObject *
    1035                 :        313 : type_get_doc(PyTypeObject *type, void *context)
    1036                 :            : {
    1037                 :            :     PyObject *result;
    1038   [ +  +  +  - ]:        313 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL) {
    1039                 :        288 :         return _PyType_GetDocFromInternalDoc(type->tp_name, type->tp_doc);
    1040                 :            :     }
    1041                 :         25 :     result = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__));
    1042         [ -  + ]:         25 :     if (result == NULL) {
    1043         [ #  # ]:          0 :         if (!PyErr_Occurred()) {
    1044                 :          0 :             result = Py_NewRef(Py_None);
    1045                 :            :         }
    1046                 :            :     }
    1047         [ -  + ]:         25 :     else if (Py_TYPE(result)->tp_descr_get) {
    1048                 :          0 :         result = Py_TYPE(result)->tp_descr_get(result, NULL,
    1049                 :            :                                                (PyObject *)type);
    1050                 :            :     }
    1051                 :            :     else {
    1052                 :         25 :         Py_INCREF(result);
    1053                 :            :     }
    1054                 :         25 :     return result;
    1055                 :            : }
    1056                 :            : 
    1057                 :            : static PyObject *
    1058                 :          0 : type_get_text_signature(PyTypeObject *type, void *context)
    1059                 :            : {
    1060                 :          0 :     return _PyType_GetTextSignatureFromInternalDoc(type->tp_name, type->tp_doc);
    1061                 :            : }
    1062                 :            : 
    1063                 :            : static int
    1064                 :          4 : type_set_doc(PyTypeObject *type, PyObject *value, void *context)
    1065                 :            : {
    1066         [ -  + ]:          4 :     if (!check_set_special_type_attr(type, value, "__doc__"))
    1067                 :          0 :         return -1;
    1068                 :          4 :     PyType_Modified(type);
    1069                 :          4 :     return PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), value);
    1070                 :            : }
    1071                 :            : 
    1072                 :            : static PyObject *
    1073                 :        213 : type_get_annotations(PyTypeObject *type, void *context)
    1074                 :            : {
    1075         [ +  + ]:        213 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    1076                 :        188 :         PyErr_Format(PyExc_AttributeError, "type object '%s' has no attribute '__annotations__'", type->tp_name);
    1077                 :        188 :         return NULL;
    1078                 :            :     }
    1079                 :            : 
    1080                 :            :     PyObject *annotations;
    1081                 :            :     /* there's no _PyDict_GetItemId without WithError, so let's LBYL. */
    1082         [ -  + ]:         25 :     if (PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) {
    1083                 :          0 :         annotations = PyDict_GetItemWithError(
    1084                 :            :                 type->tp_dict, &_Py_ID(__annotations__));
    1085                 :            :         /*
    1086                 :            :         ** PyDict_GetItemWithError could still fail,
    1087                 :            :         ** for instance with a well-timed Ctrl-C or a MemoryError.
    1088                 :            :         ** so let's be totally safe.
    1089                 :            :         */
    1090         [ #  # ]:          0 :         if (annotations) {
    1091         [ #  # ]:          0 :             if (Py_TYPE(annotations)->tp_descr_get) {
    1092                 :          0 :                 annotations = Py_TYPE(annotations)->tp_descr_get(
    1093                 :            :                         annotations, NULL, (PyObject *)type);
    1094                 :            :             } else {
    1095                 :          0 :                 Py_INCREF(annotations);
    1096                 :            :             }
    1097                 :            :         }
    1098                 :            :     } else {
    1099                 :         25 :         annotations = PyDict_New();
    1100         [ +  - ]:         25 :         if (annotations) {
    1101                 :         25 :             int result = PyDict_SetItem(
    1102                 :            :                     type->tp_dict, &_Py_ID(__annotations__), annotations);
    1103         [ -  + ]:         25 :             if (result) {
    1104         [ #  # ]:          0 :                 Py_CLEAR(annotations);
    1105                 :            :             } else {
    1106                 :         25 :                 PyType_Modified(type);
    1107                 :            :             }
    1108                 :            :         }
    1109                 :            :     }
    1110                 :         25 :     return annotations;
    1111                 :            : }
    1112                 :            : 
    1113                 :            : static int
    1114                 :          0 : type_set_annotations(PyTypeObject *type, PyObject *value, void *context)
    1115                 :            : {
    1116         [ #  # ]:          0 :     if (_PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE)) {
    1117                 :          0 :         PyErr_Format(PyExc_TypeError,
    1118                 :            :                      "cannot set '__annotations__' attribute of immutable type '%s'",
    1119                 :            :                      type->tp_name);
    1120                 :          0 :         return -1;
    1121                 :            :     }
    1122                 :            : 
    1123                 :            :     int result;
    1124         [ #  # ]:          0 :     if (value != NULL) {
    1125                 :            :         /* set */
    1126                 :          0 :         result = PyDict_SetItem(type->tp_dict, &_Py_ID(__annotations__), value);
    1127                 :            :     } else {
    1128                 :            :         /* delete */
    1129         [ #  # ]:          0 :         if (!PyDict_Contains(type->tp_dict, &_Py_ID(__annotations__))) {
    1130                 :          0 :             PyErr_Format(PyExc_AttributeError, "__annotations__");
    1131                 :          0 :             return -1;
    1132                 :            :         }
    1133                 :          0 :         result = PyDict_DelItem(type->tp_dict, &_Py_ID(__annotations__));
    1134                 :            :     }
    1135                 :            : 
    1136         [ #  # ]:          0 :     if (result == 0) {
    1137                 :          0 :         PyType_Modified(type);
    1138                 :            :     }
    1139                 :          0 :     return result;
    1140                 :            : }
    1141                 :            : 
    1142                 :            : 
    1143                 :            : /*[clinic input]
    1144                 :            : type.__instancecheck__ -> bool
    1145                 :            : 
    1146                 :            :     instance: object
    1147                 :            :     /
    1148                 :            : 
    1149                 :            : Check if an object is an instance.
    1150                 :            : [clinic start generated code]*/
    1151                 :            : 
    1152                 :            : static int
    1153                 :       6143 : type___instancecheck___impl(PyTypeObject *self, PyObject *instance)
    1154                 :            : /*[clinic end generated code: output=08b6bf5f591c3618 input=cdbfeaee82c01a0f]*/
    1155                 :            : {
    1156                 :       6143 :     return _PyObject_RealIsInstance(instance, (PyObject *)self);
    1157                 :            : }
    1158                 :            : 
    1159                 :            : /*[clinic input]
    1160                 :            : type.__subclasscheck__ -> bool
    1161                 :            : 
    1162                 :            :     subclass: object
    1163                 :            :     /
    1164                 :            : 
    1165                 :            : Check if a class is a subclass.
    1166                 :            : [clinic start generated code]*/
    1167                 :            : 
    1168                 :            : static int
    1169                 :        564 : type___subclasscheck___impl(PyTypeObject *self, PyObject *subclass)
    1170                 :            : /*[clinic end generated code: output=97a4e51694500941 input=071b2ca9e03355f4]*/
    1171                 :            : {
    1172                 :        564 :     return _PyObject_RealIsSubclass(subclass, (PyObject *)self);
    1173                 :            : }
    1174                 :            : 
    1175                 :            : 
    1176                 :            : static PyGetSetDef type_getsets[] = {
    1177                 :            :     {"__name__", (getter)type_name, (setter)type_set_name, NULL},
    1178                 :            :     {"__qualname__", (getter)type_qualname, (setter)type_set_qualname, NULL},
    1179                 :            :     {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
    1180                 :            :     {"__module__", (getter)type_module, (setter)type_set_module, NULL},
    1181                 :            :     {"__abstractmethods__", (getter)type_abstractmethods,
    1182                 :            :      (setter)type_set_abstractmethods, NULL},
    1183                 :            :     {"__dict__",  (getter)type_dict,  NULL, NULL},
    1184                 :            :     {"__doc__", (getter)type_get_doc, (setter)type_set_doc, NULL},
    1185                 :            :     {"__text_signature__", (getter)type_get_text_signature, NULL, NULL},
    1186                 :            :     {"__annotations__", (getter)type_get_annotations, (setter)type_set_annotations, NULL},
    1187                 :            :     {0}
    1188                 :            : };
    1189                 :            : 
    1190                 :            : static PyObject *
    1191                 :          0 : type_repr(PyTypeObject *type)
    1192                 :            : {
    1193         [ #  # ]:          0 :     if (type->tp_name == NULL) {
    1194                 :            :         // type_repr() called before the type is fully initialized
    1195                 :            :         // by PyType_Ready().
    1196                 :          0 :         return PyUnicode_FromFormat("<class at %p>", type);
    1197                 :            :     }
    1198                 :            : 
    1199                 :            :     PyObject *mod, *name, *rtn;
    1200                 :            : 
    1201                 :          0 :     mod = type_module(type, NULL);
    1202         [ #  # ]:          0 :     if (mod == NULL)
    1203                 :          0 :         PyErr_Clear();
    1204         [ #  # ]:          0 :     else if (!PyUnicode_Check(mod)) {
    1205                 :          0 :         Py_SETREF(mod, NULL);
    1206                 :            :     }
    1207                 :          0 :     name = type_qualname(type, NULL);
    1208         [ #  # ]:          0 :     if (name == NULL) {
    1209                 :          0 :         Py_XDECREF(mod);
    1210                 :          0 :         return NULL;
    1211                 :            :     }
    1212                 :            : 
    1213   [ #  #  #  # ]:          0 :     if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins)))
    1214                 :          0 :         rtn = PyUnicode_FromFormat("<class '%U.%U'>", mod, name);
    1215                 :            :     else
    1216                 :          0 :         rtn = PyUnicode_FromFormat("<class '%s'>", type->tp_name);
    1217                 :            : 
    1218                 :          0 :     Py_XDECREF(mod);
    1219                 :          0 :     Py_DECREF(name);
    1220                 :          0 :     return rtn;
    1221                 :            : }
    1222                 :            : 
    1223                 :            : static PyObject *
    1224                 :     994711 : type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
    1225                 :            : {
    1226                 :            :     PyObject *obj;
    1227                 :     994711 :     PyThreadState *tstate = _PyThreadState_GET();
    1228                 :            : 
    1229                 :            : #ifdef Py_DEBUG
    1230                 :            :     /* type_call() must not be called with an exception set,
    1231                 :            :        because it can clear it (directly or indirectly) and so the
    1232                 :            :        caller loses its exception */
    1233                 :            :     assert(!_PyErr_Occurred(tstate));
    1234                 :            : #endif
    1235                 :            : 
    1236                 :            :     /* Special case: type(x) should return Py_TYPE(x) */
    1237                 :            :     /* We only want type itself to accept the one-argument form (#27157) */
    1238         [ +  + ]:     994711 :     if (type == &PyType_Type) {
    1239                 :            :         assert(args != NULL && PyTuple_Check(args));
    1240                 :            :         assert(kwds == NULL || PyDict_Check(kwds));
    1241                 :       5188 :         Py_ssize_t nargs = PyTuple_GET_SIZE(args);
    1242                 :            : 
    1243   [ -  +  -  -  :       5188 :         if (nargs == 1 && (kwds == NULL || !PyDict_GET_SIZE(kwds))) {
                   -  - ]
    1244                 :          0 :             obj = (PyObject *) Py_TYPE(PyTuple_GET_ITEM(args, 0));
    1245                 :          0 :             return Py_NewRef(obj);
    1246                 :            :         }
    1247                 :            : 
    1248                 :            :         /* SF bug 475327 -- if that didn't trigger, we need 3
    1249                 :            :            arguments. But PyArg_ParseTuple in type_new may give
    1250                 :            :            a msg saying type() needs exactly 3. */
    1251         [ -  + ]:       5188 :         if (nargs != 3) {
    1252                 :          0 :             PyErr_SetString(PyExc_TypeError,
    1253                 :            :                             "type() takes 1 or 3 arguments");
    1254                 :          0 :             return NULL;
    1255                 :            :         }
    1256                 :            :     }
    1257                 :            : 
    1258         [ -  + ]:     994711 :     if (type->tp_new == NULL) {
    1259                 :          0 :         _PyErr_Format(tstate, PyExc_TypeError,
    1260                 :            :                       "cannot create '%s' instances", type->tp_name);
    1261                 :          0 :         return NULL;
    1262                 :            :     }
    1263                 :            : 
    1264                 :     994711 :     obj = type->tp_new(type, args, kwds);
    1265                 :     994711 :     obj = _Py_CheckFunctionResult(tstate, (PyObject*)type, obj, NULL);
    1266         [ +  + ]:     994711 :     if (obj == NULL)
    1267                 :        410 :         return NULL;
    1268                 :            : 
    1269                 :            :     /* If the returned object is not an instance of type,
    1270                 :            :        it won't be initialized. */
    1271         [ -  + ]:     994301 :     if (!PyObject_TypeCheck(obj, type))
    1272                 :          0 :         return obj;
    1273                 :            : 
    1274                 :     994301 :     type = Py_TYPE(obj);
    1275         [ +  - ]:     994301 :     if (type->tp_init != NULL) {
    1276                 :     994301 :         int res = type->tp_init(obj, args, kwds);
    1277         [ +  + ]:     994301 :         if (res < 0) {
    1278                 :            :             assert(_PyErr_Occurred(tstate));
    1279                 :        310 :             Py_SETREF(obj, NULL);
    1280                 :            :         }
    1281                 :            :         else {
    1282                 :            :             assert(!_PyErr_Occurred(tstate));
    1283                 :            :         }
    1284                 :            :     }
    1285                 :     994301 :     return obj;
    1286                 :            : }
    1287                 :            : 
    1288                 :            : PyObject *
    1289                 :    1419775 : _PyType_AllocNoTrack(PyTypeObject *type, Py_ssize_t nitems)
    1290                 :            : {
    1291                 :            :     PyObject *obj;
    1292                 :            :     /* The +1 on nitems is needed for most types but not all. We could save a
    1293                 :            :      * bit of space by allocating one less item in certain cases, depending on
    1294                 :            :      * the type. However, given the extra complexity (e.g. an additional type
    1295                 :            :      * flag to indicate when that is safe) it does not seem worth the memory
    1296                 :            :      * savings. An example type that doesn't need the +1 is a subclass of
    1297                 :            :      * tuple. See GH-100659 and GH-81381. */
    1298                 :    1419775 :     const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
    1299                 :            : 
    1300                 :    1419775 :     const size_t presize = _PyType_PreHeaderSize(type);
    1301                 :    1419775 :     char *alloc = PyObject_Malloc(size + presize);
    1302         [ -  + ]:    1419775 :     if (alloc  == NULL) {
    1303                 :          0 :         return PyErr_NoMemory();
    1304                 :            :     }
    1305                 :    1419775 :     obj = (PyObject *)(alloc + presize);
    1306         [ +  + ]:    1419775 :     if (presize) {
    1307                 :    1418557 :         ((PyObject **)alloc)[0] = NULL;
    1308                 :    1418557 :         ((PyObject **)alloc)[1] = NULL;
    1309                 :    1418557 :         _PyObject_GC_Link(obj);
    1310                 :            :     }
    1311                 :    1419775 :     memset(obj, '\0', size);
    1312                 :            : 
    1313         [ +  + ]:    1419775 :     if (type->tp_itemsize == 0) {
    1314                 :    1411531 :         _PyObject_Init(obj, type);
    1315                 :            :     }
    1316                 :            :     else {
    1317                 :       8244 :         _PyObject_InitVar((PyVarObject *)obj, type, nitems);
    1318                 :            :     }
    1319                 :    1419775 :     return obj;
    1320                 :            : }
    1321                 :            : 
    1322                 :            : PyObject *
    1323                 :    1418259 : PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
    1324                 :            : {
    1325                 :    1418259 :     PyObject *obj = _PyType_AllocNoTrack(type, nitems);
    1326         [ -  + ]:    1418259 :     if (obj == NULL) {
    1327                 :          0 :         return NULL;
    1328                 :            :     }
    1329                 :            : 
    1330         [ +  + ]:    1418259 :     if (_PyType_IS_GC(type)) {
    1331                 :    1417041 :         _PyObject_GC_TRACK(obj);
    1332                 :            :     }
    1333                 :    1418259 :     return obj;
    1334                 :            : }
    1335                 :            : 
    1336                 :            : PyObject *
    1337                 :     412431 : PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
    1338                 :            : {
    1339                 :     412431 :     return type->tp_alloc(type, 0);
    1340                 :            : }
    1341                 :            : 
    1342                 :            : /* Helpers for subtyping */
    1343                 :            : 
    1344                 :            : static int
    1345                 :       6566 : traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
    1346                 :            : {
    1347                 :            :     Py_ssize_t i, n;
    1348                 :            :     PyMemberDef *mp;
    1349                 :            : 
    1350                 :       6566 :     n = Py_SIZE(type);
    1351                 :       6566 :     mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
    1352         [ +  + ]:      14624 :     for (i = 0; i < n; i++, mp++) {
    1353         [ +  - ]:       8058 :         if (mp->type == T_OBJECT_EX) {
    1354                 :       8058 :             char *addr = (char *)self + mp->offset;
    1355                 :       8058 :             PyObject *obj = *(PyObject **)addr;
    1356         [ +  + ]:       8058 :             if (obj != NULL) {
    1357                 :       7914 :                 int err = visit(obj, arg);
    1358         [ -  + ]:       7914 :                 if (err)
    1359                 :          0 :                     return err;
    1360                 :            :             }
    1361                 :            :         }
    1362                 :            :     }
    1363                 :       6566 :     return 0;
    1364                 :            : }
    1365                 :            : 
    1366                 :            : static int
    1367                 :     886396 : subtype_traverse(PyObject *self, visitproc visit, void *arg)
    1368                 :            : {
    1369                 :            :     PyTypeObject *type, *base;
    1370                 :            :     traverseproc basetraverse;
    1371                 :            : 
    1372                 :            :     /* Find the nearest base with a different tp_traverse,
    1373                 :            :        and traverse slots while we're at it */
    1374                 :     886396 :     type = Py_TYPE(self);
    1375                 :     886396 :     base = type;
    1376         [ +  + ]:    2620016 :     while ((basetraverse = base->tp_traverse) == subtype_traverse) {
    1377         [ +  + ]:    1733620 :         if (Py_SIZE(base)) {
    1378                 :       6566 :             int err = traverse_slots(base, self, visit, arg);
    1379         [ -  + ]:       6566 :             if (err)
    1380                 :          0 :                 return err;
    1381                 :            :         }
    1382                 :    1733620 :         base = base->tp_base;
    1383                 :            :         assert(base);
    1384                 :            :     }
    1385                 :            : 
    1386         [ +  + ]:     886396 :     if (type->tp_dictoffset != base->tp_dictoffset) {
    1387                 :            :         assert(base->tp_dictoffset == 0);
    1388         [ +  - ]:      50728 :         if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    1389                 :            :             assert(type->tp_dictoffset == -1);
    1390                 :      50728 :             int err = _PyObject_VisitManagedDict(self, visit, arg);
    1391         [ -  + ]:      50728 :             if (err) {
    1392                 :          0 :                 return err;
    1393                 :            :             }
    1394                 :            :         }
    1395                 :            :         else {
    1396                 :          0 :             PyObject **dictptr = _PyObject_ComputedDictPointer(self);
    1397   [ #  #  #  # ]:          0 :             if (dictptr && *dictptr) {
    1398   [ #  #  #  # ]:          0 :                 Py_VISIT(*dictptr);
    1399                 :            :             }
    1400                 :            :         }
    1401                 :            :     }
    1402                 :            : 
    1403         [ +  - ]:     886396 :     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE
    1404   [ +  +  +  + ]:     886396 :         && (!basetraverse || !(base->tp_flags & Py_TPFLAGS_HEAPTYPE))) {
    1405                 :            :         /* For a heaptype, the instances count as references
    1406                 :            :            to the type.          Traverse the type so the collector
    1407                 :            :            can find cycles involving this link.
    1408                 :            :            Skip this visit if basetraverse belongs to a heap type: in that
    1409                 :            :            case, basetraverse will visit the type when we call it later.
    1410                 :            :            */
    1411   [ +  -  -  + ]:      68138 :         Py_VISIT(type);
    1412                 :            :     }
    1413                 :            : 
    1414         [ +  + ]:     886396 :     if (basetraverse)
    1415                 :     829876 :         return basetraverse(self, visit, arg);
    1416                 :      56520 :     return 0;
    1417                 :            : }
    1418                 :            : 
    1419                 :            : static void
    1420                 :     134799 : clear_slots(PyTypeObject *type, PyObject *self)
    1421                 :            : {
    1422                 :            :     Py_ssize_t i, n;
    1423                 :            :     PyMemberDef *mp;
    1424                 :            : 
    1425                 :     134799 :     n = Py_SIZE(type);
    1426                 :     134799 :     mp = _PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
    1427         [ +  + ]:     401534 :     for (i = 0; i < n; i++, mp++) {
    1428   [ +  -  +  - ]:     266735 :         if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
    1429                 :     266735 :             char *addr = (char *)self + mp->offset;
    1430                 :     266735 :             PyObject *obj = *(PyObject **)addr;
    1431         [ +  + ]:     266735 :             if (obj != NULL) {
    1432                 :     266117 :                 *(PyObject **)addr = NULL;
    1433                 :     266117 :                 Py_DECREF(obj);
    1434                 :            :             }
    1435                 :            :         }
    1436                 :            :     }
    1437                 :     134799 : }
    1438                 :            : 
    1439                 :            : static int
    1440                 :       1445 : subtype_clear(PyObject *self)
    1441                 :            : {
    1442                 :            :     PyTypeObject *type, *base;
    1443                 :            :     inquiry baseclear;
    1444                 :            : 
    1445                 :            :     /* Find the nearest base with a different tp_clear
    1446                 :            :        and clear slots while we're at it */
    1447                 :       1445 :     type = Py_TYPE(self);
    1448                 :       1445 :     base = type;
    1449         [ +  + ]:       3214 :     while ((baseclear = base->tp_clear) == subtype_clear) {
    1450         [ +  + ]:       1769 :         if (Py_SIZE(base))
    1451                 :          5 :             clear_slots(base, self);
    1452                 :       1769 :         base = base->tp_base;
    1453                 :            :         assert(base);
    1454                 :            :     }
    1455                 :            : 
    1456                 :            :     /* Clear the instance dict (if any), to break cycles involving only
    1457                 :            :        __dict__ slots (as in the case 'self.__dict__ is self'). */
    1458         [ +  + ]:       1445 :     if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    1459         [ +  - ]:        416 :         if ((base->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0) {
    1460                 :        416 :             _PyObject_ClearManagedDict(self);
    1461                 :            :         }
    1462                 :            :     }
    1463         [ -  + ]:       1029 :     else if (type->tp_dictoffset != base->tp_dictoffset) {
    1464                 :          0 :         PyObject **dictptr = _PyObject_ComputedDictPointer(self);
    1465   [ #  #  #  # ]:          0 :         if (dictptr && *dictptr)
    1466         [ #  # ]:          0 :             Py_CLEAR(*dictptr);
    1467                 :            :     }
    1468                 :            : 
    1469         [ +  + ]:       1445 :     if (baseclear)
    1470                 :       1024 :         return baseclear(self);
    1471                 :        421 :     return 0;
    1472                 :            : }
    1473                 :            : 
    1474                 :            : static void
    1475                 :     682435 : subtype_dealloc(PyObject *self)
    1476                 :            : {
    1477                 :            :     PyTypeObject *type, *base;
    1478                 :            :     destructor basedealloc;
    1479                 :            :     int has_finalizer;
    1480                 :            : 
    1481                 :            :     /* Extract the type; we expect it to be a heap type */
    1482                 :     682435 :     type = Py_TYPE(self);
    1483                 :            :     _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
    1484                 :            : 
    1485                 :            :     /* Test whether the type has GC exactly once */
    1486                 :            : 
    1487         [ +  + ]:     682435 :     if (!_PyType_IS_GC(type)) {
    1488                 :            :         /* A non GC dynamic type allows certain simplifications:
    1489                 :            :            there's no need to call clear_slots(), or DECREF the dict,
    1490                 :            :            or clear weakrefs. */
    1491                 :            : 
    1492                 :            :         /* Maybe call finalizer; exit early if resurrected */
    1493         [ -  + ]:          2 :         if (type->tp_finalize) {
    1494         [ #  # ]:          0 :             if (PyObject_CallFinalizerFromDealloc(self) < 0)
    1495                 :          0 :                 return;
    1496                 :            :         }
    1497         [ -  + ]:          2 :         if (type->tp_del) {
    1498                 :          0 :             type->tp_del(self);
    1499         [ #  # ]:          0 :             if (Py_REFCNT(self) > 0) {
    1500                 :          0 :                 return;
    1501                 :            :             }
    1502                 :            :         }
    1503                 :            : 
    1504                 :            :         /* Find the nearest base with a different tp_dealloc */
    1505                 :          2 :         base = type;
    1506         [ +  + ]:          4 :         while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
    1507                 :          2 :             base = base->tp_base;
    1508                 :            :             assert(base);
    1509                 :            :         }
    1510                 :            : 
    1511                 :            :         /* Extract the type again; tp_del may have changed it */
    1512                 :          2 :         type = Py_TYPE(self);
    1513                 :            : 
    1514                 :            :         // Don't read type memory after calling basedealloc() since basedealloc()
    1515                 :            :         // can deallocate the type and free its memory.
    1516                 :          4 :         int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
    1517   [ +  -  -  + ]:          2 :                                  && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
    1518                 :            : 
    1519                 :            :         assert((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
    1520                 :            : 
    1521                 :            :         /* Call the base tp_dealloc() */
    1522                 :            :         assert(basedealloc);
    1523                 :          2 :         basedealloc(self);
    1524                 :            : 
    1525                 :            :         /* Can't reference self beyond this point. It's possible tp_del switched
    1526                 :            :            our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
    1527                 :            :            reference counting. Only decref if the base type is not already a heap
    1528                 :            :            allocated type. Otherwise, basedealloc should have decref'd it already */
    1529         [ -  + ]:          2 :         if (type_needs_decref) {
    1530                 :          0 :             Py_DECREF(type);
    1531                 :            :         }
    1532                 :            : 
    1533                 :            :         /* Done */
    1534                 :          2 :         return;
    1535                 :            :     }
    1536                 :            : 
    1537                 :            :     /* We get here only if the type has GC */
    1538                 :            : 
    1539                 :            :     /* UnTrack and re-Track around the trashcan macro, alas */
    1540                 :            :     /* See explanation at end of function for full disclosure */
    1541                 :     682433 :     PyObject_GC_UnTrack(self);
    1542   [ +  -  -  + ]:     682433 :     Py_TRASHCAN_BEGIN(self, subtype_dealloc);
    1543                 :            : 
    1544                 :            :     /* Find the nearest base with a different tp_dealloc */
    1545                 :     682433 :     base = type;
    1546         [ +  + ]:    2482667 :     while ((/*basedealloc =*/ base->tp_dealloc) == subtype_dealloc) {
    1547                 :    1800234 :         base = base->tp_base;
    1548                 :            :         assert(base);
    1549                 :            :     }
    1550                 :            : 
    1551   [ +  +  -  + ]:     682433 :     has_finalizer = type->tp_finalize || type->tp_del;
    1552                 :            : 
    1553         [ +  + ]:     682433 :     if (type->tp_finalize) {
    1554                 :          1 :         _PyObject_GC_TRACK(self);
    1555         [ -  + ]:          1 :         if (PyObject_CallFinalizerFromDealloc(self) < 0) {
    1556                 :            :             /* Resurrected */
    1557                 :          0 :             goto endlabel;
    1558                 :            :         }
    1559                 :          1 :         _PyObject_GC_UNTRACK(self);
    1560                 :            :     }
    1561                 :            :     /*
    1562                 :            :       If we added a weaklist, we clear it. Do this *before* calling tp_del,
    1563                 :            :       clearing slots, or clearing the instance dict.
    1564                 :            : 
    1565                 :            :       GC tracking must be off at this point. weakref callbacks (if any, and
    1566                 :            :       whether directly here or indirectly in something we call) may trigger GC,
    1567                 :            :       and if self is tracked at that point, it will look like trash to GC and GC
    1568                 :            :       will try to delete self again.
    1569                 :            :     */
    1570   [ +  +  +  + ]:     682433 :     if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
    1571                 :     545689 :         PyObject_ClearWeakRefs(self);
    1572                 :            :     }
    1573                 :            : 
    1574         [ -  + ]:     682433 :     if (type->tp_del) {
    1575                 :          0 :         _PyObject_GC_TRACK(self);
    1576                 :          0 :         type->tp_del(self);
    1577         [ #  # ]:          0 :         if (Py_REFCNT(self) > 0) {
    1578                 :            :             /* Resurrected */
    1579                 :          0 :             goto endlabel;
    1580                 :            :         }
    1581                 :          0 :         _PyObject_GC_UNTRACK(self);
    1582                 :            :     }
    1583         [ +  + ]:     682433 :     if (has_finalizer) {
    1584                 :            :         /* New weakrefs could be created during the finalizer call.
    1585                 :            :            If this occurs, clear them out without calling their
    1586                 :            :            finalizers since they might rely on part of the object
    1587                 :            :            being finalized that has already been destroyed. */
    1588   [ +  -  -  + ]:          1 :         if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
    1589                 :            :             /* Modeled after GET_WEAKREFS_LISTPTR().
    1590                 :            : 
    1591                 :            :                This is never triggered for static types so we can avoid the
    1592                 :            :                (slightly) more costly _PyObject_GET_WEAKREFS_LISTPTR(). */
    1593                 :            :             PyWeakReference **list = \
    1594                 :          0 :                 _PyObject_GET_WEAKREFS_LISTPTR_FROM_OFFSET(self);
    1595         [ #  # ]:          0 :             while (*list) {
    1596                 :          0 :                 _PyWeakref_ClearRef(*list);
    1597                 :            :             }
    1598                 :            :         }
    1599                 :            :     }
    1600                 :            : 
    1601                 :            :     /*  Clear slots up to the nearest base with a different tp_dealloc */
    1602                 :     682433 :     base = type;
    1603         [ +  + ]:    2482667 :     while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
    1604         [ +  + ]:    1800234 :         if (Py_SIZE(base))
    1605                 :     134794 :             clear_slots(base, self);
    1606                 :    1800234 :         base = base->tp_base;
    1607                 :            :         assert(base);
    1608                 :            :     }
    1609                 :            : 
    1610                 :            :     /* If we added a dict, DECREF it, or free inline values. */
    1611         [ +  + ]:     682433 :     if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    1612                 :     139182 :         PyDictOrValues *dorv_ptr = _PyObject_DictOrValuesPointer(self);
    1613         [ +  + ]:     139182 :         if (_PyDictOrValues_IsValues(*dorv_ptr)) {
    1614                 :     137332 :             _PyObject_FreeInstanceAttributes(self);
    1615                 :            :         }
    1616                 :            :         else {
    1617                 :       1850 :             Py_XDECREF(_PyDictOrValues_GetDict(*dorv_ptr));
    1618                 :            :         }
    1619                 :     139182 :         dorv_ptr->values = NULL;
    1620                 :            :     }
    1621   [ +  +  -  + ]:     543251 :     else if (type->tp_dictoffset && !base->tp_dictoffset) {
    1622                 :          0 :         PyObject **dictptr = _PyObject_ComputedDictPointer(self);
    1623         [ #  # ]:          0 :         if (dictptr != NULL) {
    1624                 :          0 :             PyObject *dict = *dictptr;
    1625         [ #  # ]:          0 :             if (dict != NULL) {
    1626                 :          0 :                 Py_DECREF(dict);
    1627                 :          0 :                 *dictptr = NULL;
    1628                 :            :             }
    1629                 :            :         }
    1630                 :            :     }
    1631                 :            : 
    1632                 :            :     /* Extract the type again; tp_del may have changed it */
    1633                 :     682433 :     type = Py_TYPE(self);
    1634                 :            : 
    1635                 :            :     /* Call the base tp_dealloc(); first retrack self if
    1636                 :            :      * basedealloc knows about gc.
    1637                 :            :      */
    1638         [ +  + ]:     682433 :     if (_PyType_IS_GC(base)) {
    1639                 :     408573 :         _PyObject_GC_TRACK(self);
    1640                 :            :     }
    1641                 :            : 
    1642                 :            :     // Don't read type memory after calling basedealloc() since basedealloc()
    1643                 :            :     // can deallocate the type and free its memory.
    1644                 :    1364866 :     int type_needs_decref = (type->tp_flags & Py_TPFLAGS_HEAPTYPE
    1645   [ +  -  +  + ]:     682433 :                              && !(base->tp_flags & Py_TPFLAGS_HEAPTYPE));
    1646                 :            : 
    1647                 :            :     assert(basedealloc);
    1648                 :     682433 :     basedealloc(self);
    1649                 :            : 
    1650                 :            :     /* Can't reference self beyond this point. It's possible tp_del switched
    1651                 :            :        our type from a HEAPTYPE to a non-HEAPTYPE, so be careful about
    1652                 :            :        reference counting. Only decref if the base type is not already a heap
    1653                 :            :        allocated type. Otherwise, basedealloc should have decref'd it already */
    1654         [ +  + ]:     682433 :     if (type_needs_decref) {
    1655                 :     275352 :         Py_DECREF(type);
    1656                 :            :     }
    1657                 :            : 
    1658                 :     407081 :   endlabel:
    1659         [ +  - ]:     682433 :     Py_TRASHCAN_END
    1660                 :            : 
    1661                 :            :     /* Explanation of the weirdness around the trashcan macros:
    1662                 :            : 
    1663                 :            :        Q. What do the trashcan macros do?
    1664                 :            : 
    1665                 :            :        A. Read the comment titled "Trashcan mechanism" in object.h.
    1666                 :            :           For one, this explains why there must be a call to GC-untrack
    1667                 :            :           before the trashcan begin macro.      Without understanding the
    1668                 :            :           trashcan code, the answers to the following questions don't make
    1669                 :            :           sense.
    1670                 :            : 
    1671                 :            :        Q. Why do we GC-untrack before the trashcan and then immediately
    1672                 :            :           GC-track again afterward?
    1673                 :            : 
    1674                 :            :        A. In the case that the base class is GC-aware, the base class
    1675                 :            :           probably GC-untracks the object.      If it does that using the
    1676                 :            :           UNTRACK macro, this will crash when the object is already
    1677                 :            :           untracked.  Because we don't know what the base class does, the
    1678                 :            :           only safe thing is to make sure the object is tracked when we
    1679                 :            :           call the base class dealloc.  But...  The trashcan begin macro
    1680                 :            :           requires that the object is *untracked* before it is called.  So
    1681                 :            :           the dance becomes:
    1682                 :            : 
    1683                 :            :          GC untrack
    1684                 :            :          trashcan begin
    1685                 :            :          GC track
    1686                 :            : 
    1687                 :            :        Q. Why did the last question say "immediately GC-track again"?
    1688                 :            :           It's nowhere near immediately.
    1689                 :            : 
    1690                 :            :        A. Because the code *used* to re-track immediately.      Bad Idea.
    1691                 :            :           self has a refcount of 0, and if gc ever gets its hands on it
    1692                 :            :           (which can happen if any weakref callback gets invoked), it
    1693                 :            :           looks like trash to gc too, and gc also tries to delete self
    1694                 :            :           then.  But we're already deleting self.  Double deallocation is
    1695                 :            :           a subtle disaster.
    1696                 :            :     */
    1697                 :            : }
    1698                 :            : 
    1699                 :            : static PyTypeObject *solid_base(PyTypeObject *type);
    1700                 :            : 
    1701                 :            : /* type test with subclassing support */
    1702                 :            : 
    1703                 :            : static int
    1704                 :        254 : type_is_subtype_base_chain(PyTypeObject *a, PyTypeObject *b)
    1705                 :            : {
    1706                 :            :     do {
    1707         [ +  + ]:        254 :         if (a == b)
    1708                 :         95 :             return 1;
    1709                 :        159 :         a = a->tp_base;
    1710         [ +  - ]:        159 :     } while (a != NULL);
    1711                 :            : 
    1712                 :          0 :     return (b == &PyBaseObject_Type);
    1713                 :            : }
    1714                 :            : 
    1715                 :            : int
    1716                 :    2679155 : PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
    1717                 :            : {
    1718                 :            :     PyObject *mro;
    1719                 :            : 
    1720                 :    2679155 :     mro = a->tp_mro;
    1721         [ +  + ]:    2679155 :     if (mro != NULL) {
    1722                 :            :         /* Deal with multiple inheritance without recursion
    1723                 :            :            by walking the MRO tuple */
    1724                 :            :         Py_ssize_t i, n;
    1725                 :            :         assert(PyTuple_Check(mro));
    1726                 :    2679060 :         n = PyTuple_GET_SIZE(mro);
    1727         [ +  + ]:    7256119 :         for (i = 0; i < n; i++) {
    1728         [ +  + ]:    5961322 :             if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
    1729                 :    1384263 :                 return 1;
    1730                 :            :         }
    1731                 :    1294797 :         return 0;
    1732                 :            :     }
    1733                 :            :     else
    1734                 :            :         /* a is not completely initialized yet; follow tp_base */
    1735                 :         95 :         return type_is_subtype_base_chain(a, b);
    1736                 :            : }
    1737                 :            : 
    1738                 :            : /* Routines to do a method lookup in the type without looking in the
    1739                 :            :    instance dictionary (so we can't use PyObject_GetAttr) but still
    1740                 :            :    binding it to the instance.
    1741                 :            : 
    1742                 :            :    Variants:
    1743                 :            : 
    1744                 :            :    - _PyObject_LookupSpecial() returns NULL without raising an exception
    1745                 :            :      when the _PyType_Lookup() call fails;
    1746                 :            : 
    1747                 :            :    - lookup_maybe_method() and lookup_method() are internal routines similar
    1748                 :            :      to _PyObject_LookupSpecial(), but can return unbound PyFunction
    1749                 :            :      to avoid temporary method object. Pass self as first argument when
    1750                 :            :      unbound == 1.
    1751                 :            : */
    1752                 :            : 
    1753                 :            : PyObject *
    1754                 :     632903 : _PyObject_LookupSpecial(PyObject *self, PyObject *attr)
    1755                 :            : {
    1756                 :            :     PyObject *res;
    1757                 :            : 
    1758                 :     632903 :     res = _PyType_Lookup(Py_TYPE(self), attr);
    1759         [ +  + ]:     632903 :     if (res != NULL) {
    1760                 :            :         descrgetfunc f;
    1761         [ -  + ]:     570833 :         if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
    1762                 :          0 :             Py_INCREF(res);
    1763                 :            :         else
    1764                 :     570833 :             res = f(res, self, (PyObject *)(Py_TYPE(self)));
    1765                 :            :     }
    1766                 :     632903 :     return res;
    1767                 :            : }
    1768                 :            : 
    1769                 :            : PyObject *
    1770                 :          0 : _PyObject_LookupSpecialId(PyObject *self, _Py_Identifier *attrid)
    1771                 :            : {
    1772                 :          0 :     PyObject *attr = _PyUnicode_FromId(attrid);   /* borrowed */
    1773         [ #  # ]:          0 :     if (attr == NULL)
    1774                 :          0 :         return NULL;
    1775                 :          0 :     return _PyObject_LookupSpecial(self, attr);
    1776                 :            : }
    1777                 :            : 
    1778                 :            : static PyObject *
    1779                 :     300228 : lookup_maybe_method(PyObject *self, PyObject *attr, int *unbound)
    1780                 :            : {
    1781                 :     300228 :     PyObject *res = _PyType_Lookup(Py_TYPE(self), attr);
    1782         [ -  + ]:     300228 :     if (res == NULL) {
    1783                 :          0 :         return NULL;
    1784                 :            :     }
    1785                 :            : 
    1786         [ +  - ]:     300228 :     if (_PyType_HasFeature(Py_TYPE(res), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
    1787                 :            :         /* Avoid temporary PyMethodObject */
    1788                 :     300228 :         *unbound = 1;
    1789                 :     300228 :         Py_INCREF(res);
    1790                 :            :     }
    1791                 :            :     else {
    1792                 :          0 :         *unbound = 0;
    1793                 :          0 :         descrgetfunc f = Py_TYPE(res)->tp_descr_get;
    1794         [ #  # ]:          0 :         if (f == NULL) {
    1795                 :          0 :             Py_INCREF(res);
    1796                 :            :         }
    1797                 :            :         else {
    1798                 :          0 :             res = f(res, self, (PyObject *)(Py_TYPE(self)));
    1799                 :            :         }
    1800                 :            :     }
    1801                 :     300228 :     return res;
    1802                 :            : }
    1803                 :            : 
    1804                 :            : static PyObject *
    1805                 :     197359 : lookup_method(PyObject *self, PyObject *attr, int *unbound)
    1806                 :            : {
    1807                 :     197359 :     PyObject *res = lookup_maybe_method(self, attr, unbound);
    1808   [ -  +  -  - ]:     197359 :     if (res == NULL && !PyErr_Occurred()) {
    1809                 :          0 :         PyErr_SetObject(PyExc_AttributeError, attr);
    1810                 :            :     }
    1811                 :     197359 :     return res;
    1812                 :            : }
    1813                 :            : 
    1814                 :            : 
    1815                 :            : static inline PyObject*
    1816                 :     150968 : vectorcall_unbound(PyThreadState *tstate, int unbound, PyObject *func,
    1817                 :            :                    PyObject *const *args, Py_ssize_t nargs)
    1818                 :            : {
    1819                 :     150968 :     size_t nargsf = nargs;
    1820         [ -  + ]:     150968 :     if (!unbound) {
    1821                 :            :         /* Skip self argument, freeing up args[0] to use for
    1822                 :            :          * PY_VECTORCALL_ARGUMENTS_OFFSET */
    1823                 :          0 :         args++;
    1824                 :          0 :         nargsf = nargsf - 1 + PY_VECTORCALL_ARGUMENTS_OFFSET;
    1825                 :            :     }
    1826                 :            :     EVAL_CALL_STAT_INC_IF_FUNCTION(EVAL_CALL_SLOT, func);
    1827                 :     150968 :     return _PyObject_VectorcallTstate(tstate, func, args, nargsf, NULL);
    1828                 :            : }
    1829                 :            : 
    1830                 :            : static PyObject*
    1831                 :       4614 : call_unbound_noarg(int unbound, PyObject *func, PyObject *self)
    1832                 :            : {
    1833         [ +  - ]:       4614 :     if (unbound) {
    1834                 :       4614 :         return PyObject_CallOneArg(func, self);
    1835                 :            :     }
    1836                 :            :     else {
    1837                 :          0 :         return _PyObject_CallNoArgs(func);
    1838                 :            :     }
    1839                 :            : }
    1840                 :            : 
    1841                 :            : /* A variation of PyObject_CallMethod* that uses lookup_method()
    1842                 :            :    instead of PyObject_GetAttrString().
    1843                 :            : 
    1844                 :            :    args is an argument vector of length nargs. The first element in this
    1845                 :            :    vector is the special object "self" which is used for the method lookup */
    1846                 :            : static PyObject *
    1847                 :      56067 : vectorcall_method(PyObject *name, PyObject *const *args, Py_ssize_t nargs)
    1848                 :            : {
    1849                 :            :     assert(nargs >= 1);
    1850                 :            : 
    1851                 :      56067 :     PyThreadState *tstate = _PyThreadState_GET();
    1852                 :            :     int unbound;
    1853                 :      56067 :     PyObject *self = args[0];
    1854                 :      56067 :     PyObject *func = lookup_method(self, name, &unbound);
    1855         [ -  + ]:      56067 :     if (func == NULL) {
    1856                 :          0 :         return NULL;
    1857                 :            :     }
    1858                 :      56067 :     PyObject *retval = vectorcall_unbound(tstate, unbound, func, args, nargs);
    1859                 :      56067 :     Py_DECREF(func);
    1860                 :      56067 :     return retval;
    1861                 :            : }
    1862                 :            : 
    1863                 :            : /* Clone of vectorcall_method() that returns NotImplemented
    1864                 :            :  * when the lookup fails. */
    1865                 :            : static PyObject *
    1866                 :      41013 : vectorcall_maybe(PyThreadState *tstate, PyObject *name,
    1867                 :            :                  PyObject *const *args, Py_ssize_t nargs)
    1868                 :            : {
    1869                 :            :     assert(nargs >= 1);
    1870                 :            : 
    1871                 :            :     int unbound;
    1872                 :      41013 :     PyObject *self = args[0];
    1873                 :      41013 :     PyObject *func = lookup_maybe_method(self, name, &unbound);
    1874         [ -  + ]:      41013 :     if (func == NULL) {
    1875         [ #  # ]:          0 :         if (!PyErr_Occurred())
    1876                 :          0 :             Py_RETURN_NOTIMPLEMENTED;
    1877                 :          0 :         return NULL;
    1878                 :            :     }
    1879                 :      41013 :     PyObject *retval = vectorcall_unbound(tstate, unbound, func, args, nargs);
    1880                 :      41013 :     Py_DECREF(func);
    1881                 :      41013 :     return retval;
    1882                 :            : }
    1883                 :            : 
    1884                 :            : /*
    1885                 :            :     Method resolution order algorithm C3 described in
    1886                 :            :     "A Monotonic Superclass Linearization for Dylan",
    1887                 :            :     by Kim Barrett, Bob Cassel, Paul Haahr,
    1888                 :            :     David A. Moon, Keith Playford, and P. Tucker Withington.
    1889                 :            :     (OOPSLA 1996)
    1890                 :            : 
    1891                 :            :     Some notes about the rules implied by C3:
    1892                 :            : 
    1893                 :            :     No duplicate bases.
    1894                 :            :     It isn't legal to repeat a class in a list of base classes.
    1895                 :            : 
    1896                 :            :     The next three properties are the 3 constraints in "C3".
    1897                 :            : 
    1898                 :            :     Local precedence order.
    1899                 :            :     If A precedes B in C's MRO, then A will precede B in the MRO of all
    1900                 :            :     subclasses of C.
    1901                 :            : 
    1902                 :            :     Monotonicity.
    1903                 :            :     The MRO of a class must be an extension without reordering of the
    1904                 :            :     MRO of each of its superclasses.
    1905                 :            : 
    1906                 :            :     Extended Precedence Graph (EPG).
    1907                 :            :     Linearization is consistent if there is a path in the EPG from
    1908                 :            :     each class to all its successors in the linearization.  See
    1909                 :            :     the paper for definition of EPG.
    1910                 :            :  */
    1911                 :            : 
    1912                 :            : static int
    1913                 :       9783 : tail_contains(PyObject *tuple, int whence, PyObject *o)
    1914                 :            : {
    1915                 :            :     Py_ssize_t j, size;
    1916                 :       9783 :     size = PyTuple_GET_SIZE(tuple);
    1917                 :            : 
    1918         [ +  + ]:      18001 :     for (j = whence+1; j < size; j++) {
    1919         [ +  + ]:       9215 :         if (PyTuple_GET_ITEM(tuple, j) == o)
    1920                 :        997 :             return 1;
    1921                 :            :     }
    1922                 :       8786 :     return 0;
    1923                 :            : }
    1924                 :            : 
    1925                 :            : static PyObject *
    1926                 :          0 : class_name(PyObject *cls)
    1927                 :            : {
    1928                 :            :     PyObject *name;
    1929         [ #  # ]:          0 :     if (_PyObject_LookupAttr(cls, &_Py_ID(__name__), &name) == 0) {
    1930                 :          0 :         name = PyObject_Repr(cls);
    1931                 :            :     }
    1932                 :          0 :     return name;
    1933                 :            : }
    1934                 :            : 
    1935                 :            : static int
    1936                 :        551 : check_duplicates(PyObject *tuple)
    1937                 :            : {
    1938                 :            :     Py_ssize_t i, j, n;
    1939                 :            :     /* Let's use a quadratic time algorithm,
    1940                 :            :        assuming that the bases tuples is short.
    1941                 :            :     */
    1942                 :        551 :     n = PyTuple_GET_SIZE(tuple);
    1943         [ +  + ]:       1649 :     for (i = 0; i < n; i++) {
    1944                 :       1098 :         PyObject *o = PyTuple_GET_ITEM(tuple, i);
    1945         [ +  + ]:       1734 :         for (j = i + 1; j < n; j++) {
    1946         [ -  + ]:        636 :             if (PyTuple_GET_ITEM(tuple, j) == o) {
    1947                 :          0 :                 o = class_name(o);
    1948         [ #  # ]:          0 :                 if (o != NULL) {
    1949         [ #  # ]:          0 :                     if (PyUnicode_Check(o)) {
    1950                 :          0 :                         PyErr_Format(PyExc_TypeError,
    1951                 :            :                                      "duplicate base class %U", o);
    1952                 :            :                     }
    1953                 :            :                     else {
    1954                 :          0 :                         PyErr_SetString(PyExc_TypeError,
    1955                 :            :                                         "duplicate base class");
    1956                 :            :                     }
    1957                 :          0 :                     Py_DECREF(o);
    1958                 :            :                 }
    1959                 :          0 :                 return -1;
    1960                 :            :             }
    1961                 :            :         }
    1962                 :            :     }
    1963                 :        551 :     return 0;
    1964                 :            : }
    1965                 :            : 
    1966                 :            : /* Raise a TypeError for an MRO order disagreement.
    1967                 :            : 
    1968                 :            :    It's hard to produce a good error message.  In the absence of better
    1969                 :            :    insight into error reporting, report the classes that were candidates
    1970                 :            :    to be put next into the MRO.  There is some conflict between the
    1971                 :            :    order in which they should be put in the MRO, but it's hard to
    1972                 :            :    diagnose what constraint can't be satisfied.
    1973                 :            : */
    1974                 :            : 
    1975                 :            : static void
    1976                 :          0 : set_mro_error(PyObject **to_merge, Py_ssize_t to_merge_size, int *remain)
    1977                 :            : {
    1978                 :            :     Py_ssize_t i, n, off;
    1979                 :            :     char buf[1000];
    1980                 :            :     PyObject *k, *v;
    1981                 :          0 :     PyObject *set = PyDict_New();
    1982         [ #  # ]:          0 :     if (!set) return;
    1983                 :            : 
    1984         [ #  # ]:          0 :     for (i = 0; i < to_merge_size; i++) {
    1985                 :          0 :         PyObject *L = to_merge[i];
    1986         [ #  # ]:          0 :         if (remain[i] < PyTuple_GET_SIZE(L)) {
    1987                 :          0 :             PyObject *c = PyTuple_GET_ITEM(L, remain[i]);
    1988         [ #  # ]:          0 :             if (PyDict_SetItem(set, c, Py_None) < 0) {
    1989                 :          0 :                 Py_DECREF(set);
    1990                 :          0 :                 return;
    1991                 :            :             }
    1992                 :            :         }
    1993                 :            :     }
    1994                 :          0 :     n = PyDict_GET_SIZE(set);
    1995                 :            : 
    1996                 :          0 :     off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
    1997                 :            : consistent method resolution\norder (MRO) for bases");
    1998                 :          0 :     i = 0;
    1999   [ #  #  #  # ]:          0 :     while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
    2000                 :          0 :         PyObject *name = class_name(k);
    2001                 :          0 :         const char *name_str = NULL;
    2002         [ #  # ]:          0 :         if (name != NULL) {
    2003         [ #  # ]:          0 :             if (PyUnicode_Check(name)) {
    2004                 :          0 :                 name_str = PyUnicode_AsUTF8(name);
    2005                 :            :             }
    2006                 :            :             else {
    2007                 :          0 :                 name_str = "?";
    2008                 :            :             }
    2009                 :            :         }
    2010         [ #  # ]:          0 :         if (name_str == NULL) {
    2011                 :          0 :             Py_XDECREF(name);
    2012                 :          0 :             Py_DECREF(set);
    2013                 :          0 :             return;
    2014                 :            :         }
    2015                 :          0 :         off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s", name_str);
    2016                 :          0 :         Py_XDECREF(name);
    2017   [ #  #  #  # ]:          0 :         if (--n && (size_t)(off+1) < sizeof(buf)) {
    2018                 :          0 :             buf[off++] = ',';
    2019                 :          0 :             buf[off] = '\0';
    2020                 :            :         }
    2021                 :            :     }
    2022                 :          0 :     PyErr_SetString(PyExc_TypeError, buf);
    2023                 :          0 :     Py_DECREF(set);
    2024                 :            : }
    2025                 :            : 
    2026                 :            : static int
    2027                 :        551 : pmerge(PyObject *acc, PyObject **to_merge, Py_ssize_t to_merge_size)
    2028                 :            : {
    2029                 :        551 :     int res = 0;
    2030                 :            :     Py_ssize_t i, j, empty_cnt;
    2031                 :            :     int *remain;
    2032                 :            : 
    2033                 :            :     /* remain stores an index into each sublist of to_merge.
    2034                 :            :        remain[i] is the index of the next base in to_merge[i]
    2035                 :            :        that is not included in acc.
    2036                 :            :     */
    2037         [ +  - ]:        551 :     remain = PyMem_New(int, to_merge_size);
    2038         [ -  + ]:        551 :     if (remain == NULL) {
    2039                 :          0 :         PyErr_NoMemory();
    2040                 :          0 :         return -1;
    2041                 :            :     }
    2042         [ +  + ]:       2200 :     for (i = 0; i < to_merge_size; i++)
    2043                 :       1649 :         remain[i] = 0;
    2044                 :            : 
    2045                 :        551 :   again:
    2046                 :       3022 :     empty_cnt = 0;
    2047         [ +  + ]:       5668 :     for (i = 0; i < to_merge_size; i++) {
    2048                 :            :         PyObject *candidate;
    2049                 :            : 
    2050                 :       5117 :         PyObject *cur_tuple = to_merge[i];
    2051                 :            : 
    2052         [ +  + ]:       5117 :         if (remain[i] >= PyTuple_GET_SIZE(cur_tuple)) {
    2053                 :       1649 :             empty_cnt++;
    2054                 :       1649 :             continue;
    2055                 :            :         }
    2056                 :            : 
    2057                 :            :         /* Choose next candidate for MRO.
    2058                 :            : 
    2059                 :            :            The input sequences alone can determine the choice.
    2060                 :            :            If not, choose the class which appears in the MRO
    2061                 :            :            of the earliest direct superclass of the new class.
    2062                 :            :         */
    2063                 :            : 
    2064                 :       3468 :         candidate = PyTuple_GET_ITEM(cur_tuple, remain[i]);
    2065         [ +  + ]:      12254 :         for (j = 0; j < to_merge_size; j++) {
    2066                 :       9783 :             PyObject *j_lst = to_merge[j];
    2067         [ +  + ]:       9783 :             if (tail_contains(j_lst, remain[j], candidate))
    2068                 :        997 :                 goto skip; /* continue outer loop */
    2069                 :            :         }
    2070                 :       2471 :         res = PyList_Append(acc, candidate);
    2071         [ -  + ]:       2471 :         if (res < 0)
    2072                 :          0 :             goto out;
    2073                 :            : 
    2074         [ +  + ]:      10132 :         for (j = 0; j < to_merge_size; j++) {
    2075                 :       7661 :             PyObject *j_lst = to_merge[j];
    2076         [ +  + ]:       7661 :             if (remain[j] < PyTuple_GET_SIZE(j_lst) &&
    2077         [ +  + ]:       6317 :                 PyTuple_GET_ITEM(j_lst, remain[j]) == candidate) {
    2078                 :       4605 :                 remain[j]++;
    2079                 :            :             }
    2080                 :            :         }
    2081                 :       2471 :         goto again;
    2082                 :        997 :       skip: ;
    2083                 :            :     }
    2084                 :            : 
    2085         [ +  - ]:        551 :     if (empty_cnt != to_merge_size) {
    2086                 :          0 :         set_mro_error(to_merge, to_merge_size, remain);
    2087                 :          0 :         res = -1;
    2088                 :            :     }
    2089                 :            : 
    2090                 :        551 :   out:
    2091                 :        551 :     PyMem_Free(remain);
    2092                 :            : 
    2093                 :        551 :     return res;
    2094                 :            : }
    2095                 :            : 
    2096                 :            : static PyObject *
    2097                 :      12908 : mro_implementation(PyTypeObject *type)
    2098                 :            : {
    2099         [ -  + ]:      12908 :     if (!_PyType_IsReady(type)) {
    2100         [ #  # ]:          0 :         if (PyType_Ready(type) < 0)
    2101                 :          0 :             return NULL;
    2102                 :            :     }
    2103                 :            : 
    2104                 :      12908 :     PyObject *bases = type->tp_bases;
    2105                 :      12908 :     Py_ssize_t n = PyTuple_GET_SIZE(bases);
    2106         [ +  + ]:      26363 :     for (Py_ssize_t i = 0; i < n; i++) {
    2107                 :      13455 :         PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i));
    2108         [ -  + ]:      13455 :         if (base->tp_mro == NULL) {
    2109                 :          0 :             PyErr_Format(PyExc_TypeError,
    2110                 :            :                          "Cannot extend an incomplete type '%.100s'",
    2111                 :            :                          base->tp_name);
    2112                 :          0 :             return NULL;
    2113                 :            :         }
    2114                 :            :         assert(PyTuple_Check(base->tp_mro));
    2115                 :            :     }
    2116                 :            : 
    2117         [ +  + ]:      12908 :     if (n == 1) {
    2118                 :            :         /* Fast path: if there is a single base, constructing the MRO
    2119                 :            :          * is trivial.
    2120                 :            :          */
    2121                 :      12357 :         PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, 0));
    2122                 :      12357 :         Py_ssize_t k = PyTuple_GET_SIZE(base->tp_mro);
    2123                 :      12357 :         PyObject *result = PyTuple_New(k + 1);
    2124         [ -  + ]:      12357 :         if (result == NULL) {
    2125                 :          0 :             return NULL;
    2126                 :            :         }
    2127                 :            : 
    2128                 :            :         ;
    2129                 :      12357 :         PyTuple_SET_ITEM(result, 0, Py_NewRef(type));
    2130         [ +  + ]:      39846 :         for (Py_ssize_t i = 0; i < k; i++) {
    2131                 :      27489 :             PyObject *cls = PyTuple_GET_ITEM(base->tp_mro, i);
    2132                 :      27489 :             PyTuple_SET_ITEM(result, i + 1, Py_NewRef(cls));
    2133                 :            :         }
    2134                 :      12357 :         return result;
    2135                 :            :     }
    2136                 :            : 
    2137                 :            :     /* This is just a basic sanity check. */
    2138         [ -  + ]:        551 :     if (check_duplicates(bases) < 0) {
    2139                 :          0 :         return NULL;
    2140                 :            :     }
    2141                 :            : 
    2142                 :            :     /* Find a superclass linearization that honors the constraints
    2143                 :            :        of the explicit tuples of bases and the constraints implied by
    2144                 :            :        each base class.
    2145                 :            : 
    2146                 :            :        to_merge is an array of tuples, where each tuple is a superclass
    2147                 :            :        linearization implied by a base class.  The last element of
    2148                 :            :        to_merge is the declared tuple of bases.
    2149                 :            :     */
    2150         [ +  - ]:        551 :     PyObject **to_merge = PyMem_New(PyObject *, n + 1);
    2151         [ -  + ]:        551 :     if (to_merge == NULL) {
    2152                 :          0 :         PyErr_NoMemory();
    2153                 :          0 :         return NULL;
    2154                 :            :     }
    2155                 :            : 
    2156         [ +  + ]:       1649 :     for (Py_ssize_t i = 0; i < n; i++) {
    2157                 :       1098 :         PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(bases, i));
    2158                 :       1098 :         to_merge[i] = base->tp_mro;
    2159                 :            :     }
    2160                 :        551 :     to_merge[n] = bases;
    2161                 :            : 
    2162                 :        551 :     PyObject *result = PyList_New(1);
    2163         [ -  + ]:        551 :     if (result == NULL) {
    2164                 :          0 :         PyMem_Free(to_merge);
    2165                 :          0 :         return NULL;
    2166                 :            :     }
    2167                 :            : 
    2168                 :        551 :     PyList_SET_ITEM(result, 0, Py_NewRef(type));
    2169         [ -  + ]:        551 :     if (pmerge(result, to_merge, n + 1) < 0) {
    2170         [ #  # ]:          0 :         Py_CLEAR(result);
    2171                 :            :     }
    2172                 :        551 :     PyMem_Free(to_merge);
    2173                 :            : 
    2174                 :        551 :     return result;
    2175                 :            : }
    2176                 :            : 
    2177                 :            : /*[clinic input]
    2178                 :            : type.mro
    2179                 :            : 
    2180                 :            : Return a type's method resolution order.
    2181                 :            : [clinic start generated code]*/
    2182                 :            : 
    2183                 :            : static PyObject *
    2184                 :       1118 : type_mro_impl(PyTypeObject *self)
    2185                 :            : /*[clinic end generated code: output=bffc4a39b5b57027 input=28414f4e156db28d]*/
    2186                 :            : {
    2187                 :            :     PyObject *seq;
    2188                 :       1118 :     seq = mro_implementation(self);
    2189   [ +  -  +  + ]:       1118 :     if (seq != NULL && !PyList_Check(seq)) {
    2190                 :        883 :         Py_SETREF(seq, PySequence_List(seq));
    2191                 :            :     }
    2192                 :       1118 :     return seq;
    2193                 :            : }
    2194                 :            : 
    2195                 :            : static int
    2196                 :       1118 : mro_check(PyTypeObject *type, PyObject *mro)
    2197                 :            : {
    2198                 :            :     PyTypeObject *solid;
    2199                 :            :     Py_ssize_t i, n;
    2200                 :            : 
    2201                 :       1118 :     solid = solid_base(type);
    2202                 :            : 
    2203                 :       1118 :     n = PyTuple_GET_SIZE(mro);
    2204         [ +  + ]:       6322 :     for (i = 0; i < n; i++) {
    2205                 :       5204 :         PyObject *obj = PyTuple_GET_ITEM(mro, i);
    2206         [ -  + ]:       5204 :         if (!PyType_Check(obj)) {
    2207                 :          0 :             PyErr_Format(
    2208                 :            :                 PyExc_TypeError,
    2209                 :            :                 "mro() returned a non-class ('%.500s')",
    2210                 :          0 :                 Py_TYPE(obj)->tp_name);
    2211                 :          0 :             return -1;
    2212                 :            :         }
    2213                 :       5204 :         PyTypeObject *base = (PyTypeObject*)obj;
    2214                 :            : 
    2215         [ -  + ]:       5204 :         if (!PyType_IsSubtype(solid, solid_base(base))) {
    2216                 :          0 :             PyErr_Format(
    2217                 :            :                 PyExc_TypeError,
    2218                 :            :                 "mro() returned base with unsuitable layout ('%.500s')",
    2219                 :            :                 base->tp_name);
    2220                 :          0 :             return -1;
    2221                 :            :         }
    2222                 :            :     }
    2223                 :            : 
    2224                 :       1118 :     return 0;
    2225                 :            : }
    2226                 :            : 
    2227                 :            : /* Lookups an mcls.mro method, invokes it and checks the result (if needed,
    2228                 :            :    in case of a custom mro() implementation).
    2229                 :            : 
    2230                 :            :    Keep in mind that during execution of this function type->tp_mro
    2231                 :            :    can be replaced due to possible reentrance (for example,
    2232                 :            :    through type_set_bases):
    2233                 :            : 
    2234                 :            :       - when looking up the mcls.mro attribute (it could be
    2235                 :            :         a user-provided descriptor);
    2236                 :            : 
    2237                 :            :       - from inside a custom mro() itself;
    2238                 :            : 
    2239                 :            :       - through a finalizer of the return value of mro().
    2240                 :            : */
    2241                 :            : static PyObject *
    2242                 :      12908 : mro_invoke(PyTypeObject *type)
    2243                 :            : {
    2244                 :            :     PyObject *mro_result;
    2245                 :            :     PyObject *new_mro;
    2246                 :      12908 :     const int custom = !Py_IS_TYPE(type, &PyType_Type);
    2247                 :            : 
    2248         [ +  + ]:      12908 :     if (custom) {
    2249                 :            :         int unbound;
    2250                 :       1118 :         PyObject *mro_meth = lookup_method(
    2251                 :            :             (PyObject *)type, &_Py_ID(mro), &unbound);
    2252         [ -  + ]:       1118 :         if (mro_meth == NULL)
    2253                 :          0 :             return NULL;
    2254                 :       1118 :         mro_result = call_unbound_noarg(unbound, mro_meth, (PyObject *)type);
    2255                 :       1118 :         Py_DECREF(mro_meth);
    2256                 :            :     }
    2257                 :            :     else {
    2258                 :      11790 :         mro_result = mro_implementation(type);
    2259                 :            :     }
    2260         [ -  + ]:      12908 :     if (mro_result == NULL)
    2261                 :          0 :         return NULL;
    2262                 :            : 
    2263                 :      12908 :     new_mro = PySequence_Tuple(mro_result);
    2264                 :      12908 :     Py_DECREF(mro_result);
    2265         [ -  + ]:      12908 :     if (new_mro == NULL) {
    2266                 :          0 :         return NULL;
    2267                 :            :     }
    2268                 :            : 
    2269         [ -  + ]:      12908 :     if (PyTuple_GET_SIZE(new_mro) == 0) {
    2270                 :          0 :         Py_DECREF(new_mro);
    2271                 :          0 :         PyErr_Format(PyExc_TypeError, "type MRO must not be empty");
    2272                 :          0 :         return NULL;
    2273                 :            :     }
    2274                 :            : 
    2275   [ +  +  -  + ]:      12908 :     if (custom && mro_check(type, new_mro) < 0) {
    2276                 :          0 :         Py_DECREF(new_mro);
    2277                 :          0 :         return NULL;
    2278                 :            :     }
    2279                 :      12908 :     return new_mro;
    2280                 :            : }
    2281                 :            : 
    2282                 :            : /* Calculates and assigns a new MRO to type->tp_mro.
    2283                 :            :    Return values and invariants:
    2284                 :            : 
    2285                 :            :      - Returns 1 if a new MRO value has been set to type->tp_mro due to
    2286                 :            :        this call of mro_internal (no tricky reentrancy and no errors).
    2287                 :            : 
    2288                 :            :        In case if p_old_mro argument is not NULL, a previous value
    2289                 :            :        of type->tp_mro is put there, and the ownership of this
    2290                 :            :        reference is transferred to a caller.
    2291                 :            :        Otherwise, the previous value (if any) is decref'ed.
    2292                 :            : 
    2293                 :            :      - Returns 0 in case when type->tp_mro gets changed because of
    2294                 :            :        reentering here through a custom mro() (see a comment to mro_invoke).
    2295                 :            : 
    2296                 :            :        In this case, a refcount of an old type->tp_mro is adjusted
    2297                 :            :        somewhere deeper in the call stack (by the innermost mro_internal
    2298                 :            :        or its caller) and may become zero upon returning from here.
    2299                 :            :        This also implies that the whole hierarchy of subclasses of the type
    2300                 :            :        has seen the new value and updated their MRO accordingly.
    2301                 :            : 
    2302                 :            :      - Returns -1 in case of an error.
    2303                 :            : */
    2304                 :            : static int
    2305                 :      12908 : mro_internal(PyTypeObject *type, PyObject **p_old_mro)
    2306                 :            : {
    2307                 :            :     PyObject *new_mro, *old_mro;
    2308                 :            :     int reent;
    2309                 :            : 
    2310                 :            :     /* Keep a reference to be able to do a reentrancy check below.
    2311                 :            :        Don't let old_mro be GC'ed and its address be reused for
    2312                 :            :        another object, like (suddenly!) a new tp_mro.  */
    2313                 :      12908 :     old_mro = Py_XNewRef(type->tp_mro);
    2314                 :      12908 :     new_mro = mro_invoke(type);  /* might cause reentrance */
    2315                 :      12908 :     reent = (type->tp_mro != old_mro);
    2316                 :      12908 :     Py_XDECREF(old_mro);
    2317         [ -  + ]:      12908 :     if (new_mro == NULL) {
    2318                 :          0 :         return -1;
    2319                 :            :     }
    2320                 :            : 
    2321         [ -  + ]:      12908 :     if (reent) {
    2322                 :          0 :         Py_DECREF(new_mro);
    2323                 :          0 :         return 0;
    2324                 :            :     }
    2325                 :            : 
    2326                 :      12908 :     type->tp_mro = new_mro;
    2327                 :            : 
    2328                 :      12908 :     type_mro_modified(type, type->tp_mro);
    2329                 :            :     /* corner case: the super class might have been hidden
    2330                 :            :        from the custom MRO */
    2331                 :      12908 :     type_mro_modified(type, type->tp_bases);
    2332                 :            : 
    2333                 :      12908 :     PyType_Modified(type);
    2334                 :            : 
    2335         [ -  + ]:      12908 :     if (p_old_mro != NULL)
    2336                 :          0 :         *p_old_mro = old_mro;  /* transfer the ownership */
    2337                 :            :     else
    2338                 :      12908 :         Py_XDECREF(old_mro);
    2339                 :            : 
    2340                 :      12908 :     return 1;
    2341                 :            : }
    2342                 :            : 
    2343                 :            : /* Calculate the best base amongst multiple base classes.
    2344                 :            :    This is the first one that's on the path to the "solid base". */
    2345                 :            : 
    2346                 :            : static PyTypeObject *
    2347                 :       6258 : best_base(PyObject *bases)
    2348                 :            : {
    2349                 :            :     Py_ssize_t i, n;
    2350                 :            :     PyTypeObject *base, *winner, *candidate;
    2351                 :            : 
    2352                 :            :     assert(PyTuple_Check(bases));
    2353                 :       6258 :     n = PyTuple_GET_SIZE(bases);
    2354                 :            :     assert(n > 0);
    2355                 :       6258 :     base = NULL;
    2356                 :       6258 :     winner = NULL;
    2357         [ +  + ]:      13092 :     for (i = 0; i < n; i++) {
    2358                 :       6834 :         PyObject *base_proto = PyTuple_GET_ITEM(bases, i);
    2359         [ -  + ]:       6834 :         if (!PyType_Check(base_proto)) {
    2360                 :          0 :             PyErr_SetString(
    2361                 :            :                 PyExc_TypeError,
    2362                 :            :                 "bases must be types");
    2363                 :          0 :             return NULL;
    2364                 :            :         }
    2365                 :       6834 :         PyTypeObject *base_i = (PyTypeObject *)base_proto;
    2366                 :            : 
    2367         [ +  + ]:       6834 :         if (!_PyType_IsReady(base_i)) {
    2368         [ -  + ]:          1 :             if (PyType_Ready(base_i) < 0)
    2369                 :          0 :                 return NULL;
    2370                 :            :         }
    2371         [ -  + ]:       6834 :         if (!_PyType_HasFeature(base_i, Py_TPFLAGS_BASETYPE)) {
    2372                 :          0 :             PyErr_Format(PyExc_TypeError,
    2373                 :            :                          "type '%.100s' is not an acceptable base type",
    2374                 :            :                          base_i->tp_name);
    2375                 :          0 :             return NULL;
    2376                 :            :         }
    2377                 :       6834 :         candidate = solid_base(base_i);
    2378         [ +  + ]:       6834 :         if (winner == NULL) {
    2379                 :       6258 :             winner = candidate;
    2380                 :       6258 :             base = base_i;
    2381                 :            :         }
    2382         [ +  + ]:        576 :         else if (PyType_IsSubtype(winner, candidate))
    2383                 :            :             ;
    2384         [ +  - ]:          2 :         else if (PyType_IsSubtype(candidate, winner)) {
    2385                 :          2 :             winner = candidate;
    2386                 :          2 :             base = base_i;
    2387                 :            :         }
    2388                 :            :         else {
    2389                 :          0 :             PyErr_SetString(
    2390                 :            :                 PyExc_TypeError,
    2391                 :            :                 "multiple bases have "
    2392                 :            :                 "instance lay-out conflict");
    2393                 :          0 :             return NULL;
    2394                 :            :         }
    2395                 :            :     }
    2396                 :            :     assert (base != NULL);
    2397                 :            : 
    2398                 :       6258 :     return base;
    2399                 :            : }
    2400                 :            : 
    2401                 :            : static int
    2402                 :      34696 : shape_differs(PyTypeObject *t1, PyTypeObject *t2)
    2403                 :            : {
    2404                 :            :     return (
    2405         [ +  + ]:      63402 :         t1->tp_basicsize != t2->tp_basicsize ||
    2406         [ -  + ]:      28706 :         t1->tp_itemsize != t2->tp_itemsize
    2407                 :            :     );
    2408                 :            : }
    2409                 :            : 
    2410                 :            : static PyTypeObject *
    2411                 :      34696 : solid_base(PyTypeObject *type)
    2412                 :            : {
    2413                 :            :     PyTypeObject *base;
    2414                 :            : 
    2415         [ +  + ]:      34696 :     if (type->tp_base) {
    2416                 :      21540 :         base = solid_base(type->tp_base);
    2417                 :            :     }
    2418                 :            :     else {
    2419                 :      13156 :         base = &PyBaseObject_Type;
    2420                 :            :     }
    2421         [ +  + ]:      34696 :     if (shape_differs(type, base)) {
    2422                 :       5990 :         return type;
    2423                 :            :     }
    2424                 :            :     else {
    2425                 :      28706 :         return base;
    2426                 :            :     }
    2427                 :            : }
    2428                 :            : 
    2429                 :            : static void object_dealloc(PyObject *);
    2430                 :            : static PyObject *object_new(PyTypeObject *, PyObject *, PyObject *);
    2431                 :            : static int object_init(PyObject *, PyObject *, PyObject *);
    2432                 :            : static int update_slot(PyTypeObject *, PyObject *);
    2433                 :            : static void fixup_slot_dispatchers(PyTypeObject *);
    2434                 :            : static int type_new_set_names(PyTypeObject *);
    2435                 :            : static int type_new_init_subclass(PyTypeObject *, PyObject *);
    2436                 :            : 
    2437                 :            : /*
    2438                 :            :  * Helpers for  __dict__ descriptor.  We don't want to expose the dicts
    2439                 :            :  * inherited from various builtin types.  The builtin base usually provides
    2440                 :            :  * its own __dict__ descriptor, so we use that when we can.
    2441                 :            :  */
    2442                 :            : static PyTypeObject *
    2443                 :       3919 : get_builtin_base_with_dict(PyTypeObject *type)
    2444                 :            : {
    2445         [ +  + ]:       7880 :     while (type->tp_base != NULL) {
    2446         [ +  + ]:       3961 :         if (type->tp_dictoffset != 0 &&
    2447         [ -  + ]:       3936 :             !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
    2448                 :          0 :             return type;
    2449                 :       3961 :         type = type->tp_base;
    2450                 :            :     }
    2451                 :       3919 :     return NULL;
    2452                 :            : }
    2453                 :            : 
    2454                 :            : static PyObject *
    2455                 :          0 : get_dict_descriptor(PyTypeObject *type)
    2456                 :            : {
    2457                 :            :     PyObject *descr;
    2458                 :            : 
    2459                 :          0 :     descr = _PyType_Lookup(type, &_Py_ID(__dict__));
    2460   [ #  #  #  # ]:          0 :     if (descr == NULL || !PyDescr_IsData(descr))
    2461                 :          0 :         return NULL;
    2462                 :            : 
    2463                 :          0 :     return descr;
    2464                 :            : }
    2465                 :            : 
    2466                 :            : static void
    2467                 :          0 : raise_dict_descr_error(PyObject *obj)
    2468                 :            : {
    2469                 :          0 :     PyErr_Format(PyExc_TypeError,
    2470                 :            :                  "this __dict__ descriptor does not support "
    2471                 :          0 :                  "'%.200s' objects", Py_TYPE(obj)->tp_name);
    2472                 :          0 : }
    2473                 :            : 
    2474                 :            : static PyObject *
    2475                 :       3919 : subtype_dict(PyObject *obj, void *context)
    2476                 :            : {
    2477                 :            :     PyTypeObject *base;
    2478                 :            : 
    2479                 :       3919 :     base = get_builtin_base_with_dict(Py_TYPE(obj));
    2480         [ -  + ]:       3919 :     if (base != NULL) {
    2481                 :            :         descrgetfunc func;
    2482                 :          0 :         PyObject *descr = get_dict_descriptor(base);
    2483         [ #  # ]:          0 :         if (descr == NULL) {
    2484                 :          0 :             raise_dict_descr_error(obj);
    2485                 :          0 :             return NULL;
    2486                 :            :         }
    2487                 :          0 :         func = Py_TYPE(descr)->tp_descr_get;
    2488         [ #  # ]:          0 :         if (func == NULL) {
    2489                 :          0 :             raise_dict_descr_error(obj);
    2490                 :          0 :             return NULL;
    2491                 :            :         }
    2492                 :          0 :         return func(descr, obj, (PyObject *)(Py_TYPE(obj)));
    2493                 :            :     }
    2494                 :       3919 :     return PyObject_GenericGetDict(obj, context);
    2495                 :            : }
    2496                 :            : 
    2497                 :            : static int
    2498                 :          0 : subtype_setdict(PyObject *obj, PyObject *value, void *context)
    2499                 :            : {
    2500                 :            :     PyObject **dictptr;
    2501                 :            :     PyTypeObject *base;
    2502                 :            : 
    2503                 :          0 :     base = get_builtin_base_with_dict(Py_TYPE(obj));
    2504         [ #  # ]:          0 :     if (base != NULL) {
    2505                 :            :         descrsetfunc func;
    2506                 :          0 :         PyObject *descr = get_dict_descriptor(base);
    2507         [ #  # ]:          0 :         if (descr == NULL) {
    2508                 :          0 :             raise_dict_descr_error(obj);
    2509                 :          0 :             return -1;
    2510                 :            :         }
    2511                 :          0 :         func = Py_TYPE(descr)->tp_descr_set;
    2512         [ #  # ]:          0 :         if (func == NULL) {
    2513                 :          0 :             raise_dict_descr_error(obj);
    2514                 :          0 :             return -1;
    2515                 :            :         }
    2516                 :          0 :         return func(descr, obj, value);
    2517                 :            :     }
    2518                 :            :     /* Almost like PyObject_GenericSetDict, but allow __dict__ to be deleted. */
    2519                 :          0 :     dictptr = _PyObject_GetDictPtr(obj);
    2520         [ #  # ]:          0 :     if (dictptr == NULL) {
    2521                 :          0 :         PyErr_SetString(PyExc_AttributeError,
    2522                 :            :                         "This object has no __dict__");
    2523                 :          0 :         return -1;
    2524                 :            :     }
    2525   [ #  #  #  # ]:          0 :     if (value != NULL && !PyDict_Check(value)) {
    2526                 :          0 :         PyErr_Format(PyExc_TypeError,
    2527                 :            :                      "__dict__ must be set to a dictionary, "
    2528                 :          0 :                      "not a '%.200s'", Py_TYPE(value)->tp_name);
    2529                 :          0 :         return -1;
    2530                 :            :     }
    2531                 :          0 :     Py_XSETREF(*dictptr, Py_XNewRef(value));
    2532                 :          0 :     return 0;
    2533                 :            : }
    2534                 :            : 
    2535                 :            : static PyObject *
    2536                 :          0 : subtype_getweakref(PyObject *obj, void *context)
    2537                 :            : {
    2538                 :            :     PyObject **weaklistptr;
    2539                 :            :     PyObject *result;
    2540                 :          0 :     PyTypeObject *type = Py_TYPE(obj);
    2541                 :            : 
    2542         [ #  # ]:          0 :     if (type->tp_weaklistoffset == 0) {
    2543                 :          0 :         PyErr_SetString(PyExc_AttributeError,
    2544                 :            :                         "This object has no __weakref__");
    2545                 :          0 :         return NULL;
    2546                 :            :     }
    2547                 :            :     _PyObject_ASSERT((PyObject *)type,
    2548                 :            :                      type->tp_weaklistoffset > 0 ||
    2549                 :            :                      type->tp_weaklistoffset == MANAGED_WEAKREF_OFFSET);
    2550                 :            :     _PyObject_ASSERT((PyObject *)type,
    2551                 :            :                      ((type->tp_weaklistoffset + (Py_ssize_t)sizeof(PyObject *))
    2552                 :            :                       <= type->tp_basicsize));
    2553                 :          0 :     weaklistptr = (PyObject **)((char *)obj + type->tp_weaklistoffset);
    2554         [ #  # ]:          0 :     if (*weaklistptr == NULL)
    2555                 :          0 :         result = Py_None;
    2556                 :            :     else
    2557                 :          0 :         result = *weaklistptr;
    2558                 :          0 :     return Py_NewRef(result);
    2559                 :            : }
    2560                 :            : 
    2561                 :            : /* Three variants on the subtype_getsets list. */
    2562                 :            : 
    2563                 :            : static PyGetSetDef subtype_getsets_full[] = {
    2564                 :            :     {"__dict__", subtype_dict, subtype_setdict,
    2565                 :            :      PyDoc_STR("dictionary for instance variables (if defined)")},
    2566                 :            :     {"__weakref__", subtype_getweakref, NULL,
    2567                 :            :      PyDoc_STR("list of weak references to the object (if defined)")},
    2568                 :            :     {0}
    2569                 :            : };
    2570                 :            : 
    2571                 :            : static PyGetSetDef subtype_getsets_dict_only[] = {
    2572                 :            :     {"__dict__", subtype_dict, subtype_setdict,
    2573                 :            :      PyDoc_STR("dictionary for instance variables (if defined)")},
    2574                 :            :     {0}
    2575                 :            : };
    2576                 :            : 
    2577                 :            : static PyGetSetDef subtype_getsets_weakref_only[] = {
    2578                 :            :     {"__weakref__", subtype_getweakref, NULL,
    2579                 :            :      PyDoc_STR("list of weak references to the object (if defined)")},
    2580                 :            :     {0}
    2581                 :            : };
    2582                 :            : 
    2583                 :            : static int
    2584                 :        332 : valid_identifier(PyObject *s)
    2585                 :            : {
    2586         [ -  + ]:        332 :     if (!PyUnicode_Check(s)) {
    2587                 :          0 :         PyErr_Format(PyExc_TypeError,
    2588                 :            :                      "__slots__ items must be strings, not '%.200s'",
    2589                 :          0 :                      Py_TYPE(s)->tp_name);
    2590                 :          0 :         return 0;
    2591                 :            :     }
    2592         [ -  + ]:        332 :     if (!PyUnicode_IsIdentifier(s)) {
    2593                 :          0 :         PyErr_SetString(PyExc_TypeError,
    2594                 :            :                         "__slots__ must be identifiers");
    2595                 :          0 :         return 0;
    2596                 :            :     }
    2597                 :        332 :     return 1;
    2598                 :            : }
    2599                 :            : 
    2600                 :            : static int
    2601                 :       6226 : type_init(PyObject *cls, PyObject *args, PyObject *kwds)
    2602                 :            : {
    2603                 :            :     assert(args != NULL && PyTuple_Check(args));
    2604                 :            :     assert(kwds == NULL || PyDict_Check(kwds));
    2605                 :            : 
    2606   [ +  +  -  +  :       6226 :     if (kwds != NULL && PyTuple_GET_SIZE(args) == 1 &&
                   -  - ]
    2607                 :          0 :         PyDict_GET_SIZE(kwds) != 0) {
    2608                 :          0 :         PyErr_SetString(PyExc_TypeError,
    2609                 :            :                         "type.__init__() takes no keyword arguments");
    2610                 :          0 :         return -1;
    2611                 :            :     }
    2612                 :            : 
    2613   [ +  -  -  + ]:       6226 :     if ((PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
    2614                 :          0 :         PyErr_SetString(PyExc_TypeError,
    2615                 :            :                         "type.__init__() takes 1 or 3 arguments");
    2616                 :          0 :         return -1;
    2617                 :            :     }
    2618                 :            : 
    2619                 :       6226 :     return 0;
    2620                 :            : }
    2621                 :            : 
    2622                 :            : 
    2623                 :            : unsigned long
    2624                 :          0 : PyType_GetFlags(PyTypeObject *type)
    2625                 :            : {
    2626                 :          0 :     return type->tp_flags;
    2627                 :            : }
    2628                 :            : 
    2629                 :            : 
    2630                 :            : int
    2631                 :          0 : PyType_SUPPORTS_WEAKREFS(PyTypeObject *type)
    2632                 :            : {
    2633                 :          0 :     return _PyType_SUPPORTS_WEAKREFS(type);
    2634                 :            : }
    2635                 :            : 
    2636                 :            : 
    2637                 :            : /* Determine the most derived metatype. */
    2638                 :            : PyTypeObject *
    2639                 :       9433 : _PyType_CalculateMetaclass(PyTypeObject *metatype, PyObject *bases)
    2640                 :            : {
    2641                 :            :     Py_ssize_t i, nbases;
    2642                 :            :     PyTypeObject *winner;
    2643                 :            :     PyObject *tmp;
    2644                 :            :     PyTypeObject *tmptype;
    2645                 :            : 
    2646                 :            :     /* Determine the proper metatype to deal with this,
    2647                 :            :        and check for metatype conflicts while we're at it.
    2648                 :            :        Note that if some other metatype wins to contract,
    2649                 :            :        it's possible that its instances are not types. */
    2650                 :            : 
    2651                 :       9433 :     nbases = PyTuple_GET_SIZE(bases);
    2652                 :       9433 :     winner = metatype;
    2653         [ +  + ]:      18941 :     for (i = 0; i < nbases; i++) {
    2654                 :       9508 :         tmp = PyTuple_GET_ITEM(bases, i);
    2655                 :       9508 :         tmptype = Py_TYPE(tmp);
    2656         [ +  + ]:       9508 :         if (PyType_IsSubtype(winner, tmptype))
    2657                 :       9370 :             continue;
    2658         [ +  - ]:        138 :         if (PyType_IsSubtype(tmptype, winner)) {
    2659                 :        138 :             winner = tmptype;
    2660                 :        138 :             continue;
    2661                 :            :         }
    2662                 :            :         /* else: */
    2663                 :          0 :         PyErr_SetString(PyExc_TypeError,
    2664                 :            :                         "metaclass conflict: "
    2665                 :            :                         "the metaclass of a derived class "
    2666                 :            :                         "must be a (non-strict) subclass "
    2667                 :            :                         "of the metaclasses of all its bases");
    2668                 :          0 :         return NULL;
    2669                 :            :     }
    2670                 :       9433 :     return winner;
    2671                 :            : }
    2672                 :            : 
    2673                 :            : 
    2674                 :            : // Forward declaration
    2675                 :            : static PyObject *
    2676                 :            : type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds);
    2677                 :            : 
    2678                 :            : typedef struct {
    2679                 :            :     PyTypeObject *metatype;
    2680                 :            :     PyObject *args;
    2681                 :            :     PyObject *kwds;
    2682                 :            :     PyObject *orig_dict;
    2683                 :            :     PyObject *name;
    2684                 :            :     PyObject *bases;
    2685                 :            :     PyTypeObject *base;
    2686                 :            :     PyObject *slots;
    2687                 :            :     Py_ssize_t nslot;
    2688                 :            :     int add_dict;
    2689                 :            :     int add_weak;
    2690                 :            :     int may_add_dict;
    2691                 :            :     int may_add_weak;
    2692                 :            : } type_new_ctx;
    2693                 :            : 
    2694                 :            : 
    2695                 :            : /* Check for valid slot names and two special cases */
    2696                 :            : static int
    2697                 :        902 : type_new_visit_slots(type_new_ctx *ctx)
    2698                 :            : {
    2699                 :        902 :     PyObject *slots = ctx->slots;
    2700                 :        902 :     Py_ssize_t nslot = ctx->nslot;
    2701         [ +  + ]:       1234 :     for (Py_ssize_t i = 0; i < nslot; i++) {
    2702                 :        332 :         PyObject *name = PyTuple_GET_ITEM(slots, i);
    2703         [ -  + ]:        332 :         if (!valid_identifier(name)) {
    2704                 :          0 :             return -1;
    2705                 :            :         }
    2706                 :            :         assert(PyUnicode_Check(name));
    2707         [ +  + ]:        332 :         if (_PyUnicode_Equal(name, &_Py_ID(__dict__))) {
    2708   [ +  -  -  + ]:          5 :             if (!ctx->may_add_dict || ctx->add_dict != 0) {
    2709                 :          0 :                 PyErr_SetString(PyExc_TypeError,
    2710                 :            :                     "__dict__ slot disallowed: "
    2711                 :            :                     "we already got one");
    2712                 :          0 :                 return -1;
    2713                 :            :             }
    2714                 :          5 :             ctx->add_dict++;
    2715                 :            :         }
    2716         [ +  + ]:        332 :         if (_PyUnicode_Equal(name, &_Py_ID(__weakref__))) {
    2717   [ +  -  -  + ]:         21 :             if (!ctx->may_add_weak || ctx->add_weak != 0) {
    2718                 :          0 :                 PyErr_SetString(PyExc_TypeError,
    2719                 :            :                     "__weakref__ slot disallowed: "
    2720                 :            :                     "we already got one");
    2721                 :          0 :                 return -1;
    2722                 :            :             }
    2723                 :         21 :             ctx->add_weak++;
    2724                 :            :         }
    2725                 :            :     }
    2726                 :        902 :     return 0;
    2727                 :            : }
    2728                 :            : 
    2729                 :            : 
    2730                 :            : /* Copy slots into a list, mangle names and sort them.
    2731                 :            :    Sorted names are needed for __class__ assignment.
    2732                 :            :    Convert them back to tuple at the end.
    2733                 :            : */
    2734                 :            : static PyObject*
    2735                 :        902 : type_new_copy_slots(type_new_ctx *ctx, PyObject *dict)
    2736                 :            : {
    2737                 :        902 :     PyObject *slots = ctx->slots;
    2738                 :        902 :     Py_ssize_t nslot = ctx->nslot;
    2739                 :            : 
    2740                 :        902 :     Py_ssize_t new_nslot = nslot - ctx->add_dict - ctx->add_weak;
    2741                 :        902 :     PyObject *new_slots = PyList_New(new_nslot);
    2742         [ -  + ]:        902 :     if (new_slots == NULL) {
    2743                 :          0 :         return NULL;
    2744                 :            :     }
    2745                 :            : 
    2746                 :        902 :     Py_ssize_t j = 0;
    2747         [ +  + ]:       1234 :     for (Py_ssize_t i = 0; i < nslot; i++) {
    2748                 :        332 :         PyObject *slot = PyTuple_GET_ITEM(slots, i);
    2749   [ +  +  +  + ]:        332 :         if ((ctx->add_dict && _PyUnicode_Equal(slot, &_Py_ID(__dict__))) ||
    2750   [ +  +  +  + ]:        327 :             (ctx->add_weak && _PyUnicode_Equal(slot, &_Py_ID(__weakref__))))
    2751                 :            :         {
    2752                 :         26 :             continue;
    2753                 :            :         }
    2754                 :            : 
    2755                 :        306 :         slot =_Py_Mangle(ctx->name, slot);
    2756         [ -  + ]:        306 :         if (!slot) {
    2757                 :          0 :             goto error;
    2758                 :            :         }
    2759                 :        306 :         PyList_SET_ITEM(new_slots, j, slot);
    2760                 :            : 
    2761                 :        306 :         int r = PyDict_Contains(dict, slot);
    2762         [ -  + ]:        306 :         if (r < 0) {
    2763                 :          0 :             goto error;
    2764                 :            :         }
    2765         [ -  + ]:        306 :         if (r > 0) {
    2766                 :            :             /* CPython inserts __qualname__ and __classcell__ (when needed)
    2767                 :            :                into the namespace when creating a class.  They will be deleted
    2768                 :            :                below so won't act as class variables. */
    2769   [ #  #  #  # ]:          0 :             if (!_PyUnicode_Equal(slot, &_Py_ID(__qualname__)) &&
    2770                 :          0 :                 !_PyUnicode_Equal(slot, &_Py_ID(__classcell__)))
    2771                 :            :             {
    2772                 :          0 :                 PyErr_Format(PyExc_ValueError,
    2773                 :            :                              "%R in __slots__ conflicts with class variable",
    2774                 :            :                              slot);
    2775                 :          0 :                 goto error;
    2776                 :            :             }
    2777                 :            :         }
    2778                 :            : 
    2779                 :        306 :         j++;
    2780                 :            :     }
    2781                 :            :     assert(j == new_nslot);
    2782                 :            : 
    2783         [ -  + ]:        902 :     if (PyList_Sort(new_slots) == -1) {
    2784                 :          0 :         goto error;
    2785                 :            :     }
    2786                 :            : 
    2787                 :        902 :     PyObject *tuple = PyList_AsTuple(new_slots);
    2788                 :        902 :     Py_DECREF(new_slots);
    2789         [ -  + ]:        902 :     if (tuple == NULL) {
    2790                 :          0 :         return NULL;
    2791                 :            :     }
    2792                 :            : 
    2793                 :            :     assert(PyTuple_GET_SIZE(tuple) == new_nslot);
    2794                 :        902 :     return tuple;
    2795                 :            : 
    2796                 :          0 : error:
    2797                 :          0 :     Py_DECREF(new_slots);
    2798                 :          0 :     return NULL;
    2799                 :            : }
    2800                 :            : 
    2801                 :            : 
    2802                 :            : static void
    2803                 :        902 : type_new_slots_bases(type_new_ctx *ctx)
    2804                 :            : {
    2805                 :        902 :     Py_ssize_t nbases = PyTuple_GET_SIZE(ctx->bases);
    2806         [ +  + ]:        902 :     if (nbases > 1 &&
    2807   [ +  -  -  + ]:        139 :         ((ctx->may_add_dict && ctx->add_dict == 0) ||
    2808   [ #  #  #  # ]:          0 :          (ctx->may_add_weak && ctx->add_weak == 0)))
    2809                 :            :     {
    2810         [ +  + ]:        442 :         for (Py_ssize_t i = 0; i < nbases; i++) {
    2811                 :        303 :             PyObject *obj = PyTuple_GET_ITEM(ctx->bases, i);
    2812         [ +  + ]:        303 :             if (obj == (PyObject *)ctx->base) {
    2813                 :            :                 /* Skip primary base */
    2814                 :        139 :                 continue;
    2815                 :            :             }
    2816                 :        164 :             PyTypeObject *base = _PyType_CAST(obj);
    2817                 :            : 
    2818   [ +  -  +  - ]:        164 :             if (ctx->may_add_dict && ctx->add_dict == 0 &&
    2819         [ -  + ]:        164 :                 base->tp_dictoffset != 0)
    2820                 :            :             {
    2821                 :          0 :                 ctx->add_dict++;
    2822                 :            :             }
    2823   [ +  +  +  - ]:        164 :             if (ctx->may_add_weak && ctx->add_weak == 0 &&
    2824         [ -  + ]:        155 :                 base->tp_weaklistoffset != 0)
    2825                 :            :             {
    2826                 :          0 :                 ctx->add_weak++;
    2827                 :            :             }
    2828   [ +  -  +  - ]:        164 :             if (ctx->may_add_dict && ctx->add_dict == 0) {
    2829                 :        164 :                 continue;
    2830                 :            :             }
    2831   [ #  #  #  # ]:          0 :             if (ctx->may_add_weak && ctx->add_weak == 0) {
    2832                 :          0 :                 continue;
    2833                 :            :             }
    2834                 :            :             /* Nothing more to check */
    2835                 :          0 :             break;
    2836                 :            :         }
    2837                 :            :     }
    2838                 :        902 : }
    2839                 :            : 
    2840                 :            : 
    2841                 :            : static int
    2842                 :        902 : type_new_slots_impl(type_new_ctx *ctx, PyObject *dict)
    2843                 :            : {
    2844                 :            :     /* Are slots allowed? */
    2845   [ +  +  -  + ]:        902 :     if (ctx->nslot > 0 && ctx->base->tp_itemsize != 0) {
    2846                 :          0 :         PyErr_Format(PyExc_TypeError,
    2847                 :            :                      "nonempty __slots__ not supported for subtype of '%s'",
    2848                 :          0 :                      ctx->base->tp_name);
    2849                 :          0 :         return -1;
    2850                 :            :     }
    2851                 :            : 
    2852         [ -  + ]:        902 :     if (type_new_visit_slots(ctx) < 0) {
    2853                 :          0 :         return -1;
    2854                 :            :     }
    2855                 :            : 
    2856                 :        902 :     PyObject *new_slots = type_new_copy_slots(ctx, dict);
    2857         [ -  + ]:        902 :     if (new_slots == NULL) {
    2858                 :          0 :         return -1;
    2859                 :            :     }
    2860                 :            :     assert(PyTuple_CheckExact(new_slots));
    2861                 :            : 
    2862                 :        902 :     Py_XSETREF(ctx->slots, new_slots);
    2863                 :        902 :     ctx->nslot = PyTuple_GET_SIZE(new_slots);
    2864                 :            : 
    2865                 :            :     /* Secondary bases may provide weakrefs or dict */
    2866                 :        902 :     type_new_slots_bases(ctx);
    2867                 :        902 :     return 0;
    2868                 :            : }
    2869                 :            : 
    2870                 :            : 
    2871                 :            : static Py_ssize_t
    2872                 :       6260 : type_new_slots(type_new_ctx *ctx, PyObject *dict)
    2873                 :            : {
    2874                 :            :     // Check for a __slots__ sequence variable in dict, and count it
    2875                 :       6260 :     ctx->add_dict = 0;
    2876                 :       6260 :     ctx->add_weak = 0;
    2877                 :       6260 :     ctx->may_add_dict = (ctx->base->tp_dictoffset == 0);
    2878                 :      12520 :     ctx->may_add_weak = (ctx->base->tp_weaklistoffset == 0
    2879   [ +  +  +  + ]:       6260 :                          && ctx->base->tp_itemsize == 0);
    2880                 :            : 
    2881         [ +  + ]:       6260 :     if (ctx->slots == NULL) {
    2882         [ +  + ]:       5358 :         if (ctx->may_add_dict) {
    2883                 :       1256 :             ctx->add_dict++;
    2884                 :            :         }
    2885         [ +  + ]:       5358 :         if (ctx->may_add_weak) {
    2886                 :       1940 :             ctx->add_weak++;
    2887                 :            :         }
    2888                 :            :     }
    2889                 :            :     else {
    2890                 :            :         /* Have slots */
    2891         [ -  + ]:        902 :         if (type_new_slots_impl(ctx, dict) < 0) {
    2892                 :          0 :             return -1;
    2893                 :            :         }
    2894                 :            :     }
    2895                 :       6260 :     return 0;
    2896                 :            : }
    2897                 :            : 
    2898                 :            : 
    2899                 :            : static PyTypeObject*
    2900                 :       6260 : type_new_alloc(type_new_ctx *ctx)
    2901                 :            : {
    2902                 :       6260 :     PyTypeObject *metatype = ctx->metatype;
    2903                 :            :     PyTypeObject *type;
    2904                 :            : 
    2905                 :            :     // Allocate the type object
    2906                 :       6260 :     type = (PyTypeObject *)metatype->tp_alloc(metatype, ctx->nslot);
    2907         [ -  + ]:       6260 :     if (type == NULL) {
    2908                 :          0 :         return NULL;
    2909                 :            :     }
    2910                 :       6260 :     PyHeapTypeObject *et = (PyHeapTypeObject *)type;
    2911                 :            : 
    2912                 :            :     // Initialize tp_flags.
    2913                 :            :     // All heap types need GC, since we can create a reference cycle by storing
    2914                 :            :     // an instance on one of its parents.
    2915                 :       6260 :     type->tp_flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
    2916                 :            :                       Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_GC);
    2917                 :            : 
    2918                 :            :     // Initialize essential fields
    2919                 :       6260 :     type->tp_as_async = &et->as_async;
    2920                 :       6260 :     type->tp_as_number = &et->as_number;
    2921                 :       6260 :     type->tp_as_sequence = &et->as_sequence;
    2922                 :       6260 :     type->tp_as_mapping = &et->as_mapping;
    2923                 :       6260 :     type->tp_as_buffer = &et->as_buffer;
    2924                 :            : 
    2925                 :       6260 :     type->tp_bases = Py_NewRef(ctx->bases);
    2926                 :       6260 :     type->tp_base = (PyTypeObject *)Py_NewRef(ctx->base);
    2927                 :            : 
    2928                 :       6260 :     type->tp_dealloc = subtype_dealloc;
    2929                 :            :     /* Always override allocation strategy to use regular heap */
    2930                 :       6260 :     type->tp_alloc = PyType_GenericAlloc;
    2931                 :       6260 :     type->tp_free = PyObject_GC_Del;
    2932                 :            : 
    2933                 :       6260 :     type->tp_traverse = subtype_traverse;
    2934                 :       6260 :     type->tp_clear = subtype_clear;
    2935                 :            : 
    2936                 :       6260 :     et->ht_name = Py_NewRef(ctx->name);
    2937                 :       6260 :     et->ht_module = NULL;
    2938                 :       6260 :     et->_ht_tpname = NULL;
    2939                 :            : 
    2940                 :       6260 :     return type;
    2941                 :            : }
    2942                 :            : 
    2943                 :            : 
    2944                 :            : static int
    2945                 :       6260 : type_new_set_name(const type_new_ctx *ctx, PyTypeObject *type)
    2946                 :            : {
    2947                 :            :     Py_ssize_t name_size;
    2948                 :       6260 :     type->tp_name = PyUnicode_AsUTF8AndSize(ctx->name, &name_size);
    2949         [ -  + ]:       6260 :     if (!type->tp_name) {
    2950                 :          0 :         return -1;
    2951                 :            :     }
    2952         [ -  + ]:       6260 :     if (strlen(type->tp_name) != (size_t)name_size) {
    2953                 :          0 :         PyErr_SetString(PyExc_ValueError,
    2954                 :            :                         "type name must not contain null characters");
    2955                 :          0 :         return -1;
    2956                 :            :     }
    2957                 :       6260 :     return 0;
    2958                 :            : }
    2959                 :            : 
    2960                 :            : 
    2961                 :            : /* Set __module__ in the dict */
    2962                 :            : static int
    2963                 :       6260 : type_new_set_module(PyTypeObject *type)
    2964                 :            : {
    2965                 :       6260 :     int r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__));
    2966         [ -  + ]:       6260 :     if (r < 0) {
    2967                 :          0 :         return -1;
    2968                 :            :     }
    2969         [ +  + ]:       6260 :     if (r > 0) {
    2970                 :       6165 :         return 0;
    2971                 :            :     }
    2972                 :            : 
    2973                 :         95 :     PyObject *globals = PyEval_GetGlobals();
    2974         [ -  + ]:         95 :     if (globals == NULL) {
    2975                 :          0 :         return 0;
    2976                 :            :     }
    2977                 :            : 
    2978                 :         95 :     PyObject *module = PyDict_GetItemWithError(globals, &_Py_ID(__name__));
    2979         [ -  + ]:         95 :     if (module == NULL) {
    2980         [ #  # ]:          0 :         if (PyErr_Occurred()) {
    2981                 :          0 :             return -1;
    2982                 :            :         }
    2983                 :          0 :         return 0;
    2984                 :            :     }
    2985                 :            : 
    2986         [ -  + ]:         95 :     if (PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), module) < 0) {
    2987                 :          0 :         return -1;
    2988                 :            :     }
    2989                 :         95 :     return 0;
    2990                 :            : }
    2991                 :            : 
    2992                 :            : 
    2993                 :            : /* Set ht_qualname to dict['__qualname__'] if available, else to
    2994                 :            :    __name__.  The __qualname__ accessor will look for ht_qualname. */
    2995                 :            : static int
    2996                 :       6260 : type_new_set_ht_name(PyTypeObject *type)
    2997                 :            : {
    2998                 :       6260 :     PyHeapTypeObject *et = (PyHeapTypeObject *)type;
    2999                 :       6260 :     PyObject *qualname = PyDict_GetItemWithError(
    3000                 :            :             type->tp_dict, &_Py_ID(__qualname__));
    3001         [ +  + ]:       6260 :     if (qualname != NULL) {
    3002         [ -  + ]:       3149 :         if (!PyUnicode_Check(qualname)) {
    3003                 :          0 :             PyErr_Format(PyExc_TypeError,
    3004                 :            :                     "type __qualname__ must be a str, not %s",
    3005                 :          0 :                     Py_TYPE(qualname)->tp_name);
    3006                 :          0 :             return -1;
    3007                 :            :         }
    3008                 :       3149 :         et->ht_qualname = Py_NewRef(qualname);
    3009         [ -  + ]:       3149 :         if (PyDict_DelItem(type->tp_dict, &_Py_ID(__qualname__)) < 0) {
    3010                 :          0 :             return -1;
    3011                 :            :         }
    3012                 :            :     }
    3013                 :            :     else {
    3014         [ -  + ]:       3111 :         if (PyErr_Occurred()) {
    3015                 :          0 :             return -1;
    3016                 :            :         }
    3017                 :       3111 :         et->ht_qualname = Py_NewRef(et->ht_name);
    3018                 :            :     }
    3019                 :       6260 :     return 0;
    3020                 :            : }
    3021                 :            : 
    3022                 :            : 
    3023                 :            : /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
    3024                 :            :    and is a string.  The __doc__ accessor will first look for tp_doc;
    3025                 :            :    if that fails, it will still look into __dict__. */
    3026                 :            : static int
    3027                 :       6260 : type_new_set_doc(PyTypeObject *type)
    3028                 :            : {
    3029                 :       6260 :     PyObject *doc = PyDict_GetItemWithError(type->tp_dict, &_Py_ID(__doc__));
    3030         [ +  + ]:       6260 :     if (doc == NULL) {
    3031         [ -  + ]:       1495 :         if (PyErr_Occurred()) {
    3032                 :          0 :             return -1;
    3033                 :            :         }
    3034                 :            :         // no __doc__ key
    3035                 :       1495 :         return 0;
    3036                 :            :     }
    3037         [ -  + ]:       4765 :     if (!PyUnicode_Check(doc)) {
    3038                 :            :         // ignore non-string __doc__
    3039                 :          0 :         return 0;
    3040                 :            :     }
    3041                 :            : 
    3042                 :       4765 :     const char *doc_str = PyUnicode_AsUTF8(doc);
    3043         [ -  + ]:       4765 :     if (doc_str == NULL) {
    3044                 :          0 :         return -1;
    3045                 :            :     }
    3046                 :            : 
    3047                 :            :     // Silently truncate the docstring if it contains a null byte
    3048                 :       4765 :     Py_ssize_t size = strlen(doc_str) + 1;
    3049                 :       4765 :     char *tp_doc = (char *)PyObject_Malloc(size);
    3050         [ -  + ]:       4765 :     if (tp_doc == NULL) {
    3051                 :          0 :         PyErr_NoMemory();
    3052                 :          0 :         return -1;
    3053                 :            :     }
    3054                 :            : 
    3055                 :       4765 :     memcpy(tp_doc, doc_str, size);
    3056                 :       4765 :     type->tp_doc = tp_doc;
    3057                 :       4765 :     return 0;
    3058                 :            : }
    3059                 :            : 
    3060                 :            : 
    3061                 :            : static int
    3062                 :       6260 : type_new_staticmethod(PyTypeObject *type, PyObject *attr)
    3063                 :            : {
    3064                 :       6260 :     PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr);
    3065         [ +  + ]:       6260 :     if (func == NULL) {
    3066         [ -  + ]:       6039 :         if (PyErr_Occurred()) {
    3067                 :          0 :             return -1;
    3068                 :            :         }
    3069                 :       6039 :         return 0;
    3070                 :            :     }
    3071         [ -  + ]:        221 :     if (!PyFunction_Check(func)) {
    3072                 :          0 :         return 0;
    3073                 :            :     }
    3074                 :            : 
    3075                 :        221 :     PyObject *static_func = PyStaticMethod_New(func);
    3076         [ -  + ]:        221 :     if (static_func == NULL) {
    3077                 :          0 :         return -1;
    3078                 :            :     }
    3079         [ -  + ]:        221 :     if (PyDict_SetItem(type->tp_dict, attr, static_func) < 0) {
    3080                 :          0 :         Py_DECREF(static_func);
    3081                 :          0 :         return -1;
    3082                 :            :     }
    3083                 :        221 :     Py_DECREF(static_func);
    3084                 :        221 :     return 0;
    3085                 :            : }
    3086                 :            : 
    3087                 :            : 
    3088                 :            : static int
    3089                 :      12520 : type_new_classmethod(PyTypeObject *type, PyObject *attr)
    3090                 :            : {
    3091                 :      12520 :     PyObject *func = PyDict_GetItemWithError(type->tp_dict, attr);
    3092         [ +  + ]:      12520 :     if (func == NULL) {
    3093         [ -  + ]:      12282 :         if (PyErr_Occurred()) {
    3094                 :          0 :             return -1;
    3095                 :            :         }
    3096                 :      12282 :         return 0;
    3097                 :            :     }
    3098         [ +  + ]:        238 :     if (!PyFunction_Check(func)) {
    3099                 :        213 :         return 0;
    3100                 :            :     }
    3101                 :            : 
    3102                 :         25 :     PyObject *method = PyClassMethod_New(func);
    3103         [ -  + ]:         25 :     if (method == NULL) {
    3104                 :          0 :         return -1;
    3105                 :            :     }
    3106                 :            : 
    3107         [ -  + ]:         25 :     if (PyDict_SetItem(type->tp_dict, attr, method) < 0) {
    3108                 :          0 :         Py_DECREF(method);
    3109                 :          0 :         return -1;
    3110                 :            :     }
    3111                 :         25 :     Py_DECREF(method);
    3112                 :         25 :     return 0;
    3113                 :            : }
    3114                 :            : 
    3115                 :            : 
    3116                 :            : /* Add descriptors for custom slots from __slots__, or for __dict__ */
    3117                 :            : static int
    3118                 :       6260 : type_new_descriptors(const type_new_ctx *ctx, PyTypeObject *type)
    3119                 :            : {
    3120                 :       6260 :     PyHeapTypeObject *et = (PyHeapTypeObject *)type;
    3121                 :       6260 :     Py_ssize_t slotoffset = ctx->base->tp_basicsize;
    3122         [ +  + ]:       6260 :     if (et->ht_slots != NULL) {
    3123                 :        902 :         PyMemberDef *mp = _PyHeapType_GET_MEMBERS(et);
    3124                 :        902 :         Py_ssize_t nslot = PyTuple_GET_SIZE(et->ht_slots);
    3125         [ +  + ]:       1208 :         for (Py_ssize_t i = 0; i < nslot; i++, mp++) {
    3126                 :        612 :             mp->name = PyUnicode_AsUTF8(
    3127                 :        306 :                 PyTuple_GET_ITEM(et->ht_slots, i));
    3128         [ -  + ]:        306 :             if (mp->name == NULL) {
    3129                 :          0 :                 return -1;
    3130                 :            :             }
    3131                 :        306 :             mp->type = T_OBJECT_EX;
    3132                 :        306 :             mp->offset = slotoffset;
    3133                 :            : 
    3134                 :            :             /* __dict__ and __weakref__ are already filtered out */
    3135                 :            :             assert(strcmp(mp->name, "__dict__") != 0);
    3136                 :            :             assert(strcmp(mp->name, "__weakref__") != 0);
    3137                 :            : 
    3138                 :        306 :             slotoffset += sizeof(PyObject *);
    3139                 :            :         }
    3140                 :            :     }
    3141                 :            : 
    3142         [ +  + ]:       6260 :     if (ctx->add_weak) {
    3143                 :            :         assert((type->tp_flags & Py_TPFLAGS_MANAGED_WEAKREF) == 0);
    3144                 :       1961 :         type->tp_flags |= Py_TPFLAGS_MANAGED_WEAKREF;
    3145                 :       1961 :         type->tp_weaklistoffset = MANAGED_WEAKREF_OFFSET;
    3146                 :            :     }
    3147         [ +  + ]:       6260 :     if (ctx->add_dict) {
    3148                 :            :         assert((type->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0);
    3149                 :       1261 :         type->tp_flags |= Py_TPFLAGS_MANAGED_DICT;
    3150                 :       1261 :         type->tp_dictoffset = -1;
    3151                 :            :     }
    3152                 :            : 
    3153                 :       6260 :     type->tp_basicsize = slotoffset;
    3154                 :       6260 :     type->tp_itemsize = ctx->base->tp_itemsize;
    3155                 :       6260 :     type->tp_members = _PyHeapType_GET_MEMBERS(et);
    3156                 :       6260 :     return 0;
    3157                 :            : }
    3158                 :            : 
    3159                 :            : 
    3160                 :            : static void
    3161                 :       6260 : type_new_set_slots(const type_new_ctx *ctx, PyTypeObject *type)
    3162                 :            : {
    3163   [ +  +  +  + ]:       6260 :     if (type->tp_weaklistoffset && type->tp_dictoffset) {
    3164                 :       1183 :         type->tp_getset = subtype_getsets_full;
    3165                 :            :     }
    3166   [ +  +  +  - ]:       5077 :     else if (type->tp_weaklistoffset && !type->tp_dictoffset) {
    3167                 :        778 :         type->tp_getset = subtype_getsets_weakref_only;
    3168                 :            :     }
    3169   [ +  -  +  + ]:       4299 :     else if (!type->tp_weaklistoffset && type->tp_dictoffset) {
    3170                 :         78 :         type->tp_getset = subtype_getsets_dict_only;
    3171                 :            :     }
    3172                 :            :     else {
    3173                 :       4221 :         type->tp_getset = NULL;
    3174                 :            :     }
    3175                 :            : 
    3176                 :            :     /* Special case some slots */
    3177   [ +  +  +  + ]:       6260 :     if (type->tp_dictoffset != 0 || ctx->nslot > 0) {
    3178                 :       1360 :         PyTypeObject *base = ctx->base;
    3179   [ +  -  -  + ]:       1360 :         if (base->tp_getattr == NULL && base->tp_getattro == NULL) {
    3180                 :          0 :             type->tp_getattro = PyObject_GenericGetAttr;
    3181                 :            :         }
    3182   [ +  -  -  + ]:       1360 :         if (base->tp_setattr == NULL && base->tp_setattro == NULL) {
    3183                 :          0 :             type->tp_setattro = PyObject_GenericSetAttr;
    3184                 :            :         }
    3185                 :            :     }
    3186                 :       6260 : }
    3187                 :            : 
    3188                 :            : 
    3189                 :            : /* store type in class' cell if one is supplied */
    3190                 :            : static int
    3191                 :       6260 : type_new_set_classcell(PyTypeObject *type)
    3192                 :            : {
    3193                 :       6260 :     PyObject *cell = PyDict_GetItemWithError(
    3194                 :            :             type->tp_dict, &_Py_ID(__classcell__));
    3195         [ +  + ]:       6260 :     if (cell == NULL) {
    3196         [ -  + ]:       5933 :         if (PyErr_Occurred()) {
    3197                 :          0 :             return -1;
    3198                 :            :         }
    3199                 :       5933 :         return 0;
    3200                 :            :     }
    3201                 :            : 
    3202                 :            :     /* At least one method requires a reference to its defining class */
    3203         [ -  + ]:        327 :     if (!PyCell_Check(cell)) {
    3204                 :          0 :         PyErr_Format(PyExc_TypeError,
    3205                 :            :                      "__classcell__ must be a nonlocal cell, not %.200R",
    3206                 :            :                      Py_TYPE(cell));
    3207                 :          0 :         return -1;
    3208                 :            :     }
    3209                 :            : 
    3210                 :        327 :     (void)PyCell_Set(cell, (PyObject *) type);
    3211         [ -  + ]:        327 :     if (PyDict_DelItem(type->tp_dict, &_Py_ID(__classcell__)) < 0) {
    3212                 :          0 :         return -1;
    3213                 :            :     }
    3214                 :        327 :     return 0;
    3215                 :            : }
    3216                 :            : 
    3217                 :            : 
    3218                 :            : static int
    3219                 :       6260 : type_new_set_attrs(const type_new_ctx *ctx, PyTypeObject *type)
    3220                 :            : {
    3221         [ -  + ]:       6260 :     if (type_new_set_name(ctx, type) < 0) {
    3222                 :          0 :         return -1;
    3223                 :            :     }
    3224                 :            : 
    3225         [ -  + ]:       6260 :     if (type_new_set_module(type) < 0) {
    3226                 :          0 :         return -1;
    3227                 :            :     }
    3228                 :            : 
    3229         [ -  + ]:       6260 :     if (type_new_set_ht_name(type) < 0) {
    3230                 :          0 :         return -1;
    3231                 :            :     }
    3232                 :            : 
    3233         [ -  + ]:       6260 :     if (type_new_set_doc(type) < 0) {
    3234                 :          0 :         return -1;
    3235                 :            :     }
    3236                 :            : 
    3237                 :            :     /* Special-case __new__: if it's a plain function,
    3238                 :            :        make it a static function */
    3239         [ -  + ]:       6260 :     if (type_new_staticmethod(type, &_Py_ID(__new__)) < 0) {
    3240                 :          0 :         return -1;
    3241                 :            :     }
    3242                 :            : 
    3243                 :            :     /* Special-case __init_subclass__ and __class_getitem__:
    3244                 :            :        if they are plain functions, make them classmethods */
    3245         [ -  + ]:       6260 :     if (type_new_classmethod(type, &_Py_ID(__init_subclass__)) < 0) {
    3246                 :          0 :         return -1;
    3247                 :            :     }
    3248         [ -  + ]:       6260 :     if (type_new_classmethod(type, &_Py_ID(__class_getitem__)) < 0) {
    3249                 :          0 :         return -1;
    3250                 :            :     }
    3251                 :            : 
    3252         [ -  + ]:       6260 :     if (type_new_descriptors(ctx, type) < 0) {
    3253                 :          0 :         return -1;
    3254                 :            :     }
    3255                 :            : 
    3256                 :       6260 :     type_new_set_slots(ctx, type);
    3257                 :            : 
    3258         [ -  + ]:       6260 :     if (type_new_set_classcell(type) < 0) {
    3259                 :          0 :         return -1;
    3260                 :            :     }
    3261                 :       6260 :     return 0;
    3262                 :            : }
    3263                 :            : 
    3264                 :            : 
    3265                 :            : static int
    3266                 :       6260 : type_new_get_slots(type_new_ctx *ctx, PyObject *dict)
    3267                 :            : {
    3268                 :       6260 :     PyObject *slots = PyDict_GetItemWithError(dict, &_Py_ID(__slots__));
    3269         [ +  + ]:       6260 :     if (slots == NULL) {
    3270         [ -  + ]:       5358 :         if (PyErr_Occurred()) {
    3271                 :          0 :             return -1;
    3272                 :            :         }
    3273                 :       5358 :         ctx->slots = NULL;
    3274                 :       5358 :         ctx->nslot = 0;
    3275                 :       5358 :         return 0;
    3276                 :            :     }
    3277                 :            : 
    3278                 :            :     // Make it into a tuple
    3279                 :            :     PyObject *new_slots;
    3280         [ +  + ]:        902 :     if (PyUnicode_Check(slots)) {
    3281                 :          5 :         new_slots = PyTuple_Pack(1, slots);
    3282                 :            :     }
    3283                 :            :     else {
    3284                 :        897 :         new_slots = PySequence_Tuple(slots);
    3285                 :            :     }
    3286         [ -  + ]:        902 :     if (new_slots == NULL) {
    3287                 :          0 :         return -1;
    3288                 :            :     }
    3289                 :            :     assert(PyTuple_CheckExact(new_slots));
    3290                 :        902 :     ctx->slots = new_slots;
    3291                 :        902 :     ctx->nslot = PyTuple_GET_SIZE(new_slots);
    3292                 :        902 :     return 0;
    3293                 :            : }
    3294                 :            : 
    3295                 :            : 
    3296                 :            : static PyTypeObject*
    3297                 :       6260 : type_new_init(type_new_ctx *ctx)
    3298                 :            : {
    3299                 :       6260 :     PyObject *dict = PyDict_Copy(ctx->orig_dict);
    3300         [ -  + ]:       6260 :     if (dict == NULL) {
    3301                 :          0 :         goto error;
    3302                 :            :     }
    3303                 :            : 
    3304         [ -  + ]:       6260 :     if (type_new_get_slots(ctx, dict) < 0) {
    3305                 :          0 :         goto error;
    3306                 :            :     }
    3307                 :            :     assert(!PyErr_Occurred());
    3308                 :            : 
    3309         [ -  + ]:       6260 :     if (type_new_slots(ctx, dict) < 0) {
    3310                 :          0 :         goto error;
    3311                 :            :     }
    3312                 :            : 
    3313                 :       6260 :     PyTypeObject *type = type_new_alloc(ctx);
    3314         [ -  + ]:       6260 :     if (type == NULL) {
    3315                 :          0 :         goto error;
    3316                 :            :     }
    3317                 :            : 
    3318                 :       6260 :     type->tp_dict = dict;
    3319                 :            : 
    3320                 :       6260 :     PyHeapTypeObject *et = (PyHeapTypeObject*)type;
    3321                 :       6260 :     et->ht_slots = ctx->slots;
    3322                 :       6260 :     ctx->slots = NULL;
    3323                 :            : 
    3324                 :       6260 :     return type;
    3325                 :            : 
    3326                 :          0 : error:
    3327         [ #  # ]:          0 :     Py_CLEAR(ctx->slots);
    3328                 :          0 :     Py_XDECREF(dict);
    3329                 :          0 :     return NULL;
    3330                 :            : }
    3331                 :            : 
    3332                 :            : 
    3333                 :            : static PyObject*
    3334                 :       6260 : type_new_impl(type_new_ctx *ctx)
    3335                 :            : {
    3336                 :       6260 :     PyTypeObject *type = type_new_init(ctx);
    3337         [ -  + ]:       6260 :     if (type == NULL) {
    3338                 :          0 :         return NULL;
    3339                 :            :     }
    3340                 :            : 
    3341         [ -  + ]:       6260 :     if (type_new_set_attrs(ctx, type) < 0) {
    3342                 :          0 :         goto error;
    3343                 :            :     }
    3344                 :            : 
    3345                 :            :     /* Initialize the rest */
    3346         [ -  + ]:       6260 :     if (PyType_Ready(type) < 0) {
    3347                 :          0 :         goto error;
    3348                 :            :     }
    3349                 :            : 
    3350                 :            :     // Put the proper slots in place
    3351                 :       6260 :     fixup_slot_dispatchers(type);
    3352                 :            : 
    3353         [ -  + ]:       6260 :     if (type_new_set_names(type) < 0) {
    3354                 :          0 :         goto error;
    3355                 :            :     }
    3356                 :            : 
    3357         [ -  + ]:       6260 :     if (type_new_init_subclass(type, ctx->kwds) < 0) {
    3358                 :          0 :         goto error;
    3359                 :            :     }
    3360                 :            : 
    3361                 :            :     assert(_PyType_CheckConsistency(type));
    3362                 :            : 
    3363                 :       6260 :     return (PyObject *)type;
    3364                 :            : 
    3365                 :          0 : error:
    3366                 :          0 :     Py_DECREF(type);
    3367                 :          0 :     return NULL;
    3368                 :            : }
    3369                 :            : 
    3370                 :            : 
    3371                 :            : static int
    3372                 :       6294 : type_new_get_bases(type_new_ctx *ctx, PyObject **type)
    3373                 :            : {
    3374                 :       6294 :     Py_ssize_t nbases = PyTuple_GET_SIZE(ctx->bases);
    3375         [ +  + ]:       6294 :     if (nbases == 0) {
    3376                 :            :         // Adjust for empty tuple bases
    3377                 :       1014 :         ctx->base = &PyBaseObject_Type;
    3378                 :       1014 :         PyObject *new_bases = PyTuple_Pack(1, ctx->base);
    3379         [ -  + ]:       1014 :         if (new_bases == NULL) {
    3380                 :          0 :             return -1;
    3381                 :            :         }
    3382                 :       1014 :         ctx->bases = new_bases;
    3383                 :       1014 :         return 0;
    3384                 :            :     }
    3385                 :            : 
    3386         [ +  + ]:      11138 :     for (Py_ssize_t i = 0; i < nbases; i++) {
    3387                 :       5858 :         PyObject *base = PyTuple_GET_ITEM(ctx->bases, i);
    3388         [ +  - ]:       5858 :         if (PyType_Check(base)) {
    3389                 :       5858 :             continue;
    3390                 :            :         }
    3391                 :            :         PyObject *mro_entries;
    3392         [ #  # ]:          0 :         if (_PyObject_LookupAttr(base, &_Py_ID(__mro_entries__),
    3393                 :            :                                  &mro_entries) < 0) {
    3394                 :          0 :             return -1;
    3395                 :            :         }
    3396         [ #  # ]:          0 :         if (mro_entries != NULL) {
    3397                 :          0 :             PyErr_SetString(PyExc_TypeError,
    3398                 :            :                             "type() doesn't support MRO entry resolution; "
    3399                 :            :                             "use types.new_class()");
    3400                 :          0 :             Py_DECREF(mro_entries);
    3401                 :          0 :             return -1;
    3402                 :            :         }
    3403                 :            :     }
    3404                 :            : 
    3405                 :            :     // Search the bases for the proper metatype to deal with this
    3406                 :            :     PyTypeObject *winner;
    3407                 :       5280 :     winner = _PyType_CalculateMetaclass(ctx->metatype, ctx->bases);
    3408         [ -  + ]:       5280 :     if (winner == NULL) {
    3409                 :          0 :         return -1;
    3410                 :            :     }
    3411                 :            : 
    3412         [ +  + ]:       5280 :     if (winner != ctx->metatype) {
    3413         [ +  - ]:         34 :         if (winner->tp_new != type_new) {
    3414                 :            :             /* Pass it to the winner */
    3415                 :         34 :             *type = winner->tp_new(winner, ctx->args, ctx->kwds);
    3416         [ -  + ]:         34 :             if (*type == NULL) {
    3417                 :          0 :                 return -1;
    3418                 :            :             }
    3419                 :         34 :             return 1;
    3420                 :            :         }
    3421                 :            : 
    3422                 :          0 :         ctx->metatype = winner;
    3423                 :            :     }
    3424                 :            : 
    3425                 :            :     /* Calculate best base, and check that all bases are type objects */
    3426                 :       5246 :     PyTypeObject *base = best_base(ctx->bases);
    3427         [ -  + ]:       5246 :     if (base == NULL) {
    3428                 :          0 :         return -1;
    3429                 :            :     }
    3430                 :            : 
    3431                 :       5246 :     ctx->base = base;
    3432                 :       5246 :     ctx->bases = Py_NewRef(ctx->bases);
    3433                 :       5246 :     return 0;
    3434                 :            : }
    3435                 :            : 
    3436                 :            : 
    3437                 :            : static PyObject *
    3438                 :       6294 : type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
    3439                 :            : {
    3440                 :            :     assert(args != NULL && PyTuple_Check(args));
    3441                 :            :     assert(kwds == NULL || PyDict_Check(kwds));
    3442                 :            : 
    3443                 :            :     /* Parse arguments: (name, bases, dict) */
    3444                 :            :     PyObject *name, *bases, *orig_dict;
    3445         [ -  + ]:       6294 :     if (!PyArg_ParseTuple(args, "UO!O!:type.__new__",
    3446                 :            :                           &name,
    3447                 :            :                           &PyTuple_Type, &bases,
    3448                 :            :                           &PyDict_Type, &orig_dict))
    3449                 :            :     {
    3450                 :          0 :         return NULL;
    3451                 :            :     }
    3452                 :            : 
    3453                 :       6294 :     type_new_ctx ctx = {
    3454                 :            :         .metatype = metatype,
    3455                 :            :         .args = args,
    3456                 :            :         .kwds = kwds,
    3457                 :            :         .orig_dict = orig_dict,
    3458                 :            :         .name = name,
    3459                 :            :         .bases = bases,
    3460                 :            :         .base = NULL,
    3461                 :            :         .slots = NULL,
    3462                 :            :         .nslot = 0,
    3463                 :            :         .add_dict = 0,
    3464                 :            :         .add_weak = 0,
    3465                 :            :         .may_add_dict = 0,
    3466                 :            :         .may_add_weak = 0};
    3467                 :       6294 :     PyObject *type = NULL;
    3468                 :       6294 :     int res = type_new_get_bases(&ctx, &type);
    3469         [ -  + ]:       6294 :     if (res < 0) {
    3470                 :            :         assert(PyErr_Occurred());
    3471                 :          0 :         return NULL;
    3472                 :            :     }
    3473         [ +  + ]:       6294 :     if (res == 1) {
    3474                 :            :         assert(type != NULL);
    3475                 :         34 :         return type;
    3476                 :            :     }
    3477                 :            :     assert(ctx.base != NULL);
    3478                 :            :     assert(ctx.bases != NULL);
    3479                 :            : 
    3480                 :       6260 :     type = type_new_impl(&ctx);
    3481                 :       6260 :     Py_DECREF(ctx.bases);
    3482                 :       6260 :     return type;
    3483                 :            : }
    3484                 :            : 
    3485                 :            : 
    3486                 :            : static PyObject *
    3487                 :       6198 : type_vectorcall(PyObject *metatype, PyObject *const *args,
    3488                 :            :                  size_t nargsf, PyObject *kwnames)
    3489                 :            : {
    3490                 :       6198 :     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
    3491   [ +  +  +  - ]:       6198 :     if (nargs == 1 && metatype == (PyObject *)&PyType_Type){
    3492   [ -  +  -  - ]:       1010 :         if (!_PyArg_NoKwnames("type", kwnames)) {
    3493                 :          0 :             return NULL;
    3494                 :            :         }
    3495                 :       1010 :         return Py_NewRef(Py_TYPE(args[0]));
    3496                 :            :     }
    3497                 :            :     /* In other (much less common) cases, fall back to
    3498                 :            :        more flexible calling conventions. */
    3499                 :       5188 :     PyThreadState *tstate = _PyThreadState_GET();
    3500                 :       5188 :     return _PyObject_MakeTpCall(tstate, metatype, args, nargs, kwnames);
    3501                 :            : }
    3502                 :            : 
    3503                 :            : /* An array of type slot offsets corresponding to Py_tp_* constants,
    3504                 :            :   * for use in e.g. PyType_Spec and PyType_GetSlot.
    3505                 :            :   * Each entry has two offsets: "slot_offset" and "subslot_offset".
    3506                 :            :   * If is subslot_offset is -1, slot_offset is an offset within the
    3507                 :            :   * PyTypeObject struct.
    3508                 :            :   * Otherwise slot_offset is an offset to a pointer to a sub-slots struct
    3509                 :            :   * (such as "tp_as_number"), and subslot_offset is the offset within
    3510                 :            :   * that struct.
    3511                 :            :   * The actual table is generated by a script.
    3512                 :            :   */
    3513                 :            : static const PySlot_Offset pyslot_offsets[] = {
    3514                 :            :     {0, 0},
    3515                 :            : #include "typeslots.inc"
    3516                 :            : };
    3517                 :            : 
    3518                 :            : /* Given a PyType_FromMetaclass `bases` argument (NULL, type, or tuple of
    3519                 :            :  * types), return a tuple of types.
    3520                 :            :  */
    3521                 :            : inline static PyObject *
    3522                 :       1012 : get_bases_tuple(PyObject *bases_in, PyType_Spec *spec)
    3523                 :            : {
    3524         [ +  + ]:       1012 :     if (!bases_in) {
    3525                 :            :         /* Default: look in the spec, fall back to (type,). */
    3526                 :        541 :         PyTypeObject *base = &PyBaseObject_Type;  // borrowed ref
    3527                 :        541 :         PyObject *bases = NULL;  // borrowed ref
    3528                 :            :         const PyType_Slot *slot;
    3529         [ +  + ]:       4291 :         for (slot = spec->slots; slot->slot; slot++) {
    3530      [ +  -  + ]:       3750 :             switch (slot->slot) {
    3531                 :          3 :                 case Py_tp_base:
    3532                 :          3 :                     base = slot->pfunc;
    3533                 :          3 :                     break;
    3534                 :          0 :                 case Py_tp_bases:
    3535                 :          0 :                     bases = slot->pfunc;
    3536                 :          0 :                     break;
    3537                 :            :             }
    3538                 :       3750 :         }
    3539         [ +  - ]:        541 :         if (!bases) {
    3540                 :        541 :             return PyTuple_Pack(1, base);
    3541                 :            :         }
    3542         [ #  # ]:          0 :         if (PyTuple_Check(bases)) {
    3543                 :          0 :             return Py_NewRef(bases);
    3544                 :            :         }
    3545                 :          0 :         PyErr_SetString(PyExc_SystemError, "Py_tp_bases is not a tuple");
    3546                 :          0 :         return NULL;
    3547                 :            :     }
    3548         [ +  + ]:        471 :     if (PyTuple_Check(bases_in)) {
    3549                 :          5 :         return Py_NewRef(bases_in);
    3550                 :            :     }
    3551                 :            :     // Not a tuple, should be a single type
    3552                 :        466 :     return PyTuple_Pack(1, bases_in);
    3553                 :            : }
    3554                 :            : 
    3555                 :            : static inline int
    3556                 :       1012 : check_basicsize_includes_size_and_offsets(PyTypeObject* type)
    3557                 :            : {
    3558         [ -  + ]:       1012 :     if (type->tp_alloc != PyType_GenericAlloc) {
    3559                 :            :         // Custom allocators can ignore tp_basicsize
    3560                 :          0 :         return 1;
    3561                 :            :     }
    3562                 :       1012 :     Py_ssize_t max = (Py_ssize_t)type->tp_basicsize;
    3563                 :            : 
    3564   [ +  -  -  + ]:       1012 :     if (type->tp_base && type->tp_base->tp_basicsize > type->tp_basicsize) {
    3565                 :          0 :         PyErr_Format(PyExc_TypeError,
    3566                 :            :                      "tp_basicsize for type '%s' (%d) is too small for base '%s' (%d)",
    3567                 :            :                      type->tp_name, type->tp_basicsize,
    3568                 :          0 :                      type->tp_base->tp_name, type->tp_base->tp_basicsize);
    3569                 :          0 :         return 0;
    3570                 :            :     }
    3571         [ -  + ]:       1012 :     if (type->tp_weaklistoffset + (Py_ssize_t)sizeof(PyObject*) > max) {
    3572                 :          0 :         PyErr_Format(PyExc_TypeError,
    3573                 :            :                      "weaklist offset %d is out of bounds for type '%s' (tp_basicsize = %d)",
    3574                 :            :                      type->tp_weaklistoffset,
    3575                 :            :                      type->tp_name, type->tp_basicsize);
    3576                 :          0 :         return 0;
    3577                 :            :     }
    3578         [ -  + ]:       1012 :     if (type->tp_dictoffset + (Py_ssize_t)sizeof(PyObject*) > max) {
    3579                 :          0 :         PyErr_Format(PyExc_TypeError,
    3580                 :            :                      "dict offset %d is out of bounds for type '%s' (tp_basicsize = %d)",
    3581                 :            :                      type->tp_dictoffset,
    3582                 :            :                      type->tp_name, type->tp_basicsize);
    3583                 :          0 :         return 0;
    3584                 :            :     }
    3585         [ -  + ]:       1012 :     if (type->tp_vectorcall_offset + (Py_ssize_t)sizeof(vectorcallfunc*) > max) {
    3586                 :          0 :         PyErr_Format(PyExc_TypeError,
    3587                 :            :                      "vectorcall offset %d is out of bounds for type '%s' (tp_basicsize = %d)",
    3588                 :            :                      type->tp_vectorcall_offset,
    3589                 :            :                      type->tp_name, type->tp_basicsize);
    3590                 :          0 :         return 0;
    3591                 :            :     }
    3592                 :       1012 :     return 1;
    3593                 :            : }
    3594                 :            : 
    3595                 :            : PyObject *
    3596                 :       1012 : PyType_FromMetaclass(PyTypeObject *metaclass, PyObject *module,
    3597                 :            :                      PyType_Spec *spec, PyObject *bases_in)
    3598                 :            : {
    3599                 :            :     /* Invariant: A non-NULL value in one of these means this function holds
    3600                 :            :      * a strong reference or owns allocated memory.
    3601                 :            :      * These get decrefed/freed/returned at the end, on both success and error.
    3602                 :            :      */
    3603                 :       1012 :     PyHeapTypeObject *res = NULL;
    3604                 :            :     PyTypeObject *type;
    3605                 :       1012 :     PyObject *bases = NULL;
    3606                 :       1012 :     char *tp_doc = NULL;
    3607                 :       1012 :     PyObject *ht_name = NULL;
    3608                 :       1012 :     char *_ht_tpname = NULL;
    3609                 :            : 
    3610                 :            :     int r;
    3611                 :            : 
    3612                 :            :     /* Prepare slots that need special handling.
    3613                 :            :      * Keep in mind that a slot can be given multiple times:
    3614                 :            :      * if that would cause trouble (leaks, UB, ...), raise an exception.
    3615                 :            :      */
    3616                 :            : 
    3617                 :            :     const PyType_Slot *slot;
    3618                 :       1012 :     Py_ssize_t nmembers = 0;
    3619                 :            :     Py_ssize_t weaklistoffset, dictoffset, vectorcalloffset;
    3620                 :            :     char *res_start;
    3621                 :            : 
    3622                 :       1012 :     nmembers = weaklistoffset = dictoffset = vectorcalloffset = 0;
    3623         [ +  + ]:       8567 :     for (slot = spec->slots; slot->slot; slot++) {
    3624         [ +  - ]:       7555 :         if (slot->slot < 0
    3625         [ -  + ]:       7555 :             || (size_t)slot->slot >= Py_ARRAY_LENGTH(pyslot_offsets)) {
    3626                 :          0 :             PyErr_SetString(PyExc_RuntimeError, "invalid slot offset");
    3627                 :          0 :             goto finally;
    3628                 :            :         }
    3629      [ +  +  + ]:       7555 :         switch (slot->slot) {
    3630                 :        722 :         case Py_tp_members:
    3631         [ -  + ]:        722 :             if (nmembers != 0) {
    3632                 :          0 :                 PyErr_SetString(
    3633                 :            :                     PyExc_SystemError,
    3634                 :            :                     "Multiple Py_tp_members slots are not supported.");
    3635                 :          0 :                 goto finally;
    3636                 :            :             }
    3637         [ +  + ]:       3602 :             for (const PyMemberDef *memb = slot->pfunc; memb->name != NULL; memb++) {
    3638                 :       2880 :                 nmembers++;
    3639         [ +  + ]:       2880 :                 if (strcmp(memb->name, "__weaklistoffset__") == 0) {
    3640                 :            :                     // The PyMemberDef must be a Py_ssize_t and readonly
    3641                 :            :                     assert(memb->type == T_PYSSIZET);
    3642                 :            :                     assert(memb->flags == READONLY);
    3643                 :        352 :                     weaklistoffset = memb->offset;
    3644                 :            :                 }
    3645         [ +  + ]:       2880 :                 if (strcmp(memb->name, "__dictoffset__") == 0) {
    3646                 :            :                     // The PyMemberDef must be a Py_ssize_t and readonly
    3647                 :            :                     assert(memb->type == T_PYSSIZET);
    3648                 :            :                     assert(memb->flags == READONLY);
    3649                 :        252 :                     dictoffset = memb->offset;
    3650                 :            :                 }
    3651         [ +  + ]:       2880 :                 if (strcmp(memb->name, "__vectorcalloffset__") == 0) {
    3652                 :            :                     // The PyMemberDef must be a Py_ssize_t and readonly
    3653                 :            :                     assert(memb->type == T_PYSSIZET);
    3654                 :            :                     assert(memb->flags == READONLY);
    3655                 :         20 :                     vectorcalloffset = memb->offset;
    3656                 :            :                 }
    3657                 :            :             }
    3658                 :        722 :             break;
    3659                 :        804 :         case Py_tp_doc:
    3660                 :            :             /* For the docstring slot, which usually points to a static string
    3661                 :            :                literal, we need to make a copy */
    3662         [ -  + ]:        804 :             if (tp_doc != NULL) {
    3663                 :          0 :                 PyErr_SetString(
    3664                 :            :                     PyExc_SystemError,
    3665                 :            :                     "Multiple Py_tp_doc slots are not supported.");
    3666                 :          0 :                 goto finally;
    3667                 :            :             }
    3668         [ +  + ]:        804 :             if (slot->pfunc == NULL) {
    3669                 :          4 :                 PyObject_Free(tp_doc);
    3670                 :          4 :                 tp_doc = NULL;
    3671                 :            :             }
    3672                 :            :             else {
    3673                 :        800 :                 size_t len = strlen(slot->pfunc)+1;
    3674                 :        800 :                 tp_doc = PyObject_Malloc(len);
    3675         [ -  + ]:        800 :                 if (tp_doc == NULL) {
    3676                 :          0 :                     PyErr_NoMemory();
    3677                 :          0 :                     goto finally;
    3678                 :            :                 }
    3679                 :        800 :                 memcpy(tp_doc, slot->pfunc, len);
    3680                 :            :             }
    3681                 :        804 :             break;
    3682                 :            :         }
    3683                 :       7555 :     }
    3684                 :            : 
    3685                 :            :     /* Prepare the type name and qualname */
    3686                 :            : 
    3687         [ -  + ]:       1012 :     if (spec->name == NULL) {
    3688                 :          0 :         PyErr_SetString(PyExc_SystemError,
    3689                 :            :                         "Type spec does not define the name field.");
    3690                 :          0 :         goto finally;
    3691                 :            :     }
    3692                 :            : 
    3693                 :       1012 :     const char *s = strrchr(spec->name, '.');
    3694         [ -  + ]:       1012 :     if (s == NULL) {
    3695                 :          0 :         s = spec->name;
    3696                 :            :     }
    3697                 :            :     else {
    3698                 :       1012 :         s++;
    3699                 :            :     }
    3700                 :            : 
    3701                 :       1012 :     ht_name = PyUnicode_FromString(s);
    3702         [ -  + ]:       1012 :     if (!ht_name) {
    3703                 :          0 :         goto finally;
    3704                 :            :     }
    3705                 :            : 
    3706                 :            :     /* Copy spec->name to a buffer we own.
    3707                 :            :     *
    3708                 :            :     * Unfortunately, we can't use tp_name directly (with some
    3709                 :            :     * flag saying that it should be deallocated with the type),
    3710                 :            :     * because tp_name is public API and may be set independently
    3711                 :            :     * of any such flag.
    3712                 :            :     * So, we use a separate buffer, _ht_tpname, that's always
    3713                 :            :     * deallocated with the type (if it's non-NULL).
    3714                 :            :     */
    3715                 :       1012 :     Py_ssize_t name_buf_len = strlen(spec->name) + 1;
    3716                 :       1012 :     _ht_tpname = PyMem_Malloc(name_buf_len);
    3717         [ -  + ]:       1012 :     if (_ht_tpname == NULL) {
    3718                 :          0 :         goto finally;
    3719                 :            :     }
    3720                 :       1012 :     memcpy(_ht_tpname, spec->name, name_buf_len);
    3721                 :            : 
    3722                 :            :     /* Get a tuple of bases.
    3723                 :            :      * bases is a strong reference (unlike bases_in).
    3724                 :            :      */
    3725                 :       1012 :     bases = get_bases_tuple(bases_in, spec);
    3726         [ -  + ]:       1012 :     if (!bases) {
    3727                 :          0 :         goto finally;
    3728                 :            :     }
    3729                 :            : 
    3730                 :            :     /* If this is an immutable type, check if all bases are also immutable,
    3731                 :            :      * and (for now) fire a deprecation warning if not.
    3732                 :            :      * (This isn't necessary for static types: those can't have heap bases,
    3733                 :            :      * and only heap types can be mutable.)
    3734                 :            :      */
    3735         [ +  + ]:       1012 :     if (spec->flags & Py_TPFLAGS_IMMUTABLETYPE) {
    3736         [ +  + ]:       1198 :         for (int i=0; i<PyTuple_GET_SIZE(bases); i++) {
    3737                 :        599 :             PyTypeObject *b = (PyTypeObject*)PyTuple_GET_ITEM(bases, i);
    3738         [ -  + ]:        599 :             if (!b) {
    3739                 :          0 :                 goto finally;
    3740                 :            :             }
    3741         [ -  + ]:        599 :             if (!_PyType_HasFeature(b, Py_TPFLAGS_IMMUTABLETYPE)) {
    3742         [ #  # ]:          0 :                 if (PyErr_WarnFormat(
    3743                 :            :                     PyExc_DeprecationWarning,
    3744                 :            :                     0,
    3745                 :            :                     "Creating immutable type %s from mutable base %s is "
    3746                 :            :                     "deprecated, and slated to be disallowed in Python 3.14.",
    3747                 :            :                     spec->name,
    3748                 :            :                     b->tp_name))
    3749                 :            :                 {
    3750                 :          0 :                     goto finally;
    3751                 :            :                 }
    3752                 :            :             }
    3753                 :            :         }
    3754                 :            :     }
    3755                 :            : 
    3756                 :            :     /* Calculate the metaclass */
    3757                 :            : 
    3758         [ +  + ]:       1012 :     if (!metaclass) {
    3759                 :       1008 :         metaclass = &PyType_Type;
    3760                 :            :     }
    3761                 :       1012 :     metaclass = _PyType_CalculateMetaclass(metaclass, bases);
    3762         [ -  + ]:       1012 :     if (metaclass == NULL) {
    3763                 :          0 :         goto finally;
    3764                 :            :     }
    3765         [ -  + ]:       1012 :     if (!PyType_Check(metaclass)) {
    3766                 :          0 :         PyErr_Format(PyExc_TypeError,
    3767                 :            :                      "Metaclass '%R' is not a subclass of 'type'.",
    3768                 :            :                      metaclass);
    3769                 :          0 :         goto finally;
    3770                 :            :     }
    3771         [ -  + ]:       1012 :     if (metaclass->tp_new != PyType_Type.tp_new) {
    3772                 :          0 :         PyErr_SetString(PyExc_TypeError,
    3773                 :            :                         "Metaclasses with custom tp_new are not supported.");
    3774                 :          0 :         goto finally;
    3775                 :            :     }
    3776                 :            : 
    3777                 :            :     /* Calculate best base, and check that all bases are type objects */
    3778                 :       1012 :     PyTypeObject *base = best_base(bases);  // borrowed ref
    3779         [ -  + ]:       1012 :     if (base == NULL) {
    3780                 :          0 :         goto finally;
    3781                 :            :     }
    3782                 :            :     // best_base should check Py_TPFLAGS_BASETYPE & raise a proper exception,
    3783                 :            :     // here we just check its work
    3784                 :            :     assert(_PyType_HasFeature(base, Py_TPFLAGS_BASETYPE));
    3785                 :            : 
    3786                 :            :     /* Allocate the new type
    3787                 :            :      *
    3788                 :            :      * Between here and PyType_Ready, we should limit:
    3789                 :            :      * - calls to Python code
    3790                 :            :      * - raising exceptions
    3791                 :            :      * - memory allocations
    3792                 :            :      */
    3793                 :            : 
    3794                 :       1012 :     res = (PyHeapTypeObject*)metaclass->tp_alloc(metaclass, nmembers);
    3795         [ -  + ]:       1012 :     if (res == NULL) {
    3796                 :          0 :         goto finally;
    3797                 :            :     }
    3798                 :       1012 :     res_start = (char*)res;
    3799                 :            : 
    3800                 :       1012 :     type = &res->ht_type;
    3801                 :            :     /* The flags must be initialized early, before the GC traverses us */
    3802                 :       1012 :     type->tp_flags = spec->flags | Py_TPFLAGS_HEAPTYPE;
    3803                 :            : 
    3804                 :       1012 :     res->ht_module = Py_XNewRef(module);
    3805                 :            : 
    3806                 :            :     /* Initialize essential fields */
    3807                 :            : 
    3808                 :       1012 :     type->tp_as_async = &res->as_async;
    3809                 :       1012 :     type->tp_as_number = &res->as_number;
    3810                 :       1012 :     type->tp_as_sequence = &res->as_sequence;
    3811                 :       1012 :     type->tp_as_mapping = &res->as_mapping;
    3812                 :       1012 :     type->tp_as_buffer = &res->as_buffer;
    3813                 :            : 
    3814                 :            :     /* Set slots we have prepared */
    3815                 :            : 
    3816                 :       1012 :     type->tp_base = (PyTypeObject *)Py_NewRef(base);
    3817                 :       1012 :     type->tp_bases = bases;
    3818                 :       1012 :     bases = NULL;  // We give our reference to bases to the type
    3819                 :            : 
    3820                 :       1012 :     type->tp_doc = tp_doc;
    3821                 :       1012 :     tp_doc = NULL;  // Give ownership of the allocated memory to the type
    3822                 :            : 
    3823                 :       1012 :     res->ht_qualname = Py_NewRef(ht_name);
    3824                 :       1012 :     res->ht_name = ht_name;
    3825                 :       1012 :     ht_name = NULL;  // Give our reference to the type
    3826                 :            : 
    3827                 :       1012 :     type->tp_name = _ht_tpname;
    3828                 :       1012 :     res->_ht_tpname = _ht_tpname;
    3829                 :       1012 :     _ht_tpname = NULL;  // Give ownership to the type
    3830                 :            : 
    3831                 :            :     /* Copy the sizes */
    3832                 :            : 
    3833                 :       1012 :     type->tp_basicsize = spec->basicsize;
    3834                 :       1012 :     type->tp_itemsize = spec->itemsize;
    3835                 :            : 
    3836                 :            :     /* Copy all the ordinary slots */
    3837                 :            : 
    3838         [ +  + ]:       8567 :     for (slot = spec->slots; slot->slot; slot++) {
    3839      [ +  +  + ]:       7555 :         switch (slot->slot) {
    3840                 :        807 :         case Py_tp_base:
    3841                 :            :         case Py_tp_bases:
    3842                 :            :         case Py_tp_doc:
    3843                 :            :             /* Processed above */
    3844                 :        807 :             break;
    3845                 :        722 :         case Py_tp_members:
    3846                 :            :             {
    3847                 :            :                 /* Move the slots to the heap type itself */
    3848                 :        722 :                 size_t len = Py_TYPE(type)->tp_itemsize * nmembers;
    3849                 :        722 :                 memcpy(_PyHeapType_GET_MEMBERS(res), slot->pfunc, len);
    3850                 :        722 :                 type->tp_members = _PyHeapType_GET_MEMBERS(res);
    3851                 :            :             }
    3852                 :        722 :             break;
    3853                 :       6026 :         default:
    3854                 :            :             {
    3855                 :            :                 /* Copy other slots directly */
    3856                 :       6026 :                 PySlot_Offset slotoffsets = pyslot_offsets[slot->slot];
    3857                 :       6026 :                 short slot_offset = slotoffsets.slot_offset;
    3858         [ +  + ]:       6026 :                 if (slotoffsets.subslot_offset == -1) {
    3859                 :       5952 :                     *(void**)((char*)res_start + slot_offset) = slot->pfunc;
    3860                 :            :                 }
    3861                 :            :                 else {
    3862                 :         74 :                     void *procs = *(void**)((char*)res_start + slot_offset);
    3863                 :         74 :                     short subslot_offset = slotoffsets.subslot_offset;
    3864                 :         74 :                     *(void**)((char*)procs + subslot_offset) = slot->pfunc;
    3865                 :            :                 }
    3866                 :            :             }
    3867                 :       6026 :             break;
    3868                 :            :         }
    3869                 :            :     }
    3870         [ +  + ]:       1012 :     if (type->tp_dealloc == NULL) {
    3871                 :            :         /* It's a heap type, so needs the heap types' dealloc.
    3872                 :            :            subtype_dealloc will call the base type's tp_dealloc, if
    3873                 :            :            necessary. */
    3874                 :         31 :         type->tp_dealloc = subtype_dealloc;
    3875                 :            :     }
    3876                 :            : 
    3877                 :            :     /* Set up offsets */
    3878                 :            : 
    3879                 :       1012 :     type->tp_vectorcall_offset = vectorcalloffset;
    3880                 :       1012 :     type->tp_weaklistoffset = weaklistoffset;
    3881                 :       1012 :     type->tp_dictoffset = dictoffset;
    3882                 :            : 
    3883                 :            :     /* Ready the type (which includes inheritance).
    3884                 :            :      *
    3885                 :            :      * After this call we should generally only touch up what's
    3886                 :            :      * accessible to Python code, like __dict__.
    3887                 :            :      */
    3888                 :            : 
    3889         [ -  + ]:       1012 :     if (PyType_Ready(type) < 0) {
    3890                 :          0 :         goto finally;
    3891                 :            :     }
    3892                 :            : 
    3893         [ -  + ]:       1012 :     if (!check_basicsize_includes_size_and_offsets(type)) {
    3894                 :          0 :         goto finally;
    3895                 :            :     }
    3896                 :            : 
    3897         [ +  + ]:       1012 :     if (type->tp_doc) {
    3898                 :        800 :         PyObject *__doc__ = PyUnicode_FromString(_PyType_DocWithoutSignature(type->tp_name, type->tp_doc));
    3899         [ -  + ]:        800 :         if (!__doc__) {
    3900                 :          0 :             goto finally;
    3901                 :            :         }
    3902                 :        800 :         r = PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), __doc__);
    3903                 :        800 :         Py_DECREF(__doc__);
    3904         [ -  + ]:        800 :         if (r < 0) {
    3905                 :          0 :             goto finally;
    3906                 :            :         }
    3907                 :            :     }
    3908                 :            : 
    3909         [ +  + ]:       1012 :     if (weaklistoffset) {
    3910         [ -  + ]:        352 :         if (PyDict_DelItem((PyObject *)type->tp_dict, &_Py_ID(__weaklistoffset__)) < 0) {
    3911                 :          0 :             goto finally;
    3912                 :            :         }
    3913                 :            :     }
    3914         [ +  + ]:       1012 :     if (dictoffset) {
    3915         [ -  + ]:        252 :         if (PyDict_DelItem((PyObject *)type->tp_dict, &_Py_ID(__dictoffset__)) < 0) {
    3916                 :          0 :             goto finally;
    3917                 :            :         }
    3918                 :            :     }
    3919                 :            : 
    3920                 :            :     /* Set type.__module__ */
    3921                 :       1012 :     r = PyDict_Contains(type->tp_dict, &_Py_ID(__module__));
    3922         [ -  + ]:       1012 :     if (r < 0) {
    3923                 :          0 :         goto finally;
    3924                 :            :     }
    3925         [ -  + ]:       1012 :     if (r == 0) {
    3926                 :       1012 :         s = strrchr(spec->name, '.');
    3927         [ +  - ]:       1012 :         if (s != NULL) {
    3928                 :       1012 :             PyObject *modname = PyUnicode_FromStringAndSize(
    3929                 :       1012 :                     spec->name, (Py_ssize_t)(s - spec->name));
    3930         [ -  + ]:       1012 :             if (modname == NULL) {
    3931                 :          0 :                 goto finally;
    3932                 :            :             }
    3933                 :       1012 :             r = PyDict_SetItem(type->tp_dict, &_Py_ID(__module__), modname);
    3934                 :       1012 :             Py_DECREF(modname);
    3935         [ -  + ]:       1012 :             if (r != 0) {
    3936                 :          0 :                 goto finally;
    3937                 :            :             }
    3938                 :            :         }
    3939                 :            :         else {
    3940         [ #  # ]:          0 :             if (PyErr_WarnFormat(PyExc_DeprecationWarning, 1,
    3941                 :            :                     "builtin type %.200s has no __module__ attribute",
    3942                 :            :                     spec->name))
    3943                 :          0 :                 goto finally;
    3944                 :            :         }
    3945                 :            :     }
    3946                 :            : 
    3947                 :            :     assert(_PyType_CheckConsistency(type));
    3948                 :            : 
    3949                 :          0 :  finally:
    3950         [ -  + ]:       1012 :     if (PyErr_Occurred()) {
    3951         [ #  # ]:          0 :         Py_CLEAR(res);
    3952                 :            :     }
    3953                 :       1012 :     Py_XDECREF(bases);
    3954                 :       1012 :     PyObject_Free(tp_doc);
    3955                 :       1012 :     Py_XDECREF(ht_name);
    3956                 :       1012 :     PyMem_Free(_ht_tpname);
    3957                 :       1012 :     return (PyObject*)res;
    3958                 :            : }
    3959                 :            : 
    3960                 :            : PyObject *
    3961                 :        605 : PyType_FromModuleAndSpec(PyObject *module, PyType_Spec *spec, PyObject *bases)
    3962                 :            : {
    3963                 :        605 :     return PyType_FromMetaclass(NULL, module, spec, bases);
    3964                 :            : }
    3965                 :            : 
    3966                 :            : PyObject *
    3967                 :        255 : PyType_FromSpecWithBases(PyType_Spec *spec, PyObject *bases)
    3968                 :            : {
    3969                 :        255 :     return PyType_FromMetaclass(NULL, NULL, spec, bases);
    3970                 :            : }
    3971                 :            : 
    3972                 :            : PyObject *
    3973                 :        143 : PyType_FromSpec(PyType_Spec *spec)
    3974                 :            : {
    3975                 :        143 :     return PyType_FromMetaclass(NULL, NULL, spec, NULL);
    3976                 :            : }
    3977                 :            : 
    3978                 :            : PyObject *
    3979                 :          0 : PyType_GetName(PyTypeObject *type)
    3980                 :            : {
    3981                 :          0 :     return type_name(type, NULL);
    3982                 :            : }
    3983                 :            : 
    3984                 :            : PyObject *
    3985                 :          0 : PyType_GetQualName(PyTypeObject *type)
    3986                 :            : {
    3987                 :          0 :     return type_qualname(type, NULL);
    3988                 :            : }
    3989                 :            : 
    3990                 :            : void *
    3991                 :     408704 : PyType_GetSlot(PyTypeObject *type, int slot)
    3992                 :            : {
    3993                 :            :     void *parent_slot;
    3994                 :     408704 :     int slots_len = Py_ARRAY_LENGTH(pyslot_offsets);
    3995                 :            : 
    3996   [ +  -  -  + ]:     408704 :     if (slot <= 0 || slot >= slots_len) {
    3997                 :          0 :         PyErr_BadInternalCall();
    3998                 :          0 :         return NULL;
    3999                 :            :     }
    4000                 :            : 
    4001                 :     408704 :     parent_slot = *(void**)((char*)type + pyslot_offsets[slot].slot_offset);
    4002         [ -  + ]:     408704 :     if (parent_slot == NULL) {
    4003                 :          0 :         return NULL;
    4004                 :            :     }
    4005                 :            :     /* Return slot directly if we have no sub slot. */
    4006         [ +  - ]:     408704 :     if (pyslot_offsets[slot].subslot_offset == -1) {
    4007                 :     408704 :         return parent_slot;
    4008                 :            :     }
    4009                 :          0 :     return *(void**)((char*)parent_slot + pyslot_offsets[slot].subslot_offset);
    4010                 :            : }
    4011                 :            : 
    4012                 :            : PyObject *
    4013                 :    2900361 : PyType_GetModule(PyTypeObject *type)
    4014                 :            : {
    4015                 :            :     assert(PyType_Check(type));
    4016         [ -  + ]:    2900361 :     if (!_PyType_HasFeature(type, Py_TPFLAGS_HEAPTYPE)) {
    4017                 :          0 :         PyErr_Format(
    4018                 :            :             PyExc_TypeError,
    4019                 :            :             "PyType_GetModule: Type '%s' is not a heap type",
    4020                 :            :             type->tp_name);
    4021                 :          0 :         return NULL;
    4022                 :            :     }
    4023                 :            : 
    4024                 :    2900361 :     PyHeapTypeObject* et = (PyHeapTypeObject*)type;
    4025         [ -  + ]:    2900361 :     if (!et->ht_module) {
    4026                 :          0 :         PyErr_Format(
    4027                 :            :             PyExc_TypeError,
    4028                 :            :             "PyType_GetModule: Type '%s' has no associated module",
    4029                 :            :             type->tp_name);
    4030                 :          0 :         return NULL;
    4031                 :            :     }
    4032                 :    2900361 :     return et->ht_module;
    4033                 :            : 
    4034                 :            : }
    4035                 :            : 
    4036                 :            : void *
    4037                 :          0 : PyType_GetModuleState(PyTypeObject *type)
    4038                 :            : {
    4039                 :          0 :     PyObject *m = PyType_GetModule(type);
    4040         [ #  # ]:          0 :     if (m == NULL) {
    4041                 :          0 :         return NULL;
    4042                 :            :     }
    4043                 :          0 :     return _PyModule_GetState(m);
    4044                 :            : }
    4045                 :            : 
    4046                 :            : 
    4047                 :            : /* Get the module of the first superclass where the module has the
    4048                 :            :  * given PyModuleDef.
    4049                 :            :  */
    4050                 :            : PyObject *
    4051                 :       2573 : PyType_GetModuleByDef(PyTypeObject *type, PyModuleDef *def)
    4052                 :            : {
    4053                 :            :     assert(PyType_Check(type));
    4054                 :            : 
    4055                 :       2573 :     PyObject *mro = type->tp_mro;
    4056                 :            :     // The type must be ready
    4057                 :            :     assert(mro != NULL);
    4058                 :            :     assert(PyTuple_Check(mro));
    4059                 :            :     // mro_invoke() ensures that the type MRO cannot be empty, so we don't have
    4060                 :            :     // to check i < PyTuple_GET_SIZE(mro) at the first loop iteration.
    4061                 :            :     assert(PyTuple_GET_SIZE(mro) >= 1);
    4062                 :            : 
    4063                 :       2573 :     Py_ssize_t n = PyTuple_GET_SIZE(mro);
    4064         [ +  - ]:       2575 :     for (Py_ssize_t i = 0; i < n; i++) {
    4065                 :       2575 :         PyObject *super = PyTuple_GET_ITEM(mro, i);
    4066         [ -  + ]:       2575 :         if(!_PyType_HasFeature((PyTypeObject *)super, Py_TPFLAGS_HEAPTYPE)) {
    4067                 :            :             // Static types in the MRO need to be skipped
    4068                 :          0 :             continue;
    4069                 :            :         }
    4070                 :            : 
    4071                 :       2575 :         PyHeapTypeObject *ht = (PyHeapTypeObject*)super;
    4072                 :       2575 :         PyObject *module = ht->ht_module;
    4073   [ +  +  +  - ]:       2575 :         if (module && _PyModule_GetDef(module) == def) {
    4074                 :       2573 :             return module;
    4075                 :            :         }
    4076                 :            :     }
    4077                 :            : 
    4078                 :          0 :     PyErr_Format(
    4079                 :            :         PyExc_TypeError,
    4080                 :            :         "PyType_GetModuleByDef: No superclass of '%s' has the given module",
    4081                 :            :         type->tp_name);
    4082                 :          0 :     return NULL;
    4083                 :            : }
    4084                 :            : 
    4085                 :            : 
    4086                 :            : /* Internal API to look for a name through the MRO, bypassing the method cache.
    4087                 :            :    This returns a borrowed reference, and might set an exception.
    4088                 :            :    'error' is set to: -1: error with exception; 1: error without exception; 0: ok */
    4089                 :            : static PyObject *
    4090                 :    2076853 : find_name_in_mro(PyTypeObject *type, PyObject *name, int *error)
    4091                 :            : {
    4092                 :            :     Py_hash_t hash;
    4093         [ +  - ]:    2076853 :     if (!PyUnicode_CheckExact(name) ||
    4094         [ +  + ]:    2076853 :         (hash = _PyASCIIObject_CAST(name)->hash) == -1)
    4095                 :            :     {
    4096                 :    1448571 :         hash = PyObject_Hash(name);
    4097         [ -  + ]:    1448571 :         if (hash == -1) {
    4098                 :          0 :             *error = -1;
    4099                 :          0 :             return NULL;
    4100                 :            :         }
    4101                 :            :     }
    4102                 :            : 
    4103                 :            :     /* Look in tp_dict of types in MRO */
    4104                 :    2076853 :     PyObject *mro = type->tp_mro;
    4105         [ -  + ]:    2076853 :     if (mro == NULL) {
    4106         [ #  # ]:          0 :         if ((type->tp_flags & Py_TPFLAGS_READYING) == 0) {
    4107         [ #  # ]:          0 :             if (PyType_Ready(type) < 0) {
    4108                 :          0 :                 *error = -1;
    4109                 :          0 :                 return NULL;
    4110                 :            :             }
    4111                 :          0 :             mro = type->tp_mro;
    4112                 :            :         }
    4113         [ #  # ]:          0 :         if (mro == NULL) {
    4114                 :          0 :             *error = 1;
    4115                 :          0 :             return NULL;
    4116                 :            :         }
    4117                 :            :     }
    4118                 :            : 
    4119                 :    2076853 :     PyObject *res = NULL;
    4120                 :            :     /* Keep a strong reference to mro because type->tp_mro can be replaced
    4121                 :            :        during dict lookup, e.g. when comparing to non-string keys. */
    4122                 :    2076853 :     Py_INCREF(mro);
    4123                 :    2076853 :     Py_ssize_t n = PyTuple_GET_SIZE(mro);
    4124         [ +  + ]:    4143260 :     for (Py_ssize_t i = 0; i < n; i++) {
    4125                 :    3642574 :         PyObject *base = PyTuple_GET_ITEM(mro, i);
    4126                 :    3642574 :         PyObject *dict = _PyType_CAST(base)->tp_dict;
    4127                 :            :         assert(dict && PyDict_Check(dict));
    4128                 :    3642574 :         res = _PyDict_GetItem_KnownHash(dict, name, hash);
    4129         [ +  + ]:    3642574 :         if (res != NULL) {
    4130                 :    1576167 :             break;
    4131                 :            :         }
    4132         [ -  + ]:    2066407 :         if (PyErr_Occurred()) {
    4133                 :          0 :             *error = -1;
    4134                 :          0 :             goto done;
    4135                 :            :         }
    4136                 :            :     }
    4137                 :    2076853 :     *error = 0;
    4138                 :    2076853 : done:
    4139                 :    2076853 :     Py_DECREF(mro);
    4140                 :    2076853 :     return res;
    4141                 :            : }
    4142                 :            : 
    4143                 :            : /* Check if the "readied" PyUnicode name
    4144                 :            :    is a double-underscore special name. */
    4145                 :            : static int
    4146                 :       4864 : is_dunder_name(PyObject *name)
    4147                 :            : {
    4148                 :       4864 :     Py_ssize_t length = PyUnicode_GET_LENGTH(name);
    4149                 :       4864 :     int kind = PyUnicode_KIND(name);
    4150                 :            :     /* Special names contain at least "__x__" and are always ASCII. */
    4151   [ +  +  +  - ]:       4864 :     if (length > 4 && kind == PyUnicode_1BYTE_KIND) {
    4152                 :       4489 :         const Py_UCS1 *characters = PyUnicode_1BYTE_DATA(name);
    4153                 :            :         return (
    4154   [ +  +  +  + ]:       5913 :             ((characters[length-2] == '_') && (characters[length-1] == '_')) &&
    4155   [ +  -  +  - ]:       1424 :             ((characters[0] == '_') && (characters[1] == '_'))
    4156                 :            :         );
    4157                 :            :     }
    4158                 :        375 :     return 0;
    4159                 :            : }
    4160                 :            : 
    4161                 :            : /* Internal API to look for a name through the MRO.
    4162                 :            :    This returns a borrowed reference, and doesn't set an exception! */
    4163                 :            : PyObject *
    4164                 :    9040504 : _PyType_Lookup(PyTypeObject *type, PyObject *name)
    4165                 :            : {
    4166                 :            :     PyObject *res;
    4167                 :            :     int error;
    4168                 :            : 
    4169                 :    9040504 :     unsigned int h = MCACHE_HASH_METHOD(type, name);
    4170                 :    9040504 :     struct type_cache *cache = get_type_cache();
    4171                 :    9040504 :     struct type_cache_entry *entry = &cache->hashtable[h];
    4172         [ +  + ]:    9040504 :     if (entry->version == type->tp_version_tag &&
    4173         [ +  + ]:    8995464 :         entry->name == name) {
    4174                 :            :         assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
    4175                 :            :         OBJECT_STAT_INC_COND(type_cache_hits, !is_dunder_name(name));
    4176                 :            :         OBJECT_STAT_INC_COND(type_cache_dunder_hits, is_dunder_name(name));
    4177                 :    7539784 :         return entry->value;
    4178                 :            :     }
    4179                 :            :     OBJECT_STAT_INC_COND(type_cache_misses, !is_dunder_name(name));
    4180                 :            :     OBJECT_STAT_INC_COND(type_cache_dunder_misses, is_dunder_name(name));
    4181                 :            : 
    4182                 :            :     /* We may end up clearing live exceptions below, so make sure it's ours. */
    4183                 :            :     assert(!PyErr_Occurred());
    4184                 :            : 
    4185                 :    1500720 :     res = find_name_in_mro(type, name, &error);
    4186                 :            :     /* Only put NULL results into cache if there was no error. */
    4187         [ -  + ]:    1500720 :     if (error) {
    4188                 :            :         /* It's not ideal to clear the error condition,
    4189                 :            :            but this function is documented as not setting
    4190                 :            :            an exception, and I don't want to change that.
    4191                 :            :            E.g., when PyType_Ready() can't proceed, it won't
    4192                 :            :            set the "ready" flag, so future attempts to ready
    4193                 :            :            the same type will call it again -- hopefully
    4194                 :            :            in a context that propagates the exception out.
    4195                 :            :         */
    4196         [ #  # ]:          0 :         if (error == -1) {
    4197                 :          0 :             PyErr_Clear();
    4198                 :            :         }
    4199                 :          0 :         return NULL;
    4200                 :            :     }
    4201                 :            : 
    4202   [ +  -  +  -  :    1500720 :     if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
             +  -  +  - ]
    4203                 :    1500720 :         h = MCACHE_HASH_METHOD(type, name);
    4204                 :    1500720 :         struct type_cache_entry *entry = &cache->hashtable[h];
    4205                 :    1500720 :         entry->version = type->tp_version_tag;
    4206                 :    1500720 :         entry->value = res;  /* borrowed */
    4207                 :            :         assert(_PyASCIIObject_CAST(name)->hash != -1);
    4208                 :            :         OBJECT_STAT_INC_COND(type_cache_collisions, entry->name != Py_None && entry->name != name);
    4209                 :            :         assert(_PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG));
    4210                 :    1500720 :         Py_SETREF(entry->name, Py_NewRef(name));
    4211                 :            :     }
    4212                 :    1500720 :     return res;
    4213                 :            : }
    4214                 :            : 
    4215                 :            : PyObject *
    4216                 :          0 : _PyType_LookupId(PyTypeObject *type, _Py_Identifier *name)
    4217                 :            : {
    4218                 :            :     PyObject *oname;
    4219                 :          0 :     oname = _PyUnicode_FromId(name);   /* borrowed */
    4220         [ #  # ]:          0 :     if (oname == NULL)
    4221                 :          0 :         return NULL;
    4222                 :          0 :     return _PyType_Lookup(type, oname);
    4223                 :            : }
    4224                 :            : 
    4225                 :            : /* This is similar to PyObject_GenericGetAttr(),
    4226                 :            :    but uses _PyType_Lookup() instead of just looking in type->tp_dict.
    4227                 :            : 
    4228                 :            :    The argument suppress_missing_attribute is used to provide a
    4229                 :            :    fast path for hasattr. The possible values are:
    4230                 :            : 
    4231                 :            :    * NULL: do not suppress the exception
    4232                 :            :    * Non-zero pointer: suppress the PyExc_AttributeError and
    4233                 :            :      set *suppress_missing_attribute to 1 to signal we are returning NULL while
    4234                 :            :      having suppressed the exception (other exceptions are not suppressed)
    4235                 :            : 
    4236                 :            :    */
    4237                 :            : PyObject *
    4238                 :     325043 : _Py_type_getattro_impl(PyTypeObject *type, PyObject *name, int * suppress_missing_attribute)
    4239                 :            : {
    4240                 :     325043 :     PyTypeObject *metatype = Py_TYPE(type);
    4241                 :            :     PyObject *meta_attribute, *attribute;
    4242                 :            :     descrgetfunc meta_get;
    4243                 :            :     PyObject* res;
    4244                 :            : 
    4245         [ -  + ]:     325043 :     if (!PyUnicode_Check(name)) {
    4246                 :          0 :         PyErr_Format(PyExc_TypeError,
    4247                 :            :                      "attribute name must be string, not '%.200s'",
    4248                 :          0 :                      Py_TYPE(name)->tp_name);
    4249                 :          0 :         return NULL;
    4250                 :            :     }
    4251                 :            : 
    4252                 :            :     /* Initialize this type (we'll assume the metatype is initialized) */
    4253         [ -  + ]:     325043 :     if (!_PyType_IsReady(type)) {
    4254         [ #  # ]:          0 :         if (PyType_Ready(type) < 0)
    4255                 :          0 :             return NULL;
    4256                 :            :     }
    4257                 :            : 
    4258                 :            :     /* No readable descriptor found yet */
    4259                 :     325043 :     meta_get = NULL;
    4260                 :            : 
    4261                 :            :     /* Look for the attribute in the metatype */
    4262                 :     325043 :     meta_attribute = _PyType_Lookup(metatype, name);
    4263                 :            : 
    4264         [ +  + ]:     325043 :     if (meta_attribute != NULL) {
    4265                 :     156835 :         Py_INCREF(meta_attribute);
    4266                 :     156835 :         meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
    4267                 :            : 
    4268   [ +  +  +  + ]:     156835 :         if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
    4269                 :            :             /* Data descriptors implement tp_descr_set to intercept
    4270                 :            :              * writes. Assume the attribute is not overridden in
    4271                 :            :              * type's tp_dict (and bases): call the descriptor now.
    4272                 :            :              */
    4273                 :      10119 :             res = meta_get(meta_attribute, (PyObject *)type,
    4274                 :            :                            (PyObject *)metatype);
    4275                 :      10119 :             Py_DECREF(meta_attribute);
    4276                 :      10119 :             return res;
    4277                 :            :         }
    4278                 :            :     }
    4279                 :            : 
    4280                 :            :     /* No data descriptor found on metatype. Look in tp_dict of this
    4281                 :            :      * type and its bases */
    4282                 :     314924 :     attribute = _PyType_Lookup(type, name);
    4283         [ +  + ]:     314924 :     if (attribute != NULL) {
    4284                 :            :         /* Implement descriptor functionality, if any */
    4285                 :     312884 :         Py_INCREF(attribute);
    4286                 :     312884 :         descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
    4287                 :            : 
    4288                 :     312884 :         Py_XDECREF(meta_attribute);
    4289                 :            : 
    4290         [ +  + ]:     312884 :         if (local_get != NULL) {
    4291                 :            :             /* NULL 2nd argument indicates the descriptor was
    4292                 :            :              * found on the target object itself (or a base)  */
    4293                 :     154920 :             res = local_get(attribute, (PyObject *)NULL,
    4294                 :            :                             (PyObject *)type);
    4295                 :     154920 :             Py_DECREF(attribute);
    4296                 :     154920 :             return res;
    4297                 :            :         }
    4298                 :            : 
    4299                 :     157964 :         return attribute;
    4300                 :            :     }
    4301                 :            : 
    4302                 :            :     /* No attribute found in local __dict__ (or bases): use the
    4303                 :            :      * descriptor from the metatype, if any */
    4304         [ +  + ]:       2040 :     if (meta_get != NULL) {
    4305                 :            :         PyObject *res;
    4306                 :       1641 :         res = meta_get(meta_attribute, (PyObject *)type,
    4307                 :            :                        (PyObject *)metatype);
    4308                 :       1641 :         Py_DECREF(meta_attribute);
    4309                 :       1641 :         return res;
    4310                 :            :     }
    4311                 :            : 
    4312                 :            :     /* If an ordinary attribute was found on the metatype, return it now */
    4313         [ -  + ]:        399 :     if (meta_attribute != NULL) {
    4314                 :          0 :         return meta_attribute;
    4315                 :            :     }
    4316                 :            : 
    4317                 :            :     /* Give up */
    4318         [ +  + ]:        399 :     if (suppress_missing_attribute == NULL) {
    4319                 :         28 :         PyErr_Format(PyExc_AttributeError,
    4320                 :            :                         "type object '%.50s' has no attribute '%U'",
    4321                 :            :                         type->tp_name, name);
    4322                 :            :     } else {
    4323                 :            :         // signal the caller we have not set an PyExc_AttributeError and gave up
    4324                 :        371 :         *suppress_missing_attribute = 1;
    4325                 :            :     }
    4326                 :        399 :     return NULL;
    4327                 :            : }
    4328                 :            : 
    4329                 :            : /* This is similar to PyObject_GenericGetAttr(),
    4330                 :            :    but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
    4331                 :            : PyObject *
    4332                 :     315549 : _Py_type_getattro(PyTypeObject *type, PyObject *name)
    4333                 :            : {
    4334                 :     315549 :     return _Py_type_getattro_impl(type, name, NULL);
    4335                 :            : }
    4336                 :            : 
    4337                 :            : static int
    4338                 :       4864 : type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
    4339                 :            : {
    4340                 :            :     int res;
    4341         [ -  + ]:       4864 :     if (type->tp_flags & Py_TPFLAGS_IMMUTABLETYPE) {
    4342                 :          0 :         PyErr_Format(
    4343                 :            :             PyExc_TypeError,
    4344                 :            :             "cannot set %R attribute of immutable type '%s'",
    4345                 :            :             name, type->tp_name);
    4346                 :          0 :         return -1;
    4347                 :            :     }
    4348         [ +  - ]:       4864 :     if (PyUnicode_Check(name)) {
    4349         [ +  - ]:       4864 :         if (PyUnicode_CheckExact(name)) {
    4350         [ -  + ]:       4864 :             if (PyUnicode_READY(name) == -1)
    4351                 :          0 :                 return -1;
    4352                 :       4864 :             Py_INCREF(name);
    4353                 :            :         }
    4354                 :            :         else {
    4355                 :          0 :             name = _PyUnicode_Copy(name);
    4356         [ #  # ]:          0 :             if (name == NULL)
    4357                 :          0 :                 return -1;
    4358                 :            :         }
    4359                 :            :         /* bpo-40521: Interned strings are shared by all subinterpreters */
    4360         [ -  + ]:       4864 :         if (!PyUnicode_CHECK_INTERNED(name)) {
    4361                 :          0 :             PyUnicode_InternInPlace(&name);
    4362         [ #  # ]:          0 :             if (!PyUnicode_CHECK_INTERNED(name)) {
    4363                 :          0 :                 PyErr_SetString(PyExc_MemoryError,
    4364                 :            :                                 "Out of memory interning an attribute name");
    4365                 :          0 :                 Py_DECREF(name);
    4366                 :          0 :                 return -1;
    4367                 :            :             }
    4368                 :            :         }
    4369                 :            :     }
    4370                 :            :     else {
    4371                 :            :         /* Will fail in _PyObject_GenericSetAttrWithDict. */
    4372                 :          0 :         Py_INCREF(name);
    4373                 :            :     }
    4374                 :       4864 :     res = _PyObject_GenericSetAttrWithDict((PyObject *)type, name, value, NULL);
    4375         [ +  - ]:       4864 :     if (res == 0) {
    4376                 :            :         /* Clear the VALID_VERSION flag of 'type' and all its
    4377                 :            :            subclasses.  This could possibly be unified with the
    4378                 :            :            update_subclasses() recursion in update_slot(), but carefully:
    4379                 :            :            they each have their own conditions on which to stop
    4380                 :            :            recursing into subclasses. */
    4381                 :       4864 :         PyType_Modified(type);
    4382                 :            : 
    4383         [ +  + ]:       4864 :         if (is_dunder_name(name)) {
    4384                 :       1424 :             res = update_slot(type, name);
    4385                 :            :         }
    4386                 :            :         assert(_PyType_CheckConsistency(type));
    4387                 :            :     }
    4388                 :       4864 :     Py_DECREF(name);
    4389                 :       4864 :     return res;
    4390                 :            : }
    4391                 :            : 
    4392                 :            : extern void
    4393                 :            : _PyDictKeys_DecRef(PyDictKeysObject *keys);
    4394                 :            : 
    4395                 :            : 
    4396                 :            : static void
    4397                 :      11884 : type_dealloc_common(PyTypeObject *type)
    4398                 :            : {
    4399         [ +  - ]:      11884 :     if (type->tp_bases != NULL) {
    4400                 :      11884 :         PyObject *exc = PyErr_GetRaisedException();
    4401                 :      11884 :         remove_all_subclasses(type, type->tp_bases);
    4402                 :      11884 :         PyErr_SetRaisedException(exc);
    4403                 :            :     }
    4404                 :      11884 : }
    4405                 :            : 
    4406                 :            : 
    4407                 :            : static void clear_subclasses(PyTypeObject *self);
    4408                 :            : 
    4409                 :            : static void
    4410                 :       4775 : clear_static_tp_subclasses(PyTypeObject *type)
    4411                 :            : {
    4412                 :       4775 :     PyObject *subclasses = lookup_subclasses(type);
    4413         [ +  + ]:       4775 :     if (subclasses == NULL) {
    4414                 :       4746 :         return;
    4415                 :            :     }
    4416                 :            : 
    4417                 :            :     /* Normally it would be a problem to finalize the type if its
    4418                 :            :        tp_subclasses wasn't cleared first.  However, this is only
    4419                 :            :        ever called at the end of runtime finalization, so we can be
    4420                 :            :        more liberal in cleaning up.  If the given type still has
    4421                 :            :        subtypes at this point then some extension module did not
    4422                 :            :        correctly finalize its objects.
    4423                 :            : 
    4424                 :            :        We can safely obliterate such subtypes since the extension
    4425                 :            :        module and its objects won't be used again, except maybe if
    4426                 :            :        the runtime were re-initialized.  In that case the sticky
    4427                 :            :        situation would only happen if the module were re-imported
    4428                 :            :        then and only if the subtype were stored in a global and only
    4429                 :            :        if that global were not overwritten during import.  We'd be
    4430                 :            :        fine since the extension is otherwise unsafe and unsupported
    4431                 :            :        in that situation, and likely problematic already.
    4432                 :            : 
    4433                 :            :        In any case, this situation means at least some memory is
    4434                 :            :        going to leak.  This mostly only affects embedding scenarios.
    4435                 :            :      */
    4436                 :            : 
    4437                 :            :     // For now we just do a sanity check and then clear tp_subclasses.
    4438                 :         29 :     Py_ssize_t i = 0;
    4439                 :            :     PyObject *key, *ref;  // borrowed ref
    4440         [ +  + ]:        194 :     while (PyDict_Next(subclasses, &i, &key, &ref)) {
    4441                 :        165 :         PyTypeObject *subclass = subclass_from_ref(ref);  // borrowed
    4442         [ -  + ]:        165 :         if (subclass == NULL) {
    4443                 :          0 :             continue;
    4444                 :            :         }
    4445                 :            :         // All static builtin subtypes should have been finalized already.
    4446                 :            :         assert(!(subclass->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN));
    4447                 :            :     }
    4448                 :            : 
    4449                 :         29 :     clear_subclasses(type);
    4450                 :            : }
    4451                 :            : 
    4452                 :            : void
    4453                 :       4775 : _PyStaticType_Dealloc(PyTypeObject *type)
    4454                 :            : {
    4455                 :            :     assert(!(type->tp_flags & Py_TPFLAGS_HEAPTYPE));
    4456                 :            : 
    4457                 :       4775 :     type_dealloc_common(type);
    4458                 :            : 
    4459         [ +  - ]:       4775 :     Py_CLEAR(type->tp_dict);
    4460         [ +  - ]:       4775 :     Py_CLEAR(type->tp_bases);
    4461         [ +  - ]:       4775 :     Py_CLEAR(type->tp_mro);
    4462         [ -  + ]:       4775 :     Py_CLEAR(type->tp_cache);
    4463                 :       4775 :     clear_static_tp_subclasses(type);
    4464                 :            : 
    4465                 :            :     // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
    4466         [ -  + ]:       4775 :     if (Py_REFCNT(type) == 0) {
    4467                 :          0 :         PyObject_ClearWeakRefs((PyObject *)type);
    4468                 :            :     }
    4469                 :            : 
    4470                 :       4775 :     type->tp_flags &= ~Py_TPFLAGS_READY;
    4471                 :       4775 :     type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
    4472                 :       4775 :     type->tp_version_tag = 0;
    4473                 :            : 
    4474         [ +  + ]:       4775 :     if (type->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
    4475                 :       4625 :         _PyStaticType_ClearWeakRefs(type);
    4476                 :       4625 :         static_builtin_state_clear(type);
    4477                 :            :         /* We leave _Py_TPFLAGS_STATIC_BUILTIN set on tp_flags. */
    4478                 :            :     }
    4479                 :       4775 : }
    4480                 :            : 
    4481                 :            : 
    4482                 :            : static void
    4483                 :       7109 : type_dealloc(PyTypeObject *type)
    4484                 :            : {
    4485                 :            :     // Assert this is a heap-allocated type object
    4486                 :            :     _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
    4487                 :            : 
    4488                 :       7109 :     _PyObject_GC_UNTRACK(type);
    4489                 :            : 
    4490                 :       7109 :     type_dealloc_common(type);
    4491                 :            : 
    4492                 :            :     // PyObject_ClearWeakRefs() raises an exception if Py_REFCNT() != 0
    4493                 :            :     assert(Py_REFCNT(type) == 0);
    4494                 :       7109 :     PyObject_ClearWeakRefs((PyObject *)type);
    4495                 :            : 
    4496                 :       7109 :     Py_XDECREF(type->tp_base);
    4497                 :       7109 :     Py_XDECREF(type->tp_dict);
    4498                 :       7109 :     Py_XDECREF(type->tp_bases);
    4499                 :       7109 :     Py_XDECREF(type->tp_mro);
    4500                 :       7109 :     Py_XDECREF(type->tp_cache);
    4501                 :       7109 :     clear_subclasses(type);
    4502                 :            : 
    4503                 :            :     /* A type's tp_doc is heap allocated, unlike the tp_doc slots
    4504                 :            :      * of most other objects.  It's okay to cast it to char *.
    4505                 :            :      */
    4506                 :       7109 :     PyObject_Free((char *)type->tp_doc);
    4507                 :            : 
    4508                 :       7109 :     PyHeapTypeObject *et = (PyHeapTypeObject *)type;
    4509                 :       7109 :     Py_XDECREF(et->ht_name);
    4510                 :       7109 :     Py_XDECREF(et->ht_qualname);
    4511                 :       7109 :     Py_XDECREF(et->ht_slots);
    4512         [ +  + ]:       7109 :     if (et->ht_cached_keys) {
    4513                 :       1886 :         _PyDictKeys_DecRef(et->ht_cached_keys);
    4514                 :            :     }
    4515                 :       7109 :     Py_XDECREF(et->ht_module);
    4516                 :       7109 :     PyMem_Free(et->_ht_tpname);
    4517                 :       7109 :     Py_TYPE(type)->tp_free((PyObject *)type);
    4518                 :       7109 : }
    4519                 :            : 
    4520                 :            : 
    4521                 :            : static PyObject *
    4522                 :      34981 : lookup_subclasses(PyTypeObject *self)
    4523                 :            : {
    4524         [ +  + ]:      34981 :     if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
    4525                 :      20290 :         static_builtin_state *state = _PyStaticType_GetState(self);
    4526                 :            :         assert(state != NULL);
    4527                 :      20290 :         return state->tp_subclasses;
    4528                 :            :     }
    4529                 :      14691 :     return (PyObject *)self->tp_subclasses;
    4530                 :            : }
    4531                 :            : 
    4532                 :            : int
    4533                 :        200 : _PyType_HasSubclasses(PyTypeObject *self)
    4534                 :            : {
    4535   [ +  -  -  + ]:        400 :     if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN &&
    4536                 :        200 :             _PyStaticType_GetState(self) == NULL) {
    4537                 :          0 :         return 0;
    4538                 :            :     }
    4539         [ +  - ]:        200 :     if (lookup_subclasses(self) == NULL) {
    4540                 :        200 :         return 0;
    4541                 :            :     }
    4542                 :          0 :     return 1;
    4543                 :            : }
    4544                 :            : 
    4545                 :            : PyObject*
    4546                 :        614 : _PyType_GetSubclasses(PyTypeObject *self)
    4547                 :            : {
    4548                 :        614 :     PyObject *list = PyList_New(0);
    4549         [ -  + ]:        614 :     if (list == NULL) {
    4550                 :          0 :         return NULL;
    4551                 :            :     }
    4552                 :            : 
    4553                 :        614 :     PyObject *subclasses = lookup_subclasses(self);  // borrowed ref
    4554         [ +  + ]:        614 :     if (subclasses == NULL) {
    4555                 :        599 :         return list;
    4556                 :            :     }
    4557                 :            :     assert(PyDict_CheckExact(subclasses));
    4558                 :            :     // The loop cannot modify tp_subclasses, there is no need
    4559                 :            :     // to hold a strong reference (use a borrowed reference).
    4560                 :            : 
    4561                 :         15 :     Py_ssize_t i = 0;
    4562                 :            :     PyObject *ref;  // borrowed ref
    4563         [ +  + ]:         34 :     while (PyDict_Next(subclasses, &i, NULL, &ref)) {
    4564                 :         19 :         PyTypeObject *subclass = subclass_from_ref(ref);  // borrowed
    4565         [ -  + ]:         19 :         if (subclass == NULL) {
    4566                 :          0 :             continue;
    4567                 :            :         }
    4568                 :            : 
    4569         [ -  + ]:         19 :         if (PyList_Append(list, _PyObject_CAST(subclass)) < 0) {
    4570                 :          0 :             Py_DECREF(list);
    4571                 :          0 :             return NULL;
    4572                 :            :         }
    4573                 :            :     }
    4574                 :         15 :     return list;
    4575                 :            : }
    4576                 :            : 
    4577                 :            : 
    4578                 :            : /*[clinic input]
    4579                 :            : type.__subclasses__
    4580                 :            : 
    4581                 :            : Return a list of immediate subclasses.
    4582                 :            : [clinic start generated code]*/
    4583                 :            : 
    4584                 :            : static PyObject *
    4585                 :        614 : type___subclasses___impl(PyTypeObject *self)
    4586                 :            : /*[clinic end generated code: output=eb5eb54485942819 input=5af66132436f9a7b]*/
    4587                 :            : {
    4588                 :        614 :     return _PyType_GetSubclasses(self);
    4589                 :            : }
    4590                 :            : 
    4591                 :            : static PyObject *
    4592                 :       3102 : type_prepare(PyObject *self, PyObject *const *args, Py_ssize_t nargs,
    4593                 :            :              PyObject *kwnames)
    4594                 :            : {
    4595                 :       3102 :     return PyDict_New();
    4596                 :            : }
    4597                 :            : 
    4598                 :            : 
    4599                 :            : /*
    4600                 :            :    Merge the __dict__ of aclass into dict, and recursively also all
    4601                 :            :    the __dict__s of aclass's base classes.  The order of merging isn't
    4602                 :            :    defined, as it's expected that only the final set of dict keys is
    4603                 :            :    interesting.
    4604                 :            :    Return 0 on success, -1 on error.
    4605                 :            : */
    4606                 :            : 
    4607                 :            : static int
    4608                 :          6 : merge_class_dict(PyObject *dict, PyObject *aclass)
    4609                 :            : {
    4610                 :            :     PyObject *classdict;
    4611                 :            :     PyObject *bases;
    4612                 :            : 
    4613                 :            :     assert(PyDict_Check(dict));
    4614                 :            :     assert(aclass);
    4615                 :            : 
    4616                 :            :     /* Merge in the type's dict (if any). */
    4617         [ -  + ]:          6 :     if (_PyObject_LookupAttr(aclass, &_Py_ID(__dict__), &classdict) < 0) {
    4618                 :          0 :         return -1;
    4619                 :            :     }
    4620         [ +  - ]:          6 :     if (classdict != NULL) {
    4621                 :          6 :         int status = PyDict_Update(dict, classdict);
    4622                 :          6 :         Py_DECREF(classdict);
    4623         [ -  + ]:          6 :         if (status < 0)
    4624                 :          0 :             return -1;
    4625                 :            :     }
    4626                 :            : 
    4627                 :            :     /* Recursively merge in the base types' (if any) dicts. */
    4628         [ -  + ]:          6 :     if (_PyObject_LookupAttr(aclass, &_Py_ID(__bases__), &bases) < 0) {
    4629                 :          0 :         return -1;
    4630                 :            :     }
    4631         [ +  - ]:          6 :     if (bases != NULL) {
    4632                 :            :         /* We have no guarantee that bases is a real tuple */
    4633                 :            :         Py_ssize_t i, n;
    4634                 :          6 :         n = PySequence_Size(bases); /* This better be right */
    4635         [ -  + ]:          6 :         if (n < 0) {
    4636                 :          0 :             Py_DECREF(bases);
    4637                 :          0 :             return -1;
    4638                 :            :         }
    4639                 :            :         else {
    4640         [ +  + ]:         10 :             for (i = 0; i < n; i++) {
    4641                 :            :                 int status;
    4642                 :          4 :                 PyObject *base = PySequence_GetItem(bases, i);
    4643         [ -  + ]:          4 :                 if (base == NULL) {
    4644                 :          0 :                     Py_DECREF(bases);
    4645                 :          0 :                     return -1;
    4646                 :            :                 }
    4647                 :          4 :                 status = merge_class_dict(dict, base);
    4648                 :          4 :                 Py_DECREF(base);
    4649         [ -  + ]:          4 :                 if (status < 0) {
    4650                 :          0 :                     Py_DECREF(bases);
    4651                 :          0 :                     return -1;
    4652                 :            :                 }
    4653                 :            :             }
    4654                 :            :         }
    4655                 :          6 :         Py_DECREF(bases);
    4656                 :            :     }
    4657                 :          6 :     return 0;
    4658                 :            : }
    4659                 :            : 
    4660                 :            : /* __dir__ for type objects: returns __dict__ and __bases__.
    4661                 :            :    We deliberately don't suck up its __class__, as methods belonging to the
    4662                 :            :    metaclass would probably be more confusing than helpful.
    4663                 :            : */
    4664                 :            : /*[clinic input]
    4665                 :            : type.__dir__
    4666                 :            : 
    4667                 :            : Specialized __dir__ implementation for types.
    4668                 :            : [clinic start generated code]*/
    4669                 :            : 
    4670                 :            : static PyObject *
    4671                 :          2 : type___dir___impl(PyTypeObject *self)
    4672                 :            : /*[clinic end generated code: output=69d02fe92c0f15fa input=7733befbec645968]*/
    4673                 :            : {
    4674                 :          2 :     PyObject *result = NULL;
    4675                 :          2 :     PyObject *dict = PyDict_New();
    4676                 :            : 
    4677   [ +  -  +  - ]:          2 :     if (dict != NULL && merge_class_dict(dict, (PyObject *)self) == 0)
    4678                 :          2 :         result = PyDict_Keys(dict);
    4679                 :            : 
    4680                 :          2 :     Py_XDECREF(dict);
    4681                 :          2 :     return result;
    4682                 :            : }
    4683                 :            : 
    4684                 :            : /*[clinic input]
    4685                 :            : type.__sizeof__
    4686                 :            : 
    4687                 :            : Return memory consumption of the type object.
    4688                 :            : [clinic start generated code]*/
    4689                 :            : 
    4690                 :            : static PyObject *
    4691                 :          0 : type___sizeof___impl(PyTypeObject *self)
    4692                 :            : /*[clinic end generated code: output=766f4f16cd3b1854 input=99398f24b9cf45d6]*/
    4693                 :            : {
    4694                 :            :     size_t size;
    4695         [ #  # ]:          0 :     if (self->tp_flags & Py_TPFLAGS_HEAPTYPE) {
    4696                 :          0 :         PyHeapTypeObject* et = (PyHeapTypeObject*)self;
    4697                 :          0 :         size = sizeof(PyHeapTypeObject);
    4698         [ #  # ]:          0 :         if (et->ht_cached_keys)
    4699                 :          0 :             size += _PyDict_KeysSize(et->ht_cached_keys);
    4700                 :            :     }
    4701                 :            :     else {
    4702                 :          0 :         size = sizeof(PyTypeObject);
    4703                 :            :     }
    4704                 :          0 :     return PyLong_FromSize_t(size);
    4705                 :            : }
    4706                 :            : 
    4707                 :            : static PyMethodDef type_methods[] = {
    4708                 :            :     TYPE_MRO_METHODDEF
    4709                 :            :     TYPE___SUBCLASSES___METHODDEF
    4710                 :            :     {"__prepare__", _PyCFunction_CAST(type_prepare),
    4711                 :            :      METH_FASTCALL | METH_KEYWORDS | METH_CLASS,
    4712                 :            :      PyDoc_STR("__prepare__() -> dict\n"
    4713                 :            :                "used to create the namespace for the class statement")},
    4714                 :            :     TYPE___INSTANCECHECK___METHODDEF
    4715                 :            :     TYPE___SUBCLASSCHECK___METHODDEF
    4716                 :            :     TYPE___DIR___METHODDEF
    4717                 :            :     TYPE___SIZEOF___METHODDEF
    4718                 :            :     {0}
    4719                 :            : };
    4720                 :            : 
    4721                 :            : PyDoc_STRVAR(type_doc,
    4722                 :            : "type(object) -> the object's type\n"
    4723                 :            : "type(name, bases, dict, **kwds) -> a new type");
    4724                 :            : 
    4725                 :            : static int
    4726                 :      83318 : type_traverse(PyTypeObject *type, visitproc visit, void *arg)
    4727                 :            : {
    4728                 :            :     /* Because of type_is_gc(), the collector only calls this
    4729                 :            :        for heaptypes. */
    4730         [ -  + ]:      83318 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    4731                 :            :         char msg[200];
    4732                 :          0 :         sprintf(msg, "type_traverse() called on non-heap type '%.100s'",
    4733                 :            :                 type->tp_name);
    4734                 :          0 :         _PyObject_ASSERT_FAILED_MSG((PyObject *)type, msg);
    4735                 :            :     }
    4736                 :            : 
    4737   [ +  -  -  + ]:      83318 :     Py_VISIT(type->tp_dict);
    4738   [ -  +  -  - ]:      83318 :     Py_VISIT(type->tp_cache);
    4739   [ +  -  -  + ]:      83318 :     Py_VISIT(type->tp_mro);
    4740   [ +  -  -  + ]:      83318 :     Py_VISIT(type->tp_bases);
    4741   [ +  -  -  + ]:      83318 :     Py_VISIT(type->tp_base);
    4742   [ +  +  -  + ]:      83318 :     Py_VISIT(((PyHeapTypeObject *)type)->ht_module);
    4743                 :            : 
    4744                 :            :     /* There's no need to visit others because they can't be involved
    4745                 :            :        in cycles:
    4746                 :            :        type->tp_subclasses is a list of weak references,
    4747                 :            :        ((PyHeapTypeObject *)type)->ht_slots is a tuple of strings,
    4748                 :            :        ((PyHeapTypeObject *)type)->ht_*name are strings.
    4749                 :            :        */
    4750                 :            : 
    4751                 :      83318 :     return 0;
    4752                 :            : }
    4753                 :            : 
    4754                 :            : static int
    4755                 :       7109 : type_clear(PyTypeObject *type)
    4756                 :            : {
    4757                 :            :     /* Because of type_is_gc(), the collector only calls this
    4758                 :            :        for heaptypes. */
    4759                 :            :     _PyObject_ASSERT((PyObject *)type, type->tp_flags & Py_TPFLAGS_HEAPTYPE);
    4760                 :            : 
    4761                 :            :     /* We need to invalidate the method cache carefully before clearing
    4762                 :            :        the dict, so that other objects caught in a reference cycle
    4763                 :            :        don't start calling destroyed methods.
    4764                 :            : 
    4765                 :            :        Otherwise, the we need to clear tp_mro, which is
    4766                 :            :        part of a hard cycle (its first element is the class itself) that
    4767                 :            :        won't be broken otherwise (it's a tuple and tuples don't have a
    4768                 :            :        tp_clear handler).
    4769                 :            :        We also need to clear ht_module, if present: the module usually holds a
    4770                 :            :        reference to its class. None of the other fields need to be
    4771                 :            : 
    4772                 :            :        cleared, and here's why:
    4773                 :            : 
    4774                 :            :        tp_cache:
    4775                 :            :            Not used; if it were, it would be a dict.
    4776                 :            : 
    4777                 :            :        tp_bases, tp_base:
    4778                 :            :            If these are involved in a cycle, there must be at least
    4779                 :            :            one other, mutable object in the cycle, e.g. a base
    4780                 :            :            class's dict; the cycle will be broken that way.
    4781                 :            : 
    4782                 :            :        tp_subclasses:
    4783                 :            :            A dict of weak references can't be part of a cycle; and
    4784                 :            :            dicts have their own tp_clear.
    4785                 :            : 
    4786                 :            :        slots (in PyHeapTypeObject):
    4787                 :            :            A tuple of strings can't be part of a cycle.
    4788                 :            :     */
    4789                 :            : 
    4790                 :       7109 :     PyType_Modified(type);
    4791         [ +  - ]:       7109 :     if (type->tp_dict) {
    4792                 :       7109 :         PyDict_Clear(type->tp_dict);
    4793                 :            :     }
    4794         [ +  + ]:       7109 :     Py_CLEAR(((PyHeapTypeObject *)type)->ht_module);
    4795                 :            : 
    4796         [ +  - ]:       7109 :     Py_CLEAR(type->tp_mro);
    4797                 :            : 
    4798                 :       7109 :     return 0;
    4799                 :            : }
    4800                 :            : 
    4801                 :            : static int
    4802                 :    2466293 : type_is_gc(PyTypeObject *type)
    4803                 :            : {
    4804                 :    2466293 :     return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
    4805                 :            : }
    4806                 :            : 
    4807                 :            : 
    4808                 :            : static PyNumberMethods type_as_number = {
    4809                 :            :         .nb_or = _Py_union_type_or, // Add __or__ function
    4810                 :            : };
    4811                 :            : 
    4812                 :            : PyTypeObject PyType_Type = {
    4813                 :            :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    4814                 :            :     "type",                                     /* tp_name */
    4815                 :            :     sizeof(PyHeapTypeObject),                   /* tp_basicsize */
    4816                 :            :     sizeof(PyMemberDef),                        /* tp_itemsize */
    4817                 :            :     (destructor)type_dealloc,                   /* tp_dealloc */
    4818                 :            :     offsetof(PyTypeObject, tp_vectorcall),      /* tp_vectorcall_offset */
    4819                 :            :     0,                                          /* tp_getattr */
    4820                 :            :     0,                                          /* tp_setattr */
    4821                 :            :     0,                                          /* tp_as_async */
    4822                 :            :     (reprfunc)type_repr,                        /* tp_repr */
    4823                 :            :     &type_as_number,                            /* tp_as_number */
    4824                 :            :     0,                                          /* tp_as_sequence */
    4825                 :            :     0,                                          /* tp_as_mapping */
    4826                 :            :     0,                                          /* tp_hash */
    4827                 :            :     (ternaryfunc)type_call,                     /* tp_call */
    4828                 :            :     0,                                          /* tp_str */
    4829                 :            :     (getattrofunc)_Py_type_getattro,            /* tp_getattro */
    4830                 :            :     (setattrofunc)type_setattro,                /* tp_setattro */
    4831                 :            :     0,                                          /* tp_as_buffer */
    4832                 :            :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
    4833                 :            :     Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS |
    4834                 :            :     Py_TPFLAGS_HAVE_VECTORCALL,                 /* tp_flags */
    4835                 :            :     type_doc,                                   /* tp_doc */
    4836                 :            :     (traverseproc)type_traverse,                /* tp_traverse */
    4837                 :            :     (inquiry)type_clear,                        /* tp_clear */
    4838                 :            :     0,                                          /* tp_richcompare */
    4839                 :            :     offsetof(PyTypeObject, tp_weaklist),        /* tp_weaklistoffset */
    4840                 :            :     0,                                          /* tp_iter */
    4841                 :            :     0,                                          /* tp_iternext */
    4842                 :            :     type_methods,                               /* tp_methods */
    4843                 :            :     type_members,                               /* tp_members */
    4844                 :            :     type_getsets,                               /* tp_getset */
    4845                 :            :     0,                                          /* tp_base */
    4846                 :            :     0,                                          /* tp_dict */
    4847                 :            :     0,                                          /* tp_descr_get */
    4848                 :            :     0,                                          /* tp_descr_set */
    4849                 :            :     offsetof(PyTypeObject, tp_dict),            /* tp_dictoffset */
    4850                 :            :     type_init,                                  /* tp_init */
    4851                 :            :     0,                                          /* tp_alloc */
    4852                 :            :     type_new,                                   /* tp_new */
    4853                 :            :     PyObject_GC_Del,                            /* tp_free */
    4854                 :            :     (inquiry)type_is_gc,                        /* tp_is_gc */
    4855                 :            :     .tp_vectorcall = type_vectorcall,
    4856                 :            : };
    4857                 :            : 
    4858                 :            : 
    4859                 :            : /* The base type of all types (eventually)... except itself. */
    4860                 :            : 
    4861                 :            : /* You may wonder why object.__new__() only complains about arguments
    4862                 :            :    when object.__init__() is not overridden, and vice versa.
    4863                 :            : 
    4864                 :            :    Consider the use cases:
    4865                 :            : 
    4866                 :            :    1. When neither is overridden, we want to hear complaints about
    4867                 :            :       excess (i.e., any) arguments, since their presence could
    4868                 :            :       indicate there's a bug.
    4869                 :            : 
    4870                 :            :    2. When defining an Immutable type, we are likely to override only
    4871                 :            :       __new__(), since __init__() is called too late to initialize an
    4872                 :            :       Immutable object.  Since __new__() defines the signature for the
    4873                 :            :       type, it would be a pain to have to override __init__() just to
    4874                 :            :       stop it from complaining about excess arguments.
    4875                 :            : 
    4876                 :            :    3. When defining a Mutable type, we are likely to override only
    4877                 :            :       __init__().  So here the converse reasoning applies: we don't
    4878                 :            :       want to have to override __new__() just to stop it from
    4879                 :            :       complaining.
    4880                 :            : 
    4881                 :            :    4. When __init__() is overridden, and the subclass __init__() calls
    4882                 :            :       object.__init__(), the latter should complain about excess
    4883                 :            :       arguments; ditto for __new__().
    4884                 :            : 
    4885                 :            :    Use cases 2 and 3 make it unattractive to unconditionally check for
    4886                 :            :    excess arguments.  The best solution that addresses all four use
    4887                 :            :    cases is as follows: __init__() complains about excess arguments
    4888                 :            :    unless __new__() is overridden and __init__() is not overridden
    4889                 :            :    (IOW, if __init__() is overridden or __new__() is not overridden);
    4890                 :            :    symmetrically, __new__() complains about excess arguments unless
    4891                 :            :    __init__() is overridden and __new__() is not overridden
    4892                 :            :    (IOW, if __new__() is overridden or __init__() is not overridden).
    4893                 :            : 
    4894                 :            :    However, for backwards compatibility, this breaks too much code.
    4895                 :            :    Therefore, in 2.6, we'll *warn* about excess arguments when both
    4896                 :            :    methods are overridden; for all other cases we'll use the above
    4897                 :            :    rules.
    4898                 :            : 
    4899                 :            : */
    4900                 :            : 
    4901                 :            : /* Forward */
    4902                 :            : static PyObject *
    4903                 :            : object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
    4904                 :            : 
    4905                 :            : static int
    4906                 :     991681 : excess_args(PyObject *args, PyObject *kwds)
    4907                 :            : {
    4908   [ +  +  +  + ]:     991816 :     return PyTuple_GET_SIZE(args) ||
    4909   [ +  -  +  + ]:        135 :         (kwds && PyDict_Check(kwds) && PyDict_GET_SIZE(kwds));
    4910                 :            : }
    4911                 :            : 
    4912                 :            : static int
    4913                 :     718484 : object_init(PyObject *self, PyObject *args, PyObject *kwds)
    4914                 :            : {
    4915                 :     718484 :     PyTypeObject *type = Py_TYPE(self);
    4916         [ +  + ]:     718484 :     if (excess_args(args, kwds)) {
    4917         [ -  + ]:     712686 :         if (type->tp_init != object_init) {
    4918                 :          0 :             PyErr_SetString(PyExc_TypeError,
    4919                 :            :                             "object.__init__() takes exactly one argument (the instance to initialize)");
    4920                 :          0 :             return -1;
    4921                 :            :         }
    4922         [ -  + ]:     712686 :         if (type->tp_new == object_new) {
    4923                 :          0 :             PyErr_Format(PyExc_TypeError,
    4924                 :            :                          "%.200s.__init__() takes exactly one argument (the instance to initialize)",
    4925                 :            :                          type->tp_name);
    4926                 :          0 :             return -1;
    4927                 :            :         }
    4928                 :            :     }
    4929                 :     718484 :     return 0;
    4930                 :            : }
    4931                 :            : 
    4932                 :            : static PyObject *
    4933                 :     273197 : object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    4934                 :            : {
    4935         [ +  + ]:     273197 :     if (excess_args(args, kwds)) {
    4936         [ -  + ]:     138307 :         if (type->tp_new != object_new) {
    4937                 :          0 :             PyErr_SetString(PyExc_TypeError,
    4938                 :            :                             "object.__new__() takes exactly one argument (the type to instantiate)");
    4939                 :          0 :             return NULL;
    4940                 :            :         }
    4941         [ +  + ]:     138307 :         if (type->tp_init == object_init) {
    4942                 :         30 :             PyErr_Format(PyExc_TypeError, "%.200s() takes no arguments",
    4943                 :            :                          type->tp_name);
    4944                 :         30 :             return NULL;
    4945                 :            :         }
    4946                 :            :     }
    4947                 :            : 
    4948         [ -  + ]:     273167 :     if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
    4949                 :            :         PyObject *abstract_methods;
    4950                 :            :         PyObject *sorted_methods;
    4951                 :            :         PyObject *joined;
    4952                 :            :         PyObject* comma_w_quotes_sep;
    4953                 :            :         Py_ssize_t method_count;
    4954                 :            : 
    4955                 :            :         /* Compute "', '".join(sorted(type.__abstractmethods__))
    4956                 :            :            into joined. */
    4957                 :          0 :         abstract_methods = type_abstractmethods(type, NULL);
    4958         [ #  # ]:          0 :         if (abstract_methods == NULL)
    4959                 :          0 :             return NULL;
    4960                 :          0 :         sorted_methods = PySequence_List(abstract_methods);
    4961                 :          0 :         Py_DECREF(abstract_methods);
    4962         [ #  # ]:          0 :         if (sorted_methods == NULL)
    4963                 :          0 :             return NULL;
    4964         [ #  # ]:          0 :         if (PyList_Sort(sorted_methods)) {
    4965                 :          0 :             Py_DECREF(sorted_methods);
    4966                 :          0 :             return NULL;
    4967                 :            :         }
    4968                 :          0 :         comma_w_quotes_sep = PyUnicode_FromString("', '");
    4969                 :          0 :         joined = PyUnicode_Join(comma_w_quotes_sep, sorted_methods);
    4970                 :          0 :         method_count = PyObject_Length(sorted_methods);
    4971                 :          0 :         Py_DECREF(sorted_methods);
    4972         [ #  # ]:          0 :         if (joined == NULL)  {
    4973                 :          0 :             Py_DECREF(comma_w_quotes_sep);
    4974                 :          0 :             return NULL;
    4975                 :            :         }
    4976         [ #  # ]:          0 :         if (method_count == -1) {
    4977                 :          0 :             Py_DECREF(comma_w_quotes_sep);
    4978                 :          0 :             Py_DECREF(joined);
    4979                 :          0 :             return NULL;
    4980                 :            :         }
    4981                 :            : 
    4982         [ #  # ]:          0 :         PyErr_Format(PyExc_TypeError,
    4983                 :            :                      "Can't instantiate abstract class %s "
    4984                 :            :                      "without an implementation for abstract method%s '%U'",
    4985                 :            :                      type->tp_name,
    4986                 :            :                      method_count > 1 ? "s" : "",
    4987                 :            :                      joined);
    4988                 :          0 :         Py_DECREF(joined);
    4989                 :          0 :         Py_DECREF(comma_w_quotes_sep);
    4990                 :          0 :         return NULL;
    4991                 :            :     }
    4992                 :     273167 :     PyObject *obj = type->tp_alloc(type, 0);
    4993         [ -  + ]:     273167 :     if (obj == NULL) {
    4994                 :          0 :         return NULL;
    4995                 :            :     }
    4996         [ -  + ]:     273167 :     if (_PyObject_InitializeDict(obj)) {
    4997                 :          0 :         Py_DECREF(obj);
    4998                 :          0 :         return NULL;
    4999                 :            :     }
    5000                 :     273167 :     return obj;
    5001                 :            : }
    5002                 :            : 
    5003                 :            : static void
    5004                 :    7808977 : object_dealloc(PyObject *self)
    5005                 :            : {
    5006                 :    7808977 :     Py_TYPE(self)->tp_free(self);
    5007                 :    7808977 : }
    5008                 :            : 
    5009                 :            : static PyObject *
    5010                 :          0 : object_repr(PyObject *self)
    5011                 :            : {
    5012                 :            :     PyTypeObject *type;
    5013                 :            :     PyObject *mod, *name, *rtn;
    5014                 :            : 
    5015                 :          0 :     type = Py_TYPE(self);
    5016                 :          0 :     mod = type_module(type, NULL);
    5017         [ #  # ]:          0 :     if (mod == NULL)
    5018                 :          0 :         PyErr_Clear();
    5019         [ #  # ]:          0 :     else if (!PyUnicode_Check(mod)) {
    5020                 :          0 :         Py_SETREF(mod, NULL);
    5021                 :            :     }
    5022                 :          0 :     name = type_qualname(type, NULL);
    5023         [ #  # ]:          0 :     if (name == NULL) {
    5024                 :          0 :         Py_XDECREF(mod);
    5025                 :          0 :         return NULL;
    5026                 :            :     }
    5027   [ #  #  #  # ]:          0 :     if (mod != NULL && !_PyUnicode_Equal(mod, &_Py_ID(builtins)))
    5028                 :          0 :         rtn = PyUnicode_FromFormat("<%U.%U object at %p>", mod, name, self);
    5029                 :            :     else
    5030                 :          0 :         rtn = PyUnicode_FromFormat("<%s object at %p>",
    5031                 :            :                                   type->tp_name, self);
    5032                 :          0 :     Py_XDECREF(mod);
    5033                 :          0 :     Py_DECREF(name);
    5034                 :          0 :     return rtn;
    5035                 :            : }
    5036                 :            : 
    5037                 :            : static PyObject *
    5038                 :     298047 : object_str(PyObject *self)
    5039                 :            : {
    5040                 :            :     unaryfunc f;
    5041                 :            : 
    5042                 :     298047 :     f = Py_TYPE(self)->tp_repr;
    5043         [ -  + ]:     298047 :     if (f == NULL)
    5044                 :          0 :         f = object_repr;
    5045                 :     298047 :     return f(self);
    5046                 :            : }
    5047                 :            : 
    5048                 :            : static PyObject *
    5049                 :      91891 : object_richcompare(PyObject *self, PyObject *other, int op)
    5050                 :            : {
    5051                 :            :     PyObject *res;
    5052                 :            : 
    5053      [ +  +  - ]:      91891 :     switch (op) {
    5054                 :            : 
    5055                 :      91881 :     case Py_EQ:
    5056                 :            :         /* Return NotImplemented instead of False, so if two
    5057                 :            :            objects are compared, both get a chance at the
    5058                 :            :            comparison.  See issue #1393. */
    5059         [ +  + ]:      91881 :         res = Py_NewRef((self == other) ? Py_True : Py_NotImplemented);
    5060                 :      91881 :         break;
    5061                 :            : 
    5062                 :         10 :     case Py_NE:
    5063                 :            :         /* By default, __ne__() delegates to __eq__() and inverts the result,
    5064                 :            :            unless the latter returns NotImplemented. */
    5065         [ -  + ]:         10 :         if (Py_TYPE(self)->tp_richcompare == NULL) {
    5066                 :          0 :             res = Py_NewRef(Py_NotImplemented);
    5067                 :          0 :             break;
    5068                 :            :         }
    5069                 :         10 :         res = (*Py_TYPE(self)->tp_richcompare)(self, other, Py_EQ);
    5070   [ +  -  +  - ]:         10 :         if (res != NULL && res != Py_NotImplemented) {
    5071                 :         10 :             int ok = PyObject_IsTrue(res);
    5072                 :         10 :             Py_DECREF(res);
    5073         [ -  + ]:         10 :             if (ok < 0)
    5074                 :          0 :                 res = NULL;
    5075                 :            :             else {
    5076         [ +  - ]:         10 :                 if (ok)
    5077                 :         10 :                     res = Py_NewRef(Py_False);
    5078                 :            :                 else
    5079                 :          0 :                     res = Py_NewRef(Py_True);
    5080                 :            :             }
    5081                 :            :         }
    5082                 :         10 :         break;
    5083                 :            : 
    5084                 :          0 :     default:
    5085                 :          0 :         res = Py_NewRef(Py_NotImplemented);
    5086                 :          0 :         break;
    5087                 :            :     }
    5088                 :            : 
    5089                 :      91891 :     return res;
    5090                 :            : }
    5091                 :            : 
    5092                 :            : static PyObject *
    5093                 :     106849 : object_get_class(PyObject *self, void *closure)
    5094                 :            : {
    5095                 :     106849 :     return Py_NewRef(Py_TYPE(self));
    5096                 :            : }
    5097                 :            : 
    5098                 :            : static int
    5099                 :          0 : compatible_with_tp_base(PyTypeObject *child)
    5100                 :            : {
    5101                 :          0 :     PyTypeObject *parent = child->tp_base;
    5102                 :          0 :     return (parent != NULL &&
    5103         [ #  # ]:          0 :             child->tp_basicsize == parent->tp_basicsize &&
    5104         [ #  # ]:          0 :             child->tp_itemsize == parent->tp_itemsize &&
    5105         [ #  # ]:          0 :             child->tp_dictoffset == parent->tp_dictoffset &&
    5106         [ #  # ]:          0 :             child->tp_weaklistoffset == parent->tp_weaklistoffset &&
    5107                 :          0 :             ((child->tp_flags & Py_TPFLAGS_HAVE_GC) ==
    5108   [ #  #  #  # ]:          0 :              (parent->tp_flags & Py_TPFLAGS_HAVE_GC)) &&
    5109         [ #  # ]:          0 :             (child->tp_dealloc == subtype_dealloc ||
    5110         [ #  # ]:          0 :              child->tp_dealloc == parent->tp_dealloc));
    5111                 :            : }
    5112                 :            : 
    5113                 :            : static int
    5114                 :          0 : same_slots_added(PyTypeObject *a, PyTypeObject *b)
    5115                 :            : {
    5116                 :          0 :     PyTypeObject *base = a->tp_base;
    5117                 :            :     Py_ssize_t size;
    5118                 :            :     PyObject *slots_a, *slots_b;
    5119                 :            : 
    5120                 :            :     assert(base == b->tp_base);
    5121                 :          0 :     size = base->tp_basicsize;
    5122   [ #  #  #  # ]:          0 :     if (a->tp_dictoffset == size && b->tp_dictoffset == size)
    5123                 :          0 :         size += sizeof(PyObject *);
    5124   [ #  #  #  # ]:          0 :     if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
    5125                 :          0 :         size += sizeof(PyObject *);
    5126                 :            : 
    5127                 :            :     /* Check slots compliance */
    5128         [ #  # ]:          0 :     if (!(a->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
    5129         [ #  # ]:          0 :         !(b->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    5130                 :          0 :         return 0;
    5131                 :            :     }
    5132                 :          0 :     slots_a = ((PyHeapTypeObject *)a)->ht_slots;
    5133                 :          0 :     slots_b = ((PyHeapTypeObject *)b)->ht_slots;
    5134   [ #  #  #  # ]:          0 :     if (slots_a && slots_b) {
    5135         [ #  # ]:          0 :         if (PyObject_RichCompareBool(slots_a, slots_b, Py_EQ) != 1)
    5136                 :          0 :             return 0;
    5137                 :          0 :         size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
    5138                 :            :     }
    5139   [ #  #  #  # ]:          0 :     return size == a->tp_basicsize && size == b->tp_basicsize;
    5140                 :            : }
    5141                 :            : 
    5142                 :            : static int
    5143                 :          0 : compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, const char* attr)
    5144                 :            : {
    5145                 :            :     PyTypeObject *newbase, *oldbase;
    5146                 :            : 
    5147         [ #  # ]:          0 :     if (newto->tp_free != oldto->tp_free) {
    5148                 :          0 :         PyErr_Format(PyExc_TypeError,
    5149                 :            :                      "%s assignment: "
    5150                 :            :                      "'%s' deallocator differs from '%s'",
    5151                 :            :                      attr,
    5152                 :            :                      newto->tp_name,
    5153                 :            :                      oldto->tp_name);
    5154                 :          0 :         return 0;
    5155                 :            :     }
    5156                 :            :     /*
    5157                 :            :      It's tricky to tell if two arbitrary types are sufficiently compatible as
    5158                 :            :      to be interchangeable; e.g., even if they have the same tp_basicsize, they
    5159                 :            :      might have totally different struct fields. It's much easier to tell if a
    5160                 :            :      type and its supertype are compatible; e.g., if they have the same
    5161                 :            :      tp_basicsize, then that means they have identical fields. So to check
    5162                 :            :      whether two arbitrary types are compatible, we first find the highest
    5163                 :            :      supertype that each is compatible with, and then if those supertypes are
    5164                 :            :      compatible then the original types must also be compatible.
    5165                 :            :     */
    5166                 :          0 :     newbase = newto;
    5167                 :          0 :     oldbase = oldto;
    5168         [ #  # ]:          0 :     while (compatible_with_tp_base(newbase))
    5169                 :          0 :         newbase = newbase->tp_base;
    5170         [ #  # ]:          0 :     while (compatible_with_tp_base(oldbase))
    5171                 :          0 :         oldbase = oldbase->tp_base;
    5172         [ #  # ]:          0 :     if (newbase != oldbase &&
    5173   [ #  #  #  # ]:          0 :         (newbase->tp_base != oldbase->tp_base ||
    5174                 :          0 :          !same_slots_added(newbase, oldbase))) {
    5175                 :          0 :         goto differs;
    5176                 :            :     }
    5177                 :            :     /* The above does not check for the preheader */
    5178                 :          0 :     if ((oldto->tp_flags & Py_TPFLAGS_PREHEADER) ==
    5179         [ #  # ]:          0 :         ((newto->tp_flags & Py_TPFLAGS_PREHEADER)))
    5180                 :            :     {
    5181                 :          0 :         return 1;
    5182                 :            :     }
    5183                 :          0 : differs:
    5184                 :          0 :     PyErr_Format(PyExc_TypeError,
    5185                 :            :                     "%s assignment: "
    5186                 :            :                     "'%s' object layout differs from '%s'",
    5187                 :            :                     attr,
    5188                 :            :                     newto->tp_name,
    5189                 :            :                     oldto->tp_name);
    5190                 :          0 :     return 0;
    5191                 :            : }
    5192                 :            : 
    5193                 :            : static int
    5194                 :          0 : object_set_class(PyObject *self, PyObject *value, void *closure)
    5195                 :            : {
    5196                 :          0 :     PyTypeObject *oldto = Py_TYPE(self);
    5197                 :            : 
    5198         [ #  # ]:          0 :     if (value == NULL) {
    5199                 :          0 :         PyErr_SetString(PyExc_TypeError,
    5200                 :            :                         "can't delete __class__ attribute");
    5201                 :          0 :         return -1;
    5202                 :            :     }
    5203         [ #  # ]:          0 :     if (!PyType_Check(value)) {
    5204                 :          0 :         PyErr_Format(PyExc_TypeError,
    5205                 :            :           "__class__ must be set to a class, not '%s' object",
    5206                 :          0 :           Py_TYPE(value)->tp_name);
    5207                 :          0 :         return -1;
    5208                 :            :     }
    5209                 :          0 :     PyTypeObject *newto = (PyTypeObject *)value;
    5210                 :            : 
    5211         [ #  # ]:          0 :     if (PySys_Audit("object.__setattr__", "OsO",
    5212                 :            :                     self, "__class__", value) < 0) {
    5213                 :          0 :         return -1;
    5214                 :            :     }
    5215                 :            : 
    5216                 :            :     /* In versions of CPython prior to 3.5, the code in
    5217                 :            :        compatible_for_assignment was not set up to correctly check for memory
    5218                 :            :        layout / slot / etc. compatibility for non-HEAPTYPE classes, so we just
    5219                 :            :        disallowed __class__ assignment in any case that wasn't HEAPTYPE ->
    5220                 :            :        HEAPTYPE.
    5221                 :            : 
    5222                 :            :        During the 3.5 development cycle, we fixed the code in
    5223                 :            :        compatible_for_assignment to correctly check compatibility between
    5224                 :            :        arbitrary types, and started allowing __class__ assignment in all cases
    5225                 :            :        where the old and new types did in fact have compatible slots and
    5226                 :            :        memory layout (regardless of whether they were implemented as HEAPTYPEs
    5227                 :            :        or not).
    5228                 :            : 
    5229                 :            :        Just before 3.5 was released, though, we discovered that this led to
    5230                 :            :        problems with immutable types like int, where the interpreter assumes
    5231                 :            :        they are immutable and interns some values. Formerly this wasn't a
    5232                 :            :        problem, because they really were immutable -- in particular, all the
    5233                 :            :        types where the interpreter applied this interning trick happened to
    5234                 :            :        also be statically allocated, so the old HEAPTYPE rules were
    5235                 :            :        "accidentally" stopping them from allowing __class__ assignment. But
    5236                 :            :        with the changes to __class__ assignment, we started allowing code like
    5237                 :            : 
    5238                 :            :          class MyInt(int):
    5239                 :            :              ...
    5240                 :            :          # Modifies the type of *all* instances of 1 in the whole program,
    5241                 :            :          # including future instances (!), because the 1 object is interned.
    5242                 :            :          (1).__class__ = MyInt
    5243                 :            : 
    5244                 :            :        (see https://bugs.python.org/issue24912).
    5245                 :            : 
    5246                 :            :        In theory the proper fix would be to identify which classes rely on
    5247                 :            :        this invariant and somehow disallow __class__ assignment only for them,
    5248                 :            :        perhaps via some mechanism like a new Py_TPFLAGS_IMMUTABLE flag (a
    5249                 :            :        "denylisting" approach). But in practice, since this problem wasn't
    5250                 :            :        noticed late in the 3.5 RC cycle, we're taking the conservative
    5251                 :            :        approach and reinstating the same HEAPTYPE->HEAPTYPE check that we used
    5252                 :            :        to have, plus an "allowlist". For now, the allowlist consists only of
    5253                 :            :        ModuleType subtypes, since those are the cases that motivated the patch
    5254                 :            :        in the first place -- see https://bugs.python.org/issue22986 -- and
    5255                 :            :        since module objects are mutable we can be sure that they are
    5256                 :            :        definitely not being interned. So now we allow HEAPTYPE->HEAPTYPE *or*
    5257                 :            :        ModuleType subtype -> ModuleType subtype.
    5258                 :            : 
    5259                 :            :        So far as we know, all the code beyond the following 'if' statement
    5260                 :            :        will correctly handle non-HEAPTYPE classes, and the HEAPTYPE check is
    5261                 :            :        needed only to protect that subset of non-HEAPTYPE classes for which
    5262                 :            :        the interpreter has baked in the assumption that all instances are
    5263                 :            :        truly immutable.
    5264                 :            :     */
    5265   [ #  #  #  # ]:          0 :     if (!(PyType_IsSubtype(newto, &PyModule_Type) &&
    5266         [ #  # ]:          0 :           PyType_IsSubtype(oldto, &PyModule_Type)) &&
    5267         [ #  # ]:          0 :         (_PyType_HasFeature(newto, Py_TPFLAGS_IMMUTABLETYPE) ||
    5268                 :          0 :          _PyType_HasFeature(oldto, Py_TPFLAGS_IMMUTABLETYPE))) {
    5269                 :          0 :         PyErr_Format(PyExc_TypeError,
    5270                 :            :                      "__class__ assignment only supported for mutable types "
    5271                 :            :                      "or ModuleType subclasses");
    5272                 :          0 :         return -1;
    5273                 :            :     }
    5274                 :            : 
    5275         [ #  # ]:          0 :     if (compatible_for_assignment(oldto, newto, "__class__")) {
    5276                 :            :         /* Changing the class will change the implicit dict keys,
    5277                 :            :          * so we must materialize the dictionary first. */
    5278                 :            :         assert((oldto->tp_flags & Py_TPFLAGS_PREHEADER) == (newto->tp_flags & Py_TPFLAGS_PREHEADER));
    5279                 :          0 :         _PyObject_GetDictPtr(self);
    5280   [ #  #  #  # ]:          0 :         if (oldto->tp_flags & Py_TPFLAGS_MANAGED_DICT &&
    5281                 :          0 :             _PyDictOrValues_IsValues(*_PyObject_DictOrValuesPointer(self)))
    5282                 :            :         {
    5283                 :            :             /* Was unable to convert to dict */
    5284                 :          0 :             PyErr_NoMemory();
    5285                 :          0 :             return -1;
    5286                 :            :         }
    5287         [ #  # ]:          0 :         if (newto->tp_flags & Py_TPFLAGS_HEAPTYPE) {
    5288                 :          0 :             Py_INCREF(newto);
    5289                 :            :         }
    5290                 :          0 :         Py_SET_TYPE(self, newto);
    5291         [ #  # ]:          0 :         if (oldto->tp_flags & Py_TPFLAGS_HEAPTYPE)
    5292                 :          0 :             Py_DECREF(oldto);
    5293                 :          0 :         return 0;
    5294                 :            :     }
    5295                 :            :     else {
    5296                 :          0 :         return -1;
    5297                 :            :     }
    5298                 :            : }
    5299                 :            : 
    5300                 :            : static PyGetSetDef object_getsets[] = {
    5301                 :            :     {"__class__", object_get_class, object_set_class,
    5302                 :            :      PyDoc_STR("the object's class")},
    5303                 :            :     {0}
    5304                 :            : };
    5305                 :            : 
    5306                 :            : 
    5307                 :            : /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
    5308                 :            :    We fall back to helpers in copyreg for:
    5309                 :            :    - pickle protocols < 2
    5310                 :            :    - calculating the list of slot names (done only once per class)
    5311                 :            :    - the __newobj__ function (which is used as a token but never called)
    5312                 :            : */
    5313                 :            : 
    5314                 :            : static PyObject *
    5315                 :          0 : import_copyreg(void)
    5316                 :            : {
    5317                 :            :     /* Try to fetch cached copy of copyreg from sys.modules first in an
    5318                 :            :        attempt to avoid the import overhead. Previously this was implemented
    5319                 :            :        by storing a reference to the cached module in a static variable, but
    5320                 :            :        this broke when multiple embedded interpreters were in use (see issue
    5321                 :            :        #17408 and #19088). */
    5322                 :          0 :     PyObject *copyreg_module = PyImport_GetModule(&_Py_ID(copyreg));
    5323         [ #  # ]:          0 :     if (copyreg_module != NULL) {
    5324                 :          0 :         return copyreg_module;
    5325                 :            :     }
    5326         [ #  # ]:          0 :     if (PyErr_Occurred()) {
    5327                 :          0 :         return NULL;
    5328                 :            :     }
    5329                 :          0 :     return PyImport_Import(&_Py_ID(copyreg));
    5330                 :            : }
    5331                 :            : 
    5332                 :            : static PyObject *
    5333                 :          0 : _PyType_GetSlotNames(PyTypeObject *cls)
    5334                 :            : {
    5335                 :            :     PyObject *copyreg;
    5336                 :            :     PyObject *slotnames;
    5337                 :            : 
    5338                 :            :     assert(PyType_Check(cls));
    5339                 :            : 
    5340                 :            :     /* Get the slot names from the cache in the class if possible. */
    5341                 :          0 :     slotnames = PyDict_GetItemWithError(cls->tp_dict, &_Py_ID(__slotnames__));
    5342         [ #  # ]:          0 :     if (slotnames != NULL) {
    5343   [ #  #  #  # ]:          0 :         if (slotnames != Py_None && !PyList_Check(slotnames)) {
    5344                 :          0 :             PyErr_Format(PyExc_TypeError,
    5345                 :            :                          "%.200s.__slotnames__ should be a list or None, "
    5346                 :            :                          "not %.200s",
    5347                 :          0 :                          cls->tp_name, Py_TYPE(slotnames)->tp_name);
    5348                 :          0 :             return NULL;
    5349                 :            :         }
    5350                 :          0 :         return Py_NewRef(slotnames);
    5351                 :            :     }
    5352                 :            :     else {
    5353         [ #  # ]:          0 :         if (PyErr_Occurred()) {
    5354                 :          0 :             return NULL;
    5355                 :            :         }
    5356                 :            :         /* The class does not have the slot names cached yet. */
    5357                 :            :     }
    5358                 :            : 
    5359                 :          0 :     copyreg = import_copyreg();
    5360         [ #  # ]:          0 :     if (copyreg == NULL)
    5361                 :          0 :         return NULL;
    5362                 :            : 
    5363                 :            :     /* Use _slotnames function from the copyreg module to find the slots
    5364                 :            :        by this class and its bases. This function will cache the result
    5365                 :            :        in __slotnames__. */
    5366                 :          0 :     slotnames = PyObject_CallMethodOneArg(
    5367                 :            :             copyreg, &_Py_ID(_slotnames), (PyObject *)cls);
    5368                 :          0 :     Py_DECREF(copyreg);
    5369         [ #  # ]:          0 :     if (slotnames == NULL)
    5370                 :          0 :         return NULL;
    5371                 :            : 
    5372   [ #  #  #  # ]:          0 :     if (slotnames != Py_None && !PyList_Check(slotnames)) {
    5373                 :          0 :         PyErr_SetString(PyExc_TypeError,
    5374                 :            :                         "copyreg._slotnames didn't return a list or None");
    5375                 :          0 :         Py_DECREF(slotnames);
    5376                 :          0 :         return NULL;
    5377                 :            :     }
    5378                 :            : 
    5379                 :          0 :     return slotnames;
    5380                 :            : }
    5381                 :            : 
    5382                 :            : static PyObject *
    5383                 :          0 : object_getstate_default(PyObject *obj, int required)
    5384                 :            : {
    5385                 :            :     PyObject *state;
    5386                 :            :     PyObject *slotnames;
    5387                 :            : 
    5388   [ #  #  #  # ]:          0 :     if (required && Py_TYPE(obj)->tp_itemsize) {
    5389                 :          0 :         PyErr_Format(PyExc_TypeError,
    5390                 :            :                      "cannot pickle %.200s objects",
    5391                 :          0 :                      Py_TYPE(obj)->tp_name);
    5392                 :          0 :         return NULL;
    5393                 :            :     }
    5394                 :            : 
    5395         [ #  # ]:          0 :     if (_PyObject_IsInstanceDictEmpty(obj)) {
    5396                 :          0 :         state = Py_NewRef(Py_None);
    5397                 :            :     }
    5398                 :            :     else {
    5399                 :          0 :         state = PyObject_GenericGetDict(obj, NULL);
    5400         [ #  # ]:          0 :         if (state == NULL) {
    5401                 :          0 :             return NULL;
    5402                 :            :         }
    5403                 :            :     }
    5404                 :            : 
    5405                 :          0 :     slotnames = _PyType_GetSlotNames(Py_TYPE(obj));
    5406         [ #  # ]:          0 :     if (slotnames == NULL) {
    5407                 :          0 :         Py_DECREF(state);
    5408                 :          0 :         return NULL;
    5409                 :            :     }
    5410                 :            : 
    5411                 :            :     assert(slotnames == Py_None || PyList_Check(slotnames));
    5412         [ #  # ]:          0 :     if (required) {
    5413                 :          0 :         Py_ssize_t basicsize = PyBaseObject_Type.tp_basicsize;
    5414         [ #  # ]:          0 :         if (Py_TYPE(obj)->tp_dictoffset &&
    5415         [ #  # ]:          0 :             (Py_TYPE(obj)->tp_flags & Py_TPFLAGS_MANAGED_DICT) == 0)
    5416                 :            :         {
    5417                 :          0 :             basicsize += sizeof(PyObject *);
    5418                 :            :         }
    5419         [ #  # ]:          0 :         if (Py_TYPE(obj)->tp_weaklistoffset > 0) {
    5420                 :          0 :             basicsize += sizeof(PyObject *);
    5421                 :            :         }
    5422         [ #  # ]:          0 :         if (slotnames != Py_None) {
    5423                 :          0 :             basicsize += sizeof(PyObject *) * PyList_GET_SIZE(slotnames);
    5424                 :            :         }
    5425         [ #  # ]:          0 :         if (Py_TYPE(obj)->tp_basicsize > basicsize) {
    5426                 :          0 :             Py_DECREF(slotnames);
    5427                 :          0 :             Py_DECREF(state);
    5428                 :          0 :             PyErr_Format(PyExc_TypeError,
    5429                 :            :                          "cannot pickle '%.200s' object",
    5430                 :          0 :                          Py_TYPE(obj)->tp_name);
    5431                 :          0 :             return NULL;
    5432                 :            :         }
    5433                 :            :     }
    5434                 :            : 
    5435   [ #  #  #  # ]:          0 :     if (slotnames != Py_None && PyList_GET_SIZE(slotnames) > 0) {
    5436                 :            :         PyObject *slots;
    5437                 :            :         Py_ssize_t slotnames_size, i;
    5438                 :            : 
    5439                 :          0 :         slots = PyDict_New();
    5440         [ #  # ]:          0 :         if (slots == NULL) {
    5441                 :          0 :             Py_DECREF(slotnames);
    5442                 :          0 :             Py_DECREF(state);
    5443                 :          0 :             return NULL;
    5444                 :            :         }
    5445                 :            : 
    5446                 :          0 :         slotnames_size = PyList_GET_SIZE(slotnames);
    5447         [ #  # ]:          0 :         for (i = 0; i < slotnames_size; i++) {
    5448                 :            :             PyObject *name, *value;
    5449                 :            : 
    5450                 :          0 :             name = Py_NewRef(PyList_GET_ITEM(slotnames, i));
    5451         [ #  # ]:          0 :             if (_PyObject_LookupAttr(obj, name, &value) < 0) {
    5452                 :          0 :                 Py_DECREF(name);
    5453                 :          0 :                 goto error;
    5454                 :            :             }
    5455         [ #  # ]:          0 :             if (value == NULL) {
    5456                 :          0 :                 Py_DECREF(name);
    5457                 :            :                 /* It is not an error if the attribute is not present. */
    5458                 :            :             }
    5459                 :            :             else {
    5460                 :          0 :                 int err = PyDict_SetItem(slots, name, value);
    5461                 :          0 :                 Py_DECREF(name);
    5462                 :          0 :                 Py_DECREF(value);
    5463         [ #  # ]:          0 :                 if (err) {
    5464                 :          0 :                     goto error;
    5465                 :            :                 }
    5466                 :            :             }
    5467                 :            : 
    5468                 :            :             /* The list is stored on the class so it may mutate while we
    5469                 :            :                iterate over it */
    5470         [ #  # ]:          0 :             if (slotnames_size != PyList_GET_SIZE(slotnames)) {
    5471                 :          0 :                 PyErr_Format(PyExc_RuntimeError,
    5472                 :            :                              "__slotsname__ changed size during iteration");
    5473                 :          0 :                 goto error;
    5474                 :            :             }
    5475                 :            : 
    5476                 :            :             /* We handle errors within the loop here. */
    5477                 :            :             if (0) {
    5478                 :          0 :               error:
    5479                 :          0 :                 Py_DECREF(slotnames);
    5480                 :          0 :                 Py_DECREF(slots);
    5481                 :          0 :                 Py_DECREF(state);
    5482                 :          0 :                 return NULL;
    5483                 :            :             }
    5484                 :            :         }
    5485                 :            : 
    5486                 :            :         /* If we found some slot attributes, pack them in a tuple along
    5487                 :            :            the original attribute dictionary. */
    5488         [ #  # ]:          0 :         if (PyDict_GET_SIZE(slots) > 0) {
    5489                 :            :             PyObject *state2;
    5490                 :            : 
    5491                 :          0 :             state2 = PyTuple_Pack(2, state, slots);
    5492                 :          0 :             Py_DECREF(state);
    5493         [ #  # ]:          0 :             if (state2 == NULL) {
    5494                 :          0 :                 Py_DECREF(slotnames);
    5495                 :          0 :                 Py_DECREF(slots);
    5496                 :          0 :                 return NULL;
    5497                 :            :             }
    5498                 :          0 :             state = state2;
    5499                 :            :         }
    5500                 :          0 :         Py_DECREF(slots);
    5501                 :            :     }
    5502                 :          0 :     Py_DECREF(slotnames);
    5503                 :            : 
    5504                 :          0 :     return state;
    5505                 :            : }
    5506                 :            : 
    5507                 :            : static PyObject *
    5508                 :          0 : object_getstate(PyObject *obj, int required)
    5509                 :            : {
    5510                 :            :     PyObject *getstate, *state;
    5511                 :            : 
    5512                 :          0 :     getstate = PyObject_GetAttr(obj, &_Py_ID(__getstate__));
    5513         [ #  # ]:          0 :     if (getstate == NULL) {
    5514                 :          0 :         return NULL;
    5515                 :            :     }
    5516   [ #  #  #  # ]:          0 :     if (PyCFunction_Check(getstate) &&
    5517         [ #  # ]:          0 :         PyCFunction_GET_SELF(getstate) == obj &&
    5518                 :          0 :         PyCFunction_GET_FUNCTION(getstate) == object___getstate__)
    5519                 :            :     {
    5520                 :            :         /* If __getstate__ is not overridden pass the required argument. */
    5521                 :          0 :         state = object_getstate_default(obj, required);
    5522                 :            :     }
    5523                 :            :     else {
    5524                 :          0 :         state = _PyObject_CallNoArgs(getstate);
    5525                 :            :     }
    5526                 :          0 :     Py_DECREF(getstate);
    5527                 :          0 :     return state;
    5528                 :            : }
    5529                 :            : 
    5530                 :            : PyObject *
    5531                 :          0 : _PyObject_GetState(PyObject *obj)
    5532                 :            : {
    5533                 :          0 :     return object_getstate(obj, 0);
    5534                 :            : }
    5535                 :            : 
    5536                 :            : /*[clinic input]
    5537                 :            : object.__getstate__
    5538                 :            : 
    5539                 :            : Helper for pickle.
    5540                 :            : [clinic start generated code]*/
    5541                 :            : 
    5542                 :            : static PyObject *
    5543                 :          0 : object___getstate___impl(PyObject *self)
    5544                 :            : /*[clinic end generated code: output=5a2500dcb6217e9e input=692314d8fbe194ee]*/
    5545                 :            : {
    5546                 :          0 :     return object_getstate_default(self, 0);
    5547                 :            : }
    5548                 :            : 
    5549                 :            : static int
    5550                 :          0 : _PyObject_GetNewArguments(PyObject *obj, PyObject **args, PyObject **kwargs)
    5551                 :            : {
    5552                 :            :     PyObject *getnewargs, *getnewargs_ex;
    5553                 :            : 
    5554   [ #  #  #  # ]:          0 :     if (args == NULL || kwargs == NULL) {
    5555                 :          0 :         PyErr_BadInternalCall();
    5556                 :          0 :         return -1;
    5557                 :            :     }
    5558                 :            : 
    5559                 :            :     /* We first attempt to fetch the arguments for __new__ by calling
    5560                 :            :        __getnewargs_ex__ on the object. */
    5561                 :          0 :     getnewargs_ex = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs_ex__));
    5562         [ #  # ]:          0 :     if (getnewargs_ex != NULL) {
    5563                 :          0 :         PyObject *newargs = _PyObject_CallNoArgs(getnewargs_ex);
    5564                 :          0 :         Py_DECREF(getnewargs_ex);
    5565         [ #  # ]:          0 :         if (newargs == NULL) {
    5566                 :          0 :             return -1;
    5567                 :            :         }
    5568         [ #  # ]:          0 :         if (!PyTuple_Check(newargs)) {
    5569                 :          0 :             PyErr_Format(PyExc_TypeError,
    5570                 :            :                          "__getnewargs_ex__ should return a tuple, "
    5571                 :          0 :                          "not '%.200s'", Py_TYPE(newargs)->tp_name);
    5572                 :          0 :             Py_DECREF(newargs);
    5573                 :          0 :             return -1;
    5574                 :            :         }
    5575         [ #  # ]:          0 :         if (PyTuple_GET_SIZE(newargs) != 2) {
    5576                 :          0 :             PyErr_Format(PyExc_ValueError,
    5577                 :            :                          "__getnewargs_ex__ should return a tuple of "
    5578                 :            :                          "length 2, not %zd", PyTuple_GET_SIZE(newargs));
    5579                 :          0 :             Py_DECREF(newargs);
    5580                 :          0 :             return -1;
    5581                 :            :         }
    5582                 :          0 :         *args = Py_NewRef(PyTuple_GET_ITEM(newargs, 0));
    5583                 :          0 :         *kwargs = Py_NewRef(PyTuple_GET_ITEM(newargs, 1));
    5584                 :          0 :         Py_DECREF(newargs);
    5585                 :            : 
    5586                 :            :         /* XXX We should perhaps allow None to be passed here. */
    5587         [ #  # ]:          0 :         if (!PyTuple_Check(*args)) {
    5588                 :          0 :             PyErr_Format(PyExc_TypeError,
    5589                 :            :                          "first item of the tuple returned by "
    5590                 :            :                          "__getnewargs_ex__ must be a tuple, not '%.200s'",
    5591                 :          0 :                          Py_TYPE(*args)->tp_name);
    5592         [ #  # ]:          0 :             Py_CLEAR(*args);
    5593         [ #  # ]:          0 :             Py_CLEAR(*kwargs);
    5594                 :          0 :             return -1;
    5595                 :            :         }
    5596         [ #  # ]:          0 :         if (!PyDict_Check(*kwargs)) {
    5597                 :          0 :             PyErr_Format(PyExc_TypeError,
    5598                 :            :                          "second item of the tuple returned by "
    5599                 :            :                          "__getnewargs_ex__ must be a dict, not '%.200s'",
    5600                 :          0 :                          Py_TYPE(*kwargs)->tp_name);
    5601         [ #  # ]:          0 :             Py_CLEAR(*args);
    5602         [ #  # ]:          0 :             Py_CLEAR(*kwargs);
    5603                 :          0 :             return -1;
    5604                 :            :         }
    5605                 :          0 :         return 0;
    5606         [ #  # ]:          0 :     } else if (PyErr_Occurred()) {
    5607                 :          0 :         return -1;
    5608                 :            :     }
    5609                 :            : 
    5610                 :            :     /* The object does not have __getnewargs_ex__ so we fallback on using
    5611                 :            :        __getnewargs__ instead. */
    5612                 :          0 :     getnewargs = _PyObject_LookupSpecial(obj, &_Py_ID(__getnewargs__));
    5613         [ #  # ]:          0 :     if (getnewargs != NULL) {
    5614                 :          0 :         *args = _PyObject_CallNoArgs(getnewargs);
    5615                 :          0 :         Py_DECREF(getnewargs);
    5616         [ #  # ]:          0 :         if (*args == NULL) {
    5617                 :          0 :             return -1;
    5618                 :            :         }
    5619         [ #  # ]:          0 :         if (!PyTuple_Check(*args)) {
    5620                 :          0 :             PyErr_Format(PyExc_TypeError,
    5621                 :            :                          "__getnewargs__ should return a tuple, "
    5622                 :          0 :                          "not '%.200s'", Py_TYPE(*args)->tp_name);
    5623         [ #  # ]:          0 :             Py_CLEAR(*args);
    5624                 :          0 :             return -1;
    5625                 :            :         }
    5626                 :          0 :         *kwargs = NULL;
    5627                 :          0 :         return 0;
    5628         [ #  # ]:          0 :     } else if (PyErr_Occurred()) {
    5629                 :          0 :         return -1;
    5630                 :            :     }
    5631                 :            : 
    5632                 :            :     /* The object does not have __getnewargs_ex__ and __getnewargs__. This may
    5633                 :            :        mean __new__ does not takes any arguments on this object, or that the
    5634                 :            :        object does not implement the reduce protocol for pickling or
    5635                 :            :        copying. */
    5636                 :          0 :     *args = NULL;
    5637                 :          0 :     *kwargs = NULL;
    5638                 :          0 :     return 0;
    5639                 :            : }
    5640                 :            : 
    5641                 :            : static int
    5642                 :          0 : _PyObject_GetItemsIter(PyObject *obj, PyObject **listitems,
    5643                 :            :                        PyObject **dictitems)
    5644                 :            : {
    5645   [ #  #  #  # ]:          0 :     if (listitems == NULL || dictitems == NULL) {
    5646                 :          0 :         PyErr_BadInternalCall();
    5647                 :          0 :         return -1;
    5648                 :            :     }
    5649                 :            : 
    5650         [ #  # ]:          0 :     if (!PyList_Check(obj)) {
    5651                 :          0 :         *listitems = Py_NewRef(Py_None);
    5652                 :            :     }
    5653                 :            :     else {
    5654                 :          0 :         *listitems = PyObject_GetIter(obj);
    5655         [ #  # ]:          0 :         if (*listitems == NULL)
    5656                 :          0 :             return -1;
    5657                 :            :     }
    5658                 :            : 
    5659         [ #  # ]:          0 :     if (!PyDict_Check(obj)) {
    5660                 :          0 :         *dictitems = Py_NewRef(Py_None);
    5661                 :            :     }
    5662                 :            :     else {
    5663                 :          0 :         PyObject *items = PyObject_CallMethodNoArgs(obj, &_Py_ID(items));
    5664         [ #  # ]:          0 :         if (items == NULL) {
    5665         [ #  # ]:          0 :             Py_CLEAR(*listitems);
    5666                 :          0 :             return -1;
    5667                 :            :         }
    5668                 :          0 :         *dictitems = PyObject_GetIter(items);
    5669                 :          0 :         Py_DECREF(items);
    5670         [ #  # ]:          0 :         if (*dictitems == NULL) {
    5671         [ #  # ]:          0 :             Py_CLEAR(*listitems);
    5672                 :          0 :             return -1;
    5673                 :            :         }
    5674                 :            :     }
    5675                 :            : 
    5676                 :            :     assert(*listitems != NULL && *dictitems != NULL);
    5677                 :            : 
    5678                 :          0 :     return 0;
    5679                 :            : }
    5680                 :            : 
    5681                 :            : static PyObject *
    5682                 :          0 : reduce_newobj(PyObject *obj)
    5683                 :            : {
    5684                 :          0 :     PyObject *args = NULL, *kwargs = NULL;
    5685                 :            :     PyObject *copyreg;
    5686                 :            :     PyObject *newobj, *newargs, *state, *listitems, *dictitems;
    5687                 :            :     PyObject *result;
    5688                 :            :     int hasargs;
    5689                 :            : 
    5690         [ #  # ]:          0 :     if (Py_TYPE(obj)->tp_new == NULL) {
    5691                 :          0 :         PyErr_Format(PyExc_TypeError,
    5692                 :            :                      "cannot pickle '%.200s' object",
    5693                 :          0 :                      Py_TYPE(obj)->tp_name);
    5694                 :          0 :         return NULL;
    5695                 :            :     }
    5696         [ #  # ]:          0 :     if (_PyObject_GetNewArguments(obj, &args, &kwargs) < 0)
    5697                 :          0 :         return NULL;
    5698                 :            : 
    5699                 :          0 :     copyreg = import_copyreg();
    5700         [ #  # ]:          0 :     if (copyreg == NULL) {
    5701                 :          0 :         Py_XDECREF(args);
    5702                 :          0 :         Py_XDECREF(kwargs);
    5703                 :          0 :         return NULL;
    5704                 :            :     }
    5705                 :          0 :     hasargs = (args != NULL);
    5706   [ #  #  #  # ]:          0 :     if (kwargs == NULL || PyDict_GET_SIZE(kwargs) == 0) {
    5707                 :            :         PyObject *cls;
    5708                 :            :         Py_ssize_t i, n;
    5709                 :            : 
    5710                 :          0 :         Py_XDECREF(kwargs);
    5711                 :          0 :         newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj__));
    5712                 :          0 :         Py_DECREF(copyreg);
    5713         [ #  # ]:          0 :         if (newobj == NULL) {
    5714                 :          0 :             Py_XDECREF(args);
    5715                 :          0 :             return NULL;
    5716                 :            :         }
    5717         [ #  # ]:          0 :         n = args ? PyTuple_GET_SIZE(args) : 0;
    5718                 :          0 :         newargs = PyTuple_New(n+1);
    5719         [ #  # ]:          0 :         if (newargs == NULL) {
    5720                 :          0 :             Py_XDECREF(args);
    5721                 :          0 :             Py_DECREF(newobj);
    5722                 :          0 :             return NULL;
    5723                 :            :         }
    5724                 :          0 :         cls = (PyObject *) Py_TYPE(obj);
    5725                 :          0 :         PyTuple_SET_ITEM(newargs, 0, Py_NewRef(cls));
    5726         [ #  # ]:          0 :         for (i = 0; i < n; i++) {
    5727                 :          0 :             PyObject *v = PyTuple_GET_ITEM(args, i);
    5728                 :          0 :             PyTuple_SET_ITEM(newargs, i+1, Py_NewRef(v));
    5729                 :            :         }
    5730                 :          0 :         Py_XDECREF(args);
    5731                 :            :     }
    5732         [ #  # ]:          0 :     else if (args != NULL) {
    5733                 :          0 :         newobj = PyObject_GetAttr(copyreg, &_Py_ID(__newobj_ex__));
    5734                 :          0 :         Py_DECREF(copyreg);
    5735         [ #  # ]:          0 :         if (newobj == NULL) {
    5736                 :          0 :             Py_DECREF(args);
    5737                 :          0 :             Py_DECREF(kwargs);
    5738                 :          0 :             return NULL;
    5739                 :            :         }
    5740                 :          0 :         newargs = PyTuple_Pack(3, Py_TYPE(obj), args, kwargs);
    5741                 :          0 :         Py_DECREF(args);
    5742                 :          0 :         Py_DECREF(kwargs);
    5743         [ #  # ]:          0 :         if (newargs == NULL) {
    5744                 :          0 :             Py_DECREF(newobj);
    5745                 :          0 :             return NULL;
    5746                 :            :         }
    5747                 :            :     }
    5748                 :            :     else {
    5749                 :            :         /* args == NULL */
    5750                 :          0 :         Py_DECREF(kwargs);
    5751                 :          0 :         PyErr_BadInternalCall();
    5752                 :          0 :         return NULL;
    5753                 :            :     }
    5754                 :            : 
    5755   [ #  #  #  #  :          0 :     state = object_getstate(obj, !(hasargs || PyList_Check(obj) || PyDict_Check(obj)));
                   #  # ]
    5756         [ #  # ]:          0 :     if (state == NULL) {
    5757                 :          0 :         Py_DECREF(newobj);
    5758                 :          0 :         Py_DECREF(newargs);
    5759                 :          0 :         return NULL;
    5760                 :            :     }
    5761         [ #  # ]:          0 :     if (_PyObject_GetItemsIter(obj, &listitems, &dictitems) < 0) {
    5762                 :          0 :         Py_DECREF(newobj);
    5763                 :          0 :         Py_DECREF(newargs);
    5764                 :          0 :         Py_DECREF(state);
    5765                 :          0 :         return NULL;
    5766                 :            :     }
    5767                 :            : 
    5768                 :          0 :     result = PyTuple_Pack(5, newobj, newargs, state, listitems, dictitems);
    5769                 :          0 :     Py_DECREF(newobj);
    5770                 :          0 :     Py_DECREF(newargs);
    5771                 :          0 :     Py_DECREF(state);
    5772                 :          0 :     Py_DECREF(listitems);
    5773                 :          0 :     Py_DECREF(dictitems);
    5774                 :          0 :     return result;
    5775                 :            : }
    5776                 :            : 
    5777                 :            : /*
    5778                 :            :  * There were two problems when object.__reduce__ and object.__reduce_ex__
    5779                 :            :  * were implemented in the same function:
    5780                 :            :  *  - trying to pickle an object with a custom __reduce__ method that
    5781                 :            :  *    fell back to object.__reduce__ in certain circumstances led to
    5782                 :            :  *    infinite recursion at Python level and eventual RecursionError.
    5783                 :            :  *  - Pickling objects that lied about their type by overwriting the
    5784                 :            :  *    __class__ descriptor could lead to infinite recursion at C level
    5785                 :            :  *    and eventual segfault.
    5786                 :            :  *
    5787                 :            :  * Because of backwards compatibility, the two methods still have to
    5788                 :            :  * behave in the same way, even if this is not required by the pickle
    5789                 :            :  * protocol. This common functionality was moved to the _common_reduce
    5790                 :            :  * function.
    5791                 :            :  */
    5792                 :            : static PyObject *
    5793                 :          0 : _common_reduce(PyObject *self, int proto)
    5794                 :            : {
    5795                 :            :     PyObject *copyreg, *res;
    5796                 :            : 
    5797         [ #  # ]:          0 :     if (proto >= 2)
    5798                 :          0 :         return reduce_newobj(self);
    5799                 :            : 
    5800                 :          0 :     copyreg = import_copyreg();
    5801         [ #  # ]:          0 :     if (!copyreg)
    5802                 :          0 :         return NULL;
    5803                 :            : 
    5804                 :          0 :     res = PyObject_CallMethod(copyreg, "_reduce_ex", "Oi", self, proto);
    5805                 :          0 :     Py_DECREF(copyreg);
    5806                 :            : 
    5807                 :          0 :     return res;
    5808                 :            : }
    5809                 :            : 
    5810                 :            : /*[clinic input]
    5811                 :            : object.__reduce__
    5812                 :            : 
    5813                 :            : Helper for pickle.
    5814                 :            : [clinic start generated code]*/
    5815                 :            : 
    5816                 :            : static PyObject *
    5817                 :          0 : object___reduce___impl(PyObject *self)
    5818                 :            : /*[clinic end generated code: output=d4ca691f891c6e2f input=11562e663947e18b]*/
    5819                 :            : {
    5820                 :          0 :     return _common_reduce(self, 0);
    5821                 :            : }
    5822                 :            : 
    5823                 :            : /*[clinic input]
    5824                 :            : object.__reduce_ex__
    5825                 :            : 
    5826                 :            :   protocol: int
    5827                 :            :   /
    5828                 :            : 
    5829                 :            : Helper for pickle.
    5830                 :            : [clinic start generated code]*/
    5831                 :            : 
    5832                 :            : static PyObject *
    5833                 :          0 : object___reduce_ex___impl(PyObject *self, int protocol)
    5834                 :            : /*[clinic end generated code: output=2e157766f6b50094 input=f326b43fb8a4c5ff]*/
    5835                 :            : {
    5836                 :            : #define objreduce \
    5837                 :            :     (_Py_INTERP_CACHED_OBJECT(_PyInterpreterState_Get(), objreduce))
    5838                 :            :     PyObject *reduce, *res;
    5839                 :            : 
    5840         [ #  # ]:          0 :     if (objreduce == NULL) {
    5841                 :          0 :         objreduce = PyDict_GetItemWithError(
    5842                 :            :                 PyBaseObject_Type.tp_dict, &_Py_ID(__reduce__));
    5843   [ #  #  #  # ]:          0 :         if (objreduce == NULL && PyErr_Occurred()) {
    5844                 :          0 :             return NULL;
    5845                 :            :         }
    5846                 :            :     }
    5847                 :            : 
    5848         [ #  # ]:          0 :     if (_PyObject_LookupAttr(self, &_Py_ID(__reduce__), &reduce) < 0) {
    5849                 :          0 :         return NULL;
    5850                 :            :     }
    5851         [ #  # ]:          0 :     if (reduce != NULL) {
    5852                 :            :         PyObject *cls, *clsreduce;
    5853                 :            :         int override;
    5854                 :            : 
    5855                 :          0 :         cls = (PyObject *) Py_TYPE(self);
    5856                 :          0 :         clsreduce = PyObject_GetAttr(cls, &_Py_ID(__reduce__));
    5857         [ #  # ]:          0 :         if (clsreduce == NULL) {
    5858                 :          0 :             Py_DECREF(reduce);
    5859                 :          0 :             return NULL;
    5860                 :            :         }
    5861                 :          0 :         override = (clsreduce != objreduce);
    5862                 :          0 :         Py_DECREF(clsreduce);
    5863         [ #  # ]:          0 :         if (override) {
    5864                 :          0 :             res = _PyObject_CallNoArgs(reduce);
    5865                 :          0 :             Py_DECREF(reduce);
    5866                 :          0 :             return res;
    5867                 :            :         }
    5868                 :            :         else
    5869                 :          0 :             Py_DECREF(reduce);
    5870                 :            :     }
    5871                 :            : 
    5872                 :          0 :     return _common_reduce(self, protocol);
    5873                 :            : #undef objreduce
    5874                 :            : }
    5875                 :            : 
    5876                 :            : static PyObject *
    5877                 :        229 : object_subclasshook(PyObject *cls, PyObject *args)
    5878                 :            : {
    5879                 :        229 :     Py_RETURN_NOTIMPLEMENTED;
    5880                 :            : }
    5881                 :            : 
    5882                 :            : PyDoc_STRVAR(object_subclasshook_doc,
    5883                 :            : "Abstract classes can override this to customize issubclass().\n"
    5884                 :            : "\n"
    5885                 :            : "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
    5886                 :            : "It should return True, False or NotImplemented.  If it returns\n"
    5887                 :            : "NotImplemented, the normal algorithm is used.  Otherwise, it\n"
    5888                 :            : "overrides the normal algorithm (and the outcome is cached).\n");
    5889                 :            : 
    5890                 :            : static PyObject *
    5891                 :       6198 : object_init_subclass(PyObject *cls, PyObject *arg)
    5892                 :            : {
    5893                 :       6198 :     Py_RETURN_NONE;
    5894                 :            : }
    5895                 :            : 
    5896                 :            : PyDoc_STRVAR(object_init_subclass_doc,
    5897                 :            : "This method is called when a class is subclassed.\n"
    5898                 :            : "\n"
    5899                 :            : "The default implementation does nothing. It may be\n"
    5900                 :            : "overridden to extend subclasses.\n");
    5901                 :            : 
    5902                 :            : /*[clinic input]
    5903                 :            : object.__format__
    5904                 :            : 
    5905                 :            :   format_spec: unicode
    5906                 :            :   /
    5907                 :            : 
    5908                 :            : Default object formatter.
    5909                 :            : 
    5910                 :            : Return str(self) if format_spec is empty. Raise TypeError otherwise.
    5911                 :            : [clinic start generated code]*/
    5912                 :            : 
    5913                 :            : static PyObject *
    5914                 :       3132 : object___format___impl(PyObject *self, PyObject *format_spec)
    5915                 :            : /*[clinic end generated code: output=34897efb543a974b input=b94d8feb006689ea]*/
    5916                 :            : {
    5917                 :            :     /* Issue 7994: If we're converting to a string, we
    5918                 :            :        should reject format specifications */
    5919         [ -  + ]:       3132 :     if (PyUnicode_GET_LENGTH(format_spec) > 0) {
    5920                 :          0 :         PyErr_Format(PyExc_TypeError,
    5921                 :            :                      "unsupported format string passed to %.200s.__format__",
    5922                 :          0 :                      Py_TYPE(self)->tp_name);
    5923                 :          0 :         return NULL;
    5924                 :            :     }
    5925                 :       3132 :     return PyObject_Str(self);
    5926                 :            : }
    5927                 :            : 
    5928                 :            : /*[clinic input]
    5929                 :            : object.__sizeof__
    5930                 :            : 
    5931                 :            : Size of object in memory, in bytes.
    5932                 :            : [clinic start generated code]*/
    5933                 :            : 
    5934                 :            : static PyObject *
    5935                 :          0 : object___sizeof___impl(PyObject *self)
    5936                 :            : /*[clinic end generated code: output=73edab332f97d550 input=1200ff3dfe485306]*/
    5937                 :            : {
    5938                 :            :     Py_ssize_t res, isize;
    5939                 :            : 
    5940                 :          0 :     res = 0;
    5941                 :          0 :     isize = Py_TYPE(self)->tp_itemsize;
    5942         [ #  # ]:          0 :     if (isize > 0)
    5943                 :          0 :         res = Py_SIZE(self) * isize;
    5944                 :          0 :     res += Py_TYPE(self)->tp_basicsize;
    5945                 :            : 
    5946                 :          0 :     return PyLong_FromSsize_t(res);
    5947                 :            : }
    5948                 :            : 
    5949                 :            : /* __dir__ for generic objects: returns __dict__, __class__,
    5950                 :            :    and recursively up the __class__.__bases__ chain.
    5951                 :            : */
    5952                 :            : /*[clinic input]
    5953                 :            : object.__dir__
    5954                 :            : 
    5955                 :            : Default dir() implementation.
    5956                 :            : [clinic start generated code]*/
    5957                 :            : 
    5958                 :            : static PyObject *
    5959                 :          0 : object___dir___impl(PyObject *self)
    5960                 :            : /*[clinic end generated code: output=66dd48ea62f26c90 input=0a89305bec669b10]*/
    5961                 :            : {
    5962                 :          0 :     PyObject *result = NULL;
    5963                 :          0 :     PyObject *dict = NULL;
    5964                 :          0 :     PyObject *itsclass = NULL;
    5965                 :            : 
    5966                 :            :     /* Get __dict__ (which may or may not be a real dict...) */
    5967         [ #  # ]:          0 :     if (_PyObject_LookupAttr(self, &_Py_ID(__dict__), &dict) < 0) {
    5968                 :          0 :         return NULL;
    5969                 :            :     }
    5970         [ #  # ]:          0 :     if (dict == NULL) {
    5971                 :          0 :         dict = PyDict_New();
    5972                 :            :     }
    5973         [ #  # ]:          0 :     else if (!PyDict_Check(dict)) {
    5974                 :          0 :         Py_DECREF(dict);
    5975                 :          0 :         dict = PyDict_New();
    5976                 :            :     }
    5977                 :            :     else {
    5978                 :            :         /* Copy __dict__ to avoid mutating it. */
    5979                 :          0 :         PyObject *temp = PyDict_Copy(dict);
    5980                 :          0 :         Py_SETREF(dict, temp);
    5981                 :            :     }
    5982                 :            : 
    5983         [ #  # ]:          0 :     if (dict == NULL)
    5984                 :          0 :         goto error;
    5985                 :            : 
    5986                 :            :     /* Merge in attrs reachable from its class. */
    5987         [ #  # ]:          0 :     if (_PyObject_LookupAttr(self, &_Py_ID(__class__), &itsclass) < 0) {
    5988                 :          0 :         goto error;
    5989                 :            :     }
    5990                 :            :     /* XXX(tomer): Perhaps fall back to Py_TYPE(obj) if no
    5991                 :            :                    __class__ exists? */
    5992   [ #  #  #  # ]:          0 :     if (itsclass != NULL && merge_class_dict(dict, itsclass) < 0)
    5993                 :          0 :         goto error;
    5994                 :            : 
    5995                 :          0 :     result = PyDict_Keys(dict);
    5996                 :            :     /* fall through */
    5997                 :          0 : error:
    5998                 :          0 :     Py_XDECREF(itsclass);
    5999                 :          0 :     Py_XDECREF(dict);
    6000                 :          0 :     return result;
    6001                 :            : }
    6002                 :            : 
    6003                 :            : static PyMethodDef object_methods[] = {
    6004                 :            :     OBJECT___REDUCE_EX___METHODDEF
    6005                 :            :     OBJECT___REDUCE___METHODDEF
    6006                 :            :     OBJECT___GETSTATE___METHODDEF
    6007                 :            :     {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
    6008                 :            :      object_subclasshook_doc},
    6009                 :            :     {"__init_subclass__", object_init_subclass, METH_CLASS | METH_NOARGS,
    6010                 :            :      object_init_subclass_doc},
    6011                 :            :     OBJECT___FORMAT___METHODDEF
    6012                 :            :     OBJECT___SIZEOF___METHODDEF
    6013                 :            :     OBJECT___DIR___METHODDEF
    6014                 :            :     {0}
    6015                 :            : };
    6016                 :            : 
    6017                 :            : PyDoc_STRVAR(object_doc,
    6018                 :            : "object()\n--\n\n"
    6019                 :            : "The base class of the class hierarchy.\n\n"
    6020                 :            : "When called, it accepts no arguments and returns a new featureless\n"
    6021                 :            : "instance that has no instance attributes and cannot be given any.\n");
    6022                 :            : 
    6023                 :            : PyTypeObject PyBaseObject_Type = {
    6024                 :            :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    6025                 :            :     "object",                                   /* tp_name */
    6026                 :            :     sizeof(PyObject),                           /* tp_basicsize */
    6027                 :            :     0,                                          /* tp_itemsize */
    6028                 :            :     object_dealloc,                             /* tp_dealloc */
    6029                 :            :     0,                                          /* tp_vectorcall_offset */
    6030                 :            :     0,                                          /* tp_getattr */
    6031                 :            :     0,                                          /* tp_setattr */
    6032                 :            :     0,                                          /* tp_as_async */
    6033                 :            :     object_repr,                                /* tp_repr */
    6034                 :            :     0,                                          /* tp_as_number */
    6035                 :            :     0,                                          /* tp_as_sequence */
    6036                 :            :     0,                                          /* tp_as_mapping */
    6037                 :            :     (hashfunc)_Py_HashPointer,                  /* tp_hash */
    6038                 :            :     0,                                          /* tp_call */
    6039                 :            :     object_str,                                 /* tp_str */
    6040                 :            :     PyObject_GenericGetAttr,                    /* tp_getattro */
    6041                 :            :     PyObject_GenericSetAttr,                    /* tp_setattro */
    6042                 :            :     0,                                          /* tp_as_buffer */
    6043                 :            :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,   /* tp_flags */
    6044                 :            :     object_doc,                                 /* tp_doc */
    6045                 :            :     0,                                          /* tp_traverse */
    6046                 :            :     0,                                          /* tp_clear */
    6047                 :            :     object_richcompare,                         /* tp_richcompare */
    6048                 :            :     0,                                          /* tp_weaklistoffset */
    6049                 :            :     0,                                          /* tp_iter */
    6050                 :            :     0,                                          /* tp_iternext */
    6051                 :            :     object_methods,                             /* tp_methods */
    6052                 :            :     0,                                          /* tp_members */
    6053                 :            :     object_getsets,                             /* tp_getset */
    6054                 :            :     0,                                          /* tp_base */
    6055                 :            :     0,                                          /* tp_dict */
    6056                 :            :     0,                                          /* tp_descr_get */
    6057                 :            :     0,                                          /* tp_descr_set */
    6058                 :            :     0,                                          /* tp_dictoffset */
    6059                 :            :     object_init,                                /* tp_init */
    6060                 :            :     PyType_GenericAlloc,                        /* tp_alloc */
    6061                 :            :     object_new,                                 /* tp_new */
    6062                 :            :     PyObject_Del,                               /* tp_free */
    6063                 :            : };
    6064                 :            : 
    6065                 :            : 
    6066                 :            : static int
    6067                 :      20207 : type_add_method(PyTypeObject *type, PyMethodDef *meth)
    6068                 :            : {
    6069                 :            :     PyObject *descr;
    6070                 :      20207 :     int isdescr = 1;
    6071         [ +  + ]:      20207 :     if (meth->ml_flags & METH_CLASS) {
    6072         [ -  + ]:        747 :         if (meth->ml_flags & METH_STATIC) {
    6073                 :          0 :             PyErr_SetString(PyExc_ValueError,
    6074                 :            :                     "method cannot be both class and static");
    6075                 :          0 :             return -1;
    6076                 :            :         }
    6077                 :        747 :         descr = PyDescr_NewClassMethod(type, meth);
    6078                 :            :     }
    6079         [ +  + ]:      19460 :     else if (meth->ml_flags & METH_STATIC) {
    6080                 :        100 :         PyObject *cfunc = PyCFunction_NewEx(meth, (PyObject*)type, NULL);
    6081         [ -  + ]:        100 :         if (cfunc == NULL) {
    6082                 :          0 :             return -1;
    6083                 :            :         }
    6084                 :        100 :         descr = PyStaticMethod_New(cfunc);
    6085                 :        100 :         isdescr = 0;  // PyStaticMethod is not PyDescrObject
    6086                 :        100 :         Py_DECREF(cfunc);
    6087                 :            :     }
    6088                 :            :     else {
    6089                 :      19360 :         descr = PyDescr_NewMethod(type, meth);
    6090                 :            :     }
    6091         [ -  + ]:      20207 :     if (descr == NULL) {
    6092                 :          0 :         return -1;
    6093                 :            :     }
    6094                 :            : 
    6095                 :            :     PyObject *name;
    6096         [ +  + ]:      20207 :     if (isdescr) {
    6097                 :      20107 :         name = PyDescr_NAME(descr);
    6098                 :            :     }
    6099                 :            :     else {
    6100                 :        100 :         name = PyUnicode_FromString(meth->ml_name);
    6101         [ -  + ]:        100 :         if (name == NULL) {
    6102                 :          0 :             Py_DECREF(descr);
    6103                 :          0 :             return -1;
    6104                 :            :         }
    6105                 :            :     }
    6106                 :            : 
    6107                 :            :     int err;
    6108         [ +  + ]:      20207 :     if (!(meth->ml_flags & METH_COEXIST)) {
    6109                 :      20062 :         err = PyDict_SetDefault(type->tp_dict, name, descr) == NULL;
    6110                 :            :     }
    6111                 :            :     else {
    6112                 :        145 :         err = PyDict_SetItem(type->tp_dict, name, descr) < 0;
    6113                 :            :     }
    6114         [ +  + ]:      20207 :     if (!isdescr) {
    6115                 :        100 :         Py_DECREF(name);
    6116                 :            :     }
    6117                 :      20207 :     Py_DECREF(descr);
    6118         [ -  + ]:      20207 :     if (err) {
    6119                 :          0 :         return -1;
    6120                 :            :     }
    6121                 :      20207 :     return 0;
    6122                 :            : }
    6123                 :            : 
    6124                 :            : 
    6125                 :            : /* Add the methods from tp_methods to the __dict__ in a type object */
    6126                 :            : static int
    6127                 :      12908 : type_add_methods(PyTypeObject *type)
    6128                 :            : {
    6129                 :      12908 :     PyMethodDef *meth = type->tp_methods;
    6130         [ +  + ]:      12908 :     if (meth == NULL) {
    6131                 :       9070 :         return 0;
    6132                 :            :     }
    6133                 :            : 
    6134         [ +  + ]:      24045 :     for (; meth->ml_name != NULL; meth++) {
    6135         [ -  + ]:      20207 :         if (type_add_method(type, meth) < 0) {
    6136                 :          0 :             return -1;
    6137                 :            :         }
    6138                 :            :     }
    6139                 :       3838 :     return 0;
    6140                 :            : }
    6141                 :            : 
    6142                 :            : 
    6143                 :            : static int
    6144                 :      12908 : type_add_members(PyTypeObject *type)
    6145                 :            : {
    6146                 :      12908 :     PyMemberDef *memb = type->tp_members;
    6147         [ +  + ]:      12908 :     if (memb == NULL) {
    6148                 :       4424 :         return 0;
    6149                 :            :     }
    6150                 :            : 
    6151                 :       8484 :     PyObject *dict = type->tp_dict;
    6152         [ +  + ]:      16922 :     for (; memb->name != NULL; memb++) {
    6153                 :       8438 :         PyObject *descr = PyDescr_NewMember(type, memb);
    6154         [ -  + ]:       8438 :         if (descr == NULL)
    6155                 :          0 :             return -1;
    6156                 :            : 
    6157         [ -  + ]:       8438 :         if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
    6158                 :          0 :             Py_DECREF(descr);
    6159                 :          0 :             return -1;
    6160                 :            :         }
    6161                 :       8438 :         Py_DECREF(descr);
    6162                 :            :     }
    6163                 :       8484 :     return 0;
    6164                 :            : }
    6165                 :            : 
    6166                 :            : 
    6167                 :            : static int
    6168                 :      12908 : type_add_getset(PyTypeObject *type)
    6169                 :            : {
    6170                 :      12908 :     PyGetSetDef *gsp = type->tp_getset;
    6171         [ +  + ]:      12908 :     if (gsp == NULL) {
    6172                 :       9405 :         return 0;
    6173                 :            :     }
    6174                 :            : 
    6175                 :       3503 :     PyObject *dict = type->tp_dict;
    6176         [ +  + ]:      11275 :     for (; gsp->name != NULL; gsp++) {
    6177                 :       7772 :         PyObject *descr = PyDescr_NewGetSet(type, gsp);
    6178         [ -  + ]:       7772 :         if (descr == NULL) {
    6179                 :          0 :             return -1;
    6180                 :            :         }
    6181                 :            : 
    6182         [ -  + ]:       7772 :         if (PyDict_SetDefault(dict, PyDescr_NAME(descr), descr) == NULL) {
    6183                 :          0 :             Py_DECREF(descr);
    6184                 :          0 :             return -1;
    6185                 :            :         }
    6186                 :       7772 :         Py_DECREF(descr);
    6187                 :            :     }
    6188                 :       3503 :     return 0;
    6189                 :            : }
    6190                 :            : 
    6191                 :            : 
    6192                 :            : static void
    6193                 :      12879 : inherit_special(PyTypeObject *type, PyTypeObject *base)
    6194                 :            : {
    6195                 :            :     /* Copying tp_traverse and tp_clear is connected to the GC flags */
    6196         [ +  + ]:      12879 :     if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
    6197         [ +  + ]:       1195 :         (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
    6198   [ +  -  +  - ]:        211 :         (!type->tp_traverse && !type->tp_clear)) {
    6199                 :        211 :         type->tp_flags |= Py_TPFLAGS_HAVE_GC;
    6200         [ +  - ]:        211 :         if (type->tp_traverse == NULL)
    6201                 :        211 :             type->tp_traverse = base->tp_traverse;
    6202         [ +  - ]:        211 :         if (type->tp_clear == NULL)
    6203                 :        211 :             type->tp_clear = base->tp_clear;
    6204                 :            :     }
    6205                 :      12879 :     type->tp_flags |= (base->tp_flags & Py_TPFLAGS_PREHEADER);
    6206                 :            : 
    6207         [ +  + ]:      12879 :     if (type->tp_basicsize == 0)
    6208                 :        270 :         type->tp_basicsize = base->tp_basicsize;
    6209                 :            : 
    6210                 :            :     /* Copy other non-function slots */
    6211                 :            : 
    6212                 :            : #define COPYVAL(SLOT) \
    6213                 :            :     if (type->SLOT == 0) { type->SLOT = base->SLOT; }
    6214                 :            : 
    6215         [ +  + ]:      12879 :     COPYVAL(tp_itemsize);
    6216         [ +  + ]:      12879 :     COPYVAL(tp_weaklistoffset);
    6217         [ +  + ]:      12879 :     COPYVAL(tp_dictoffset);
    6218                 :            : 
    6219                 :            : #undef COPYVAL
    6220                 :            : 
    6221                 :            :     /* Setup fast subclass flags */
    6222         [ +  + ]:      12879 :     if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException)) {
    6223                 :       2217 :         type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
    6224                 :            :     }
    6225         [ +  + ]:      10662 :     else if (PyType_IsSubtype(base, &PyType_Type)) {
    6226                 :         66 :         type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
    6227                 :            :     }
    6228         [ +  + ]:      10596 :     else if (PyType_IsSubtype(base, &PyLong_Type)) {
    6229                 :         73 :         type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
    6230                 :            :     }
    6231         [ -  + ]:      10523 :     else if (PyType_IsSubtype(base, &PyBytes_Type)) {
    6232                 :          0 :         type->tp_flags |= Py_TPFLAGS_BYTES_SUBCLASS;
    6233                 :            :     }
    6234         [ +  + ]:      10523 :     else if (PyType_IsSubtype(base, &PyUnicode_Type)) {
    6235                 :         18 :         type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
    6236                 :            :     }
    6237         [ +  + ]:      10505 :     else if (PyType_IsSubtype(base, &PyTuple_Type)) {
    6238                 :        585 :         type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
    6239                 :            :     }
    6240         [ +  + ]:       9920 :     else if (PyType_IsSubtype(base, &PyList_Type)) {
    6241                 :         10 :         type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
    6242                 :            :     }
    6243         [ +  + ]:       9910 :     else if (PyType_IsSubtype(base, &PyDict_Type)) {
    6244                 :         52 :         type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
    6245                 :            :     }
    6246         [ +  + ]:      12879 :     if (PyType_HasFeature(base, _Py_TPFLAGS_MATCH_SELF)) {
    6247                 :        741 :         type->tp_flags |= _Py_TPFLAGS_MATCH_SELF;
    6248                 :            :     }
    6249                 :      12879 : }
    6250                 :            : 
    6251                 :            : static int
    6252                 :      12110 : overrides_hash(PyTypeObject *type)
    6253                 :            : {
    6254                 :      12110 :     PyObject *dict = type->tp_dict;
    6255                 :            : 
    6256                 :            :     assert(dict != NULL);
    6257                 :      12110 :     int r = PyDict_Contains(dict, &_Py_ID(__eq__));
    6258         [ +  + ]:      12110 :     if (r == 0) {
    6259                 :      11505 :         r = PyDict_Contains(dict, &_Py_ID(__hash__));
    6260                 :            :     }
    6261                 :      12110 :     return r;
    6262                 :            : }
    6263                 :            : 
    6264                 :            : static int
    6265                 :      29960 : inherit_slots(PyTypeObject *type, PyTypeObject *base)
    6266                 :            : {
    6267                 :            :     PyTypeObject *basebase;
    6268                 :            : 
    6269                 :            : #undef SLOTDEFINED
    6270                 :            : #undef COPYSLOT
    6271                 :            : #undef COPYNUM
    6272                 :            : #undef COPYSEQ
    6273                 :            : #undef COPYMAP
    6274                 :            : #undef COPYBUF
    6275                 :            : 
    6276                 :            : #define SLOTDEFINED(SLOT) \
    6277                 :            :     (base->SLOT != 0 && \
    6278                 :            :      (basebase == NULL || base->SLOT != basebase->SLOT))
    6279                 :            : 
    6280                 :            : #define COPYSLOT(SLOT) \
    6281                 :            :     if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
    6282                 :            : 
    6283                 :            : #define COPYASYNC(SLOT) COPYSLOT(tp_as_async->SLOT)
    6284                 :            : #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
    6285                 :            : #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
    6286                 :            : #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
    6287                 :            : #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
    6288                 :            : 
    6289                 :            :     /* This won't inherit indirect slots (from tp_as_number etc.)
    6290                 :            :        if type doesn't provide the space. */
    6291                 :            : 
    6292   [ +  +  +  + ]:      29960 :     if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
    6293                 :       9525 :         basebase = base->tp_base;
    6294         [ +  + ]:       9525 :         if (basebase->tp_as_number == NULL)
    6295                 :       5561 :             basebase = NULL;
    6296   [ +  +  +  +  :       9525 :         COPYNUM(nb_add);
             +  +  +  + ]
    6297   [ +  +  +  +  :       9525 :         COPYNUM(nb_subtract);
             +  +  +  + ]
    6298   [ +  +  +  +  :       9525 :         COPYNUM(nb_multiply);
             +  +  +  + ]
    6299   [ +  +  +  +  :       9525 :         COPYNUM(nb_remainder);
             +  +  +  + ]
    6300   [ +  +  +  +  :       9525 :         COPYNUM(nb_divmod);
             +  +  +  + ]
    6301   [ +  +  +  +  :       9525 :         COPYNUM(nb_power);
             +  +  +  + ]
    6302   [ +  +  +  +  :       9525 :         COPYNUM(nb_negative);
             +  +  +  + ]
    6303   [ +  +  +  +  :       9525 :         COPYNUM(nb_positive);
             +  +  +  + ]
    6304   [ +  +  +  +  :       9525 :         COPYNUM(nb_absolute);
             +  +  +  + ]
    6305   [ +  +  +  +  :       9525 :         COPYNUM(nb_bool);
             +  +  +  + ]
    6306   [ +  +  +  +  :       9525 :         COPYNUM(nb_invert);
             +  +  +  + ]
    6307   [ +  +  +  +  :       9525 :         COPYNUM(nb_lshift);
             +  +  -  + ]
    6308   [ +  +  +  +  :       9525 :         COPYNUM(nb_rshift);
             +  +  -  + ]
    6309   [ +  +  +  +  :       9525 :         COPYNUM(nb_and);
             +  +  +  + ]
    6310   [ +  +  +  +  :       9525 :         COPYNUM(nb_xor);
             +  +  +  + ]
    6311   [ +  +  +  +  :       9525 :         COPYNUM(nb_or);
             +  +  +  + ]
    6312   [ +  +  +  +  :       9525 :         COPYNUM(nb_int);
             +  +  -  + ]
    6313   [ +  +  +  +  :       9525 :         COPYNUM(nb_float);
             +  +  +  + ]
    6314   [ +  +  +  +  :       9525 :         COPYNUM(nb_inplace_add);
             +  -  +  - ]
    6315   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_subtract);
             -  -  -  - ]
    6316   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_multiply);
             -  -  -  - ]
    6317   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_remainder);
             -  -  -  - ]
    6318   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_power);
             -  -  -  - ]
    6319   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_lshift);
             -  -  -  - ]
    6320   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_rshift);
             -  -  -  - ]
    6321   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_and);
             -  -  -  - ]
    6322   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_xor);
             -  -  -  - ]
    6323   [ +  +  +  +  :       9525 :         COPYNUM(nb_inplace_or);
             +  +  +  - ]
    6324   [ +  +  +  +  :       9525 :         COPYNUM(nb_true_divide);
             +  +  +  + ]
    6325   [ +  +  +  +  :       9525 :         COPYNUM(nb_floor_divide);
             +  +  +  + ]
    6326   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_true_divide);
             -  -  -  - ]
    6327   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_floor_divide);
             -  -  -  - ]
    6328   [ +  +  +  +  :       9525 :         COPYNUM(nb_index);
             +  +  -  + ]
    6329   [ +  -  -  +  :       9525 :         COPYNUM(nb_matrix_multiply);
             -  -  -  - ]
    6330   [ +  -  -  +  :       9525 :         COPYNUM(nb_inplace_matrix_multiply);
             -  -  -  - ]
    6331                 :            :     }
    6332                 :            : 
    6333   [ +  +  +  + ]:      29960 :     if (type->tp_as_async != NULL && base->tp_as_async != NULL) {
    6334                 :       9264 :         basebase = base->tp_base;
    6335         [ +  + ]:       9264 :         if (basebase->tp_as_async == NULL)
    6336                 :       5344 :             basebase = NULL;
    6337   [ +  +  +  +  :       9264 :         COPYASYNC(am_await);
             -  +  -  - ]
    6338   [ +  -  +  +  :       9264 :         COPYASYNC(am_aiter);
             +  +  -  + ]
    6339   [ +  +  +  +  :       9264 :         COPYASYNC(am_anext);
             +  -  +  - ]
    6340                 :            :     }
    6341                 :            : 
    6342   [ +  +  +  + ]:      29960 :     if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
    6343                 :       9662 :         basebase = base->tp_base;
    6344         [ +  + ]:       9662 :         if (basebase->tp_as_sequence == NULL)
    6345                 :       5713 :             basebase = NULL;
    6346   [ +  +  +  +  :       9662 :         COPYSEQ(sq_length);
             +  +  +  + ]
    6347   [ +  +  +  +  :       9662 :         COPYSEQ(sq_concat);
             +  +  -  + ]
    6348   [ +  +  +  +  :       9662 :         COPYSEQ(sq_repeat);
             +  +  -  + ]
    6349   [ +  +  +  +  :       9662 :         COPYSEQ(sq_item);
             +  +  +  + ]
    6350   [ +  +  +  +  :       9662 :         COPYSEQ(sq_ass_item);
             +  +  +  + ]
    6351   [ +  +  +  +  :       9662 :         COPYSEQ(sq_contains);
             +  +  +  + ]
    6352   [ +  -  +  +  :       9662 :         COPYSEQ(sq_inplace_concat);
             -  +  -  - ]
    6353   [ +  -  +  +  :       9662 :         COPYSEQ(sq_inplace_repeat);
             -  +  -  - ]
    6354                 :            :     }
    6355                 :            : 
    6356   [ +  +  +  + ]:      29960 :     if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
    6357                 :       9716 :         basebase = base->tp_base;
    6358         [ +  + ]:       9716 :         if (basebase->tp_as_mapping == NULL)
    6359                 :       5767 :             basebase = NULL;
    6360   [ +  +  +  +  :       9716 :         COPYMAP(mp_length);
             +  +  +  + ]
    6361   [ +  +  +  +  :       9716 :         COPYMAP(mp_subscript);
             +  +  +  + ]
    6362   [ +  +  +  +  :       9716 :         COPYMAP(mp_ass_subscript);
             +  +  +  + ]
    6363                 :            :     }
    6364                 :            : 
    6365   [ +  +  +  + ]:      29960 :     if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
    6366                 :       9352 :         basebase = base->tp_base;
    6367         [ +  + ]:       9352 :         if (basebase->tp_as_buffer == NULL)
    6368                 :       5394 :             basebase = NULL;
    6369   [ +  +  +  +  :       9352 :         COPYBUF(bf_getbuffer);
             +  +  -  + ]
    6370   [ +  -  -  +  :       9352 :         COPYBUF(bf_releasebuffer);
             -  -  -  - ]
    6371                 :            :     }
    6372                 :            : 
    6373                 :      29960 :     basebase = base->tp_base;
    6374                 :            : 
    6375   [ +  +  +  -  :      29960 :     COPYSLOT(tp_dealloc);
             +  +  +  + ]
    6376   [ +  -  +  + ]:      29960 :     if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
    6377                 :      10050 :         type->tp_getattr = base->tp_getattr;
    6378                 :      10050 :         type->tp_getattro = base->tp_getattro;
    6379                 :            :     }
    6380   [ +  +  +  + ]:      29960 :     if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
    6381                 :      12539 :         type->tp_setattr = base->tp_setattr;
    6382                 :      12539 :         type->tp_setattro = base->tp_setattro;
    6383                 :            :     }
    6384   [ +  +  +  -  :      29960 :     COPYSLOT(tp_repr);
             +  +  +  + ]
    6385                 :            :     /* tp_hash see tp_richcompare */
    6386                 :            :     {
    6387                 :            :         /* Always inherit tp_vectorcall_offset to support PyVectorcall_Call().
    6388                 :            :          * If Py_TPFLAGS_HAVE_VECTORCALL is not inherited, then vectorcall
    6389                 :            :          * won't be used automatically. */
    6390   [ +  +  +  +  :      29960 :         COPYSLOT(tp_vectorcall_offset);
             +  -  +  + ]
    6391                 :            : 
    6392                 :            :         /* Inherit Py_TPFLAGS_HAVE_VECTORCALL if tp_call is not overridden */
    6393   [ +  +  +  + ]:      59143 :         if (!type->tp_call &&
    6394                 :      29183 :             _PyType_HasFeature(base, Py_TPFLAGS_HAVE_VECTORCALL))
    6395                 :            :         {
    6396                 :        133 :             type->tp_flags |= Py_TPFLAGS_HAVE_VECTORCALL;
    6397                 :            :         }
    6398   [ +  +  +  +  :      29960 :         COPYSLOT(tp_call);
             +  -  +  + ]
    6399                 :            :     }
    6400   [ +  +  +  -  :      29960 :     COPYSLOT(tp_str);
             +  +  +  + ]
    6401                 :            :     {
    6402                 :            :         /* Copy comparison-related slots only when
    6403                 :            :            not overriding them anywhere */
    6404         [ +  + ]:      29960 :         if (type->tp_richcompare == NULL &&
    6405         [ +  + ]:      12376 :             type->tp_hash == NULL)
    6406                 :            :         {
    6407                 :      12110 :             int r = overrides_hash(type);
    6408         [ -  + ]:      12110 :             if (r < 0) {
    6409                 :          0 :                 return -1;
    6410                 :            :             }
    6411         [ +  + ]:      12110 :             if (!r) {
    6412                 :      11464 :                 type->tp_richcompare = base->tp_richcompare;
    6413                 :      11464 :                 type->tp_hash = base->tp_hash;
    6414                 :            :             }
    6415                 :            :         }
    6416                 :            :     }
    6417                 :            :     {
    6418   [ +  +  +  +  :      29960 :         COPYSLOT(tp_iter);
             +  -  +  + ]
    6419   [ +  +  +  +  :      29960 :         COPYSLOT(tp_iternext);
             +  -  +  + ]
    6420                 :            :     }
    6421                 :            :     {
    6422   [ +  +  +  +  :      29960 :         COPYSLOT(tp_descr_get);
             +  -  +  - ]
    6423                 :            :         /* Inherit Py_TPFLAGS_METHOD_DESCRIPTOR if tp_descr_get was inherited,
    6424                 :            :          * but only for extension types */
    6425         [ +  + ]:      29960 :         if (base->tp_descr_get &&
    6426   [ +  +  +  + ]:        168 :             type->tp_descr_get == base->tp_descr_get &&
    6427         [ +  - ]:         87 :             _PyType_HasFeature(type, Py_TPFLAGS_IMMUTABLETYPE) &&
    6428                 :          4 :             _PyType_HasFeature(base, Py_TPFLAGS_METHOD_DESCRIPTOR))
    6429                 :            :         {
    6430                 :          4 :             type->tp_flags |= Py_TPFLAGS_METHOD_DESCRIPTOR;
    6431                 :            :         }
    6432   [ +  +  +  +  :      29960 :         COPYSLOT(tp_descr_set);
             +  -  +  - ]
    6433   [ +  +  -  +  :      29960 :         COPYSLOT(tp_dictoffset);
             -  -  -  - ]
    6434   [ +  +  +  -  :      29960 :         COPYSLOT(tp_init);
             +  +  +  + ]
    6435   [ +  +  +  -  :      29960 :         COPYSLOT(tp_alloc);
             +  +  +  + ]
    6436   [ +  +  +  +  :      29960 :         COPYSLOT(tp_is_gc);
             +  -  +  + ]
    6437   [ +  +  +  +  :      29960 :         COPYSLOT(tp_finalize);
             +  -  +  + ]
    6438                 :      29960 :         if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
    6439         [ +  + ]:      29960 :             (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
    6440                 :            :             /* They agree about gc. */
    6441   [ +  +  +  -  :      17920 :             COPYSLOT(tp_free);
             +  +  +  + ]
    6442                 :            :         }
    6443         [ +  - ]:      12040 :         else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
    6444         [ +  + ]:      12040 :                  type->tp_free == NULL &&
    6445         [ +  - ]:       1911 :                  base->tp_free == PyObject_Free) {
    6446                 :            :             /* A bit of magic to plug in the correct default
    6447                 :            :              * tp_free function when a derived class adds gc,
    6448                 :            :              * didn't define tp_free, and the base uses the
    6449                 :            :              * default non-gc tp_free.
    6450                 :            :              */
    6451                 :       1911 :             type->tp_free = PyObject_GC_Del;
    6452                 :            :         }
    6453                 :            :         /* else they didn't agree about gc, and there isn't something
    6454                 :            :          * obvious to be done -- the type is on its own.
    6455                 :            :          */
    6456                 :            :     }
    6457                 :      29960 :     return 0;
    6458                 :            : }
    6459                 :            : 
    6460                 :            : static int add_operators(PyTypeObject *);
    6461                 :            : static int add_tp_new_wrapper(PyTypeObject *type);
    6462                 :            : 
    6463                 :            : #define COLLECTION_FLAGS (Py_TPFLAGS_SEQUENCE | Py_TPFLAGS_MAPPING)
    6464                 :            : 
    6465                 :            : static int
    6466                 :      12908 : type_ready_pre_checks(PyTypeObject *type)
    6467                 :            : {
    6468                 :            :     /* Consistency checks for PEP 590:
    6469                 :            :      * - Py_TPFLAGS_METHOD_DESCRIPTOR requires tp_descr_get
    6470                 :            :      * - Py_TPFLAGS_HAVE_VECTORCALL requires tp_call and
    6471                 :            :      *   tp_vectorcall_offset > 0
    6472                 :            :      * To avoid mistakes, we require this before inheriting.
    6473                 :            :      */
    6474                 :      12908 :     if (type->tp_flags & Py_TPFLAGS_METHOD_DESCRIPTOR) {
    6475                 :            :         _PyObject_ASSERT((PyObject *)type, type->tp_descr_get != NULL);
    6476                 :            :     }
    6477                 :      12908 :     if (type->tp_flags & Py_TPFLAGS_HAVE_VECTORCALL) {
    6478                 :            :         _PyObject_ASSERT((PyObject *)type, type->tp_vectorcall_offset > 0);
    6479                 :            :         _PyObject_ASSERT((PyObject *)type, type->tp_call != NULL);
    6480                 :            :     }
    6481                 :            : 
    6482                 :            :     /* Consistency checks for pattern matching
    6483                 :            :      * Py_TPFLAGS_SEQUENCE and Py_TPFLAGS_MAPPING are mutually exclusive */
    6484                 :            :     _PyObject_ASSERT((PyObject *)type, (type->tp_flags & COLLECTION_FLAGS) != COLLECTION_FLAGS);
    6485                 :            : 
    6486         [ -  + ]:      12908 :     if (type->tp_name == NULL) {
    6487                 :          0 :         PyErr_Format(PyExc_SystemError,
    6488                 :            :                      "Type does not define the tp_name field.");
    6489                 :          0 :         return -1;
    6490                 :            :     }
    6491                 :      12908 :     return 0;
    6492                 :            : }
    6493                 :            : 
    6494                 :            : 
    6495                 :            : static int
    6496                 :      12908 : type_ready_set_bases(PyTypeObject *type)
    6497                 :            : {
    6498                 :            :     /* Initialize tp_base (defaults to BaseObject unless that's us) */
    6499                 :      12908 :     PyTypeObject *base = type->tp_base;
    6500   [ +  +  +  + ]:      12908 :     if (base == NULL && type != &PyBaseObject_Type) {
    6501                 :       3187 :         base = &PyBaseObject_Type;
    6502         [ -  + ]:       3187 :         if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
    6503                 :          0 :             type->tp_base = (PyTypeObject*)Py_NewRef((PyObject*)base);
    6504                 :            :         }
    6505                 :            :         else {
    6506                 :       3187 :             type->tp_base = base;
    6507                 :            :         }
    6508                 :            :     }
    6509                 :            :     assert(type->tp_base != NULL || type == &PyBaseObject_Type);
    6510                 :            : 
    6511                 :            :     /* Now the only way base can still be NULL is if type is
    6512                 :            :      * &PyBaseObject_Type. */
    6513                 :            : 
    6514                 :            :     /* Initialize the base class */
    6515   [ +  +  -  + ]:      12908 :     if (base != NULL && !_PyType_IsReady(base)) {
    6516         [ #  # ]:          0 :         if (PyType_Ready(base) < 0) {
    6517                 :          0 :             return -1;
    6518                 :            :         }
    6519                 :            :     }
    6520                 :            : 
    6521                 :            :     /* Initialize ob_type if NULL.      This means extensions that want to be
    6522                 :            :        compilable separately on Windows can call PyType_Ready() instead of
    6523                 :            :        initializing the ob_type field of their type objects. */
    6524                 :            :     /* The test for base != NULL is really unnecessary, since base is only
    6525                 :            :        NULL when type is &PyBaseObject_Type, and we know its ob_type is
    6526                 :            :        not NULL (it's initialized to &PyType_Type).      But coverity doesn't
    6527                 :            :        know that. */
    6528   [ +  +  +  - ]:      12908 :     if (Py_IS_TYPE(type, NULL) && base != NULL) {
    6529                 :       2547 :         Py_SET_TYPE(type, Py_TYPE(base));
    6530                 :            :     }
    6531                 :            : 
    6532                 :            :     /* Initialize tp_bases */
    6533                 :      12908 :     PyObject *bases = type->tp_bases;
    6534         [ +  + ]:      12908 :     if (bases == NULL) {
    6535                 :       5636 :         PyTypeObject *base = type->tp_base;
    6536         [ +  + ]:       5636 :         if (base == NULL) {
    6537                 :         29 :             bases = PyTuple_New(0);
    6538                 :            :         }
    6539                 :            :         else {
    6540                 :       5607 :             bases = PyTuple_Pack(1, base);
    6541                 :            :         }
    6542         [ -  + ]:       5636 :         if (bases == NULL) {
    6543                 :          0 :             return -1;
    6544                 :            :         }
    6545                 :       5636 :         type->tp_bases = bases;
    6546                 :            :     }
    6547                 :      12908 :     return 0;
    6548                 :            : }
    6549                 :            : 
    6550                 :            : 
    6551                 :            : static int
    6552                 :      12908 : type_ready_set_dict(PyTypeObject *type)
    6553                 :            : {
    6554         [ +  + ]:      12908 :     if (type->tp_dict != NULL) {
    6555                 :       6260 :         return 0;
    6556                 :            :     }
    6557                 :            : 
    6558                 :       6648 :     PyObject *dict = PyDict_New();
    6559         [ -  + ]:       6648 :     if (dict == NULL) {
    6560                 :          0 :         return -1;
    6561                 :            :     }
    6562                 :       6648 :     type->tp_dict = dict;
    6563                 :       6648 :     return 0;
    6564                 :            : }
    6565                 :            : 
    6566                 :            : 
    6567                 :            : /* If the type dictionary doesn't contain a __doc__, set it from
    6568                 :            :    the tp_doc slot. */
    6569                 :            : static int
    6570                 :      12908 : type_dict_set_doc(PyTypeObject *type)
    6571                 :            : {
    6572                 :      12908 :     int r = PyDict_Contains(type->tp_dict, &_Py_ID(__doc__));
    6573         [ -  + ]:      12908 :     if (r < 0) {
    6574                 :          0 :         return -1;
    6575                 :            :     }
    6576         [ +  + ]:      12908 :     if (r > 0) {
    6577                 :       5121 :         return 0;
    6578                 :            :     }
    6579                 :            : 
    6580         [ +  + ]:       7787 :     if (type->tp_doc != NULL) {
    6581                 :            :         const char *doc_str;
    6582                 :       4185 :         doc_str = _PyType_DocWithoutSignature(type->tp_name, type->tp_doc);
    6583                 :       4185 :         PyObject *doc = PyUnicode_FromString(doc_str);
    6584         [ -  + ]:       4185 :         if (doc == NULL) {
    6585                 :          0 :             return -1;
    6586                 :            :         }
    6587                 :            : 
    6588         [ -  + ]:       4185 :         if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), doc) < 0) {
    6589                 :          0 :             Py_DECREF(doc);
    6590                 :          0 :             return -1;
    6591                 :            :         }
    6592                 :       4185 :         Py_DECREF(doc);
    6593                 :            :     }
    6594                 :            :     else {
    6595         [ -  + ]:       3602 :         if (PyDict_SetItem(type->tp_dict, &_Py_ID(__doc__), Py_None) < 0) {
    6596                 :          0 :             return -1;
    6597                 :            :         }
    6598                 :            :     }
    6599                 :       7787 :     return 0;
    6600                 :            : }
    6601                 :            : 
    6602                 :            : 
    6603                 :            : static int
    6604                 :      12908 : type_ready_fill_dict(PyTypeObject *type)
    6605                 :            : {
    6606                 :            :     /* Add type-specific descriptors to tp_dict */
    6607         [ -  + ]:      12908 :     if (add_operators(type) < 0) {
    6608                 :          0 :         return -1;
    6609                 :            :     }
    6610         [ -  + ]:      12908 :     if (type_add_methods(type) < 0) {
    6611                 :          0 :         return -1;
    6612                 :            :     }
    6613         [ -  + ]:      12908 :     if (type_add_members(type) < 0) {
    6614                 :          0 :         return -1;
    6615                 :            :     }
    6616         [ -  + ]:      12908 :     if (type_add_getset(type) < 0) {
    6617                 :          0 :         return -1;
    6618                 :            :     }
    6619         [ -  + ]:      12908 :     if (type_dict_set_doc(type) < 0) {
    6620                 :          0 :         return -1;
    6621                 :            :     }
    6622                 :      12908 :     return 0;
    6623                 :            : }
    6624                 :            : 
    6625                 :            : static int
    6626                 :      12908 : type_ready_preheader(PyTypeObject *type)
    6627                 :            : {
    6628         [ +  + ]:      12908 :     if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    6629   [ +  -  -  + ]:       1941 :         if (type->tp_dictoffset > 0 || type->tp_dictoffset < -1) {
    6630                 :          0 :             PyErr_Format(PyExc_TypeError,
    6631                 :            :                         "type %s has the Py_TPFLAGS_MANAGED_DICT flag "
    6632                 :            :                         "but tp_dictoffset is set",
    6633                 :            :                         type->tp_name);
    6634                 :          0 :             return -1;
    6635                 :            :         }
    6636                 :       1941 :         type->tp_dictoffset = -1;
    6637                 :            :     }
    6638         [ +  + ]:      12908 :     if (type->tp_flags & Py_TPFLAGS_MANAGED_WEAKREF) {
    6639         [ +  + ]:       5134 :         if (type->tp_weaklistoffset != 0 &&
    6640         [ -  + ]:       5132 :             type->tp_weaklistoffset != MANAGED_WEAKREF_OFFSET)
    6641                 :            :         {
    6642                 :          0 :             PyErr_Format(PyExc_TypeError,
    6643                 :            :                         "type %s has the Py_TPFLAGS_MANAGED_WEAKREF flag "
    6644                 :            :                         "but tp_weaklistoffset is set",
    6645                 :            :                         type->tp_name);
    6646                 :          0 :             return -1;
    6647                 :            :         }
    6648                 :       5134 :         type->tp_weaklistoffset = MANAGED_WEAKREF_OFFSET;
    6649                 :            :     }
    6650                 :      12908 :     return 0;
    6651                 :            : }
    6652                 :            : 
    6653                 :            : static int
    6654                 :      12908 : type_ready_mro(PyTypeObject *type)
    6655                 :            : {
    6656                 :            :     /* Calculate method resolution order */
    6657         [ -  + ]:      12908 :     if (mro_internal(type, NULL) < 0) {
    6658                 :          0 :         return -1;
    6659                 :            :     }
    6660                 :            :     assert(type->tp_mro != NULL);
    6661                 :            :     assert(PyTuple_Check(type->tp_mro));
    6662                 :            : 
    6663                 :            :     /* All bases of statically allocated type should be statically allocated */
    6664         [ +  + ]:      12908 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    6665                 :       5636 :         PyObject *mro = type->tp_mro;
    6666                 :       5636 :         Py_ssize_t n = PyTuple_GET_SIZE(mro);
    6667         [ +  + ]:      22454 :         for (Py_ssize_t i = 0; i < n; i++) {
    6668                 :      16818 :             PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(mro, i));
    6669         [ -  + ]:      16818 :             if (base->tp_flags & Py_TPFLAGS_HEAPTYPE) {
    6670                 :          0 :                 PyErr_Format(PyExc_TypeError,
    6671                 :            :                              "type '%.100s' is not dynamically allocated but "
    6672                 :            :                              "its base type '%.100s' is dynamically allocated",
    6673                 :            :                              type->tp_name, base->tp_name);
    6674                 :          0 :                 return -1;
    6675                 :            :             }
    6676                 :            :         }
    6677                 :            :     }
    6678                 :      12908 :     return 0;
    6679                 :            : }
    6680                 :            : 
    6681                 :            : 
    6682                 :            : // For static types, inherit tp_as_xxx structures from the base class
    6683                 :            : // if it's NULL.
    6684                 :            : //
    6685                 :            : // For heap types, tp_as_xxx structures are not NULL: they are set to the
    6686                 :            : // PyHeapTypeObject.as_xxx fields by type_new_alloc().
    6687                 :            : static void
    6688                 :      12879 : type_ready_inherit_as_structs(PyTypeObject *type, PyTypeObject *base)
    6689                 :            : {
    6690         [ +  + ]:      12879 :     if (type->tp_as_async == NULL) {
    6691                 :       5431 :         type->tp_as_async = base->tp_as_async;
    6692                 :            :     }
    6693         [ +  + ]:      12879 :     if (type->tp_as_number == NULL) {
    6694                 :       4920 :         type->tp_as_number = base->tp_as_number;
    6695                 :            :     }
    6696         [ +  + ]:      12879 :     if (type->tp_as_sequence == NULL) {
    6697                 :       5064 :         type->tp_as_sequence = base->tp_as_sequence;
    6698                 :            :     }
    6699         [ +  + ]:      12879 :     if (type->tp_as_mapping == NULL) {
    6700                 :       5050 :         type->tp_as_mapping = base->tp_as_mapping;
    6701                 :            :     }
    6702         [ +  + ]:      12879 :     if (type->tp_as_buffer == NULL) {
    6703                 :       5452 :         type->tp_as_buffer = base->tp_as_buffer;
    6704                 :            :     }
    6705                 :      12879 : }
    6706                 :            : 
    6707                 :            : static void
    6708                 :      29960 : inherit_patma_flags(PyTypeObject *type, PyTypeObject *base) {
    6709         [ +  + ]:      29960 :     if ((type->tp_flags & COLLECTION_FLAGS) == 0) {
    6710                 :      28323 :         type->tp_flags |= base->tp_flags & COLLECTION_FLAGS;
    6711                 :            :     }
    6712                 :      29960 : }
    6713                 :            : 
    6714                 :            : static int
    6715                 :      12908 : type_ready_inherit(PyTypeObject *type)
    6716                 :            : {
    6717                 :            :     /* Inherit special flags from dominant base */
    6718                 :      12908 :     PyTypeObject *base = type->tp_base;
    6719         [ +  + ]:      12908 :     if (base != NULL) {
    6720                 :      12879 :         inherit_special(type, base);
    6721                 :            :     }
    6722                 :            : 
    6723                 :            :     // Inherit slots
    6724                 :      12908 :     PyObject *mro = type->tp_mro;
    6725                 :      12908 :     Py_ssize_t n = PyTuple_GET_SIZE(type->tp_mro);
    6726         [ +  + ]:      42868 :     for (Py_ssize_t i = 1; i < n; i++) {
    6727                 :      29960 :         PyObject *b = PyTuple_GET_ITEM(mro, i);
    6728         [ +  - ]:      29960 :         if (PyType_Check(b)) {
    6729         [ -  + ]:      29960 :             if (inherit_slots(type, (PyTypeObject *)b) < 0) {
    6730                 :          0 :                 return -1;
    6731                 :            :             }
    6732                 :      29960 :             inherit_patma_flags(type, (PyTypeObject *)b);
    6733                 :            :         }
    6734                 :            :     }
    6735                 :            : 
    6736         [ +  + ]:      12908 :     if (base != NULL) {
    6737                 :      12879 :         type_ready_inherit_as_structs(type, base);
    6738                 :            :     }
    6739                 :            : 
    6740                 :            :     /* Sanity check for tp_free. */
    6741   [ +  +  +  + ]:      12908 :     if (_PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
    6742   [ +  -  -  + ]:       9328 :         (type->tp_free == NULL || type->tp_free == PyObject_Del))
    6743                 :            :     {
    6744                 :            :         /* This base class needs to call tp_free, but doesn't have
    6745                 :            :          * one, or its tp_free is for non-gc'ed objects.
    6746                 :            :          */
    6747                 :          0 :         PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
    6748                 :            :                      "gc and is a base type but has inappropriate "
    6749                 :            :                      "tp_free slot",
    6750                 :            :                      type->tp_name);
    6751                 :          0 :         return -1;
    6752                 :            :     }
    6753                 :            : 
    6754                 :      12908 :     return 0;
    6755                 :            : }
    6756                 :            : 
    6757                 :            : 
    6758                 :            : /* Hack for tp_hash and __hash__.
    6759                 :            :    If after all that, tp_hash is still NULL, and __hash__ is not in
    6760                 :            :    tp_dict, set tp_hash to PyObject_HashNotImplemented and
    6761                 :            :    tp_dict['__hash__'] equal to None.
    6762                 :            :    This signals that __hash__ is not inherited. */
    6763                 :            : static int
    6764                 :      12908 : type_ready_set_hash(PyTypeObject *type)
    6765                 :            : {
    6766         [ +  + ]:      12908 :     if (type->tp_hash != NULL) {
    6767                 :      12390 :         return 0;
    6768                 :            :     }
    6769                 :            : 
    6770                 :        518 :     int r = PyDict_Contains(type->tp_dict, &_Py_ID(__hash__));
    6771         [ -  + ]:        518 :     if (r < 0) {
    6772                 :          0 :         return -1;
    6773                 :            :     }
    6774         [ +  + ]:        518 :     if (r > 0) {
    6775                 :        129 :         return 0;
    6776                 :            :     }
    6777                 :            : 
    6778         [ -  + ]:        389 :     if (PyDict_SetItem(type->tp_dict, &_Py_ID(__hash__), Py_None) < 0) {
    6779                 :          0 :         return -1;
    6780                 :            :     }
    6781                 :        389 :     type->tp_hash = PyObject_HashNotImplemented;
    6782                 :        389 :     return 0;
    6783                 :            : }
    6784                 :            : 
    6785                 :            : 
    6786                 :            : /* Link into each base class's list of subclasses */
    6787                 :            : static int
    6788                 :      12908 : type_ready_add_subclasses(PyTypeObject *type)
    6789                 :            : {
    6790                 :      12908 :     PyObject *bases = type->tp_bases;
    6791                 :      12908 :     Py_ssize_t nbase = PyTuple_GET_SIZE(bases);
    6792         [ +  + ]:      26363 :     for (Py_ssize_t i = 0; i < nbase; i++) {
    6793                 :      13455 :         PyObject *b = PyTuple_GET_ITEM(bases, i);
    6794   [ +  -  -  + ]:      13455 :         if (PyType_Check(b) && add_subclass((PyTypeObject *)b, type) < 0) {
    6795                 :          0 :             return -1;
    6796                 :            :         }
    6797                 :            :     }
    6798                 :      12908 :     return 0;
    6799                 :            : }
    6800                 :            : 
    6801                 :            : 
    6802                 :            : // Set tp_new and the "__new__" key in the type dictionary.
    6803                 :            : // Use the Py_TPFLAGS_DISALLOW_INSTANTIATION flag.
    6804                 :            : static int
    6805                 :      12908 : type_ready_set_new(PyTypeObject *type)
    6806                 :            : {
    6807                 :      12908 :     PyTypeObject *base = type->tp_base;
    6808                 :            :     /* The condition below could use some explanation.
    6809                 :            : 
    6810                 :            :        It appears that tp_new is not inherited for static types whose base
    6811                 :            :        class is 'object'; this seems to be a precaution so that old extension
    6812                 :            :        types don't suddenly become callable (object.__new__ wouldn't insure the
    6813                 :            :        invariants that the extension type's own factory function ensures).
    6814                 :            : 
    6815                 :            :        Heap types, of course, are under our control, so they do inherit tp_new;
    6816                 :            :        static extension types that specify some other built-in type as the
    6817                 :            :        default also inherit object.__new__. */
    6818         [ +  + ]:      12908 :     if (type->tp_new == NULL
    6819         [ +  + ]:       9393 :         && base == &PyBaseObject_Type
    6820         [ +  + ]:       3244 :         && !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
    6821                 :            :     {
    6822                 :       1787 :         type->tp_flags |= Py_TPFLAGS_DISALLOW_INSTANTIATION;
    6823                 :            :     }
    6824                 :            : 
    6825         [ +  + ]:      12908 :     if (!(type->tp_flags & Py_TPFLAGS_DISALLOW_INSTANTIATION)) {
    6826         [ +  + ]:      10886 :         if (type->tp_new != NULL) {
    6827                 :            :             // If "__new__" key does not exists in the type dictionary,
    6828                 :            :             // set it to tp_new_wrapper().
    6829         [ -  + ]:       3456 :             if (add_tp_new_wrapper(type) < 0) {
    6830                 :          0 :                 return -1;
    6831                 :            :             }
    6832                 :            :         }
    6833                 :            :         else {
    6834                 :            :             // tp_new is NULL: inherit tp_new from base
    6835                 :       7430 :             type->tp_new = base->tp_new;
    6836                 :            :         }
    6837                 :            :     }
    6838                 :            :     else {
    6839                 :            :         // Py_TPFLAGS_DISALLOW_INSTANTIATION sets tp_new to NULL
    6840                 :       2022 :         type->tp_new = NULL;
    6841                 :            :     }
    6842                 :      12908 :     return 0;
    6843                 :            : }
    6844                 :            : 
    6845                 :            : static int
    6846                 :      12908 : type_ready_managed_dict(PyTypeObject *type)
    6847                 :            : {
    6848         [ +  + ]:      12908 :     if (!(type->tp_flags & Py_TPFLAGS_MANAGED_DICT)) {
    6849                 :      10967 :         return 0;
    6850                 :            :     }
    6851         [ -  + ]:       1941 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    6852                 :          0 :         PyErr_Format(PyExc_SystemError,
    6853                 :            :                      "type %s has the Py_TPFLAGS_MANAGED_DICT flag "
    6854                 :            :                      "but not Py_TPFLAGS_HEAPTYPE flag",
    6855                 :            :                      type->tp_name);
    6856                 :          0 :         return -1;
    6857                 :            :     }
    6858                 :       1941 :     PyHeapTypeObject* et = (PyHeapTypeObject*)type;
    6859         [ +  - ]:       1941 :     if (et->ht_cached_keys == NULL) {
    6860                 :       1941 :         et->ht_cached_keys = _PyDict_NewKeysForClass();
    6861         [ -  + ]:       1941 :         if (et->ht_cached_keys == NULL) {
    6862                 :          0 :             PyErr_NoMemory();
    6863                 :          0 :             return -1;
    6864                 :            :         }
    6865                 :            :     }
    6866                 :       1941 :     return 0;
    6867                 :            : }
    6868                 :            : 
    6869                 :            : static int
    6870                 :      12908 : type_ready_post_checks(PyTypeObject *type)
    6871                 :            : {
    6872                 :            :     // bpo-44263: tp_traverse is required if Py_TPFLAGS_HAVE_GC is set.
    6873                 :            :     // Note: tp_clear is optional.
    6874         [ +  + ]:      12908 :     if (type->tp_flags & Py_TPFLAGS_HAVE_GC
    6875         [ -  + ]:      11895 :         && type->tp_traverse == NULL)
    6876                 :            :     {
    6877                 :          0 :         PyErr_Format(PyExc_SystemError,
    6878                 :            :                      "type %s has the Py_TPFLAGS_HAVE_GC flag "
    6879                 :            :                      "but has no traverse function",
    6880                 :            :                      type->tp_name);
    6881                 :          0 :         return -1;
    6882                 :            :     }
    6883         [ +  + ]:      12908 :     if (type->tp_flags & Py_TPFLAGS_MANAGED_DICT) {
    6884         [ -  + ]:       1941 :         if (type->tp_dictoffset != -1) {
    6885                 :          0 :             PyErr_Format(PyExc_SystemError,
    6886                 :            :                         "type %s has the Py_TPFLAGS_MANAGED_DICT flag "
    6887                 :            :                         "but tp_dictoffset is set to incompatible value",
    6888                 :            :                         type->tp_name);
    6889                 :          0 :             return -1;
    6890                 :            :         }
    6891                 :            :     }
    6892         [ +  + ]:      10967 :     else if (type->tp_dictoffset < (Py_ssize_t)sizeof(PyObject)) {
    6893         [ -  + ]:       5055 :         if (type->tp_dictoffset + type->tp_basicsize <= 0) {
    6894                 :          0 :             PyErr_Format(PyExc_SystemError,
    6895                 :            :                          "type %s has a tp_dictoffset that is too small");
    6896                 :            :         }
    6897                 :            :     }
    6898                 :      12908 :     return 0;
    6899                 :            : }
    6900                 :            : 
    6901                 :            : 
    6902                 :            : static int
    6903                 :      12908 : type_ready(PyTypeObject *type)
    6904                 :            : {
    6905         [ -  + ]:      12908 :     if (type_ready_pre_checks(type) < 0) {
    6906                 :          0 :         return -1;
    6907                 :            :     }
    6908                 :            : 
    6909                 :            : #ifdef Py_TRACE_REFS
    6910                 :            :     /* PyType_Ready is the closest thing we have to a choke point
    6911                 :            :      * for type objects, so is the best place I can think of to try
    6912                 :            :      * to get type objects into the doubly-linked list of all objects.
    6913                 :            :      * Still, not all type objects go through PyType_Ready.
    6914                 :            :      */
    6915                 :            :     _Py_AddToAllObjects((PyObject *)type, 0);
    6916                 :            : #endif
    6917                 :            : 
    6918                 :            :     /* Initialize tp_dict: _PyType_IsReady() tests if tp_dict != NULL */
    6919         [ -  + ]:      12908 :     if (type_ready_set_dict(type) < 0) {
    6920                 :          0 :         return -1;
    6921                 :            :     }
    6922         [ -  + ]:      12908 :     if (type_ready_set_bases(type) < 0) {
    6923                 :          0 :         return -1;
    6924                 :            :     }
    6925         [ -  + ]:      12908 :     if (type_ready_mro(type) < 0) {
    6926                 :          0 :         return -1;
    6927                 :            :     }
    6928         [ -  + ]:      12908 :     if (type_ready_set_new(type) < 0) {
    6929                 :          0 :         return -1;
    6930                 :            :     }
    6931         [ -  + ]:      12908 :     if (type_ready_fill_dict(type) < 0) {
    6932                 :          0 :         return -1;
    6933                 :            :     }
    6934         [ -  + ]:      12908 :     if (type_ready_inherit(type) < 0) {
    6935                 :          0 :         return -1;
    6936                 :            :     }
    6937         [ -  + ]:      12908 :     if (type_ready_preheader(type) < 0) {
    6938                 :          0 :         return -1;
    6939                 :            :     }
    6940         [ -  + ]:      12908 :     if (type_ready_set_hash(type) < 0) {
    6941                 :          0 :         return -1;
    6942                 :            :     }
    6943         [ -  + ]:      12908 :     if (type_ready_add_subclasses(type) < 0) {
    6944                 :          0 :         return -1;
    6945                 :            :     }
    6946         [ -  + ]:      12908 :     if (type_ready_managed_dict(type) < 0) {
    6947                 :          0 :         return -1;
    6948                 :            :     }
    6949         [ -  + ]:      12908 :     if (type_ready_post_checks(type) < 0) {
    6950                 :          0 :         return -1;
    6951                 :            :     }
    6952                 :      12908 :     return 0;
    6953                 :            : }
    6954                 :            : 
    6955                 :            : 
    6956                 :            : int
    6957                 :      13939 : PyType_Ready(PyTypeObject *type)
    6958                 :            : {
    6959         [ +  + ]:      13939 :     if (type->tp_flags & Py_TPFLAGS_READY) {
    6960                 :            :         assert(_PyType_CheckConsistency(type));
    6961                 :       1031 :         return 0;
    6962                 :            :     }
    6963                 :            :     _PyObject_ASSERT((PyObject *)type,
    6964                 :            :                      (type->tp_flags & Py_TPFLAGS_READYING) == 0);
    6965                 :            : 
    6966                 :      12908 :     type->tp_flags |= Py_TPFLAGS_READYING;
    6967                 :            : 
    6968                 :            :     /* Historically, all static types were immutable. See bpo-43908 */
    6969         [ +  + ]:      12908 :     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
    6970                 :       5636 :         type->tp_flags |= Py_TPFLAGS_IMMUTABLETYPE;
    6971                 :            :     }
    6972                 :            : 
    6973         [ -  + ]:      12908 :     if (type_ready(type) < 0) {
    6974                 :          0 :         type->tp_flags &= ~Py_TPFLAGS_READYING;
    6975                 :          0 :         return -1;
    6976                 :            :     }
    6977                 :            : 
    6978                 :            :     /* All done -- set the ready flag */
    6979                 :      12908 :     type->tp_flags = (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
    6980                 :            :     assert(_PyType_CheckConsistency(type));
    6981                 :      12908 :     return 0;
    6982                 :            : }
    6983                 :            : 
    6984                 :            : int
    6985                 :       5365 : _PyStaticType_InitBuiltin(PyTypeObject *self)
    6986                 :            : {
    6987                 :       5365 :     self->tp_flags = self->tp_flags | _Py_TPFLAGS_STATIC_BUILTIN;
    6988                 :            : 
    6989                 :       5365 :     static_builtin_state_init(self);
    6990                 :            : 
    6991                 :       5365 :     int res = PyType_Ready(self);
    6992         [ -  + ]:       5365 :     if (res < 0) {
    6993                 :          0 :         static_builtin_state_clear(self);
    6994                 :            :     }
    6995                 :       5365 :     return res;
    6996                 :            : }
    6997                 :            : 
    6998                 :            : 
    6999                 :            : static PyObject *
    7000                 :       2223 : init_subclasses(PyTypeObject *self)
    7001                 :            : {
    7002                 :       2223 :     PyObject *subclasses = PyDict_New();
    7003         [ -  + ]:       2223 :     if (subclasses == NULL) {
    7004                 :          0 :         return NULL;
    7005                 :            :     }
    7006         [ +  + ]:       2223 :     if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
    7007                 :        844 :         static_builtin_state *state = _PyStaticType_GetState(self);
    7008                 :        844 :         state->tp_subclasses = subclasses;
    7009                 :        844 :         return subclasses;
    7010                 :            :     }
    7011                 :       1379 :     self->tp_subclasses = (void *)subclasses;
    7012                 :       1379 :     return subclasses;
    7013                 :            : }
    7014                 :            : 
    7015                 :            : static void
    7016                 :       9179 : clear_subclasses(PyTypeObject *self)
    7017                 :            : {
    7018                 :            :     /* Delete the dictionary to save memory. _PyStaticType_Dealloc()
    7019                 :            :        callers also test if tp_subclasses is NULL to check if a static type
    7020                 :            :        has no subclass. */
    7021         [ +  + ]:       9179 :     if (self->tp_flags & _Py_TPFLAGS_STATIC_BUILTIN) {
    7022                 :        752 :         static_builtin_state *state = _PyStaticType_GetState(self);
    7023         [ +  - ]:        752 :         Py_CLEAR(state->tp_subclasses);
    7024                 :        752 :         return;
    7025                 :            :     }
    7026         [ +  + ]:       8427 :     Py_CLEAR(self->tp_subclasses);
    7027                 :            : }
    7028                 :            : 
    7029                 :            : static int
    7030                 :      13455 : add_subclass(PyTypeObject *base, PyTypeObject *type)
    7031                 :            : {
    7032                 :      13455 :     PyObject *key = PyLong_FromVoidPtr((void *) type);
    7033         [ -  + ]:      13455 :     if (key == NULL)
    7034                 :          0 :         return -1;
    7035                 :            : 
    7036                 :      13455 :     PyObject *ref = PyWeakref_NewRef((PyObject *)type, NULL);
    7037         [ -  + ]:      13455 :     if (ref == NULL) {
    7038                 :          0 :         Py_DECREF(key);
    7039                 :          0 :         return -1;
    7040                 :            :     }
    7041                 :            : 
    7042                 :            :     // Only get tp_subclasses after creating the key and value.
    7043                 :            :     // PyWeakref_NewRef() can trigger a garbage collection which can execute
    7044                 :            :     // arbitrary Python code and so modify base->tp_subclasses.
    7045                 :      13455 :     PyObject *subclasses = lookup_subclasses(base);
    7046         [ +  + ]:      13455 :     if (subclasses == NULL) {
    7047                 :       2223 :         subclasses = init_subclasses(base);
    7048         [ -  + ]:       2223 :         if (subclasses == NULL) {
    7049                 :          0 :             Py_DECREF(key);
    7050                 :          0 :             Py_DECREF(ref);
    7051                 :          0 :             return -1;
    7052                 :            :         }
    7053                 :            :     }
    7054                 :            :     assert(PyDict_CheckExact(subclasses));
    7055                 :            : 
    7056                 :      13455 :     int result = PyDict_SetItem(subclasses, key, ref);
    7057                 :      13455 :     Py_DECREF(ref);
    7058                 :      13455 :     Py_DECREF(key);
    7059                 :      13455 :     return result;
    7060                 :            : }
    7061                 :            : 
    7062                 :            : static int
    7063                 :          0 : add_all_subclasses(PyTypeObject *type, PyObject *bases)
    7064                 :            : {
    7065                 :          0 :     Py_ssize_t n = PyTuple_GET_SIZE(bases);
    7066                 :          0 :     int res = 0;
    7067         [ #  # ]:          0 :     for (Py_ssize_t i = 0; i < n; i++) {
    7068                 :          0 :         PyObject *obj = PyTuple_GET_ITEM(bases, i);
    7069                 :            :         // bases tuple must only contain types
    7070                 :          0 :         PyTypeObject *base = _PyType_CAST(obj);
    7071         [ #  # ]:          0 :         if (add_subclass(base, type) < 0) {
    7072                 :          0 :             res = -1;
    7073                 :            :         }
    7074                 :            :     }
    7075                 :          0 :     return res;
    7076                 :            : }
    7077                 :            : 
    7078                 :            : static inline PyTypeObject *
    7079                 :       1496 : subclass_from_ref(PyObject *ref)
    7080                 :            : {
    7081                 :            :     assert(PyWeakref_CheckRef(ref));
    7082                 :       1496 :     PyObject *obj = PyWeakref_GET_OBJECT(ref);  // borrowed ref
    7083                 :            :     assert(obj != NULL);
    7084         [ +  + ]:       1496 :     if (obj == Py_None) {
    7085                 :       1310 :         return NULL;
    7086                 :            :     }
    7087                 :            :     assert(PyType_Check(obj));
    7088                 :        186 :     return _PyType_CAST(obj);
    7089                 :            : }
    7090                 :            : 
    7091                 :            : static PyObject *
    7092                 :      12404 : get_subclasses_key(PyTypeObject *type, PyTypeObject *base)
    7093                 :            : {
    7094                 :      12404 :     PyObject *key = PyLong_FromVoidPtr((void *) type);
    7095         [ +  - ]:      12404 :     if (key != NULL) {
    7096                 :      12404 :         return key;
    7097                 :            :     }
    7098                 :          0 :     PyErr_Clear();
    7099                 :            : 
    7100                 :            :     /* This basically means we're out of memory.
    7101                 :            :        We fall back to manually traversing the values. */
    7102                 :          0 :     Py_ssize_t i = 0;
    7103                 :            :     PyObject *ref;  // borrowed ref
    7104                 :          0 :     PyObject *subclasses = lookup_subclasses(base);
    7105         [ #  # ]:          0 :     if (subclasses != NULL) {
    7106         [ #  # ]:          0 :         while (PyDict_Next(subclasses, &i, &key, &ref)) {
    7107                 :          0 :             PyTypeObject *subclass = subclass_from_ref(ref);  // borrowed
    7108         [ #  # ]:          0 :             if (subclass == type) {
    7109                 :          0 :                 return Py_NewRef(key);
    7110                 :            :             }
    7111                 :            :         }
    7112                 :            :     }
    7113                 :            :     /* It wasn't found. */
    7114                 :          0 :     return NULL;
    7115                 :            : }
    7116                 :            : 
    7117                 :            : static void
    7118                 :      12404 : remove_subclass(PyTypeObject *base, PyTypeObject *type)
    7119                 :            : {
    7120                 :      12404 :     PyObject *subclasses = lookup_subclasses(base);  // borrowed ref
    7121         [ -  + ]:      12404 :     if (subclasses == NULL) {
    7122                 :          0 :         return;
    7123                 :            :     }
    7124                 :            :     assert(PyDict_CheckExact(subclasses));
    7125                 :            : 
    7126                 :      12404 :     PyObject *key = get_subclasses_key(type, base);
    7127   [ +  -  -  + ]:      12404 :     if (key != NULL && PyDict_DelItem(subclasses, key)) {
    7128                 :            :         /* This can happen if the type initialization errored out before
    7129                 :            :            the base subclasses were updated (e.g. a non-str __qualname__
    7130                 :            :            was passed in the type dict). */
    7131                 :          0 :         PyErr_Clear();
    7132                 :            :     }
    7133                 :      12404 :     Py_XDECREF(key);
    7134                 :            : 
    7135         [ +  + ]:      12404 :     if (PyDict_Size(subclasses) == 0) {
    7136                 :       2041 :         clear_subclasses(base);
    7137                 :            :     }
    7138                 :            : }
    7139                 :            : 
    7140                 :            : static void
    7141                 :      11884 : remove_all_subclasses(PyTypeObject *type, PyObject *bases)
    7142                 :            : {
    7143                 :            :     assert(bases != NULL);
    7144                 :            :     // remove_subclass() can clear the current exception
    7145                 :            :     assert(!PyErr_Occurred());
    7146                 :            : 
    7147         [ +  + ]:      24288 :     for (Py_ssize_t i = 0; i < PyTuple_GET_SIZE(bases); i++) {
    7148                 :      12404 :         PyObject *base = PyTuple_GET_ITEM(bases, i);
    7149         [ +  - ]:      12404 :         if (PyType_Check(base)) {
    7150                 :      12404 :             remove_subclass((PyTypeObject*) base, type);
    7151                 :            :         }
    7152                 :            :     }
    7153                 :            :     assert(!PyErr_Occurred());
    7154                 :      11884 : }
    7155                 :            : 
    7156                 :            : static int
    7157                 :      15833 : check_num_args(PyObject *ob, int n)
    7158                 :            : {
    7159         [ -  + ]:      15833 :     if (!PyTuple_CheckExact(ob)) {
    7160                 :          0 :         PyErr_SetString(PyExc_SystemError,
    7161                 :            :             "PyArg_UnpackTuple() argument list is not a tuple");
    7162                 :          0 :         return 0;
    7163                 :            :     }
    7164         [ +  - ]:      15833 :     if (n == PyTuple_GET_SIZE(ob))
    7165                 :      15833 :         return 1;
    7166         [ #  # ]:          0 :     PyErr_Format(
    7167                 :            :         PyExc_TypeError,
    7168                 :            :         "expected %d argument%s, got %zd", n, n == 1 ? "" : "s", PyTuple_GET_SIZE(ob));
    7169                 :          0 :     return 0;
    7170                 :            : }
    7171                 :            : 
    7172                 :            : /* Generic wrappers for overloadable 'operators' such as __getitem__ */
    7173                 :            : 
    7174                 :            : /* There's a wrapper *function* for each distinct function typedef used
    7175                 :            :    for type object slots (e.g. binaryfunc, ternaryfunc, etc.).  There's a
    7176                 :            :    wrapper *table* for each distinct operation (e.g. __len__, __add__).
    7177                 :            :    Most tables have only one entry; the tables for binary operators have two
    7178                 :            :    entries, one regular and one with reversed arguments. */
    7179                 :            : 
    7180                 :            : static PyObject *
    7181                 :          0 : wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
    7182                 :            : {
    7183                 :          0 :     lenfunc func = (lenfunc)wrapped;
    7184                 :            :     Py_ssize_t res;
    7185                 :            : 
    7186         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7187                 :          0 :         return NULL;
    7188                 :          0 :     res = (*func)(self);
    7189   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7190                 :          0 :         return NULL;
    7191                 :          0 :     return PyLong_FromSsize_t(res);
    7192                 :            : }
    7193                 :            : 
    7194                 :            : static PyObject *
    7195                 :          0 : wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
    7196                 :            : {
    7197                 :          0 :     inquiry func = (inquiry)wrapped;
    7198                 :            :     int res;
    7199                 :            : 
    7200         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7201                 :          0 :         return NULL;
    7202                 :          0 :     res = (*func)(self);
    7203   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7204                 :          0 :         return NULL;
    7205                 :          0 :     return PyBool_FromLong((long)res);
    7206                 :            : }
    7207                 :            : 
    7208                 :            : static PyObject *
    7209                 :         12 : wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
    7210                 :            : {
    7211                 :         12 :     binaryfunc func = (binaryfunc)wrapped;
    7212                 :            :     PyObject *other;
    7213                 :            : 
    7214         [ -  + ]:         12 :     if (!check_num_args(args, 1))
    7215                 :          0 :         return NULL;
    7216                 :         12 :     other = PyTuple_GET_ITEM(args, 0);
    7217                 :         12 :     return (*func)(self, other);
    7218                 :            : }
    7219                 :            : 
    7220                 :            : static PyObject *
    7221                 :          0 : wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
    7222                 :            : {
    7223                 :          0 :     binaryfunc func = (binaryfunc)wrapped;
    7224                 :            :     PyObject *other;
    7225                 :            : 
    7226         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7227                 :          0 :         return NULL;
    7228                 :          0 :     other = PyTuple_GET_ITEM(args, 0);
    7229                 :          0 :     return (*func)(self, other);
    7230                 :            : }
    7231                 :            : 
    7232                 :            : static PyObject *
    7233                 :          0 : wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
    7234                 :            : {
    7235                 :          0 :     binaryfunc func = (binaryfunc)wrapped;
    7236                 :            :     PyObject *other;
    7237                 :            : 
    7238         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7239                 :          0 :         return NULL;
    7240                 :          0 :     other = PyTuple_GET_ITEM(args, 0);
    7241                 :          0 :     return (*func)(other, self);
    7242                 :            : }
    7243                 :            : 
    7244                 :            : static PyObject *
    7245                 :          0 : wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
    7246                 :            : {
    7247                 :          0 :     ternaryfunc func = (ternaryfunc)wrapped;
    7248                 :            :     PyObject *other;
    7249                 :          0 :     PyObject *third = Py_None;
    7250                 :            : 
    7251                 :            :     /* Note: This wrapper only works for __pow__() */
    7252                 :            : 
    7253         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
    7254                 :          0 :         return NULL;
    7255                 :          0 :     return (*func)(self, other, third);
    7256                 :            : }
    7257                 :            : 
    7258                 :            : static PyObject *
    7259                 :          0 : wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
    7260                 :            : {
    7261                 :          0 :     ternaryfunc func = (ternaryfunc)wrapped;
    7262                 :            :     PyObject *other;
    7263                 :          0 :     PyObject *third = Py_None;
    7264                 :            : 
    7265                 :            :     /* Note: This wrapper only works for __pow__() */
    7266                 :            : 
    7267         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
    7268                 :          0 :         return NULL;
    7269                 :          0 :     return (*func)(other, self, third);
    7270                 :            : }
    7271                 :            : 
    7272                 :            : static PyObject *
    7273                 :          0 : wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
    7274                 :            : {
    7275                 :          0 :     unaryfunc func = (unaryfunc)wrapped;
    7276                 :            : 
    7277         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7278                 :          0 :         return NULL;
    7279                 :          0 :     return (*func)(self);
    7280                 :            : }
    7281                 :            : 
    7282                 :            : static PyObject *
    7283                 :          0 : wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
    7284                 :            : {
    7285                 :          0 :     ssizeargfunc func = (ssizeargfunc)wrapped;
    7286                 :            :     PyObject* o;
    7287                 :            :     Py_ssize_t i;
    7288                 :            : 
    7289         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
    7290                 :          0 :         return NULL;
    7291                 :          0 :     i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
    7292   [ #  #  #  # ]:          0 :     if (i == -1 && PyErr_Occurred())
    7293                 :          0 :         return NULL;
    7294                 :          0 :     return (*func)(self, i);
    7295                 :            : }
    7296                 :            : 
    7297                 :            : static Py_ssize_t
    7298                 :          0 : getindex(PyObject *self, PyObject *arg)
    7299                 :            : {
    7300                 :            :     Py_ssize_t i;
    7301                 :            : 
    7302                 :          0 :     i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
    7303   [ #  #  #  # ]:          0 :     if (i == -1 && PyErr_Occurred())
    7304                 :          0 :         return -1;
    7305         [ #  # ]:          0 :     if (i < 0) {
    7306                 :          0 :         PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
    7307   [ #  #  #  # ]:          0 :         if (sq && sq->sq_length) {
    7308                 :          0 :             Py_ssize_t n = (*sq->sq_length)(self);
    7309         [ #  # ]:          0 :             if (n < 0) {
    7310                 :            :                 assert(PyErr_Occurred());
    7311                 :          0 :                 return -1;
    7312                 :            :             }
    7313                 :          0 :             i += n;
    7314                 :            :         }
    7315                 :            :     }
    7316                 :          0 :     return i;
    7317                 :            : }
    7318                 :            : 
    7319                 :            : static PyObject *
    7320                 :          0 : wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
    7321                 :            : {
    7322                 :          0 :     ssizeargfunc func = (ssizeargfunc)wrapped;
    7323                 :            :     PyObject *arg;
    7324                 :            :     Py_ssize_t i;
    7325                 :            : 
    7326         [ #  # ]:          0 :     if (PyTuple_GET_SIZE(args) == 1) {
    7327                 :          0 :         arg = PyTuple_GET_ITEM(args, 0);
    7328                 :          0 :         i = getindex(self, arg);
    7329   [ #  #  #  # ]:          0 :         if (i == -1 && PyErr_Occurred())
    7330                 :          0 :             return NULL;
    7331                 :          0 :         return (*func)(self, i);
    7332                 :            :     }
    7333                 :          0 :     check_num_args(args, 1);
    7334                 :            :     assert(PyErr_Occurred());
    7335                 :          0 :     return NULL;
    7336                 :            : }
    7337                 :            : 
    7338                 :            : static PyObject *
    7339                 :          0 : wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
    7340                 :            : {
    7341                 :          0 :     ssizeobjargproc func = (ssizeobjargproc)wrapped;
    7342                 :            :     Py_ssize_t i;
    7343                 :            :     int res;
    7344                 :            :     PyObject *arg, *value;
    7345                 :            : 
    7346         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
    7347                 :          0 :         return NULL;
    7348                 :          0 :     i = getindex(self, arg);
    7349   [ #  #  #  # ]:          0 :     if (i == -1 && PyErr_Occurred())
    7350                 :          0 :         return NULL;
    7351                 :          0 :     res = (*func)(self, i, value);
    7352   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7353                 :          0 :         return NULL;
    7354                 :          0 :     Py_RETURN_NONE;
    7355                 :            : }
    7356                 :            : 
    7357                 :            : static PyObject *
    7358                 :          0 : wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
    7359                 :            : {
    7360                 :          0 :     ssizeobjargproc func = (ssizeobjargproc)wrapped;
    7361                 :            :     Py_ssize_t i;
    7362                 :            :     int res;
    7363                 :            :     PyObject *arg;
    7364                 :            : 
    7365         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7366                 :          0 :         return NULL;
    7367                 :          0 :     arg = PyTuple_GET_ITEM(args, 0);
    7368                 :          0 :     i = getindex(self, arg);
    7369   [ #  #  #  # ]:          0 :     if (i == -1 && PyErr_Occurred())
    7370                 :          0 :         return NULL;
    7371                 :          0 :     res = (*func)(self, i, NULL);
    7372   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7373                 :          0 :         return NULL;
    7374                 :          0 :     Py_RETURN_NONE;
    7375                 :            : }
    7376                 :            : 
    7377                 :            : /* XXX objobjproc is a misnomer; should be objargpred */
    7378                 :            : static PyObject *
    7379                 :          0 : wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
    7380                 :            : {
    7381                 :          0 :     objobjproc func = (objobjproc)wrapped;
    7382                 :            :     int res;
    7383                 :            :     PyObject *value;
    7384                 :            : 
    7385         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7386                 :          0 :         return NULL;
    7387                 :          0 :     value = PyTuple_GET_ITEM(args, 0);
    7388                 :          0 :     res = (*func)(self, value);
    7389   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7390                 :          0 :         return NULL;
    7391                 :            :     else
    7392                 :          0 :         return PyBool_FromLong(res);
    7393                 :            : }
    7394                 :            : 
    7395                 :            : static PyObject *
    7396                 :        352 : wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
    7397                 :            : {
    7398                 :        352 :     objobjargproc func = (objobjargproc)wrapped;
    7399                 :            :     int res;
    7400                 :            :     PyObject *key, *value;
    7401                 :            : 
    7402         [ -  + ]:        352 :     if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
    7403                 :          0 :         return NULL;
    7404                 :        352 :     res = (*func)(self, key, value);
    7405   [ -  +  -  - ]:        352 :     if (res == -1 && PyErr_Occurred())
    7406                 :          0 :         return NULL;
    7407                 :        352 :     Py_RETURN_NONE;
    7408                 :            : }
    7409                 :            : 
    7410                 :            : static PyObject *
    7411                 :          0 : wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
    7412                 :            : {
    7413                 :          0 :     objobjargproc func = (objobjargproc)wrapped;
    7414                 :            :     int res;
    7415                 :            :     PyObject *key;
    7416                 :            : 
    7417         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7418                 :          0 :         return NULL;
    7419                 :          0 :     key = PyTuple_GET_ITEM(args, 0);
    7420                 :          0 :     res = (*func)(self, key, NULL);
    7421   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7422                 :          0 :         return NULL;
    7423                 :          0 :     Py_RETURN_NONE;
    7424                 :            : }
    7425                 :            : 
    7426                 :            : /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
    7427                 :            :    This is called the Carlo Verre hack after its discoverer.  See
    7428                 :            :    https://mail.python.org/pipermail/python-dev/2003-April/034535.html
    7429                 :            :    */
    7430                 :            : static int
    7431                 :       2024 : hackcheck(PyObject *self, setattrofunc func, const char *what)
    7432                 :            : {
    7433                 :       2024 :     PyTypeObject *type = Py_TYPE(self);
    7434                 :       2024 :     PyObject *mro = type->tp_mro;
    7435         [ -  + ]:       2024 :     if (!mro) {
    7436                 :            :         /* Probably ok not to check the call in this case. */
    7437                 :          0 :         return 1;
    7438                 :            :     }
    7439                 :            :     assert(PyTuple_Check(mro));
    7440                 :            : 
    7441                 :            :     /* Find the (base) type that defined the type's slot function. */
    7442                 :       2024 :     PyTypeObject *defining_type = type;
    7443                 :            :     Py_ssize_t i;
    7444         [ +  + ]:      10322 :     for (i = PyTuple_GET_SIZE(mro) - 1; i >= 0; i--) {
    7445                 :       8298 :         PyTypeObject *base = _PyType_CAST(PyTuple_GET_ITEM(mro, i));
    7446         [ +  + ]:       8298 :         if (base->tp_setattro == slot_tp_setattro) {
    7447                 :            :             /* Ignore Python classes:
    7448                 :            :                they never define their own C-level setattro. */
    7449                 :            :         }
    7450         [ -  + ]:       4952 :         else if (base->tp_setattro == type->tp_setattro) {
    7451                 :          0 :             defining_type = base;
    7452                 :          0 :             break;
    7453                 :            :         }
    7454                 :            :     }
    7455                 :            : 
    7456                 :            :     /* Reject calls that jump over intermediate C-level overrides. */
    7457         [ +  - ]:       4291 :     for (PyTypeObject *base = defining_type; base; base = base->tp_base) {
    7458         [ +  + ]:       4291 :         if (base->tp_setattro == func) {
    7459                 :            :             /* 'func' is the right slot function to call. */
    7460                 :       2024 :             break;
    7461                 :            :         }
    7462         [ -  + ]:       2267 :         else if (base->tp_setattro != slot_tp_setattro) {
    7463                 :            :             /* 'base' is not a Python class and overrides 'func'.
    7464                 :            :                Its tp_setattro should be called instead. */
    7465                 :          0 :             PyErr_Format(PyExc_TypeError,
    7466                 :            :                          "can't apply this %s to %s object",
    7467                 :            :                          what,
    7468                 :            :                          type->tp_name);
    7469                 :          0 :             return 0;
    7470                 :            :         }
    7471                 :            :     }
    7472                 :       2024 :     return 1;
    7473                 :            : }
    7474                 :            : 
    7475                 :            : static PyObject *
    7476                 :       1837 : wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
    7477                 :            : {
    7478                 :       1837 :     setattrofunc func = (setattrofunc)wrapped;
    7479                 :            :     int res;
    7480                 :            :     PyObject *name, *value;
    7481                 :            : 
    7482         [ -  + ]:       1837 :     if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
    7483                 :          0 :         return NULL;
    7484         [ -  + ]:       1837 :     if (!hackcheck(self, func, "__setattr__"))
    7485                 :          0 :         return NULL;
    7486                 :       1837 :     res = (*func)(self, name, value);
    7487         [ -  + ]:       1837 :     if (res < 0)
    7488                 :          0 :         return NULL;
    7489                 :       1837 :     Py_RETURN_NONE;
    7490                 :            : }
    7491                 :            : 
    7492                 :            : static PyObject *
    7493                 :        187 : wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
    7494                 :            : {
    7495                 :        187 :     setattrofunc func = (setattrofunc)wrapped;
    7496                 :            :     int res;
    7497                 :            :     PyObject *name;
    7498                 :            : 
    7499         [ -  + ]:        187 :     if (!check_num_args(args, 1))
    7500                 :          0 :         return NULL;
    7501                 :        187 :     name = PyTuple_GET_ITEM(args, 0);
    7502         [ -  + ]:        187 :     if (!hackcheck(self, func, "__delattr__"))
    7503                 :          0 :         return NULL;
    7504                 :        187 :     res = (*func)(self, name, NULL);
    7505         [ -  + ]:        187 :     if (res < 0)
    7506                 :          0 :         return NULL;
    7507                 :        187 :     Py_RETURN_NONE;
    7508                 :            : }
    7509                 :            : 
    7510                 :            : static PyObject *
    7511                 :          0 : wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
    7512                 :            : {
    7513                 :          0 :     hashfunc func = (hashfunc)wrapped;
    7514                 :            :     Py_hash_t res;
    7515                 :            : 
    7516         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7517                 :          0 :         return NULL;
    7518                 :          0 :     res = (*func)(self);
    7519   [ #  #  #  # ]:          0 :     if (res == -1 && PyErr_Occurred())
    7520                 :          0 :         return NULL;
    7521                 :          0 :     return PyLong_FromSsize_t(res);
    7522                 :            : }
    7523                 :            : 
    7524                 :            : static PyObject *
    7525                 :          0 : wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
    7526                 :            : {
    7527                 :          0 :     ternaryfunc func = (ternaryfunc)wrapped;
    7528                 :            : 
    7529                 :          0 :     return (*func)(self, args, kwds);
    7530                 :            : }
    7531                 :            : 
    7532                 :            : static PyObject *
    7533                 :          0 : wrap_del(PyObject *self, PyObject *args, void *wrapped)
    7534                 :            : {
    7535                 :          0 :     destructor func = (destructor)wrapped;
    7536                 :            : 
    7537         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7538                 :          0 :         return NULL;
    7539                 :            : 
    7540                 :          0 :     (*func)(self);
    7541                 :          0 :     Py_RETURN_NONE;
    7542                 :            : }
    7543                 :            : 
    7544                 :            : static PyObject *
    7545                 :      15634 : wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
    7546                 :            : {
    7547                 :      15634 :     richcmpfunc func = (richcmpfunc)wrapped;
    7548                 :            :     PyObject *other;
    7549                 :            : 
    7550         [ -  + ]:      15634 :     if (!check_num_args(args, 1))
    7551                 :          0 :         return NULL;
    7552                 :      15634 :     other = PyTuple_GET_ITEM(args, 0);
    7553                 :      15634 :     return (*func)(self, other, op);
    7554                 :            : }
    7555                 :            : 
    7556                 :            : #undef RICHCMP_WRAPPER
    7557                 :            : #define RICHCMP_WRAPPER(NAME, OP) \
    7558                 :            : static PyObject * \
    7559                 :            : richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
    7560                 :            : { \
    7561                 :            :     return wrap_richcmpfunc(self, args, wrapped, OP); \
    7562                 :            : }
    7563                 :            : 
    7564                 :          0 : RICHCMP_WRAPPER(lt, Py_LT)
    7565                 :          0 : RICHCMP_WRAPPER(le, Py_LE)
    7566                 :      15632 : RICHCMP_WRAPPER(eq, Py_EQ)
    7567                 :          2 : RICHCMP_WRAPPER(ne, Py_NE)
    7568                 :          0 : RICHCMP_WRAPPER(gt, Py_GT)
    7569                 :          0 : RICHCMP_WRAPPER(ge, Py_GE)
    7570                 :            : 
    7571                 :            : static PyObject *
    7572                 :          0 : wrap_next(PyObject *self, PyObject *args, void *wrapped)
    7573                 :            : {
    7574                 :          0 :     unaryfunc func = (unaryfunc)wrapped;
    7575                 :            :     PyObject *res;
    7576                 :            : 
    7577         [ #  # ]:          0 :     if (!check_num_args(args, 0))
    7578                 :          0 :         return NULL;
    7579                 :          0 :     res = (*func)(self);
    7580   [ #  #  #  # ]:          0 :     if (res == NULL && !PyErr_Occurred())
    7581                 :          0 :         PyErr_SetNone(PyExc_StopIteration);
    7582                 :          0 :     return res;
    7583                 :            : }
    7584                 :            : 
    7585                 :            : static PyObject *
    7586                 :          0 : wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
    7587                 :            : {
    7588                 :          0 :     descrgetfunc func = (descrgetfunc)wrapped;
    7589                 :            :     PyObject *obj;
    7590                 :          0 :     PyObject *type = NULL;
    7591                 :            : 
    7592         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
    7593                 :          0 :         return NULL;
    7594         [ #  # ]:          0 :     if (obj == Py_None)
    7595                 :          0 :         obj = NULL;
    7596         [ #  # ]:          0 :     if (type == Py_None)
    7597                 :          0 :         type = NULL;
    7598   [ #  #  #  # ]:          0 :     if (type == NULL && obj == NULL) {
    7599                 :          0 :         PyErr_SetString(PyExc_TypeError,
    7600                 :            :                         "__get__(None, None) is invalid");
    7601                 :          0 :         return NULL;
    7602                 :            :     }
    7603                 :          0 :     return (*func)(self, obj, type);
    7604                 :            : }
    7605                 :            : 
    7606                 :            : static PyObject *
    7607                 :          0 : wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
    7608                 :            : {
    7609                 :          0 :     descrsetfunc func = (descrsetfunc)wrapped;
    7610                 :            :     PyObject *obj, *value;
    7611                 :            :     int ret;
    7612                 :            : 
    7613         [ #  # ]:          0 :     if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
    7614                 :          0 :         return NULL;
    7615                 :          0 :     ret = (*func)(self, obj, value);
    7616         [ #  # ]:          0 :     if (ret < 0)
    7617                 :          0 :         return NULL;
    7618                 :          0 :     Py_RETURN_NONE;
    7619                 :            : }
    7620                 :            : 
    7621                 :            : static PyObject *
    7622                 :          0 : wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
    7623                 :            : {
    7624                 :          0 :     descrsetfunc func = (descrsetfunc)wrapped;
    7625                 :            :     PyObject *obj;
    7626                 :            :     int ret;
    7627                 :            : 
    7628         [ #  # ]:          0 :     if (!check_num_args(args, 1))
    7629                 :          0 :         return NULL;
    7630                 :          0 :     obj = PyTuple_GET_ITEM(args, 0);
    7631                 :          0 :     ret = (*func)(self, obj, NULL);
    7632         [ #  # ]:          0 :     if (ret < 0)
    7633                 :          0 :         return NULL;
    7634                 :          0 :     Py_RETURN_NONE;
    7635                 :            : }
    7636                 :            : 
    7637                 :            : static PyObject *
    7638                 :         55 : wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
    7639                 :            : {
    7640                 :         55 :     initproc func = (initproc)wrapped;
    7641                 :            : 
    7642         [ -  + ]:         55 :     if (func(self, args, kwds) < 0)
    7643                 :          0 :         return NULL;
    7644                 :         55 :     Py_RETURN_NONE;
    7645                 :            : }
    7646                 :            : 
    7647                 :            : static PyObject *
    7648                 :     132856 : tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
    7649                 :            : {
    7650                 :            :     PyTypeObject *staticbase;
    7651                 :            :     PyObject *arg0, *res;
    7652                 :            : 
    7653   [ +  -  -  + ]:     132856 :     if (self == NULL || !PyType_Check(self)) {
    7654                 :          0 :         PyErr_Format(PyExc_SystemError,
    7655                 :            :                      "__new__() called with non-type 'self'");
    7656                 :          0 :         return NULL;
    7657                 :            :     }
    7658                 :     132856 :     PyTypeObject *type = (PyTypeObject *)self;
    7659                 :            : 
    7660   [ +  -  -  + ]:     132856 :     if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
    7661                 :          0 :         PyErr_Format(PyExc_TypeError,
    7662                 :            :                      "%s.__new__(): not enough arguments",
    7663                 :            :                      type->tp_name);
    7664                 :          0 :         return NULL;
    7665                 :            :     }
    7666                 :     132856 :     arg0 = PyTuple_GET_ITEM(args, 0);
    7667         [ -  + ]:     132856 :     if (!PyType_Check(arg0)) {
    7668                 :          0 :         PyErr_Format(PyExc_TypeError,
    7669                 :            :                      "%s.__new__(X): X is not a type object (%s)",
    7670                 :            :                      type->tp_name,
    7671                 :          0 :                      Py_TYPE(arg0)->tp_name);
    7672                 :          0 :         return NULL;
    7673                 :            :     }
    7674                 :     132856 :     PyTypeObject *subtype = (PyTypeObject *)arg0;
    7675                 :            : 
    7676         [ -  + ]:     132856 :     if (!PyType_IsSubtype(subtype, type)) {
    7677                 :          0 :         PyErr_Format(PyExc_TypeError,
    7678                 :            :                      "%s.__new__(%s): %s is not a subtype of %s",
    7679                 :            :                      type->tp_name,
    7680                 :            :                      subtype->tp_name,
    7681                 :            :                      subtype->tp_name,
    7682                 :            :                      type->tp_name);
    7683                 :          0 :         return NULL;
    7684                 :            :     }
    7685                 :            : 
    7686                 :            :     /* Check that the use doesn't do something silly and unsafe like
    7687                 :            :        object.__new__(dict).  To do this, we check that the
    7688                 :            :        most derived base that's not a heap type is this type. */
    7689                 :     132856 :     staticbase = subtype;
    7690   [ +  -  +  + ]:     266538 :     while (staticbase && (staticbase->tp_new == slot_tp_new))
    7691                 :     133682 :         staticbase = staticbase->tp_base;
    7692                 :            :     /* If staticbase is NULL now, it is a really weird type.
    7693                 :            :        In the spirit of backwards compatibility (?), just shut up. */
    7694   [ +  -  -  + ]:     132856 :     if (staticbase && staticbase->tp_new != type->tp_new) {
    7695                 :          0 :         PyErr_Format(PyExc_TypeError,
    7696                 :            :                      "%s.__new__(%s) is not safe, use %s.__new__()",
    7697                 :            :                      type->tp_name,
    7698                 :            :                      subtype->tp_name,
    7699                 :            :                      staticbase->tp_name);
    7700                 :          0 :         return NULL;
    7701                 :            :     }
    7702                 :            : 
    7703                 :     132856 :     args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
    7704         [ -  + ]:     132856 :     if (args == NULL)
    7705                 :          0 :         return NULL;
    7706                 :     132856 :     res = type->tp_new(subtype, args, kwds);
    7707                 :     132856 :     Py_DECREF(args);
    7708                 :     132856 :     return res;
    7709                 :            : }
    7710                 :            : 
    7711                 :            : static struct PyMethodDef tp_new_methoddef[] = {
    7712                 :            :     {"__new__", _PyCFunction_CAST(tp_new_wrapper), METH_VARARGS|METH_KEYWORDS,
    7713                 :            :      PyDoc_STR("__new__($type, *args, **kwargs)\n--\n\n"
    7714                 :            :                "Create and return a new object.  "
    7715                 :            :                "See help(type) for accurate signature.")},
    7716                 :            :     {0}
    7717                 :            : };
    7718                 :            : 
    7719                 :            : static int
    7720                 :       3456 : add_tp_new_wrapper(PyTypeObject *type)
    7721                 :            : {
    7722                 :       3456 :     int r = PyDict_Contains(type->tp_dict, &_Py_ID(__new__));
    7723         [ -  + ]:       3456 :     if (r > 0) {
    7724                 :          0 :         return 0;
    7725                 :            :     }
    7726         [ -  + ]:       3456 :     if (r < 0) {
    7727                 :          0 :         return -1;
    7728                 :            :     }
    7729                 :            : 
    7730                 :       3456 :     PyObject *func = PyCFunction_NewEx(tp_new_methoddef, (PyObject *)type, NULL);
    7731         [ -  + ]:       3456 :     if (func == NULL) {
    7732                 :          0 :         return -1;
    7733                 :            :     }
    7734                 :       3456 :     r = PyDict_SetItem(type->tp_dict, &_Py_ID(__new__), func);
    7735                 :       3456 :     Py_DECREF(func);
    7736                 :       3456 :     return r;
    7737                 :            : }
    7738                 :            : 
    7739                 :            : /* Slot wrappers that call the corresponding __foo__ slot.  See comments
    7740                 :            :    below at override_slots() for more explanation. */
    7741                 :            : 
    7742                 :            : #define SLOT0(FUNCNAME, DUNDER) \
    7743                 :            : static PyObject * \
    7744                 :            : FUNCNAME(PyObject *self) \
    7745                 :            : { \
    7746                 :            :     PyObject* stack[1] = {self}; \
    7747                 :            :     return vectorcall_method(&_Py_ID(DUNDER), stack, 1); \
    7748                 :            : }
    7749                 :            : 
    7750                 :            : #define SLOT1(FUNCNAME, DUNDER, ARG1TYPE) \
    7751                 :            : static PyObject * \
    7752                 :            : FUNCNAME(PyObject *self, ARG1TYPE arg1) \
    7753                 :            : { \
    7754                 :            :     PyObject* stack[2] = {self, arg1}; \
    7755                 :            :     return vectorcall_method(&_Py_ID(DUNDER), stack, 2); \
    7756                 :            : }
    7757                 :            : 
    7758                 :            : /* Boolean helper for SLOT1BINFULL().
    7759                 :            :    right.__class__ is a nontrivial subclass of left.__class__. */
    7760                 :            : static int
    7761                 :          0 : method_is_overloaded(PyObject *left, PyObject *right, PyObject *name)
    7762                 :            : {
    7763                 :            :     PyObject *a, *b;
    7764                 :            :     int ok;
    7765                 :            : 
    7766         [ #  # ]:          0 :     if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(right)), name, &b) < 0) {
    7767                 :          0 :         return -1;
    7768                 :            :     }
    7769         [ #  # ]:          0 :     if (b == NULL) {
    7770                 :            :         /* If right doesn't have it, it's not overloaded */
    7771                 :          0 :         return 0;
    7772                 :            :     }
    7773                 :            : 
    7774         [ #  # ]:          0 :     if (_PyObject_LookupAttr((PyObject *)(Py_TYPE(left)), name, &a) < 0) {
    7775                 :          0 :         Py_DECREF(b);
    7776                 :          0 :         return -1;
    7777                 :            :     }
    7778         [ #  # ]:          0 :     if (a == NULL) {
    7779                 :          0 :         Py_DECREF(b);
    7780                 :            :         /* If right has it but left doesn't, it's overloaded */
    7781                 :          0 :         return 1;
    7782                 :            :     }
    7783                 :            : 
    7784                 :          0 :     ok = PyObject_RichCompareBool(a, b, Py_NE);
    7785                 :          0 :     Py_DECREF(a);
    7786                 :          0 :     Py_DECREF(b);
    7787                 :          0 :     return ok;
    7788                 :            : }
    7789                 :            : 
    7790                 :            : 
    7791                 :            : #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, DUNDER, RDUNDER) \
    7792                 :            : static PyObject * \
    7793                 :            : FUNCNAME(PyObject *self, PyObject *other) \
    7794                 :            : { \
    7795                 :            :     PyObject* stack[2]; \
    7796                 :            :     PyThreadState *tstate = _PyThreadState_GET(); \
    7797                 :            :     int do_other = !Py_IS_TYPE(self, Py_TYPE(other)) && \
    7798                 :            :         Py_TYPE(other)->tp_as_number != NULL && \
    7799                 :            :         Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \
    7800                 :            :     if (Py_TYPE(self)->tp_as_number != NULL && \
    7801                 :            :         Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \
    7802                 :            :         PyObject *r; \
    7803                 :            :         if (do_other && PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) { \
    7804                 :            :             int ok = method_is_overloaded(self, other, &_Py_ID(RDUNDER)); \
    7805                 :            :             if (ok < 0) { \
    7806                 :            :                 return NULL; \
    7807                 :            :             } \
    7808                 :            :             if (ok) { \
    7809                 :            :                 stack[0] = other; \
    7810                 :            :                 stack[1] = self; \
    7811                 :            :                 r = vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \
    7812                 :            :                 if (r != Py_NotImplemented) \
    7813                 :            :                     return r; \
    7814                 :            :                 Py_DECREF(r); \
    7815                 :            :                 do_other = 0; \
    7816                 :            :             } \
    7817                 :            :         } \
    7818                 :            :         stack[0] = self; \
    7819                 :            :         stack[1] = other; \
    7820                 :            :         r = vectorcall_maybe(tstate, &_Py_ID(DUNDER), stack, 2); \
    7821                 :            :         if (r != Py_NotImplemented || \
    7822                 :            :             Py_IS_TYPE(other, Py_TYPE(self))) \
    7823                 :            :             return r; \
    7824                 :            :         Py_DECREF(r); \
    7825                 :            :     } \
    7826                 :            :     if (do_other) { \
    7827                 :            :         stack[0] = other; \
    7828                 :            :         stack[1] = self; \
    7829                 :            :         return vectorcall_maybe(tstate, &_Py_ID(RDUNDER), stack, 2); \
    7830                 :            :     } \
    7831                 :            :     Py_RETURN_NOTIMPLEMENTED; \
    7832                 :            : }
    7833                 :            : 
    7834                 :            : #define SLOT1BIN(FUNCNAME, SLOTNAME, DUNDER, RDUNDER) \
    7835                 :            :     SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, DUNDER, RDUNDER)
    7836                 :            : 
    7837                 :            : static Py_ssize_t
    7838                 :       1238 : slot_sq_length(PyObject *self)
    7839                 :            : {
    7840                 :       1238 :     PyObject* stack[1] = {self};
    7841                 :       1238 :     PyObject *res = vectorcall_method(&_Py_ID(__len__), stack, 1);
    7842                 :            :     Py_ssize_t len;
    7843                 :            : 
    7844         [ -  + ]:       1238 :     if (res == NULL)
    7845                 :          0 :         return -1;
    7846                 :            : 
    7847                 :       1238 :     Py_SETREF(res, _PyNumber_Index(res));
    7848         [ -  + ]:       1238 :     if (res == NULL)
    7849                 :          0 :         return -1;
    7850                 :            : 
    7851                 :            :     assert(PyLong_Check(res));
    7852         [ -  + ]:       1238 :     if (Py_SIZE(res) < 0) {
    7853                 :          0 :         Py_DECREF(res);
    7854                 :          0 :         PyErr_SetString(PyExc_ValueError,
    7855                 :            :                         "__len__() should return >= 0");
    7856                 :          0 :         return -1;
    7857                 :            :     }
    7858                 :            : 
    7859                 :       1238 :     len = PyNumber_AsSsize_t(res, PyExc_OverflowError);
    7860                 :            :     assert(len >= 0 || PyErr_ExceptionMatches(PyExc_OverflowError));
    7861                 :       1238 :     Py_DECREF(res);
    7862                 :       1238 :     return len;
    7863                 :            : }
    7864                 :            : 
    7865                 :            : static PyObject *
    7866                 :       1296 : slot_sq_item(PyObject *self, Py_ssize_t i)
    7867                 :            : {
    7868                 :       1296 :     PyObject *ival = PyLong_FromSsize_t(i);
    7869         [ -  + ]:       1296 :     if (ival == NULL) {
    7870                 :          0 :         return NULL;
    7871                 :            :     }
    7872                 :       1296 :     PyObject *stack[2] = {self, ival};
    7873                 :       1296 :     PyObject *retval = vectorcall_method(&_Py_ID(__getitem__), stack, 2);
    7874                 :       1296 :     Py_DECREF(ival);
    7875                 :       1296 :     return retval;
    7876                 :            : }
    7877                 :            : 
    7878                 :            : static int
    7879                 :          0 : slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value)
    7880                 :            : {
    7881                 :            :     PyObject *stack[3];
    7882                 :            :     PyObject *res;
    7883                 :            :     PyObject *index_obj;
    7884                 :            : 
    7885                 :          0 :     index_obj = PyLong_FromSsize_t(index);
    7886         [ #  # ]:          0 :     if (index_obj == NULL) {
    7887                 :          0 :         return -1;
    7888                 :            :     }
    7889                 :            : 
    7890                 :          0 :     stack[0] = self;
    7891                 :          0 :     stack[1] = index_obj;
    7892         [ #  # ]:          0 :     if (value == NULL) {
    7893                 :          0 :         res = vectorcall_method(&_Py_ID(__delitem__), stack, 2);
    7894                 :            :     }
    7895                 :            :     else {
    7896                 :          0 :         stack[2] = value;
    7897                 :          0 :         res = vectorcall_method(&_Py_ID(__setitem__), stack, 3);
    7898                 :            :     }
    7899                 :          0 :     Py_DECREF(index_obj);
    7900                 :            : 
    7901         [ #  # ]:          0 :     if (res == NULL) {
    7902                 :          0 :         return -1;
    7903                 :            :     }
    7904                 :          0 :     Py_DECREF(res);
    7905                 :          0 :     return 0;
    7906                 :            : }
    7907                 :            : 
    7908                 :            : static int
    7909                 :        134 : slot_sq_contains(PyObject *self, PyObject *value)
    7910                 :            : {
    7911                 :        134 :     PyThreadState *tstate = _PyThreadState_GET();
    7912                 :            :     PyObject *func, *res;
    7913                 :        134 :     int result = -1, unbound;
    7914                 :            : 
    7915                 :        134 :     func = lookup_maybe_method(self, &_Py_ID(__contains__), &unbound);
    7916         [ -  + ]:        134 :     if (func == Py_None) {
    7917                 :          0 :         Py_DECREF(func);
    7918                 :          0 :         PyErr_Format(PyExc_TypeError,
    7919                 :            :                      "'%.200s' object is not a container",
    7920                 :          0 :                      Py_TYPE(self)->tp_name);
    7921                 :          0 :         return -1;
    7922                 :            :     }
    7923         [ +  - ]:        134 :     if (func != NULL) {
    7924                 :        134 :         PyObject *args[2] = {self, value};
    7925                 :        134 :         res = vectorcall_unbound(tstate, unbound, func, args, 2);
    7926                 :        134 :         Py_DECREF(func);
    7927         [ +  - ]:        134 :         if (res != NULL) {
    7928                 :        134 :             result = PyObject_IsTrue(res);
    7929                 :        134 :             Py_DECREF(res);
    7930                 :            :         }
    7931                 :            :     }
    7932         [ #  # ]:          0 :     else if (! PyErr_Occurred()) {
    7933                 :            :         /* Possible results: -1 and 1 */
    7934                 :          0 :         result = (int)_PySequence_IterSearch(self, value,
    7935                 :            :                                          PY_ITERSEARCH_CONTAINS);
    7936                 :            :     }
    7937                 :        134 :     return result;
    7938                 :            : }
    7939                 :            : 
    7940                 :            : #define slot_mp_length slot_sq_length
    7941                 :            : 
    7942                 :        860 : SLOT1(slot_mp_subscript, __getitem__, PyObject *)
    7943                 :            : 
    7944                 :            : static int
    7945                 :        621 : slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value)
    7946                 :            : {
    7947                 :            :     PyObject *stack[3];
    7948                 :            :     PyObject *res;
    7949                 :            : 
    7950                 :        621 :     stack[0] = self;
    7951                 :        621 :     stack[1] = key;
    7952         [ +  + ]:        621 :     if (value == NULL) {
    7953                 :         12 :         res = vectorcall_method(&_Py_ID(__delitem__), stack, 2);
    7954                 :            :     }
    7955                 :            :     else {
    7956                 :        609 :         stack[2] = value;
    7957                 :        609 :         res = vectorcall_method(&_Py_ID(__setitem__), stack, 3);
    7958                 :            :     }
    7959                 :            : 
    7960         [ -  + ]:        621 :     if (res == NULL)
    7961                 :          0 :         return -1;
    7962                 :        621 :     Py_DECREF(res);
    7963                 :        621 :     return 0;
    7964                 :            : }
    7965                 :            : 
    7966   [ +  +  +  -  :         17 : SLOT1BIN(slot_nb_add, nb_add, __add__, __radd__)
          +  +  +  -  +  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                   +  - ]
    7967   [ -  +  -  -  :       9848 : SLOT1BIN(slot_nb_subtract, nb_subtract, __sub__, __rsub__)
          -  -  +  -  +  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                   -  - ]
    7968   [ +  +  +  -  :         22 : SLOT1BIN(slot_nb_multiply, nb_multiply, __mul__, __rmul__)
          +  +  +  -  +  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                   +  - ]
    7969   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_matrix_multiply, nb_matrix_multiply, __matmul__, __rmatmul__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7970   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_remainder, nb_remainder, __mod__, __rmod__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7971   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_divmod, nb_divmod, __divmod__, __rdivmod__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7972                 :            : 
    7973                 :            : static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *);
    7974                 :            : 
    7975   [ #  #  #  #  :          0 : SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, nb_power, __pow__, __rpow__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    7976                 :            : 
    7977                 :            : static PyObject *
    7978                 :          0 : slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus)
    7979                 :            : {
    7980         [ #  # ]:          0 :     if (modulus == Py_None)
    7981                 :          0 :         return slot_nb_power_binary(self, other);
    7982                 :            :     /* Three-arg power doesn't use __rpow__.  But ternary_op
    7983                 :            :        can call this when the second argument's type uses
    7984                 :            :        slot_nb_power, so check before calling self.__pow__. */
    7985         [ #  # ]:          0 :     if (Py_TYPE(self)->tp_as_number != NULL &&
    7986         [ #  # ]:          0 :         Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) {
    7987                 :          0 :         PyObject* stack[3] = {self, other, modulus};
    7988                 :          0 :         return vectorcall_method(&_Py_ID(__pow__), stack, 3);
    7989                 :            :     }
    7990                 :          0 :     Py_RETURN_NOTIMPLEMENTED;
    7991                 :            : }
    7992                 :            : 
    7993                 :          2 : SLOT0(slot_nb_negative, __neg__)
    7994                 :          0 : SLOT0(slot_nb_positive, __pos__)
    7995                 :      39392 : SLOT0(slot_nb_absolute, __abs__)
    7996                 :            : 
    7997                 :            : static int
    7998                 :          0 : slot_nb_bool(PyObject *self)
    7999                 :            : {
    8000                 :            :     PyObject *func, *value;
    8001                 :            :     int result, unbound;
    8002                 :          0 :     int using_len = 0;
    8003                 :            : 
    8004                 :          0 :     func = lookup_maybe_method(self, &_Py_ID(__bool__), &unbound);
    8005         [ #  # ]:          0 :     if (func == NULL) {
    8006         [ #  # ]:          0 :         if (PyErr_Occurred()) {
    8007                 :          0 :             return -1;
    8008                 :            :         }
    8009                 :            : 
    8010                 :          0 :         func = lookup_maybe_method(self, &_Py_ID(__len__), &unbound);
    8011         [ #  # ]:          0 :         if (func == NULL) {
    8012         [ #  # ]:          0 :             if (PyErr_Occurred()) {
    8013                 :          0 :                 return -1;
    8014                 :            :             }
    8015                 :          0 :             return 1;
    8016                 :            :         }
    8017                 :          0 :         using_len = 1;
    8018                 :            :     }
    8019                 :            : 
    8020                 :          0 :     value = call_unbound_noarg(unbound, func, self);
    8021         [ #  # ]:          0 :     if (value == NULL) {
    8022                 :          0 :         goto error;
    8023                 :            :     }
    8024                 :            : 
    8025         [ #  # ]:          0 :     if (using_len) {
    8026                 :            :         /* bool type enforced by slot_nb_len */
    8027                 :          0 :         result = PyObject_IsTrue(value);
    8028                 :            :     }
    8029         [ #  # ]:          0 :     else if (PyBool_Check(value)) {
    8030                 :          0 :         result = PyObject_IsTrue(value);
    8031                 :            :     }
    8032                 :            :     else {
    8033                 :          0 :         PyErr_Format(PyExc_TypeError,
    8034                 :            :                      "__bool__ should return "
    8035                 :            :                      "bool, returned %s",
    8036                 :          0 :                      Py_TYPE(value)->tp_name);
    8037                 :          0 :         result = -1;
    8038                 :            :     }
    8039                 :            : 
    8040                 :          0 :     Py_DECREF(value);
    8041                 :          0 :     Py_DECREF(func);
    8042                 :          0 :     return result;
    8043                 :            : 
    8044                 :          0 : error:
    8045                 :          0 :     Py_DECREF(func);
    8046                 :          0 :     return -1;
    8047                 :            : }
    8048                 :            : 
    8049                 :            : 
    8050                 :            : static PyObject *
    8051                 :         22 : slot_nb_index(PyObject *self)
    8052                 :            : {
    8053                 :         22 :     PyObject *stack[1] = {self};
    8054                 :         22 :     return vectorcall_method(&_Py_ID(__index__), stack, 1);
    8055                 :            : }
    8056                 :            : 
    8057                 :            : 
    8058                 :          0 : SLOT0(slot_nb_invert, __invert__)
    8059   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_lshift, nb_lshift, __lshift__, __rlshift__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8060   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_rshift, nb_rshift, __rshift__, __rrshift__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8061   [ +  -  +  -  :        144 : SLOT1BIN(slot_nb_and, nb_and, __and__, __rand__)
          +  -  +  -  -  
          +  -  -  -  -  
          -  -  -  -  -  
          -  -  -  -  -  
                   +  - ]
    8062   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_xor, nb_xor, __xor__, __rxor__)
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8063   [ +  +  +  -  :         13 : SLOT1BIN(slot_nb_or, nb_or, __or__, __ror__)
          +  -  +  -  +  
          +  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                   +  - ]
    8064                 :            : 
    8065                 :      10480 : SLOT0(slot_nb_int, __int__)
    8066                 :         26 : SLOT0(slot_nb_float, __float__)
    8067                 :          0 : SLOT1(slot_nb_inplace_add, __iadd__, PyObject *)
    8068                 :          0 : SLOT1(slot_nb_inplace_subtract, __isub__, PyObject *)
    8069                 :          0 : SLOT1(slot_nb_inplace_multiply, __imul__, PyObject *)
    8070                 :          0 : SLOT1(slot_nb_inplace_matrix_multiply, __imatmul__, PyObject *)
    8071                 :          0 : SLOT1(slot_nb_inplace_remainder, __imod__, PyObject *)
    8072                 :            : /* Can't use SLOT1 here, because nb_inplace_power is ternary */
    8073                 :            : static PyObject *
    8074                 :          0 : slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2)
    8075                 :            : {
    8076                 :          0 :     PyObject *stack[2] = {self, arg1};
    8077                 :          0 :     return vectorcall_method(&_Py_ID(__ipow__), stack, 2);
    8078                 :            : }
    8079                 :          0 : SLOT1(slot_nb_inplace_lshift, __ilshift__, PyObject *)
    8080                 :          0 : SLOT1(slot_nb_inplace_rshift, __irshift__, PyObject *)
    8081                 :          0 : SLOT1(slot_nb_inplace_and, __iand__, PyObject *)
    8082                 :          0 : SLOT1(slot_nb_inplace_xor, __ixor__, PyObject *)
    8083                 :          0 : SLOT1(slot_nb_inplace_or, __ior__, PyObject *)
    8084   [ #  #  #  #  :          0 : SLOT1BIN(slot_nb_floor_divide, nb_floor_divide,
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
          #  #  #  #  #  
                   #  # ]
    8085                 :            :          __floordiv__, __rfloordiv__)
    8086   [ +  +  +  -  :      30969 : SLOT1BIN(slot_nb_true_divide, nb_true_divide, __truediv__, __rtruediv__)
          -  +  +  -  +  
          -  -  +  -  -  
          -  -  -  -  -  
          -  -  +  -  -  
                   -  - ]
    8087                 :          0 : SLOT1(slot_nb_inplace_floor_divide, __ifloordiv__, PyObject *)
    8088                 :          0 : SLOT1(slot_nb_inplace_true_divide, __itruediv__, PyObject *)
    8089                 :            : 
    8090                 :            : static PyObject *
    8091                 :       3132 : slot_tp_repr(PyObject *self)
    8092                 :            : {
    8093                 :            :     PyObject *func, *res;
    8094                 :            :     int unbound;
    8095                 :            : 
    8096                 :       3132 :     func = lookup_maybe_method(self, &_Py_ID(__repr__), &unbound);
    8097         [ +  - ]:       3132 :     if (func != NULL) {
    8098                 :       3132 :         res = call_unbound_noarg(unbound, func, self);
    8099                 :       3132 :         Py_DECREF(func);
    8100                 :       3132 :         return res;
    8101                 :            :     }
    8102                 :          0 :     PyErr_Clear();
    8103                 :          0 :     return PyUnicode_FromFormat("<%s object at %p>",
    8104                 :          0 :                                Py_TYPE(self)->tp_name, self);
    8105                 :            : }
    8106                 :            : 
    8107                 :         94 : SLOT0(slot_tp_str, __str__)
    8108                 :            : 
    8109                 :            : static Py_hash_t
    8110                 :        317 : slot_tp_hash(PyObject *self)
    8111                 :            : {
    8112                 :            :     PyObject *func, *res;
    8113                 :            :     Py_ssize_t h;
    8114                 :            :     int unbound;
    8115                 :            : 
    8116                 :        317 :     func = lookup_maybe_method(self, &_Py_ID(__hash__), &unbound);
    8117                 :            : 
    8118         [ -  + ]:        317 :     if (func == Py_None) {
    8119                 :          0 :         Py_SETREF(func, NULL);
    8120                 :            :     }
    8121                 :            : 
    8122         [ -  + ]:        317 :     if (func == NULL) {
    8123                 :          0 :         return PyObject_HashNotImplemented(self);
    8124                 :            :     }
    8125                 :            : 
    8126                 :        317 :     res = call_unbound_noarg(unbound, func, self);
    8127                 :        317 :     Py_DECREF(func);
    8128         [ -  + ]:        317 :     if (res == NULL)
    8129                 :          0 :         return -1;
    8130                 :            : 
    8131         [ -  + ]:        317 :     if (!PyLong_Check(res)) {
    8132                 :          0 :         PyErr_SetString(PyExc_TypeError,
    8133                 :            :                         "__hash__ method should return an integer");
    8134                 :          0 :         return -1;
    8135                 :            :     }
    8136                 :            :     /* Transform the PyLong `res` to a Py_hash_t `h`.  For an existing
    8137                 :            :        hashable Python object x, hash(x) will always lie within the range of
    8138                 :            :        Py_hash_t.  Therefore our transformation must preserve values that
    8139                 :            :        already lie within this range, to ensure that if x.__hash__() returns
    8140                 :            :        hash(y) then hash(x) == hash(y). */
    8141                 :        317 :     h = PyLong_AsSsize_t(res);
    8142   [ -  +  -  - ]:        317 :     if (h == -1 && PyErr_Occurred()) {
    8143                 :            :         /* res was not within the range of a Py_hash_t, so we're free to
    8144                 :            :            use any sufficiently bit-mixing transformation;
    8145                 :            :            long.__hash__ will do nicely. */
    8146                 :          0 :         PyErr_Clear();
    8147                 :          0 :         h = PyLong_Type.tp_hash(res);
    8148                 :            :     }
    8149                 :            :     /* -1 is reserved for errors. */
    8150         [ -  + ]:        317 :     if (h == -1)
    8151                 :          0 :         h = -2;
    8152                 :        317 :     Py_DECREF(res);
    8153                 :        317 :     return h;
    8154                 :            : }
    8155                 :            : 
    8156                 :            : static PyObject *
    8157                 :        240 : slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds)
    8158                 :            : {
    8159                 :        240 :     PyThreadState *tstate = _PyThreadState_GET();
    8160                 :            :     int unbound;
    8161                 :            : 
    8162                 :        240 :     PyObject *meth = lookup_method(self, &_Py_ID(__call__), &unbound);
    8163         [ -  + ]:        240 :     if (meth == NULL) {
    8164                 :          0 :         return NULL;
    8165                 :            :     }
    8166                 :            : 
    8167                 :            :     PyObject *res;
    8168         [ +  - ]:        240 :     if (unbound) {
    8169                 :        240 :         res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds);
    8170                 :            :     }
    8171                 :            :     else {
    8172                 :          0 :         res = _PyObject_Call(tstate, meth, args, kwds);
    8173                 :            :     }
    8174                 :            : 
    8175                 :        240 :     Py_DECREF(meth);
    8176                 :        240 :     return res;
    8177                 :            : }
    8178                 :            : 
    8179                 :            : /* There are two slot dispatch functions for tp_getattro.
    8180                 :            : 
    8181                 :            :    - _Py_slot_tp_getattro() is used when __getattribute__ is overridden
    8182                 :            :      but no __getattr__ hook is present;
    8183                 :            : 
    8184                 :            :    - _Py_slot_tp_getattr_hook() is used when a __getattr__ hook is present.
    8185                 :            : 
    8186                 :            :    The code in update_one_slot() always installs _Py_slot_tp_getattr_hook();
    8187                 :            :    this detects the absence of __getattr__ and then installs the simpler
    8188                 :            :    slot if necessary. */
    8189                 :            : 
    8190                 :            : PyObject *
    8191                 :         12 : _Py_slot_tp_getattro(PyObject *self, PyObject *name)
    8192                 :            : {
    8193                 :         12 :     PyObject *stack[2] = {self, name};
    8194                 :         12 :     return vectorcall_method(&_Py_ID(__getattribute__), stack, 2);
    8195                 :            : }
    8196                 :            : 
    8197                 :            : static inline PyObject *
    8198                 :         25 : call_attribute(PyObject *self, PyObject *attr, PyObject *name)
    8199                 :            : {
    8200                 :         25 :     PyObject *res, *descr = NULL;
    8201                 :            : 
    8202         [ +  - ]:         25 :     if (_PyType_HasFeature(Py_TYPE(attr), Py_TPFLAGS_METHOD_DESCRIPTOR)) {
    8203                 :         25 :         PyObject *args[] = { self, name };
    8204                 :         25 :         res = PyObject_Vectorcall(attr, args, 2, NULL);
    8205                 :         25 :         return res;
    8206                 :            :     }
    8207                 :            : 
    8208                 :          0 :     descrgetfunc f = Py_TYPE(attr)->tp_descr_get;
    8209                 :            : 
    8210         [ #  # ]:          0 :     if (f != NULL) {
    8211                 :          0 :         descr = f(attr, self, (PyObject *)(Py_TYPE(self)));
    8212         [ #  # ]:          0 :         if (descr == NULL)
    8213                 :          0 :             return NULL;
    8214                 :            :         else
    8215                 :          0 :             attr = descr;
    8216                 :            :     }
    8217                 :          0 :     res = PyObject_CallOneArg(attr, name);
    8218                 :          0 :     Py_XDECREF(descr);
    8219                 :          0 :     return res;
    8220                 :            : }
    8221                 :            : 
    8222                 :            : PyObject *
    8223                 :        382 : _Py_slot_tp_getattr_hook(PyObject *self, PyObject *name)
    8224                 :            : {
    8225                 :        382 :     PyTypeObject *tp = Py_TYPE(self);
    8226                 :            :     PyObject *getattr, *getattribute, *res;
    8227                 :            : 
    8228                 :            :     /* speed hack: we could use lookup_maybe, but that would resolve the
    8229                 :            :        method fully for each attribute lookup for classes with
    8230                 :            :        __getattr__, even when the attribute is present. So we use
    8231                 :            :        _PyType_Lookup and create the method only when needed, with
    8232                 :            :        call_attribute. */
    8233                 :        382 :     getattr = _PyType_Lookup(tp, &_Py_ID(__getattr__));
    8234         [ +  + ]:        382 :     if (getattr == NULL) {
    8235                 :            :         /* No __getattr__ hook: use a simpler dispatcher */
    8236                 :          3 :         tp->tp_getattro = _Py_slot_tp_getattro;
    8237                 :          3 :         return _Py_slot_tp_getattro(self, name);
    8238                 :            :     }
    8239                 :        379 :     Py_INCREF(getattr);
    8240                 :            :     /* speed hack: we could use lookup_maybe, but that would resolve the
    8241                 :            :        method fully for each attribute lookup for classes with
    8242                 :            :        __getattr__, even when self has the default __getattribute__
    8243                 :            :        method. So we use _PyType_Lookup and create the method only when
    8244                 :            :        needed, with call_attribute. */
    8245                 :        379 :     getattribute = _PyType_Lookup(tp, &_Py_ID(__getattribute__));
    8246   [ +  -  +  - ]:        758 :     if (getattribute == NULL ||
    8247                 :        379 :         (Py_IS_TYPE(getattribute, &PyWrapperDescr_Type) &&
    8248         [ +  - ]:        379 :          ((PyWrapperDescrObject *)getattribute)->d_wrapped ==
    8249                 :            :          (void *)PyObject_GenericGetAttr))
    8250                 :            :         /* finding nothing is reasonable when __getattr__ is defined */
    8251                 :        379 :         res = _PyObject_GenericTryGetAttr(self, name);
    8252                 :            :     else {
    8253                 :          0 :         Py_INCREF(getattribute);
    8254                 :          0 :         res = call_attribute(self, getattribute, name);
    8255                 :          0 :         Py_DECREF(getattribute);
    8256                 :            :     }
    8257         [ +  + ]:        379 :     if (res == NULL) {
    8258         [ -  + ]:         25 :         if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
    8259                 :          0 :             PyErr_Clear();
    8260                 :            :         }
    8261                 :         25 :         res = call_attribute(self, getattr, name);
    8262                 :            :     }
    8263                 :        379 :     Py_DECREF(getattr);
    8264                 :        379 :     return res;
    8265                 :            : }
    8266                 :            : 
    8267                 :            : static int
    8268                 :       2024 : slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value)
    8269                 :            : {
    8270                 :            :     PyObject *stack[3];
    8271                 :            :     PyObject *res;
    8272                 :            : 
    8273                 :       2024 :     stack[0] = self;
    8274                 :       2024 :     stack[1] = name;
    8275         [ +  + ]:       2024 :     if (value == NULL) {
    8276                 :        187 :         res = vectorcall_method(&_Py_ID(__delattr__), stack, 2);
    8277                 :            :     }
    8278                 :            :     else {
    8279                 :       1837 :         stack[2] = value;
    8280                 :       1837 :         res = vectorcall_method(&_Py_ID(__setattr__), stack, 3);
    8281                 :            :     }
    8282         [ -  + ]:       2024 :     if (res == NULL)
    8283                 :          0 :         return -1;
    8284                 :       2024 :     Py_DECREF(res);
    8285                 :       2024 :     return 0;
    8286                 :            : }
    8287                 :            : 
    8288                 :            : static PyObject *name_op[] = {
    8289                 :            :     &_Py_ID(__lt__),
    8290                 :            :     &_Py_ID(__le__),
    8291                 :            :     &_Py_ID(__eq__),
    8292                 :            :     &_Py_ID(__ne__),
    8293                 :            :     &_Py_ID(__gt__),
    8294                 :            :     &_Py_ID(__ge__),
    8295                 :            : };
    8296                 :            : 
    8297                 :            : static PyObject *
    8298                 :      53754 : slot_tp_richcompare(PyObject *self, PyObject *other, int op)
    8299                 :            : {
    8300                 :      53754 :     PyThreadState *tstate = _PyThreadState_GET();
    8301                 :            : 
    8302                 :            :     int unbound;
    8303                 :      53754 :     PyObject *func = lookup_maybe_method(self, name_op[op], &unbound);
    8304         [ -  + ]:      53754 :     if (func == NULL) {
    8305                 :          0 :         PyErr_Clear();
    8306                 :          0 :         Py_RETURN_NOTIMPLEMENTED;
    8307                 :            :     }
    8308                 :            : 
    8309                 :      53754 :     PyObject *stack[2] = {self, other};
    8310                 :      53754 :     PyObject *res = vectorcall_unbound(tstate, unbound, func, stack, 2);
    8311                 :      53754 :     Py_DECREF(func);
    8312                 :      53754 :     return res;
    8313                 :            : }
    8314                 :            : 
    8315                 :            : static PyObject *
    8316                 :         47 : slot_tp_iter(PyObject *self)
    8317                 :            : {
    8318                 :            :     int unbound;
    8319                 :            :     PyObject *func, *res;
    8320                 :            : 
    8321                 :         47 :     func = lookup_maybe_method(self, &_Py_ID(__iter__), &unbound);
    8322         [ -  + ]:         47 :     if (func == Py_None) {
    8323                 :          0 :         Py_DECREF(func);
    8324                 :          0 :         PyErr_Format(PyExc_TypeError,
    8325                 :            :                      "'%.200s' object is not iterable",
    8326                 :          0 :                      Py_TYPE(self)->tp_name);
    8327                 :          0 :         return NULL;
    8328                 :            :     }
    8329                 :            : 
    8330         [ +  - ]:         47 :     if (func != NULL) {
    8331                 :         47 :         res = call_unbound_noarg(unbound, func, self);
    8332                 :         47 :         Py_DECREF(func);
    8333                 :         47 :         return res;
    8334                 :            :     }
    8335                 :            : 
    8336                 :          0 :     PyErr_Clear();
    8337                 :          0 :     func = lookup_maybe_method(self, &_Py_ID(__getitem__), &unbound);
    8338         [ #  # ]:          0 :     if (func == NULL) {
    8339                 :          0 :         PyErr_Format(PyExc_TypeError,
    8340                 :            :                      "'%.200s' object is not iterable",
    8341                 :          0 :                      Py_TYPE(self)->tp_name);
    8342                 :          0 :         return NULL;
    8343                 :            :     }
    8344                 :          0 :     Py_DECREF(func);
    8345                 :          0 :     return PySeqIter_New(self);
    8346                 :            : }
    8347                 :            : 
    8348                 :            : static PyObject *
    8349                 :          0 : slot_tp_iternext(PyObject *self)
    8350                 :            : {
    8351                 :          0 :     PyObject *stack[1] = {self};
    8352                 :          0 :     return vectorcall_method(&_Py_ID(__next__), stack, 1);
    8353                 :            : }
    8354                 :            : 
    8355                 :            : static PyObject *
    8356                 :       1502 : slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type)
    8357                 :            : {
    8358                 :       1502 :     PyTypeObject *tp = Py_TYPE(self);
    8359                 :            :     PyObject *get;
    8360                 :            : 
    8361                 :       1502 :     get = _PyType_Lookup(tp, &_Py_ID(__get__));
    8362         [ -  + ]:       1502 :     if (get == NULL) {
    8363                 :            :         /* Avoid further slowdowns */
    8364         [ #  # ]:          0 :         if (tp->tp_descr_get == slot_tp_descr_get)
    8365                 :          0 :             tp->tp_descr_get = NULL;
    8366                 :          0 :         return Py_NewRef(self);
    8367                 :            :     }
    8368         [ +  + ]:       1502 :     if (obj == NULL)
    8369                 :       1463 :         obj = Py_None;
    8370         [ -  + ]:       1502 :     if (type == NULL)
    8371                 :          0 :         type = Py_None;
    8372                 :       1502 :     PyObject *stack[3] = {self, obj, type};
    8373                 :       1502 :     return PyObject_Vectorcall(get, stack, 3, NULL);
    8374                 :            : }
    8375                 :            : 
    8376                 :            : static int
    8377                 :          0 : slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value)
    8378                 :            : {
    8379                 :            :     PyObject* stack[3];
    8380                 :            :     PyObject *res;
    8381                 :            : 
    8382                 :          0 :     stack[0] = self;
    8383                 :          0 :     stack[1] = target;
    8384         [ #  # ]:          0 :     if (value == NULL) {
    8385                 :          0 :         res = vectorcall_method(&_Py_ID(__delete__), stack, 2);
    8386                 :            :     }
    8387                 :            :     else {
    8388                 :          0 :         stack[2] = value;
    8389                 :          0 :         res = vectorcall_method(&_Py_ID(__set__), stack, 3);
    8390                 :            :     }
    8391         [ #  # ]:          0 :     if (res == NULL)
    8392                 :          0 :         return -1;
    8393                 :          0 :     Py_DECREF(res);
    8394                 :          0 :     return 0;
    8395                 :            : }
    8396                 :            : 
    8397                 :            : static int
    8398                 :     139934 : slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds)
    8399                 :            : {
    8400                 :     139934 :     PyThreadState *tstate = _PyThreadState_GET();
    8401                 :            : 
    8402                 :            :     int unbound;
    8403                 :     139934 :     PyObject *meth = lookup_method(self, &_Py_ID(__init__), &unbound);
    8404         [ -  + ]:     139934 :     if (meth == NULL) {
    8405                 :          0 :         return -1;
    8406                 :            :     }
    8407                 :            : 
    8408                 :            :     PyObject *res;
    8409         [ +  - ]:     139934 :     if (unbound) {
    8410                 :     139934 :         res = _PyObject_Call_Prepend(tstate, meth, self, args, kwds);
    8411                 :            :     }
    8412                 :            :     else {
    8413                 :          0 :         res = _PyObject_Call(tstate, meth, args, kwds);
    8414                 :            :     }
    8415                 :     139934 :     Py_DECREF(meth);
    8416         [ +  + ]:     139934 :     if (res == NULL)
    8417                 :        150 :         return -1;
    8418         [ -  + ]:     139784 :     if (res != Py_None) {
    8419                 :          0 :         PyErr_Format(PyExc_TypeError,
    8420                 :            :                      "__init__() should return None, not '%.200s'",
    8421                 :          0 :                      Py_TYPE(res)->tp_name);
    8422                 :          0 :         Py_DECREF(res);
    8423                 :          0 :         return -1;
    8424                 :            :     }
    8425                 :     139784 :     Py_DECREF(res);
    8426                 :     139784 :     return 0;
    8427                 :            : }
    8428                 :            : 
    8429                 :            : static PyObject *
    8430                 :      52139 : slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
    8431                 :            : {
    8432                 :      52139 :     PyThreadState *tstate = _PyThreadState_GET();
    8433                 :            :     PyObject *func, *result;
    8434                 :            : 
    8435                 :      52139 :     func = PyObject_GetAttr((PyObject *)type, &_Py_ID(__new__));
    8436         [ -  + ]:      52139 :     if (func == NULL) {
    8437                 :          0 :         return NULL;
    8438                 :            :     }
    8439                 :            : 
    8440                 :      52139 :     result = _PyObject_Call_Prepend(tstate, func, (PyObject *)type, args, kwds);
    8441                 :      52139 :     Py_DECREF(func);
    8442                 :      52139 :     return result;
    8443                 :            : }
    8444                 :            : 
    8445                 :            : static void
    8446                 :          0 : slot_tp_finalize(PyObject *self)
    8447                 :            : {
    8448                 :            :     int unbound;
    8449                 :            :     PyObject *del, *res;
    8450                 :            : 
    8451                 :            :     /* Save the current exception, if any. */
    8452                 :          0 :     PyObject *exc = PyErr_GetRaisedException();
    8453                 :            : 
    8454                 :            :     /* Execute __del__ method, if any. */
    8455                 :          0 :     del = lookup_maybe_method(self, &_Py_ID(__del__), &unbound);
    8456         [ #  # ]:          0 :     if (del != NULL) {
    8457                 :          0 :         res = call_unbound_noarg(unbound, del, self);
    8458         [ #  # ]:          0 :         if (res == NULL)
    8459                 :          0 :             PyErr_WriteUnraisable(del);
    8460                 :            :         else
    8461                 :          0 :             Py_DECREF(res);
    8462                 :          0 :         Py_DECREF(del);
    8463                 :            :     }
    8464                 :            : 
    8465                 :            :     /* Restore the saved exception. */
    8466                 :          0 :     PyErr_SetRaisedException(exc);
    8467                 :          0 : }
    8468                 :            : 
    8469                 :            : static PyObject *
    8470                 :          0 : slot_am_await(PyObject *self)
    8471                 :            : {
    8472                 :            :     int unbound;
    8473                 :            :     PyObject *func, *res;
    8474                 :            : 
    8475                 :          0 :     func = lookup_maybe_method(self, &_Py_ID(__await__), &unbound);
    8476         [ #  # ]:          0 :     if (func != NULL) {
    8477                 :          0 :         res = call_unbound_noarg(unbound, func, self);
    8478                 :          0 :         Py_DECREF(func);
    8479                 :          0 :         return res;
    8480                 :            :     }
    8481                 :          0 :     PyErr_Format(PyExc_AttributeError,
    8482                 :            :                  "object %.50s does not have __await__ method",
    8483                 :          0 :                  Py_TYPE(self)->tp_name);
    8484                 :          0 :     return NULL;
    8485                 :            : }
    8486                 :            : 
    8487                 :            : static PyObject *
    8488                 :          0 : slot_am_aiter(PyObject *self)
    8489                 :            : {
    8490                 :            :     int unbound;
    8491                 :            :     PyObject *func, *res;
    8492                 :            : 
    8493                 :          0 :     func = lookup_maybe_method(self, &_Py_ID(__aiter__), &unbound);
    8494         [ #  # ]:          0 :     if (func != NULL) {
    8495                 :          0 :         res = call_unbound_noarg(unbound, func, self);
    8496                 :          0 :         Py_DECREF(func);
    8497                 :          0 :         return res;
    8498                 :            :     }
    8499                 :          0 :     PyErr_Format(PyExc_AttributeError,
    8500                 :            :                  "object %.50s does not have __aiter__ method",
    8501                 :          0 :                  Py_TYPE(self)->tp_name);
    8502                 :          0 :     return NULL;
    8503                 :            : }
    8504                 :            : 
    8505                 :            : static PyObject *
    8506                 :          0 : slot_am_anext(PyObject *self)
    8507                 :            : {
    8508                 :            :     int unbound;
    8509                 :            :     PyObject *func, *res;
    8510                 :            : 
    8511                 :          0 :     func = lookup_maybe_method(self, &_Py_ID(__anext__), &unbound);
    8512         [ #  # ]:          0 :     if (func != NULL) {
    8513                 :          0 :         res = call_unbound_noarg(unbound, func, self);
    8514                 :          0 :         Py_DECREF(func);
    8515                 :          0 :         return res;
    8516                 :            :     }
    8517                 :          0 :     PyErr_Format(PyExc_AttributeError,
    8518                 :            :                  "object %.50s does not have __anext__ method",
    8519                 :          0 :                  Py_TYPE(self)->tp_name);
    8520                 :          0 :     return NULL;
    8521                 :            : }
    8522                 :            : 
    8523                 :            : /*
    8524                 :            : Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper functions.
    8525                 :            : 
    8526                 :            : The table is ordered by offsets relative to the 'PyHeapTypeObject' structure,
    8527                 :            : which incorporates the additional structures used for numbers, sequences and
    8528                 :            : mappings.  Note that multiple names may map to the same slot (e.g. __eq__,
    8529                 :            : __ne__ etc. all map to tp_richcompare) and one name may map to multiple slots
    8530                 :            : (e.g. __str__ affects tp_str as well as tp_repr). The table is terminated with
    8531                 :            : an all-zero entry.
    8532                 :            : */
    8533                 :            : 
    8534                 :            : #undef TPSLOT
    8535                 :            : #undef FLSLOT
    8536                 :            : #undef AMSLOT
    8537                 :            : #undef ETSLOT
    8538                 :            : #undef SQSLOT
    8539                 :            : #undef MPSLOT
    8540                 :            : #undef NBSLOT
    8541                 :            : #undef UNSLOT
    8542                 :            : #undef IBSLOT
    8543                 :            : #undef BINSLOT
    8544                 :            : #undef RBINSLOT
    8545                 :            : 
    8546                 :            : #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8547                 :            :     {#NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
    8548                 :            :      PyDoc_STR(DOC), .name_strobj = &_Py_ID(NAME)}
    8549                 :            : #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \
    8550                 :            :     {#NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
    8551                 :            :      PyDoc_STR(DOC), FLAGS, .name_strobj = &_Py_ID(NAME) }
    8552                 :            : #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8553                 :            :     {#NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \
    8554                 :            :      PyDoc_STR(DOC), .name_strobj = &_Py_ID(NAME) }
    8555                 :            : #define AMSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8556                 :            :     ETSLOT(NAME, as_async.SLOT, FUNCTION, WRAPPER, DOC)
    8557                 :            : #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8558                 :            :     ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC)
    8559                 :            : #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8560                 :            :     ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC)
    8561                 :            : #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8562                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC)
    8563                 :            : #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8564                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
    8565                 :            :            #NAME "($self, /)\n--\n\n" DOC)
    8566                 :            : #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \
    8567                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \
    8568                 :            :            #NAME "($self, value, /)\n--\n\nReturn self" DOC "value.")
    8569                 :            : #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \
    8570                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
    8571                 :            :            #NAME "($self, value, /)\n--\n\nReturn self" DOC "value.")
    8572                 :            : #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \
    8573                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
    8574                 :            :            #NAME "($self, value, /)\n--\n\nReturn value" DOC "self.")
    8575                 :            : #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
    8576                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \
    8577                 :            :            #NAME "($self, value, /)\n--\n\n" DOC)
    8578                 :            : #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \
    8579                 :            :     ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \
    8580                 :            :            #NAME "($self, value, /)\n--\n\n" DOC)
    8581                 :            : 
    8582                 :            : static pytype_slotdef slotdefs[] = {
    8583                 :            :     TPSLOT(__getattribute__, tp_getattr, NULL, NULL, ""),
    8584                 :            :     TPSLOT(__getattr__, tp_getattr, NULL, NULL, ""),
    8585                 :            :     TPSLOT(__setattr__, tp_setattr, NULL, NULL, ""),
    8586                 :            :     TPSLOT(__delattr__, tp_setattr, NULL, NULL, ""),
    8587                 :            :     TPSLOT(__repr__, tp_repr, slot_tp_repr, wrap_unaryfunc,
    8588                 :            :            "__repr__($self, /)\n--\n\nReturn repr(self)."),
    8589                 :            :     TPSLOT(__hash__, tp_hash, slot_tp_hash, wrap_hashfunc,
    8590                 :            :            "__hash__($self, /)\n--\n\nReturn hash(self)."),
    8591                 :            :     FLSLOT(__call__, tp_call, slot_tp_call, (wrapperfunc)(void(*)(void))wrap_call,
    8592                 :            :            "__call__($self, /, *args, **kwargs)\n--\n\nCall self as a function.",
    8593                 :            :            PyWrapperFlag_KEYWORDS),
    8594                 :            :     TPSLOT(__str__, tp_str, slot_tp_str, wrap_unaryfunc,
    8595                 :            :            "__str__($self, /)\n--\n\nReturn str(self)."),
    8596                 :            :     TPSLOT(__getattribute__, tp_getattro, _Py_slot_tp_getattr_hook,
    8597                 :            :            wrap_binaryfunc,
    8598                 :            :            "__getattribute__($self, name, /)\n--\n\nReturn getattr(self, name)."),
    8599                 :            :     TPSLOT(__getattr__, tp_getattro, _Py_slot_tp_getattr_hook, NULL, ""),
    8600                 :            :     TPSLOT(__setattr__, tp_setattro, slot_tp_setattro, wrap_setattr,
    8601                 :            :            "__setattr__($self, name, value, /)\n--\n\nImplement setattr(self, name, value)."),
    8602                 :            :     TPSLOT(__delattr__, tp_setattro, slot_tp_setattro, wrap_delattr,
    8603                 :            :            "__delattr__($self, name, /)\n--\n\nImplement delattr(self, name)."),
    8604                 :            :     TPSLOT(__lt__, tp_richcompare, slot_tp_richcompare, richcmp_lt,
    8605                 :            :            "__lt__($self, value, /)\n--\n\nReturn self<value."),
    8606                 :            :     TPSLOT(__le__, tp_richcompare, slot_tp_richcompare, richcmp_le,
    8607                 :            :            "__le__($self, value, /)\n--\n\nReturn self<=value."),
    8608                 :            :     TPSLOT(__eq__, tp_richcompare, slot_tp_richcompare, richcmp_eq,
    8609                 :            :            "__eq__($self, value, /)\n--\n\nReturn self==value."),
    8610                 :            :     TPSLOT(__ne__, tp_richcompare, slot_tp_richcompare, richcmp_ne,
    8611                 :            :            "__ne__($self, value, /)\n--\n\nReturn self!=value."),
    8612                 :            :     TPSLOT(__gt__, tp_richcompare, slot_tp_richcompare, richcmp_gt,
    8613                 :            :            "__gt__($self, value, /)\n--\n\nReturn self>value."),
    8614                 :            :     TPSLOT(__ge__, tp_richcompare, slot_tp_richcompare, richcmp_ge,
    8615                 :            :            "__ge__($self, value, /)\n--\n\nReturn self>=value."),
    8616                 :            :     TPSLOT(__iter__, tp_iter, slot_tp_iter, wrap_unaryfunc,
    8617                 :            :            "__iter__($self, /)\n--\n\nImplement iter(self)."),
    8618                 :            :     TPSLOT(__next__, tp_iternext, slot_tp_iternext, wrap_next,
    8619                 :            :            "__next__($self, /)\n--\n\nImplement next(self)."),
    8620                 :            :     TPSLOT(__get__, tp_descr_get, slot_tp_descr_get, wrap_descr_get,
    8621                 :            :            "__get__($self, instance, owner=None, /)\n--\n\nReturn an attribute of instance, which is of type owner."),
    8622                 :            :     TPSLOT(__set__, tp_descr_set, slot_tp_descr_set, wrap_descr_set,
    8623                 :            :            "__set__($self, instance, value, /)\n--\n\nSet an attribute of instance to value."),
    8624                 :            :     TPSLOT(__delete__, tp_descr_set, slot_tp_descr_set,
    8625                 :            :            wrap_descr_delete,
    8626                 :            :            "__delete__($self, instance, /)\n--\n\nDelete an attribute of instance."),
    8627                 :            :     FLSLOT(__init__, tp_init, slot_tp_init, (wrapperfunc)(void(*)(void))wrap_init,
    8628                 :            :            "__init__($self, /, *args, **kwargs)\n--\n\n"
    8629                 :            :            "Initialize self.  See help(type(self)) for accurate signature.",
    8630                 :            :            PyWrapperFlag_KEYWORDS),
    8631                 :            :     TPSLOT(__new__, tp_new, slot_tp_new, NULL,
    8632                 :            :            "__new__(type, /, *args, **kwargs)\n--\n\n"
    8633                 :            :            "Create and return new object.  See help(type) for accurate signature."),
    8634                 :            :     TPSLOT(__del__, tp_finalize, slot_tp_finalize, (wrapperfunc)wrap_del, ""),
    8635                 :            : 
    8636                 :            :     AMSLOT(__await__, am_await, slot_am_await, wrap_unaryfunc,
    8637                 :            :            "__await__($self, /)\n--\n\nReturn an iterator to be used in await expression."),
    8638                 :            :     AMSLOT(__aiter__, am_aiter, slot_am_aiter, wrap_unaryfunc,
    8639                 :            :            "__aiter__($self, /)\n--\n\nReturn an awaitable, that resolves in asynchronous iterator."),
    8640                 :            :     AMSLOT(__anext__, am_anext, slot_am_anext, wrap_unaryfunc,
    8641                 :            :            "__anext__($self, /)\n--\n\nReturn a value or raise StopAsyncIteration."),
    8642                 :            : 
    8643                 :            :     BINSLOT(__add__, nb_add, slot_nb_add,
    8644                 :            :            "+"),
    8645                 :            :     RBINSLOT(__radd__, nb_add, slot_nb_add,
    8646                 :            :            "+"),
    8647                 :            :     BINSLOT(__sub__, nb_subtract, slot_nb_subtract,
    8648                 :            :            "-"),
    8649                 :            :     RBINSLOT(__rsub__, nb_subtract, slot_nb_subtract,
    8650                 :            :            "-"),
    8651                 :            :     BINSLOT(__mul__, nb_multiply, slot_nb_multiply,
    8652                 :            :            "*"),
    8653                 :            :     RBINSLOT(__rmul__, nb_multiply, slot_nb_multiply,
    8654                 :            :            "*"),
    8655                 :            :     BINSLOT(__mod__, nb_remainder, slot_nb_remainder,
    8656                 :            :            "%"),
    8657                 :            :     RBINSLOT(__rmod__, nb_remainder, slot_nb_remainder,
    8658                 :            :            "%"),
    8659                 :            :     BINSLOTNOTINFIX(__divmod__, nb_divmod, slot_nb_divmod,
    8660                 :            :            "Return divmod(self, value)."),
    8661                 :            :     RBINSLOTNOTINFIX(__rdivmod__, nb_divmod, slot_nb_divmod,
    8662                 :            :            "Return divmod(value, self)."),
    8663                 :            :     NBSLOT(__pow__, nb_power, slot_nb_power, wrap_ternaryfunc,
    8664                 :            :            "__pow__($self, value, mod=None, /)\n--\n\nReturn pow(self, value, mod)."),
    8665                 :            :     NBSLOT(__rpow__, nb_power, slot_nb_power, wrap_ternaryfunc_r,
    8666                 :            :            "__rpow__($self, value, mod=None, /)\n--\n\nReturn pow(value, self, mod)."),
    8667                 :            :     UNSLOT(__neg__, nb_negative, slot_nb_negative, wrap_unaryfunc, "-self"),
    8668                 :            :     UNSLOT(__pos__, nb_positive, slot_nb_positive, wrap_unaryfunc, "+self"),
    8669                 :            :     UNSLOT(__abs__, nb_absolute, slot_nb_absolute, wrap_unaryfunc,
    8670                 :            :            "abs(self)"),
    8671                 :            :     UNSLOT(__bool__, nb_bool, slot_nb_bool, wrap_inquirypred,
    8672                 :            :            "True if self else False"),
    8673                 :            :     UNSLOT(__invert__, nb_invert, slot_nb_invert, wrap_unaryfunc, "~self"),
    8674                 :            :     BINSLOT(__lshift__, nb_lshift, slot_nb_lshift, "<<"),
    8675                 :            :     RBINSLOT(__rlshift__, nb_lshift, slot_nb_lshift, "<<"),
    8676                 :            :     BINSLOT(__rshift__, nb_rshift, slot_nb_rshift, ">>"),
    8677                 :            :     RBINSLOT(__rrshift__, nb_rshift, slot_nb_rshift, ">>"),
    8678                 :            :     BINSLOT(__and__, nb_and, slot_nb_and, "&"),
    8679                 :            :     RBINSLOT(__rand__, nb_and, slot_nb_and, "&"),
    8680                 :            :     BINSLOT(__xor__, nb_xor, slot_nb_xor, "^"),
    8681                 :            :     RBINSLOT(__rxor__, nb_xor, slot_nb_xor, "^"),
    8682                 :            :     BINSLOT(__or__, nb_or, slot_nb_or, "|"),
    8683                 :            :     RBINSLOT(__ror__, nb_or, slot_nb_or, "|"),
    8684                 :            :     UNSLOT(__int__, nb_int, slot_nb_int, wrap_unaryfunc,
    8685                 :            :            "int(self)"),
    8686                 :            :     UNSLOT(__float__, nb_float, slot_nb_float, wrap_unaryfunc,
    8687                 :            :            "float(self)"),
    8688                 :            :     IBSLOT(__iadd__, nb_inplace_add, slot_nb_inplace_add,
    8689                 :            :            wrap_binaryfunc, "+="),
    8690                 :            :     IBSLOT(__isub__, nb_inplace_subtract, slot_nb_inplace_subtract,
    8691                 :            :            wrap_binaryfunc, "-="),
    8692                 :            :     IBSLOT(__imul__, nb_inplace_multiply, slot_nb_inplace_multiply,
    8693                 :            :            wrap_binaryfunc, "*="),
    8694                 :            :     IBSLOT(__imod__, nb_inplace_remainder, slot_nb_inplace_remainder,
    8695                 :            :            wrap_binaryfunc, "%="),
    8696                 :            :     IBSLOT(__ipow__, nb_inplace_power, slot_nb_inplace_power,
    8697                 :            :            wrap_ternaryfunc, "**="),
    8698                 :            :     IBSLOT(__ilshift__, nb_inplace_lshift, slot_nb_inplace_lshift,
    8699                 :            :            wrap_binaryfunc, "<<="),
    8700                 :            :     IBSLOT(__irshift__, nb_inplace_rshift, slot_nb_inplace_rshift,
    8701                 :            :            wrap_binaryfunc, ">>="),
    8702                 :            :     IBSLOT(__iand__, nb_inplace_and, slot_nb_inplace_and,
    8703                 :            :            wrap_binaryfunc, "&="),
    8704                 :            :     IBSLOT(__ixor__, nb_inplace_xor, slot_nb_inplace_xor,
    8705                 :            :            wrap_binaryfunc, "^="),
    8706                 :            :     IBSLOT(__ior__, nb_inplace_or, slot_nb_inplace_or,
    8707                 :            :            wrap_binaryfunc, "|="),
    8708                 :            :     BINSLOT(__floordiv__, nb_floor_divide, slot_nb_floor_divide, "//"),
    8709                 :            :     RBINSLOT(__rfloordiv__, nb_floor_divide, slot_nb_floor_divide, "//"),
    8710                 :            :     BINSLOT(__truediv__, nb_true_divide, slot_nb_true_divide, "/"),
    8711                 :            :     RBINSLOT(__rtruediv__, nb_true_divide, slot_nb_true_divide, "/"),
    8712                 :            :     IBSLOT(__ifloordiv__, nb_inplace_floor_divide,
    8713                 :            :            slot_nb_inplace_floor_divide, wrap_binaryfunc, "//="),
    8714                 :            :     IBSLOT(__itruediv__, nb_inplace_true_divide,
    8715                 :            :            slot_nb_inplace_true_divide, wrap_binaryfunc, "/="),
    8716                 :            :     NBSLOT(__index__, nb_index, slot_nb_index, wrap_unaryfunc,
    8717                 :            :            "__index__($self, /)\n--\n\n"
    8718                 :            :            "Return self converted to an integer, if self is suitable "
    8719                 :            :            "for use as an index into a list."),
    8720                 :            :     BINSLOT(__matmul__, nb_matrix_multiply, slot_nb_matrix_multiply,
    8721                 :            :             "@"),
    8722                 :            :     RBINSLOT(__rmatmul__, nb_matrix_multiply, slot_nb_matrix_multiply,
    8723                 :            :              "@"),
    8724                 :            :     IBSLOT(__imatmul__, nb_inplace_matrix_multiply, slot_nb_inplace_matrix_multiply,
    8725                 :            :            wrap_binaryfunc, "@="),
    8726                 :            :     MPSLOT(__len__, mp_length, slot_mp_length, wrap_lenfunc,
    8727                 :            :            "__len__($self, /)\n--\n\nReturn len(self)."),
    8728                 :            :     MPSLOT(__getitem__, mp_subscript, slot_mp_subscript,
    8729                 :            :            wrap_binaryfunc,
    8730                 :            :            "__getitem__($self, key, /)\n--\n\nReturn self[key]."),
    8731                 :            :     MPSLOT(__setitem__, mp_ass_subscript, slot_mp_ass_subscript,
    8732                 :            :            wrap_objobjargproc,
    8733                 :            :            "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."),
    8734                 :            :     MPSLOT(__delitem__, mp_ass_subscript, slot_mp_ass_subscript,
    8735                 :            :            wrap_delitem,
    8736                 :            :            "__delitem__($self, key, /)\n--\n\nDelete self[key]."),
    8737                 :            : 
    8738                 :            :     SQSLOT(__len__, sq_length, slot_sq_length, wrap_lenfunc,
    8739                 :            :            "__len__($self, /)\n--\n\nReturn len(self)."),
    8740                 :            :     /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL.
    8741                 :            :        The logic in abstract.c always falls back to nb_add/nb_multiply in
    8742                 :            :        this case.  Defining both the nb_* and the sq_* slots to call the
    8743                 :            :        user-defined methods has unexpected side-effects, as shown by
    8744                 :            :        test_descr.notimplemented() */
    8745                 :            :     SQSLOT(__add__, sq_concat, NULL, wrap_binaryfunc,
    8746                 :            :            "__add__($self, value, /)\n--\n\nReturn self+value."),
    8747                 :            :     SQSLOT(__mul__, sq_repeat, NULL, wrap_indexargfunc,
    8748                 :            :            "__mul__($self, value, /)\n--\n\nReturn self*value."),
    8749                 :            :     SQSLOT(__rmul__, sq_repeat, NULL, wrap_indexargfunc,
    8750                 :            :            "__rmul__($self, value, /)\n--\n\nReturn value*self."),
    8751                 :            :     SQSLOT(__getitem__, sq_item, slot_sq_item, wrap_sq_item,
    8752                 :            :            "__getitem__($self, key, /)\n--\n\nReturn self[key]."),
    8753                 :            :     SQSLOT(__setitem__, sq_ass_item, slot_sq_ass_item, wrap_sq_setitem,
    8754                 :            :            "__setitem__($self, key, value, /)\n--\n\nSet self[key] to value."),
    8755                 :            :     SQSLOT(__delitem__, sq_ass_item, slot_sq_ass_item, wrap_sq_delitem,
    8756                 :            :            "__delitem__($self, key, /)\n--\n\nDelete self[key]."),
    8757                 :            :     SQSLOT(__contains__, sq_contains, slot_sq_contains, wrap_objobjproc,
    8758                 :            :            "__contains__($self, key, /)\n--\n\nReturn bool(key in self)."),
    8759                 :            :     SQSLOT(__iadd__, sq_inplace_concat, NULL,
    8760                 :            :            wrap_binaryfunc,
    8761                 :            :            "__iadd__($self, value, /)\n--\n\nImplement self+=value."),
    8762                 :            :     SQSLOT(__imul__, sq_inplace_repeat, NULL,
    8763                 :            :            wrap_indexargfunc,
    8764                 :            :            "__imul__($self, value, /)\n--\n\nImplement self*=value."),
    8765                 :            : 
    8766                 :            :     {NULL}
    8767                 :            : };
    8768                 :            : 
    8769                 :            : /* Given a type pointer and an offset gotten from a slotdef entry, return a
    8770                 :            :    pointer to the actual slot.  This is not quite the same as simply adding
    8771                 :            :    the offset to the type pointer, since it takes care to indirect through the
    8772                 :            :    proper indirection pointer (as_buffer, etc.); it returns NULL if the
    8773                 :            :    indirection pointer is NULL. */
    8774                 :            : static void **
    8775                 :    1656320 : slotptr(PyTypeObject *type, int ioffset)
    8776                 :            : {
    8777                 :            :     char *ptr;
    8778                 :    1656320 :     long offset = ioffset;
    8779                 :            : 
    8780                 :            :     /* Note: this depends on the order of the members of PyHeapTypeObject! */
    8781                 :            :     assert(offset >= 0);
    8782                 :            :     assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer));
    8783         [ +  + ]:    1656320 :     if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) {
    8784                 :     180977 :         ptr = (char *)type->tp_as_sequence;
    8785                 :     180977 :         offset -= offsetof(PyHeapTypeObject, as_sequence);
    8786                 :            :     }
    8787         [ +  + ]:    1475343 :     else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) {
    8788                 :      71022 :         ptr = (char *)type->tp_as_mapping;
    8789                 :      71022 :         offset -= offsetof(PyHeapTypeObject, as_mapping);
    8790                 :            :     }
    8791         [ +  + ]:    1404321 :     else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) {
    8792                 :     854313 :         ptr = (char *)type->tp_as_number;
    8793                 :     854313 :         offset -= offsetof(PyHeapTypeObject, as_number);
    8794                 :            :     }
    8795         [ +  + ]:     550008 :     else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_async)) {
    8796                 :      57505 :         ptr = (char *)type->tp_as_async;
    8797                 :      57505 :         offset -= offsetof(PyHeapTypeObject, as_async);
    8798                 :            :     }
    8799                 :            :     else {
    8800                 :     492503 :         ptr = (char *)type;
    8801                 :            :     }
    8802         [ +  + ]:    1656320 :     if (ptr != NULL)
    8803                 :    1326193 :         ptr += offset;
    8804                 :    1656320 :     return (void **)ptr;
    8805                 :            : }
    8806                 :            : 
    8807                 :            : /* Return a slot pointer for a given name, but ONLY if the attribute has
    8808                 :            :    exactly one slot function.  The name must be an interned string. */
    8809                 :            : static void **
    8810                 :     100029 : resolve_slotdups(PyTypeObject *type, PyObject *name)
    8811                 :            : {
    8812                 :            :     /* XXX Maybe this could be optimized more -- but is it worth it? */
    8813                 :            : 
    8814                 :            :     /* pname and ptrs act as a little cache */
    8815                 :     100029 :     PyInterpreterState *interp = _PyInterpreterState_Get();
    8816                 :            : #define pname _Py_INTERP_CACHED_OBJECT(interp, type_slots_pname)
    8817                 :            : #define ptrs _Py_INTERP_CACHED_OBJECT(interp, type_slots_ptrs)
    8818                 :            :     pytype_slotdef *p, **pp;
    8819                 :            :     void **res, **ptr;
    8820                 :            : 
    8821         [ +  + ]:     100029 :     if (pname != name) {
    8822                 :            :         /* Collect all slotdefs that match name into ptrs. */
    8823                 :     100023 :         pname = name;
    8824                 :     100023 :         pp = ptrs;
    8825         [ +  + ]:    9302139 :         for (p = slotdefs; p->name_strobj; p++) {
    8826         [ +  + ]:    9202116 :             if (p->name_strobj == name)
    8827                 :     139162 :                 *pp++ = p;
    8828                 :            :         }
    8829                 :     100023 :         *pp = NULL;
    8830                 :            :     }
    8831                 :            : 
    8832                 :            :     /* Look in all slots of the type matching the name. If exactly one of these
    8833                 :            :        has a filled-in slot, return a pointer to that slot.
    8834                 :            :        Otherwise, return NULL. */
    8835                 :     100029 :     res = NULL;
    8836         [ +  + ]:     238653 :     for (pp = ptrs; *pp; pp++) {
    8837                 :     139173 :         ptr = slotptr(type, (*pp)->offset);
    8838   [ +  -  +  + ]:     139173 :         if (ptr == NULL || *ptr == NULL)
    8839                 :      39870 :             continue;
    8840         [ +  + ]:      99303 :         if (res != NULL)
    8841                 :        549 :             return NULL;
    8842                 :      98754 :         res = ptr;
    8843                 :            :     }
    8844                 :      99480 :     return res;
    8845                 :            : #undef pname
    8846                 :            : #undef ptrs
    8847                 :            : }
    8848                 :            : 
    8849                 :            : 
    8850                 :            : /* Common code for update_slots_callback() and fixup_slot_dispatchers().
    8851                 :            :  *
    8852                 :            :  * This is meant to set a "slot" like type->tp_repr or
    8853                 :            :  * type->tp_as_sequence->sq_concat by looking up special methods like
    8854                 :            :  * __repr__ or __add__. The opposite (adding special methods from slots) is
    8855                 :            :  * done by add_operators(), called from PyType_Ready(). Since update_one_slot()
    8856                 :            :  * calls PyType_Ready() if needed, the special methods are already in place.
    8857                 :            :  *
    8858                 :            :  * The special methods corresponding to each slot are defined in the "slotdef"
    8859                 :            :  * array. Note that one slot may correspond to multiple special methods and vice
    8860                 :            :  * versa. For example, tp_richcompare uses 6 methods __lt__, ..., __ge__ and
    8861                 :            :  * tp_as_number->nb_add uses __add__ and __radd__. In the other direction,
    8862                 :            :  * __add__ is used by the number and sequence protocols and __getitem__ by the
    8863                 :            :  * sequence and mapping protocols. This causes a lot of complications.
    8864                 :            :  *
    8865                 :            :  * In detail, update_one_slot() does the following:
    8866                 :            :  *
    8867                 :            :  * First of all, if the slot in question does not exist, return immediately.
    8868                 :            :  * This can happen for example if it's tp_as_number->nb_add but tp_as_number
    8869                 :            :  * is NULL.
    8870                 :            :  *
    8871                 :            :  * For the given slot, we loop over all the special methods with a name
    8872                 :            :  * corresponding to that slot (for example, for tp_descr_set, this would be
    8873                 :            :  * __set__ and __delete__) and we look up these names in the MRO of the type.
    8874                 :            :  * If we don't find any special method, the slot is set to NULL (regardless of
    8875                 :            :  * what was in the slot before).
    8876                 :            :  *
    8877                 :            :  * Suppose that we find exactly one special method. If it's a wrapper_descriptor
    8878                 :            :  * (i.e. a special method calling a slot, for example str.__repr__ which calls
    8879                 :            :  * the tp_repr for the 'str' class) with the correct name ("__repr__" for
    8880                 :            :  * tp_repr), for the right class, calling the right wrapper C function (like
    8881                 :            :  * wrap_unaryfunc for tp_repr), then the slot is set to the slot that the
    8882                 :            :  * wrapper_descriptor originally wrapped. For example, a class inheriting
    8883                 :            :  * from 'str' and not redefining __repr__ will have tp_repr set to the tp_repr
    8884                 :            :  * of 'str'.
    8885                 :            :  * In all other cases where the special method exists, the slot is set to a
    8886                 :            :  * wrapper calling the special method. There is one exception: if the special
    8887                 :            :  * method is a wrapper_descriptor with the correct name but the type has
    8888                 :            :  * precisely one slot set for that name and that slot is not the one that we
    8889                 :            :  * are updating, then NULL is put in the slot (this exception is the only place
    8890                 :            :  * in update_one_slot() where the *existing* slots matter).
    8891                 :            :  *
    8892                 :            :  * When there are multiple special methods for the same slot, the above is
    8893                 :            :  * applied for each special method. As long as the results agree, the common
    8894                 :            :  * resulting slot is applied. If the results disagree, then a wrapper for
    8895                 :            :  * the special methods is installed. This is always safe, but less efficient
    8896                 :            :  * because it uses method lookup instead of direct C calls.
    8897                 :            :  *
    8898                 :            :  * There are some further special cases for specific slots, like supporting
    8899                 :            :  * __hash__ = None for tp_hash and special code for tp_new.
    8900                 :            :  *
    8901                 :            :  * When done, return a pointer to the next slotdef with a different offset,
    8902                 :            :  * because that's convenient for fixup_slot_dispatchers(). This function never
    8903                 :            :  * sets an exception: if an internal error happens (unlikely), it's ignored. */
    8904                 :            : static pytype_slotdef *
    8905                 :     407059 : update_one_slot(PyTypeObject *type, pytype_slotdef *p)
    8906                 :            : {
    8907                 :            :     PyObject *descr;
    8908                 :            :     PyWrapperDescrObject *d;
    8909                 :            : 
    8910                 :            :     // The correct specialized C function, like "tp_repr of str" in the
    8911                 :            :     // example above
    8912                 :     407059 :     void *specific = NULL;
    8913                 :            : 
    8914                 :            :     // A generic wrapper that uses method lookup (safe but slow)
    8915                 :     407059 :     void *generic = NULL;
    8916                 :            : 
    8917                 :            :     // Set to 1 if the generic wrapper is necessary
    8918                 :     407059 :     int use_generic = 0;
    8919                 :            : 
    8920                 :     407059 :     int offset = p->offset;
    8921                 :            :     int error;
    8922                 :     407059 :     void **ptr = slotptr(type, offset);
    8923                 :            : 
    8924         [ -  + ]:     407059 :     if (ptr == NULL) {
    8925                 :            :         do {
    8926                 :          0 :             ++p;
    8927         [ #  # ]:          0 :         } while (p->offset == offset);
    8928                 :          0 :         return p;
    8929                 :            :     }
    8930                 :            :     /* We may end up clearing live exceptions below, so make sure it's ours. */
    8931                 :            :     assert(!PyErr_Occurred());
    8932                 :            :     do {
    8933                 :            :         /* Use faster uncached lookup as we won't get any cache hits during type setup. */
    8934                 :     576133 :         descr = find_name_in_mro(type, p->name_strobj, &error);
    8935         [ +  + ]:     576133 :         if (descr == NULL) {
    8936         [ -  + ]:     459178 :             if (error == -1) {
    8937                 :            :                 /* It is unlikely but not impossible that there has been an exception
    8938                 :            :                    during lookup. Since this function originally expected no errors,
    8939                 :            :                    we ignore them here in order to keep up the interface. */
    8940                 :          0 :                 PyErr_Clear();
    8941                 :            :             }
    8942         [ +  + ]:     459178 :             if (ptr == (void**)&type->tp_iternext) {
    8943                 :       5968 :                 specific = (void *)_PyObject_NextNotImplemented;
    8944                 :            :             }
    8945                 :     459178 :             continue;
    8946                 :            :         }
    8947         [ +  + ]:     116955 :         if (Py_IS_TYPE(descr, &PyWrapperDescr_Type) &&
    8948         [ +  + ]:     200090 :             ((PyWrapperDescrObject *)descr)->d_base->name_strobj == p->name_strobj) {
    8949                 :     100029 :             void **tptr = resolve_slotdups(type, p->name_strobj);
    8950   [ +  +  +  + ]:     100029 :             if (tptr == NULL || tptr == ptr)
    8951                 :      80728 :                 generic = p->function;
    8952                 :     100029 :             d = (PyWrapperDescrObject *)descr;
    8953   [ +  +  +  + ]:     100029 :             if ((specific == NULL || specific == d->d_wrapped) &&
    8954   [ +  +  +  - ]:     180626 :                 d->d_base->wrapper == p->wrapper &&
    8955                 :      80602 :                 PyType_IsSubtype(type, PyDescr_TYPE(d)))
    8956                 :            :             {
    8957                 :      80602 :                 specific = d->d_wrapped;
    8958                 :            :             }
    8959                 :            :             else {
    8960                 :            :                 /* We cannot use the specific slot function because either
    8961                 :            :                    - it is not unique: there are multiple methods for this
    8962                 :            :                      slot and they conflict
    8963                 :            :                    - the signature is wrong (as checked by the ->wrapper
    8964                 :            :                      comparison above)
    8965                 :            :                    - it's wrapping the wrong class
    8966                 :            :                  */
    8967                 :      19427 :                 use_generic = 1;
    8968                 :            :             }
    8969                 :            :         }
    8970   [ +  +  +  + ]:      22896 :         else if (Py_IS_TYPE(descr, &PyCFunction_Type) &&
    8971                 :       5970 :                  PyCFunction_GET_FUNCTION(descr) ==
    8972                 :       5968 :                  _PyCFunction_CAST(tp_new_wrapper) &&
    8973         [ +  - ]:       5968 :                  ptr == (void**)&type->tp_new)
    8974                 :            :         {
    8975                 :            :             /* The __new__ wrapper is not a wrapper descriptor,
    8976                 :            :                so must be special-cased differently.
    8977                 :            :                If we don't do this, creating an instance will
    8978                 :            :                always use slot_tp_new which will look up
    8979                 :            :                __new__ in the MRO which will call tp_new_wrapper
    8980                 :            :                which will look through the base classes looking
    8981                 :            :                for a static base and call its tp_new (usually
    8982                 :            :                PyType_GenericNew), after performing various
    8983                 :            :                sanity checks and constructing a new argument
    8984                 :            :                list.  Cut all that nonsense short -- this speeds
    8985                 :            :                up instance creation tremendously. */
    8986                 :       5968 :             specific = (void *)type->tp_new;
    8987                 :            :             /* XXX I'm not 100% sure that there isn't a hole
    8988                 :            :                in this reasoning that requires additional
    8989                 :            :                sanity checks.  I'll buy the first person to
    8990                 :            :                point out a bug in this reasoning a beer. */
    8991                 :            :         }
    8992         [ +  + ]:      10958 :         else if (descr == Py_None &&
    8993         [ +  + ]:        317 :                  ptr == (void**)&type->tp_hash) {
    8994                 :            :             /* We specifically allow __hash__ to be set to None
    8995                 :            :                to prevent inheritance of the default
    8996                 :            :                implementation from object.__hash__ */
    8997                 :        290 :             specific = (void *)PyObject_HashNotImplemented;
    8998                 :            :         }
    8999                 :            :         else {
    9000                 :      10668 :             use_generic = 1;
    9001                 :      10668 :             generic = p->function;
    9002         [ +  + ]:      10668 :             if (p->function == slot_tp_call) {
    9003                 :            :                 /* A generic __call__ is incompatible with vectorcall */
    9004                 :        253 :                 type->tp_flags &= ~Py_TPFLAGS_HAVE_VECTORCALL;
    9005                 :            :             }
    9006                 :            :         }
    9007         [ +  + ]:     576133 :     } while ((++p)->offset == offset);
    9008   [ +  +  +  + ]:     407059 :     if (specific && !use_generic)
    9009                 :      54750 :         *ptr = specific;
    9010                 :            :     else
    9011                 :     352309 :         *ptr = generic;
    9012                 :     407059 :     return p;
    9013                 :            : }
    9014                 :            : 
    9015                 :            : /* In the type, update the slots whose slotdefs are gathered in the pp array.
    9016                 :            :    This is a callback for update_subclasses(). */
    9017                 :            : static int
    9018                 :        159 : update_slots_callback(PyTypeObject *type, void *data)
    9019                 :            : {
    9020                 :        159 :     pytype_slotdef **pp = (pytype_slotdef **)data;
    9021         [ +  + ]:        318 :     for (; *pp; pp++) {
    9022                 :        159 :         update_one_slot(type, *pp);
    9023                 :            :     }
    9024                 :        159 :     return 0;
    9025                 :            : }
    9026                 :            : 
    9027                 :            : /* Update the slots after assignment to a class (type) attribute. */
    9028                 :            : static int
    9029                 :       1424 : update_slot(PyTypeObject *type, PyObject *name)
    9030                 :            : {
    9031                 :            :     pytype_slotdef *ptrs[MAX_EQUIV];
    9032                 :            :     pytype_slotdef *p;
    9033                 :            :     pytype_slotdef **pp;
    9034                 :            :     int offset;
    9035                 :            : 
    9036                 :            :     assert(PyUnicode_CheckExact(name));
    9037                 :            :     assert(PyUnicode_CHECK_INTERNED(name));
    9038                 :            : 
    9039                 :       1424 :     pp = ptrs;
    9040         [ +  + ]:     132432 :     for (p = slotdefs; p->name; p++) {
    9041                 :            :         assert(PyUnicode_CheckExact(p->name_strobj));
    9042                 :            :         assert(PyUnicode_CHECK_INTERNED(p->name_strobj));
    9043                 :            :         assert(PyUnicode_CheckExact(name));
    9044                 :            :         /* bpo-40521: Using interned strings. */
    9045         [ +  + ]:     131008 :         if (p->name_strobj == name) {
    9046                 :        159 :             *pp++ = p;
    9047                 :            :         }
    9048                 :            :     }
    9049                 :       1424 :     *pp = NULL;
    9050         [ +  + ]:       1583 :     for (pp = ptrs; *pp; pp++) {
    9051                 :        159 :         p = *pp;
    9052                 :        159 :         offset = p->offset;
    9053   [ +  -  +  + ]:        187 :         while (p > slotdefs && (p-1)->offset == offset)
    9054                 :         28 :             --p;
    9055                 :        159 :         *pp = p;
    9056                 :            :     }
    9057         [ +  + ]:       1424 :     if (ptrs[0] == NULL)
    9058                 :       1265 :         return 0; /* Not an attribute that affects any slots */
    9059                 :        159 :     return update_subclasses(type, name,
    9060                 :            :                              update_slots_callback, (void *)ptrs);
    9061                 :            : }
    9062                 :            : 
    9063                 :            : /* Store the proper functions in the slot dispatches at class (type)
    9064                 :            :    definition time, based upon which operations the class overrides in its
    9065                 :            :    dict. */
    9066                 :            : static void
    9067                 :       6260 : fixup_slot_dispatchers(PyTypeObject *type)
    9068                 :            : {
    9069                 :            :     assert(!PyErr_Occurred());
    9070         [ +  + ]:     413160 :     for (pytype_slotdef *p = slotdefs; p->name; ) {
    9071                 :     406900 :         p = update_one_slot(type, p);
    9072                 :            :     }
    9073                 :       6260 : }
    9074                 :            : 
    9075                 :            : static void
    9076                 :          0 : update_all_slots(PyTypeObject* type)
    9077                 :            : {
    9078                 :            :     pytype_slotdef *p;
    9079                 :            : 
    9080                 :            :     /* Clear the VALID_VERSION flag of 'type' and all its subclasses. */
    9081                 :          0 :     PyType_Modified(type);
    9082                 :            : 
    9083         [ #  # ]:          0 :     for (p = slotdefs; p->name; p++) {
    9084                 :            :         /* update_slot returns int but can't actually fail */
    9085                 :          0 :         update_slot(type, p->name_strobj);
    9086                 :            :     }
    9087                 :          0 : }
    9088                 :            : 
    9089                 :            : 
    9090                 :            : /* Call __set_name__ on all attributes (including descriptors)
    9091                 :            :   in a newly generated type */
    9092                 :            : static int
    9093                 :       6260 : type_new_set_names(PyTypeObject *type)
    9094                 :            : {
    9095                 :       6260 :     PyObject *names_to_set = PyDict_Copy(type->tp_dict);
    9096         [ -  + ]:       6260 :     if (names_to_set == NULL) {
    9097                 :          0 :         return -1;
    9098                 :            :     }
    9099                 :            : 
    9100                 :       6260 :     Py_ssize_t i = 0;
    9101                 :            :     PyObject *key, *value;
    9102         [ +  + ]:      47994 :     while (PyDict_Next(names_to_set, &i, &key, &value)) {
    9103                 :      41734 :         PyObject *set_name = _PyObject_LookupSpecial(value,
    9104                 :            :                                                      &_Py_ID(__set_name__));
    9105         [ +  + ]:      41734 :         if (set_name == NULL) {
    9106         [ -  + ]:      41293 :             if (PyErr_Occurred()) {
    9107                 :          0 :                 goto error;
    9108                 :            :             }
    9109                 :      41293 :             continue;
    9110                 :            :         }
    9111                 :            : 
    9112                 :        441 :         PyObject *res = PyObject_CallFunctionObjArgs(set_name, type, key, NULL);
    9113                 :        441 :         Py_DECREF(set_name);
    9114                 :            : 
    9115         [ -  + ]:        441 :         if (res == NULL) {
    9116                 :          0 :             _PyErr_FormatFromCause(PyExc_RuntimeError,
    9117                 :            :                 "Error calling __set_name__ on '%.100s' instance %R "
    9118                 :            :                 "in '%.100s'",
    9119                 :          0 :                 Py_TYPE(value)->tp_name, key, type->tp_name);
    9120                 :          0 :             goto error;
    9121                 :            :         }
    9122                 :        441 :         Py_DECREF(res);
    9123                 :            :     }
    9124                 :            : 
    9125                 :       6260 :     Py_DECREF(names_to_set);
    9126                 :       6260 :     return 0;
    9127                 :            : 
    9128                 :          0 : error:
    9129                 :          0 :     Py_DECREF(names_to_set);
    9130                 :          0 :     return -1;
    9131                 :            : }
    9132                 :            : 
    9133                 :            : 
    9134                 :            : /* Call __init_subclass__ on the parent of a newly generated type */
    9135                 :            : static int
    9136                 :       6260 : type_new_init_subclass(PyTypeObject *type, PyObject *kwds)
    9137                 :            : {
    9138                 :       6260 :     PyObject *args[2] = {(PyObject *)type, (PyObject *)type};
    9139                 :       6260 :     PyObject *super = _PyObject_FastCall((PyObject *)&PySuper_Type, args, 2);
    9140         [ -  + ]:       6260 :     if (super == NULL) {
    9141                 :          0 :         return -1;
    9142                 :            :     }
    9143                 :            : 
    9144                 :       6260 :     PyObject *func = PyObject_GetAttr(super, &_Py_ID(__init_subclass__));
    9145                 :       6260 :     Py_DECREF(super);
    9146         [ -  + ]:       6260 :     if (func == NULL) {
    9147                 :          0 :         return -1;
    9148                 :            :     }
    9149                 :            : 
    9150                 :       6260 :     PyObject *result = PyObject_VectorcallDict(func, NULL, 0, kwds);
    9151                 :       6260 :     Py_DECREF(func);
    9152         [ -  + ]:       6260 :     if (result == NULL) {
    9153                 :          0 :         return -1;
    9154                 :            :     }
    9155                 :            : 
    9156                 :       6260 :     Py_DECREF(result);
    9157                 :       6260 :     return 0;
    9158                 :            : }
    9159                 :            : 
    9160                 :            : 
    9161                 :            : /* recurse_down_subclasses() and update_subclasses() are mutually
    9162                 :            :    recursive functions to call a callback for all subclasses,
    9163                 :            :    but refraining from recursing into subclasses that define 'attr_name'. */
    9164                 :            : 
    9165                 :            : static int
    9166                 :        159 : update_subclasses(PyTypeObject *type, PyObject *attr_name,
    9167                 :            :                   update_callback callback, void *data)
    9168                 :            : {
    9169         [ -  + ]:        159 :     if (callback(type, data) < 0) {
    9170                 :          0 :         return -1;
    9171                 :            :     }
    9172                 :        159 :     return recurse_down_subclasses(type, attr_name, callback, data);
    9173                 :            : }
    9174                 :            : 
    9175                 :            : static int
    9176                 :        159 : recurse_down_subclasses(PyTypeObject *type, PyObject *attr_name,
    9177                 :            :                         update_callback callback, void *data)
    9178                 :            : {
    9179                 :            :     // It is safe to use a borrowed reference because update_subclasses() is
    9180                 :            :     // only used with update_slots_callback() which doesn't modify
    9181                 :            :     // tp_subclasses.
    9182                 :        159 :     PyObject *subclasses = lookup_subclasses(type);  // borrowed ref
    9183         [ +  - ]:        159 :     if (subclasses == NULL) {
    9184                 :        159 :         return 0;
    9185                 :            :     }
    9186                 :            :     assert(PyDict_CheckExact(subclasses));
    9187                 :            : 
    9188                 :          0 :     Py_ssize_t i = 0;
    9189                 :            :     PyObject *ref;
    9190         [ #  # ]:          0 :     while (PyDict_Next(subclasses, &i, NULL, &ref)) {
    9191                 :          0 :         PyTypeObject *subclass = subclass_from_ref(ref);  // borrowed
    9192         [ #  # ]:          0 :         if (subclass == NULL) {
    9193                 :          0 :             continue;
    9194                 :            :         }
    9195                 :            : 
    9196                 :            :         /* Avoid recursing down into unaffected classes */
    9197                 :          0 :         PyObject *dict = subclass->tp_dict;
    9198   [ #  #  #  # ]:          0 :         if (dict != NULL && PyDict_Check(dict)) {
    9199                 :          0 :             int r = PyDict_Contains(dict, attr_name);
    9200         [ #  # ]:          0 :             if (r < 0) {
    9201                 :          0 :                 return -1;
    9202                 :            :             }
    9203         [ #  # ]:          0 :             if (r > 0) {
    9204                 :          0 :                 continue;
    9205                 :            :             }
    9206                 :            :         }
    9207                 :            : 
    9208         [ #  # ]:          0 :         if (update_subclasses(subclass, attr_name, callback, data) < 0) {
    9209                 :          0 :             return -1;
    9210                 :            :         }
    9211                 :            :     }
    9212                 :          0 :     return 0;
    9213                 :            : }
    9214                 :            : 
    9215                 :            : /* This function is called by PyType_Ready() to populate the type's
    9216                 :            :    dictionary with method descriptors for function slots.  For each
    9217                 :            :    function slot (like tp_repr) that's defined in the type, one or more
    9218                 :            :    corresponding descriptors are added in the type's tp_dict dictionary
    9219                 :            :    under the appropriate name (like __repr__).  Some function slots
    9220                 :            :    cause more than one descriptor to be added (for example, the nb_add
    9221                 :            :    slot adds both __add__ and __radd__ descriptors) and some function
    9222                 :            :    slots compete for the same descriptor (for example both sq_item and
    9223                 :            :    mp_subscript generate a __getitem__ descriptor).
    9224                 :            : 
    9225                 :            :    In the latter case, the first slotdef entry encountered wins.  Since
    9226                 :            :    slotdef entries are sorted by the offset of the slot in the
    9227                 :            :    PyHeapTypeObject, this gives us some control over disambiguating
    9228                 :            :    between competing slots: the members of PyHeapTypeObject are listed
    9229                 :            :    from most general to least general, so the most general slot is
    9230                 :            :    preferred.  In particular, because as_mapping comes before as_sequence,
    9231                 :            :    for a type that defines both mp_subscript and sq_item, mp_subscript
    9232                 :            :    wins.
    9233                 :            : 
    9234                 :            :    This only adds new descriptors and doesn't overwrite entries in
    9235                 :            :    tp_dict that were previously defined.  The descriptors contain a
    9236                 :            :    reference to the C function they must call, so that it's safe if they
    9237                 :            :    are copied into a subtype's __dict__ and the subtype has a different
    9238                 :            :    C function in its slot -- calling the method defined by the
    9239                 :            :    descriptor will call the C function that was used to create it,
    9240                 :            :    rather than the C function present in the slot when it is called.
    9241                 :            :    (This is important because a subtype may have a C function in the
    9242                 :            :    slot that calls the method from the dictionary, and we want to avoid
    9243                 :            :    infinite recursion here.) */
    9244                 :            : 
    9245                 :            : static int
    9246                 :      12908 : add_operators(PyTypeObject *type)
    9247                 :            : {
    9248                 :      12908 :     PyObject *dict = type->tp_dict;
    9249                 :            :     pytype_slotdef *p;
    9250                 :            :     PyObject *descr;
    9251                 :            :     void **ptr;
    9252                 :            : 
    9253         [ +  + ]:    1200444 :     for (p = slotdefs; p->name; p++) {
    9254         [ +  + ]:    1187536 :         if (p->wrapper == NULL)
    9255                 :      77448 :             continue;
    9256                 :    1110088 :         ptr = slotptr(type, p->offset);
    9257   [ +  +  +  + ]:    1110088 :         if (!ptr || !*ptr)
    9258                 :    1078821 :             continue;
    9259                 :      31267 :         int r = PyDict_Contains(dict, p->name_strobj);
    9260         [ +  + ]:      31267 :         if (r > 0)
    9261                 :        608 :             continue;
    9262         [ -  + ]:      30659 :         if (r < 0) {
    9263                 :          0 :             return -1;
    9264                 :            :         }
    9265         [ +  + ]:      30659 :         if (*ptr == (void *)PyObject_HashNotImplemented) {
    9266                 :            :             /* Classes may prevent the inheritance of the tp_hash
    9267                 :            :                slot by storing PyObject_HashNotImplemented in it. Make it
    9268                 :            :                visible as a None value for the __hash__ attribute. */
    9269         [ -  + ]:        270 :             if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0)
    9270                 :          0 :                 return -1;
    9271                 :            :         }
    9272                 :            :         else {
    9273                 :      30389 :             descr = PyDescr_NewWrapper(type, p, *ptr);
    9274         [ -  + ]:      30389 :             if (descr == NULL)
    9275                 :          0 :                 return -1;
    9276         [ -  + ]:      30389 :             if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) {
    9277                 :          0 :                 Py_DECREF(descr);
    9278                 :          0 :                 return -1;
    9279                 :            :             }
    9280                 :      30389 :             Py_DECREF(descr);
    9281                 :            :         }
    9282                 :            :     }
    9283                 :      12908 :     return 0;
    9284                 :            : }
    9285                 :            : 
    9286                 :            : 
    9287                 :            : /* Cooperative 'super' */
    9288                 :            : 
    9289                 :            : typedef struct {
    9290                 :            :     PyObject_HEAD
    9291                 :            :     PyTypeObject *type;
    9292                 :            :     PyObject *obj;
    9293                 :            :     PyTypeObject *obj_type;
    9294                 :            : } superobject;
    9295                 :            : 
    9296                 :            : static PyMemberDef super_members[] = {
    9297                 :            :     {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY,
    9298                 :            :      "the class invoking super()"},
    9299                 :            :     {"__self__",  T_OBJECT, offsetof(superobject, obj), READONLY,
    9300                 :            :      "the instance invoking super(); may be None"},
    9301                 :            :     {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY,
    9302                 :            :      "the type of the instance invoking super(); may be None"},
    9303                 :            :     {0}
    9304                 :            : };
    9305                 :            : 
    9306                 :            : static void
    9307                 :     169996 : super_dealloc(PyObject *self)
    9308                 :            : {
    9309                 :     169996 :     superobject *su = (superobject *)self;
    9310                 :            : 
    9311                 :     169996 :     _PyObject_GC_UNTRACK(self);
    9312                 :     169996 :     Py_XDECREF(su->obj);
    9313                 :     169996 :     Py_XDECREF(su->type);
    9314                 :     169996 :     Py_XDECREF(su->obj_type);
    9315                 :     169996 :     Py_TYPE(self)->tp_free(self);
    9316                 :     169996 : }
    9317                 :            : 
    9318                 :            : static PyObject *
    9319                 :          0 : super_repr(PyObject *self)
    9320                 :            : {
    9321                 :          0 :     superobject *su = (superobject *)self;
    9322                 :            : 
    9323         [ #  # ]:          0 :     if (su->obj_type)
    9324                 :          0 :         return PyUnicode_FromFormat(
    9325                 :            :             "<super: <class '%s'>, <%s object>>",
    9326                 :          0 :             su->type ? su->type->tp_name : "NULL",
    9327         [ #  # ]:          0 :             su->obj_type->tp_name);
    9328                 :            :     else
    9329                 :          0 :         return PyUnicode_FromFormat(
    9330                 :            :             "<super: <class '%s'>, NULL>",
    9331         [ #  # ]:          0 :             su->type ? su->type->tp_name : "NULL");
    9332                 :            : }
    9333                 :            : 
    9334                 :            : static PyObject *
    9335                 :     169996 : super_getattro(PyObject *self, PyObject *name)
    9336                 :            : {
    9337                 :     169996 :     superobject *su = (superobject *)self;
    9338                 :            :     PyTypeObject *starttype;
    9339                 :            :     PyObject *mro;
    9340                 :            :     Py_ssize_t i, n;
    9341                 :            : 
    9342                 :     169996 :     starttype = su->obj_type;
    9343         [ -  + ]:     169996 :     if (starttype == NULL)
    9344                 :          0 :         goto skip;
    9345                 :            : 
    9346                 :            :     /* We want __class__ to return the class of the super object
    9347                 :            :        (i.e. super, or a subclass), not the class of su->obj. */
    9348   [ +  -  +  + ]:     339992 :     if (PyUnicode_Check(name) &&
    9349         [ -  + ]:     170152 :         PyUnicode_GET_LENGTH(name) == 9 &&
    9350                 :        156 :         _PyUnicode_Equal(name, &_Py_ID(__class__)))
    9351                 :          0 :         goto skip;
    9352                 :            : 
    9353                 :     169996 :     mro = starttype->tp_mro;
    9354         [ -  + ]:     169996 :     if (mro == NULL)
    9355                 :          0 :         goto skip;
    9356                 :            : 
    9357                 :            :     assert(PyTuple_Check(mro));
    9358                 :     169996 :     n = PyTuple_GET_SIZE(mro);
    9359                 :            : 
    9360                 :            :     /* No need to check the last one: it's gonna be skipped anyway.  */
    9361         [ +  - ]:     186899 :     for (i = 0; i+1 < n; i++) {
    9362         [ +  + ]:     186899 :         if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i))
    9363                 :     169996 :             break;
    9364                 :            :     }
    9365                 :     169996 :     i++;  /* skip su->type (if any)  */
    9366         [ -  + ]:     169996 :     if (i >= n)
    9367                 :          0 :         goto skip;
    9368                 :            : 
    9369                 :            :     /* keep a strong reference to mro because starttype->tp_mro can be
    9370                 :            :        replaced during PyDict_GetItemWithError(dict, name)  */
    9371                 :     169996 :     Py_INCREF(mro);
    9372                 :            :     do {
    9373                 :     704403 :         PyObject *obj = PyTuple_GET_ITEM(mro, i);
    9374                 :     704403 :         PyObject *dict = _PyType_CAST(obj)->tp_dict;
    9375                 :            :         assert(dict != NULL && PyDict_Check(dict));
    9376                 :            : 
    9377                 :     704403 :         PyObject *res = PyDict_GetItemWithError(dict, name);
    9378         [ +  + ]:     704403 :         if (res != NULL) {
    9379                 :     169996 :             Py_INCREF(res);
    9380                 :            : 
    9381                 :     169996 :             descrgetfunc f = Py_TYPE(res)->tp_descr_get;
    9382         [ +  + ]:     169996 :             if (f != NULL) {
    9383                 :            :                 PyObject *res2;
    9384                 :      38043 :                 res2 = f(res,
    9385                 :            :                     /* Only pass 'obj' param if this is instance-mode super
    9386                 :            :                        (See SF ID #743627)  */
    9387         [ +  + ]:      38043 :                     (su->obj == (PyObject *)starttype) ? NULL : su->obj,
    9388                 :            :                     (PyObject *)starttype);
    9389                 :      38043 :                 Py_SETREF(res, res2);
    9390                 :            :             }
    9391                 :            : 
    9392                 :     169996 :             Py_DECREF(mro);
    9393                 :     169996 :             return res;
    9394                 :            :         }
    9395         [ -  + ]:     534407 :         else if (PyErr_Occurred()) {
    9396                 :          0 :             Py_DECREF(mro);
    9397                 :          0 :             return NULL;
    9398                 :            :         }
    9399                 :            : 
    9400                 :     534407 :         i++;
    9401         [ +  - ]:     534407 :     } while (i < n);
    9402                 :          0 :     Py_DECREF(mro);
    9403                 :            : 
    9404                 :          0 :   skip:
    9405                 :          0 :     return PyObject_GenericGetAttr(self, name);
    9406                 :            : }
    9407                 :            : 
    9408                 :            : static PyTypeObject *
    9409                 :     169996 : supercheck(PyTypeObject *type, PyObject *obj)
    9410                 :            : {
    9411                 :            :     /* Check that a super() call makes sense.  Return a type object.
    9412                 :            : 
    9413                 :            :        obj can be a class, or an instance of one:
    9414                 :            : 
    9415                 :            :        - If it is a class, it must be a subclass of 'type'.      This case is
    9416                 :            :          used for class methods; the return value is obj.
    9417                 :            : 
    9418                 :            :        - If it is an instance, it must be an instance of 'type'.  This is
    9419                 :            :          the normal case; the return value is obj.__class__.
    9420                 :            : 
    9421                 :            :        But... when obj is an instance, we want to allow for the case where
    9422                 :            :        Py_TYPE(obj) is not a subclass of type, but obj.__class__ is!
    9423                 :            :        This will allow using super() with a proxy for obj.
    9424                 :            :     */
    9425                 :            : 
    9426                 :            :     /* Check for first bullet above (special case) */
    9427   [ +  +  +  + ]:     169996 :     if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) {
    9428                 :     138281 :         return (PyTypeObject *)Py_NewRef(obj);
    9429                 :            :     }
    9430                 :            : 
    9431                 :            :     /* Normal case */
    9432         [ +  - ]:      31715 :     if (PyType_IsSubtype(Py_TYPE(obj), type)) {
    9433                 :      31715 :         return (PyTypeObject*)Py_NewRef(Py_TYPE(obj));
    9434                 :            :     }
    9435                 :            :     else {
    9436                 :            :         /* Try the slow way */
    9437                 :            :         PyObject *class_attr;
    9438                 :            : 
    9439         [ #  # ]:          0 :         if (_PyObject_LookupAttr(obj, &_Py_ID(__class__), &class_attr) < 0) {
    9440                 :          0 :             return NULL;
    9441                 :            :         }
    9442   [ #  #  #  # ]:          0 :         if (class_attr != NULL &&
    9443                 :          0 :             PyType_Check(class_attr) &&
    9444         [ #  # ]:          0 :             (PyTypeObject *)class_attr != Py_TYPE(obj))
    9445                 :            :         {
    9446                 :          0 :             int ok = PyType_IsSubtype(
    9447                 :            :                 (PyTypeObject *)class_attr, type);
    9448         [ #  # ]:          0 :             if (ok) {
    9449                 :          0 :                 return (PyTypeObject *)class_attr;
    9450                 :            :             }
    9451                 :            :         }
    9452                 :          0 :         Py_XDECREF(class_attr);
    9453                 :            :     }
    9454                 :            : 
    9455                 :          0 :     PyErr_SetString(PyExc_TypeError,
    9456                 :            :                     "super(type, obj): "
    9457                 :            :                     "obj must be an instance or subtype of type");
    9458                 :          0 :     return NULL;
    9459                 :            : }
    9460                 :            : 
    9461                 :            : static PyObject *
    9462                 :          0 : super_descr_get(PyObject *self, PyObject *obj, PyObject *type)
    9463                 :            : {
    9464                 :          0 :     superobject *su = (superobject *)self;
    9465                 :            :     superobject *newobj;
    9466                 :            : 
    9467   [ #  #  #  #  :          0 :     if (obj == NULL || obj == Py_None || su->obj != NULL) {
                   #  # ]
    9468                 :            :         /* Not binding to an object, or already bound */
    9469                 :          0 :         return Py_NewRef(self);
    9470                 :            :     }
    9471         [ #  # ]:          0 :     if (!Py_IS_TYPE(su, &PySuper_Type))
    9472                 :            :         /* If su is an instance of a (strict) subclass of super,
    9473                 :            :            call its type */
    9474                 :          0 :         return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su),
    9475                 :            :                                             su->type, obj, NULL);
    9476                 :            :     else {
    9477                 :            :         /* Inline the common case */
    9478                 :          0 :         PyTypeObject *obj_type = supercheck(su->type, obj);
    9479         [ #  # ]:          0 :         if (obj_type == NULL)
    9480                 :          0 :             return NULL;
    9481                 :          0 :         newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type,
    9482                 :            :                                                  NULL, NULL);
    9483         [ #  # ]:          0 :         if (newobj == NULL)
    9484                 :          0 :             return NULL;
    9485                 :          0 :         newobj->type = (PyTypeObject*)Py_NewRef(su->type);
    9486                 :          0 :         newobj->obj = Py_NewRef(obj);
    9487                 :          0 :         newobj->obj_type = obj_type;
    9488                 :          0 :         return (PyObject *)newobj;
    9489                 :            :     }
    9490                 :            : }
    9491                 :            : 
    9492                 :            : static int
    9493                 :      18197 : super_init_without_args(_PyInterpreterFrame *cframe, PyCodeObject *co,
    9494                 :            :                         PyTypeObject **type_p, PyObject **obj_p)
    9495                 :            : {
    9496         [ -  + ]:      18197 :     if (co->co_argcount == 0) {
    9497                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    9498                 :            :                         "super(): no arguments");
    9499                 :          0 :         return -1;
    9500                 :            :     }
    9501                 :            : 
    9502                 :            :     assert(cframe->f_code->co_nlocalsplus > 0);
    9503                 :      18197 :     PyObject *firstarg = _PyFrame_GetLocalsArray(cframe)[0];
    9504                 :            :     // The first argument might be a cell.
    9505   [ +  -  +  + ]:      18197 :     if (firstarg != NULL && (_PyLocals_GetKind(co->co_localspluskinds, 0) & CO_FAST_CELL)) {
    9506                 :            :         // "firstarg" is a cell here unless (very unlikely) super()
    9507                 :            :         // was called from the C-API before the first MAKE_CELL op.
    9508         [ +  - ]:         21 :         if (_PyInterpreterFrame_LASTI(cframe) >= 0) {
    9509                 :            :             // MAKE_CELL and COPY_FREE_VARS have no quickened forms, so no need
    9510                 :            :             // to use _PyOpcode_Deopt here:
    9511                 :            :             assert(_PyCode_CODE(co)[0].op.code == MAKE_CELL ||
    9512                 :            :                    _PyCode_CODE(co)[0].op.code == COPY_FREE_VARS);
    9513                 :            :             assert(PyCell_Check(firstarg));
    9514                 :         21 :             firstarg = PyCell_GET(firstarg);
    9515                 :            :         }
    9516                 :            :     }
    9517         [ -  + ]:      18197 :     if (firstarg == NULL) {
    9518                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    9519                 :            :                         "super(): arg[0] deleted");
    9520                 :          0 :         return -1;
    9521                 :            :     }
    9522                 :            : 
    9523                 :            :     // Look for __class__ in the free vars.
    9524                 :      18197 :     PyTypeObject *type = NULL;
    9525                 :      18197 :     int i = PyCode_GetFirstFree(co);
    9526         [ +  - ]:      18197 :     for (; i < co->co_nlocalsplus; i++) {
    9527                 :            :         assert((_PyLocals_GetKind(co->co_localspluskinds, i) & CO_FAST_FREE) != 0);
    9528                 :      18197 :         PyObject *name = PyTuple_GET_ITEM(co->co_localsplusnames, i);
    9529                 :            :         assert(PyUnicode_Check(name));
    9530         [ +  - ]:      18197 :         if (_PyUnicode_Equal(name, &_Py_ID(__class__))) {
    9531                 :      18197 :             PyObject *cell = _PyFrame_GetLocalsArray(cframe)[i];
    9532   [ +  -  -  + ]:      18197 :             if (cell == NULL || !PyCell_Check(cell)) {
    9533                 :          0 :                 PyErr_SetString(PyExc_RuntimeError,
    9534                 :            :                   "super(): bad __class__ cell");
    9535                 :          0 :                 return -1;
    9536                 :            :             }
    9537                 :      18197 :             type = (PyTypeObject *) PyCell_GET(cell);
    9538         [ -  + ]:      18197 :             if (type == NULL) {
    9539                 :          0 :                 PyErr_SetString(PyExc_RuntimeError,
    9540                 :            :                   "super(): empty __class__ cell");
    9541                 :          0 :                 return -1;
    9542                 :            :             }
    9543         [ -  + ]:      18197 :             if (!PyType_Check(type)) {
    9544                 :          0 :                 PyErr_Format(PyExc_RuntimeError,
    9545                 :            :                   "super(): __class__ is not a type (%s)",
    9546                 :          0 :                   Py_TYPE(type)->tp_name);
    9547                 :          0 :                 return -1;
    9548                 :            :             }
    9549                 :      18197 :             break;
    9550                 :            :         }
    9551                 :            :     }
    9552         [ -  + ]:      18197 :     if (type == NULL) {
    9553                 :          0 :         PyErr_SetString(PyExc_RuntimeError,
    9554                 :            :                         "super(): __class__ cell not found");
    9555                 :          0 :         return -1;
    9556                 :            :     }
    9557                 :            : 
    9558                 :      18197 :     *type_p = type;
    9559                 :      18197 :     *obj_p = firstarg;
    9560                 :      18197 :     return 0;
    9561                 :            : }
    9562                 :            : 
    9563                 :            : static int super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj);
    9564                 :            : 
    9565                 :            : static int
    9566                 :          0 : super_init(PyObject *self, PyObject *args, PyObject *kwds)
    9567                 :            : {
    9568                 :          0 :     PyTypeObject *type = NULL;
    9569                 :          0 :     PyObject *obj = NULL;
    9570                 :            : 
    9571   [ #  #  #  # ]:          0 :     if (!_PyArg_NoKeywords("super", kwds))
    9572                 :          0 :         return -1;
    9573         [ #  # ]:          0 :     if (!PyArg_ParseTuple(args, "|O!O:super", &PyType_Type, &type, &obj))
    9574                 :          0 :         return -1;
    9575         [ #  # ]:          0 :     if (super_init_impl(self, type, obj) < 0) {
    9576                 :          0 :         return -1;
    9577                 :            :     }
    9578                 :          0 :     return 0;
    9579                 :            : }
    9580                 :            : 
    9581                 :            : static inline int
    9582                 :     169996 : super_init_impl(PyObject *self, PyTypeObject *type, PyObject *obj) {
    9583                 :     169996 :     superobject *su = (superobject *)self;
    9584                 :     169996 :     PyTypeObject *obj_type = NULL;
    9585         [ +  + ]:     169996 :     if (type == NULL) {
    9586                 :            :         /* Call super(), without args -- fill in from __class__
    9587                 :            :            and first local variable on the stack. */
    9588                 :      18197 :         PyThreadState *tstate = _PyThreadState_GET();
    9589                 :      18197 :         _PyInterpreterFrame *frame = _PyThreadState_GetFrame(tstate);
    9590         [ -  + ]:      18197 :         if (frame == NULL) {
    9591                 :          0 :             PyErr_SetString(PyExc_RuntimeError,
    9592                 :            :                             "super(): no current frame");
    9593                 :          0 :             return -1;
    9594                 :            :         }
    9595                 :      18197 :         int res = super_init_without_args(frame, frame->f_code, &type, &obj);
    9596                 :            : 
    9597         [ -  + ]:      18197 :         if (res < 0) {
    9598                 :          0 :             return -1;
    9599                 :            :         }
    9600                 :            :     }
    9601                 :            : 
    9602         [ -  + ]:     169996 :     if (obj == Py_None)
    9603                 :          0 :         obj = NULL;
    9604         [ +  - ]:     169996 :     if (obj != NULL) {
    9605                 :     169996 :         obj_type = supercheck(type, obj);
    9606         [ -  + ]:     169996 :         if (obj_type == NULL)
    9607                 :          0 :             return -1;
    9608                 :     169996 :         Py_INCREF(obj);
    9609                 :            :     }
    9610                 :     169996 :     Py_XSETREF(su->type, (PyTypeObject*)Py_NewRef(type));
    9611                 :     169996 :     Py_XSETREF(su->obj, obj);
    9612                 :     169996 :     Py_XSETREF(su->obj_type, obj_type);
    9613                 :     169996 :     return 0;
    9614                 :            : }
    9615                 :            : 
    9616                 :            : PyDoc_STRVAR(super_doc,
    9617                 :            : "super() -> same as super(__class__, <first argument>)\n"
    9618                 :            : "super(type) -> unbound super object\n"
    9619                 :            : "super(type, obj) -> bound super object; requires isinstance(obj, type)\n"
    9620                 :            : "super(type, type2) -> bound super object; requires issubclass(type2, type)\n"
    9621                 :            : "Typical use to call a cooperative superclass method:\n"
    9622                 :            : "class C(B):\n"
    9623                 :            : "    def meth(self, arg):\n"
    9624                 :            : "        super().meth(arg)\n"
    9625                 :            : "This works for class methods too:\n"
    9626                 :            : "class C(B):\n"
    9627                 :            : "    @classmethod\n"
    9628                 :            : "    def cmeth(cls, arg):\n"
    9629                 :            : "        super().cmeth(arg)\n");
    9630                 :            : 
    9631                 :            : static int
    9632                 :          2 : super_traverse(PyObject *self, visitproc visit, void *arg)
    9633                 :            : {
    9634                 :          2 :     superobject *su = (superobject *)self;
    9635                 :            : 
    9636   [ +  -  -  + ]:          2 :     Py_VISIT(su->obj);
    9637   [ +  -  -  + ]:          2 :     Py_VISIT(su->type);
    9638   [ +  -  -  + ]:          2 :     Py_VISIT(su->obj_type);
    9639                 :            : 
    9640                 :          2 :     return 0;
    9641                 :            : }
    9642                 :            : 
    9643                 :            : static PyObject *
    9644                 :     169996 : super_vectorcall(PyObject *self, PyObject *const *args,
    9645                 :            :     size_t nargsf, PyObject *kwnames)
    9646                 :            : {
    9647                 :            :     assert(PyType_Check(self));
    9648   [ -  +  -  - ]:     169996 :     if (!_PyArg_NoKwnames("super", kwnames)) {
    9649                 :          0 :         return NULL;
    9650                 :            :     }
    9651                 :     169996 :     Py_ssize_t nargs = PyVectorcall_NARGS(nargsf);
    9652   [ +  -  -  +  :     169996 :     if (!_PyArg_CheckPositional("super()", nargs, 0, 2)) {
                   -  - ]
    9653                 :          0 :         return NULL;
    9654                 :            :     }
    9655                 :     169996 :     PyTypeObject *type = NULL;
    9656                 :     169996 :     PyObject *obj = NULL;
    9657                 :     169996 :     PyTypeObject *self_type = (PyTypeObject *)self;
    9658                 :     169996 :     PyObject *su = self_type->tp_alloc(self_type, 0);
    9659         [ -  + ]:     169996 :     if (su == NULL) {
    9660                 :          0 :         return NULL;
    9661                 :            :     }
    9662                 :            :     // 1 or 2 argument form super().
    9663         [ +  + ]:     169996 :     if (nargs != 0) {
    9664                 :     151799 :         PyObject *arg0 = args[0];
    9665         [ -  + ]:     151799 :         if (!PyType_Check(arg0)) {
    9666                 :          0 :             PyErr_Format(PyExc_TypeError,
    9667                 :          0 :                 "super() argument 1 must be a type, not %.200s", Py_TYPE(arg0)->tp_name);
    9668                 :          0 :             goto fail;
    9669                 :            :         }
    9670                 :     151799 :         type = (PyTypeObject *)arg0;
    9671                 :            :     }
    9672         [ +  + ]:     169996 :     if (nargs == 2) {
    9673                 :     151799 :         obj = args[1];
    9674                 :            :     }
    9675         [ -  + ]:     169996 :     if (super_init_impl(su, type, obj) < 0) {
    9676                 :          0 :         goto fail;
    9677                 :            :     }
    9678                 :     169996 :     return su;
    9679                 :          0 : fail:
    9680                 :          0 :     Py_DECREF(su);
    9681                 :          0 :     return NULL;
    9682                 :            : }
    9683                 :            : 
    9684                 :            : PyTypeObject PySuper_Type = {
    9685                 :            :     PyVarObject_HEAD_INIT(&PyType_Type, 0)
    9686                 :            :     "super",                                    /* tp_name */
    9687                 :            :     sizeof(superobject),                        /* tp_basicsize */
    9688                 :            :     0,                                          /* tp_itemsize */
    9689                 :            :     /* methods */
    9690                 :            :     super_dealloc,                              /* tp_dealloc */
    9691                 :            :     0,                                          /* tp_vectorcall_offset */
    9692                 :            :     0,                                          /* tp_getattr */
    9693                 :            :     0,                                          /* tp_setattr */
    9694                 :            :     0,                                          /* tp_as_async */
    9695                 :            :     super_repr,                                 /* tp_repr */
    9696                 :            :     0,                                          /* tp_as_number */
    9697                 :            :     0,                                          /* tp_as_sequence */
    9698                 :            :     0,                                          /* tp_as_mapping */
    9699                 :            :     0,                                          /* tp_hash */
    9700                 :            :     0,                                          /* tp_call */
    9701                 :            :     0,                                          /* tp_str */
    9702                 :            :     super_getattro,                             /* tp_getattro */
    9703                 :            :     0,                                          /* tp_setattro */
    9704                 :            :     0,                                          /* tp_as_buffer */
    9705                 :            :     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
    9706                 :            :         Py_TPFLAGS_BASETYPE,                    /* tp_flags */
    9707                 :            :     super_doc,                                  /* tp_doc */
    9708                 :            :     super_traverse,                             /* tp_traverse */
    9709                 :            :     0,                                          /* tp_clear */
    9710                 :            :     0,                                          /* tp_richcompare */
    9711                 :            :     0,                                          /* tp_weaklistoffset */
    9712                 :            :     0,                                          /* tp_iter */
    9713                 :            :     0,                                          /* tp_iternext */
    9714                 :            :     0,                                          /* tp_methods */
    9715                 :            :     super_members,                              /* tp_members */
    9716                 :            :     0,                                          /* tp_getset */
    9717                 :            :     0,                                          /* tp_base */
    9718                 :            :     0,                                          /* tp_dict */
    9719                 :            :     super_descr_get,                            /* tp_descr_get */
    9720                 :            :     0,                                          /* tp_descr_set */
    9721                 :            :     0,                                          /* tp_dictoffset */
    9722                 :            :     super_init,                                 /* tp_init */
    9723                 :            :     PyType_GenericAlloc,                        /* tp_alloc */
    9724                 :            :     PyType_GenericNew,                          /* tp_new */
    9725                 :            :     PyObject_GC_Del,                            /* tp_free */
    9726                 :            :     .tp_vectorcall = (vectorcallfunc)super_vectorcall,
    9727                 :            : };

Generated by: LCOV version 1.14