Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 1 | /*********************************************************** |
Guido van Rossum | 6610ad9 | 1995-01-04 19:07:38 +0000 | [diff] [blame] | 2 | Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
| 3 | The Netherlands. |
Guido van Rossum | f70e43a | 1991-02-19 12:39:46 +0000 | [diff] [blame] | 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 | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 33 | struct methodlist *m_ml; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 34 | object *m_self; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 35 | } methodobject; |
| 36 | |
| 37 | object * |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 38 | newmethodobject(ml, self) |
| 39 | struct methodlist *ml; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 40 | object *self; |
| 41 | { |
| 42 | methodobject *op = NEWOBJ(methodobject, &Methodtype); |
| 43 | if (op != NULL) { |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 44 | op->m_ml = ml; |
| 45 | XINCREF(self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 46 | op->m_self = self; |
| 47 | } |
| 48 | return (object *)op; |
| 49 | } |
| 50 | |
| 51 | method |
| 52 | getmethod(op) |
| 53 | object *op; |
| 54 | { |
| 55 | if (!is_methodobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 56 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 57 | return NULL; |
| 58 | } |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 59 | return ((methodobject *)op) -> m_ml -> ml_meth; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | object * |
| 63 | getself(op) |
| 64 | object *op; |
| 65 | { |
| 66 | if (!is_methodobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 67 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 68 | return NULL; |
| 69 | } |
| 70 | return ((methodobject *)op) -> m_self; |
| 71 | } |
| 72 | |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 73 | int |
| 74 | getvarargs(op) |
| 75 | object *op; |
| 76 | { |
| 77 | if (!is_methodobject(op)) { |
| 78 | err_badcall(); |
| 79 | return -1; |
| 80 | } |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 81 | return ((methodobject *)op) -> m_ml -> ml_flags & METH_VARARGS; |
Guido van Rossum | c060229 | 1991-12-16 13:07:24 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 84 | /* Methods (the standard built-in methods, that is) */ |
| 85 | |
| 86 | static void |
| 87 | meth_dealloc(m) |
| 88 | methodobject *m; |
| 89 | { |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 90 | XDECREF(m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 91 | free((char *)m); |
| 92 | } |
| 93 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 94 | static object * |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 95 | meth_getattr(m, name) |
| 96 | methodobject *m; |
| 97 | char *name; |
| 98 | { |
| 99 | if (strcmp(name, "__name__") == 0) { |
| 100 | return newstringobject(m->m_ml->ml_name); |
| 101 | } |
| 102 | if (strcmp(name, "__doc__") == 0) { |
| 103 | char *doc = m->m_ml->ml_doc; |
| 104 | if (doc != NULL) |
| 105 | return newstringobject(doc); |
| 106 | INCREF(None); |
| 107 | return None; |
| 108 | } |
| 109 | if (strcmp(name, "__self__") == 0) { |
Guido van Rossum | 10393b1 | 1995-01-10 10:39:49 +0000 | [diff] [blame] | 110 | object *self; |
| 111 | if (getrestricted()) { |
| 112 | err_setstr(RuntimeError, |
| 113 | "method.__self__ not accessible in restricted mode"); |
| 114 | return NULL; |
| 115 | } |
| 116 | self = m->m_self; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 117 | if (self == NULL) |
| 118 | self = None; |
| 119 | INCREF(self); |
| 120 | return self; |
| 121 | } |
| 122 | if (strcmp(name, "__members__") == 0) { |
| 123 | return mkvalue("[sss]", "__doc__", "__name__", "__self__"); |
| 124 | } |
| 125 | err_setstr(AttributeError, name); |
| 126 | return NULL; |
| 127 | } |
| 128 | |
| 129 | static object * |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 130 | meth_repr(m) |
| 131 | methodobject *m; |
| 132 | { |
| 133 | char buf[200]; |
| 134 | if (m->m_self == NULL) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 135 | sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 136 | else |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 137 | sprintf(buf, |
Guido van Rossum | b3f7258 | 1993-05-21 19:56:10 +0000 | [diff] [blame] | 138 | "<built-in method %.80s of %.80s object at %lx>", |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 139 | m->m_ml->ml_name, m->m_self->ob_type->tp_name, |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 140 | (long)m->m_self); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 141 | return newstringobject(buf); |
| 142 | } |
| 143 | |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 144 | static int |
| 145 | meth_compare(a, b) |
| 146 | methodobject *a, *b; |
| 147 | { |
| 148 | if (a->m_self != b->m_self) |
| 149 | return cmpobject(a->m_self, b->m_self); |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 150 | if (a->m_ml->ml_meth == b->m_ml->ml_meth) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 151 | return 0; |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 152 | if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0) |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 153 | return -1; |
| 154 | else |
| 155 | return 1; |
| 156 | } |
| 157 | |
| 158 | static long |
| 159 | meth_hash(a) |
| 160 | methodobject *a; |
| 161 | { |
Guido van Rossum | d7047b3 | 1995-01-02 19:07:15 +0000 | [diff] [blame] | 162 | long x; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 163 | if (a->m_self == NULL) |
| 164 | x = 0; |
| 165 | else { |
| 166 | x = hashobject(a->m_self); |
| 167 | if (x == -1) |
| 168 | return -1; |
| 169 | } |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 170 | return x ^ (long) a->m_ml->ml_meth; |
Guido van Rossum | 9bfef44 | 1993-03-29 10:43:31 +0000 | [diff] [blame] | 171 | } |
| 172 | |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 173 | typeobject Methodtype = { |
| 174 | OB_HEAD_INIT(&Typetype) |
| 175 | 0, |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 176 | "builtin_function_or_method", |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 177 | sizeof(methodobject), |
| 178 | 0, |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 179 | (destructor)meth_dealloc, /*tp_dealloc*/ |
Guido van Rossum | 7066dd7 | 1992-09-17 17:54:56 +0000 | [diff] [blame] | 180 | 0, /*tp_print*/ |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 181 | (getattrfunc)meth_getattr, /*tp_getattr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 182 | 0, /*tp_setattr*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 183 | (cmpfunc)meth_compare, /*tp_compare*/ |
| 184 | (reprfunc)meth_repr, /*tp_repr*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 185 | 0, /*tp_as_number*/ |
| 186 | 0, /*tp_as_sequence*/ |
| 187 | 0, /*tp_as_mapping*/ |
Guido van Rossum | 1d5735e | 1994-08-30 08:27:36 +0000 | [diff] [blame] | 188 | (hashfunc)meth_hash, /*tp_hash*/ |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 189 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 190 | |
Guido van Rossum | b73cc04 | 1993-11-01 16:28:59 +0000 | [diff] [blame] | 191 | static object *listmethods PROTO((struct methodlist *)); /* Forward */ |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 192 | |
| 193 | static object * |
| 194 | listmethods(ml) |
| 195 | struct methodlist *ml; |
| 196 | { |
| 197 | int i, n; |
| 198 | object *v; |
| 199 | for (n = 0; ml[n].ml_name != NULL; n++) |
| 200 | ; |
| 201 | v = newlistobject(n); |
| 202 | if (v != NULL) { |
| 203 | for (i = 0; i < n; i++) |
| 204 | setlistitem(v, i, newstringobject(ml[i].ml_name)); |
| 205 | if (err_occurred()) { |
| 206 | DECREF(v); |
| 207 | v = NULL; |
| 208 | } |
| 209 | else { |
| 210 | sortlist(v); |
| 211 | } |
| 212 | } |
| 213 | return v; |
| 214 | } |
| 215 | |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 216 | /* Find a method in a module's method table. |
| 217 | Usually called from an object's getattr method. */ |
| 218 | |
| 219 | object * |
| 220 | findmethod(ml, op, name) |
| 221 | struct methodlist *ml; |
| 222 | object *op; |
| 223 | char *name; |
| 224 | { |
Guido van Rossum | e9c430f | 1991-10-20 20:21:15 +0000 | [diff] [blame] | 225 | if (strcmp(name, "__methods__") == 0) |
| 226 | return listmethods(ml); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 227 | for (; ml->ml_name != NULL; ml++) { |
| 228 | if (strcmp(name, ml->ml_name) == 0) |
Guido van Rossum | cab650d | 1995-01-07 12:34:58 +0000 | [diff] [blame] | 229 | return newmethodobject(ml, op); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 230 | } |
Guido van Rossum | bd3a2e6 | 1991-12-10 13:58:49 +0000 | [diff] [blame] | 231 | err_setstr(AttributeError, name); |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 232 | return NULL; |
| 233 | } |