annotate Objects/methodobject.c @ 4566:6403c94de50d legacy-trunk

Added safeguard against failure in __del__.
author Guido van Rossum <guido@python.org>
date Wed, 27 Nov 1996 19:39:58 +0000
parents 65d2d8e67da3
children d1447950fa04
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
1 /***********************************************************
2059
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
866c81068dbd Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents: 2053
diff changeset
3 The Netherlands.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
4
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
5 All Rights Reserved
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
6
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
7 Permission to use, copy, modify, and distribute this software and its
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
8 documentation for any purpose and without fee is hereby granted,
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
9 provided that the above copyright notice appear in all copies and that
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
10 both that copyright notice and this permission notice appear in
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
11 supporting documentation, and that the names of Stichting Mathematisch
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
12 Centrum or CWI or Corporation for National Research Initiatives or
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
13 CNRI not be used in advertising or publicity pertaining to
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
14 distribution of the software without specific, written prior
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
15 permission.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
16
4503
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
17 While CWI is the initial source for this software, a modified version
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
18 is made available by the Corporation for National Research Initiatives
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
19 (CNRI) at the Internet address ftp://ftp.python.org.
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
20
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
65d2d8e67da3 New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents: 2864
diff changeset
28 PERFORMANCE OF THIS SOFTWARE.
145
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
29
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
30 ******************************************************************/
853b736a6a9e Added copyright notice.
Guido van Rossum <guido@python.org>
parents: 86
diff changeset
31
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
32 /* Method object implementation */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
34 #include "allobjects.h"
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
35
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 #include "token.h"
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
37
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 typedef struct {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
39 OB_HEAD
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
40 struct methodlist *m_ml;
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
41 object *m_self;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 } methodobject;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
44 object *
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
45 newmethodobject(ml, self)
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
46 struct methodlist *ml;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
47 object *self;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 methodobject *op = NEWOBJ(methodobject, &Methodtype);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
50 if (op != NULL) {
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
51 op->m_ml = ml;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
52 XINCREF(self);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
53 op->m_self = self;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
55 return (object *)op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
56 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
57
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
58 method
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
59 getmethod(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
60 object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
62 if (!is_methodobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
63 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
64 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
65 }
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
66 return ((methodobject *)op) -> m_ml -> ml_meth;
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
67 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
68
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
69 object *
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
70 getself(op)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
71 object *op;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
72 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
73 if (!is_methodobject(op)) {
27
82c7bab78bb1 New errors.
Guido van Rossum <guido@python.org>
parents: 6
diff changeset
74 err_badcall();
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
75 return NULL;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
76 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
77 return ((methodobject *)op) -> m_self;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
78 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
79
488
90f35fcf8ea0 Add "varargs" attribute.
Guido van Rossum <guido@python.org>
parents: 472
diff changeset
80 int
2864
5f951912e995 changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents: 2256
diff changeset
81 getflags(op)
488
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 }
2864
5f951912e995 changes for keyword args to built-in functions and classes
Guido van Rossum <guido@python.org>
parents: 2256
diff changeset
88 return ((methodobject *)op) -> m_ml -> ml_flags;
488
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
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
91 /* Methods (the standard built-in methods, that is) */
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
92
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
93 static void
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
94 meth_dealloc(m)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
95 methodobject *m;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
96 {
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
97 XDECREF(m->m_self);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
98 free((char *)m);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
99 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
100
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
101 static object *
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
102 meth_getattr(m, name)
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
103 methodobject *m;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
104 char *name;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
105 {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
106 if (strcmp(name, "__name__") == 0) {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
107 return newstringobject(m->m_ml->ml_name);
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
108 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
109 if (strcmp(name, "__doc__") == 0) {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
110 char *doc = m->m_ml->ml_doc;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
111 if (doc != NULL)
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
112 return newstringobject(doc);
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
113 INCREF(None);
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
114 return None;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
115 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
116 if (strcmp(name, "__self__") == 0) {
2105
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
117 object *self;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
118 if (getrestricted()) {
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
119 err_setstr(RuntimeError,
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
120 "method.__self__ not accessible in restricted mode");
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
121 return NULL;
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
122 }
ea7b2bf0b521 add restrictions in restricted mode
Guido van Rossum <guido@python.org>
parents: 2083
diff changeset
123 self = m->m_self;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
124 if (self == NULL)
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
125 self = None;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
126 INCREF(self);
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
127 return self;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
128 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
129 if (strcmp(name, "__members__") == 0) {
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
130 return mkvalue("[sss]", "__doc__", "__name__", "__self__");
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
131 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
132 err_setstr(AttributeError, name);
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
133 return NULL;
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
134 }
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
135
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
136 static object *
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
137 meth_repr(m)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
138 methodobject *m;
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
139 {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 char buf[200];
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
141 if (m->m_self == NULL)
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
142 sprintf(buf, "<built-in function %.80s>", m->m_ml->ml_name);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
143 else
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
144 sprintf(buf,
1290
7971fecbf28e * Lots of small changes related to access.
Guido van Rossum <guido@python.org>
parents: 1257
diff changeset
145 "<built-in method %.80s of %.80s object at %lx>",
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
146 m->m_ml->ml_name, m->m_self->ob_type->tp_name,
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
147 (long)m->m_self);
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
148 return newstringobject(buf);
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
149 }
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
150
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
151 static int
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
152 meth_compare(a, b)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
153 methodobject *a, *b;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
154 {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
155 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
156 return cmpobject(a->m_self, b->m_self);
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
157 if (a->m_ml->ml_meth == b->m_ml->ml_meth)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
158 return 0;
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
159 if (strcmp(a->m_ml->ml_name, b->m_ml->ml_name) < 0)
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
160 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
161 else
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
162 return 1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
163 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
164
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
165 static long
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
166 meth_hash(a)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
167 methodobject *a;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
168 {
2053
de49d49044d7 Lots of minor changes. Note for mappingobject.c: the hash table pointer
Guido van Rossum <guido@python.org>
parents: 1848
diff changeset
169 long x;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
170 if (a->m_self == NULL)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
171 x = 0;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
172 else {
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
173 x = hashobject(a->m_self);
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
174 if (x == -1)
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
175 return -1;
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
176 }
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
177 return x ^ (long) a->m_ml->ml_meth;
1257
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
178 }
1cabc2b6c970 * Changed all copyright messages to include 1993.
Guido van Rossum <guido@python.org>
parents: 1051
diff changeset
179
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
180 typeobject Methodtype = {
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
181 OB_HEAD_INIT(&Typetype)
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
182 0,
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
183 "builtin_function_or_method",
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
184 sizeof(methodobject),
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
185 0,
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
186 (destructor)meth_dealloc, /*tp_dealloc*/
1051
f54e64c8b9b7 * Makefile: added IMGFILE; moved some stuff around.
Guido van Rossum <guido@python.org>
parents: 795
diff changeset
187 0, /*tp_print*/
2083
f2d267edbfd6 * Objects/methodobject.c: changed drastically, the object now
Guido van Rossum <guido@python.org>
parents: 2059
diff changeset
188 (getattrfunc)meth_getattr, /*tp_getattr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
189 0, /*tp_setattr*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
190 (cmpfunc)meth_compare, /*tp_compare*/
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
191 (reprfunc)meth_repr, /*tp_repr*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
192 0, /*tp_as_number*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
193 0, /*tp_as_sequence*/
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
194 0, /*tp_as_mapping*/
1848
b724599401ff Merge back to main trunk
Guido van Rossum <guido@python.org>
parents: 1391
diff changeset
195 (hashfunc)meth_hash, /*tp_hash*/
6
fc6fcd7df4f7 Initial revision
Guido van Rossum <guido@python.org>
parents:
diff changeset
196 };
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
197
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
198 /* List all methods in a chain -- helper for findmethodinchain */
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
199
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
200 static object *
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
201 listmethodchain(chain)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
202 struct methodchain *chain;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
203 {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
204 struct methodchain *c;
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
205 struct methodlist *ml;
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
206 int i, n;
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
207 object *v;
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
208
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
209 n = 0;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
210 for (c = chain; c != NULL; c = c->link) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
211 for (ml = c->methods; ml->ml_name != NULL; ml++)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
212 n++;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
213 }
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
214 v = newlistobject(n);
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
215 if (v == NULL)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
216 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
217 i = 0;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
218 for (c = chain; c != NULL; c = c->link) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
219 for (ml = c->methods; ml->ml_name != NULL; ml++) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
220 setlistitem(v, i, newstringobject(ml->ml_name));
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
221 i++;
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
222 }
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
223 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
224 if (err_occurred()) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
225 DECREF(v);
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
226 return NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
227 }
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
228 sortlist(v);
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
229 return v;
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
230 }
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
231
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
232 /* Find a method in a method chain */
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
233
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
234 object *
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
235 findmethodinchain(chain, self, name)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
236 struct methodchain *chain;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
237 object *self;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
238 char *name;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
239 {
395
659b8dbf695c Implemented __methods__ attribute
Guido van Rossum <guido@python.org>
parents: 284
diff changeset
240 if (strcmp(name, "__methods__") == 0)
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
241 return listmethodchain(chain);
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
242 while (chain != NULL) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
243 struct methodlist *ml = chain->methods;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
244 for (; ml->ml_name != NULL; ml++) {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
245 if (name[0] == ml->ml_name[0] &&
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
246 strcmp(name+1, ml->ml_name+1) == 0)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
247 return newmethodobject(ml, self);
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
248 }
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
249 chain = chain->link;
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
250 }
472
1d4b0339b83d Use AttributeError.
Guido van Rossum <guido@python.org>
parents: 395
diff changeset
251 err_setstr(AttributeError, name);
86
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
252 return NULL;
a6934380c6e7 "Compiling" version
Guido van Rossum <guido@python.org>
parents: 27
diff changeset
253 }
2256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
254
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
255 /* Find a method in a single method list */
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
256
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
257 object *
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
258 findmethod(methods, self, name)
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
259 struct methodlist *methods;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
260 object *self;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
261 char *name;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
262 {
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
263 struct methodchain chain;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
264 chain.methods = methods;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
265 chain.link = NULL;
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
266 return findmethodinchain(&chain, self, name);
d091b246a3a7 added findmethodinchain and methodchain data types
Guido van Rossum <guido@python.org>
parents: 2105
diff changeset
267 }