home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Objects / methodobject.c < prev    next >
C/C++ Source or Header  |  1994-01-11  |  4KB  |  208 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993, 1994 by Stichting Mathematisch Centrum,
  3. Amsterdam, 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 not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Method object implementation */
  26.  
  27. #include "allobjects.h"
  28.  
  29. #include "token.h"
  30.  
  31. typedef struct {
  32.     OB_HEAD
  33.     char    *m_name;
  34.     method    m_meth;
  35.     object    *m_self;
  36.     int    m_varargs;
  37. } methodobject;
  38.  
  39. object *
  40. newmethodobject(name, meth, self, varargs)
  41.     char *name; /* static string */
  42.     method meth;
  43.     object *self;
  44.     int varargs;
  45. {
  46.     methodobject *op = NEWOBJ(methodobject, &Methodtype);
  47.     if (op != NULL) {
  48.         op->m_name = name;
  49.         op->m_meth = meth;
  50.         if (self != NULL)
  51.             INCREF(self);
  52.         op->m_self = self;
  53.         op->m_varargs = varargs;
  54.     }
  55.     return (object *)op;
  56. }
  57.  
  58. method
  59. getmethod(op)
  60.     object *op;
  61. {
  62.     if (!is_methodobject(op)) {
  63.         err_badcall();
  64.         return NULL;
  65.     }
  66.     return ((methodobject *)op) -> m_meth;
  67. }
  68.  
  69. object *
  70. getself(op)
  71.     object *op;
  72. {
  73.     if (!is_methodobject(op)) {
  74.         err_badcall();
  75.         return NULL;
  76.     }
  77.     return ((methodobject *)op) -> m_self;
  78. }
  79.  
  80. int
  81. getvarargs(op)
  82.     object *op;
  83. {
  84.     if (!is_methodobject(op)) {
  85.         err_badcall();
  86.         return -1;
  87.     }
  88.     return ((methodobject *)op) -> m_varargs;
  89. }
  90.  
  91. /* Methods (the standard built-in methods, that is) */
  92.  
  93. static void
  94. meth_dealloc(m)
  95.     methodobject *m;
  96. {
  97.     if (m->m_self != NULL)
  98.         DECREF(m->m_self);
  99.     free((char *)m);
  100. }
  101.  
  102. static object *
  103. meth_repr(m)
  104.     methodobject *m;
  105. {
  106.     char buf[200];
  107.     if (m->m_self == NULL)
  108.         sprintf(buf, "<built-in function %.80s>", m->m_name);
  109.     else
  110.         sprintf(buf,
  111.             "<built-in method %.80s of %.80s object at %lx>",
  112.             m->m_name, m->m_self->ob_type->tp_name,
  113.             (long)m->m_self);
  114.     return newstringobject(buf);
  115. }
  116.  
  117. static int
  118. meth_compare(a, b)
  119.     methodobject *a, *b;
  120. {
  121.     if (a->m_self != b->m_self)
  122.         return cmpobject(a->m_self, b->m_self);
  123.     if (a->m_meth == b->m_meth)
  124.         return 0;
  125.     if (strcmp(a->m_name, b->m_name) < 0)
  126.         return -1;
  127.     else
  128.         return 1;
  129. }
  130.  
  131. static long
  132. meth_hash(a)
  133.     methodobject *a;
  134. {
  135.     long x, y;
  136.     if (a->m_self == NULL)
  137.         x = 0;
  138.     else {
  139.         x = hashobject(a->m_self);
  140.         if (x == -1)
  141.             return -1;
  142.     }
  143.     return x ^ (long) a->m_meth;
  144. }
  145.  
  146. typeobject Methodtype = {
  147.     OB_HEAD_INIT(&Typetype)
  148.     0,
  149.     "builtin_function_or_method",
  150.     sizeof(methodobject),
  151.     0,
  152.     (destructor)meth_dealloc, /*tp_dealloc*/
  153.     0,        /*tp_print*/
  154.     0,        /*tp_getattr*/
  155.     0,        /*tp_setattr*/
  156.     (cmpfunc)meth_compare, /*tp_compare*/
  157.     (reprfunc)meth_repr, /*tp_repr*/
  158.     0,        /*tp_as_number*/
  159.     0,        /*tp_as_sequence*/
  160.     0,        /*tp_as_mapping*/
  161.     (hashfunc)meth_hash, /*tp_hash*/
  162. };
  163.  
  164. static object *listmethods PROTO((struct methodlist *)); /* Forward */
  165.  
  166. static object *
  167. listmethods(ml)
  168.     struct methodlist *ml;
  169. {
  170.     int i, n;
  171.     object *v;
  172.     for (n = 0; ml[n].ml_name != NULL; n++)
  173.         ;
  174.     v = newlistobject(n);
  175.     if (v != NULL) {
  176.         for (i = 0; i < n; i++)
  177.             setlistitem(v, i, newstringobject(ml[i].ml_name));
  178.         if (err_occurred()) {
  179.             DECREF(v);
  180.             v = NULL;
  181.         }
  182.         else {
  183.             sortlist(v);
  184.         }
  185.     }
  186.     return v;
  187. }
  188.  
  189. /* Find a method in a module's method table.
  190.    Usually called from an object's getattr method. */
  191.  
  192. object *
  193. findmethod(ml, op, name)
  194.     struct methodlist *ml;
  195.     object *op;
  196.     char *name;
  197. {
  198.     if (strcmp(name, "__methods__") == 0)
  199.         return listmethods(ml);
  200.     for (; ml->ml_name != NULL; ml++) {
  201.         if (strcmp(name, ml->ml_name) == 0)
  202.             return newmethodobject(ml->ml_name, ml->ml_meth,
  203.                     op, ml->ml_varargs);
  204.     }
  205.     err_setstr(AttributeError, name);
  206.     return NULL;
  207. }
  208.