Mercurial > cpython-lru_cache-weakref
annotate Objects/methodobject.c @ 742:f710110cfd39 legacy-trunk
Lint
./
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Fri, 27 Mar 1992 17:26:44 +0000 |
| parents | 90f35fcf8ea0 |
| children | 0cd197947d81 |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
| 2 Copyright 1991 by Stichting Mathematisch Centrum, Amsterdam, The | |
| 3 Netherlands. | |
| 4 | |
| 5 All Rights Reserved | |
| 6 | |
| 7 Permission to use, copy, modify, and distribute this software and its | |
| 8 documentation for any purpose and without fee is hereby granted, | |
| 9 provided that the above copyright notice appear in all copies and that | |
| 10 both that copyright notice and this permission notice appear in | |
| 11 supporting documentation, and that the names of Stichting Mathematisch | |
| 12 Centrum or CWI not be used in advertising or publicity pertaining to | |
| 13 distribution of the software without specific, written prior permission. | |
| 14 | |
| 15 STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO | |
| 16 THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND | |
| 17 FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE | |
| 18 FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | |
| 19 WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | |
| 20 ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT | |
| 21 OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | |
| 22 | |
| 23 ******************************************************************/ | |
| 24 | |
| 6 | 25 /* Method object implementation */ |
| 26 | |
| 86 | 27 #include "allobjects.h" |
| 6 | 28 |
| 29 #include "token.h" | |
| 30 | |
| 31 typedef struct { | |
| 32 OB_HEAD | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
33 char *m_name; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
34 method m_meth; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
35 object *m_self; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
36 int m_varargs; |
| 6 | 37 } methodobject; |
| 38 | |
| 39 object * | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
40 newmethodobject(name, meth, self, varargs) |
| 6 | 41 char *name; /* static string */ |
| 42 method meth; | |
| 43 object *self; | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
44 int varargs; |
| 6 | 45 { |
| 46 methodobject *op = NEWOBJ(methodobject, &Methodtype); | |
| 47 if (op != NULL) { | |
| 48 op->m_name = name; | |
| 49 op->m_meth = meth; | |
| 50 if (self != NULL) | |
| 51 INCREF(self); | |
| 52 op->m_self = self; | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
53 op->m_varargs = varargs; |
| 6 | 54 } |
| 55 return (object *)op; | |
| 56 } | |
| 57 | |
| 58 method | |
| 59 getmethod(op) | |
| 60 object *op; | |
| 61 { | |
| 62 if (!is_methodobject(op)) { | |
| 27 | 63 err_badcall(); |
| 6 | 64 return NULL; |
| 65 } | |
| 66 return ((methodobject *)op) -> m_meth; | |
| 67 } | |
| 68 | |
| 69 object * | |
| 70 getself(op) | |
| 71 object *op; | |
| 72 { | |
| 73 if (!is_methodobject(op)) { | |
| 27 | 74 err_badcall(); |
| 6 | 75 return NULL; |
| 76 } | |
| 77 return ((methodobject *)op) -> m_self; | |
| 78 } | |
| 79 | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
80 int |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
81 getvarargs(op) |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
82 object *op; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
83 { |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
84 if (!is_methodobject(op)) { |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
85 err_badcall(); |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
86 return -1; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
87 } |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
88 return ((methodobject *)op) -> m_varargs; |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
89 } |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
90 |
| 6 | 91 /* Methods (the standard built-in methods, that is) */ |
| 92 | |
| 93 static void | |
| 94 meth_dealloc(m) | |
| 95 methodobject *m; | |
| 96 { | |
| 97 if (m->m_self != NULL) | |
| 98 DECREF(m->m_self); | |
| 99 free((char *)m); | |
| 100 } | |
| 101 | |
| 742 | 102 /* ARGSUSED */ |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
103 static int |
| 6 | 104 meth_print(m, fp, flags) |
| 105 methodobject *m; | |
| 106 FILE *fp; | |
| 742 | 107 int flags; /* Not used but required by interface */ |
| 6 | 108 { |
| 109 if (m->m_self == NULL) | |
| 86 | 110 fprintf(fp, "<built-in function '%s'>", m->m_name); |
| 6 | 111 else |
| 86 | 112 fprintf(fp, "<built-in method '%s' of some %s object>", |
| 113 m->m_name, m->m_self->ob_type->tp_name); | |
|
284
6ac402023cac
printobject now returns an error code
Guido van Rossum <guido@python.org>
parents:
145
diff
changeset
|
114 return 0; |
| 6 | 115 } |
| 116 | |
| 117 static object * | |
| 118 meth_repr(m) | |
| 119 methodobject *m; | |
| 120 { | |
| 121 char buf[200]; | |
| 122 if (m->m_self == NULL) | |
| 86 | 123 sprintf(buf, "<built-in function '%.80s'>", m->m_name); |
| 6 | 124 else |
| 86 | 125 sprintf(buf, |
| 126 "<built-in method '%.80s' of some %.80s object>", | |
| 127 m->m_name, m->m_self->ob_type->tp_name); | |
| 6 | 128 return newstringobject(buf); |
| 129 } | |
| 130 | |
| 131 typeobject Methodtype = { | |
| 132 OB_HEAD_INIT(&Typetype) | |
| 133 0, | |
| 134 "method", | |
| 135 sizeof(methodobject), | |
| 136 0, | |
| 137 meth_dealloc, /*tp_dealloc*/ | |
| 138 meth_print, /*tp_print*/ | |
| 139 0, /*tp_getattr*/ | |
| 140 0, /*tp_setattr*/ | |
| 141 0, /*tp_compare*/ | |
| 142 meth_repr, /*tp_repr*/ | |
| 143 0, /*tp_as_number*/ | |
| 144 0, /*tp_as_sequence*/ | |
| 145 0, /*tp_as_mapping*/ | |
| 146 }; | |
| 86 | 147 |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
148 object *listmethods PROTO((struct methodlist *)); /* Forward */ |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
149 |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
150 static object * |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
151 listmethods(ml) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
152 struct methodlist *ml; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
153 { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
154 int i, n; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
155 object *v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
156 for (n = 0; ml[n].ml_name != NULL; n++) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
157 ; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
158 v = newlistobject(n); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
159 if (v != NULL) { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
160 for (i = 0; i < n; i++) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
161 setlistitem(v, i, newstringobject(ml[i].ml_name)); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
162 if (err_occurred()) { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
163 DECREF(v); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
164 v = NULL; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
165 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
166 else { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
167 sortlist(v); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
168 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
169 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
170 return v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
171 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
172 |
| 86 | 173 /* Find a method in a module's method table. |
| 174 Usually called from an object's getattr method. */ | |
| 175 | |
| 176 object * | |
| 177 findmethod(ml, op, name) | |
| 178 struct methodlist *ml; | |
| 179 object *op; | |
| 180 char *name; | |
| 181 { | |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
182 if (strcmp(name, "__methods__") == 0) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
183 return listmethods(ml); |
| 86 | 184 for (; ml->ml_name != NULL; ml++) { |
| 185 if (strcmp(name, ml->ml_name) == 0) | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
186 return newmethodobject(ml->ml_name, ml->ml_meth, |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
187 op, ml->ml_varargs); |
| 86 | 188 } |
| 472 | 189 err_setstr(AttributeError, name); |
| 86 | 190 return NULL; |
| 191 } |
