blob: 62649f191c6ddad07433c952aafe582216a4e424 [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
33 char *m_name;
34 method m_meth;
35 object *m_self;
36} methodobject;
37
38object *
39newmethodobject(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
55method
56getmethod(op)
57 object *op;
58{
59 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000060 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000061 return NULL;
62 }
63 return ((methodobject *)op) -> m_meth;
64}
65
66object *
67getself(op)
68 object *op;
69{
70 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000071 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 return NULL;
73 }
74 return ((methodobject *)op) -> m_self;
75}
76
77/* Methods (the standard built-in methods, that is) */
78
79static void
80meth_dealloc(m)
81 methodobject *m;
82{
83 if (m->m_self != NULL)
84 DECREF(m->m_self);
85 free((char *)m);
86}
87
88static void
89meth_print(m, fp, flags)
90 methodobject *m;
91 FILE *fp;
92 int flags;
93{
94 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000095 fprintf(fp, "<built-in function '%s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000096 else
Guido van Rossum3f5da241990-12-20 15:06:42 +000097 fprintf(fp, "<built-in method '%s' of some %s object>",
98 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000099}
100
101static object *
102meth_repr(m)
103 methodobject *m;
104{
105 char buf[200];
106 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107 sprintf(buf, "<built-in function '%.80s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000108 else
Guido van Rossum3f5da241990-12-20 15:06:42 +0000109 sprintf(buf,
110 "<built-in method '%.80s' of some %.80s object>",
111 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000112 return newstringobject(buf);
113}
114
115typeobject Methodtype = {
116 OB_HEAD_INIT(&Typetype)
117 0,
118 "method",
119 sizeof(methodobject),
120 0,
121 meth_dealloc, /*tp_dealloc*/
122 meth_print, /*tp_print*/
123 0, /*tp_getattr*/
124 0, /*tp_setattr*/
125 0, /*tp_compare*/
126 meth_repr, /*tp_repr*/
127 0, /*tp_as_number*/
128 0, /*tp_as_sequence*/
129 0, /*tp_as_mapping*/
130};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000131
132/* Find a method in a module's method table.
133 Usually called from an object's getattr method. */
134
135object *
136findmethod(ml, op, name)
137 struct methodlist *ml;
138 object *op;
139 char *name;
140{
141 for (; ml->ml_name != NULL; ml++) {
142 if (strcmp(name, ml->ml_name) == 0)
143 return newmethodobject(ml->ml_name, ml->ml_meth, op);
144 }
145 err_setstr(NameError, name);
146 return NULL;
147}