Python-2.7.3/Objects/typeobject.c

Location Tool Test ID Function Issue
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4737:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4737:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4744:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4744:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4752:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4752:22 clang-analyzer Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4767:45 clang-analyzer Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4767:45 clang-analyzer Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4779:11 clang-analyzer Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:4779:11 clang-analyzer Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:6041:23 clang-analyzer Access to field 'tp_as_sequence' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:6045:23 clang-analyzer Access to field 'tp_as_mapping' results in a dereference of a null pointer (loaded from variable 'type')
/builddir/build/BUILD/Python-2.7.3/Objects/typeobject.c:6049:23 clang-analyzer Access to field 'tp_as_number' results in a dereference of a null pointer (loaded from variable 'type')
   1 /* Type object implementation */
   2 
   3 #include "Python.h"
   4 #include "structmember.h"
   5 
   6 #include <ctype.h>
   7 
   8 
   9 /* Support type attribute cache */
  10 
  11 /* The cache can keep references to the names alive for longer than
  12    they normally would.  This is why the maximum size is limited to
  13    MCACHE_MAX_ATTR_SIZE, since it might be a problem if very large
  14    strings are used as attribute names. */
  15 #define MCACHE_MAX_ATTR_SIZE    100
  16 #define MCACHE_SIZE_EXP         10
  17 #define MCACHE_HASH(version, name_hash)                                 \
  18         (((unsigned int)(version) * (unsigned int)(name_hash))          \
  19          >> (8*sizeof(unsigned int) - MCACHE_SIZE_EXP))
  20 #define MCACHE_HASH_METHOD(type, name)                                  \
  21         MCACHE_HASH((type)->tp_version_tag,                     \
  22                     ((PyStringObject *)(name))->ob_shash)
  23 #define MCACHE_CACHEABLE_NAME(name)                                     \
  24         PyString_CheckExact(name) &&                            \
  25         PyString_GET_SIZE(name) <= MCACHE_MAX_ATTR_SIZE
  26 
  27 struct method_cache_entry {
  28     unsigned int version;
  29     PyObject *name;             /* reference to exactly a str or None */
  30     PyObject *value;            /* borrowed */
  31 };
  32 
  33 static struct method_cache_entry method_cache[1 << MCACHE_SIZE_EXP];
  34 static unsigned int next_version_tag = 0;
  35 
  36 unsigned int
  37 PyType_ClearCache(void)
  38 {
  39     Py_ssize_t i;
  40     unsigned int cur_version_tag = next_version_tag - 1;
  41 
  42     for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
  43         method_cache[i].version = 0;
  44         Py_CLEAR(method_cache[i].name);
  45         method_cache[i].value = NULL;
  46     }
  47     next_version_tag = 0;
  48     /* mark all version tags as invalid */
  49     PyType_Modified(&PyBaseObject_Type);
  50     return cur_version_tag;
  51 }
  52 
  53 void
  54 PyType_Modified(PyTypeObject *type)
  55 {
  56     /* Invalidate any cached data for the specified type and all
  57        subclasses.  This function is called after the base
  58        classes, mro, or attributes of the type are altered.
  59 
  60        Invariants:
  61 
  62        - Py_TPFLAGS_VALID_VERSION_TAG is never set if
  63          Py_TPFLAGS_HAVE_VERSION_TAG is not set (e.g. on type
  64          objects coming from non-recompiled extension modules)
  65 
  66        - before Py_TPFLAGS_VALID_VERSION_TAG can be set on a type,
  67          it must first be set on all super types.
  68 
  69        This function clears the Py_TPFLAGS_VALID_VERSION_TAG of a
  70        type (so it must first clear it on all subclasses).  The
  71        tp_version_tag value is meaningless unless this flag is set.
  72        We don't assign new version tags eagerly, but only as
  73        needed.
  74      */
  75     PyObject *raw, *ref;
  76     Py_ssize_t i, n;
  77 
  78     if (!PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
  79         return;
  80 
  81     raw = type->tp_subclasses;
  82     if (raw != NULL) {
  83         n = PyList_GET_SIZE(raw);
  84         for (i = 0; i < n; i++) {
  85             ref = PyList_GET_ITEM(raw, i);
  86             ref = PyWeakref_GET_OBJECT(ref);
  87             if (ref != Py_None) {
  88                 PyType_Modified((PyTypeObject *)ref);
  89             }
  90         }
  91     }
  92     type->tp_flags &= ~Py_TPFLAGS_VALID_VERSION_TAG;
  93 }
  94 
  95 static void
  96 type_mro_modified(PyTypeObject *type, PyObject *bases) {
  97     /*
  98        Check that all base classes or elements of the mro of type are
  99        able to be cached.  This function is called after the base
 100        classes or mro of the type are altered.
 101 
 102        Unset HAVE_VERSION_TAG and VALID_VERSION_TAG if the type
 103        inherits from an old-style class, either directly or if it
 104        appears in the MRO of a new-style class.  No support either for
 105        custom MROs that include types that are not officially super
 106        types.
 107 
 108        Called from mro_internal, which will subsequently be called on
 109        each subclass when their mro is recursively updated.
 110      */
 111     Py_ssize_t i, n;
 112     int clear = 0;
 113 
 114     if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
 115         return;
 116 
 117     n = PyTuple_GET_SIZE(bases);
 118     for (i = 0; i < n; i++) {
 119         PyObject *b = PyTuple_GET_ITEM(bases, i);
 120         PyTypeObject *cls;
 121 
 122         if (!PyType_Check(b) ) {
 123             clear = 1;
 124             break;
 125         }
 126 
 127         cls = (PyTypeObject *)b;
 128 
 129         if (!PyType_HasFeature(cls, Py_TPFLAGS_HAVE_VERSION_TAG) ||
 130             !PyType_IsSubtype(type, cls)) {
 131             clear = 1;
 132             break;
 133         }
 134     }
 135 
 136     if (clear)
 137         type->tp_flags &= ~(Py_TPFLAGS_HAVE_VERSION_TAG|
 138                             Py_TPFLAGS_VALID_VERSION_TAG);
 139 }
 140 
 141 static int
 142 assign_version_tag(PyTypeObject *type)
 143 {
 144     /* Ensure that the tp_version_tag is valid and set
 145        Py_TPFLAGS_VALID_VERSION_TAG.  To respect the invariant, this
 146        must first be done on all super classes.  Return 0 if this
 147        cannot be done, 1 if Py_TPFLAGS_VALID_VERSION_TAG.
 148     */
 149     Py_ssize_t i, n;
 150     PyObject *bases;
 151 
 152     if (PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG))
 153         return 1;
 154     if (!PyType_HasFeature(type, Py_TPFLAGS_HAVE_VERSION_TAG))
 155         return 0;
 156     if (!PyType_HasFeature(type, Py_TPFLAGS_READY))
 157         return 0;
 158 
 159     type->tp_version_tag = next_version_tag++;
 160     /* for stress-testing: next_version_tag &= 0xFF; */
 161 
 162     if (type->tp_version_tag == 0) {
 163         /* wrap-around or just starting Python - clear the whole
 164            cache by filling names with references to Py_None.
 165            Values are also set to NULL for added protection, as they
 166            are borrowed reference */
 167         for (i = 0; i < (1 << MCACHE_SIZE_EXP); i++) {
 168             method_cache[i].value = NULL;
 169             Py_XDECREF(method_cache[i].name);
 170             method_cache[i].name = Py_None;
 171             Py_INCREF(Py_None);
 172         }
 173         /* mark all version tags as invalid */
 174         PyType_Modified(&PyBaseObject_Type);
 175         return 1;
 176     }
 177     bases = type->tp_bases;
 178     n = PyTuple_GET_SIZE(bases);
 179     for (i = 0; i < n; i++) {
 180         PyObject *b = PyTuple_GET_ITEM(bases, i);
 181         assert(PyType_Check(b));
 182         if (!assign_version_tag((PyTypeObject *)b))
 183             return 0;
 184     }
 185     type->tp_flags |= Py_TPFLAGS_VALID_VERSION_TAG;
 186     return 1;
 187 }
 188 
 189 
 190 static PyMemberDef type_members[] = {
 191     {"__basicsize__", T_PYSSIZET, offsetof(PyTypeObject,tp_basicsize),READONLY},
 192     {"__itemsize__", T_PYSSIZET, offsetof(PyTypeObject, tp_itemsize), READONLY},
 193     {"__flags__", T_LONG, offsetof(PyTypeObject, tp_flags), READONLY},
 194     {"__weakrefoffset__", T_LONG,
 195      offsetof(PyTypeObject, tp_weaklistoffset), READONLY},
 196     {"__base__", T_OBJECT, offsetof(PyTypeObject, tp_base), READONLY},
 197     {"__dictoffset__", T_LONG,
 198      offsetof(PyTypeObject, tp_dictoffset), READONLY},
 199     {"__mro__", T_OBJECT, offsetof(PyTypeObject, tp_mro), READONLY},
 200     {0}
 201 };
 202 
 203 static PyObject *
 204 type_name(PyTypeObject *type, void *context)
 205 {
 206     const char *s;
 207 
 208     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
 209         PyHeapTypeObject* et = (PyHeapTypeObject*)type;
 210 
 211         Py_INCREF(et->ht_name);
 212         return et->ht_name;
 213     }
 214     else {
 215         s = strrchr(type->tp_name, '.');
 216         if (s == NULL)
 217             s = type->tp_name;
 218         else
 219             s++;
 220         return PyString_FromString(s);
 221     }
 222 }
 223 
 224 static int
 225 type_set_name(PyTypeObject *type, PyObject *value, void *context)
 226 {
 227     PyHeapTypeObject* et;
 228 
 229     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
 230         PyErr_Format(PyExc_TypeError,
 231                      "can't set %s.__name__", type->tp_name);
 232         return -1;
 233     }
 234     if (!value) {
 235         PyErr_Format(PyExc_TypeError,
 236                      "can't delete %s.__name__", type->tp_name);
 237         return -1;
 238     }
 239     if (!PyString_Check(value)) {
 240         PyErr_Format(PyExc_TypeError,
 241                      "can only assign string to %s.__name__, not '%s'",
 242                      type->tp_name, Py_TYPE(value)->tp_name);
 243         return -1;
 244     }
 245     if (strlen(PyString_AS_STRING(value))
 246         != (size_t)PyString_GET_SIZE(value)) {
 247         PyErr_Format(PyExc_ValueError,
 248                      "__name__ must not contain null bytes");
 249         return -1;
 250     }
 251 
 252     et = (PyHeapTypeObject*)type;
 253 
 254     Py_INCREF(value);
 255 
 256     Py_DECREF(et->ht_name);
 257     et->ht_name = value;
 258 
 259     type->tp_name = PyString_AS_STRING(value);
 260 
 261     return 0;
 262 }
 263 
 264 static PyObject *
 265 type_module(PyTypeObject *type, void *context)
 266 {
 267     PyObject *mod;
 268     char *s;
 269 
 270     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE) {
 271         mod = PyDict_GetItemString(type->tp_dict, "__module__");
 272         if (!mod) {
 273             PyErr_Format(PyExc_AttributeError, "__module__");
 274             return 0;
 275         }
 276         Py_XINCREF(mod);
 277         return mod;
 278     }
 279     else {
 280         s = strrchr(type->tp_name, '.');
 281         if (s != NULL)
 282             return PyString_FromStringAndSize(
 283                 type->tp_name, (Py_ssize_t)(s - type->tp_name));
 284         return PyString_FromString("__builtin__");
 285     }
 286 }
 287 
 288 static int
 289 type_set_module(PyTypeObject *type, PyObject *value, void *context)
 290 {
 291     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
 292         PyErr_Format(PyExc_TypeError,
 293                      "can't set %s.__module__", type->tp_name);
 294         return -1;
 295     }
 296     if (!value) {
 297         PyErr_Format(PyExc_TypeError,
 298                      "can't delete %s.__module__", type->tp_name);
 299         return -1;
 300     }
 301 
 302     PyType_Modified(type);
 303 
 304     return PyDict_SetItemString(type->tp_dict, "__module__", value);
 305 }
 306 
 307 static PyObject *
 308 type_abstractmethods(PyTypeObject *type, void *context)
 309 {
 310     PyObject *mod = NULL;
 311     /* type itself has an __abstractmethods__ descriptor (this). Don't return
 312        that. */
 313     if (type != &PyType_Type)
 314         mod = PyDict_GetItemString(type->tp_dict, "__abstractmethods__");
 315     if (!mod) {
 316         PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
 317         return NULL;
 318     }
 319     Py_XINCREF(mod);
 320     return mod;
 321 }
 322 
 323 static int
 324 type_set_abstractmethods(PyTypeObject *type, PyObject *value, void *context)
 325 {
 326     /* __abstractmethods__ should only be set once on a type, in
 327        abc.ABCMeta.__new__, so this function doesn't do anything
 328        special to update subclasses.
 329     */
 330     int res;
 331     if (value != NULL) {
 332         res = PyDict_SetItemString(type->tp_dict, "__abstractmethods__", value);
 333     }
 334     else {
 335         res = PyDict_DelItemString(type->tp_dict, "__abstractmethods__");
 336         if (res && PyErr_ExceptionMatches(PyExc_KeyError)) {
 337             PyErr_SetString(PyExc_AttributeError, "__abstractmethods__");
 338             return -1;
 339         }
 340     }
 341     if (res == 0) {
 342         PyType_Modified(type);
 343         if (value && PyObject_IsTrue(value)) {
 344             type->tp_flags |= Py_TPFLAGS_IS_ABSTRACT;
 345         }
 346         else {
 347             type->tp_flags &= ~Py_TPFLAGS_IS_ABSTRACT;
 348         }
 349     }
 350     return res;
 351 }
 352 
 353 static PyObject *
 354 type_get_bases(PyTypeObject *type, void *context)
 355 {
 356     Py_INCREF(type->tp_bases);
 357     return type->tp_bases;
 358 }
 359 
 360 static PyTypeObject *best_base(PyObject *);
 361 static int mro_internal(PyTypeObject *);
 362 static int compatible_for_assignment(PyTypeObject *, PyTypeObject *, char *);
 363 static int add_subclass(PyTypeObject*, PyTypeObject*);
 364 static void remove_subclass(PyTypeObject *, PyTypeObject *);
 365 static void update_all_slots(PyTypeObject *);
 366 
 367 typedef int (*update_callback)(PyTypeObject *, void *);
 368 static int update_subclasses(PyTypeObject *type, PyObject *name,
 369                              update_callback callback, void *data);
 370 static int recurse_down_subclasses(PyTypeObject *type, PyObject *name,
 371                                    update_callback callback, void *data);
 372 
 373 static int
 374 mro_subclasses(PyTypeObject *type, PyObject* temp)
 375 {
 376     PyTypeObject *subclass;
 377     PyObject *ref, *subclasses, *old_mro;
 378     Py_ssize_t i, n;
 379 
 380     subclasses = type->tp_subclasses;
 381     if (subclasses == NULL)
 382         return 0;
 383     assert(PyList_Check(subclasses));
 384     n = PyList_GET_SIZE(subclasses);
 385     for (i = 0; i < n; i++) {
 386         ref = PyList_GET_ITEM(subclasses, i);
 387         assert(PyWeakref_CheckRef(ref));
 388         subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref);
 389         assert(subclass != NULL);
 390         if ((PyObject *)subclass == Py_None)
 391             continue;
 392         assert(PyType_Check(subclass));
 393         old_mro = subclass->tp_mro;
 394         if (mro_internal(subclass) < 0) {
 395             subclass->tp_mro = old_mro;
 396             return -1;
 397         }
 398         else {
 399             PyObject* tuple;
 400             tuple = PyTuple_Pack(2, subclass, old_mro);
 401             Py_DECREF(old_mro);
 402             if (!tuple)
 403                 return -1;
 404             if (PyList_Append(temp, tuple) < 0)
 405                 return -1;
 406             Py_DECREF(tuple);
 407         }
 408         if (mro_subclasses(subclass, temp) < 0)
 409             return -1;
 410     }
 411     return 0;
 412 }
 413 
 414 static int
 415 type_set_bases(PyTypeObject *type, PyObject *value, void *context)
 416 {
 417     Py_ssize_t i;
 418     int r = 0;
 419     PyObject *ob, *temp;
 420     PyTypeObject *new_base, *old_base;
 421     PyObject *old_bases, *old_mro;
 422 
 423     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
 424         PyErr_Format(PyExc_TypeError,
 425                      "can't set %s.__bases__", type->tp_name);
 426         return -1;
 427     }
 428     if (!value) {
 429         PyErr_Format(PyExc_TypeError,
 430                      "can't delete %s.__bases__", type->tp_name);
 431         return -1;
 432     }
 433     if (!PyTuple_Check(value)) {
 434         PyErr_Format(PyExc_TypeError,
 435              "can only assign tuple to %s.__bases__, not %s",
 436                  type->tp_name, Py_TYPE(value)->tp_name);
 437         return -1;
 438     }
 439     if (PyTuple_GET_SIZE(value) == 0) {
 440         PyErr_Format(PyExc_TypeError,
 441              "can only assign non-empty tuple to %s.__bases__, not ()",
 442                  type->tp_name);
 443         return -1;
 444     }
 445     for (i = 0; i < PyTuple_GET_SIZE(value); i++) {
 446         ob = PyTuple_GET_ITEM(value, i);
 447         if (!PyClass_Check(ob) && !PyType_Check(ob)) {
 448             PyErr_Format(
 449                 PyExc_TypeError,
 450     "%s.__bases__ must be tuple of old- or new-style classes, not '%s'",
 451                             type->tp_name, Py_TYPE(ob)->tp_name);
 452                     return -1;
 453         }
 454         if (PyType_Check(ob)) {
 455             if (PyType_IsSubtype((PyTypeObject*)ob, type)) {
 456                 PyErr_SetString(PyExc_TypeError,
 457             "a __bases__ item causes an inheritance cycle");
 458                 return -1;
 459             }
 460         }
 461     }
 462 
 463     new_base = best_base(value);
 464 
 465     if (!new_base) {
 466         return -1;
 467     }
 468 
 469     if (!compatible_for_assignment(type->tp_base, new_base, "__bases__"))
 470         return -1;
 471 
 472     Py_INCREF(new_base);
 473     Py_INCREF(value);
 474 
 475     old_bases = type->tp_bases;
 476     old_base = type->tp_base;
 477     old_mro = type->tp_mro;
 478 
 479     type->tp_bases = value;
 480     type->tp_base = new_base;
 481 
 482     if (mro_internal(type) < 0) {
 483         goto bail;
 484     }
 485 
 486     temp = PyList_New(0);
 487     if (!temp)
 488         goto bail;
 489 
 490     r = mro_subclasses(type, temp);
 491 
 492     if (r < 0) {
 493         for (i = 0; i < PyList_Size(temp); i++) {
 494             PyTypeObject* cls;
 495             PyObject* mro;
 496             PyArg_UnpackTuple(PyList_GET_ITEM(temp, i),
 497                              "", 2, 2, &cls, &mro);
 498             Py_INCREF(mro);
 499             ob = cls->tp_mro;
 500             cls->tp_mro = mro;
 501             Py_DECREF(ob);
 502         }
 503         Py_DECREF(temp);
 504         goto bail;
 505     }
 506 
 507     Py_DECREF(temp);
 508 
 509     /* any base that was in __bases__ but now isn't, we
 510        need to remove |type| from its tp_subclasses.
 511        conversely, any class now in __bases__ that wasn't
 512        needs to have |type| added to its subclasses. */
 513 
 514     /* for now, sod that: just remove from all old_bases,
 515        add to all new_bases */
 516 
 517     for (i = PyTuple_GET_SIZE(old_bases) - 1; i >= 0; i--) {
 518         ob = PyTuple_GET_ITEM(old_bases, i);
 519         if (PyType_Check(ob)) {
 520             remove_subclass(
 521                 (PyTypeObject*)ob, type);
 522         }
 523     }
 524 
 525     for (i = PyTuple_GET_SIZE(value) - 1; i >= 0; i--) {
 526         ob = PyTuple_GET_ITEM(value, i);
 527         if (PyType_Check(ob)) {
 528             if (add_subclass((PyTypeObject*)ob, type) < 0)
 529                 r = -1;
 530         }
 531     }
 532 
 533     update_all_slots(type);
 534 
 535     Py_DECREF(old_bases);
 536     Py_DECREF(old_base);
 537     Py_DECREF(old_mro);
 538 
 539     return r;
 540 
 541   bail:
 542     Py_DECREF(type->tp_bases);
 543     Py_DECREF(type->tp_base);
 544     if (type->tp_mro != old_mro) {
 545         Py_DECREF(type->tp_mro);
 546     }
 547 
 548     type->tp_bases = old_bases;
 549     type->tp_base = old_base;
 550     type->tp_mro = old_mro;
 551 
 552     return -1;
 553 }
 554 
 555 static PyObject *
 556 type_dict(PyTypeObject *type, void *context)
 557 {
 558     if (type->tp_dict == NULL) {
 559         Py_INCREF(Py_None);
 560         return Py_None;
 561     }
 562     return PyDictProxy_New(type->tp_dict);
 563 }
 564 
 565 static PyObject *
 566 type_get_doc(PyTypeObject *type, void *context)
 567 {
 568     PyObject *result;
 569     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE) && type->tp_doc != NULL)
 570         return PyString_FromString(type->tp_doc);
 571     result = PyDict_GetItemString(type->tp_dict, "__doc__");
 572     if (result == NULL) {
 573         result = Py_None;
 574         Py_INCREF(result);
 575     }
 576     else if (Py_TYPE(result)->tp_descr_get) {
 577         result = Py_TYPE(result)->tp_descr_get(result, NULL,
 578                                                (PyObject *)type);
 579     }
 580     else {
 581         Py_INCREF(result);
 582     }
 583     return result;
 584 }
 585 
 586 static PyObject *
 587 type___instancecheck__(PyObject *type, PyObject *inst)
 588 {
 589     switch (_PyObject_RealIsInstance(inst, type)) {
 590     case -1:
 591         return NULL;
 592     case 0:
 593         Py_RETURN_FALSE;
 594     default:
 595         Py_RETURN_TRUE;
 596     }
 597 }
 598 
 599 
 600 static PyObject *
 601 type___subclasscheck__(PyObject *type, PyObject *inst)
 602 {
 603     switch (_PyObject_RealIsSubclass(inst, type)) {
 604     case -1:
 605         return NULL;
 606     case 0:
 607         Py_RETURN_FALSE;
 608     default:
 609         Py_RETURN_TRUE;
 610     }
 611 }
 612 
 613 
 614 static PyGetSetDef type_getsets[] = {
 615     {"__name__", (getter)type_name, (setter)type_set_name, NULL},
 616     {"__bases__", (getter)type_get_bases, (setter)type_set_bases, NULL},
 617     {"__module__", (getter)type_module, (setter)type_set_module, NULL},
 618     {"__abstractmethods__", (getter)type_abstractmethods,
 619      (setter)type_set_abstractmethods, NULL},
 620     {"__dict__",  (getter)type_dict,  NULL, NULL},
 621     {"__doc__", (getter)type_get_doc, NULL, NULL},
 622     {0}
 623 };
 624 
 625 
 626 static PyObject*
 627 type_richcompare(PyObject *v, PyObject *w, int op)
 628 {
 629     PyObject *result;
 630     Py_uintptr_t vv, ww;
 631     int c;
 632 
 633     /* Make sure both arguments are types. */
 634     if (!PyType_Check(v) || !PyType_Check(w) ||
 635         /* If there is a __cmp__ method defined, let it be called instead
 636            of our dumb function designed merely to warn.  See bug
 637            #7491. */
 638         Py_TYPE(v)->tp_compare || Py_TYPE(w)->tp_compare) {
 639         result = Py_NotImplemented;
 640         goto out;
 641     }
 642 
 643     /* Py3K warning if comparison isn't == or !=  */
 644     if (Py_Py3kWarningFlag && op != Py_EQ && op != Py_NE &&
 645         PyErr_WarnEx(PyExc_DeprecationWarning,
 646                    "type inequality comparisons not supported "
 647                    "in 3.x", 1) < 0) {
 648         return NULL;
 649     }
 650 
 651     /* Compare addresses */
 652     vv = (Py_uintptr_t)v;
 653     ww = (Py_uintptr_t)w;
 654     switch (op) {
 655     case Py_LT: c = vv <  ww; break;
 656     case Py_LE: c = vv <= ww; break;
 657     case Py_EQ: c = vv == ww; break;
 658     case Py_NE: c = vv != ww; break;
 659     case Py_GT: c = vv >  ww; break;
 660     case Py_GE: c = vv >= ww; break;
 661     default:
 662         result = Py_NotImplemented;
 663         goto out;
 664     }
 665     result = c ? Py_True : Py_False;
 666 
 667   /* incref and return */
 668   out:
 669     Py_INCREF(result);
 670     return result;
 671 }
 672 
 673 static PyObject *
 674 type_repr(PyTypeObject *type)
 675 {
 676     PyObject *mod, *name, *rtn;
 677     char *kind;
 678 
 679     mod = type_module(type, NULL);
 680     if (mod == NULL)
 681         PyErr_Clear();
 682     else if (!PyString_Check(mod)) {
 683         Py_DECREF(mod);
 684         mod = NULL;
 685     }
 686     name = type_name(type, NULL);
 687     if (name == NULL)
 688         return NULL;
 689 
 690     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
 691         kind = "class";
 692     else
 693         kind = "type";
 694 
 695     if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__")) {
 696         rtn = PyString_FromFormat("<%s '%s.%s'>",
 697                                   kind,
 698                                   PyString_AS_STRING(mod),
 699                                   PyString_AS_STRING(name));
 700     }
 701     else
 702         rtn = PyString_FromFormat("<%s '%s'>", kind, type->tp_name);
 703 
 704     Py_XDECREF(mod);
 705     Py_DECREF(name);
 706     return rtn;
 707 }
 708 
 709 static PyObject *
 710 type_call(PyTypeObject *type, PyObject *args, PyObject *kwds)
 711 {
 712     PyObject *obj;
 713 
 714     if (type->tp_new == NULL) {
 715         PyErr_Format(PyExc_TypeError,
 716                      "cannot create '%.100s' instances",
 717                      type->tp_name);
 718         return NULL;
 719     }
 720 
 721     obj = type->tp_new(type, args, kwds);
 722     if (obj != NULL) {
 723         /* Ugly exception: when the call was type(something),
 724            don't call tp_init on the result. */
 725         if (type == &PyType_Type &&
 726             PyTuple_Check(args) && PyTuple_GET_SIZE(args) == 1 &&
 727             (kwds == NULL ||
 728              (PyDict_Check(kwds) && PyDict_Size(kwds) == 0)))
 729             return obj;
 730         /* If the returned object is not an instance of type,
 731            it won't be initialized. */
 732         if (!PyType_IsSubtype(obj->ob_type, type))
 733             return obj;
 734         type = obj->ob_type;
 735         if (PyType_HasFeature(type, Py_TPFLAGS_HAVE_CLASS) &&
 736             type->tp_init != NULL &&
 737             type->tp_init(obj, args, kwds) < 0) {
 738             Py_DECREF(obj);
 739             obj = NULL;
 740         }
 741     }
 742     return obj;
 743 }
 744 
 745 PyObject *
 746 PyType_GenericAlloc(PyTypeObject *type, Py_ssize_t nitems)
 747 {
 748     PyObject *obj;
 749     const size_t size = _PyObject_VAR_SIZE(type, nitems+1);
 750     /* note that we need to add one, for the sentinel */
 751 
 752     if (PyType_IS_GC(type))
 753         obj = _PyObject_GC_Malloc(size);
 754     else
 755         obj = (PyObject *)PyObject_MALLOC(size);
 756 
 757     if (obj == NULL)
 758         return PyErr_NoMemory();
 759 
 760     memset(obj, '\0', size);
 761 
 762     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
 763         Py_INCREF(type);
 764 
 765     if (type->tp_itemsize == 0)
 766         PyObject_INIT(obj, type);
 767     else
 768         (void) PyObject_INIT_VAR((PyVarObject *)obj, type, nitems);
 769 
 770     if (PyType_IS_GC(type))
 771         _PyObject_GC_TRACK(obj);
 772     return obj;
 773 }
 774 
 775 PyObject *
 776 PyType_GenericNew(PyTypeObject *type, PyObject *args, PyObject *kwds)
 777 {
 778     return type->tp_alloc(type, 0);
 779 }
 780 
 781 /* Helpers for subtyping */
 782 
 783 static int
 784 traverse_slots(PyTypeObject *type, PyObject *self, visitproc visit, void *arg)
 785 {
 786     Py_ssize_t i, n;
 787     PyMemberDef *mp;
 788 
 789     n = Py_SIZE(type);
 790     mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
 791     for (i = 0; i < n; i++, mp++) {
 792         if (mp->type == T_OBJECT_EX) {
 793             char *addr = (char *)self + mp->offset;
 794             PyObject *obj = *(PyObject **)addr;
 795             if (obj != NULL) {
 796                 int err = visit(obj, arg);
 797                 if (err)
 798                     return err;
 799             }
 800         }
 801     }
 802     return 0;
 803 }
 804 
 805 static int
 806 subtype_traverse(PyObject *self, visitproc visit, void *arg)
 807 {
 808     PyTypeObject *type, *base;
 809     traverseproc basetraverse;
 810 
 811     /* Find the nearest base with a different tp_traverse,
 812        and traverse slots while we're at it */
 813     type = Py_TYPE(self);
 814     base = type;
 815     while ((basetraverse = base->tp_traverse) == subtype_traverse) {
 816         if (Py_SIZE(base)) {
 817             int err = traverse_slots(base, self, visit, arg);
 818             if (err)
 819                 return err;
 820         }
 821         base = base->tp_base;
 822         assert(base);
 823     }
 824 
 825     if (type->tp_dictoffset != base->tp_dictoffset) {
 826         PyObject **dictptr = _PyObject_GetDictPtr(self);
 827         if (dictptr && *dictptr)
 828             Py_VISIT(*dictptr);
 829     }
 830 
 831     if (type->tp_flags & Py_TPFLAGS_HEAPTYPE)
 832         /* For a heaptype, the instances count as references
 833            to the type.          Traverse the type so the collector
 834            can find cycles involving this link. */
 835         Py_VISIT(type);
 836 
 837     if (basetraverse)
 838         return basetraverse(self, visit, arg);
 839     return 0;
 840 }
 841 
 842 static void
 843 clear_slots(PyTypeObject *type, PyObject *self)
 844 {
 845     Py_ssize_t i, n;
 846     PyMemberDef *mp;
 847 
 848     n = Py_SIZE(type);
 849     mp = PyHeapType_GET_MEMBERS((PyHeapTypeObject *)type);
 850     for (i = 0; i < n; i++, mp++) {
 851         if (mp->type == T_OBJECT_EX && !(mp->flags & READONLY)) {
 852             char *addr = (char *)self + mp->offset;
 853             PyObject *obj = *(PyObject **)addr;
 854             if (obj != NULL) {
 855                 *(PyObject **)addr = NULL;
 856                 Py_DECREF(obj);
 857             }
 858         }
 859     }
 860 }
 861 
 862 static int
 863 subtype_clear(PyObject *self)
 864 {
 865     PyTypeObject *type, *base;
 866     inquiry baseclear;
 867 
 868     /* Find the nearest base with a different tp_clear
 869        and clear slots while we're at it */
 870     type = Py_TYPE(self);
 871     base = type;
 872     while ((baseclear = base->tp_clear) == subtype_clear) {
 873         if (Py_SIZE(base))
 874             clear_slots(base, self);
 875         base = base->tp_base;
 876         assert(base);
 877     }
 878 
 879     /* There's no need to clear the instance dict (if any);
 880        the collector will call its tp_clear handler. */
 881 
 882     if (baseclear)
 883         return baseclear(self);
 884     return 0;
 885 }
 886 
 887 static void
 888 subtype_dealloc(PyObject *self)
 889 {
 890     PyTypeObject *type, *base;
 891     destructor basedealloc;
 892 
 893     /* Extract the type; we expect it to be a heap type */
 894     type = Py_TYPE(self);
 895     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
 896 
 897     /* Test whether the type has GC exactly once */
 898 
 899     if (!PyType_IS_GC(type)) {
 900         /* It's really rare to find a dynamic type that doesn't have
 901            GC; it can only happen when deriving from 'object' and not
 902            adding any slots or instance variables.  This allows
 903            certain simplifications: there's no need to call
 904            clear_slots(), or DECREF the dict, or clear weakrefs. */
 905 
 906         /* Maybe call finalizer; exit early if resurrected */
 907         if (type->tp_del) {
 908             type->tp_del(self);
 909             if (self->ob_refcnt > 0)
 910                 return;
 911         }
 912 
 913         /* Find the nearest base with a different tp_dealloc */
 914         base = type;
 915         while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
 916             assert(Py_SIZE(base) == 0);
 917             base = base->tp_base;
 918             assert(base);
 919         }
 920 
 921         /* Extract the type again; tp_del may have changed it */
 922         type = Py_TYPE(self);
 923 
 924         /* Call the base tp_dealloc() */
 925         assert(basedealloc);
 926         basedealloc(self);
 927 
 928         /* Can't reference self beyond this point */
 929         Py_DECREF(type);
 930 
 931         /* Done */
 932         return;
 933     }
 934 
 935     /* We get here only if the type has GC */
 936 
 937     /* UnTrack and re-Track around the trashcan macro, alas */
 938     /* See explanation at end of function for full disclosure */
 939     PyObject_GC_UnTrack(self);
 940     ++_PyTrash_delete_nesting;
 941     Py_TRASHCAN_SAFE_BEGIN(self);
 942     --_PyTrash_delete_nesting;
 943     /* DO NOT restore GC tracking at this point.  weakref callbacks
 944      * (if any, and whether directly here or indirectly in something we
 945      * call) may trigger GC, and if self is tracked at that point, it
 946      * will look like trash to GC and GC will try to delete self again.
 947      */
 948 
 949     /* Find the nearest base with a different tp_dealloc */
 950     base = type;
 951     while ((basedealloc = base->tp_dealloc) == subtype_dealloc) {
 952         base = base->tp_base;
 953         assert(base);
 954     }
 955 
 956     /* If we added a weaklist, we clear it.      Do this *before* calling
 957        the finalizer (__del__), clearing slots, or clearing the instance
 958        dict. */
 959 
 960     if (type->tp_weaklistoffset && !base->tp_weaklistoffset)
 961         PyObject_ClearWeakRefs(self);
 962 
 963     /* Maybe call finalizer; exit early if resurrected */
 964     if (type->tp_del) {
 965         _PyObject_GC_TRACK(self);
 966         type->tp_del(self);
 967         if (self->ob_refcnt > 0)
 968             goto endlabel;              /* resurrected */
 969         else
 970             _PyObject_GC_UNTRACK(self);
 971         /* New weakrefs could be created during the finalizer call.
 972             If this occurs, clear them out without calling their
 973             finalizers since they might rely on part of the object
 974             being finalized that has already been destroyed. */
 975         if (type->tp_weaklistoffset && !base->tp_weaklistoffset) {
 976             /* Modeled after GET_WEAKREFS_LISTPTR() */
 977             PyWeakReference **list = (PyWeakReference **) \
 978                 PyObject_GET_WEAKREFS_LISTPTR(self);
 979             while (*list)
 980                 _PyWeakref_ClearRef(*list);
 981         }
 982     }
 983 
 984     /*  Clear slots up to the nearest base with a different tp_dealloc */
 985     base = type;
 986     while (base->tp_dealloc == subtype_dealloc) {
 987         if (Py_SIZE(base))
 988             clear_slots(base, self);
 989         base = base->tp_base;
 990         assert(base);
 991     }
 992 
 993     /* If we added a dict, DECREF it */
 994     if (type->tp_dictoffset && !base->tp_dictoffset) {
 995         PyObject **dictptr = _PyObject_GetDictPtr(self);
 996         if (dictptr != NULL) {
 997             PyObject *dict = *dictptr;
 998             if (dict != NULL) {
 999                 Py_DECREF(dict);
1000                 *dictptr = NULL;
1001             }
1002         }
1003     }
1004 
1005     /* Extract the type again; tp_del may have changed it */
1006     type = Py_TYPE(self);
1007 
1008     /* Call the base tp_dealloc(); first retrack self if
1009      * basedealloc knows about gc.
1010      */
1011     if (PyType_IS_GC(base))
1012         _PyObject_GC_TRACK(self);
1013     assert(basedealloc);
1014     basedealloc(self);
1015 
1016     /* Can't reference self beyond this point */
1017     Py_DECREF(type);
1018 
1019   endlabel:
1020     ++_PyTrash_delete_nesting;
1021     Py_TRASHCAN_SAFE_END(self);
1022     --_PyTrash_delete_nesting;
1023 
1024     /* Explanation of the weirdness around the trashcan macros:
1025 
1026        Q. What do the trashcan macros do?
1027 
1028        A. Read the comment titled "Trashcan mechanism" in object.h.
1029           For one, this explains why there must be a call to GC-untrack
1030           before the trashcan begin macro.      Without understanding the
1031           trashcan code, the answers to the following questions don't make
1032           sense.
1033 
1034        Q. Why do we GC-untrack before the trashcan and then immediately
1035           GC-track again afterward?
1036 
1037        A. In the case that the base class is GC-aware, the base class
1038           probably GC-untracks the object.      If it does that using the
1039           UNTRACK macro, this will crash when the object is already
1040           untracked.  Because we don't know what the base class does, the
1041           only safe thing is to make sure the object is tracked when we
1042           call the base class dealloc.  But...  The trashcan begin macro
1043           requires that the object is *untracked* before it is called.  So
1044           the dance becomes:
1045 
1046          GC untrack
1047          trashcan begin
1048          GC track
1049 
1050        Q. Why did the last question say "immediately GC-track again"?
1051           It's nowhere near immediately.
1052 
1053        A. Because the code *used* to re-track immediately.      Bad Idea.
1054           self has a refcount of 0, and if gc ever gets its hands on it
1055           (which can happen if any weakref callback gets invoked), it
1056           looks like trash to gc too, and gc also tries to delete self
1057           then.  But we're already deleting self.  Double deallocation is
1058           a subtle disaster.
1059 
1060        Q. Why the bizarre (net-zero) manipulation of
1061           _PyTrash_delete_nesting around the trashcan macros?
1062 
1063        A. Some base classes (e.g. list) also use the trashcan mechanism.
1064           The following scenario used to be possible:
1065 
1066           - suppose the trashcan level is one below the trashcan limit
1067 
1068           - subtype_dealloc() is called
1069 
1070           - the trashcan limit is not yet reached, so the trashcan level
1071         is incremented and the code between trashcan begin and end is
1072         executed
1073 
1074           - this destroys much of the object's contents, including its
1075         slots and __dict__
1076 
1077           - basedealloc() is called; this is really list_dealloc(), or
1078         some other type which also uses the trashcan macros
1079 
1080           - the trashcan limit is now reached, so the object is put on the
1081         trashcan's to-be-deleted-later list
1082 
1083           - basedealloc() returns
1084 
1085           - subtype_dealloc() decrefs the object's type
1086 
1087           - subtype_dealloc() returns
1088 
1089           - later, the trashcan code starts deleting the objects from its
1090         to-be-deleted-later list
1091 
1092           - subtype_dealloc() is called *AGAIN* for the same object
1093 
1094           - at the very least (if the destroyed slots and __dict__ don't
1095         cause problems) the object's type gets decref'ed a second
1096         time, which is *BAD*!!!
1097 
1098           The remedy is to make sure that if the code between trashcan
1099           begin and end in subtype_dealloc() is called, the code between
1100           trashcan begin and end in basedealloc() will also be called.
1101           This is done by decrementing the level after passing into the
1102           trashcan block, and incrementing it just before leaving the
1103           block.
1104 
1105           But now it's possible that a chain of objects consisting solely
1106           of objects whose deallocator is subtype_dealloc() will defeat
1107           the trashcan mechanism completely: the decremented level means
1108           that the effective level never reaches the limit.      Therefore, we
1109           *increment* the level *before* entering the trashcan block, and
1110           matchingly decrement it after leaving.  This means the trashcan
1111           code will trigger a little early, but that's no big deal.
1112 
1113        Q. Are there any live examples of code in need of all this
1114           complexity?
1115 
1116        A. Yes.  See SF bug 668433 for code that crashed (when Python was
1117           compiled in debug mode) before the trashcan level manipulations
1118           were added.  For more discussion, see SF patches 581742, 575073
1119           and bug 574207.
1120     */
1121 }
1122 
1123 static PyTypeObject *solid_base(PyTypeObject *type);
1124 
1125 /* type test with subclassing support */
1126 
1127 int
1128 PyType_IsSubtype(PyTypeObject *a, PyTypeObject *b)
1129 {
1130     PyObject *mro;
1131 
1132     if (!(a->tp_flags & Py_TPFLAGS_HAVE_CLASS))
1133         return b == a || b == &PyBaseObject_Type;
1134 
1135     mro = a->tp_mro;
1136     if (mro != NULL) {
1137         /* Deal with multiple inheritance without recursion
1138            by walking the MRO tuple */
1139         Py_ssize_t i, n;
1140         assert(PyTuple_Check(mro));
1141         n = PyTuple_GET_SIZE(mro);
1142         for (i = 0; i < n; i++) {
1143             if (PyTuple_GET_ITEM(mro, i) == (PyObject *)b)
1144                 return 1;
1145         }
1146         return 0;
1147     }
1148     else {
1149         /* a is not completely initilized yet; follow tp_base */
1150         do {
1151             if (a == b)
1152                 return 1;
1153             a = a->tp_base;
1154         } while (a != NULL);
1155         return b == &PyBaseObject_Type;
1156     }
1157 }
1158 
1159 /* Internal routines to do a method lookup in the type
1160    without looking in the instance dictionary
1161    (so we can't use PyObject_GetAttr) but still binding
1162    it to the instance.  The arguments are the object,
1163    the method name as a C string, and the address of a
1164    static variable used to cache the interned Python string.
1165 
1166    Two variants:
1167 
1168    - lookup_maybe() returns NULL without raising an exception
1169      when the _PyType_Lookup() call fails;
1170 
1171    - lookup_method() always raises an exception upon errors.
1172 
1173    - _PyObject_LookupSpecial() exported for the benefit of other places.
1174 */
1175 
1176 static PyObject *
1177 lookup_maybe(PyObject *self, char *attrstr, PyObject **attrobj)
1178 {
1179     PyObject *res;
1180 
1181     if (*attrobj == NULL) {
1182         *attrobj = PyString_InternFromString(attrstr);
1183         if (*attrobj == NULL)
1184             return NULL;
1185     }
1186     res = _PyType_Lookup(Py_TYPE(self), *attrobj);
1187     if (res != NULL) {
1188         descrgetfunc f;
1189         if ((f = Py_TYPE(res)->tp_descr_get) == NULL)
1190             Py_INCREF(res);
1191         else
1192             res = f(res, self, (PyObject *)(Py_TYPE(self)));
1193     }
1194     return res;
1195 }
1196 
1197 static PyObject *
1198 lookup_method(PyObject *self, char *attrstr, PyObject **attrobj)
1199 {
1200     PyObject *res = lookup_maybe(self, attrstr, attrobj);
1201     if (res == NULL && !PyErr_Occurred())
1202         PyErr_SetObject(PyExc_AttributeError, *attrobj);
1203     return res;
1204 }
1205 
1206 PyObject *
1207 _PyObject_LookupSpecial(PyObject *self, char *attrstr, PyObject **attrobj)
1208 {
1209     assert(!PyInstance_Check(self));
1210     return lookup_maybe(self, attrstr, attrobj);
1211 }
1212 
1213 /* A variation of PyObject_CallMethod that uses lookup_method()
1214    instead of PyObject_GetAttrString().  This uses the same convention
1215    as lookup_method to cache the interned name string object. */
1216 
1217 static PyObject *
1218 call_method(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1219 {
1220     va_list va;
1221     PyObject *args, *func = 0, *retval;
1222     va_start(va, format);
1223 
1224     func = lookup_maybe(o, name, nameobj);
1225     if (func == NULL) {
1226         va_end(va);
1227         if (!PyErr_Occurred())
1228             PyErr_SetObject(PyExc_AttributeError, *nameobj);
1229         return NULL;
1230     }
1231 
1232     if (format && *format)
1233         args = Py_VaBuildValue(format, va);
1234     else
1235         args = PyTuple_New(0);
1236 
1237     va_end(va);
1238 
1239     if (args == NULL)
1240         return NULL;
1241 
1242     assert(PyTuple_Check(args));
1243     retval = PyObject_Call(func, args, NULL);
1244 
1245     Py_DECREF(args);
1246     Py_DECREF(func);
1247 
1248     return retval;
1249 }
1250 
1251 /* Clone of call_method() that returns NotImplemented when the lookup fails. */
1252 
1253 static PyObject *
1254 call_maybe(PyObject *o, char *name, PyObject **nameobj, char *format, ...)
1255 {
1256     va_list va;
1257     PyObject *args, *func = 0, *retval;
1258     va_start(va, format);
1259 
1260     func = lookup_maybe(o, name, nameobj);
1261     if (func == NULL) {
1262         va_end(va);
1263         if (!PyErr_Occurred()) {
1264             Py_INCREF(Py_NotImplemented);
1265             return Py_NotImplemented;
1266         }
1267         return NULL;
1268     }
1269 
1270     if (format && *format)
1271         args = Py_VaBuildValue(format, va);
1272     else
1273         args = PyTuple_New(0);
1274 
1275     va_end(va);
1276 
1277     if (args == NULL)
1278         return NULL;
1279 
1280     assert(PyTuple_Check(args));
1281     retval = PyObject_Call(func, args, NULL);
1282 
1283     Py_DECREF(args);
1284     Py_DECREF(func);
1285 
1286     return retval;
1287 }
1288 
1289 static int
1290 fill_classic_mro(PyObject *mro, PyObject *cls)
1291 {
1292     PyObject *bases, *base;
1293     Py_ssize_t i, n;
1294 
1295     assert(PyList_Check(mro));
1296     assert(PyClass_Check(cls));
1297     i = PySequence_Contains(mro, cls);
1298     if (i < 0)
1299         return -1;
1300     if (!i) {
1301         if (PyList_Append(mro, cls) < 0)
1302             return -1;
1303     }
1304     bases = ((PyClassObject *)cls)->cl_bases;
1305     assert(bases && PyTuple_Check(bases));
1306     n = PyTuple_GET_SIZE(bases);
1307     for (i = 0; i < n; i++) {
1308         base = PyTuple_GET_ITEM(bases, i);
1309         if (fill_classic_mro(mro, base) < 0)
1310             return -1;
1311     }
1312     return 0;
1313 }
1314 
1315 static PyObject *
1316 classic_mro(PyObject *cls)
1317 {
1318     PyObject *mro;
1319 
1320     assert(PyClass_Check(cls));
1321     mro = PyList_New(0);
1322     if (mro != NULL) {
1323         if (fill_classic_mro(mro, cls) == 0)
1324             return mro;
1325         Py_DECREF(mro);
1326     }
1327     return NULL;
1328 }
1329 
1330 /*
1331     Method resolution order algorithm C3 described in
1332     "A Monotonic Superclass Linearization for Dylan",
1333     by Kim Barrett, Bob Cassel, Paul Haahr,
1334     David A. Moon, Keith Playford, and P. Tucker Withington.
1335     (OOPSLA 1996)
1336 
1337     Some notes about the rules implied by C3:
1338 
1339     No duplicate bases.
1340     It isn't legal to repeat a class in a list of base classes.
1341 
1342     The next three properties are the 3 constraints in "C3".
1343 
1344     Local precendece order.
1345     If A precedes B in C's MRO, then A will precede B in the MRO of all
1346     subclasses of C.
1347 
1348     Monotonicity.
1349     The MRO of a class must be an extension without reordering of the
1350     MRO of each of its superclasses.
1351 
1352     Extended Precedence Graph (EPG).
1353     Linearization is consistent if there is a path in the EPG from
1354     each class to all its successors in the linearization.  See
1355     the paper for definition of EPG.
1356  */
1357 
1358 static int
1359 tail_contains(PyObject *list, int whence, PyObject *o) {
1360     Py_ssize_t j, size;
1361     size = PyList_GET_SIZE(list);
1362 
1363     for (j = whence+1; j < size; j++) {
1364         if (PyList_GET_ITEM(list, j) == o)
1365             return 1;
1366     }
1367     return 0;
1368 }
1369 
1370 static PyObject *
1371 class_name(PyObject *cls)
1372 {
1373     PyObject *name = PyObject_GetAttrString(cls, "__name__");
1374     if (name == NULL) {
1375         PyErr_Clear();
1376         Py_XDECREF(name);
1377         name = PyObject_Repr(cls);
1378     }
1379     if (name == NULL)
1380         return NULL;
1381     if (!PyString_Check(name)) {
1382         Py_DECREF(name);
1383         return NULL;
1384     }
1385     return name;
1386 }
1387 
1388 static int
1389 check_duplicates(PyObject *list)
1390 {
1391     Py_ssize_t i, j, n;
1392     /* Let's use a quadratic time algorithm,
1393        assuming that the bases lists is short.
1394     */
1395     n = PyList_GET_SIZE(list);
1396     for (i = 0; i < n; i++) {
1397         PyObject *o = PyList_GET_ITEM(list, i);
1398         for (j = i + 1; j < n; j++) {
1399             if (PyList_GET_ITEM(list, j) == o) {
1400                 o = class_name(o);
1401                 PyErr_Format(PyExc_TypeError,
1402                              "duplicate base class %s",
1403                              o ? PyString_AS_STRING(o) : "?");
1404                 Py_XDECREF(o);
1405                 return -1;
1406             }
1407         }
1408     }
1409     return 0;
1410 }
1411 
1412 /* Raise a TypeError for an MRO order disagreement.
1413 
1414    It's hard to produce a good error message.  In the absence of better
1415    insight into error reporting, report the classes that were candidates
1416    to be put next into the MRO.  There is some conflict between the
1417    order in which they should be put in the MRO, but it's hard to
1418    diagnose what constraint can't be satisfied.
1419 */
1420 
1421 static void
1422 set_mro_error(PyObject *to_merge, int *remain)
1423 {
1424     Py_ssize_t i, n, off, to_merge_size;
1425     char buf[1000];
1426     PyObject *k, *v;
1427     PyObject *set = PyDict_New();
1428     if (!set) return;
1429 
1430     to_merge_size = PyList_GET_SIZE(to_merge);
1431     for (i = 0; i < to_merge_size; i++) {
1432         PyObject *L = PyList_GET_ITEM(to_merge, i);
1433         if (remain[i] < PyList_GET_SIZE(L)) {
1434             PyObject *c = PyList_GET_ITEM(L, remain[i]);
1435             if (PyDict_SetItem(set, c, Py_None) < 0) {
1436                 Py_DECREF(set);
1437                 return;
1438             }
1439         }
1440     }
1441     n = PyDict_Size(set);
1442 
1443     off = PyOS_snprintf(buf, sizeof(buf), "Cannot create a \
1444 consistent method resolution\norder (MRO) for bases");
1445     i = 0;
1446     while (PyDict_Next(set, &i, &k, &v) && (size_t)off < sizeof(buf)) {
1447         PyObject *name = class_name(k);
1448         off += PyOS_snprintf(buf + off, sizeof(buf) - off, " %s",
1449                              name ? PyString_AS_STRING(name) : "?");
1450         Py_XDECREF(name);
1451         if (--n && (size_t)(off+1) < sizeof(buf)) {
1452             buf[off++] = ',';
1453             buf[off] = '\0';
1454         }
1455     }
1456     PyErr_SetString(PyExc_TypeError, buf);
1457     Py_DECREF(set);
1458 }
1459 
1460 static int
1461 pmerge(PyObject *acc, PyObject* to_merge) {
1462     Py_ssize_t i, j, to_merge_size, empty_cnt;
1463     int *remain;
1464     int ok;
1465 
1466     to_merge_size = PyList_GET_SIZE(to_merge);
1467 
1468     /* remain stores an index into each sublist of to_merge.
1469        remain[i] is the index of the next base in to_merge[i]
1470        that is not included in acc.
1471     */
1472     remain = (int *)PyMem_MALLOC(SIZEOF_INT*to_merge_size);
1473     if (remain == NULL)
1474         return -1;
1475     for (i = 0; i < to_merge_size; i++)
1476         remain[i] = 0;
1477 
1478   again:
1479     empty_cnt = 0;
1480     for (i = 0; i < to_merge_size; i++) {
1481         PyObject *candidate;
1482 
1483         PyObject *cur_list = PyList_GET_ITEM(to_merge, i);
1484 
1485         if (remain[i] >= PyList_GET_SIZE(cur_list)) {
1486             empty_cnt++;
1487             continue;
1488         }
1489 
1490         /* Choose next candidate for MRO.
1491 
1492            The input sequences alone can determine the choice.
1493            If not, choose the class which appears in the MRO
1494            of the earliest direct superclass of the new class.
1495         */
1496 
1497         candidate = PyList_GET_ITEM(cur_list, remain[i]);
1498         for (j = 0; j < to_merge_size; j++) {
1499             PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1500             if (tail_contains(j_lst, remain[j], candidate)) {
1501                 goto skip; /* continue outer loop */
1502             }
1503         }
1504         ok = PyList_Append(acc, candidate);
1505         if (ok < 0) {
1506             PyMem_Free(remain);
1507             return -1;
1508         }
1509         for (j = 0; j < to_merge_size; j++) {
1510             PyObject *j_lst = PyList_GET_ITEM(to_merge, j);
1511             if (remain[j] < PyList_GET_SIZE(j_lst) &&
1512                 PyList_GET_ITEM(j_lst, remain[j]) == candidate) {
1513                 remain[j]++;
1514             }
1515         }
1516         goto again;
1517       skip: ;
1518     }
1519 
1520     if (empty_cnt == to_merge_size) {
1521         PyMem_FREE(remain);
1522         return 0;
1523     }
1524     set_mro_error(to_merge, remain);
1525     PyMem_FREE(remain);
1526     return -1;
1527 }
1528 
1529 static PyObject *
1530 mro_implementation(PyTypeObject *type)
1531 {
1532     Py_ssize_t i, n;
1533     int ok;
1534     PyObject *bases, *result;
1535     PyObject *to_merge, *bases_aslist;
1536 
1537     if (type->tp_dict == NULL) {
1538         if (PyType_Ready(type) < 0)
1539             return NULL;
1540     }
1541 
1542     /* Find a superclass linearization that honors the constraints
1543        of the explicit lists of bases and the constraints implied by
1544        each base class.
1545 
1546        to_merge is a list of lists, where each list is a superclass
1547        linearization implied by a base class.  The last element of
1548        to_merge is the declared list of bases.
1549     */
1550 
1551     bases = type->tp_bases;
1552     n = PyTuple_GET_SIZE(bases);
1553 
1554     to_merge = PyList_New(n+1);
1555     if (to_merge == NULL)
1556         return NULL;
1557 
1558     for (i = 0; i < n; i++) {
1559         PyObject *base = PyTuple_GET_ITEM(bases, i);
1560         PyObject *parentMRO;
1561         if (PyType_Check(base))
1562             parentMRO = PySequence_List(
1563                 ((PyTypeObject*)base)->tp_mro);
1564         else
1565             parentMRO = classic_mro(base);
1566         if (parentMRO == NULL) {
1567             Py_DECREF(to_merge);
1568             return NULL;
1569         }
1570 
1571         PyList_SET_ITEM(to_merge, i, parentMRO);
1572     }
1573 
1574     bases_aslist = PySequence_List(bases);
1575     if (bases_aslist == NULL) {
1576         Py_DECREF(to_merge);
1577         return NULL;
1578     }
1579     /* This is just a basic sanity check. */
1580     if (check_duplicates(bases_aslist) < 0) {
1581         Py_DECREF(to_merge);
1582         Py_DECREF(bases_aslist);
1583         return NULL;
1584     }
1585     PyList_SET_ITEM(to_merge, n, bases_aslist);
1586 
1587     result = Py_BuildValue("[O]", (PyObject *)type);
1588     if (result == NULL) {
1589         Py_DECREF(to_merge);
1590         return NULL;
1591     }
1592 
1593     ok = pmerge(result, to_merge);
1594     Py_DECREF(to_merge);
1595     if (ok < 0) {
1596         Py_DECREF(result);
1597         return NULL;
1598     }
1599 
1600     return result;
1601 }
1602 
1603 static PyObject *
1604 mro_external(PyObject *self)
1605 {
1606     PyTypeObject *type = (PyTypeObject *)self;
1607 
1608     return mro_implementation(type);
1609 }
1610 
1611 static int
1612 mro_internal(PyTypeObject *type)
1613 {
1614     PyObject *mro, *result, *tuple;
1615     int checkit = 0;
1616 
1617     if (Py_TYPE(type) == &PyType_Type) {
1618         result = mro_implementation(type);
1619     }
1620     else {
1621         static PyObject *mro_str;
1622         checkit = 1;
1623         mro = lookup_method((PyObject *)type, "mro", &mro_str);
1624         if (mro == NULL)
1625             return -1;
1626         result = PyObject_CallObject(mro, NULL);
1627         Py_DECREF(mro);
1628     }
1629     if (result == NULL)
1630         return -1;
1631     tuple = PySequence_Tuple(result);
1632     Py_DECREF(result);
1633     if (tuple == NULL)
1634         return -1;
1635     if (checkit) {
1636         Py_ssize_t i, len;
1637         PyObject *cls;
1638         PyTypeObject *solid;
1639 
1640         solid = solid_base(type);
1641 
1642         len = PyTuple_GET_SIZE(tuple);
1643 
1644         for (i = 0; i < len; i++) {
1645             PyTypeObject *t;
1646             cls = PyTuple_GET_ITEM(tuple, i);
1647             if (PyClass_Check(cls))
1648                 continue;
1649             else if (!PyType_Check(cls)) {
1650                 PyErr_Format(PyExc_TypeError,
1651                  "mro() returned a non-class ('%.500s')",
1652                                  Py_TYPE(cls)->tp_name);
1653                 Py_DECREF(tuple);
1654                 return -1;
1655             }
1656             t = (PyTypeObject*)cls;
1657             if (!PyType_IsSubtype(solid, solid_base(t))) {
1658                 PyErr_Format(PyExc_TypeError,
1659              "mro() returned base with unsuitable layout ('%.500s')",
1660                                      t->tp_name);
1661                         Py_DECREF(tuple);
1662                         return -1;
1663             }
1664         }
1665     }
1666     type->tp_mro = tuple;
1667 
1668     type_mro_modified(type, type->tp_mro);
1669     /* corner case: the old-style super class might have been hidden
1670        from the custom MRO */
1671     type_mro_modified(type, type->tp_bases);
1672 
1673     PyType_Modified(type);
1674 
1675     return 0;
1676 }
1677 
1678 
1679 /* Calculate the best base amongst multiple base classes.
1680    This is the first one that's on the path to the "solid base". */
1681 
1682 static PyTypeObject *
1683 best_base(PyObject *bases)
1684 {
1685     Py_ssize_t i, n;
1686     PyTypeObject *base, *winner, *candidate, *base_i;
1687     PyObject *base_proto;
1688 
1689     assert(PyTuple_Check(bases));
1690     n = PyTuple_GET_SIZE(bases);
1691     assert(n > 0);
1692     base = NULL;
1693     winner = NULL;
1694     for (i = 0; i < n; i++) {
1695         base_proto = PyTuple_GET_ITEM(bases, i);
1696         if (PyClass_Check(base_proto))
1697             continue;
1698         if (!PyType_Check(base_proto)) {
1699             PyErr_SetString(
1700                 PyExc_TypeError,
1701                 "bases must be types");
1702             return NULL;
1703         }
1704         base_i = (PyTypeObject *)base_proto;
1705         if (base_i->tp_dict == NULL) {
1706             if (PyType_Ready(base_i) < 0)
1707                 return NULL;
1708         }
1709         candidate = solid_base(base_i);
1710         if (winner == NULL) {
1711             winner = candidate;
1712             base = base_i;
1713         }
1714         else if (PyType_IsSubtype(winner, candidate))
1715             ;
1716         else if (PyType_IsSubtype(candidate, winner)) {
1717             winner = candidate;
1718             base = base_i;
1719         }
1720         else {
1721             PyErr_SetString(
1722                 PyExc_TypeError,
1723                 "multiple bases have "
1724                 "instance lay-out conflict");
1725             return NULL;
1726         }
1727     }
1728     if (base == NULL)
1729         PyErr_SetString(PyExc_TypeError,
1730             "a new-style class can't have only classic bases");
1731     return base;
1732 }
1733 
1734 static int
1735 extra_ivars(PyTypeObject *type, PyTypeObject *base)
1736 {
1737     size_t t_size = type->tp_basicsize;
1738     size_t b_size = base->tp_basicsize;
1739 
1740     assert(t_size >= b_size); /* Else type smaller than base! */
1741     if (type->tp_itemsize || base->tp_itemsize) {
1742         /* If itemsize is involved, stricter rules */
1743         return t_size != b_size ||
1744             type->tp_itemsize != base->tp_itemsize;
1745     }
1746     if (type->tp_weaklistoffset && base->tp_weaklistoffset == 0 &&
1747         type->tp_weaklistoffset + sizeof(PyObject *) == t_size &&
1748         type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1749         t_size -= sizeof(PyObject *);
1750     if (type->tp_dictoffset && base->tp_dictoffset == 0 &&
1751         type->tp_dictoffset + sizeof(PyObject *) == t_size &&
1752         type->tp_flags & Py_TPFLAGS_HEAPTYPE)
1753         t_size -= sizeof(PyObject *);
1754 
1755     return t_size != b_size;
1756 }
1757 
1758 static PyTypeObject *
1759 solid_base(PyTypeObject *type)
1760 {
1761     PyTypeObject *base;
1762 
1763     if (type->tp_base)
1764         base = solid_base(type->tp_base);
1765     else
1766         base = &PyBaseObject_Type;
1767     if (extra_ivars(type, base))
1768         return type;
1769     else
1770         return base;
1771 }
1772 
1773 static void object_dealloc(PyObject *);
1774 static int object_init(PyObject *, PyObject *, PyObject *);
1775 static int update_slot(PyTypeObject *, PyObject *);
1776 static void fixup_slot_dispatchers(PyTypeObject *);
1777 
1778 /*
1779  * Helpers for  __dict__ descriptor.  We don't want to expose the dicts
1780  * inherited from various builtin types.  The builtin base usually provides
1781  * its own __dict__ descriptor, so we use that when we can.
1782  */
1783 static PyTypeObject *
1784 get_builtin_base_with_dict(PyTypeObject *type)
1785 {
1786     while (type->tp_base != NULL) {
1787         if (type->tp_dictoffset != 0 &&
1788             !(type->tp_flags & Py_TPFLAGS_HEAPTYPE))
1789             return type;
1790         type = type->tp_base;
1791     }
1792     return NULL;
1793 }
1794 
1795 static PyObject *
1796 get_dict_descriptor(PyTypeObject *type)
1797 {
1798     static PyObject *dict_str;
1799     PyObject *descr;
1800 
1801     if (dict_str == NULL) {
1802         dict_str = PyString_InternFromString("__dict__");
1803         if (dict_str == NULL)
1804             return NULL;
1805     }
1806     descr = _PyType_Lookup(type, dict_str);
1807     if (descr == NULL || !PyDescr_IsData(descr))
1808         return NULL;
1809 
1810     return descr;
1811 }
1812 
1813 static void
1814 raise_dict_descr_error(PyObject *obj)
1815 {
1816     PyErr_Format(PyExc_TypeError,
1817                  "this __dict__ descriptor does not support "
1818                  "'%.200s' objects", obj->ob_type->tp_name);
1819 }
1820 
1821 static PyObject *
1822 subtype_dict(PyObject *obj, void *context)
1823 {
1824     PyObject **dictptr;
1825     PyObject *dict;
1826     PyTypeObject *base;
1827 
1828     base = get_builtin_base_with_dict(obj->ob_type);
1829     if (base != NULL) {
1830         descrgetfunc func;
1831         PyObject *descr = get_dict_descriptor(base);
1832         if (descr == NULL) {
1833             raise_dict_descr_error(obj);
1834             return NULL;
1835         }
1836         func = descr->ob_type->tp_descr_get;
1837         if (func == NULL) {
1838             raise_dict_descr_error(obj);
1839             return NULL;
1840         }
1841         return func(descr, obj, (PyObject *)(obj->ob_type));
1842     }
1843 
1844     dictptr = _PyObject_GetDictPtr(obj);
1845     if (dictptr == NULL) {
1846         PyErr_SetString(PyExc_AttributeError,
1847                         "This object has no __dict__");
1848         return NULL;
1849     }
1850     dict = *dictptr;
1851     if (dict == NULL)
1852         *dictptr = dict = PyDict_New();
1853     Py_XINCREF(dict);
1854     return dict;
1855 }
1856 
1857 static int
1858 subtype_setdict(PyObject *obj, PyObject *value, void *context)
1859 {
1860     PyObject **dictptr;
1861     PyObject *dict;
1862     PyTypeObject *base;
1863 
1864     base = get_builtin_base_with_dict(obj->ob_type);
1865     if (base != NULL) {
1866         descrsetfunc func;
1867         PyObject *descr = get_dict_descriptor(base);
1868         if (descr == NULL) {
1869             raise_dict_descr_error(obj);
1870             return -1;
1871         }
1872         func = descr->ob_type->tp_descr_set;
1873         if (func == NULL) {
1874             raise_dict_descr_error(obj);
1875             return -1;
1876         }
1877         return func(descr, obj, value);
1878     }
1879 
1880     dictptr = _PyObject_GetDictPtr(obj);
1881     if (dictptr == NULL) {
1882         PyErr_SetString(PyExc_AttributeError,
1883                         "This object has no __dict__");
1884         return -1;
1885     }
1886     if (value != NULL && !PyDict_Check(value)) {
1887         PyErr_Format(PyExc_TypeError,
1888                      "__dict__ must be set to a dictionary, "
1889                      "not a '%.200s'", Py_TYPE(value)->tp_name);
1890         return -1;
1891     }
1892     dict = *dictptr;
1893     Py_XINCREF(value);
1894     *dictptr = value;
1895     Py_XDECREF(dict);
1896     return 0;
1897 }
1898 
1899 static PyObject *
1900 subtype_getweakref(PyObject *obj, void *context)
1901 {
1902     PyObject **weaklistptr;
1903     PyObject *result;
1904 
1905     if (Py_TYPE(obj)->tp_weaklistoffset == 0) {
1906         PyErr_SetString(PyExc_AttributeError,
1907                         "This object has no __weakref__");
1908         return NULL;
1909     }
1910     assert(Py_TYPE(obj)->tp_weaklistoffset > 0);
1911     assert(Py_TYPE(obj)->tp_weaklistoffset + sizeof(PyObject *) <=
1912            (size_t)(Py_TYPE(obj)->tp_basicsize));
1913     weaklistptr = (PyObject **)
1914         ((char *)obj + Py_TYPE(obj)->tp_weaklistoffset);
1915     if (*weaklistptr == NULL)
1916         result = Py_None;
1917     else
1918         result = *weaklistptr;
1919     Py_INCREF(result);
1920     return result;
1921 }
1922 
1923 /* Three variants on the subtype_getsets list. */
1924 
1925 static PyGetSetDef subtype_getsets_full[] = {
1926     {"__dict__", subtype_dict, subtype_setdict,
1927      PyDoc_STR("dictionary for instance variables (if defined)")},
1928     {"__weakref__", subtype_getweakref, NULL,
1929      PyDoc_STR("list of weak references to the object (if defined)")},
1930     {0}
1931 };
1932 
1933 static PyGetSetDef subtype_getsets_dict_only[] = {
1934     {"__dict__", subtype_dict, subtype_setdict,
1935      PyDoc_STR("dictionary for instance variables (if defined)")},
1936     {0}
1937 };
1938 
1939 static PyGetSetDef subtype_getsets_weakref_only[] = {
1940     {"__weakref__", subtype_getweakref, NULL,
1941      PyDoc_STR("list of weak references to the object (if defined)")},
1942     {0}
1943 };
1944 
1945 static int
1946 valid_identifier(PyObject *s)
1947 {
1948     unsigned char *p;
1949     Py_ssize_t i, n;
1950 
1951     if (!PyString_Check(s)) {
1952         PyErr_Format(PyExc_TypeError,
1953                      "__slots__ items must be strings, not '%.200s'",
1954                      Py_TYPE(s)->tp_name);
1955         return 0;
1956     }
1957     p = (unsigned char *) PyString_AS_STRING(s);
1958     n = PyString_GET_SIZE(s);
1959     /* We must reject an empty name.  As a hack, we bump the
1960        length to 1 so that the loop will balk on the trailing \0. */
1961     if (n == 0)
1962         n = 1;
1963     for (i = 0; i < n; i++, p++) {
1964         if (!(i == 0 ? isalpha(*p) : isalnum(*p)) && *p != '_') {
1965             PyErr_SetString(PyExc_TypeError,
1966                             "__slots__ must be identifiers");
1967             return 0;
1968         }
1969     }
1970     return 1;
1971 }
1972 
1973 #ifdef Py_USING_UNICODE
1974 /* Replace Unicode objects in slots.  */
1975 
1976 static PyObject *
1977 _unicode_to_string(PyObject *slots, Py_ssize_t nslots)
1978 {
1979     PyObject *tmp = NULL;
1980     PyObject *slot_name, *new_name;
1981     Py_ssize_t i;
1982 
1983     for (i = 0; i < nslots; i++) {
1984         if (PyUnicode_Check(slot_name = PyTuple_GET_ITEM(slots, i))) {
1985             if (tmp == NULL) {
1986                 tmp = PySequence_List(slots);
1987                 if (tmp == NULL)
1988                     return NULL;
1989             }
1990             new_name = _PyUnicode_AsDefaultEncodedString(slot_name,
1991                                                          NULL);
1992             if (new_name == NULL) {
1993                 Py_DECREF(tmp);
1994                 return NULL;
1995             }
1996             Py_INCREF(new_name);
1997             PyList_SET_ITEM(tmp, i, new_name);
1998             Py_DECREF(slot_name);
1999         }
2000     }
2001     if (tmp != NULL) {
2002         slots = PyList_AsTuple(tmp);
2003         Py_DECREF(tmp);
2004     }
2005     return slots;
2006 }
2007 #endif
2008 
2009 /* Forward */
2010 static int
2011 object_init(PyObject *self, PyObject *args, PyObject *kwds);
2012 
2013 static int
2014 type_init(PyObject *cls, PyObject *args, PyObject *kwds)
2015 {
2016     int res;
2017 
2018     assert(args != NULL && PyTuple_Check(args));
2019     assert(kwds == NULL || PyDict_Check(kwds));
2020 
2021     if (kwds != NULL && PyDict_Check(kwds) && PyDict_Size(kwds) != 0) {
2022         PyErr_SetString(PyExc_TypeError,
2023                         "type.__init__() takes no keyword arguments");
2024         return -1;
2025     }
2026 
2027     if (args != NULL && PyTuple_Check(args) &&
2028         (PyTuple_GET_SIZE(args) != 1 && PyTuple_GET_SIZE(args) != 3)) {
2029         PyErr_SetString(PyExc_TypeError,
2030                         "type.__init__() takes 1 or 3 arguments");
2031         return -1;
2032     }
2033 
2034     /* Call object.__init__(self) now. */
2035     /* XXX Could call super(type, cls).__init__() but what's the point? */
2036     args = PyTuple_GetSlice(args, 0, 0);
2037     res = object_init(cls, args, NULL);
2038     Py_DECREF(args);
2039     return res;
2040 }
2041 
2042 static PyObject *
2043 type_new(PyTypeObject *metatype, PyObject *args, PyObject *kwds)
2044 {
2045     PyObject *name, *bases, *dict;
2046     static char *kwlist[] = {"name", "bases", "dict", 0};
2047     PyObject *slots, *tmp, *newslots;
2048     PyTypeObject *type, *base, *tmptype, *winner;
2049     PyHeapTypeObject *et;
2050     PyMemberDef *mp;
2051     Py_ssize_t i, nbases, nslots, slotoffset, add_dict, add_weak;
2052     int j, may_add_dict, may_add_weak;
2053 
2054     assert(args != NULL && PyTuple_Check(args));
2055     assert(kwds == NULL || PyDict_Check(kwds));
2056 
2057     /* Special case: type(x) should return x->ob_type */
2058     {
2059         const Py_ssize_t nargs = PyTuple_GET_SIZE(args);
2060         const Py_ssize_t nkwds = kwds == NULL ? 0 : PyDict_Size(kwds);
2061 
2062         if (PyType_CheckExact(metatype) && nargs == 1 && nkwds == 0) {
2063             PyObject *x = PyTuple_GET_ITEM(args, 0);
2064             Py_INCREF(Py_TYPE(x));
2065             return (PyObject *) Py_TYPE(x);
2066         }
2067 
2068         /* SF bug 475327 -- if that didn't trigger, we need 3
2069            arguments. but PyArg_ParseTupleAndKeywords below may give
2070            a msg saying type() needs exactly 3. */
2071         if (nargs + nkwds != 3) {
2072             PyErr_SetString(PyExc_TypeError,
2073                             "type() takes 1 or 3 arguments");
2074             return NULL;
2075         }
2076     }
2077 
2078     /* Check arguments: (name, bases, dict) */
2079     if (!PyArg_ParseTupleAndKeywords(args, kwds, "SO!O!:type", kwlist,
2080                                      &name,
2081                                      &PyTuple_Type, &bases,
2082                                      &PyDict_Type, &dict))
2083         return NULL;
2084 
2085     /* Determine the proper metatype to deal with this,
2086        and check for metatype conflicts while we're at it.
2087        Note that if some other metatype wins to contract,
2088        it's possible that its instances are not types. */
2089     nbases = PyTuple_GET_SIZE(bases);
2090     winner = metatype;
2091     for (i = 0; i < nbases; i++) {
2092         tmp = PyTuple_GET_ITEM(bases, i);
2093         tmptype = tmp->ob_type;
2094         if (tmptype == &PyClass_Type)
2095             continue; /* Special case classic classes */
2096         if (PyType_IsSubtype(winner, tmptype))
2097             continue;
2098         if (PyType_IsSubtype(tmptype, winner)) {
2099             winner = tmptype;
2100             continue;
2101         }
2102         PyErr_SetString(PyExc_TypeError,
2103                         "metaclass conflict: "
2104                         "the metaclass of a derived class "
2105                         "must be a (non-strict) subclass "
2106                         "of the metaclasses of all its bases");
2107         return NULL;
2108     }
2109     if (winner != metatype) {
2110         if (winner->tp_new != type_new) /* Pass it to the winner */
2111             return winner->tp_new(winner, args, kwds);
2112         metatype = winner;
2113     }
2114 
2115     /* Adjust for empty tuple bases */
2116     if (nbases == 0) {
2117         bases = PyTuple_Pack(1, &PyBaseObject_Type);
2118         if (bases == NULL)
2119             return NULL;
2120         nbases = 1;
2121     }
2122     else
2123         Py_INCREF(bases);
2124 
2125     /* XXX From here until type is allocated, "return NULL" leaks bases! */
2126 
2127     /* Calculate best base, and check that all bases are type objects */
2128     base = best_base(bases);
2129     if (base == NULL) {
2130         Py_DECREF(bases);
2131         return NULL;
2132     }
2133     if (!PyType_HasFeature(base, Py_TPFLAGS_BASETYPE)) {
2134         PyErr_Format(PyExc_TypeError,
2135                      "type '%.100s' is not an acceptable base type",
2136                      base->tp_name);
2137         Py_DECREF(bases);
2138         return NULL;
2139     }
2140 
2141     /* Check for a __slots__ sequence variable in dict, and count it */
2142     slots = PyDict_GetItemString(dict, "__slots__");
2143     nslots = 0;
2144     add_dict = 0;
2145     add_weak = 0;
2146     may_add_dict = base->tp_dictoffset == 0;
2147     may_add_weak = base->tp_weaklistoffset == 0 && base->tp_itemsize == 0;
2148     if (slots == NULL) {
2149         if (may_add_dict) {
2150             add_dict++;
2151         }
2152         if (may_add_weak) {
2153             add_weak++;
2154         }
2155     }
2156     else {
2157         /* Have slots */
2158 
2159         /* Make it into a tuple */
2160         if (PyString_Check(slots) || PyUnicode_Check(slots))
2161             slots = PyTuple_Pack(1, slots);
2162         else
2163             slots = PySequence_Tuple(slots);
2164         if (slots == NULL) {
2165             Py_DECREF(bases);
2166             return NULL;
2167         }
2168         assert(PyTuple_Check(slots));
2169 
2170         /* Are slots allowed? */
2171         nslots = PyTuple_GET_SIZE(slots);
2172         if (nslots > 0 && base->tp_itemsize != 0) {
2173             PyErr_Format(PyExc_TypeError,
2174                          "nonempty __slots__ "
2175                          "not supported for subtype of '%s'",
2176                          base->tp_name);
2177           bad_slots:
2178             Py_DECREF(bases);
2179             Py_DECREF(slots);
2180             return NULL;
2181         }
2182 
2183 #ifdef Py_USING_UNICODE
2184         tmp = _unicode_to_string(slots, nslots);
2185         if (tmp == NULL)
2186             goto bad_slots;
2187         if (tmp != slots) {
2188             Py_DECREF(slots);
2189             slots = tmp;
2190         }
2191 #endif
2192         /* Check for valid slot names and two special cases */
2193         for (i = 0; i < nslots; i++) {
2194             PyObject *tmp = PyTuple_GET_ITEM(slots, i);
2195             char *s;
2196             if (!valid_identifier(tmp))
2197                 goto bad_slots;
2198             assert(PyString_Check(tmp));
2199             s = PyString_AS_STRING(tmp);
2200             if (strcmp(s, "__dict__") == 0) {
2201                 if (!may_add_dict || add_dict) {
2202                     PyErr_SetString(PyExc_TypeError,
2203                         "__dict__ slot disallowed: "
2204                         "we already got one");
2205                     goto bad_slots;
2206                 }
2207                 add_dict++;
2208             }
2209             if (strcmp(s, "__weakref__") == 0) {
2210                 if (!may_add_weak || add_weak) {
2211                     PyErr_SetString(PyExc_TypeError,
2212                         "__weakref__ slot disallowed: "
2213                         "either we already got one, "
2214                         "or __itemsize__ != 0");
2215                     goto bad_slots;
2216                 }
2217                 add_weak++;
2218             }
2219         }
2220 
2221         /* Copy slots into a list, mangle names and sort them.
2222            Sorted names are needed for __class__ assignment.
2223            Convert them back to tuple at the end.
2224         */
2225         newslots = PyList_New(nslots - add_dict - add_weak);
2226         if (newslots == NULL)
2227             goto bad_slots;
2228         for (i = j = 0; i < nslots; i++) {
2229             char *s;
2230             tmp = PyTuple_GET_ITEM(slots, i);
2231             s = PyString_AS_STRING(tmp);
2232             if ((add_dict && strcmp(s, "__dict__") == 0) ||
2233                 (add_weak && strcmp(s, "__weakref__") == 0))
2234                 continue;
2235             tmp =_Py_Mangle(name, tmp);
2236             if (!tmp) {
2237                 Py_DECREF(newslots);
2238                 goto bad_slots;
2239             }
2240             PyList_SET_ITEM(newslots, j, tmp);
2241             j++;
2242         }
2243         assert(j == nslots - add_dict - add_weak);
2244         nslots = j;
2245         Py_DECREF(slots);
2246         if (PyList_Sort(newslots) == -1) {
2247             Py_DECREF(bases);
2248             Py_DECREF(newslots);
2249             return NULL;
2250         }
2251         slots = PyList_AsTuple(newslots);
2252         Py_DECREF(newslots);
2253         if (slots == NULL) {
2254             Py_DECREF(bases);
2255             return NULL;
2256         }
2257 
2258         /* Secondary bases may provide weakrefs or dict */
2259         if (nbases > 1 &&
2260             ((may_add_dict && !add_dict) ||
2261              (may_add_weak && !add_weak))) {
2262             for (i = 0; i < nbases; i++) {
2263                 tmp = PyTuple_GET_ITEM(bases, i);
2264                 if (tmp == (PyObject *)base)
2265                     continue; /* Skip primary base */
2266                 if (PyClass_Check(tmp)) {
2267                     /* Classic base class provides both */
2268                     if (may_add_dict && !add_dict)
2269                         add_dict++;
2270                     if (may_add_weak && !add_weak)
2271                         add_weak++;
2272                     break;
2273                 }
2274                 assert(PyType_Check(tmp));
2275                 tmptype = (PyTypeObject *)tmp;
2276                 if (may_add_dict && !add_dict &&
2277                     tmptype->tp_dictoffset != 0)
2278                     add_dict++;
2279                 if (may_add_weak && !add_weak &&
2280                     tmptype->tp_weaklistoffset != 0)
2281                     add_weak++;
2282                 if (may_add_dict && !add_dict)
2283                     continue;
2284                 if (may_add_weak && !add_weak)
2285                     continue;
2286                 /* Nothing more to check */
2287                 break;
2288             }
2289         }
2290     }
2291 
2292     /* XXX From here until type is safely allocated,
2293        "return NULL" may leak slots! */
2294 
2295     /* Allocate the type object */
2296     type = (PyTypeObject *)metatype->tp_alloc(metatype, nslots);
2297     if (type == NULL) {
2298         Py_XDECREF(slots);
2299         Py_DECREF(bases);
2300         return NULL;
2301     }
2302 
2303     /* Keep name and slots alive in the extended type object */
2304     et = (PyHeapTypeObject *)type;
2305     Py_INCREF(name);
2306     et->ht_name = name;
2307     et->ht_slots = slots;
2308 
2309     /* Initialize tp_flags */
2310     type->tp_flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HEAPTYPE |
2311         Py_TPFLAGS_BASETYPE;
2312     if (base->tp_flags & Py_TPFLAGS_HAVE_GC)
2313         type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2314     if (base->tp_flags & Py_TPFLAGS_HAVE_NEWBUFFER)
2315         type->tp_flags |= Py_TPFLAGS_HAVE_NEWBUFFER;
2316 
2317     /* It's a new-style number unless it specifically inherits any
2318        old-style numeric behavior */
2319     if ((base->tp_flags & Py_TPFLAGS_CHECKTYPES) ||
2320         (base->tp_as_number == NULL))
2321         type->tp_flags |= Py_TPFLAGS_CHECKTYPES;
2322 
2323     /* Initialize essential fields */
2324     type->tp_as_number = &et->as_number;
2325     type->tp_as_sequence = &et->as_sequence;
2326     type->tp_as_mapping = &et->as_mapping;
2327     type->tp_as_buffer = &et->as_buffer;
2328     type->tp_name = PyString_AS_STRING(name);
2329 
2330     /* Set tp_base and tp_bases */
2331     type->tp_bases = bases;
2332     Py_INCREF(base);
2333     type->tp_base = base;
2334 
2335     /* Initialize tp_dict from passed-in dict */
2336     type->tp_dict = dict = PyDict_Copy(dict);
2337     if (dict == NULL) {
2338         Py_DECREF(type);
2339         return NULL;
2340     }
2341 
2342     /* Set __module__ in the dict */
2343     if (PyDict_GetItemString(dict, "__module__") == NULL) {
2344         tmp = PyEval_GetGlobals();
2345         if (tmp != NULL) {
2346             tmp = PyDict_GetItemString(tmp, "__name__");
2347             if (tmp != NULL) {
2348                 if (PyDict_SetItemString(dict, "__module__",
2349                                          tmp) < 0)
2350                     return NULL;
2351             }
2352         }
2353     }
2354 
2355     /* Set tp_doc to a copy of dict['__doc__'], if the latter is there
2356        and is a string.  The __doc__ accessor will first look for tp_doc;
2357        if that fails, it will still look into __dict__.
2358     */
2359     {
2360         PyObject *doc = PyDict_GetItemString(dict, "__doc__");
2361         if (doc != NULL && PyString_Check(doc)) {
2362             const size_t n = (size_t)PyString_GET_SIZE(doc);
2363             char *tp_doc = (char *)PyObject_MALLOC(n+1);
2364             if (tp_doc == NULL) {
2365                 Py_DECREF(type);
2366                 return NULL;
2367             }
2368             memcpy(tp_doc, PyString_AS_STRING(doc), n+1);
2369             type->tp_doc = tp_doc;
2370         }
2371     }
2372 
2373     /* Special-case __new__: if it's a plain function,
2374        make it a static function */
2375     tmp = PyDict_GetItemString(dict, "__new__");
2376     if (tmp != NULL && PyFunction_Check(tmp)) {
2377         tmp = PyStaticMethod_New(tmp);
2378         if (tmp == NULL) {
2379             Py_DECREF(type);
2380             return NULL;
2381         }
2382         PyDict_SetItemString(dict, "__new__", tmp);
2383         Py_DECREF(tmp);
2384     }
2385 
2386     /* Add descriptors for custom slots from __slots__, or for __dict__ */
2387     mp = PyHeapType_GET_MEMBERS(et);
2388     slotoffset = base->tp_basicsize;
2389     if (slots != NULL) {
2390         for (i = 0; i < nslots; i++, mp++) {
2391             mp->name = PyString_AS_STRING(
2392                 PyTuple_GET_ITEM(slots, i));
2393             mp->type = T_OBJECT_EX;
2394             mp->offset = slotoffset;
2395 
2396             /* __dict__ and __weakref__ are already filtered out */
2397             assert(strcmp(mp->name, "__dict__") != 0);
2398             assert(strcmp(mp->name, "__weakref__") != 0);
2399 
2400             slotoffset += sizeof(PyObject *);
2401         }
2402     }
2403     if (add_dict) {
2404         if (base->tp_itemsize)
2405             type->tp_dictoffset = -(long)sizeof(PyObject *);
2406         else
2407             type->tp_dictoffset = slotoffset;
2408         slotoffset += sizeof(PyObject *);
2409     }
2410     if (add_weak) {
2411         assert(!base->tp_itemsize);
2412         type->tp_weaklistoffset = slotoffset;
2413         slotoffset += sizeof(PyObject *);
2414     }
2415     type->tp_basicsize = slotoffset;
2416     type->tp_itemsize = base->tp_itemsize;
2417     type->tp_members = PyHeapType_GET_MEMBERS(et);
2418 
2419     if (type->tp_weaklistoffset && type->tp_dictoffset)
2420         type->tp_getset = subtype_getsets_full;
2421     else if (type->tp_weaklistoffset && !type->tp_dictoffset)
2422         type->tp_getset = subtype_getsets_weakref_only;
2423     else if (!type->tp_weaklistoffset && type->tp_dictoffset)
2424         type->tp_getset = subtype_getsets_dict_only;
2425     else
2426         type->tp_getset = NULL;
2427 
2428     /* Special case some slots */
2429     if (type->tp_dictoffset != 0 || nslots > 0) {
2430         if (base->tp_getattr == NULL && base->tp_getattro == NULL)
2431             type->tp_getattro = PyObject_GenericGetAttr;
2432         if (base->tp_setattr == NULL && base->tp_setattro == NULL)
2433             type->tp_setattro = PyObject_GenericSetAttr;
2434     }
2435     type->tp_dealloc = subtype_dealloc;
2436 
2437     /* Enable GC unless there are really no instance variables possible */
2438     if (!(type->tp_basicsize == sizeof(PyObject) &&
2439           type->tp_itemsize == 0))
2440         type->tp_flags |= Py_TPFLAGS_HAVE_GC;
2441 
2442     /* Always override allocation strategy to use regular heap */
2443     type->tp_alloc = PyType_GenericAlloc;
2444     if (type->tp_flags & Py_TPFLAGS_HAVE_GC) {
2445         type->tp_free = PyObject_GC_Del;
2446         type->tp_traverse = subtype_traverse;
2447         type->tp_clear = subtype_clear;
2448     }
2449     else
2450         type->tp_free = PyObject_Del;
2451 
2452     /* Initialize the rest */
2453     if (PyType_Ready(type) < 0) {
2454         Py_DECREF(type);
2455         return NULL;
2456     }
2457 
2458     /* Put the proper slots in place */
2459     fixup_slot_dispatchers(type);
2460 
2461     return (PyObject *)type;
2462 }
2463 
2464 /* Internal API to look for a name through the MRO.
2465    This returns a borrowed reference, and doesn't set an exception! */
2466 PyObject *
2467 _PyType_Lookup(PyTypeObject *type, PyObject *name)
2468 {
2469     Py_ssize_t i, n;
2470     PyObject *mro, *res, *base, *dict;
2471     unsigned int h;
2472 
2473     if (MCACHE_CACHEABLE_NAME(name) &&
2474         PyType_HasFeature(type, Py_TPFLAGS_VALID_VERSION_TAG)) {
2475         /* fast path */
2476         h = MCACHE_HASH_METHOD(type, name);
2477         if (method_cache[h].version == type->tp_version_tag &&
2478             method_cache[h].name == name)
2479             return method_cache[h].value;
2480     }
2481 
2482     /* Look in tp_dict of types in MRO */
2483     mro = type->tp_mro;
2484 
2485     /* If mro is NULL, the type is either not yet initialized
2486        by PyType_Ready(), or already cleared by type_clear().
2487        Either way the safest thing to do is to return NULL. */
2488     if (mro == NULL)
2489         return NULL;
2490 
2491     res = NULL;
2492     assert(PyTuple_Check(mro));
2493     n = PyTuple_GET_SIZE(mro);
2494     for (i = 0; i < n; i++) {
2495         base = PyTuple_GET_ITEM(mro, i);
2496         if (PyClass_Check(base))
2497             dict = ((PyClassObject *)base)->cl_dict;
2498         else {
2499             assert(PyType_Check(base));
2500             dict = ((PyTypeObject *)base)->tp_dict;
2501         }
2502         assert(dict && PyDict_Check(dict));
2503         res = PyDict_GetItem(dict, name);
2504         if (res != NULL)
2505             break;
2506     }
2507 
2508     if (MCACHE_CACHEABLE_NAME(name) && assign_version_tag(type)) {
2509         h = MCACHE_HASH_METHOD(type, name);
2510         method_cache[h].version = type->tp_version_tag;
2511         method_cache[h].value = res;  /* borrowed */
2512         Py_INCREF(name);
2513         Py_DECREF(method_cache[h].name);
2514         method_cache[h].name = name;
2515     }
2516     return res;
2517 }
2518 
2519 /* This is similar to PyObject_GenericGetAttr(),
2520    but uses _PyType_Lookup() instead of just looking in type->tp_dict. */
2521 static PyObject *
2522 type_getattro(PyTypeObject *type, PyObject *name)
2523 {
2524     PyTypeObject *metatype = Py_TYPE(type);
2525     PyObject *meta_attribute, *attribute;
2526     descrgetfunc meta_get;
2527 
2528     /* Initialize this type (we'll assume the metatype is initialized) */
2529     if (type->tp_dict == NULL) {
2530         if (PyType_Ready(type) < 0)
2531             return NULL;
2532     }
2533 
2534     /* No readable descriptor found yet */
2535     meta_get = NULL;
2536 
2537     /* Look for the attribute in the metatype */
2538     meta_attribute = _PyType_Lookup(metatype, name);
2539 
2540     if (meta_attribute != NULL) {
2541         meta_get = Py_TYPE(meta_attribute)->tp_descr_get;
2542 
2543         if (meta_get != NULL && PyDescr_IsData(meta_attribute)) {
2544             /* Data descriptors implement tp_descr_set to intercept
2545              * writes. Assume the attribute is not overridden in
2546              * type's tp_dict (and bases): call the descriptor now.
2547              */
2548             return meta_get(meta_attribute, (PyObject *)type,
2549                             (PyObject *)metatype);
2550         }
2551         Py_INCREF(meta_attribute);
2552     }
2553 
2554     /* No data descriptor found on metatype. Look in tp_dict of this
2555      * type and its bases */
2556     attribute = _PyType_Lookup(type, name);
2557     if (attribute != NULL) {
2558         /* Implement descriptor functionality, if any */
2559         descrgetfunc local_get = Py_TYPE(attribute)->tp_descr_get;
2560 
2561         Py_XDECREF(meta_attribute);
2562 
2563         if (local_get != NULL) {
2564             /* NULL 2nd argument indicates the descriptor was
2565              * found on the target object itself (or a base)  */
2566             return local_get(attribute, (PyObject *)NULL,
2567                              (PyObject *)type);
2568         }
2569 
2570         Py_INCREF(attribute);
2571         return attribute;
2572     }
2573 
2574     /* No attribute found in local __dict__ (or bases): use the
2575      * descriptor from the metatype, if any */
2576     if (meta_get != NULL) {
2577         PyObject *res;
2578         res = meta_get(meta_attribute, (PyObject *)type,
2579                        (PyObject *)metatype);
2580         Py_DECREF(meta_attribute);
2581         return res;
2582     }
2583 
2584     /* If an ordinary attribute was found on the metatype, return it now */
2585     if (meta_attribute != NULL) {
2586         return meta_attribute;
2587     }
2588 
2589     /* Give up */
2590     PyErr_Format(PyExc_AttributeError,
2591                      "type object '%.50s' has no attribute '%.400s'",
2592                      type->tp_name, PyString_AS_STRING(name));
2593     return NULL;
2594 }
2595 
2596 static int
2597 type_setattro(PyTypeObject *type, PyObject *name, PyObject *value)
2598 {
2599     if (!(type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
2600         PyErr_Format(
2601             PyExc_TypeError,
2602             "can't set attributes of built-in/extension type '%s'",
2603             type->tp_name);
2604         return -1;
2605     }
2606     if (PyObject_GenericSetAttr((PyObject *)type, name, value) < 0)
2607         return -1;
2608     return update_slot(type, name);
2609 }
2610 
2611 static void
2612 type_dealloc(PyTypeObject *type)
2613 {
2614     PyHeapTypeObject *et;
2615 
2616     /* Assert this is a heap-allocated type object */
2617     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2618     _PyObject_GC_UNTRACK(type);
2619     PyObject_ClearWeakRefs((PyObject *)type);
2620     et = (PyHeapTypeObject *)type;
2621     Py_XDECREF(type->tp_base);
2622     Py_XDECREF(type->tp_dict);
2623     Py_XDECREF(type->tp_bases);
2624     Py_XDECREF(type->tp_mro);
2625     Py_XDECREF(type->tp_cache);
2626     Py_XDECREF(type->tp_subclasses);
2627     /* A type's tp_doc is heap allocated, unlike the tp_doc slots
2628      * of most other objects.  It's okay to cast it to char *.
2629      */
2630     PyObject_Free((char *)type->tp_doc);
2631     Py_XDECREF(et->ht_name);
2632     Py_XDECREF(et->ht_slots);
2633     Py_TYPE(type)->tp_free((PyObject *)type);
2634 }
2635 
2636 static PyObject *
2637 type_subclasses(PyTypeObject *type, PyObject *args_ignored)
2638 {
2639     PyObject *list, *raw, *ref;
2640     Py_ssize_t i, n;
2641 
2642     list = PyList_New(0);
2643     if (list == NULL)
2644         return NULL;
2645     raw = type->tp_subclasses;
2646     if (raw == NULL)
2647         return list;
2648     assert(PyList_Check(raw));
2649     n = PyList_GET_SIZE(raw);
2650     for (i = 0; i < n; i++) {
2651         ref = PyList_GET_ITEM(raw, i);
2652         assert(PyWeakref_CheckRef(ref));
2653         ref = PyWeakref_GET_OBJECT(ref);
2654         if (ref != Py_None) {
2655             if (PyList_Append(list, ref) < 0) {
2656                 Py_DECREF(list);
2657                 return NULL;
2658             }
2659         }
2660     }
2661     return list;
2662 }
2663 
2664 static PyMethodDef type_methods[] = {
2665     {"mro", (PyCFunction)mro_external, METH_NOARGS,
2666      PyDoc_STR("mro() -> list\nreturn a type's method resolution order")},
2667     {"__subclasses__", (PyCFunction)type_subclasses, METH_NOARGS,
2668      PyDoc_STR("__subclasses__() -> list of immediate subclasses")},
2669     {"__instancecheck__", type___instancecheck__, METH_O,
2670      PyDoc_STR("__instancecheck__() -> bool\ncheck if an object is an instance")},
2671     {"__subclasscheck__", type___subclasscheck__, METH_O,
2672      PyDoc_STR("__subclasscheck__() -> bool\ncheck if a class is a subclass")},
2673     {0}
2674 };
2675 
2676 PyDoc_STRVAR(type_doc,
2677 "type(object) -> the object's type\n"
2678 "type(name, bases, dict) -> a new type");
2679 
2680 static int
2681 type_traverse(PyTypeObject *type, visitproc visit, void *arg)
2682 {
2683     /* Because of type_is_gc(), the collector only calls this
2684        for heaptypes. */
2685     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2686 
2687     Py_VISIT(type->tp_dict);
2688     Py_VISIT(type->tp_cache);
2689     Py_VISIT(type->tp_mro);
2690     Py_VISIT(type->tp_bases);
2691     Py_VISIT(type->tp_base);
2692 
2693     /* There's no need to visit type->tp_subclasses or
2694        ((PyHeapTypeObject *)type)->ht_slots, because they can't be involved
2695        in cycles; tp_subclasses is a list of weak references,
2696        and slots is a tuple of strings. */
2697 
2698     return 0;
2699 }
2700 
2701 static int
2702 type_clear(PyTypeObject *type)
2703 {
2704     /* Because of type_is_gc(), the collector only calls this
2705        for heaptypes. */
2706     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
2707 
2708     /* We need to invalidate the method cache carefully before clearing
2709        the dict, so that other objects caught in a reference cycle
2710        don't start calling destroyed methods.
2711 
2712        Otherwise, the only field we need to clear is tp_mro, which is
2713        part of a hard cycle (its first element is the class itself) that
2714        won't be broken otherwise (it's a tuple and tuples don't have a
2715        tp_clear handler).  None of the other fields need to be
2716        cleared, and here's why:
2717 
2718        tp_cache:
2719            Not used; if it were, it would be a dict.
2720 
2721        tp_bases, tp_base:
2722            If these are involved in a cycle, there must be at least
2723            one other, mutable object in the cycle, e.g. a base
2724            class's dict; the cycle will be broken that way.
2725 
2726        tp_subclasses:
2727            A list of weak references can't be part of a cycle; and
2728            lists have their own tp_clear.
2729 
2730        slots (in PyHeapTypeObject):
2731            A tuple of strings can't be part of a cycle.
2732     */
2733 
2734     PyType_Modified(type);
2735     if (type->tp_dict)
2736         PyDict_Clear(type->tp_dict);
2737     Py_CLEAR(type->tp_mro);
2738 
2739     return 0;
2740 }
2741 
2742 static int
2743 type_is_gc(PyTypeObject *type)
2744 {
2745     return type->tp_flags & Py_TPFLAGS_HEAPTYPE;
2746 }
2747 
2748 PyTypeObject PyType_Type = {
2749     PyVarObject_HEAD_INIT(&PyType_Type, 0)
2750     "type",                                     /* tp_name */
2751     sizeof(PyHeapTypeObject),                   /* tp_basicsize */
2752     sizeof(PyMemberDef),                        /* tp_itemsize */
2753     (destructor)type_dealloc,                   /* tp_dealloc */
2754     0,                                          /* tp_print */
2755     0,                                          /* tp_getattr */
2756     0,                                          /* tp_setattr */
2757     0,                                  /* tp_compare */
2758     (reprfunc)type_repr,                        /* tp_repr */
2759     0,                                          /* tp_as_number */
2760     0,                                          /* tp_as_sequence */
2761     0,                                          /* tp_as_mapping */
2762     (hashfunc)_Py_HashPointer,                  /* tp_hash */
2763     (ternaryfunc)type_call,                     /* tp_call */
2764     0,                                          /* tp_str */
2765     (getattrofunc)type_getattro,                /* tp_getattro */
2766     (setattrofunc)type_setattro,                /* tp_setattro */
2767     0,                                          /* tp_as_buffer */
2768     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC |
2769         Py_TPFLAGS_BASETYPE | Py_TPFLAGS_TYPE_SUBCLASS,         /* tp_flags */
2770     type_doc,                                   /* tp_doc */
2771     (traverseproc)type_traverse,                /* tp_traverse */
2772     (inquiry)type_clear,                        /* tp_clear */
2773     type_richcompare,                                           /* tp_richcompare */
2774     offsetof(PyTypeObject, tp_weaklist),        /* tp_weaklistoffset */
2775     0,                                          /* tp_iter */
2776     0,                                          /* tp_iternext */
2777     type_methods,                               /* tp_methods */
2778     type_members,                               /* tp_members */
2779     type_getsets,                               /* tp_getset */
2780     0,                                          /* tp_base */
2781     0,                                          /* tp_dict */
2782     0,                                          /* tp_descr_get */
2783     0,                                          /* tp_descr_set */
2784     offsetof(PyTypeObject, tp_dict),            /* tp_dictoffset */
2785     type_init,                                  /* tp_init */
2786     0,                                          /* tp_alloc */
2787     type_new,                                   /* tp_new */
2788     PyObject_GC_Del,                            /* tp_free */
2789     (inquiry)type_is_gc,                        /* tp_is_gc */
2790 };
2791 
2792 
2793 /* The base type of all types (eventually)... except itself. */
2794 
2795 /* You may wonder why object.__new__() only complains about arguments
2796    when object.__init__() is not overridden, and vice versa.
2797 
2798    Consider the use cases:
2799 
2800    1. When neither is overridden, we want to hear complaints about
2801       excess (i.e., any) arguments, since their presence could
2802       indicate there's a bug.
2803 
2804    2. When defining an Immutable type, we are likely to override only
2805       __new__(), since __init__() is called too late to initialize an
2806       Immutable object.  Since __new__() defines the signature for the
2807       type, it would be a pain to have to override __init__() just to
2808       stop it from complaining about excess arguments.
2809 
2810    3. When defining a Mutable type, we are likely to override only
2811       __init__().  So here the converse reasoning applies: we don't
2812       want to have to override __new__() just to stop it from
2813       complaining.
2814 
2815    4. When __init__() is overridden, and the subclass __init__() calls
2816       object.__init__(), the latter should complain about excess
2817       arguments; ditto for __new__().
2818 
2819    Use cases 2 and 3 make it unattractive to unconditionally check for
2820    excess arguments.  The best solution that addresses all four use
2821    cases is as follows: __init__() complains about excess arguments
2822    unless __new__() is overridden and __init__() is not overridden
2823    (IOW, if __init__() is overridden or __new__() is not overridden);
2824    symmetrically, __new__() complains about excess arguments unless
2825    __init__() is overridden and __new__() is not overridden
2826    (IOW, if __new__() is overridden or __init__() is not overridden).
2827 
2828    However, for backwards compatibility, this breaks too much code.
2829    Therefore, in 2.6, we'll *warn* about excess arguments when both
2830    methods are overridden; for all other cases we'll use the above
2831    rules.
2832 
2833 */
2834 
2835 /* Forward */
2836 static PyObject *
2837 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds);
2838 
2839 static int
2840 excess_args(PyObject *args, PyObject *kwds)
2841 {
2842     return PyTuple_GET_SIZE(args) ||
2843         (kwds && PyDict_Check(kwds) && PyDict_Size(kwds));
2844 }
2845 
2846 static int
2847 object_init(PyObject *self, PyObject *args, PyObject *kwds)
2848 {
2849     int err = 0;
2850     if (excess_args(args, kwds)) {
2851         PyTypeObject *type = Py_TYPE(self);
2852         if (type->tp_init != object_init &&
2853             type->tp_new != object_new)
2854         {
2855             err = PyErr_WarnEx(PyExc_DeprecationWarning,
2856                        "object.__init__() takes no parameters",
2857                        1);
2858         }
2859         else if (type->tp_init != object_init ||
2860                  type->tp_new == object_new)
2861         {
2862             PyErr_SetString(PyExc_TypeError,
2863                 "object.__init__() takes no parameters");
2864             err = -1;
2865         }
2866     }
2867     return err;
2868 }
2869 
2870 static PyObject *
2871 object_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
2872 {
2873     int err = 0;
2874     if (excess_args(args, kwds)) {
2875         if (type->tp_new != object_new &&
2876             type->tp_init != object_init)
2877         {
2878             err = PyErr_WarnEx(PyExc_DeprecationWarning,
2879                        "object.__new__() takes no parameters",
2880                        1);
2881         }
2882         else if (type->tp_new != object_new ||
2883                  type->tp_init == object_init)
2884         {
2885             PyErr_SetString(PyExc_TypeError,
2886                 "object.__new__() takes no parameters");
2887             err = -1;
2888         }
2889     }
2890     if (err < 0)
2891         return NULL;
2892 
2893     if (type->tp_flags & Py_TPFLAGS_IS_ABSTRACT) {
2894         static PyObject *comma = NULL;
2895         PyObject *abstract_methods = NULL;
2896         PyObject *builtins;
2897         PyObject *sorted;
2898         PyObject *sorted_methods = NULL;
2899         PyObject *joined = NULL;
2900         const char *joined_str;
2901 
2902         /* Compute ", ".join(sorted(type.__abstractmethods__))
2903            into joined. */
2904         abstract_methods = type_abstractmethods(type, NULL);
2905         if (abstract_methods == NULL)
2906             goto error;
2907         builtins = PyEval_GetBuiltins();
2908         if (builtins == NULL)
2909             goto error;
2910         sorted = PyDict_GetItemString(builtins, "sorted");
2911         if (sorted == NULL)
2912             goto error;
2913         sorted_methods = PyObject_CallFunctionObjArgs(sorted,
2914                                                       abstract_methods,
2915                                                       NULL);
2916         if (sorted_methods == NULL)
2917             goto error;
2918         if (comma == NULL) {
2919             comma = PyString_InternFromString(", ");
2920             if (comma == NULL)
2921                 goto error;
2922         }
2923         joined = PyObject_CallMethod(comma, "join",
2924                                      "O",  sorted_methods);
2925         if (joined == NULL)
2926             goto error;
2927         joined_str = PyString_AsString(joined);
2928         if (joined_str == NULL)
2929             goto error;
2930 
2931         PyErr_Format(PyExc_TypeError,
2932                      "Can't instantiate abstract class %s "
2933                      "with abstract methods %s",
2934                      type->tp_name,
2935                      joined_str);
2936     error:
2937         Py_XDECREF(joined);
2938         Py_XDECREF(sorted_methods);
2939         Py_XDECREF(abstract_methods);
2940         return NULL;
2941     }
2942     return type->tp_alloc(type, 0);
2943 }
2944 
2945 static void
2946 object_dealloc(PyObject *self)
2947 {
2948     Py_TYPE(self)->tp_free(self);
2949 }
2950 
2951 static PyObject *
2952 object_repr(PyObject *self)
2953 {
2954     PyTypeObject *type;
2955     PyObject *mod, *name, *rtn;
2956 
2957     type = Py_TYPE(self);
2958     mod = type_module(type, NULL);
2959     if (mod == NULL)
2960         PyErr_Clear();
2961     else if (!PyString_Check(mod)) {
2962         Py_DECREF(mod);
2963         mod = NULL;
2964     }
2965     name = type_name(type, NULL);
2966     if (name == NULL)
2967         return NULL;
2968     if (mod != NULL && strcmp(PyString_AS_STRING(mod), "__builtin__"))
2969         rtn = PyString_FromFormat("<%s.%s object at %p>",
2970                                   PyString_AS_STRING(mod),
2971                                   PyString_AS_STRING(name),
2972                                   self);
2973     else
2974         rtn = PyString_FromFormat("<%s object at %p>",
2975                                   type->tp_name, self);
2976     Py_XDECREF(mod);
2977     Py_DECREF(name);
2978     return rtn;
2979 }
2980 
2981 static PyObject *
2982 object_str(PyObject *self)
2983 {
2984     unaryfunc f;
2985 
2986     f = Py_TYPE(self)->tp_repr;
2987     if (f == NULL || f == object_str)
2988         f = object_repr;
2989     return f(self);
2990 }
2991 
2992 static PyObject *
2993 object_get_class(PyObject *self, void *closure)
2994 {
2995     Py_INCREF(Py_TYPE(self));
2996     return (PyObject *)(Py_TYPE(self));
2997 }
2998 
2999 static int
3000 equiv_structs(PyTypeObject *a, PyTypeObject *b)
3001 {
3002     return a == b ||
3003            (a != NULL &&
3004         b != NULL &&
3005         a->tp_basicsize == b->tp_basicsize &&
3006         a->tp_itemsize == b->tp_itemsize &&
3007         a->tp_dictoffset == b->tp_dictoffset &&
3008         a->tp_weaklistoffset == b->tp_weaklistoffset &&
3009         ((a->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3010          (b->tp_flags & Py_TPFLAGS_HAVE_GC)));
3011 }
3012 
3013 static int
3014 same_slots_added(PyTypeObject *a, PyTypeObject *b)
3015 {
3016     PyTypeObject *base = a->tp_base;
3017     Py_ssize_t size;
3018     PyObject *slots_a, *slots_b;
3019 
3020     assert(base == b->tp_base);
3021     size = base->tp_basicsize;
3022     if (a->tp_dictoffset == size && b->tp_dictoffset == size)
3023         size += sizeof(PyObject *);
3024     if (a->tp_weaklistoffset == size && b->tp_weaklistoffset == size)
3025         size += sizeof(PyObject *);
3026 
3027     /* Check slots compliance */
3028     slots_a = ((PyHeapTypeObject *)a)->ht_slots;
3029     slots_b = ((PyHeapTypeObject *)b)->ht_slots;
3030     if (slots_a && slots_b) {
3031         if (PyObject_Compare(slots_a, slots_b) != 0)
3032             return 0;
3033         size += sizeof(PyObject *) * PyTuple_GET_SIZE(slots_a);
3034     }
3035     return size == a->tp_basicsize && size == b->tp_basicsize;
3036 }
3037 
3038 static int
3039 compatible_for_assignment(PyTypeObject* oldto, PyTypeObject* newto, char* attr)
3040 {
3041     PyTypeObject *newbase, *oldbase;
3042 
3043     if (newto->tp_dealloc != oldto->tp_dealloc ||
3044         newto->tp_free != oldto->tp_free)
3045     {
3046         PyErr_Format(PyExc_TypeError,
3047                      "%s assignment: "
3048                      "'%s' deallocator differs from '%s'",
3049                      attr,
3050                      newto->tp_name,
3051                      oldto->tp_name);
3052         return 0;
3053     }
3054     newbase = newto;
3055     oldbase = oldto;
3056     while (equiv_structs(newbase, newbase->tp_base))
3057         newbase = newbase->tp_base;
3058     while (equiv_structs(oldbase, oldbase->tp_base))
3059         oldbase = oldbase->tp_base;
3060     if (newbase != oldbase &&
3061         (newbase->tp_base != oldbase->tp_base ||
3062          !same_slots_added(newbase, oldbase))) {
3063         PyErr_Format(PyExc_TypeError,
3064                      "%s assignment: "
3065                      "'%s' object layout differs from '%s'",
3066                      attr,
3067                      newto->tp_name,
3068                      oldto->tp_name);
3069         return 0;
3070     }
3071 
3072     return 1;
3073 }
3074 
3075 static int
3076 object_set_class(PyObject *self, PyObject *value, void *closure)
3077 {
3078     PyTypeObject *oldto = Py_TYPE(self);
3079     PyTypeObject *newto;
3080 
3081     if (value == NULL) {
3082         PyErr_SetString(PyExc_TypeError,
3083                         "can't delete __class__ attribute");
3084         return -1;
3085     }
3086     if (!PyType_Check(value)) {
3087         PyErr_Format(PyExc_TypeError,
3088           "__class__ must be set to new-style class, not '%s' object",
3089           Py_TYPE(value)->tp_name);
3090         return -1;
3091     }
3092     newto = (PyTypeObject *)value;
3093     if (!(newto->tp_flags & Py_TPFLAGS_HEAPTYPE) ||
3094         !(oldto->tp_flags & Py_TPFLAGS_HEAPTYPE))
3095     {
3096         PyErr_Format(PyExc_TypeError,
3097                      "__class__ assignment: only for heap types");
3098         return -1;
3099     }
3100     if (compatible_for_assignment(newto, oldto, "__class__")) {
3101         Py_INCREF(newto);
3102         Py_TYPE(self) = newto;
3103         Py_DECREF(oldto);
3104         return 0;
3105     }
3106     else {
3107         return -1;
3108     }
3109 }
3110 
3111 static PyGetSetDef object_getsets[] = {
3112     {"__class__", object_get_class, object_set_class,
3113      PyDoc_STR("the object's class")},
3114     {0}
3115 };
3116 
3117 
3118 /* Stuff to implement __reduce_ex__ for pickle protocols >= 2.
3119    We fall back to helpers in copy_reg for:
3120    - pickle protocols < 2
3121    - calculating the list of slot names (done only once per class)
3122    - the __newobj__ function (which is used as a token but never called)
3123 */
3124 
3125 static PyObject *
3126 import_copyreg(void)
3127 {
3128     static PyObject *copyreg_str;
3129 
3130     if (!copyreg_str) {
3131         copyreg_str = PyString_InternFromString("copy_reg");
3132         if (copyreg_str == NULL)
3133             return NULL;
3134     }
3135 
3136     return PyImport_Import(copyreg_str);
3137 }
3138 
3139 static PyObject *
3140 slotnames(PyObject *cls)
3141 {
3142     PyObject *clsdict;
3143     PyObject *copyreg;
3144     PyObject *slotnames;
3145 
3146     if (!PyType_Check(cls)) {
3147         Py_INCREF(Py_None);
3148         return Py_None;
3149     }
3150 
3151     clsdict = ((PyTypeObject *)cls)->tp_dict;
3152     slotnames = PyDict_GetItemString(clsdict, "__slotnames__");
3153     if (slotnames != NULL && PyList_Check(slotnames)) {
3154         Py_INCREF(slotnames);
3155         return slotnames;
3156     }
3157 
3158     copyreg = import_copyreg();
3159     if (copyreg == NULL)
3160         return NULL;
3161 
3162     slotnames = PyObject_CallMethod(copyreg, "_slotnames", "O", cls);
3163     Py_DECREF(copyreg);
3164     if (slotnames != NULL &&
3165         slotnames != Py_None &&
3166         !PyList_Check(slotnames))
3167     {
3168         PyErr_SetString(PyExc_TypeError,
3169             "copy_reg._slotnames didn't return a list or None");
3170         Py_DECREF(slotnames);
3171         slotnames = NULL;
3172     }
3173 
3174     return slotnames;
3175 }
3176 
3177 static PyObject *
3178 reduce_2(PyObject *obj)
3179 {
3180     PyObject *cls, *getnewargs;
3181     PyObject *args = NULL, *args2 = NULL;
3182     PyObject *getstate = NULL, *state = NULL, *names = NULL;
3183     PyObject *slots = NULL, *listitems = NULL, *dictitems = NULL;
3184     PyObject *copyreg = NULL, *newobj = NULL, *res = NULL;
3185     Py_ssize_t i, n;
3186 
3187     cls = PyObject_GetAttrString(obj, "__class__");
3188     if (cls == NULL)
3189         return NULL;
3190 
3191     getnewargs = PyObject_GetAttrString(obj, "__getnewargs__");
3192     if (getnewargs != NULL) {
3193         args = PyObject_CallObject(getnewargs, NULL);
3194         Py_DECREF(getnewargs);
3195         if (args != NULL && !PyTuple_Check(args)) {
3196             PyErr_Format(PyExc_TypeError,
3197                 "__getnewargs__ should return a tuple, "
3198                 "not '%.200s'", Py_TYPE(args)->tp_name);
3199             goto end;
3200         }
3201     }
3202     else {
3203         PyErr_Clear();
3204         args = PyTuple_New(0);
3205     }
3206     if (args == NULL)
3207         goto end;
3208 
3209     getstate = PyObject_GetAttrString(obj, "__getstate__");
3210     if (getstate != NULL) {
3211         state = PyObject_CallObject(getstate, NULL);
3212         Py_DECREF(getstate);
3213         if (state == NULL)
3214             goto end;
3215     }
3216     else {
3217         PyErr_Clear();
3218         state = PyObject_GetAttrString(obj, "__dict__");
3219         if (state == NULL) {
3220             PyErr_Clear();
3221             state = Py_None;
3222             Py_INCREF(state);
3223         }
3224         names = slotnames(cls);
3225         if (names == NULL)
3226             goto end;
3227         if (names != Py_None) {
3228             assert(PyList_Check(names));
3229             slots = PyDict_New();
3230             if (slots == NULL)
3231                 goto end;
3232             n = 0;
3233             /* Can't pre-compute the list size; the list
3234                is stored on the class so accessible to other
3235                threads, which may be run by DECREF */
3236             for (i = 0; i < PyList_GET_SIZE(names); i++) {
3237                 PyObject *name, *value;
3238                 name = PyList_GET_ITEM(names, i);
3239                 value = PyObject_GetAttr(obj, name);
3240                 if (value == NULL)
3241                     PyErr_Clear();
3242                 else {
3243                     int err = PyDict_SetItem(slots, name,
3244                                              value);
3245                     Py_DECREF(value);
3246                     if (err)
3247                         goto end;
3248                     n++;
3249                 }
3250             }
3251             if (n) {
3252                 state = Py_BuildValue("(NO)", state, slots);
3253                 if (state == NULL)
3254                     goto end;
3255             }
3256         }
3257     }
3258 
3259     if (!PyList_Check(obj)) {
3260         listitems = Py_None;
3261         Py_INCREF(listitems);
3262     }
3263     else {
3264         listitems = PyObject_GetIter(obj);
3265         if (listitems == NULL)
3266             goto end;
3267     }
3268 
3269     if (!PyDict_Check(obj)) {
3270         dictitems = Py_None;
3271         Py_INCREF(dictitems);
3272     }
3273     else {
3274         dictitems = PyObject_CallMethod(obj, "iteritems", "");
3275         if (dictitems == NULL)
3276             goto end;
3277     }
3278 
3279     copyreg = import_copyreg();
3280     if (copyreg == NULL)
3281         goto end;
3282     newobj = PyObject_GetAttrString(copyreg, "__newobj__");
3283     if (newobj == NULL)
3284         goto end;
3285 
3286     n = PyTuple_GET_SIZE(args);
3287     args2 = PyTuple_New(n+1);
3288     if (args2 == NULL)
3289         goto end;
3290     PyTuple_SET_ITEM(args2, 0, cls);
3291     cls = NULL;
3292     for (i = 0; i < n; i++) {
3293         PyObject *v = PyTuple_GET_ITEM(args, i);
3294         Py_INCREF(v);
3295         PyTuple_SET_ITEM(args2, i+1, v);
3296     }
3297 
3298     res = PyTuple_Pack(5, newobj, args2, state, listitems, dictitems);
3299 
3300   end:
3301     Py_XDECREF(cls);
3302     Py_XDECREF(args);
3303     Py_XDECREF(args2);
3304     Py_XDECREF(slots);
3305     Py_XDECREF(state);
3306     Py_XDECREF(names);
3307     Py_XDECREF(listitems);
3308     Py_XDECREF(dictitems);
3309     Py_XDECREF(copyreg);
3310     Py_XDECREF(newobj);
3311     return res;
3312 }
3313 
3314 /*
3315  * There were two problems when object.__reduce__ and object.__reduce_ex__
3316  * were implemented in the same function:
3317  *  - trying to pickle an object with a custom __reduce__ method that
3318  *    fell back to object.__reduce__ in certain circumstances led to
3319  *    infinite recursion at Python level and eventual RuntimeError.
3320  *  - Pickling objects that lied about their type by overwriting the
3321  *    __class__ descriptor could lead to infinite recursion at C level
3322  *    and eventual segfault.
3323  *
3324  * Because of backwards compatibility, the two methods still have to
3325  * behave in the same way, even if this is not required by the pickle
3326  * protocol. This common functionality was moved to the _common_reduce
3327  * function.
3328  */
3329 static PyObject *
3330 _common_reduce(PyObject *self, int proto)
3331 {
3332     PyObject *copyreg, *res;
3333 
3334     if (proto >= 2)
3335         return reduce_2(self);
3336 
3337     copyreg = import_copyreg();
3338     if (!copyreg)
3339         return NULL;
3340 
3341     res = PyEval_CallMethod(copyreg, "_reduce_ex", "(Oi)", self, proto);
3342     Py_DECREF(copyreg);
3343 
3344     return res;
3345 }
3346 
3347 static PyObject *
3348 object_reduce(PyObject *self, PyObject *args)
3349 {
3350     int proto = 0;
3351 
3352     if (!PyArg_ParseTuple(args, "|i:__reduce__", &proto))
3353         return NULL;
3354 
3355     return _common_reduce(self, proto);
3356 }
3357 
3358 static PyObject *
3359 object_reduce_ex(PyObject *self, PyObject *args)
3360 {
3361     PyObject *reduce, *res;
3362     int proto = 0;
3363 
3364     if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto))
3365         return NULL;
3366 
3367     reduce = PyObject_GetAttrString(self, "__reduce__");
3368     if (reduce == NULL)
3369         PyErr_Clear();
3370     else {
3371         PyObject *cls, *clsreduce, *objreduce;
3372         int override;
3373         cls = PyObject_GetAttrString(self, "__class__");
3374         if (cls == NULL) {
3375             Py_DECREF(reduce);
3376             return NULL;
3377         }
3378         clsreduce = PyObject_GetAttrString(cls, "__reduce__");
3379         Py_DECREF(cls);
3380         if (clsreduce == NULL) {
3381             Py_DECREF(reduce);
3382             return NULL;
3383         }
3384         objreduce = PyDict_GetItemString(PyBaseObject_Type.tp_dict,
3385                                          "__reduce__");
3386         override = (clsreduce != objreduce);
3387         Py_DECREF(clsreduce);
3388         if (override) {
3389             res = PyObject_CallObject(reduce, NULL);
3390             Py_DECREF(reduce);
3391             return res;
3392         }
3393         else
3394             Py_DECREF(reduce);
3395     }
3396 
3397     return _common_reduce(self, proto);
3398 }
3399 
3400 static PyObject *
3401 object_subclasshook(PyObject *cls, PyObject *args)
3402 {
3403     Py_INCREF(Py_NotImplemented);
3404     return Py_NotImplemented;
3405 }
3406 
3407 PyDoc_STRVAR(object_subclasshook_doc,
3408 "Abstract classes can override this to customize issubclass().\n"
3409 "\n"
3410 "This is invoked early on by abc.ABCMeta.__subclasscheck__().\n"
3411 "It should return True, False or NotImplemented.  If it returns\n"
3412 "NotImplemented, the normal algorithm is used.  Otherwise, it\n"
3413 "overrides the normal algorithm (and the outcome is cached).\n");
3414 
3415 /*
3416    from PEP 3101, this code implements:
3417 
3418    class object:
3419        def __format__(self, format_spec):
3420        if isinstance(format_spec, str):
3421            return format(str(self), format_spec)
3422        elif isinstance(format_spec, unicode):
3423            return format(unicode(self), format_spec)
3424 */
3425 static PyObject *
3426 object_format(PyObject *self, PyObject *args)
3427 {
3428     PyObject *format_spec;
3429     PyObject *self_as_str = NULL;
3430     PyObject *result = NULL;
3431     Py_ssize_t format_len;
3432 
3433     if (!PyArg_ParseTuple(args, "O:__format__", &format_spec))
3434         return NULL;
3435 #ifdef Py_USING_UNICODE
3436     if (PyUnicode_Check(format_spec)) {
3437         format_len = PyUnicode_GET_SIZE(format_spec);
3438         self_as_str = PyObject_Unicode(self);
3439     } else if (PyString_Check(format_spec)) {
3440 #else
3441     if (PyString_Check(format_spec)) {
3442 #endif
3443         format_len = PyString_GET_SIZE(format_spec);
3444         self_as_str = PyObject_Str(self);
3445     } else {
3446         PyErr_SetString(PyExc_TypeError,
3447                  "argument to __format__ must be unicode or str");
3448         return NULL;
3449     }
3450 
3451     if (self_as_str != NULL) {
3452         /* Issue 7994: If we're converting to a string, we
3453            should reject format specifications */
3454         if (format_len > 0) {
3455             if (PyErr_WarnEx(PyExc_PendingDeprecationWarning,
3456              "object.__format__ with a non-empty format "
3457              "string is deprecated", 1) < 0) {
3458                 goto done;
3459             }
3460             /* Eventually this will become an error:
3461             PyErr_Format(PyExc_TypeError,
3462                "non-empty format string passed to object.__format__");
3463             goto done;
3464             */
3465         }
3466         result = PyObject_Format(self_as_str, format_spec);
3467     }
3468 
3469 done:
3470     Py_XDECREF(self_as_str);
3471 
3472     return result;
3473 }
3474 
3475 static PyObject *
3476 object_sizeof(PyObject *self, PyObject *args)
3477 {
3478     Py_ssize_t res, isize;
3479 
3480     res = 0;
3481     isize = self->ob_type->tp_itemsize;
3482     if (isize > 0)
3483         res = self->ob_type->ob_size * isize;
3484     res += self->ob_type->tp_basicsize;
3485 
3486     return PyInt_FromSsize_t(res);
3487 }
3488 
3489 static PyMethodDef object_methods[] = {
3490     {"__reduce_ex__", object_reduce_ex, METH_VARARGS,
3491      PyDoc_STR("helper for pickle")},
3492     {"__reduce__", object_reduce, METH_VARARGS,
3493      PyDoc_STR("helper for pickle")},
3494     {"__subclasshook__", object_subclasshook, METH_CLASS | METH_VARARGS,
3495      object_subclasshook_doc},
3496     {"__format__", object_format, METH_VARARGS,
3497      PyDoc_STR("default object formatter")},
3498     {"__sizeof__", object_sizeof, METH_NOARGS,
3499      PyDoc_STR("__sizeof__() -> int\nsize of object in memory, in bytes")},
3500     {0}
3501 };
3502 
3503 
3504 PyTypeObject PyBaseObject_Type = {
3505     PyVarObject_HEAD_INIT(&PyType_Type, 0)
3506     "object",                                   /* tp_name */
3507     sizeof(PyObject),                           /* tp_basicsize */
3508     0,                                          /* tp_itemsize */
3509     object_dealloc,                             /* tp_dealloc */
3510     0,                                          /* tp_print */
3511     0,                                          /* tp_getattr */
3512     0,                                          /* tp_setattr */
3513     0,                                          /* tp_compare */
3514     object_repr,                                /* tp_repr */
3515     0,                                          /* tp_as_number */
3516     0,                                          /* tp_as_sequence */
3517     0,                                          /* tp_as_mapping */
3518     (hashfunc)_Py_HashPointer,                  /* tp_hash */
3519     0,                                          /* tp_call */
3520     object_str,                                 /* tp_str */
3521     PyObject_GenericGetAttr,                    /* tp_getattro */
3522     PyObject_GenericSetAttr,                    /* tp_setattro */
3523     0,                                          /* tp_as_buffer */
3524     Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
3525     PyDoc_STR("The most base type"),            /* tp_doc */
3526     0,                                          /* tp_traverse */
3527     0,                                          /* tp_clear */
3528     0,                                          /* tp_richcompare */
3529     0,                                          /* tp_weaklistoffset */
3530     0,                                          /* tp_iter */
3531     0,                                          /* tp_iternext */
3532     object_methods,                             /* tp_methods */
3533     0,                                          /* tp_members */
3534     object_getsets,                             /* tp_getset */
3535     0,                                          /* tp_base */
3536     0,                                          /* tp_dict */
3537     0,                                          /* tp_descr_get */
3538     0,                                          /* tp_descr_set */
3539     0,                                          /* tp_dictoffset */
3540     object_init,                                /* tp_init */
3541     PyType_GenericAlloc,                        /* tp_alloc */
3542     object_new,                                 /* tp_new */
3543     PyObject_Del,                               /* tp_free */
3544 };
3545 
3546 
3547 /* Initialize the __dict__ in a type object */
3548 
3549 static int
3550 add_methods(PyTypeObject *type, PyMethodDef *meth)
3551 {
3552     PyObject *dict = type->tp_dict;
3553 
3554     for (; meth->ml_name != NULL; meth++) {
3555         PyObject *descr;
3556         if (PyDict_GetItemString(dict, meth->ml_name) &&
3557             !(meth->ml_flags & METH_COEXIST))
3558                 continue;
3559         if (meth->ml_flags & METH_CLASS) {
3560             if (meth->ml_flags & METH_STATIC) {
3561                 PyErr_SetString(PyExc_ValueError,
3562                      "method cannot be both class and static");
3563                 return -1;
3564             }
3565             descr = PyDescr_NewClassMethod(type, meth);
3566         }
3567         else if (meth->ml_flags & METH_STATIC) {
3568             PyObject *cfunc = PyCFunction_New(meth, NULL);
3569             if (cfunc == NULL)
3570                 return -1;
3571             descr = PyStaticMethod_New(cfunc);
3572             Py_DECREF(cfunc);
3573         }
3574         else {
3575             descr = PyDescr_NewMethod(type, meth);
3576         }
3577         if (descr == NULL)
3578             return -1;
3579         if (PyDict_SetItemString(dict, meth->ml_name, descr) < 0)
3580             return -1;
3581         Py_DECREF(descr);
3582     }
3583     return 0;
3584 }
3585 
3586 static int
3587 add_members(PyTypeObject *type, PyMemberDef *memb)
3588 {
3589     PyObject *dict = type->tp_dict;
3590 
3591     for (; memb->name != NULL; memb++) {
3592         PyObject *descr;
3593         if (PyDict_GetItemString(dict, memb->name))
3594             continue;
3595         descr = PyDescr_NewMember(type, memb);
3596         if (descr == NULL)
3597             return -1;
3598         if (PyDict_SetItemString(dict, memb->name, descr) < 0)
3599             return -1;
3600         Py_DECREF(descr);
3601     }
3602     return 0;
3603 }
3604 
3605 static int
3606 add_getset(PyTypeObject *type, PyGetSetDef *gsp)
3607 {
3608     PyObject *dict = type->tp_dict;
3609 
3610     for (; gsp->name != NULL; gsp++) {
3611         PyObject *descr;
3612         if (PyDict_GetItemString(dict, gsp->name))
3613             continue;
3614         descr = PyDescr_NewGetSet(type, gsp);
3615 
3616         if (descr == NULL)
3617             return -1;
3618         if (PyDict_SetItemString(dict, gsp->name, descr) < 0)
3619             return -1;
3620         Py_DECREF(descr);
3621     }
3622     return 0;
3623 }
3624 
3625 #define BUFFER_FLAGS (Py_TPFLAGS_HAVE_GETCHARBUFFER | Py_TPFLAGS_HAVE_NEWBUFFER)
3626 
3627 static void
3628 inherit_special(PyTypeObject *type, PyTypeObject *base)
3629 {
3630     Py_ssize_t oldsize, newsize;
3631 
3632     /* Special flag magic */
3633     if (!type->tp_as_buffer && base->tp_as_buffer) {
3634         type->tp_flags &= ~BUFFER_FLAGS;
3635         type->tp_flags |=
3636             base->tp_flags & BUFFER_FLAGS;
3637     }
3638     if (!type->tp_as_sequence && base->tp_as_sequence) {
3639         type->tp_flags &= ~Py_TPFLAGS_HAVE_SEQUENCE_IN;
3640         type->tp_flags |= base->tp_flags & Py_TPFLAGS_HAVE_SEQUENCE_IN;
3641     }
3642     if ((type->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS) !=
3643         (base->tp_flags & Py_TPFLAGS_HAVE_INPLACEOPS)) {
3644         if ((!type->tp_as_number && base->tp_as_number) ||
3645             (!type->tp_as_sequence && base->tp_as_sequence)) {
3646             type->tp_flags &= ~Py_TPFLAGS_HAVE_INPLACEOPS;
3647             if (!type->tp_as_number && !type->tp_as_sequence) {
3648                 type->tp_flags |= base->tp_flags &
3649                     Py_TPFLAGS_HAVE_INPLACEOPS;
3650             }
3651         }
3652         /* Wow */
3653     }
3654     if (!type->tp_as_number && base->tp_as_number) {
3655         type->tp_flags &= ~Py_TPFLAGS_CHECKTYPES;
3656         type->tp_flags |= base->tp_flags & Py_TPFLAGS_CHECKTYPES;
3657     }
3658 
3659     /* Copying basicsize is connected to the GC flags */
3660     oldsize = base->tp_basicsize;
3661     newsize = type->tp_basicsize ? type->tp_basicsize : oldsize;
3662     if (!(type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3663         (base->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3664         (type->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE/*GC slots exist*/) &&
3665         (!type->tp_traverse && !type->tp_clear)) {
3666         type->tp_flags |= Py_TPFLAGS_HAVE_GC;
3667         if (type->tp_traverse == NULL)
3668             type->tp_traverse = base->tp_traverse;
3669         if (type->tp_clear == NULL)
3670             type->tp_clear = base->tp_clear;
3671     }
3672     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3673         /* The condition below could use some explanation.
3674            It appears that tp_new is not inherited for static types
3675            whose base class is 'object'; this seems to be a precaution
3676            so that old extension types don't suddenly become
3677            callable (object.__new__ wouldn't insure the invariants
3678            that the extension type's own factory function ensures).
3679            Heap types, of course, are under our control, so they do
3680            inherit tp_new; static extension types that specify some
3681            other built-in type as the default are considered
3682            new-style-aware so they also inherit object.__new__. */
3683         if (base != &PyBaseObject_Type ||
3684             (type->tp_flags & Py_TPFLAGS_HEAPTYPE)) {
3685             if (type->tp_new == NULL)
3686                 type->tp_new = base->tp_new;
3687         }
3688     }
3689     type->tp_basicsize = newsize;
3690 
3691     /* Copy other non-function slots */
3692 
3693 #undef COPYVAL
3694 #define COPYVAL(SLOT) \
3695     if (type->SLOT == 0) type->SLOT = base->SLOT
3696 
3697     COPYVAL(tp_itemsize);
3698     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_WEAKREFS) {
3699         COPYVAL(tp_weaklistoffset);
3700     }
3701     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3702         COPYVAL(tp_dictoffset);
3703     }
3704 
3705     /* Setup fast subclass flags */
3706     if (PyType_IsSubtype(base, (PyTypeObject*)PyExc_BaseException))
3707         type->tp_flags |= Py_TPFLAGS_BASE_EXC_SUBCLASS;
3708     else if (PyType_IsSubtype(base, &PyType_Type))
3709         type->tp_flags |= Py_TPFLAGS_TYPE_SUBCLASS;
3710     else if (PyType_IsSubtype(base, &PyInt_Type))
3711         type->tp_flags |= Py_TPFLAGS_INT_SUBCLASS;
3712     else if (PyType_IsSubtype(base, &PyLong_Type))
3713         type->tp_flags |= Py_TPFLAGS_LONG_SUBCLASS;
3714     else if (PyType_IsSubtype(base, &PyString_Type))
3715         type->tp_flags |= Py_TPFLAGS_STRING_SUBCLASS;
3716 #ifdef Py_USING_UNICODE
3717     else if (PyType_IsSubtype(base, &PyUnicode_Type))
3718         type->tp_flags |= Py_TPFLAGS_UNICODE_SUBCLASS;
3719 #endif
3720     else if (PyType_IsSubtype(base, &PyTuple_Type))
3721         type->tp_flags |= Py_TPFLAGS_TUPLE_SUBCLASS;
3722     else if (PyType_IsSubtype(base, &PyList_Type))
3723         type->tp_flags |= Py_TPFLAGS_LIST_SUBCLASS;
3724     else if (PyType_IsSubtype(base, &PyDict_Type))
3725         type->tp_flags |= Py_TPFLAGS_DICT_SUBCLASS;
3726 }
3727 
3728 static int
3729 overrides_name(PyTypeObject *type, char *name)
3730 {
3731     PyObject *dict = type->tp_dict;
3732 
3733     assert(dict != NULL);
3734     if (PyDict_GetItemString(dict, name) != NULL) {
3735         return 1;
3736     }
3737     return 0;
3738 }
3739 
3740 #define OVERRIDES_HASH(x)       overrides_name(x, "__hash__")
3741 #define OVERRIDES_EQ(x)         overrides_name(x, "__eq__")
3742 
3743 static void
3744 inherit_slots(PyTypeObject *type, PyTypeObject *base)
3745 {
3746     PyTypeObject *basebase;
3747 
3748 #undef SLOTDEFINED
3749 #undef COPYSLOT
3750 #undef COPYNUM
3751 #undef COPYSEQ
3752 #undef COPYMAP
3753 #undef COPYBUF
3754 
3755 #define SLOTDEFINED(SLOT) \
3756     (base->SLOT != 0 && \
3757      (basebase == NULL || base->SLOT != basebase->SLOT))
3758 
3759 #define COPYSLOT(SLOT) \
3760     if (!type->SLOT && SLOTDEFINED(SLOT)) type->SLOT = base->SLOT
3761 
3762 #define COPYNUM(SLOT) COPYSLOT(tp_as_number->SLOT)
3763 #define COPYSEQ(SLOT) COPYSLOT(tp_as_sequence->SLOT)
3764 #define COPYMAP(SLOT) COPYSLOT(tp_as_mapping->SLOT)
3765 #define COPYBUF(SLOT) COPYSLOT(tp_as_buffer->SLOT)
3766 
3767     /* This won't inherit indirect slots (from tp_as_number etc.)
3768        if type doesn't provide the space. */
3769 
3770     if (type->tp_as_number != NULL && base->tp_as_number != NULL) {
3771         basebase = base->tp_base;
3772         if (basebase->tp_as_number == NULL)
3773             basebase = NULL;
3774         COPYNUM(nb_add);
3775         COPYNUM(nb_subtract);
3776         COPYNUM(nb_multiply);
3777         COPYNUM(nb_divide);
3778         COPYNUM(nb_remainder);
3779         COPYNUM(nb_divmod);
3780         COPYNUM(nb_power);
3781         COPYNUM(nb_negative);
3782         COPYNUM(nb_positive);
3783         COPYNUM(nb_absolute);
3784         COPYNUM(nb_nonzero);
3785         COPYNUM(nb_invert);
3786         COPYNUM(nb_lshift);
3787         COPYNUM(nb_rshift);
3788         COPYNUM(nb_and);
3789         COPYNUM(nb_xor);
3790         COPYNUM(nb_or);
3791         COPYNUM(nb_coerce);
3792         COPYNUM(nb_int);
3793         COPYNUM(nb_long);
3794         COPYNUM(nb_float);
3795         COPYNUM(nb_oct);
3796         COPYNUM(nb_hex);
3797         COPYNUM(nb_inplace_add);
3798         COPYNUM(nb_inplace_subtract);
3799         COPYNUM(nb_inplace_multiply);
3800         COPYNUM(nb_inplace_divide);
3801         COPYNUM(nb_inplace_remainder);
3802         COPYNUM(nb_inplace_power);
3803         COPYNUM(nb_inplace_lshift);
3804         COPYNUM(nb_inplace_rshift);
3805         COPYNUM(nb_inplace_and);
3806         COPYNUM(nb_inplace_xor);
3807         COPYNUM(nb_inplace_or);
3808         if (base->tp_flags & Py_TPFLAGS_CHECKTYPES) {
3809             COPYNUM(nb_true_divide);
3810             COPYNUM(nb_floor_divide);
3811             COPYNUM(nb_inplace_true_divide);
3812             COPYNUM(nb_inplace_floor_divide);
3813         }
3814         if (base->tp_flags & Py_TPFLAGS_HAVE_INDEX) {
3815             COPYNUM(nb_index);
3816         }
3817     }
3818 
3819     if (type->tp_as_sequence != NULL && base->tp_as_sequence != NULL) {
3820         basebase = base->tp_base;
3821         if (basebase->tp_as_sequence == NULL)
3822             basebase = NULL;
3823         COPYSEQ(sq_length);
3824         COPYSEQ(sq_concat);
3825         COPYSEQ(sq_repeat);
3826         COPYSEQ(sq_item);
3827         COPYSEQ(sq_slice);
3828         COPYSEQ(sq_ass_item);
3829         COPYSEQ(sq_ass_slice);
3830         COPYSEQ(sq_contains);
3831         COPYSEQ(sq_inplace_concat);
3832         COPYSEQ(sq_inplace_repeat);
3833     }
3834 
3835     if (type->tp_as_mapping != NULL && base->tp_as_mapping != NULL) {
3836         basebase = base->tp_base;
3837         if (basebase->tp_as_mapping == NULL)
3838             basebase = NULL;
3839         COPYMAP(mp_length);
3840         COPYMAP(mp_subscript);
3841         COPYMAP(mp_ass_subscript);
3842     }
3843 
3844     if (type->tp_as_buffer != NULL && base->tp_as_buffer != NULL) {
3845         basebase = base->tp_base;
3846         if (basebase->tp_as_buffer == NULL)
3847             basebase = NULL;
3848         COPYBUF(bf_getreadbuffer);
3849         COPYBUF(bf_getwritebuffer);
3850         COPYBUF(bf_getsegcount);
3851         COPYBUF(bf_getcharbuffer);
3852         COPYBUF(bf_getbuffer);
3853         COPYBUF(bf_releasebuffer);
3854     }
3855 
3856     basebase = base->tp_base;
3857 
3858     COPYSLOT(tp_dealloc);
3859     COPYSLOT(tp_print);
3860     if (type->tp_getattr == NULL && type->tp_getattro == NULL) {
3861         type->tp_getattr = base->tp_getattr;
3862         type->tp_getattro = base->tp_getattro;
3863     }
3864     if (type->tp_setattr == NULL && type->tp_setattro == NULL) {
3865         type->tp_setattr = base->tp_setattr;
3866         type->tp_setattro = base->tp_setattro;
3867     }
3868     /* tp_compare see tp_richcompare */
3869     COPYSLOT(tp_repr);
3870     /* tp_hash see tp_richcompare */
3871     COPYSLOT(tp_call);
3872     COPYSLOT(tp_str);
3873     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_RICHCOMPARE) {
3874         if (type->tp_compare == NULL &&
3875             type->tp_richcompare == NULL &&
3876             type->tp_hash == NULL)
3877         {
3878             type->tp_compare = base->tp_compare;
3879             type->tp_richcompare = base->tp_richcompare;
3880             type->tp_hash = base->tp_hash;
3881             /* Check for changes to inherited methods in Py3k*/
3882             if (Py_Py3kWarningFlag) {
3883                 if (base->tp_hash &&
3884                                 (base->tp_hash != PyObject_HashNotImplemented) &&
3885                                 !OVERRIDES_HASH(type)) {
3886                     if (OVERRIDES_EQ(type)) {
3887                         if (PyErr_WarnPy3k("Overriding "
3888                                            "__eq__ blocks inheritance "
3889                                            "of __hash__ in 3.x",
3890                                            1) < 0)
3891                             /* XXX This isn't right.  If the warning is turned
3892                                into an exception, we should be communicating
3893                                the error back to the caller, but figuring out
3894                                how to clean up in that case is tricky.  See
3895                                issue 8627 for more. */
3896                             PyErr_Clear();
3897                     }
3898                 }
3899             }
3900         }
3901     }
3902     else {
3903         COPYSLOT(tp_compare);
3904     }
3905     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_ITER) {
3906         COPYSLOT(tp_iter);
3907         COPYSLOT(tp_iternext);
3908     }
3909     if (type->tp_flags & base->tp_flags & Py_TPFLAGS_HAVE_CLASS) {
3910         COPYSLOT(tp_descr_get);
3911         COPYSLOT(tp_descr_set);
3912         COPYSLOT(tp_dictoffset);
3913         COPYSLOT(tp_init);
3914         COPYSLOT(tp_alloc);
3915         COPYSLOT(tp_is_gc);
3916         if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) ==
3917             (base->tp_flags & Py_TPFLAGS_HAVE_GC)) {
3918             /* They agree about gc. */
3919             COPYSLOT(tp_free);
3920         }
3921         else if ((type->tp_flags & Py_TPFLAGS_HAVE_GC) &&
3922                  type->tp_free == NULL &&
3923                  base->tp_free == _PyObject_Del) {
3924             /* A bit of magic to plug in the correct default
3925              * tp_free function when a derived class adds gc,
3926              * didn't define tp_free, and the base uses the
3927              * default non-gc tp_free.
3928              */
3929             type->tp_free = PyObject_GC_Del;
3930         }
3931         /* else they didn't agree about gc, and there isn't something
3932          * obvious to be done -- the type is on its own.
3933          */
3934     }
3935 }
3936 
3937 static int add_operators(PyTypeObject *);
3938 
3939 int
3940 PyType_Ready(PyTypeObject *type)
3941 {
3942     PyObject *dict, *bases;
3943     PyTypeObject *base;
3944     Py_ssize_t i, n;
3945 
3946     if (type->tp_flags & Py_TPFLAGS_READY) {
3947         assert(type->tp_dict != NULL);
3948         return 0;
3949     }
3950     assert((type->tp_flags & Py_TPFLAGS_READYING) == 0);
3951 
3952     type->tp_flags |= Py_TPFLAGS_READYING;
3953 
3954 #ifdef Py_TRACE_REFS
3955     /* PyType_Ready is the closest thing we have to a choke point
3956      * for type objects, so is the best place I can think of to try
3957      * to get type objects into the doubly-linked list of all objects.
3958      * Still, not all type objects go thru PyType_Ready.
3959      */
3960     _Py_AddToAllObjects((PyObject *)type, 0);
3961 #endif
3962 
3963     /* Initialize tp_base (defaults to BaseObject unless that's us) */
3964     base = type->tp_base;
3965     if (base == NULL && type != &PyBaseObject_Type) {
3966         base = type->tp_base = &PyBaseObject_Type;
3967         Py_INCREF(base);
3968     }
3969 
3970     /* Now the only way base can still be NULL is if type is
3971      * &PyBaseObject_Type.
3972      */
3973 
3974     /* Initialize the base class */
3975     if (base && base->tp_dict == NULL) {
3976         if (PyType_Ready(base) < 0)
3977             goto error;
3978     }
3979 
3980     /* Initialize ob_type if NULL.      This means extensions that want to be
3981        compilable separately on Windows can call PyType_Ready() instead of
3982        initializing the ob_type field of their type objects. */
3983     /* The test for base != NULL is really unnecessary, since base is only
3984        NULL when type is &PyBaseObject_Type, and we know its ob_type is
3985        not NULL (it's initialized to &PyType_Type).      But coverity doesn't
3986        know that. */
3987     if (Py_TYPE(type) == NULL && base != NULL)
3988         Py_TYPE(type) = Py_TYPE(base);
3989 
3990     /* Initialize tp_bases */
3991     bases = type->tp_bases;
3992     if (bases == NULL) {
3993         if (base == NULL)
3994             bases = PyTuple_New(0);
3995         else
3996             bases = PyTuple_Pack(1, base);
3997         if (bases == NULL)
3998             goto error;
3999         type->tp_bases = bases;
4000     }
4001 
4002     /* Initialize tp_dict */
4003     dict = type->tp_dict;
4004     if (dict == NULL) {
4005         dict = PyDict_New();
4006         if (dict == NULL)
4007             goto error;
4008         type->tp_dict = dict;
4009     }
4010 
4011     /* Add type-specific descriptors to tp_dict */
4012     if (add_operators(type) < 0)
4013         goto error;
4014     if (type->tp_methods != NULL) {
4015         if (add_methods(type, type->tp_methods) < 0)
4016             goto error;
4017     }
4018     if (type->tp_members != NULL) {
4019         if (add_members(type, type->tp_members) < 0)
4020             goto error;
4021     }
4022     if (type->tp_getset != NULL) {
4023         if (add_getset(type, type->tp_getset) < 0)
4024             goto error;
4025     }
4026 
4027     /* Calculate method resolution order */
4028     if (mro_internal(type) < 0) {
4029         goto error;
4030     }
4031 
4032     /* Inherit special flags from dominant base */
4033     if (type->tp_base != NULL)
4034         inherit_special(type, type->tp_base);
4035 
4036     /* Initialize tp_dict properly */
4037     bases = type->tp_mro;
4038     assert(bases != NULL);
4039     assert(PyTuple_Check(bases));
4040     n = PyTuple_GET_SIZE(bases);
4041     for (i = 1; i < n; i++) {
4042         PyObject *b = PyTuple_GET_ITEM(bases, i);
4043         if (PyType_Check(b))
4044             inherit_slots(type, (PyTypeObject *)b);
4045     }
4046 
4047     /* Sanity check for tp_free. */
4048     if (PyType_IS_GC(type) && (type->tp_flags & Py_TPFLAGS_BASETYPE) &&
4049         (type->tp_free == NULL || type->tp_free == PyObject_Del)) {
4050         /* This base class needs to call tp_free, but doesn't have
4051          * one, or its tp_free is for non-gc'ed objects.
4052          */
4053         PyErr_Format(PyExc_TypeError, "type '%.100s' participates in "
4054                      "gc and is a base type but has inappropriate "
4055                      "tp_free slot",
4056                      type->tp_name);
4057         goto error;
4058     }
4059 
4060     /* if the type dictionary doesn't contain a __doc__, set it from
4061        the tp_doc slot.
4062      */
4063     if (PyDict_GetItemString(type->tp_dict, "__doc__") == NULL) {
4064         if (type->tp_doc != NULL) {
4065             PyObject *doc = PyString_FromString(type->tp_doc);
4066             if (doc == NULL)
4067                 goto error;
4068             PyDict_SetItemString(type->tp_dict, "__doc__", doc);
4069             Py_DECREF(doc);
4070         } else {
4071             PyDict_SetItemString(type->tp_dict,
4072                                  "__doc__", Py_None);
4073         }
4074     }
4075 
4076     /* Some more special stuff */
4077     base = type->tp_base;
4078     if (base != NULL) {
4079         if (type->tp_as_number == NULL)
4080             type->tp_as_number = base->tp_as_number;
4081         if (type->tp_as_sequence == NULL)
4082             type->tp_as_sequence = base->tp_as_sequence;
4083         if (type->tp_as_mapping == NULL)
4084             type->tp_as_mapping = base->tp_as_mapping;
4085         if (type->tp_as_buffer == NULL)
4086             type->tp_as_buffer = base->tp_as_buffer;
4087     }
4088 
4089     /* Link into each base class's list of subclasses */
4090     bases = type->tp_bases;
4091     n = PyTuple_GET_SIZE(bases);
4092     for (i = 0; i < n; i++) {
4093         PyObject *b = PyTuple_GET_ITEM(bases, i);
4094         if (PyType_Check(b) &&
4095             add_subclass((PyTypeObject *)b, type) < 0)
4096             goto error;
4097     }
4098 
4099     /* All done -- set the ready flag */
4100     assert(type->tp_dict != NULL);
4101     type->tp_flags =
4102         (type->tp_flags & ~Py_TPFLAGS_READYING) | Py_TPFLAGS_READY;
4103     return 0;
4104 
4105   error:
4106     type->tp_flags &= ~Py_TPFLAGS_READYING;
4107     return -1;
4108 }
4109 
4110 static int
4111 add_subclass(PyTypeObject *base, PyTypeObject *type)
4112 {
4113     Py_ssize_t i;
4114     int result;
4115     PyObject *list, *ref, *newobj;
4116 
4117     list = base->tp_subclasses;
4118     if (list == NULL) {
4119         base->tp_subclasses = list = PyList_New(0);
4120         if (list == NULL)
4121             return -1;
4122     }
4123     assert(PyList_Check(list));
4124     newobj = PyWeakref_NewRef((PyObject *)type, NULL);
4125     i = PyList_GET_SIZE(list);
4126     while (--i >= 0) {
4127         ref = PyList_GET_ITEM(list, i);
4128         assert(PyWeakref_CheckRef(ref));
4129         if (PyWeakref_GET_OBJECT(ref) == Py_None)
4130             return PyList_SetItem(list, i, newobj);
4131     }
4132     result = PyList_Append(list, newobj);
4133     Py_DECREF(newobj);
4134     return result;
4135 }
4136 
4137 static void
4138 remove_subclass(PyTypeObject *base, PyTypeObject *type)
4139 {
4140     Py_ssize_t i;
4141     PyObject *list, *ref;
4142 
4143     list = base->tp_subclasses;
4144     if (list == NULL) {
4145         return;
4146     }
4147     assert(PyList_Check(list));
4148     i = PyList_GET_SIZE(list);
4149     while (--i >= 0) {
4150         ref = PyList_GET_ITEM(list, i);
4151         assert(PyWeakref_CheckRef(ref));
4152         if (PyWeakref_GET_OBJECT(ref) == (PyObject*)type) {
4153             /* this can't fail, right? */
4154             PySequence_DelItem(list, i);
4155             return;
4156         }
4157     }
4158 }
4159 
4160 static int
4161 check_num_args(PyObject *ob, int n)
4162 {
4163     if (!PyTuple_CheckExact(ob)) {
4164         PyErr_SetString(PyExc_SystemError,
4165             "PyArg_UnpackTuple() argument list is not a tuple");
4166         return 0;
4167     }
4168     if (n == PyTuple_GET_SIZE(ob))
4169         return 1;
4170     PyErr_Format(
4171         PyExc_TypeError,
4172         "expected %d arguments, got %zd", n, PyTuple_GET_SIZE(ob));
4173     return 0;
4174 }
4175 
4176 /* Generic wrappers for overloadable 'operators' such as __getitem__ */
4177 
4178 /* There's a wrapper *function* for each distinct function typedef used
4179    for type object slots (e.g. binaryfunc, ternaryfunc, etc.).  There's a
4180    wrapper *table* for each distinct operation (e.g. __len__, __add__).
4181    Most tables have only one entry; the tables for binary operators have two
4182    entries, one regular and one with reversed arguments. */
4183 
4184 static PyObject *
4185 wrap_lenfunc(PyObject *self, PyObject *args, void *wrapped)
4186 {
4187     lenfunc func = (lenfunc)wrapped;
4188     Py_ssize_t res;
4189 
4190     if (!check_num_args(args, 0))
4191         return NULL;
4192     res = (*func)(self);
4193     if (res == -1 && PyErr_Occurred())
4194         return NULL;
4195     return PyInt_FromLong((long)res);
4196 }
4197 
4198 static PyObject *
4199 wrap_inquirypred(PyObject *self, PyObject *args, void *wrapped)
4200 {
4201     inquiry func = (inquiry)wrapped;
4202     int res;
4203 
4204     if (!check_num_args(args, 0))
4205         return NULL;
4206     res = (*func)(self);
4207     if (res == -1 && PyErr_Occurred())
4208         return NULL;
4209     return PyBool_FromLong((long)res);
4210 }
4211 
4212 static PyObject *
4213 wrap_binaryfunc(PyObject *self, PyObject *args, void *wrapped)
4214 {
4215     binaryfunc func = (binaryfunc)wrapped;
4216     PyObject *other;
4217 
4218     if (!check_num_args(args, 1))
4219         return NULL;
4220     other = PyTuple_GET_ITEM(args, 0);
4221     return (*func)(self, other);
4222 }
4223 
4224 static PyObject *
4225 wrap_binaryfunc_l(PyObject *self, PyObject *args, void *wrapped)
4226 {
4227     binaryfunc func = (binaryfunc)wrapped;
4228     PyObject *other;
4229 
4230     if (!check_num_args(args, 1))
4231         return NULL;
4232     other = PyTuple_GET_ITEM(args, 0);
4233     if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4234         !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4235         Py_INCREF(Py_NotImplemented);
4236         return Py_NotImplemented;
4237     }
4238     return (*func)(self, other);
4239 }
4240 
4241 static PyObject *
4242 wrap_binaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4243 {
4244     binaryfunc func = (binaryfunc)wrapped;
4245     PyObject *other;
4246 
4247     if (!check_num_args(args, 1))
4248         return NULL;
4249     other = PyTuple_GET_ITEM(args, 0);
4250     if (!(self->ob_type->tp_flags & Py_TPFLAGS_CHECKTYPES) &&
4251         !PyType_IsSubtype(other->ob_type, self->ob_type)) {
4252         Py_INCREF(Py_NotImplemented);
4253         return Py_NotImplemented;
4254     }
4255     return (*func)(other, self);
4256 }
4257 
4258 static PyObject *
4259 wrap_coercefunc(PyObject *self, PyObject *args, void *wrapped)
4260 {
4261     coercion func = (coercion)wrapped;
4262     PyObject *other, *res;
4263     int ok;
4264 
4265     if (!check_num_args(args, 1))
4266         return NULL;
4267     other = PyTuple_GET_ITEM(args, 0);
4268     ok = func(&self, &other);
4269     if (ok < 0)
4270         return NULL;
4271     if (ok > 0) {
4272         Py_INCREF(Py_NotImplemented);
4273         return Py_NotImplemented;
4274     }
4275     res = PyTuple_New(2);
4276     if (res == NULL) {
4277         Py_DECREF(self);
4278         Py_DECREF(other);
4279         return NULL;
4280     }
4281     PyTuple_SET_ITEM(res, 0, self);
4282     PyTuple_SET_ITEM(res, 1, other);
4283     return res;
4284 }
4285 
4286 static PyObject *
4287 wrap_ternaryfunc(PyObject *self, PyObject *args, void *wrapped)
4288 {
4289     ternaryfunc func = (ternaryfunc)wrapped;
4290     PyObject *other;
4291     PyObject *third = Py_None;
4292 
4293     /* Note: This wrapper only works for __pow__() */
4294 
4295     if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4296         return NULL;
4297     return (*func)(self, other, third);
4298 }
4299 
4300 static PyObject *
4301 wrap_ternaryfunc_r(PyObject *self, PyObject *args, void *wrapped)
4302 {
4303     ternaryfunc func = (ternaryfunc)wrapped;
4304     PyObject *other;
4305     PyObject *third = Py_None;
4306 
4307     /* Note: This wrapper only works for __pow__() */
4308 
4309     if (!PyArg_UnpackTuple(args, "", 1, 2, &other, &third))
4310         return NULL;
4311     return (*func)(other, self, third);
4312 }
4313 
4314 static PyObject *
4315 wrap_unaryfunc(PyObject *self, PyObject *args, void *wrapped)
4316 {
4317     unaryfunc func = (unaryfunc)wrapped;
4318 
4319     if (!check_num_args(args, 0))
4320         return NULL;
4321     return (*func)(self);
4322 }
4323 
4324 static PyObject *
4325 wrap_indexargfunc(PyObject *self, PyObject *args, void *wrapped)
4326 {
4327     ssizeargfunc func = (ssizeargfunc)wrapped;
4328     PyObject* o;
4329     Py_ssize_t i;
4330 
4331     if (!PyArg_UnpackTuple(args, "", 1, 1, &o))
4332         return NULL;
4333     i = PyNumber_AsSsize_t(o, PyExc_OverflowError);
4334     if (i == -1 && PyErr_Occurred())
4335         return NULL;
4336     return (*func)(self, i);
4337 }
4338 
4339 static Py_ssize_t
4340 getindex(PyObject *self, PyObject *arg)
4341 {
4342     Py_ssize_t i;
4343 
4344     i = PyNumber_AsSsize_t(arg, PyExc_OverflowError);
4345     if (i == -1 && PyErr_Occurred())
4346         return -1;
4347     if (i < 0) {
4348         PySequenceMethods *sq = Py_TYPE(self)->tp_as_sequence;
4349         if (sq && sq->sq_length) {
4350             Py_ssize_t n = (*sq->sq_length)(self);
4351             if (n < 0)
4352                 return -1;
4353             i += n;
4354         }
4355     }
4356     return i;
4357 }
4358 
4359 static PyObject *
4360 wrap_sq_item(PyObject *self, PyObject *args, void *wrapped)
4361 {
4362     ssizeargfunc func = (ssizeargfunc)wrapped;
4363     PyObject *arg;
4364     Py_ssize_t i;
4365 
4366     if (PyTuple_GET_SIZE(args) == 1) {
4367         arg = PyTuple_GET_ITEM(args, 0);
4368         i = getindex(self, arg);
4369         if (i == -1 && PyErr_Occurred())
4370             return NULL;
4371         return (*func)(self, i);
4372     }
4373     check_num_args(args, 1);
4374     assert(PyErr_Occurred());
4375     return NULL;
4376 }
4377 
4378 static PyObject *
4379 wrap_ssizessizeargfunc(PyObject *self, PyObject *args, void *wrapped)
4380 {
4381     ssizessizeargfunc func = (ssizessizeargfunc)wrapped;
4382     Py_ssize_t i, j;
4383 
4384     if (!PyArg_ParseTuple(args, "nn", &i, &j))
4385         return NULL;
4386     return (*func)(self, i, j);
4387 }
4388 
4389 static PyObject *
4390 wrap_sq_setitem(PyObject *self, PyObject *args, void *wrapped)
4391 {
4392     ssizeobjargproc func = (ssizeobjargproc)wrapped;
4393     Py_ssize_t i;
4394     int res;
4395     PyObject *arg, *value;
4396 
4397     if (!PyArg_UnpackTuple(args, "", 2, 2, &arg, &value))
4398         return NULL;
4399     i = getindex(self, arg);
4400     if (i == -1 && PyErr_Occurred())
4401         return NULL;
4402     res = (*func)(self, i, value);
4403     if (res == -1 && PyErr_Occurred())
4404         return NULL;
4405     Py_INCREF(Py_None);
4406     return Py_None;
4407 }
4408 
4409 static PyObject *
4410 wrap_sq_delitem(PyObject *self, PyObject *args, void *wrapped)
4411 {
4412     ssizeobjargproc func = (ssizeobjargproc)wrapped;
4413     Py_ssize_t i;
4414     int res;
4415     PyObject *arg;
4416 
4417     if (!check_num_args(args, 1))
4418         return NULL;
4419     arg = PyTuple_GET_ITEM(args, 0);
4420     i = getindex(self, arg);
4421     if (i == -1 && PyErr_Occurred())
4422         return NULL;
4423     res = (*func)(self, i, NULL);
4424     if (res == -1 && PyErr_Occurred())
4425         return NULL;
4426     Py_INCREF(Py_None);
4427     return Py_None;
4428 }
4429 
4430 static PyObject *
4431 wrap_ssizessizeobjargproc(PyObject *self, PyObject *args, void *wrapped)
4432 {
4433     ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4434     Py_ssize_t i, j;
4435     int res;
4436     PyObject *value;
4437 
4438     if (!PyArg_ParseTuple(args, "nnO", &i, &j, &value))
4439         return NULL;
4440     res = (*func)(self, i, j, value);
4441     if (res == -1 && PyErr_Occurred())
4442         return NULL;
4443     Py_INCREF(Py_None);
4444     return Py_None;
4445 }
4446 
4447 static PyObject *
4448 wrap_delslice(PyObject *self, PyObject *args, void *wrapped)
4449 {
4450     ssizessizeobjargproc func = (ssizessizeobjargproc)wrapped;
4451     Py_ssize_t i, j;
4452     int res;
4453 
4454     if (!PyArg_ParseTuple(args, "nn", &i, &j))
4455         return NULL;
4456     res = (*func)(self, i, j, NULL);
4457     if (res == -1 && PyErr_Occurred())
4458         return NULL;
4459     Py_INCREF(Py_None);
4460     return Py_None;
4461 }
4462 
4463 /* XXX objobjproc is a misnomer; should be objargpred */
4464 static PyObject *
4465 wrap_objobjproc(PyObject *self, PyObject *args, void *wrapped)
4466 {
4467     objobjproc func = (objobjproc)wrapped;
4468     int res;
4469     PyObject *value;
4470 
4471     if (!check_num_args(args, 1))
4472         return NULL;
4473     value = PyTuple_GET_ITEM(args, 0);
4474     res = (*func)(self, value);
4475     if (res == -1 && PyErr_Occurred())
4476         return NULL;
4477     else
4478         return PyBool_FromLong(res);
4479 }
4480 
4481 static PyObject *
4482 wrap_objobjargproc(PyObject *self, PyObject *args, void *wrapped)
4483 {
4484     objobjargproc func = (objobjargproc)wrapped;
4485     int res;
4486     PyObject *key, *value;
4487 
4488     if (!PyArg_UnpackTuple(args, "", 2, 2, &key, &value))
4489         return NULL;
4490     res = (*func)(self, key, value);
4491     if (res == -1 && PyErr_Occurred())
4492         return NULL;
4493     Py_INCREF(Py_None);
4494     return Py_None;
4495 }
4496 
4497 static PyObject *
4498 wrap_delitem(PyObject *self, PyObject *args, void *wrapped)
4499 {
4500     objobjargproc func = (objobjargproc)wrapped;
4501     int res;
4502     PyObject *key;
4503 
4504     if (!check_num_args(args, 1))
4505         return NULL;
4506     key = PyTuple_GET_ITEM(args, 0);
4507     res = (*func)(self, key, NULL);
4508     if (res == -1 && PyErr_Occurred())
4509         return NULL;
4510     Py_INCREF(Py_None);
4511     return Py_None;
4512 }
4513 
4514 static PyObject *
4515 wrap_cmpfunc(PyObject *self, PyObject *args, void *wrapped)
4516 {
4517     cmpfunc func = (cmpfunc)wrapped;
4518     int res;
4519     PyObject *other;
4520 
4521     if (!check_num_args(args, 1))
4522         return NULL;
4523     other = PyTuple_GET_ITEM(args, 0);
4524     if (Py_TYPE(other)->tp_compare != func &&
4525         !PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self))) {
4526         PyErr_Format(
4527             PyExc_TypeError,
4528             "%s.__cmp__(x,y) requires y to be a '%s', not a '%s'",
4529             Py_TYPE(self)->tp_name,
4530             Py_TYPE(self)->tp_name,
4531             Py_TYPE(other)->tp_name);
4532         return NULL;
4533     }
4534     res = (*func)(self, other);
4535     if (PyErr_Occurred())
4536         return NULL;
4537     return PyInt_FromLong((long)res);
4538 }
4539 
4540 /* Helper to check for object.__setattr__ or __delattr__ applied to a type.
4541    This is called the Carlo Verre hack after its discoverer. */
4542 static int
4543 hackcheck(PyObject *self, setattrofunc func, char *what)
4544 {
4545     PyTypeObject *type = Py_TYPE(self);
4546     while (type && type->tp_flags & Py_TPFLAGS_HEAPTYPE)
4547         type = type->tp_base;
4548     /* If type is NULL now, this is a really weird type.
4549        In the spirit of backwards compatibility (?), just shut up. */
4550     if (type && type->tp_setattro != func) {
4551         PyErr_Format(PyExc_TypeError,
4552                      "can't apply this %s to %s object",
4553                      what,
4554                      type->tp_name);
4555         return 0;
4556     }
4557     return 1;
4558 }
4559 
4560 static PyObject *
4561 wrap_setattr(PyObject *self, PyObject *args, void *wrapped)
4562 {
4563     setattrofunc func = (setattrofunc)wrapped;
4564     int res;
4565     PyObject *name, *value;
4566 
4567     if (!PyArg_UnpackTuple(args, "", 2, 2, &name, &value))
4568         return NULL;
4569     if (!hackcheck(self, func, "__setattr__"))
4570         return NULL;
4571     res = (*func)(self, name, value);
4572     if (res < 0)
4573         return NULL;
4574     Py_INCREF(Py_None);
4575     return Py_None;
4576 }
4577 
4578 static PyObject *
4579 wrap_delattr(PyObject *self, PyObject *args, void *wrapped)
4580 {
4581     setattrofunc func = (setattrofunc)wrapped;
4582     int res;
4583     PyObject *name;
4584 
4585     if (!check_num_args(args, 1))
4586         return NULL;
4587     name = PyTuple_GET_ITEM(args, 0);
4588     if (!hackcheck(self, func, "__delattr__"))
4589         return NULL;
4590     res = (*func)(self, name, NULL);
4591     if (res < 0)
4592         return NULL;
4593     Py_INCREF(Py_None);
4594     return Py_None;
4595 }
4596 
4597 static PyObject *
4598 wrap_hashfunc(PyObject *self, PyObject *args, void *wrapped)
4599 {
4600     hashfunc func = (hashfunc)wrapped;
4601     long res;
4602 
4603     if (!check_num_args(args, 0))
4604         return NULL;
4605     res = (*func)(self);
4606     if (res == -1 && PyErr_Occurred())
4607         return NULL;
4608     return PyInt_FromLong(res);
4609 }
4610 
4611 static PyObject *
4612 wrap_call(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4613 {
4614     ternaryfunc func = (ternaryfunc)wrapped;
4615 
4616     return (*func)(self, args, kwds);
4617 }
4618 
4619 static PyObject *
4620 wrap_richcmpfunc(PyObject *self, PyObject *args, void *wrapped, int op)
4621 {
4622     richcmpfunc func = (richcmpfunc)wrapped;
4623     PyObject *other;
4624 
4625     if (!check_num_args(args, 1))
4626         return NULL;
4627     other = PyTuple_GET_ITEM(args, 0);
4628     return (*func)(self, other, op);
4629 }
4630 
4631 #undef RICHCMP_WRAPPER
4632 #define RICHCMP_WRAPPER(NAME, OP) \
4633 static PyObject * \
4634 richcmp_##NAME(PyObject *self, PyObject *args, void *wrapped) \
4635 { \
4636     return wrap_richcmpfunc(self, args, wrapped, OP); \
4637 }
4638 
4639 RICHCMP_WRAPPER(lt, Py_LT)
4640 RICHCMP_WRAPPER(le, Py_LE)
4641 RICHCMP_WRAPPER(eq, Py_EQ)
4642 RICHCMP_WRAPPER(ne, Py_NE)
4643 RICHCMP_WRAPPER(gt, Py_GT)
4644 RICHCMP_WRAPPER(ge, Py_GE)
4645 
4646 static PyObject *
4647 wrap_next(PyObject *self, PyObject *args, void *wrapped)
4648 {
4649     unaryfunc func = (unaryfunc)wrapped;
4650     PyObject *res;
4651 
4652     if (!check_num_args(args, 0))
4653         return NULL;
4654     res = (*func)(self);
4655     if (res == NULL && !PyErr_Occurred())
4656         PyErr_SetNone(PyExc_StopIteration);
4657     return res;
4658 }
4659 
4660 static PyObject *
4661 wrap_descr_get(PyObject *self, PyObject *args, void *wrapped)
4662 {
4663     descrgetfunc func = (descrgetfunc)wrapped;
4664     PyObject *obj;
4665     PyObject *type = NULL;
4666 
4667     if (!PyArg_UnpackTuple(args, "", 1, 2, &obj, &type))
4668         return NULL;
4669     if (obj == Py_None)
4670         obj = NULL;
4671     if (type == Py_None)
4672         type = NULL;
4673     if (type == NULL &&obj == NULL) {
4674         PyErr_SetString(PyExc_TypeError,
4675                         "__get__(None, None) is invalid");
4676         return NULL;
4677     }
4678     return (*func)(self, obj, type);
4679 }
4680 
4681 static PyObject *
4682 wrap_descr_set(PyObject *self, PyObject *args, void *wrapped)
4683 {
4684     descrsetfunc func = (descrsetfunc)wrapped;
4685     PyObject *obj, *value;
4686     int ret;
4687 
4688     if (!PyArg_UnpackTuple(args, "", 2, 2, &obj, &value))
4689         return NULL;
4690     ret = (*func)(self, obj, value);
4691     if (ret < 0)
4692         return NULL;
4693     Py_INCREF(Py_None);
4694     return Py_None;
4695 }
4696 
4697 static PyObject *
4698 wrap_descr_delete(PyObject *self, PyObject *args, void *wrapped)
4699 {
4700     descrsetfunc func = (descrsetfunc)wrapped;
4701     PyObject *obj;
4702     int ret;
4703 
4704     if (!check_num_args(args, 1))
4705         return NULL;
4706     obj = PyTuple_GET_ITEM(args, 0);
4707     ret = (*func)(self, obj, NULL);
4708     if (ret < 0)
4709         return NULL;
4710     Py_INCREF(Py_None);
4711     return Py_None;
4712 }
4713 
4714 static PyObject *
4715 wrap_init(PyObject *self, PyObject *args, void *wrapped, PyObject *kwds)
4716 {
4717     initproc func = (initproc)wrapped;
4718 
4719     if (func(self, args, kwds) < 0)
4720         return NULL;
4721     Py_INCREF(Py_None);
4722     return Py_None;
4723 }
4724 
4725 static PyObject *
4726 tp_new_wrapper(PyObject *self, PyObject *args, PyObject *kwds)
4727 {
4728     PyTypeObject *type, *subtype, *staticbase;
4729     PyObject *arg0, *res;
4730 
4731     if (self == NULL || !PyType_Check(self))
4732         Py_FatalError("__new__() called with non-type 'self'");
4733     type = (PyTypeObject *)self;
4734     if (!PyTuple_Check(args) || PyTuple_GET_SIZE(args) < 1) {
4735         PyErr_Format(PyExc_TypeError,
4736                      "%s.__new__(): not enough arguments",
4737                      type->tp_name);
Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

4738 return NULL; 4739 } 4740 arg0 = PyTuple_GET_ITEM(args, 0); 4741 if (!PyType_Check(arg0)) { 4742 PyErr_Format(PyExc_TypeError, 4743 "%s.__new__(X): X is not a type object (%s)", 4744 type->tp_name,
Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

4745 Py_TYPE(arg0)->tp_name); 4746 return NULL; 4747 } 4748 subtype = (PyTypeObject *)arg0; 4749 if (!PyType_IsSubtype(subtype, type)) { 4750 PyErr_Format(PyExc_TypeError, 4751 "%s.__new__(%s): %s is not a subtype of %s", 4752 type->tp_name,
Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Access to field 'tp_name' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

4753 subtype->tp_name, 4754 subtype->tp_name, 4755 type->tp_name); 4756 return NULL; 4757 } 4758 4759 /* Check that the use doesn't do something silly and unsafe like 4760 object.__new__(dict). To do this, we check that the 4761 most derived base that's not a heap type is this type. */ 4762 staticbase = subtype; 4763 while (staticbase && (staticbase->tp_flags & Py_TPFLAGS_HEAPTYPE)) 4764 staticbase = staticbase->tp_base; 4765 /* If staticbase is NULL now, it is a really weird type. 4766 In the spirit of backwards compatibility (?), just shut up. */ 4767 if (staticbase && staticbase->tp_new != type->tp_new) {
Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

4768 PyErr_Format(PyExc_TypeError, 4769 "%s.__new__(%s) is not safe, use %s.__new__()", 4770 type->tp_name, 4771 subtype->tp_name, 4772 staticbase == NULL ? "?" : staticbase->tp_name); 4773 return NULL; 4774 } 4775 4776 args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); 4777 if (args == NULL) 4778 return NULL; 4779 res = type->tp_new(subtype, args, kwds);
Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

Access to field 'tp_new' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

4780 Py_DECREF(args); 4781 return res; 4782 } 4783 4784 static struct PyMethodDef tp_new_methoddef[] = { 4785 {"__new__", (PyCFunction)tp_new_wrapper, METH_VARARGS|METH_KEYWORDS, 4786 PyDoc_STR("T.__new__(S, ...) -> " 4787 "a new object with type S, a subtype of T")}, 4788 {0} 4789 }; 4790 4791 static int 4792 add_tp_new_wrapper(PyTypeObject *type) 4793 { 4794 PyObject *func; 4795 4796 if (PyDict_GetItemString(type->tp_dict, "__new__") != NULL) 4797 return 0; 4798 func = PyCFunction_New(tp_new_methoddef, (PyObject *)type); 4799 if (func == NULL) 4800 return -1; 4801 if (PyDict_SetItemString(type->tp_dict, "__new__", func)) { 4802 Py_DECREF(func); 4803 return -1; 4804 } 4805 Py_DECREF(func); 4806 return 0; 4807 } 4808 4809 /* Slot wrappers that call the corresponding __foo__ slot. See comments 4810 below at override_slots() for more explanation. */ 4811 4812 #define SLOT0(FUNCNAME, OPSTR) \ 4813 static PyObject * \ 4814 FUNCNAME(PyObject *self) \ 4815 { \ 4816 static PyObject *cache_str; \ 4817 return call_method(self, OPSTR, &cache_str, "()"); \ 4818 } 4819 4820 #define SLOT1(FUNCNAME, OPSTR, ARG1TYPE, ARGCODES) \ 4821 static PyObject * \ 4822 FUNCNAME(PyObject *self, ARG1TYPE arg1) \ 4823 { \ 4824 static PyObject *cache_str; \ 4825 return call_method(self, OPSTR, &cache_str, "(" ARGCODES ")", arg1); \ 4826 } 4827 4828 /* Boolean helper for SLOT1BINFULL(). 4829 right.__class__ is a nontrivial subclass of left.__class__. */ 4830 static int 4831 method_is_overloaded(PyObject *left, PyObject *right, char *name) 4832 { 4833 PyObject *a, *b; 4834 int ok; 4835 4836 b = PyObject_GetAttrString((PyObject *)(Py_TYPE(right)), name); 4837 if (b == NULL) { 4838 PyErr_Clear(); 4839 /* If right doesn't have it, it's not overloaded */ 4840 return 0; 4841 } 4842 4843 a = PyObject_GetAttrString((PyObject *)(Py_TYPE(left)), name); 4844 if (a == NULL) { 4845 PyErr_Clear(); 4846 Py_DECREF(b); 4847 /* If right has it but left doesn't, it's overloaded */ 4848 return 1; 4849 } 4850 4851 ok = PyObject_RichCompareBool(a, b, Py_NE); 4852 Py_DECREF(a); 4853 Py_DECREF(b); 4854 if (ok < 0) { 4855 PyErr_Clear(); 4856 return 0; 4857 } 4858 4859 return ok; 4860 } 4861 4862 4863 #define SLOT1BINFULL(FUNCNAME, TESTFUNC, SLOTNAME, OPSTR, ROPSTR) \ 4864 static PyObject * \ 4865 FUNCNAME(PyObject *self, PyObject *other) \ 4866 { \ 4867 static PyObject *cache_str, *rcache_str; \ 4868 int do_other = Py_TYPE(self) != Py_TYPE(other) && \ 4869 Py_TYPE(other)->tp_as_number != NULL && \ 4870 Py_TYPE(other)->tp_as_number->SLOTNAME == TESTFUNC; \ 4871 if (Py_TYPE(self)->tp_as_number != NULL && \ 4872 Py_TYPE(self)->tp_as_number->SLOTNAME == TESTFUNC) { \ 4873 PyObject *r; \ 4874 if (do_other && \ 4875 PyType_IsSubtype(Py_TYPE(other), Py_TYPE(self)) && \ 4876 method_is_overloaded(self, other, ROPSTR)) { \ 4877 r = call_maybe( \ 4878 other, ROPSTR, &rcache_str, "(O)", self); \ 4879 if (r != Py_NotImplemented) \ 4880 return r; \ 4881 Py_DECREF(r); \ 4882 do_other = 0; \ 4883 } \ 4884 r = call_maybe( \ 4885 self, OPSTR, &cache_str, "(O)", other); \ 4886 if (r != Py_NotImplemented || \ 4887 Py_TYPE(other) == Py_TYPE(self)) \ 4888 return r; \ 4889 Py_DECREF(r); \ 4890 } \ 4891 if (do_other) { \ 4892 return call_maybe( \ 4893 other, ROPSTR, &rcache_str, "(O)", self); \ 4894 } \ 4895 Py_INCREF(Py_NotImplemented); \ 4896 return Py_NotImplemented; \ 4897 } 4898 4899 #define SLOT1BIN(FUNCNAME, SLOTNAME, OPSTR, ROPSTR) \ 4900 SLOT1BINFULL(FUNCNAME, FUNCNAME, SLOTNAME, OPSTR, ROPSTR) 4901 4902 #define SLOT2(FUNCNAME, OPSTR, ARG1TYPE, ARG2TYPE, ARGCODES) \ 4903 static PyObject * \ 4904 FUNCNAME(PyObject *self, ARG1TYPE arg1, ARG2TYPE arg2) \ 4905 { \ 4906 static PyObject *cache_str; \ 4907 return call_method(self, OPSTR, &cache_str, \ 4908 "(" ARGCODES ")", arg1, arg2); \ 4909 } 4910 4911 static Py_ssize_t 4912 slot_sq_length(PyObject *self) 4913 { 4914 static PyObject *len_str; 4915 PyObject *res = call_method(self, "__len__", &len_str, "()"); 4916 Py_ssize_t len; 4917 4918 if (res == NULL) 4919 return -1; 4920 len = PyInt_AsSsize_t(res); 4921 Py_DECREF(res); 4922 if (len < 0) { 4923 if (!PyErr_Occurred()) 4924 PyErr_SetString(PyExc_ValueError, 4925 "__len__() should return >= 0"); 4926 return -1; 4927 } 4928 return len; 4929 } 4930 4931 /* Super-optimized version of slot_sq_item. 4932 Other slots could do the same... */ 4933 static PyObject * 4934 slot_sq_item(PyObject *self, Py_ssize_t i) 4935 { 4936 static PyObject *getitem_str; 4937 PyObject *func, *args = NULL, *ival = NULL, *retval = NULL; 4938 descrgetfunc f; 4939 4940 if (getitem_str == NULL) { 4941 getitem_str = PyString_InternFromString("__getitem__"); 4942 if (getitem_str == NULL) 4943 return NULL; 4944 } 4945 func = _PyType_Lookup(Py_TYPE(self), getitem_str); 4946 if (func != NULL) { 4947 if ((f = Py_TYPE(func)->tp_descr_get) == NULL) 4948 Py_INCREF(func); 4949 else { 4950 func = f(func, self, (PyObject *)(Py_TYPE(self))); 4951 if (func == NULL) { 4952 return NULL; 4953 } 4954 } 4955 ival = PyInt_FromSsize_t(i); 4956 if (ival != NULL) { 4957 args = PyTuple_New(1); 4958 if (args != NULL) { 4959 PyTuple_SET_ITEM(args, 0, ival); 4960 retval = PyObject_Call(func, args, NULL); 4961 Py_XDECREF(args); 4962 Py_XDECREF(func); 4963 return retval; 4964 } 4965 } 4966 } 4967 else { 4968 PyErr_SetObject(PyExc_AttributeError, getitem_str); 4969 } 4970 Py_XDECREF(args); 4971 Py_XDECREF(ival); 4972 Py_XDECREF(func); 4973 return NULL; 4974 } 4975 4976 static PyObject* 4977 slot_sq_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j) 4978 { 4979 static PyObject *getslice_str; 4980 4981 if (PyErr_WarnPy3k("in 3.x, __getslice__ has been removed; " 4982 "use __getitem__", 1) < 0) 4983 return NULL; 4984 return call_method(self, "__getslice__", &getslice_str, 4985 "nn", i, j); 4986 } 4987 4988 static int 4989 slot_sq_ass_item(PyObject *self, Py_ssize_t index, PyObject *value) 4990 { 4991 PyObject *res; 4992 static PyObject *delitem_str, *setitem_str; 4993 4994 if (value == NULL) 4995 res = call_method(self, "__delitem__", &delitem_str, 4996 "(n)", index); 4997 else 4998 res = call_method(self, "__setitem__", &setitem_str, 4999 "(nO)", index, value); 5000 if (res == NULL) 5001 return -1; 5002 Py_DECREF(res); 5003 return 0; 5004 } 5005 5006 static int 5007 slot_sq_ass_slice(PyObject *self, Py_ssize_t i, Py_ssize_t j, PyObject *value) 5008 { 5009 PyObject *res; 5010 static PyObject *delslice_str, *setslice_str; 5011 5012 if (value == NULL) { 5013 if (PyErr_WarnPy3k("in 3.x, __delslice__ has been removed; " 5014 "use __delitem__", 1) < 0) 5015 return -1; 5016 res = call_method(self, "__delslice__", &delslice_str, 5017 "(nn)", i, j); 5018 } 5019 else { 5020 if (PyErr_WarnPy3k("in 3.x, __setslice__ has been removed; " 5021 "use __setitem__", 1) < 0) 5022 return -1; 5023 res = call_method(self, "__setslice__", &setslice_str, 5024 "(nnO)", i, j, value); 5025 } 5026 if (res == NULL) 5027 return -1; 5028 Py_DECREF(res); 5029 return 0; 5030 } 5031 5032 static int 5033 slot_sq_contains(PyObject *self, PyObject *value) 5034 { 5035 PyObject *func, *res, *args; 5036 int result = -1; 5037 5038 static PyObject *contains_str; 5039 5040 func = lookup_maybe(self, "__contains__", &contains_str); 5041 if (func != NULL) { 5042 args = PyTuple_Pack(1, value); 5043 if (args == NULL) 5044 res = NULL; 5045 else { 5046 res = PyObject_Call(func, args, NULL); 5047 Py_DECREF(args); 5048 } 5049 Py_DECREF(func); 5050 if (res != NULL) { 5051 result = PyObject_IsTrue(res); 5052 Py_DECREF(res); 5053 } 5054 } 5055 else if (! PyErr_Occurred()) { 5056 /* Possible results: -1 and 1 */ 5057 result = (int)_PySequence_IterSearch(self, value, 5058 PY_ITERSEARCH_CONTAINS); 5059 } 5060 return result; 5061 } 5062 5063 #define slot_mp_length slot_sq_length 5064 5065 SLOT1(slot_mp_subscript, "__getitem__", PyObject *, "O") 5066 5067 static int 5068 slot_mp_ass_subscript(PyObject *self, PyObject *key, PyObject *value) 5069 { 5070 PyObject *res; 5071 static PyObject *delitem_str, *setitem_str; 5072 5073 if (value == NULL) 5074 res = call_method(self, "__delitem__", &delitem_str, 5075 "(O)", key); 5076 else 5077 res = call_method(self, "__setitem__", &setitem_str, 5078 "(OO)", key, value); 5079 if (res == NULL) 5080 return -1; 5081 Py_DECREF(res); 5082 return 0; 5083 } 5084 5085 SLOT1BIN(slot_nb_add, nb_add, "__add__", "__radd__") 5086 SLOT1BIN(slot_nb_subtract, nb_subtract, "__sub__", "__rsub__") 5087 SLOT1BIN(slot_nb_multiply, nb_multiply, "__mul__", "__rmul__") 5088 SLOT1BIN(slot_nb_divide, nb_divide, "__div__", "__rdiv__") 5089 SLOT1BIN(slot_nb_remainder, nb_remainder, "__mod__", "__rmod__") 5090 SLOT1BIN(slot_nb_divmod, nb_divmod, "__divmod__", "__rdivmod__") 5091 5092 static PyObject *slot_nb_power(PyObject *, PyObject *, PyObject *); 5093 5094 SLOT1BINFULL(slot_nb_power_binary, slot_nb_power, 5095 nb_power, "__pow__", "__rpow__") 5096 5097 static PyObject * 5098 slot_nb_power(PyObject *self, PyObject *other, PyObject *modulus) 5099 { 5100 static PyObject *pow_str; 5101 5102 if (modulus == Py_None) 5103 return slot_nb_power_binary(self, other); 5104 /* Three-arg power doesn't use __rpow__. But ternary_op 5105 can call this when the second argument's type uses 5106 slot_nb_power, so check before calling self.__pow__. */ 5107 if (Py_TYPE(self)->tp_as_number != NULL && 5108 Py_TYPE(self)->tp_as_number->nb_power == slot_nb_power) { 5109 return call_method(self, "__pow__", &pow_str, 5110 "(OO)", other, modulus); 5111 } 5112 Py_INCREF(Py_NotImplemented); 5113 return Py_NotImplemented; 5114 } 5115 5116 SLOT0(slot_nb_negative, "__neg__") 5117 SLOT0(slot_nb_positive, "__pos__") 5118 SLOT0(slot_nb_absolute, "__abs__") 5119 5120 static int 5121 slot_nb_nonzero(PyObject *self) 5122 { 5123 PyObject *func, *args; 5124 static PyObject *nonzero_str, *len_str; 5125 int result = -1; 5126 int using_len = 0; 5127 5128 func = lookup_maybe(self, "__nonzero__", &nonzero_str); 5129 if (func == NULL) { 5130 if (PyErr_Occurred()) 5131 return -1; 5132 func = lookup_maybe(self, "__len__", &len_str); 5133 if (func == NULL) 5134 return PyErr_Occurred() ? -1 : 1; 5135 using_len = 1; 5136 } 5137 args = PyTuple_New(0); 5138 if (args != NULL) { 5139 PyObject *temp = PyObject_Call(func, args, NULL); 5140 Py_DECREF(args); 5141 if (temp != NULL) { 5142 if (PyInt_CheckExact(temp) || PyBool_Check(temp)) 5143 result = PyObject_IsTrue(temp); 5144 else { 5145 PyErr_Format(PyExc_TypeError, 5146 "%s should return " 5147 "bool or int, returned %s", 5148 (using_len ? "__len__" 5149 : "__nonzero__"), 5150 temp->ob_type->tp_name); 5151 result = -1; 5152 } 5153 Py_DECREF(temp); 5154 } 5155 } 5156 Py_DECREF(func); 5157 return result; 5158 } 5159 5160 5161 static PyObject * 5162 slot_nb_index(PyObject *self) 5163 { 5164 static PyObject *index_str; 5165 return call_method(self, "__index__", &index_str, "()"); 5166 } 5167 5168 5169 SLOT0(slot_nb_invert, "__invert__") 5170 SLOT1BIN(slot_nb_lshift, nb_lshift, "__lshift__", "__rlshift__") 5171 SLOT1BIN(slot_nb_rshift, nb_rshift, "__rshift__", "__rrshift__") 5172 SLOT1BIN(slot_nb_and, nb_and, "__and__", "__rand__") 5173 SLOT1BIN(slot_nb_xor, nb_xor, "__xor__", "__rxor__") 5174 SLOT1BIN(slot_nb_or, nb_or, "__or__", "__ror__") 5175 5176 static int 5177 slot_nb_coerce(PyObject **a, PyObject **b) 5178 { 5179 static PyObject *coerce_str; 5180 PyObject *self = *a, *other = *b; 5181 5182 if (self->ob_type->tp_as_number != NULL && 5183 self->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { 5184 PyObject *r; 5185 r = call_maybe( 5186 self, "__coerce__", &coerce_str, "(O)", other); 5187 if (r == NULL) 5188 return -1; 5189 if (r == Py_NotImplemented) { 5190 Py_DECREF(r); 5191 } 5192 else { 5193 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { 5194 PyErr_SetString(PyExc_TypeError, 5195 "__coerce__ didn't return a 2-tuple"); 5196 Py_DECREF(r); 5197 return -1; 5198 } 5199 *a = PyTuple_GET_ITEM(r, 0); 5200 Py_INCREF(*a); 5201 *b = PyTuple_GET_ITEM(r, 1); 5202 Py_INCREF(*b); 5203 Py_DECREF(r); 5204 return 0; 5205 } 5206 } 5207 if (other->ob_type->tp_as_number != NULL && 5208 other->ob_type->tp_as_number->nb_coerce == slot_nb_coerce) { 5209 PyObject *r; 5210 r = call_maybe( 5211 other, "__coerce__", &coerce_str, "(O)", self); 5212 if (r == NULL) 5213 return -1; 5214 if (r == Py_NotImplemented) { 5215 Py_DECREF(r); 5216 return 1; 5217 } 5218 if (!PyTuple_Check(r) || PyTuple_GET_SIZE(r) != 2) { 5219 PyErr_SetString(PyExc_TypeError, 5220 "__coerce__ didn't return a 2-tuple"); 5221 Py_DECREF(r); 5222 return -1; 5223 } 5224 *a = PyTuple_GET_ITEM(r, 1); 5225 Py_INCREF(*a); 5226 *b = PyTuple_GET_ITEM(r, 0); 5227 Py_INCREF(*b); 5228 Py_DECREF(r); 5229 return 0; 5230 } 5231 return 1; 5232 } 5233 5234 SLOT0(slot_nb_int, "__int__") 5235 SLOT0(slot_nb_long, "__long__") 5236 SLOT0(slot_nb_float, "__float__") 5237 SLOT0(slot_nb_oct, "__oct__") 5238 SLOT0(slot_nb_hex, "__hex__") 5239 SLOT1(slot_nb_inplace_add, "__iadd__", PyObject *, "O") 5240 SLOT1(slot_nb_inplace_subtract, "__isub__", PyObject *, "O") 5241 SLOT1(slot_nb_inplace_multiply, "__imul__", PyObject *, "O") 5242 SLOT1(slot_nb_inplace_divide, "__idiv__", PyObject *, "O") 5243 SLOT1(slot_nb_inplace_remainder, "__imod__", PyObject *, "O") 5244 /* Can't use SLOT1 here, because nb_inplace_power is ternary */ 5245 static PyObject * 5246 slot_nb_inplace_power(PyObject *self, PyObject * arg1, PyObject *arg2) 5247 { 5248 static PyObject *cache_str; 5249 return call_method(self, "__ipow__", &cache_str, "(" "O" ")", arg1); 5250 } 5251 SLOT1(slot_nb_inplace_lshift, "__ilshift__", PyObject *, "O") 5252 SLOT1(slot_nb_inplace_rshift, "__irshift__", PyObject *, "O") 5253 SLOT1(slot_nb_inplace_and, "__iand__", PyObject *, "O") 5254 SLOT1(slot_nb_inplace_xor, "__ixor__", PyObject *, "O") 5255 SLOT1(slot_nb_inplace_or, "__ior__", PyObject *, "O") 5256 SLOT1BIN(slot_nb_floor_divide, nb_floor_divide, 5257 "__floordiv__", "__rfloordiv__") 5258 SLOT1BIN(slot_nb_true_divide, nb_true_divide, "__truediv__", "__rtruediv__") 5259 SLOT1(slot_nb_inplace_floor_divide, "__ifloordiv__", PyObject *, "O") 5260 SLOT1(slot_nb_inplace_true_divide, "__itruediv__", PyObject *, "O") 5261 5262 static int 5263 half_compare(PyObject *self, PyObject *other) 5264 { 5265 PyObject *func, *args, *res; 5266 static PyObject *cmp_str; 5267 Py_ssize_t c; 5268 5269 func = lookup_method(self, "__cmp__", &cmp_str); 5270 if (func == NULL) { 5271 PyErr_Clear(); 5272 } 5273 else { 5274 args = PyTuple_Pack(1, other); 5275 if (args == NULL) 5276 res = NULL; 5277 else { 5278 res = PyObject_Call(func, args, NULL); 5279 Py_DECREF(args); 5280 } 5281 Py_DECREF(func); 5282 if (res != Py_NotImplemented) { 5283 if (res == NULL) 5284 return -2; 5285 c = PyInt_AsLong(res); 5286 Py_DECREF(res); 5287 if (c == -1 && PyErr_Occurred()) 5288 return -2; 5289 return (c < 0) ? -1 : (c > 0) ? 1 : 0; 5290 } 5291 Py_DECREF(res); 5292 } 5293 return 2; 5294 } 5295 5296 /* This slot is published for the benefit of try_3way_compare in object.c */ 5297 int 5298 _PyObject_SlotCompare(PyObject *self, PyObject *other) 5299 { 5300 int c; 5301 5302 if (Py_TYPE(self)->tp_compare == _PyObject_SlotCompare) { 5303 c = half_compare(self, other); 5304 if (c <= 1) 5305 return c; 5306 } 5307 if (Py_TYPE(other)->tp_compare == _PyObject_SlotCompare) { 5308 c = half_compare(other, self); 5309 if (c < -1) 5310 return -2; 5311 if (c <= 1) 5312 return -c; 5313 } 5314 return (void *)self < (void *)other ? -1 : 5315 (void *)self > (void *)other ? 1 : 0; 5316 } 5317 5318 static PyObject * 5319 slot_tp_repr(PyObject *self) 5320 { 5321 PyObject *func, *res; 5322 static PyObject *repr_str; 5323 5324 func = lookup_method(self, "__repr__", &repr_str); 5325 if (func != NULL) { 5326 res = PyEval_CallObject(func, NULL); 5327 Py_DECREF(func); 5328 return res; 5329 } 5330 PyErr_Clear(); 5331 return PyString_FromFormat("<%s object at %p>", 5332 Py_TYPE(self)->tp_name, self); 5333 } 5334 5335 static PyObject * 5336 slot_tp_str(PyObject *self) 5337 { 5338 PyObject *func, *res; 5339 static PyObject *str_str; 5340 5341 func = lookup_method(self, "__str__", &str_str); 5342 if (func != NULL) { 5343 res = PyEval_CallObject(func, NULL); 5344 Py_DECREF(func); 5345 return res; 5346 } 5347 else { 5348 PyErr_Clear(); 5349 return slot_tp_repr(self); 5350 } 5351 } 5352 5353 static long 5354 slot_tp_hash(PyObject *self) 5355 { 5356 PyObject *func; 5357 static PyObject *hash_str, *eq_str, *cmp_str; 5358 long h; 5359 5360 func = lookup_method(self, "__hash__", &hash_str); 5361 5362 if (func != NULL && func != Py_None) { 5363 PyObject *res = PyEval_CallObject(func, NULL); 5364 Py_DECREF(func); 5365 if (res == NULL) 5366 return -1; 5367 if (PyLong_Check(res)) 5368 h = PyLong_Type.tp_hash(res); 5369 else 5370 h = PyInt_AsLong(res); 5371 Py_DECREF(res); 5372 } 5373 else { 5374 Py_XDECREF(func); /* may be None */ 5375 PyErr_Clear(); 5376 func = lookup_method(self, "__eq__", &eq_str); 5377 if (func == NULL) { 5378 PyErr_Clear(); 5379 func = lookup_method(self, "__cmp__", &cmp_str); 5380 } 5381 if (func != NULL) { 5382 Py_DECREF(func); 5383 return PyObject_HashNotImplemented(self); 5384 } 5385 PyErr_Clear(); 5386 h = _Py_HashPointer((void *)self); 5387 } 5388 if (h == -1 && !PyErr_Occurred()) 5389 h = -2; 5390 return h; 5391 } 5392 5393 static PyObject * 5394 slot_tp_call(PyObject *self, PyObject *args, PyObject *kwds) 5395 { 5396 static PyObject *call_str; 5397 PyObject *meth = lookup_method(self, "__call__", &call_str); 5398 PyObject *res; 5399 5400 if (meth == NULL) 5401 return NULL; 5402 5403 res = PyObject_Call(meth, args, kwds); 5404 5405 Py_DECREF(meth); 5406 return res; 5407 } 5408 5409 /* There are two slot dispatch functions for tp_getattro. 5410 5411 - slot_tp_getattro() is used when __getattribute__ is overridden 5412 but no __getattr__ hook is present; 5413 5414 - slot_tp_getattr_hook() is used when a __getattr__ hook is present. 5415 5416 The code in update_one_slot() always installs slot_tp_getattr_hook(); this 5417 detects the absence of __getattr__ and then installs the simpler slot if 5418 necessary. */ 5419 5420 static PyObject * 5421 slot_tp_getattro(PyObject *self, PyObject *name) 5422 { 5423 static PyObject *getattribute_str = NULL; 5424 return call_method(self, "__getattribute__", &getattribute_str, 5425 "(O)", name); 5426 } 5427 5428 static PyObject * 5429 call_attribute(PyObject *self, PyObject *attr, PyObject *name) 5430 { 5431 PyObject *res, *descr = NULL; 5432 descrgetfunc f = Py_TYPE(attr)->tp_descr_get; 5433 5434 if (f != NULL) { 5435 descr = f(attr, self, (PyObject *)(Py_TYPE(self))); 5436 if (descr == NULL) 5437 return NULL; 5438 else 5439 attr = descr; 5440 } 5441 res = PyObject_CallFunctionObjArgs(attr, name, NULL); 5442 Py_XDECREF(descr); 5443 return res; 5444 } 5445 5446 static PyObject * 5447 slot_tp_getattr_hook(PyObject *self, PyObject *name) 5448 { 5449 PyTypeObject *tp = Py_TYPE(self); 5450 PyObject *getattr, *getattribute, *res; 5451 static PyObject *getattribute_str = NULL; 5452 static PyObject *getattr_str = NULL; 5453 5454 if (getattr_str == NULL) { 5455 getattr_str = PyString_InternFromString("__getattr__"); 5456 if (getattr_str == NULL) 5457 return NULL; 5458 } 5459 if (getattribute_str == NULL) { 5460 getattribute_str = 5461 PyString_InternFromString("__getattribute__"); 5462 if (getattribute_str == NULL) 5463 return NULL; 5464 } 5465 /* speed hack: we could use lookup_maybe, but that would resolve the 5466 method fully for each attribute lookup for classes with 5467 __getattr__, even when the attribute is present. So we use 5468 _PyType_Lookup and create the method only when needed, with 5469 call_attribute. */ 5470 getattr = _PyType_Lookup(tp, getattr_str); 5471 if (getattr == NULL) { 5472 /* No __getattr__ hook: use a simpler dispatcher */ 5473 tp->tp_getattro = slot_tp_getattro; 5474 return slot_tp_getattro(self, name); 5475 } 5476 Py_INCREF(getattr); 5477 /* speed hack: we could use lookup_maybe, but that would resolve the 5478 method fully for each attribute lookup for classes with 5479 __getattr__, even when self has the default __getattribute__ 5480 method. So we use _PyType_Lookup and create the method only when 5481 needed, with call_attribute. */ 5482 getattribute = _PyType_Lookup(tp, getattribute_str); 5483 if (getattribute == NULL || 5484 (Py_TYPE(getattribute) == &PyWrapperDescr_Type && 5485 ((PyWrapperDescrObject *)getattribute)->d_wrapped == 5486 (void *)PyObject_GenericGetAttr)) 5487 res = PyObject_GenericGetAttr(self, name); 5488 else { 5489 Py_INCREF(getattribute); 5490 res = call_attribute(self, getattribute, name); 5491 Py_DECREF(getattribute); 5492 } 5493 if (res == NULL && PyErr_ExceptionMatches(PyExc_AttributeError)) { 5494 PyErr_Clear(); 5495 res = call_attribute(self, getattr, name); 5496 } 5497 Py_DECREF(getattr); 5498 return res; 5499 } 5500 5501 static int 5502 slot_tp_setattro(PyObject *self, PyObject *name, PyObject *value) 5503 { 5504 PyObject *res; 5505 static PyObject *delattr_str, *setattr_str; 5506 5507 if (value == NULL) 5508 res = call_method(self, "__delattr__", &delattr_str, 5509 "(O)", name); 5510 else 5511 res = call_method(self, "__setattr__", &setattr_str, 5512 "(OO)", name, value); 5513 if (res == NULL) 5514 return -1; 5515 Py_DECREF(res); 5516 return 0; 5517 } 5518 5519 static char *name_op[] = { 5520 "__lt__", 5521 "__le__", 5522 "__eq__", 5523 "__ne__", 5524 "__gt__", 5525 "__ge__", 5526 }; 5527 5528 static PyObject * 5529 half_richcompare(PyObject *self, PyObject *other, int op) 5530 { 5531 PyObject *func, *args, *res; 5532 static PyObject *op_str[6]; 5533 5534 func = lookup_method(self, name_op[op], &op_str[op]); 5535 if (func == NULL) { 5536 PyErr_Clear(); 5537 Py_INCREF(Py_NotImplemented); 5538 return Py_NotImplemented; 5539 } 5540 args = PyTuple_Pack(1, other); 5541 if (args == NULL) 5542 res = NULL; 5543 else { 5544 res = PyObject_Call(func, args, NULL); 5545 Py_DECREF(args); 5546 } 5547 Py_DECREF(func); 5548 return res; 5549 } 5550 5551 static PyObject * 5552 slot_tp_richcompare(PyObject *self, PyObject *other, int op) 5553 { 5554 PyObject *res; 5555 5556 if (Py_TYPE(self)->tp_richcompare == slot_tp_richcompare) { 5557 res = half_richcompare(self, other, op); 5558 if (res != Py_NotImplemented) 5559 return res; 5560 Py_DECREF(res); 5561 } 5562 if (Py_TYPE(other)->tp_richcompare == slot_tp_richcompare) { 5563 res = half_richcompare(other, self, _Py_SwappedOp[op]); 5564 if (res != Py_NotImplemented) { 5565 return res; 5566 } 5567 Py_DECREF(res); 5568 } 5569 Py_INCREF(Py_NotImplemented); 5570 return Py_NotImplemented; 5571 } 5572 5573 static PyObject * 5574 slot_tp_iter(PyObject *self) 5575 { 5576 PyObject *func, *res; 5577 static PyObject *iter_str, *getitem_str; 5578 5579 func = lookup_method(self, "__iter__", &iter_str); 5580 if (func != NULL) { 5581 PyObject *args; 5582 args = res = PyTuple_New(0); 5583 if (args != NULL) { 5584 res = PyObject_Call(func, args, NULL); 5585 Py_DECREF(args); 5586 } 5587 Py_DECREF(func); 5588 return res; 5589 } 5590 PyErr_Clear(); 5591 func = lookup_method(self, "__getitem__", &getitem_str); 5592 if (func == NULL) { 5593 PyErr_Format(PyExc_TypeError, 5594 "'%.200s' object is not iterable", 5595 Py_TYPE(self)->tp_name); 5596 return NULL; 5597 } 5598 Py_DECREF(func); 5599 return PySeqIter_New(self); 5600 } 5601 5602 static PyObject * 5603 slot_tp_iternext(PyObject *self) 5604 { 5605 static PyObject *next_str; 5606 return call_method(self, "next", &next_str, "()"); 5607 } 5608 5609 static PyObject * 5610 slot_tp_descr_get(PyObject *self, PyObject *obj, PyObject *type) 5611 { 5612 PyTypeObject *tp = Py_TYPE(self); 5613 PyObject *get; 5614 static PyObject *get_str = NULL; 5615 5616 if (get_str == NULL) { 5617 get_str = PyString_InternFromString("__get__"); 5618 if (get_str == NULL) 5619 return NULL; 5620 } 5621 get = _PyType_Lookup(tp, get_str); 5622 if (get == NULL) { 5623 /* Avoid further slowdowns */ 5624 if (tp->tp_descr_get == slot_tp_descr_get) 5625 tp->tp_descr_get = NULL; 5626 Py_INCREF(self); 5627 return self; 5628 } 5629 if (obj == NULL) 5630 obj = Py_None; 5631 if (type == NULL) 5632 type = Py_None; 5633 return PyObject_CallFunctionObjArgs(get, self, obj, type, NULL); 5634 } 5635 5636 static int 5637 slot_tp_descr_set(PyObject *self, PyObject *target, PyObject *value) 5638 { 5639 PyObject *res; 5640 static PyObject *del_str, *set_str; 5641 5642 if (value == NULL) 5643 res = call_method(self, "__delete__", &del_str, 5644 "(O)", target); 5645 else 5646 res = call_method(self, "__set__", &set_str, 5647 "(OO)", target, value); 5648 if (res == NULL) 5649 return -1; 5650 Py_DECREF(res); 5651 return 0; 5652 } 5653 5654 static int 5655 slot_tp_init(PyObject *self, PyObject *args, PyObject *kwds) 5656 { 5657 static PyObject *init_str; 5658 PyObject *meth = lookup_method(self, "__init__", &init_str); 5659 PyObject *res; 5660 5661 if (meth == NULL) 5662 return -1; 5663 res = PyObject_Call(meth, args, kwds); 5664 Py_DECREF(meth); 5665 if (res == NULL) 5666 return -1; 5667 if (res != Py_None) { 5668 PyErr_Format(PyExc_TypeError, 5669 "__init__() should return None, not '%.200s'", 5670 Py_TYPE(res)->tp_name); 5671 Py_DECREF(res); 5672 return -1; 5673 } 5674 Py_DECREF(res); 5675 return 0; 5676 } 5677 5678 static PyObject * 5679 slot_tp_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 5680 { 5681 static PyObject *new_str; 5682 PyObject *func; 5683 PyObject *newargs, *x; 5684 Py_ssize_t i, n; 5685 5686 if (new_str == NULL) { 5687 new_str = PyString_InternFromString("__new__"); 5688 if (new_str == NULL) 5689 return NULL; 5690 } 5691 func = PyObject_GetAttr((PyObject *)type, new_str); 5692 if (func == NULL) 5693 return NULL; 5694 assert(PyTuple_Check(args)); 5695 n = PyTuple_GET_SIZE(args); 5696 newargs = PyTuple_New(n+1); 5697 if (newargs == NULL) 5698 return NULL; 5699 Py_INCREF(type); 5700 PyTuple_SET_ITEM(newargs, 0, (PyObject *)type); 5701 for (i = 0; i < n; i++) { 5702 x = PyTuple_GET_ITEM(args, i); 5703 Py_INCREF(x); 5704 PyTuple_SET_ITEM(newargs, i+1, x); 5705 } 5706 x = PyObject_Call(func, newargs, kwds); 5707 Py_DECREF(newargs); 5708 Py_DECREF(func); 5709 return x; 5710 } 5711 5712 static void 5713 slot_tp_del(PyObject *self) 5714 { 5715 static PyObject *del_str = NULL; 5716 PyObject *del, *res; 5717 PyObject *error_type, *error_value, *error_traceback; 5718 5719 /* Temporarily resurrect the object. */ 5720 assert(self->ob_refcnt == 0); 5721 self->ob_refcnt = 1; 5722 5723 /* Save the current exception, if any. */ 5724 PyErr_Fetch(&error_type, &error_value, &error_traceback); 5725 5726 /* Execute __del__ method, if any. */ 5727 del = lookup_maybe(self, "__del__", &del_str); 5728 if (del != NULL) { 5729 res = PyEval_CallObject(del, NULL); 5730 if (res == NULL) 5731 PyErr_WriteUnraisable(del); 5732 else 5733 Py_DECREF(res); 5734 Py_DECREF(del); 5735 } 5736 5737 /* Restore the saved exception. */ 5738 PyErr_Restore(error_type, error_value, error_traceback); 5739 5740 /* Undo the temporary resurrection; can't use DECREF here, it would 5741 * cause a recursive call. 5742 */ 5743 assert(self->ob_refcnt > 0); 5744 if (--self->ob_refcnt == 0) 5745 return; /* this is the normal path out */ 5746 5747 /* __del__ resurrected it! Make it look like the original Py_DECREF 5748 * never happened. 5749 */ 5750 { 5751 Py_ssize_t refcnt = self->ob_refcnt; 5752 _Py_NewReference(self); 5753 self->ob_refcnt = refcnt; 5754 } 5755 assert(!PyType_IS_GC(Py_TYPE(self)) || 5756 _Py_AS_GC(self)->gc.gc_refs != _PyGC_REFS_UNTRACKED); 5757 /* If Py_REF_DEBUG, _Py_NewReference bumped _Py_RefTotal, so 5758 * we need to undo that. */ 5759 _Py_DEC_REFTOTAL; 5760 /* If Py_TRACE_REFS, _Py_NewReference re-added self to the object 5761 * chain, so no more to do there. 5762 * If COUNT_ALLOCS, the original decref bumped tp_frees, and 5763 * _Py_NewReference bumped tp_allocs: both of those need to be 5764 * undone. 5765 */ 5766 #ifdef COUNT_ALLOCS 5767 --Py_TYPE(self)->tp_frees; 5768 --Py_TYPE(self)->tp_allocs; 5769 #endif 5770 } 5771 5772 5773 /* Table mapping __foo__ names to tp_foo offsets and slot_tp_foo wrapper 5774 functions. The offsets here are relative to the 'PyHeapTypeObject' 5775 structure, which incorporates the additional structures used for numbers, 5776 sequences and mappings. 5777 Note that multiple names may map to the same slot (e.g. __eq__, 5778 __ne__ etc. all map to tp_richcompare) and one name may map to multiple 5779 slots (e.g. __str__ affects tp_str as well as tp_repr). The table is 5780 terminated with an all-zero entry. (This table is further initialized and 5781 sorted in init_slotdefs() below.) */ 5782 5783 typedef struct wrapperbase slotdef; 5784 5785 #undef TPSLOT 5786 #undef FLSLOT 5787 #undef ETSLOT 5788 #undef SQSLOT 5789 #undef MPSLOT 5790 #undef NBSLOT 5791 #undef UNSLOT 5792 #undef IBSLOT 5793 #undef BINSLOT 5794 #undef RBINSLOT 5795 5796 #define TPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5797 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 5798 PyDoc_STR(DOC)} 5799 #define FLSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC, FLAGS) \ 5800 {NAME, offsetof(PyTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 5801 PyDoc_STR(DOC), FLAGS} 5802 #define ETSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5803 {NAME, offsetof(PyHeapTypeObject, SLOT), (void *)(FUNCTION), WRAPPER, \ 5804 PyDoc_STR(DOC)} 5805 #define SQSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5806 ETSLOT(NAME, as_sequence.SLOT, FUNCTION, WRAPPER, DOC) 5807 #define MPSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5808 ETSLOT(NAME, as_mapping.SLOT, FUNCTION, WRAPPER, DOC) 5809 #define NBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5810 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, DOC) 5811 #define UNSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5812 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ 5813 "x." NAME "() <==> " DOC) 5814 #define IBSLOT(NAME, SLOT, FUNCTION, WRAPPER, DOC) \ 5815 ETSLOT(NAME, as_number.SLOT, FUNCTION, WRAPPER, \ 5816 "x." NAME "(y) <==> x" DOC "y") 5817 #define BINSLOT(NAME, SLOT, FUNCTION, DOC) \ 5818 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ 5819 "x." NAME "(y) <==> x" DOC "y") 5820 #define RBINSLOT(NAME, SLOT, FUNCTION, DOC) \ 5821 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ 5822 "x." NAME "(y) <==> y" DOC "x") 5823 #define BINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ 5824 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_l, \ 5825 "x." NAME "(y) <==> " DOC) 5826 #define RBINSLOTNOTINFIX(NAME, SLOT, FUNCTION, DOC) \ 5827 ETSLOT(NAME, as_number.SLOT, FUNCTION, wrap_binaryfunc_r, \ 5828 "x." NAME "(y) <==> " DOC) 5829 5830 static slotdef slotdefs[] = { 5831 SQSLOT("__len__", sq_length, slot_sq_length, wrap_lenfunc, 5832 "x.__len__() <==> len(x)"), 5833 /* Heap types defining __add__/__mul__ have sq_concat/sq_repeat == NULL. 5834 The logic in abstract.c always falls back to nb_add/nb_multiply in 5835 this case. Defining both the nb_* and the sq_* slots to call the 5836 user-defined methods has unexpected side-effects, as shown by 5837 test_descr.notimplemented() */ 5838 SQSLOT("__add__", sq_concat, NULL, wrap_binaryfunc, 5839 "x.__add__(y) <==> x+y"), 5840 SQSLOT("__mul__", sq_repeat, NULL, wrap_indexargfunc, 5841 "x.__mul__(n) <==> x*n"), 5842 SQSLOT("__rmul__", sq_repeat, NULL, wrap_indexargfunc, 5843 "x.__rmul__(n) <==> n*x"), 5844 SQSLOT("__getitem__", sq_item, slot_sq_item, wrap_sq_item, 5845 "x.__getitem__(y) <==> x[y]"), 5846 SQSLOT("__getslice__", sq_slice, slot_sq_slice, wrap_ssizessizeargfunc, 5847 "x.__getslice__(i, j) <==> x[i:j]\n\ 5848 \n\ 5849 Use of negative indices is not supported."), 5850 SQSLOT("__setitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_setitem, 5851 "x.__setitem__(i, y) <==> x[i]=y"), 5852 SQSLOT("__delitem__", sq_ass_item, slot_sq_ass_item, wrap_sq_delitem, 5853 "x.__delitem__(y) <==> del x[y]"), 5854 SQSLOT("__setslice__", sq_ass_slice, slot_sq_ass_slice, 5855 wrap_ssizessizeobjargproc, 5856 "x.__setslice__(i, j, y) <==> x[i:j]=y\n\ 5857 \n\ 5858 Use of negative indices is not supported."), 5859 SQSLOT("__delslice__", sq_ass_slice, slot_sq_ass_slice, wrap_delslice, 5860 "x.__delslice__(i, j) <==> del x[i:j]\n\ 5861 \n\ 5862 Use of negative indices is not supported."), 5863 SQSLOT("__contains__", sq_contains, slot_sq_contains, wrap_objobjproc, 5864 "x.__contains__(y) <==> y in x"), 5865 SQSLOT("__iadd__", sq_inplace_concat, NULL, 5866 wrap_binaryfunc, "x.__iadd__(y) <==> x+=y"), 5867 SQSLOT("__imul__", sq_inplace_repeat, NULL, 5868 wrap_indexargfunc, "x.__imul__(y) <==> x*=y"), 5869 5870 MPSLOT("__len__", mp_length, slot_mp_length, wrap_lenfunc, 5871 "x.__len__() <==> len(x)"), 5872 MPSLOT("__getitem__", mp_subscript, slot_mp_subscript, 5873 wrap_binaryfunc, 5874 "x.__getitem__(y) <==> x[y]"), 5875 MPSLOT("__setitem__", mp_ass_subscript, slot_mp_ass_subscript, 5876 wrap_objobjargproc, 5877 "x.__setitem__(i, y) <==> x[i]=y"), 5878 MPSLOT("__delitem__", mp_ass_subscript, slot_mp_ass_subscript, 5879 wrap_delitem, 5880 "x.__delitem__(y) <==> del x[y]"), 5881 5882 BINSLOT("__add__", nb_add, slot_nb_add, 5883 "+"), 5884 RBINSLOT("__radd__", nb_add, slot_nb_add, 5885 "+"), 5886 BINSLOT("__sub__", nb_subtract, slot_nb_subtract, 5887 "-"), 5888 RBINSLOT("__rsub__", nb_subtract, slot_nb_subtract, 5889 "-"), 5890 BINSLOT("__mul__", nb_multiply, slot_nb_multiply, 5891 "*"), 5892 RBINSLOT("__rmul__", nb_multiply, slot_nb_multiply, 5893 "*"), 5894 BINSLOT("__div__", nb_divide, slot_nb_divide, 5895 "/"), 5896 RBINSLOT("__rdiv__", nb_divide, slot_nb_divide, 5897 "/"), 5898 BINSLOT("__mod__", nb_remainder, slot_nb_remainder, 5899 "%"), 5900 RBINSLOT("__rmod__", nb_remainder, slot_nb_remainder, 5901 "%"), 5902 BINSLOTNOTINFIX("__divmod__", nb_divmod, slot_nb_divmod, 5903 "divmod(x, y)"), 5904 RBINSLOTNOTINFIX("__rdivmod__", nb_divmod, slot_nb_divmod, 5905 "divmod(y, x)"), 5906 NBSLOT("__pow__", nb_power, slot_nb_power, wrap_ternaryfunc, 5907 "x.__pow__(y[, z]) <==> pow(x, y[, z])"), 5908 NBSLOT("__rpow__", nb_power, slot_nb_power, wrap_ternaryfunc_r, 5909 "y.__rpow__(x[, z]) <==> pow(x, y[, z])"), 5910 UNSLOT("__neg__", nb_negative, slot_nb_negative, wrap_unaryfunc, "-x"), 5911 UNSLOT("__pos__", nb_positive, slot_nb_positive, wrap_unaryfunc, "+x"), 5912 UNSLOT("__abs__", nb_absolute, slot_nb_absolute, wrap_unaryfunc, 5913 "abs(x)"), 5914 UNSLOT("__nonzero__", nb_nonzero, slot_nb_nonzero, wrap_inquirypred, 5915 "x != 0"), 5916 UNSLOT("__invert__", nb_invert, slot_nb_invert, wrap_unaryfunc, "~x"), 5917 BINSLOT("__lshift__", nb_lshift, slot_nb_lshift, "<<"), 5918 RBINSLOT("__rlshift__", nb_lshift, slot_nb_lshift, "<<"), 5919 BINSLOT("__rshift__", nb_rshift, slot_nb_rshift, ">>"), 5920 RBINSLOT("__rrshift__", nb_rshift, slot_nb_rshift, ">>"), 5921 BINSLOT("__and__", nb_and, slot_nb_and, "&"), 5922 RBINSLOT("__rand__", nb_and, slot_nb_and, "&"), 5923 BINSLOT("__xor__", nb_xor, slot_nb_xor, "^"), 5924 RBINSLOT("__rxor__", nb_xor, slot_nb_xor, "^"), 5925 BINSLOT("__or__", nb_or, slot_nb_or, "|"), 5926 RBINSLOT("__ror__", nb_or, slot_nb_or, "|"), 5927 NBSLOT("__coerce__", nb_coerce, slot_nb_coerce, wrap_coercefunc, 5928 "x.__coerce__(y) <==> coerce(x, y)"), 5929 UNSLOT("__int__", nb_int, slot_nb_int, wrap_unaryfunc, 5930 "int(x)"), 5931 UNSLOT("__long__", nb_long, slot_nb_long, wrap_unaryfunc, 5932 "long(x)"), 5933 UNSLOT("__float__", nb_float, slot_nb_float, wrap_unaryfunc, 5934 "float(x)"), 5935 UNSLOT("__oct__", nb_oct, slot_nb_oct, wrap_unaryfunc, 5936 "oct(x)"), 5937 UNSLOT("__hex__", nb_hex, slot_nb_hex, wrap_unaryfunc, 5938 "hex(x)"), 5939 NBSLOT("__index__", nb_index, slot_nb_index, wrap_unaryfunc, 5940 "x[y:z] <==> x[y.__index__():z.__index__()]"), 5941 IBSLOT("__iadd__", nb_inplace_add, slot_nb_inplace_add, 5942 wrap_binaryfunc, "+="), 5943 IBSLOT("__isub__", nb_inplace_subtract, slot_nb_inplace_subtract, 5944 wrap_binaryfunc, "-="), 5945 IBSLOT("__imul__", nb_inplace_multiply, slot_nb_inplace_multiply, 5946 wrap_binaryfunc, "*="), 5947 IBSLOT("__idiv__", nb_inplace_divide, slot_nb_inplace_divide, 5948 wrap_binaryfunc, "/="), 5949 IBSLOT("__imod__", nb_inplace_remainder, slot_nb_inplace_remainder, 5950 wrap_binaryfunc, "%="), 5951 IBSLOT("__ipow__", nb_inplace_power, slot_nb_inplace_power, 5952 wrap_binaryfunc, "**="), 5953 IBSLOT("__ilshift__", nb_inplace_lshift, slot_nb_inplace_lshift, 5954 wrap_binaryfunc, "<<="), 5955 IBSLOT("__irshift__", nb_inplace_rshift, slot_nb_inplace_rshift, 5956 wrap_binaryfunc, ">>="), 5957 IBSLOT("__iand__", nb_inplace_and, slot_nb_inplace_and, 5958 wrap_binaryfunc, "&="), 5959 IBSLOT("__ixor__", nb_inplace_xor, slot_nb_inplace_xor, 5960 wrap_binaryfunc, "^="), 5961 IBSLOT("__ior__", nb_inplace_or, slot_nb_inplace_or, 5962 wrap_binaryfunc, "|="), 5963 BINSLOT("__floordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), 5964 RBINSLOT("__rfloordiv__", nb_floor_divide, slot_nb_floor_divide, "//"), 5965 BINSLOT("__truediv__", nb_true_divide, slot_nb_true_divide, "/"), 5966 RBINSLOT("__rtruediv__", nb_true_divide, slot_nb_true_divide, "/"), 5967 IBSLOT("__ifloordiv__", nb_inplace_floor_divide, 5968 slot_nb_inplace_floor_divide, wrap_binaryfunc, "//"), 5969 IBSLOT("__itruediv__", nb_inplace_true_divide, 5970 slot_nb_inplace_true_divide, wrap_binaryfunc, "/"), 5971 5972 TPSLOT("__str__", tp_str, slot_tp_str, wrap_unaryfunc, 5973 "x.__str__() <==> str(x)"), 5974 TPSLOT("__str__", tp_print, NULL, NULL, ""), 5975 TPSLOT("__repr__", tp_repr, slot_tp_repr, wrap_unaryfunc, 5976 "x.__repr__() <==> repr(x)"), 5977 TPSLOT("__repr__", tp_print, NULL, NULL, ""), 5978 TPSLOT("__cmp__", tp_compare, _PyObject_SlotCompare, wrap_cmpfunc, 5979 "x.__cmp__(y) <==> cmp(x,y)"), 5980 TPSLOT("__hash__", tp_hash, slot_tp_hash, wrap_hashfunc, 5981 "x.__hash__() <==> hash(x)"), 5982 FLSLOT("__call__", tp_call, slot_tp_call, (wrapperfunc)wrap_call, 5983 "x.__call__(...) <==> x(...)", PyWrapperFlag_KEYWORDS), 5984 TPSLOT("__getattribute__", tp_getattro, slot_tp_getattr_hook, 5985 wrap_binaryfunc, "x.__getattribute__('name') <==> x.name"), 5986 TPSLOT("__getattribute__", tp_getattr, NULL, NULL, ""), 5987 TPSLOT("__getattr__", tp_getattro, slot_tp_getattr_hook, NULL, ""), 5988 TPSLOT("__getattr__", tp_getattr, NULL, NULL, ""), 5989 TPSLOT("__setattr__", tp_setattro, slot_tp_setattro, wrap_setattr, 5990 "x.__setattr__('name', value) <==> x.name = value"), 5991 TPSLOT("__setattr__", tp_setattr, NULL, NULL, ""), 5992 TPSLOT("__delattr__", tp_setattro, slot_tp_setattro, wrap_delattr, 5993 "x.__delattr__('name') <==> del x.name"), 5994 TPSLOT("__delattr__", tp_setattr, NULL, NULL, ""), 5995 TPSLOT("__lt__", tp_richcompare, slot_tp_richcompare, richcmp_lt, 5996 "x.__lt__(y) <==> x<y"), 5997 TPSLOT("__le__", tp_richcompare, slot_tp_richcompare, richcmp_le, 5998 "x.__le__(y) <==> x<=y"), 5999 TPSLOT("__eq__", tp_richcompare, slot_tp_richcompare, richcmp_eq, 6000 "x.__eq__(y) <==> x==y"), 6001 TPSLOT("__ne__", tp_richcompare, slot_tp_richcompare, richcmp_ne, 6002 "x.__ne__(y) <==> x!=y"), 6003 TPSLOT("__gt__", tp_richcompare, slot_tp_richcompare, richcmp_gt, 6004 "x.__gt__(y) <==> x>y"), 6005 TPSLOT("__ge__", tp_richcompare, slot_tp_richcompare, richcmp_ge, 6006 "x.__ge__(y) <==> x>=y"), 6007 TPSLOT("__iter__", tp_iter, slot_tp_iter, wrap_unaryfunc, 6008 "x.__iter__() <==> iter(x)"), 6009 TPSLOT("next", tp_iternext, slot_tp_iternext, wrap_next, 6010 "x.next() -> the next value, or raise StopIteration"), 6011 TPSLOT("__get__", tp_descr_get, slot_tp_descr_get, wrap_descr_get, 6012 "descr.__get__(obj[, type]) -> value"), 6013 TPSLOT("__set__", tp_descr_set, slot_tp_descr_set, wrap_descr_set, 6014 "descr.__set__(obj, value)"), 6015 TPSLOT("__delete__", tp_descr_set, slot_tp_descr_set, 6016 wrap_descr_delete, "descr.__delete__(obj)"), 6017 FLSLOT("__init__", tp_init, slot_tp_init, (wrapperfunc)wrap_init, 6018 "x.__init__(...) initializes x; " 6019 "see help(type(x)) for signature", 6020 PyWrapperFlag_KEYWORDS), 6021 TPSLOT("__new__", tp_new, slot_tp_new, NULL, ""), 6022 TPSLOT("__del__", tp_del, slot_tp_del, NULL, ""), 6023 {NULL} 6024 }; 6025 6026 /* Given a type pointer and an offset gotten from a slotdef entry, return a 6027 pointer to the actual slot. This is not quite the same as simply adding 6028 the offset to the type pointer, since it takes care to indirect through the 6029 proper indirection pointer (as_buffer, etc.); it returns NULL if the 6030 indirection pointer is NULL. */ 6031 static void ** 6032 slotptr(PyTypeObject *type, int ioffset) 6033 { 6034 char *ptr; 6035 long offset = ioffset; 6036 6037 /* Note: this depends on the order of the members of PyHeapTypeObject! */ 6038 assert(offset >= 0); 6039 assert((size_t)offset < offsetof(PyHeapTypeObject, as_buffer)); 6040 if ((size_t)offset >= offsetof(PyHeapTypeObject, as_sequence)) { 6041 ptr = (char *)type->tp_as_sequence;
Access to field 'tp_as_sequence' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

6042 offset -= offsetof(PyHeapTypeObject, as_sequence); 6043 } 6044 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_mapping)) { 6045 ptr = (char *)type->tp_as_mapping;
Access to field 'tp_as_mapping' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

6046 offset -= offsetof(PyHeapTypeObject, as_mapping); 6047 } 6048 else if ((size_t)offset >= offsetof(PyHeapTypeObject, as_number)) { 6049 ptr = (char *)type->tp_as_number;
Access to field 'tp_as_number' results in a dereference of a null pointer (loaded from variable 'type')
(emitted by clang-analyzer)

TODO: a detailed trace is available in the data model (not yet rendered in this report)

6050 offset -= offsetof(PyHeapTypeObject, as_number); 6051 } 6052 else { 6053 ptr = (char *)type; 6054 } 6055 if (ptr != NULL) 6056 ptr += offset; 6057 return (void **)ptr; 6058 } 6059 6060 /* Length of array of slotdef pointers used to store slots with the 6061 same __name__. There should be at most MAX_EQUIV-1 slotdef entries with 6062 the same __name__, for any __name__. Since that's a static property, it is 6063 appropriate to declare fixed-size arrays for this. */ 6064 #define MAX_EQUIV 10 6065 6066 /* Return a slot pointer for a given name, but ONLY if the attribute has 6067 exactly one slot function. The name must be an interned string. */ 6068 static void ** 6069 resolve_slotdups(PyTypeObject *type, PyObject *name) 6070 { 6071 /* XXX Maybe this could be optimized more -- but is it worth it? */ 6072 6073 /* pname and ptrs act as a little cache */ 6074 static PyObject *pname; 6075 static slotdef *ptrs[MAX_EQUIV]; 6076 slotdef *p, **pp; 6077 void **res, **ptr; 6078 6079 if (pname != name) { 6080 /* Collect all slotdefs that match name into ptrs. */ 6081 pname = name; 6082 pp = ptrs; 6083 for (p = slotdefs; p->name_strobj; p++) { 6084 if (p->name_strobj == name) 6085 *pp++ = p; 6086 } 6087 *pp = NULL; 6088 } 6089 6090 /* Look in all matching slots of the type; if exactly one of these has 6091 a filled-in slot, return its value. Otherwise return NULL. */ 6092 res = NULL; 6093 for (pp = ptrs; *pp; pp++) { 6094 ptr = slotptr(type, (*pp)->offset); 6095 if (ptr == NULL || *ptr == NULL) 6096 continue; 6097 if (res != NULL) 6098 return NULL; 6099 res = ptr; 6100 } 6101 return res; 6102 } 6103 6104 /* Common code for update_slots_callback() and fixup_slot_dispatchers(). This 6105 does some incredibly complex thinking and then sticks something into the 6106 slot. (It sees if the adjacent slotdefs for the same slot have conflicting 6107 interests, and then stores a generic wrapper or a specific function into 6108 the slot.) Return a pointer to the next slotdef with a different offset, 6109 because that's convenient for fixup_slot_dispatchers(). */ 6110 static slotdef * 6111 update_one_slot(PyTypeObject *type, slotdef *p) 6112 { 6113 PyObject *descr; 6114 PyWrapperDescrObject *d; 6115 void *generic = NULL, *specific = NULL; 6116 int use_generic = 0; 6117 int offset = p->offset; 6118 void **ptr = slotptr(type, offset); 6119 6120 if (ptr == NULL) { 6121 do { 6122 ++p; 6123 } while (p->offset == offset); 6124 return p; 6125 } 6126 do { 6127 descr = _PyType_Lookup(type, p->name_strobj); 6128 if (descr == NULL) { 6129 if (ptr == (void**)&type->tp_iternext) { 6130 specific = _PyObject_NextNotImplemented; 6131 } 6132 continue; 6133 } 6134 if (Py_TYPE(descr) == &PyWrapperDescr_Type) { 6135 void **tptr = resolve_slotdups(type, p->name_strobj); 6136 if (tptr == NULL || tptr == ptr) 6137 generic = p->function; 6138 d = (PyWrapperDescrObject *)descr; 6139 if (d->d_base->wrapper == p->wrapper && 6140 PyType_IsSubtype(type, d->d_type)) 6141 { 6142 if (specific == NULL || 6143 specific == d->d_wrapped) 6144 specific = d->d_wrapped; 6145 else 6146 use_generic = 1; 6147 } 6148 } 6149 else if (Py_TYPE(descr) == &PyCFunction_Type && 6150 PyCFunction_GET_FUNCTION(descr) == 6151 (PyCFunction)tp_new_wrapper && 6152 ptr == (void**)&type->tp_new) 6153 { 6154 /* The __new__ wrapper is not a wrapper descriptor, 6155 so must be special-cased differently. 6156 If we don't do this, creating an instance will 6157 always use slot_tp_new which will look up 6158 __new__ in the MRO which will call tp_new_wrapper 6159 which will look through the base classes looking 6160 for a static base and call its tp_new (usually 6161 PyType_GenericNew), after performing various 6162 sanity checks and constructing a new argument 6163 list. Cut all that nonsense short -- this speeds 6164 up instance creation tremendously. */ 6165 specific = (void *)type->tp_new; 6166 /* XXX I'm not 100% sure that there isn't a hole 6167 in this reasoning that requires additional 6168 sanity checks. I'll buy the first person to 6169 point out a bug in this reasoning a beer. */ 6170 } 6171 else if (descr == Py_None && 6172 ptr == (void**)&type->tp_hash) { 6173 /* We specifically allow __hash__ to be set to None 6174 to prevent inheritance of the default 6175 implementation from object.__hash__ */ 6176 specific = PyObject_HashNotImplemented; 6177 } 6178 else { 6179 use_generic = 1; 6180 generic = p->function; 6181 } 6182 } while ((++p)->offset == offset); 6183 if (specific && !use_generic) 6184 *ptr = specific; 6185 else 6186 *ptr = generic; 6187 return p; 6188 } 6189 6190 /* In the type, update the slots whose slotdefs are gathered in the pp array. 6191 This is a callback for update_subclasses(). */ 6192 static int 6193 update_slots_callback(PyTypeObject *type, void *data) 6194 { 6195 slotdef **pp = (slotdef **)data; 6196 6197 for (; *pp; pp++) 6198 update_one_slot(type, *pp); 6199 return 0; 6200 } 6201 6202 /* Comparison function for qsort() to compare slotdefs by their offset, and 6203 for equal offset by their address (to force a stable sort). */ 6204 static int 6205 slotdef_cmp(const void *aa, const void *bb) 6206 { 6207 const slotdef *a = (const slotdef *)aa, *b = (const slotdef *)bb; 6208 int c = a->offset - b->offset; 6209 if (c != 0) 6210 return c; 6211 else 6212 /* Cannot use a-b, as this gives off_t, 6213 which may lose precision when converted to int. */ 6214 return (a > b) ? 1 : (a < b) ? -1 : 0; 6215 } 6216 6217 /* Initialize the slotdefs table by adding interned string objects for the 6218 names and sorting the entries. */ 6219 static void 6220 init_slotdefs(void) 6221 { 6222 slotdef *p; 6223 static int initialized = 0; 6224 6225 if (initialized) 6226 return; 6227 for (p = slotdefs; p->name; p++) { 6228 p->name_strobj = PyString_InternFromString(p->name); 6229 if (!p->name_strobj) 6230 Py_FatalError("Out of memory interning slotdef names"); 6231 } 6232 qsort((void *)slotdefs, (size_t)(p-slotdefs), sizeof(slotdef), 6233 slotdef_cmp); 6234 initialized = 1; 6235 } 6236 6237 /* Update the slots after assignment to a class (type) attribute. */ 6238 static int 6239 update_slot(PyTypeObject *type, PyObject *name) 6240 { 6241 slotdef *ptrs[MAX_EQUIV]; 6242 slotdef *p; 6243 slotdef **pp; 6244 int offset; 6245 6246 /* Clear the VALID_VERSION flag of 'type' and all its 6247 subclasses. This could possibly be unified with the 6248 update_subclasses() recursion below, but carefully: 6249 they each have their own conditions on which to stop 6250 recursing into subclasses. */ 6251 PyType_Modified(type); 6252 6253 init_slotdefs(); 6254 pp = ptrs; 6255 for (p = slotdefs; p->name; p++) { 6256 /* XXX assume name is interned! */ 6257 if (p->name_strobj == name) 6258 *pp++ = p; 6259 } 6260 *pp = NULL; 6261 for (pp = ptrs; *pp; pp++) { 6262 p = *pp; 6263 offset = p->offset; 6264 while (p > slotdefs && (p-1)->offset == offset) 6265 --p; 6266 *pp = p; 6267 } 6268 if (ptrs[0] == NULL) 6269 return 0; /* Not an attribute that affects any slots */ 6270 return update_subclasses(type, name, 6271 update_slots_callback, (void *)ptrs); 6272 } 6273 6274 /* Store the proper functions in the slot dispatches at class (type) 6275 definition time, based upon which operations the class overrides in its 6276 dict. */ 6277 static void 6278 fixup_slot_dispatchers(PyTypeObject *type) 6279 { 6280 slotdef *p; 6281 6282 init_slotdefs(); 6283 for (p = slotdefs; p->name; ) 6284 p = update_one_slot(type, p); 6285 } 6286 6287 static void 6288 update_all_slots(PyTypeObject* type) 6289 { 6290 slotdef *p; 6291 6292 init_slotdefs(); 6293 for (p = slotdefs; p->name; p++) { 6294 /* update_slot returns int but can't actually fail */ 6295 update_slot(type, p->name_strobj); 6296 } 6297 } 6298 6299 /* recurse_down_subclasses() and update_subclasses() are mutually 6300 recursive functions to call a callback for all subclasses, 6301 but refraining from recursing into subclasses that define 'name'. */ 6302 6303 static int 6304 update_subclasses(PyTypeObject *type, PyObject *name, 6305 update_callback callback, void *data) 6306 { 6307 if (callback(type, data) < 0) 6308 return -1; 6309 return recurse_down_subclasses(type, name, callback, data); 6310 } 6311 6312 static int 6313 recurse_down_subclasses(PyTypeObject *type, PyObject *name, 6314 update_callback callback, void *data) 6315 { 6316 PyTypeObject *subclass; 6317 PyObject *ref, *subclasses, *dict; 6318 Py_ssize_t i, n; 6319 6320 subclasses = type->tp_subclasses; 6321 if (subclasses == NULL) 6322 return 0; 6323 assert(PyList_Check(subclasses)); 6324 n = PyList_GET_SIZE(subclasses); 6325 for (i = 0; i < n; i++) { 6326 ref = PyList_GET_ITEM(subclasses, i); 6327 assert(PyWeakref_CheckRef(ref)); 6328 subclass = (PyTypeObject *)PyWeakref_GET_OBJECT(ref); 6329 assert(subclass != NULL); 6330 if ((PyObject *)subclass == Py_None) 6331 continue; 6332 assert(PyType_Check(subclass)); 6333 /* Avoid recursing down into unaffected classes */ 6334 dict = subclass->tp_dict; 6335 if (dict != NULL && PyDict_Check(dict) && 6336 PyDict_GetItem(dict, name) != NULL) 6337 continue; 6338 if (update_subclasses(subclass, name, callback, data) < 0) 6339 return -1; 6340 } 6341 return 0; 6342 } 6343 6344 /* This function is called by PyType_Ready() to populate the type's 6345 dictionary with method descriptors for function slots. For each 6346 function slot (like tp_repr) that's defined in the type, one or more 6347 corresponding descriptors are added in the type's tp_dict dictionary 6348 under the appropriate name (like __repr__). Some function slots 6349 cause more than one descriptor to be added (for example, the nb_add 6350 slot adds both __add__ and __radd__ descriptors) and some function 6351 slots compete for the same descriptor (for example both sq_item and 6352 mp_subscript generate a __getitem__ descriptor). 6353 6354 In the latter case, the first slotdef entry encountered wins. Since 6355 slotdef entries are sorted by the offset of the slot in the 6356 PyHeapTypeObject, this gives us some control over disambiguating 6357 between competing slots: the members of PyHeapTypeObject are listed 6358 from most general to least general, so the most general slot is 6359 preferred. In particular, because as_mapping comes before as_sequence, 6360 for a type that defines both mp_subscript and sq_item, mp_subscript 6361 wins. 6362 6363 This only adds new descriptors and doesn't overwrite entries in 6364 tp_dict that were previously defined. The descriptors contain a 6365 reference to the C function they must call, so that it's safe if they 6366 are copied into a subtype's __dict__ and the subtype has a different 6367 C function in its slot -- calling the method defined by the 6368 descriptor will call the C function that was used to create it, 6369 rather than the C function present in the slot when it is called. 6370 (This is important because a subtype may have a C function in the 6371 slot that calls the method from the dictionary, and we want to avoid 6372 infinite recursion here.) */ 6373 6374 static int 6375 add_operators(PyTypeObject *type) 6376 { 6377 PyObject *dict = type->tp_dict; 6378 slotdef *p; 6379 PyObject *descr; 6380 void **ptr; 6381 6382 init_slotdefs(); 6383 for (p = slotdefs; p->name; p++) { 6384 if (p->wrapper == NULL) 6385 continue; 6386 ptr = slotptr(type, p->offset); 6387 if (!ptr || !*ptr) 6388 continue; 6389 if (PyDict_GetItem(dict, p->name_strobj)) 6390 continue; 6391 if (*ptr == PyObject_HashNotImplemented) { 6392 /* Classes may prevent the inheritance of the tp_hash 6393 slot by storing PyObject_HashNotImplemented in it. Make it 6394 visible as a None value for the __hash__ attribute. */ 6395 if (PyDict_SetItem(dict, p->name_strobj, Py_None) < 0) 6396 return -1; 6397 } 6398 else { 6399 descr = PyDescr_NewWrapper(type, p, *ptr); 6400 if (descr == NULL) 6401 return -1; 6402 if (PyDict_SetItem(dict, p->name_strobj, descr) < 0) 6403 return -1; 6404 Py_DECREF(descr); 6405 } 6406 } 6407 if (type->tp_new != NULL) { 6408 if (add_tp_new_wrapper(type) < 0) 6409 return -1; 6410 } 6411 return 0; 6412 } 6413 6414 6415 /* Cooperative 'super' */ 6416 6417 typedef struct { 6418 PyObject_HEAD 6419 PyTypeObject *type; 6420 PyObject *obj; 6421 PyTypeObject *obj_type; 6422 } superobject; 6423 6424 static PyMemberDef super_members[] = { 6425 {"__thisclass__", T_OBJECT, offsetof(superobject, type), READONLY, 6426 "the class invoking super()"}, 6427 {"__self__", T_OBJECT, offsetof(superobject, obj), READONLY, 6428 "the instance invoking super(); may be None"}, 6429 {"__self_class__", T_OBJECT, offsetof(superobject, obj_type), READONLY, 6430 "the type of the instance invoking super(); may be None"}, 6431 {0} 6432 }; 6433 6434 static void 6435 super_dealloc(PyObject *self) 6436 { 6437 superobject *su = (superobject *)self; 6438 6439 _PyObject_GC_UNTRACK(self); 6440 Py_XDECREF(su->obj); 6441 Py_XDECREF(su->type); 6442 Py_XDECREF(su->obj_type); 6443 Py_TYPE(self)->tp_free(self); 6444 } 6445 6446 static PyObject * 6447 super_repr(PyObject *self) 6448 { 6449 superobject *su = (superobject *)self; 6450 6451 if (su->obj_type) 6452 return PyString_FromFormat( 6453 "<super: <class '%s'>, <%s object>>", 6454 su->type ? su->type->tp_name : "NULL", 6455 su->obj_type->tp_name); 6456 else 6457 return PyString_FromFormat( 6458 "<super: <class '%s'>, NULL>", 6459 su->type ? su->type->tp_name : "NULL"); 6460 } 6461 6462 static PyObject * 6463 super_getattro(PyObject *self, PyObject *name) 6464 { 6465 superobject *su = (superobject *)self; 6466 int skip = su->obj_type == NULL; 6467 6468 if (!skip) { 6469 /* We want __class__ to return the class of the super object 6470 (i.e. super, or a subclass), not the class of su->obj. */ 6471 skip = (PyString_Check(name) && 6472 PyString_GET_SIZE(name) == 9 && 6473 strcmp(PyString_AS_STRING(name), "__class__") == 0); 6474 } 6475 6476 if (!skip) { 6477 PyObject *mro, *res, *tmp, *dict; 6478 PyTypeObject *starttype; 6479 descrgetfunc f; 6480 Py_ssize_t i, n; 6481 6482 starttype = su->obj_type; 6483 mro = starttype->tp_mro; 6484 6485 if (mro == NULL) 6486 n = 0; 6487 else { 6488 assert(PyTuple_Check(mro)); 6489 n = PyTuple_GET_SIZE(mro); 6490 } 6491 for (i = 0; i < n; i++) { 6492 if ((PyObject *)(su->type) == PyTuple_GET_ITEM(mro, i)) 6493 break; 6494 } 6495 i++; 6496 res = NULL; 6497 for (; i < n; i++) { 6498 tmp = PyTuple_GET_ITEM(mro, i); 6499 if (PyType_Check(tmp)) 6500 dict = ((PyTypeObject *)tmp)->tp_dict; 6501 else if (PyClass_Check(tmp)) 6502 dict = ((PyClassObject *)tmp)->cl_dict; 6503 else 6504 continue; 6505 res = PyDict_GetItem(dict, name); 6506 if (res != NULL) { 6507 Py_INCREF(res); 6508 f = Py_TYPE(res)->tp_descr_get; 6509 if (f != NULL) { 6510 tmp = f(res, 6511 /* Only pass 'obj' param if 6512 this is instance-mode super 6513 (See SF ID #743627) 6514 */ 6515 (su->obj == (PyObject *) 6516 su->obj_type 6517 ? (PyObject *)NULL 6518 : su->obj), 6519 (PyObject *)starttype); 6520 Py_DECREF(res); 6521 res = tmp; 6522 } 6523 return res; 6524 } 6525 } 6526 } 6527 return PyObject_GenericGetAttr(self, name); 6528 } 6529 6530 static PyTypeObject * 6531 supercheck(PyTypeObject *type, PyObject *obj) 6532 { 6533 /* Check that a super() call makes sense. Return a type object. 6534 6535 obj can be a new-style class, or an instance of one: 6536 6537 - If it is a class, it must be a subclass of 'type'. This case is 6538 used for class methods; the return value is obj. 6539 6540 - If it is an instance, it must be an instance of 'type'. This is 6541 the normal case; the return value is obj.__class__. 6542 6543 But... when obj is an instance, we want to allow for the case where 6544 Py_TYPE(obj) is not a subclass of type, but obj.__class__ is! 6545 This will allow using super() with a proxy for obj. 6546 */ 6547 6548 /* Check for first bullet above (special case) */ 6549 if (PyType_Check(obj) && PyType_IsSubtype((PyTypeObject *)obj, type)) { 6550 Py_INCREF(obj); 6551 return (PyTypeObject *)obj; 6552 } 6553 6554 /* Normal case */ 6555 if (PyType_IsSubtype(Py_TYPE(obj), type)) { 6556 Py_INCREF(Py_TYPE(obj)); 6557 return Py_TYPE(obj); 6558 } 6559 else { 6560 /* Try the slow way */ 6561 static PyObject *class_str = NULL; 6562 PyObject *class_attr; 6563 6564 if (class_str == NULL) { 6565 class_str = PyString_FromString("__class__"); 6566 if (class_str == NULL) 6567 return NULL; 6568 } 6569 6570 class_attr = PyObject_GetAttr(obj, class_str); 6571 6572 if (class_attr != NULL && 6573 PyType_Check(class_attr) && 6574 (PyTypeObject *)class_attr != Py_TYPE(obj)) 6575 { 6576 int ok = PyType_IsSubtype( 6577 (PyTypeObject *)class_attr, type); 6578 if (ok) 6579 return (PyTypeObject *)class_attr; 6580 } 6581 6582 if (class_attr == NULL) 6583 PyErr_Clear(); 6584 else 6585 Py_DECREF(class_attr); 6586 } 6587 6588 PyErr_SetString(PyExc_TypeError, 6589 "super(type, obj): " 6590 "obj must be an instance or subtype of type"); 6591 return NULL; 6592 } 6593 6594 static PyObject * 6595 super_descr_get(PyObject *self, PyObject *obj, PyObject *type) 6596 { 6597 superobject *su = (superobject *)self; 6598 superobject *newobj; 6599 6600 if (obj == NULL || obj == Py_None || su->obj != NULL) { 6601 /* Not binding to an object, or already bound */ 6602 Py_INCREF(self); 6603 return self; 6604 } 6605 if (Py_TYPE(su) != &PySuper_Type) 6606 /* If su is an instance of a (strict) subclass of super, 6607 call its type */ 6608 return PyObject_CallFunctionObjArgs((PyObject *)Py_TYPE(su), 6609 su->type, obj, NULL); 6610 else { 6611 /* Inline the common case */ 6612 PyTypeObject *obj_type = supercheck(su->type, obj); 6613 if (obj_type == NULL) 6614 return NULL; 6615 newobj = (superobject *)PySuper_Type.tp_new(&PySuper_Type, 6616 NULL, NULL); 6617 if (newobj == NULL) 6618 return NULL; 6619 Py_INCREF(su->type); 6620 Py_INCREF(obj); 6621 newobj->type = su->type; 6622 newobj->obj = obj; 6623 newobj->obj_type = obj_type; 6624 return (PyObject *)newobj; 6625 } 6626 } 6627 6628 static int 6629 super_init(PyObject *self, PyObject *args, PyObject *kwds) 6630 { 6631 superobject *su = (superobject *)self; 6632 PyTypeObject *type; 6633 PyObject *obj = NULL; 6634 PyTypeObject *obj_type = NULL; 6635 6636 if (!_PyArg_NoKeywords("super", kwds)) 6637 return -1; 6638 if (!PyArg_ParseTuple(args, "O!|O:super", &PyType_Type, &type, &obj)) 6639 return -1; 6640 if (obj == Py_None) 6641 obj = NULL; 6642 if (obj != NULL) { 6643 obj_type = supercheck(type, obj); 6644 if (obj_type == NULL) 6645 return -1; 6646 Py_INCREF(obj); 6647 } 6648 Py_INCREF(type); 6649 su->type = type; 6650 su->obj = obj; 6651 su->obj_type = obj_type; 6652 return 0; 6653 } 6654 6655 PyDoc_STRVAR(super_doc, 6656 "super(type) -> unbound super object\n" 6657 "super(type, obj) -> bound super object; requires isinstance(obj, type)\n" 6658 "super(type, type2) -> bound super object; requires issubclass(type2, type)\n" 6659 "Typical use to call a cooperative superclass method:\n" 6660 "class C(B):\n" 6661 " def meth(self, arg):\n" 6662 " super(C, self).meth(arg)"); 6663 6664 static int 6665 super_traverse(PyObject *self, visitproc visit, void *arg) 6666 { 6667 superobject *su = (superobject *)self; 6668 6669 Py_VISIT(su->obj); 6670 Py_VISIT(su->type); 6671 Py_VISIT(su->obj_type); 6672 6673 return 0; 6674 } 6675 6676 PyTypeObject PySuper_Type = { 6677 PyVarObject_HEAD_INIT(&PyType_Type, 0) 6678 "super", /* tp_name */ 6679 sizeof(superobject), /* tp_basicsize */ 6680 0, /* tp_itemsize */ 6681 /* methods */ 6682 super_dealloc, /* tp_dealloc */ 6683 0, /* tp_print */ 6684 0, /* tp_getattr */ 6685 0, /* tp_setattr */ 6686 0, /* tp_compare */ 6687 super_repr, /* tp_repr */ 6688 0, /* tp_as_number */ 6689 0, /* tp_as_sequence */ 6690 0, /* tp_as_mapping */ 6691 0, /* tp_hash */ 6692 0, /* tp_call */ 6693 0, /* tp_str */ 6694 super_getattro, /* tp_getattro */ 6695 0, /* tp_setattro */ 6696 0, /* tp_as_buffer */ 6697 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | 6698 Py_TPFLAGS_BASETYPE, /* tp_flags */ 6699 super_doc, /* tp_doc */ 6700 super_traverse, /* tp_traverse */ 6701 0, /* tp_clear */ 6702 0, /* tp_richcompare */ 6703 0, /* tp_weaklistoffset */ 6704 0, /* tp_iter */ 6705 0, /* tp_iternext */ 6706 0, /* tp_methods */ 6707 super_members, /* tp_members */ 6708 0, /* tp_getset */ 6709 0, /* tp_base */ 6710 0, /* tp_dict */ 6711 super_descr_get, /* tp_descr_get */ 6712 0, /* tp_descr_set */ 6713 0, /* tp_dictoffset */ 6714 super_init, /* tp_init */ 6715 PyType_GenericAlloc, /* tp_alloc */ 6716 PyType_GenericNew, /* tp_new */ 6717 PyObject_GC_Del, /* tp_free */ 6718 };