blob: 46a2f86beed62764a1bc8958fb836e7d6efcbd8a [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
Guido van Rossum90933611991-06-07 16:10:43 +000088static int
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000089meth_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 Rossum90933611991-06-07 16:10:43 +000099 return 0;
Guido van Rossum85a5fbb1990-10-14 12:07:46 +0000100}
101
102static 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,
119 "method",
120 sizeof(methodobject),
121 0,
122 meth_dealloc, /*tp_dealloc*/
123 meth_print, /*tp_print*/
124 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
133/* Find a method in a module's method table.
134 Usually called from an object's getattr method. */
135
136object *
137findmethod(ml, op, name)
138 struct methodlist *ml;
139 object *op;
140 char *name;
141{
142 for (; ml->ml_name != NULL; ml++) {
143 if (strcmp(name, ml->ml_name) == 0)
144 return newmethodobject(ml->ml_name, ml->ml_meth, op);
145 }
146 err_setstr(NameError, name);
147 return NULL;
148}