blob: d0b29c7ee08e79833e8f3a35a4b988617d26bbb5 [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossumbab9d031992-04-05 14:26:55 +00002Copyright 1991, 1992 by Stichting Mathematisch Centrum, Amsterdam, The
Guido van Rossumf70e43a1991-02-19 12:39:46 +00003Netherlands.
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 Rossum85a5fbb1990-10-14 12:07:46 +0000102static object *
103meth_repr(m)
104 methodobject *m;
105{
106 char buf[200];
107 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000108 sprintf(buf, "<built-in function '%.80s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000109 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000110 sprintf(buf,
111 "<built-in method '%.80s' of some %.80s object>",
112 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000113 return newstringobject(buf);
114}
115
116typeobject Methodtype = {
117 OB_HEAD_INIT(&Typetype)
118 0,
Guido van Rossum7066dd71992-09-17 17:54:56 +0000119 "builtin_function_or_method",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000120 sizeof(methodobject),
121 0,
122 meth_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000123 0, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000124 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 Rossum3f5da241990-12-20 15:06:42 +0000132
Guido van Rossume9c430f1991-10-20 20:21:15 +0000133object *listmethods PROTO((struct methodlist *)); /* Forward */
134
135static object *
136listmethods(ml)
137 struct methodlist *ml;
138{
139 int i, n;
140 object *v;
141 for (n = 0; ml[n].ml_name != NULL; n++)
142 ;
143 v = newlistobject(n);
144 if (v != NULL) {
145 for (i = 0; i < n; i++)
146 setlistitem(v, i, newstringobject(ml[i].ml_name));
147 if (err_occurred()) {
148 DECREF(v);
149 v = NULL;
150 }
151 else {
152 sortlist(v);
153 }
154 }
155 return v;
156}
157
Guido van Rossum3f5da241990-12-20 15:06:42 +0000158/* Find a method in a module's method table.
159 Usually called from an object's getattr method. */
160
161object *
162findmethod(ml, op, name)
163 struct methodlist *ml;
164 object *op;
165 char *name;
166{
Guido van Rossume9c430f1991-10-20 20:21:15 +0000167 if (strcmp(name, "__methods__") == 0)
168 return listmethods(ml);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000169 for (; ml->ml_name != NULL; ml++) {
170 if (strcmp(name, ml->ml_name) == 0)
Guido van Rossumc0602291991-12-16 13:07:24 +0000171 return newmethodobject(ml->ml_name, ml->ml_meth,
172 op, ml->ml_varargs);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000173 }
Guido van Rossumbd3a2e61991-12-10 13:58:49 +0000174 err_setstr(AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000175 return NULL;
176}