Mercurial > cpython-lru_cache-weakref
annotate Objects/methodobject.c @ 1590:ac2e5cc29949 legacy-trunk
Added tests for missing host to open_http and open_gopher
| author | Guido van Rossum <guido@python.org> |
|---|---|
| date | Mon, 18 Apr 1994 09:39:56 +0000 |
| parents | 7c3245c18677 |
| children | b724599401ff |
| rev | line source |
|---|---|
| 145 | 1 /*********************************************************** |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
2 Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
3 Amsterdam, The Netherlands. |
| 145 | 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 | |
| 102 static object * | |
| 103 meth_repr(m) | |
| 104 methodobject *m; | |
| 105 { | |
| 106 char buf[200]; | |
| 107 if (m->m_self == NULL) | |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
108 sprintf(buf, "<built-in function %.80s>", m->m_name); |
| 6 | 109 else |
| 86 | 110 sprintf(buf, |
|
1290
7971fecbf28e
* Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents:
1257
diff
changeset
|
111 "<built-in method %.80s of %.80s object at %lx>", |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
112 m->m_name, m->m_self->ob_type->tp_name, |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
113 (long)m->m_self); |
| 6 | 114 return newstringobject(buf); |
| 115 } | |
| 116 | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
117 static int |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
118 meth_compare(a, b) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
119 methodobject *a, *b; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
120 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
121 if (a->m_self != b->m_self) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
122 return cmpobject(a->m_self, b->m_self); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
123 if (a->m_meth == b->m_meth) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
124 return 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
125 if (strcmp(a->m_name, b->m_name) < 0) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
126 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
127 else |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
128 return 1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
129 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
130 |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
131 static long |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
132 meth_hash(a) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
133 methodobject *a; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
134 { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
135 long x, y; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
136 if (a->m_self == NULL) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
137 x = 0; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
138 else { |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
139 x = hashobject(a->m_self); |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
140 if (x == -1) |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
141 return -1; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
142 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
143 return x ^ (long) a->m_meth; |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
144 } |
|
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
145 |
| 6 | 146 typeobject Methodtype = { |
| 147 OB_HEAD_INIT(&Typetype) | |
| 148 0, | |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
149 "builtin_function_or_method", |
| 6 | 150 sizeof(methodobject), |
| 151 0, | |
| 152 meth_dealloc, /*tp_dealloc*/ | |
|
1051
f54e64c8b9b7
* Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents:
795
diff
changeset
|
153 0, /*tp_print*/ |
| 6 | 154 0, /*tp_getattr*/ |
| 155 0, /*tp_setattr*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
156 meth_compare, /*tp_compare*/ |
| 6 | 157 meth_repr, /*tp_repr*/ |
| 158 0, /*tp_as_number*/ | |
| 159 0, /*tp_as_sequence*/ | |
| 160 0, /*tp_as_mapping*/ | |
|
1257
1cabc2b6c970
* Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents:
1051
diff
changeset
|
161 meth_hash, /*tp_hash*/ |
| 6 | 162 }; |
| 86 | 163 |
|
1391
7c3245c18677
* ceval.c, longobject.c, methodobject.c, listnode.c, arraymodule.c,
Guido van Rossum <guido@python.org>
parents:
1290
diff
changeset
|
164 static object *listmethods PROTO((struct methodlist *)); /* Forward */ |
|
395
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 static object * |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
167 listmethods(ml) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
168 struct methodlist *ml; |
|
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 int i, n; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
171 object *v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
172 for (n = 0; ml[n].ml_name != NULL; n++) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
173 ; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
174 v = newlistobject(n); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
175 if (v != NULL) { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
176 for (i = 0; i < n; i++) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
177 setlistitem(v, i, newstringobject(ml[i].ml_name)); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
178 if (err_occurred()) { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
179 DECREF(v); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
180 v = NULL; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
181 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
182 else { |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
183 sortlist(v); |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
184 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
185 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
186 return v; |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
187 } |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
188 |
| 86 | 189 /* Find a method in a module's method table. |
| 190 Usually called from an object's getattr method. */ | |
| 191 | |
| 192 object * | |
| 193 findmethod(ml, op, name) | |
| 194 struct methodlist *ml; | |
| 195 object *op; | |
| 196 char *name; | |
| 197 { | |
|
395
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
198 if (strcmp(name, "__methods__") == 0) |
|
659b8dbf695c
Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents:
284
diff
changeset
|
199 return listmethods(ml); |
| 86 | 200 for (; ml->ml_name != NULL; ml++) { |
| 201 if (strcmp(name, ml->ml_name) == 0) | |
|
488
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
202 return newmethodobject(ml->ml_name, ml->ml_meth, |
|
90f35fcf8ea0
Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents:
472
diff
changeset
|
203 op, ml->ml_varargs); |
| 86 | 204 } |
| 472 | 205 err_setstr(AttributeError, name); |
| 86 | 206 return NULL; |
| 207 } |
