object.c revision b18618dab7b6b85bb05b084693706e59211fa180
1/*********************************************************** 2Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, 3The Netherlands. 4 5 All Rights Reserved 6 7Permission to use, copy, modify, and distribute this software and its 8documentation for any purpose and without fee is hereby granted, 9provided that the above copyright notice appear in all copies and that 10both that copyright notice and this permission notice appear in 11supporting documentation, and that the names of Stichting Mathematisch 12Centrum or CWI or Corporation for National Research Initiatives or 13CNRI not be used in advertising or publicity pertaining to 14distribution of the software without specific, written prior 15permission. 16 17While CWI is the initial source for this software, a modified version 18is made available by the Corporation for National Research Initiatives 19(CNRI) at the Internet address ftp://ftp.python.org. 20 21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH 22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF 23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH 24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL 25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR 26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER 27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR 28PERFORMANCE OF THIS SOFTWARE. 29 30******************************************************************/ 31 32/* Generic object operations; and implementation of None (NoObject) */ 33 34#include "Python.h" 35 36/* just for trashcan: */ 37#include "compile.h" 38#include "frameobject.h" 39#include "traceback.h" 40 41#if defined( Py_TRACE_REFS ) || defined( Py_REF_DEBUG ) 42DL_IMPORT(long) _Py_RefTotal; 43#endif 44 45/* Object allocation routines used by NEWOBJ and NEWVAROBJ macros. 46 These are used by the individual routines for object creation. 47 Do not call them otherwise, they do not initialize the object! */ 48 49#ifdef COUNT_ALLOCS 50static PyTypeObject *type_list; 51extern int tuple_zero_allocs, fast_tuple_allocs; 52extern int quick_int_allocs, quick_neg_int_allocs; 53extern int null_strings, one_strings; 54void 55dump_counts() 56{ 57 PyTypeObject *tp; 58 59 for (tp = type_list; tp; tp = tp->tp_next) 60 fprintf(stderr, "%s alloc'd: %d, freed: %d, max in use: %d\n", 61 tp->tp_name, tp->tp_alloc, tp->tp_free, 62 tp->tp_maxalloc); 63 fprintf(stderr, "fast tuple allocs: %d, empty: %d\n", 64 fast_tuple_allocs, tuple_zero_allocs); 65 fprintf(stderr, "fast int allocs: pos: %d, neg: %d\n", 66 quick_int_allocs, quick_neg_int_allocs); 67 fprintf(stderr, "null strings: %d, 1-strings: %d\n", 68 null_strings, one_strings); 69} 70 71PyObject * 72get_counts() 73{ 74 PyTypeObject *tp; 75 PyObject *result; 76 PyObject *v; 77 78 result = PyList_New(0); 79 if (result == NULL) 80 return NULL; 81 for (tp = type_list; tp; tp = tp->tp_next) { 82 v = Py_BuildValue("(siii)", tp->tp_name, tp->tp_alloc, 83 tp->tp_free, tp->tp_maxalloc); 84 if (v == NULL) { 85 Py_DECREF(result); 86 return NULL; 87 } 88 if (PyList_Append(result, v) < 0) { 89 Py_DECREF(v); 90 Py_DECREF(result); 91 return NULL; 92 } 93 Py_DECREF(v); 94 } 95 return result; 96} 97 98void 99inc_count(tp) 100 PyTypeObject *tp; 101{ 102 if (tp->tp_alloc == 0) { 103 /* first time; insert in linked list */ 104 if (tp->tp_next != NULL) /* sanity check */ 105 Py_FatalError("XXX inc_count sanity check"); 106 tp->tp_next = type_list; 107 type_list = tp; 108 } 109 tp->tp_alloc++; 110 if (tp->tp_alloc - tp->tp_free > tp->tp_maxalloc) 111 tp->tp_maxalloc = tp->tp_alloc - tp->tp_free; 112} 113#endif 114 115PyObject * 116PyObject_Init(op, tp) 117 PyObject *op; 118 PyTypeObject *tp; 119{ 120 if (op == NULL) { 121 PyErr_SetString(PyExc_SystemError, 122 "NULL object passed to PyObject_Init"); 123 return op; 124 } 125 /* Any changes should be reflected in PyObject_INIT (objimpl.h) */ 126 op->ob_type = tp; 127 _Py_NewReference(op); 128 return op; 129} 130 131PyVarObject * 132PyObject_InitVar(op, tp, size) 133 PyVarObject *op; 134 PyTypeObject *tp; 135 int size; 136{ 137 if (op == NULL) { 138 PyErr_SetString(PyExc_SystemError, 139 "NULL object passed to PyObject_InitVar"); 140 return op; 141 } 142 /* Any changes should be reflected in PyObject_INIT_VAR */ 143 op->ob_size = size; 144 op->ob_type = tp; 145 _Py_NewReference((PyObject *)op); 146 return op; 147} 148 149PyObject * 150_PyObject_New(tp) 151 PyTypeObject *tp; 152{ 153 PyObject *op; 154 op = (PyObject *) PyObject_MALLOC(_PyObject_SIZE(tp)); 155 if (op == NULL) 156 return PyErr_NoMemory(); 157 return PyObject_INIT(op, tp); 158} 159 160PyVarObject * 161_PyObject_NewVar(tp, size) 162 PyTypeObject *tp; 163 int size; 164{ 165 PyVarObject *op; 166 op = (PyVarObject *) PyObject_MALLOC(_PyObject_VAR_SIZE(tp, size)); 167 if (op == NULL) 168 return (PyVarObject *)PyErr_NoMemory(); 169 return PyObject_INIT_VAR(op, tp, size); 170} 171 172void 173_PyObject_Del(op) 174 PyObject *op; 175{ 176 PyObject_FREE(op); 177} 178 179int 180PyObject_Print(op, fp, flags) 181 PyObject *op; 182 FILE *fp; 183 int flags; 184{ 185 int ret = 0; 186 if (PyErr_CheckSignals()) 187 return -1; 188#ifdef USE_STACKCHECK 189 if (PyOS_CheckStack()) { 190 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); 191 return -1; 192 } 193#endif 194 clearerr(fp); /* Clear any previous error condition */ 195 if (op == NULL) { 196 fprintf(fp, "<nil>"); 197 } 198 else { 199 if (op->ob_refcnt <= 0) 200 fprintf(fp, "<refcnt %u at %lx>", 201 op->ob_refcnt, (long)op); 202 else if (op->ob_type->tp_print == NULL) { 203 if (op->ob_type->tp_repr == NULL) { 204 fprintf(fp, "<%s object at %lx>", 205 op->ob_type->tp_name, (long)op); 206 } 207 else { 208 PyObject *s; 209 if (flags & Py_PRINT_RAW) 210 s = PyObject_Str(op); 211 else 212 s = PyObject_Repr(op); 213 if (s == NULL) 214 ret = -1; 215 else { 216 ret = PyObject_Print(s, fp, 217 Py_PRINT_RAW); 218 } 219 Py_XDECREF(s); 220 } 221 } 222 else 223 ret = (*op->ob_type->tp_print)(op, fp, flags); 224 } 225 if (ret == 0) { 226 if (ferror(fp)) { 227 PyErr_SetFromErrno(PyExc_IOError); 228 clearerr(fp); 229 ret = -1; 230 } 231 } 232 return ret; 233} 234 235PyObject * 236PyObject_Repr(v) 237 PyObject *v; 238{ 239 if (PyErr_CheckSignals()) 240 return NULL; 241#ifdef USE_STACKCHECK 242 if (PyOS_CheckStack()) { 243 PyErr_SetString(PyExc_MemoryError, "Stack overflow"); 244 return NULL; 245 } 246#endif 247 if (v == NULL) 248 return PyString_FromString("<NULL>"); 249 else if (v->ob_type->tp_repr == NULL) { 250 char buf[120]; 251 sprintf(buf, "<%.80s object at %lx>", 252 v->ob_type->tp_name, (long)v); 253 return PyString_FromString(buf); 254 } 255 else { 256 PyObject *res; 257 res = (*v->ob_type->tp_repr)(v); 258 if (res == NULL) 259 return NULL; 260 if (!PyString_Check(res)) { 261 PyErr_Format(PyExc_TypeError, 262 "__repr__ returned non-string (type %.200s)", 263 res->ob_type->tp_name); 264 Py_DECREF(res); 265 return NULL; 266 } 267 return res; 268 } 269} 270 271PyObject * 272PyObject_Str(v) 273 PyObject *v; 274{ 275 PyObject *res; 276 277 if (v == NULL) 278 return PyString_FromString("<NULL>"); 279 else if (PyString_Check(v)) { 280 Py_INCREF(v); 281 return v; 282 } 283 else if (v->ob_type->tp_str != NULL) 284 res = (*v->ob_type->tp_str)(v); 285 else { 286 PyObject *func; 287 if (!PyInstance_Check(v) || 288 (func = PyObject_GetAttrString(v, "__str__")) == NULL) { 289 PyErr_Clear(); 290 return PyObject_Repr(v); 291 } 292 res = PyEval_CallObject(func, (PyObject *)NULL); 293 Py_DECREF(func); 294 } 295 if (res == NULL) 296 return NULL; 297 if (!PyString_Check(res)) { 298 PyErr_Format(PyExc_TypeError, 299 "__str__ returned non-string (type %.200s)", 300 res->ob_type->tp_name); 301 Py_DECREF(res); 302 return NULL; 303 } 304 return res; 305} 306 307static PyObject * 308do_cmp(v, w) 309 PyObject *v, *w; 310{ 311 long c; 312 /* __rcmp__ actually won't be called unless __cmp__ isn't defined, 313 because the check in cmpobject() reverses the objects first. 314 This is intentional -- it makes no sense to define cmp(x,y) 315 different than -cmp(y,x). */ 316 if (PyInstance_Check(v) || PyInstance_Check(w)) 317 return PyInstance_DoBinOp(v, w, "__cmp__", "__rcmp__", do_cmp); 318 c = PyObject_Compare(v, w); 319 if (c && PyErr_Occurred()) 320 return NULL; 321 return PyInt_FromLong(c); 322} 323 324PyObject *_PyCompareState_Key; 325 326/* _PyCompareState_nesting is incremented beforing call compare (for 327 some types) and decremented on exit. If the count exceeds the 328 nesting limit, enable code to detect circular data structures. 329*/ 330#define NESTING_LIMIT 500 331int _PyCompareState_nesting = 0; 332 333static PyObject* 334get_inprogress_dict() 335{ 336 PyObject *tstate_dict, *inprogress; 337 338 tstate_dict = PyThreadState_GetDict(); 339 if (tstate_dict == NULL) { 340 PyErr_BadInternalCall(); 341 return NULL; 342 } 343 inprogress = PyDict_GetItem(tstate_dict, _PyCompareState_Key); 344 if (inprogress == NULL) { 345 PyErr_Clear(); 346 inprogress = PyDict_New(); 347 if (inprogress == NULL) 348 return NULL; 349 if (PyDict_SetItem(tstate_dict, _PyCompareState_Key, 350 inprogress) == -1) { 351 Py_DECREF(inprogress); 352 return NULL; 353 } 354 } 355 return inprogress; 356} 357 358static PyObject * 359make_pair(v, w) 360 PyObject *v, *w; 361{ 362 PyObject *pair; 363 364 pair = PyTuple_New(2); 365 if (pair == NULL) { 366 return NULL; 367 } 368 if ((long)v <= (long)w) { 369 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)v)); 370 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)w)); 371 } else { 372 PyTuple_SET_ITEM(pair, 0, PyLong_FromVoidPtr((void *)w)); 373 PyTuple_SET_ITEM(pair, 1, PyLong_FromVoidPtr((void *)v)); 374 } 375 return pair; 376} 377 378int 379PyObject_Compare(v, w) 380 PyObject *v, *w; 381{ 382 PyTypeObject *vtp, *wtp; 383 int result; 384 385 if (v == NULL || w == NULL) { 386 PyErr_BadInternalCall(); 387 return -1; 388 } 389 if (v == w) 390 return 0; 391 if (PyInstance_Check(v) || PyInstance_Check(w)) { 392 PyObject *res; 393 int c; 394 if (!PyInstance_Check(v)) 395 return -PyObject_Compare(w, v); 396 if (++_PyCompareState_nesting > NESTING_LIMIT) { 397 PyObject *inprogress, *pair; 398 399 inprogress = get_inprogress_dict(); 400 if (inprogress == NULL) { 401 return -1; 402 } 403 pair = make_pair(v, w); 404 if (PyDict_GetItem(inprogress, pair)) { 405 /* already comparing these objects. assume 406 they're equal until shown otherwise */ 407 Py_DECREF(pair); 408 --_PyCompareState_nesting; 409 return 0; 410 } 411 if (PyDict_SetItem(inprogress, pair, pair) == -1) { 412 return -1; 413 } 414 res = do_cmp(v, w); 415 _PyCompareState_nesting--; 416 /* XXX DelItem shouldn't fail */ 417 PyDict_DelItem(inprogress, pair); 418 Py_DECREF(pair); 419 } else { 420 res = do_cmp(v, w); 421 } 422 if (res == NULL) 423 return -1; 424 if (!PyInt_Check(res)) { 425 Py_DECREF(res); 426 PyErr_SetString(PyExc_TypeError, 427 "comparison did not return an int"); 428 return -1; 429 } 430 c = PyInt_AsLong(res); 431 Py_DECREF(res); 432 return (c < 0) ? -1 : (c > 0) ? 1 : 0; 433 } 434 if ((vtp = v->ob_type) != (wtp = w->ob_type)) { 435 char *vname = vtp->tp_name; 436 char *wname = wtp->tp_name; 437 if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) { 438 int err; 439 err = PyNumber_CoerceEx(&v, &w); 440 if (err < 0) 441 return -1; 442 else if (err == 0) { 443 int cmp; 444 vtp = v->ob_type; 445 if (vtp->tp_compare == NULL) 446 cmp = (v < w) ? -1 : 1; 447 else 448 cmp = (*vtp->tp_compare)(v, w); 449 Py_DECREF(v); 450 Py_DECREF(w); 451 return cmp; 452 } 453 } 454 else if (PyUnicode_Check(v) || PyUnicode_Check(w)) { 455 int result = PyUnicode_Compare(v, w); 456 if (result == -1 && PyErr_Occurred() && 457 PyErr_ExceptionMatches(PyExc_TypeError)) 458 /* TypeErrors are ignored: if Unicode coercion 459 fails due to one of the arguments not 460 having the right type, we continue as 461 defined by the coercion protocol (see 462 above). Luckily, decoding errors are 463 reported as ValueErrors and are not masked 464 by this technique. */ 465 PyErr_Clear(); 466 else 467 return result; 468 } 469 else if (vtp->tp_as_number != NULL) 470 vname = ""; 471 else if (wtp->tp_as_number != NULL) 472 wname = ""; 473 /* Numerical types compare smaller than all other types */ 474 return strcmp(vname, wname); 475 } 476 if (vtp->tp_compare == NULL) { 477 return (v < w) ? -1 : 1; 478 } 479 if (++_PyCompareState_nesting > NESTING_LIMIT 480 && (vtp->tp_as_mapping 481 || (vtp->tp_as_sequence && !PyString_Check(v)))) { 482 PyObject *inprogress, *pair; 483 484 inprogress = get_inprogress_dict(); 485 if (inprogress == NULL) { 486 return -1; 487 } 488 pair = make_pair(v, w); 489 if (PyDict_GetItem(inprogress, pair)) { 490 /* already comparing these objects. assume 491 they're equal until shown otherwise */ 492 _PyCompareState_nesting--; 493 Py_DECREF(pair); 494 return 0; 495 } 496 if (PyDict_SetItem(inprogress, pair, pair) == -1) { 497 return -1; 498 } 499 result = (*vtp->tp_compare)(v, w); 500 _PyCompareState_nesting--; 501 PyDict_DelItem(inprogress, pair); /* XXX shouldn't fail */ 502 Py_DECREF(pair); 503 } else { 504 result = (*vtp->tp_compare)(v, w); 505 } 506 return result; 507} 508 509long 510PyObject_Hash(v) 511 PyObject *v; 512{ 513 PyTypeObject *tp = v->ob_type; 514 if (tp->tp_hash != NULL) 515 return (*tp->tp_hash)(v); 516 if (tp->tp_compare == NULL) 517 return (long) v; /* Use address as hash value */ 518 /* If there's a cmp but no hash defined, the object can't be hashed */ 519 PyErr_SetString(PyExc_TypeError, "unhashable type"); 520 return -1; 521} 522 523PyObject * 524PyObject_GetAttrString(v, name) 525 PyObject *v; 526 char *name; 527{ 528 if (v->ob_type->tp_getattro != NULL) { 529 PyObject *w, *res; 530 w = PyString_InternFromString(name); 531 if (w == NULL) 532 return NULL; 533 res = (*v->ob_type->tp_getattro)(v, w); 534 Py_XDECREF(w); 535 return res; 536 } 537 538 if (v->ob_type->tp_getattr == NULL) { 539 PyErr_Format(PyExc_AttributeError, 540 "'%.50s' object has no attribute '%.400s'", 541 v->ob_type->tp_name, 542 name); 543 return NULL; 544 } 545 else { 546 return (*v->ob_type->tp_getattr)(v, name); 547 } 548} 549 550int 551PyObject_HasAttrString(v, name) 552 PyObject *v; 553 char *name; 554{ 555 PyObject *res = PyObject_GetAttrString(v, name); 556 if (res != NULL) { 557 Py_DECREF(res); 558 return 1; 559 } 560 PyErr_Clear(); 561 return 0; 562} 563 564int 565PyObject_SetAttrString(v, name, w) 566 PyObject *v; 567 char *name; 568 PyObject *w; 569{ 570 if (v->ob_type->tp_setattro != NULL) { 571 PyObject *s; 572 int res; 573 s = PyString_InternFromString(name); 574 if (s == NULL) 575 return -1; 576 res = (*v->ob_type->tp_setattro)(v, s, w); 577 Py_XDECREF(s); 578 return res; 579 } 580 581 if (v->ob_type->tp_setattr == NULL) { 582 if (v->ob_type->tp_getattr == NULL) 583 PyErr_SetString(PyExc_TypeError, 584 "attribute-less object (assign or del)"); 585 else 586 PyErr_SetString(PyExc_TypeError, 587 "object has read-only attributes"); 588 return -1; 589 } 590 else { 591 return (*v->ob_type->tp_setattr)(v, name, w); 592 } 593} 594 595PyObject * 596PyObject_GetAttr(v, name) 597 PyObject *v; 598 PyObject *name; 599{ 600 if (v->ob_type->tp_getattro != NULL) 601 return (*v->ob_type->tp_getattro)(v, name); 602 else 603 return PyObject_GetAttrString(v, PyString_AsString(name)); 604} 605 606int 607PyObject_HasAttr(v, name) 608 PyObject *v; 609 PyObject *name; 610{ 611 PyObject *res = PyObject_GetAttr(v, name); 612 if (res != NULL) { 613 Py_DECREF(res); 614 return 1; 615 } 616 PyErr_Clear(); 617 return 0; 618} 619 620int 621PyObject_SetAttr(v, name, value) 622 PyObject *v; 623 PyObject *name; 624 PyObject *value; 625{ 626 int err; 627 Py_INCREF(name); 628 PyString_InternInPlace(&name); 629 if (v->ob_type->tp_setattro != NULL) 630 err = (*v->ob_type->tp_setattro)(v, name, value); 631 else 632 err = PyObject_SetAttrString( 633 v, PyString_AsString(name), value); 634 Py_DECREF(name); 635 return err; 636} 637 638/* Test a value used as condition, e.g., in a for or if statement. 639 Return -1 if an error occurred */ 640 641int 642PyObject_IsTrue(v) 643 PyObject *v; 644{ 645 int res; 646 if (v == Py_None) 647 res = 0; 648 else if (v->ob_type->tp_as_number != NULL && 649 v->ob_type->tp_as_number->nb_nonzero != NULL) 650 res = (*v->ob_type->tp_as_number->nb_nonzero)(v); 651 else if (v->ob_type->tp_as_mapping != NULL && 652 v->ob_type->tp_as_mapping->mp_length != NULL) 653 res = (*v->ob_type->tp_as_mapping->mp_length)(v); 654 else if (v->ob_type->tp_as_sequence != NULL && 655 v->ob_type->tp_as_sequence->sq_length != NULL) 656 res = (*v->ob_type->tp_as_sequence->sq_length)(v); 657 else 658 res = 1; 659 if (res > 0) 660 res = 1; 661 return res; 662} 663 664/* equivalent of 'not v' 665 Return -1 if an error occurred */ 666 667int 668PyObject_Not(v) 669 PyObject *v; 670{ 671 int res; 672 res = PyObject_IsTrue(v); 673 if (res < 0) 674 return res; 675 return res == 0; 676} 677 678/* Coerce two numeric types to the "larger" one. 679 Increment the reference count on each argument. 680 Return -1 and raise an exception if no coercion is possible 681 (and then no reference count is incremented). 682*/ 683 684int 685PyNumber_CoerceEx(pv, pw) 686 PyObject **pv, **pw; 687{ 688 register PyObject *v = *pv; 689 register PyObject *w = *pw; 690 int res; 691 692 if (v->ob_type == w->ob_type && !PyInstance_Check(v)) { 693 Py_INCREF(v); 694 Py_INCREF(w); 695 return 0; 696 } 697 if (v->ob_type->tp_as_number && v->ob_type->tp_as_number->nb_coerce) { 698 res = (*v->ob_type->tp_as_number->nb_coerce)(pv, pw); 699 if (res <= 0) 700 return res; 701 } 702 if (w->ob_type->tp_as_number && w->ob_type->tp_as_number->nb_coerce) { 703 res = (*w->ob_type->tp_as_number->nb_coerce)(pw, pv); 704 if (res <= 0) 705 return res; 706 } 707 return 1; 708} 709 710int 711PyNumber_Coerce(pv, pw) 712 PyObject **pv, **pw; 713{ 714 int err = PyNumber_CoerceEx(pv, pw); 715 if (err <= 0) 716 return err; 717 PyErr_SetString(PyExc_TypeError, "number coercion failed"); 718 return -1; 719} 720 721 722/* Test whether an object can be called */ 723 724int 725PyCallable_Check(x) 726 PyObject *x; 727{ 728 if (x == NULL) 729 return 0; 730 if (x->ob_type->tp_call != NULL || 731 PyFunction_Check(x) || 732 PyMethod_Check(x) || 733 PyCFunction_Check(x) || 734 PyClass_Check(x)) 735 return 1; 736 if (PyInstance_Check(x)) { 737 PyObject *call = PyObject_GetAttrString(x, "__call__"); 738 if (call == NULL) { 739 PyErr_Clear(); 740 return 0; 741 } 742 /* Could test recursively but don't, for fear of endless 743 recursion if some joker sets self.__call__ = self */ 744 Py_DECREF(call); 745 return 1; 746 } 747 return 0; 748} 749 750 751/* 752NoObject is usable as a non-NULL undefined value, used by the macro None. 753There is (and should be!) no way to create other objects of this type, 754so there is exactly one (which is indestructible, by the way). 755*/ 756 757/* ARGSUSED */ 758static PyObject * 759none_repr(op) 760 PyObject *op; 761{ 762 return PyString_FromString("None"); 763} 764 765static PyTypeObject PyNothing_Type = { 766 PyObject_HEAD_INIT(&PyType_Type) 767 0, 768 "None", 769 0, 770 0, 771 0, /*tp_dealloc*/ /*never called*/ 772 0, /*tp_print*/ 773 0, /*tp_getattr*/ 774 0, /*tp_setattr*/ 775 0, /*tp_compare*/ 776 (reprfunc)none_repr, /*tp_repr*/ 777 0, /*tp_as_number*/ 778 0, /*tp_as_sequence*/ 779 0, /*tp_as_mapping*/ 780 0, /*tp_hash */ 781}; 782 783PyObject _Py_NoneStruct = { 784 PyObject_HEAD_INIT(&PyNothing_Type) 785}; 786 787 788#ifdef Py_TRACE_REFS 789 790static PyObject refchain = {&refchain, &refchain}; 791 792void 793_Py_ResetReferences() 794{ 795 refchain._ob_prev = refchain._ob_next = &refchain; 796 _Py_RefTotal = 0; 797} 798 799void 800_Py_NewReference(op) 801 PyObject *op; 802{ 803 _Py_RefTotal++; 804 op->ob_refcnt = 1; 805 op->_ob_next = refchain._ob_next; 806 op->_ob_prev = &refchain; 807 refchain._ob_next->_ob_prev = op; 808 refchain._ob_next = op; 809#ifdef COUNT_ALLOCS 810 inc_count(op->ob_type); 811#endif 812} 813 814void 815_Py_ForgetReference(op) 816 register PyObject *op; 817{ 818#ifdef SLOW_UNREF_CHECK 819 register PyObject *p; 820#endif 821 if (op->ob_refcnt < 0) 822 Py_FatalError("UNREF negative refcnt"); 823 if (op == &refchain || 824 op->_ob_prev->_ob_next != op || op->_ob_next->_ob_prev != op) 825 Py_FatalError("UNREF invalid object"); 826#ifdef SLOW_UNREF_CHECK 827 for (p = refchain._ob_next; p != &refchain; p = p->_ob_next) { 828 if (p == op) 829 break; 830 } 831 if (p == &refchain) /* Not found */ 832 Py_FatalError("UNREF unknown object"); 833#endif 834 op->_ob_next->_ob_prev = op->_ob_prev; 835 op->_ob_prev->_ob_next = op->_ob_next; 836 op->_ob_next = op->_ob_prev = NULL; 837#ifdef COUNT_ALLOCS 838 op->ob_type->tp_free++; 839#endif 840} 841 842void 843_Py_Dealloc(op) 844 PyObject *op; 845{ 846 destructor dealloc = op->ob_type->tp_dealloc; 847 _Py_ForgetReference(op); 848 if (_PyTrash_delete_nesting < PyTrash_UNWIND_LEVEL-1) 849 op->ob_type = NULL; 850 (*dealloc)(op); 851} 852 853void 854_Py_PrintReferences(fp) 855 FILE *fp; 856{ 857 PyObject *op; 858 fprintf(fp, "Remaining objects:\n"); 859 for (op = refchain._ob_next; op != &refchain; op = op->_ob_next) { 860 fprintf(fp, "[%d] ", op->ob_refcnt); 861 if (PyObject_Print(op, fp, 0) != 0) 862 PyErr_Clear(); 863 putc('\n', fp); 864 } 865} 866 867PyObject * 868_Py_GetObjects(self, args) 869 PyObject *self; 870 PyObject *args; 871{ 872 int i, n; 873 PyObject *t = NULL; 874 PyObject *res, *op; 875 876 if (!PyArg_ParseTuple(args, "i|O", &n, &t)) 877 return NULL; 878 op = refchain._ob_next; 879 res = PyList_New(0); 880 if (res == NULL) 881 return NULL; 882 for (i = 0; (n == 0 || i < n) && op != &refchain; i++) { 883 while (op == self || op == args || op == res || op == t || 884 t != NULL && op->ob_type != (PyTypeObject *) t) { 885 op = op->_ob_next; 886 if (op == &refchain) 887 return res; 888 } 889 if (PyList_Append(res, op) < 0) { 890 Py_DECREF(res); 891 return NULL; 892 } 893 op = op->_ob_next; 894 } 895 return res; 896} 897 898#endif 899 900 901/* Hack to force loading of cobject.o */ 902PyTypeObject *_Py_cobject_hack = &PyCObject_Type; 903 904 905/* Hack to force loading of abstract.o */ 906int (*_Py_abstract_hack) Py_FPROTO((PyObject *)) = &PyObject_Length; 907 908 909/* Python's malloc wrappers (see mymalloc.h) */ 910 911ANY * 912PyMem_Malloc(nbytes) 913 size_t nbytes; 914{ 915#if _PyMem_EXTRA > 0 916 if (nbytes == 0) 917 nbytes = _PyMem_EXTRA; 918#endif 919 return PyMem_MALLOC(nbytes); 920} 921 922ANY * 923PyMem_Realloc(p, nbytes) 924 ANY *p; 925 size_t nbytes; 926{ 927#if _PyMem_EXTRA > 0 928 if (nbytes == 0) 929 nbytes = _PyMem_EXTRA; 930#endif 931 return PyMem_REALLOC(p, nbytes); 932} 933 934void 935PyMem_Free(p) 936 ANY *p; 937{ 938 PyMem_FREE(p); 939} 940 941 942/* Python's object malloc wrappers (see objimpl.h) */ 943 944ANY * 945PyObject_Malloc(nbytes) 946 size_t nbytes; 947{ 948 return PyObject_MALLOC(nbytes); 949} 950 951ANY * 952PyObject_Realloc(p, nbytes) 953 ANY *p; 954 size_t nbytes; 955{ 956 return PyObject_REALLOC(p, nbytes); 957} 958 959void 960PyObject_Free(p) 961 ANY *p; 962{ 963 PyObject_FREE(p); 964} 965 966 967/* These methods are used to control infinite recursion in repr, str, print, 968 etc. Container objects that may recursively contain themselves, 969 e.g. builtin dictionaries and lists, should used Py_ReprEnter() and 970 Py_ReprLeave() to avoid infinite recursion. 971 972 Py_ReprEnter() returns 0 the first time it is called for a particular 973 object and 1 every time thereafter. It returns -1 if an exception 974 occurred. Py_ReprLeave() has no return value. 975 976 See dictobject.c and listobject.c for examples of use. 977*/ 978 979#define KEY "Py_Repr" 980 981int 982Py_ReprEnter(obj) 983 PyObject *obj; 984{ 985 PyObject *dict; 986 PyObject *list; 987 int i; 988 989 dict = PyThreadState_GetDict(); 990 if (dict == NULL) 991 return -1; 992 list = PyDict_GetItemString(dict, KEY); 993 if (list == NULL) { 994 list = PyList_New(0); 995 if (list == NULL) 996 return -1; 997 if (PyDict_SetItemString(dict, KEY, list) < 0) 998 return -1; 999 Py_DECREF(list); 1000 } 1001 i = PyList_GET_SIZE(list); 1002 while (--i >= 0) { 1003 if (PyList_GET_ITEM(list, i) == obj) 1004 return 1; 1005 } 1006 PyList_Append(list, obj); 1007 return 0; 1008} 1009 1010void 1011Py_ReprLeave(obj) 1012 PyObject *obj; 1013{ 1014 PyObject *dict; 1015 PyObject *list; 1016 int i; 1017 1018 dict = PyThreadState_GetDict(); 1019 if (dict == NULL) 1020 return; 1021 list = PyDict_GetItemString(dict, KEY); 1022 if (list == NULL || !PyList_Check(list)) 1023 return; 1024 i = PyList_GET_SIZE(list); 1025 /* Count backwards because we always expect obj to be list[-1] */ 1026 while (--i >= 0) { 1027 if (PyList_GET_ITEM(list, i) == obj) { 1028 PyList_SetSlice(list, i, i + 1, NULL); 1029 break; 1030 } 1031 } 1032} 1033 1034/* 1035 trashcan 1036 CT 2k0130 1037 non-recursively destroy nested objects 1038 1039 CT 2k0223 1040 everything is now done in a macro. 1041 1042 CT 2k0305 1043 modified to use functions, after Tim Peter's suggestion. 1044 1045 CT 2k0309 1046 modified to restore a possible error. 1047 1048 CT 2k0325 1049 added better safe than sorry check for threadstate 1050 1051 CT 2k0422 1052 complete rewrite. We now build a chain via ob_type 1053 and save the limited number of types in ob_refcnt. 1054 This is perfect since we don't need any memory. 1055 A patch for free-threading would need just a lock. 1056*/ 1057 1058#define Py_TRASHCAN_TUPLE 1 1059#define Py_TRASHCAN_LIST 2 1060#define Py_TRASHCAN_DICT 3 1061#define Py_TRASHCAN_FRAME 4 1062#define Py_TRASHCAN_TRACEBACK 5 1063/* extend here if other objects want protection */ 1064 1065int _PyTrash_delete_nesting = 0; 1066 1067PyObject * _PyTrash_delete_later = NULL; 1068 1069void 1070_PyTrash_deposit_object(op) 1071 PyObject *op; 1072{ 1073 int typecode; 1074 PyObject *hold = _PyTrash_delete_later; 1075 1076 if (PyTuple_Check(op)) 1077 typecode = Py_TRASHCAN_TUPLE; 1078 else if (PyList_Check(op)) 1079 typecode = Py_TRASHCAN_LIST; 1080 else if (PyDict_Check(op)) 1081 typecode = Py_TRASHCAN_DICT; 1082 else if (PyFrame_Check(op)) 1083 typecode = Py_TRASHCAN_FRAME; 1084 else if (PyTraceBack_Check(op)) 1085 typecode = Py_TRASHCAN_TRACEBACK; 1086 op->ob_refcnt = typecode; 1087 1088 op->ob_type = (PyTypeObject*)_PyTrash_delete_later; 1089 _PyTrash_delete_later = op; 1090} 1091 1092void 1093_PyTrash_destroy_chain() 1094{ 1095 while (_PyTrash_delete_later) { 1096 PyObject *shredder = _PyTrash_delete_later; 1097 _PyTrash_delete_later = (PyObject*) shredder->ob_type; 1098 1099 switch (shredder->ob_refcnt) { 1100 case Py_TRASHCAN_TUPLE: 1101 shredder->ob_type = &PyTuple_Type; 1102 break; 1103 case Py_TRASHCAN_LIST: 1104 shredder->ob_type = &PyList_Type; 1105 break; 1106 case Py_TRASHCAN_DICT: 1107 shredder->ob_type = &PyDict_Type; 1108 break; 1109 case Py_TRASHCAN_FRAME: 1110 shredder->ob_type = &PyFrame_Type; 1111 break; 1112 case Py_TRASHCAN_TRACEBACK: 1113 shredder->ob_type = &PyTraceBack_Type; 1114 break; 1115 } 1116 _Py_NewReference(shredder); 1117 1118 ++_PyTrash_delete_nesting; 1119 Py_DECREF(shredder); 1120 --_PyTrash_delete_nesting; 1121 } 1122} 1123 1124