blob: 98b70a6d8d268f8e5d7797000bdfe272db604cda [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
2Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The
3Netherlands.
4
5 All Rights Reserved
6
7Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
9provided that the above copyright notice appear in all copies and that
10both that copyright notice and this permission notice appear in
11supporting documentation, and that the names of Stichting Mathematisch
12Centrum or CWI not be used in advertising or publicity pertaining to
13distribution of the software without specific, written prior permission.
14
15STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
16THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
17FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
18FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
19WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
20ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
21OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
22
23******************************************************************/
24
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000025/* Method object implementation */
26
Guido van Rossum3f5da241990-12-20 15:06:42 +000027#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000028
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000029#include "token.h"
30
31typedef struct {
32 OB_HEAD
Guido van Rossumc0602291991-12-16 13:07:24 +000033 char *m_name;
34 method m_meth;
35 object *m_self;
36 int m_varargs;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037} methodobject;
38
39object *
Guido van Rossumc0602291991-12-16 13:07:24 +000040newmethodobject(name, meth, self, varargs)
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000041 char *name; /* static string */
42 method meth;
43 object *self;
Guido van Rossumc0602291991-12-16 13:07:24 +000044 int varargs;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000045{
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 Rossumc0602291991-12-16 13:07:24 +000053 op->m_varargs = varargs;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000054 }
55 return (object *)op;
56}
57
58method
59getmethod(op)
60 object *op;
61{
62 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000063 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000064 return NULL;
65 }
66 return ((methodobject *)op) -> m_meth;
67}
68
69object *
70getself(op)
71 object *op;
72{
73 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000074 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075 return NULL;
76 }
77 return ((methodobject *)op) -> m_self;
78}
79
Guido van Rossumc0602291991-12-16 13:07:24 +000080int
81getvarargs(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 Rossum85a5fbb1990-10-14 12:07:46 +000091/* Methods (the standard built-in methods, that is) */
92
93static void
94meth_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 Rossum90933611991-06-07 16:10:43 +0000102static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000103meth_print(m, fp, flags)
104 methodobject *m;
105 FILE *fp;
106 int flags;
107{
108 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109 fprintf(fp, "<built-in function '%s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000110 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000111 fprintf(fp, "<built-in method '%s' of some %s object>",
112 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum90933611991-06-07 16:10:43 +0000113 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114}
115
116static object *
117meth_repr(m)
118 methodobject *m;
119{
120 char buf[200];
121 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000122 sprintf(buf, "<built-in function '%.80s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000123 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000124 sprintf(buf,
125 "<built-in method '%.80s' of some %.80s object>",
126 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000127 return newstringobject(buf);
128}
129
130typeobject 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 Rossum3f5da241990-12-20 15:06:42 +0000146
Guido van Rossume9c430f1991-10-20 20:21:15 +0000147object *listmethods PROTO((struct methodlist *)); /* Forward */
148
149static object *
150listmethods(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 Rossum3f5da241990-12-20 15:06:42 +0000172/* Find a method in a module's method table.
173 Usually called from an object's getattr method. */
174
175object *
176findmethod(ml, op, name)
177 struct methodlist *ml;
178 object *op;
179 char *name;
180{
Guido van Rossume9c430f1991-10-20 20:21:15 +0000181 if (strcmp(name, "__methods__") == 0)
182 return listmethods(ml);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000183 for (; ml->ml_name != NULL; ml++) {
184 if (strcmp(name, ml->ml_name) == 0)
Guido van Rossumc0602291991-12-16 13:07:24 +0000185 return newmethodobject(ml->ml_name, ml->ml_meth,
186 op, ml->ml_varargs);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000187 }
Guido van Rossumbd3a2e61991-12-10 13:58:49 +0000188 err_setstr(AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000189 return NULL;
190}