| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 1 | /* 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 Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 12 | #include "errors.h" |
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 13 | |
| 14 | typedef struct { |
| 15 | OB_HEAD |
| 16 | char *m_name; |
| 17 | method m_meth; |
| 18 | object *m_self; |
| 19 | } methodobject; |
| 20 | |
| 21 | object * |
| 22 | newmethodobject(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 | |
| 38 | method |
| 39 | getmethod(op) |
| 40 | object *op; |
| 41 | { |
| 42 | if (!is_methodobject(op)) { |
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 43 | err_badcall(); |
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 44 | return NULL; |
| 45 | } |
| 46 | return ((methodobject *)op) -> m_meth; |
| 47 | } |
| 48 | |
| 49 | object * |
| 50 | getself(op) |
| 51 | object *op; |
| 52 | { |
| 53 | if (!is_methodobject(op)) { |
| Guido van Rossum | 2a9096b | 1990-10-21 22:15:08 +0000 | [diff] [blame] | 54 | err_badcall(); |
| Guido van Rossum | 85a5fbb | 1990-10-14 12:07:46 +0000 | [diff] [blame] | 55 | return NULL; |
| 56 | } |
| 57 | return ((methodobject *)op) -> m_self; |
| 58 | } |
| 59 | |
| 60 | /* Methods (the standard built-in methods, that is) */ |
| 61 | |
| 62 | static void |
| 63 | meth_dealloc(m) |
| 64 | methodobject *m; |
| 65 | { |
| 66 | if (m->m_self != NULL) |
| 67 | DECREF(m->m_self); |
| 68 | free((char *)m); |
| 69 | } |
| 70 | |
| 71 | static void |
| 72 | meth_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 | |
| 85 | static object * |
| 86 | meth_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 | |
| 99 | typeobject 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 | }; |