blob: c8b5ee01dc1ecb743d7ee42b3a2f27921350d910 [file] [log] [blame]
Guido van Rossum85a5fbb1990-10-14 12:07:46 +00001/* Method object implementation */
2
3#include <stdio.h>
4
5#include "PROTO.h"
6#include "object.h"
7#include "node.h"
8#include "stringobject.h"
9#include "methodobject.h"
10#include "objimpl.h"
11#include "token.h"
Guido van Rossum2a9096b1990-10-21 22:15:08 +000012#include "errors.h"
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000013
14typedef struct {
15 OB_HEAD
16 char *m_name;
17 method m_meth;
18 object *m_self;
19} methodobject;
20
21object *
22newmethodobject(name, meth, self)
23 char *name; /* static string */
24 method meth;
25 object *self;
26{
27 methodobject *op = NEWOBJ(methodobject, &Methodtype);
28 if (op != NULL) {
29 op->m_name = name;
30 op->m_meth = meth;
31 if (self != NULL)
32 INCREF(self);
33 op->m_self = self;
34 }
35 return (object *)op;
36}
37
38method
39getmethod(op)
40 object *op;
41{
42 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000043 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000044 return NULL;
45 }
46 return ((methodobject *)op) -> m_meth;
47}
48
49object *
50getself(op)
51 object *op;
52{
53 if (!is_methodobject(op)) {
Guido van Rossum2a9096b1990-10-21 22:15:08 +000054 err_badcall();
Guido van Rossum85a5fbb1990-10-14 12:07:46 +000055 return NULL;
56 }
57 return ((methodobject *)op) -> m_self;
58}
59
60/* Methods (the standard built-in methods, that is) */
61
62static void
63meth_dealloc(m)
64 methodobject *m;
65{
66 if (m->m_self != NULL)
67 DECREF(m->m_self);
68 free((char *)m);
69}
70
71static void
72meth_print(m, fp, flags)
73 methodobject *m;
74 FILE *fp;
75 int flags;
76{
77 if (m->m_self == NULL)
78 fprintf(fp, "<%s method>", m->m_name);
79 else
80 fprintf(fp, "<%s method of %s object at %lx>",
81 m->m_name, m->m_self->ob_type->tp_name,
82 (long)m->m_self);
83}
84
85static object *
86meth_repr(m)
87 methodobject *m;
88{
89 char buf[200];
90 if (m->m_self == NULL)
91 sprintf(buf, "<%.80s method>", m->m_name);
92 else
93 sprintf(buf, "<%.80s method of %.80s object at %lx>",
94 m->m_name, m->m_self->ob_type->tp_name,
95 (long)m->m_self);
96 return newstringobject(buf);
97}
98
99typeobject Methodtype = {
100 OB_HEAD_INIT(&Typetype)
101 0,
102 "method",
103 sizeof(methodobject),
104 0,
105 meth_dealloc, /*tp_dealloc*/
106 meth_print, /*tp_print*/
107 0, /*tp_getattr*/
108 0, /*tp_setattr*/
109 0, /*tp_compare*/
110 meth_repr, /*tp_repr*/
111 0, /*tp_as_number*/
112 0, /*tp_as_sequence*/
113 0, /*tp_as_mapping*/
114};