classobject.c revision b7f1afe4a84c6ca6ecb3005e1ba3ef801a0257e7
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/* Class object implementation */ 33 34#include "Python.h" 35#include "structmember.h" 36 37/* Forward */ 38static PyObject *class_lookup 39 Py_PROTO((PyClassObject *, PyObject *, PyClassObject **)); 40static PyObject *instance_getattr1 Py_PROTO((PyInstanceObject *, PyObject *)); 41 42PyObject * 43PyClass_New(bases, dict, name) 44 PyObject *bases; /* NULL or tuple of classobjects! */ 45 PyObject *dict; 46 PyObject *name; 47{ 48 PyClassObject *op, *dummy; 49 static PyObject *getattrstr, *setattrstr, *delattrstr; 50 static PyObject *docstr, *modstr, *namestr; 51 if (docstr == NULL) { 52 docstr= PyString_InternFromString("__doc__"); 53 if (docstr == NULL) 54 return NULL; 55 } 56 if (modstr == NULL) { 57 modstr= PyString_InternFromString("__module__"); 58 if (modstr == NULL) 59 return NULL; 60 } 61 if (namestr == NULL) { 62 namestr= PyString_InternFromString("__name__"); 63 if (namestr == NULL) 64 return NULL; 65 } 66 if (name == NULL || !PyString_Check(name)) { 67 PyErr_SetString(PyExc_SystemError, 68 "PyClass_New: name must be a string"); 69 return NULL; 70 } 71 if (dict == NULL || !PyDict_Check(dict)) { 72 PyErr_SetString(PyExc_SystemError, 73 "PyClass_New: dict must be a dictionary"); 74 return NULL; 75 } 76 if (PyDict_GetItem(dict, docstr) == NULL) { 77 if (PyDict_SetItem(dict, docstr, Py_None) < 0) 78 return NULL; 79 } 80 if (PyDict_GetItem(dict, modstr) == NULL) { 81 PyObject *globals = PyEval_GetGlobals(); 82 if (globals != NULL) { 83 PyObject *modname = PyDict_GetItem(globals, namestr); 84 if (modname != NULL) { 85 if (PyDict_SetItem(dict, modstr, modname) < 0) 86 return NULL; 87 } 88 } 89 } 90 if (bases == NULL) { 91 bases = PyTuple_New(0); 92 if (bases == NULL) 93 return NULL; 94 } 95 else { 96 int i; 97 if (!PyTuple_Check(bases)) { 98 PyErr_SetString(PyExc_SystemError, 99 "PyClass_New: bases must be a tuple"); 100 return NULL; 101 } 102 i = PyTuple_Size(bases); 103 while (--i >= 0) { 104 if (!PyClass_Check(PyTuple_GetItem(bases, i))) { 105 PyErr_SetString(PyExc_SystemError, 106 "PyClass_New: base must be a class"); 107 return NULL; 108 } 109 } 110 Py_INCREF(bases); 111 } 112 op = PyObject_NEW(PyClassObject, &PyClass_Type); 113 if (op == NULL) { 114 Py_DECREF(bases); 115 return NULL; 116 } 117 op->cl_bases = bases; 118 Py_INCREF(dict); 119 op->cl_dict = dict; 120 Py_XINCREF(name); 121 op->cl_name = name; 122 if (getattrstr == NULL) { 123 getattrstr = PyString_InternFromString("__getattr__"); 124 setattrstr = PyString_InternFromString("__setattr__"); 125 delattrstr = PyString_InternFromString("__delattr__"); 126 } 127 op->cl_getattr = class_lookup(op, getattrstr, &dummy); 128 op->cl_setattr = class_lookup(op, setattrstr, &dummy); 129 op->cl_delattr = class_lookup(op, delattrstr, &dummy); 130 Py_XINCREF(op->cl_getattr); 131 Py_XINCREF(op->cl_setattr); 132 Py_XINCREF(op->cl_delattr); 133 return (PyObject *) op; 134} 135 136/* Class methods */ 137 138static void 139class_dealloc(op) 140 PyClassObject *op; 141{ 142 Py_DECREF(op->cl_bases); 143 Py_DECREF(op->cl_dict); 144 Py_XDECREF(op->cl_name); 145 free((ANY *)op); 146} 147 148static PyObject * 149class_lookup(cp, name, pclass) 150 PyClassObject *cp; 151 PyObject *name; 152 PyClassObject **pclass; 153{ 154 int i, n; 155 PyObject *value = PyDict_GetItem(cp->cl_dict, name); 156 if (value != NULL) { 157 *pclass = cp; 158 return value; 159 } 160 n = PyTuple_Size(cp->cl_bases); 161 for (i = 0; i < n; i++) { 162 /* XXX What if one of the bases is not a class? */ 163 PyObject *v = class_lookup( 164 (PyClassObject *) 165 PyTuple_GetItem(cp->cl_bases, i), name, pclass); 166 if (v != NULL) 167 return v; 168 } 169 return NULL; 170} 171 172static PyObject * 173class_getattr(op, name) 174 register PyClassObject *op; 175 PyObject *name; 176{ 177 register PyObject *v; 178 register char *sname = PyString_AsString(name); 179 PyClassObject *class; 180 if (sname[0] == '_' && sname[1] == '_') { 181 if (strcmp(sname, "__dict__") == 0) { 182 if (PyEval_GetRestricted()) { 183 PyErr_SetString(PyExc_RuntimeError, 184 "class.__dict__ not accessible in restricted mode"); 185 return NULL; 186 } 187 Py_INCREF(op->cl_dict); 188 return op->cl_dict; 189 } 190 if (strcmp(sname, "__bases__") == 0) { 191 Py_INCREF(op->cl_bases); 192 return op->cl_bases; 193 } 194 if (strcmp(sname, "__name__") == 0) { 195 if (op->cl_name == NULL) 196 v = Py_None; 197 else 198 v = op->cl_name; 199 Py_INCREF(v); 200 return v; 201 } 202 } 203 v = class_lookup(op, name, &class); 204 if (v == NULL) { 205 PyErr_SetObject(PyExc_AttributeError, name); 206 return NULL; 207 } 208 Py_INCREF(v); 209 if (PyFunction_Check(v)) { 210 PyObject *w = PyMethod_New(v, (PyObject *)NULL, 211 (PyObject *)class); 212 Py_DECREF(v); 213 v = w; 214 } 215 return v; 216} 217 218static int 219class_setattr(op, name, v) 220 PyClassObject *op; 221 PyObject *name; 222 PyObject *v; 223{ 224 char *sname; 225 if (PyEval_GetRestricted()) { 226 PyErr_SetString(PyExc_RuntimeError, 227 "classes are read-only in restricted mode"); 228 return -1; 229 } 230 sname = PyString_AsString(name); 231 if (sname[0] == '_' && sname[1] == '_') { 232 int n = PyString_Size(name); 233 if (sname[n-1] == '_' && sname[n-2] == '_') { 234 if (strcmp(sname, "__dict__") == 0 || 235 strcmp(sname, "__bases__") == 0 || 236 strcmp(sname, "__name__") == 0 || 237 strcmp(sname, "__getattr__") == 0 || 238 strcmp(sname, "__setattr__") == 0 || 239 strcmp(sname, "__delattr__") == 0) 240 { 241 /* XXX In unrestricted mode, we should 242 XXX allow this -- with a type check */ 243 PyErr_SetString(PyExc_TypeError, 244 "read-only special attribute"); 245 return -1; 246 } 247 } 248 } 249 if (v == NULL) { 250 int rv = PyDict_DelItem(op->cl_dict, name); 251 if (rv < 0) 252 PyErr_SetString(PyExc_AttributeError, 253 "delete non-existing class attribute"); 254 return rv; 255 } 256 else 257 return PyDict_SetItem(op->cl_dict, name, v); 258} 259 260static PyObject * 261class_repr(op) 262 PyClassObject *op; 263{ 264 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); 265 char buf[140]; 266 char *name; 267 if (op->cl_name == NULL || !PyString_Check(op->cl_name)) 268 name = "?"; 269 else 270 name = PyString_AsString(op->cl_name); 271 if (mod == NULL || !PyString_Check(mod)) 272 sprintf(buf, "<class ?.%.100s at %lx>", name, (long)op); 273 else 274 sprintf(buf, "<class %.50s.%.50s at %lx>", 275 PyString_AsString(mod), 276 name, (long)op); 277 return PyString_FromString(buf); 278} 279 280static PyObject * 281class_str(op) 282 PyClassObject *op; 283{ 284 PyObject *mod = PyDict_GetItemString(op->cl_dict, "__module__"); 285 PyObject *name = op->cl_name; 286 PyObject *res; 287 int m, n; 288 289 if (name == NULL || !PyString_Check(name)) 290 return class_repr(op); 291 if (mod == NULL || !PyString_Check(mod)) { 292 Py_INCREF(name); 293 return name; 294 } 295 m = PyString_Size(mod); 296 n = PyString_Size(name); 297 res = PyString_FromStringAndSize((char *)NULL, m+1+n); 298 if (res != NULL) { 299 char *s = PyString_AsString(res); 300 memcpy(s, PyString_AsString(mod), m); 301 s += m; 302 *s++ = '.'; 303 memcpy(s, PyString_AsString(name), n); 304 } 305 return res; 306} 307 308PyTypeObject PyClass_Type = { 309 PyObject_HEAD_INIT(&PyType_Type) 310 0, 311 "class", 312 sizeof(PyClassObject), 313 0, 314 (destructor)class_dealloc, /*tp_dealloc*/ 315 0, /*tp_print*/ 316 0, /*tp_getattr*/ 317 0, /*tp_setattr*/ 318 0, /*tp_compare*/ 319 (reprfunc)class_repr, /*tp_repr*/ 320 0, /*tp_as_number*/ 321 0, /*tp_as_sequence*/ 322 0, /*tp_as_mapping*/ 323 0, /*tp_hash*/ 324 0, /*tp_call*/ 325 (reprfunc)class_str, /*tp_str*/ 326 (getattrofunc)class_getattr, /*tp_getattro*/ 327 (setattrofunc)class_setattr, /*tp_setattro*/ 328}; 329 330int 331PyClass_IsSubclass(class, base) 332 PyObject *class; 333 PyObject *base; 334{ 335 int i, n; 336 PyClassObject *cp; 337 if (class == base) 338 return 1; 339 if (class == NULL || !PyClass_Check(class)) 340 return 0; 341 cp = (PyClassObject *)class; 342 n = PyTuple_Size(cp->cl_bases); 343 for (i = 0; i < n; i++) { 344 if (PyClass_IsSubclass(PyTuple_GetItem(cp->cl_bases, i), base)) 345 return 1; 346 } 347 return 0; 348} 349 350 351/* Instance objects */ 352 353PyObject * 354PyInstance_New(class, arg, kw) 355 PyObject *class; 356 PyObject *arg; 357 PyObject *kw; 358{ 359 register PyInstanceObject *inst; 360 PyObject *init; 361 static PyObject *initstr; 362 if (!PyClass_Check(class)) { 363 PyErr_BadInternalCall(); 364 return NULL; 365 } 366 inst = PyObject_NEW(PyInstanceObject, &PyInstance_Type); 367 if (inst == NULL) 368 return NULL; 369 Py_INCREF(class); 370 inst->in_class = (PyClassObject *)class; 371 inst->in_dict = PyDict_New(); 372 if (inst->in_dict == NULL) { 373 Py_DECREF(inst); 374 return NULL; 375 } 376 if (initstr == NULL) 377 initstr = PyString_InternFromString("__init__"); 378 init = instance_getattr1(inst, initstr); 379 if (init == NULL) { 380 PyErr_Clear(); 381 if ((arg != NULL && (!PyTuple_Check(arg) || 382 PyTuple_Size(arg) != 0)) 383 || (kw != NULL && (!PyDict_Check(kw) || 384 PyDict_Size(kw) != 0))) { 385 PyErr_SetString(PyExc_TypeError, 386 "this constructor takes no arguments"); 387 Py_DECREF(inst); 388 inst = NULL; 389 } 390 } 391 else { 392 PyObject *res = PyEval_CallObjectWithKeywords(init, arg, kw); 393 Py_DECREF(init); 394 if (res == NULL) { 395 Py_DECREF(inst); 396 inst = NULL; 397 } 398 else { 399 if (res != Py_None) { 400 PyErr_SetString(PyExc_TypeError, 401 "__init__() should return None"); 402 Py_DECREF(inst); 403 inst = NULL; 404 } 405 Py_DECREF(res); 406 } 407 } 408 return (PyObject *)inst; 409} 410 411/* Instance methods */ 412 413static void 414instance_dealloc(inst) 415 register PyInstanceObject *inst; 416{ 417 PyObject *error_type, *error_value, *error_traceback; 418 PyObject *del; 419 static PyObject *delstr; 420 /* Call the __del__ method if it exists. First temporarily 421 revive the object and save the current exception, if any. */ 422#ifdef Py_TRACE_REFS 423 /* much too complicated if Py_TRACE_REFS defined */ 424 extern long _Py_RefTotal; 425 inst->ob_type = &PyInstance_Type; 426 _Py_NewReference(inst); 427 _Py_RefTotal--; /* compensate for increment in NEWREF */ 428#ifdef COUNT_ALLOCS 429 inst->ob_type->tp_alloc--; /* ditto */ 430#endif 431#else /* !Py_TRACE_REFS */ 432 Py_INCREF(inst); 433#endif /* !Py_TRACE_REFS */ 434 PyErr_Fetch(&error_type, &error_value, &error_traceback); 435 if (delstr == NULL) 436 delstr = PyString_InternFromString("__del__"); 437 if ((del = instance_getattr1(inst, delstr)) != NULL) { 438 PyObject *res = PyEval_CallObject(del, (PyObject *)NULL); 439 if (res == NULL) { 440 PyObject *f, *t, *v, *tb; 441 PyErr_Fetch(&t, &v, &tb); 442 f = PySys_GetObject("stderr"); 443 if (f != NULL) { 444 PyFile_WriteString("Exception ", f); 445 if (t) { 446 PyFile_WriteObject(t, f, Py_PRINT_RAW); 447 if (v && v != Py_None) { 448 PyFile_WriteString(": ", f); 449 PyFile_WriteObject(v, f, 0); 450 } 451 } 452 PyFile_WriteString(" in ", f); 453 PyFile_WriteObject(del, f, 0); 454 PyFile_WriteString(" ignored\n", f); 455 PyErr_Clear(); /* Just in case */ 456 } 457 Py_XDECREF(t); 458 Py_XDECREF(v); 459 Py_XDECREF(tb); 460 } 461 else 462 Py_DECREF(res); 463 Py_DECREF(del); 464 } 465 /* Restore the saved exception and undo the temporary revival */ 466 PyErr_Restore(error_type, error_value, error_traceback); 467 /* Can't use DECREF here, it would cause a recursive call */ 468 if (--inst->ob_refcnt > 0) { 469#ifdef COUNT_ALLOCS 470 inst->ob_type->tp_free--; 471#endif 472 return; /* __del__ added a reference; don't delete now */ 473 } 474#ifdef Py_TRACE_REFS 475#ifdef COUNT_ALLOCS 476 inst->ob_type->tp_free--; /* compensate for increment in UNREF */ 477#endif 478 _Py_ForgetReference(inst); 479 inst->ob_type = NULL; 480#endif /* Py_TRACE_REFS */ 481 Py_DECREF(inst->in_class); 482 Py_XDECREF(inst->in_dict); 483 free((ANY *)inst); 484} 485 486static PyObject * 487instance_getattr1(inst, name) 488 register PyInstanceObject *inst; 489 PyObject *name; 490{ 491 register PyObject *v; 492 register char *sname = PyString_AsString(name); 493 PyClassObject *class; 494 if (sname[0] == '_' && sname[1] == '_') { 495 if (strcmp(sname, "__dict__") == 0) { 496 if (PyEval_GetRestricted()) { 497 PyErr_SetString(PyExc_RuntimeError, 498 "instance.__dict__ not accessible in restricted mode"); 499 return NULL; 500 } 501 Py_INCREF(inst->in_dict); 502 return inst->in_dict; 503 } 504 if (strcmp(sname, "__class__") == 0) { 505 Py_INCREF(inst->in_class); 506 return (PyObject *)inst->in_class; 507 } 508 } 509 class = NULL; 510 v = PyDict_GetItem(inst->in_dict, name); 511 if (v == NULL) { 512 v = class_lookup(inst->in_class, name, &class); 513 if (v == NULL) { 514 PyErr_SetObject(PyExc_AttributeError, name); 515 return NULL; 516 } 517 } 518 Py_INCREF(v); 519 if (class != NULL) { 520 if (PyFunction_Check(v)) { 521 PyObject *w = PyMethod_New(v, (PyObject *)inst, 522 (PyObject *)class); 523 Py_DECREF(v); 524 v = w; 525 } 526 else if (PyMethod_Check(v)) { 527 PyObject *im_class = PyMethod_Class(v); 528 /* Only if classes are compatible */ 529 if (PyClass_IsSubclass((PyObject *)class, im_class)) { 530 PyObject *im_func = PyMethod_Function(v); 531 PyObject *w = PyMethod_New(im_func, 532 (PyObject *)inst, im_class); 533 Py_DECREF(v); 534 v = w; 535 } 536 } 537 } 538 return v; 539} 540 541static PyObject * 542instance_getattr(inst, name) 543 register PyInstanceObject *inst; 544 PyObject *name; 545{ 546 register PyObject *func, *res; 547 res = instance_getattr1(inst, name); 548 if (res == NULL && (func = inst->in_class->cl_getattr) != NULL) { 549 PyObject *args; 550 PyErr_Clear(); 551 args = Py_BuildValue("(OO)", inst, name); 552 if (args == NULL) 553 return NULL; 554 res = PyEval_CallObject(func, args); 555 Py_DECREF(args); 556 } 557 return res; 558} 559 560static int 561instance_setattr1(inst, name, v) 562 PyInstanceObject *inst; 563 PyObject *name; 564 PyObject *v; 565{ 566 if (v == NULL) { 567 int rv = PyDict_DelItem(inst->in_dict, name); 568 if (rv < 0) 569 PyErr_SetString(PyExc_AttributeError, 570 "delete non-existing instance attribute"); 571 return rv; 572 } 573 else 574 return PyDict_SetItem(inst->in_dict, name, v); 575} 576 577static int 578instance_setattr(inst, name, v) 579 PyInstanceObject *inst; 580 PyObject *name; 581 PyObject *v; 582{ 583 PyObject *func, *args, *res, *tmp; 584 char *sname = PyString_AsString(name); 585 if (sname[0] == '_' && sname[1] == '_') { 586 int n = PyString_Size(name); 587 if (sname[n-1] == '_' && sname[n-2] == '_') { 588 if (strcmp(sname, "__dict__") == 0) { 589 if (PyEval_GetRestricted()) { 590 PyErr_SetString(PyExc_RuntimeError, 591 "__dict__ not accessible in restricted mode"); 592 return -1; 593 } 594 if (v == NULL || !PyDict_Check(v)) { 595 PyErr_SetString(PyExc_TypeError, 596 "__dict__ must be set to a dictionary"); 597 return -1; 598 } 599 tmp = inst->in_dict; 600 Py_INCREF(v); 601 inst->in_dict = v; 602 Py_DECREF(tmp); 603 return 0; 604 } 605 if (strcmp(sname, "__class__") == 0) { 606 if (PyEval_GetRestricted()) { 607 PyErr_SetString(PyExc_RuntimeError, 608 "__class__ not accessible in restricted mode"); 609 return -1; 610 } 611 if (v == NULL || !PyClass_Check(v)) { 612 PyErr_SetString(PyExc_TypeError, 613 "__class__ must be set to a class"); 614 return -1; 615 } 616 tmp = (PyObject *)(inst->in_class); 617 Py_INCREF(v); 618 inst->in_class = (PyClassObject *)v; 619 Py_DECREF(tmp); 620 return 0; 621 } 622 } 623 } 624 if (v == NULL) 625 func = inst->in_class->cl_delattr; 626 else 627 func = inst->in_class->cl_setattr; 628 if (func == NULL) 629 return instance_setattr1(inst, name, v); 630 if (v == NULL) 631 args = Py_BuildValue("(OO)", inst, name); 632 else 633 args = Py_BuildValue("(OOO)", inst, name, v); 634 if (args == NULL) 635 return -1; 636 res = PyEval_CallObject(func, args); 637 Py_DECREF(args); 638 if (res == NULL) 639 return -1; 640 Py_DECREF(res); 641 return 0; 642} 643 644static PyObject * 645instance_repr(inst) 646 PyInstanceObject *inst; 647{ 648 PyObject *func; 649 PyObject *res; 650 static PyObject *reprstr; 651 652 if (reprstr == NULL) 653 reprstr = PyString_InternFromString("__repr__"); 654 func = instance_getattr(inst, reprstr); 655 if (func == NULL) { 656 char buf[140]; 657 PyObject *classname = inst->in_class->cl_name; 658 PyObject *mod = PyDict_GetItemString( 659 inst->in_class->cl_dict, "__module__"); 660 char *cname; 661 if (classname != NULL && PyString_Check(classname)) 662 cname = PyString_AsString(classname); 663 else 664 cname = "?"; 665 PyErr_Clear(); 666 if (mod == NULL || !PyString_Check(mod)) 667 sprintf(buf, "<?.%.100s instance at %lx>", 668 cname, (long)inst); 669 else 670 sprintf(buf, "<%.50s.%.50s instance at %lx>", 671 PyString_AsString(mod), 672 cname, (long)inst); 673 return PyString_FromString(buf); 674 } 675 res = PyEval_CallObject(func, (PyObject *)NULL); 676 Py_DECREF(func); 677 return res; 678} 679 680static PyObject * 681instance_compare1(inst, other) 682 PyObject *inst, *other; 683{ 684 return PyInstance_DoBinOp(inst, other, "__cmp__", "__rcmp__", 685 instance_compare1); 686} 687 688static int 689instance_compare(inst, other) 690 PyObject *inst, *other; 691{ 692 PyObject *result; 693 long outcome; 694 result = instance_compare1(inst, other); 695 if (result == NULL) 696 return -1; 697 if (!PyInt_Check(result)) { 698 Py_DECREF(result); 699 PyErr_SetString(PyExc_TypeError, 700 "comparison did not return an int"); 701 return -1; 702 } 703 outcome = PyInt_AsLong(result); 704 Py_DECREF(result); 705 if (outcome < 0) 706 return -1; 707 else if (outcome > 0) 708 return 1; 709 return 0; 710} 711 712static long 713instance_hash(inst) 714 PyInstanceObject *inst; 715{ 716 PyObject *func; 717 PyObject *res; 718 long outcome; 719 static PyObject *hashstr, *cmpstr; 720 721 if (hashstr == NULL) 722 hashstr = PyString_InternFromString("__hash__"); 723 func = instance_getattr(inst, hashstr); 724 if (func == NULL) { 725 /* If there is no __cmp__ method, we hash on the address. 726 If a __cmp__ method exists, there must be a __hash__. */ 727 PyErr_Clear(); 728 if (cmpstr == NULL) 729 cmpstr = PyString_InternFromString("__cmp__"); 730 func = instance_getattr(inst, cmpstr); 731 if (func == NULL) { 732 PyErr_Clear(); 733 outcome = (long)inst; 734 if (outcome == -1) 735 outcome = -2; 736 return outcome; 737 } 738 PyErr_SetString(PyExc_TypeError, "unhashable instance"); 739 return -1; 740 } 741 res = PyEval_CallObject(func, (PyObject *)NULL); 742 Py_DECREF(func); 743 if (res == NULL) 744 return -1; 745 if (PyInt_Check(res)) { 746 outcome = PyInt_AsLong(res); 747 if (outcome == -1) 748 outcome = -2; 749 } 750 else { 751 PyErr_SetString(PyExc_TypeError, 752 "__hash__() should return an int"); 753 outcome = -1; 754 } 755 Py_DECREF(res); 756 return outcome; 757} 758 759static PyObject *getitemstr, *setitemstr, *delitemstr, *lenstr; 760 761static int 762instance_length(inst) 763 PyInstanceObject *inst; 764{ 765 PyObject *func; 766 PyObject *res; 767 int outcome; 768 769 if (lenstr == NULL) 770 lenstr = PyString_InternFromString("__len__"); 771 func = instance_getattr(inst, lenstr); 772 if (func == NULL) 773 return -1; 774 res = PyEval_CallObject(func, (PyObject *)NULL); 775 Py_DECREF(func); 776 if (res == NULL) 777 return -1; 778 if (PyInt_Check(res)) { 779 outcome = PyInt_AsLong(res); 780 if (outcome < 0) 781 PyErr_SetString(PyExc_ValueError, 782 "__len__() should return >= 0"); 783 } 784 else { 785 PyErr_SetString(PyExc_TypeError, 786 "__len__() should return an int"); 787 outcome = -1; 788 } 789 Py_DECREF(res); 790 return outcome; 791} 792 793static PyObject * 794instance_subscript(inst, key) 795 PyInstanceObject *inst; 796 PyObject *key; 797{ 798 PyObject *func; 799 PyObject *arg; 800 PyObject *res; 801 802 if (getitemstr == NULL) 803 getitemstr = PyString_InternFromString("__getitem__"); 804 func = instance_getattr(inst, getitemstr); 805 if (func == NULL) 806 return NULL; 807 arg = Py_BuildValue("(O)", key); 808 if (arg == NULL) { 809 Py_DECREF(func); 810 return NULL; 811 } 812 res = PyEval_CallObject(func, arg); 813 Py_DECREF(func); 814 Py_DECREF(arg); 815 return res; 816} 817 818static int 819instance_ass_subscript(inst, key, value) 820 PyInstanceObject*inst; 821 PyObject *key; 822 PyObject *value; 823{ 824 PyObject *func; 825 PyObject *arg; 826 PyObject *res; 827 828 if (value == NULL) { 829 if (delitemstr == NULL) 830 delitemstr = PyString_InternFromString("__delitem__"); 831 func = instance_getattr(inst, delitemstr); 832 } 833 else { 834 if (setitemstr == NULL) 835 setitemstr = PyString_InternFromString("__setitem__"); 836 func = instance_getattr(inst, setitemstr); 837 } 838 if (func == NULL) 839 return -1; 840 if (value == NULL) 841 arg = Py_BuildValue("(O)", key); 842 else 843 arg = Py_BuildValue("(OO)", key, value); 844 if (arg == NULL) { 845 Py_DECREF(func); 846 return -1; 847 } 848 res = PyEval_CallObject(func, arg); 849 Py_DECREF(func); 850 Py_DECREF(arg); 851 if (res == NULL) 852 return -1; 853 Py_DECREF(res); 854 return 0; 855} 856 857static PyMappingMethods instance_as_mapping = { 858 (inquiry)instance_length, /*mp_length*/ 859 (binaryfunc)instance_subscript, /*mp_subscript*/ 860 (objobjargproc)instance_ass_subscript, /*mp_ass_subscript*/ 861}; 862 863static PyObject * 864instance_item(inst, i) 865 PyInstanceObject *inst; 866 int i; 867{ 868 PyObject *func, *arg, *res; 869 870 if (getitemstr == NULL) 871 getitemstr = PyString_InternFromString("__getitem__"); 872 func = instance_getattr(inst, getitemstr); 873 if (func == NULL) 874 return NULL; 875 arg = Py_BuildValue("(i)", i); 876 if (arg == NULL) { 877 Py_DECREF(func); 878 return NULL; 879 } 880 res = PyEval_CallObject(func, arg); 881 Py_DECREF(func); 882 Py_DECREF(arg); 883 return res; 884} 885 886static PyObject * 887instance_slice(inst, i, j) 888 PyInstanceObject *inst; 889 int i, j; 890{ 891 PyObject *func, *arg, *res; 892 static PyObject *getslicestr; 893 894 if (getslicestr == NULL) 895 getslicestr = PyString_InternFromString("__getslice__"); 896 func = instance_getattr(inst, getslicestr); 897 if (func == NULL) 898 return NULL; 899 arg = Py_BuildValue("(ii)", i, j); 900 if (arg == NULL) { 901 Py_DECREF(func); 902 return NULL; 903 } 904 res = PyEval_CallObject(func, arg); 905 Py_DECREF(func); 906 Py_DECREF(arg); 907 return res; 908} 909 910static int 911instance_ass_item(inst, i, item) 912 PyInstanceObject *inst; 913 int i; 914 PyObject *item; 915{ 916 PyObject *func, *arg, *res; 917 918 if (item == NULL) { 919 if (delitemstr == NULL) 920 delitemstr = PyString_InternFromString("__delitem__"); 921 func = instance_getattr(inst, delitemstr); 922 } 923 else { 924 if (setitemstr == NULL) 925 setitemstr = PyString_InternFromString("__setitem__"); 926 func = instance_getattr(inst, setitemstr); 927 } 928 if (func == NULL) 929 return -1; 930 if (item == NULL) 931 arg = Py_BuildValue("i", i); 932 else 933 arg = Py_BuildValue("(iO)", i, item); 934 if (arg == NULL) { 935 Py_DECREF(func); 936 return -1; 937 } 938 res = PyEval_CallObject(func, arg); 939 Py_DECREF(func); 940 Py_DECREF(arg); 941 if (res == NULL) 942 return -1; 943 Py_DECREF(res); 944 return 0; 945} 946 947static int 948instance_ass_slice(inst, i, j, value) 949 PyInstanceObject *inst; 950 int i, j; 951 PyObject *value; 952{ 953 PyObject *func, *arg, *res; 954 static PyObject *setslicestr, *delslicestr; 955 956 if (value == NULL) { 957 if (delslicestr == NULL) 958 delslicestr = 959 PyString_InternFromString("__delslice__"); 960 func = instance_getattr(inst, delslicestr); 961 } 962 else { 963 if (setslicestr == NULL) 964 setslicestr = 965 PyString_InternFromString("__setslice__"); 966 func = instance_getattr(inst, setslicestr); 967 } 968 if (func == NULL) 969 return -1; 970 if (value == NULL) 971 arg = Py_BuildValue("(ii)", i, j); 972 else 973 arg = Py_BuildValue("(iiO)", i, j, value); 974 if (arg == NULL) { 975 Py_DECREF(func); 976 return -1; 977 } 978 res = PyEval_CallObject(func, arg); 979 Py_DECREF(func); 980 Py_DECREF(arg); 981 if (res == NULL) 982 return -1; 983 Py_DECREF(res); 984 return 0; 985} 986 987static PySequenceMethods instance_as_sequence = { 988 (inquiry)instance_length, /*sq_length*/ 989 0, /*sq_concat*/ 990 0, /*sq_repeat*/ 991 (intargfunc)instance_item, /*sq_item*/ 992 (intintargfunc)instance_slice, /*sq_slice*/ 993 (intobjargproc)instance_ass_item, /*sq_ass_item*/ 994 (intintobjargproc)instance_ass_slice, /*sq_ass_slice*/ 995}; 996 997static PyObject * 998generic_unary_op(self, methodname) 999 PyInstanceObject *self; 1000 PyObject *methodname; 1001{ 1002 PyObject *func, *res; 1003 1004 if ((func = instance_getattr(self, methodname)) == NULL) 1005 return NULL; 1006 res = PyEval_CallObject(func, (PyObject *)NULL); 1007 Py_DECREF(func); 1008 return res; 1009} 1010 1011 1012/* Forward */ 1013static int halfbinop Py_PROTO((PyObject *, PyObject *, char *, PyObject **, 1014 PyObject * (*) Py_PROTO((PyObject *, PyObject *)), int )); 1015 1016 1017/* Implement a binary operator involving at least one class instance. */ 1018 1019PyObject * 1020PyInstance_DoBinOp(v, w, opname, ropname, thisfunc) 1021 PyObject *v; 1022 PyObject *w; 1023 char *opname; 1024 char *ropname; 1025 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); 1026{ 1027 char buf[256]; 1028 PyObject *result = NULL; 1029 if (halfbinop(v, w, opname, &result, thisfunc, 0) <= 0) 1030 return result; 1031 if (halfbinop(w, v, ropname, &result, thisfunc, 1) <= 0) 1032 return result; 1033 /* Sigh -- special case for comnparisons */ 1034 if (strcmp(opname, "__cmp__") == 0) { 1035 long c = (v < w) ? -1 : (v > w) ? 1 : 0; 1036 return PyInt_FromLong(c); 1037 } 1038 sprintf(buf, "%s nor %s defined for these operands", opname, ropname); 1039 PyErr_SetString(PyExc_TypeError, buf); 1040 return NULL; 1041} 1042 1043 1044/* Try one half of a binary operator involving a class instance. 1045 Return value: 1046 -1 if an exception is to be reported right away 1047 0 if we have a valid result 1048 1 if we could try another operation 1049*/ 1050 1051static PyObject *coerce_obj; 1052 1053static int 1054halfbinop(v, w, opname, r_result, thisfunc, swapped) 1055 PyObject *v; 1056 PyObject *w; 1057 char *opname; 1058 PyObject **r_result; 1059 PyObject * (*thisfunc) Py_PROTO((PyObject *, PyObject *)); 1060 int swapped; 1061{ 1062 PyObject *func; 1063 PyObject *args; 1064 PyObject *coercefunc; 1065 PyObject *coerced = NULL; 1066 PyObject *v1; 1067 1068 if (!PyInstance_Check(v)) 1069 return 1; 1070 if (coerce_obj == NULL) { 1071 coerce_obj = PyString_InternFromString("__coerce__"); 1072 if (coerce_obj == NULL) 1073 return -1; 1074 } 1075 coercefunc = PyObject_GetAttr(v, coerce_obj); 1076 if (coercefunc == NULL) { 1077 PyErr_Clear(); 1078 } 1079 else { 1080 args = Py_BuildValue("(O)", w); 1081 if (args == NULL) { 1082 return -1; 1083 } 1084 coerced = PyEval_CallObject(coercefunc, args); 1085 Py_DECREF(args); 1086 Py_DECREF(coercefunc); 1087 if (coerced == NULL) { 1088 return -1; 1089 } 1090 if (coerced == Py_None) { 1091 Py_DECREF(coerced); 1092 return 1; 1093 } 1094 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { 1095 Py_DECREF(coerced); 1096 PyErr_SetString(PyExc_TypeError, 1097 "coercion should return None or 2-tuple"); 1098 return -1; 1099 } 1100 v1 = PyTuple_GetItem(coerced, 0); 1101 w = PyTuple_GetItem(coerced, 1); 1102 if (v1 != v) { 1103 v = v1; 1104 if (!PyInstance_Check(v) && !PyInstance_Check(w)) { 1105 if (swapped) 1106 *r_result = (*thisfunc)(w, v); 1107 else 1108 *r_result = (*thisfunc)(v, w); 1109 Py_DECREF(coerced); 1110 return *r_result == NULL ? -1 : 0; 1111 } 1112 } 1113 w = PyTuple_GetItem(coerced, 1); 1114 } 1115 func = PyObject_GetAttrString(v, opname); 1116 if (func == NULL) { 1117 Py_XDECREF(coerced); 1118 if (PyErr_Occurred() != PyExc_AttributeError) 1119 return -1; 1120 PyErr_Clear(); 1121 return 1; 1122 } 1123 args = Py_BuildValue("(O)", w); 1124 if (args == NULL) { 1125 Py_DECREF(func); 1126 Py_XDECREF(coerced); 1127 return -1; 1128 } 1129 *r_result = PyEval_CallObject(func, args); 1130 Py_DECREF(args); 1131 Py_DECREF(func); 1132 Py_XDECREF(coerced); 1133 return *r_result == NULL ? -1 : 0; 1134} 1135 1136static int 1137instance_coerce(pv, pw) 1138 PyObject **pv; 1139 PyObject **pw; 1140{ 1141 PyObject *v = *pv; 1142 PyObject *w = *pw; 1143 PyObject *coercefunc; 1144 PyObject *args; 1145 PyObject *coerced; 1146 1147 if (coerce_obj == NULL) { 1148 coerce_obj = PyString_InternFromString("__coerce__"); 1149 if (coerce_obj == NULL) 1150 return -1; 1151 } 1152 coercefunc = PyObject_GetAttr(v, coerce_obj); 1153 if (coercefunc == NULL) { 1154 /* No __coerce__ method: always OK */ 1155 PyErr_Clear(); 1156 Py_INCREF(v); 1157 Py_INCREF(w); 1158 return 0; 1159 } 1160 /* Has __coerce__ method: call it */ 1161 args = Py_BuildValue("(O)", w); 1162 if (args == NULL) { 1163 return -1; 1164 } 1165 coerced = PyEval_CallObject(coercefunc, args); 1166 Py_DECREF(args); 1167 Py_DECREF(coercefunc); 1168 if (coerced == NULL) { 1169 /* __coerce__ call raised an exception */ 1170 return -1; 1171 } 1172 if (coerced == Py_None) { 1173 /* __coerce__ says "I can't do it" */ 1174 Py_DECREF(coerced); 1175 return 1; 1176 } 1177 if (!PyTuple_Check(coerced) || PyTuple_Size(coerced) != 2) { 1178 /* __coerce__ return value is malformed */ 1179 Py_DECREF(coerced); 1180 PyErr_SetString(PyExc_TypeError, 1181 "coercion should return None or 2-tuple"); 1182 return -1; 1183 } 1184 /* __coerce__ returned two new values */ 1185 *pv = PyTuple_GetItem(coerced, 0); 1186 *pw = PyTuple_GetItem(coerced, 1); 1187 Py_INCREF(*pv); 1188 Py_INCREF(*pw); 1189 Py_DECREF(coerced); 1190 return 0; 1191} 1192 1193 1194#define UNARY(funcname, methodname) \ 1195static PyObject *funcname(self) PyInstanceObject *self; { \ 1196 static PyObject *o; \ 1197 if (o == NULL) o = PyString_InternFromString(methodname); \ 1198 return generic_unary_op(self, o); \ 1199} 1200 1201UNARY(instance_neg, "__neg__") 1202UNARY(instance_pos, "__pos__") 1203UNARY(instance_abs, "__abs__") 1204 1205static int 1206instance_nonzero(self) 1207 PyInstanceObject *self; 1208{ 1209 PyObject *func, *res; 1210 long outcome; 1211 static PyObject *nonzerostr; 1212 1213 if (nonzerostr == NULL) 1214 nonzerostr = PyString_InternFromString("__nonzero__"); 1215 if ((func = instance_getattr(self, nonzerostr)) == NULL) { 1216 PyErr_Clear(); 1217 if (lenstr == NULL) 1218 lenstr = PyString_InternFromString("__len__"); 1219 if ((func = instance_getattr(self, lenstr)) == NULL) { 1220 PyErr_Clear(); 1221 /* Fall back to the default behavior: 1222 all instances are nonzero */ 1223 return 1; 1224 } 1225 } 1226 res = PyEval_CallObject(func, (PyObject *)NULL); 1227 Py_DECREF(func); 1228 if (res == NULL) 1229 return -1; 1230 if (!PyInt_Check(res)) { 1231 Py_DECREF(res); 1232 PyErr_SetString(PyExc_TypeError, 1233 "__nonzero__ should return an int"); 1234 return -1; 1235 } 1236 outcome = PyInt_AsLong(res); 1237 Py_DECREF(res); 1238 if (outcome < 0) { 1239 PyErr_SetString(PyExc_ValueError, 1240 "__nonzero__ should return >= 0"); 1241 return -1; 1242 } 1243 return outcome > 0; 1244} 1245 1246UNARY(instance_invert, "__invert__") 1247UNARY(instance_int, "__int__") 1248UNARY(instance_long, "__long__") 1249UNARY(instance_float, "__float__") 1250UNARY(instance_oct, "__oct__") 1251UNARY(instance_hex, "__hex__") 1252 1253/* This version is for ternary calls only (z != None) */ 1254static PyObject * 1255instance_pow(v, w, z) 1256 PyObject *v; 1257 PyObject *w; 1258 PyObject *z; 1259{ 1260 /* XXX Doesn't do coercions... */ 1261 PyObject *func; 1262 PyObject *args; 1263 PyObject *result; 1264 static PyObject *powstr; 1265 1266 if (powstr == NULL) 1267 powstr = PyString_InternFromString("__pow__"); 1268 func = PyObject_GetAttr(v, powstr); 1269 if (func == NULL) 1270 return NULL; 1271 args = Py_BuildValue("(OO)", w, z); 1272 if (args == NULL) { 1273 Py_DECREF(func); 1274 return NULL; 1275 } 1276 result = PyEval_CallObject(func, args); 1277 Py_DECREF(func); 1278 Py_DECREF(args); 1279 return result; 1280} 1281 1282static PyNumberMethods instance_as_number = { 1283 0, /*nb_add*/ 1284 0, /*nb_subtract*/ 1285 0, /*nb_multiply*/ 1286 0, /*nb_divide*/ 1287 0, /*nb_remainder*/ 1288 0, /*nb_divmod*/ 1289 (ternaryfunc)instance_pow, /*nb_power*/ 1290 (unaryfunc)instance_neg, /*nb_negative*/ 1291 (unaryfunc)instance_pos, /*nb_positive*/ 1292 (unaryfunc)instance_abs, /*nb_absolute*/ 1293 (inquiry)instance_nonzero, /*nb_nonzero*/ 1294 (unaryfunc)instance_invert, /*nb_invert*/ 1295 0, /*nb_lshift*/ 1296 0, /*nb_rshift*/ 1297 0, /*nb_and*/ 1298 0, /*nb_xor*/ 1299 0, /*nb_or*/ 1300 (coercion)instance_coerce, /*nb_coerce*/ 1301 (unaryfunc)instance_int, /*nb_int*/ 1302 (unaryfunc)instance_long, /*nb_long*/ 1303 (unaryfunc)instance_float, /*nb_float*/ 1304 (unaryfunc)instance_oct, /*nb_oct*/ 1305 (unaryfunc)instance_hex, /*nb_hex*/ 1306}; 1307 1308PyTypeObject PyInstance_Type = { 1309 PyObject_HEAD_INIT(&PyType_Type) 1310 0, 1311 "instance", 1312 sizeof(PyInstanceObject), 1313 0, 1314 (destructor)instance_dealloc, /*tp_dealloc*/ 1315 0, /*tp_print*/ 1316 0, /*tp_getattr*/ 1317 0, /*tp_setattr*/ 1318 instance_compare, /*tp_compare*/ 1319 (reprfunc)instance_repr, /*tp_repr*/ 1320 &instance_as_number, /*tp_as_number*/ 1321 &instance_as_sequence, /*tp_as_sequence*/ 1322 &instance_as_mapping, /*tp_as_mapping*/ 1323 (hashfunc)instance_hash, /*tp_hash*/ 1324 0, /*tp_call*/ 1325 0, /*tp_str*/ 1326 (getattrofunc)instance_getattr, /*tp_getattro*/ 1327 (setattrofunc)instance_setattr, /*tp_setattro*/ 1328}; 1329 1330 1331/* Instance method objects are used for two purposes: 1332 (a) as bound instance methods (returned by instancename.methodname) 1333 (b) as unbound methods (returned by ClassName.methodname) 1334 In case (b), im_self is NULL 1335*/ 1336 1337typedef struct { 1338 PyObject_HEAD 1339 PyObject *im_func; /* The function implementing the method */ 1340 PyObject *im_self; /* The instance it is bound to, or NULL */ 1341 PyObject *im_class; /* The class that defined the method */ 1342} PyMethodObject; 1343 1344 1345static PyMethodObject *free_list; 1346 1347PyObject * 1348PyMethod_New(func, self, class) 1349 PyObject *func; 1350 PyObject *self; 1351 PyObject *class; 1352{ 1353 register PyMethodObject *im; 1354 if (!PyFunction_Check(func)) { 1355 PyErr_BadInternalCall(); 1356 return NULL; 1357 } 1358 im = free_list; 1359 if (im != NULL) { 1360 free_list = (PyMethodObject *)(im->im_self); 1361 im->ob_type = &PyMethod_Type; 1362 _Py_NewReference(im); 1363 } 1364 else { 1365 im = PyObject_NEW(PyMethodObject, &PyMethod_Type); 1366 if (im == NULL) 1367 return NULL; 1368 } 1369 Py_INCREF(func); 1370 im->im_func = func; 1371 Py_XINCREF(self); 1372 im->im_self = self; 1373 Py_INCREF(class); 1374 im->im_class = class; 1375 return (PyObject *)im; 1376} 1377 1378PyObject * 1379PyMethod_Function(im) 1380 register PyObject *im; 1381{ 1382 if (!PyMethod_Check(im)) { 1383 PyErr_BadInternalCall(); 1384 return NULL; 1385 } 1386 return ((PyMethodObject *)im)->im_func; 1387} 1388 1389PyObject * 1390PyMethod_Self(im) 1391 register PyObject *im; 1392{ 1393 if (!PyMethod_Check(im)) { 1394 PyErr_BadInternalCall(); 1395 return NULL; 1396 } 1397 return ((PyMethodObject *)im)->im_self; 1398} 1399 1400PyObject * 1401PyMethod_Class(im) 1402 register PyObject *im; 1403{ 1404 if (!PyMethod_Check(im)) { 1405 PyErr_BadInternalCall(); 1406 return NULL; 1407 } 1408 return ((PyMethodObject *)im)->im_class; 1409} 1410 1411/* Class method methods */ 1412 1413#define OFF(x) offsetof(PyMethodObject, x) 1414 1415static struct memberlist instancemethod_memberlist[] = { 1416 {"im_func", T_OBJECT, OFF(im_func)}, 1417 {"im_self", T_OBJECT, OFF(im_self)}, 1418 {"im_class", T_OBJECT, OFF(im_class)}, 1419 /* Dummies that are not handled by getattr() except for __members__ */ 1420 {"__doc__", T_INT, 0}, 1421 {"__name__", T_INT, 0}, 1422 {NULL} /* Sentinel */ 1423}; 1424 1425static PyObject * 1426instancemethod_getattr(im, name) 1427 register PyMethodObject *im; 1428 PyObject *name; 1429{ 1430 char *sname = PyString_AsString(name); 1431 if (sname[0] == '_') { 1432 PyFunctionObject *func = (PyFunctionObject *)(im->im_func); 1433 if (strcmp(sname, "__name__") == 0) { 1434 Py_INCREF(func->func_name); 1435 return func->func_name; 1436 } 1437 if (strcmp(sname, "__doc__") == 0) { 1438 Py_INCREF(func->func_doc); 1439 return func->func_doc; 1440 } 1441 } 1442 if (PyEval_GetRestricted()) { 1443 PyErr_SetString(PyExc_RuntimeError, 1444 "instance-method attributes not accessible in restricted mode"); 1445 return NULL; 1446 } 1447 return PyMember_Get((char *)im, instancemethod_memberlist, sname); 1448} 1449 1450static void 1451instancemethod_dealloc(im) 1452 register PyMethodObject *im; 1453{ 1454 Py_DECREF(im->im_func); 1455 Py_XDECREF(im->im_self); 1456 Py_DECREF(im->im_class); 1457 im->im_self = (PyObject *)free_list; 1458 free_list = im; 1459} 1460 1461static int 1462instancemethod_compare(a, b) 1463 PyMethodObject *a, *b; 1464{ 1465 if (a->im_self != b->im_self) 1466 return (a->im_self < b->im_self) ? -1 : 1; 1467 return PyObject_Compare(a->im_func, b->im_func); 1468} 1469 1470static PyObject * 1471instancemethod_repr(a) 1472 PyMethodObject *a; 1473{ 1474 char buf[240]; 1475 PyInstanceObject *self = (PyInstanceObject *)(a->im_self); 1476 PyFunctionObject *func = (PyFunctionObject *)(a->im_func); 1477 PyClassObject *class = (PyClassObject *)(a->im_class); 1478 PyObject *fclassname, *iclassname, *funcname; 1479 char *fcname, *icname, *fname; 1480 fclassname = class->cl_name; 1481 funcname = func->func_name; 1482 if (fclassname != NULL && PyString_Check(fclassname)) 1483 fcname = PyString_AsString(fclassname); 1484 else 1485 fcname = "?"; 1486 if (funcname != NULL && PyString_Check(funcname)) 1487 fname = PyString_AsString(funcname); 1488 else 1489 fname = "?"; 1490 if (self == NULL) 1491 sprintf(buf, "<unbound method %.100s.%.100s>", fcname, fname); 1492 else { 1493 iclassname = self->in_class->cl_name; 1494 if (iclassname != NULL && PyString_Check(iclassname)) 1495 icname = PyString_AsString(iclassname); 1496 else 1497 icname = "?"; 1498 sprintf(buf, "<method %.60s.%.60s of %.60s instance at %lx>", 1499 fcname, fname, icname, (long)self); 1500 } 1501 return PyString_FromString(buf); 1502} 1503 1504static long 1505instancemethod_hash(a) 1506 PyMethodObject *a; 1507{ 1508 long x, y; 1509 if (a->im_self == NULL) 1510 x = PyObject_Hash(Py_None); 1511 else 1512 x = PyObject_Hash(a->im_self); 1513 if (x == -1) 1514 return -1; 1515 y = PyObject_Hash(a->im_func); 1516 if (y == -1) 1517 return -1; 1518 return x ^ y; 1519} 1520 1521PyTypeObject PyMethod_Type = { 1522 PyObject_HEAD_INIT(&PyType_Type) 1523 0, 1524 "instance method", 1525 sizeof(PyMethodObject), 1526 0, 1527 (destructor)instancemethod_dealloc, /*tp_dealloc*/ 1528 0, /*tp_print*/ 1529 0, /*tp_getattr*/ 1530 0, /*tp_setattr*/ 1531 (cmpfunc)instancemethod_compare, /*tp_compare*/ 1532 (reprfunc)instancemethod_repr, /*tp_repr*/ 1533 0, /*tp_as_number*/ 1534 0, /*tp_as_sequence*/ 1535 0, /*tp_as_mapping*/ 1536 (hashfunc)instancemethod_hash, /*tp_hash*/ 1537 0, /*tp_call*/ 1538 0, /*tp_str*/ 1539 (getattrofunc)instancemethod_getattr, /*tp_getattro*/ 1540 0, /*tp_setattro*/ 1541}; 1542 1543/* Clear out the free list */ 1544 1545void 1546PyMethod_Fini() 1547{ 1548 while (free_list) { 1549 PyMethodObject *v = free_list; 1550 free_list = (PyMethodObject *)(v->im_self); 1551 PyMem_DEL(v); 1552 } 1553} 1554