blob: 5cc4b88e8fd7d97fa50c5b5cfc96d8785d25791e [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Method object implementation */
2
Guido van Rossum3f5da241990-12-20 15:06:42 +00003#include "allobjects.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00004
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00005#include "token.h"
6
7typedef struct {
8 OB_HEAD
9 char *m_name;
10 method m_meth;
11 object *m_self;
12} methodobject;
13
14object *
15newmethodobject(name, meth, self)
16 char *name; /* static string */
17 method meth;
18 object *self;
19{
20 methodobject *op = NEWOBJ(methodobject, &Methodtype);
21 if (op != NULL) {
22 op->m_name = name;
23 op->m_meth = meth;
24 if (self != NULL)
25 INCREF(self);
26 op->m_self = self;
27 }
28 return (object *)op;
29}
30
31method
32getmethod(op)
33 object *op;
34{
35 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000036 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000037 return NULL;
38 }
39 return ((methodobject *)op) -> m_meth;
40}
41
42object *
43getself(op)
44 object *op;
45{
46 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000047 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000048 return NULL;
49 }
50 return ((methodobject *)op) -> m_self;
51}
52
53/* Methods (the standard built-in methods, that is) */
54
55static void
56meth_dealloc(m)
57 methodobject *m;
58{
59 if (m->m_self != NULL)
60 DECREF(m->m_self);
61 free((char *)m);
62}
63
64static void
65meth_print(m, fp, flags)
66 methodobject *m;
67 FILE *fp;
68 int flags;
69{
70 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000071 fprintf(fp, "<built-in function '%s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000072 else
Guido van Rossum3f5da241990-12-20 15:06:42 +000073 fprintf(fp, "<built-in method '%s' of some %s object>",
74 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000075}
76
77static object *
78meth_repr(m)
79 methodobject *m;
80{
81 char buf[200];
82 if (m->m_self == NULL)
Guido van Rossum3f5da241990-12-20 15:06:42 +000083 sprintf(buf, "<built-in function '%.80s'>", m->m_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000084 else
Guido van Rossum3f5da241990-12-20 15:06:42 +000085 sprintf(buf,
86 "<built-in method '%.80s' of some %.80s object>",
87 m->m_name, m->m_self->ob_type->tp_name);
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000088 return newstringobject(buf);
89}
90
91typeobject Methodtype = {
92 OB_HEAD_INIT(&Typetype)
93 0,
94 "method",
95 sizeof(methodobject),
96 0,
97 meth_dealloc, /*tp_dealloc*/
98 meth_print, /*tp_print*/
99 0, /*tp_getattr*/
100 0, /*tp_setattr*/
101 0, /*tp_compare*/
102 meth_repr, /*tp_repr*/
103 0, /*tp_as_number*/
104 0, /*tp_as_sequence*/
105 0, /*tp_as_mapping*/
106};
Guido van Rossum3f5da241990-12-20 15:06:42 +0000107
108/* Find a method in a module's method table.
109 Usually called from an object's getattr method. */
110
111object *
112findmethod(ml, op, name)
113 struct methodlist *ml;
114 object *op;
115 char *name;
116{
117 for (; ml->ml_name != NULL; ml++) {
118 if (strcmp(name, ml->ml_name) == 0)
119 return newmethodobject(ml->ml_name, ml->ml_meth, op);
120 }
121 err_setstr(NameError, name);
122 return NULL;
123}