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 |
| 33 | char *m_name; |
| 34 | method m_meth; |
| 35 | object *m_self; |
| 36 | } methodobject; |
| 37 | |
| 38 | object * |
| 39 | newmethodobject(name, meth, self) |
| 40 | char *name; /* static string */ |
| 41 | method meth; |
| 42 | object *self; |
| 43 | { |
| 44 | methodobject *op = NEWOBJ(methodobject, &Methodtype); |
| 45 | if (op != NULL) { |
| 46 | op->m_name = name; |
| 47 | op->m_meth = meth; |
| 48 | if (self != NULL) |
| 49 | INCREF(self); |
| 50 | op->m_self = self; |
| 51 | } |
| 52 | return (object *)op; |
| 53 | } |
| 54 | |
| 55 | method |
| 56 | getmethod(op) |
| 57 | object *op; |
| 58 | { |
| 59 | if (!is_methodobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 60 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 61 | return NULL; |
| 62 | } |
| 63 | return ((methodobject *)op) -> m_meth; |
| 64 | } |
| 65 | |
| 66 | object * |
| 67 | getself(op) |
| 68 | object *op; |
| 69 | { |
| 70 | if (!is_methodobject(op)) { |
Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 71 | err_badcall(); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 72 | return NULL; |
| 73 | } |
| 74 | return ((methodobject *)op) -> m_self; |
| 75 | } |
| 76 | |
| 77 | /* Methods (the standard built-in methods, that is) */ |
| 78 | |
| 79 | static void |
| 80 | meth_dealloc(m) |
| 81 | methodobject *m; |
| 82 | { |
| 83 | if (m->m_self != NULL) |
| 84 | DECREF(m->m_self); |
| 85 | free((char *)m); |
| 86 | } |
| 87 | |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 88 | static int |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 89 | meth_print(m, fp, flags) |
| 90 | methodobject *m; |
| 91 | FILE *fp; |
| 92 | int flags; |
| 93 | { |
| 94 | if (m->m_self == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 95 | fprintf(fp, "<built-in function '%s'>", m->m_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 96 | else |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 97 | fprintf(fp, "<built-in method '%s' of some %s object>", |
| 98 | m->m_name, m->m_self->ob_type->tp_name); |
Guido van Rossum | 9093361 | 1991-06-07 16:10:43 +0000 | [diff] [blame] | 99 | return 0; |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | static object * |
| 103 | meth_repr(m) |
| 104 | methodobject *m; |
| 105 | { |
| 106 | char buf[200]; |
| 107 | if (m->m_self == NULL) |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 108 | sprintf(buf, "<built-in function '%.80s'>", m->m_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 109 | else |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 110 | sprintf(buf, |
| 111 | "<built-in method '%.80s' of some %.80s object>", |
| 112 | m->m_name, m->m_self->ob_type->tp_name); |
Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 113 | return newstringobject(buf); |
| 114 | } |
| 115 | |
| 116 | typeobject Methodtype = { |
| 117 | OB_HEAD_INIT(&Typetype) |
| 118 | 0, |
| 119 | "method", |
| 120 | sizeof(methodobject), |
| 121 | 0, |
| 122 | meth_dealloc, /*tp_dealloc*/ |
| 123 | meth_print, /*tp_print*/ |
| 124 | 0, /*tp_getattr*/ |
| 125 | 0, /*tp_setattr*/ |
| 126 | 0, /*tp_compare*/ |
| 127 | meth_repr, /*tp_repr*/ |
| 128 | 0, /*tp_as_number*/ |
| 129 | 0, /*tp_as_sequence*/ |
| 130 | 0, /*tp_as_mapping*/ |
| 131 | }; |
Guido van Rossum | 3f5da24 | 1990-12-20 15:06:42 +0000 | [diff] [blame] | 132 | |
| 133 | /* Find a method in a module's method table. |
| 134 | Usually called from an object's getattr method. */ |
| 135 | |
| 136 | object * |
| 137 | findmethod(ml, op, name) |
| 138 | struct methodlist *ml; |
| 139 | object *op; |
| 140 | char *name; |
| 141 | { |
| 142 | for (; ml->ml_name != NULL; ml++) { |
| 143 | if (strcmp(name, ml->ml_name) == 0) |
| 144 | return newmethodobject(ml->ml_name, ml->ml_meth, op); |
| 145 | } |
| 146 | err_setstr(NameError, name); |
| 147 | return NULL; |
| 148 | } |