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