Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
| 2 | Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The |
| 3 | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 25 | /* Method object implementation */ |
| 26 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 27 | #include "allobjects.h" |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 28 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 29 | #include "token.h" |
| 30 | |
| 31 | typedef struct { |
| 32 | OB_HEAD |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 33 | char *m_name; |
| 34 | method m_meth; |
| 35 | object *m_self; |
| 36 | int m_varargs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 37 | } methodobject; |
| 38 | |
| 39 | object * |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 40 | newmethodobject(name, meth, self, varargs) |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 41 | char *name; /* static string */ |
| 42 | method meth; |
| 43 | object *self; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 44 | int varargs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 53 | op->m_varargs = varargs; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 54 | } |
| 55 | return (object *)op; |
| 56 | } |
| 57 | |
| 58 | method |
| 59 | getmethod(op) |
| 60 | object *op; |
| 61 | { |
| 62 | if (!is_methodobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 63 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 74 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 75 | return NULL; |
| 76 | } |
| 77 | return ((methodobject *)op) -> m_self; |
| 78 | } |
| 79 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 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 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 102 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 103 | meth_print(m, fp, flags) |
| 104 | methodobject *m; |
| 105 | FILE *fp; |
| 106 | int flags; |
| 107 | { |
| 108 | if (m->m_self == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 109 | fprintf(fp, "<built-in function '%s'>", m->m_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 110 | else |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 111 | fprintf(fp, "<built-in method '%s' of some %s object>", |
| 112 | m->m_name, m->m_self->ob_type->tp_name); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 113 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | static object * |
| 117 | meth_repr(m) |
| 118 | methodobject *m; |
| 119 | { |
| 120 | char buf[200]; |
| 121 | if (m->m_self == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 122 | sprintf(buf, "<built-in function '%.80s'>", m->m_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 123 | else |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 124 | sprintf(buf, |
| 125 | "<built-in method '%.80s' of some %.80s object>", |
| 126 | m->m_name, m->m_self->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 127 | return newstringobject(buf); |
| 128 | } |
| 129 | |
| 130 | typeobject Methodtype = { |
| 131 | OB_HEAD_INIT(&Typetype) |
| 132 | 0, |
| 133 | "method", |
| 134 | sizeof(methodobject), |
| 135 | 0, |
| 136 | meth_dealloc, /*tp_dealloc*/ |
| 137 | meth_print, /*tp_print*/ |
| 138 | 0, /*tp_getattr*/ |
| 139 | 0, /*tp_setattr*/ |
| 140 | 0, /*tp_compare*/ |
| 141 | meth_repr, /*tp_repr*/ |
| 142 | 0, /*tp_as_number*/ |
| 143 | 0, /*tp_as_sequence*/ |
| 144 | 0, /*tp_as_mapping*/ |
| 145 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 146 | |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 147 | object *listmethods PROTO((struct methodlist *)); /* Forward */ |
| 148 | |
| 149 | static object * |
| 150 | listmethods(ml) |
| 151 | struct methodlist *ml; |
| 152 | { |
| 153 | int i, n; |
| 154 | object *v; |
| 155 | for (n = 0; ml[n].ml_name != NULL; n++) |
| 156 | ; |
| 157 | v = newlistobject(n); |
| 158 | if (v != NULL) { |
| 159 | for (i = 0; i < n; i++) |
| 160 | setlistitem(v, i, newstringobject(ml[i].ml_name)); |
| 161 | if (err_occurred()) { |
| 162 | DECREF(v); |
| 163 | v = NULL; |
| 164 | } |
| 165 | else { |
| 166 | sortlist(v); |
| 167 | } |
| 168 | } |
| 169 | return v; |
| 170 | } |
| 171 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 172 | /* Find a method in a module's method table. |
| 173 | Usually called from an object's getattr method. */ |
| 174 | |
| 175 | object * |
| 176 | findmethod(ml, op, name) |
| 177 | struct methodlist *ml; |
| 178 | object *op; |
| 179 | char *name; |
| 180 | { |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 181 | if (strcmp(name, "__methods__") == 0) |
| 182 | return listmethods(ml); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 183 | for (; ml->ml_name != NULL; ml++) { |
| 184 | if (strcmp(name, ml->ml_name) == 0) |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 185 | return newmethodobject(ml->ml_name, ml->ml_meth, |
| 186 | op, ml->ml_varargs); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 187 | } |
Guido van Rossum | bd3a2e6 | 1991-12-10 13:58:49 +0000 | [diff] [blame] | 188 | err_setstr(AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 189 | return NULL; |
| 190 | } |