blob: d7ba02e7a1d4818714850f47ab432529023090de [file] [log] [blame]
Guido van Rossumf70e43a1991-02-19 12:39:46 +00001/***********************************************************
Guido van Rossum9bfef441993-03-29 10:43:31 +00002Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
3Amsterdam, The Netherlands.
Guido van Rossumf70e43a1991-02-19 12:39:46 +00004
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,
Guido van Rossum9bfef441993-03-29 10:43:31 +0000111 "<built-in method '%.80s' of %.80s object at %lx>",
112 m->m_name, m->m_self->ob_type->tp_name,
113 (long)m->m_self);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000114 return newstringobject(buf);
115}
116
Guido van Rossum9bfef441993-03-29 10:43:31 +0000117static int
118meth_compare(a, b)
119 methodobject *a, *b;
120{
121 if (a->m_self != b->m_self)
122 return cmpobject(a->m_self, b->m_self);
123 if (a->m_meth == b->m_meth)
124 return 0;
125 if (strcmp(a->m_name, b->m_name) < 0)
126 return -1;
127 else
128 return 1;
129}
130
131static long
132meth_hash(a)
133 methodobject *a;
134{
135 long x, y;
136 if (a->m_self == NULL)
137 x = 0;
138 else {
139 x = hashobject(a->m_self);
140 if (x == -1)
141 return -1;
142 }
143 return x ^ (long) a->m_meth;
144}
145
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000146typeobject Methodtype = {
147 OB_HEAD_INIT(&Typetype)
148 0,
Guido van Rossum7066dd71992-09-17 17:54:56 +0000149 "builtin_function_or_method",
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000150 sizeof(methodobject),
151 0,
152 meth_dealloc, /*tp_dealloc*/
Guido van Rossum7066dd71992-09-17 17:54:56 +0000153 0, /*tp_print*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000154 0, /*tp_getattr*/
155 0, /*tp_setattr*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000156 meth_compare, /*tp_compare*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000157 meth_repr, /*tp_repr*/
158 0, /*tp_as_number*/
159 0, /*tp_as_sequence*/
160 0, /*tp_as_mapping*/
Guido van Rossum9bfef441993-03-29 10:43:31 +0000161 meth_hash, /*tp_hash*/
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000162};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000163
Guido van Rossume9c430f1991-10-20 20:21:15 +0000164object *listmethods PROTO((struct methodlist *)); /* Forward */
165
166static object *
167listmethods(ml)
168 struct methodlist *ml;
169{
170 int i, n;
171 object *v;
172 for (n = 0; ml[n].ml_name != NULL; n++)
173 ;
174 v = newlistobject(n);
175 if (v != NULL) {
176 for (i = 0; i < n; i++)
177 setlistitem(v, i, newstringobject(ml[i].ml_name));
178 if (err_occurred()) {
179 DECREF(v);
180 v = NULL;
181 }
182 else {
183 sortlist(v);
184 }
185 }
186 return v;
187}
188
Guido van Rossum3f5da241990-12-20 15:06:42 +0000189/* Find a method in a module's method table.
190 Usually called from an object's getattr method. */
191
192object *
193findmethod(ml, op, name)
194 struct methodlist *ml;
195 object *op;
196 char *name;
197{
Guido van Rossume9c430f1991-10-20 20:21:15 +0000198 if (strcmp(name, "__methods__") == 0)
199 return listmethods(ml);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000200 for (; ml->ml_name != NULL; ml++) {
201 if (strcmp(name, ml->ml_name) == 0)
Guido van Rossumc0602291991-12-16 13:07:24 +0000202 return newmethodobject(ml->ml_name, ml->ml_meth,
203 op, ml->ml_varargs);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000204 }
Guido van Rossumbd3a2e61991-12-10 13:58:49 +0000205 err_setstr(AttributeError, name);
Guido van Rossum3f5da241990-12-20 15:06:42 +0000206 return NULL;
207}