blob: f6777ea164d97da3779c3edd18326edd0d4ed0d0 [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 Rossumeec181a1992-03-27 17:26:44 +0000102/* ARGSUSED */
Guido van Rossum90933611991-06-07 16:10:43 +0000103static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000104meth_print(m, fp, flags)
105 methodobject *m;
106 FILE *fp;
Guido van Rossumeec181a1992-03-27 17:26:44 +0000107 int flags; /* Not used but required by interface */
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108{
109 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 fprintf(fp, "<built-in function '%s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000111 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000112 fprintf(fp, "<built-in method '%s' of some %s object>",
113 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum90933611991-06-07 16:10:43 +0000114 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000115}
116
117static object *
118meth_repr(m)
119 methodobject *m;
120{
121 char buf[200];
122 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000123 sprintf(buf, "<built-in function '%.80s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000125 sprintf(buf,
126 "<built-in method '%.80s' of some %.80s object>",
127 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000128 return newstringobject(buf);
129}
130
131typeobject Methodtype = {
132 OB_HEAD_INIT(&Typetype)
133 0,
134 "method",
135 sizeof(methodobject),
136 0,
137 meth_dealloc, /*tp_dealloc*/
138 meth_print, /*tp_print*/
139 0, /*tp_getattr*/
140 0, /*tp_setattr*/
141 0, /*tp_compare*/
142 meth_repr, /*tp_repr*/
143 0, /*tp_as_number*/
144 0, /*tp_as_sequence*/
145 0, /*tp_as_mapping*/
146};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000147
Guido van Rossume9c430f1991-10-20 20:21:15 +0000148object *listmethods PROTO((struct methodlist *)); /* Forward */
149
150static object *
151listmethods(ml)
152 struct methodlist *ml;
153{
154 int i, n;
155 object *v;
156 for (n = 0; ml[n].ml_name != NULL; n++)
157 ;
158 v = newlistobject(n);
159 if (v != NULL) {
160 for (i = 0; i < n; i++)
161 setlistitem(v, i, newstringobject(ml[i].ml_name));
162 if (err_occurred()) {
163 DECREF(v);
164 v = NULL;
165 }
166 else {
167 sortlist(v);
168 }
169 }
170 return v;
171}
172
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173/* Find a method in a module's method table.
174 Usually called from an object's getattr method. */
175
176object *
177findmethod(ml, op, name)
178 struct methodlist *ml;
179 object *op;
180 char *name;
181{
Guido van Rossume9c430f1991-10-20 20:21:15 +0000182 if (strcmp(name, "__methods__") == 0)
183 return listmethods(ml);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000184 for (; ml->ml_name != NULL; ml++) {
185 if (strcmp(name, ml->ml_name) == 0)
Guido van Rossumc0602291991-12-16 13:07:24 +0000186 return newmethodobject(ml->ml_name, ml->ml_meth,
187 op, ml->ml_varargs);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000188 }
Guido van Rossumbd3a2e61991-12-10 13:58:49 +0000189 err_setstr(AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000190 return NULL;
191}