home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Source Code / C / Applications / Python 1.4 / Python 1.4 source / Objects / classobject.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-28  |  31.8 KB  |  1,454 lines  |  [TEXT/CWIE]

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