object.c revision 6ca130d0dba71dd494ae284a76e45e7050132468
1 2/* Generic object operations; and implementation of None (NoObject) */ 3 4#include "Python.h" 5#include "sliceobject.h" /* For PyEllipsis_Type */ 6 7#ifdef __cplusplus 8extern "C" { 9#endif 10 11#ifdef Py_REF_DEBUG 12Py_ssize_t _Py_RefTotal; 13 14Py_ssize_t 15_Py_GetRefTotal(void) 16{ 17 PyObject *o; 18 Py_ssize_t total = _Py_RefTotal; 19 /* ignore the references to the dummy object of the dicts and sets 20 because they are not reliable and not useful (now that the 21 hash table code is well-tested) */ 22 o = _PyDict_Dummy(); 23 if (o != NULL) 24 total -= o->ob_refcnt; 25 o = _PySet_Dummy(); 26 if (o != NULL) 27 total -= o->ob_refcnt; 28 return total; 29} 30#endif /* Py_REF_DEBUG */ 31 32int Py_DivisionWarningFlag; 33 34/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. 35 These are used by the individual routines for object creation. 36 Do not call them otherwise, they do not initialize the object! */ 37 38#ifdef Py_TRACE_REFS 39/* Head of circular doubly-linked list of all objects. These are linked 40 * together via the _ob_prev and _ob_next members of a PyObject, which 41 * exist only in a Py_TRACE_REFS build. 42 */ 43static PyObject refchain = {&refchain, &refchain}; 44 45/* Insert op at the front of the list of all objects. If force is true, 46 * op is added even if _ob_prev and _ob_next are non-NULL already. If 47 * force is false amd _ob_prev or _ob_next are non-NULL, do nothing. 48 * force should be true if and only if op points to freshly allocated, 49 * uninitialized memory, or you've unlinked op from the list and are 50 * relinking it into the front. 51 * Note that objects are normally added to the list via _Py_NewReference, 52 * which is called by PyObject_Init. Not all objects are initialized that 53 * way, though; exceptions include statically allocated type objects, and 54 * statically allocated singletons (like Py_True and Py_None). 55 */ 56void 57_Py_AddToAllObjects(PyObject *op, int force) 58{ 59#ifdef Py_DEBUG 60 if (!force) { 61 /* If it's initialized memory, op must be in or out of 62 * the list unambiguously. 63 */ 64 assert((op->_ob_prev == NULL) == (op->_ob_next == NULL)); 65 } 66#endif 67 if (force || op->_ob_prev == NULL) { 68 op->_ob_next = refchain._ob_next; 69 op->_ob_prev = &refchain; 70 refchain._ob_next->_ob_prev = op; 71 refchain._ob_next = op; 72 } 73} 74#endif /* Py_TRACE_REFS */ 75 76#ifdef COUNT_ALLOCS 77static PyTypeObject *type_list; 78/* All types are added to type_list, at least when 79 they get one object created. That makes them 80 immortal, which unfortunately contributes to 81 garbage itself. If unlist_types_without_objects 82 is set, they will be removed from the type_list 83 once the last object is deallocated. */ 84int unlist_types_without_objects; 85extern int tuple_zero_allocs, fast_tuple_allocs; 86extern int quick_int_allocs, quick_neg_int_allocs; 87extern int null_strings, one_strings; 88void 89dump_counts(FILE* f) 90{ 91 PyTypeObject *tp; 92 93 for (tp = type_list; tp; tp = tp->tp_next) 94 fprintf(f, "%s alloc'd: %d, freed: %d, max in use: %d\n", 95 tp->tp_name, tp->tp_allocs, tp->tp_frees, 96 tp->tp_maxalloc); 97 fprintf(f, "fast tuple allocs: %d, empty: %d\n", 98 fast_tuple_allocs, tuple_zero_allocs); 99 fprintf(f, "fast int allocs: pos: %d, neg: %d\n", 100 quick_int_allocs, quick_neg_int_allocs); 101 fprintf(f, "null strings: %d, 1-strings: %d\n", 102 null_strings, one_strings); 103} 104 105PyObject * 106get_counts(void) 107{ 108 PyTypeObject *tp; 109 PyObject *result; 110 PyObject *v; 111 112 result = PyList_New(0); 113 if (result == NULL) 114 return NULL; 115 for (tp = type_list; tp; tp = tp->tp_next) { 116 v = Py_BuildValue("(snnn)", tp->tp_name, tp->tp_allocs, 117 tp->tp_frees, tp->tp_maxalloc); 118 if (v == NULL) { 119 Py_DECREF(result); 120 return NULL; 121 } 122 if (PyList_Append(result, v) < 0) { 123 Py_DECREF(v); 124 Py_DECREF(result); 125 return NULL; 126 } 127 Py_DECREF(v); 128 } 129 return result; 130} 131 132void 133inc_count(PyTypeObject *tp) 134{ 135 if (tp->tp_next == NULL && tp->tp_prev == NULL) { 136 /* first time; insert in linked list */ 137 if (tp->tp_next != NULL) /* sanity check */ 138 Py_FatalError("XXX inc_count sanity check"); 139 if (type_list) 140 type_list->tp_prev = tp; 141 tp->tp_next = type_list; 142 /* Note that as of Python 2.2, heap-allocated type objects 143 * can go away, but this code requires that they stay alive 144 * until program exit. That's why we're careful with 145 * refcounts here. type_list gets a new reference to tp, 146 * while ownership of the reference type_list used to hold 147 * (if any) was transferred to tp->tp_next in the line above. 148 * tp is thus effectively immortal after this. 149 */ 150 Py_INCREF(tp); 151 type_list = tp; 152#ifdef Py_TRACE_REFS 153 /* Also insert in the doubly-linked list of all objects, 154 * if not already there. 155 */ 156 _Py_AddToAllObjects((PyObject *)tp, 0); 157#endif 158 } 159 tp->tp_allocs++; 160 if (tp->tp_allocs - tp->tp_frees > tp->tp_maxalloc) 161 tp->tp_maxalloc = tp->tp_allocs - tp->tp_frees; 162} 163 164void dec_count(PyTypeObject *tp) 165{ 166 tp->tp_frees++; 167 if (unlist_types_without_objects && 168 tp->tp_allocs == tp->tp_frees) { 169 /* unlink the type from type_list */ 170 if (tp->tp_prev) 171 tp->tp_prev->tp_next = tp->tp_next; 172 else 173 type_list = tp->tp_next; 174 if (tp->tp_next) 175 tp->tp_next->tp_prev = tp->tp_prev; 176 tp->tp_next = tp->tp_prev = NULL; 177 Py_DECREF(tp); 178 } 179} 180 181#endif 182 183#ifdef Py_REF_DEBUG 184/* Log a fatal error; doesn't return. */ 185void 186_Py_NegativeRefcount(const char *fname, int lineno, PyObject *op) 187{ 188 char buf[300]; 189 190 PyOS_snprintf(buf, sizeof(buf), 191 "%s:%i object at %p has negative ref count " 192 "%" PY_FORMAT_SIZE_T "d", 193 fname, lineno, op, op->ob_refcnt); 194 Py_FatalError(buf); 195} 196 197#endif /* Py_REF_DEBUG */ 198 199void 200Py_IncRef(PyObject *o) 201{ 202 Py_XINCREF(o); 203} 204 205void 206Py_DecRef(PyObject *o) 207{ 208 Py_XDECREF(o); 209} 210 211PyObject * 212PyObject_Init(PyObject *op, PyTypeObject *tp) 213{ 214 if (op == NULL) 215 return PyErr_NoMemory(); 216 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ 217 Py_Type(op) = tp; 218 _Py_NewReference(op); 219 return op; 220} 221 222PyVarObject * 223PyObject_InitVar(PyVarObject *op, PyTypeObject *tp, Py_ssize_t size) 224{ 225 if (op == NULL) 226 return (PyVarObject *) PyErr_NoMemory(); 227 /* Any changes should be reflected in PyObject_INIT_VAR */ 228 op->ob_size = size; 229 Py_Type(op) = tp; 230 _Py_NewReference((PyObject *)op); 231 return op; 232} 233 234PyObject * 235_PyObject_New(PyTypeObject *tp) 236{ 237 PyObject *op; 238 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); 239 if (op == NULL) 240 return PyErr_NoMemory(); 241 return PyObject_INIT(op, tp); 242} 243 244PyVarObject * 245_PyObject_NewVar(PyTypeObject *tp, Py_ssize_t nitems) 246{ 247 PyVarObject *op; 248 const size_t size = _PyObject_VAR_SIZE(tp, nitems); 249 op = (PyVarObject *) PyObject_MALLOC(size); 250 if (op == NULL) 251 return (PyVarObject *)PyErr_NoMemory(); 252 return PyObject_INIT_VAR(op, tp, nitems); 253} 254 255/* Implementation of PyObject_Print with recursion checking */ 256static int 257internal_print(PyObject *op, FILE *fp, int flags, int nesting) 258{ 259 int ret = 0; 260 if (nesting > 10) { 261 PyErr_SetString(PyExc_RuntimeError, "print recursion"); 262 return -1; 263 } 264 if (PyErr_CheckSignals()) 265 return -1; 266#ifdef USE_STACKCHECK 267 if (PyOS_CheckStack()) { 268 PyErr_SetString(PyExc_MemoryError, "stack overflow"); 269 return -1; 270 } 271#endif 272 clearerr(fp); /* Clear any previous error condition */ 273 if (op == NULL) { 274 fprintf(fp, "<nil>"); 275 } 276 else { 277 if (op->ob_refcnt <= 0) 278 /* XXX(twouters) cast refcount to long until %zd is 279 universally available */ 280 fprintf(fp, "<refcnt %ld at %p>", 281 (long)op->ob_refcnt, op); 282 else { 283 PyObject *s; 284 if (flags & Py_PRINT_RAW) 285 s = PyObject_Str(op); 286 else 287 s = PyObject_Repr(op); 288 if (s == NULL) 289 ret = -1; 290 else if (PyString_Check(s)) { 291 fwrite(PyString_AS_STRING(s), 1, 292 PyString_GET_SIZE(s), fp); 293 } 294 else if (PyUnicode_Check(s)) { 295 PyObject *t; 296 t = _PyUnicode_AsDefaultEncodedString(s, NULL); 297 if (t == NULL) 298 ret = 0; 299 else { 300 fwrite(PyString_AS_STRING(t), 1, 301 PyString_GET_SIZE(t), fp); 302 } 303 } 304 else { 305 PyErr_Format(PyExc_TypeError, 306 "str() or repr() returned '%.100s'", 307 s->ob_type->tp_name); 308 ret = -1; 309 } 310 Py_XDECREF(s); 311 } 312 } 313 if (ret == 0) { 314 if (ferror(fp)) { 315 PyErr_SetFromErrno(PyExc_IOError); 316 clearerr(fp); 317 ret = -1; 318 } 319 } 320 return ret; 321} 322 323int 324PyObject_Print(PyObject *op, FILE *fp, int flags) 325{ 326 return internal_print(op, fp, flags, 0); 327} 328 329/* For debugging convenience. Set a breakpoint here and call it from your DLL */ 330void 331_Py_BreakPoint(void) 332{ 333} 334 335 336/* For debugging convenience. See Misc/gdbinit for some useful gdb hooks */ 337void 338_PyObject_Dump(PyObject* op) 339{ 340 if (op == NULL) 341 fprintf(stderr, "NULL\n"); 342 else { 343 fprintf(stderr, "object : "); 344 (void)PyObject_Print(op, stderr, 0); 345 /* XXX(twouters) cast refcount to long until %zd is 346 universally available */ 347 fprintf(stderr, "\n" 348 "type : %s\n" 349 "refcount: %ld\n" 350 "address : %p\n", 351 Py_Type(op)==NULL ? "NULL" : Py_Type(op)->tp_name, 352 (long)op->ob_refcnt, 353 op); 354 } 355} 356 357PyObject * 358PyObject_Repr(PyObject *v) 359{ 360 PyObject *ress, *resu; 361 if (PyErr_CheckSignals()) 362 return NULL; 363#ifdef USE_STACKCHECK 364 if (PyOS_CheckStack()) { 365 PyErr_SetString(PyExc_MemoryError, "stack overflow"); 366 return NULL; 367 } 368#endif 369 if (v == NULL) 370 return PyUnicode_FromString("<NULL>"); 371 else if (Py_Type(v)->tp_repr == NULL) 372 return PyUnicode_FromFormat("<%s object at %p>", v->ob_type->tp_name, v); 373 else { 374 ress = (*v->ob_type->tp_repr)(v); 375 if (!ress) 376 return NULL; 377 if (PyUnicode_Check(ress)) 378 return ress; 379 if (!PyString_Check(ress)) { 380 PyErr_Format(PyExc_TypeError, 381 "__repr__ returned non-string (type %.200s)", 382 ress->ob_type->tp_name); 383 Py_DECREF(ress); 384 return NULL; 385 } 386 resu = PyUnicode_FromObject(ress); 387 Py_DECREF(ress); 388 return resu; 389 } 390} 391 392PyObject * 393PyObject_ReprStr8(PyObject *v) 394{ 395 PyObject *resu = PyObject_Repr(v); 396 if (resu) { 397 PyObject *resb = PyUnicode_AsEncodedString(resu, NULL, NULL); 398 Py_DECREF(resu); 399 if (resb) { 400 PyObject *ress = PyString_FromStringAndSize( 401 PyBytes_AS_STRING(resb), 402 PyBytes_GET_SIZE(resb) 403 ); 404 Py_DECREF(resb); 405 return ress; 406 } 407 } 408 return NULL; 409} 410 411PyObject * 412_PyObject_Str(PyObject *v) 413{ 414 PyObject *res; 415 if (v == NULL) 416 return PyString_FromString("<NULL>"); 417 if (PyString_CheckExact(v)) { 418 Py_INCREF(v); 419 return v; 420 } 421 if (PyUnicode_CheckExact(v)) { 422 Py_INCREF(v); 423 return v; 424 } 425 if (Py_Type(v)->tp_str == NULL) 426 return PyObject_Repr(v); 427 428 res = (*Py_Type(v)->tp_str)(v); 429 if (res == NULL) 430 return NULL; 431 if (!(PyString_Check(res) || PyUnicode_Check(res))) { 432 PyErr_Format(PyExc_TypeError, 433 "__str__ returned non-string (type %.200s)", 434 Py_Type(res)->tp_name); 435 Py_DECREF(res); 436 return NULL; 437 } 438 return res; 439} 440 441PyObject * 442PyObject_Str(PyObject *v) 443{ 444 PyObject *res = _PyObject_Str(v); 445 if (res == NULL) 446 return NULL; 447 if (PyUnicode_Check(res)) { 448 PyObject* str; 449 str = _PyUnicode_AsDefaultEncodedString(res, NULL); 450 Py_XINCREF(str); 451 Py_DECREF(res); 452 if (str) 453 res = str; 454 else 455 return NULL; 456 } 457 assert(PyString_Check(res)); 458 return res; 459} 460 461PyObject * 462PyObject_Unicode(PyObject *v) 463{ 464 PyObject *res; 465 PyObject *func; 466 PyObject *str; 467 static PyObject *unicodestr; 468 469 if (v == NULL) 470 return PyUnicode_FromString("<NULL>"); 471 else if (PyUnicode_CheckExact(v)) { 472 Py_INCREF(v); 473 return v; 474 } 475 /* XXX As soon as we have a tp_unicode slot, we should 476 check this before trying the __unicode__ 477 method. */ 478 if (unicodestr == NULL) { 479 unicodestr= PyUnicode_InternFromString("__unicode__"); 480 if (unicodestr == NULL) 481 return NULL; 482 } 483 func = PyObject_GetAttr(v, unicodestr); 484 if (func != NULL) { 485 res = PyEval_CallObject(func, (PyObject *)NULL); 486 Py_DECREF(func); 487 } 488 else { 489 PyErr_Clear(); 490 if (PyUnicode_Check(v) && 491 v->ob_type->tp_str == PyUnicode_Type.tp_str) { 492 /* For a Unicode subtype that's didn't overwrite 493 __unicode__ or __str__, 494 return a true Unicode object with the same data. */ 495 return PyUnicode_FromUnicode(PyUnicode_AS_UNICODE(v), 496 PyUnicode_GET_SIZE(v)); 497 } 498 if (PyString_CheckExact(v)) { 499 Py_INCREF(v); 500 res = v; 501 } 502 else { 503 if (Py_Type(v)->tp_str != NULL) 504 res = (*Py_Type(v)->tp_str)(v); 505 else 506 res = PyObject_Repr(v); 507 } 508 } 509 if (res == NULL) 510 return NULL; 511 if (!PyUnicode_Check(res)) { 512 str = PyUnicode_FromEncodedObject(res, NULL, "strict"); 513 Py_DECREF(res); 514 res = str; 515 } 516 return res; 517} 518 519 520/* The new comparison philosophy is: we completely separate three-way 521 comparison from rich comparison. That is, PyObject_Compare() and 522 PyObject_Cmp() *just* use the tp_compare slot. And PyObject_RichCompare() 523 and PyObject_RichCompareBool() *just* use the tp_richcompare slot. 524 525 See (*) below for practical amendments. 526 527 IOW, only cmp() uses tp_compare; the comparison operators (==, !=, <=, <, 528 >=, >) only use tp_richcompare. Note that list.sort() only uses <. 529 530 (And yes, eventually we'll rip out cmp() and tp_compare.) 531 532 The calling conventions are different: tp_compare only gets called with two 533 objects of the appropriate type; tp_richcompare gets called with a first 534 argument of the appropriate type and a second object of an arbitrary type. 535 We never do any kind of coercion. 536 537 The return conventions are also different. 538 539 The tp_compare slot should return a C int, as follows: 540 541 -1 if a < b or if an exception occurred 542 0 if a == b 543 +1 if a > b 544 545 No other return values are allowed. PyObject_Compare() has the same 546 calling convention. 547 548 The tp_richcompare slot should return an object, as follows: 549 550 NULL if an exception occurred 551 NotImplemented if the requested comparison is not implemented 552 any other false value if the requested comparison is false 553 any other true value if the requested comparison is true 554 555 The PyObject_RichCompare[Bool]() wrappers raise TypeError when they get 556 NotImplemented. 557 558 (*) Practical amendments: 559 560 - If rich comparison returns NotImplemented, == and != are decided by 561 comparing the object pointer (i.e. falling back to the base object 562 implementation). 563 564 - If three-way comparison is not implemented, it falls back on rich 565 comparison (but not the other way around!). 566 567*/ 568 569/* Forward */ 570static PyObject *do_richcompare(PyObject *v, PyObject *w, int op); 571 572/* Perform a three-way comparison, raising TypeError if three-way comparison 573 is not supported. */ 574static int 575do_compare(PyObject *v, PyObject *w) 576{ 577 cmpfunc f; 578 int ok; 579 580 if (v->ob_type == w->ob_type && 581 (f = v->ob_type->tp_compare) != NULL) { 582 return (*f)(v, w); 583 } 584 585 /* Now try three-way compare before giving up. This is intentionally 586 elaborate; if you have a it will raise TypeError if it detects two 587 objects that aren't ordered with respect to each other. */ 588 ok = PyObject_RichCompareBool(v, w, Py_LT); 589 if (ok < 0) 590 return -1; /* Error */ 591 if (ok) 592 return -1; /* Less than */ 593 ok = PyObject_RichCompareBool(v, w, Py_GT); 594 if (ok < 0) 595 return -1; /* Error */ 596 if (ok) 597 return 1; /* Greater than */ 598 ok = PyObject_RichCompareBool(v, w, Py_EQ); 599 if (ok < 0) 600 return -1; /* Error */ 601 if (ok) 602 return 0; /* Equal */ 603 604 /* Give up */ 605 PyErr_Format(PyExc_TypeError, 606 "unorderable types: '%.100s' != '%.100s'", 607 v->ob_type->tp_name, 608 w->ob_type->tp_name); 609 return -1; 610} 611 612/* Perform a three-way comparison. This wraps do_compare() with a check for 613 NULL arguments and a recursion check. */ 614int 615PyObject_Compare(PyObject *v, PyObject *w) 616{ 617 int res; 618 619 if (v == NULL || w == NULL) { 620 if (!PyErr_Occurred()) 621 PyErr_BadInternalCall(); 622 return -1; 623 } 624 if (Py_EnterRecursiveCall(" in cmp")) 625 return -1; 626 res = do_compare(v, w); 627 Py_LeaveRecursiveCall(); 628 return res < 0 ? -1 : res; 629} 630 631/* Map rich comparison operators to their swapped version, e.g. LT <--> GT */ 632int _Py_SwappedOp[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE}; 633 634static char *opstrings[] = {"<", "<=", "==", "!=", ">", ">="}; 635 636/* Perform a rich comparison, raising TypeError when the requested comparison 637 operator is not supported. */ 638static PyObject * 639do_richcompare(PyObject *v, PyObject *w, int op) 640{ 641 richcmpfunc f; 642 PyObject *res; 643 644 if (v->ob_type != w->ob_type && 645 PyType_IsSubtype(w->ob_type, v->ob_type) && 646 (f = w->ob_type->tp_richcompare) != NULL) { 647 res = (*f)(w, v, _Py_SwappedOp[op]); 648 if (res != Py_NotImplemented) 649 return res; 650 Py_DECREF(res); 651 } 652 if ((f = v->ob_type->tp_richcompare) != NULL) { 653 res = (*f)(v, w, op); 654 if (res != Py_NotImplemented) 655 return res; 656 Py_DECREF(res); 657 } 658 if ((f = w->ob_type->tp_richcompare) != NULL) { 659 res = (*f)(w, v, _Py_SwappedOp[op]); 660 if (res != Py_NotImplemented) 661 return res; 662 Py_DECREF(res); 663 } 664 /* If neither object implements it, provide a sensible default 665 for == and !=, but raise an exception for ordering. */ 666 switch (op) { 667 case Py_EQ: 668 res = (v == w) ? Py_True : Py_False; 669 break; 670 case Py_NE: 671 res = (v != w) ? Py_True : Py_False; 672 break; 673 default: 674 /* XXX Special-case None so it doesn't show as NoneType() */ 675 PyErr_Format(PyExc_TypeError, 676 "unorderable types: %.100s() %s %.100s()", 677 v->ob_type->tp_name, 678 opstrings[op], 679 w->ob_type->tp_name); 680 return NULL; 681 } 682 Py_INCREF(res); 683 return res; 684} 685 686/* Perform a rich comparison with object result. This wraps do_richcompare() 687 with a check for NULL arguments and a recursion check. */ 688 689PyObject * 690PyObject_RichCompare(PyObject *v, PyObject *w, int op) 691{ 692 PyObject *res; 693 694 assert(Py_LT <= op && op <= Py_GE); 695 if (v == NULL || w == NULL) { 696 if (!PyErr_Occurred()) 697 PyErr_BadInternalCall(); 698 return NULL; 699 } 700 if (Py_EnterRecursiveCall(" in cmp")) 701 return NULL; 702 res = do_richcompare(v, w, op); 703 Py_LeaveRecursiveCall(); 704 return res; 705} 706 707/* Perform a rich comparison with integer result. This wraps 708 PyObject_RichCompare(), returning -1 for error, 0 for false, 1 for true. */ 709int 710PyObject_RichCompareBool(PyObject *v, PyObject *w, int op) 711{ 712 PyObject *res; 713 int ok; 714 715 res = PyObject_RichCompare(v, w, op); 716 if (res == NULL) 717 return -1; 718 if (PyBool_Check(res)) 719 ok = (res == Py_True); 720 else 721 ok = PyObject_IsTrue(res); 722 Py_DECREF(res); 723 return ok; 724} 725 726/* Turn the result of a three-way comparison into the result expected by a 727 rich comparison. */ 728PyObject * 729Py_CmpToRich(int op, int cmp) 730{ 731 PyObject *res; 732 int ok; 733 734 if (PyErr_Occurred()) 735 return NULL; 736 switch (op) { 737 case Py_LT: 738 ok = cmp < 0; 739 break; 740 case Py_LE: 741 ok = cmp <= 0; 742 break; 743 case Py_EQ: 744 ok = cmp == 0; 745 break; 746 case Py_NE: 747 ok = cmp != 0; 748 break; 749 case Py_GT: 750 ok = cmp > 0; 751 break; 752 case Py_GE: 753 ok = cmp >= 0; 754 break; 755 default: 756 PyErr_BadArgument(); 757 return NULL; 758 } 759 res = ok ? Py_True : Py_False; 760 Py_INCREF(res); 761 return res; 762} 763 764/* Set of hash utility functions to help maintaining the invariant that 765 if a==b then hash(a)==hash(b) 766 767 All the utility functions (_Py_Hash*()) return "-1" to signify an error. 768*/ 769 770long 771_Py_HashDouble(double v) 772{ 773 double intpart, fractpart; 774 int expo; 775 long hipart; 776 long x; /* the final hash value */ 777 /* This is designed so that Python numbers of different types 778 * that compare equal hash to the same value; otherwise comparisons 779 * of mapping keys will turn out weird. 780 */ 781 782 fractpart = modf(v, &intpart); 783 if (fractpart == 0.0) { 784 /* This must return the same hash as an equal int or long. */ 785 if (intpart > LONG_MAX || -intpart > LONG_MAX) { 786 /* Convert to long and use its hash. */ 787 PyObject *plong; /* converted to Python long */ 788 if (Py_IS_INFINITY(intpart)) 789 /* can't convert to long int -- arbitrary */ 790 v = v < 0 ? -271828.0 : 314159.0; 791 plong = PyLong_FromDouble(v); 792 if (plong == NULL) 793 return -1; 794 x = PyObject_Hash(plong); 795 Py_DECREF(plong); 796 return x; 797 } 798 /* Fits in a C long == a Python int, so is its own hash. */ 799 x = (long)intpart; 800 if (x == -1) 801 x = -2; 802 return x; 803 } 804 /* The fractional part is non-zero, so we don't have to worry about 805 * making this match the hash of some other type. 806 * Use frexp to get at the bits in the double. 807 * Since the VAX D double format has 56 mantissa bits, which is the 808 * most of any double format in use, each of these parts may have as 809 * many as (but no more than) 56 significant bits. 810 * So, assuming sizeof(long) >= 4, each part can be broken into two 811 * longs; frexp and multiplication are used to do that. 812 * Also, since the Cray double format has 15 exponent bits, which is 813 * the most of any double format in use, shifting the exponent field 814 * left by 15 won't overflow a long (again assuming sizeof(long) >= 4). 815 */ 816 v = frexp(v, &expo); 817 v *= 2147483648.0; /* 2**31 */ 818 hipart = (long)v; /* take the top 32 bits */ 819 v = (v - (double)hipart) * 2147483648.0; /* get the next 32 bits */ 820 x = hipart + (long)v + (expo << 15); 821 if (x == -1) 822 x = -2; 823 return x; 824} 825 826long 827_Py_HashPointer(void *p) 828{ 829#if SIZEOF_LONG >= SIZEOF_VOID_P 830 return (long)p; 831#else 832 /* convert to a Python long and hash that */ 833 PyObject* longobj; 834 long x; 835 836 if ((longobj = PyLong_FromVoidPtr(p)) == NULL) { 837 x = -1; 838 goto finally; 839 } 840 x = PyObject_Hash(longobj); 841 842finally: 843 Py_XDECREF(longobj); 844 return x; 845#endif 846} 847 848 849long 850PyObject_Hash(PyObject *v) 851{ 852 PyTypeObject *tp = v->ob_type; 853 if (tp->tp_hash != NULL) 854 return (*tp->tp_hash)(v); 855 /* Otherwise, the object can't be hashed */ 856 PyErr_Format(PyExc_TypeError, "unhashable type: '%.200s'", 857 v->ob_type->tp_name); 858 return -1; 859} 860 861PyObject * 862PyObject_GetAttrString(PyObject *v, const char *name) 863{ 864 PyObject *w, *res; 865 866 if (Py_Type(v)->tp_getattr != NULL) 867 return (*Py_Type(v)->tp_getattr)(v, (char*)name); 868 w = PyUnicode_InternFromString(name); 869 if (w == NULL) 870 return NULL; 871 res = PyObject_GetAttr(v, w); 872 Py_XDECREF(w); 873 return res; 874} 875 876int 877PyObject_HasAttrString(PyObject *v, const char *name) 878{ 879 PyObject *res = PyObject_GetAttrString(v, name); 880 if (res != NULL) { 881 Py_DECREF(res); 882 return 1; 883 } 884 PyErr_Clear(); 885 return 0; 886} 887 888int 889PyObject_SetAttrString(PyObject *v, const char *name, PyObject *w) 890{ 891 PyObject *s; 892 int res; 893 894 if (Py_Type(v)->tp_setattr != NULL) 895 return (*Py_Type(v)->tp_setattr)(v, (char*)name, w); 896 s = PyUnicode_InternFromString(name); 897 if (s == NULL) 898 return -1; 899 res = PyObject_SetAttr(v, s, w); 900 Py_XDECREF(s); 901 return res; 902} 903 904PyObject * 905PyObject_GetAttr(PyObject *v, PyObject *name) 906{ 907 PyTypeObject *tp = Py_Type(v); 908 909 if (!PyUnicode_Check(name)) { 910 PyErr_Format(PyExc_TypeError, 911 "attribute name must be string, not '%.200s'", 912 name->ob_type->tp_name); 913 return NULL; 914 } 915 if (tp->tp_getattro != NULL) 916 return (*tp->tp_getattro)(v, name); 917 if (tp->tp_getattr != NULL) 918 return (*tp->tp_getattr)(v, PyUnicode_AsString(name)); 919 PyErr_Format(PyExc_AttributeError, 920 "'%.50s' object has no attribute '%U'", 921 tp->tp_name, name); 922 return NULL; 923} 924 925int 926PyObject_HasAttr(PyObject *v, PyObject *name) 927{ 928 PyObject *res = PyObject_GetAttr(v, name); 929 if (res != NULL) { 930 Py_DECREF(res); 931 return 1; 932 } 933 PyErr_Clear(); 934 return 0; 935} 936 937int 938PyObject_SetAttr(PyObject *v, PyObject *name, PyObject *value) 939{ 940 PyTypeObject *tp = Py_Type(v); 941 int err; 942 943 if (!PyUnicode_Check(name)) { 944 PyErr_Format(PyExc_TypeError, 945 "attribute name must be string, not '%.200s'", 946 name->ob_type->tp_name); 947 return -1; 948 } 949 Py_INCREF(name); 950 951 PyUnicode_InternInPlace(&name); 952 if (tp->tp_setattro != NULL) { 953 err = (*tp->tp_setattro)(v, name, value); 954 Py_DECREF(name); 955 return err; 956 } 957 if (tp->tp_setattr != NULL) { 958 err = (*tp->tp_setattr)(v, PyUnicode_AsString(name), value); 959 Py_DECREF(name); 960 return err; 961 } 962 Py_DECREF(name); 963 assert(name->ob_refcnt >= 1); 964 if (tp->tp_getattr == NULL && tp->tp_getattro == NULL) 965 PyErr_Format(PyExc_TypeError, 966 "'%.100s' object has no attributes " 967 "(%s .%U)", 968 tp->tp_name, 969 value==NULL ? "del" : "assign to", 970 name); 971 else 972 PyErr_Format(PyExc_TypeError, 973 "'%.100s' object has only read-only attributes " 974 "(%s .%U)", 975 tp->tp_name, 976 value==NULL ? "del" : "assign to", 977 name); 978 return -1; 979} 980 981/* Helper to get a pointer to an object's __dict__ slot, if any */ 982 983PyObject ** 984_PyObject_GetDictPtr(PyObject *obj) 985{ 986 Py_ssize_t dictoffset; 987 PyTypeObject *tp = Py_Type(obj); 988 989 dictoffset = tp->tp_dictoffset; 990 if (dictoffset == 0) 991 return NULL; 992 if (dictoffset < 0) { 993 Py_ssize_t tsize; 994 size_t size; 995 996 tsize = ((PyVarObject *)obj)->ob_size; 997 if (tsize < 0) 998 tsize = -tsize; 999 size = _PyObject_VAR_SIZE(tp, tsize); 1000 1001 dictoffset += (long)size; 1002 assert(dictoffset > 0); 1003 assert(dictoffset % SIZEOF_VOID_P == 0); 1004 } 1005 return (PyObject **) ((char *)obj + dictoffset); 1006} 1007 1008PyObject * 1009PyObject_SelfIter(PyObject *obj) 1010{ 1011 Py_INCREF(obj); 1012 return obj; 1013} 1014 1015/* Generic GetAttr functions - put these in your tp_[gs]etattro slot */ 1016 1017PyObject * 1018PyObject_GenericGetAttr(PyObject *obj, PyObject *name) 1019{ 1020 PyTypeObject *tp = Py_Type(obj); 1021 PyObject *descr = NULL; 1022 PyObject *res = NULL; 1023 descrgetfunc f; 1024 Py_ssize_t dictoffset; 1025 PyObject **dictptr; 1026 1027 if (!PyUnicode_Check(name)){ 1028 PyErr_Format(PyExc_TypeError, 1029 "attribute name must be string, not '%.200s'", 1030 name->ob_type->tp_name); 1031 return NULL; 1032 } 1033 else 1034 Py_INCREF(name); 1035 1036 if (tp->tp_dict == NULL) { 1037 if (PyType_Ready(tp) < 0) 1038 goto done; 1039 } 1040 1041 /* Inline _PyType_Lookup */ 1042 { 1043 Py_ssize_t i, n; 1044 PyObject *mro, *base, *dict; 1045 1046 /* Look in tp_dict of types in MRO */ 1047 mro = tp->tp_mro; 1048 assert(mro != NULL); 1049 assert(PyTuple_Check(mro)); 1050 n = PyTuple_GET_SIZE(mro); 1051 for (i = 0; i < n; i++) { 1052 base = PyTuple_GET_ITEM(mro, i); 1053 assert(PyType_Check(base)); 1054 dict = ((PyTypeObject *)base)->tp_dict; 1055 assert(dict && PyDict_Check(dict)); 1056 descr = PyDict_GetItem(dict, name); 1057 if (descr != NULL) 1058 break; 1059 } 1060 } 1061 1062 Py_XINCREF(descr); 1063 1064 f = NULL; 1065 if (descr != NULL) { 1066 f = descr->ob_type->tp_descr_get; 1067 if (f != NULL && PyDescr_IsData(descr)) { 1068 res = f(descr, obj, (PyObject *)obj->ob_type); 1069 Py_DECREF(descr); 1070 goto done; 1071 } 1072 } 1073 1074 /* Inline _PyObject_GetDictPtr */ 1075 dictoffset = tp->tp_dictoffset; 1076 if (dictoffset != 0) { 1077 PyObject *dict; 1078 if (dictoffset < 0) { 1079 Py_ssize_t tsize; 1080 size_t size; 1081 1082 tsize = ((PyVarObject *)obj)->ob_size; 1083 if (tsize < 0) 1084 tsize = -tsize; 1085 size = _PyObject_VAR_SIZE(tp, tsize); 1086 1087 dictoffset += (long)size; 1088 assert(dictoffset > 0); 1089 assert(dictoffset % SIZEOF_VOID_P == 0); 1090 } 1091 dictptr = (PyObject **) ((char *)obj + dictoffset); 1092 dict = *dictptr; 1093 if (dict != NULL) { 1094 res = PyDict_GetItem(dict, name); 1095 if (res != NULL) { 1096 Py_INCREF(res); 1097 Py_XDECREF(descr); 1098 goto done; 1099 } 1100 } 1101 } 1102 1103 if (f != NULL) { 1104 res = f(descr, obj, (PyObject *)Py_Type(obj)); 1105 Py_DECREF(descr); 1106 goto done; 1107 } 1108 1109 if (descr != NULL) { 1110 res = descr; 1111 /* descr was already increfed above */ 1112 goto done; 1113 } 1114 1115 PyErr_Format(PyExc_AttributeError, 1116 "'%.50s' object has no attribute '%.400s'", 1117 tp->tp_name, PyUnicode_AsString(name)); 1118 done: 1119 Py_DECREF(name); 1120 return res; 1121} 1122 1123int 1124PyObject_GenericSetAttr(PyObject *obj, PyObject *name, PyObject *value) 1125{ 1126 PyTypeObject *tp = Py_Type(obj); 1127 PyObject *descr; 1128 descrsetfunc f; 1129 PyObject **dictptr; 1130 int res = -1; 1131 1132 if (!PyUnicode_Check(name)){ 1133 PyErr_Format(PyExc_TypeError, 1134 "attribute name must be string, not '%.200s'", 1135 name->ob_type->tp_name); 1136 return -1; 1137 } 1138 else 1139 Py_INCREF(name); 1140 1141 if (tp->tp_dict == NULL) { 1142 if (PyType_Ready(tp) < 0) 1143 goto done; 1144 } 1145 1146 descr = _PyType_Lookup(tp, name); 1147 f = NULL; 1148 if (descr != NULL) { 1149 f = descr->ob_type->tp_descr_set; 1150 if (f != NULL && PyDescr_IsData(descr)) { 1151 res = f(descr, obj, value); 1152 goto done; 1153 } 1154 } 1155 1156 dictptr = _PyObject_GetDictPtr(obj); 1157 if (dictptr != NULL) { 1158 PyObject *dict = *dictptr; 1159 if (dict == NULL && value != NULL) { 1160 dict = PyDict_New(); 1161 if (dict == NULL) 1162 goto done; 1163 *dictptr = dict; 1164 } 1165 if (dict != NULL) { 1166 if (value == NULL) 1167 res = PyDict_DelItem(dict, name); 1168 else 1169 res = PyDict_SetItem(dict, name, value); 1170 if (res < 0 && PyErr_ExceptionMatches(PyExc_KeyError)) 1171 PyErr_SetObject(PyExc_AttributeError, name); 1172 goto done; 1173 } 1174 } 1175 1176 if (f != NULL) { 1177 res = f(descr, obj, value); 1178 goto done; 1179 } 1180 1181 if (descr == NULL) { 1182 PyErr_Format(PyExc_AttributeError, 1183 "'%.100s' object has no attribute '%U'", 1184 tp->tp_name, name); 1185 goto done; 1186 } 1187 1188 PyErr_Format(PyExc_AttributeError, 1189 "'%.50s' object attribute '%U' is read-only", 1190 tp->tp_name, name); 1191 done: 1192 Py_DECREF(name); 1193 return res; 1194} 1195 1196/* Test a value used as condition, e.g., in a for or if statement. 1197 Return -1 if an error occurred */ 1198 1199int 1200PyObject_IsTrue(PyObject *v) 1201{ 1202 Py_ssize_t res; 1203 if (v == Py_True) 1204 return 1; 1205 if (v == Py_False) 1206 return 0; 1207 if (v == Py_None) 1208 return 0; 1209 else if (v->ob_type->tp_as_number != NULL && 1210 v->ob_type->tp_as_number->nb_bool != NULL) 1211 res = (*v->ob_type->tp_as_number->nb_bool)(v); 1212 else if (v->ob_type->tp_as_mapping != NULL && 1213 v->ob_type->tp_as_mapping->mp_length != NULL) 1214 res = (*v->ob_type->tp_as_mapping->mp_length)(v); 1215 else if (v->ob_type->tp_as_sequence != NULL && 1216 v->ob_type->tp_as_sequence->sq_length != NULL) 1217 res = (*v->ob_type->tp_as_sequence->sq_length)(v); 1218 else 1219 return 1; 1220 /* if it is negative, it should be either -1 or -2 */ 1221 return (res > 0) ? 1 : Py_SAFE_DOWNCAST(res, Py_ssize_t, int); 1222} 1223 1224/* equivalent of 'not v' 1225 Return -1 if an error occurred */ 1226 1227int 1228PyObject_Not(PyObject *v) 1229{ 1230 int res; 1231 res = PyObject_IsTrue(v); 1232 if (res < 0) 1233 return res; 1234 return res == 0; 1235} 1236 1237/* Test whether an object can be called */ 1238 1239int 1240PyCallable_Check(PyObject *x) 1241{ 1242 if (x == NULL) 1243 return 0; 1244 return x->ob_type->tp_call != NULL; 1245} 1246 1247/* ------------------------- PyObject_Dir() helpers ------------------------- */ 1248 1249/* Helper for PyObject_Dir. 1250 Merge the __dict__ of aclass into dict, and recursively also all 1251 the __dict__s of aclass's base classes. The order of merging isn't 1252 defined, as it's expected that only the final set of dict keys is 1253 interesting. 1254 Return 0 on success, -1 on error. 1255*/ 1256 1257static int 1258merge_class_dict(PyObject* dict, PyObject* aclass) 1259{ 1260 PyObject *classdict; 1261 PyObject *bases; 1262 1263 assert(PyDict_Check(dict)); 1264 assert(aclass); 1265 1266 /* Merge in the type's dict (if any). */ 1267 classdict = PyObject_GetAttrString(aclass, "__dict__"); 1268 if (classdict == NULL) 1269 PyErr_Clear(); 1270 else { 1271 int status = PyDict_Update(dict, classdict); 1272 Py_DECREF(classdict); 1273 if (status < 0) 1274 return -1; 1275 } 1276 1277 /* Recursively merge in the base types' (if any) dicts. */ 1278 bases = PyObject_GetAttrString(aclass, "__bases__"); 1279 if (bases == NULL) 1280 PyErr_Clear(); 1281 else { 1282 /* We have no guarantee that bases is a real tuple */ 1283 Py_ssize_t i, n; 1284 n = PySequence_Size(bases); /* This better be right */ 1285 if (n < 0) 1286 PyErr_Clear(); 1287 else { 1288 for (i = 0; i < n; i++) { 1289 int status; 1290 PyObject *base = PySequence_GetItem(bases, i); 1291 if (base == NULL) { 1292 Py_DECREF(bases); 1293 return -1; 1294 } 1295 status = merge_class_dict(dict, base); 1296 Py_DECREF(base); 1297 if (status < 0) { 1298 Py_DECREF(bases); 1299 return -1; 1300 } 1301 } 1302 } 1303 Py_DECREF(bases); 1304 } 1305 return 0; 1306} 1307 1308/* Helper for PyObject_Dir without arguments: returns the local scope. */ 1309static PyObject * 1310_dir_locals(void) 1311{ 1312 PyObject *names; 1313 PyObject *locals = PyEval_GetLocals(); 1314 1315 if (locals == NULL) { 1316 PyErr_SetString(PyExc_SystemError, "frame does not exist"); 1317 return NULL; 1318 } 1319 1320 names = PyMapping_Keys(locals); 1321 if (!names) 1322 return NULL; 1323 if (!PyList_Check(names)) { 1324 PyErr_Format(PyExc_TypeError, 1325 "dir(): expected keys() of locals to be a list, " 1326 "not '%.200s'", Py_Type(names)->tp_name); 1327 Py_DECREF(names); 1328 return NULL; 1329 } 1330 /* the locals don't need to be DECREF'd */ 1331 return names; 1332} 1333 1334/* Helper for PyObject_Dir of type objects: returns __dict__ and __bases__. 1335 We deliberately don't suck up its __class__, as methods belonging to the 1336 metaclass would probably be more confusing than helpful. 1337*/ 1338static PyObject * 1339_specialized_dir_type(PyObject *obj) 1340{ 1341 PyObject *result = NULL; 1342 PyObject *dict = PyDict_New(); 1343 1344 if (dict != NULL && merge_class_dict(dict, obj) == 0) 1345 result = PyDict_Keys(dict); 1346 1347 Py_XDECREF(dict); 1348 return result; 1349} 1350 1351/* Helper for PyObject_Dir of module objects: returns the module's __dict__. */ 1352static PyObject * 1353_specialized_dir_module(PyObject *obj) 1354{ 1355 PyObject *result = NULL; 1356 PyObject *dict = PyObject_GetAttrString(obj, "__dict__"); 1357 1358 if (dict != NULL) { 1359 if (PyDict_Check(dict)) 1360 result = PyDict_Keys(dict); 1361 else { 1362 PyErr_Format(PyExc_TypeError, 1363 "%.200s.__dict__ is not a dictionary", 1364 PyModule_GetName(obj)); 1365 } 1366 } 1367 1368 Py_XDECREF(dict); 1369 return result; 1370} 1371 1372/* Helper for PyObject_Dir of generic objects: returns __dict__, __class__, 1373 and recursively up the __class__.__bases__ chain. 1374*/ 1375static PyObject * 1376_generic_dir(PyObject *obj) 1377{ 1378 PyObject *result = NULL; 1379 PyObject *dict = NULL; 1380 PyObject *itsclass = NULL; 1381 1382 /* Get __dict__ (which may or may not be a real dict...) */ 1383 dict = PyObject_GetAttrString(obj, "__dict__"); 1384 if (dict == NULL) { 1385 PyErr_Clear(); 1386 dict = PyDict_New(); 1387 } 1388 else if (!PyDict_Check(dict)) { 1389 Py_DECREF(dict); 1390 dict = PyDict_New(); 1391 } 1392 else { 1393 /* Copy __dict__ to avoid mutating it. */ 1394 PyObject *temp = PyDict_Copy(dict); 1395 Py_DECREF(dict); 1396 dict = temp; 1397 } 1398 1399 if (dict == NULL) 1400 goto error; 1401 1402 /* Merge in attrs reachable from its class. */ 1403 itsclass = PyObject_GetAttrString(obj, "__class__"); 1404 if (itsclass == NULL) 1405 /* XXX(tomer): Perhaps fall back to obj->ob_type if no 1406 __class__ exists? */ 1407 PyErr_Clear(); 1408 else { 1409 if (merge_class_dict(dict, itsclass) != 0) 1410 goto error; 1411 } 1412 1413 result = PyDict_Keys(dict); 1414 /* fall through */ 1415error: 1416 Py_XDECREF(itsclass); 1417 Py_XDECREF(dict); 1418 return result; 1419} 1420 1421/* Helper for PyObject_Dir: object introspection. 1422 This calls one of the above specialized versions if no __dir__ method 1423 exists. */ 1424static PyObject * 1425_dir_object(PyObject *obj) 1426{ 1427 PyObject * result = NULL; 1428 PyObject * dirfunc = PyObject_GetAttrString((PyObject*)obj->ob_type, 1429 "__dir__"); 1430 1431 assert(obj); 1432 if (dirfunc == NULL) { 1433 /* use default implementation */ 1434 PyErr_Clear(); 1435 if (PyModule_Check(obj)) 1436 result = _specialized_dir_module(obj); 1437 else if (PyType_Check(obj)) 1438 result = _specialized_dir_type(obj); 1439 else 1440 result = _generic_dir(obj); 1441 } 1442 else { 1443 /* use __dir__ */ 1444 result = PyObject_CallFunctionObjArgs(dirfunc, obj, NULL); 1445 Py_DECREF(dirfunc); 1446 if (result == NULL) 1447 return NULL; 1448 1449 /* result must be a list */ 1450 /* XXX(gbrandl): could also check if all items are strings */ 1451 if (!PyList_Check(result)) { 1452 PyErr_Format(PyExc_TypeError, 1453 "__dir__() must return a list, not %.200s", 1454 Py_Type(result)->tp_name); 1455 Py_DECREF(result); 1456 result = NULL; 1457 } 1458 } 1459 1460 return result; 1461} 1462 1463/* Implementation of dir() -- if obj is NULL, returns the names in the current 1464 (local) scope. Otherwise, performs introspection of the object: returns a 1465 sorted list of attribute names (supposedly) accessible from the object 1466*/ 1467PyObject * 1468PyObject_Dir(PyObject *obj) 1469{ 1470 PyObject * result; 1471 1472 if (obj == NULL) 1473 /* no object -- introspect the locals */ 1474 result = _dir_locals(); 1475 else 1476 /* object -- introspect the object */ 1477 result = _dir_object(obj); 1478 1479 assert(result == NULL || PyList_Check(result)); 1480 1481 if (result != NULL && PyList_Sort(result) != 0) { 1482 /* sorting the list failed */ 1483 Py_DECREF(result); 1484 result = NULL; 1485 } 1486 1487 return result; 1488} 1489 1490/* 1491NoObject is usable as a non-NULL undefined value, used by the macro None. 1492There is (and should be!) no way to create other objects of this type, 1493so there is exactly one (which is indestructible, by the way). 1494(XXX This type and the type of NotImplemented below should be unified.) 1495*/ 1496 1497/* ARGSUSED */ 1498static PyObject * 1499none_repr(PyObject *op) 1500{ 1501 return PyUnicode_FromString("None"); 1502} 1503 1504/* ARGUSED */ 1505static void 1506none_dealloc(PyObject* ignore) 1507{ 1508 /* This should never get called, but we also don't want to SEGV if 1509 * we accidently decref None out of existance. 1510 */ 1511 Py_FatalError("deallocating None"); 1512} 1513 1514 1515static PyTypeObject PyNone_Type = { 1516 PyVarObject_HEAD_INIT(&PyType_Type, 0) 1517 "NoneType", 1518 0, 1519 0, 1520 none_dealloc, /*tp_dealloc*/ /*never called*/ 1521 0, /*tp_print*/ 1522 0, /*tp_getattr*/ 1523 0, /*tp_setattr*/ 1524 0, /*tp_compare*/ 1525 none_repr, /*tp_repr*/ 1526 0, /*tp_as_number*/ 1527 0, /*tp_as_sequence*/ 1528 0, /*tp_as_mapping*/ 1529 0, /*tp_hash */ 1530}; 1531 1532PyObject _Py_NoneStruct = { 1533 _PyObject_EXTRA_INIT 1534 1, &PyNone_Type 1535}; 1536 1537/* NotImplemented is an object that can be used to signal that an 1538 operation is not implemented for the given type combination. */ 1539 1540static PyObject * 1541NotImplemented_repr(PyObject *op) 1542{ 1543 return PyUnicode_FromString("NotImplemented"); 1544} 1545 1546static PyTypeObject PyNotImplemented_Type = { 1547 PyVarObject_HEAD_INIT(&PyType_Type, 0) 1548 "NotImplementedType", 1549 0, 1550 0, 1551 none_dealloc, /*tp_dealloc*/ /*never called*/ 1552 0, /*tp_print*/ 1553 0, /*tp_getattr*/ 1554 0, /*tp_setattr*/ 1555 0, /*tp_compare*/ 1556 NotImplemented_repr, /*tp_repr*/ 1557 0, /*tp_as_number*/ 1558 0, /*tp_as_sequence*/ 1559 0, /*tp_as_mapping*/ 1560 0, /*tp_hash */ 1561}; 1562 1563PyObject _Py_NotImplementedStruct = { 1564 _PyObject_EXTRA_INIT 1565 1, &PyNotImplemented_Type 1566}; 1567 1568void 1569_Py_ReadyTypes(void) 1570{ 1571 if (PyType_Ready(&PyType_Type) < 0) 1572 Py_FatalError("Can't initialize 'type'"); 1573 1574 if (PyType_Ready(&_PyWeakref_RefType) < 0) 1575 Py_FatalError("Can't initialize 'weakref'"); 1576 1577 if (PyType_Ready(&PyBool_Type) < 0) 1578 Py_FatalError("Can't initialize 'bool'"); 1579 1580 if (PyType_Ready(&PyBytes_Type) < 0) 1581 Py_FatalError("Can't initialize 'bytes'"); 1582 1583 if (PyType_Ready(&PyString_Type) < 0) 1584 Py_FatalError("Can't initialize 'str'"); 1585 1586 if (PyType_Ready(&PyList_Type) < 0) 1587 Py_FatalError("Can't initialize 'list'"); 1588 1589 if (PyType_Ready(&PyNone_Type) < 0) 1590 Py_FatalError("Can't initialize type(None)"); 1591 1592 if (PyType_Ready(Py_Ellipsis->ob_type) < 0) 1593 Py_FatalError("Can't initialize type(Ellipsis)"); 1594 1595 if (PyType_Ready(&PyNotImplemented_Type) < 0) 1596 Py_FatalError("Can't initialize type(NotImplemented)"); 1597 1598 if (PyType_Ready(&PyCode_Type) < 0) 1599 Py_FatalError("Can't initialize 'code'"); 1600} 1601 1602 1603#ifdef Py_TRACE_REFS 1604 1605void 1606_Py_NewReference(PyObject *op) 1607{ 1608 _Py_INC_REFTOTAL; 1609 op->ob_refcnt = 1; 1610 _Py_AddToAllObjects(op, 1); 1611 _Py_INC_TPALLOCS(op); 1612} 1613 1614void 1615_Py_ForgetReference(register PyObject *op) 1616{ 1617#ifdef SLOW_UNREF_CHECK 1618 register PyObject *p; 1619#endif 1620 if (op->ob_refcnt < 0) 1621 Py_FatalError("UNREF negative refcnt"); 1622 if (op == &refchain || 1623 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) 1624 Py_FatalError("UNREF invalid object"); 1625#ifdef SLOW_UNREF_CHECK 1626 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { 1627 if (p == op) 1628 break; 1629 } 1630 if (p == &refchain) /* Not found */ 1631 Py_FatalError("UNREF unknown object"); 1632#endif 1633 op->_ob_next->_ob_prev = op->_ob_prev; 1634 op->_ob_prev->_ob_next = op->_ob_next; 1635 op->_ob_next = op->_ob_prev = NULL; 1636 _Py_INC_TPFREES(op); 1637} 1638 1639void 1640_Py_Dealloc(PyObject *op) 1641{ 1642 destructor dealloc = Py_Type(op)->tp_dealloc; 1643 _Py_ForgetReference(op); 1644 (*dealloc)(op); 1645} 1646 1647/* Print all live objects. Because PyObject_Print is called, the 1648 * interpreter must be in a healthy state. 1649 */ 1650void 1651_Py_PrintReferences(FILE *fp) 1652{ 1653 PyObject *op; 1654 fprintf(fp, "Remaining objects:\n"); 1655 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { 1656 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] ", op, op->ob_refcnt); 1657 if (PyObject_Print(op, fp, 0) != 0) 1658 PyErr_Clear(); 1659 putc('\n', fp); 1660 } 1661} 1662 1663/* Print the addresses of all live objects. Unlike _Py_PrintReferences, this 1664 * doesn't make any calls to the Python C API, so is always safe to call. 1665 */ 1666void 1667_Py_PrintReferenceAddresses(FILE *fp) 1668{ 1669 PyObject *op; 1670 fprintf(fp, "Remaining object addresses:\n"); 1671 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) 1672 fprintf(fp, "%p [%" PY_FORMAT_SIZE_T "d] %s\n", op, 1673 op->ob_refcnt, Py_Type(op)->tp_name); 1674} 1675 1676PyObject * 1677_Py_GetObjects(PyObject *self, PyObject *args) 1678{ 1679 int i, n; 1680 PyObject *t = NULL; 1681 PyObject *res, *op; 1682 1683 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) 1684 return NULL; 1685 op = refchain._ob_next; 1686 res = PyList_New(0); 1687 if (res == NULL) 1688 return NULL; 1689 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { 1690 while (op == self || op == args || op == res || op == t || 1691 (t != NULL && Py_Type(op) != (PyTypeObject *) t)) { 1692 op = op->_ob_next; 1693 if (op == &refchain) 1694 return res; 1695 } 1696 if (PyList_Append(res, op) < 0) { 1697 Py_DECREF(res); 1698 return NULL; 1699 } 1700 op = op->_ob_next; 1701 } 1702 return res; 1703} 1704 1705#endif 1706 1707 1708/* Hack to force loading of cobject.o */ 1709PyTypeObject *_Py_cobject_hack = &PyCObject_Type; 1710 1711 1712/* Hack to force loading of abstract.o */ 1713Py_ssize_t (*_Py_abstract_hack)(PyObject *) = PyObject_Size; 1714 1715 1716/* Python's malloc wrappers (see pymem.h) */ 1717 1718void * 1719PyMem_Malloc(size_t nbytes) 1720{ 1721 return PyMem_MALLOC(nbytes); 1722} 1723 1724void * 1725PyMem_Realloc(void *p, size_t nbytes) 1726{ 1727 return PyMem_REALLOC(p, nbytes); 1728} 1729 1730void 1731PyMem_Free(void *p) 1732{ 1733 PyMem_FREE(p); 1734} 1735 1736 1737/* These methods are used to control infinite recursion in repr, str, print, 1738 etc. Container objects that may recursively contain themselves, 1739 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and 1740 Py_ReprLeave() to avoid infinite recursion. 1741 1742 Py_ReprEnter() returns 0 the first time it is called for a particular 1743 object and 1 every time thereafter. It returns -1 if an exception 1744 occurred. Py_ReprLeave() has no return value. 1745 1746 See dictobject.c and listobject.c for examples of use. 1747*/ 1748 1749#define KEY "Py_Repr" 1750 1751int 1752Py_ReprEnter(PyObject *obj) 1753{ 1754 PyObject *dict; 1755 PyObject *list; 1756 Py_ssize_t i; 1757 1758 dict = PyThreadState_GetDict(); 1759 if (dict == NULL) 1760 return 0; 1761 list = PyDict_GetItemString(dict, KEY); 1762 if (list == NULL) { 1763 list = PyList_New(0); 1764 if (list == NULL) 1765 return -1; 1766 if (PyDict_SetItemString(dict, KEY, list) < 0) 1767 return -1; 1768 Py_DECREF(list); 1769 } 1770 i = PyList_GET_SIZE(list); 1771 while (--i >= 0) { 1772 if (PyList_GET_ITEM(list, i) == obj) 1773 return 1; 1774 } 1775 PyList_Append(list, obj); 1776 return 0; 1777} 1778 1779void 1780Py_ReprLeave(PyObject *obj) 1781{ 1782 PyObject *dict; 1783 PyObject *list; 1784 Py_ssize_t i; 1785 1786 dict = PyThreadState_GetDict(); 1787 if (dict == NULL) 1788 return; 1789 list = PyDict_GetItemString(dict, KEY); 1790 if (list == NULL || !PyList_Check(list)) 1791 return; 1792 i = PyList_GET_SIZE(list); 1793 /* Count backwards because we always expect obj to be list[-1] */ 1794 while (--i >= 0) { 1795 if (PyList_GET_ITEM(list, i) == obj) { 1796 PyList_SetSlice(list, i, i + 1, NULL); 1797 break; 1798 } 1799 } 1800} 1801 1802/* Trashcan support. */ 1803 1804/* Current call-stack depth of tp_dealloc calls. */ 1805int _PyTrash_delete_nesting = 0; 1806 1807/* List of objects that still need to be cleaned up, singly linked via their 1808 * gc headers' gc_prev pointers. 1809 */ 1810PyObject *_PyTrash_delete_later = NULL; 1811 1812/* Add op to the _PyTrash_delete_later list. Called when the current 1813 * call-stack depth gets large. op must be a currently untracked gc'ed 1814 * object, with refcount 0. Py_DECREF must already have been called on it. 1815 */ 1816void 1817_PyTrash_deposit_object(PyObject *op) 1818{ 1819 assert(PyObject_IS_GC(op)); 1820 assert(_Py_AS_GC(op)->gc.gc_refs == _PyGC_REFS_UNTRACKED); 1821 assert(op->ob_refcnt == 0); 1822 _Py_AS_GC(op)->gc.gc_prev = (PyGC_Head *)_PyTrash_delete_later; 1823 _PyTrash_delete_later = op; 1824} 1825 1826/* Dealloccate all the objects in the _PyTrash_delete_later list. Called when 1827 * the call-stack unwinds again. 1828 */ 1829void 1830_PyTrash_destroy_chain(void) 1831{ 1832 while (_PyTrash_delete_later) { 1833 PyObject *op = _PyTrash_delete_later; 1834 destructor dealloc = Py_Type(op)->tp_dealloc; 1835 1836 _PyTrash_delete_later = 1837 (PyObject*) _Py_AS_GC(op)->gc.gc_prev; 1838 1839 /* Call the deallocator directly. This used to try to 1840 * fool Py_DECREF into calling it indirectly, but 1841 * Py_DECREF was already called on this object, and in 1842 * assorted non-release builds calling Py_DECREF again ends 1843 * up distorting allocation statistics. 1844 */ 1845 assert(op->ob_refcnt == 0); 1846 ++_PyTrash_delete_nesting; 1847 (*dealloc)(op); 1848 --_PyTrash_delete_nesting; 1849 } 1850} 1851 1852#ifdef __cplusplus 1853} 1854#endif 1855